1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5bef6b7d2Swebaker * Common Development and Distribution License (the "License").
6bef6b7d2Swebaker * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21fa9e4066Sahrens /*
22f13665b7Sbo zhou - Sun Microsystems - Beijing China * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23bf16b11eSMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24a5b57771SDan McDonald * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
2539cddb10SJoshua M. Clulow * Copyright (c) 2013 Joyent, Inc. All rights reserved.
26fa9e4066Sahrens */
27fa9e4066Sahrens
28fa9e4066Sahrens #include <sys/zfs_context.h>
29dcba9f3fSGeorge Wilson #include <sys/spa_impl.h>
30e7cbe64fSgw25295 #include <sys/refcount.h>
31fa9e4066Sahrens #include <sys/vdev_disk.h>
32fa9e4066Sahrens #include <sys/vdev_impl.h>
33fa9e4066Sahrens #include <sys/fs/zfs.h>
34fa9e4066Sahrens #include <sys/zio.h>
35afefbcddSeschrock #include <sys/sunldi.h>
364263d13fSGeorge Wilson #include <sys/efi_partition.h>
3751ece835Seschrock #include <sys/fm/fs/zfs.h>
38fa9e4066Sahrens
39fa9e4066Sahrens /*
40fa9e4066Sahrens * Virtual device vector for disks.
41fa9e4066Sahrens */
42fa9e4066Sahrens
43fa9e4066Sahrens extern ldi_ident_t zfs_li;
44fa9e4066Sahrens
4539cddb10SJoshua M. Clulow static void vdev_disk_close(vdev_t *);
4639cddb10SJoshua M. Clulow
4739cddb10SJoshua M. Clulow typedef struct vdev_disk_ldi_cb {
4839cddb10SJoshua M. Clulow list_node_t lcb_next;
4939cddb10SJoshua M. Clulow ldi_callback_id_t lcb_id;
5039cddb10SJoshua M. Clulow } vdev_disk_ldi_cb_t;
5139cddb10SJoshua M. Clulow
5239cddb10SJoshua M. Clulow static void
vdev_disk_alloc(vdev_t * vd)5339cddb10SJoshua M. Clulow vdev_disk_alloc(vdev_t *vd)
5439cddb10SJoshua M. Clulow {
5539cddb10SJoshua M. Clulow vdev_disk_t *dvd;
5639cddb10SJoshua M. Clulow
5739cddb10SJoshua M. Clulow dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
5839cddb10SJoshua M. Clulow /*
5939cddb10SJoshua M. Clulow * Create the LDI event callback list.
6039cddb10SJoshua M. Clulow */
6139cddb10SJoshua M. Clulow list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
6239cddb10SJoshua M. Clulow offsetof(vdev_disk_ldi_cb_t, lcb_next));
6339cddb10SJoshua M. Clulow }
6439cddb10SJoshua M. Clulow
6539cddb10SJoshua M. Clulow static void
vdev_disk_free(vdev_t * vd)6639cddb10SJoshua M. Clulow vdev_disk_free(vdev_t *vd)
6739cddb10SJoshua M. Clulow {
6839cddb10SJoshua M. Clulow vdev_disk_t *dvd = vd->vdev_tsd;
6939cddb10SJoshua M. Clulow vdev_disk_ldi_cb_t *lcb;
7039cddb10SJoshua M. Clulow
7139cddb10SJoshua M. Clulow if (dvd == NULL)
7239cddb10SJoshua M. Clulow return;
7339cddb10SJoshua M. Clulow
7439cddb10SJoshua M. Clulow /*
7539cddb10SJoshua M. Clulow * We have already closed the LDI handle. Clean up the LDI event
7639cddb10SJoshua M. Clulow * callbacks and free vd->vdev_tsd.
7739cddb10SJoshua M. Clulow */
7839cddb10SJoshua M. Clulow while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
7939cddb10SJoshua M. Clulow list_remove(&dvd->vd_ldi_cbs, lcb);
8039cddb10SJoshua M. Clulow (void) ldi_ev_remove_callbacks(lcb->lcb_id);
8139cddb10SJoshua M. Clulow kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
8239cddb10SJoshua M. Clulow }
8339cddb10SJoshua M. Clulow list_destroy(&dvd->vd_ldi_cbs);
8439cddb10SJoshua M. Clulow kmem_free(dvd, sizeof (vdev_disk_t));
8539cddb10SJoshua M. Clulow vd->vdev_tsd = NULL;
8639cddb10SJoshua M. Clulow }
8739cddb10SJoshua M. Clulow
8839cddb10SJoshua M. Clulow /* ARGSUSED */
8939cddb10SJoshua M. Clulow static int
vdev_disk_off_notify(ldi_handle_t lh,ldi_ev_cookie_t ecookie,void * arg,void * ev_data)9039cddb10SJoshua M. Clulow vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
9139cddb10SJoshua M. Clulow void *ev_data)
9239cddb10SJoshua M. Clulow {
9339cddb10SJoshua M. Clulow vdev_t *vd = (vdev_t *)arg;
9439cddb10SJoshua M. Clulow vdev_disk_t *dvd = vd->vdev_tsd;
9539cddb10SJoshua M. Clulow
9639cddb10SJoshua M. Clulow /*
9739cddb10SJoshua M. Clulow * Ignore events other than offline.
9839cddb10SJoshua M. Clulow */
9939cddb10SJoshua M. Clulow if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
10039cddb10SJoshua M. Clulow return (LDI_EV_SUCCESS);
10139cddb10SJoshua M. Clulow
10239cddb10SJoshua M. Clulow /*
10339cddb10SJoshua M. Clulow * All LDI handles must be closed for the state change to succeed, so
10439cddb10SJoshua M. Clulow * call on vdev_disk_close() to do this.
10539cddb10SJoshua M. Clulow *
10639cddb10SJoshua M. Clulow * We inform vdev_disk_close that it is being called from offline
10739cddb10SJoshua M. Clulow * notify context so it will defer cleanup of LDI event callbacks and
10839cddb10SJoshua M. Clulow * freeing of vd->vdev_tsd to the offline finalize or a reopen.
10939cddb10SJoshua M. Clulow */
11039cddb10SJoshua M. Clulow dvd->vd_ldi_offline = B_TRUE;
11139cddb10SJoshua M. Clulow vdev_disk_close(vd);
11239cddb10SJoshua M. Clulow
11339cddb10SJoshua M. Clulow /*
11439cddb10SJoshua M. Clulow * Now that the device is closed, request that the spa_async_thread
11539cddb10SJoshua M. Clulow * mark the device as REMOVED and notify FMA of the removal.
11639cddb10SJoshua M. Clulow */
11739cddb10SJoshua M. Clulow zfs_post_remove(vd->vdev_spa, vd);
11839cddb10SJoshua M. Clulow vd->vdev_remove_wanted = B_TRUE;
11939cddb10SJoshua M. Clulow spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
12039cddb10SJoshua M. Clulow
12139cddb10SJoshua M. Clulow return (LDI_EV_SUCCESS);
12239cddb10SJoshua M. Clulow }
12339cddb10SJoshua M. Clulow
12439cddb10SJoshua M. Clulow /* ARGSUSED */
12539cddb10SJoshua M. Clulow static void
vdev_disk_off_finalize(ldi_handle_t lh,ldi_ev_cookie_t ecookie,int ldi_result,void * arg,void * ev_data)12639cddb10SJoshua M. Clulow vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
12739cddb10SJoshua M. Clulow int ldi_result, void *arg, void *ev_data)
12839cddb10SJoshua M. Clulow {
12939cddb10SJoshua M. Clulow vdev_t *vd = (vdev_t *)arg;
13039cddb10SJoshua M. Clulow
13139cddb10SJoshua M. Clulow /*
13239cddb10SJoshua M. Clulow * Ignore events other than offline.
13339cddb10SJoshua M. Clulow */
13439cddb10SJoshua M. Clulow if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
13539cddb10SJoshua M. Clulow return;
13639cddb10SJoshua M. Clulow
13739cddb10SJoshua M. Clulow /*
13839cddb10SJoshua M. Clulow * We have already closed the LDI handle in notify.
13939cddb10SJoshua M. Clulow * Clean up the LDI event callbacks and free vd->vdev_tsd.
14039cddb10SJoshua M. Clulow */
14139cddb10SJoshua M. Clulow vdev_disk_free(vd);
14239cddb10SJoshua M. Clulow
14339cddb10SJoshua M. Clulow /*
14439cddb10SJoshua M. Clulow * Request that the vdev be reopened if the offline state change was
14539cddb10SJoshua M. Clulow * unsuccessful.
14639cddb10SJoshua M. Clulow */
14739cddb10SJoshua M. Clulow if (ldi_result != LDI_EV_SUCCESS) {
14839cddb10SJoshua M. Clulow vd->vdev_probe_wanted = B_TRUE;
14939cddb10SJoshua M. Clulow spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
15039cddb10SJoshua M. Clulow }
15139cddb10SJoshua M. Clulow }
15239cddb10SJoshua M. Clulow
15339cddb10SJoshua M. Clulow static ldi_ev_callback_t vdev_disk_off_callb = {
15439cddb10SJoshua M. Clulow .cb_vers = LDI_EV_CB_VERS,
15539cddb10SJoshua M. Clulow .cb_notify = vdev_disk_off_notify,
15639cddb10SJoshua M. Clulow .cb_finalize = vdev_disk_off_finalize
15739cddb10SJoshua M. Clulow };
15839cddb10SJoshua M. Clulow
15939cddb10SJoshua M. Clulow /* ARGSUSED */
16039cddb10SJoshua M. Clulow static void
vdev_disk_dgrd_finalize(ldi_handle_t lh,ldi_ev_cookie_t ecookie,int ldi_result,void * arg,void * ev_data)16139cddb10SJoshua M. Clulow vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
16239cddb10SJoshua M. Clulow int ldi_result, void *arg, void *ev_data)
16339cddb10SJoshua M. Clulow {
16439cddb10SJoshua M. Clulow vdev_t *vd = (vdev_t *)arg;
16539cddb10SJoshua M. Clulow
16639cddb10SJoshua M. Clulow /*
16739cddb10SJoshua M. Clulow * Ignore events other than degrade.
16839cddb10SJoshua M. Clulow */
16939cddb10SJoshua M. Clulow if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
17039cddb10SJoshua M. Clulow return;
17139cddb10SJoshua M. Clulow
17239cddb10SJoshua M. Clulow /*
17339cddb10SJoshua M. Clulow * Degrade events always succeed. Mark the vdev as degraded.
17439cddb10SJoshua M. Clulow * This status is purely informative for the user.
17539cddb10SJoshua M. Clulow */
17639cddb10SJoshua M. Clulow (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
17739cddb10SJoshua M. Clulow }
17839cddb10SJoshua M. Clulow
17939cddb10SJoshua M. Clulow static ldi_ev_callback_t vdev_disk_dgrd_callb = {
18039cddb10SJoshua M. Clulow .cb_vers = LDI_EV_CB_VERS,
18139cddb10SJoshua M. Clulow .cb_notify = NULL,
18239cddb10SJoshua M. Clulow .cb_finalize = vdev_disk_dgrd_finalize
18339cddb10SJoshua M. Clulow };
18439cddb10SJoshua M. Clulow
185dcba9f3fSGeorge Wilson static void
vdev_disk_hold(vdev_t * vd)186dcba9f3fSGeorge Wilson vdev_disk_hold(vdev_t *vd)
187dcba9f3fSGeorge Wilson {
188dcba9f3fSGeorge Wilson ddi_devid_t devid;
189dcba9f3fSGeorge Wilson char *minor;
190dcba9f3fSGeorge Wilson
191dcba9f3fSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
192dcba9f3fSGeorge Wilson
193dcba9f3fSGeorge Wilson /*
194dcba9f3fSGeorge Wilson * We must have a pathname, and it must be absolute.
195dcba9f3fSGeorge Wilson */
196dcba9f3fSGeorge Wilson if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
197dcba9f3fSGeorge Wilson return;
198dcba9f3fSGeorge Wilson
199dcba9f3fSGeorge Wilson /*
200dcba9f3fSGeorge Wilson * Only prefetch path and devid info if the device has
201dcba9f3fSGeorge Wilson * never been opened.
202dcba9f3fSGeorge Wilson */
203dcba9f3fSGeorge Wilson if (vd->vdev_tsd != NULL)
204dcba9f3fSGeorge Wilson return;
205dcba9f3fSGeorge Wilson
206dcba9f3fSGeorge Wilson if (vd->vdev_wholedisk == -1ULL) {
207dcba9f3fSGeorge Wilson size_t len = strlen(vd->vdev_path) + 3;
208dcba9f3fSGeorge Wilson char *buf = kmem_alloc(len, KM_SLEEP);
209dcba9f3fSGeorge Wilson
210dcba9f3fSGeorge Wilson (void) snprintf(buf, len, "%ss0", vd->vdev_path);
211dcba9f3fSGeorge Wilson
212dcba9f3fSGeorge Wilson (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
213dcba9f3fSGeorge Wilson kmem_free(buf, len);
214dcba9f3fSGeorge Wilson }
215dcba9f3fSGeorge Wilson
216dcba9f3fSGeorge Wilson if (vd->vdev_name_vp == NULL)
217dcba9f3fSGeorge Wilson (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
218dcba9f3fSGeorge Wilson
219dcba9f3fSGeorge Wilson if (vd->vdev_devid != NULL &&
220dcba9f3fSGeorge Wilson ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
221dcba9f3fSGeorge Wilson (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
222dcba9f3fSGeorge Wilson ddi_devid_str_free(minor);
223dcba9f3fSGeorge Wilson ddi_devid_free(devid);
224dcba9f3fSGeorge Wilson }
225dcba9f3fSGeorge Wilson }
226dcba9f3fSGeorge Wilson
227dcba9f3fSGeorge Wilson static void
vdev_disk_rele(vdev_t * vd)228dcba9f3fSGeorge Wilson vdev_disk_rele(vdev_t *vd)
229dcba9f3fSGeorge Wilson {
230dcba9f3fSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
231dcba9f3fSGeorge Wilson
232dcba9f3fSGeorge Wilson if (vd->vdev_name_vp) {
233dcba9f3fSGeorge Wilson VN_RELE_ASYNC(vd->vdev_name_vp,
234dcba9f3fSGeorge Wilson dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
235dcba9f3fSGeorge Wilson vd->vdev_name_vp = NULL;
236dcba9f3fSGeorge Wilson }
237dcba9f3fSGeorge Wilson if (vd->vdev_devid_vp) {
238dcba9f3fSGeorge Wilson VN_RELE_ASYNC(vd->vdev_devid_vp,
239dcba9f3fSGeorge Wilson dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
240dcba9f3fSGeorge Wilson vd->vdev_devid_vp = NULL;
241dcba9f3fSGeorge Wilson }
242dcba9f3fSGeorge Wilson }
243dcba9f3fSGeorge Wilson
2444263d13fSGeorge Wilson static uint64_t
vdev_disk_get_space(vdev_t * vd,uint64_t capacity,uint_t blksz)2454263d13fSGeorge Wilson vdev_disk_get_space(vdev_t *vd, uint64_t capacity, uint_t blksz)
2464263d13fSGeorge Wilson {
2474263d13fSGeorge Wilson ASSERT(vd->vdev_wholedisk);
2484263d13fSGeorge Wilson
2494263d13fSGeorge Wilson vdev_disk_t *dvd = vd->vdev_tsd;
2504263d13fSGeorge Wilson dk_efi_t dk_ioc;
2514263d13fSGeorge Wilson efi_gpt_t *efi;
2524263d13fSGeorge Wilson uint64_t avail_space = 0;
2534263d13fSGeorge Wilson int efisize = EFI_LABEL_SIZE * 2;
2544263d13fSGeorge Wilson
2554263d13fSGeorge Wilson dk_ioc.dki_data = kmem_alloc(efisize, KM_SLEEP);
2564263d13fSGeorge Wilson dk_ioc.dki_lba = 1;
2574263d13fSGeorge Wilson dk_ioc.dki_length = efisize;
2584263d13fSGeorge Wilson dk_ioc.dki_data_64 = (uint64_t)(uintptr_t)dk_ioc.dki_data;
2594263d13fSGeorge Wilson efi = dk_ioc.dki_data;
2604263d13fSGeorge Wilson
2614263d13fSGeorge Wilson if (ldi_ioctl(dvd->vd_lh, DKIOCGETEFI, (intptr_t)&dk_ioc,
2624263d13fSGeorge Wilson FKIOCTL, kcred, NULL) == 0) {
2634263d13fSGeorge Wilson uint64_t efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
2644263d13fSGeorge Wilson
2654263d13fSGeorge Wilson if (capacity > efi_altern_lba)
2664263d13fSGeorge Wilson avail_space = (capacity - efi_altern_lba) * blksz;
2674263d13fSGeorge Wilson }
2684263d13fSGeorge Wilson kmem_free(dk_ioc.dki_data, efisize);
2694263d13fSGeorge Wilson return (avail_space);
2704263d13fSGeorge Wilson }
2714263d13fSGeorge Wilson
272a5b57771SDan McDonald /*
273a5b57771SDan McDonald * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when
274a5b57771SDan McDonald * even a fallback to DKIOCGMEDIAINFO fails.
275a5b57771SDan McDonald */
276a5b57771SDan McDonald #ifdef DEBUG
277a5b57771SDan McDonald #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__)
278a5b57771SDan McDonald #else
279a5b57771SDan McDonald #define VDEV_DEBUG(...) /* Nothing... */
280a5b57771SDan McDonald #endif
281a5b57771SDan McDonald
282fa9e4066Sahrens static int
vdev_disk_open(vdev_t * vd,uint64_t * psize,uint64_t * max_psize,uint64_t * ashift)2834263d13fSGeorge Wilson vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
2844263d13fSGeorge Wilson uint64_t *ashift)
285fa9e4066Sahrens {
2868ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa;
28739cddb10SJoshua M. Clulow vdev_disk_t *dvd = vd->vdev_tsd;
28839cddb10SJoshua M. Clulow ldi_ev_cookie_t ecookie;
28939cddb10SJoshua M. Clulow vdev_disk_ldi_cb_t *lcb;
290a5b57771SDan McDonald union {
291a5b57771SDan McDonald struct dk_minfo_ext ude;
292a5b57771SDan McDonald struct dk_minfo ud;
293a5b57771SDan McDonald } dks;
294a5b57771SDan McDonald struct dk_minfo_ext *dkmext = &dks.ude;
295a5b57771SDan McDonald struct dk_minfo *dkm = &dks.ud;
2960a4e9518Sgw25295 int error;
297e14bb325SJeff Bonwick dev_t dev;
298e14bb325SJeff Bonwick int otyp;
299fb02ae02SGeorge Wilson boolean_t validate_devid = B_FALSE;
300fb02ae02SGeorge Wilson ddi_devid_t devid;
301a5b57771SDan McDonald uint64_t capacity = 0, blksz = 0, pbsize;
302fa9e4066Sahrens
303fa9e4066Sahrens /*
304fa9e4066Sahrens * We must have a pathname, and it must be absolute.
305fa9e4066Sahrens */
306fa9e4066Sahrens if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
307fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
308be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
309fa9e4066Sahrens }
310fa9e4066Sahrens
311095bcd66SGeorge Wilson /*
312095bcd66SGeorge Wilson * Reopen the device if it's not currently open. Otherwise,
313095bcd66SGeorge Wilson * just update the physical size of the device.
314095bcd66SGeorge Wilson */
31539cddb10SJoshua M. Clulow if (dvd != NULL) {
31639cddb10SJoshua M. Clulow if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
31739cddb10SJoshua M. Clulow /*
31839cddb10SJoshua M. Clulow * If we are opening a device in its offline notify
31939cddb10SJoshua M. Clulow * context, the LDI handle was just closed. Clean
32039cddb10SJoshua M. Clulow * up the LDI event callbacks and free vd->vdev_tsd.
32139cddb10SJoshua M. Clulow */
32239cddb10SJoshua M. Clulow vdev_disk_free(vd);
32339cddb10SJoshua M. Clulow } else {
324095bcd66SGeorge Wilson ASSERT(vd->vdev_reopening);
325095bcd66SGeorge Wilson goto skip_open;
326095bcd66SGeorge Wilson }
32739cddb10SJoshua M. Clulow }
328095bcd66SGeorge Wilson
32939cddb10SJoshua M. Clulow /*
33039cddb10SJoshua M. Clulow * Create vd->vdev_tsd.
33139cddb10SJoshua M. Clulow */
33239cddb10SJoshua M. Clulow vdev_disk_alloc(vd);
33339cddb10SJoshua M. Clulow dvd = vd->vdev_tsd;
334fa9e4066Sahrens
335fa9e4066Sahrens /*
336fa9e4066Sahrens * When opening a disk device, we want to preserve the user's original
337fa9e4066Sahrens * intent. We always want to open the device by the path the user gave
3381724dc7bSJoshua M. Clulow * us, even if it is one of multiple paths to the same device. But we
339fa9e4066Sahrens * also want to be able to survive disks being removed/recabled.
340fa9e4066Sahrens * Therefore the sequence of opening devices is:
341fa9e4066Sahrens *
342afefbcddSeschrock * 1. Try opening the device by path. For legacy pools without the
343afefbcddSeschrock * 'whole_disk' property, attempt to fix the path by appending 's0'.
344fa9e4066Sahrens *
345fa9e4066Sahrens * 2. If the devid of the device matches the stored value, return
346fa9e4066Sahrens * success.
347fa9e4066Sahrens *
348fa9e4066Sahrens * 3. Otherwise, the device may have moved. Try opening the device
349fa9e4066Sahrens * by the devid instead.
350fa9e4066Sahrens */
351fa9e4066Sahrens if (vd->vdev_devid != NULL) {
352fa9e4066Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
353fa9e4066Sahrens &dvd->vd_minor) != 0) {
354fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
355be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
356fa9e4066Sahrens }
357fa9e4066Sahrens }
358fa9e4066Sahrens
359fa9e4066Sahrens error = EINVAL; /* presume failure */
360fa9e4066Sahrens
361095bcd66SGeorge Wilson if (vd->vdev_path != NULL) {
362afefbcddSeschrock
363afefbcddSeschrock if (vd->vdev_wholedisk == -1ULL) {
364fa9e4066Sahrens size_t len = strlen(vd->vdev_path) + 3;
365fa9e4066Sahrens char *buf = kmem_alloc(len, KM_SLEEP);
366fa9e4066Sahrens
367fa9e4066Sahrens (void) snprintf(buf, len, "%ss0", vd->vdev_path);
368fa9e4066Sahrens
36939cddb10SJoshua M. Clulow error = ldi_open_by_name(buf, spa_mode(spa), kcred,
37039cddb10SJoshua M. Clulow &dvd->vd_lh, zfs_li);
37139cddb10SJoshua M. Clulow if (error == 0) {
372afefbcddSeschrock spa_strfree(vd->vdev_path);
373afefbcddSeschrock vd->vdev_path = buf;
374afefbcddSeschrock vd->vdev_wholedisk = 1ULL;
375afefbcddSeschrock } else {
376fa9e4066Sahrens kmem_free(buf, len);
377afefbcddSeschrock }
378afefbcddSeschrock }
379afefbcddSeschrock
38039cddb10SJoshua M. Clulow /*
38139cddb10SJoshua M. Clulow * If we have not yet opened the device, try to open it by the
38239cddb10SJoshua M. Clulow * specified path.
38339cddb10SJoshua M. Clulow */
38439cddb10SJoshua M. Clulow if (error != 0) {
38539cddb10SJoshua M. Clulow error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
38639cddb10SJoshua M. Clulow kcred, &dvd->vd_lh, zfs_li);
38739cddb10SJoshua M. Clulow }
388fa9e4066Sahrens
389fa9e4066Sahrens /*
390fa9e4066Sahrens * Compare the devid to the stored value.
391fa9e4066Sahrens */
392fa9e4066Sahrens if (error == 0 && vd->vdev_devid != NULL &&
393fa9e4066Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) {
394fa9e4066Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
395be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
3968ad4d6ddSJeff Bonwick (void) ldi_close(dvd->vd_lh, spa_mode(spa),
3978ad4d6ddSJeff Bonwick kcred);
398fa9e4066Sahrens dvd->vd_lh = NULL;
399fa9e4066Sahrens }
400fa9e4066Sahrens ddi_devid_free(devid);
401fa9e4066Sahrens }
402afefbcddSeschrock
403afefbcddSeschrock /*
404afefbcddSeschrock * If we succeeded in opening the device, but 'vdev_wholedisk'
405afefbcddSeschrock * is not yet set, then this must be a slice.
406afefbcddSeschrock */
407afefbcddSeschrock if (error == 0 && vd->vdev_wholedisk == -1ULL)
408afefbcddSeschrock vd->vdev_wholedisk = 0;
409fa9e4066Sahrens }
410fa9e4066Sahrens
411fa9e4066Sahrens /*
412fa9e4066Sahrens * If we were unable to open by path, or the devid check fails, open by
413fa9e4066Sahrens * devid instead.
414fa9e4066Sahrens */
415fb02ae02SGeorge Wilson if (error != 0 && vd->vdev_devid != NULL) {
416fa9e4066Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
4178ad4d6ddSJeff Bonwick spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
418fb02ae02SGeorge Wilson }
419fa9e4066Sahrens
4203d7072f8Seschrock /*
4213d7072f8Seschrock * If all else fails, then try opening by physical path (if available)
4223d7072f8Seschrock * or the logical path (if we failed due to the devid check). While not
4233d7072f8Seschrock * as reliable as the devid, this will give us something, and the higher
4243d7072f8Seschrock * level vdev validation will prevent us from opening the wrong device.
4253d7072f8Seschrock */
4263d7072f8Seschrock if (error) {
427fb02ae02SGeorge Wilson if (vd->vdev_devid != NULL)
428fb02ae02SGeorge Wilson validate_devid = B_TRUE;
429fb02ae02SGeorge Wilson
4303d7072f8Seschrock if (vd->vdev_physpath != NULL &&
431deb8317bSMark J Musante (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
4328ad4d6ddSJeff Bonwick error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
4333d7072f8Seschrock kcred, &dvd->vd_lh, zfs_li);
4343d7072f8Seschrock
4353d7072f8Seschrock /*
4363d7072f8Seschrock * Note that we don't support the legacy auto-wholedisk support
4373d7072f8Seschrock * as above. This hasn't been used in a very long time and we
4383d7072f8Seschrock * don't need to propagate its oddities to this edge condition.
4393d7072f8Seschrock */
440095bcd66SGeorge Wilson if (error && vd->vdev_path != NULL)
4418ad4d6ddSJeff Bonwick error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
4428ad4d6ddSJeff Bonwick kcred, &dvd->vd_lh, zfs_li);
4433d7072f8Seschrock }
4443d7072f8Seschrock
445e14bb325SJeff Bonwick if (error) {
446fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
447fa9e4066Sahrens return (error);
448fa9e4066Sahrens }
449fa9e4066Sahrens
450fa9e4066Sahrens /*
451fb02ae02SGeorge Wilson * Now that the device has been successfully opened, update the devid
452fb02ae02SGeorge Wilson * if necessary.
453fb02ae02SGeorge Wilson */
454fb02ae02SGeorge Wilson if (validate_devid && spa_writeable(spa) &&
455fb02ae02SGeorge Wilson ldi_get_devid(dvd->vd_lh, &devid) == 0) {
456fb02ae02SGeorge Wilson if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
457fb02ae02SGeorge Wilson char *vd_devid;
458fb02ae02SGeorge Wilson
459fb02ae02SGeorge Wilson vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
460fb02ae02SGeorge Wilson zfs_dbgmsg("vdev %s: update devid from %s, "
461fb02ae02SGeorge Wilson "to %s", vd->vdev_path, vd->vdev_devid, vd_devid);
462fb02ae02SGeorge Wilson spa_strfree(vd->vdev_devid);
463fb02ae02SGeorge Wilson vd->vdev_devid = spa_strdup(vd_devid);
464fb02ae02SGeorge Wilson ddi_devid_str_free(vd_devid);
465fb02ae02SGeorge Wilson }
466fb02ae02SGeorge Wilson ddi_devid_free(devid);
467fb02ae02SGeorge Wilson }
468fb02ae02SGeorge Wilson
469fb02ae02SGeorge Wilson /*
4703d7072f8Seschrock * Once a device is opened, verify that the physical device path (if
4713d7072f8Seschrock * available) is up to date.
4723d7072f8Seschrock */
4733d7072f8Seschrock if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
4743d7072f8Seschrock ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
4750a4e9518Sgw25295 char *physpath, *minorname;
4760a4e9518Sgw25295
4773d7072f8Seschrock physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4783d7072f8Seschrock minorname = NULL;
4793d7072f8Seschrock if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
4803d7072f8Seschrock ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
4813d7072f8Seschrock (vd->vdev_physpath == NULL ||
4823d7072f8Seschrock strcmp(vd->vdev_physpath, physpath) != 0)) {
4833d7072f8Seschrock if (vd->vdev_physpath)
4843d7072f8Seschrock spa_strfree(vd->vdev_physpath);
4853d7072f8Seschrock (void) strlcat(physpath, ":", MAXPATHLEN);
4863d7072f8Seschrock (void) strlcat(physpath, minorname, MAXPATHLEN);
4873d7072f8Seschrock vd->vdev_physpath = spa_strdup(physpath);
4883d7072f8Seschrock }
4893d7072f8Seschrock if (minorname)
4903d7072f8Seschrock kmem_free(minorname, strlen(minorname) + 1);
4913d7072f8Seschrock kmem_free(physpath, MAXPATHLEN);
4923d7072f8Seschrock }
4933d7072f8Seschrock
49439cddb10SJoshua M. Clulow /*
49539cddb10SJoshua M. Clulow * Register callbacks for the LDI offline event.
49639cddb10SJoshua M. Clulow */
49739cddb10SJoshua M. Clulow if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
49839cddb10SJoshua M. Clulow LDI_EV_SUCCESS) {
49939cddb10SJoshua M. Clulow lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
50039cddb10SJoshua M. Clulow list_insert_tail(&dvd->vd_ldi_cbs, lcb);
50139cddb10SJoshua M. Clulow (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
50239cddb10SJoshua M. Clulow &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
50339cddb10SJoshua M. Clulow }
50439cddb10SJoshua M. Clulow
50539cddb10SJoshua M. Clulow /*
50639cddb10SJoshua M. Clulow * Register callbacks for the LDI degrade event.
50739cddb10SJoshua M. Clulow */
50839cddb10SJoshua M. Clulow if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
50939cddb10SJoshua M. Clulow LDI_EV_SUCCESS) {
51039cddb10SJoshua M. Clulow lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
51139cddb10SJoshua M. Clulow list_insert_tail(&dvd->vd_ldi_cbs, lcb);
51239cddb10SJoshua M. Clulow (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
51339cddb10SJoshua M. Clulow &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
51439cddb10SJoshua M. Clulow }
515095bcd66SGeorge Wilson skip_open:
5163d7072f8Seschrock /*
517fa9e4066Sahrens * Determine the actual size of the device.
518fa9e4066Sahrens */
519fa9e4066Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) {
520fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
521be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
522fa9e4066Sahrens }
523fa9e4066Sahrens
524a5b57771SDan McDonald *max_psize = *psize;
525a5b57771SDan McDonald
526ecc2d604Sbonwick /*
527ecc2d604Sbonwick * Determine the device's minimum transfer size.
528ecc2d604Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE.
529bef6b7d2Swebaker */
530a5b57771SDan McDonald if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
531a5b57771SDan McDonald (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
532a5b57771SDan McDonald capacity = dkmext->dki_capacity - 1;
533a5b57771SDan McDonald blksz = dkmext->dki_lbsize;
534a5b57771SDan McDonald pbsize = dkmext->dki_pbsize;
535a5b57771SDan McDonald } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
536a5b57771SDan McDonald (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
537a5b57771SDan McDonald VDEV_DEBUG(
538a5b57771SDan McDonald "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
539a5b57771SDan McDonald vd->vdev_path);
540a5b57771SDan McDonald capacity = dkm->dki_capacity - 1;
541a5b57771SDan McDonald blksz = dkm->dki_lbsize;
542a5b57771SDan McDonald pbsize = blksz;
543a5b57771SDan McDonald } else {
544a5b57771SDan McDonald VDEV_DEBUG("vdev_disk_open(\"%s\"): "
545a5b57771SDan McDonald "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
546a5b57771SDan McDonald vd->vdev_path, error);
547a5b57771SDan McDonald pbsize = DEV_BSIZE;
548a5b57771SDan McDonald }
549bef6b7d2Swebaker
550bf16b11eSMatthew Ahrens *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
551bef6b7d2Swebaker
5524263d13fSGeorge Wilson if (vd->vdev_wholedisk == 1) {
5534263d13fSGeorge Wilson int wce = 1;
5544263d13fSGeorge Wilson
555a5b57771SDan McDonald if (error == 0) {
5564263d13fSGeorge Wilson /*
557a5b57771SDan McDonald * If we have the capability to expand, we'd have
558a5b57771SDan McDonald * found out via success from DKIOCGMEDIAINFO{,EXT}.
559a5b57771SDan McDonald * Adjust max_psize upward accordingly since we know
560a5b57771SDan McDonald * we own the whole disk now.
561a5b57771SDan McDonald */
562a5b57771SDan McDonald *max_psize += vdev_disk_get_space(vd, capacity, blksz);
563a5b57771SDan McDonald zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
564a5b57771SDan McDonald "max_psize %llu", vd->vdev_path, *psize,
565a5b57771SDan McDonald *max_psize);
566a5b57771SDan McDonald }
567a5b57771SDan McDonald
568a5b57771SDan McDonald /*
569a5b57771SDan McDonald * Since we own the whole disk, try to enable disk write
570a5b57771SDan McDonald * caching. We ignore errors because it's OK if we can't do it.
5714263d13fSGeorge Wilson */
5724263d13fSGeorge Wilson (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
5734263d13fSGeorge Wilson FKIOCTL, kcred, NULL);
5744263d13fSGeorge Wilson }
5754263d13fSGeorge Wilson
576b468a217Seschrock /*
577b468a217Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will
578b468a217Seschrock * try again.
579b468a217Seschrock */
580b468a217Seschrock vd->vdev_nowritecache = B_FALSE;
581b468a217Seschrock
582fa9e4066Sahrens return (0);
583fa9e4066Sahrens }
584fa9e4066Sahrens
585fa9e4066Sahrens static void
vdev_disk_close(vdev_t * vd)586fa9e4066Sahrens vdev_disk_close(vdev_t *vd)
587fa9e4066Sahrens {
588fa9e4066Sahrens vdev_disk_t *dvd = vd->vdev_tsd;
589fa9e4066Sahrens
590095bcd66SGeorge Wilson if (vd->vdev_reopening || dvd == NULL)
591fa9e4066Sahrens return;
592fa9e4066Sahrens
59339cddb10SJoshua M. Clulow if (dvd->vd_minor != NULL) {
594fa9e4066Sahrens ddi_devid_str_free(dvd->vd_minor);
59539cddb10SJoshua M. Clulow dvd->vd_minor = NULL;
59639cddb10SJoshua M. Clulow }
597fa9e4066Sahrens
59839cddb10SJoshua M. Clulow if (dvd->vd_devid != NULL) {
599fa9e4066Sahrens ddi_devid_free(dvd->vd_devid);
60039cddb10SJoshua M. Clulow dvd->vd_devid = NULL;
60139cddb10SJoshua M. Clulow }
602fa9e4066Sahrens
60339cddb10SJoshua M. Clulow if (dvd->vd_lh != NULL) {
6048ad4d6ddSJeff Bonwick (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
60539cddb10SJoshua M. Clulow dvd->vd_lh = NULL;
60639cddb10SJoshua M. Clulow }
607fa9e4066Sahrens
60898d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE;
60939cddb10SJoshua M. Clulow /*
61039cddb10SJoshua M. Clulow * If we closed the LDI handle due to an offline notify from LDI,
61139cddb10SJoshua M. Clulow * don't free vd->vdev_tsd or unregister the callbacks here;
61239cddb10SJoshua M. Clulow * the offline finalize callback or a reopen will take care of it.
61339cddb10SJoshua M. Clulow */
61439cddb10SJoshua M. Clulow if (dvd->vd_ldi_offline)
61539cddb10SJoshua M. Clulow return;
61639cddb10SJoshua M. Clulow
61739cddb10SJoshua M. Clulow vdev_disk_free(vd);
618fa9e4066Sahrens }
619fa9e4066Sahrens
620e7cbe64fSgw25295 int
vdev_disk_physio(vdev_t * vd,caddr_t data,size_t size,uint64_t offset,int flags,boolean_t isdump)621810e43b2SBill Pijewski vdev_disk_physio(vdev_t *vd, caddr_t data,
622810e43b2SBill Pijewski size_t size, uint64_t offset, int flags, boolean_t isdump)
623810e43b2SBill Pijewski {
624810e43b2SBill Pijewski vdev_disk_t *dvd = vd->vdev_tsd;
625810e43b2SBill Pijewski
62639cddb10SJoshua M. Clulow /*
62739cddb10SJoshua M. Clulow * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
62839cddb10SJoshua M. Clulow * Nothing to be done here but return failure.
62939cddb10SJoshua M. Clulow */
63039cddb10SJoshua M. Clulow if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
63139cddb10SJoshua M. Clulow return (EIO);
63239cddb10SJoshua M. Clulow
633810e43b2SBill Pijewski ASSERT(vd->vdev_ops == &vdev_disk_ops);
634810e43b2SBill Pijewski
635810e43b2SBill Pijewski /*
636810e43b2SBill Pijewski * If in the context of an active crash dump, use the ldi_dump(9F)
637810e43b2SBill Pijewski * call instead of ldi_strategy(9F) as usual.
638810e43b2SBill Pijewski */
639810e43b2SBill Pijewski if (isdump) {
640810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL);
641810e43b2SBill Pijewski return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
642810e43b2SBill Pijewski lbtodb(size)));
643810e43b2SBill Pijewski }
644810e43b2SBill Pijewski
645810e43b2SBill Pijewski return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
646810e43b2SBill Pijewski }
647810e43b2SBill Pijewski
648810e43b2SBill Pijewski int
vdev_disk_ldi_physio(ldi_handle_t vd_lh,caddr_t data,size_t size,uint64_t offset,int flags)649810e43b2SBill Pijewski vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
650810e43b2SBill Pijewski size_t size, uint64_t offset, int flags)
651e7cbe64fSgw25295 {
652e7cbe64fSgw25295 buf_t *bp;
653e7cbe64fSgw25295 int error = 0;
654e7cbe64fSgw25295
655e7cbe64fSgw25295 if (vd_lh == NULL)
656be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
657e7cbe64fSgw25295
658e7cbe64fSgw25295 ASSERT(flags & B_READ || flags & B_WRITE);
659e7cbe64fSgw25295
660e7cbe64fSgw25295 bp = getrbuf(KM_SLEEP);
661e7cbe64fSgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
662e7cbe64fSgw25295 bp->b_bcount = size;
663e7cbe64fSgw25295 bp->b_un.b_addr = (void *)data;
664e7cbe64fSgw25295 bp->b_lblkno = lbtodb(offset);
665e7cbe64fSgw25295 bp->b_bufsize = size;
666e7cbe64fSgw25295
667e7cbe64fSgw25295 error = ldi_strategy(vd_lh, bp);
668e7cbe64fSgw25295 ASSERT(error == 0);
669e7cbe64fSgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
670be6fd75aSMatthew Ahrens error = SET_ERROR(EIO);
671e7cbe64fSgw25295 freerbuf(bp);
672e7cbe64fSgw25295
673e7cbe64fSgw25295 return (error);
674e7cbe64fSgw25295 }
675e7cbe64fSgw25295
676fa9e4066Sahrens static void
vdev_disk_io_intr(buf_t * bp)677fa9e4066Sahrens vdev_disk_io_intr(buf_t *bp)
678fa9e4066Sahrens {
67931d7e8faSGeorge Wilson vdev_buf_t *vb = (vdev_buf_t *)bp;
68031d7e8faSGeorge Wilson zio_t *zio = vb->vb_io;
681fa9e4066Sahrens
68251ece835Seschrock /*
68351ece835Seschrock * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
68451ece835Seschrock * Rather than teach the rest of the stack about other error
68551ece835Seschrock * possibilities (EFAULT, etc), we normalize the error value here.
68651ece835Seschrock */
68751ece835Seschrock zio->io_error = (geterror(bp) != 0 ? EIO : 0);
68851ece835Seschrock
68951ece835Seschrock if (zio->io_error == 0 && bp->b_resid != 0)
690be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO);
691fa9e4066Sahrens
69231d7e8faSGeorge Wilson kmem_free(vb, sizeof (vdev_buf_t));
693fa9e4066Sahrens
694e05725b1Sbonwick zio_interrupt(zio);
695fa9e4066Sahrens }
696fa9e4066Sahrens
697fa9e4066Sahrens static void
vdev_disk_ioctl_free(zio_t * zio)698f4a72450SJeff Bonwick vdev_disk_ioctl_free(zio_t *zio)
699f4a72450SJeff Bonwick {
700f4a72450SJeff Bonwick kmem_free(zio->io_vsd, sizeof (struct dk_callback));
701f4a72450SJeff Bonwick }
702f4a72450SJeff Bonwick
70322fe2c88SJonathan Adams static const zio_vsd_ops_t vdev_disk_vsd_ops = {
70422fe2c88SJonathan Adams vdev_disk_ioctl_free,
70522fe2c88SJonathan Adams zio_vsd_default_cksum_report
70622fe2c88SJonathan Adams };
70722fe2c88SJonathan Adams
708f4a72450SJeff Bonwick static void
vdev_disk_ioctl_done(void * zio_arg,int error)709fa9e4066Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
710fa9e4066Sahrens {
711fa9e4066Sahrens zio_t *zio = zio_arg;
712fa9e4066Sahrens
713fa9e4066Sahrens zio->io_error = error;
714fa9e4066Sahrens
715e05725b1Sbonwick zio_interrupt(zio);
716fa9e4066Sahrens }
717fa9e4066Sahrens
718*738f37bcSGeorge Wilson static void
vdev_disk_io_start(zio_t * zio)719fa9e4066Sahrens vdev_disk_io_start(zio_t *zio)
720fa9e4066Sahrens {
721fa9e4066Sahrens vdev_t *vd = zio->io_vd;
722fa9e4066Sahrens vdev_disk_t *dvd = vd->vdev_tsd;
72331d7e8faSGeorge Wilson vdev_buf_t *vb;
724e14bb325SJeff Bonwick struct dk_callback *dkc;
725fa9e4066Sahrens buf_t *bp;
726e14bb325SJeff Bonwick int error;
727fa9e4066Sahrens
72839cddb10SJoshua M. Clulow /*
72939cddb10SJoshua M. Clulow * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
73039cddb10SJoshua M. Clulow * Nothing to be done here but return failure.
73139cddb10SJoshua M. Clulow */
73239cddb10SJoshua M. Clulow if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
73339cddb10SJoshua M. Clulow zio->io_error = ENXIO;
734*738f37bcSGeorge Wilson zio_interrupt(zio);
735*738f37bcSGeorge Wilson return;
73639cddb10SJoshua M. Clulow }
73739cddb10SJoshua M. Clulow
738fa9e4066Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) {
739fa9e4066Sahrens /* XXPOLICY */
7400a4e9518Sgw25295 if (!vdev_readable(vd)) {
741be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO);
742*738f37bcSGeorge Wilson zio_interrupt(zio);
743*738f37bcSGeorge Wilson return;
744fa9e4066Sahrens }
745fa9e4066Sahrens
746fa9e4066Sahrens switch (zio->io_cmd) {
747fa9e4066Sahrens
748fa9e4066Sahrens case DKIOCFLUSHWRITECACHE:
749fa9e4066Sahrens
750a2eea2e1Sahrens if (zfs_nocacheflush)
751a2eea2e1Sahrens break;
752a2eea2e1Sahrens
753b468a217Seschrock if (vd->vdev_nowritecache) {
754be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENOTSUP);
755b468a217Seschrock break;
756b468a217Seschrock }
757b468a217Seschrock
758e14bb325SJeff Bonwick zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
75922fe2c88SJonathan Adams zio->io_vsd_ops = &vdev_disk_vsd_ops;
760e14bb325SJeff Bonwick
761e14bb325SJeff Bonwick dkc->dkc_callback = vdev_disk_ioctl_done;
762e14bb325SJeff Bonwick dkc->dkc_flag = FLUSH_VOLATILE;
763e14bb325SJeff Bonwick dkc->dkc_cookie = zio;
764fa9e4066Sahrens
765fa9e4066Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
766e14bb325SJeff Bonwick (uintptr_t)dkc, FKIOCTL, kcred, NULL);
767fa9e4066Sahrens
768fa9e4066Sahrens if (error == 0) {
769fa9e4066Sahrens /*
770fa9e4066Sahrens * The ioctl will be done asychronously,
771fa9e4066Sahrens * and will call vdev_disk_ioctl_done()
772fa9e4066Sahrens * upon completion.
773fa9e4066Sahrens */
774*738f37bcSGeorge Wilson return;
775e05725b1Sbonwick }
776e05725b1Sbonwick
777e05725b1Sbonwick if (error == ENOTSUP || error == ENOTTY) {
778b468a217Seschrock /*
779d5782879Smishra * If we get ENOTSUP or ENOTTY, we know that
780d5782879Smishra * no future attempts will ever succeed.
781d5782879Smishra * In this case we set a persistent bit so
782d5782879Smishra * that we don't bother with the ioctl in the
783d5782879Smishra * future.
784b468a217Seschrock */
785b468a217Seschrock vd->vdev_nowritecache = B_TRUE;
786fa9e4066Sahrens }
787fa9e4066Sahrens zio->io_error = error;
788b468a217Seschrock
789fa9e4066Sahrens break;
790fa9e4066Sahrens
791fa9e4066Sahrens default:
792be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENOTSUP);
793fa9e4066Sahrens }
794fa9e4066Sahrens
795*738f37bcSGeorge Wilson zio_execute(zio);
796*738f37bcSGeorge Wilson return;
797fa9e4066Sahrens }
798fa9e4066Sahrens
79931d7e8faSGeorge Wilson vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
800fa9e4066Sahrens
80131d7e8faSGeorge Wilson vb->vb_io = zio;
80231d7e8faSGeorge Wilson bp = &vb->vb_buf;
803fa9e4066Sahrens
804fa9e4066Sahrens bioinit(bp);
805e14bb325SJeff Bonwick bp->b_flags = B_BUSY | B_NOCACHE |
8068956713aSEric Schrock (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
8078956713aSEric Schrock if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
8088956713aSEric Schrock bp->b_flags |= B_FAILFAST;
809fa9e4066Sahrens bp->b_bcount = zio->io_size;
810fa9e4066Sahrens bp->b_un.b_addr = zio->io_data;
811fa9e4066Sahrens bp->b_lblkno = lbtodb(zio->io_offset);
812fa9e4066Sahrens bp->b_bufsize = zio->io_size;
813fa9e4066Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr;
814fa9e4066Sahrens
815fa9e4066Sahrens /* ldi_strategy() will return non-zero only on programming errors */
816e14bb325SJeff Bonwick VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
817fa9e4066Sahrens }
818fa9e4066Sahrens
819e14bb325SJeff Bonwick static void
vdev_disk_io_done(zio_t * zio)820fa9e4066Sahrens vdev_disk_io_done(zio_t *zio)
821fa9e4066Sahrens {
822e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd;
823ea8dc4b6Seschrock
8243d7072f8Seschrock /*
8253d7072f8Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
8263d7072f8Seschrock * the device has been removed. If this is the case, then we trigger an
8270a4e9518Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and
8281f7ad2e1Sgw25295 * make sure it's still accessible.
8293d7072f8Seschrock */
8301d713200SEric Schrock if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
8310a4e9518Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd;
832e14bb325SJeff Bonwick int state = DKIO_NONE;
8330a4e9518Sgw25295
834e14bb325SJeff Bonwick if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
835e14bb325SJeff Bonwick FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
8361d713200SEric Schrock /*
8371d713200SEric Schrock * We post the resource as soon as possible, instead of
8381d713200SEric Schrock * when the async removal actually happens, because the
8391d713200SEric Schrock * DE is using this information to discard previous I/O
8401d713200SEric Schrock * errors.
8411d713200SEric Schrock */
8421d713200SEric Schrock zfs_post_remove(zio->io_spa, vd);
8433d7072f8Seschrock vd->vdev_remove_wanted = B_TRUE;
8443d7072f8Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
84598d1cbfeSGeorge Wilson } else if (!vd->vdev_delayed_close) {
84698d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_TRUE;
84751ece835Seschrock }
8483d7072f8Seschrock }
8493d7072f8Seschrock }
8503d7072f8Seschrock
851fa9e4066Sahrens vdev_ops_t vdev_disk_ops = {
852fa9e4066Sahrens vdev_disk_open,
853fa9e4066Sahrens vdev_disk_close,
854fa9e4066Sahrens vdev_default_asize,
855fa9e4066Sahrens vdev_disk_io_start,
856fa9e4066Sahrens vdev_disk_io_done,
857fa9e4066Sahrens NULL,
858dcba9f3fSGeorge Wilson vdev_disk_hold,
859dcba9f3fSGeorge Wilson vdev_disk_rele,
860fa9e4066Sahrens VDEV_TYPE_DISK, /* name of this vdev type */
861fa9e4066Sahrens B_TRUE /* leaf vdev */
862fa9e4066Sahrens };
863e7cbe64fSgw25295
864e7cbe64fSgw25295 /*
865051aabe6Staylor * Given the root disk device devid or pathname, read the label from
866051aabe6Staylor * the device, and construct a configuration nvlist.
867e7cbe64fSgw25295 */
868f940fbb1SLin Ling int
vdev_disk_read_rootlabel(char * devpath,char * devid,nvlist_t ** config)869f940fbb1SLin Ling vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
870e7cbe64fSgw25295 {
871e7cbe64fSgw25295 ldi_handle_t vd_lh;
872e7cbe64fSgw25295 vdev_label_t *label;
873e7cbe64fSgw25295 uint64_t s, size;
874e7cbe64fSgw25295 int l;
875051aabe6Staylor ddi_devid_t tmpdevid;
876f4565e39SLin Ling int error = -1;
877051aabe6Staylor char *minor_name;
878e7cbe64fSgw25295
879e7cbe64fSgw25295 /*
880e7cbe64fSgw25295 * Read the device label and build the nvlist.
881e7cbe64fSgw25295 */
882f4565e39SLin Ling if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
883051aabe6Staylor &minor_name) == 0) {
884051aabe6Staylor error = ldi_open_by_devid(tmpdevid, minor_name,
8858ad4d6ddSJeff Bonwick FREAD, kcred, &vd_lh, zfs_li);
886051aabe6Staylor ddi_devid_free(tmpdevid);
887051aabe6Staylor ddi_devid_str_free(minor_name);
888051aabe6Staylor }
889051aabe6Staylor
890f4565e39SLin Ling if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
891f4565e39SLin Ling zfs_li)))
892f940fbb1SLin Ling return (error);
893e7cbe64fSgw25295
894bf82a41bSeschrock if (ldi_get_size(vd_lh, &s)) {
895bf82a41bSeschrock (void) ldi_close(vd_lh, FREAD, kcred);
896be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
897bf82a41bSeschrock }
898e7cbe64fSgw25295
899e7cbe64fSgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
900e7cbe64fSgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
901e7cbe64fSgw25295
90217f1e64aSEric Taylor *config = NULL;
903e7cbe64fSgw25295 for (l = 0; l < VDEV_LABELS; l++) {
904e7cbe64fSgw25295 uint64_t offset, state, txg = 0;
905e7cbe64fSgw25295
906e7cbe64fSgw25295 /* read vdev label */
907e7cbe64fSgw25295 offset = vdev_label_offset(size, l, 0);
908810e43b2SBill Pijewski if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
9092264ca7fSLin Ling VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
910e7cbe64fSgw25295 continue;
911e7cbe64fSgw25295
912e7cbe64fSgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
913f940fbb1SLin Ling sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
914f940fbb1SLin Ling *config = NULL;
915e7cbe64fSgw25295 continue;
916e7cbe64fSgw25295 }
917e7cbe64fSgw25295
918f940fbb1SLin Ling if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
919e7cbe64fSgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) {
920f940fbb1SLin Ling nvlist_free(*config);
921f940fbb1SLin Ling *config = NULL;
922e7cbe64fSgw25295 continue;
923e7cbe64fSgw25295 }
924e7cbe64fSgw25295
925f940fbb1SLin Ling if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
926e7cbe64fSgw25295 &txg) != 0 || txg == 0) {
927f940fbb1SLin Ling nvlist_free(*config);
928f940fbb1SLin Ling *config = NULL;
929e7cbe64fSgw25295 continue;
930e7cbe64fSgw25295 }
931e7cbe64fSgw25295
932e7cbe64fSgw25295 break;
933e7cbe64fSgw25295 }
934e7cbe64fSgw25295
935e7cbe64fSgw25295 kmem_free(label, sizeof (vdev_label_t));
936bf82a41bSeschrock (void) ldi_close(vd_lh, FREAD, kcred);
93717f1e64aSEric Taylor if (*config == NULL)
938be6fd75aSMatthew Ahrens error = SET_ERROR(EIDRM);
939bf82a41bSeschrock
940f940fbb1SLin Ling return (error);
941e7cbe64fSgw25295 }
942