]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - fs/cifs/connect.c
CIFS: Simplify socket reading in demultiplex thread
[karo-tx-linux.git] / fs / cifs / connect.c
index e66297bad41231af902c99d386f654130b289020..a2e9fcf4a996507c8fec2bf55e9198417b98eb68 100644 (file)
@@ -319,25 +319,120 @@ requeue_echo:
        queue_delayed_work(system_nrt_wq, &server->echo, SMB_ECHO_INTERVAL);
 }
 
+static bool
+allocate_buffers(char **bigbuf, char **smallbuf, unsigned int size,
+                bool is_large_buf)
+{
+       char *bbuf = *bigbuf, *sbuf = *smallbuf;
+
+       if (bbuf == NULL) {
+               bbuf = (char *)cifs_buf_get();
+               if (!bbuf) {
+                       cERROR(1, "No memory for large SMB response");
+                       msleep(3000);
+                       /* retry will check if exiting */
+                       return false;
+               }
+       } else if (is_large_buf) {
+               /* we are reusing a dirty large buf, clear its start */
+               memset(bbuf, 0, size);
+       }
+
+       if (sbuf == NULL) {
+               sbuf = (char *)cifs_small_buf_get();
+               if (!sbuf) {
+                       cERROR(1, "No memory for SMB response");
+                       msleep(1000);
+                       /* retry will check if exiting */
+                       return false;
+               }
+               /* beginning of smb buffer is cleared in our buf_get */
+       } else {
+               /* if existing small buf clear beginning */
+               memset(sbuf, 0, size);
+       }
+
+       *bigbuf = bbuf;
+       *smallbuf = sbuf;
+
+       return true;
+}
+
+static int
+read_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg,
+                struct kvec *iov, unsigned int to_read,
+                unsigned int *ptotal_read, bool is_header_read)
+{
+       int length, rc = 0;
+       unsigned int total_read;
+       char *buf = iov->iov_base;
+
+       for (total_read = 0; total_read < to_read; total_read += length) {
+               length = kernel_recvmsg(server->ssocket, smb_msg, iov, 1,
+                                       to_read - total_read, 0);
+               if (server->tcpStatus == CifsExiting) {
+                       /* then will exit */
+                       rc = 2;
+                       break;
+               } else if (server->tcpStatus == CifsNeedReconnect) {
+                       cifs_reconnect(server);
+                       /* Reconnect wakes up rspns q */
+                       /* Now we will reread sock */
+                       rc = 1;
+                       break;
+               } else if (length == -ERESTARTSYS ||
+                          length == -EAGAIN ||
+                          length == -EINTR) {
+                       /*
+                        * Minimum sleep to prevent looping, allowing socket
+                        * to clear and app threads to set tcpStatus
+                        * CifsNeedReconnect if server hung.
+                        */
+                       usleep_range(1000, 2000);
+                       length = 0;
+                       if (!is_header_read)
+                               continue;
+                       /* Special handling for header read */
+                       if (total_read) {
+                               iov->iov_base = (to_read - total_read) +
+                                               buf;
+                               iov->iov_len = to_read - total_read;
+                               smb_msg->msg_control = NULL;
+                               smb_msg->msg_controllen = 0;
+                               rc = 3;
+                       } else
+                               rc = 1;
+                       break;
+               } else if (length <= 0) {
+                       cERROR(1, "Received no data, expecting %d",
+                              to_read - total_read);
+                       cifs_reconnect(server);
+                       rc = 1;
+                       break;
+               }
+       }
+
+       *ptotal_read = total_read;
+       return rc;
+}
+
 static int
 cifs_demultiplex_thread(void *p)
 {
        int length;
        struct TCP_Server_Info *server = p;
        unsigned int pdu_length, total_read;
+       char *buf = NULL, *bigbuf = NULL, *smallbuf = NULL;
        struct smb_hdr *smb_buffer = NULL;
-       struct smb_hdr *bigbuf = NULL;
-       struct smb_hdr *smallbuf = NULL;
        struct msghdr smb_msg;
        struct kvec iov;
-       struct socket *csocket = server->ssocket;
        struct list_head *tmp, *tmp2;
        struct task_struct *task_to_wake = NULL;
        struct mid_q_entry *mid_entry;
        char temp;
        bool isLargeBuf = false;
        bool isMultiRsp;
-       int reconnect;
+       int rc;
 
        current->flags |= PF_MEMALLOC;
        cFYI(1, "Demultiplex PID: %d", task_pid_nr(current));
@@ -351,35 +446,16 @@ cifs_demultiplex_thread(void *p)
        while (server->tcpStatus != CifsExiting) {
                if (try_to_freeze())
                        continue;
-               if (bigbuf == NULL) {
-                       bigbuf = cifs_buf_get();
-                       if (!bigbuf) {
-                               cERROR(1, "No memory for large SMB response");
-                               msleep(3000);
-                               /* retry will check if exiting */
-                               continue;
-                       }
-               } else if (isLargeBuf) {
-                       /* we are reusing a dirty large buf, clear its start */
-                       memset(bigbuf, 0, sizeof(struct smb_hdr));
-               }
 
-               if (smallbuf == NULL) {
-                       smallbuf = cifs_small_buf_get();
-                       if (!smallbuf) {
-                               cERROR(1, "No memory for SMB response");
-                               msleep(1000);
-                               /* retry will check if exiting */
-                               continue;
-                       }
-                       /* beginning of smb buffer is cleared in our buf_get */
-               } else /* if existing small buf clear beginning */
-                       memset(smallbuf, 0, sizeof(struct smb_hdr));
+               if (!allocate_buffers(&bigbuf, &smallbuf,
+                                     sizeof(struct smb_hdr), isLargeBuf))
+                       continue;
 
                isLargeBuf = false;
                isMultiRsp = false;
-               smb_buffer = smallbuf;
-               iov.iov_base = smb_buffer;
+               smb_buffer = (struct smb_hdr *)smallbuf;
+               buf = smallbuf;
+               iov.iov_base = buf;
                iov.iov_len = 4;
                smb_msg.msg_control = NULL;
                smb_msg.msg_controllen = 0;
@@ -393,52 +469,18 @@ incomplete_rcv:
                                  "Reconnecting...", server->hostname,
                                  (echo_retries * SMB_ECHO_INTERVAL / HZ));
                        cifs_reconnect(server);
-                       csocket = server->ssocket;
                        wake_up(&server->response_q);
                        continue;
                }
 
-               length =
-                   kernel_recvmsg(csocket, &smb_msg,
-                               &iov, 1, pdu_length, 0 /* BB other flags? */);
-
-               if (server->tcpStatus == CifsExiting) {
+               rc = read_from_socket(server, &smb_msg, &iov, pdu_length,
+                                     &total_read, true /* header read */);
+               if (rc == 3)
+                       goto incomplete_rcv;
+               else if (rc == 2)
                        break;
-               } else if (server->tcpStatus == CifsNeedReconnect) {
-                       cFYI(1, "Reconnect after server stopped responding");
-                       cifs_reconnect(server);
-                       cFYI(1, "call to reconnect done");
-                       csocket = server->ssocket;
-                       continue;
-               } else if (length == -ERESTARTSYS ||
-                          length == -EAGAIN ||
-                          length == -EINTR) {
-                       msleep(1); /* minimum sleep to prevent looping
-                               allowing socket to clear and app threads to set
-                               tcpStatus CifsNeedReconnect if server hung */
-                       if (pdu_length < 4) {
-                               iov.iov_base = (4 - pdu_length) +
-                                                       (char *)smb_buffer;
-                               iov.iov_len = pdu_length;
-                               smb_msg.msg_control = NULL;
-                               smb_msg.msg_controllen = 0;
-                               goto incomplete_rcv;
-                       } else
-                               continue;
-               } else if (length <= 0) {
-                       cFYI(1, "Reconnect after unexpected peek error %d",
-                               length);
-                       cifs_reconnect(server);
-                       csocket = server->ssocket;
-                       wake_up(&server->response_q);
+               else if (rc == 1)
                        continue;
-               } else if (length < pdu_length) {
-                       cFYI(1, "requested %d bytes but only got %d bytes",
-                                 pdu_length, length);
-                       pdu_length -= length;
-                       msleep(1);
-                       goto incomplete_rcv;
-               }
 
                /* The right amount was read from socket - 4 bytes */
                /* so we can now interpret the length field */
@@ -446,7 +488,7 @@ incomplete_rcv:
                /* the first byte big endian of the length field,
                is actually not part of the length but the type
                with the most common, zero, as regular data */
-               temp = *((char *) smb_buffer);
+               temp = *buf;
 
                /* Note that FC 1001 length is big endian on the wire,
                but we convert it here so it is always manipulated
@@ -475,15 +517,12 @@ incomplete_rcv:
                        cifs_set_port((struct sockaddr *)
                                        &server->dstaddr, CIFS_PORT);
                        cifs_reconnect(server);
-                       csocket = server->ssocket;
                        wake_up(&server->response_q);
                        continue;
                } else if (temp != (char) 0) {
                        cERROR(1, "Unknown RFC 1002 frame");
-                       cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
-                                     length);
+                       cifs_dump_mem(" Received Data: ", buf, length);
                        cifs_reconnect(server);
-                       csocket = server->ssocket;
                        continue;
                }
 
@@ -493,58 +532,25 @@ incomplete_rcv:
                        cERROR(1, "Invalid size SMB length %d pdu_length %d",
                                        length, pdu_length+4);
                        cifs_reconnect(server);
-                       csocket = server->ssocket;
                        wake_up(&server->response_q);
                        continue;
                }
 
                /* else length ok */
-               reconnect = 0;
-
                if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
                        isLargeBuf = true;
                        memcpy(bigbuf, smallbuf, 4);
-                       smb_buffer = bigbuf;
+                       smb_buffer = (struct smb_hdr *)bigbuf;
+                       buf = bigbuf;
                }
-               length = 0;
-               iov.iov_base = 4 + (char *)smb_buffer;
+
+               iov.iov_base = 4 + buf;
                iov.iov_len = pdu_length;
-               for (total_read = 0; total_read < pdu_length;
-                    total_read += length) {
-                       length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
-                                               pdu_length - total_read, 0);
-                       if (server->tcpStatus == CifsExiting) {
-                               /* then will exit */
-                               reconnect = 2;
-                               break;
-                       } else if (server->tcpStatus == CifsNeedReconnect) {
-                               cifs_reconnect(server);
-                               csocket = server->ssocket;
-                               /* Reconnect wakes up rspns q */
-                               /* Now we will reread sock */
-                               reconnect = 1;
-                               break;
-                       } else if (length == -ERESTARTSYS ||
-                                  length == -EAGAIN ||
-                                  length == -EINTR) {
-                               msleep(1); /* minimum sleep to prevent looping,
-                                             allowing socket to clear and app
-                                             threads to set tcpStatus
-                                             CifsNeedReconnect if server hung*/
-                               length = 0;
-                               continue;
-                       } else if (length <= 0) {
-                               cERROR(1, "Received no data, expecting %d",
-                                             pdu_length - total_read);
-                               cifs_reconnect(server);
-                               csocket = server->ssocket;
-                               reconnect = 1;
-                               break;
-                       }
-               }
-               if (reconnect == 2)
+               rc = read_from_socket(server, &smb_msg, &iov, pdu_length,
+                                     &total_read, false);
+               if (rc == 2)
                        break;
-               else if (reconnect == 1)
+               else if (rc == 1)
                        continue;
 
                total_read += 4; /* account for rfc1002 hdr */
@@ -562,8 +568,8 @@ incomplete_rcv:
                 */
                length = checkSMB(smb_buffer, smb_buffer->Mid, total_read);
                if (length != 0)
-                       cifs_dump_mem("Bad SMB: ", smb_buffer,
-                                       min_t(unsigned int, total_read, 48));
+                       cifs_dump_mem("Bad SMB: ", buf,
+                                     min_t(unsigned int, total_read, 48));
 
                mid_entry = NULL;
                server->lstrp = jiffies;
@@ -648,7 +654,7 @@ multi_t2_fnd:
                           !isMultiRsp) {
                        cERROR(1, "No task to wake, unknown frame received! "
                                   "NumMids %d", atomic_read(&midCount));
-                       cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
+                       cifs_dump_mem("Received Data is: ", buf,
                                      sizeof(struct smb_hdr));
 #ifdef CONFIG_CIFS_DEBUG2
                        cifs_dump_detail(smb_buffer);
@@ -687,7 +693,7 @@ multi_t2_fnd:
        msleep(125);
 
        if (server->ssocket) {
-               sock_release(csocket);
+               sock_release(server->ssocket);
                server->ssocket = NULL;
        }
        /* buffer usually freed in free_mid - need to free it here on exit */
@@ -3193,15 +3199,9 @@ mount_fail_check:
                else
                        cifs_put_tcp_session(srvTcp);
                bdi_destroy(&cifs_sb->bdi);
-               goto out;
        }
 
-       /* volume_info->password is freed above when existing session found
-       (in which case it is not needed anymore) but when new sesion is created
-       the password ptr is put in the new session structure (in which case the
-       password will be freed at unmount time) */
 out:
-       /* zero out password before freeing */
        FreeXid(xid);
        return rc;
 }