⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transport.c

📁 Linux内核自带的cifs模块
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *   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 "cifspdu.h"#include "cifsglob.h"#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)#include <linux/mempool.h>#elseextern kmem_cache_t *cifs_mid_cachep;#endif#include "cifsproto.h"#include "cifs_debug.h"#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)extern mempool_t *cifs_mid_poolp;#endifextern 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;	}#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)	temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,						    GFP_KERNEL | GFP_NOFS);#else	temp = (struct mid_q_entry *) kmem_cache_alloc(cifs_mid_cachep,							SLAB_KERNEL);#endif	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#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)	mempool_free(midEntry, cifs_mid_poolp);#else	kmem_cache_free(cifs_mid_cachep, midEntry);#endif	}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 LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	mm_segment_t temp_fs;#endif	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);#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	smb_msg.msg_iov = &iov;	smb_msg.msg_iovlen = 1;#endif	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);#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	temp_fs = get_fs();	/* we must turn off socket api parm checking */	set_fs(get_ds());#endif	while (len > 0) {#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)		rc = sock_sendmsg(ssocket, &smb_msg, len);#else		rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);#endif		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 LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	set_fs(temp_fs);#endif	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 LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	mm_segment_t temp_fs;#endif	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);#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	temp_fs = get_fs();	/* we must turn off socket api parm checking */	set_fs(get_ds());#endif	while (total_len) {#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)		smb_msg.msg_iov = &iov[first_vec];		smb_msg.msg_iovlen = n_vec - first_vec;		rc = sock_sendmsg(ssocket, &smb_msg, total_len);#else		rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],				    n_vec - first_vec, total_len);#endif		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 LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)	set_fs(temp_fs);#endif	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 == -1) {		/* 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 < 3)					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;		}	}}intSendReceive2(const unsigned int xid, struct cifsSesInfo *ses,	     struct kvec *iov, int n_vec, int *pRespBufType /* ret */,	     const int long_op){	int rc = 0;	unsigned int receive_len;	unsigned long timeout;	struct mid_q_entry *midQ;	struct smb_hdr *in_buf = iov[0].iov_base;	*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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -