usr/src/cmd/zfs/zfs_main.c
author gw25295
Wed, 14 Feb 2007 19:54:59 -0800
changeset 3654 9c6042b08979
parent 3635 63e2169c1249
child 3883 a988d101ecca
permissions -rw-r--r--
6483718 need a stable interface to get all native properties 6514071 recent changes to zfs.h breaks Sun Cluster
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
/*
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3265
diff changeset
    23
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <assert.h>
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>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <locale.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <stddef.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <stdio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <stdlib.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <strings.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <fcntl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <zone.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <sys/mkdev.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
#include <sys/mntent.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
#include <sys/mnttab.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
#include <sys/mount.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
#include <sys/stat.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
#include <libzfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
#include "zfs_iter.h"
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    52
#include "zfs_util.h"
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    53
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    54
libzfs_handle_t *g_zfs;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
static FILE *mnttab_file;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
static int zfs_do_clone(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
static int zfs_do_create(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
static int zfs_do_destroy(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
static int zfs_do_get(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
static int zfs_do_inherit(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
static int zfs_do_list(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
static int zfs_do_mount(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
static int zfs_do_rename(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
static int zfs_do_rollback(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
static int zfs_do_set(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
static int zfs_do_snapshot(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
static int zfs_do_unmount(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
static int zfs_do_share(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
static int zfs_do_unshare(int argc, char **argv);
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
    72
static int zfs_do_send(int argc, char **argv);
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
    73
static int zfs_do_receive(int argc, char **argv);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    74
static int zfs_do_promote(int argc, char **argv);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
 * These libumem hooks provide a reasonable set of defaults for the allocator's
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
 * debugging facilities.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
const char *
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
    81
_umem_debug_init(void)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
	return ("default,verbose"); /* $UMEM_DEBUG setting */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
_umem_logging_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
	return ("fail,contents"); /* $UMEM_LOGGING setting */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    92
typedef enum {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    93
	HELP_CLONE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    94
	HELP_CREATE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    95
	HELP_DESTROY,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    96
	HELP_GET,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    97
	HELP_INHERIT,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    98
	HELP_LIST,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    99
	HELP_MOUNT,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   100
	HELP_PROMOTE,
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   101
	HELP_RECEIVE,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   102
	HELP_RENAME,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   103
	HELP_ROLLBACK,
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   104
	HELP_SEND,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   105
	HELP_SET,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   106
	HELP_SHARE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   107
	HELP_SNAPSHOT,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   108
	HELP_UNMOUNT,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   109
	HELP_UNSHARE
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   110
} zfs_help_t;
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   111
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
typedef struct zfs_command {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
	const char	*name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
	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
   115
	zfs_help_t	usage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
} zfs_command_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
 * Master command table.  Each ZFS command has a name, associated function, and
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
   120
 * usage message.  The usage messages need to be internationalized, so we have
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
   121
 * 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
   122
 *
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   123
 * 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
   124
 * 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
   125
 * the generic usage message.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
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
   128
	{ "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
   129
	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   131
	{ "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
   132
	{ "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
   133
	{ "clone",	zfs_do_clone,		HELP_CLONE		},
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   134
	{ "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
   135
	{ "rename",	zfs_do_rename,		HELP_RENAME		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   137
	{ "list",	zfs_do_list,		HELP_LIST		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   139
	{ "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
   140
	{ "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
   141
	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   143
	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   145
	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
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
	{ "share",	zfs_do_share,		HELP_SHARE		},
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
	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
	{ NULL },
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   151
	{ "send",	zfs_do_send,		HELP_SEND		},
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   152
	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
#define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
zfs_command_t *current_command;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   159
static const char *
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   160
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
   161
{
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   162
	switch (idx) {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   163
	case HELP_CLONE:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   164
		return (gettext("\tclone <snapshot> <filesystem|volume>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   165
	case HELP_CREATE:
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   166
		return (gettext("\tcreate [[-o property=value] ... ] "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   167
		    "<filesystem>\n"
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   168
		    "\tcreate [-s] [-b blocksize] [[-o property=value] ...]\n"
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   169
		    "\t    -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
   170
	case HELP_DESTROY:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   171
		return (gettext("\tdestroy [-rRf] "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   172
		    "<filesystem|volume|snapshot>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   173
	case HELP_GET:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   174
		return (gettext("\tget [-rHp] [-o field[,field]...] "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   175
		    "[-s source[,source]...]\n"
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   176
		    "\t    <all | property[,property]...> "
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   177
		    "[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
   178
	case HELP_INHERIT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   179
		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
   180
		    "<filesystem|volume> ...\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   181
	case HELP_LIST:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   182
		return (gettext("\tlist [-rH] [-o property[,property]...] "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   183
		    "[-t type[,type]...]\n"
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
   184
		    "\t    [-s property [-s property]...]"
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
   185
		    " [-S property [-S property]...]\n"
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   186
		    "\t    [filesystem|volume|snapshot] ...\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   187
	case HELP_MOUNT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   188
		return (gettext("\tmount\n"
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   189
		    "\tmount [-o opts] [-O] -a\n"
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   190
		    "\tmount [-o opts] [-O] <filesystem>\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   191
	case HELP_PROMOTE:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   192
		return (gettext("\tpromote <clone filesystem>\n"));
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   193
	case HELP_RECEIVE:
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
   194
		return (gettext("\treceive [-vnF] <filesystem|volume|"
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
   195
		"snapshot>\n"
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
   196
		"\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
   197
	case HELP_RENAME:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   198
		return (gettext("\trename <filesystem|volume|snapshot> "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   199
		    "<filesystem|volume|snapshot>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   200
	case HELP_ROLLBACK:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   201
		return (gettext("\trollback [-rRf] <snapshot>\n"));
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   202
	case HELP_SEND:
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   203
		return (gettext("\tsend [-i <snapshot>] <snapshot>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   204
	case HELP_SET:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   205
		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
   206
		    "<filesystem|volume> ...\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   207
	case HELP_SHARE:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   208
		return (gettext("\tshare -a\n"
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   209
		    "\tshare <filesystem>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   210
	case HELP_SNAPSHOT:
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   211
		return (gettext("\tsnapshot [-r] "
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   212
		    "<filesystem@name|volume@name>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   213
	case HELP_UNMOUNT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   214
		return (gettext("\tunmount [-f] -a\n"
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   215
		    "\tunmount [-f] <filesystem|mountpoint>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   216
	case HELP_UNSHARE:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   217
		return (gettext("\tunshare [-f] -a\n"
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   218
		    "\tunshare [-f] <filesystem|mountpoint>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   219
	}
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   220
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   221
	abort();
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   222
	/* NOTREACHED */
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   223
}
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   224
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
 * Utility function to guarantee malloc() success.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
safe_malloc(size_t size)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
	void *data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
	if ((data = calloc(1, size)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
		(void) fprintf(stderr, "internal error: out of memory\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
		exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
	return (data);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
/*
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   242
 * Callback routinue that will print out information for each of the
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   243
 * the properties.
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   244
 */
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   245
static zfs_prop_t
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   246
usage_prop_cb(zfs_prop_t prop, void *cb)
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   247
{
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   248
	FILE *fp = cb;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   249
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   250
	(void) fprintf(fp, "\t%-13s  ", zfs_prop_to_name(prop));
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   251
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   252
	if (zfs_prop_readonly(prop))
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   253
		(void) fprintf(fp, "  NO    ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   254
	else
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   255
		(void) fprintf(fp, " YES    ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   256
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   257
	if (zfs_prop_inheritable(prop))
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   258
		(void) fprintf(fp, "  YES   ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   259
	else
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   260
		(void) fprintf(fp, "   NO   ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   261
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   262
	if (zfs_prop_values(prop) == NULL)
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   263
		(void) fprintf(fp, "-\n");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   264
	else
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   265
		(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
   266
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   267
	return (ZFS_PROP_CONT);
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   268
}
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   269
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   270
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
 * Display usage message.  If we're inside a command, display only the usage for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
 * that command.  Otherwise, iterate over the entire command table and display
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
 * a complete usage message.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
static void
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   276
usage(boolean_t requested)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
	int i;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   279
	boolean_t show_properties = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
	FILE *fp = requested ? stdout : stderr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
	if (current_command == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
		(void) fprintf(fp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
		    gettext("where 'command' is one of the following:\n\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
		for (i = 0; i < NCOMMAND; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
			if (command_table[i].name == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
				(void) fprintf(fp, "\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
				(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
   293
				    get_usage(command_table[i].usage));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
		    "pool/[dataset/]*dataset[@name]\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
		(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
   300
		(void) fprintf(fp, "%s", get_usage(current_command->usage));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   303
	if (current_command != NULL &&
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   304
	    (strcmp(current_command->name, "set") == 0 ||
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
	    strcmp(current_command->name, "get") == 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
	    strcmp(current_command->name, "inherit") == 0 ||
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   307
	    strcmp(current_command->name, "list") == 0))
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   308
		show_properties = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
	if (show_properties) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
		(void) fprintf(fp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
		    gettext("\nThe following properties are supported:\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
		(void) fprintf(fp, "\n\t%-13s  %s  %s   %s\n\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   318
		/* Iterate over all properties */
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   319
		(void) zfs_prop_iter(usage_prop_cb, fp, B_FALSE);
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   320
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
		    "with standard units such as K, M, G, etc.\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   323
		(void) fprintf(fp, gettext("\n\nUser-defined properties can "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   324
		    "be specified by using a name containing a colon (:).\n"));
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   325
	} else {
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   326
		/*
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   327
		 * TRANSLATION NOTE:
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   328
		 * "zfs set|get" must not be localised this is the
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   329
		 * command name and arguments.
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   330
		 */
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   331
		(void) fprintf(fp,
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   332
		    gettext("\nFor the property list, run: zfs set|get\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   335
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   336
	 * See comments at end of main().
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   337
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   338
	if (getenv("ZFS_ABORT") != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   339
		(void) printf("dumping core by request\n");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   340
		abort();
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   341
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   342
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
	exit(requested ? 0 : 2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
 * zfs clone <fs, snap, vol> fs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
 * Given an existing dataset, create a writable copy whose initial contents
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
 * are the same as the source.  The newly created dataset maintains a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
 * dependency on the original; the original cannot be destroyed so long as
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
 * the clone exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
zfs_do_clone(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
	if (argc > 1 && argv[1][0] == '-') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
		    argv[1][1]);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   364
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
		(void) fprintf(stderr, gettext("missing source dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   371
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
	if (argc < 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
		(void) fprintf(stderr, gettext("missing target dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   376
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
	if (argc > 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   380
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
	/* open the source dataset */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   384
	if ((zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_SNAPSHOT)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
	/* pass to libzfs */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   388
	ret = zfs_clone(zhp, argv[2], NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
	/* create the mountpoint if necessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
	if (ret == 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   392
		zfs_handle_t *clone = zfs_open(g_zfs, argv[2], ZFS_TYPE_ANY);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
		if (clone != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
			if ((ret = zfs_mount(clone, NULL, 0)) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
				ret = zfs_share(clone);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
			zfs_close(clone);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
		}
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   398
		zpool_log_history(g_zfs, argc, argv, argv[2], B_FALSE, B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
	return (ret == 0 ? 0 : 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   407
 * zfs create [-o prop=value] ... fs
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   408
 * zfs create [-s] [-b blocksize] [-o prop=value] ... -V vol size
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
 * Create a new dataset.  This command can be used to create filesystems
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
 * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
 * For volumes, the user must specify a size to be used.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
 * The '-s' flag applies only to volumes, and indicates that we should not try
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
 * to set the reservation for this volume.  By default we set a reservation
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
 * equal to the size for any volume.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
zfs_do_create(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   422
	zfs_handle_t *zhp = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   423
	uint64_t volsize;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
	int c;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   425
	boolean_t noreserve = B_FALSE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   426
	int ret = 1;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   427
	nvlist_t *props = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   428
	uint64_t intval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   429
	char *propname;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   430
	char *propval = NULL;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   431
	char *strval;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   432
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   433
	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   434
		(void) fprintf(stderr, gettext("internal error: "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   435
		    "out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   436
		return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   437
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	/* check options */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   440
	while ((c = getopt(argc, argv, ":V:b:so:")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
		case 'V':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
			type = ZFS_TYPE_VOLUME;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   444
			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   445
				(void) fprintf(stderr, gettext("bad volume "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   446
				    "size '%s': %s\n"), optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   447
				    libzfs_error_description(g_zfs));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   448
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   449
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   450
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   451
			if (nvlist_add_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   452
			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   453
			    intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   454
				(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   455
				    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   456
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   457
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   458
			volsize = intval;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
		case 'b':
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   461
			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   462
				(void) fprintf(stderr, gettext("bad volume "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   463
				    "block size '%s': %s\n"), optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   464
				    libzfs_error_description(g_zfs));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   465
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   466
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   467
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   468
			if (nvlist_add_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   469
			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   470
			    intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   471
				(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   472
				    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   473
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   474
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   475
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   476
		case 'o':
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   477
			propname = optarg;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   478
			if ((propval = strchr(propname, '=')) == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   479
				(void) fprintf(stderr, gettext("missing "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   480
				    "'=' for -o option\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   481
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   482
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   483
			*propval = '\0';
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   484
			propval++;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   485
			if (nvlist_lookup_string(props, propname,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   486
			    &strval) == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   487
				(void) fprintf(stderr, gettext("property '%s' "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   488
				    "specified multiple times\n"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   489
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   490
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   491
			if (nvlist_add_string(props, propname, propval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   492
				(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   493
				    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   494
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   495
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
		case 's':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   498
			noreserve = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
			(void) fprintf(stderr, gettext("missing size "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
			    "argument\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   503
			goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
			    optopt);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   508
			goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
	if (noreserve && type != ZFS_TYPE_VOLUME) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
		(void) fprintf(stderr, gettext("'-s' can only be used when "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
		    "creating a volume\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   515
		goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
	if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
		(void) fprintf(stderr, gettext("missing %s argument\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
		    zfs_type_to_name(type));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   525
		goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
		(void) fprintf(stderr, gettext("too many arguments\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   529
		goto badusage;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   530
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   531
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   532
	if (type == ZFS_TYPE_VOLUME && !noreserve &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   533
	    nvlist_lookup_string(props, zfs_prop_to_name(ZFS_PROP_RESERVATION),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   534
	    &strval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   535
		if (nvlist_add_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   536
		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   537
		    volsize) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   538
			(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   539
			    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   540
			nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   541
			return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   542
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
	/* pass to libzfs */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   546
	if (zfs_create(g_zfs, argv[0], type, props) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   547
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   549
	if (propval != NULL)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   550
		*(propval - 1) = '=';
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   551
	zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   552
	    B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   553
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   554
	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   555
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
	 * Mount and/or share the new filesystem as appropriate.  We provide a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
	 * verbose error message to let the user know that their filesystem was
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
	 * in fact created, even if we failed to mount or share it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
	if (zfs_mount(zhp, NULL, 0) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
		(void) fprintf(stderr, gettext("filesystem successfully "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
		    "created, but not mounted\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
		ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
	} else if (zfs_share(zhp) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
		(void) fprintf(stderr, gettext("filesystem successfully "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
		    "created, but not shared\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
		ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
		ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   574
error:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   575
	if (zhp)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   576
		zfs_close(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   577
	nvlist_free(props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
	return (ret);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   579
badusage:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   580
	nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   581
	usage(B_FALSE);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   582
	return (2);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
 * zfs destroy [-rf] <fs, snap, vol>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
 * 	-r	Recursively destroy all children
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
 * 	-R	Recursively destroy all dependents, including clones
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
 * 	-f	Force unmounting of any dependents
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
 * Destroys the given dataset.  By default, it will unmount any filesystems,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
 * and refuse to destroy a dataset that has any dependents.  A dependent can
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
 * either be a child, or a clone of a child.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
typedef struct destroy_cbdata {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   597
	boolean_t	cb_first;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	int		cb_force;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
	int		cb_recurse;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
	int		cb_error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	int		cb_needforce;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
	int		cb_doclones;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   603
	boolean_t	cb_closezhp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
	zfs_handle_t	*cb_target;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   605
	char		*cb_snapname;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
} destroy_cbdata_t;
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
 * Check for any dependents based on the '-r' or '-R' flags.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
destroy_check_dependent(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
	destroy_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
	const char *tname = zfs_get_name(cbp->cb_target);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
	const char *name = zfs_get_name(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
	if (strncmp(tname, name, strlen(tname)) == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
		 * This is a direct descendant, not a clone somewhere else in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
		 * the hierarchy.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
		if (cbp->cb_recurse)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
		if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
			(void) fprintf(stderr, gettext("cannot destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
			    "%s has children\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
			    zfs_get_name(cbp->cb_target),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
			(void) fprintf(stderr, gettext("use '-r' to destroy "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
			    "the following datasets:\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   634
			cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
			cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
		 * This is a clone.  We only want to report this if the '-r'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
		 * wasn't specified, or the target is a snapshot.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
		if (!cbp->cb_recurse &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   648
		if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
			(void) fprintf(stderr, gettext("cannot destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
			    "%s has dependent clones\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
			    zfs_get_name(cbp->cb_target),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
			(void) fprintf(stderr, gettext("use '-R' to destroy "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
			    "the following datasets:\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   655
			cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   656
			cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
destroy_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
	destroy_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
	 * Ignore pools (which we've already flagged as an error before getting
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
	 * here.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
	if (strchr(zfs_get_name(zhp), '/') == NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
	 * Bail out on the first error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
	if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
	    zfs_destroy(zhp) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   695
static int
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   696
destroy_snap_clones(zfs_handle_t *zhp, void *arg)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   697
{
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   698
	destroy_cbdata_t *cbp = arg;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   699
	char thissnap[MAXPATHLEN];
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   700
	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
   701
	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
   702
	int rv;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   703
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   704
	(void) snprintf(thissnap, sizeof (thissnap),
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   705
	    "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   706
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   707
	libzfs_print_on_error(g_zfs, B_FALSE);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   708
	szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   709
	libzfs_print_on_error(g_zfs, B_TRUE);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   710
	if (szhp) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   711
		/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   712
		 * Destroy any clones of this snapshot
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   713
		 */
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   714
		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
   715
		    cbp) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   716
			zfs_close(szhp);
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   717
			if (closezhp)
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   718
				zfs_close(zhp);
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   719
			return (-1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   720
		}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   721
		zfs_close(szhp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   722
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   723
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   724
	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
   725
	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
   726
	if (closezhp)
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   727
		zfs_close(zhp);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   728
	return (rv);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   729
}
789
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
zfs_do_destroy(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
	destroy_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
	zfs_handle_t *zhp;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   737
	char *cp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
	while ((c = getopt(argc, argv, "frR")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
			cb.cb_force = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
		case 'r':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
		case 'R':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
			cb.cb_doclones = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   756
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
	if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
		(void) fprintf(stderr, gettext("missing path argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   766
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   770
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   773
	/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   774
	 * If we are doing recursive destroy of a snapshot, then the
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   775
	 * named snapshot may not exist.  Go straight to libzfs.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   776
	 */
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   777
	if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   778
		int ret;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   779
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   780
		*cp = '\0';
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   781
		if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   782
			return (1);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   783
		*cp = '@';
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   784
		cp++;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   785
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   786
		if (cb.cb_doclones) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   787
			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
   788
			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
   789
				zfs_close(zhp);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   790
				return (1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   791
			}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   792
		}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   793
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   794
		ret = zfs_destroy_snaps(zhp, cp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   795
		zfs_close(zhp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   796
		if (ret) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   797
			(void) fprintf(stderr,
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   798
			    gettext("no snapshots destroyed\n"));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   799
		}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   800
		return (ret != 0);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   801
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   802
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   803
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
	/* Open the given dataset */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   805
	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
	cb.cb_target = zhp;
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
	 * Perform an explicit check for pools before going any further.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
	if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   815
		(void) fprintf(stderr, gettext("cannot destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
		    "operation does not apply to pools\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
		    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
		(void) fprintf(stderr, gettext("use 'zfs destroy -r "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
		    "%s' to destroy all datasets in the pool\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
		    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
		(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
		    "to destroy the pool itself\n"), zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   823
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   827
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
	 * Check for any dependents and/or clones.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   830
	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
   831
	if (!cb.cb_doclones &&
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   832
	    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
   833
	    &cb) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   834
		zfs_close(zhp);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   835
		return (1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   836
	}
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   837
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   838
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   839
	if (cb.cb_error ||
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   840
	    zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
	/*
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   846
	 * 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
   847
	 * whether it succeeds or not.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
	 */
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   849
	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
   850
		return (1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   851
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   852
	zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   853
	    B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   854
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   855
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
/*
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
   859
 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
   860
 * 	< all | property[,property]... > < fs | snap | vol > ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
 *	-r	recurse over any child datasets
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
 *	-H	scripted mode.  Headers are stripped, and fields are separated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
 *		by tabs instead of spaces.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
 *	-o	Set of fields to display.  One of "name,property,value,source".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
 *		Default is all four.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
 *	-s	Set of sources to allow.  One of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
 *		"local,default,inherited,temporary,none".  Default is all
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
 *		five.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
 *	-p	Display values in parsable (literal) format.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
 *  Prints properties for the given datasets.  The user can control which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
 *  columns to display as well as which property types to allow.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
typedef struct get_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   876
	int cb_sources;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
	int cb_columns[4];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   878
	int cb_colwidths[5];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   879
	boolean_t cb_scripted;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   880
	boolean_t cb_literal;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   881
	boolean_t cb_first;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   882
	zfs_proplist_t *cb_proplist;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
} get_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
#define	GET_COL_NAME		1
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
#define	GET_COL_PROPERTY	2
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
#define	GET_COL_VALUE		3
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
#define	GET_COL_SOURCE		4
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   891
 * Print the column headers for 'zfs get'.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   892
 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   893
static void
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   894
print_get_headers(get_cbdata_t *cbp)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   895
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   896
	zfs_proplist_t *pl = cbp->cb_proplist;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   897
	int i;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   898
	char *title;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   899
	size_t len;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   900
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   901
	cbp->cb_first = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   902
	if (cbp->cb_scripted)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   903
		return;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   904
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   905
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   906
	 * Start with the length of the column headers.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   907
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   908
	cbp->cb_colwidths[GET_COL_NAME] = strlen(gettext("NAME"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   909
	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(gettext("PROPERTY"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   910
	cbp->cb_colwidths[GET_COL_VALUE] = strlen(gettext("VALUE"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   911
	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(gettext("SOURCE"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   912
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   913
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   914
	 * Go through and calculate the widths for each column.  For the
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   915
	 * 'source' column, we kludge it up by taking the worst-case scenario of
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   916
	 * inheriting from the longest name.  This is acceptable because in the
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   917
	 * majority of cases 'SOURCE' is the last column displayed, and we don't
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   918
	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   919
	 * if the name of the property is much longer the any values we find.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   920
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   921
	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   922
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   923
		 * 'PROPERTY' column
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   924
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   925
		if (pl->pl_prop != ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   926
			len = strlen(zfs_prop_to_name(pl->pl_prop));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   927
			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   928
				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   929
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   930
			len = strlen(pl->pl_user_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   931
			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   932
				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   933
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   934
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   935
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   936
		 * 'VALUE' column
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   937
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   938
		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   939
		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   940
			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   941
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   942
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   943
		 * 'NAME' and 'SOURCE' columns
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   944
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   945
		if (pl->pl_prop == ZFS_PROP_NAME &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   946
		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   947
			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   948
			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   949
			    strlen(gettext("inherited from"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   950
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   951
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   952
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   953
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   954
	 * Now go through and print the headers.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   955
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   956
	for (i = 0; i < 4; i++) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   957
		switch (cbp->cb_columns[i]) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   958
		case GET_COL_NAME:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   959
			title = gettext("NAME");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   960
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   961
		case GET_COL_PROPERTY:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   962
			title = gettext("PROPERTY");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   963
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   964
		case GET_COL_VALUE:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   965
			title = gettext("VALUE");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   966
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   967
		case GET_COL_SOURCE:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   968
			title = gettext("SOURCE");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   969
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   970
		default:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   971
			title = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   972
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   973
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   974
		if (title != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   975
			if (i == 3 || cbp->cb_columns[i + 1] == 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   976
				(void) printf("%s", title);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   977
			else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   978
				(void) printf("%-*s  ",
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   979
				    cbp->cb_colwidths[cbp->cb_columns[i]],
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   980
				    title);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   981
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   982
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   983
	(void) printf("\n");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   984
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   985
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   986
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
 * Display a single line of output, according to the settings in the callback
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   988
 * structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   989
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   990
static void
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   991
print_one_property(zfs_handle_t *zhp, get_cbdata_t *cbp, const char *propname,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   992
    const char *value, zfs_source_t sourcetype, const char *source)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
	const char *str;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   996
	char buf[128];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   998
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   999
	 * Ignore those source types that the user has chosen to ignore.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1000
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1001
	if ((sourcetype & cbp->cb_sources) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1002
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1003
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1004
	if (cbp->cb_first)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1005
		print_get_headers(cbp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1006
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
	for (i = 0; i < 4; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
		switch (cbp->cb_columns[i]) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
		case GET_COL_NAME:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
			str = zfs_get_name(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
		case GET_COL_PROPERTY:
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1014
			str = propname;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1016
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1017
		case GET_COL_VALUE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1018
			str = value;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1021
		case GET_COL_SOURCE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1022
			switch (sourcetype) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1023
			case ZFS_SRC_NONE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1024
				str = "-";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1027
			case ZFS_SRC_DEFAULT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
				str = "default";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1030
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1031
			case ZFS_SRC_LOCAL:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1032
				str = "local";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1033
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1034
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1035
			case ZFS_SRC_TEMPORARY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1036
				str = "temporary";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1038
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
			case ZFS_SRC_INHERITED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1040
				(void) snprintf(buf, sizeof (buf),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
				    "inherited from %s", source);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
				str = buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1043
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1044
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1045
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1046
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1047
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1048
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1050
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1051
		if (cbp->cb_columns[i + 1] == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1052
			(void) printf("%s", str);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1053
		else if (cbp->cb_scripted)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1054
			(void) printf("%s\t", str);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1055
		else
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1056
			(void) printf("%-*s  ",
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1057
			    cbp->cb_colwidths[cbp->cb_columns[i]],
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1058
			    str);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1059
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1060
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1061
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1062
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
 * Invoked to display the properties for a single dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
get_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
	char buf[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
	zfs_source_t sourcetype;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
	char source[ZFS_MAXNAMELEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
	get_cbdata_t *cbp = data;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1075
	nvlist_t *userprop = zfs_get_user_props(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1076
	zfs_proplist_t *pl = cbp->cb_proplist;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1077
	nvlist_t *propval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1078
	char *strval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1079
	char *sourceval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1080
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1081
	for (; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1082
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1083
		 * Skip the special fake placeholder.  This will also skip over
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1084
		 * the name property when 'all' is specified.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1085
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1086
		if (pl->pl_prop == ZFS_PROP_NAME &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1087
		    pl == cbp->cb_proplist)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1088
			continue;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1089
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1090
		if (pl->pl_prop != ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1091
			if (zfs_prop_get(zhp, pl->pl_prop, buf,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1092
			    sizeof (buf), &sourcetype, source,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1093
			    sizeof (source),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1094
			    cbp->cb_literal) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1095
				if (pl->pl_all)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1096
					continue;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1097
				sourcetype = ZFS_SRC_NONE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1098
				(void) strlcpy(buf, "-", sizeof (buf));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1099
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1100
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1101
			print_one_property(zhp, cbp,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1102
			    zfs_prop_to_name(pl->pl_prop),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1103
			    buf, sourcetype, source);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1104
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1105
			if (nvlist_lookup_nvlist(userprop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1106
			    pl->pl_user_prop, &propval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1107
				if (pl->pl_all)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1108
					continue;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1109
				sourcetype = ZFS_SRC_NONE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1110
				strval = "-";
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1111
			} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1112
				verify(nvlist_lookup_string(propval,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1113
				    ZFS_PROP_VALUE, &strval) == 0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1114
				verify(nvlist_lookup_string(propval,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1115
				    ZFS_PROP_SOURCE, &sourceval) == 0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1116
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1117
				if (strcmp(sourceval,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1118
				    zfs_get_name(zhp)) == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1119
					sourcetype = ZFS_SRC_LOCAL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1120
				} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1121
					sourcetype = ZFS_SRC_INHERITED;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1122
					(void) strlcpy(source,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1123
					    sourceval, sizeof (source));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1124
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1125
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1126
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1127
			print_one_property(zhp, cbp,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1128
			    pl->pl_user_prop, strval, sourcetype,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1129
			    source);
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1130
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1131
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1132
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1133
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1134
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
zfs_do_get(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
	get_cbdata_t cb = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1140
	boolean_t recurse = B_FALSE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1141
	int i, c;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1142
	char *value, *fields;
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1143
	int ret;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1144
	zfs_proplist_t fake_name = { 0 };
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1146
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
	 * Set up default columns and sources.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
	cb.cb_sources = ZFS_SRC_ALL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
	cb.cb_columns[0] = GET_COL_NAME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
	cb.cb_columns[1] = GET_COL_PROPERTY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
	cb.cb_columns[2] = GET_COL_VALUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
	cb.cb_columns[3] = GET_COL_SOURCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
	while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
		case 'p':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1159
			cb.cb_literal = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
		case 'r':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1162
			recurse = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
		case 'H':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1165
			cb.cb_scripted = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1167
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1169
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1170
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1171
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1172
		case 'o':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1173
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
			 * Process the set of columns to display.  We zero out
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
			 * the structure to give us a blank slate.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1177
			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1178
			i = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
			while (*optarg != '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
				static char *col_subopts[] =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
				    { "name", "property", "value", "source",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
				    NULL };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
				if (i == 4) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
					(void) fprintf(stderr, gettext("too "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1186
					    "many fields given to -o "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1187
					    "option\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1188
					usage(B_FALSE);
789
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
				switch (getsubopt(&optarg, col_subopts,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
				    &value)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1193
				case 0:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1194
					cb.cb_columns[i++] = GET_COL_NAME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
				case 1:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1197
					cb.cb_columns[i++] = GET_COL_PROPERTY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1199
				case 2:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
					cb.cb_columns[i++] = GET_COL_VALUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
				case 3:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
					cb.cb_columns[i++] = GET_COL_SOURCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1204
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
				default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
					(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1207
					    gettext("invalid column name "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
					    "'%s'\n"), value);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1209
					    usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1210
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1211
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1212
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1213
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1214
		case 's':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1215
			cb.cb_sources = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1216
			while (*optarg != '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1217
				static char *source_subopts[] = {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1218
					"local", "default", "inherited",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1219
					"temporary", "none", NULL };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1220
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1221
				switch (getsubopt(&optarg, source_subopts,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1222
				    &value)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1223
				case 0:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1224
					cb.cb_sources |= ZFS_SRC_LOCAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1225
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1226
				case 1:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1227
					cb.cb_sources |= ZFS_SRC_DEFAULT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1228
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1229
				case 2:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1230
					cb.cb_sources |= ZFS_SRC_INHERITED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1231
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1232
				case 3:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1233
					cb.cb_sources |= ZFS_SRC_TEMPORARY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1234
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1235
				case 4:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1236
					cb.cb_sources |= ZFS_SRC_NONE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1238
				default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1239
					(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1240
					    gettext("invalid source "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1241
					    "'%s'\n"), value);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1242
					    usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1243
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1244
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1245
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1246
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1247
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1248
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1249
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1250
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1251
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1252
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1253
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1254
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1255
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1256
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1257
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1258
		(void) fprintf(stderr, gettext("missing property "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1259
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1260
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1261
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1262
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1263
	fields = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1264
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1265
	if (zfs_get_proplist(g_zfs, fields, &cb.cb_proplist) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1266
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1267
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1268
	argc--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1269
	argv++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1270
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1271
	/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1272
	 * 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
  1273
	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1274
	 * need to know the maximum name length.  However, the user likely did
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1275
	 * 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
  1276
	 * make sure we always include at least this property for
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1277
	 * print_get_headers() to work properly.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1278
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1279
	if (cb.cb_proplist != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1280
		fake_name.pl_prop = ZFS_PROP_NAME;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1281
		fake_name.pl_width = strlen(gettext("NAME"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1282
		fake_name.pl_next = cb.cb_proplist;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1283
		cb.cb_proplist = &fake_name;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1284
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1285
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1286
	cb.cb_first = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1287
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1288
	/* run for each object */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1289
	ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_ANY, NULL,
3635
63e2169c1249 PSARC 2007/050 zfs list -d
ck153898
parents: 3504
diff changeset
  1290
	    &cb.cb_proplist, get_callback, &cb, B_FALSE);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1291
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1292
	if (cb.cb_proplist == &fake_name)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1293
		zfs_free_proplist(fake_name.pl_next);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1294
	else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1295
		zfs_free_proplist(cb.cb_proplist);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1296
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1297
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1298
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1299
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1300
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1301
 * inherit [-r] <property> <fs|vol> ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1302
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1303
 * 	-r	Recurse over all children
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1304
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1305
 * For each dataset specified on the command line, inherit the given property
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1306
 * from its parent.  Inheriting a property at the pool level will cause it to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1307
 * use the default value.  The '-r' flag will recurse over all children, and is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1308
 * useful for setting a property on a hierarchy-wide basis, regardless of any
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1309
 * local modifications for each dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1310
 */
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1311
typedef struct inherit_cbdata {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1312
	char		*cb_propname;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1313
	boolean_t	cb_any_successful;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1314
} inherit_cbdata_t;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1315
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1316
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1317
inherit_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1318
{
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1319
	inherit_cbdata_t *cbp = data;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1320
	int ret;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1321
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1322
	ret = zfs_prop_inherit(zhp, cbp->cb_propname);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1323
	if (ret == 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1324
		cbp->cb_any_successful = B_TRUE;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1325
	return (ret != 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1328
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1329
zfs_do_inherit(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1331
	boolean_t recurse = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1333
	zfs_prop_t prop;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1334
	inherit_cbdata_t cb;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1335
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1336
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1337
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1338
	while ((c = getopt(argc, argv, "r")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1339
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1340
		case 'r':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1341
			recurse = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1342
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1343
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1344
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1345
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1346
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1347
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1348
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1349
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1350
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1351
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1352
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1353
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1354
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1355
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1356
		(void) fprintf(stderr, gettext("missing property argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1357
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1358
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1359
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1360
		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1361
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1362
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1363
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1364
	cb.cb_propname = argv[0];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1365
	argc--;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1366
	argv++;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1367
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1368
	if ((prop = zfs_name_to_prop(cb.cb_propname)) != ZFS_PROP_INVAL) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1369
		if (zfs_prop_readonly(prop)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1370
			(void) fprintf(stderr, gettext(
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1371
			    "%s property is read-only\n"),
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1372
			    cb.cb_propname);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1373
			return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1374
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1375
		if (!zfs_prop_inheritable(prop)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1376
			(void) fprintf(stderr, gettext("'%s' property cannot "
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1377
			    "be inherited\n"), cb.cb_propname);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1378
			if (prop == ZFS_PROP_QUOTA ||
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1379
			    prop == ZFS_PROP_RESERVATION)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1380
				(void) fprintf(stderr, gettext("use 'zfs set "
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1381
				    "%s=none' to clear\n"), cb.cb_propname);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1382
			return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1383
		}
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1384
	} else if (!zfs_prop_user(cb.cb_propname)) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1385
		(void) fprintf(stderr, gettext(
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1386
		    "invalid property '%s'\n"),
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1387
		    cb.cb_propname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1388
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1389
	}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1390
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1391
	cb.cb_any_successful = B_FALSE;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1392
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1393
	ret = zfs_for_each(argc, argv, recurse,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1394
	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL,
3635
63e2169c1249 PSARC 2007/050 zfs list -d
ck153898
parents: 3504
diff changeset
  1395
	    inherit_callback, &cb, B_FALSE);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1396
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1397
	if (cb.cb_any_successful) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1398
		zpool_log_history(g_zfs, argc + optind + 1, argv - optind - 1,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1399
		    argv[0], B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1400
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1401
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1402
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1403
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1404
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1405
/*
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1406
 * list [-rH] [-o property[,property]...] [-t type[,type]...]
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1407
 *      [-s property [-s property]...] [-S property [-S property]...]
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1408
 *      <dataset> ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1409
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1410
 * 	-r	Recurse over all children
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1411
 * 	-H	Scripted mode; elide headers and separate colums by tabs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1412
 * 	-o	Control which fields to display.
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1413
 * 	-t	Control which object types to display.
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1414
 *	-s	Specify sort columns, descending order.
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1415
 *	-S	Specify sort columns, ascending order.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1416
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1417
 * When given no arguments, lists all filesystems in the system.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1418
 * Otherwise, list the specified datasets, optionally recursing down them if
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1419
 * '-r' is specified.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1420
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1421
typedef struct list_cbdata {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1422
	boolean_t	cb_first;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1423
	boolean_t	cb_scripted;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1424
	zfs_proplist_t	*cb_proplist;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1425
} list_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1426
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1427
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1428
 * Given a list of columns to display, output appropriate headers for each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1429
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1430
static void
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1431
print_header(zfs_proplist_t *pl)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1432
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1433
	char headerbuf[ZFS_MAXPROPLEN];
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1434
	const char *header;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1435
	int i;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1436
	boolean_t first = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1437
	boolean_t right_justify;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1438
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1439
	for (; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1440
		if (!first) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1441
			(void) printf("  ");
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1442
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1443
			first = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1444
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1445
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1446
		right_justify = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1447
		if (pl->pl_prop != ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1448
			header = zfs_prop_column_name(pl->pl_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1449
			right_justify = zfs_prop_align_right(pl->pl_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1450
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1451
			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1452
				headerbuf[i] = toupper(pl->pl_user_prop[i]);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1453
			headerbuf[i] = '\0';
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1454
			header = headerbuf;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1455
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1456
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1457
		if (pl->pl_next == NULL && !right_justify)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1458
			(void) printf("%s", header);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1459
		else if (right_justify)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1460
			(void) printf("%*s", pl->pl_width, header);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1461
		else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1462
			(void) printf("%-*s", pl->pl_width, header);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1463
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1464
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1465
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1466
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1467
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1468
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1469
 * Given a dataset and a list of fields, print out all the properties according
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1470
 * to the described layout.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1471
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1472
static void
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1473
print_dataset(zfs_handle_t *zhp, zfs_proplist_t *pl, int scripted)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1474
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1475
	boolean_t first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1476
	char property[ZFS_MAXPROPLEN];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1477
	nvlist_t *userprops = zfs_get_user_props(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1478
	nvlist_t *propval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1479
	char *propstr;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1480
	boolean_t right_justify;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1481
	int width;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1482
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1483
	for (; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1484
		if (!first) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1485
			if (scripted)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1486
				(void) printf("\t");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1487
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1488
				(void) printf("  ");
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1489
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1490
			first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1491
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1492
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1493
		right_justify = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1494
		if (pl->pl_prop != ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1495
			if (zfs_prop_get(zhp, pl->pl_prop, property,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1496
			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1497
				propstr = "-";
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1498
			else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1499
				propstr = property;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1500
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1501
			right_justify = zfs_prop_align_right(pl->pl_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1502
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1503
			if (nvlist_lookup_nvlist(userprops,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1504
			    pl->pl_user_prop, &propval) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1505
				propstr = "-";
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1506
			else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1507
				verify(nvlist_lookup_string(propval,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1508
				    ZFS_PROP_VALUE, &propstr) == 0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1509
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1510
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1511
		width = pl->pl_width;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1512
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1513
		/*
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1514
		 * If this is being called in scripted mode, or if this is the
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1515
		 * last column and it is left-justified, don't include a width
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1516
		 * format specifier.
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1517
		 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1518
		if (scripted || (pl->pl_next == NULL && !right_justify))
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1519
			(void) printf("%s", propstr);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1520
		else if (right_justify)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1521
			(void) printf("%*s", width, propstr);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1522
		else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1523
			(void) printf("%-*s", width, propstr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1524
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1525
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1526
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1527
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1528
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1529
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1530
 * Generic callback function to list a dataset or snapshot.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1531
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1532
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1533
list_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1534
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1535
	list_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1536
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1537
	if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1538
		if (!cbp->cb_scripted)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1539
			print_header(cbp->cb_proplist);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1540
		cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1541
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1542
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1543
	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1544
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1545
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1546
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1547
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1548
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1549
zfs_do_list(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1550
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1551
	int c;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1552
	boolean_t recurse = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1553
	boolean_t scripted = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1554
	static char default_fields[] =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1555
	    "name,used,available,referenced,mountpoint";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1556
	int types = ZFS_TYPE_ANY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1557
	char *fields = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
	char *basic_fields = default_fields;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1559
	list_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1560
	char *value;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1561
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1562
	char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1563
	zfs_sort_column_t *sortcol = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
	/* check options */
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1566
	while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1567
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1568
		case 'o':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1569
			fields = optarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1570
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1571
		case 'r':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1572
			recurse = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1573
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
		case 'H':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1575
			scripted = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1576
			break;
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1577
		case 's':
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1578
			if (zfs_add_sort_column(&sortcol, optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1579
			    B_FALSE) != 0) {
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1580
				(void) fprintf(stderr,
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1581
				    gettext("invalid property '%s'\n"), optarg);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1582
				usage(B_FALSE);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1583
			}
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1584
			break;
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1585
		case 'S':
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1586
			if (zfs_add_sort_column(&sortcol, optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1587
			    B_TRUE) != 0) {
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1588
				(void) fprintf(stderr,
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1589
				    gettext("invalid property '%s'\n"), optarg);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1590
				usage(B_FALSE);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1591
			}
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1592
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1593
		case 't':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1594
			types = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1595
			while (*optarg != '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1596
				switch (getsubopt(&optarg, type_subopts,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1597
				    &value)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1598
				case 0:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1599
					types |= ZFS_TYPE_FILESYSTEM;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1600
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1601
				case 1:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1602
					types |= ZFS_TYPE_VOLUME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1603
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1604
				case 2:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1605
					types |= ZFS_TYPE_SNAPSHOT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1606
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1607
				default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1608
					(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1609
					    gettext("invalid type '%s'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1610
					    value);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1611
					usage(B_FALSE);
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
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1615
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1616
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1617
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1618
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1619
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1620
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1622
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1623
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1624
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1625
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1626
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1627
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1628
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1629
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1630
	if (fields == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1631
		fields = basic_fields;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1632
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1633
	/*
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1634
	 * If the user specifies '-o all', the zfs_get_proplist() doesn't
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1635
	 * normally include the name of the dataset.  For 'zfs list', we always
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1636
	 * want this property to be first.
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1637
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1638
	if (zfs_get_proplist(g_zfs, fields, &cb.cb_proplist) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1639
		usage(B_FALSE);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1640
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
	cb.cb_scripted = scripted;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1642
	cb.cb_first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1643
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1644
	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
  1645
	    list_callback, &cb, B_TRUE);
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1646
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1647
	zfs_free_proplist(cb.cb_proplist);
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1648
	zfs_free_sort_columns(sortcol);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1649
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1650
	if (ret == 0 && cb.cb_first)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1651
		(void) printf(gettext("no datasets available\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1652
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1653
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1654
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1655
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1656
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1657
 * zfs rename <fs | snap | vol> <fs | snap | vol>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1658
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1659
 * Renames the given dataset to another of the same type.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1660
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1661
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1662
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1663
zfs_do_rename(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1664
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1665
	zfs_handle_t *zhp;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1666
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1667
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1668
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1669
	if (argc > 1 && argv[1][0] == '-') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1670
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1671
		    argv[1][1]);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1672
		usage(B_FALSE);
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
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1676
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1677
		(void) fprintf(stderr, gettext("missing source dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1678
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1679
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1680
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1681
	if (argc < 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1682
		(void) fprintf(stderr, gettext("missing target dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1684
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
	if (argc > 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1687
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1688
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1690
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1691
	if ((zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_ANY)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1692
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1693
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1694
	ret = (zfs_rename(zhp, argv[2]) != 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1695
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1696
	if (!ret)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1697
		zpool_log_history(g_zfs, argc, argv, argv[2], B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1698
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1699
	zfs_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1700
	return (ret);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1701
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1702
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1703
/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1704
 * zfs promote <fs>
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1705
 *
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1706
 * Promotes the given clone fs to be the parent
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1707
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1708
/* ARGSUSED */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1709
static int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1710
zfs_do_promote(int argc, char **argv)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1711
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1712
	zfs_handle_t *zhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1713
	int ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1714
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1715
	/* check options */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1716
	if (argc > 1 && argv[1][0] == '-') {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1717
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1718
		    argv[1][1]);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1719
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1720
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1721
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1722
	/* check number of arguments */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1723
	if (argc < 2) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1724
		(void) fprintf(stderr, gettext("missing clone filesystem"
2597
21c0f93f2513 6349987 lzjb.c lived longer than expected?
nd150628
parents: 2474
diff changeset
  1725
		    " argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1726
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1727
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1728
	if (argc > 2) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1729
		(void) fprintf(stderr, gettext("too many arguments\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1730
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1731
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1732
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1733
	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
  1734
	if (zhp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1735
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1736
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1737
	ret = (zfs_promote(zhp) != 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1738
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1739
	if (!ret)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1740
		zpool_log_history(g_zfs, argc, argv, argv[1], B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1741
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1742
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1743
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1744
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1745
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1746
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1747
 * zfs rollback [-rfR] <snapshot>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1748
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1749
 * 	-r	Delete any intervening snapshots before doing rollback
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1750
 * 	-R	Delete any snapshots and their clones
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1751
 * 	-f	Force unmount filesystems, even if they are in use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1752
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1753
 * Given a filesystem, rollback to a specific snapshot, discarding any changes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1754
 * since then and making it the active dataset.  If more recent snapshots exist,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1755
 * the command will complain unless the '-r' flag is given.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1756
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1757
typedef struct rollback_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1758
	uint64_t	cb_create;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1759
	boolean_t	cb_first;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1760
	int		cb_doclones;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1761
	char		*cb_target;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1762
	int		cb_error;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1763
	boolean_t	cb_recurse;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1764
	boolean_t	cb_dependent;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1765
} rollback_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1766
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1767
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
 * Report any snapshots more recent than the one specified.  Used when '-r' is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
 * not specified.  We reuse this same callback for the snapshot dependents - if
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1770
 * 'cb_dependent' is set, then this is a dependent and we should report it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1771
 * without checking the transaction group.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1772
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1773
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1774
rollback_check(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1775
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1776
	rollback_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1777
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1778
	if (cbp->cb_doclones) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1779
		zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1780
		return (0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1781
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1782
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1783
	if (!cbp->cb_dependent) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1784
		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
  1785
		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1786
		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1787
		    cbp->cb_create) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1789
			if (cbp->cb_first && !cbp->cb_recurse) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1790
				(void) fprintf(stderr, gettext("cannot "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1791
				    "rollback to '%s': more recent snapshots "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1792
				    "exist\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1793
				    cbp->cb_target);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1794
				(void) fprintf(stderr, gettext("use '-r' to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1795
				    "force deletion of the following "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1796
				    "snapshots:\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1797
				cbp->cb_first = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1798
				cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1799
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1800
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1801
			if (cbp->cb_recurse) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1802
				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
  1803
				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
  1804
				    rollback_check, cbp) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1805
					zfs_close(zhp);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1806
					return (-1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1807
				}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1808
				cbp->cb_dependent = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1809
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1810
				(void) fprintf(stderr, "%s\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1811
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1812
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1813
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1814
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1815
		if (cbp->cb_first && cbp->cb_recurse) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1816
			(void) fprintf(stderr, gettext("cannot rollback to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1817
			    "'%s': clones of previous snapshots exist\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1818
			    cbp->cb_target);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1819
			(void) fprintf(stderr, gettext("use '-R' to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1820
			    "force deletion of the following clones and "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1821
			    "dependents:\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1822
			cbp->cb_first = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1823
			cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1824
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1825
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1826
		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1827
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1828
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1829
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1830
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1831
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1832
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1833
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1834
zfs_do_rollback(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1835
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1836
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1837
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1838
	rollback_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1839
	zfs_handle_t *zhp, *snap;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
	char parentname[ZFS_MAXNAMELEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1841
	char *delim;
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1842
	int force = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1843
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1844
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1845
	while ((c = getopt(argc, argv, "rfR")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1846
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1847
		case 'f':
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1848
			force = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1849
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1850
		case 'r':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1851
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1852
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1853
		case 'R':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1854
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1855
			cb.cb_doclones = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1856
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1857
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1858
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1859
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1860
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1861
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1862
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1863
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1864
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1865
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1866
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1867
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1868
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1869
		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1870
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1871
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1872
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1873
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1874
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1875
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1876
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1877
	/* open the snapshot */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1878
	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1879
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1880
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1881
	/* open the parent dataset */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1882
	(void) strlcpy(parentname, argv[0], sizeof (parentname));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1883
	verify((delim = strrchr(parentname, '@')) != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1884
	*delim = '\0';
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1885
	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_ANY)) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1886
		zfs_close(snap);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1887
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1888
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1889
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1890
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1891
	 * Check for more recent snapshots and/or clones based on the presence
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1892
	 * of '-r' and '-R'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1893
	 */
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1894
	cb.cb_target = argv[0];
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1895
	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1896
	cb.cb_first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1897
	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
  1898
	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
  1899
		goto out;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1900
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1901
	if ((ret = cb.cb_error) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1902
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1903
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1904
	/*
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1905
	 * Rollback parent to the given snapshot.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1906
	 */
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1907
	ret = zfs_rollback(zhp, snap, force);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1908
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1909
	if (!ret) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1910
		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1911
		    B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1912
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1913
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1914
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1915
	zfs_close(snap);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1916
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1917
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1918
	if (ret == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1919
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1920
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1921
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1922
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1923
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1924
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1925
 * zfs set property=value { fs | snap | vol } ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1926
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
 * Sets the given property for all datasets specified on the command line.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1928
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1929
typedef struct set_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1930
	char		*cb_propname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1931
	char		*cb_value;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1932
	boolean_t	cb_any_successful;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
} set_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1934
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1936
set_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1938
	set_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1939
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1940
	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
  1941
		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
  1942
		case EZFS_MOUNTFAILED:
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  1943
			(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
  1944
			    "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
  1945
			break;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1946
		case EZFS_SHARENFSFAILED:
2169
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  1947
			(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
  1948
			    "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
  1949
			break;
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  1950
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1951
		return (1);
2169
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  1952
	}
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1953
	cbp->cb_any_successful = B_TRUE;
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  1954
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1955
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1956
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1957
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1958
zfs_do_set(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1959
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1960
	set_cbdata_t cb;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1961
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1962
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1963
	/* check for options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1964
	if (argc > 1 && argv[1][0] == '-') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1965
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1966
		    argv[1][1]);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1967
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1969
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1970
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1971
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1972
		(void) fprintf(stderr, gettext("missing property=value "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1973
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1974
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1975
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1976
	if (argc < 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1977
		(void) fprintf(stderr, gettext("missing dataset name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1978
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1979
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1980
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1981
	/* validate property=value argument */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1982
	cb.cb_propname = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1983
	if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1984
		(void) fprintf(stderr, gettext("missing value in "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1985
		    "property=value argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1986
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1987
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1988
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1989
	*cb.cb_value = '\0';
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1990
	cb.cb_value++;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1991
	cb.cb_any_successful = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1992
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1993
	if (*cb.cb_propname == '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1994
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1995
		    gettext("missing property in property=value argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1996
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1997
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1998
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1999
	ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
3635
63e2169c1249 PSARC 2007/050 zfs list -d
ck153898
parents: 3504
diff changeset
  2000
	    ZFS_TYPE_ANY, NULL, NULL, set_callback, &cb, B_FALSE);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2001
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2002
	if (cb.cb_any_successful) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2003
		*(cb.cb_value - 1) = '=';
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2004
		zpool_log_history(g_zfs, argc, argv, argv[2], B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2005
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2006
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2007
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2008
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2010
/*
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2011
 * zfs snapshot [-r] <fs@snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2012
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2013
 * Creates a snapshot with the given name.  While functionally equivalent to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2014
 * 'zfs create', it is a separate command to diffferentiate intent.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
zfs_do_snapshot(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
{
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2019
	int recursive = B_FALSE;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2020
	int ret;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2021
	char c;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2022
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2023
	/* check options */
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2024
	while ((c = getopt(argc, argv, ":r")) != -1) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2025
		switch (c) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2026
		case 'r':
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2027
			recursive = B_TRUE;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2028
			break;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2029
		case '?':
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2030
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2031
			    optopt);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2032
			usage(B_FALSE);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2033
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2034
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2035
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2036
	argc -= optind;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2037
	argv += optind;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2038
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2039
	/* check number of arguments */
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2040
	if (argc < 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2041
		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2042
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2043
	}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2044
	if (argc > 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2045
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2046
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2047
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2048
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2049
	ret = zfs_snapshot(g_zfs, argv[0], recursive);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2050
	if (ret && recursive)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2051
		(void) fprintf(stderr, gettext("no snapshots were created\n"));
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2052
	if (!ret) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2053
		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2054
		    B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2055
	}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2056
	return (ret != 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2057
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2058
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2059
/*
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2060
 * zfs send [-i <@snap>] <fs@snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2061
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2062
 * Send a backup stream to stdout.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2063
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2064
static int
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  2065
zfs_do_send(int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2066
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2067
	char *fromname = NULL;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2068
	char *cp;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2069
	zfs_handle_t *zhp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2070
	int c, err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2071
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2072
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2073
	while ((c = getopt(argc, argv, ":i:")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2074
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2075
		case 'i':
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2076
			if (fromname)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2077
				usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2078
			fromname = optarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2079
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2080
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2081
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2082
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2083
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2084
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2085
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2086
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2088
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2090
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2092
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2093
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2094
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2095
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2096
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2097
		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2098
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2099
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2100
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2101
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2102
		usage(B_FALSE);
789
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
	if (isatty(STDOUT_FILENO)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2106
		(void) fprintf(stderr,
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2107
		    gettext("Error: Stream can not be written to a terminal.\n"
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2108
			    "You must redirect standard output.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2109
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2110
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2111
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2112
	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2113
		return (1);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2114
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2115
	/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2116
	 * If they specified the full path to the snapshot, chop off
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2117
	 * everything except the short name of the snapshot.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2118
	 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2119
	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2120
		if (cp != fromname &&
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2121
		    strncmp(argv[0], fromname, cp - fromname + 1)) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2122
			(void) fprintf(stderr,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2123
			    gettext("incremental source must be "
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2124
			    "in same filesystem\n"));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2125
			usage(B_FALSE);
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2126
		}
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2127
		fromname = cp + 1;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2128
		if (strchr(fromname, '@') || strchr(fromname, '/')) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2129
			(void) fprintf(stderr,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2130
			    gettext("invalid incremental source\n"));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2131
			usage(B_FALSE);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2132
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2133
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2134
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3265
diff changeset
  2135
	err = zfs_send(zhp, fromname, STDOUT_FILENO);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2136
	zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2137
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2138
	return (err != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2139
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2140
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2141
/*
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  2142
 * zfs receive <fs@snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2143
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2144
 * Restore a backup stream from stdin.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2145
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2146
static int
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  2147
zfs_do_receive(int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2148
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2149
	int c, err;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2150
	boolean_t isprefix = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2151
	boolean_t dryrun = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2152
	boolean_t verbose = B_FALSE;
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2153
	boolean_t force = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2154
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2155
	/* check options */
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2156
	while ((c = getopt(argc, argv, ":dnvF")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2157
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2158
		case 'd':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2159
			isprefix = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2160
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2161
		case 'n':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2162
			dryrun = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2163
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2164
		case 'v':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2165
			verbose = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2166
			break;
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2167
		case 'F':
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2168
			force = B_TRUE;
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2169
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2170
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2171
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2172
			    "'%c' option\n"), optopt);
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
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2176
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2177
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2178
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2179
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2180
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2181
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2182
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2183
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2184
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2185
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2186
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2187
		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2188
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2189
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2190
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2191
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2192
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2193
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2194
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2195
	if (isatty(STDIN_FILENO)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2196
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2197
		    gettext("Error: Backup stream can not be read "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2198
			    "from a terminal.\n"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2199
			    "You must redirect standard input.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2200
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2201
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2202
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3265
diff changeset
  2203
	err = zfs_receive(g_zfs, argv[0], isprefix, verbose, dryrun, force,
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3265
diff changeset
  2204
	    STDIN_FILENO);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2205
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2206
	if (!err) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2207
		zpool_log_history(g_zfs, argc + optind, argv - optind, argv[0],
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2208
		    B_FALSE, B_FALSE);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2209
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2210
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2211
	return (err != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2212
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2213
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2214
typedef struct get_all_cbdata {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2215
	zfs_handle_t	**cb_handles;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2216
	size_t		cb_alloc;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2217
	size_t		cb_used;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2218
	uint_t		cb_types;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2219
} get_all_cbdata_t;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2220
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2221
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2222
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
  2223
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2224
	get_all_cbdata_t *cbp = data;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2225
	zfs_type_t type = zfs_get_type(zhp);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2226
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2227
	/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2228
	 * Interate over any nested datasets.
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2229
	 */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2230
	if (type == ZFS_TYPE_FILESYSTEM &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2231
	    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
  2232
		zfs_close(zhp);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2233
		return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2234
	}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2235
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2236
	/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2237
	 * Skip any datasets whose type does not match.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2238
	 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2239
	if ((type & cbp->cb_types) == 0) {
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2240
		zfs_close(zhp);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2241
		return (0);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2242
	}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2243
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2244
	if (cbp->cb_alloc == cbp->cb_used) {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2245
		zfs_handle_t **handles;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2246
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2247
		if (cbp->cb_alloc == 0)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2248
			cbp->cb_alloc = 64;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2249
		else
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2250
			cbp->cb_alloc *= 2;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2251
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2252
		handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2253
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2254
		if (cbp->cb_handles) {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2255
			bcopy(cbp->cb_handles, handles,
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2256
			    cbp->cb_used * sizeof (void *));
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2257
			free(cbp->cb_handles);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2258
		}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2259
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2260
		cbp->cb_handles = handles;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2261
	}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2262
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2263
	cbp->cb_handles[cbp->cb_used++] = zhp;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2264
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2265
	return (0);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2266
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2267
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2268
static void
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2269
get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count)
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2270
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2271
	get_all_cbdata_t cb = { 0 };
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2272
	cb.cb_types = types;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2273
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2274
	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2275
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2276
	*dslist = cb.cb_handles;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2277
	*count = cb.cb_used;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2278
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2279
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2280
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2281
dataset_cmp(const void *a, const void *b)
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2282
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2283
	zfs_handle_t **za = (zfs_handle_t **)a;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2284
	zfs_handle_t **zb = (zfs_handle_t **)b;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2285
	char mounta[MAXPATHLEN];
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2286
	char mountb[MAXPATHLEN];
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2287
	boolean_t gota, gotb;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2288
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2289
	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2290
		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2291
		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2292
	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2293
		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2294
		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2295
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2296
	if (gota && gotb)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2297
		return (strcmp(mounta, mountb));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2298
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2299
	if (gota)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2300
		return (-1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2301
	if (gotb)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2302
		return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2303
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2304
	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
  2305
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2306
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2307
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2308
 * Generic callback for sharing or mounting filesystems.  Because the code is so
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2309
 * similar, we have a common function with an extra parameter to determine which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2310
 * mode we are using.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2311
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2312
#define	OP_SHARE	0x1
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2313
#define	OP_MOUNT	0x2
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2314
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2316
 * Share or mount a dataset.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2317
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2318
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2319
share_mount_one(zfs_handle_t *zhp, int op, int flags, boolean_t explicit,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2320
    const char *options)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2321
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2322
	char mountpoint[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2323
	char shareopts[ZFS_MAXPROPLEN];
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2324
	const char *cmdname = op == OP_SHARE ? "share" : "mount";
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2325
	struct mnttab mnt;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2326
	uint64_t zoned, canmount;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2327
	zfs_type_t type = zfs_get_type(zhp);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2328
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2329
	assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2330
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2331
	if (type == ZFS_TYPE_FILESYSTEM) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2332
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2333
		 * Check to make sure we can mount/share this dataset.  If we
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2334
		 * are in the global zone and the filesystem is exported to a
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2335
		 * local zone, or if we are in a local zone and the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2336
		 * filesystem is not exported, then it is an error.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2337
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2338
		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2339
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2340
		if (zoned && getzoneid() == GLOBAL_ZONEID) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2341
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2342
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2343
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2344
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2345
			    "dataset is exported to a local zone\n"), cmdname,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2346
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2347
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2348
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2349
		} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2350
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2351
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2352
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2353
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2354
			    "permission denied\n"), cmdname,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2355
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2356
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2357
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2358
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2359
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2360
		 * Ignore any filesystems which don't apply to us. This
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2361
		 * includes those with a legacy mountpoint, or those with
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2362
		 * legacy share options.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2363
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2364
		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2365
		    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2366
		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2367
		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2368
		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2369
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2370
		if (op == OP_SHARE && strcmp(shareopts, "off") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2371
			if (!explicit)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2372
				return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2373
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2374
			(void) fprintf(stderr, gettext("cannot share '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2375
			    "legacy share\n"), zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2376
			(void) fprintf(stderr, gettext("use share(1M) to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2377
			    "share this filesystem\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2378
			return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2379
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2380
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2381
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2382
		 * We cannot share or mount legacy filesystems. If the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2383
		 * shareopts is non-legacy but the mountpoint is legacy, we
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2384
		 * treat it as a legacy share.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2385
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2386
		if (strcmp(mountpoint, "legacy") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2387
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2388
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2389
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2390
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2391
			    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2392
			(void) fprintf(stderr, gettext("use %s to "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2393
			    "%s this filesystem\n"), op == OP_SHARE ?
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2394
			    "share(1M)" : "mount(1M)", cmdname);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2395
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2396
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2397
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2398
		if (strcmp(mountpoint, "none") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2399
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2400
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2401
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2402
			(void) fprintf(stderr, gettext("cannot %s '%s': no "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2403
			    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2404
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2405
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2406
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2407
		if (!canmount) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2408
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2409
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2410
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2411
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2412
			    "'canmount' property is set to 'off'\n"), cmdname,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2413
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2414
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2415
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2416
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2417
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2418
		 * At this point, we have verified that the mountpoint and/or
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2419
		 * shareopts are appropriate for auto management. If the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2420
		 * filesystem is already mounted or shared, return (failing
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2421
		 * for explicit requests); otherwise mount or share the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2422
		 * filesystem.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2423
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2424
		switch (op) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2425
		case OP_SHARE:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2426
			if (zfs_is_shared_nfs(zhp, NULL)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2427
				if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2428
					return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2429
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2430
				(void) fprintf(stderr, gettext("cannot share "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2431
				    "'%s': filesystem already shared\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2432
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2433
				return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2434
			}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2435
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2436
			if (!zfs_is_mounted(zhp, NULL) &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2437
			    zfs_mount(zhp, NULL, 0) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2438
				return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2439
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2440
			if (zfs_share_nfs(zhp) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2441
				return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2442
			break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2443
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2444
		case OP_MOUNT:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2445
			if (options == NULL)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2446
				mnt.mnt_mntopts = "";
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2447
			else
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2448
				mnt.mnt_mntopts = (char *)options;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2449
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2450
			if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2451
			    zfs_is_mounted(zhp, NULL)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2452
				if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2453
					return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2454
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2455
				(void) fprintf(stderr, gettext("cannot mount "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2456
				    "'%s': filesystem already mounted\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2457
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2458
				return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2459
			}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2460
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2461
			if (zfs_mount(zhp, options, flags) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2462
				return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2463
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2464
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2465
	} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2466
		assert(op == OP_SHARE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2467
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2468
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2469
		 * Ignore any volumes that aren't shared.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2470
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2471
		verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2472
		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2473
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2474
		if (strcmp(shareopts, "off") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2475
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2476
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2477
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2478
			(void) fprintf(stderr, gettext("cannot share '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2479
			    "'shareiscsi' property not set\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2480
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2481
			(void) fprintf(stderr, gettext("set 'shareiscsi' "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2482
			    "property or use iscsitadm(1M) to share this "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2483
			    "volume\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2484
			return (1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2485
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2486
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2487
		if (zfs_is_shared_iscsi(zhp)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2488
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2489
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2490
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2491
			(void) fprintf(stderr, gettext("cannot share "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2492
			    "'%s': volume already shared\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2493
			    zfs_get_name(zhp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2494
			return (1);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2495
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2496
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2497
		if (zfs_share_iscsi(zhp) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2498
			return (1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2499
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2500
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2501
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2502
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2503
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2504
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2505
share_mount(int op, int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2506
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2507
	int do_all = 0;
2372
ecbf1ba25c2e 6449033 PIT nightly fails due to the fix for 6436514
lling
parents: 2369
diff changeset
  2508
	int c, ret = 0;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2509
	const char *options = NULL;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2510
	int types, flags = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2511
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2512
	/* check options */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2513
	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ao:O" : "a"))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2514
	    != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2515
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2516
		case 'a':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2517
			do_all = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2518
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2519
		case 'o':
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2520
			options = optarg;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2521
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2522
		case 'O':
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2523
			flags |= MS_OVERLAY;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2524
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2525
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2526
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2527
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2528
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2529
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2530
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2531
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2532
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2533
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2534
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2535
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2536
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2537
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2538
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2539
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2540
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2541
	if (do_all) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2542
		zfs_handle_t **dslist = NULL;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2543
		size_t i, count = 0;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2544
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2545
		if (op == OP_MOUNT) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2546
			types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2547
		} else if (argc > 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2548
			if (strcmp(argv[0], "nfs") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2549
				types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2550
			} else if (strcmp(argv[0], "iscsi") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2551
				types = ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2552
			} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2553
				(void) fprintf(stderr, gettext("share type "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2554
				    "must be 'nfs' or 'iscsi'\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2555
				usage(B_FALSE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2556
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2557
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2558
			argc--;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2559
			argv++;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2560
		} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2561
			types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2562
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2563
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2564
		if (argc != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2565
			(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2566
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2567
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2568
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2569
		get_all_datasets(types, &dslist, &count);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2570
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2571
		if (count == 0)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2572
			return (0);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2573
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2574
		qsort(dslist, count, sizeof (void *), dataset_cmp);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2575
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2576
		for (i = 0; i < count; i++) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2577
			if (share_mount_one(dslist[i], op, flags, B_FALSE,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2578
			    options) != 0)
2369
8e9197fd3c58 6436514 zfs share on /var/mail needs to be run explicitly after system boots
lling
parents: 2199
diff changeset
  2579
				ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2580
			zfs_close(dslist[i]);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2581
		}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2582
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2583
		free(dslist);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2584
	} else if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2585
		struct mnttab entry;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2586
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2587
		if (op == OP_SHARE) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2588
			(void) fprintf(stderr, gettext("missing filesystem "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2589
			    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2590
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2591
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2592
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2593
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2594
		 * When mount is given no arguments, go through /etc/mnttab and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2595
		 * display any active ZFS mounts.  We hide any snapshots, since
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2596
		 * they are controlled automatically.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2597
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2598
		rewind(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2599
		while (getmntent(mnttab_file, &entry) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2600
			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2601
			    strchr(entry.mnt_special, '@') != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2602
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2603
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2604
			(void) printf("%-30s  %s\n", entry.mnt_special,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2605
			    entry.mnt_mountp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2606
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2607
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2608
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2609
		zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2610
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2611
		types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2612
		if (op == OP_SHARE)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2613
			types |= ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2614
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2615
		if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2616
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2617
			    gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2618
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2619
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2620
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2621
		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2622
			ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2623
		} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2624
			ret = share_mount_one(zhp, op, flags, B_TRUE,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2625
			    options);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2626
			zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2627
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2628
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2629
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2630
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2631
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2632
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2633
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2634
 * zfs mount -a [nfs | iscsi]
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2635
 * zfs mount filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2636
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2637
 * Mount all filesystems, or mount the given filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2638
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2639
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2640
zfs_do_mount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2641
{
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2642
	return (share_mount(OP_MOUNT, argc, argv));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2643
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2644
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2645
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2646
 * zfs share -a [nfs | iscsi]
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2647
 * zfs share filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2648
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2649
 * Share all filesystems, or share the given filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2650
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2651
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2652
zfs_do_share(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2653
{
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2654
	return (share_mount(OP_SHARE, argc, argv));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2655
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2656
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2657
typedef struct unshare_unmount_node {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2658
	zfs_handle_t	*un_zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2659
	char		*un_mountp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2660
	uu_avl_node_t	un_avlnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2661
} unshare_unmount_node_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2662
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2663
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2664
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2665
unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2666
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2667
	const unshare_unmount_node_t *l = larg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2668
	const unshare_unmount_node_t *r = rarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2669
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2670
	return (strcmp(l->un_mountp, r->un_mountp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2671
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2672
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2673
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2674
 * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2675
 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2676
 * and unmount it appropriately.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2677
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2678
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2679
unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2680
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2681
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2682
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2683
	struct stat64 statbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2684
	struct extmnttab entry;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2685
	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2686
	char property[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2687
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2688
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2689
	 * Search for the path in /etc/mnttab.  Rather than looking for the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2690
	 * specific path, which can be fooled by non-standard paths (i.e. ".."
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2691
	 * or "//"), we stat() the path and search for the corresponding
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2692
	 * (major,minor) device pair.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2693
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2694
	if (stat64(path, &statbuf) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2695
		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2696
		    cmdname, path, strerror(errno));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2697
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2698
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2699
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2700
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2701
	 * Search for the given (major,minor) pair in the mount table.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2702
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2703
	rewind(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2704
	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2705
		if (entry.mnt_major == major(statbuf.st_dev) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2706
		    entry.mnt_minor == minor(statbuf.st_dev))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2707
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2708
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2709
	if (ret != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2710
		(void) fprintf(stderr, gettext("cannot %s '%s': not "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2711
		    "currently mounted\n"), cmdname, path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2712
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2713
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2714
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2715
	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2716
		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2717
		    "filesystem\n"), cmdname, path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2718
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2719
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2720
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2721
	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2722
	    ZFS_TYPE_FILESYSTEM)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2723
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2724
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2725
	verify(zfs_prop_get(zhp, op == OP_SHARE ?
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2726
		ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2727
		sizeof (property), NULL, NULL, 0, B_FALSE) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2728
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2729
	if (op == OP_SHARE) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2730
		if (strcmp(property, "off") == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2731
			(void) fprintf(stderr, gettext("cannot unshare "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2732
			    "'%s': legacy share\n"), path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2733
			(void) fprintf(stderr, gettext("use "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2734
			    "unshare(1M) to unshare this filesystem\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2735
			ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2736
		} else if (!zfs_is_shared_nfs(zhp, NULL)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2737
			(void) fprintf(stderr, gettext("cannot unshare '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2738
			    "not currently shared\n"), path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2739
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2740
		} else {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2741
			ret = zfs_unshareall_nfs(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2742
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2743
	} else {
1264
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  2744
		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
  2745
			ret = zfs_unmount(zhp, NULL, flags);
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  2746
		} else if (strcmp(property, "legacy") == 0) {
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  2747
			(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
  2748
			    "'%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
  2749
			    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
  2750
			(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
  2751
			    "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
  2752
			ret = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2753
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2754
			ret = zfs_unmountall(zhp, flags);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2755
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2756
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2757
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2758
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2759
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2760
	return (ret != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2761
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2762
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2763
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2764
 * Generic callback for unsharing or unmounting a filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2765
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2766
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2767
unshare_unmount(int op, int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2768
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2769
	int do_all = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2770
	int flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2771
	int ret = 0;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2772
	int types, c;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2773
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2774
	char property[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2775
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2776
	/* check options */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2777
	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2778
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2779
		case 'a':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2780
			do_all = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2781
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2782
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2783
			flags = MS_FORCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2784
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2785
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2786
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2787
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2788
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2789
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2790
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2791
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2792
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2793
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2794
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2795
	if (do_all) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2796
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2797
		 * We could make use of zfs_for_each() to walk all datasets in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2798
		 * the system, but this would be very inefficient, especially
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2799
		 * since we would have to linearly search /etc/mnttab for each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2800
		 * one.  Instead, do one pass through /etc/mnttab looking for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2801
		 * zfs entries and call zfs_unmount() for each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2802
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2803
		 * Things get a little tricky if the administrator has created
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2804
		 * mountpoints beneath other ZFS filesystems.  In this case, we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2805
		 * have to unmount the deepest filesystems first.  To accomplish
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2806
		 * this, we place all the mountpoints in an AVL tree sorted by
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2807
		 * the special type (dataset name), and walk the result in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2808
		 * reverse to make sure to get any snapshots first.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2809
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2810
		struct mnttab entry;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2811
		uu_avl_pool_t *pool;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2812
		uu_avl_t *tree;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2813
		unshare_unmount_node_t *node;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2814
		uu_avl_index_t idx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2815
		uu_avl_walk_t *walk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2816
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2817
		if (argc != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2818
			(void) fprintf(stderr, gettext("too many arguments\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2819
			usage(B_FALSE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2820
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2821
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2822
		if ((pool = uu_avl_pool_create("unmount_pool",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2823
		    sizeof (unshare_unmount_node_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2824
		    offsetof(unshare_unmount_node_t, un_avlnode),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2825
		    unshare_unmount_compare,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2826
		    UU_DEFAULT)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2827
			(void) fprintf(stderr, gettext("internal error: "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2828
			    "out of memory\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2829
			exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2830
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2831
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2832
		if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2833
			(void) fprintf(stderr, gettext("internal error: "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2834
			    "out of memory\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2835
			exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2836
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2837
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2838
		rewind(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2839
		while (getmntent(mnttab_file, &entry) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2840
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2841
			/* ignore non-ZFS entries */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2842
			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2843
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2844
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2845
			/* ignore snapshots */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2846
			if (strchr(entry.mnt_special, '@') != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2847
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2848
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2849
			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2850
			    ZFS_TYPE_FILESYSTEM)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2851
				ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2852
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2853
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2854
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2855
			verify(zfs_prop_get(zhp, op == OP_SHARE ?
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2856
			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2857
			    property, sizeof (property), NULL, NULL,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2858
			    0, B_FALSE) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2859
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2860
			/* Ignore legacy mounts and shares */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2861
			if ((op == OP_SHARE &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2862
			    strcmp(property, "off") == 0) ||
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2863
			    (op == OP_MOUNT &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2864
			    strcmp(property, "legacy") == 0)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2865
				zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2866
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2867
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2868
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2869
			node = safe_malloc(sizeof (unshare_unmount_node_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2870
			node->un_zhp = zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2871
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2872
			if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2873
			    NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2874
				(void) fprintf(stderr, gettext("internal error:"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2875
				    " out of memory\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2876
				exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2877
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2878
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2879
			uu_avl_node_init(node, &node->un_avlnode, pool);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2880
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2881
			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2882
				uu_avl_insert(tree, node, idx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2883
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2884
				zfs_close(node->un_zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2885
				free(node->un_mountp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2886
				free(node);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2887
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2888
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2889
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2890
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2891
		 * Walk the AVL tree in reverse, unmounting each filesystem and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2892
		 * removing it from the AVL tree in the process.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2893
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2894
		if ((walk = uu_avl_walk_start(tree,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2895
		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2896
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2897
			    gettext("internal error: out of memory"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2898
			exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2899
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2900
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2901
		while ((node = uu_avl_walk_next(walk)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2902
			uu_avl_remove(tree, node);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2903
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2904
			switch (op) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2905
			case OP_SHARE:
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2906
				if (zfs_unshare_nfs(node->un_zhp,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2907
				    node->un_mountp) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2908
					ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2909
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2910
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2911
			case OP_MOUNT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2912
				if (zfs_unmount(node->un_zhp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2913
				    node->un_mountp, flags) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2914
					ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2915
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2916
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2917
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2918
			zfs_close(node->un_zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2919
			free(node->un_mountp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2920
			free(node);
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
		uu_avl_walk_end(walk);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2924
		uu_avl_destroy(tree);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2925
		uu_avl_pool_destroy(pool);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2926
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2927
		if (op == OP_SHARE) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2928
			/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2929
			 * Finally, unshare any volumes shared via iSCSI.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2930
			 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2931
			zfs_handle_t **dslist = NULL;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2932
			size_t i, count = 0;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2933
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2934
			get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2935
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2936
			if (count != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2937
				qsort(dslist, count, sizeof (void *),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2938
				    dataset_cmp);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2939
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2940
				for (i = 0; i < count; i++) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2941
					if (zfs_unshare_iscsi(dslist[i]) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2942
						ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2943
					zfs_close(dslist[i]);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2944
				}
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
				free(dslist);
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
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2949
	} else {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2950
		if (argc != 1) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2951
			if (argc == 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2952
				(void) fprintf(stderr,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2953
				    gettext("missing filesystem argument\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2954
			else
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2955
				(void) fprintf(stderr,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2956
				    gettext("too many arguments\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2957
			usage(B_FALSE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2958
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2959
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2960
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2961
		 * We have an argument, but it may be a full path or a ZFS
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2962
		 * filesystem.  Pass full paths off to unmount_path() (shared by
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2963
		 * manual_unmount), otherwise open the filesystem and pass to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2964
		 * zfs_unmount().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2965
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2966
		if (argv[0][0] == '/')
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2967
			return (unshare_unmount_path(op, argv[0],
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2968
				flags, B_FALSE));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2969
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2970
		types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2971
		if (op == OP_SHARE)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2972
			types |= ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2973
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2974
		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2975
			return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2976
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2977
		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2978
			verify(zfs_prop_get(zhp, op == OP_SHARE ?
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2979
			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2980
			    sizeof (property), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2981
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2982
			switch (op) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2983
			case OP_SHARE:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2984
				if (strcmp(property, "off") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2985
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2986
					    "unshare '%s': legacy share\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2987
					    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2988
					(void) fprintf(stderr, gettext("use "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2989
					    "unshare(1M) to unshare this "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2990
					    "filesystem\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2991
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2992
				} else if (!zfs_is_shared_nfs(zhp, NULL)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2993
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2994
					    "unshare '%s': not currently "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2995
					    "shared\n"), zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2996
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2997
				} else if (zfs_unshareall_nfs(zhp) != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2998
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2999
				}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3000
				break;
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
			case OP_MOUNT:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3003
				if (strcmp(property, "legacy") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3004
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3005
					    "unmount '%s': legacy "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3006
					    "mountpoint\n"), zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3007
					(void) fprintf(stderr, gettext("use "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3008
					    "umount(1M) to unmount this "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3009
					    "filesystem\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3010
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3011
				} else if (!zfs_is_mounted(zhp, NULL)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3012
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3013
					    "unmount '%s': not currently "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3014
					    "mounted\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3015
					    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3016
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3017
				} else if (zfs_unmountall(zhp, flags) != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3018
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3019
				}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3020
				break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3021
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3022
		} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3023
			assert(op == OP_SHARE);
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
			verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, property,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3026
			    sizeof (property), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3027
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3028
			if (strcmp(property, "off") == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3029
				(void) fprintf(stderr, gettext("cannot unshare "
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3030
				    "'%s': 'shareiscsi' property not set\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3031
				    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3032
				(void) fprintf(stderr, gettext("set "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3033
				    "'shareiscsi' property or use "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3034
				    "iscsitadm(1M) to share this volume\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3035
				ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3036
			} else if (!zfs_is_shared_iscsi(zhp)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3037
				(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3038
				    "unshare '%s': not currently shared\n"),
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3039
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3040
				ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3041
			} else if (zfs_unshare_iscsi(zhp) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3042
				ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3043
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3044
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3045
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3046
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3047
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3048
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3049
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3050
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3051
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3052
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3053
 * zfs unmount -a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3054
 * zfs unmount filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3055
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3056
 * Unmount all filesystems, or a specific ZFS filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3057
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3058
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3059
zfs_do_unmount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3060
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3061
	return (unshare_unmount(OP_MOUNT, argc, argv));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3062
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3063
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3064
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3065
 * zfs unshare -a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3066
 * zfs unshare filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3067
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3068
 * Unshare all filesystems, or a specific ZFS filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3069
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3070
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3071
zfs_do_unshare(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3072
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3073
	return (unshare_unmount(OP_SHARE, argc, argv));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3074
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3075
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3076
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3077
 * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3078
 * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3079
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3080
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3081
manual_mount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3082
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3083
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3084
	char mountpoint[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3085
	char mntopts[MNT_LINE_MAX] = { '\0' };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3086
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3087
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3088
	int flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3089
	char *dataset, *path;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3090
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3091
	/* check options */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3092
	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3093
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3094
		case 'o':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3095
			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3096
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3097
		case 'O':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3098
			flags |= MS_OVERLAY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3099
			break;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3100
		case 'm':
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3101
			flags |= MS_NOMNTTAB;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3102
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3103
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3104
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3105
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3106
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3107
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3108
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3109
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3110
			    optopt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3111
			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3112
			    "<path>\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3113
			return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3114
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3115
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3116
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3117
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3118
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3119
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3120
	/* check that we only have two arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3121
	if (argc != 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3122
		if (argc == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3123
			(void) fprintf(stderr, gettext("missing dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3124
			    "argument\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3125
		else if (argc == 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3126
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3127
			    gettext("missing mountpoint argument\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3128
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3129
			(void) fprintf(stderr, gettext("too many arguments\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3130
		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3131
		return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3132
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3133
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3134
	dataset = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3135
	path = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3136
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3137
	/* try to open the dataset */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3138
	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3139
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3140
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3141
	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3142
	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3143
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3144
	/* check for legacy mountpoint and complain appropriately */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3145
	ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3146
	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3147
		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3148
		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3149
			(void) fprintf(stderr, gettext("mount failed: %s\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3150
			    strerror(errno));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3151
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3152
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3153
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3154
		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3155
		    "mounted using 'mount -F zfs'\n"), dataset);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3156
		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3157
		    "instead.\n"), path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3158
		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3159
		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3160
		(void) fprintf(stderr, gettext("See zfs(1M) for more "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3161
		    "information.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3162
		ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3163
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3165
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3166
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3167
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3168
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3169
 * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3170
 * unmounts of non-legacy filesystems, as this is the dominant administrative
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3171
 * interface.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3172
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3173
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3174
manual_unmount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3175
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3176
	int flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3177
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3178
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3179
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3180
	while ((c = getopt(argc, argv, "f")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3181
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3182
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3183
			flags = MS_FORCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3184
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3185
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3186
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3187
			    optopt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3188
			(void) fprintf(stderr, gettext("usage: unmount [-f] "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3189
			    "<path>\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3190
			return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3191
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3192
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3193
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3194
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3195
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3196
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3197
	/* check arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3198
	if (argc != 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3199
		if (argc == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3200
			(void) fprintf(stderr, gettext("missing path "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3201
			    "argument\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3202
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3203
			(void) fprintf(stderr, gettext("too many arguments\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3204
		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3205
		return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3206
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3207
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3208
	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3209
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3210
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3211
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3212
volcheck(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3213
{
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  3214
	boolean_t isinit = *((boolean_t *)data);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3215
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3216
	if (isinit)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3217
		return (zpool_create_zvol_links(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3218
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3219
		return (zpool_remove_zvol_links(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3220
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3221
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3222
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3223
 * Iterate over all pools in the system and either create or destroy /dev/zvol
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3224
 * links, depending on the value of 'isinit'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3225
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3226
static int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3227
do_volcheck(boolean_t isinit)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3228
{
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  3229
	return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3230
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3231
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3232
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3233
main(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3234
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3235
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3236
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3237
	char *progname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3238
	char *cmdname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3239
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3240
	(void) setlocale(LC_ALL, "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3241
	(void) textdomain(TEXT_DOMAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3242
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3243
	opterr = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3244
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3245
	if ((g_zfs = libzfs_init()) == NULL) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3246
		(void) fprintf(stderr, gettext("internal error: failed to "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3247
		    "initialize ZFS library\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3248
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3249
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3250
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3251
	libzfs_print_on_error(g_zfs, B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3252
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3253
	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3254
		(void) fprintf(stderr, gettext("internal error: unable to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3255
		    "open %s\n"), MNTTAB);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3256
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3257
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3258
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3259
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3260
	 * This command also doubles as the /etc/fs mount and unmount program.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3261
	 * Determine if we should take this behavior based on argv[0].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3262
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3263
	progname = basename(argv[0]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3264
	if (strcmp(progname, "mount") == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3265
		ret = manual_mount(argc, argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3266
	} else if (strcmp(progname, "umount") == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3267
		ret = manual_unmount(argc, argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3268
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3269
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3270
		 * Make sure the user has specified some command.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3271
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3272
		if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3273
			(void) fprintf(stderr, gettext("missing command\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3274
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3275
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3276
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3277
		cmdname = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3278
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3279
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3280
		 * The 'umount' command is an alias for 'unmount'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3281
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3282
		if (strcmp(cmdname, "umount") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3283
			cmdname = "unmount";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3284
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3285
		/*
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  3286
		 * The 'recv' command is an alias for 'receive'
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  3287
		 */
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  3288
		if (strcmp(cmdname, "recv") == 0)
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  3289
			cmdname = "receive";
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  3290
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  3291
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3292
		 * Special case '-?'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3293
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3294
		if (strcmp(cmdname, "-?") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3295
			usage(B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3296
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3297
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3298
		 * 'volinit' and 'volfini' do not appear in the usage message,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3299
		 * so we have to special case them here.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3300
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3301
		if (strcmp(cmdname, "volinit") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3302
			return (do_volcheck(B_TRUE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3303
		else if (strcmp(cmdname, "volfini") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3304
			return (do_volcheck(B_FALSE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3305
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3306
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3307
		 * Run the appropriate command.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3308
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3309
		for (i = 0; i < NCOMMAND; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3310
			if (command_table[i].name == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3311
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3312
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3313
			if (strcmp(cmdname, command_table[i].name) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3314
				current_command = &command_table[i];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3315
				ret = command_table[i].func(argc - 1, argv + 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3316
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3317
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3318
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3319
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3320
		if (i == NCOMMAND) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3321
			(void) fprintf(stderr, gettext("unrecognized "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3322
			    "command '%s'\n"), cmdname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3323
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3324
		}
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
	(void) fclose(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3328
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3329
	libzfs_fini(g_zfs);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3330
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3331
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3332
	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3333
	 * for the purposes of running ::findleaks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3334
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3335
	if (getenv("ZFS_ABORT") != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3336
		(void) printf("dumping core by request\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3337
		abort();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3338
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3339
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3340
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3341
}