[PATCHv2 2/2] cld: kill CLD_MAX_PKT_MSG, add CLD_MAX_PAYLOAD_SZ

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Get rid of CLD_MAX_PKT_MSG. It only existed so that we could use static arrays
in a few places.

Create CLD_MAX_PAYLOAD_SZ to represent the maximum size of a message that the
API user can GET or PUT. Reducing this constant could break users who
relied on the old maximum data size, so we should try not to do it often.

Signed-off-by: Colin McCabe <cmccabe@xxxxxxxxxxxxxx>
---
 include/cld_msg.h |    7 ++++---
 include/cldc.h    |    4 ++--
 lib/cldc.c        |   24 +++++++++++++++++-------
 server/msg.c      |    7 +++++++
 server/session.c  |   12 ++++++++----
 5 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/include/cld_msg.h b/include/cld_msg.h
index 6117be8..f24e4e0 100644
--- a/include/cld_msg.h
+++ b/include/cld_msg.h
@@ -34,9 +34,10 @@ enum {
 	CLD_MAX_SECRET_KEY	= 128,		/**< includes req. nul */
 
 	CLD_MAX_PKT_MSG_SZ	= 1024,
-	CLD_MAX_PKT_MSG		= 128,
-	CLD_MAX_MSG_SZ		= CLD_MAX_PKT_MSG * 1024, /**< maximum total
-					      msg size, including all packets */
+	CLD_MAX_PAYLOAD_SZ	= 131072,	/**< maximum size of data that users
+						  can GET or PUT */
+	CLD_MAX_MSG_SZ		= 196608,	/**< maximum total
+						msg size, including all packets */
 };
 
 /** available RPC operations */
diff --git a/include/cldc.h b/include/cldc.h
index 0d72669..37d6eb6 100644
--- a/include/cldc.h
+++ b/include/cldc.h
@@ -73,10 +73,10 @@ struct cldc_msg {
 	int		data_len;
 	int		n_pkts;
 
-	struct cldc_pkt_info *pkt_info[CLD_MAX_PKT_MSG];
+	uint8_t		*data;
 
 	/* must be at end of struct */
-	uint8_t		data[0];
+	struct cldc_pkt_info *pkt_info[0];
 };
 
 /** an open file handle associated with a session */
diff --git a/lib/cldc.c b/lib/cldc.c
index dcc179c..0eec70f 100644
--- a/lib/cldc.c
+++ b/lib/cldc.c
@@ -260,7 +260,9 @@ static void cldc_msg_free(struct cldc_msg *msg)
 	if (!msg)
 		return;
 
-	for (i = 0; i < CLD_MAX_PKT_MSG; i++)
+	free(msg->data);
+
+	for (i = 0; i < msg->n_pkts; i++)
 		free(msg->pkt_info[i]);
 
 	free(msg);
@@ -492,14 +494,23 @@ static struct cldc_msg *cldc_new_msg(struct cldc_session *sess,
 	struct cldc_msg *msg;
 	struct cld_msg_hdr *hdr;
 	struct timeval tv;
-	int i, data_left;
+	int n_pkts, i, data_left;
 	void *p;
 
 	gettimeofday(&tv, NULL);
 
-	msg = calloc(1, sizeof(*msg) + msg_len);
+	n_pkts = msg_len / CLD_MAX_PKT_MSG_SZ;
+	n_pkts += ((msg_len % CLD_MAX_PKT_MSG_SZ) ? 1 : 0);
+
+	msg = calloc(1, sizeof(*msg) +
+		n_pkts * sizeof(struct cldc_pkt_info *));
 	if (!msg)
 		return NULL;
+	msg->data = calloc(1, msg_len);
+	if (!msg->data) {
+		free(msg);
+		return NULL;
+	}
 
 	__cld_rand64(&msg->xid);
 
@@ -512,8 +523,7 @@ static struct cldc_msg *cldc_new_msg(struct cldc_session *sess,
 
 	msg->data_len = msg_len;
 
-	msg->n_pkts = msg_len / CLD_MAX_PKT_MSG_SZ;
-	msg->n_pkts += ((msg_len % CLD_MAX_PKT_MSG_SZ) ? 1 : 0);
+	msg->n_pkts = n_pkts;
 
 	p = msg->data;
 	data_left = msg_len;
@@ -542,7 +552,7 @@ static struct cldc_msg *cldc_new_msg(struct cldc_session *sess,
 		data_left -= pkt_len;
 	}
 
-	hdr = (struct cld_msg_hdr *) &msg->data[0];
+	hdr = (struct cld_msg_hdr *) msg->data;
 	memcpy(&hdr->magic, CLD_MSG_MAGIC, CLD_MAGIC_SZ);
 	hdr->op = op;
 	hdr->xid = msg->xid;
@@ -1099,7 +1109,7 @@ int cldc_put(struct cldc_fh *fh, const struct cldc_call_opts *copts,
 	struct cldc_msg *msg;
 	struct cld_msg_put *put;
 
-	if (!data || !data_len || data_len > CLD_MAX_MSG_SZ)
+	if (!data || !data_len || data_len > CLD_MAX_PAYLOAD_SZ)
 		return -EINVAL;
 
 	if (!fh->valid)
diff --git a/server/msg.c b/server/msg.c
index 301f698..62f8a71 100644
--- a/server/msg.c
+++ b/server/msg.c
@@ -757,6 +757,13 @@ void msg_put(struct msg_params *mp)
 
 	/* make sure additional input data as large as expected */
 	data_size = le32_to_cpu(msg->data_size);
+	if (data_size > CLD_MAX_PAYLOAD_SZ) {
+		HAIL_ERR(&srv_log, "%s: can't PUT %d bytes of data: "
+			"%d is the maximum.\n",
+			__func__, data_size, CLD_MAX_PAYLOAD_SZ);
+		resp_rc = CLE_BAD_PKT;
+		goto err_out_noabort;
+	}
 	if (mp->msg_len != (data_size + sizeof(*msg))) {
 		HAIL_INFO(&srv_log, "PUT len mismatch: msg len %zu, "
 			  "wanted %zu + %u (== %zu)",
diff --git a/server/session.c b/server/session.c
index 9774fb5..f63f66a 100644
--- a/server/session.c
+++ b/server/session.c
@@ -623,13 +623,20 @@ bool sess_sendmsg(struct session *sess, const void *msg_, size_t msglen,
 	struct cld_packet *outpkt;
 	unsigned int n_pkts, i;
 	size_t pkt_len, msg_left = msglen;
-	struct session_outpkt *pkts[CLD_MAX_PKT_MSG], *op;
+	struct session_outpkt **pkts, *op;
 	GList *tmp_root = NULL;
 	const void *p;
 	bool first_frag = true;
 
+	if (msglen > CLD_MAX_MSG_SZ) {
+		HAIL_ERR(&srv_log, "%s: message too big at %d bytes\n",
+			__func__, msglen);
+		return false;
+	}
+
 	n_pkts = (msglen / CLD_MAX_PKT_MSG_SZ);
 	n_pkts += (msglen % CLD_MAX_PKT_MSG_SZ) ? 1 : 0;
+	pkts = alloca(sizeof(struct session_outpkt *) * n_pkts);
 
 	if (srv_log.verbose) {
 		const struct cld_msg_hdr *hdr = msg_;
@@ -671,9 +678,6 @@ bool sess_sendmsg(struct session *sess, const void *msg_, size_t msglen,
 		}
 	}
 
-	if (n_pkts > CLD_MAX_PKT_MSG)
-		return false;
-
 	/* pass 1: perform allocations */
 	for (i = 0; i < n_pkts; i++) {
 		pkts[i] = op = op_alloc(sizeof(*outpkt) +
-- 
1.6.2.5

--
To unsubscribe from this list: send the line "unsubscribe hail-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Fedora Clound]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux