usr/src/cmd/syseventd/modules/zfs_mod/zfs_mod.c
author Eric Taylor <Eric.Taylor@Sun.COM>
Mon, 28 Jun 2010 09:19:17 -0600
changeset 12710 66c9c36f2046
parent 12456 318640f559d8
child 12768 0e29ce2277d6
permissions -rw-r--r--
6677657 smb_list.c shouldn't exist 6840819 zfs does not automount directories after the pool is restored 6953725 sporadic syseventd core dump during lun expansion due to double free 6962880 erroneous creation date in usr/src/cmd/mdb/Makefile.common copyright 6963174 user-land libraries shouldn't directly compile uts/common/os/list.c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     1
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     2
 * CDDL HEADER START
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     3
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     5
 * Common Development and Distribution License (the "License").
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     6
 * You may not use this file except in compliance with the License.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     7
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    11
 * and limitations under the License.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    12
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    18
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    19
 * CDDL HEADER END
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    20
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    21
/*
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
    22
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    23
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    24
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    25
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    26
 * ZFS syseventd module.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    27
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    28
 * The purpose of this module is to identify when devices are added to the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    29
 * system, and appropriately online or replace the affected vdevs.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    30
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    31
 * When a device is added to the system:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    32
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    33
 * 	1. Search for any vdevs whose devid matches that of the newly added
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    34
 *	   device.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    35
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    36
 * 	2. If no vdevs are found, then search for any vdevs whose devfs path
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    37
 *	   matches that of the new device.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    38
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    39
 *	3. If no vdevs match by either method, then ignore the event.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    40
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    41
 * 	4. Attempt to online the device with a flag to indicate that it should
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    42
 *	   be unspared when resilvering completes.  If this succeeds, then the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    43
 *	   same device was inserted and we should continue normally.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    44
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    45
 *	5. If the pool does not have the 'autoreplace' property set, attempt to
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    46
 *	   online the device again without the unspare flag, which will
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    47
 *	   generate a FMA fault.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    48
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    49
 *	6. If the pool has the 'autoreplace' property set, and the matching vdev
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    50
 *	   is a whole disk, then label the new disk and attempt a 'zpool
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    51
 *	   replace'.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    52
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    53
 * The module responds to EC_DEV_ADD events for both disks and lofi devices,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    54
 * with the latter used for testing.  The special ESC_ZFS_VDEV_CHECK event
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    55
 * indicates that a device failed to open during pool load, but the autoreplace
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    56
 * property was set.  In this case, we deferred the associated FMA fault until
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    57
 * our module had a chance to process the autoreplace logic.  If the device
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    58
 * could not be replaced, then the second online attempt will trigger the FMA
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    59
 * fault that we skipped earlier.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    60
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    61
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    62
#include <alloca.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    63
#include <devid.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    64
#include <fcntl.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    65
#include <libnvpair.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    66
#include <libsysevent.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    67
#include <libzfs.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    68
#include <limits.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    69
#include <stdlib.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    70
#include <string.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    71
#include <syslog.h>
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    72
#include <sys/list.h>
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    73
#include <sys/sunddi.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    74
#include <sys/sysevent/eventdefs.h>
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    75
#include <sys/sysevent/dev.h>
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    76
#include <thread_pool.h>
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    77
#include <unistd.h>
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
    78
#include "syseventd.h"
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    79
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    80
#if defined(__i386) || defined(__amd64)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    81
#define	PHYS_PATH	":q"
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    82
#define	RAW_SLICE	"p0"
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    83
#elif defined(__sparc)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    84
#define	PHYS_PATH	":c"
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    85
#define	RAW_SLICE	"s2"
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    86
#else
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    87
#error Unknown architecture
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    88
#endif
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    89
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    90
typedef void (*zfs_process_func_t)(zpool_handle_t *, nvlist_t *, boolean_t);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    91
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
    92
libzfs_handle_t *g_zfshdl;
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    93
list_t g_pool_list;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    94
tpool_t *g_tpool;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    95
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    96
typedef struct unavailpool {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    97
	zpool_handle_t	*uap_zhp;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    98
	list_node_t	uap_node;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
    99
} unavailpool_t;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   100
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   101
int
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   102
zfs_toplevel_state(zpool_handle_t *zhp)
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   103
{
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   104
	nvlist_t *nvroot;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   105
	vdev_stat_t *vs;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   106
	unsigned int c;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   107
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   108
	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   109
	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   110
	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   111
	    (uint64_t **)&vs, &c) == 0);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   112
	return (vs->vs_state);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   113
}
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   114
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   115
static int
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   116
zfs_unavail_pool(zpool_handle_t *zhp, void *data)
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   117
{
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   118
	if (zfs_toplevel_state(zhp) < VDEV_STATE_DEGRADED) {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   119
		unavailpool_t *uap;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   120
		uap = malloc(sizeof (unavailpool_t));
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   121
		uap->uap_zhp = zhp;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   122
		list_insert_tail((list_t *)data, uap);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   123
	} else {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   124
		zpool_close(zhp);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   125
	}
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   126
	return (0);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   127
}
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   128
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   129
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   130
 * The device associated with the given vdev (either by devid or physical path)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   131
 * has been added to the system.  If 'isdisk' is set, then we only attempt a
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   132
 * replacement if it's a whole disk.  This also implies that we should label the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   133
 * disk first.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   134
 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   135
 * First, we attempt to online the device (making sure to undo any spare
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   136
 * operation when finished).  If this succeeds, then we're done.  If it fails,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   137
 * and the new state is VDEV_CANT_OPEN, it indicates that the device was opened,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   138
 * but that the label was not what we expected.  If the 'autoreplace' property
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   139
 * is not set, then we relabel the disk (if specified), and attempt a 'zpool
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   140
 * replace'.  If the online is successful, but the new state is something else
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   141
 * (REMOVED or FAULTED), it indicates that we're out of sync or in some sort of
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   142
 * race, and we should avoid attempting to relabel the disk.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   143
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   144
static void
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   145
zfs_process_add(zpool_handle_t *zhp, nvlist_t *vdev, boolean_t isdisk)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   146
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   147
	char *path;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   148
	vdev_state_t newstate;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   149
	nvlist_t *nvroot, *newvd;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   150
	uint64_t wholedisk = 0ULL;
6673
be079d6124af 6697301 deadlock between ZFS and devfs can hang system
eschrock
parents: 6643
diff changeset
   151
	char *physpath = NULL;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   152
	char rawpath[PATH_MAX], fullpath[PATH_MAX];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   153
	size_t len;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   154
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   155
	if (nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path) != 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   156
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   157
6673
be079d6124af 6697301 deadlock between ZFS and devfs can hang system
eschrock
parents: 6643
diff changeset
   158
	(void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_PHYS_PATH, &physpath);
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   159
	(void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   160
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   161
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   162
	 * We should have a way to online a device by guid.  With the current
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   163
	 * interface, we are forced to chop off the 's0' for whole disks.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   164
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   165
	(void) strlcpy(fullpath, path, sizeof (fullpath));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   166
	if (wholedisk)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   167
		fullpath[strlen(fullpath) - 2] = '\0';
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   168
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   169
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   170
	 * Attempt to online the device.  It would be nice to online this by
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   171
	 * GUID, but the current interface only supports lookup by path.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   172
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   173
	if (zpool_vdev_online(zhp, fullpath,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   174
	    ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE, &newstate) == 0 &&
6673
be079d6124af 6697301 deadlock between ZFS and devfs can hang system
eschrock
parents: 6643
diff changeset
   175
	    (newstate == VDEV_STATE_HEALTHY || newstate == VDEV_STATE_DEGRADED))
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   176
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   177
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   178
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   179
	 * If the pool doesn't have the autoreplace property set, then attempt a
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   180
	 * true online (without the unspare flag), which will trigger a FMA
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   181
	 * fault.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   182
	 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4451
diff changeset
   183
	if (!zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOREPLACE, NULL) ||
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   184
	    (isdisk && !wholedisk)) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   185
		(void) zpool_vdev_online(zhp, fullpath, ZFS_ONLINE_FORCEFAULT,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   186
		    &newstate);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   187
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   188
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   189
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   190
	if (isdisk) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   191
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   192
		 * If this is a request to label a whole disk, then attempt to
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   193
		 * write out the label.  Before we can label the disk, we need
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   194
		 * access to a raw node.  Ideally, we'd like to walk the devinfo
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   195
		 * tree and find a raw node from the corresponding parent node.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   196
		 * This is overly complicated, and since we know how we labeled
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   197
		 * this device in the first place, we know it's save to switch
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   198
		 * from /dev/dsk to /dev/rdsk and append the backup slice.
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   199
		 *
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   200
		 * If any part of this process fails, then do a force online to
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   201
		 * trigger a ZFS fault for the device (and any hot spare
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   202
		 * replacement).
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   203
		 */
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   204
		if (strncmp(path, "/dev/dsk/", 9) != 0) {
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   205
			(void) zpool_vdev_online(zhp, fullpath,
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   206
			    ZFS_ONLINE_FORCEFAULT, &newstate);
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   207
			return;
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   208
		}
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   209
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   210
		(void) strlcpy(rawpath, path + 9, sizeof (rawpath));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   211
		len = strlen(rawpath);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   212
		rawpath[len - 2] = '\0';
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   213
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   214
		if (zpool_label_disk(g_zfshdl, zhp, rawpath) != 0) {
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   215
			(void) zpool_vdev_online(zhp, fullpath,
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   216
			    ZFS_ONLINE_FORCEFAULT, &newstate);
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   217
			return;
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 5756
diff changeset
   218
		}
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   219
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   220
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   221
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   222
	 * Cosntruct the root vdev to pass to zpool_vdev_attach().  While adding
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   223
	 * the entire vdev structure is harmless, we construct a reduced set of
6673
be079d6124af 6697301 deadlock between ZFS and devfs can hang system
eschrock
parents: 6643
diff changeset
   224
	 * path/physpath/wholedisk to keep it simple.
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   225
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   226
	if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   227
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   228
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   229
	if (nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   230
		nvlist_free(nvroot);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   231
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   232
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   233
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   234
	if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   235
	    nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0 ||
6673
be079d6124af 6697301 deadlock between ZFS and devfs can hang system
eschrock
parents: 6643
diff changeset
   236
	    (physpath != NULL && nvlist_add_string(newvd,
be079d6124af 6697301 deadlock between ZFS and devfs can hang system
eschrock
parents: 6643
diff changeset
   237
	    ZPOOL_CONFIG_PHYS_PATH, physpath) != 0) ||
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   238
	    nvlist_add_uint64(newvd, ZPOOL_CONFIG_WHOLE_DISK, wholedisk) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   239
	    nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   240
	    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &newvd,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   241
	    1) != 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   242
		nvlist_free(newvd);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   243
		nvlist_free(nvroot);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   244
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   245
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   246
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   247
	nvlist_free(newvd);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   248
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   249
	(void) zpool_vdev_attach(zhp, fullpath, path, nvroot, B_TRUE);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   250
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   251
	nvlist_free(nvroot);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   252
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   253
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   254
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   255
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   256
 * Utility functions to find a vdev matching given criteria.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   257
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   258
typedef struct dev_data {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   259
	const char		*dd_compare;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   260
	const char		*dd_prop;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   261
	zfs_process_func_t	dd_func;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   262
	boolean_t		dd_found;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   263
	boolean_t		dd_isdisk;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   264
	uint64_t		dd_pool_guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   265
	uint64_t		dd_vdev_guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   266
} dev_data_t;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   267
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   268
static void
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   269
zfs_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *data)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   270
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   271
	dev_data_t *dp = data;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   272
	char *path;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   273
	uint_t c, children;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   274
	nvlist_t **child;
5756
05eb4c1ff492 6585441 spa_event_notify() doesn't pass its attributes
eschrock
parents: 5094
diff changeset
   275
	size_t len;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   276
	uint64_t guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   277
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   278
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   279
	 * First iterate over any children.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   280
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   281
	if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   282
	    &child, &children) == 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   283
		for (c = 0; c < children; c++)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   284
			zfs_iter_vdev(zhp, child[c], data);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   285
		return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   286
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   287
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   288
	if (dp->dd_vdev_guid != 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   289
		if (nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_GUID,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   290
		    &guid) != 0 || guid != dp->dd_vdev_guid)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   291
			return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   292
	} else {
5756
05eb4c1ff492 6585441 spa_event_notify() doesn't pass its attributes
eschrock
parents: 5094
diff changeset
   293
		len = strlen(dp->dd_compare);
05eb4c1ff492 6585441 spa_event_notify() doesn't pass its attributes
eschrock
parents: 5094
diff changeset
   294
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   295
		if (nvlist_lookup_string(nvl, dp->dd_prop, &path) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   296
		    strncmp(dp->dd_compare, path, len) != 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   297
			return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   298
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   299
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   300
		 * Normally, we want to have an exact match for the comparison
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   301
		 * string.  However, we allow substring matches in the following
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   302
		 * cases:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   303
		 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   304
		 * 	<path>:		This is a devpath, and the target is one
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   305
		 * 			of its children.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   306
		 *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   307
		 * 	<path/>		This is a devid for a whole disk, and
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   308
		 * 			the target is one of its children.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   309
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   310
		if (path[len] != '\0' && path[len] != ':' &&
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   311
		    path[len - 1] != '/')
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   312
			return;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   313
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   314
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   315
	(dp->dd_func)(zhp, nvl, dp->dd_isdisk);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   316
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   317
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   318
void
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   319
zfs_enable_ds(void *arg)
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   320
{
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   321
	unavailpool_t *pool = (unavailpool_t *)arg;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   322
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   323
	(void) zpool_enable_datasets(pool->uap_zhp, NULL, 0);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   324
	zpool_close(pool->uap_zhp);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   325
	free(pool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   326
}
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   327
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   328
static int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   329
zfs_iter_pool(zpool_handle_t *zhp, void *data)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   330
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   331
	nvlist_t *config, *nvl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   332
	dev_data_t *dp = data;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   333
	uint64_t pool_guid;
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   334
	unavailpool_t *pool;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   335
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   336
	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   337
		if (dp->dd_pool_guid == 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   338
		    (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   339
		    &pool_guid) == 0 && pool_guid == dp->dd_pool_guid)) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   340
			(void) nvlist_lookup_nvlist(config,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   341
			    ZPOOL_CONFIG_VDEV_TREE, &nvl);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   342
			zfs_iter_vdev(zhp, nvl, data);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   343
		}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   344
	}
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   345
	for (pool = list_head(&g_pool_list); pool != NULL;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   346
	    pool = list_next(&g_pool_list, pool)) {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   347
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   348
		if (strcmp(zpool_get_name(zhp),
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   349
		    zpool_get_name(pool->uap_zhp)))
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   350
			continue;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   351
		if (zfs_toplevel_state(zhp) >= VDEV_STATE_DEGRADED) {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   352
			list_remove(&g_pool_list, pool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   353
			(void) tpool_dispatch(g_tpool, zfs_enable_ds, pool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   354
			break;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   355
		}
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   356
	}
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   357
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   358
	zpool_close(zhp);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   359
	return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   360
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   361
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   362
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   363
 * Given a physical device path, iterate over all (pool, vdev) pairs which
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   364
 * correspond to the given path.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   365
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   366
static boolean_t
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   367
devpath_iter(const char *devpath, zfs_process_func_t func, boolean_t wholedisk)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   368
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   369
	dev_data_t data = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   370
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   371
	data.dd_compare = devpath;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   372
	data.dd_func = func;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   373
	data.dd_prop = ZPOOL_CONFIG_PHYS_PATH;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   374
	data.dd_found = B_FALSE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   375
	data.dd_isdisk = wholedisk;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   376
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   377
	(void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   378
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   379
	return (data.dd_found);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   380
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   381
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   382
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   383
 * Given a /devices path, lookup the corresponding devid for each minor node,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   384
 * and find any vdevs with matching devids.  Doing this straight up would be
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   385
 * rather inefficient, O(minor nodes * vdevs in system), so we take advantage of
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   386
 * the fact that each devid ends with "/<minornode>".  Once we find any valid
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   387
 * minor node, we chop off the portion after the last slash, and then search for
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   388
 * matching vdevs, which is O(vdevs in system).
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   389
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   390
static boolean_t
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   391
devid_iter(const char *devpath, zfs_process_func_t func, boolean_t wholedisk)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   392
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   393
	size_t len = strlen(devpath) + sizeof ("/devices") +
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   394
	    sizeof (PHYS_PATH) - 1;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   395
	char *fullpath;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   396
	int fd;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   397
	ddi_devid_t devid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   398
	char *devidstr, *fulldevid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   399
	dev_data_t data = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   400
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   401
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   402
	 * Try to open a known minor node.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   403
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   404
	fullpath = alloca(len);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   405
	(void) snprintf(fullpath, len, "/devices%s%s", devpath, PHYS_PATH);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   406
	if ((fd = open(fullpath, O_RDONLY)) < 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   407
		return (B_FALSE);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   408
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   409
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   410
	 * Determine the devid as a string, with no trailing slash for the minor
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   411
	 * node.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   412
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   413
	if (devid_get(fd, &devid) != 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   414
		(void) close(fd);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   415
		return (B_FALSE);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   416
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   417
	(void) close(fd);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   418
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   419
	if ((devidstr = devid_str_encode(devid, NULL)) == NULL) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   420
		devid_free(devid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   421
		return (B_FALSE);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   422
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   423
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   424
	len = strlen(devidstr) + 2;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   425
	fulldevid = alloca(len);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   426
	(void) snprintf(fulldevid, len, "%s/", devidstr);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   427
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   428
	data.dd_compare = fulldevid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   429
	data.dd_func = func;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   430
	data.dd_prop = ZPOOL_CONFIG_DEVID;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   431
	data.dd_found = B_FALSE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   432
	data.dd_isdisk = wholedisk;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   433
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   434
	(void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   435
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   436
	devid_str_free(devidstr);
12456
318640f559d8 6952269 several memory leaks in zfs_mod
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12318
diff changeset
   437
	devid_free(devid);
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   438
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   439
	return (data.dd_found);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   440
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   441
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   442
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   443
 * This function is called when we receive a devfs add event.  This can be
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   444
 * either a disk event or a lofi event, and the behavior is slightly different
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   445
 * depending on which it is.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   446
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   447
static int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   448
zfs_deliver_add(nvlist_t *nvl, boolean_t is_lofi)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   449
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   450
	char *devpath, *devname;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   451
	char path[PATH_MAX], realpath[PATH_MAX];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   452
	char *colon, *raw;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   453
	int ret;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   454
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   455
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   456
	 * The main unit of operation is the physical device path.  For disks,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   457
	 * this is the device node, as all minor nodes are affected.  For lofi
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   458
	 * devices, this includes the minor path.  Unfortunately, this isn't
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   459
	 * represented in the DEV_PHYS_PATH for various reasons.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   460
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   461
	if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devpath) != 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   462
		return (-1);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   463
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   464
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   465
	 * If this is a lofi device, then also get the minor instance name.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   466
	 * Unfortunately, the current payload doesn't include an easy way to get
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   467
	 * this information.  So we cheat by resolving the 'dev_name' (which
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   468
	 * refers to the raw device) and taking the portion between ':(*),raw'.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   469
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   470
	(void) strlcpy(realpath, devpath, sizeof (realpath));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   471
	if (is_lofi) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   472
		if (nvlist_lookup_string(nvl, DEV_NAME,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   473
		    &devname) == 0 &&
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   474
		    (ret = resolvepath(devname, path,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   475
		    sizeof (path))) > 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   476
			path[ret] = '\0';
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   477
			colon = strchr(path, ':');
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   478
			if (colon != NULL)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   479
				raw = strstr(colon + 1, ",raw");
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   480
			if (colon != NULL && raw != NULL) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   481
				*raw = '\0';
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   482
				(void) snprintf(realpath,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   483
				    sizeof (realpath), "%s%s",
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   484
				    devpath, colon);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   485
				*raw = ',';
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   486
			}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   487
		}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   488
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   489
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   490
	/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   491
	 * Iterate over all vdevs with a matching devid, and then those with a
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   492
	 * matching /devices path.  For disks, we only want to pay attention to
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   493
	 * vdevs marked as whole disks.  For lofi, we don't care (because we're
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   494
	 * matching an exact minor name).
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   495
	 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   496
	if (!devid_iter(realpath, zfs_process_add, !is_lofi))
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   497
		(void) devpath_iter(realpath, zfs_process_add, !is_lofi);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   498
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   499
	return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   500
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   501
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   502
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   503
 * Called when we receive a VDEV_CHECK event, which indicates a device could not
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   504
 * be opened during initial pool open, but the autoreplace property was set on
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   505
 * the pool.  In this case, we treat it as if it were an add event.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   506
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   507
static int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   508
zfs_deliver_check(nvlist_t *nvl)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   509
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   510
	dev_data_t data = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   511
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   512
	if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   513
	    &data.dd_pool_guid) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   514
	    nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   515
	    &data.dd_vdev_guid) != 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   516
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   517
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   518
	data.dd_isdisk = B_TRUE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   519
	data.dd_func = zfs_process_add;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   520
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   521
	(void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   522
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   523
	return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   524
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   525
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   526
#define	DEVICE_PREFIX	"/devices"
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   527
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   528
static int
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   529
zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   530
{
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   531
	char *devname = data;
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   532
	boolean_t avail_spare, l2cache;
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   533
	vdev_state_t newstate;
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   534
	nvlist_t *tgt;
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   535
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   536
	syseventd_print(9, "zfsdle_vdev_online: searching for %s in pool %s\n",
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   537
	    devname, zpool_get_name(zhp));
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   538
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   539
	if ((tgt = zpool_find_vdev_by_physpath(zhp, devname,
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   540
	    &avail_spare, &l2cache, NULL)) != NULL) {
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   541
		char *path, fullpath[MAXPATHLEN];
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   542
		uint64_t wholedisk = 0ULL;
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   543
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   544
		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   545
		    &path) == 0);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   546
		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   547
		    &wholedisk) == 0);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   548
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   549
		(void) strlcpy(fullpath, path, sizeof (fullpath));
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   550
		if (wholedisk)
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   551
			fullpath[strlen(fullpath) - 2] = '\0';
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   552
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   553
		if (zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   554
			syseventd_print(9, "zfsdle_vdev_online: setting device"
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   555
			    " device %s to ONLINE state in pool %s.\n",
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   556
			    fullpath, zpool_get_name(zhp));
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   557
			if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL)
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   558
				(void) zpool_vdev_online(zhp, fullpath, 0,
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   559
				    &newstate);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   560
		}
12456
318640f559d8 6952269 several memory leaks in zfs_mod
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12318
diff changeset
   561
		zpool_close(zhp);
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   562
		return (1);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   563
	}
12456
318640f559d8 6952269 several memory leaks in zfs_mod
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12318
diff changeset
   564
	zpool_close(zhp);
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   565
	return (0);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   566
}
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   567
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   568
int
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   569
zfs_deliver_dle(nvlist_t *nvl)
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   570
{
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   571
	char *devname;
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   572
	if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devname) != 0) {
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   573
		syseventd_print(9, "zfs_deliver_event: no physpath\n");
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   574
		return (-1);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   575
	}
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   576
	if (strncmp(devname, DEVICE_PREFIX, strlen(DEVICE_PREFIX)) != 0) {
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   577
		syseventd_print(9, "zfs_deliver_event: invalid "
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   578
		    "device '%s'", devname);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   579
		return (-1);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   580
	}
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   581
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   582
	/*
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   583
	 * We try to find the device using the physical
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   584
	 * path that has been supplied. We need to strip off
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   585
	 * the /devices prefix before starting our search.
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   586
	 */
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   587
	devname += strlen(DEVICE_PREFIX);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   588
	if (zpool_iter(g_zfshdl, zfsdle_vdev_online, devname) != 1) {
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   589
		syseventd_print(9, "zfs_deliver_event: device '%s' not"
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   590
		    " found\n", devname);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   591
		return (1);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   592
	}
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   593
	return (0);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   594
}
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   595
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   596
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   597
/*ARGSUSED*/
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   598
static int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   599
zfs_deliver_event(sysevent_t *ev, int unused)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   600
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   601
	const char *class = sysevent_get_class_name(ev);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   602
	const char *subclass = sysevent_get_subclass_name(ev);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   603
	nvlist_t *nvl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   604
	int ret;
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   605
	boolean_t is_lofi, is_check, is_dle = B_FALSE;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   606
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   607
	if (strcmp(class, EC_DEV_ADD) == 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   608
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   609
		 * We're mainly interested in disk additions, but we also listen
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   610
		 * for new lofi devices, to allow for simplified testing.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   611
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   612
		if (strcmp(subclass, ESC_DISK) == 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   613
			is_lofi = B_FALSE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   614
		else if (strcmp(subclass, ESC_LOFI) == 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   615
			is_lofi = B_TRUE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   616
		else
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   617
			return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   618
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   619
		is_check = B_FALSE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   620
	} else if (strcmp(class, EC_ZFS) == 0 &&
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   621
	    strcmp(subclass, ESC_ZFS_VDEV_CHECK) == 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   622
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   623
		 * This event signifies that a device failed to open during pool
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   624
		 * load, but the 'autoreplace' property was set, so we should
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   625
		 * pretend it's just been added.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   626
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   627
		is_check = B_TRUE;
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   628
	} else if (strcmp(class, EC_DEV_STATUS) == 0 &&
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   629
	    strcmp(subclass, ESC_DEV_DLE) == 0) {
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   630
		is_dle = B_TRUE;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   631
	} else {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   632
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   633
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   634
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   635
	if (sysevent_get_attr_list(ev, &nvl) != 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   636
		return (-1);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   637
12318
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   638
	if (is_dle)
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   639
		ret = zfs_deliver_dle(nvl);
a036286976a0 6907687 zfs pool is not automatically fixed when disk are brought back online or after boot
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 6673
diff changeset
   640
	else if (is_check)
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   641
		ret = zfs_deliver_check(nvl);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   642
	else
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   643
		ret = zfs_deliver_add(nvl, is_lofi);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   644
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   645
	nvlist_free(nvl);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   646
	return (ret);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   647
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   648
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   649
static struct slm_mod_ops zfs_mod_ops = {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   650
	SE_MAJOR_VERSION, SE_MINOR_VERSION, 10, zfs_deliver_event
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   651
};
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   652
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   653
struct slm_mod_ops *
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   654
slm_init()
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   655
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   656
	if ((g_zfshdl = libzfs_init()) == NULL)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   657
		return (NULL);
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   658
	/* collect a list of unavailable pools */
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   659
	list_create(&g_pool_list, sizeof (struct unavailpool),
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   660
	    offsetof(struct unavailpool, uap_node));
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   661
	(void) zpool_iter(g_zfshdl, zfs_unavail_pool, (void *)&g_pool_list);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   662
	if (!list_is_empty(&g_pool_list))
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   663
		g_tpool = tpool_create(1, sysconf(_SC_NPROCESSORS_ONLN),
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   664
		    0, NULL);
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   665
	return (&zfs_mod_ops);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   666
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   667
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   668
void
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   669
slm_fini()
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   670
{
12710
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   671
	unavailpool_t *pool;
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   672
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   673
	if (g_tpool) {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   674
		tpool_wait(g_tpool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   675
		tpool_destroy(g_tpool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   676
	}
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   677
	while ((pool = (list_head(&g_pool_list))) != NULL) {
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   678
		list_remove(&g_pool_list, pool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   679
		zpool_close(pool->uap_zhp);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   680
		free(pool);
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   681
	}
66c9c36f2046 6677657 smb_list.c shouldn't exist
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12456
diff changeset
   682
	list_destroy(&g_pool_list);
12456
318640f559d8 6952269 several memory leaks in zfs_mod
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 12318
diff changeset
   683
	libzfs_fini(g_zfshdl);
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents:
diff changeset
   684
}