- Optimized the msg_list_item structure to make the buffer an
[rsync/rsync.git] / io.c
diff --git a/io.c b/io.c
index ba71098..4ade1ea 100644 (file)
--- a/io.c
+++ b/io.c
@@ -41,7 +41,6 @@
 
 extern int bwlimit;
 extern size_t bwlimit_writemax;
-extern int verbose;
 extern int io_timeout;
 extern int allowed_lull;
 extern int am_server;
@@ -103,8 +102,10 @@ static char io_filesfrom_buf[2048];
 static char *io_filesfrom_bp;
 static char io_filesfrom_lastchar;
 static int io_filesfrom_buflen;
-static size_t contiguous_write_len = 0;
+static int defer_forwarding_messages = 0;
 static int select_timeout = SELECT_TIMEOUT;
+static int active_filecnt = 0;
+static OFF_T active_bytecnt = 0;
 
 static void read_loop(int fd, char *buf, size_t len);
 
@@ -121,15 +122,15 @@ static struct flist_ndx_list redo_list, hlink_list;
 
 struct msg_list_item {
        struct msg_list_item *next;
-       char *buf;
        int len;
+       char buf[1];
 };
 
 struct msg_list {
        struct msg_list_item *head, *tail;
 };
 
-static struct msg_list msg_list;
+static struct msg_list msg2genr, msg2sndr;
 
 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
 {
@@ -225,23 +226,22 @@ void set_msg_fd_out(int fd)
 }
 
 /* Add a message to the pending MSG_* list. */
-static void msg_list_add(int code, char *buf, int len)
+static void msg_list_add(struct msg_list *lst, int code, char *buf, int len)
 {
-       struct msg_list_item *ml;
+       struct msg_list_item *m;
+       int sz = len + 4 + sizeof m[0] - 1;
 
-       if (!(ml = new(struct msg_list_item)))
+       if (!(m = (struct msg_list_item *)new_array(char, sz)))
                out_of_memory("msg_list_add");
-       ml->next = NULL;
-       if (!(ml->buf = new_array(char, len+4)))
-               out_of_memory("msg_list_add");
-       SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
-       memcpy(ml->buf+4, buf, len);
-       ml->len = len+4;
-       if (msg_list.tail)
-               msg_list.tail->next = ml;
+       m->next = NULL;
+       m->len = len + 4;
+       SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len);
+       memcpy(m->buf + 4, buf, len);
+       if (lst->tail)
+               lst->tail->next = m;
        else
-               msg_list.head = ml;
-       msg_list.tail = ml;
+               lst->head = m;
+       lst->tail = m;
 }
 
 /* Read a message from the MSG_* fd and handle it.  This is called either
@@ -279,6 +279,8 @@ static void read_msg_fd(void)
                        exit_cleanup(RERR_STREAMIO);
                }
                read_loop(fd, buf, 4);
+               if (remove_sent_files)
+                       decrement_active_files(IVAL(buf,0));
                flist_ndx_push(&redo_list, IVAL(buf,0));
                break;
        case MSG_DELETED:
@@ -287,7 +289,10 @@ static void read_msg_fd(void)
                        exit_cleanup(RERR_STREAMIO);
                }
                read_loop(fd, buf, len);
-               io_multiplex_write(MSG_DELETED, buf, len);
+               if (defer_forwarding_messages)
+                       msg_list_add(&msg2sndr, MSG_DELETED, buf, len);
+               else
+                       io_multiplex_write(MSG_DELETED, buf, len);
                break;
        case MSG_SUCCESS:
                if (len != 4 || !am_generator) {
@@ -295,11 +300,23 @@ static void read_msg_fd(void)
                        exit_cleanup(RERR_STREAMIO);
                }
                read_loop(fd, buf, len);
-               if (remove_sent_files)
-                       io_multiplex_write(MSG_SUCCESS, buf, len);
+               if (remove_sent_files) {
+                       decrement_active_files(IVAL(buf,0));
+                       if (defer_forwarding_messages)
+                               msg_list_add(&msg2sndr, MSG_SUCCESS, buf, len);
+                       else
+                               io_multiplex_write(MSG_SUCCESS, buf, len);
+               }
                if (preserve_hard_links)
                        flist_ndx_push(&hlink_list, IVAL(buf,0));
                break;
+       case MSG_SOCKERR:
+               if (!am_generator) {
+                       rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+                       exit_cleanup(RERR_STREAMIO);
+               }
+               close_multiplexing_out();
+               /* FALL THROUGH */
        case MSG_INFO:
        case MSG_ERROR:
        case MSG_LOG:
@@ -308,7 +325,10 @@ static void read_msg_fd(void)
                        if (n >= sizeof buf)
                                n = sizeof buf - 1;
                        read_loop(fd, buf, n);
-                       rwrite((enum logcode)tag, buf, n);
+                       if (am_generator && am_server && defer_forwarding_messages)
+                               msg_list_add(&msg2sndr, tag, buf, n);
+                       else
+                               rwrite((enum logcode)tag, buf, n);
                        len -= n;
                }
                break;
@@ -320,10 +340,32 @@ static void read_msg_fd(void)
        msg_fd_in = fd;
 }
 
+/* This is used by the generator to limit how many file transfers can
+ * be active at once when --remove-sent-files is specified.  Without
+ * this, sender-side deletions were mostly happening at the end. */
+void increment_active_files(int ndx, int itemizing, enum logcode code)
+{
+       /* TODO: tune these limits? */
+       while (active_filecnt >= (active_bytecnt >= 128*1024 ? 10 : 50)) {
+               if (hlink_list.head)
+                       check_for_finished_hlinks(itemizing, code);
+               read_msg_fd();
+       }
+
+       active_filecnt++;
+       active_bytecnt += the_file_list->files[ndx]->length;
+}
+
+void decrement_active_files(int ndx)
+{
+       active_filecnt--;
+       active_bytecnt -= the_file_list->files[ndx]->length;
+}
+
 /* Try to push messages off the list onto the wire.  If we leave with more
  * to do, return 0.  On error, return -1.  If everything flushed, return 1.
  * This is only active in the receiver. */
-static int msg_list_flush(int flush_it_all)
+static int msg2genr_flush(int flush_it_all)
 {
        static int written = 0;
        struct timeval tv;
@@ -332,9 +374,9 @@ static int msg_list_flush(int flush_it_all)
        if (msg_fd_out < 0)
                return -1;
 
-       while (msg_list.head) {
-               struct msg_list_item *ml = msg_list.head;
-               int n = write(msg_fd_out, ml->buf + written, ml->len - written);
+       while (msg2genr.head) {
+               struct msg_list_item *m = msg2genr.head;
+               int n = write(msg_fd_out, m->buf + written, m->len - written);
                if (n < 0) {
                        if (errno == EINTR)
                                continue;
@@ -348,12 +390,11 @@ static int msg_list_flush(int flush_it_all)
                        tv.tv_usec = 0;
                        if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
                                check_timeout();
-               } else if ((written += n) == ml->len) {
-                       free(ml->buf);
-                       msg_list.head = ml->next;
-                       if (!msg_list.head)
-                               msg_list.tail = NULL;
-                       free(ml);
+               } else if ((written += n) == m->len) {
+                       msg2genr.head = m->next;
+                       if (!msg2genr.head)
+                               msg2genr.tail = NULL;
+                       free(m);
                        written = 0;
                }
        }
@@ -366,8 +407,8 @@ void send_msg(enum msgcode code, char *buf, int len)
                io_multiplex_write(code, buf, len);
                return;
        }
-       msg_list_add(code, buf, len);
-       msg_list_flush(NORMAL_FLUSH);
+       msg_list_add(&msg2genr, code, buf, len);
+       msg2genr_flush(NORMAL_FLUSH);
 }
 
 int get_redo_num(int itemizing, enum logcode code)
@@ -464,7 +505,7 @@ static int read_timeout(int fd, char *buf, size_t len)
                FD_ZERO(&r_fds);
                FD_ZERO(&w_fds);
                FD_SET(fd, &r_fds);
-               if (msg_list.head) {
+               if (msg2genr.head) {
                        FD_SET(msg_fd_out, &w_fds);
                        if (msg_fd_out > maxfd)
                                maxfd = msg_fd_out;
@@ -501,8 +542,8 @@ static int read_timeout(int fd, char *buf, size_t len)
                        continue;
                }
 
-               if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
-                       msg_list_flush(NORMAL_FLUSH);
+               if (msg2genr.head && FD_ISSET(msg_fd_out, &w_fds))
+                       msg2genr_flush(NORMAL_FLUSH);
 
                if (io_filesfrom_f_out >= 0) {
                        if (io_filesfrom_buflen) {
@@ -580,9 +621,11 @@ static int read_timeout(int fd, char *buf, size_t len)
                                continue;
 
                        /* Don't write errors on a dead socket. */
-                       if (fd == sock_f_in)
+                       if (fd == sock_f_in) {
                                close_multiplexing_out();
-                       rsyserr(FERROR, errno, "read error");
+                               rsyserr(FSOCKERR, errno, "read error");
+                       } else
+                               rsyserr(FERROR, errno, "read error");
                        exit_cleanup(RERR_STREAMIO);
                }
 
@@ -765,12 +808,13 @@ static int readfd_unbuffered(int fd, char *buf, size_t len)
                        if (msg_bytes >= sizeof line)
                                goto overflow;
                        read_loop(fd, line, msg_bytes);
-                       line[msg_bytes] = '\0';
                        /* A directory name was sent with the trailing null */
                        if (msg_bytes > 0 && !line[msg_bytes-1])
                                log_delete(line, S_IFDIR);
-                       else
+                       else {
+                               line[msg_bytes] = '\0';
                                log_delete(line, S_IFREG);
+                       }
                        break;
                case MSG_SUCCESS:
                        if (msg_bytes != 4) {
@@ -911,6 +955,11 @@ int read_vstring(int f, char *buf, int bufsize)
 void read_sum_head(int f, struct sum_struct *sum)
 {
        sum->count = read_int(f);
+       if (sum->count < 0) {
+               rprintf(FERROR, "Invalid checksum count %ld [%s]\n",
+                       (long)sum->count, who_am_i());
+               exit_cleanup(RERR_PROTOCOL);
+       }
        sum->blength = read_int(f);
        if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
                rprintf(FERROR, "Invalid block length %ld [%s]\n",
@@ -973,7 +1022,7 @@ static void sleep_for_bwlimit(int bytes_written)
 
 #define ONE_SEC        1000000L /* # of microseconds in a second */
 
-       if (!bwlimit)
+       if (!bwlimit_writemax)
                return;
 
        total_written += bytes_written; 
@@ -1023,7 +1072,7 @@ static void writefd_unbuffered(int fd,char *buf,size_t len)
                FD_SET(fd,&w_fds);
                maxfd = fd;
 
-               if (msg_fd_in >= 0 && len-total >= contiguous_write_len) {
+               if (msg_fd_in >= 0) {
                        FD_ZERO(&r_fds);
                        FD_SET(msg_fd_in,&r_fds);
                        if (msg_fd_in > maxfd)
@@ -1052,8 +1101,19 @@ static void writefd_unbuffered(int fd,char *buf,size_t len)
                if (!FD_ISSET(fd, &w_fds))
                        continue;
 
+               if (msg2sndr.head && !defer_forwarding_messages) {
+                       struct msg_list_item *m = msg2sndr.head;
+                       if (!(msg2sndr.head = m->next))
+                               msg2sndr.tail = NULL;
+                       defer_forwarding_messages = 1;
+                       io_multiplex_write(IVAL(m->buf,0), m->buf+4, m->len-4);
+                       defer_forwarding_messages = 0;
+                       free(m);
+                       continue;
+               }
+
                n = len - total;
-               if (bwlimit && n > bwlimit_writemax)
+               if (bwlimit_writemax && n > bwlimit_writemax)
                        n = bwlimit_writemax;
                cnt = write(fd, buf + total, n);
 
@@ -1085,6 +1145,7 @@ static void writefd_unbuffered(int fd,char *buf,size_t len)
                }
 
                total += cnt;
+               defer_forwarding_messages = 1;
 
                if (fd == sock_f_out) {
                        if (io_timeout || am_generator)
@@ -1092,6 +1153,7 @@ static void writefd_unbuffered(int fd,char *buf,size_t len)
                        sleep_for_bwlimit(cnt);
                }
        }
+       defer_forwarding_messages = 0;
 
        no_flush--;
 }
@@ -1102,22 +1164,16 @@ static void writefd_unbuffered(int fd,char *buf,size_t len)
  **/
 static void mplex_write(enum msgcode code, char *buf, size_t len)
 {
-       char buffer[BIGPATHBUFLEN];
+       char buffer[1024];
        size_t n = len;
 
        SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
 
-       /* When the generator reads messages from the msg_fd_in pipe, it can
-        * cause output to occur down the socket.  Setting contiguous_write_len
-        * prevents the reading of msg_fd_in once we actually start to write
-        * this sequence of data (though we might read it before the start). */
-       if (am_generator && msg_fd_in >= 0)
-               contiguous_write_len = len + 4;
-
        if (n > sizeof buffer - 4)
-               n = sizeof buffer - 4;
+               n = 0;
+       else
+               memcpy(buffer + 4, buf, n);
 
-       memcpy(&buffer[4], buf, n);
        writefd_unbuffered(sock_f_out, buffer, n+4);
 
        len -= n;
@@ -1125,14 +1181,11 @@ static void mplex_write(enum msgcode code, char *buf, size_t len)
 
        if (len)
                writefd_unbuffered(sock_f_out, buf, len);
-
-       if (am_generator && msg_fd_in >= 0)
-               contiguous_write_len = 0;
 }
 
 void io_flush(int flush_it_all)
 {
-       msg_list_flush(flush_it_all);
+       msg2genr_flush(flush_it_all);
 
        if (!iobuf_out_cnt || no_flush)
                return;