📄 transport.c
字号:
/* * fs/cifs/transport.c * * Copyright (C) International Business Machines Corp., 2002,2007 * Author(s): Steve French (sfrench@us.ibm.com) * Jeremy Allison (jra@samba.org) 2006. * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <linux/fs.h>#include <linux/list.h>#include <linux/wait.h>#include <linux/net.h>#include <linux/delay.h>#include <asm/uaccess.h>#include <asm/processor.h>#include <linux/mempool.h>#include "cifspdu.h"#include "cifsglob.h"#include "cifsproto.h"#include "cifs_debug.h"extern mempool_t *cifs_mid_poolp;extern struct kmem_cache *cifs_oplock_cachep;static struct mid_q_entry *AllocMidQEntry(const struct smb_hdr *smb_buffer, struct cifsSesInfo *ses){ struct mid_q_entry *temp; if (ses == NULL) { cERROR(1, ("Null session passed in to AllocMidQEntry")); return NULL; } if (ses->server == NULL) { cERROR(1, ("Null TCP session in AllocMidQEntry")); return NULL; } temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp, GFP_KERNEL | GFP_NOFS); if (temp == NULL) return temp; else { memset(temp, 0, sizeof(struct mid_q_entry)); temp->mid = smb_buffer->Mid; /* always LE */ temp->pid = current->pid; temp->command = smb_buffer->Command; cFYI(1, ("For smb_command %d", temp->command)); /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */ /* when mid allocated can be before when sent */ temp->when_alloc = jiffies; temp->ses = ses; temp->tsk = current; } spin_lock(&GlobalMid_Lock); list_add_tail(&temp->qhead, &ses->server->pending_mid_q); atomic_inc(&midCount); temp->midState = MID_REQUEST_ALLOCATED; spin_unlock(&GlobalMid_Lock); return temp;}static voidDeleteMidQEntry(struct mid_q_entry *midEntry){#ifdef CONFIG_CIFS_STATS2 unsigned long now;#endif spin_lock(&GlobalMid_Lock); midEntry->midState = MID_FREE; list_del(&midEntry->qhead); atomic_dec(&midCount); spin_unlock(&GlobalMid_Lock); if (midEntry->largeBuf) cifs_buf_release(midEntry->resp_buf); else cifs_small_buf_release(midEntry->resp_buf);#ifdef CONFIG_CIFS_STATS2 now = jiffies; /* commands taking longer than one second are indications that something is wrong, unless it is quite a slow link or server */ if ((now - midEntry->when_alloc) > HZ) { if ((cifsFYI & CIFS_TIMER) && (midEntry->command != SMB_COM_LOCKING_ANDX)) { printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d", midEntry->command, midEntry->mid); printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n", now - midEntry->when_alloc, now - midEntry->when_sent, now - midEntry->when_received); } }#endif mempool_free(midEntry, cifs_mid_poolp);}struct oplock_q_entry *AllocOplockQEntry(struct inode *pinode, __u16 fid, struct cifsTconInfo *tcon){ struct oplock_q_entry *temp; if ((pinode == NULL) || (tcon == NULL)) { cERROR(1, ("Null parms passed to AllocOplockQEntry")); return NULL; } temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep, GFP_KERNEL); if (temp == NULL) return temp; else { temp->pinode = pinode; temp->tcon = tcon; temp->netfid = fid; spin_lock(&GlobalMid_Lock); list_add_tail(&temp->qhead, &GlobalOplock_Q); spin_unlock(&GlobalMid_Lock); } return temp;}void DeleteOplockQEntry(struct oplock_q_entry *oplockEntry){ spin_lock(&GlobalMid_Lock); /* should we check if list empty first? */ list_del(&oplockEntry->qhead); spin_unlock(&GlobalMid_Lock); kmem_cache_free(cifs_oplock_cachep, oplockEntry);}intsmb_send(struct socket *ssocket, struct smb_hdr *smb_buffer, unsigned int smb_buf_length, struct sockaddr *sin){ int rc = 0; int i = 0; struct msghdr smb_msg; struct kvec iov; unsigned len = smb_buf_length + 4; if (ssocket == NULL) return -ENOTSOCK; /* BB eventually add reconnect code here */ iov.iov_base = smb_buffer; iov.iov_len = len; smb_msg.msg_name = sin; smb_msg.msg_namelen = sizeof(struct sockaddr); smb_msg.msg_control = NULL; smb_msg.msg_controllen = 0; smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/ /* smb header is converted in header_assemble. bcc and rest of SMB word area, and byte area if necessary, is converted to littleendian in cifssmb.c and RFC1001 len is converted to bigendian in smb_send Flags2 is converted in SendReceive */ smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length); cFYI(1, ("Sending smb of length %d", smb_buf_length)); dump_smb(smb_buffer, len); while (len > 0) { rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len); if ((rc == -ENOSPC) || (rc == -EAGAIN)) { i++; /* smaller timeout here than send2 since smaller size */ /* Although it may not be required, this also is smaller oplock break time */ if (i > 12) { cERROR(1, ("sends on sock %p stuck for 7 seconds", ssocket)); rc = -EAGAIN; break; } msleep(1 << i); continue; } if (rc < 0) break; else i = 0; /* reset i after each successful send */ iov.iov_base += rc; iov.iov_len -= rc; len -= rc; } if (rc < 0) { cERROR(1, ("Error %d sending data on socket to server", rc)); } else { rc = 0; } /* Don't want to modify the buffer as a side effect of this call. */ smb_buffer->smb_buf_length = smb_buf_length; return rc;}static intsmb_send2(struct socket *ssocket, struct kvec *iov, int n_vec, struct sockaddr *sin){ int rc = 0; int i = 0; struct msghdr smb_msg; struct smb_hdr *smb_buffer = iov[0].iov_base; unsigned int len = iov[0].iov_len; unsigned int total_len; int first_vec = 0; unsigned int smb_buf_length = smb_buffer->smb_buf_length; if (ssocket == NULL) return -ENOTSOCK; /* BB eventually add reconnect code here */ smb_msg.msg_name = sin; smb_msg.msg_namelen = sizeof(struct sockaddr); smb_msg.msg_control = NULL; smb_msg.msg_controllen = 0; smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/ /* smb header is converted in header_assemble. bcc and rest of SMB word area, and byte area if necessary, is converted to littleendian in cifssmb.c and RFC1001 len is converted to bigendian in smb_send Flags2 is converted in SendReceive */ total_len = 0; for (i = 0; i < n_vec; i++) total_len += iov[i].iov_len; smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length); cFYI(1, ("Sending smb: total_len %d", total_len)); dump_smb(smb_buffer, len); while (total_len) { rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec], n_vec - first_vec, total_len); if ((rc == -ENOSPC) || (rc == -EAGAIN)) { i++; if (i >= 14) { cERROR(1, ("sends on sock %p stuck for 15 seconds", ssocket)); rc = -EAGAIN; break; } msleep(1 << i); continue; } if (rc < 0) break; if (rc >= total_len) { WARN_ON(rc > total_len); break; } if (rc == 0) { /* should never happen, letting socket clear before retrying is our only obvious option here */ cERROR(1, ("tcp sent no data")); msleep(500); continue; } total_len -= rc; /* the line below resets i */ for (i = first_vec; i < n_vec; i++) { if (iov[i].iov_len) { if (rc > iov[i].iov_len) { rc -= iov[i].iov_len; iov[i].iov_len = 0; } else { iov[i].iov_base += rc; iov[i].iov_len -= rc; first_vec = i; break; } } } i = 0; /* in case we get ENOSPC on the next send */ } if (rc < 0) { cERROR(1, ("Error %d sending data on socket to server", rc)); } else rc = 0; /* Don't want to modify the buffer as a side effect of this call. */ smb_buffer->smb_buf_length = smb_buf_length; return rc;}static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op){ if (long_op == CIFS_ASYNC_OP) { /* oplock breaks must not be held up */ atomic_inc(&ses->server->inFlight); } else { spin_lock(&GlobalMid_Lock); while (1) { if (atomic_read(&ses->server->inFlight) >= cifs_max_pending){ spin_unlock(&GlobalMid_Lock);#ifdef CONFIG_CIFS_STATS2 atomic_inc(&ses->server->num_waiters);#endif wait_event(ses->server->request_q, atomic_read(&ses->server->inFlight) < cifs_max_pending);#ifdef CONFIG_CIFS_STATS2 atomic_dec(&ses->server->num_waiters);#endif spin_lock(&GlobalMid_Lock); } else { if (ses->server->tcpStatus == CifsExiting) { spin_unlock(&GlobalMid_Lock); return -ENOENT; } /* can not count locking commands against total as they are allowed to block on server */ /* update # of requests on the wire to server */ if (long_op != CIFS_BLOCKING_OP) atomic_inc(&ses->server->inFlight); spin_unlock(&GlobalMid_Lock); break; } } } return 0;}static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf, struct mid_q_entry **ppmidQ){ if (ses->server->tcpStatus == CifsExiting) { return -ENOENT; } else if (ses->server->tcpStatus == CifsNeedReconnect) { cFYI(1, ("tcp session dead - return to caller to retry")); return -EAGAIN; } else if (ses->status != CifsGood) { /* check if SMB session is bad because we are setting it up */ if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) && (in_buf->Command != SMB_COM_NEGOTIATE)) { return -EAGAIN; } /* else ok - we are setting up session */ } *ppmidQ = AllocMidQEntry(in_buf, ses); if (*ppmidQ == NULL) return -ENOMEM; return 0;}static int wait_for_response(struct cifsSesInfo *ses, struct mid_q_entry *midQ, unsigned long timeout, unsigned long time_to_wait){ unsigned long curr_timeout; for (;;) { curr_timeout = timeout + jiffies; wait_event(ses->server->response_q, (!(midQ->midState == MID_REQUEST_SUBMITTED)) || time_after(jiffies, curr_timeout) || ((ses->server->tcpStatus != CifsGood) && (ses->server->tcpStatus != CifsNew))); if (time_after(jiffies, curr_timeout) && (midQ->midState == MID_REQUEST_SUBMITTED) && ((ses->server->tcpStatus == CifsGood) || (ses->server->tcpStatus == CifsNew))) { unsigned long lrt; /* We timed out. Is the server still sending replies ? */ spin_lock(&GlobalMid_Lock); lrt = ses->server->lstrp; spin_unlock(&GlobalMid_Lock); /* Calculate time_to_wait past last receive time. Although we prefer not to time out if the server is still responding - we will time out if the server takes more than 15 (or 45 or 180) seconds to respond to this request and has not responded to any request from other threads on the client within 10 seconds */ lrt += time_to_wait; if (time_after(jiffies, lrt)) { /* No replies for time_to_wait. */ cERROR(1, ("server not responding")); return -1; } } else { return 0; } }}/* * * Send an SMB Request. No response info (other than return code) * needs to be parsed. * * flags indicate the type of request buffer and how long to wait * and whether to log NT STATUS code (error) before mapping it to POSIX error * */intSendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses, struct smb_hdr *in_buf, int flags){ int rc; struct kvec iov[1]; int resp_buf_type; iov[0].iov_base = (char *)in_buf; iov[0].iov_len = in_buf->smb_buf_length + 4; flags |= CIFS_NO_RESP; rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);#ifdef CONFIG_CIFS_DEBUG2 cFYI(1, ("SendRcvNoR flags %d rc %d", flags, rc));#endif return rc;}intSendReceive2(const unsigned int xid, struct cifsSesInfo *ses, struct kvec *iov, int n_vec, int *pRespBufType /* ret */, const int flags){ int rc = 0; int long_op; unsigned int receive_len; unsigned long timeout; struct mid_q_entry *midQ; struct smb_hdr *in_buf = iov[0].iov_base; long_op = flags & CIFS_TIMEOUT_MASK; *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */ if ((ses == NULL) || (ses->server == NULL)) { cifs_small_buf_release(in_buf); cERROR(1, ("Null session")); return -EIO; } if (ses->server->tcpStatus == CifsExiting) { cifs_small_buf_release(in_buf); return -ENOENT; } /* Ensure that we do not send more than 50 overlapping requests to the same server. We may make this configurable later or use ses->maxReq */ rc = wait_for_free_request(ses, long_op); if (rc) { cifs_small_buf_release(in_buf); return rc; } /* make sure that we sign in the same order that we send on this socket and avoid races inside tcp sendmsg code that could cause corruption of smb data */ down(&ses->server->tcpSem); rc = allocate_mid(ses, in_buf, &midQ); if (rc) { up(&ses->server->tcpSem); cifs_small_buf_release(in_buf); /* Update # of requests on wire to server */ atomic_dec(&ses->server->inFlight); wake_up(&ses->server->request_q); return rc; } rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number); midQ->midState = MID_REQUEST_SUBMITTED;#ifdef CONFIG_CIFS_STATS2 atomic_inc(&ses->server->inSend);#endif rc = smb_send2(ses->server->ssocket, iov, n_vec, (struct sockaddr *) &(ses->server->addr.sockAddr));#ifdef CONFIG_CIFS_STATS2 atomic_dec(&ses->server->inSend); midQ->when_sent = jiffies;#endif up(&ses->server->tcpSem); cifs_small_buf_release(in_buf); if (rc < 0) goto out; if (long_op == CIFS_STD_OP) timeout = 15 * HZ; else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */ timeout = 180 * HZ; else if (long_op == CIFS_LONG_OP) timeout = 45 * HZ; /* should be greater than servers oplock break timeout (about 43 seconds) */ else if (long_op == CIFS_ASYNC_OP) goto out; else if (long_op == CIFS_BLOCKING_OP) timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */ else { cERROR(1, ("unknown timeout flag %d", long_op));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -