cam(4): Improve XPT_DEV_MATCH

Remove *_MATCH_NONE enums, making no sense and so never used.  Make
*_MATCH_ANY enums 0 (no any match flags set), previously used by
*_MATCH_NONE.  Bump CAM_VERSION to 0x1a reflecting those changes and
add compat shims.

When traversing through buses and devices do not descend if we can
already see that requested pattern does not match the bus or device.
It allows to save significant amount of time on system with thousands
of disks when doing limited searches.

Reviewed by:	imp
MFC after:	2 weeks
Sponsored by:	iXsystems, Inc.
Differential Revision:	https://reviews.freebsd.org/D32304
This commit is contained in:
Alexander Motin 2021-10-05 14:54:03 -04:00
parent 7835b2cb4a
commit 8f9be1eed1
4 changed files with 78 additions and 66 deletions

View File

@ -422,13 +422,12 @@ struct ccb_getdevlist {
};
typedef enum {
PERIPH_MATCH_NONE = 0x000,
PERIPH_MATCH_ANY = 0x000,
PERIPH_MATCH_PATH = 0x001,
PERIPH_MATCH_TARGET = 0x002,
PERIPH_MATCH_LUN = 0x004,
PERIPH_MATCH_NAME = 0x008,
PERIPH_MATCH_UNIT = 0x010,
PERIPH_MATCH_ANY = 0x01f
} periph_pattern_flags;
struct periph_match_pattern {
@ -441,13 +440,12 @@ struct periph_match_pattern {
};
typedef enum {
DEV_MATCH_NONE = 0x000,
DEV_MATCH_ANY = 0x000,
DEV_MATCH_PATH = 0x001,
DEV_MATCH_TARGET = 0x002,
DEV_MATCH_LUN = 0x004,
DEV_MATCH_INQUIRY = 0x008,
DEV_MATCH_DEVID = 0x010,
DEV_MATCH_ANY = 0x00f
} dev_pattern_flags;
struct device_id_match_pattern {
@ -467,12 +465,11 @@ struct device_match_pattern {
};
typedef enum {
BUS_MATCH_NONE = 0x000,
BUS_MATCH_ANY = 0x000,
BUS_MATCH_PATH = 0x001,
BUS_MATCH_NAME = 0x002,
BUS_MATCH_UNIT = 0x004,
BUS_MATCH_BUS_ID = 0x008,
BUS_MATCH_ANY = 0x00f
} bus_pattern_flags;
struct bus_match_pattern {
@ -594,7 +591,7 @@ struct ccb_dev_match {
/*
* Definitions for the path inquiry CCB fields.
*/
#define CAM_VERSION 0x19 /* Hex value for current version */
#define CAM_VERSION 0x1a /* Hex value for current version */
typedef enum {
PI_MDP_ABLE = 0x80, /* Supports MDP message */

View File

@ -58,6 +58,8 @@ static int cam_compat_handle_0x17(struct cdev *dev, u_long cmd, caddr_t addr,
int flag, struct thread *td, d_ioctl_t *cbfnp);
static int cam_compat_handle_0x18(struct cdev *dev, u_long cmd, caddr_t addr,
int flag, struct thread *td, d_ioctl_t *cbfnp);
static int cam_compat_handle_0x19(struct cdev *dev, u_long cmd, caddr_t addr,
int flag, struct thread *td, d_ioctl_t *cbfnp);
static int cam_compat_translate_dev_match_0x18(union ccb *ccb);
int
@ -108,6 +110,22 @@ cam_compat_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
cmd = CAMGETPASSTHRU;
error = cam_compat_handle_0x18(dev, cmd, addr, flag, td, cbfnp);
break;
case CAMIOCOMMAND_0x19:
cmd = CAMIOCOMMAND;
error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
break;
case CAMGETPASSTHRU_0x19:
cmd = CAMGETPASSTHRU;
error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
break;
case CAMIOQUEUE_0x19:
cmd = CAMIOQUEUE;
error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
break;
case CAMIOGET_0x19:
cmd = CAMIOGET;
error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
break;
default:
error = ENOTTY;
}
@ -170,7 +188,7 @@ cam_compat_handle_0x17(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
bcopy(ccbb17, ccbb, CAM_0X17_DATA_LEN);
}
error = (cbfnp)(dev, cmd, (caddr_t)ccb, flag, td);
error = cam_compat_handle_0x19(dev, cmd, (caddr_t)ccb, flag, td, cbfnp);
hdr17->pinfo = hdr->pinfo;
hdr17->xpt_links = hdr->xpt_links;
@ -310,7 +328,7 @@ cam_compat_handle_0x18(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
bcopy(ccbb18, ccbb, CAM_0X18_DATA_LEN);
}
error = (cbfnp)(dev, cmd, (caddr_t)ccb, flag, td);
error = cam_compat_handle_0x19(dev, cmd, (caddr_t)ccb, flag, td, cbfnp);
hdr18->pinfo = hdr->pinfo;
hdr18->xpt_links = hdr->xpt_links;
@ -420,3 +438,31 @@ cam_compat_translate_dev_match_0x18(union ccb *ccb)
return (0);
}
static int
cam_compat_handle_0x19(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
struct thread *td, d_ioctl_t *cbfnp)
{
union ccb *ccb = (union ccb *)addr;
struct cam_periph_map_info mapinfo;
if (cmd == CAMIOCOMMAND && ccb->ccb_h.func_code == XPT_DEV_MATCH) {
bzero(&mapinfo, sizeof(mapinfo));
cam_periph_mapmem(ccb, &mapinfo, maxphys);
for (int i = 0; i < ccb->cdm.num_patterns; i++) {
struct dev_match_pattern *p = &ccb->cdm.patterns[i];
if (p->type == DEV_MATCH_BUS &&
p->pattern.bus_pattern.flags == 0x00f)
p->pattern.bus_pattern.flags = BUS_MATCH_ANY;
if (p->type == DEV_MATCH_DEVICE &&
p->pattern.device_pattern.flags == 0x00f)
p->pattern.device_pattern.flags = DEV_MATCH_ANY;
if (p->type == DEV_MATCH_PERIPH &&
p->pattern.periph_pattern.flags == 0x01f)
p->pattern.periph_pattern.flags = PERIPH_MATCH_ANY;
}
cam_periph_unmapmem(ccb, &mapinfo);
}
return ((cbfnp)(dev, cmd, addr, flag, td));
}

View File

@ -220,5 +220,12 @@ struct dev_match_result_0x18 {
#define CAMIOCOMMAND_0x18 _IOC(IOC_INOUT, CAM_VERSION_0x18, 2, CAM_0X18_LEN)
#define CAMGETPASSTHRU_0x18 _IOC(IOC_INOUT, CAM_VERSION_0x18, 3, CAM_0X18_LEN)
/* Version 0x19 compatibility */
#define CAM_VERSION_0x19 0x19
#define CAMIOCOMMAND_0x19 _IOWR(CAM_VERSION_0x19, 2, union ccb)
#define CAMGETPASSTHRU_0x19 _IOWR(CAM_VERSION_0x19, 3, union ccb)
#define CAMIOQUEUE_0x19 _IO(CAM_VERSION_0x19, 4)
#define CAMIOGET_0x19 _IO(CAM_VERSION_0x19, 5)
#endif
#endif

View File

@ -1386,6 +1386,8 @@ xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
for (i = 0; i < num_patterns; i++) {
struct bus_match_pattern *cur_pattern;
struct device_match_pattern *dp = &patterns[i].pattern.device_pattern;
struct periph_match_pattern *pp = &patterns[i].pattern.periph_pattern;
/*
* If the pattern in question isn't for a bus node, we
@ -1394,6 +1396,14 @@ xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
* tree, since the user wants to match against lower-level
* EDT elements.
*/
if (patterns[i].type == DEV_MATCH_DEVICE &&
(dp->flags & DEV_MATCH_PATH) != 0 &&
dp->path_id != bus->path_id)
continue;
if (patterns[i].type == DEV_MATCH_PERIPH &&
(pp->flags & PERIPH_MATCH_PATH) != 0 &&
pp->path_id != bus->path_id)
continue;
if (patterns[i].type != DEV_MATCH_BUS) {
if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
retval |= DM_RET_DESCEND;
@ -1402,28 +1412,6 @@ xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
cur_pattern = &patterns[i].pattern.bus_pattern;
/*
* If they want to match any bus node, we give them any
* device node.
*/
if (cur_pattern->flags == BUS_MATCH_ANY) {
/* set the copy flag */
retval |= DM_RET_COPY;
/*
* If we've already decided on an action, go ahead
* and return.
*/
if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
return(retval);
}
/*
* Not sure why someone would do this...
*/
if (cur_pattern->flags == BUS_MATCH_NONE)
continue;
if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
&& (cur_pattern->path_id != bus->path_id))
continue;
@ -1499,11 +1487,20 @@ xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
for (i = 0; i < num_patterns; i++) {
struct device_match_pattern *cur_pattern;
struct scsi_vpd_device_id *device_id_page;
struct periph_match_pattern *pp = &patterns[i].pattern.periph_pattern;
/*
* If the pattern in question isn't for a device node, we
* aren't interested.
*/
if (patterns[i].type == DEV_MATCH_PERIPH &&
(pp->flags & PERIPH_MATCH_TARGET) != 0 &&
pp->target_id != device->target->target_id)
continue;
if (patterns[i].type == DEV_MATCH_PERIPH &&
(pp->flags & PERIPH_MATCH_LUN) != 0 &&
pp->target_lun != device->lun_id)
continue;
if (patterns[i].type != DEV_MATCH_DEVICE) {
if ((patterns[i].type == DEV_MATCH_PERIPH)
&& ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
@ -1518,19 +1515,6 @@ xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
== (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
return(DM_RET_ERROR);
/*
* If they want to match any device node, we give them any
* device node.
*/
if (cur_pattern->flags == DEV_MATCH_ANY)
goto copy_dev_node;
/*
* Not sure why someone would do this...
*/
if (cur_pattern->flags == DEV_MATCH_NONE)
continue;
if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
&& (cur_pattern->path_id != device->target->bus->path_id))
continue;
@ -1560,7 +1544,6 @@ xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
cur_pattern->data.devid_pat.id_len) != 0))
continue;
copy_dev_node:
/*
* If we get to this point, the user definitely wants
* information on this device. So tell the caller to copy
@ -1635,27 +1618,6 @@ xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
cur_pattern = &patterns[i].pattern.periph_pattern;
/*
* If they want to match on anything, then we will do so.
*/
if (cur_pattern->flags == PERIPH_MATCH_ANY) {
/* set the copy flag */
retval |= DM_RET_COPY;
/*
* We've already set the return action to stop,
* since there are no nodes below peripherals in
* the tree.
*/
return(retval);
}
/*
* Not sure why someone would do this...
*/
if (cur_pattern->flags == PERIPH_MATCH_NONE)
continue;
if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
&& (cur_pattern->path_id != periph->path->bus->path_id))
continue;