[SCM] Samba Shared Repository - branch v3-3-test updated - release-3-2-0pre2-4745-g8c75527

Volker Lendecke vlendec at samba.org
Mon Dec 29 11:09:15 GMT 2008


The branch, v3-3-test has been updated
       via  8c7552790934397c1bbb7e93eb64716a5fed31fa (commit)
       via  1cbe293e7ba16968bf0e106cb6ee723a1f31d5bb (commit)
       via  f77527188bdc40a77764451c2fb7bba511a1f5a2 (commit)
       via  efd5aa6a75c68c6fa48eaa928e652e3cacf0e18e (commit)
       via  886ebe3b6f7b5304ae9c185ee14a444977afac24 (commit)
      from  c1e0d89d00885dd227b9cec719f2fb3fbc69aea3 (commit)

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-3-test


- Log -----------------------------------------------------------------
commit 8c7552790934397c1bbb7e93eb64716a5fed31fa
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Dec 22 22:42:44 2008 +0100

    Add code to test write_data_iov a bit

commit 1cbe293e7ba16968bf0e106cb6ee723a1f31d5bb
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Dec 22 22:32:12 2008 +0100

    Attempt to fix bug 5953: Make cli_send_smb_direct_writeX use writev
    
    It seems there are SMB servers around which can't cope with the write header
    being sent in a packet of its own. With writev we keep the advantage of direct
    writes, giving the kernel the chance to coalesce the write calls.

commit f77527188bdc40a77764451c2fb7bba511a1f5a2
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Dec 22 22:17:59 2008 +0100

    Make write_data use write_data_iov

commit efd5aa6a75c68c6fa48eaa928e652e3cacf0e18e
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Dec 22 22:17:28 2008 +0100

    Add write_data_iov

commit 886ebe3b6f7b5304ae9c185ee14a444977afac24
Author: Volker Lendecke <vl at samba.org>
Date:   Sun Dec 21 23:22:30 2008 +0100

    Add sys_writev

-----------------------------------------------------------------------

Summary of changes:
 source/include/proto.h    |    2 +
 source/lib/system.c       |   25 ++++++++++
 source/lib/util_sock.c    |  115 ++++++++++++++++++++++++++++++++++++---------
 source/libsmb/clientgen.c |   40 +++++-----------
 4 files changed, 132 insertions(+), 50 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source/include/proto.h b/source/include/proto.h
index f6b1906..db47e2f 100644
--- a/source/include/proto.h
+++ b/source/include/proto.h
@@ -1037,6 +1037,7 @@ void *sys_memalign( size_t align, size_t size );
 int sys_usleep(long usecs);
 ssize_t sys_read(int fd, void *buf, size_t count);
 ssize_t sys_write(int fd, const void *buf, size_t count);
+ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt);
 ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off);
 ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off);
 ssize_t sys_send(int s, const void *msg, size_t len, int flags);
@@ -1575,6 +1576,7 @@ NTSTATUS read_socket_with_timeout(int fd, char *buf,
 				  size_t *size_ret);
 NTSTATUS read_data(int fd, char *buffer, size_t N);
 ssize_t write_data(int fd, const char *buffer, size_t N);
+ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt);
 bool send_keepalive(int client);
 NTSTATUS read_smb_length_return_keepalive(int fd, char *inbuf,
 					  unsigned int timeout,
diff --git a/source/lib/system.c b/source/lib/system.c
index eabb6d6..48ecf90 100644
--- a/source/lib/system.c
+++ b/source/lib/system.c
@@ -142,6 +142,31 @@ ssize_t sys_write(int fd, const void *buf, size_t count)
 }
 
 /*******************************************************************
+A writev wrapper that will deal with EINTR.
+********************************************************************/
+
+ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
+{
+	ssize_t ret;
+
+#if 0
+	/* Try to confuse write_data_iov a bit */
+	if ((random() % 5) == 0) {
+		return sys_write(fd, iov[0].iov_base, iov[0].iov_len);
+	}
+	if (iov[0].iov_len > 1) {
+		return sys_write(fd, iov[0].iov_base,
+				 (random() % (iov[0].iov_len-1)) + 1);
+	}
+#endif
+
+	do {
+		ret = writev(fd, iov, iovcnt);
+	} while (ret == -1 && errno == EINTR);
+	return ret;
+}
+
+/*******************************************************************
 A pread wrapper that will deal with EINTR and 64-bit file offsets.
 ********************************************************************/
 
diff --git a/source/lib/util_sock.c b/source/lib/util_sock.c
index e64b003..9c37e0c 100644
--- a/source/lib/util_sock.c
+++ b/source/lib/util_sock.c
@@ -1037,40 +1037,109 @@ NTSTATUS read_data(int fd, char *buffer, size_t N)
 }
 
 /****************************************************************************
- Write data to a fd.
+ Write all data from an iov array
 ****************************************************************************/
 
-ssize_t write_data(int fd, const char *buffer, size_t N)
+ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt)
 {
-	size_t total=0;
-	ssize_t ret;
-	char addr[INET6_ADDRSTRLEN];
+	int i;
+	size_t to_send;
+	ssize_t thistime;
+	size_t sent;
+	struct iovec *iov_copy, *iov;
 
-	while (total < N) {
-		ret = sys_write(fd,buffer + total,N - total);
+	to_send = 0;
+	for (i=0; i<iovcnt; i++) {
+		to_send += orig_iov[i].iov_len;
+	}
 
-		if (ret == -1) {
-			if (fd == get_client_fd()) {
-				/* Try and give an error message saying
-				 * what client failed. */
-				DEBUG(0,("write_data: write failure in "
-					"writing to client %s. Error %s\n",
-					get_peer_addr(fd,addr,sizeof(addr)),
-					strerror(errno) ));
-			} else {
-				DEBUG(0,("write_data: write failure. "
-					"Error = %s\n", strerror(errno) ));
+	thistime = sys_writev(fd, orig_iov, iovcnt);
+	if ((thistime <= 0) || (thistime == to_send)) {
+		return thistime;
+	}
+	sent = thistime;
+
+	/*
+	 * We could not send everything in one call. Make a copy of iov that
+	 * we can mess with. We keep a copy of the array start in iov_copy for
+	 * the TALLOC_FREE, because we're going to modify iov later on,
+	 * discarding elements.
+	 */
+
+	iov_copy = (struct iovec *)TALLOC_MEMDUP(
+		talloc_tos(), orig_iov, sizeof(struct iovec) * iovcnt);
+
+	if (iov_copy == NULL) {
+		errno = ENOMEM;
+		return -1;
+	}
+	iov = iov_copy;
+
+	while (sent < to_send) {
+		/*
+		 * We have to discard "thistime" bytes from the beginning
+		 * iov array, "thistime" contains the number of bytes sent
+		 * via writev last.
+		 */
+		while (thistime > 0) {
+			if (thistime < iov[0].iov_len) {
+				char *new_base =
+					(char *)iov[0].iov_base + thistime;
+				iov[0].iov_base = new_base;
+				iov[0].iov_len -= thistime;
+				break;
 			}
-			return -1;
+			thistime -= iov[0].iov_len;
+			iov += 1;
+			iovcnt -= 1;
 		}
 
-		if (ret == 0) {
-			return total;
+		thistime = sys_writev(fd, iov, iovcnt);
+		if (thistime <= 0) {
+			break;
 		}
+		sent += thistime;
+	}
+
+	TALLOC_FREE(iov_copy);
+	return sent;
+}
+
+/****************************************************************************
+ Write data to a fd.
+****************************************************************************/
+
+/****************************************************************************
+ Write data to a fd.
+****************************************************************************/
+
+ssize_t write_data(int fd, const char *buffer, size_t N)
+{
+	ssize_t ret;
+	struct iovec iov;
+
+	iov.iov_base = CONST_DISCARD(char *, buffer);
+	iov.iov_len = N;
+
+	ret = write_data_iov(fd, &iov, 1);
+	if (ret >= 0) {
+		return ret;
+	}
 
-		total += ret;
+	if (fd == get_client_fd()) {
+		char addr[INET6_ADDRSTRLEN];
+		/*
+		 * Try and give an error message saying what client failed.
+		 */
+		DEBUG(0, ("write_data: write failure in writing to client %s. "
+			  "Error %s\n", get_peer_addr(fd,addr,sizeof(addr)),
+			  strerror(errno)));
+	} else {
+		DEBUG(0,("write_data: write failure. Error = %s\n",
+			 strerror(errno) ));
 	}
-	return (ssize_t)total;
+
+	return -1;
 }
 
 /****************************************************************************
diff --git a/source/libsmb/clientgen.c b/source/libsmb/clientgen.c
index 2c0950d..b51eb47 100644
--- a/source/libsmb/clientgen.c
+++ b/source/libsmb/clientgen.c
@@ -315,7 +315,7 @@ bool cli_send_smb_direct_writeX(struct cli_state *cli,
 	/* First length to send is the offset to the data. */
 	size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
 	size_t nwritten=0;
-	ssize_t ret;
+	struct iovec iov[2];
 
 	/* fd == -1 causes segfaults -- Tom (tom at ninja.nl) */
 	if (cli->fd == -1) {
@@ -327,33 +327,19 @@ bool cli_send_smb_direct_writeX(struct cli_state *cli,
 		return false;
 	}
 
-	while (nwritten < len) {
-		ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
-		if (ret <= 0) {
-			close(cli->fd);
-			cli->fd = -1;
-			cli->smb_rw_error = SMB_WRITE_ERROR;
-			DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
-				(int)len,(int)ret, strerror(errno) ));
-			return false;
-		}
-		nwritten += ret;
-	}
+	iov[0].iov_base = cli->outbuf;
+	iov[0].iov_len = len;
+	iov[1].iov_base = CONST_DISCARD(char *, p);
+	iov[1].iov_len = extradata;
 
-	/* Now write the extra data. */
-	nwritten=0;
-	while (nwritten < extradata) {
-		ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
-		if (ret <= 0) {
-			close(cli->fd);
-			cli->fd = -1;
-			cli->smb_rw_error = SMB_WRITE_ERROR;
-			DEBUG(0,("Error writing %d extradata "
-				"bytes to client. %d (%s)\n",
-				(int)extradata,(int)ret, strerror(errno) ));
-			return false;
-		}
-		nwritten += ret;
+	nwritten = write_data_iov(cli->fd, iov, 2);
+	if (nwritten < (len + extradata)) {
+		close(cli->fd);
+		cli->fd = -1;
+		cli->smb_rw_error = SMB_WRITE_ERROR;
+		DEBUG(0,("Error writing %d bytes to client. (%s)\n",
+			 (int)(len+extradata), strerror(errno)));
+		return false;
 	}
 
 	/* Increment the mid so we can tell between responses. */


-- 
Samba Shared Repository


More information about the samba-cvs mailing list