/*
 * Public interface for creating an IP stream with shared conn_t
 * Handles multiple callers in parallel by using conn_lock.
 * Note that we allocate the helper stream without any locks, which means
 * we might need to free it if we had two threads doing this concurrently
 * for the conn_t.
 */
int
ip_create_helper_stream(conn_t *connp, ldi_ident_t li)
{
	ip_helper_stream_info_t *helper;
	int	error;
	int	ret;

	ASSERT(!servicing_interrupt());

	if (connp->conn_helper_info != NULL) {
		/* Already allocated */
		return (0);
	}

	error = 0;
	helper = kmem_alloc(sizeof (ip_helper_stream_info_t), KM_SLEEP);

	/*
	 * open ip device via the layered interface.
	 * pass in kcred as some threads do not have the
	 * priviledge to open /dev/ip and the check in
	 * secpolicy_spec_open() will fail the open
	 */
	error = ldi_open_by_name((connp->conn_family == AF_INET6 ? DEV_IP6 :
	    DEV_IP), IP_HELPER_STR, kcred, &helper->iphs_handle, li);

	if (error != 0) {
		kmem_free(helper, sizeof (ip_helper_stream_info_t));
		return (error);
	}
	/* Make sure we are the only one */
	mutex_enter(&connp->conn_lock);
	if (connp->conn_helper_info != NULL) {
		/* Some other thread won - discard this stream */
		mutex_exit(&connp->conn_lock);
		(void) ldi_close(helper->iphs_handle, 0, kcred);
		kmem_free(helper, sizeof (ip_helper_stream_info_t));
		return (0);
	}
	connp->conn_helper_info = helper;
	/*
	 * Share connp with the helper stream. We hold conn_lock across this
	 * operation.
	 */
	error = ldi_ioctl(helper->iphs_handle, SIOCSQPTR, (intptr_t)connp,
	    FKIOCTL, kcred, &ret);

	if (error != 0) {
		/*
		 * Passing in a zero flag indicates that an error
		 * occured and stream was not shared
		 */
		(void) ldi_close(helper->iphs_handle, 0, kcred);
		kmem_free(helper, sizeof (ip_helper_stream_info_t));
		connp->conn_helper_info = NULL;
	}
	mutex_exit(&connp->conn_lock);
	return (error);
}
Ejemplo n.º 2
0
static void
vdev_disk_close(vdev_t *vd)
{
	vdev_disk_t *dvd = vd->vdev_tsd;
	vdev_disk_ldi_cb_t *lcb;

	if (vd->vdev_reopening || dvd == NULL)
		return;

	if (dvd->vd_minor != NULL) {
		ddi_devid_str_free(dvd->vd_minor);
		dvd->vd_minor = NULL;
	}

	if (dvd->vd_devid != NULL) {
		ddi_devid_free(dvd->vd_devid);
		dvd->vd_devid = NULL;
	}

	if (dvd->vd_lh != NULL) {
		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
		dvd->vd_lh = NULL;
	}

	vd->vdev_delayed_close = B_FALSE;
	/*
	 * If we closed the LDI handle due to an offline notify from LDI,
	 * don't free vd->vdev_tsd or unregister the callbacks here;
	 * the offline finalize callback or a reopen will take care of it.
	 */
	if (dvd->vd_ldi_offline)
		return;

	vdev_disk_free(vd);
}
Ejemplo n.º 3
0
static int
_raw_close(dev_t fd)
{
	struct cred *cred;
	raw_dev_t *cdi;
	int rc;
	int cd;

	mutex_enter(&_nsc_raw_lock);

	if ((cd = __raw_get_cd(fd)) == -1 || !_nsc_raw_files[cd].in_use) {
		mutex_exit(&_nsc_raw_lock);
		return (EIO);
	}

	cdi = &_nsc_raw_files[cd];

	cred = ddi_get_cred();

	rc = ldi_close(cdi->lh, FREAD|FWRITE, cred);
	if (rc != 0) {
		mutex_exit(&_nsc_raw_lock);
		return (rc);
	}

	kmem_free(cdi->path, cdi->plen);

	bzero(cdi, sizeof (*cdi));

	mutex_exit(&_nsc_raw_lock);

	return (0);
}
Ejemplo n.º 4
0
/*ARGSUSED*/
static int
lx_ptm_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
	ldi_handle_t	lh;
	major_t		maj;
	minor_t		min, lastmin;
	uint_t		index;
	int		err;

	index = DEVT_TO_INDEX(dev);

	/*
	 * we must cleanup all the state associated with this major/minor
	 * terminal pair before actually closing the ptm master device.
	 * this is required because once the close of the ptm device is
	 * complete major/minor terminal pair is immediatly available for
	 * re-use in any zone.
	 */

	/* free up our saved reference for this layered handle */
	lh = lx_ptm_lh_remove(index);

	/* unconfigure autopush for the associated terminal slave device */
	maj = lps.lps_pts_major;
	min = index;
	lastmin = 0;
	do {
		/*
		 * we loop here because we don't want to release this ptm
		 * node if autopush can't be disabled on the associated
		 * slave device because then bad things could happen if
		 * another brand were to get this terminal allocated
		 * to them.
		 *
		 * XXX should we ever give up?
		 */
		err = kstr_autopush(CLR_AUTOPUSH, &maj, &min, &lastmin,
		    0, NULL);
	} while (err != 0);

	err = ldi_close(lh, flag, credp);

	/*
	 * note that we don't have to bother with changing the permissions
	 * on the associated slave device here.  the reason is that no one
	 * can actually open the device untill it's associated master
	 * device is re-opened, which will result in the permissions on
	 * it being reset.
	 */
	return (err);
}
Ejemplo n.º 5
0
int
rds_close_transport_driver()
{
	ASSERT(rds_transport_ops != NULL);

	rw_enter(&rds_transport_lock, RW_WRITER);
	if (rds_transport_handle != NULL) {
		rds_transport_ops->rds_transport_close_ib();
		(void) ldi_close(rds_transport_handle, FNDELAY, kcred);
		rds_transport_handle = NULL;
	}
	rw_exit(&rds_transport_lock);

	return (0);
}
Ejemplo n.º 6
0
/* Not static so zfs_osx.cpp can call it on device removal */
void
vdev_disk_close(vdev_t *vd)
{
	vdev_disk_t *dvd = vd->vdev_tsd;

	if (vd->vdev_reopening || dvd == NULL)
		return;

#ifdef illumos
	if (dvd->vd_minor != NULL) {
		ddi_devid_str_free(dvd->vd_minor);
		dvd->vd_minor = NULL;
	}

	if (dvd->vd_devid != NULL) {
		ddi_devid_free(dvd->vd_devid);
		dvd->vd_devid = NULL;
	}

	if (dvd->vd_lh != NULL) {
		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
		dvd->vd_lh = NULL;
	}
#endif

#ifdef __APPLE__
	if (dvd->vd_devvp != NULL) {
		/* vnode_close() can stall during removal, so clear vd_devvp now */
		struct vnode *vp = dvd->vd_devvp;
		dvd->vd_devvp = NULL;
		(void) thread_create(NULL, 0, vdev_disk_close_thread,
							 vp, 0, &p0,
							 TS_RUN, minclsyspri);
	}
#endif

	vd->vdev_delayed_close = B_FALSE;

	/*
	 * If we closed the LDI handle due to an offline notify from LDI,
	 * don't free vd->vdev_tsd or unregister the callbacks here;
	 * the offline finalize callback or a reopen will take care of it.
	 */
	if (dvd->vd_offline)
		return;

	vdev_disk_free(vd);
}
/*
 * Public interface for freeing IP helper stream
 * Caller must ensure no concurrent use of the conn_t, which is normally
 * done by calling this from the close routine when the conn_t is quiesced.
 */
void
ip_free_helper_stream(conn_t *connp)
{
	ASSERT(!servicing_interrupt());

	if (connp->conn_helper_info == NULL)
		return;

	ASSERT(connp->conn_helper_info->iphs_handle != NULL);

	connp->conn_helper_info->iphs_rq->q_ptr =
	    connp->conn_helper_info->iphs_wq->q_ptr =
	    connp->conn_helper_info->iphs_minfo;
	(void) ldi_close(connp->conn_helper_info->iphs_handle,
	    IP_HELPER_STR, kcred);
	kmem_free(connp->conn_helper_info, sizeof (ip_helper_stream_info_t));
	connp->conn_helper_info = NULL;
}
Ejemplo n.º 8
0
static void
vdev_disk_close(vdev_t *vd)
{
	vdev_disk_t *dvd = vd->vdev_tsd;

	if (vd->vdev_reopening || dvd == NULL)
		return;

	if (dvd->vd_minor != NULL)
		ddi_devid_str_free(dvd->vd_minor);

	if (dvd->vd_devid != NULL)
		ddi_devid_free(dvd->vd_devid);

	if (dvd->vd_lh != NULL)
		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);

	vd->vdev_delayed_close = B_FALSE;
	kmem_free(dvd, sizeof (vdev_disk_t));
	vd->vdev_tsd = NULL;
}
Ejemplo n.º 9
0
int
rds_open_transport_driver()
{
	int ret = 0;

	rw_enter(&rds_transport_lock, RW_WRITER);
	if (rds_transport_handle != NULL) {
		/*
		 * Someone beat us to it.
		 */
		goto done;
	}

	if (ibt_hw_is_present() == 0) {
		ret = ENODEV;
		goto done;
	}

	if (rds_li == NULL) {
		ret = EPROTONOSUPPORT;
		goto done;
	}

	ret = ldi_open_by_name("/devices/ib/rdsib@0:rdsib",
	    FREAD | FWRITE, kcred, &rds_transport_handle, rds_li);
	if (ret != 0) {
		ret = EPROTONOSUPPORT;
		rds_transport_handle = NULL;
		goto done;
	}

	ret = rds_transport_ops->rds_transport_open_ib();
	if (ret != 0) {
		(void) ldi_close(rds_transport_handle, FNDELAY, kcred);
		rds_transport_handle = NULL;
	}
done:
	rw_exit(&rds_transport_lock);
	return (ret);
}
Ejemplo n.º 10
0
static void
vdev_disk_close_impl(vdev_t *vd, boolean_t ldi_offline)
{
	vdev_disk_t *dvd;

	rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
	dvd = vd->vdev_tsd;

	if (vd->vdev_reopening || dvd == NULL)
		goto out;

	if (dvd->vd_minor != NULL) {
		ddi_devid_str_free(dvd->vd_minor);
		dvd->vd_minor = NULL;
	}

	if (dvd->vd_devid != NULL) {
		ddi_devid_free(dvd->vd_devid);
		dvd->vd_devid = NULL;
	}

	if (dvd->vd_lh != NULL) {
		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
		dvd->vd_lh = NULL;
	}

	vd->vdev_delayed_close = B_FALSE;
	/*
	 * If we closed the LDI handle due to an offline notify from LDI,
	 * don't free vd->vdev_tsd or unregister the callbacks here;
	 * the offline finalize callback or a reopen will take care of it.
	 */
	if (!ldi_offline)
		vdev_disk_free_locked(vd);
out:
	rw_exit(&vd->vdev_tsd_lock);
}
Ejemplo n.º 11
0
/* ARGSUSED */
static int
_raw_open(char *path, int flag, blind_t *cdp, void *iodev)
{
	struct cred *cred;
	raw_dev_t *cdi = NULL;
	char *spath;
	dev_t rdev;
	int rc, cd, the_cd;
	int plen;
	ldi_ident_t	li;

	if (proc_nskernd == NULL) {
		cmn_err(CE_WARN, "nskern: no nskernd daemon running!");
		return (ENXIO);
	}

	if (_nsc_raw_maxdevs == 0) {
		cmn_err(CE_WARN, "nskern: _raw_open() before _nsc_init_raw()!");
		return (ENXIO);
	}

	plen = strlen(path) + 1;
	spath = kmem_alloc(plen, KM_SLEEP);
	if (spath == NULL) {
		cmn_err(CE_WARN,
		    "nskern: unable to alloc memory in _raw_open()");
		return (ENOMEM);
	}

	(void) strcpy(spath, path);

	/*
	 * Lookup the vnode to extract the dev_t info,
	 * then release the vnode.
	 */
	if ((rdev = ldi_get_dev_t_from_path(path)) == 0) {
		kmem_free(spath, plen);
		return (ENXIO);
	}

	/*
	 * See if this device is already opened
	 */

	the_cd = -1;

	mutex_enter(&_nsc_raw_lock);

	for (cd = 0, cdi = _nsc_raw_files; cd < fd_hwm; cd++, cdi++) {
		if (rdev == cdi->rdev) {
			the_cd = cd;
			break;
		} else if (the_cd == -1 && !cdi->in_use)
			the_cd = cd;
	}

	if (the_cd == -1) {
		if (fd_hwm < _nsc_raw_maxdevs)
			the_cd = fd_hwm++;
		else {
			mutex_exit(&_nsc_raw_lock);
			cmn_err(CE_WARN, "_raw_open: too many open devices");
			kmem_free(spath, plen);
			return (EIO);
		}
	}

	cdi = &_nsc_raw_files[the_cd];
	if (cdi->in_use) {
		/* already set up - just return */
		mutex_exit(&_nsc_raw_lock);
		*cdp = (blind_t)cdi->rdev;
		kmem_free(spath, plen);
		return (0);
	}

	cdi->partition = -1;
	cdi->size = (uint64_t)0;
	cdi->rdev = rdev;
	cdi->path = spath;
	cdi->plen = plen;

	cred = ddi_get_cred();

	/*
	 * Layered driver
	 *
	 * We use xxx_open_by_dev() since this guarantees that a
	 * specfs vnode is created and used, not a standard filesystem
	 * vnode. This is necessary since in a cluster PXFS will block
	 * vnode operations during switchovers, so we have to use the
	 * underlying specfs vnode not the PXFS vnode.
	 *
	 */

	if ((rc = ldi_ident_from_dev(cdi->rdev, &li)) == 0) {
		rc = ldi_open_by_dev(&cdi->rdev,
		    OTYP_BLK, FREAD|FWRITE, cred, &cdi->lh, li);
	}
	if (rc != 0) {
		cdi->lh = NULL;
		goto failed;
	}

	/*
	 * grab the major_t related information
	 */

	cdi->major = _raw_get_maj_info(getmajor(rdev));
	if (cdi->major == NULL) {
		/* Out of memory */
		cmn_err(CE_WARN,
		    "_raw_open: cannot alloc major number structure");

		rc = ENOMEM;
		goto failed;
	}

	*cdp = (blind_t)cdi->rdev;
	cdi->in_use++;

	mutex_exit(&_nsc_raw_lock);

	return (rc);

failed:

	if (cdi->lh)
		(void) ldi_close(cdi->lh, FWRITE|FREAD, cred);

	bzero(cdi, sizeof (*cdi));

	mutex_exit(&_nsc_raw_lock);

	kmem_free(spath, plen);
	return (rc);
}
Ejemplo n.º 12
0
static int
vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
    uint64_t *ashift)
{
	spa_t *spa = vd->vdev_spa;
	vdev_disk_t *dvd = vd->vdev_tsd;
	ldi_ev_cookie_t ecookie;
	vdev_disk_ldi_cb_t *lcb;
	union {
		struct dk_minfo_ext ude;
		struct dk_minfo ud;
	} dks;
	struct dk_minfo_ext *dkmext = &dks.ude;
	struct dk_minfo *dkm = &dks.ud;
	int error;
/* XXX Apple - must leave devid unchanged */
#ifdef illumos
	dev_t dev;
	int otyp;
	boolean_t validate_devid = B_FALSE;
	ddi_devid_t devid;
#endif
	uint64_t capacity = 0, blksz = 0, pbsize;
#ifdef __APPLE__
	int isssd;
#endif

	/*
	 * We must have a pathname, and it must be absolute.
	 */
	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
		return (SET_ERROR(EINVAL));
	}

	/*
	 * Reopen the device if it's not currently open. Otherwise,
	 * just update the physical size of the device.
	 */
	if (dvd != NULL) {
		if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
			/*
			 * If we are opening a device in its offline notify
			 * context, the LDI handle was just closed. Clean
			 * up the LDI event callbacks and free vd->vdev_tsd.
			 */
			vdev_disk_free(vd);
		} else {
			ASSERT(vd->vdev_reopening);
			goto skip_open;
		}
	}

	/*
	 * Create vd->vdev_tsd.
	 */
	vdev_disk_alloc(vd);
	dvd = vd->vdev_tsd;

	/*
	 * When opening a disk device, we want to preserve the user's original
	 * intent.  We always want to open the device by the path the user gave
	 * us, even if it is one of multiple paths to the same device.  But we
	 * also want to be able to survive disks being removed/recabled.
	 * Therefore the sequence of opening devices is:
	 *
	 * 1. Try opening the device by path.  For legacy pools without the
	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
	 *
	 * 2. If the devid of the device matches the stored value, return
	 *    success.
	 *
	 * 3. Otherwise, the device may have moved.  Try opening the device
	 *    by the devid instead.
	 */
/*
 * XXX We must not set or modify the devid as this check would prevent
 * import on Solaris/illumos.
 */
#ifdef illumos
	if (vd->vdev_devid != NULL) {
		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
		    &dvd->vd_minor) != 0) {
			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
			vdev_dbgmsg(vd, "vdev_disk_open: invalid "
			    "vdev_devid '%s'", vd->vdev_devid);
			return (SET_ERROR(EINVAL));
		}
	}
#endif

	error = EINVAL;		/* presume failure */

	if (vd->vdev_path != NULL) {

/*
 * XXX This assumes that if vdev_path refers to a device path /dev/dsk/cNtNdN,
 * then the whole disk can be found by slice 0 at path /dev/dsk/cNtNdNs0.
 */
#ifdef illumos
		if (vd->vdev_wholedisk == -1ULL) {
			size_t len = strlen(vd->vdev_path) + 3;
			char *buf = kmem_alloc(len, KM_SLEEP);

			(void) snprintf(buf, len, "%ss0", vd->vdev_path);

			error = ldi_open_by_name(buf, spa_mode(spa), kcred,
			    &dvd->vd_lh, zfs_li);
			if (error == 0) {
				spa_strfree(vd->vdev_path);
				vd->vdev_path = buf;
				vd->vdev_wholedisk = 1ULL;
			} else {
				kmem_free(buf, len);
			}
		}
#endif

		/*
		 * If we have not yet opened the device, try to open it by the
		 * specified path.
		 */
		if (error != 0) {
			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);
		}

/* XXX Apple - must leave devid unchanged */
#ifdef illumos
		/*
		 * Compare the devid to the stored value.
		 */
		if (error == 0 && vd->vdev_devid != NULL &&
		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
				error = SET_ERROR(EINVAL);
				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
				    kcred);
				dvd->vd_lh = NULL;
			}
			ddi_devid_free(devid);
		}
#endif

		/*
		 * If we succeeded in opening the device, but 'vdev_wholedisk'
		 * is not yet set, then this must be a slice.
		 */
		if (error == 0 && vd->vdev_wholedisk == -1ULL)
			vd->vdev_wholedisk = 0;
	}

/* XXX Apple - must leave devid unchanged */
#ifdef illumos
	/*
	 * If we were unable to open by path, or the devid check fails, open by
	 * devid instead.
	 */
	if (error != 0 && vd->vdev_devid != NULL) {
		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
	}
#endif

	/*
	 * If all else fails, then try opening by physical path (if available)
	 * or the logical path (if we failed due to the devid check).  While not
	 * as reliable as the devid, this will give us something, and the higher
	 * level vdev validation will prevent us from opening the wrong device.
	 */
	if (error) {
/* XXX Apple - must leave devid unchanged */
#ifdef illumos
		if (vd->vdev_devid != NULL)
			validate_devid = B_TRUE;
#endif

/* XXX Apple to do - make ddi_ interface for this, using IORegistry path */
#ifdef illumos
		if (vd->vdev_physpath != NULL &&
		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);
#endif

		/*
		 * Note that we don't support the legacy auto-wholedisk support
		 * as above.  This hasn't been used in a very long time and we
		 * don't need to propagate its oddities to this edge condition.
		 */
		if (error && vd->vdev_path != NULL)
			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);
	}

	if (error) {
		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
		vdev_dbgmsg(vd, "vdev_disk_open: failed to open [error=%d]",
		    error);
		return (error);
	}

/*
 * XXX Apple - We must not set or modify the devid. Import on Solaris/illumos
 * expects a valid devid and fails if it cannot be decoded.
 */
#ifdef illumos
	/*
	 * Now that the device has been successfully opened, update the devid
	 * if necessary.
	 */
	if (validate_devid && spa_writeable(spa) &&
	    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
		if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
			char *vd_devid;

			vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
			vdev_dbgmsg(vd, "vdev_disk_open: update devid from "
			    "'%s' to '%s'", vd->vdev_devid, vd_devid);
			spa_strfree(vd->vdev_devid);
			vd->vdev_devid = spa_strdup(vd_devid);
			ddi_devid_str_free(vd_devid);
		}
		ddi_devid_free(devid);
	}
#endif

/* XXX Apple to do, needs IORegistry physpath interface */
#ifdef illumos
	/*
	 * Once a device is opened, verify that the physical device path (if
	 * available) is up to date.
	 */
	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
		char *physpath, *minorname;

		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
		minorname = NULL;
		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
		    (vd->vdev_physpath == NULL ||
		    strcmp(vd->vdev_physpath, physpath) != 0)) {
			if (vd->vdev_physpath)
				spa_strfree(vd->vdev_physpath);
			(void) strlcat(physpath, ":", MAXPATHLEN);
			(void) strlcat(physpath, minorname, MAXPATHLEN);
			vd->vdev_physpath = spa_strdup(physpath);
		}
		if (minorname)
			kmem_free(minorname, strlen(minorname) + 1);
		kmem_free(physpath, MAXPATHLEN);
	}
#endif

	/*
	 * Register callbacks for the LDI offline event.
	 */
	if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
	    LDI_EV_SUCCESS) {
		lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
		list_insert_tail(&dvd->vd_ldi_cbs, lcb);
		(void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
		    &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
	}

/* XXX Apple to do - we could support the degrade event, or just no-op */
#ifdef illumos
	/*
	 * Register callbacks for the LDI degrade event.
	 */
	if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
	    LDI_EV_SUCCESS) {
		lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
		list_insert_tail(&dvd->vd_ldi_cbs, lcb);
		(void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
		    &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
	}
#endif

#if 0
	int len = MAXPATHLEN;
	if (vn_getpath(devvp, dvd->vd_readlinkname, &len) == 0) {
		dprintf("ZFS: '%s' resolved name is '%s'\n",
			   vd->vdev_path, dvd->vd_readlinkname);
	} else {
		dvd->vd_readlinkname[0] = 0;
	}
#endif

skip_open:
	/*
	 * Determine the actual size of the device.
	 */
	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
		vdev_dbgmsg(vd, "vdev_disk_open: failed to get size");
		return (SET_ERROR(EINVAL));
	}

	*max_psize = *psize;

	/*
	 * Determine the device's minimum transfer size.
	 * If the ioctl isn't supported, assume DEV_BSIZE.
	 */
	if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
	    (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
		capacity = dkmext->dki_capacity - 1;
		blksz = dkmext->dki_lbsize;
		pbsize = dkmext->dki_pbsize;
	} else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
	    (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
		VDEV_DEBUG(
		    "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
		    vd->vdev_path);
		capacity = dkm->dki_capacity - 1;
		blksz = dkm->dki_lbsize;
		pbsize = blksz;
	} else {
		VDEV_DEBUG("vdev_disk_open(\"%s\"): "
		    "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
		    vd->vdev_path, error);
		pbsize = DEV_BSIZE;
	}

	*ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;

/* XXX Now that we opened the device, determine if it is a whole disk. */
#ifdef __APPLE__
	/*
	 * XXX Apple to do - provide an ldi_ mechanism
	 * to report whether this is a whole disk or a
	 * partition.
	 * Return 0 (no), 1 (yes), or -1 (error).
	 */
//	vd->vdev_wholedisk = ldi_is_wholedisk(vd->vd_lh);
#endif

	if (vd->vdev_wholedisk == 1) {
		int wce = 1;

/* Gets information about the disk if it has GPT partitions */
#ifdef illumos
		if (error == 0) {
			/*
			 * If we have the capability to expand, we'd have
			 * found out via success from DKIOCGMEDIAINFO{,EXT}.
			 * Adjust max_psize upward accordingly since we know
			 * we own the whole disk now.
			 */
			*max_psize = capacity * blksz;
		}
#endif

		/*
		 * Since we own the whole disk, try to enable disk write
		 * caching.  We ignore errors because it's OK if we can't do it.
		 */
		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
		    FKIOCTL, kcred, NULL);
	}

	/*
	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
	 * try again.
	 */
	vd->vdev_nowritecache = B_FALSE;

#ifdef __APPLE__
	/* Inform the ZIO pipeline that we are non-rotational */
	vd->vdev_nonrot = B_FALSE;
	if (ldi_ioctl(dvd->vd_lh, DKIOCISSOLIDSTATE, (intptr_t)&isssd,
	    FKIOCTL, kcred, NULL) == 0) {
		vd->vdev_nonrot = (isssd ? B_TRUE : B_FALSE);
	}
#endif //__APPLE__

	return (0);
}
Ejemplo n.º 13
0
/*
 * Given the root disk device devid or pathname, read the label from
 * the device, and construct a configuration nvlist.
 */
int
vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
{
	ldi_handle_t vd_lh;
	vdev_label_t *label;
	uint64_t s, size;
	int l;
	ddi_devid_t tmpdevid;
	int error = -1;
	char *minor_name;

	/*
	 * Read the device label and build the nvlist.
	 */
	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
	    &minor_name) == 0) {
		error = ldi_open_by_devid(tmpdevid, minor_name,
		    FREAD, kcred, &vd_lh, zfs_li);
		ddi_devid_free(tmpdevid);
		ddi_devid_str_free(minor_name);
	}

	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
	    zfs_li)))
		return (error);

	if (ldi_get_size(vd_lh, &s)) {
		(void) ldi_close(vd_lh, FREAD, kcred);
		return (SET_ERROR(EIO));
	}

	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);

	*config = NULL;
	for (l = 0; l < VDEV_LABELS; l++) {
		uint64_t offset, state, txg = 0;

		/* read vdev label */
		offset = vdev_label_offset(size, l, 0);
		if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
			continue;

		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
			*config = NULL;
			continue;
		}

		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
			nvlist_free(*config);
			*config = NULL;
			continue;
		}

		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
		    &txg) != 0 || txg == 0) {
			nvlist_free(*config);
			*config = NULL;
			continue;
		}

		break;
	}

	kmem_free(label, sizeof (vdev_label_t));
	(void) ldi_close(vd_lh, FREAD, kcred);
	if (*config == NULL)
		error = SET_ERROR(EIDRM);

	return (error);
}
Ejemplo n.º 14
0
static int
vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
    uint64_t *ashift)
{
	spa_t *spa = vd->vdev_spa;
	vdev_disk_t *dvd;
	ldi_ev_cookie_t ecookie;
	vdev_disk_ldi_cb_t *lcb;
	union {
		struct dk_minfo_ext ude;
		struct dk_minfo ud;
	} dks;
	struct dk_minfo_ext *dkmext = &dks.ude;
	struct dk_minfo *dkm = &dks.ud;
	int error;
	dev_t dev;
	int otyp;
	boolean_t validate_devid = B_FALSE;
	ddi_devid_t devid;
	uint64_t capacity = 0, blksz = 0, pbsize;

	/*
	 * We must have a pathname, and it must be absolute.
	 */
	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
		return (SET_ERROR(EINVAL));
	}
	rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
	dvd = vd->vdev_tsd;
	/*
	 * Reopen the device if it's not currently open. Otherwise,
	 * just update the physical size of the device.
	 */
	if (dvd != NULL) {
		ASSERT(vd->vdev_reopening);
		/*
		 * Here vd_lh is protected by vdev_tsd_lock
		 */
		ASSERT(dvd->vd_lh != NULL);
		/* This should not happen, but let's be safe */
		if (dvd->vd_lh == NULL) {
			/* What are we going to do here??? */
			rw_exit(&vd->vdev_tsd_lock);
			return (SET_ERROR(ENXIO));
		}
		goto skip_open;
	}
	/*
	 * Create dvd to be used as vd->vdev_tsd.
	 */
	vd->vdev_tsd = dvd = vdev_disk_alloc();

	/*
	 * When opening a disk device, we want to preserve the user's original
	 * intent.  We always want to open the device by the path the user gave
	 * us, even if it is one of multiple paths to the same device.  But we
	 * also want to be able to survive disks being removed/recabled.
	 * Therefore the sequence of opening devices is:
	 *
	 * 1. Try opening the device by path.  For legacy pools without the
	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
	 *
	 * 2. If the devid of the device matches the stored value, return
	 *    success.
	 *
	 * 3. Otherwise, the device may have moved.  Try opening the device
	 *    by the devid instead.
	 */
	if (vd->vdev_devid != NULL) {
		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
		    &dvd->vd_minor) != 0) {
			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
			vdev_disk_free_locked(vd);
			rw_exit(&vd->vdev_tsd_lock);
			return (SET_ERROR(EINVAL));
		}
	}

	error = EINVAL;		/* presume failure */

	if (vd->vdev_path != NULL) {

		if (vd->vdev_wholedisk == -1ULL) {
			size_t len = strlen(vd->vdev_path) + 3;
			char *buf = kmem_alloc(len, KM_SLEEP);

			(void) snprintf(buf, len, "%ss0", vd->vdev_path);

			error = ldi_open_by_name(buf, spa_mode(spa), kcred,
			    &dvd->vd_lh, zfs_li);
			if (error == 0) {
				spa_strfree(vd->vdev_path);
				vd->vdev_path = buf;
				vd->vdev_wholedisk = 1ULL;
			} else {
				kmem_free(buf, len);
			}
		}

		/*
		 * If we have not yet opened the device, try to open it by the
		 * specified path.
		 */
		if (error != 0) {
			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);
		}

		/*
		 * Compare the devid to the stored value.
		 */
		if (error == 0 && vd->vdev_devid != NULL &&
		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
				error = SET_ERROR(EINVAL);
				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
				    kcred);
				dvd->vd_lh = NULL;
			}
			ddi_devid_free(devid);
		}

		/*
		 * If we succeeded in opening the device, but 'vdev_wholedisk'
		 * is not yet set, then this must be a slice.
		 */
		if (error == 0 && vd->vdev_wholedisk == -1ULL)
			vd->vdev_wholedisk = 0;
	}

	/*
	 * If we were unable to open by path, or the devid check fails, open by
	 * devid instead.
	 */
	if (error != 0 && vd->vdev_devid != NULL) {
		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
	}

	/*
	 * If all else fails, then try opening by physical path (if available)
	 * or the logical path (if we failed due to the devid check).  While not
	 * as reliable as the devid, this will give us something, and the higher
	 * level vdev validation will prevent us from opening the wrong device.
	 */
	if (error) {
		if (vd->vdev_devid != NULL)
			validate_devid = B_TRUE;

		if (vd->vdev_physpath != NULL &&
		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);

		/*
		 * Note that we don't support the legacy auto-wholedisk support
		 * as above.  This hasn't been used in a very long time and we
		 * don't need to propagate its oddities to this edge condition.
		 */
		if (error && vd->vdev_path != NULL)
			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);
	}

	if (error) {
		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
		vdev_disk_free_locked(vd);
		rw_exit(&vd->vdev_tsd_lock);
		return (error);
	}

	/*
	 * Now that the device has been successfully opened, update the devid
	 * if necessary.
	 */
	if (validate_devid && spa_writeable(spa) &&
	    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
		if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
			char *vd_devid;

			vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
			zfs_dbgmsg("vdev %s: update devid from %s, "
			    "to %s", vd->vdev_path, vd->vdev_devid, vd_devid);
			spa_strfree(vd->vdev_devid);
			vd->vdev_devid = spa_strdup(vd_devid);
			ddi_devid_str_free(vd_devid);
		}
		ddi_devid_free(devid);
	}

	/*
	 * Once a device is opened, verify that the physical device path (if
	 * available) is up to date.
	 */
	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
		char *physpath, *minorname;

		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
		minorname = NULL;
		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
		    (vd->vdev_physpath == NULL ||
		    strcmp(vd->vdev_physpath, physpath) != 0)) {
			if (vd->vdev_physpath)
				spa_strfree(vd->vdev_physpath);
			(void) strlcat(physpath, ":", MAXPATHLEN);
			(void) strlcat(physpath, minorname, MAXPATHLEN);
			vd->vdev_physpath = spa_strdup(physpath);
		}
		if (minorname)
			kmem_free(minorname, strlen(minorname) + 1);
		kmem_free(physpath, MAXPATHLEN);
	}

	/*
	 * Register callbacks for the LDI offline event.
	 */
	if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
	    LDI_EV_SUCCESS) {
		lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
		list_insert_tail(&dvd->vd_ldi_cbs, lcb);
		(void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
		    &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
	}

	/*
	 * Register callbacks for the LDI degrade event.
	 */
	if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
	    LDI_EV_SUCCESS) {
		lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
		list_insert_tail(&dvd->vd_ldi_cbs, lcb);
		(void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
		    &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
	}

	/* Reset TRIM flag, as underlying device support may have changed */
	vd->vdev_notrim = B_FALSE;

skip_open:
	ASSERT(dvd != NULL);
	/*
	 * Determine the actual size of the device.
	 */
	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
		vdev_disk_free_locked(vd);
		rw_exit(&vd->vdev_tsd_lock);
		return (SET_ERROR(EINVAL));
	}

	*max_psize = *psize;

	/*
	 * Determine the device's minimum transfer size.
	 * If the ioctl isn't supported, assume DEV_BSIZE.
	 */
	if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
	    (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
		capacity = dkmext->dki_capacity - 1;
		blksz = dkmext->dki_lbsize;
		pbsize = dkmext->dki_pbsize;
	} else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
	    (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
		VDEV_DEBUG(
		    "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
		    vd->vdev_path);
		capacity = dkm->dki_capacity - 1;
		blksz = dkm->dki_lbsize;
		pbsize = blksz;
	} else {
		VDEV_DEBUG("vdev_disk_open(\"%s\"): "
		    "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
		    vd->vdev_path, error);
		pbsize = DEV_BSIZE;
	}

	*ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;

	if (vd->vdev_wholedisk == 1) {
		int wce = 1;

		if (error == 0) {
			/*
			 * If we have the capability to expand, we'd have
			 * found out via success from DKIOCGMEDIAINFO{,EXT}.
			 * Adjust max_psize upward accordingly since we know
			 * we own the whole disk now.
			 */
			*max_psize += vdev_disk_get_space(vd, capacity, blksz);
			zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
			    "max_psize %llu", vd->vdev_path, *psize,
			    *max_psize);
		}

		/*
		 * Since we own the whole disk, try to enable disk write
		 * caching.  We ignore errors because it's OK if we can't do it.
		 */
		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
		    FKIOCTL, kcred, NULL);
	}
	/*
	 * We are done with vd_lh and vdev_tsd, release the vdev_tsd_lock
	 */
	rw_exit(&vd->vdev_tsd_lock);

	/*
	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
	 * try again.
	 */
	vd->vdev_nowritecache = B_FALSE;

	return (0);
}
Ejemplo n.º 15
0
/*ARGSUSED*/
static int
lx_ptm_open(dev_t *devp, int flag, int otyp, cred_t *credp)
{
	struct strioctl	iocb;
	ptmptsopencb_t	ppocb = { NULL, NULL };
	ldi_handle_t	lh;
	major_t		maj, our_major = getmajor(*devp);
	minor_t		min, lastmin;
	uint_t		index, anchor = 1;
	dev_t		ptm_dev;
	int		err, rval = 0;

	/*
	 * Don't support the FNDELAY flag and FNONBLOCK until we either
	 * find a Linux app that opens /dev/ptmx with the O_NDELAY
	 * or O_NONBLOCK flags explicitly, or until we create test cases
	 * to determine how reads of master terminal devices opened with
	 * these flags behave in different situations on Linux.  Supporting
	 * these flags will involve enhancing our read implementation
	 * and changing the way it deals with EOF notifications.
	 */
	if (flag & (FNDELAY | FNONBLOCK))
		return (ENOTSUP);

	/*
	 * we're layered on top of the ptm driver so open that driver
	 * first.  (note that we're opening /dev/ptmx in the global
	 * zone, not ourselves in the Linux zone.)
	 */
	err = ldi_open_by_name(LP_PTM_PATH, flag, credp, &lh, lps.lps_li);
	if (err != 0)
		return (err);

	/* get the devt returned by the ptmx open */
	err = ldi_get_dev(lh, &ptm_dev);
	if (err != 0) {
		(void) ldi_close(lh, flag, credp);
		return (err);
	}

	/*
	 * we're a cloning driver so here's well change the devt that we
	 * return.  the ptmx is also a cloning driver so we'll just use
	 * it's minor number as our minor number (it already manages it's
	 * minor name space so no reason to duplicate the effort.)
	 */
	index = getminor(ptm_dev);
	*devp = makedevice(our_major, INDEX_TO_MINOR(index));

	/* Get a callback function to query if the pts device is open. */
	iocb.ic_cmd = PTMPTSOPENCB;
	iocb.ic_timout = 0;
	iocb.ic_len = sizeof (ppocb);
	iocb.ic_dp = (char *)&ppocb;

	err = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, kcred, &rval);
	if ((err != 0) || (rval != 0)) {
		(void) ldi_close(lh, flag, credp);
		return (EIO); /* XXX return something else here? */
	}
	ASSERT(ppocb.ppocb_func != NULL);

	/*
	 * now setup autopush for the terminal slave device.  this is
	 * necessary so that when a Linux program opens the device we
	 * can push required strmod modules onto the stream.  in Solaris
	 * this is normally done by the application that actually
	 * allocates the terminal.
	 */
	maj = lps.lps_pts_major;
	min = index;
	lastmin = 0;
	err = kstr_autopush(SET_AUTOPUSH, &maj, &min, &lastmin,
	    &anchor, lx_pts_mods);
	if (err != 0) {
		(void) ldi_close(lh, flag, credp);
		return (EIO); /* XXX return something else here? */
	}

	/* save off this layered handle for future accesses */
	lx_ptm_lh_insert(index, lh);
	lx_ptm_lh_set_ppocb(index, &ppocb);
	return (0);
}
Ejemplo n.º 16
0
static int
vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
{
	spa_t *spa = vd->vdev_spa;
	vdev_disk_t *dvd;
	struct dk_minfo_ext dkmext;
	int error;
	dev_t dev;
	int otyp;

	/*
	 * We must have a pathname, and it must be absolute.
	 */
	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
		return (EINVAL);
	}

	/*
	 * Reopen the device if it's not currently open. Otherwise,
	 * just update the physical size of the device.
	 */
	if (vd->vdev_tsd != NULL) {
		ASSERT(vd->vdev_reopening);
		dvd = vd->vdev_tsd;
		goto skip_open;
	}

	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);

	/*
	 * When opening a disk device, we want to preserve the user's original
	 * intent.  We always want to open the device by the path the user gave
	 * us, even if it is one of multiple paths to the save device.  But we
	 * also want to be able to survive disks being removed/recabled.
	 * Therefore the sequence of opening devices is:
	 *
	 * 1. Try opening the device by path.  For legacy pools without the
	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
	 *
	 * 2. If the devid of the device matches the stored value, return
	 *    success.
	 *
	 * 3. Otherwise, the device may have moved.  Try opening the device
	 *    by the devid instead.
	 */
	if (vd->vdev_devid != NULL) {
		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
		    &dvd->vd_minor) != 0) {
			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
			return (EINVAL);
		}
	}

	error = EINVAL;		/* presume failure */

	if (vd->vdev_path != NULL) {
		ddi_devid_t devid;

		if (vd->vdev_wholedisk == -1ULL) {
			size_t len = strlen(vd->vdev_path) + 3;
			char *buf = kmem_alloc(len, KM_SLEEP);
			ldi_handle_t lh;

			(void) snprintf(buf, len, "%ss0", vd->vdev_path);

			if (ldi_open_by_name(buf, spa_mode(spa), kcred,
			    &lh, zfs_li) == 0) {
				spa_strfree(vd->vdev_path);
				vd->vdev_path = buf;
				vd->vdev_wholedisk = 1ULL;
				(void) ldi_close(lh, spa_mode(spa), kcred);
			} else {
				kmem_free(buf, len);
			}
		}

		error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
		    &dvd->vd_lh, zfs_li);

		/*
		 * Compare the devid to the stored value.
		 */
		if (error == 0 && vd->vdev_devid != NULL &&
		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
				error = EINVAL;
				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
				    kcred);
				dvd->vd_lh = NULL;
			}
			ddi_devid_free(devid);
		}

		/*
		 * If we succeeded in opening the device, but 'vdev_wholedisk'
		 * is not yet set, then this must be a slice.
		 */
		if (error == 0 && vd->vdev_wholedisk == -1ULL)
			vd->vdev_wholedisk = 0;
	}

	/*
	 * If we were unable to open by path, or the devid check fails, open by
	 * devid instead.
	 */
	if (error != 0 && vd->vdev_devid != NULL)
		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);

	/*
	 * If all else fails, then try opening by physical path (if available)
	 * or the logical path (if we failed due to the devid check).  While not
	 * as reliable as the devid, this will give us something, and the higher
	 * level vdev validation will prevent us from opening the wrong device.
	 */
	if (error) {
		if (vd->vdev_physpath != NULL &&
		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);

		/*
		 * Note that we don't support the legacy auto-wholedisk support
		 * as above.  This hasn't been used in a very long time and we
		 * don't need to propagate its oddities to this edge condition.
		 */
		if (error && vd->vdev_path != NULL)
			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
			    kcred, &dvd->vd_lh, zfs_li);
	}

	if (error) {
		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
		return (error);
	}

	/*
	 * Once a device is opened, verify that the physical device path (if
	 * available) is up to date.
	 */
	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
		char *physpath, *minorname;

		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
		minorname = NULL;
		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
		    (vd->vdev_physpath == NULL ||
		    strcmp(vd->vdev_physpath, physpath) != 0)) {
			if (vd->vdev_physpath)
				spa_strfree(vd->vdev_physpath);
			(void) strlcat(physpath, ":", MAXPATHLEN);
			(void) strlcat(physpath, minorname, MAXPATHLEN);
			vd->vdev_physpath = spa_strdup(physpath);
		}
		if (minorname)
			kmem_free(minorname, strlen(minorname) + 1);
		kmem_free(physpath, MAXPATHLEN);
	}

skip_open:
	/*
	 * Determine the actual size of the device.
	 */
	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
		return (EINVAL);
	}

	/*
	 * If we own the whole disk, try to enable disk write caching.
	 * We ignore errors because it's OK if we can't do it.
	 */
	if (vd->vdev_wholedisk == 1) {
		int wce = 1;
		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
		    FKIOCTL, kcred, NULL);
	}

	/*
	 * Determine the device's minimum transfer size.
	 * If the ioctl isn't supported, assume DEV_BSIZE.
	 */
	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext,
	    FKIOCTL, kcred, NULL) != 0)
		dkmext.dki_pbsize = DEV_BSIZE;

	*ashift = highbit(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1;

	/*
	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
	 * try again.
	 */
	vd->vdev_nowritecache = B_FALSE;

	return (0);
}
Ejemplo n.º 17
0
static void
sdp_gen_ioctl(queue_t *q, mblk_t *mp)
{
	struct iocblk *iocp;
	int32_t enable = 0;
	int ret;
	boolean_t priv = B_TRUE;

	/* LINTED */
	iocp = (struct iocblk *)mp->b_rptr;
	switch (iocp->ioc_cmd) {
			int32_t send_enable;
		case SIOCSENABLESDP:
			bcopy(mp->b_cont->b_rptr, &enable, sizeof (int));

			send_enable = enable;

			/*
			 * Check for root privs.
			 * if not net config privs - return state of system SDP
			 */
			if (secpolicy_net_config(CRED(), B_FALSE) != 0) {
				priv = B_FALSE;
			}


			/*
			 * The sdpib driver is loaded if root enables sdp the
			 * first time (sdp_transport_handle is NULL). It is
			 * unloaded during the following first disable. At all
			 * other times for root as well as non-root users, the
			 * action of enabling/disabling sdp is simply acked.
			 */
			rw_enter(&sdp_transport_lock, RW_READER);
			if ((send_enable == 1) &&
			    (sdp_transport_handle == NULL) &&
			    (priv == B_TRUE)) {
				/* Initialize sdpib transport driver */
				rw_exit(&sdp_transport_lock);
				ret = sdp_open_sdpib_driver();
				rw_enter(&sdp_transport_lock,
				    RW_READER);
				if (ret != 0) {
					/* Transport failed to load */
					rw_exit(&sdp_transport_lock);
					enable = 0;
					goto done;
				}
				(void) ldi_ioctl(sdp_transport_handle,
				    iocp->ioc_cmd, (intptr_t)&send_enable,
				    FKIOCTL, CRED(), (int *)&enable);
			} else if (sdp_transport_handle != NULL) {
				(void) ldi_ioctl(sdp_transport_handle,
				    iocp->ioc_cmd, (intptr_t)&send_enable,
				    FKIOCTL, CRED(), (int *)&enable);
				if (send_enable == 0 && priv == B_TRUE) {
					(void) ldi_close(sdp_transport_handle,
					    FNDELAY, kcred);
					sdp_transport_handle = NULL;
				}
			} else {
				enable = 0;
			}
			rw_exit(&sdp_transport_lock);

done:
			bcopy(&enable, mp->b_cont->b_rptr, sizeof (int));

			/* ACK the ioctl */
			mp->b_datap->db_type = M_IOCACK;
			iocp->ioc_count = sizeof (int);
			qreply(q, mp);
			break;
		default:
			miocnak(q, mp, 0, ENOTSUP);
	}
}