git: 7d7168478380 - stable/13 - cam(4): Improve XPT_DEV_MATCH

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Tue, 19 Oct 2021 15:05:15 UTC
The branch stable/13 has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=7d7168478380f08b3a7fd58ca7cf83182fae6850

commit 7d7168478380f08b3a7fd58ca7cf83182fae6850
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-10-05 18:54:03 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2021-10-19 14:10:57 +0000

    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
    
    (cherry picked from commit 8f9be1eed11c27c66386c3d72cd6c6aef597fa0d)
---
 sys/cam/cam_ccb.h    | 11 +++-----
 sys/cam/cam_compat.c | 50 ++++++++++++++++++++++++++++++++--
 sys/cam/cam_compat.h |  7 +++++
 sys/cam/cam_xpt.c    | 76 +++++++++++++---------------------------------------
 4 files changed, 78 insertions(+), 66 deletions(-)

diff --git a/sys/cam/cam_ccb.h b/sys/cam/cam_ccb.h
index b853e3165ba3..a0066eae3361 100644
--- a/sys/cam/cam_ccb.h
+++ b/sys/cam/cam_ccb.h
@@ -410,13 +410,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 {
@@ -429,13 +428,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 {
@@ -455,12 +453,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 {
@@ -582,7 +579,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 */
diff --git a/sys/cam/cam_compat.c b/sys/cam/cam_compat.c
index 4c89072fa389..6893402a3d9c 100644
--- a/sys/cam/cam_compat.c
+++ b/sys/cam/cam_compat.c
@@ -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));
+}
diff --git a/sys/cam/cam_compat.h b/sys/cam/cam_compat.h
index dd728d86839e..9a1996d230ce 100644
--- a/sys/cam/cam_compat.h
+++ b/sys/cam/cam_compat.h
@@ -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
diff --git a/sys/cam/cam_xpt.c b/sys/cam/cam_xpt.c
index 0e38166b6ba0..678d8e0b4f3d 100644
--- a/sys/cam/cam_xpt.c
+++ b/sys/cam/cam_xpt.c
@@ -1374,6 +1374,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
@@ -1382,6 +1384,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;
@@ -1390,28 +1400,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;
@@ -1487,11 +1475,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))
@@ -1506,19 +1503,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;
@@ -1548,7 +1532,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
@@ -1623,27 +1606,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;