Skip to contentSkip to conversation

cifs: [WIP] Overhaul message handling and improve nework transport

RFC PATCH In review David Howells 2 participants 38 messages May 19

Subsystems (8)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

cover cifs: [WIP] Overhaul message handling and improve nework transport

Hi Steve et al,

[!] NOTE: These patches are NOT FULLY WRITTEN, won't necessarily compile
all the way through and haven't been fully tested and this is intended as a preview of what I'm working on. Basic SMB2+ has worked as far as "cifs: Convert SMB2 Write request". Encrypted and signed messages should work but haven't been tested; compressed is disabled. Assume that anything beyond the specified point won't work.

SMB1 should work up to somewhere around "cifs: Rewrite base TCP transmission", but somewhere beyond that it won't compile. I need to go back and fix this up.

RDMA almost certainly won't work. Ideally, I would like to make RDMA message passing (rather than direct data transport) supply the received fragments in a bvecq to the message parsing routines.

The aim of this patchset is to build up a list of fragments for each request using a bvecq. These form a segmented list and can be spliced together when assembling a compound request. The segmented list can then be passed to sendmsg() with MSG_SPLICE_PAGES in a single call, thereby only having a single loop (in the TCP stack) to shovel data, rather than loops within loops. Possibly we can dispense with TCP corking also, provided we can tell the socket to flush the record boundaries. (Note that this also simplifies smbd_send() for RDMA).

To make this easier, I want to introduce a "request descriptor", which I'm calling "struct smb_message" and allocate it at a higher level, notably the PDU marshalling routines in cifssmb.c and smb2pdu.c and then hand that down into the transport. It will contain the list of fragments that form the message.

mid_q_struct is then 'absorbed' into smb_message. The transport then doesn't allocate these, but uses the ones that it is given and the I/O thread gets to simplify its refcounting and do less of it. The rule is that smb_message gets an extra ref when it is enqueued and whoever dequeues it gets this ref and either puts it or hands it on. The PDU encoding routines get a ref when allocating them and keep the refs until they complete.

smb_message is then given a next pointer to allow compounds to be trivially assembled, with the protocol wrangling being done in the transport. This next pointer also allows a bunch of fixed-size arrays to be got rid of (which were imposing weird restrictions like reducing the maximum component count of a compound if we stole a kvec[] slot for the transform header).

Request buffers will be allocated from a per-connection page frag allocator rather than from kmalloc(), thereby allowing them to be passed to MSG_SPLICE_PAGES.

To this end, I make the following significant changes. Note that some of the changes are a way to transit to a later stage.

(0) Make SMB1 transport use the SMB2 transport rather than having parallel dispatch code (now upstream).

(1) Make skb_splice_from_iter() special case ITER_BVECQ-type iterators and walk the bvecq directly rather than calling iov_iter_extract_pages(). This allows access to the information on the bvecq about whether a memory fragment is held by a page ref or by a pin - which is something sk_buff needs to take account of at some point.

(2) Provide netfslib facilities to splice the receive buffers directly out of a TCP socket into a bvecq, allowing the socket lock to be dropped earlier and reducing the amount of time sendmsg is held up.

(3) Replace mid_q_struct with smb_message and also include credits and smb_rqst therein.

(4) Rewrite cifs TCP transmission to be able to use MSG_SPLICE_PAGES:

(a) Copy all the data involved in a message into a big buffer formed of a sequence of pages attached to a bvecq.

(b) If encrypting the message just encrypt this buffer. Converting this to a scatterlist is much simpler (and uses less memory) than encrypting from the protocol elements.

(c) As the pages in the bvecq are just that, they have refcounts and can be passed to MSG_SPLICE_PAGES - thereby avoiding the copy in TCP.

(d) Compression should be a matter of vmap()'ing these pages to form the source buffer, allocating a second buffer of pages to form a dest buffer, also in a bvecq, vmapping that and then doing the compression. The first buffer can then just be replaced by the second.

(e) __smb_send_rqst() can then do a single sendmsg() with MSG_SPLICE_PAGES() from an ITER_BVECQ-type iterator.

(f) smbd_send() can push the same buffer to smbd_post_send_iter() from the same iterator.

(5) Rewrite cifs TCP reception to use the facility to splice the receive queue out of the socket and into a bvecq rather than using recvmsg() to read it. The bvecq is then processed through helper functions to parse incoming messages for both SMB1 and SMB2/3. This allows reading to be deferred to avoid blocking the I/O thread.

(6) Clean up mid->callback_data. Replace it with a waitqueue in smb_message (for most commands) and a cifs_io_subrequest pointer (for read and write). Make request completion wait on the smb_message waitqueue rather than on server->response_q to avoid thundering herd issues.

(Also, I note that under some circumstances, cifs just wakes up the first thing on server->response_q without any reference to *what* it is waking up).

(7) Add some more bits to smb_message to hold the buffers in a bvecq with the intent of killing of the smb_rqst struct.

(a) The PDU encoders will have to work out how much memory they need for the request protocol bits in advance and tell the smb_message allocator their requirements. This will get the requested amount from the netmem allocator, so it needs to be correctly sized. A pointer is then set in smb->request to the buffer.

(b) The smb_message is given a pointer (->next) to chain to another message to be compounded after it.

(c) smb_send_recv_messages() will be used to dispatch a synchronous request. If the head smb_message's ->next pointer is not NULL, it will set the appropriate compound chaining stuff and insert appropriate padding. Then it will link the bvecq structs of those messages together.

(8) Convert PDU encoders to allocate and use smb_message and pass it down.

(a) So far, SMB2 Negotiate Protocol, Session Setup, Logoff, Tree Connect, Tree Disconnect, Read and Write have been done - and though they build if SMB1 and compression are disabled, they won't work yet and so haven't been tested.

(b) SMB2 Posix Mkdir has been attempted and will compile, but is likely to need rejigging as it's a close associate of Create.

(c) SMB2 Create/Open is partially done and won't compile. This gets complicated because it's used in a lot of places and also gets compounded - so anything that gets compounded with it must also be converted.

The patches can be found here also:

https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=cifs-experimental

Thanks,
David

David Howells (36):
net: Perform special handling for a splice from a bvecq
netfs: Add a facility to splice TCP receive buffers into a bvecq
netfs: Add some TCP receive queue helpers
cifs, nls: Provide unicode size determination func
cifs: Introduce an ALIGN8() macro
cifs: Rename mid_q_entry to smb_message
cifs: Add "Has dynamic part" flag form SMB2/3 StructureSize LSB
cifs: Add an enum to hold a trace value for the command/subcommand
cifs: Institute message managing struct
cifs: Split crypt_message() into encrypt and decrypt variants
cifs: Add new AEAD alloc and setup routines that draw from an iterator
cifs: [WIP] Rewrite base Rx to put data off the socket into a bvecq
cifs: Remove validate_t2()
cifs: Remove cifs_io_subrequest::got_bytes
cifs: Pass smb_message to cifs_verify_signature()
cifs: Rewrite base TCP transmission
cifs: Don't use corking
cifs: Use page frag allocator for Tx buffers
cifs: Try to better handle the "Dynamic" flag in StructureSize2 in

    SMB2/3

cifs: Pass smb_message structs down into the transport layer
cifs: Add a tracepoint to trace the smb_message refcount
cifs: Trace smb1/2_copy_to_prepped_buffers()
cifs: Clean up mid->callback_data and kill off mid->creator
cifs: Add netmem allocation functions
cifs: Add more pieces to smb_message
cifs: Convert SMB2 Negotiate Protocol request
cifs: Convert SMB2 Session Setup request
cifs: Convert SMB2 Logoff request
cifs: Convert SMB2 Tree Connect request
cifs: Convert SMB2 Tree Disconnect request
cifs: Convert SMB2 Read request
cifs: Convert SMB2 Write request
cifs: [WIP] Don't copy new-style smb_messages to a set of pages
cifs: [WIP] Rearrange Create request subfuncs
cifs: [WIP] Convert SMB2 Posix Mkdir request
cifs: [WIP] Convert SMB2 Open request

 fs/netfs/Makefile             |    4 +
 fs/netfs/rxqueue.c            |  532 ++++++
 fs/netfs/tcp_splice.c         |  269 +++
 fs/nls/nls_base.c             |   33 +
 fs/smb/client/cached_dir.c    |   41 +-
 fs/smb/client/cifs_debug.c    |   53 +-
 fs/smb/client/cifs_debug.h    |    3 +-
 fs/smb/client/cifs_unicode.c  |   39 +
 fs/smb/client/cifs_unicode.h  |    2 +
 fs/smb/client/cifsencrypt.c   |    4 +-
 fs/smb/client/cifsfs.c        |   30 +-
 fs/smb/client/cifsglob.h      |  297 ++--
 fs/smb/client/cifsproto.h     |  168 +-
 fs/smb/client/cifssmb.c       |  345 ++--
 fs/smb/client/compress.c      |  155 +-
 fs/smb/client/compress.h      |   14 +-
 fs/smb/client/connect.c       |  707 ++++----
 fs/smb/client/ntlmssp.h       |    8 +-
 fs/smb/client/reparse.c       |    2 +-
 fs/smb/client/sess.c          |  306 ++--
 fs/smb/client/smb1debug.c     |   56 +-
 fs/smb/client/smb1encrypt.c   |  132 +-
 fs/smb/client/smb1maperror.c  |   15 +-
 fs/smb/client/smb1misc.c      |   22 +-
 fs/smb/client/smb1ops.c       |   96 +-
 fs/smb/client/smb1pdu.h       |   62 +-
 fs/smb/client/smb1proto.h     |   58 +-
 fs/smb/client/smb1session.c   |    4 +-
 fs/smb/client/smb1transport.c | 1154 +++++++++----
 fs/smb/client/smb2file.c      |    3 +-
 fs/smb/client/smb2inode.c     |    8 +-
 fs/smb/client/smb2maperror.c  |    3 +-
 fs/smb/client/smb2misc.c      |  423 +++--
 fs/smb/client/smb2ops.c       | 1190 ++------------
 fs/smb/client/smb2pdu.c       | 2889 +++++++++++++++++----------------
 fs/smb/client/smb2proto.h     |   83 +-
 fs/smb/client/smb2transport.c | 1172 +++++++++++--
 fs/smb/client/smbdirect.c     |  105 +-
 fs/smb/client/smbdirect.h     |    5 +-
 fs/smb/client/trace.h         |  180 ++
 fs/smb/client/transport.c     | 1254 ++++++++------
 fs/smb/common/smb2pdu.h       |   55 +-
 fs/smb/server/smb2pdu.c       |   22 +-
 include/linux/netfs.h         |   37 +
 include/linux/nls.h           |    1 +
 include/trace/events/netfs.h  |   28 +
 net/core/skbuff.c             |  119 ++
 47 files changed, 7135 insertions(+), 5053 deletions(-)
 create mode 100644 fs/netfs/rxqueue.c
 create mode 100644 fs/netfs/tcp_splice.c
author maintainer
#1 lore raw

RFC PATCH 1/36 net: Perform special handling for a splice from a bvecq

In skb_splice_from_iter() used for MSG_SPLICE_PAGES, if it sees that it is given a bvecq-based iterator, walk the bvecq directly rather than calling iov_iter_extract_pages() to extract into a separate table.

Note that the bvecq chain carries information about whether a page can be get_page'd or whether it has to be pinned (GUP), though the sk_buff can't currently make use of this information.

cc Eric Dumazet <[email protected]>
cc Neal Cardwell <[email protected]>
cc Kuniyuki Iwashima <[email protected]>
cc Mina Almasry <[email protected]>
cc Steve French <[email protected]>
cc Paulo Alcantara <[email protected]>
cc Shyam Prasad N <[email protected]>
cc Tom Talpey <[email protected]>
---
 net/core/skbuff.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 119 insertions(+)
net/core/skbuff.c +119 -0
index 7dad68e3b518..127c300ab938 100644
@@ -7295,6 +7295,122 @@ nodefer:	kfree_skb_napi_cache(skb);
 		kick_defer_list_purge(cpu);
 }
 
+static __always_inline
+size_t iterate_one_bvec(struct bio_vec *p, size_t skip, size_t len,
+			void *priv, void *priv2, iov_step_f step)
+{
+#ifdef CONFIG_HIGHMEM
+	size_t progress = 0;
+
+	if (skip >= p->bv_len)
+		return 0;
+
+	len = umin(len, p->bv_len - skip);
+	while (len > 0) {
+		size_t remain, consumed;
+		size_t offset = p->bv_offset + skip, part;
+		void *kaddr = kmap_local_bvec(p, skip);
+
+		part = umin(len, PAGE_SIZE - offset % PAGE_SIZE);
+		remain = step(kaddr, progress, part, priv, priv2);
+		kunmap_local(kaddr);
+
+		consumed = part - remain;
+		len -= consumed;
+		progress += consumed;
+		skip += consumed;
+		if (remain)
+			break;
+	}
+
+	return progress;
+#else
+	return len - step(bvec_virt(p) + skip, 0, len, priv, priv2);
+#endif
+}
+
+static __always_inline
+size_t csum_one(void *iter_from, size_t progress,
+		size_t len, void *priv, void *priv2)
+{
+	__wsum *csum = priv;
+
+	*csum = csum_partial(iter_from, len, *csum);
+	return 0;
+}
+
+static void skb_splice_csum_bv(struct sk_buff *skb, struct bio_vec *bv,
+			       size_t skip, size_t len)
+{
+	__wsum csum = 0;
+
+	size_t did = iterate_one_bvec(bv, skip, len, &csum, NULL, csum_one);
+
+	WARN_ON_ONCE(did != len);
+	skb->csum = csum_block_add(skb->csum, csum, len);
+}
+
+/*
+ * Splice from a bvecq iterator to an skbuff.
+ */
+static size_t skb_splice_from_bvecq(struct sk_buff *skb, struct iov_iter *iter,
+				    size_t len)
+{
+	const struct bvecq *bq = iter->bvecq;
+	unsigned int slot = iter->bvecq_slot;
+	size_t frag_limit = READ_ONCE(net_hotdata.sysctl_max_skb_frags);
+	size_t spliced = 0, skip = iter->iov_offset;
+
+	len = umin(len, iter->count);
+	if (!len)
+		return 0;
+	if (slot == bq->nr_slots) {
+		/* The iterator may have been extended. */
+		bq = bq->next;
+		slot = 0;
+	}
+
+	do {
+		struct bio_vec *bvec = &bq->bv[slot];
+
+		if (skip < bvec->bv_len) {
+			size_t part = umin(bvec->bv_len - skip, len);
+			size_t off = bvec->bv_offset + skip;
+			int ret = -EIO;
+
+			if (WARN_ON_ONCE(!sendpage_ok(bvec->bv_page)))
+				break;
+
+			ret = skb_append_pagefrags(skb, bvec->bv_page, off, part,
+						   frag_limit);
+			if (ret < 0)
+				return ret;
+
+			if (skb->ip_summed == CHECKSUM_NONE)
+				skb_splice_csum_bv(skb, bvec, off, part);
+
+			len -= part;
+			spliced += part;
+			skip += part;
+		}
+		if (skip >= bvec->bv_len) {
+			skip = 0;
+			slot++;
+			if (slot >= bq->nr_slots && bq->next) {
+				bq = bq->next;
+				slot = 0;
+			}
+		}
+	} while (len);
+
+	iter->bvecq_slot = slot;
+	iter->bvecq = bq;
+	iter->iov_offset = skip;
+	iter->count -= spliced;
+	skb_len_add(skb, spliced);
+	return spliced;
+}
+
 static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
 				 size_t offset, size_t len)
 {
@@ -7329,6 +7445,9 @@ ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
 	ssize_t spliced = 0, ret = 0;
 	unsigned int i;
 
+	if (iov_iter_is_bvecq(iter))
+		return skb_splice_from_bvecq(skb, iter, maxsize);
+
 	while (iter->count > 0) {
 		ssize_t space, nr, len;
 		size_t off;
author maintainer
#2 lore raw

RFC PATCH 2/36 netfs: Add a facility to splice TCP receive buffers into a bvecq

In reply to David Howells

Add a function by which receive buffers can be spliced from a TCP socket into a bvecq (bio_vec queue) allowing the caller to process the contained data without holding the socket lock. This is of particular interest where, say, a network filesystem has to copy a lot of data from a TCP socket from the response to a Read request - but holding the socket lock prevents messages from being sent.

---
 fs/netfs/Makefile     |   1 +
 fs/netfs/tcp_splice.c | 269 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/netfs.h |   6 +
 3 files changed, 276 insertions(+)
 create mode 100644 fs/netfs/tcp_splice.c
fs/netfs/Makefile +1 -0
index 421dd0be413b..9cfc3ccf46a0 100644
@@ -20,6 +20,7 @@ netfs-y := \
 
 netfs-$(CONFIG_NETFS_PGPRIV2) += read_pgpriv2.o
 netfs-$(CONFIG_NETFS_STATS) += stats.o
+netfs-$(CONFIG_INET) += tcp_splice.o
 
 netfs-$(CONFIG_FSCACHE) += \
 	fscache_cache.o \
fs/netfs/tcp_splice.c new file +269 -0

269 lines not on this page · raw message

include/linux/netfs.h +6 -0
index 86bef8fec14b..8fd23653a911 100644
@@ -23,6 +23,7 @@
 enum netfs_sreq_ref_trace;
 typedef struct mempool mempool_t;
 struct readahead_control;
+struct socket;
 struct netfs_io_request;
 struct netfs_io_subrequest;
 struct fscache_occupancy;
@@ -482,6 +483,11 @@ void netfs_end_io_write(struct inode *inode);
 int netfs_start_io_direct(struct inode *inode);
 void netfs_end_io_direct(struct inode *inode);
 
+/* TCP transport helper API. */
+#ifdef CONFIG_INET
+ssize_t netfs_tcp_splice_to_bvecq(struct socket *sock, struct bvecq *bvecq, size_t len);
+#endif
+
 /**
  * netfs_inode - Get the netfs inode context from the inode
  * @inode: The inode to query
author maintainer
#3 lore raw

RFC PATCH 3/36 netfs: Add some TCP receive queue helpers

In reply to David Howells

Add some helpers to splice buffers from a TCP receive queue to a private queue from where they can be processed without the need to hold the socket lock. This is particularly significant if a large amount of data is being copied, say for a read-data RPC call.

---
 fs/netfs/Makefile            |   5 +-
 fs/netfs/rxqueue.c           | 532 +++++++++++++++++++++++++++++++++++
 include/linux/netfs.h        |  31 ++
 include/trace/events/netfs.h |  28 ++
 4 files changed, 595 insertions(+), 1 deletion(-)
 create mode 100644 fs/netfs/rxqueue.c
fs/netfs/Makefile +4 -1
index 9cfc3ccf46a0..66e642650a5f 100644
@@ -20,7 +20,10 @@ netfs-y := \
 
 netfs-$(CONFIG_NETFS_PGPRIV2) += read_pgpriv2.o
 netfs-$(CONFIG_NETFS_STATS) += stats.o
-netfs-$(CONFIG_INET) += tcp_splice.o
+
+netfs-$(CONFIG_INET) += \
+	rxqueue.o \
+	tcp_splice.o
 
 netfs-$(CONFIG_FSCACHE) += \
 	fscache_cache.o \
fs/netfs/rxqueue.c new file +532 -0

532 lines not on this page · raw message

include/linux/netfs.h +31 -0
index 8fd23653a911..9f5168994a95 100644
@@ -422,6 +422,22 @@ struct netfs_cache_ops {
 			      enum netfs_cache_collect block_type);
 };
 
+/*
+ * (An excerpt from) a receive queue.  Data buffers can be spliced out of, say,
+ * a TCP socket into a sequence of these and then a variety of helpers used
+ * to manipulate them and extract data.
+ */
+struct netfs_rxqueue {
+	struct bvecq		*add_to;	/* Where to add data to the Rx queue */
+	struct bvecq		*take_from;	/* Where to take data from the Rx queue */
+	unsigned int		take_offset;	/* Current offset in rx_take_slot */
+	unsigned short		take_slot;	/* Current slot in rx_take_from */
+	bool			refillable;	/* T if refillable; F if excerpt */
+	unsigned int		qsize;		/* Amount of data in rx_queue */
+	unsigned int		pdu_remain;	/* Amount of current PDU left */
+	unsigned int		msg_id;		/* ID to log in tracepoints as MSG=xx */
+};
+
 /* High-level read API. */
 ssize_t netfs_unbuffered_read_iter_locked(struct kiocb *iocb, struct iov_iter *iter);
 ssize_t netfs_unbuffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
@@ -483,6 +499,21 @@ void netfs_end_io_write(struct inode *inode);
 int netfs_start_io_direct(struct inode *inode);
 void netfs_end_io_direct(struct inode *inode);
 
+/* Receive queue API. */
+struct bvecq *netfs_alloc_rx_bvecq(unsigned int nr_bv);
+void netfs_put_rx_bvecq(struct bvecq *bq);
+size_t netfs_rxqueue_read_iter(const struct netfs_rxqueue *rxq,
+			       struct iov_iter *dest, size_t skip, size_t amount);
+size_t netfs_rxqueue_read(const struct netfs_rxqueue *rxq,
+			  void *buffer, size_t skip, size_t amount);
+void netfs_rxqueue_discard(struct netfs_rxqueue *rxq, size_t amount);
+unsigned int netfs_rxqueue_count(const struct netfs_rxqueue *rxq, size_t amount);
+struct bvecq *netfs_rxqueue_decant(struct netfs_rxqueue *rxq, size_t amount);
+int netfs_rxqueue_tcp_refill(struct socket *tcp_sock, struct netfs_rxqueue *rxq,
+			     size_t min_size);
+int netfs_rxqueue_tcp_consume(struct socket *tcp_sock, struct netfs_rxqueue *rxq,
+			      size_t amount);
+
 /* TCP transport helper API. */
 #ifdef CONFIG_INET
 ssize_t netfs_tcp_splice_to_bvecq(struct socket *sock, struct bvecq *bvecq, size_t len);
include/trace/events/netfs.h +28 -0
index fbd000399b26..96a2c1f3880e 100644
@@ -855,6 +855,34 @@ TRACE_EVENT(netfs_bv_slot,
 		      __entry->pfn, __entry->offset, __entry->offset + __entry->len)
 	    );
 
+TRACE_EVENT(netfs_rxq_read,
+	    TP_PROTO(const struct netfs_rxqueue *rxq, unsigned int slot,
+		     size_t skip, size_t part, size_t copied),
+
+	    TP_ARGS(rxq, slot, skip, part, copied),
+
+	    TP_STRUCT__entry(
+		    __field(unsigned int,	msg_id)
+		    __field(unsigned int,	slot)
+		    __field(unsigned int,	skip)
+		    __field(unsigned int,	part)
+		    __field(unsigned int,	copied)
+			     ),
+
+	    TP_fast_assign(
+		    __entry->msg_id	= rxq->msg_id;
+		    __entry->slot	= slot;
+		    __entry->skip	= skip;
+		    __entry->part	= part;
+		    __entry->copied	= copied;
+			   ),
+
+	    TP_printk("MSG=%08x [%02x] b=%04x-%04x c=%04x",
+		      __entry->msg_id, __entry->slot,
+		      __entry->skip, __entry->skip + __entry->part,
+		      __entry->copied)
+	    );
+
 #undef EM
 #undef E_
 #endif /* _TRACE_NETFS_H */
author maintainer
#4 lore raw

RFC PATCH 4/36 cifs, nls: Provide unicode size determination func

In reply to David Howells
---
 fs/nls/nls_base.c            | 33 ++++++++++++++++++++++++++++++
 fs/smb/client/cifs_unicode.c | 39 ++++++++++++++++++++++++++++++++++++
 fs/smb/client/cifs_unicode.h |  2 ++
 include/linux/nls.h          |  1 +
 4 files changed, 75 insertions(+)
fs/nls/nls_base.c +33 -0
index a5c3a9f1b8dc..69bc266b4a49 100644
@@ -174,6 +174,39 @@ int utf8s_to_utf16s(const u8 *s, int inlen, enum utf16_endian endian,
 }
 EXPORT_SYMBOL(utf8s_to_utf16s);
 
+/**
+ * utf8s_to_len_utf16s - Determine the length of a conversion of UTF8 to UTF16.
+ * @s: The source utf8 string
+ * @inlen: The length of the string
+ */
+ssize_t utf8s_to_len_utf16s(const u8 *s, int inlen)
+{
+	unicode_t u;
+	size_t outcount = 0;
+	int size;
+
+	while (inlen > 0 && *s) {
+		if (*s & 0x80) {
+			size = utf8_to_utf32(s, inlen, &u);
+			if (size < 0)
+				return -EINVAL;
+			s += size;
+			inlen -= size;
+
+			if (u >= PLANE_SIZE)
+				outcount += 2;
+			else
+				outcount++;
+		} else {
+			s++;
+			outcount++;
+			inlen--;
+		}
+	}
+	return outcount * sizeof(wchar_t);
+}
+EXPORT_SYMBOL(utf8s_to_len_utf16s);
+
 static inline unsigned long get_utf16(unsigned c, enum utf16_endian endian)
 {
 	switch (endian) {
fs/smb/client/cifs_unicode.c +39 -0
index 4a8a591f4bca..f8fac73dcc1e 100644
@@ -276,6 +276,45 @@ cifs_strtoUTF16(__le16 *to, const char *from, int len,
 	return i;
 }
 
+/*
+ * Work out how long a string will be once converted to UTF16 in bytes.  This
+ * does not include a NUL terminator.
+ */
+size_t cifs_size_strtoUTF16(const char *from, int len,
+			    const struct nls_table *codepage)
+{
+	wchar_t wchar_to; /* needed to quiet sparse */
+	ssize_t out_len = 0;
+	int charlen;
+
+	/* special case for utf8 to handle no plane0 chars */
+	if (strcmp(codepage->charset, "utf8") == 0) {
+		out_len = utf8s_to_len_utf16s(from, len);
+		if (out_len >= 0)
+			goto success;
+		/*
+		 * On failure, fall back to UCS encoding as this function
+		 * should not return negative values currently can fail only if
+		 * source contains invalid encoded characters
+		 */
+	}
+
+	for (; len && *from; len -= charlen) {
+		charlen = codepage->char2uni(from, len, &wchar_to);
+		if (charlen < 1) {
+			cifs_dbg(VFS, "strtoUTF16: char2uni of 0x%x returned %d\n",
+				 *from, charlen);
+			/* Replace with a question mark */
+			charlen = 1;
+		}
+		from += charlen;
+		out_len += 2;
+	}
+
+success:
+	return out_len;
+}
+
 /*
  * cifs_utf16_bytes - how long will a string be after conversion?
  * @utf16 - pointer to input string
fs/smb/client/cifs_unicode.h +2 -0
index 3e9cd9acf0a9..f774e0dd7461 100644
@@ -61,6 +61,8 @@ int cifs_utf16_bytes(const __le16 *from, int maxbytes,
 		     const struct nls_table *codepage);
 int cifs_strtoUTF16(__le16 *to, const char *from, int len,
 		    const struct nls_table *codepage);
+size_t cifs_size_strtoUTF16(const char *from, int len,
+			    const struct nls_table *codepage);
 char *cifs_strndup_from_utf16(const char *src, const int maxlen,
 			      const bool is_unicode,
 			      const struct nls_table *codepage);
include/linux/nls.h +1 -0
index e0bf8367b274..026da1d5ffaa 100644
@@ -56,6 +56,7 @@ extern int utf8_to_utf32(const u8 *s, int len, unicode_t *pu);
 extern int utf32_to_utf8(unicode_t u, u8 *s, int maxlen);
 extern int utf8s_to_utf16s(const u8 *s, int len,
 		enum utf16_endian endian, wchar_t *pwcs, int maxlen);
+ssize_t utf8s_to_len_utf16s(const u8 *s, int inlen);
 extern int utf16s_to_utf8s(const wchar_t *pwcs, int len,
 		enum utf16_endian endian, u8 *s, int maxlen);
 
author maintainer
#5 lore raw

RFC PATCH 5/36 cifs: Introduce an ALIGN8() macro

In reply to David Howells

The PDU generation seems to do ALIGN(x, 8) a lot, so make a macro for that.

---
 fs/smb/client/cifsglob.h |  2 ++
 fs/smb/client/reparse.c  |  2 +-
 fs/smb/client/smb2file.c |  3 +--
 fs/smb/client/smb2misc.c |  2 +-
 fs/smb/client/smb2pdu.c  | 28 ++++++++++++++--------------
 5 files changed, 19 insertions(+), 18 deletions(-)
fs/smb/client/cifsglob.h +2 -0
index fc4028b5b5c8..69924b01dd2c 100644
@@ -2392,4 +2392,6 @@ static inline int cifs_open_create_options(unsigned int oflags, int opts)
  */
 #define CIFS_INO_BLOCKS(size) DIV_ROUND_UP_ULL((u64)(size), 512)
 
+#define ALIGN8(x) ALIGN((x), 8)
+
 #endif	/* _CIFS_GLOB_H */
fs/smb/client/reparse.c +1 -1
index cd1e1eaee67a..f6f9093e04fd 100644
@@ -575,7 +575,7 @@ static struct smb2_create_ea_ctx *ea_create_context(u32 dlen, size_t *cc_len)
 {
 	struct smb2_create_ea_ctx *cc;
 
-	*cc_len = round_up(sizeof(*cc) + dlen, 8);
+	*cc_len = ALIGN8(sizeof(*cc) + dlen);
 	cc = kzalloc(*cc_len, GFP_KERNEL);
 	if (!cc)
 		return ERR_PTR(-ENOMEM);
fs/smb/client/smb2file.c +1 -2
index 6860eff31693..152ede08cd09 100644
@@ -48,10 +48,9 @@ static struct smb2_symlink_err_rsp *symlink_data(const struct kvec *iov)
 			cifs_dbg(FYI, "%s: skipping unhandled error context: 0x%x\n",
 				 __func__, le32_to_cpu(p->ErrorId));
 
-			len = ALIGN(le32_to_cpu(p->ErrorDataLength), 8);
+			len = ALIGN8(le32_to_cpu(p->ErrorDataLength));
 			if (len > end - ((u8 *)p + sizeof(*p)))
 				return ERR_PTR(-EINVAL);
-
 			p = (struct smb2_error_context_rsp *)(p->ErrorContextData + len);
 		}
 	} else if (le32_to_cpu(err->ByteCount) >= sizeof(*sym) &&
fs/smb/client/smb2misc.c +1 -1
index 2a7355ce1a07..6194d677b0ba 100644
@@ -255,7 +255,7 @@ smb2_check_message(char *buf, unsigned int pdu_len, unsigned int len,
 		 * Some windows servers (win2016) will pad also the final
 		 * PDU in a compound to 8 bytes.
 		 */
-		if (ALIGN(calc_len, 8) == len)
+		if (ALIGN8(calc_len) == len)
 			return 0;
 
 		/*
fs/smb/client/smb2pdu.c +14 -14
index 971b075e2e7d..2d9fe803682e 100644
@@ -652,14 +652,14 @@ build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
 	/*
 	 * Context Data length must be rounded to multiple of 8 for some servers
 	 */
-	pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
+	pneg_ctxt->DataLength = cpu_to_le16(ALIGN8(sizeof(struct smb2_signing_capabilities) -
 					    sizeof(struct smb2_neg_context) +
-					    (num_algs * sizeof(u16)), 8));
+					    (num_algs * sizeof(u16))));
 	pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
 	pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
 
 	ctxt_len += sizeof(__le16) * num_algs;
-	ctxt_len = ALIGN(ctxt_len, 8);
+	ctxt_len = ALIGN8(ctxt_len);
 	return ctxt_len;
 	/* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
 }
@@ -696,7 +696,7 @@ build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
 	/* copy up to max of first 100 bytes of server name to NetName field */
 	pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
 	/* context size is DataLength + minimal smb2_neg_context */
-	return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
+	return ALIGN8(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context));
 }
 
 static void
@@ -742,18 +742,18 @@ assemble_neg_contexts(struct smb2_negotiate_req *req,
 	 * round up total_len of fixed part of SMB3 negotiate request to 8
 	 * byte boundary before adding negotiate contexts
 	 */
-	*total_len = ALIGN(*total_len, 8);
+	*total_len = ALIGN8(*total_len);
 
 	pneg_ctxt = (*total_len) + (char *)req;
 	req->NegotiateContextOffset = cpu_to_le32(*total_len);
 
 	build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
-	ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
+	ctxt_len = ALIGN8(sizeof(struct smb2_preauth_neg_context));
 	*total_len += ctxt_len;
 	pneg_ctxt += ctxt_len;
 
 	build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
-	ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
+	ctxt_len = ALIGN8(sizeof(struct smb2_encryption_neg_context));
 	*total_len += ctxt_len;
 	pneg_ctxt += ctxt_len;
 
@@ -782,7 +782,7 @@ assemble_neg_contexts(struct smb2_negotiate_req *req,
 	if (server->compression.requested) {
 		build_compression_ctxt((struct smb2_compression_capabilities_context *)
 				pneg_ctxt);
-		ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
+		ctxt_len = ALIGN8(sizeof(struct smb2_compression_capabilities_context));
 		*total_len += ctxt_len;
 		pneg_ctxt += ctxt_len;
 		neg_context_count++;
@@ -973,7 +973,7 @@ static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
 		 * aligned offset following the previous negotiate context.
 		 */
 		if (i + 1 != ctxt_cnt)
-			clen = ALIGN(clen, 8);
+			clen = ALIGN8(clen);
 		if (clen > len_of_ctxts)
 			break;
 
@@ -2702,7 +2702,7 @@ create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
 	unsigned int group_offset = 0;
 	struct smb3_acl acl = {};
 
-	*len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct smb_ace) * 4), 8);
+	*len = ALIGN8(sizeof(struct crt_sd_ctxt) + (sizeof(struct smb_ace) * 4));
 
 	if (set_owner) {
 		/* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
@@ -2777,7 +2777,7 @@ create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
 	memcpy(aclptr, &acl, sizeof(struct smb3_acl));
 
 	buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
-	*len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
+	*len = ALIGN8((unsigned int)(ptr - (__u8 *)buf));
 
 	return buf;
 }
@@ -2870,7 +2870,7 @@ alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
 	 * final path needs to be 8-byte aligned as specified in
 	 * MS-SMB2 2.2.13 SMB2 CREATE Request.
 	 */
-	*out_size = round_up(*out_len * sizeof(__le16), 8);
+	*out_size = ALIGN8(*out_len * sizeof(__le16));
 	*out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
 	if (!*out_path)
 		return -ENOMEM;
@@ -3141,7 +3141,7 @@ SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 		uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
 		/* MUST set path len (NameLength) to 0 opening root of share */
 		req->NameLength = cpu_to_le16(uni_path_len - 2);
-		copy_size = round_up(uni_path_len, 8);
+		copy_size = ALIGN8(uni_path_len);
 		copy_path = kzalloc(copy_size, GFP_KERNEL);
 		if (!copy_path)
 			return -ENOMEM;
@@ -4580,7 +4580,7 @@ smb2_new_read_req(void **buf, unsigned int *total_len,
 	if (request_type & CHAINED_REQUEST) {
 		if (!(request_type & END_OF_CHAIN)) {
 			/* next 8-byte aligned request */
-			*total_len = ALIGN(*total_len, 8);
+			*total_len = ALIGN8(*total_len);
 			shdr->NextCommand = cpu_to_le32(*total_len);
 		} else /* END_OF_CHAIN */
 			shdr->NextCommand = 0;
author maintainer
#6 lore raw

RFC PATCH 6/36 cifs: Rename mid_q_entry to smb_message

In reply to David Howells

Rename mid_q_entry to smb_message as future development that will allocate it at a higher level in the marshalling code and then hand it down into the transport layer. It will also be used to pass parameters such as the send and receive data buffers, credits and other things.

---
 fs/smb/client/cifs_debug.c    |  44 +++----
 fs/smb/client/cifsfs.c        |  30 ++---
 fs/smb/client/cifsglob.h      |  58 ++++----
 fs/smb/client/cifsproto.h     |  31 +++--
 fs/smb/client/cifssmb.c       |  36 ++---
 fs/smb/client/connect.c       | 150 ++++++++++-----------
 fs/smb/client/smb1maperror.c  |  12 +-
 fs/smb/client/smb1ops.c       |  40 +++---
 fs/smb/client/smb1proto.h     |  10 +-
 fs/smb/client/smb1transport.c |  86 ++++++------
 fs/smb/client/smb2misc.c      |   8 +-
 fs/smb/client/smb2ops.c       | 122 ++++++++---------
 fs/smb/client/smb2pdu.c       |  38 +++---
 fs/smb/client/smb2proto.h     |  10 +-
 fs/smb/client/smb2transport.c |  76 +++++------
 fs/smb/client/transport.c     | 240 +++++++++++++++++-----------------
 16 files changed, 495 insertions(+), 496 deletions(-)
fs/smb/client/cifs_debug.c +22 -22
index 4ed4f55a0bb7..2d0d26ee57ce 100644
@@ -38,33 +38,33 @@ cifs_dump_mem(char *label, void *data, int length)
 void cifs_dump_mids(struct TCP_Server_Info *server)
 {
 #ifdef CONFIG_CIFS_DEBUG2
-	struct mid_q_entry *mid_entry;
+	struct smb_message *smb;
 
 	if (server == NULL)
 		return;
 
 	cifs_dbg(VFS, "Dump pending requests:\n");
 	spin_lock(&server->mid_queue_lock);
-	list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
+	list_for_each_entry(smb, &server->pending_mid_q, qhead) {
 		cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d Cbdata: %p Mid %llu\n",
-			 mid_entry->mid_state,
-			 le16_to_cpu(mid_entry->command),
-			 mid_entry->pid,
-			 mid_entry->callback_data,
-			 mid_entry->mid);
+			 smb->mid_state,
+			 le16_to_cpu(smb->command),
+			 smb->pid,
+			 smb->callback_data,
+			 smb->mid);
 #ifdef CONFIG_CIFS_STATS2
 		cifs_dbg(VFS, "IsLarge: %d buf: %p time rcv: %ld now: %ld\n",
-			 mid_entry->large_buf,
-			 mid_entry->resp_buf,
-			 mid_entry->when_received,
+			 smb->large_buf,
+			 smb->resp_buf,
+			 smb->when_received,
 			 jiffies);
 #endif /* STATS2 */
 		cifs_dbg(VFS, "IsMult: %d IsEnd: %d\n",
-			 mid_entry->multiRsp, mid_entry->multiEnd);
-		if (mid_entry->resp_buf) {
-			server->ops->dump_detail(mid_entry->resp_buf,
-					 mid_entry->response_pdu_len, server);
-			cifs_dump_mem("existing buf: ", mid_entry->resp_buf, 62);
+			 smb->multiRsp, smb->multiEnd);
+		if (smb->resp_buf) {
+			server->ops->dump_detail(smb->resp_buf,
+						 smb->response_pdu_len, server);
+			cifs_dump_mem("existing buf: ", smb->resp_buf, 62);
 		}
 	}
 	spin_unlock(&server->mid_queue_lock);
@@ -437,7 +437,7 @@ static __always_inline const char *cipher_alg_str(__le16 cipher)
 
 static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
 {
-	struct mid_q_entry *mid_entry;
+	struct smb_message *smb;
 	struct TCP_Server_Info *server;
 	struct TCP_Server_Info *chan_server;
 	struct cifs_ses *ses;
@@ -694,13 +694,13 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
 				seq_printf(m, "\n\tServer ConnectionId: 0x%llx",
 					   chan_server->conn_id);
 				spin_lock(&chan_server->mid_queue_lock);
-				list_for_each_entry(mid_entry, &chan_server->pending_mid_q, qhead) {
+				list_for_each_entry(smb, &chan_server->pending_mid_q, qhead) {
 					seq_printf(m, "\n\t\tState: %d com: %d pid: %d cbdata: %p mid %llu",
-						   mid_entry->mid_state,
-						   le16_to_cpu(mid_entry->command),
-						   mid_entry->pid,
-						   mid_entry->callback_data,
-						   mid_entry->mid);
+						   smb->mid_state,
+						   le16_to_cpu(smb->command),
+						   smb->pid,
+						   smb->callback_data,
+						   smb->mid);
 				}
 				spin_unlock(&chan_server->mid_queue_lock);
 			}
fs/smb/client/cifsfs.c +15 -15
index f557eb7875c7..434c6943dd69 100644
@@ -405,13 +405,13 @@ static int cifs_permission(struct mnt_idmap *idmap,
 
 static struct kmem_cache *cifs_inode_cachep;
 static struct kmem_cache *cifs_req_cachep;
-static struct kmem_cache *cifs_mid_cachep;
+static struct kmem_cache *smb_message_cachep;
 static struct kmem_cache *cifs_sm_req_cachep;
 static struct kmem_cache *cifs_io_request_cachep;
 static struct kmem_cache *cifs_io_subrequest_cachep;
 mempool_t *cifs_sm_req_poolp;
 mempool_t *cifs_req_poolp;
-mempool_t cifs_mid_pool;
+mempool_t smb_message_pool;
 mempool_t cifs_io_request_pool;
 mempool_t cifs_io_subrequest_pool;
 
@@ -1820,27 +1820,27 @@ cifs_destroy_request_bufs(void)
 	kmem_cache_destroy(cifs_sm_req_cachep);
 }
 
-static int init_mids(void)
+static int init_smb_message(void)
 {
-	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
-					    sizeof(struct mid_q_entry), 0,
-					    SLAB_HWCACHE_ALIGN, NULL);
-	if (cifs_mid_cachep == NULL)
+	smb_message_cachep = kmem_cache_create("cifs_smb_message",
+					       sizeof(struct smb_message), 0,
+					       SLAB_HWCACHE_ALIGN, NULL);
+	if (smb_message_cachep == NULL)
 		return -ENOMEM;
 
 	/* 3 is a reasonable minimum number of simultaneous operations */
-	if (mempool_init_slab_pool(&cifs_mid_pool, 3, cifs_mid_cachep) < 0) {
-		kmem_cache_destroy(cifs_mid_cachep);
+	if (mempool_init_slab_pool(&smb_message_pool, 3, smb_message_cachep) < 0) {
+		kmem_cache_destroy(smb_message_cachep);
 		return -ENOMEM;
 	}
 
 	return 0;
 }
 
-static void destroy_mids(void)
+static void destroy_smb_message(void)
 {
-	mempool_exit(&cifs_mid_pool);
-	kmem_cache_destroy(cifs_mid_cachep);
+	mempool_exit(&smb_message_pool);
+	kmem_cache_destroy(smb_message_cachep);
 }
 
 static int cifs_init_netfs(void)
@@ -2015,7 +2015,7 @@ init_cifs(void)
 	if (rc)
 		goto out_destroy_inodecache;
 
-	rc = init_mids();
+	rc = init_smb_message();
 	if (rc)
 		goto out_destroy_netfs;
 
@@ -2072,7 +2072,7 @@ init_cifs(void)
 #endif
 	cifs_destroy_request_bufs();
 out_destroy_mids:
-	destroy_mids();
+	destroy_smb_message();
 out_destroy_netfs:
 	cifs_destroy_netfs();
 out_destroy_inodecache:
@@ -2114,7 +2114,7 @@ exit_cifs(void)
 	dfs_cache_destroy();
 #endif
 	cifs_destroy_request_bufs();
-	destroy_mids();
+	destroy_smb_message();
 	cifs_destroy_netfs();
 	cifs_destroy_inodecache();
 	destroy_workqueue(deferredclose_wq);
fs/smb/client/cifsglob.h +29 -29

148 lines not on this page · raw message

fs/smb/client/cifsproto.h +15 -16
index 79d891f7df1a..f88ae04af85d 100644
@@ -80,13 +80,12 @@ char *cifs_build_path_to_root(struct smb3_fs_context *ctx,
 			      struct cifs_sb_info *cifs_sb,
 			      struct cifs_tcon *tcon, int add_treename);
 char *cifs_build_devname(char *nodename, const char *prepath);
-void delete_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid);
-void __release_mid(struct TCP_Server_Info *server,
-		   struct mid_q_entry *midEntry);
+void delete_mid(struct TCP_Server_Info *server, struct smb_message *smb);
+void __release_mid(struct TCP_Server_Info *server, struct smb_message *smb);
 void cifs_wake_up_task(struct TCP_Server_Info *server,
-		       struct mid_q_entry *mid);
+		       struct smb_message *smb);
 int cifs_handle_standard(struct TCP_Server_Info *server,
-			 struct mid_q_entry *mid);
+			 struct smb_message *smb);
 char *smb3_fs_context_fullpath(const struct smb3_fs_context *ctx, char dirsep);
 int smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx);
 int cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs);
@@ -104,7 +103,7 @@ int compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
 		       struct TCP_Server_Info *server, const int flags,
 		       const int num_rqst, struct smb_rqst *rqst,
 		       int *resp_buf_type, struct kvec *resp_iov);
-int cifs_sync_mid_result(struct mid_q_entry *mid,
+int cifs_sync_mid_result(struct smb_message *mid,
 			 struct TCP_Server_Info *server);
 int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
 		    struct smb_rqst *rqst);
@@ -115,14 +114,14 @@ int cifs_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
 
 static inline int
 send_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-	    struct smb_rqst *rqst, struct mid_q_entry *mid,
+	    struct smb_rqst *rqst, struct smb_message *smb,
 	    unsigned int xid)
 {
 	return server->ops->send_cancel ?
-		server->ops->send_cancel(ses, server, rqst, mid, xid) : 0;
+		server->ops->send_cancel(ses, server, rqst, smb, xid) : 0;
 }
 
-int wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *mid);
+int wait_for_response(struct TCP_Server_Info *server, struct smb_message *smb);
 
 void smb2_query_server_interfaces(struct work_struct *work);
 void cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
@@ -222,7 +221,7 @@ unsigned int setup_special_mode_ACE(struct smb_ace *pntace, bool posix,
 				    __u64 nmode);
 unsigned int setup_special_user_owner_ACE(struct smb_ace *pntace);
 
-void dequeue_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid,
+void dequeue_mid(struct TCP_Server_Info *server, struct smb_message *smb,
 		 bool malformed);
 int cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
 			  unsigned int to_read);
@@ -334,7 +333,7 @@ struct cifs_ses *cifs_get_smb_ses(struct TCP_Server_Info *server,
 				  struct smb3_fs_context *ctx);
 
 int cifs_readv_receive(struct TCP_Server_Info *server,
-		       struct mid_q_entry *mid);
+		       struct smb_message *smb);
 
 int cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
 			  struct cifs_sb_info *cifs_sb,
@@ -471,15 +470,15 @@ static inline bool dfs_src_pathname_equal(const char *s1, const char *s2)
 	return true;
 }
 
-static inline void smb_get_mid(struct mid_q_entry *mid)
+static inline void smb_get_mid(struct smb_message *smb)
 {
-	refcount_inc(&mid->refcount);
+	refcount_inc(&smb->refcount);
 }
 
-static inline void release_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+static inline void release_mid(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	if (refcount_dec_and_test(&mid->refcount))
-		__release_mid(server, mid);
+	if (refcount_dec_and_test(&smb->refcount))
+		__release_mid(server, smb);
 }
 
 static inline void cifs_free_open_info(struct cifs_open_info_data *data)
fs/smb/client/cifssmb.c +18 -18
index 0b1051a17ca8..25f6289ee72f 100644
@@ -727,11 +727,11 @@ CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
  * FIXME: maybe we should consider checking that the reply matches request?
  */
 static void
-cifs_echo_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+cifs_echo_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
 	struct cifs_credits credits = { .value = 1, .instance = 0 };
 
-	release_mid(server, mid);
+	release_mid(server, smb);
 	add_credits(server, &credits, CIFS_ECHO_OP);
 }
 
@@ -1460,9 +1460,9 @@ CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
 }
 
 static void
-cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *rdata = mid->callback_data;
+	struct cifs_io_subrequest *rdata = smb->callback_data;
 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct inode *inode = &ictx->inode;
@@ -1478,7 +1478,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	unsigned int subreq_debug_index = rdata->subreq.debug_index;
 
 	cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%zu\n",
-		 __func__, mid->mid, mid->mid_state, rdata->result,
+		 __func__, smb->mid, smb->mid_state, rdata->result,
 		 rdata->subreq.len);
 
 	if (rdata->got_bytes)
@@ -1486,7 +1486,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 				    rdata->subreq.content.bvecq, rdata->subreq.content.slot,
 				    rdata->subreq.content.offset, rdata->subreq.len);
 
-	switch (mid->mid_state) {
+	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
 		/* result already set, check signature */
 		if (server->sign) {
@@ -1494,7 +1494,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 
 			iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
 			rc = cifs_verify_signature(&rqst, server,
-						  mid->sequence_number);
+						  smb->sequence_number);
 			if (rc)
 				cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
 					 rc);
@@ -1525,7 +1525,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	default:
 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_unknown);
 		rdata->result = smb_EIO1(smb_eio_trace_read_mid_state_unknown,
-					 mid->mid_state);
+					 smb->mid_state);
 		break;
 	}
 
@@ -1568,7 +1568,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	rdata->subreq.transferred += rdata->got_bytes;
 	trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
 	netfs_read_subreq_terminated(&rdata->subreq);
-	release_mid(server, mid);
+	release_mid(server, smb);
 	add_credits(server, &credits, 0);
 	trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
 			      server->credits, server->in_flight,
@@ -1885,11 +1885,11 @@ CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
  * workqueue completion task.
  */
 static void
-cifs_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+cifs_writev_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *wdata = mid->callback_data;
+	struct cifs_io_subrequest *wdata = smb->callback_data;
 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
-	WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
+	WRITE_RSP *rsp = (WRITE_RSP *)smb->resp_buf;
 	struct cifs_credits credits = {
 		.value = 1,
 		.instance = 0,
@@ -1899,15 +1899,15 @@ cifs_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	ssize_t result;
 	size_t written;
 
-	switch (mid->mid_state) {
+	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
-		result = cifs_check_receive(mid, tcon->ses->server, 0);
+		result = cifs_check_receive(smb, tcon->ses->server, 0);
 		if (result != 0)
 			break;
 
-		written = le16_to_cpu(smb->CountHigh);
+		written = le16_to_cpu(rsp->CountHigh);
 		written <<= 16;
-		written += le16_to_cpu(smb->Count);
+		written += le16_to_cpu(rsp->Count);
 		/*
 		 * Mask off high 16 bits when bytes written as returned
 		 * by the server is greater than bytes requested by the
@@ -1942,7 +1942,7 @@ cifs_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	default:
 		trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_unknown);
 		result = smb_EIO1(smb_eio_trace_write_mid_state_unknown,
-				  mid->mid_state);
+				  smb->mid_state);
 		break;
 	}
 
@@ -1952,7 +1952,7 @@ cifs_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 			      0, cifs_trace_rw_credits_write_response_clear);
 	wdata->credits.value = 0;
 	cifs_write_subrequest_terminated(wdata, result);
-	release_mid(server, mid);
+	release_mid(server, smb);
 	trace_smb3_rw_credits(credits.rreq_debug_id, credits.rreq_debug_index, 0,
 			      server->credits, server->in_flight,
 			      credits.value, cifs_trace_rw_credits_write_response_add);
fs/smb/client/connect.c +75 -75

304 lines not on this page · raw message

fs/smb/client/smb1maperror.c +6 -6
index 74530088d17d..c84f6256e146 100644
@@ -172,16 +172,16 @@ map_smb_to_linux_error(char *buf, bool logErr)
 
 int
 map_and_check_smb_error(struct TCP_Server_Info *server,
-			struct mid_q_entry *mid, bool logErr)
+			struct smb_message *smb, bool logErr)
 {
 	int rc;
-	struct smb_hdr *smb = (struct smb_hdr *)mid->resp_buf;
+	struct smb_hdr *rhdr = (struct smb_hdr *)smb->resp_buf;
 
-	rc = map_smb_to_linux_error((char *)smb, logErr);
-	if (rc == -EACCES && !(smb->Flags2 & SMBFLG2_ERR_STATUS)) {
+	rc = map_smb_to_linux_error((char *)rhdr, logErr);
+	if (rc == -EACCES && !(rhdr->Flags2 & SMBFLG2_ERR_STATUS)) {
 		/* possible ERRBaduid */
-		__u8 class = smb->Status.DosError.ErrorClass;
-		__u16 code = le16_to_cpu(smb->Status.DosError.Error);
+		__u8 class = rhdr->Status.DosError.ErrorClass;
+		__u16 code = le16_to_cpu(rhdr->Status.DosError.Error);
 
 		/* switch can be used to handle different errors */
 		if (class == ERRSRV && code == ERRbaduid) {
fs/smb/client/smb1ops.c +20 -20
index e198e3dda917..c1c16f6346fb 100644
@@ -139,7 +139,7 @@ void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
  */
 static int
 send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-	       struct smb_rqst *rqst, struct mid_q_entry *mid,
+	       struct smb_rqst *rqst, struct smb_message *smb,
 	       unsigned int xid)
 {
 	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
@@ -156,7 +156,7 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
 	iov[0].iov_len  = sizeof(struct smb_hdr) + 2;
 
 	cifs_server_lock(server);
-	rc = cifs_sign_rqst(&crqst, server, &mid->sequence_number);
+	rc = cifs_sign_rqst(&crqst, server, &smb->sequence_number);
 	if (rc) {
 		cifs_server_unlock(server);
 		return rc;
@@ -186,7 +186,7 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
  */
 static int
 send_lock_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-		 struct smb_rqst *rqst, struct mid_q_entry *mid,
+		 struct smb_rqst *rqst, struct smb_message *smb,
 		 unsigned int xid)
 {
 	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
@@ -212,12 +212,12 @@ send_lock_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
 }
 
 static int cifs_send_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-			    struct smb_rqst *rqst, struct mid_q_entry *mid,
+			    struct smb_rqst *rqst, struct smb_message *smb,
 			    unsigned int xid)
 {
-	if (mid->sr_flags & CIFS_WINDOWS_LOCK)
-		return send_lock_cancel(ses, server, rqst, mid, xid);
-	return send_nt_cancel(ses, server, rqst, mid, xid);
+	if (smb->sr_flags & CIFS_WINDOWS_LOCK)
+		return send_lock_cancel(ses, server, rqst, smb, xid);
+	return send_nt_cancel(ses, server, rqst, smb, xid);
 }
 
 static bool
@@ -243,20 +243,20 @@ cifs_read_data_length(char *buf, bool in_remaining)
 	       le16_to_cpu(rsp->DataLength);
 }
 
-static struct mid_q_entry *
+static struct smb_message *
 cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
 {
 	struct smb_hdr *buf = (struct smb_hdr *)buffer;
-	struct mid_q_entry *mid;
+	struct smb_message *smb;
 
 	spin_lock(&server->mid_queue_lock);
-	list_for_each_entry(mid, &server->pending_mid_q, qhead) {
-		if (compare_mid(mid->mid, buf) &&
-		    mid->mid_state == MID_REQUEST_SUBMITTED &&
-		    le16_to_cpu(mid->command) == buf->Command) {
-			smb_get_mid(mid);
+	list_for_each_entry(smb, &server->pending_mid_q, qhead) {
+		if (compare_mid(smb->mid, buf) &&
+		    smb->mid_state == MID_REQUEST_SUBMITTED &&
+		    le16_to_cpu(smb->command) == buf->Command) {
+			smb_get_mid(smb);
 			spin_unlock(&server->mid_queue_lock);
-			return mid;
+			return smb;
 		}
 	}
 	spin_unlock(&server->mid_queue_lock);
@@ -290,7 +290,7 @@ cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
 }
 
 static unsigned int
-cifs_get_credits(struct mid_q_entry *mid)
+cifs_get_credits(struct smb_message *smb)
 {
 	return 1;
 }
@@ -343,7 +343,7 @@ cifs_get_next_mid(struct TCP_Server_Info *server)
 	 * did not time out).
 	 */
 	while (cur_mid != last_mid) {
-		struct mid_q_entry *mid_entry;
+		struct smb_message *smb;
 		unsigned int num_mids;
 
 		collision = false;
@@ -352,10 +352,10 @@ cifs_get_next_mid(struct TCP_Server_Info *server)
 
 		num_mids = 0;
 		spin_lock(&server->mid_queue_lock);
-		list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
+		list_for_each_entry(smb, &server->pending_mid_q, qhead) {
 			++num_mids;
-			if (mid_entry->mid == cur_mid &&
-			    mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
+			if (smb->mid == cur_mid &&
+			    smb->mid_state == MID_REQUEST_SUBMITTED) {
 				/* This mid is in use, try a different one */
 				collision = true;
 				break;
fs/smb/client/smb1proto.h +5 -5
index 5f522d359952..4ecf50b0922c 100644
@@ -236,7 +236,7 @@ int cifs_verify_signature(struct smb_rqst *rqst,
 int map_smb_to_linux_error(char *buf, bool logErr);
 int smb1_init_maperror(void);
 int map_and_check_smb_error(struct TCP_Server_Info *server,
-			    struct mid_q_entry *mid, bool logErr);
+			    struct smb_message *smb, bool logErr);
 #if IS_ENABLED(CONFIG_SMB1_KUNIT_TESTS)
 extern const struct ntstatus_to_dos_err *ntstatus_to_dos_map_test;
 extern unsigned int ntstatus_to_dos_num;
@@ -280,13 +280,13 @@ int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
 /*
  * smb1transport.c
  */
-struct mid_q_entry *cifs_setup_async_request(struct TCP_Server_Info *server,
+struct smb_message *cifs_setup_async_request(struct TCP_Server_Info *server,
 					     struct smb_rqst *rqst);
 int SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
 		     char *in_buf, unsigned int in_len, int flags);
-int cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
+int cifs_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
 		       bool log_error);
-struct mid_q_entry *cifs_setup_request(struct cifs_ses *ses,
+struct smb_message *cifs_setup_request(struct cifs_ses *ses,
 				       struct TCP_Server_Info *server,
 				       struct smb_rqst *rqst);
 int SendReceive2(const unsigned int xid, struct cifs_ses *ses,
@@ -296,7 +296,7 @@ int SendReceive(const unsigned int xid, struct cifs_ses *ses,
 		struct smb_hdr *in_buf, unsigned int in_len,
 		struct smb_hdr *out_buf, int *pbytes_returned,
 		const int flags);
-bool cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
+bool cifs_check_trans2(struct smb_message *smb, struct TCP_Server_Info *server,
 		       char *buf, int malformed);
 int checkSMB(char *buf, unsigned int pdu_len, unsigned int total_read,
 	     struct TCP_Server_Info *server);
fs/smb/client/smb1transport.c +43 -43

194 lines not on this page · raw message

fs/smb/client/smb2misc.c +4 -4
index 6194d677b0ba..5c1d41baff7e 100644
@@ -843,14 +843,14 @@ smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
 }
 
 int
-smb2_handle_cancelled_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server)
+smb2_handle_cancelled_mid(struct smb_message *smb, struct TCP_Server_Info *server)
 {
-	struct smb2_hdr *hdr = mid->resp_buf;
-	struct smb2_create_rsp *rsp = mid->resp_buf;
+	struct smb2_hdr *hdr = smb->resp_buf;
+	struct smb2_create_rsp *rsp = smb->resp_buf;
 	struct cifs_tcon *tcon;
 	int rc;
 
-	if ((mid->optype & CIFS_CP_CREATE_CLOSE_OP) || hdr->Command != SMB2_CREATE ||
+	if ((smb->optype & CIFS_CP_CREATE_CLOSE_OP) || hdr->Command != SMB2_CREATE ||
 	    hdr->Status != STATUS_SUCCESS)
 		return 0;
 
fs/smb/client/smb2ops.c +61 -61

312 lines not on this page · raw message

fs/smb/client/smb2pdu.c +19 -19
index 2d9fe803682e..d4d2da2846e9 100644
@@ -4172,18 +4172,18 @@ SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
  * FIXME: maybe we should consider checking that the reply matches request?
  */
 static void
-smb2_echo_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+smb2_echo_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
+	struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)smb->resp_buf;
 	struct cifs_credits credits = { .value = 0, .instance = 0 };
 
-	if (mid->mid_state == MID_RESPONSE_RECEIVED
-	    || mid->mid_state == MID_RESPONSE_MALFORMED) {
+	if (smb->mid_state == MID_RESPONSE_RECEIVED
+	    || smb->mid_state == MID_RESPONSE_MALFORMED) {
 		credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
 		credits.instance = server->reconnect_instance;
 	}
 
-	release_mid(server, mid);
+	release_mid(server, smb);
 	add_credits(server, &credits, CIFS_ECHO_OP);
 }
 
@@ -4606,9 +4606,9 @@ smb2_new_read_req(void **buf, unsigned int *total_len,
 }
 
 static void
-smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *rdata = mid->callback_data;
+	struct cifs_io_subrequest *rdata = smb->callback_data;
 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct smb2_hdr *shdr = (struct smb2_hdr *)rdata->iov[0].iov_base;
@@ -4633,15 +4633,15 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 		  rdata->server, server);
 
 	cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%zu/%zu\n",
-		 __func__, mid->mid, mid->mid_state, rdata->result,
+		 __func__, smb->mid, smb->mid_state, rdata->result,
 		 rdata->got_bytes, rdata->subreq.len - rdata->subreq.transferred);
 
-	switch (mid->mid_state) {
+	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
 		credits.value = le16_to_cpu(shdr->CreditRequest);
 		credits.instance = server->reconnect_instance;
 		/* result already set, check signature */
-		if (server->sign && !mid->decrypted) {
+		if (server->sign && !smb->decrypted) {
 			int rc;
 
 			iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
@@ -4688,7 +4688,7 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	default:
 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_unknown);
 		rdata->result = smb_EIO1(smb_eio_trace_read_mid_state_unknown,
-					 mid->mid_state);
+					 smb->mid_state);
 		break;
 	}
 #ifdef CONFIG_CIFS_SMB_DIRECT
@@ -4750,7 +4750,7 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	rdata->subreq.transferred += rdata->got_bytes;
 	trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
 	netfs_read_subreq_terminated(&rdata->subreq);
-	release_mid(server, mid);
+	release_mid(server, smb);
 	trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
 			      server->credits, server->in_flight,
 			      credits.value, cifs_trace_rw_credits_read_response_add);
@@ -4946,11 +4946,11 @@ SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  * workqueue completion task.
  */
 static void
-smb2_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
+smb2_writev_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *wdata = mid->callback_data;
+	struct cifs_io_subrequest *wdata = smb->callback_data;
 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
-	struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
+	struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)smb->resp_buf;
 	struct cifs_credits credits = {
 		.value = 0,
 		.instance = 0,
@@ -4966,11 +4966,11 @@ smb2_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 		  "wdata server %p != mid server %p",
 		  wdata->server, server);
 
-	switch (mid->mid_state) {
+	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
 		credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
 		credits.instance = server->reconnect_instance;
-		result = smb2_check_receive(mid, server, 0);
+		result = smb2_check_receive(smb, server, 0);
 		if (result != 0) {
 			if (is_replayable_error(result)) {
 				trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_retry_needed);
@@ -5021,7 +5021,7 @@ smb2_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 	default:
 		trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_unknown);
 		result = smb_EIO1(smb_eio_trace_write_mid_state_unknown,
-				  mid->mid_state);
+				  smb->mid_state);
 		break;
 	}
 #ifdef CONFIG_CIFS_SMB_DIRECT
@@ -5070,7 +5070,7 @@ smb2_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 		wdata->replay = true;
 
 	cifs_write_subrequest_terminated(wdata, result ?: written);
-	release_mid(server, mid);
+	release_mid(server, smb);
 	trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
 			      server->credits, server->in_flight,
 			      credits.value, cifs_trace_rw_credits_write_response_add);
fs/smb/client/smb2proto.h +5 -5
index 1ceb95b907e6..5727e47733c8 100644
@@ -38,19 +38,19 @@ __le16 *cifs_convert_path_to_utf16(const char *from,
 
 int smb2_verify_signature(struct smb_rqst *rqst,
 			  struct TCP_Server_Info *server);
-int smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
+int smb2_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
 		       bool log_error);
-struct mid_q_entry *smb2_setup_request(struct cifs_ses *ses,
+struct smb_message *smb2_setup_request(struct cifs_ses *ses,
 				       struct TCP_Server_Info *server,
 				       struct smb_rqst *rqst);
-struct mid_q_entry *smb2_setup_async_request(struct TCP_Server_Info *server,
+struct smb_message *smb2_setup_async_request(struct TCP_Server_Info *server,
 					     struct smb_rqst *rqst);
 struct cifs_tcon *smb2_find_smb_tcon(struct TCP_Server_Info *server,
 				     __u64 ses_id, __u32  tid);
 __le32 smb2_get_lease_state(struct cifsInodeInfo *cinode, unsigned int oplock);
 bool smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server);
 int smb3_handle_read_data(struct TCP_Server_Info *server,
-			  struct mid_q_entry *mid);
+			  struct smb_message *smb);
 struct inode *smb2_create_reparse_inode(struct cifs_open_info_data *data,
 					struct super_block *sb,
 					const unsigned int xid,
@@ -222,7 +222,7 @@ int SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
 		      __u8 oplock_level);
 int smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
 				__u64 volatile_fid);
-int smb2_handle_cancelled_mid(struct mid_q_entry *mid,
+int smb2_handle_cancelled_mid(struct smb_message *smb,
 			      struct TCP_Server_Info *server);
 void smb2_cancelled_close_fid(struct work_struct *work);
 int SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
fs/smb/client/smb2transport.c +38 -38

176 lines not on this page · raw message

fs/smb/client/transport.c +120 -120

517 lines not on this page · raw message

author maintainer
#7 lore raw

RFC PATCH 7/36 cifs: Add "Has dynamic part" flag form SMB2/3 StructureSize LSB

In reply to David Howells

The LSB in the StructureSize that follows the smb2/3_hdr struct isn't really part of the length, but is rather a flag indicating that the operation structure is dynamic in size, so define a symbol for it.

---
 fs/smb/common/smb2pdu.h | 1 +
 1 file changed, 1 insertion(+)
fs/smb/common/smb2pdu.h +1 -0
index aeb0a245c532..dc5ec2643669 100644
@@ -185,6 +185,7 @@ struct smb3_hdr_req {
 struct smb2_pdu {
 	struct smb2_hdr hdr;
 	__le16 StructureSize2; /* size of wct area (varies, request specific) */
+#define SMB2_STRUCT_HAS_DYNAMIC_PART   0x01
 } __packed;
 
 #define SMB2_ERROR_STRUCTURE_SIZE2	9
author maintainer
#8 lore raw

RFC PATCH 8/36 cifs: Add an enum to hold a trace value for the command/subcommand

In reply to David Howells
---
 fs/smb/client/trace.h | 5 +++++
 1 file changed, 5 insertions(+)
fs/smb/client/trace.h +5 -0
index b99ec5a417fa..ede61d8a192c 100644
@@ -20,6 +20,9 @@
 /*
  * Specify enums for tracing information.
  */
+#define smb_command_traces \
+	E_(smb_unknown_command,				"unknown-command")
+
 #define smb_eio_traces \
 	EM(smb_eio_trace_compress_copy,			"compress_copy") \
 	EM(smb_eio_trace_copychunk_inv_rsp,		"copychunk_inv_rsp") \
@@ -211,6 +214,7 @@
 #define EM(a, b) a,
 #define E_(a, b) a
 
+enum smb_command_trace		{ smb_command_traces } __mode(byte);
 enum smb_eio_trace		{ smb_eio_traces } __mode(byte);
 enum smb3_rw_credits_trace	{ smb3_rw_credits_traces } __mode(byte);
 enum smb3_tcon_ref_trace	{ smb3_tcon_ref_traces } __mode(byte);
@@ -225,6 +229,7 @@ enum smb3_tcon_ref_trace	{ smb3_tcon_ref_traces } __mode(byte);
 #define EM(a, b) TRACE_DEFINE_ENUM(a);
 #define E_(a, b) TRACE_DEFINE_ENUM(a);
 
+smb_command_traces;
 smb_eio_traces;
 smb3_rw_credits_traces;
 smb3_tcon_ref_traces;
author maintainer
#9 lore raw

RFC PATCH 9/36 cifs: Institute message managing struct

In reply to David Howells

Turn the smb_message struct into a message handling struct to aid in building an SMB message, queuing them and holding the resources and buffers. It has absorbed the mid_q_struct and now other fields are added.

The idea is that the smb_message struct will be allocated and filled in much higher up (typically in the PDU encoding code) and passed down to the transport.

In particular, the following fields:

(*) ->next: This is used to link together messages into compounds and then
walk through the message list.

(*) ->credits: The credit requirements for the message.

(*) ->request: Pointer to the smb_hdr struct for the request.

(*) ->command_trace: An ID of the command type for use in tracing.

(*) ->total_len: The total length of the request message, not including
rfc1002 or transform headers.

(*) ->response: Pointer to the smb_hdr struct for the response.

(*) ->rqst, ->resp_buf_type, ->resp_iov: The old request info stuff.

Functions are provided to get and put refs upon the struct and also to drop all the refs on a compound string of structs.

---
 fs/smb/client/cifsglob.h      | 104 ++++++++++++++++++++++++----------
 fs/smb/client/cifsproto.h     |  11 ++--
 fs/smb/client/connect.c       |   6 +-
 fs/smb/client/smb1ops.c       |   2 +-
 fs/smb/client/smb1transport.c |   2 +-
 fs/smb/client/smb2ops.c       |   2 +-
 fs/smb/client/smb2transport.c |   4 +-
 fs/smb/client/transport.c     |  42 +++++++++++++-
 8 files changed, 127 insertions(+), 46 deletions(-)
fs/smb/client/cifsglob.h +73 -31
index a5e12a2eefdd..0f876feb0dbf 100644
@@ -1701,40 +1701,82 @@ typedef void (*mid_callback_t)(struct TCP_Server_Info *srv, struct smb_message *
 typedef int (*mid_handle_t)(struct TCP_Server_Info *server,
 			    struct smb_message *smb);
 
-/* one of these for every pending CIFS request to the server */
+/*
+ * Definition of an SMB request message to be transmitted.  These may be
+ * chained together and will automatically be turned into compound messages if
+ * they are.
+ *
+ *	+-----------------------+
+ *	| NetBIOS/padding	|
+ *	+-----------------------+ <--- smb->request + pre_offset
+ *	| (Transform header)	|
+ *	+-----------------------+ <--- smb->request
+ *	| SMB2 Header		| }				 }
+ *	+-----------------------+ } header_size			 }
+ *	| Req/Rsp struct	| }				 }
+ *	+-----------------------+ <--- smb->request + ext_offset } protocol_size
+ *	|			|				 }
+ *	| Extra protocol data	|				 }
+ *	|			|				 }
+ *	+-----------------------+ <--- smb->request + smb->data_offset
+ *	|			|
+ *	| Data Payload		|  data_size
+ *	|			|
+ *	+-----------------------+
+ *
+ *
+ * If the data is to be RDMA'd, it will be kept separate from the protocol.
+ */
 struct smb_message {
-	struct list_head qhead;	/* mids waiting on reply from this server */
-	refcount_t refcount;
-	__u64 mid;		/* multiplex id */
-	__u16 credits;		/* number of credits consumed by this mid */
-	__u16 credits_received;	/* number of credits from the response */
-	__u32 pid;		/* process id */
-	__u32 sequence_number;  /* for CIFS signing */
-	unsigned int sr_flags;	/* Flags passed to send_recv() */
-	unsigned long when_alloc;  /* when mid was created */
+	struct smb_message	*next;		/* Next message in compound */
+	struct cifs_credits	credits;	/* Credit requirements for this message */
+	void			*request;	/* Pointer to request message body */
+	refcount_t		ref;
+	bool			sensitive;	/* Request contains sensitive data */
+	bool			cancelled;	/* T if cancelled */
+	unsigned int		sr_flags;	/* Flags passed to send_recv() */
+
+	/* Queue state */
+	struct list_head	qhead;		/* mids waiting on reply from this server */
+	__u64			mid;		/* multiplex id */
+	__u16			credits_consumed; /* number of credits consumed by this op */
+	__u16			credits_received; /* number of credits from the response */
+	__u32			pid;		/* process id */
+	__u32			sequence_number;  /* for CIFS signing */
+	unsigned long		when_alloc;	/* when mid was created */
 #ifdef CONFIG_CIFS_STATS2
-	unsigned long when_sent; /* time when smb send finished */
-	unsigned long when_received; /* when demux complete (taken off wire) */
+	unsigned long		when_sent;	/* time when smb send finished */
+	unsigned long		when_received;	/* when demux complete (taken off wire) */
 #endif
-	mid_receive_t receive;	/* call receive callback */
-	mid_callback_t callback; /* call completion callback */
-	mid_handle_t handle;	/* call handle mid callback */
-	void *callback_data;	  /* general purpose pointer for callback */
-	struct task_struct *creator;
-	void *resp_buf;		/* pointer to received SMB header */
-	unsigned int resp_buf_size;
-	u32 response_pdu_len;
-	int mid_state;	/* wish this were enum but can not pass to wait_event */
-	int mid_rc;		/* rc for MID_RC */
-	__le16 command;		/* smb command code */
-	unsigned int optype;	/* operation type */
-	spinlock_t mid_lock;
-	bool wait_cancelled:1;  /* Cancelled while waiting for response */
-	bool deleted_from_q:1;  /* Whether Mid has been dequeued frem pending_mid_q */
-	bool large_buf:1;	/* if valid response, is pointer to large buf */
-	bool multiRsp:1;	/* multiple trans2 responses for one request  */
-	bool multiEnd:1;	/* both received */
-	bool decrypted:1;	/* decrypted entry */
+	mid_receive_t		receive;	/* call receive callback */
+	mid_callback_t		callback;	/* call completion callback */
+	mid_handle_t		handle;		/* call handle mid callback */
+	void			*callback_data;	/* general purpose pointer for callback */
+	struct task_struct	*creator;
+	void			*resp_buf;	/* pointer to received SMB header */
+	unsigned int		resp_buf_size;
+	int			mid_state;	/* wish this were enum but can not pass to wait_event */
+	int			mid_rc;		/* rc for MID_RC */
+	unsigned int		optype;		/* operation type */
+	spinlock_t		mid_lock;
+	bool			wait_cancelled:1;  /* Cancelled while waiting for response */
+	bool			deleted_from_q:1;  /* Mid has been dequeued from pending_mid_q */
+	bool			large_buf:1;	/* if valid response, is pointer to large buf */
+	bool			multiRsp:1;	/* multiple trans2 responses for one request  */
+	bool			multiEnd:1;	/* both received */
+	bool			decrypted:1;	/* decrypted entry */
+
+	/* Request details */
+	u8			command_trace;	/* enum smb_command_trace - Command trace ID */
+	__le16			command;	/* smb command code */
+	s16			pre_offset;	/* Offset of pre-headers from ->body (negative) */
+	unsigned int		total_len;	/* Total length of from hdr_offset onwards */
+	/* Response */
+	u32			response_pdu_len; /* Size of response PDU */
+	/* Compat with old code */
+	struct smb_rqst		rqst;
+	int			*resp_buf_type;
+	struct kvec		*resp_iov;
 };
 
 struct close_cancelled_open {
fs/smb/client/cifsproto.h +5 -6
index f88ae04af85d..9c60fffcf53d 100644
@@ -81,6 +81,10 @@ char *cifs_build_path_to_root(struct smb3_fs_context *ctx,
 			      struct cifs_tcon *tcon, int add_treename);
 char *cifs_build_devname(char *nodename, const char *prepath);
 void delete_mid(struct TCP_Server_Info *server, struct smb_message *smb);
+struct smb_message *smb_message_alloc(enum smb_command_trace cmd, gfp_t gfp);
+void smb_get_message(struct smb_message *smb);
+void smb_put_message(struct smb_message *smb);
+void smb_put_messages(struct smb_message *smb);
 void __release_mid(struct TCP_Server_Info *server, struct smb_message *smb);
 void cifs_wake_up_task(struct TCP_Server_Info *server,
 		       struct smb_message *smb);
@@ -470,14 +474,9 @@ static inline bool dfs_src_pathname_equal(const char *s1, const char *s2)
 	return true;
 }
 
-static inline void smb_get_mid(struct smb_message *smb)
-{
-	refcount_inc(&smb->refcount);
-}
-
 static inline void release_mid(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	if (refcount_dec_and_test(&smb->refcount))
+	if (refcount_dec_and_test(&smb->ref))
 		__release_mid(server, smb);
 }
 
fs/smb/client/connect.c +3 -3
index 97de3ae68d7a..3d7e279ba149 100644
@@ -327,7 +327,7 @@ cifs_abort_connection(struct TCP_Server_Info *server)
 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
 	spin_lock(&server->mid_queue_lock);
 	list_for_each_entry_safe(smb, nsmb, &server->pending_mid_q, qhead) {
-		smb_get_mid(smb);
+		smb_get_message(smb);
 		if (smb->mid_state == MID_REQUEST_SUBMITTED)
 			smb->mid_state = MID_RETRY_NEEDED;
 		list_move(&smb->qhead, &retry_list);
@@ -884,7 +884,7 @@ is_smb_response(struct TCP_Server_Info *server, unsigned char type)
 			 */
 			spin_lock(&server->mid_queue_lock);
 			list_for_each_entry_safe(smb, nsmb, &server->pending_mid_q, qhead) {
-				smb_get_mid(smb);
+				smb_get_message(smb);
 				list_move(&smb->qhead, &dispose_list);
 				smb->deleted_from_q = true;
 			}
@@ -1103,7 +1103,7 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
 		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
 			smb = list_entry(tmp, struct smb_message, qhead);
 			cifs_dbg(FYI, "Clearing mid %llu\n", smb->mid);
-			smb_get_mid(smb);
+			smb_get_message(smb);
 			smb->mid_state = MID_SHUTDOWN;
 			list_move(&smb->qhead, &dispose_list);
 			smb->deleted_from_q = true;
fs/smb/client/smb1ops.c +1 -1
index c1c16f6346fb..df74975374ee 100644
@@ -254,7 +254,7 @@ cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
 		if (compare_mid(smb->mid, buf) &&
 		    smb->mid_state == MID_REQUEST_SUBMITTED &&
 		    le16_to_cpu(smb->command) == buf->Command) {
-			smb_get_mid(smb);
+			smb_get_message(smb);
 			spin_unlock(&server->mid_queue_lock);
 			return smb;
 		}
fs/smb/client/smb1transport.c +1 -1
index c2d211a62577..f38fe262c7ea 100644
@@ -45,7 +45,7 @@ alloc_mid(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
 
 	smb = mempool_alloc(&smb_message_pool, GFP_NOFS);
 	memset(smb, 0, sizeof(struct smb_message));
-	refcount_set(&smb->refcount, 1);
+	refcount_set(&smb->ref, 1);
 	spin_lock_init(&smb->mid_lock);
 	smb->mid = get_mid(smb_buffer);
 	smb->pid = current->pid;
fs/smb/client/smb2ops.c +1 -1
index 236eb560bdff..f39074d0e4a0 100644
@@ -416,7 +416,7 @@ __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
 		if ((smb->mid == wire_mid) &&
 		    (smb->mid_state == MID_REQUEST_SUBMITTED) &&
 		    (smb->command == shdr->Command)) {
-			smb_get_mid(smb);
+			smb_get_message(smb);
 			if (dequeue) {
 				list_del_init(&smb->qhead);
 				smb->deleted_from_q = true;
fs/smb/client/smb2transport.c +2 -2
index a28b66ead690..f5b676802ce7 100644
@@ -619,10 +619,10 @@ smb2_mid_entry_alloc(const struct smb2_hdr *shdr,
 
 	smb = mempool_alloc(&smb_message_pool, GFP_NOFS);
 	memset(smb, 0, sizeof(*smb));
-	refcount_set(&smb->refcount, 1);
+	refcount_set(&smb->ref, 1);
 	spin_lock_init(&smb->mid_lock);
 	smb->mid = le64_to_cpu(shdr->MessageId);
-	smb->credits = credits > 0 ? credits : 1;
+	smb->credits_consumed = credits > 0 ? credits : 1;
 	smb->pid = current->pid;
 	smb->command = shdr->Command; /* Always LE */
 	smb->when_alloc = jiffies;
fs/smb/client/transport.c +41 -1

54 lines not on this page · raw message

author maintainer
#10 lore raw

RFC PATCH 10/36 cifs: Split crypt_message() into encrypt and decrypt variants

In reply to David Howells

Split crypt_message() into encrypt and decrypt variants so that the encrypt variant can be substantially changed.

---
 fs/smb/client/smb2ops.c | 103 +++++++++++++++++++++++++++++++++-------
 1 file changed, 87 insertions(+), 16 deletions(-)
fs/smb/client/smb2ops.c +87 -16
index f39074d0e4a0..10bb0fe6b77b 100644
@@ -4458,16 +4458,17 @@ smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
 
 	return -EAGAIN;
 }
+
 /*
- * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
+ * Encrypt @rqst message. @rqst[0] has the following format:
  * iov[0]   - transform header (associate data),
  * iov[1-N] - SMB2 header and pages - data to encrypt.
  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
  * untouched.
  */
 static int
-crypt_message(struct TCP_Server_Info *server, int num_rqst,
-	      struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
+encrypt_message(struct TCP_Server_Info *server, int num_rqst,
+		struct smb_rqst *rqst, struct crypto_aead *tfm)
 {
 	struct smb2_transform_hdr *tr_hdr =
 		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
@@ -4482,10 +4483,10 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
 	void *creq;
 
-	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
+	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), 1, key);
 	if (rc) {
-		cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
-			 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
+		cifs_server_dbg(FYI, "%s: Could not get encryption key. sid: 0x%llx\n",
+				__func__, le64_to_cpu(tr_hdr->SessionId));
 		return rc;
 	}
 
@@ -4510,11 +4511,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	if (IS_ERR(creq))
 		return PTR_ERR(creq);
 
-	if (!enc) {
-		memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
-		crypt_len += SMB2_SIGNATURE_SIZE;
-	}
-
 	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
 	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
 		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
@@ -4530,16 +4526,91 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
 				  crypto_req_done, &wait);
 
-	rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
-				: crypto_aead_decrypt(req), &wait);
+	rc = crypto_wait_req(crypto_aead_encrypt(req), &wait);
 
-	if (!rc && enc)
+	if (!rc)
 		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
 
 	kfree_sensitive(creq);
 	return rc;
 }
 
+/*
+ * Decrypt @rqst message. @rqst[0] has the following format:
+ * iov[0]   - transform header (associate data),
+ * iov[1-N] - SMB2 header and pages - data to decrypt.
+ * On success return encrypted data in iov[1-N] and pages, leave iov[0]
+ * untouched.
+ */
+static int
+decrypt_message(struct TCP_Server_Info *server, int num_rqst,
+		struct smb_rqst *rqst, struct crypto_aead *tfm)
+{
+	struct smb2_transform_hdr *tr_hdr =
+		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
+	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
+	int rc = 0;
+	struct scatterlist *sg;
+	u8 sign[SMB2_SIGNATURE_SIZE] = {};
+	u8 key[SMB3_ENC_DEC_KEY_SIZE];
+	struct aead_request *req;
+	u8 *iv;
+	DECLARE_CRYPTO_WAIT(wait);
+	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
+	void *creq;
+
+	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), 0, key);
+	if (rc) {
+		cifs_server_dbg(FYI, "%s: Could not get decryption key. sid: 0x%llx\n",
+				__func__, le64_to_cpu(tr_hdr->SessionId));
+		return rc;
+	}
+
+	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
+		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
+	else
+		rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
+
+	if (rc) {
+		cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
+		return rc;
+	}
+
+	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
+	if (rc) {
+		cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
+		return rc;
+	}
+
+	creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg);
+	if (IS_ERR(creq))
+		return PTR_ERR(creq);
+
+	memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
+	crypt_len += SMB2_SIGNATURE_SIZE;
+
+	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
+	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
+	else {
+		iv[0] = 3;
+		memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
+	}
+
+	aead_request_set_tfm(req, tfm);
+	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
+	aead_request_set_ad(req, assoc_data_len);
+
+	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+				  crypto_req_done, &wait);
+
+	rc = crypto_wait_req(crypto_aead_decrypt(req), &wait);
+
+	kfree_sensitive(creq);
+	return rc;
+}
+
 /*
  * Copy data from an iterator to the pages in a bvec queue buffer.
  */
@@ -4618,7 +4689,7 @@ smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
 	/* fill the 1st iov with a transform header */
 	fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
 
-	rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
+	rc = encrypt_message(server, num_rqst, new_rq, server->secmech.enc);
 	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
 	if (rc)
 		goto err_free;
@@ -4680,7 +4751,7 @@ decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
 		tfm = server->secmech.dec;
 	}
 
-	rc = crypt_message(server, 1, &rqst, 0, tfm);
+	rc = decrypt_message(server, 1, &rqst, tfm);
 	cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
 
 	if (is_offloaded)
author maintainer
#11 lore raw

RFC PATCH 11/36 cifs: Add new AEAD alloc and setup routines that draw from an iterator

In reply to David Howells

Add new AEAD alloc and setup routines that solely use an iterator to supply the buffers rather than disassembling an smb_rqst struct.

---
 fs/smb/client/smb2transport.c | 69 +++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
fs/smb/client/smb2transport.c +69 -0
index f5b676802ce7..9df3a0b530e2 100644
@@ -812,3 +812,72 @@ smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
 
 	return 0;
 }
+
+/*
+ * Allocate the context info needed for the encryption operation, along with a
+ * scatterlist to point to the buffer.
+ */
+static void *smb2_aead_req_alloc_new(struct crypto_aead *tfm, const struct iov_iter *iter,
+				 u8 **iv,
+				 struct aead_request **req, struct sg_table *sgt,
+				 unsigned int *num_sgs, size_t *sensitive_size)
+{
+	unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
+	unsigned int iv_size = crypto_aead_ivsize(tfm);
+	unsigned int len;
+	u8 *p;
+
+	*num_sgs = iov_iter_npages(iter, INT_MAX);
+
+	len = iv_size;
+	len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
+	len = ALIGN(len, crypto_tfm_ctx_alignment());
+	len += req_size;
+	len = ALIGN(len, __alignof__(struct scatterlist));
+	len += array_size(*num_sgs + 2, sizeof(struct scatterlist));
+	*sensitive_size = len;
+
+	p = kvzalloc(len, GFP_NOFS);
+	if (!p)
+		return ERR_PTR(-ENOMEM);
+
+	*iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
+	*req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
+						crypto_tfm_ctx_alignment());
+	sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
+						   __alignof__(struct scatterlist));
+	return p;
+}
+
+/*
+ * Set up for doing a crypto operation, building a scatterlist from the
+ * supplied iterator.
+ */
+static void *smb2_get_aead_req_new(struct crypto_aead *tfm, const struct iov_iter *iter,
+				   const u8 *sig, u8 **iv,
+				   struct aead_request **req, struct scatterlist **sgl,
+				   size_t *sensitive_size)
+{
+	struct sg_table sgtable = {};
+	struct iov_iter tmp = *iter;
+	unsigned int num_sgs;
+	ssize_t rc;
+	void *p;
+
+	p = smb2_aead_req_alloc_new(tfm, iter, iv, req, &sgtable,
+				    &num_sgs, sensitive_size);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	sg_init_marker(sgtable.sgl, num_sgs + 2);
+
+	rc = extract_iter_to_sg(&tmp, iov_iter_count(iter), &sgtable, num_sgs, 0);
+	sgtable.orig_nents = sgtable.nents + 1;
+	if (rc < 0)
+		return ERR_PTR(rc);
+
+	cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
+	sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
+	*sgl = sgtable.sgl;
+	return p;
+}
author maintainer
#12 lore raw

RFC PATCH 12/36 cifs: [WIP] Rewrite base Rx to put data off the socket into a bvecq

In reply to David Howells

[!] Note: This patch is a WIP and doesn't support RDMA yet

Rewrite base Rx to splice incoming data buffers directly the socket into a bvec-queue based receive queue and then use iov_iter functions to take it apart. It can also be decrypted within those buffers.

Non-blocking splice is used to get as many data segments as are available and can be spliced into the empty slots of the bvecq passed to TCP. Since the splice doesn't wait, ->sk_data_ready() is used to sense new PDUs becoming available.

For the case of RDMA, the smbdirect reassembly should just add buffers to the receive queue.

We can then offer the iterator to the individual PDU decoders and they can use copy_from_iter() to extract data.

Special handling is provided for any smb_message that wants it (e.g. unencrypted READs), such that the headers are copied into heap objects and then the rest is copied directly from the receive queue into the target buffer. This is done incrementally so that reads aren't held up whilst until all the data is received; further, spent receive buffers are discarded as soon as possible to prevent holding on to DMA buffer space.

---
 fs/smb/client/cifs_debug.c    |   38 +-
 fs/smb/client/cifs_debug.h    |    3 +-
 fs/smb/client/cifsglob.h      |  106 ++--
 fs/smb/client/cifsproto.h     |   21 +-
 fs/smb/client/cifssmb.c       |   35 +-
 fs/smb/client/connect.c       |  616 ++++++++-----------
 fs/smb/client/smb1debug.c     |   55 +-
 fs/smb/client/smb1encrypt.c   |  111 ++++
 fs/smb/client/smb1maperror.c  |    7 +-
 fs/smb/client/smb1misc.c      |   20 +-
 fs/smb/client/smb1ops.c       |   44 +-
 fs/smb/client/smb1pdu.h       |   62 +-
 fs/smb/client/smb1proto.h     |   43 +-
 fs/smb/client/smb1transport.c | 1042 ++++++++++++++++++++++++++-------
 fs/smb/client/smb2inode.c     |    2 +-
 fs/smb/client/smb2maperror.c  |    3 +-
 fs/smb/client/smb2misc.c      |  358 +++++------
 fs/smb/client/smb2ops.c       |  908 ++++------------------------
 fs/smb/client/smb2pdu.c       |   67 ++-
 fs/smb/client/smb2proto.h     |   25 +-
 fs/smb/client/smb2transport.c |  633 +++++++++++++++++++-
 fs/smb/client/trace.h         |   63 ++
 fs/smb/client/transport.c     |  221 +------
 fs/smb/common/smb2pdu.h       |   30 +
 24 files changed, 2473 insertions(+), 2040 deletions(-)
fs/smb/client/cifs_debug.c +1 -37
index 2d0d26ee57ce..47dadab67440 100644
@@ -28,49 +28,13 @@
 #include "cached_dir.h"
 
 void
-cifs_dump_mem(char *label, void *data, int length)
+cifs_dump_mem(const char *label, void *data, int length)
 {
 	pr_debug("%s: dump of %d bytes of data at 0x%p\n", label, length, data);
 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
 		       data, length, true);
 }
 
-void cifs_dump_mids(struct TCP_Server_Info *server)
-{
-#ifdef CONFIG_CIFS_DEBUG2
-	struct smb_message *smb;
-
-	if (server == NULL)
-		return;
-
-	cifs_dbg(VFS, "Dump pending requests:\n");
-	spin_lock(&server->mid_queue_lock);
-	list_for_each_entry(smb, &server->pending_mid_q, qhead) {
-		cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d Cbdata: %p Mid %llu\n",
-			 smb->mid_state,
-			 le16_to_cpu(smb->command),
-			 smb->pid,
-			 smb->callback_data,
-			 smb->mid);
-#ifdef CONFIG_CIFS_STATS2
-		cifs_dbg(VFS, "IsLarge: %d buf: %p time rcv: %ld now: %ld\n",
-			 smb->large_buf,
-			 smb->resp_buf,
-			 smb->when_received,
-			 jiffies);
-#endif /* STATS2 */
-		cifs_dbg(VFS, "IsMult: %d IsEnd: %d\n",
-			 smb->multiRsp, smb->multiEnd);
-		if (smb->resp_buf) {
-			server->ops->dump_detail(smb->resp_buf,
-						 smb->response_pdu_len, server);
-			cifs_dump_mem("existing buf: ", smb->resp_buf, 62);
-		}
-	}
-	spin_unlock(&server->mid_queue_lock);
-#endif /* CONFIG_CIFS_DEBUG2 */
-}
-
 #ifdef CONFIG_PROC_FS
 static void cifs_debug_tcon(struct seq_file *m, struct cifs_tcon *tcon)
 {
fs/smb/client/cifs_debug.h +1 -2
index 00650929a133..fbc3ab351e5c 100644
@@ -14,8 +14,7 @@
 
 #define pr_fmt(fmt) "CIFS: " fmt
 
-void cifs_dump_mem(char *label, void *data, int length);
-void cifs_dump_mids(struct TCP_Server_Info *server);
+void cifs_dump_mem(const char *label, void *data, int length);
 extern bool traceSMB;		/* flag which enables the function below */
 void dump_smb(void *buf, int smb_buf_length);
 #define CIFS_INFO	0x01
fs/smb/client/cifsglob.h +41 -65

201 lines not on this page · raw message

fs/smb/client/cifsproto.h +9 -12
index 9c60fffcf53d..8dedfd677f8d 100644
@@ -88,17 +88,14 @@ void smb_put_messages(struct smb_message *smb);
 void __release_mid(struct TCP_Server_Info *server, struct smb_message *smb);
 void cifs_wake_up_task(struct TCP_Server_Info *server,
 		       struct smb_message *smb);
-int cifs_handle_standard(struct TCP_Server_Info *server,
-			 struct smb_message *smb);
 char *smb3_fs_context_fullpath(const struct smb3_fs_context *ctx, char dirsep);
 int smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx);
 int cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs);
 bool cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs);
-int cifs_discard_remaining_data(struct TCP_Server_Info *server);
 int cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
-		    mid_receive_t receive, mid_callback_t callback,
-		    mid_handle_t handle, void *cbdata, const int flags,
-		    const struct cifs_credits *exist_credits);
+		    mid_callback_t callback, void *cbdata, const int flags,
+		    const struct cifs_credits *exist_credits,
+		    struct iov_iter *resp_buf);
 struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses);
 int cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
 		   struct TCP_Server_Info *server, struct smb_rqst *rqst,
@@ -227,12 +224,7 @@ unsigned int setup_special_user_owner_ACE(struct smb_ace *pntace);
 
 void dequeue_mid(struct TCP_Server_Info *server, struct smb_message *smb,
 		 bool malformed);
-int cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
-			  unsigned int to_read);
-ssize_t cifs_discard_from_socket(struct TCP_Server_Info *server,
-				 size_t to_read);
-int cifs_read_iter_from_socket(struct TCP_Server_Info *server,
-			       struct iov_iter *iter, unsigned int to_read);
+bool allocate_buffers(struct TCP_Server_Info *server);
 int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb);
 void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx);
 int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx);
@@ -609,4 +601,9 @@ find_readable_file(struct cifsInodeInfo *cinode, unsigned int find_flags)
 	return __find_readable_file(cinode, find_flags, 0);
 }
 
+int smb_rxqueue_refill(struct TCP_Server_Info *server, struct netfs_rxqueue *rxq,
+		       size_t min_size);
+int smb_rxqueue_consume(struct TCP_Server_Info *server, struct netfs_rxqueue *rxq,
+			size_t amount);
+
 #endif			/* _CIFSPROTO_H */
fs/smb/client/cifssmb.c +23 -12
index 25f6289ee72f..c537cc3e66c8 100644
@@ -336,6 +336,8 @@ static int validate_t2(struct smb_t2_rsp *pSMB)
 {
 	unsigned int total_size;
 
+	return 0; /* Checking now done in reception routine. */
+
 	/* check for plausible wct */
 	if (pSMB->hdr.WordCount < 10)
 		goto vt2_err;
@@ -428,7 +430,7 @@ CIFSSMBNegotiate(const unsigned int xid,
 	SMB_NEGOTIATE_RSP *pSMBr;
 	unsigned int in_len;
 	int rc = 0;
-	int bytes_returned;
+	int bytes_returned = 0;
 	int i;
 	u16 count;
 
@@ -768,8 +770,8 @@ CIFSSMBEcho(struct TCP_Server_Info *server)
 	iov[0].iov_len = in_len;
 	iov[0].iov_base = smb;
 
-	rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
-			     server, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
+	rc = cifs_call_async(server, &rqst, cifs_echo_callback, server,
+			     CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL, NULL);
 	if (rc)
 		cifs_dbg(FYI, "Echo request failed: %d\n", rc);
 
@@ -1466,8 +1468,11 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct inode *inode = &ictx->inode;
-	struct smb_rqst rqst = { .rq_iov = rdata->iov,
-				 .rq_nvec = 1};
+	struct kvec iov = {
+		.iov_base = smb->response,
+		.iov_len  = smb->resp_len,
+	};
+	struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
 	struct cifs_credits credits = {
 		.value = 1,
 		.instance = 0,
@@ -1489,12 +1494,13 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
 		/* result already set, check signature */
+		rdata->got_bytes = smb->resp_data_len;
 		if (server->sign) {
 			int rc = 0;
 
-			iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
+			iov_iter_truncate(&rqst.rq_iter, smb->resp_data_len);
 			rc = cifs_verify_signature(&rqst, server,
-						  smb->sequence_number);
+						   smb->sequence_number);
 			if (rc)
 				cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
 					 rc);
@@ -1585,6 +1591,7 @@ cifs_async_readv(struct cifs_io_subrequest *rdata)
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct smb_rqst rqst = { .rq_iov = rdata->iov,
 				 .rq_nvec = 1 };
+	struct iov_iter iter;
 	unsigned int in_len;
 
 	cifs_dbg(FYI, "%s: offset=%llu bytes=%zu\n",
@@ -1636,8 +1643,12 @@ cifs_async_readv(struct cifs_io_subrequest *rdata)
 			      tcon->tid, tcon->ses->Suid,
 			      rdata->subreq.start, rdata->subreq.len);
 
-	rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
-			     cifs_readv_callback, NULL, rdata, 0, NULL);
+	iov_iter_bvec_queue(&rqst.rq_iter, ITER_DEST,
+			    rdata->subreq.content.bvecq, rdata->subreq.content.slot,
+			    rdata->subreq.content.offset, rdata->subreq.len);
+
+	rc = cifs_call_async(tcon->ses->server, &rqst,
+			     cifs_readv_callback, rdata, 0, NULL, &iter);
 
 	if (rc == 0)
 		cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
@@ -1889,7 +1900,7 @@ cifs_writev_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
 	struct cifs_io_subrequest *wdata = smb->callback_data;
 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
-	WRITE_RSP *rsp = (WRITE_RSP *)smb->resp_buf;
+	WRITE_RSP *rsp = (WRITE_RSP *)smb->response;
 	struct cifs_credits credits = {
 		.value = 1,
 		.instance = 0,
@@ -2030,8 +2041,8 @@ cifs_async_writev(struct cifs_io_subrequest *wdata)
 		iov[0].iov_len += 4; /* pad bigger by four bytes */
 	}
 
-	rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
-			     cifs_writev_callback, NULL, wdata, 0, NULL);
+	rc = cifs_call_async(tcon->ses->server, &rqst,
+			     cifs_writev_callback, wdata, 0, NULL, NULL);
 	/* Can't touch wdata if rc == 0 */
 	if (rc == 0)
 		cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
fs/smb/client/connect.c +246 -370

764 lines not on this page · raw message

fs/smb/client/smb1debug.c +48 -7

65 lines not on this page · raw message

fs/smb/client/smb1encrypt.c +111 -0

126 lines not on this page · raw message

fs/smb/client/smb1maperror.c +3 -4
index c84f6256e146..e44a51039776 100644
@@ -97,9 +97,8 @@ search_mapping_table_ERRSRV(__u16 smb_err)
 }
 
 int
-map_smb_to_linux_error(char *buf, bool logErr)
+map_smb_to_linux_error(const struct smb_hdr *smb, bool logErr)
 {
-	struct smb_hdr *smb = (struct smb_hdr *)buf;
 	int rc = -EIO;	/* if transport error smb error may not be set */
 	__u8 smberrclass;
 	__u16 smberrcode;
@@ -175,9 +174,9 @@ map_and_check_smb_error(struct TCP_Server_Info *server,
 			struct smb_message *smb, bool logErr)
 {
 	int rc;
-	struct smb_hdr *rhdr = (struct smb_hdr *)smb->resp_buf;
+	struct smb_hdr *rhdr = (struct smb_hdr *)smb->response;
 
-	rc = map_smb_to_linux_error((char *)rhdr, logErr);
+	rc = map_smb_to_linux_error(rhdr, logErr);
 	if (rc == -EACCES && !(rhdr->Flags2 & SMBFLG2_ERR_STATUS)) {
 		/* possible ERRBaduid */
 		__u8 class = rhdr->Status.DosError.ErrorClass;
fs/smb/client/smb1misc.c +10 -10
index ba56023010d8..357850e9691f 100644
@@ -63,10 +63,11 @@ header_assemble(struct smb_hdr *buffer, char smb_command,
 }
 
 bool
-is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
+smb1_is_valid_oplock_break(union smb1_response_hdr *buf, unsigned int pdu_len,
+			   struct TCP_Server_Info *srv)
 {
-	struct smb_hdr *buf = (struct smb_hdr *)buffer;
-	struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
+	struct smb_hdr *hdr = &buf->hdr;
+	struct smb_com_lock_req *pSMB = &buf->oplock_break;
 	struct TCP_Server_Info *pserver;
 	struct cifs_ses *ses;
 	struct cifs_tcon *tcon;
@@ -76,13 +77,12 @@ is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
 	cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
 	if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
 	   (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
-		struct smb_com_transaction_change_notify_rsp *pSMBr =
-			(struct smb_com_transaction_change_notify_rsp *)buf;
+		struct smb_com_transaction_change_notify_rsp *pSMBr = &buf->change;
 		struct file_notify_information *pnotify;
 		__u32 data_offset = 0;
-		size_t len = srv->total_read - srv->pdu_size;
+		size_t len = pdu_len;
 
-		if (get_bcc(buf) > sizeof(struct file_notify_information)) {
+		if (get_bcc(hdr) > sizeof(struct file_notify_information)) {
 			data_offset = le32_to_cpu(pSMBr->DataOffset);
 
 			if (data_offset >
@@ -141,7 +141,7 @@ is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
 		if (cifs_ses_exiting(ses))
 			continue;
 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
-			if (tcon->tid != buf->Tid)
+			if (tcon->tid != hdr->Tid)
 				continue;
 
 			cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
@@ -183,7 +183,7 @@ is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
 unsigned int
 smbCalcSize(void *buf)
 {
-	struct smb_hdr *ptr = buf;
-	return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
+	const struct smb_hdr *ptr = buf;
+	return (sizeof(*ptr) + (2 * ptr->WordCount) +
 		2 /* size of the bcc field */ + get_bcc(ptr));
 }
fs/smb/client/smb1ops.c +9 -35

90 lines not on this page · raw message

fs/smb/client/smb1pdu.h +56 -6

102 lines not on this page · raw message

fs/smb/client/smb1proto.h +19 -24

96 lines not on this page · raw message

fs/smb/client/smb1transport.c +830 -212

1,162 lines not on this page · raw message

fs/smb/client/smb2inode.c +1 -1
index 6c9c229b91f6..6789049db053 100644
@@ -590,7 +590,7 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
 		char *buf = rsp_iov[i + 1].iov_base;
 
 		if (buf && resp_buftype[i + 1] != CIFS_NO_BUFFER)
-			rc = server->ops->map_error(buf, false);
+			rc = map_smb2_to_linux_error((struct smb2_hdr *)buf, false);
 		else
 			rc = tmp_rc;
 		switch (cmds[i]) {
fs/smb/client/smb2maperror.c +1 -2
index 9ed21f7b618c..e4ec8c777d78 100644
@@ -47,9 +47,8 @@ static const struct status_to_posix_error *smb2_get_err_map(__u32 smb2_status)
 }
 
 int
-map_smb2_to_linux_error(char *buf, bool log_err)
+map_smb2_to_linux_error(const struct smb2_hdr *shdr, bool log_err)
 {
-	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
 	int rc = -EIO;
 	__le32 smb2err = shdr->Status;
 	const struct status_to_posix_error *err_map;
fs/smb/client/smb2misc.c +162 -196

114 lines not on this page · raw message

fs/smb/client/smb2ops.c +106 -802

908 lines not on this page · raw message

fs/smb/client/smb2pdu.c +41 -26

67 lines not on this page · raw message

fs/smb/client/smb2proto.h +19 -6
index 5727e47733c8..0b3df644222b 100644

Lines of this file are not shown. View the raw message.

fs/smb/client/smb2transport.c +627 -6

633 lines not on this page · raw message

fs/smb/client/trace.h +63 -0

63 lines not on this page · raw message

fs/smb/client/transport.c +16 -205

221 lines not on this page · raw message

fs/smb/common/smb2pdu.h +30 -0
index dc5ec2643669..3d69daf8d7ef 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

author maintainer
#13 lore raw

RFC PATCH 13/36 cifs: Remove validate_t2()

In reply to David Howells

validate_t2() is no longer necessary as the checking is now done up front.

---
 fs/smb/client/cifssmb.c | 132 +++++++---------------------------------
 1 file changed, 22 insertions(+), 110 deletions(-)
fs/smb/client/cifssmb.c +22 -110
index c537cc3e66c8..901f8aff6134 100644
@@ -332,40 +332,6 @@ smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
 	return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
 }
 
-static int validate_t2(struct smb_t2_rsp *pSMB)
-{
-	unsigned int total_size;
-
-	return 0; /* Checking now done in reception routine. */
-
-	/* check for plausible wct */
-	if (pSMB->hdr.WordCount < 10)
-		goto vt2_err;
-
-	/* check for parm and data offset going beyond end of smb */
-	if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
-	    get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
-		goto vt2_err;
-
-	total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
-	if (total_size >= 512)
-		goto vt2_err;
-
-	/* check that bcc is at least as big as parms + data, and that it is
-	 * less than negotiated smb buffer
-	 */
-	total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
-	if (total_size > get_bcc(&pSMB->hdr) ||
-	    total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
-		goto vt2_err;
-
-	return 0;
-vt2_err:
-	cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
-		sizeof(struct smb_t2_rsp) + 16);
-	return -EINVAL;
-}
-
 static int
 decode_ext_sec_blob(struct cifs_ses *ses, SMB_NEGOTIATE_RSP *pSMBr)
 {
@@ -1122,9 +1088,7 @@ CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
 	}
 
 	cifs_dbg(FYI, "copying inode info\n");
-	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-	if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
+	if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
 		rc = smb_EIO2(smb_eio_trace_create_rsp_too_small,
 			      get_bcc(&pSMBr->hdr), sizeof(OPEN_PSX_RSP));
 		goto psx_create_err;
@@ -2372,9 +2336,7 @@ CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
 		/* lock structure can be returned on get */
 		__u16 data_offset;
 		__u16 data_count;
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
+		if (get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
 			rc = smb_EIO2(smb_eio_trace_lock_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), sizeof(*parm_data));
 			goto plk_err_exit;
@@ -2939,9 +2901,8 @@ CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
 	} else {
 		/* decode response */
 
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 		/* BB also check enough total bytes returned */
-		if (rc || get_bcc(&pSMBr->hdr) < 2)
+		if (get_bcc(&pSMBr->hdr) < 2)
 			rc = smb_EIO2(smb_eio_trace_qsym_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 2);
 		else {
@@ -3524,9 +3485,8 @@ int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
 	} else {
 		/* decode response */
 
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 		/* BB also check enough total bytes returned */
-		if (rc || get_bcc(&pSMBr->hdr) < 2)
+		if (get_bcc(&pSMBr->hdr) < 2)
 			rc = smb_EIO2(smb_eio_trace_getacl_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 2);
 		else {
@@ -3696,9 +3656,8 @@ CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
 		cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
 	} else {
 		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 		/* BB also check enough total bytes returned */
-		if (rc || get_bcc(&pSMBr->hdr) < 2)
+		if (get_bcc(&pSMBr->hdr) < 2)
 			/* If rc should we check for EOPNOSUPP and
 			   disable the srvino flag? or in caller? */
 			rc = smb_EIO2(smb_eio_trace_getextattr_bcc_too_small,
@@ -4099,12 +4058,7 @@ CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in QFileInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc) /* BB add auto retry on EOPNOTSUPP? */
-			rc = smb_EIO2(smb_eio_trace_qfileinfo_invalid,
-				      get_bcc(&pSMBr->hdr), 40);
-		else if (get_bcc(&pSMBr->hdr) < 40)
+		if (get_bcc(&pSMBr->hdr) < 40)
 			rc = smb_EIO2(smb_eio_trace_qfileinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 40);
 		else if (pFindData) {
@@ -4188,12 +4142,7 @@ CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc) /* BB add auto retry on EOPNOTSUPP? */
-			rc = smb_EIO2(smb_eio_trace_qpathinfo_invalid,
-				      get_bcc(&pSMBr->hdr), 40);
-		else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
+		if (!legacy && get_bcc(&pSMBr->hdr) < 40)
 			rc = smb_EIO2(smb_eio_trace_qpathinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 40);
 		else if (legacy && get_bcc(&pSMBr->hdr) < 24)
@@ -4275,9 +4224,7 @@ CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in UnixQFileInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
+		if (get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
 			cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
 			rc = smb_EIO2(smb_eio_trace_unixqfileinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), sizeof(FILE_UNIX_BASIC_INFO));
@@ -4360,9 +4307,7 @@ CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in UnixQPathInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
+		if (get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
 			cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
 			rc = smb_EIO2(smb_eio_trace_unixqpathinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), sizeof(FILE_UNIX_BASIC_INFO));
@@ -4504,13 +4449,8 @@ CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
 			goto findFirstRetry;
 		return rc;
 	}
-	/* decode response */
-	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-	if (rc) {
-		cifs_buf_release(pSMB);
-		return rc;
-	}
 
+	/* decode response */
 	psrch_inf->unicode = !!(pSMBr->hdr.Flags2 & SMBFLG2_UNICODE);
 	psrch_inf->ntwrk_buf_start = (char *)pSMBr;
 	psrch_inf->smallBuf = false;
@@ -4619,11 +4559,6 @@ int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
 	}
 
 	/* decode response */
-	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-	if (rc) {
-		cifs_buf_release(pSMB);
-		return rc;
-	}
 	/* BB fixme add lock for file (srch_info) struct here */
 	psrch_inf->unicode = !!(pSMBr->hdr.Flags2 & SMBFLG2_UNICODE);
 	response_data = (char *)&pSMBr->hdr.Protocol +
@@ -4763,9 +4698,8 @@ CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
 		cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
 	} else {
 		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 		/* BB also check enough total bytes returned */
-		if (rc || get_bcc(&pSMBr->hdr) < 2)
+		if (get_bcc(&pSMBr->hdr) < 2)
 			/* If rc should we check for EOPNOSUPP and
 			disable the srvino flag? or in caller? */
 			rc = smb_EIO2(smb_eio_trace_getsrvinonum_bcc_too_small,
@@ -4883,10 +4817,9 @@ CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
 		cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
 		goto GetDFSRefExit;
 	}
-	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
 	/* BB Also check if enough total bytes returned? */
-	if (rc || get_bcc(&pSMBr->hdr) < 17) {
+	if (get_bcc(&pSMBr->hdr) < 17) {
 		rc = smb_EIO2(smb_eio_trace_getdfsrefer_bcc_too_small,
 			      get_bcc(&pSMBr->hdr), 17);
 		goto GetDFSRefExit;
@@ -4961,12 +4894,10 @@ SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
 	} else {                /* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < 18)
+		if (get_bcc(&pSMBr->hdr) < 18) {
 			rc = smb_EIO2(smb_eio_trace_oldqfsinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 18);
-		else {
+		} else {
 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
 			cifs_dbg(FYI, "qfsinf resp BCC: %d  Offset %d\n",
 				 get_bcc(&pSMBr->hdr), data_offset);
@@ -5051,12 +4982,10 @@ CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < 24)
+		if (get_bcc(&pSMBr->hdr) < 24) {
 			rc = smb_EIO2(smb_eio_trace_qfsinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 24);
-		else {
+		} else {
 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
 
 			response_data =
@@ -5141,9 +5070,7 @@ CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
 	if (rc) {
 		cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < 13) {
+		if (get_bcc(&pSMBr->hdr) < 13) {
 			/* BB also check if enough bytes returned */
 			rc = smb_EIO2(smb_eio_trace_qfsattrinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 13);
@@ -5215,9 +5142,7 @@ CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
 	if (rc) {
 		cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) <
+		if (get_bcc(&pSMBr->hdr) <
 			  sizeof(FILE_SYSTEM_DEVICE_INFO))
 			rc = smb_EIO2(smb_eio_trace_qfsdevinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr),
@@ -5289,9 +5214,7 @@ CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
 	if (rc) {
 		cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < 13) {
+		if (get_bcc(&pSMBr->hdr) < 13) {
 			rc = smb_EIO2(smb_eio_trace_qfsunixinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 13);
 		} else {
@@ -5371,13 +5294,8 @@ CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
 
 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
-	if (rc) {
+	if (rc)
 		cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
-	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-		if (rc)
-			rc = -EIO;	/* bad smb */
-	}
 	cifs_buf_release(pSMB);
 
 	if (rc == -EAGAIN)
@@ -5438,9 +5356,7 @@ CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
 	if (rc) {
 		cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
 	} else {		/* decode response */
-		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-
-		if (rc || get_bcc(&pSMBr->hdr) < 13) {
+		if (get_bcc(&pSMBr->hdr) < 13) {
 			rc = smb_EIO2(smb_eio_trace_qfsposixinfo_bcc_too_small,
 				      get_bcc(&pSMBr->hdr), 13);
 		} else {
@@ -6237,11 +6153,7 @@ CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
 
 
 	/* BB also check enough total bytes returned */
-	/* BB we need to improve the validity checking
-	of these trans2 responses */
-
-	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-	if (rc || get_bcc(&pSMBr->hdr) < 4) {
+	if (get_bcc(&pSMBr->hdr) < 4) {
 		rc = smb_EIO2(smb_eio_trace_qalleas_bcc_too_small,
 			      get_bcc(&pSMBr->hdr), 4);
 		goto QAllEAsOut;
author maintainer
#14 lore raw

RFC PATCH 14/36 cifs: Remove cifs_io_subrequest::got_bytes

In reply to David Howells

Remove cifs_io_subrequest::got_bytes as smb->resp_data_len can be used instead.

---
 fs/smb/client/cifsglob.h |  1 -
 fs/smb/client/cifssmb.c  | 27 +++++++++++++--------------
 fs/smb/client/smb2pdu.c  | 22 ++++++++++------------
 3 files changed, 23 insertions(+), 27 deletions(-)
fs/smb/client/cifsglob.h +0 -1
index 7187e304c42e..2cb858495bc1 100644
@@ -1467,7 +1467,6 @@ struct cifs_io_subrequest {
 		struct netfs_io_request *rreq;
 		struct cifs_io_request *req;
 	};
-	ssize_t				got_bytes;
 	unsigned int			xid;
 	int				result;
 	bool				have_xid;
fs/smb/client/cifssmb.c +13 -14
index 901f8aff6134..62f554f97a5c 100644
@@ -1450,7 +1450,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 		 __func__, smb->mid, smb->mid_state, rdata->result,
 		 rdata->subreq.len);
 
-	if (rdata->got_bytes)
+	if (smb->resp_data_len)
 		iov_iter_bvec_queue(&rqst.rq_iter, ITER_DEST,
 				    rdata->subreq.content.bvecq, rdata->subreq.content.slot,
 				    rdata->subreq.content.offset, rdata->subreq.len);
@@ -1458,9 +1458,8 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
 		/* result already set, check signature */
-		rdata->got_bytes = smb->resp_data_len;
 		if (server->sign) {
-			int rc = 0;
+			int rc;
 
 			iov_iter_truncate(&rqst.rq_iter, smb->resp_data_len);
 			rc = cifs_verify_signature(&rqst, server,
@@ -1470,8 +1469,8 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 					 rc);
 		}
 		/* FIXME: should this be counted toward the initiating task? */
-		task_io_account_read(rdata->got_bytes);
-		cifs_stats_bytes_read(tcon, rdata->got_bytes);
+		task_io_account_read(smb->resp_data_len);
+		cifs_stats_bytes_read(tcon, smb->resp_data_len);
 		break;
 	case MID_REQUEST_SUBMITTED:
 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_req_submitted);
@@ -1481,12 +1480,12 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 do_retry:
 		__set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags);
 		rdata->result = -EAGAIN;
-		if (server->sign && rdata->got_bytes)
+		if (server->sign && smb->resp_data_len)
 			/* reset bytes number since we can not check a sign */
-			rdata->got_bytes = 0;
+			smb->resp_data_len = 0;
 		/* FIXME: should this be counted toward the initiating task? */
-		task_io_account_read(rdata->got_bytes);
-		cifs_stats_bytes_read(tcon, rdata->got_bytes);
+		task_io_account_read(smb->resp_data_len);
+		cifs_stats_bytes_read(tcon, smb->resp_data_len);
 		break;
 	case MID_RESPONSE_MALFORMED:
 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_malformed);
@@ -1511,15 +1510,15 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 				    rdata->subreq.len   - rdata->subreq.transferred,
 				    rdata->result);
 	} else {
-		size_t trans = rdata->subreq.transferred + rdata->got_bytes;
+		size_t trans = rdata->subreq.transferred + smb->resp_data_len;
 		if (trans < rdata->subreq.len &&
 		    rdata->subreq.start + trans >= netfs_read_remote_i_size(inode)) {
 			rdata->result = 0;
 			__set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
-		} else if (rdata->got_bytes > 0) {
+		} else if (smb->resp_data_len > 0) {
 			__set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags);
 		}
-		if (rdata->got_bytes)
+		if (smb->resp_data_len)
 			__set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags);
 		trace_smb3_read_done(rdata->rreq->debug_id,
 				     rdata->subreq.debug_index,
@@ -1527,7 +1526,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 				     rdata->req->cfile->fid.persistent_fid,
 				     tcon->tid, tcon->ses->Suid,
 				     rdata->subreq.start + rdata->subreq.transferred,
-				     rdata->got_bytes);
+				     smb->resp_data_len);
 	}
 
 	trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value,
@@ -1535,7 +1534,7 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 			      0, cifs_trace_rw_credits_read_response_clear);
 	rdata->credits.value = 0;
 	rdata->subreq.error = rdata->result;
-	rdata->subreq.transferred += rdata->got_bytes;
+	rdata->subreq.transferred += smb->resp_data_len;
 	trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
 	netfs_read_subreq_terminated(&rdata->subreq);
 	release_mid(server, smb);
fs/smb/client/smb2pdu.c +10 -12
index ab193f84c1ab..ddf127dc27bb 100644
@@ -4671,10 +4671,9 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 				trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
 		}
 
-		rdata->got_bytes = smb->resp_data_len;
 		/* FIXME: should this be counted toward the initiating task? */
-		task_io_account_read(rdata->got_bytes);
-		cifs_stats_bytes_read(tcon, rdata->got_bytes);
+		task_io_account_read(smb->resp_data_len);
+		cifs_stats_bytes_read(tcon, smb->resp_data_len);
 		break;
 	case MID_REQUEST_SUBMITTED:
 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_req_submitted);
@@ -4684,12 +4683,12 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 do_retry:
 		__set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags);
 		rdata->result = -EAGAIN;
-		if (server->sign && rdata->got_bytes)
+		if (server->sign && smb->resp_data_len)
 			/* reset bytes number since we can not check a sign */
-			rdata->got_bytes = 0;
+			smb->resp_data_len = 0;
 		/* FIXME: should this be counted toward the initiating task? */
-		task_io_account_read(rdata->got_bytes);
-		cifs_stats_bytes_read(tcon, rdata->got_bytes);
+		task_io_account_read(smb->resp_data_len);
+		cifs_stats_bytes_read(tcon, smb->resp_data_len);
 		break;
 	case MID_RESPONSE_MALFORMED:
 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_malformed);
@@ -4731,19 +4730,19 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 				     rdata->req->cfile->fid.persistent_fid,
 				     tcon->tid, tcon->ses->Suid,
 				     rdata->subreq.start + rdata->subreq.transferred,
-				     rdata->got_bytes);
+				     smb->resp_data_len);
 
 	if (rdata->result == -ENODATA) {
 		__set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
 		rdata->result = 0;
 	} else {
-		size_t trans = rdata->subreq.transferred + rdata->got_bytes;
+		size_t trans = rdata->subreq.transferred + smb->resp_data_len;
 		if (trans < rdata->subreq.len &&
 		    rdata->subreq.start + trans >= netfs_read_remote_i_size(inode)) {
 			__set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
 			rdata->result = 0;
 		}
-		if (rdata->got_bytes)
+		if (smb->resp_data_len)
 			__set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags);
 	}
 
@@ -4759,7 +4758,7 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 			      0, cifs_trace_rw_credits_read_response_clear);
 	rdata->credits.value = 0;
 	rdata->subreq.error = rdata->result;
-	rdata->subreq.transferred += rdata->got_bytes;
+	rdata->subreq.transferred += smb->resp_data_len;
 	trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
 	netfs_read_subreq_terminated(&rdata->subreq);
 	release_mid(server, smb);
@@ -4814,7 +4813,6 @@ smb2_async_readv(struct cifs_io_subrequest *rdata)
 
 	rdata->iov[0].iov_base = buf;
 	rdata->iov[0].iov_len = total_len;
-	rdata->got_bytes = 0;
 	rdata->result = 0;
 
 	shdr = (struct smb2_hdr *)buf;
author maintainer
#15 lore raw

RFC PATCH 15/36 cifs: Pass smb_message to cifs_verify_signature()

In reply to David Howells

Pass smb_message to cifs_verify_signature() rather than an smb_rqst.

---
 fs/smb/client/cifssmb.c       | 13 +------------
 fs/smb/client/smb1encrypt.c   | 19 ++++++++++++++-----
 fs/smb/client/smb1proto.h     |  2 +-
 fs/smb/client/smb1transport.c | 16 ++++------------
 4 files changed, 20 insertions(+), 30 deletions(-)
fs/smb/client/cifssmb.c +1 -12
index 62f554f97a5c..c5b18e1c44ab 100644
@@ -1432,11 +1432,6 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct inode *inode = &ictx->inode;
-	struct kvec iov = {
-		.iov_base = smb->response,
-		.iov_len  = smb->resp_len,
-	};
-	struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
 	struct cifs_credits credits = {
 		.value = 1,
 		.instance = 0,
@@ -1450,19 +1445,13 @@ cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 		 __func__, smb->mid, smb->mid_state, rdata->result,
 		 rdata->subreq.len);
 
-	if (smb->resp_data_len)
-		iov_iter_bvec_queue(&rqst.rq_iter, ITER_DEST,
-				    rdata->subreq.content.bvecq, rdata->subreq.content.slot,
-				    rdata->subreq.content.offset, rdata->subreq.len);
-
 	switch (smb->mid_state) {
 	case MID_RESPONSE_RECEIVED:
 		/* result already set, check signature */
 		if (server->sign) {
 			int rc;
 
-			iov_iter_truncate(&rqst.rq_iter, smb->resp_data_len);
-			rc = cifs_verify_signature(&rqst, server,
+			rc = cifs_verify_signature(smb, server,
 						   smb->sequence_number);
 			if (rc)
 				cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
fs/smb/client/smb1encrypt.c +14 -5
index 819c736c26e9..c61202ae54c6 100644
@@ -106,14 +106,23 @@ int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
 	return rc;
 }
 
-int cifs_verify_signature(struct smb_rqst *rqst,
+int cifs_verify_signature(struct smb_message *smb,
 			  struct TCP_Server_Info *server,
 			  __u32 expected_sequence_number)
 {
-	unsigned int rc;
-	char server_response_sig[8];
+	struct smb_hdr *cifs_pdu = smb->response;
+	struct kvec iov = {
+		.iov_base = smb->response,
+		.iov_len  = smb->resp_len,
+	};
+	struct smb_rqst rqst = {
+		.rq_iov = &iov,
+		.rq_nvec = 1,
+		.rq_iter = smb->response_iter
+	};
 	char what_we_think_sig_should_be[20];
-	struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
+	char server_response_sig[8];
+	int rc;
 
 	if (cifs_pdu == NULL || server == NULL)
 		return -EINVAL;
@@ -145,7 +154,7 @@ int cifs_verify_signature(struct smb_rqst *rqst,
 	cifs_pdu->Signature.Sequence.Reserved = 0;
 
 	cifs_server_lock(server);
-	rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
+	rc = cifs_calc_signature(&rqst, server, what_we_think_sig_should_be);
 	cifs_server_unlock(server);
 
 	if (rc)
fs/smb/client/smb1proto.h +1 -1
index b2ad0ef64051..54a5261fabce 100644
@@ -227,7 +227,7 @@ void cifs_dump_mids(struct TCP_Server_Info *server);
  */
 int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
 		   __u32 *pexpected_response_sequence_number);
-int cifs_verify_signature(struct smb_rqst *rqst,
+int cifs_verify_signature(struct smb_message *smb,
 			  struct TCP_Server_Info *server,
 			  __u32 expected_sequence_number);
 int cifs_verify_trans_signature(struct TCP_Server_Info *server,
fs/smb/client/smb1transport.c +4 -12
index 776d615f69d3..a6af5bd20847 100644
@@ -156,22 +156,14 @@ int
 cifs_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
 		   bool log_error)
 {
-	unsigned int len = smb->resp_len;
-
-	dump_smb(smb->response, min_t(u32, 92, len));
+	dump_smb(smb->response, min_t(u32, 92, smb->resp_len));
 
 	/* convert the length into a more usable form */
 	if (server->sign && !smb->sig_checked) {
-		struct kvec iov[1];
-		int rc = 0;
-		struct smb_rqst rqst = { .rq_iov = iov,
-					 .rq_nvec = ARRAY_SIZE(iov) };
-
-		iov[0].iov_base = smb->response;
-		iov[0].iov_len = len;
+		int rc;
 
-		rc = cifs_verify_signature(&rqst, server,
-					   smb->sequence_number);
+		/* FIXME: add code to kill session */
+		rc = cifs_verify_signature(smb, server, smb->sequence_number);
 		if (rc) {
 			cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
 				 rc);
author maintainer
#16 lore raw

RFC PATCH 16/36 cifs: Rewrite base TCP transmission

In reply to David Howells

Rewrite the base TCP transmission in cifs to copy all the content in a (compound) message into a buffer of pages in a bvecq chain. In future, the pages in this bvecq chain will be allocated from the netmem allocator so that the advance DMA/IOMMU handling will be done.

This list of pages can then be attached to an ITER_BVEC_QUEUE-type iov_iter and passed in a single call to sendmsg() with MSG_SPLICE_PAGES, thereby avoiding the need to copy the data in the TCP stack.

The encryption code can also be simplified as it only needs to encrypt the data stored in those pages, with the bonus that the content in the pages is correctly aligned for the encryption in place such that it does not need to copy the data whilst encrypting it.

It could be arranged for the data to be held in contiguous pages if possible and if necessary such that crypto can be offloaded to devices that only permit a single contiguous buffer.

Note that with this patch, "mount -o compress,seal" will now compress writes and then encrypt them.

---
 fs/smb/client/cifsglob.h      |   6 +-
 fs/smb/client/cifsproto.h     |   3 +-
 fs/smb/client/compress.c      | 146 ++++++++-----
 fs/smb/client/compress.h      |  10 +-
 fs/smb/client/smb1ops.c       |   5 +-
 fs/smb/client/smb2ops.c       | 273 +-----------------------
 fs/smb/client/smb2proto.h     |   4 +
 fs/smb/client/smb2transport.c | 113 ++++++++++
 fs/smb/client/smbdirect.c     | 105 ++-------
 fs/smb/client/smbdirect.h     |   5 +-
 fs/smb/client/transport.c     | 387 +++++++++++++++++++++++-----------
 11 files changed, 517 insertions(+), 540 deletions(-)
fs/smb/client/cifsglob.h +4 -2
index 2cb858495bc1..f7c12d24e2ea 100644
@@ -571,8 +571,10 @@ struct smb_version_operations {
 	long (*fallocate)(struct file *, struct cifs_tcon *, int, loff_t,
 			  loff_t);
 	/* init transform (compress/encrypt) request */
-	int (*init_transform_rq)(struct TCP_Server_Info *, int num_rqst,
-				 struct smb_rqst *, struct smb_rqst *);
+	int (*init_transform_rq)(struct TCP_Server_Info *server,
+				 int num_rqst, const struct smb_rqst *rqst,
+				 struct smb2_transform_hdr *tr_hdr,
+				 struct iov_iter *iter);
 	enum securityEnum (*select_sectype)(struct TCP_Server_Info *,
 			    enum securityEnum);
 	int (*next_header)(struct TCP_Server_Info *server, char *buf,
fs/smb/client/cifsproto.h +1 -2
index 8dedfd677f8d..1c9ac67b7294 100644
@@ -106,8 +106,7 @@ int compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
 		       int *resp_buf_type, struct kvec *resp_iov);
 int cifs_sync_mid_result(struct smb_message *mid,
 			 struct TCP_Server_Info *server);
-int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
-		    struct smb_rqst *rqst);
+int __smb_send_rqst(struct TCP_Server_Info *server, struct iov_iter *iter);
 int wait_for_free_request(struct TCP_Server_Info *server, const int flags,
 			  unsigned int *instance);
 int cifs_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
fs/smb/client/compress.c +98 -48

185 lines not on this page · raw message

fs/smb/client/compress.h +7 -3
index 2679baca129b..48812b0e7476 100644
@@ -30,9 +30,10 @@
 typedef int (*compress_send_fn)(struct TCP_Server_Info *, int, struct smb_rqst *);
 
 
-int smb_compress(struct TCP_Server_Info *server, struct smb_rqst *rq,
-		 compress_send_fn send_fn);
+int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
+		 struct bvecq **bq);
 bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq);
+bool is_compressible(const struct iov_iter *data);
 
 /*
  * smb_compress_alg_valid() - Validate a compression algorithm.
@@ -58,11 +59,14 @@ static __always_inline int smb_compress_alg_valid(__le16 alg, bool valid_none)
 	return false;
 }
 #else /* !CONFIG_CIFS_COMPRESSION */
-static inline int smb_compress(void *unused1, void *unused2, void *unused3)
+static inline
+int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
+		 struct bvecq **bq)
 {
 	return -EOPNOTSUPP;
 }
 
+static inline bool is_compressible(const struct iov_iter *data) { return false; }
 static inline bool should_compress(void *unused1, void *unused2)
 {
 	return false;
fs/smb/client/smb1ops.c +4 -1
index 042d44788321..8477440bbcc2 100644
@@ -142,6 +142,7 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
 	       struct smb_rqst *rqst, struct smb_message *smb,
 	       unsigned int xid)
 {
+	struct iov_iter iter;
 	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
 	struct kvec iov[1];
 	struct smb_rqst crqst = { .rq_iov = iov, .rq_nvec = 1 };
@@ -162,13 +163,15 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
 		return rc;
 	}
 
+	iov_iter_kvec(&iter, ITER_SOURCE, iov, 1, iov[0].iov_len);
+
 	/*
 	 * The response to this call was already factored into the sequence
 	 * number when the call went out, so we must adjust it back downward
 	 * after signing here.
 	 */
 	--server->sequence_number;
-	rc = __smb_send_rqst(server, 1, &crqst);
+	rc = __smb_send_rqst(server, &iter);
 	if (rc < 0)
 		server->sequence_number--;
 
fs/smb/client/smb2ops.c +5 -268

297 lines not on this page · raw message

fs/smb/client/smb2proto.h +4 -0
index 0b3df644222b..3b4bc946d1bb 100644
@@ -115,6 +115,10 @@ int smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
 int smb2_push_mandatory_locks(struct cifsFileInfo *cfile);
 void smb2_reconnect_server(struct work_struct *work);
 int smb3_crypto_aead_allocate(struct TCP_Server_Info *server);
+int smb3_init_transform_rq(struct TCP_Server_Info *server,
+			   int num_rqst, const struct smb_rqst *rqst,
+			   struct smb2_transform_hdr *tr_hdr,
+			   struct iov_iter *iter);
 unsigned long smb_rqst_len(struct TCP_Server_Info *server,
 			   struct smb_rqst *rqst);
 void smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst);
fs/smb/client/smb2transport.c +113 -0

119 lines not on this page · raw message

fs/smb/client/smbdirect.c +18 -87

139 lines not on this page · raw message

fs/smb/client/smbdirect.h +2 -3
index be205ec02077..a5c17e796ff9 100644
@@ -40,8 +40,7 @@ void smbd_destroy(struct TCP_Server_Info *server);
 
 /* Interface for carrying upper layer I/O through send/recv */
 int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
-int smbd_send(struct TCP_Server_Info *server,
-	int num_rqst, struct smb_rqst *rqst);
+int smbd_send(struct TCP_Server_Info *server, struct iov_iter *iter);
 
 /* Interfaces to register and deregister MR for RDMA read/write */
 struct smbdirect_mr_io *smbd_register_mr(
@@ -61,7 +60,7 @@ static inline void *smbd_get_connection(
 static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
 static inline void smbd_destroy(struct TCP_Server_Info *server) {}
 static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
-static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }
+static inline int smbd_send(struct TCP_Server_Info *server, struct iov_iter *iter) {return -1; }
 #endif
 
 #endif
fs/smb/client/transport.c +261 -126

452 lines not on this page · raw message

#17 lore raw

Re: [RFC PATCH 16/36] cifs: Rewrite base TCP transmission

Am 19.05.26 um 12:21 schrieb David Howells:

Quoted 8 lines

Rewrite the base TCP transmission in cifs to copy all the content in a (compound) message into a buffer of pages in a bvecq chain. In future, the pages in this bvecq chain will be allocated from the netmem allocator so that the advance DMA/IOMMU handling will be done.

This list of pages can then be attached to an ITER_BVEC_QUEUE-type iov_iter and passed in a single call to sendmsg() with MSG_SPLICE_PAGES, thereby avoiding the need to copy the data in the TCP stack.

If smbd_send() would just call smbdirect_connection_send_iter() the iov_iter_advance(iter, 4); is no longer needed.

It also allows me to make these static:
smbdirect_connection_send_batch_flush
smbdirect_init_send_batch_storage
smbdirect_connection_send_single_iter
smbdirect_connection_send_wait_zero_pending

And it will help with the transition to use sendmsg() with MSG_SPLICE_PAGES for smbdirect too.

Below are some inline comments basically saying the same.

Thanks!
metze

Quoted 806 lines

The encryption code can also be simplified as it only needs to encrypt the data stored in those pages, with the bonus that the content in the pages is correctly aligned for the encryption in place such that it does not need to copy the data whilst encrypting it.

It could be arranged for the data to be held in contiguous pages if possible and if necessary such that crypto can be offloaded to devices that only permit a single contiguous buffer.

Note that with this patch, "mount -o compress,seal" will now compress writes and then encrypt them.

---
  fs/smb/client/cifsglob.h      |   6 +-
  fs/smb/client/cifsproto.h     |   3 +-
  fs/smb/client/compress.c      | 146 ++++++++-----
  fs/smb/client/compress.h      |  10 +-
  fs/smb/client/smb1ops.c       |   5 +-
  fs/smb/client/smb2ops.c       | 273 +-----------------------
  fs/smb/client/smb2proto.h     |   4 +
  fs/smb/client/smb2transport.c | 113 ++++++++++
  fs/smb/client/smbdirect.c     | 105 ++-------
  fs/smb/client/smbdirect.h     |   5 +-
  fs/smb/client/transport.c     | 387 +++++++++++++++++++++++-----------
  11 files changed, 517 insertions(+), 540 deletions(-)
fs/smb/client/cifsglob.h +4 -2
index 2cb858495bc1..f7c12d24e2ea 100644
@@ -571,8 +571,10 @@ struct smb_version_operations {
  	long (*fallocate)(struct file *, struct cifs_tcon *, int, loff_t,
  			  loff_t);
  	/* init transform (compress/encrypt) request */
-	int (*init_transform_rq)(struct TCP_Server_Info *, int num_rqst,
-				 struct smb_rqst *, struct smb_rqst *);
+	int (*init_transform_rq)(struct TCP_Server_Info *server,
+				 int num_rqst, const struct smb_rqst *rqst,
+				 struct smb2_transform_hdr *tr_hdr,
+				 struct iov_iter *iter);
  	enum securityEnum (*select_sectype)(struct TCP_Server_Info *,
  			    enum securityEnum);
  	int (*next_header)(struct TCP_Server_Info *server, char *buf,
fs/smb/client/cifsproto.h +1 -2
index 8dedfd677f8d..1c9ac67b7294 100644
@@ -106,8 +106,7 @@ int compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
  		       int *resp_buf_type, struct kvec *resp_iov);
  int cifs_sync_mid_result(struct smb_message *mid,
  			 struct TCP_Server_Info *server);
-int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
-		    struct smb_rqst *rqst);
+int __smb_send_rqst(struct TCP_Server_Info *server, struct iov_iter *iter);
  int wait_for_free_request(struct TCP_Server_Info *server, const int flags,
  			  unsigned int *instance);
  int cifs_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
fs/smb/client/compress.c +98 -48
index be9023f841e6..a9496983ac56 100644
@@ -16,6 +16,7 @@
  #include <linux/kernel.h>
  #include <linux/uio.h>
  #include <linux/sort.h>
+#include <linux/vmalloc.h>
 
  #include "cifsglob.h"
  #include "../common/smb2pdu.h"
@@ -191,7 +192,7 @@ static int collect_sample(const struct iov_iter *source, ssize_t max, u8 *sample
   * Tests shows that this function is quite reliable in predicting data compressibility,
   * matching close to 1:1 with the behaviour of LZ77 compression success and failures.
   */
-static bool is_compressible(const struct iov_iter *data)
+bool is_compressible(const struct iov_iter *data)
  {
  	const size_t read_size = SZ_2K, bkt_size = 256, max = SZ_4M;
  	struct bucket *bkt = NULL;
@@ -295,74 +296,123 @@ bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq)
  		if (le32_to_cpu(wreq->Length) < SMB_COMPRESS_MIN_LEN)
  			return false;
 
-		return is_compressible(&rq->rq_iter);
+		return true;
  	}
 
  	return (shdr->Command == SMB2_READ);
  }
 
-int smb_compress(struct TCP_Server_Info *server, struct smb_rqst *rq, compress_send_fn send_fn)
+/*
+ * vmap the pages from a BVECQ-type iterator.
+ */
+static void *vmap_bvecq(struct iov_iter *iter, pgprot_t prot)
  {
-	struct iov_iter iter;
-	u32 slen, dlen;
-	void *src, *dst = NULL;
-	int ret;
+	struct page **pages = NULL;
+	ssize_t size, offset;
+	void *map;
 
-	if (!server || !rq || !rq->rq_iov || !rq->rq_iov->iov_base)
-		return -EINVAL;
+	if (WARN_ON(!iov_iter_is_bvecq(iter)))
+		return ERR_PTR(-EIO);
 
-	if (rq->rq_iov->iov_len != sizeof(struct smb2_write_req))
-		return -EINVAL;
+	size = iov_iter_extract_pages(iter, &pages, INT_MAX, INT_MAX, 0, &offset);
+	if (size < 0)
+		return ERR_PTR(size);
 
-	slen = iov_iter_count(&rq->rq_iter);
-	src = kvzalloc(slen, GFP_KERNEL);
-	if (!src) {
-		ret = -ENOMEM;
-		goto err_free;
+	if (size == 0 || offset > 0) {
+		kvfree(pages);
+		return ERR_PTR(-EIO);
  	}
 
-	/* Keep the original iter intact. */
-	iter = rq->rq_iter;
+	map = vmap(pages, DIV_ROUND_UP(size, PAGE_SIZE), 0, prot);
+	kvfree(pages);
+	if (!map)
+		return ERR_PTR(-ENOMEM);
+	return map;
+}
+
+int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
+		 struct bvecq **bq)
+{
+	struct smb2_compression_hdr *z_hdr;
+	struct smb2_write_req *w_hdr;
+	struct smb2_hdr *shdr;
+	struct iov_iter tmp;
+	struct bvecq *sbq = *bq, *dbq = NULL;
+	void *src, *dst = NULL;
+	u32 slen, dlen, hlen, datalen;
+	int ret;
 
-	if (!copy_from_iter_full(src, slen, &iter)) {
-		ret = smb_EIO(smb_eio_trace_compress_copy);
+	/* We need contiguous buffers for the compression algorithm. */
+	slen = iov_iter_count(iter);
+	src = vmap_bvecq(iter, PAGE_KERNEL_RO);
+	if (IS_ERR(src)) {
+		ret = PTR_ERR(src);
+		src = NULL;
  		goto err_free;
  	}
 
+	shdr = src;
+	hlen  = le16_to_cpu(shdr->StructureSize);
+	w_hdr = src;
+	hlen = le16_to_cpu(w_hdr->DataOffset);
+	datalen = le32_to_cpu(w_hdr->Length);
+	if (datalen != slen - hlen) {
+		pr_warn("datalen %x != slen-hlen %x\n", datalen, slen - hlen);
+		return -EMSGSIZE;
+	}
+
+	/*
+	 * This is just overprovisioning, as the algorithm will error out if @dst reaches 7/8
+	 * of @slen.
+	 */
  	dlen = lz77_compressed_alloc_size(slen);
-	dst = kvzalloc(dlen, GFP_KERNEL);
-	if (!dst) {
+	dlen = sizeof(*z_hdr) + slen;
+	dbq = bvecq_alloc_buffer(dlen, GFP_NOFS);
+	if (!dbq) {
  		ret = -ENOMEM;
  		goto err_free;
  	}
 
-	ret = lz77_compress(src, slen, dst, &dlen);
-	if (!ret) {
-		struct smb2_compression_hdr hdr = { 0 };
-		struct smb_rqst comp_rq = { .rq_nvec = 3, };
-		struct kvec iov[3];
-
-		hdr.ProtocolId = SMB2_COMPRESSION_TRANSFORM_ID;
-		hdr.OriginalCompressedSegmentSize = cpu_to_le32(slen);
-		hdr.CompressionAlgorithm = SMB3_COMPRESS_LZ77;
-		hdr.Flags = SMB2_COMPRESSION_FLAG_NONE;
-		hdr.Offset = cpu_to_le32(rq->rq_iov[0].iov_len);
-
-		iov[0].iov_base = &hdr;
-		iov[0].iov_len = sizeof(hdr);
-		iov[1] = rq->rq_iov[0];
-		iov[2].iov_base = dst;
-		iov[2].iov_len = dlen;
-
-		comp_rq.rq_iov = iov;
-
-		ret = send_fn(server, 1, &comp_rq);
-	} else if (ret == -EMSGSIZE || dlen >= slen) {
-		ret = send_fn(server, 1, rq);
+	iov_iter_bvec_queue(&tmp, ITER_DEST, dbq, 1, 0, dlen);
+	dst = vmap_bvecq(&tmp, PAGE_KERNEL);
+	if (IS_ERR(dst)) {
+		ret = PTR_ERR(dst);
+		dst = NULL;
+		goto err_free;
  	}
-err_free:
-	kvfree(dst);
-	kvfree(src);
+	z_hdr = dst;
+	dst  += sizeof(*z_hdr) + hlen;
+	dlen -= sizeof(*z_hdr) + hlen;
+
+	ret = lz77_compress(src + hlen, slen - hlen, dst, &dlen);
+	if (ret)
+		goto err_free;
+
+	dlen += sizeof(*z_hdr) + hlen;
+	dst -= hlen;
+	memcpy(dst, src, hlen);
 
+	z_hdr->ProtocolId			= SMB2_COMPRESSION_TRANSFORM_ID;
+	z_hdr->OriginalCompressedSegmentSize	= cpu_to_le32(datalen);
+	z_hdr->CompressionAlgorithm		= SMB3_COMPRESS_LZ77;
+	z_hdr->Flags				= SMB2_COMPRESSION_FLAG_NONE;
+	z_hdr->Offset				= cpu_to_le32(hlen);
+
+	vunmap(z_hdr);
+	vunmap(src);
+
+	dbq->bv[0] = sbq->bv[0];
+	memset(&sbq->bv[0], 0, sizeof(sbq->bv[0]));
+	bvecq_shorten_buffer(dbq, 1, dlen);
+	bvecq_dump(dbq);
+
+	bvecq_put(sbq);
+	*bq = dbq;
+	return dlen;
+
+err_free:
+	vunmap(z_hdr);
+	vunmap(src);
+	bvecq_put(dbq);
  	return ret;
  }
fs/smb/client/compress.h +7 -3
index 2679baca129b..48812b0e7476 100644
@@ -30,9 +30,10 @@
  typedef int (*compress_send_fn)(struct TCP_Server_Info *, int, struct smb_rqst *);
 
 
-int smb_compress(struct TCP_Server_Info *server, struct smb_rqst *rq,
-		 compress_send_fn send_fn);
+int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
+		 struct bvecq **bq);
  bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq);
+bool is_compressible(const struct iov_iter *data);
 
  /*
   * smb_compress_alg_valid() - Validate a compression algorithm.
@@ -58,11 +59,14 @@ static __always_inline int smb_compress_alg_valid(__le16 alg, bool valid_none)
  	return false;
  }
  #else /* !CONFIG_CIFS_COMPRESSION */
-static inline int smb_compress(void *unused1, void *unused2, void *unused3)
+static inline
+int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
+		 struct bvecq **bq)
  {
  	return -EOPNOTSUPP;
  }
 
+static inline bool is_compressible(const struct iov_iter *data) { return false; }
  static inline bool should_compress(void *unused1, void *unused2)
  {
  	return false;
fs/smb/client/smb1ops.c +4 -1
index 042d44788321..8477440bbcc2 100644
@@ -142,6 +142,7 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
  	       struct smb_rqst *rqst, struct smb_message *smb,
  	       unsigned int xid)
  {
+	struct iov_iter iter;
  	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  	struct kvec iov[1];
  	struct smb_rqst crqst = { .rq_iov = iov, .rq_nvec = 1 };
@@ -162,13 +163,15 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
  		return rc;
  	}
 
+	iov_iter_kvec(&iter, ITER_SOURCE, iov, 1, iov[0].iov_len);
+
  	/*
  	 * The response to this call was already factored into the sequence
  	 * number when the call went out, so we must adjust it back downward
  	 * after signing here.
  	 */
  	--server->sequence_number;
-	rc = __smb_send_rqst(server, 1, &crqst);
+	rc = __smb_send_rqst(server, &iter);
  	if (rc < 0)
  		server->sequence_number--;
 
fs/smb/client/smb2ops.c +5 -268
index 3c30417a3ea6..6125184d9826 100644
@@ -2746,9 +2746,7 @@ void
  smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
  {
  	struct smb2_hdr *shdr;
-	struct cifs_ses *ses = tcon->ses;
-	struct TCP_Server_Info *server = ses->server;
-	unsigned long len = smb_rqst_len(server, rqst);
+	size_t len = 0;
  	int num_padding;
 
  	shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
@@ -2757,6 +2755,10 @@ smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
  		return;
  	}
 
+	for (int i = 0; i < rqst->rq_nvec; i++)
+		len += rqst->rq_iov[i].iov_len;
+	len += iov_iter_count(&rqst->rq_iter);
+
  	/* SMB headers in a compound are 8 byte aligned. */
  	if (IS_ALIGNED(len, 8))
  		goto out;
@@ -4326,105 +4328,6 @@ smb2_dir_needs_close(struct cifsFileInfo *cfile)
  	return !cfile->invalidHandle;
  }
 
-static void
-fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
-		   struct smb_rqst *old_rq, __le16 cipher_type)
-{
-	struct smb2_hdr *shdr =
-			(struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
-
-	memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
-	tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
-	tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
-	tr_hdr->Flags = cpu_to_le16(0x01);
-	if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
-	    (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
-		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
-	else
-		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
-	memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
-}
-
-static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
-				 int num_rqst, const u8 *sig, u8 **iv,
-				 struct aead_request **req, struct sg_table *sgt,
-				 unsigned int *num_sgs)
-{
-	unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
-	unsigned int iv_size = crypto_aead_ivsize(tfm);
-	unsigned int len;
-	u8 *p;
-
-	*num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
-	if (IS_ERR_VALUE((long)(int)*num_sgs))
-		return ERR_PTR(*num_sgs);
-
-	len = iv_size;
-	len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
-	len = ALIGN(len, crypto_tfm_ctx_alignment());
-	len += req_size;
-	len = ALIGN(len, __alignof__(struct scatterlist));
-	len += array_size(*num_sgs, sizeof(struct scatterlist));
-
-	p = kzalloc(len, GFP_NOFS);
-	if (!p)
-		return ERR_PTR(-ENOMEM);
-
-	*iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
-	*req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
-						crypto_tfm_ctx_alignment());
-	sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
-						   __alignof__(struct scatterlist));
-	return p;
-}
-
-static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
-			       int num_rqst, const u8 *sig, u8 **iv,
-			       struct aead_request **req, struct scatterlist **sgl)
-{
-	struct sg_table sgtable = {};
-	unsigned int skip, num_sgs, i, j;
-	ssize_t rc;
-	void *p;
-
-	p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable, &num_sgs);
-	if (IS_ERR(p))
-		return ERR_CAST(p);
-
-	sg_init_marker(sgtable.sgl, num_sgs);
-
-	/*
-	 * The first rqst has a transform header where the
-	 * first 20 bytes are not part of the encrypted blob.
-	 */
-	skip = 20;
-
-	for (i = 0; i < num_rqst; i++) {
-		struct iov_iter *iter = &rqst[i].rq_iter;
-		size_t count = iov_iter_count(iter);
-
-		for (j = 0; j < rqst[i].rq_nvec; j++) {
-			cifs_sg_set_buf(&sgtable,
-					rqst[i].rq_iov[j].iov_base + skip,
-					rqst[i].rq_iov[j].iov_len - skip);
-
-			/* See the above comment on the 'skip' assignment */
-			skip = 0;
-		}
-		sgtable.orig_nents = sgtable.nents;
-
-		rc = extract_iter_to_sg(iter, count, &sgtable,
-					num_sgs - sgtable.nents, 0);
-		iov_iter_revert(iter, rc);
-		sgtable.orig_nents = sgtable.nents;
-	}
-
-	cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
-	sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
-	*sgl = sgtable.sgl;
-	return p;
-}
-
  int
  smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
  {
@@ -4454,172 +4357,6 @@ smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
  	return -EAGAIN;
  }
 
-/*
- * Encrypt @rqst message. @rqst[0] has the following format:
- * iov[0]   - transform header (associate data),
- * iov[1-N] - SMB2 header and pages - data to encrypt.
- * On success return encrypted data in iov[1-N] and pages, leave iov[0]
- * untouched.
- */
-static int
-encrypt_message(struct TCP_Server_Info *server, int num_rqst,
-		struct smb_rqst *rqst, struct crypto_aead *tfm)
-{
-	struct smb2_transform_hdr *tr_hdr =
-		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
-	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
-	int rc = 0;
-	struct scatterlist *sg;
-	u8 sign[SMB2_SIGNATURE_SIZE] = {};
-	u8 key[SMB3_ENC_DEC_KEY_SIZE];
-	struct aead_request *req;
-	u8 *iv;
-	DECLARE_CRYPTO_WAIT(wait);
-	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
-	void *creq;
-
-	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), 1, key);
-	if (rc) {
-		cifs_server_dbg(FYI, "%s: Could not get encryption key. sid: 0x%llx\n",
-				__func__, le64_to_cpu(tr_hdr->SessionId));
-		return rc;
-	}
-
-	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
-		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
-		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
-	else
-		rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
-
-	if (rc) {
-		cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
-		return rc;
-	}
-
-	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
-	if (rc) {
-		cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
-		return rc;
-	}
-
-	creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg);
-	if (IS_ERR(creq))
-		return PTR_ERR(creq);
-
-	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
-	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
-		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
-	else {
-		iv[0] = 3;
-		memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
-	}
-
-	aead_request_set_tfm(req, tfm);
-	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
-	aead_request_set_ad(req, assoc_data_len);
-
-	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
-				  crypto_req_done, &wait);
-
-	rc = crypto_wait_req(crypto_aead_encrypt(req), &wait);
-
-	if (!rc)
-		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
-
-	kfree_sensitive(creq);
-	return rc;
-}
-
-/*
- * Copy data from an iterator to the pages in a bvec queue buffer.
- */
-static bool cifs_copy_iter_to_bvecq(struct iov_iter *iter, size_t size,
-				    struct bvecq *buffer)
-{
-	for (; buffer; buffer = buffer->next) {
-		for (int s = 0; s < buffer->nr_slots; s++) {
-			struct bio_vec *bv = &buffer->bv[s];
-			size_t part = umin(bv->bv_len, size);
-
-			if (copy_page_from_iter(bv->bv_page, bv->bv_offset,
-						part, iter) != part)
-				return false;
-			size -= part;
-		}
-	}
-	return true;
-}
-
-void
-smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
-{
-	for (int i = 0; i < num_rqst; i++)
-		bvecq_put(rqst[i].rq_buffer);
-}
-
-/*
- * This function will initialize new_rq and encrypt the content.
- * The first entry, new_rq[0], only contains a single iov which contains
- * a smb2_transform_hdr and is pre-allocated by the caller.
- * This function then populates new_rq[1+] with the content from olq_rq[0+].
- *
- * The end result is an array of smb_rqst structures where the first structure
- * only contains a single iov for the transform header which we then can pass
- * to crypt_message().
- *
- * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
- * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
- */
-static int
-smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
-		       struct smb_rqst *new_rq, struct smb_rqst *old_rq)
-{
-	struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
-	unsigned int orig_len = 0;
-	int rc = -ENOMEM;
-
-	for (int i = 1; i < num_rqst; i++) {
-		struct smb_rqst *old = &old_rq[i - 1];
-		struct smb_rqst *new = &new_rq[i];
-		struct bvecq *buffer = NULL;
-		size_t size = iov_iter_count(&old->rq_iter);
-
-		orig_len += smb_rqst_len(server, old);
-		new->rq_iov = old->rq_iov;
-		new->rq_nvec = old->rq_nvec;
-
-		if (size > 0) {
-			rc = -ENOMEM;
-			buffer = bvecq_alloc_buffer(size, GFP_NOFS);
-			if (!buffer)
-				goto err_free;
-
-			new->rq_buffer = buffer;
-			iov_iter_bvec_queue(&new->rq_iter, ITER_SOURCE,
-					    buffer, 0, 0, size);
-
-			if (!cifs_copy_iter_to_bvecq(&old->rq_iter, size, buffer)) {
-				rc = smb_EIO1(smb_eio_trace_tx_copy_iter_to_buf, size);
-				goto err_free;
-			}
-		}
-	}
-
-	/* fill the 1st iov with a transform header */
-	fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
-
-	rc = encrypt_message(server, num_rqst, new_rq, server->secmech.enc);
-	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
-	if (rc)
-		goto err_free;
-
-	return rc;
-
-err_free:
-	smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
-	return rc;
-}
-
  int __cifs_sfu_make_node(unsigned int xid, struct inode *inode,
  				struct dentry *dentry, struct cifs_tcon *tcon,
  				const char *full_path, umode_t mode, dev_t dev,
fs/smb/client/smb2proto.h +4 -0
index 0b3df644222b..3b4bc946d1bb 100644
@@ -115,6 +115,10 @@ int smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  int smb2_push_mandatory_locks(struct cifsFileInfo *cfile);
  void smb2_reconnect_server(struct work_struct *work);
  int smb3_crypto_aead_allocate(struct TCP_Server_Info *server);
+int smb3_init_transform_rq(struct TCP_Server_Info *server,
+			   int num_rqst, const struct smb_rqst *rqst,
+			   struct smb2_transform_hdr *tr_hdr,
+			   struct iov_iter *iter);
  unsigned long smb_rqst_len(struct TCP_Server_Info *server,
  			   struct smb_rqst *rqst);
  void smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst);
fs/smb/client/smb2transport.c +113 -0
index 1086877e9d84..874956515322 100644
@@ -886,6 +886,119 @@ static void *smb2_get_aead_req_new(struct crypto_aead *tfm, const struct iov_ite
  	return p;
  }
 
+/*
+ * Encrypt the message in the buffer described by the iterator.
+ * On success return encrypted data in iov[1-N] and pages, leave iov[0]
+ * untouched.
+ */
+static int
+encrypt_message(struct TCP_Server_Info *server,
+		struct smb2_transform_hdr *tr_hdr,
+		struct iov_iter *iter, struct crypto_aead *tfm)
+{
+	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
+	int rc = 0;
+	struct scatterlist *sg;
+	u8 key[SMB3_ENC_DEC_KEY_SIZE];
+	struct aead_request *req;
+	u8 *iv;
+	DECLARE_CRYPTO_WAIT(wait);
+	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
+	void *creq;
+	size_t sensitive_size;
+
+	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), 1, key);
+	if (rc) {
+		cifs_server_dbg(FYI, "%s: Could not get encryption key. sid: 0x%llx\n",
+				__func__, le64_to_cpu(tr_hdr->SessionId));
+		return rc;
+	}
+
+	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
+		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
+	else
+		rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
+
+	if (rc) {
+		cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
+		return rc;
+	}
+
+	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
+	if (rc) {
+		cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
+		return rc;
+	}
+
+	creq = smb2_get_aead_req_new(tfm, iter, tr_hdr->Signature, &iv, &req, &sg,
+				     &sensitive_size);
+	if (IS_ERR(creq))
+		return PTR_ERR(creq);
+
+	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
+	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
+	else {
+		iv[0] = 3;
+		memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
+	}
+
+	aead_request_set_tfm(req, tfm);
+	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
+	aead_request_set_ad(req, assoc_data_len);
+
+	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+				  crypto_req_done, &wait);
+
+	rc = crypto_wait_req(crypto_aead_encrypt(req), &wait);
+
+	kvfree_sensitive(creq, sensitive_size);
+	return rc;
+}
+
+static void
+fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
+		   const struct smb_rqst *old_rq, __le16 cipher_type)
+{
+	struct smb2_hdr *shdr = (struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
+
+	*tr_hdr = (struct smb2_transform_hdr){
+		.ProtocolId		= SMB2_TRANSFORM_PROTO_NUM,
+		.OriginalMessageSize	= cpu_to_le32(orig_len),
+		.Flags			= cpu_to_le16(0x01),
+		.SessionId		= shdr->SessionId,
+	};
+	if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
+	    (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
+	else
+		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
+}
+
+/*
+ * This function encrypts the content in the buffer described by the iterator
+ * and fills in the transform header.  The source request buffers are provided
+ * for reference.
+ */
+int
+smb3_init_transform_rq(struct TCP_Server_Info *server,
+		       int num_rqst, const struct smb_rqst *rqst,
+		       struct smb2_transform_hdr *tr_hdr,
+		       struct iov_iter *iter)
+{
+	size_t orig_len = iov_iter_count(iter) - sizeof(*tr_hdr);
+	int rc;
+
+	fill_transform_hdr(tr_hdr, orig_len, rqst, server->cipher_type);
+
+	iov_iter_advance(iter, offsetof(struct smb2_transform_hdr, Nonce));
+
+	rc = encrypt_message(server, tr_hdr, iter, server->secmech.enc);
+	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
+	return rc;
+}
+
  /*
   * Decrypt the PDU in the iterator.  The PDU begins with the transform header.
   */
fs/smb/client/smbdirect.c +0 -30
index 563ef488a225..e20fbc10c82e 100644
@@ -164,36 +164,6 @@ do {									\
  #define log_rdma_mr(level, fmt, args...) \
  		log_rdma(level, LOG_RDMA_MR, fmt, ##args)
 
-static int smbd_post_send_full_iter(struct smbdirect_socket *sc,
-				    struct smbdirect_send_batch *batch,
-				    struct iov_iter *iter,
-				    u32 remaining_data_length)
-{
-	int bytes = 0;
-
-	/*
-	 * smbdirect_connection_send_single_iter() respects the
-	 * negotiated max_send_size, so we need to
-	 * loop until the full iter is posted
-	 */
-
-	while (iov_iter_count(iter) > 0) {
-		int rc;
-
-		rc = smbdirect_connection_send_single_iter(sc,
-							   batch,
-							   iter,
-							   0, /* flags */
-							   remaining_data_length);
-		if (rc < 0)
-			return rc;
-		remaining_data_length -= rc;
-		bytes += rc;
-	}
-
-	return bytes;
-}
-
  /*
   * Destroy the transport and related RDMA and memory resources
   * Need to go through all the pending counters and make sure on one is using
@@ -405,85 +375,46 @@ int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
 

fs/smb/client/smbdirect.c int smbd_recv(struct smbd_connection *info, struct msghdr *msg) · line 381

Quoted 10 lines
fs/smb/client/smbdirect.c +1 -4
  /*
   * Send data to transport
- * Each rqst is transported as a SMBDirect payload
- * rqst: the data to write
   * return value: 0 if successfully write, otherwise error code
   */
-int smbd_send(struct TCP_Server_Info *server,
-	int num_rqst, struct smb_rqst *rqst_array)
+int smbd_send(struct TCP_Server_Info *server, struct iov_iter *iter)
  {

I think this can become a tiny wrapper just calling smbdirect_connection_send_iter()

Quoted 248 lines
fs/smb/client/smbdirect.c +17 -53
  	struct smbd_connection *info = server->smbd_conn;
  	struct smbdirect_socket *sc = info->socket;
  	const struct smbdirect_socket_parameters *sp = smbd_get_parameters(info);
-	struct smb_rqst *rqst;
-	struct iov_iter iter;
  	struct smbdirect_send_batch_storage bstorage;
  	struct smbdirect_send_batch *batch;
-	unsigned int remaining_data_length, klen;
-	int rc, i, rqst_idx;
-	int error = 0;
+	size_t size = iov_iter_count(iter);
+	int rc, error = 0;
 
  	if (!smbdirect_connection_is_connected(sc))
  		return -EAGAIN;
 
-	/*
-	 * Add in the page array if there is one. The caller needs to set
-	 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
-	 * ends at page boundary
-	 */
-	remaining_data_length = 0;
-	for (i = 0; i < num_rqst; i++)
-		remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
-
-	if (unlikely(remaining_data_length > sp->max_fragmented_send_size)) {
+	if (unlikely(size > sp->max_fragmented_send_size)) {
  		/* assertion: payload never exceeds negotiated maximum */
-		log_write(ERR, "payload size %d > max size %d\n",
-			remaining_data_length, sp->max_fragmented_send_size);
+		log_write(ERR, "payload size %zu > max size %d\n",
+			  size, sp->max_fragmented_send_size);
  		return -EINVAL;
  	}
 
-	log_write(INFO, "num_rqst=%d total length=%u\n",
-			num_rqst, remaining_data_length);
+	log_write(INFO, "size=%zu\n", size);
 
-	rqst_idx = 0;
+	/*
+	 * smbdirect_connection_send_single_iter() respects the negotiated
+	 * max_send_size, so we need to loop until the full iter is posted
+	 */
  	batch = smbdirect_init_send_batch_storage(&bstorage, false, 0);
-	do {
-		rqst = &rqst_array[rqst_idx];
-
-		cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
-			 rqst_idx, smb_rqst_len(server, rqst));
-		for (i = 0; i < rqst->rq_nvec; i++)
-			dump_smb(rqst->rq_iov[i].iov_base, rqst->rq_iov[i].iov_len);
-
-		log_write(INFO, "RDMA-WR[%u] nvec=%d len=%u iter=%zu rqlen=%lu\n",
-			  rqst_idx, rqst->rq_nvec, remaining_data_length,
-			  iov_iter_count(&rqst->rq_iter), smb_rqst_len(server, rqst));
-
-		/* Send the metadata pages. */
-		klen = 0;
-		for (i = 0; i < rqst->rq_nvec; i++)
-			klen += rqst->rq_iov[i].iov_len;
-		iov_iter_kvec(&iter, ITER_SOURCE, rqst->rq_iov, rqst->rq_nvec, klen);
-
-		rc = smbd_post_send_full_iter(sc, batch, &iter, remaining_data_length);
+	while (iov_iter_count(iter) > 0) {
+		rc = smbdirect_connection_send_single_iter(sc,
+							   batch,
+							   iter,
+							   0, /* flags */
+							   iov_iter_count(iter));
  		if (rc < 0) {
  			error = rc;
  			break;
  		}
-		remaining_data_length -= rc;
-
-		if (iov_iter_count(&rqst->rq_iter) > 0) {
-			/* And then the data pages if there are any */
-			rc = smbd_post_send_full_iter(sc, batch, &rqst->rq_iter,
-						      remaining_data_length);
-			if (rc < 0) {
-				error = rc;
-				break;
-			}
-			remaining_data_length -= rc;
-		}
-
-	} while (++rqst_idx < num_rqst);
+	}
 
  	rc = smbdirect_connection_send_batch_flush(sc, batch, true);
  	if (unlikely(!rc && error))
fs/smb/client/smbdirect.h +2 -3
index be205ec02077..a5c17e796ff9 100644
@@ -40,8 +40,7 @@ void smbd_destroy(struct TCP_Server_Info *server);
 
  /* Interface for carrying upper layer I/O through send/recv */
  int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
-int smbd_send(struct TCP_Server_Info *server,
-	int num_rqst, struct smb_rqst *rqst);
+int smbd_send(struct TCP_Server_Info *server, struct iov_iter *iter);
 
  /* Interfaces to register and deregister MR for RDMA read/write */
  struct smbdirect_mr_io *smbd_register_mr(
@@ -61,7 +60,7 @@ static inline void *smbd_get_connection(
  static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
  static inline void smbd_destroy(struct TCP_Server_Info *server) {}
  static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
-static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }
+static inline int smbd_send(struct TCP_Server_Info *server, struct iov_iter *iter) {return -1; }
  #endif
 
  #endif
fs/smb/client/transport.c +80 -31
index 3f4d1a52b45c..3ead26f76112 100644
@@ -23,6 +23,9 @@
  #include <linux/sched/signal.h>
  #include <linux/task_io_accounting_ops.h>
  #include <linux/task_work.h>
+#include <linux/iov_iter.h>
+#include "rfc1002pdu.h"
+#include "cifspdu.h"
  #include "cifsglob.h"
  #include "cifsproto.h"
  #include "cifs_debug.h"
@@ -252,51 +255,100 @@ smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
  	return 0;
  }
 
-unsigned long
-smb_rqst_len(struct TCP_Server_Info *server, struct smb_rqst *rqst)
+/*
+ * smb_sendmsg - send a buffer to the socket
+ * @server:	Server to send the data to
+ * @iter:	The data to send (not advanced)
+ * @sent:	The amount of data sent
+ *
+ * Our basic "send data to server" function. Should be called with srv_mutex
+ * held. The caller is responsible for handling the results.
+ */
+static int smb_sendmsg(struct TCP_Server_Info *server, const struct iov_iter *iter,
+		       size_t *sent)
  {
-	unsigned int i;
-	struct kvec *iov;
-	int nvec;
-	unsigned long buflen = 0;
-
-	if (!is_smb1(server) && rqst->rq_nvec >= 2 &&
-	    rqst->rq_iov[0].iov_len == 4) {
-		iov = &rqst->rq_iov[1];
-		nvec = rqst->rq_nvec - 1;
-	} else {
-		iov = rqst->rq_iov;
-		nvec = rqst->rq_nvec;
-	}
+	struct socket *ssocket = server->ssocket;
+	struct msghdr msg = {
+		/*
+		 * MSG_SPLICE_PAGES causes tcp_sendmsg() to splice in the pages
+		 * in the iterator rather than copying from them.
+		 */
+		.msg_flags	= MSG_NOSIGNAL | MSG_SPLICE_PAGES,
+		.msg_iter	= *iter,
+	};
+	int retries = 0;
+	int rc = 0;
+
+	*sent = 0;
+	if (server->noblocksnd)
+		msg.msg_flags = MSG_DONTWAIT;
+
+	while (msg_data_left(&msg)) {
+		/*
+		 * If blocking send, we try 3 times, since each can block for 5
+		 * seconds. For nonblocking we have to try more but wait
+		 * increasing amounts of time allowing time for socket to
+		 * clear.  The overall time we wait in either case to send on
+		 * the socket is about 15 seconds.  Similarly we wait for 15
+		 * seconds for a response from the server in SendReceive[2] for
+		 * the server to send a response back for most types of
+		 * requests (except SMB Write past end of file which can be
+		 * slow, and blocking lock operations). NFS waits slightly
+		 * longer than CIFS, but this can make it take longer for
+		 * nonresponsive servers to be detected and 15 seconds is more
+		 * than enough time for modern networks to send a packet.  In
+		 * most cases if we fail to send after the retries we will kill
+		 * the socket and reconnect which may clear the network
+		 * problem.
+		 */
+		rc = sock_sendmsg(ssocket, &msg);
+		if (rc == -EAGAIN) {
+			retries++;
+			if (retries >= 14 ||
+			    (!server->noblocksnd && (retries > 2))) {
+				cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
+					 ssocket);
+				return -EAGAIN;
+			}
+			msleep(1 << retries);
+			continue;
+		}
+
+		if (rc < 0)
+			return rc;
+
+		if (rc == 0) {
+			/* should never happen, letting socket clear before
+			   retrying is our only obvious option here */
+			cifs_server_dbg(VFS, "tcp sent no data\n");
+			msleep(500);
+			continue;
+		}
 
-	/* total up iov array first */
-	for (i = 0; i < nvec; i++)
-		buflen += iov[i].iov_len;
+		*sent += rc;
 
-	buflen += iov_iter_count(&rqst->rq_iter);
-	return buflen;
+		/* send was at least partially successful */
+		retries = 0; /* in case we get ENOSPC on the next send */
+	}
+	return 0;
  }
 
-int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
-		    struct smb_rqst *rqst)
+int
+__smb_send_rqst(struct TCP_Server_Info *server, struct iov_iter *iter)
  {
-	int rc;
-	struct kvec *iov;
-	int n_vec;
-	unsigned int send_length = 0;
-	unsigned int i, j;
-	sigset_t mask, oldmask;
-	size_t total_len = 0, sent, size;
  	struct socket *ssocket = server->ssocket;
-	struct msghdr smb_msg = {};
-	__be32 rfc1002_marker;
+	sigset_t mask, oldmask;
+	size_t total_to_send = iov_iter_count(iter), sent = 0;

fs/smb/client/transport.c smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg, · line 349

Quoted 10 lines
fs/smb/client/transport.c +3 -2
+	int rc;
 
  	cifs_in_send_inc(server);
  	if (cifs_rdma_enabled(server)) {
  		/* return -EAGAIN when connecting or reconnecting */
  		rc = -EAGAIN;
-		if (server->smbd_conn)
-			rc = smbd_send(server, num_rqst, rqst);
+		if (server->smbd_conn) {
+			iov_iter_advance(iter, 4);

This is not needed if smbd_send() just calls
smbdirect_connection_send_iter()

Quoted 319 lines
fs/smb/client/transport.c +178 -93
+			rc = smbd_send(server, iter);
+		}
  		goto smbd_done;
  	}
 
@@ -314,10 +366,6 @@ int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
  	/* cork the socket */
  	tcp_sock_set_cork(ssocket->sk, true);
 
-	for (j = 0; j < num_rqst; j++)
-		send_length += smb_rqst_len(server, &rqst[j]);
-	rfc1002_marker = cpu_to_be32(send_length);
-
  	/*
  	 * We should not allow signals to interrupt the network send because
  	 * any partial send will cause session reconnects thus increasing
@@ -328,83 +376,44 @@ int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
  	sigfillset(&mask);
  	sigprocmask(SIG_BLOCK, &mask, &oldmask);
 
-	/* Generate a rfc1002 marker */
-	{
-		struct kvec hiov = {
-			.iov_base = &rfc1002_marker,
-			.iov_len  = 4
-		};
-		iov_iter_kvec(&smb_msg.msg_iter, ITER_SOURCE, &hiov, 1, 4);
-		rc = smb_send_kvec(server, &smb_msg, &sent);
-		if (rc < 0)
-			goto unmask;
-
-		total_len += sent;
-		send_length += 4;
-	}
-
-	cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
-
-	for (j = 0; j < num_rqst; j++) {
-		iov = rqst[j].rq_iov;
-		n_vec = rqst[j].rq_nvec;
-
-		size = 0;
-		for (i = 0; i < n_vec; i++) {
-			dump_smb(iov[i].iov_base, iov[i].iov_len);
-			size += iov[i].iov_len;
-		}
-
-		iov_iter_kvec(&smb_msg.msg_iter, ITER_SOURCE, iov, n_vec, size);
+	cifs_dbg(FYI, "Sending smb: smb_len=%zu\n", iov_iter_count(iter));
+	rc = smb_sendmsg(server, iter, &sent);
 
-		rc = smb_send_kvec(server, &smb_msg, &sent);
-		if (rc < 0)
-			goto unmask;
-
-		total_len += sent;
-
-		if (iov_iter_count(&rqst[j].rq_iter) > 0) {
-			smb_msg.msg_iter = rqst[j].rq_iter;
-			rc = smb_send_kvec(server, &smb_msg, &sent);
-			if (rc < 0)
-				break;
-			total_len += sent;
-		}
-	}
-
-unmask:
  	sigprocmask(SIG_SETMASK, &oldmask, NULL);
 
-	/*
-	 * If signal is pending but we have already sent the whole packet to
-	 * the server we need to return success status to allow a corresponding
-	 * mid entry to be kept in the pending requests queue thus allowing
-	 * to handle responses from the server by the client.
-	 *
-	 * If only part of the packet has been sent there is no need to hide
-	 * interrupt because the session will be reconnected anyway, so there
-	 * won't be any response from the server to handle.
-	 */
-
-	if (signal_pending(current) && (total_len != send_length)) {
-		cifs_dbg(FYI, "signal is pending after attempt to send\n");
-		rc = -ERESTARTSYS;
-	}
-
  	/* uncork it */
  	tcp_sock_set_cork(ssocket->sk, false);
 
-	if ((total_len > 0) && (total_len != send_length)) {
-		cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
-			 send_length, total_len);
+	if (sent > 0) {
+		/*
+		 * If signal is pending but we have already sent the whole
+		 * packet to the server we need to return success status to
+		 * allow a corresponding mid entry to be kept in the pending
+		 * requests queue thus allowing to handle responses from the
+		 * server by the client.
+		 *
+		 * If only part of the packet has been sent there is no need to
+		 * hide interrupt because the session will be reconnected
+		 * anyway, so there won't be any response from the server to
+		 * handle.
+		 */
+		if (signal_pending(current)) {
+			cifs_dbg(FYI, "signal is pending after attempt to send\n");
+			rc = -ERESTARTSYS;
+		}
+
  		/*
  		 * If we have only sent part of an SMB then the next SMB could
  		 * be taken as the remainder of this one. We need to kill the
  		 * socket so the server throws away the partial SMB
  		 */
-		cifs_signal_cifsd_for_reconnect(server, false);
-		trace_smb3_partial_send_reconnect(server->current_mid,
-						  server->conn_id, server->hostname);
+		if (sent != total_to_send) {
+			cifs_dbg(FYI, "partial send (wanted=%zu sent=%zu): terminating session\n",
+				 total_to_send, sent);
+			cifs_signal_cifsd_for_reconnect(server, false);
+			trace_smb3_partial_send_reconnect(server->current_mid,
+							  server->conn_id, server->hostname);
+		}
  	}
  smbd_done:
  	/*
@@ -424,41 +433,167 @@ int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
  	return rc;
  }
 
+static size_t smb3_copy_data_iter(void *iter_from, size_t progress, size_t len,
+				  void *priv, void *priv2)
+{
+	struct iov_iter *iter = priv;
+	return copy_to_iter(iter_from, len, iter) == len ? 0 : len;
+}
+
+/*
+ * Copy the data into a buffer that we can use for encryption in place and also
+ * pass to sendmsg() with MSG_SPLICE_PAGES.  This avoids a lot of copies in TCP
+ * at the expense of doing it upfront here.  A spare slot is left in the bvec
+ * queue at the front for the header(s).
+ *
+ * TODO: In future, the buffers should be allocated by the marshalling code.
+ */
+static int smb_copy_data_into_buffer(struct TCP_Server_Info *server,
+				     int num_rqst, struct smb_rqst *rqst,
+				     struct iov_iter *iter, struct bvecq **_bq)
+{
+	struct bvecq *bq;
+	size_t total_len = 0, offset = 0;
+
+	for (int i = 0; i < num_rqst; i++) {
+		struct smb_rqst *req = &rqst[i];
+		size_t size = iov_iter_count(&req->rq_iter);
+
+		for (int j = 0; j < req->rq_nvec; j++)
+			size += req->rq_iov[j].iov_len;
+		total_len = ALIGN8(total_len);
+		total_len += size;
+	}
+
+	bq = bvecq_alloc_buffer(total_len, GFP_NOFS);
+	if (!bq)
+		return -ENOMEM;
+
+	iov_iter_bvec_queue(iter, ITER_DEST, bq, 1, 0, total_len);
+
+	for (int i = 0; i < num_rqst; i++) {
+		struct smb_rqst *req = &rqst[i];
+		size_t size = iov_iter_count(&req->rq_iter);
+
+		if (offset & 7) {
+			unsigned int tmp = offset;
+			offset = ALIGN8(offset);
+			iov_iter_zero(offset - tmp, iter);
+		}
+
+		for (int j = 0; j < req->rq_nvec; j++) {
+			size_t len = req->rq_iov[j].iov_len;
+			if (copy_to_iter(req->rq_iov[j].iov_base, len, iter) != len)
+				goto error;
+			offset += len;
+		}
+
+		if (iterate_and_advance_kernel(&req->rq_iter,
+					       size, iter, NULL,
+					       smb3_copy_data_iter) != size)
+			goto error;
+
+		offset += size;
+	}
+
+	if (WARN_ONCE(offset != total_len,
+		      "offset=%zx total_len=%zx\n", offset, total_len)) {
+		goto error;
+	}
+	iov_iter_bvec_queue(iter, ITER_DEST, bq, 1, 0, total_len);
+	*_bq = bq;
+	return 0;
+error:
+	bvecq_put(bq);
+	*_bq = NULL;
+	return -EIO;
+}
+
  static int
  smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
  	      struct smb_rqst *rqst, int flags)
  {
-	struct smb2_transform_hdr tr_hdr;
-	struct smb_rqst new_rqst[MAX_COMPOUND] = {};
-	struct kvec iov = {
-		.iov_base = &tr_hdr,
-		.iov_len = sizeof(tr_hdr),
-	};
+	struct iov_iter iter;
+	struct bvecq *bq;
+	u32 content_len;
  	int rc;
 
-	if (flags & CIFS_COMPRESS_REQ)
-		return smb_compress(server, &rqst[0], __smb_send_rqst);
-
-	if (!(flags & CIFS_TRANSFORM_REQ))
-		return __smb_send_rqst(server, num_rqst, rqst);
-
-	if (WARN_ON_ONCE(num_rqst > MAX_COMPOUND - 1))
-		return smb_EIO1(smb_eio_trace_tx_max_compound, num_rqst);
-
-	if (!server->ops->init_transform_rq) {
+	if ((flags & CIFS_TRANSFORM_REQ) &&
+	    !server->ops->init_transform_rq) {
  		cifs_server_dbg(VFS, "Encryption requested but transform callback is missing\n");
  		return smb_EIO(smb_eio_trace_tx_need_transform);
  	}
 
-	new_rqst[0].rq_iov = &iov;
-	new_rqst[0].rq_nvec = 1;
+	rc = smb_copy_data_into_buffer(server, num_rqst, rqst, &iter, &bq);
+	if (rc)
+		return rc;
+	content_len = iov_iter_count(&iter);
+
+	{
+		struct smb2_transform_hdr *tr_hdr;
+		unsigned int hdr_len = 4, troff = 0;
+		__le32 *rfc1002;
+		void *hdr_blob;
+
+		if (flags & CIFS_TRANSFORM_REQ) {
+			troff = hdr_len;
+			hdr_len += sizeof(*tr_hdr);
+		}
+
+		/* TODO: Allocate netmem here */
+		rc = -ENOMEM;
+		hdr_blob = (void *)__get_free_page(GFP_NOFS);
+		if (!hdr_blob)
+			goto error;
+		bvec_set_virt(&bq->bv[0], hdr_blob, hdr_len);
+
+		if (flags & CIFS_COMPRESS_REQ) {
+			struct smb2_write_req *whdr = bvec_virt(&bq->bv[1]);
+			size_t doff = le16_to_cpu(whdr->DataOffset);
+
+			iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 1, doff,
+					    content_len - doff);
+
+			if (is_compressible(&iter)) {
+				iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 1, 0,
+						    content_len);
+
+				rc = smb_compress(server, &iter, &bq);
+				if (rc > 0) {
+					content_len = rc;
+				} else if (rc == -EMSGSIZE) {
+					/* Fall back to uncompressed. */
+				} else {
+					if (rc == 0)
+						rc = -EIO;
+					goto error;
+				}
+			}
+		}
+
+		if (flags & CIFS_TRANSFORM_REQ) {
+			iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 0, 0,
+					    hdr_len + content_len);
+			iov_iter_advance(&iter, troff);
+			tr_hdr = hdr_blob + troff;
+
+			rc = server->ops->init_transform_rq(server, num_rqst, rqst, tr_hdr, &iter);
+			if (rc)
+				goto error;
+			content_len += sizeof(*tr_hdr);
+		} else {
+			bvec_set_virt(&bq->bv[0], hdr_blob, hdr_len);
+		}
+
+		/* Set the RFC1002 header at the front. */
+		rfc1002 = hdr_blob;
+		*rfc1002 = cpu_to_be32(RFC1002_SESSION_MESSAGE << 24 | content_len);
 
-	rc = server->ops->init_transform_rq(server, num_rqst + 1,
-					    new_rqst, rqst);
-	if (!rc) {
-		rc = __smb_send_rqst(server, num_rqst + 1, new_rqst);
-		smb3_free_compound_rqst(num_rqst, &new_rqst[1]);
+		iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 0, 0, 4 + content_len);
  	}
+	rc = __smb_send_rqst(server, &iter);
+error:
+	bvecq_put(bq);
  	return rc;
  }
 
author maintainer
#18 lore raw

RFC PATCH 17/36 cifs: Don't use corking

In reply to David Howells

We don't need to cork the TCP socket if we send each message in a single sendmsg() and set MSG_EOR to mark the last packet of the message with a don't-append flag.

---
 fs/smb/client/transport.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
fs/smb/client/transport.c +4 -9
index 3ead26f76112..e43fce1099a9 100644
@@ -271,9 +271,11 @@ static int smb_sendmsg(struct TCP_Server_Info *server, const struct iov_iter *it
 	struct msghdr msg = {
 		/*
 		 * MSG_SPLICE_PAGES causes tcp_sendmsg() to splice in the pages
-		 * in the iterator rather than copying from them.
+		 * in the iterator rather than copying from them; MSG_EOR
+		 * indicates that the last TCP packet we create should be
+		 * marked no-append with regards to the next sendmsg.
 		 */
-		.msg_flags	= MSG_NOSIGNAL | MSG_SPLICE_PAGES,
+		.msg_flags	= MSG_NOSIGNAL | MSG_SPLICE_PAGES | MSG_EOR,
 		.msg_iter	= *iter,
 	};
 	int retries = 0;
@@ -362,10 +364,6 @@ __smb_send_rqst(struct TCP_Server_Info *server, struct iov_iter *iter)
 		goto out;
 	}
 
-	rc = 0;
-	/* cork the socket */
-	tcp_sock_set_cork(ssocket->sk, true);
-
 	/*
 	 * We should not allow signals to interrupt the network send because
 	 * any partial send will cause session reconnects thus increasing
@@ -381,9 +379,6 @@ __smb_send_rqst(struct TCP_Server_Info *server, struct iov_iter *iter)
 
 	sigprocmask(SIG_SETMASK, &oldmask, NULL);
 
-	/* uncork it */
-	tcp_sock_set_cork(ssocket->sk, false);
-
 	if (sent > 0) {
 		/*
 		 * If signal is pending but we have already sent the whole
author maintainer
#19 lore raw

RFC PATCH 18/36 cifs: Use page frag allocator for Tx buffers

In reply to David Howells

Use a per-TCP connection page fragment allocator for transmission buffers rather than using whole pages for messages less than half a page in size. Currently, the data is copied into there, but we should allocate these in future up front instead of kmalloc'd bits and avoid that copy too.

Note that these page fragments can be used with MSG_SPLICE_PAGES and thus avoid a copy inside the TCP stack.

cc Steve French <[email protected]>
cc Paulo Alcantara <[email protected]>
cc Shyam Prasad N <[email protected]>
cc Tom Talpey <[email protected]>
---
 fs/smb/client/cifsglob.h  |  2 ++
 fs/smb/client/connect.c   |  3 +++
 fs/smb/client/transport.c | 38 ++++++++++++++++++++++++++++++++------
 3 files changed, 37 insertions(+), 6 deletions(-)
fs/smb/client/cifsglob.h +2 -0
index f7c12d24e2ea..cd9bf550a144 100644
@@ -662,6 +662,8 @@ struct TCP_Server_Info {
 	/* updates to tcpStatus protected by cifs_tcp_ses_lock */
 	enum statusEnum tcpStatus; /* what we think the status is */
 	char *hostname; /* hostname portion of UNC string */
+	struct page_frag_cache tx_alloc; /* Transmission buffer allocator */
+	struct mutex tx_alloc_lock; /* Lock for ->tx_alloc */
 	struct socket *ssocket;
 	struct sockaddr_storage dstaddr;
 	struct sockaddr_storage srcaddr; /* locally bind to this IP */
fs/smb/client/connect.c +3 -0
index 4a3ebbd6d71b..1d6a18390a25 100644
@@ -1054,6 +1054,8 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
 		 */
 	}
 
+	page_frag_cache_drain(&server->tx_alloc);
+
 	put_net(cifs_net_ns(server));
 	kfree(server->leaf_fullpath);
 	kfree(server->hostname);
@@ -1694,6 +1696,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
 	INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
 	INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
 	mutex_init(&tcp_ses->reconnect_mutex);
+	mutex_init(&tcp_ses->tx_alloc_lock);
 	memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
 	       sizeof(tcp_ses->srcaddr));
 	memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
fs/smb/client/transport.c +32 -6
index e43fce1099a9..98dca2524376 100644
@@ -445,7 +445,8 @@ static size_t smb3_copy_data_iter(void *iter_from, size_t progress, size_t len,
  */
 static int smb_copy_data_into_buffer(struct TCP_Server_Info *server,
 				     int num_rqst, struct smb_rqst *rqst,
-				     struct iov_iter *iter, struct bvecq **_bq)
+				     struct iov_iter *iter, struct bvecq **_bq,
+				     unsigned int flags)
 {
 	struct bvecq *bq;
 	size_t total_len = 0, offset = 0;
@@ -460,9 +461,31 @@ static int smb_copy_data_into_buffer(struct TCP_Server_Info *server,
 		total_len += size;
 	}
 
-	bq = bvecq_alloc_buffer(total_len, GFP_NOFS);
-	if (!bq)
-		return -ENOMEM;
+	if (total_len <= PAGE_SIZE / 2) {
+		/* TODO: Choose algo-based alignment. */
+		unsigned int align = (flags & CIFS_TRANSFORM_REQ) ? 32 : 1;
+		size_t alen = (flags & CIFS_TRANSFORM_REQ) ?
+			round_up(total_len, align) : total_len;
+
+		bq = bvecq_alloc_chain(2, GFP_NOFS);
+		if (!bq)
+			return -ENOMEM;
+		mutex_lock(&server->tx_alloc_lock);
+		void *p = page_frag_alloc_align(&server->tx_alloc, alen,
+						GFP_NOFS, align);
+		mutex_unlock(&server->tx_alloc_lock);
+		if (!p) {
+			bvecq_put(bq);
+			return -ENOMEM;
+		}
+		bvec_set_virt(&bq->bv[1], p, total_len);
+		bq->nr_slots = 2;
+		bq->mem_type = BVECQ_MEM_PAGECACHE;
+	} else {
+		bq = bvecq_alloc_buffer2(total_len, 1, GFP_NOFS);
+		if (!bq)
+			return -ENOMEM;
+	}
 
 	iov_iter_bvec_queue(iter, ITER_DEST, bq, 1, 0, total_len);
 
@@ -495,6 +518,7 @@ static int smb_copy_data_into_buffer(struct TCP_Server_Info *server,
 		      "offset=%zx total_len=%zx\n", offset, total_len)) {
 		goto error;
 	}
+
 	iov_iter_bvec_queue(iter, ITER_DEST, bq, 1, 0, total_len);
 	*_bq = bq;
 	return 0;
@@ -519,7 +543,7 @@ smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
 		return smb_EIO(smb_eio_trace_tx_need_transform);
 	}
 
-	rc = smb_copy_data_into_buffer(server, num_rqst, rqst, &iter, &bq);
+	rc = smb_copy_data_into_buffer(server, num_rqst, rqst, &iter, &bq, flags);
 	if (rc)
 		return rc;
 	content_len = iov_iter_count(&iter);
@@ -537,7 +561,9 @@ smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
 
 		/* TODO: Allocate netmem here */
 		rc = -ENOMEM;
-		hdr_blob = (void *)__get_free_page(GFP_NOFS);
+		mutex_lock(&server->tx_alloc_lock);
+		hdr_blob = page_frag_alloc(&server->tx_alloc, hdr_len, GFP_NOFS);
+		mutex_unlock(&server->tx_alloc_lock);
 		if (!hdr_blob)
 			goto error;
 		bvec_set_virt(&bq->bv[0], hdr_blob, hdr_len);
author maintainer
#20 lore raw

RFC PATCH 19/36 cifs: Try to better handle the "Dynamic" flag in StructureSize2 in SMB2/3

In reply to David Howells

The bottom bit of the size field of the op-specific SMB2/3 struct that immediately follows the smb[23]_hdr struct is a bit weird. Wireshark considers it a "dynamic flag" and it would seem that it needs to be masked off to get the actualy size of the struct - and if there is data following the struct, it is kind of meaningless.

The adjusted value, however, must still be set that way, and if there is nothing following the struct, it seems that the message must be padded out with an extra zero byte so that the message size is no less than the header including that flag in the StructureSize.

Possibly this is a remnant of someone putting an empty data field at the end of the struct as "unsigned char Buffer[1];". But whatever the origin, there are a bunch of places in which the extra 1 needs to be subtracted off.

Since subtracting 1 is the rule rather than the exception, return the size with the bit masked off and add the pad byte if we don't have anything else to add (Read being the main example of this).

This can then be handled more transparently in future.

cc Steve French <[email protected]>
cc Paulo Alcantara <[email protected]>
cc Shyam Prasad N <[email protected]>
cc Tom Talpey <[email protected]>
---
 fs/smb/client/smb2pdu.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
fs/smb/client/smb2pdu.c +16 -12
index ddf127dc27bb..48f56ce73cd3 100644
@@ -543,7 +543,11 @@ fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
 	smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
 	spdu->StructureSize2 = cpu_to_le16(parmsize);
 
-	*total_len = parmsize + sizeof(struct smb2_hdr);
+	if (smb2_command == SMB2_READ)
+		*total_len = sizeof(struct smb2_hdr) + parmsize;
+	else
+		*total_len = sizeof(struct smb2_hdr) +
+			(parmsize & ~SMB2_STRUCT_HAS_DYNAMIC_PART);
 }
 
 /*
@@ -1589,7 +1593,7 @@ SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
 
 	sess_data->iov[0].iov_base = (char *)req;
 	/* 1 for pad */
-	sess_data->iov[0].iov_len = total_len - 1;
+	sess_data->iov[0].iov_len = total_len;
 	/*
 	 * This variable will be used to clear the buffer
 	 * allocated above in case of any error in the calling function.
@@ -2154,7 +2158,7 @@ SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 
 	iov[0].iov_base = (char *)req;
 	/* 1 for pad */
-	iov[0].iov_len = total_len - 1;
+	iov[0].iov_len = total_len;
 
 	/* Testing shows that buffer offset must be at location of Buffer[0] */
 	req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
@@ -2958,7 +2962,7 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
 
 	iov[0].iov_base = (char *)req;
 	/* -1 since last byte is buf[0] which is sent below (path) */
-	iov[0].iov_len = total_len - 1;
+	iov[0].iov_len = total_len;
 
 	req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
 
@@ -3105,7 +3109,7 @@ SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 
 	iov[0].iov_base = (char *)req;
 	/* -1 since last byte is buf[0] which is sent below (path) */
-	iov[0].iov_len = total_len - 1;
+	iov[0].iov_len = total_len;
 
 	if (oparms->create_options & CREATE_OPTION_READONLY)
 		file_attributes |= ATTR_READONLY;
@@ -3427,12 +3431,12 @@ SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 		req->InputOffset =
 		       cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
 		rqst->rq_nvec = 2;
-		iov[0].iov_len = total_len - 1;
+		iov[0].iov_len = total_len;
 		iov[1].iov_base = in_data_buf;
 		iov[1].iov_len = indatalen;
 	} else {
 		rqst->rq_nvec = 1;
-		iov[0].iov_len = total_len;
+		iov[0].iov_len = total_len + 1;
 	}
 
 	req->OutputOffset = 0;
@@ -3874,7 +3878,7 @@ SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 	if (input_len) {
 		req->InputBufferLength = cpu_to_le32(input_len);
 		/* total_len for smb query request never close to le16 max */
-		req->InputBufferOffset = cpu_to_le16(total_len - 1);
+		req->InputBufferOffset = cpu_to_le16(total_len);
 		memcpy(req->Buffer, input, input_len);
 	}
 
@@ -4579,7 +4583,7 @@ smb2_new_read_req(void **buf, unsigned int *total_len,
 		v1 = (struct smbdirect_buffer_descriptor_v1 *) &req->Buffer[0];
 		smbd_mr_fill_buffer_descriptor(rdata->mr, v1);
 
-		*total_len += sizeof(*v1) - 1;
+		*total_len += sizeof(*v1);
 	}
 #endif
 	if (request_type & CHAINED_REQUEST) {
@@ -5339,7 +5343,7 @@ SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
 
 	iov[0].iov_base = (char *)req;
 	/* 1 for Buffer */
-	iov[0].iov_len = total_len - 1;
+	iov[0].iov_len = total_len;
 
 	memset(&rqst, 0, sizeof(struct smb_rqst));
 	rqst.rq_iov = iov;
@@ -5597,7 +5601,7 @@ int SMB2_query_directory_init(const unsigned int xid,
 
 	iov[0].iov_base = (char *)req;
 	/* 1 for Buffer */
-	iov[0].iov_len = total_len - 1;
+	iov[0].iov_len = total_len;
 
 	iov[1].iov_base = (char *)(req->Buffer);
 	iov[1].iov_len = len;
@@ -5810,7 +5814,7 @@ SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 
 	iov[0].iov_base = (char *)req;
 	/* 1 for Buffer */
-	iov[0].iov_len = total_len - 1;
+	iov[0].iov_len = total_len;
 
 	for (i = 1; i < rqst->rq_nvec; i++) {
 		le32_add_cpu(&req->BufferLength, size[i]);
author maintainer
#21 lore raw

RFC PATCH 20/36 cifs: Pass smb_message structs down into the transport layer

In reply to David Howells

Institute the creation of smb_message structs at the layer above the transport layer and pass them down into the transport layer. This replaces the handling of mid_q_structs entirely within the transport layer.

This includes the following changes:

(1) The smb_rqst struct is partially absorbed into the smb_message struct. Because the latter is on a linked list, the fixed-size arrays of requests can be got rid of - along with the COMPOUND_MAX-1 limit that gets imposed by the encryption code because it needs to steal a slot for the transform header.

(2) Hand smb_message structs into the message sending code in the transport rather than allocating them (as it did for mid_q_structs) in the ->setup_request/->setup_async_request functions.

For the moment compound_send_rcv() does the generation of smb_message structs, stringing them together before passing them down.

(3) Protocol Message IDs are allocated at the time of transmission and as the code to do that is called by protocol-specific code, we don't need a function pointer or so many wrapper functions for it.

---
 fs/smb/client/cifsglob.h      |  40 +---
 fs/smb/client/cifsproto.h     | 105 +---------
 fs/smb/client/cifssmb.c       | 155 ++++++++------
 fs/smb/client/compress.c      |  10 +-
 fs/smb/client/compress.h      |   4 +-
 fs/smb/client/connect.c       |  21 +-
 fs/smb/client/smb1misc.c      |   2 -
 fs/smb/client/smb1ops.c       |  25 +--
 fs/smb/client/smb1proto.h     |  17 +-
 fs/smb/client/smb1transport.c | 105 ++++------
 fs/smb/client/smb2misc.c      |  42 ++--
 fs/smb/client/smb2ops.c       |  13 +-
 fs/smb/client/smb2pdu.c       | 230 +++++++++++---------
 fs/smb/client/smb2proto.h     |  29 +--
 fs/smb/client/smb2transport.c | 242 ++++++++++-----------
 fs/smb/client/trace.h         |  26 ++-
 fs/smb/client/transport.c     | 382 ++++++++++++++++++++--------------
 17 files changed, 710 insertions(+), 738 deletions(-)
fs/smb/client/cifsglob.h +9 -31
index cd9bf550a144..c444b7d81c23 100644
@@ -312,18 +312,16 @@ struct cifs_credits;
 
 struct smb_version_operations {
 	int (*send_cancel)(struct cifs_ses *ses, struct TCP_Server_Info *server,
-			   struct smb_rqst *rqst, struct smb_message *smb,
-			   unsigned int xid);
+			   struct smb_message *smb, unsigned int xid);
 	bool (*compare_fids)(struct cifsFileInfo *, struct cifsFileInfo *);
-	/* setup request: allocate mid, sign message */
-	struct smb_message *(*setup_request)(struct cifs_ses *ses,
-					     struct TCP_Server_Info *server,
-					     struct smb_rqst *rqst);
+	/* setup request: set up mid, sign message */
+	int (*setup_request)(struct cifs_ses *ses,
+			     struct TCP_Server_Info *server,
+			     struct smb_message *smb);
 	/* setup async request: allocate mid, sign message */
-	struct smb_message *(*setup_async_request)(struct TCP_Server_Info *server,
-						   struct smb_rqst *rqst);
+	int (*setup_async_request)(struct TCP_Server_Info *server, struct smb_message *smb);
 	/* check response: verify signature, map error */
-	int (*check_receive)(struct smb_message *mid, struct TCP_Server_Info *server,
+	int (*check_receive)(struct smb_message *smb, struct TCP_Server_Info *server,
 			     bool log_error);
 	void (*add_credits)(struct TCP_Server_Info *server,
 			    struct cifs_credits *credits,
@@ -331,7 +329,6 @@ struct smb_version_operations {
 	void (*set_credits)(struct TCP_Server_Info *, const int);
 	int * (*get_credits_field)(struct TCP_Server_Info *, const int);
 	unsigned int (*get_credits)(struct smb_message *smb);
-	__u64 (*get_next_mid)(struct TCP_Server_Info *);
 	void (*revert_current_mid)(struct TCP_Server_Info *server,
 				   const unsigned int val);
 	/* Finish receiving a PDU and decrypt and decompress and parse it. */
@@ -571,10 +568,8 @@ struct smb_version_operations {
 	long (*fallocate)(struct file *, struct cifs_tcon *, int, loff_t,
 			  loff_t);
 	/* init transform (compress/encrypt) request */
-	int (*init_transform_rq)(struct TCP_Server_Info *server,
-				 int num_rqst, const struct smb_rqst *rqst,
-				 struct smb2_transform_hdr *tr_hdr,
-				 struct iov_iter *iter);
+	int (*init_transform_rq)(struct TCP_Server_Info *server, struct smb_message *head_smb,
+				 struct smb2_transform_hdr *tr_hdr, struct iov_iter *iter);
 	enum securityEnum (*select_sectype)(struct TCP_Server_Info *,
 			    enum securityEnum);
 	int (*next_header)(struct TCP_Server_Info *server, char *buf,
@@ -898,23 +893,6 @@ adjust_credits(struct TCP_Server_Info *server, struct cifs_io_subrequest *subreq
 		server->ops->adjust_credits(server, subreq, trace) : 0;
 }
 
-static inline __le64
-get_next_mid64(struct TCP_Server_Info *server)
-{
-	return cpu_to_le64(server->ops->get_next_mid(server));
-}
-
-static inline __le16
-get_next_mid(struct TCP_Server_Info *server)
-{
-	__u16 mid = server->ops->get_next_mid(server);
-	/*
-	 * The value in the SMB header should be little endian for easy
-	 * on-the-wire decoding.
-	 */
-	return cpu_to_le16(mid);
-}
-
 static inline void
 revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
 {
fs/smb/client/cifsproto.h +4 -101

131 lines not on this page · raw message

fs/smb/client/cifssmb.c +88 -67

301 lines not on this page · raw message

fs/smb/client/compress.c +4 -6
index a9496983ac56..36eb8a604603 100644
@@ -277,10 +277,8 @@ bool is_compressible(const struct iov_iter *data)
  *
  * Return false otherwise.
  */
-bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq)
+bool should_compress(const struct cifs_tcon *tcon, const struct smb_message *smb)
 {
-	const struct smb2_hdr *shdr = rq->rq_iov->iov_base;
-
 	if (unlikely(!tcon || !tcon->ses || !tcon->ses->server))
 		return false;
 
@@ -290,8 +288,8 @@ bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq)
 	if (!(tcon->share_flags & SMB2_SHAREFLAG_COMPRESS_DATA))
 		return false;
 
-	if (shdr->Command == SMB2_WRITE) {
-		const struct smb2_write_req *wreq = rq->rq_iov->iov_base;
+	if (smb->command == SMB2_WRITE) {
+		const struct smb2_write_req *wreq = smb->request;
 
 		if (le32_to_cpu(wreq->Length) < SMB_COMPRESS_MIN_LEN)
 			return false;
@@ -299,7 +297,7 @@ bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq)
 		return true;
 	}
 
-	return (shdr->Command == SMB2_READ);
+	return smb->command == SMB2_READ;
 }
 
 /*
fs/smb/client/compress.h +2 -2
index 48812b0e7476..d03f7342b4bd 100644
@@ -32,7 +32,7 @@ typedef int (*compress_send_fn)(struct TCP_Server_Info *, int, struct smb_rqst *
 
 int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
 		 struct bvecq **bq);
-bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq);
+bool should_compress(const struct cifs_tcon *tcon, const struct smb_message *smb);
 bool is_compressible(const struct iov_iter *data);
 
 /*
@@ -67,7 +67,7 @@ int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
 }
 
 static inline bool is_compressible(const struct iov_iter *data) { return false; }
-static inline bool should_compress(void *unused1, void *unused2)
+static inline bool should_compress(const struct cifs_tcon *tcon, const struct smb_message *smb)
 {
 	return false;
 }
fs/smb/client/connect.c +8 -13
index 1d6a18390a25..dd092f0a0515 100644
@@ -364,7 +364,6 @@ cifs_abort_connection(struct TCP_Server_Info *server)
 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
 	spin_lock(&server->mid_queue_lock);
 	list_for_each_entry_safe(smb, nsmb, &server->pending_mid_q, qhead) {
-		smb_get_message(smb);
 		if (smb->mid_state == MID_REQUEST_SUBMITTED)
 			smb->mid_state = MID_RETRY_NEEDED;
 		list_move(&smb->qhead, &retry_list);
@@ -377,7 +376,7 @@ cifs_abort_connection(struct TCP_Server_Info *server)
 	list_for_each_entry_safe(smb, nsmb, &retry_list, qhead) {
 		list_del_init(&smb->qhead);
 		mid_execute_callback(server, smb);
-		release_mid(server, smb);
+		smb_put_message(smb);
 	}
 }
 
@@ -807,9 +806,9 @@ static bool smb_decode_rfc1002(struct TCP_Server_Info *server, u32 rfc1002_hdr)
 		    server->tcpStatus == CifsInNegotiate &&
 		    !server->with_rfc1001 &&
 		    server->rfc1001_sessinit != 0) {
-			int rc, mid_rc;
 			struct smb_message *smb, *nsmb;
 			LIST_HEAD(dispose_list);
+			int rc, mid_rc;
 
 			cifs_dbg(FYI, "RFC 1002 negative session response during SMB Negotiate, retrying with NetBIOS session\n");
 
@@ -822,7 +821,6 @@ static bool smb_decode_rfc1002(struct TCP_Server_Info *server, u32 rfc1002_hdr)
 			 */
 			spin_lock(&server->mid_queue_lock);
 			list_for_each_entry_safe(smb, nsmb, &server->pending_mid_q, qhead) {
-				smb_get_message(smb);
 				list_move(&smb->qhead, &dispose_list);
 				smb->deleted_from_q = true;
 			}
@@ -856,7 +854,7 @@ static bool smb_decode_rfc1002(struct TCP_Server_Info *server, u32 rfc1002_hdr)
 				smb->mid_rc = mid_rc;
 				smb->mid_state = MID_RC;
 				mid_execute_callback(server, smb);
-				release_mid(server, smb);
+				smb_put_message(smb);
 			}
 
 			/*
@@ -910,6 +908,7 @@ dequeue_mid(struct TCP_Server_Info *server, struct smb_message *smb, bool malfor
 		spin_unlock(&server->mid_queue_lock);
 		pr_warn_once("trying to dequeue a deleted mid\n");
 	} else {
+		smb_put_message(smb);
 		list_del_init(&smb->qhead);
 		smb->deleted_from_q = true;
 		spin_unlock(&server->mid_queue_lock);
@@ -1010,15 +1009,12 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
 	}
 
 	if (!list_empty(&server->pending_mid_q)) {
-		struct smb_message *smb;
-		struct list_head *tmp, *tmp2;
+		struct smb_message *smb, *smb2;
 		LIST_HEAD(dispose_list);
 
 		spin_lock(&server->mid_queue_lock);
-		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
-			smb = list_entry(tmp, struct smb_message, qhead);
+		list_for_each_entry_safe(smb, smb2, &server->pending_mid_q, qhead) {
 			cifs_dbg(FYI, "Clearing mid %llu\n", smb->mid);
-			smb_get_message(smb);
 			smb->mid_state = MID_SHUTDOWN;
 			list_move(&smb->qhead, &dispose_list);
 			smb->deleted_from_q = true;
@@ -1026,12 +1022,11 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
 		spin_unlock(&server->mid_queue_lock);
 
 		/* now walk dispose list and issue callbacks */
-		list_for_each_safe(tmp, tmp2, &dispose_list) {
-			smb = list_entry(tmp, struct smb_message, qhead);
+		list_for_each_entry_safe(smb, smb2, &dispose_list, qhead) {
 			cifs_dbg(FYI, "Callback mid %llu\n", smb->mid);
 			list_del_init(&smb->qhead);
 			mid_execute_callback(server, smb);
-			release_mid(server, smb);
+			smb_put_message(smb);
 		}
 		/* 1/8th of sec is more than enough time for them to exit */
 		msleep(125);
fs/smb/client/smb1misc.c +0 -2
index 357850e9691f..a765b18fcab8 100644
@@ -45,8 +45,6 @@ header_assemble(struct smb_hdr *buffer, char smb_command,
 
 			/* Uid is not converted */
 			buffer->Uid = treeCon->ses->Suid;
-			if (treeCon->ses->server)
-				buffer->Mid = get_next_mid(treeCon->ses->server);
 		}
 		if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
 			buffer->Flags2 |= SMBFLG2_DFS;
fs/smb/client/smb1ops.c +10 -15
index 8477440bbcc2..fb3529a79c6b 100644
@@ -139,11 +139,10 @@ void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
  */
 static int
 send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-	       struct smb_rqst *rqst, struct smb_message *smb,
-	       unsigned int xid)
+	       struct smb_message *smb, unsigned int xid)
 {
 	struct iov_iter iter;
-	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
+	struct smb_hdr *in_buf = (struct smb_hdr *)smb->rqst.rq_iov[0].iov_base;
 	struct kvec iov[1];
 	struct smb_rqst crqst = { .rq_iov = iov, .rq_nvec = 1 };
 	int rc = 0;
@@ -189,11 +188,10 @@ send_nt_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
  */
 static int
 send_lock_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-		 struct smb_rqst *rqst, struct smb_message *smb,
-		 unsigned int xid)
+		 struct smb_message *smb, unsigned int xid)
 {
-	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
-	unsigned int in_len = rqst->rq_iov[0].iov_len;
+	struct smb_hdr *in_buf = (struct smb_hdr *)smb->rqst.rq_iov[0].iov_base;
+	unsigned int in_len = smb->rqst.rq_iov[0].iov_len;
 	LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
 	int rc;
 
@@ -204,7 +202,6 @@ send_lock_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
 	 */
 	pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
 	pSMB->Timeout = 0;
-	pSMB->hdr.Mid = get_next_mid(ses->server);
 
 	rc = SendReceive(xid, ses, in_buf, in_len, NULL, NULL, 0);
 	if (rc == -ENOLCK)
@@ -215,12 +212,11 @@ send_lock_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
 }
 
 static int cifs_send_cancel(struct cifs_ses *ses, struct TCP_Server_Info *server,
-			    struct smb_rqst *rqst, struct smb_message *smb,
-			    unsigned int xid)
+			    struct smb_message *smb, unsigned int xid)
 {
 	if (smb->sr_flags & CIFS_WINDOWS_LOCK)
-		return send_lock_cancel(ses, server, rqst, smb, xid);
-	return send_nt_cancel(ses, server, rqst, smb, xid);
+		return send_lock_cancel(ses, server, smb, xid);
+	return send_nt_cancel(ses, server, smb, xid);
 }
 
 static bool
@@ -233,7 +229,7 @@ struct smb_message *
 cifs_find_mid(struct TCP_Server_Info *server, const struct smb_hdr *shdr)
 {
 	struct smb_message *smb;
-	u16 mid = le16_to_cpu(shdr->Mid);
+	u64 mid = le16_to_cpu(shdr->Mid);
 
 	spin_lock(&server->mid_queue_lock);
 	list_for_each_entry(smb, &server->pending_mid_q, qhead) {
@@ -302,7 +298,7 @@ cifs_get_credits(struct smb_message *smb)
  * to somewhat less than 64K-1 although it is hard to imagine
  * so many threads being in the vfs at one time.
  */
-static __u64
+u16
 cifs_get_next_mid(struct TCP_Server_Info *server)
 {
 	__u64 mid = 0;
@@ -1380,7 +1376,6 @@ struct smb_version_operations smb1_operations = {
 	.get_credits_field = cifs_get_credits_field,
 	.get_credits = cifs_get_credits,
 	.wait_mtu_credits = cifs_wait_mtu_credits,
-	.get_next_mid = cifs_get_next_mid,
 	.clear_stats = cifs_clear_stats,
 	.print_stats = cifs_print_stats,
 	.downgrade_oplock = cifs_downgrade_oplock,
fs/smb/client/smb1proto.h +8 -9
index 54a5261fabce..ac0ba1fa2b74 100644
@@ -287,16 +287,18 @@ int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
 /*
  * smb1transport.c
  */
-struct smb_message *cifs_setup_async_request(struct TCP_Server_Info *server,
-					     struct smb_rqst *rqst);
+u16 cifs_get_next_mid(struct TCP_Server_Info *server);
+struct smb_message *cifs_find_mid(struct TCP_Server_Info *server, const struct smb_hdr *shdr);
+bool cifs_is_network_name_deleted(const struct smb_hdr *shdr, struct TCP_Server_Info *server);
+int cifs_setup_async_request(struct TCP_Server_Info *server,
+			     struct smb_message *smb);
 int SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
 		     char *in_buf, unsigned int in_len, int flags);
 int checkSMB(const struct TCP_Server_Info *server, struct cifs_receive *recv);
 int cifs_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
 		       bool log_error);
-struct smb_message *cifs_setup_request(struct cifs_ses *ses,
-				       struct TCP_Server_Info *server,
-				       struct smb_rqst *rqst);
+int cifs_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
+		       struct smb_message *smb);
 int SendReceive2(const unsigned int xid, struct cifs_ses *ses,
 		 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
 		 const int flags, struct kvec *resp_iov);
@@ -304,6 +306,7 @@ int SendReceive(const unsigned int xid, struct cifs_ses *ses,
 		struct smb_hdr *in_buf, unsigned int in_len,
 		struct smb_hdr *out_buf, int *pbytes_returned,
 		const int flags);
+int smb1_receive_pdu(struct TCP_Server_Info *server, unsigned int pdu_len);
 
 #define GETU16(var)  (*((__u16 *)var))	/* BB check for endian issues */
 #define GETU32(var)  (*((__u32 *)var))	/* BB check for endian issues */
@@ -336,10 +339,6 @@ put_bcc(__u16 count, struct smb_hdr *hdr)
 	put_unaligned_le16(count, bc_ptr);
 }
 
-struct smb_message *cifs_find_mid(struct TCP_Server_Info *server, const struct smb_hdr *shdr);
-bool cifs_is_network_name_deleted(const struct smb_hdr *shdr, struct TCP_Server_Info *server);
-int smb1_receive_pdu(struct TCP_Server_Info *server, unsigned int pdu_len);
-
 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
 
 #endif /* _SMB1PROTO_H */
fs/smb/client/smb1transport.c +35 -70

168 lines not on this page · raw message

fs/smb/client/smb2misc.c +22 -20

69 lines not on this page · raw message

fs/smb/client/smb2ops.c +5 -8
index 6125184d9826..93ec64d67d54 100644
@@ -402,13 +402,14 @@ smb2_add_credits_from_hdr(struct smb2_hdr *shdr, struct TCP_Server_Info *server)
 	}
 }
 
-static __u64
-smb2_get_next_mid(struct TCP_Server_Info *server)
+u64
+smb2_get_next_mid(struct TCP_Server_Info *server, unsigned int count)
 {
-	__u64 mid;
+	u64 mid;
 	/* for SMB2 we need the current value */
 	spin_lock(&server->mid_counter_lock);
-	mid = server->current_mid++;
+	mid = server->current_mid;
+	server->current_mid = mid + umax(count, 1);
 	spin_unlock(&server->mid_counter_lock);
 	return mid;
 }
@@ -4559,7 +4560,6 @@ struct smb_version_operations smb20_operations = {
 	.get_credits_field = smb2_get_credits_field,
 	.get_credits = smb2_get_credits,
 	.wait_mtu_credits = cifs_wait_mtu_credits,
-	.get_next_mid = smb2_get_next_mid,
 	.revert_current_mid = smb2_revert_current_mid,
 	.clear_stats = smb2_clear_stats,
 	.print_stats = smb2_print_stats,
@@ -4650,7 +4650,6 @@ struct smb_version_operations smb21_operations = {
 	.get_credits = smb2_get_credits,
 	.wait_mtu_credits = smb2_wait_mtu_credits,
 	.adjust_credits = smb2_adjust_credits,
-	.get_next_mid = smb2_get_next_mid,
 	.revert_current_mid = smb2_revert_current_mid,
 	.receive_pdu = smb2_receive_pdu,
 	.clear_stats = smb2_clear_stats,
@@ -4743,7 +4742,6 @@ struct smb_version_operations smb30_operations = {
 	.get_credits = smb2_get_credits,
 	.wait_mtu_credits = smb2_wait_mtu_credits,
 	.adjust_credits = smb2_adjust_credits,
-	.get_next_mid = smb2_get_next_mid,
 	.revert_current_mid = smb2_revert_current_mid,
 	.receive_pdu = smb2_receive_pdu,
 	.clear_stats = smb2_clear_stats,
@@ -4846,7 +4844,6 @@ struct smb_version_operations smb311_operations = {
 	.get_credits = smb2_get_credits,
 	.wait_mtu_credits = smb2_wait_mtu_credits,
 	.adjust_credits = smb2_adjust_credits,
-	.get_next_mid = smb2_get_next_mid,
 	.revert_current_mid = smb2_revert_current_mid,
 	.receive_pdu = smb2_receive_pdu,
 	.clear_stats = smb2_clear_stats,
fs/smb/client/smb2pdu.c +129 -101

408 lines not on this page · raw message

fs/smb/client/smb2proto.h +17 -12
index 3b4bc946d1bb..bac01d15b94a 100644
@@ -35,16 +35,9 @@ char *smb2_get_data_area_len(struct cifs_receive *recv);
 __le16 *cifs_convert_path_to_utf16(const char *from,
 				   struct cifs_sb_info *cifs_sb);
 
-int smb2_receive_pdu(struct TCP_Server_Info *server, unsigned int pdu_len);
-int smb2_verify_signature(struct smb_rqst *rqst,
-			  struct TCP_Server_Info *server);
-int smb2_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
-		       bool log_error);
-struct smb_message *smb2_setup_request(struct cifs_ses *ses,
-				       struct TCP_Server_Info *server,
-				       struct smb_rqst *rqst);
-struct smb_message *smb2_setup_async_request(struct TCP_Server_Info *server,
-					     struct smb_rqst *rqst);
+int smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
+		       struct smb_message *smb);
+int smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_message *smb);
 struct cifs_tcon *smb2_find_smb_tcon(struct TCP_Server_Info *server,
 				     __u64 ses_id, __u32  tid);
 __le32 smb2_get_lease_state(struct cifsInodeInfo *cinode, unsigned int oplock);
@@ -52,6 +45,10 @@ void smb2_is_valid_oplock_break(struct TCP_Server_Info *server,
 				union smb2_response_hdr *h);
 int smb3_handle_read_data(struct TCP_Server_Info *server,
 			  struct smb_message *smb);
+int smb2_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
+		       bool log_error);
+int smb2_verify_signature(struct smb_message *smb, struct TCP_Server_Info *server);
+int smb2_receive_pdu(struct TCP_Server_Info *server, unsigned int pdu_len);
 struct inode *smb2_create_reparse_inode(struct cifs_open_info_data *data,
 					struct super_block *sb,
 					const unsigned int xid,
@@ -116,7 +113,7 @@ int smb2_push_mandatory_locks(struct cifsFileInfo *cfile);
 void smb2_reconnect_server(struct work_struct *work);
 int smb3_crypto_aead_allocate(struct TCP_Server_Info *server);
 int smb3_init_transform_rq(struct TCP_Server_Info *server,
-			   int num_rqst, const struct smb_rqst *rqst,
+			   struct smb_message *head_smb,
 			   struct smb2_transform_hdr *tr_hdr,
 			   struct iov_iter *iter);
 unsigned long smb_rqst_len(struct TCP_Server_Info *server,
@@ -128,6 +125,7 @@ bool smb2_should_replay(struct cifs_tcon *tcon, int *pretries,
 			int *pcur_sleep);
 void smb2_add_credits_from_hdr(struct smb2_hdr *shdr,
 			       struct TCP_Server_Info *server);
+u64 smb2_get_next_mid(struct TCP_Server_Info *server, unsigned int count);
 struct smb_message *smb2_find_mid(struct TCP_Server_Info *server,
 				  struct smb2_hdr *shdr, bool dequeue);
 #ifdef CONFIG_CIFS_DEBUG2
@@ -274,7 +272,7 @@ void smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
 				  struct kstatfs *kst);
 void smb311_update_preauth_hash(struct cifs_ses *ses,
 				struct TCP_Server_Info *server,
-				struct kvec *iov, int nvec);
+				struct smb_message *smb, bool hash_resp);
 int smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
 			     const char *path, u32 desired_access, u32 class,
 			     u32 type, u32 output_len, struct kvec *rsp,
@@ -288,4 +286,11 @@ int posix_info_sid_size(const void *beg, const void *end);
 int smb2_rename_pending_delete(const char *full_path, struct dentry *dentry,
 			       const unsigned int xid);
 
+/*
+ * SMB2 Worker functions - most of protocol specific implementation details
+ * are contained within these calls.
+ */
+
+/* query path info from the server using SMB311 POSIX extensions*/
+
 #endif			/* _SMB2PROTO_H */
fs/smb/client/smb2transport.c +115 -127

484 lines not on this page · raw message

fs/smb/client/trace.h +25 -1
index 7bd04ebbce5d..03ed4925df08 100644
@@ -21,7 +21,31 @@
  * Specify enums for tracing information.
  */
 #define smb_command_traces \
-	E_(smb_unknown_command,				"unknown-command")
+	EM(smb2_command_trace_negotiate,	"smb2-negotiate") \
+	EM(smb2_command_trace_session_setup,	"smb2-session_setup") \
+	EM(smb2_command_trace_logoff,		"smb2-logoff") \
+	EM(smb2_command_trace_tree_connect,	"smb2-tree_connect") \
+	EM(smb2_command_trace_tree_disconnect,	"smb2-tree_disconnect") \
+	EM(smb2_command_trace_create,		"smb2-create") \
+	EM(smb2_command_trace_close,		"smb2-close") \
+	EM(smb2_command_trace_flush,		"smb2-flush") \
+	EM(smb2_command_trace_read,		"smb2-read") \
+	EM(smb2_command_trace_write,		"smb2-write") \
+	EM(smb2_command_trace_lock,		"smb2-lock") \
+	EM(smb2_command_trace_ioctl,		"smb2-ioctl") \
+	EM(smb2_command_trace_cancel,		"smb2-cancel") \
+	EM(smb2_command_trace_echo,		"smb2-echo") \
+	EM(smb2_command_trace_query_directory,	"smb2-query_directory") \
+	EM(smb2_command_trace_change_notify,	"smb2-change_notify") \
+	EM(smb2_command_trace_query_info,	"smb2-query_info") \
+	EM(smb2_command_trace_set_info,		"smb2-set_info") \
+	EM(smb2_command_trace_oplock_break,	"smb2-oplock_break") \
+	EM(smb2_command_trace_s2c_notification,	"smb2-s2c-notification") \
+	EM(smb1_command_trace_read,		"smb1-read") \
+	EM(smb1_command_trace_write,		"smb1-write") \
+	EM(smb1_command_trace_echo,		"smb1-echo") \
+	EM(smb1_command_trace_unknown,		"smb1-unknown") \
+	E_(smb_command_trace_unknown,		"unknown-command")
 
 #define smb_eio_traces \
 	EM(smb_eio_trace_compress_copy,			"compress_copy") \
fs/smb/client/transport.c +229 -153

632 lines not on this page · raw message

author maintainer
#22 lore raw

RFC PATCH 21/36 cifs: Add a tracepoint to trace the smb_message refcount

In reply to David Howells

Add a tracepoint to trace the smb_message refcount and to log the various different reasons that things that can happen to it.

---
 fs/smb/client/cifsglob.h      |  1 +
 fs/smb/client/cifsproto.h     |  5 ++-
 fs/smb/client/cifssmb.c       |  6 +--
 fs/smb/client/connect.c       | 11 +++--
 fs/smb/client/smb1ops.c       |  2 +-
 fs/smb/client/smb1transport.c | 10 ++---
 fs/smb/client/smb2ops.c       |  2 +-
 fs/smb/client/smb2pdu.c       | 10 ++---
 fs/smb/client/smb2transport.c | 25 ++++++------
 fs/smb/client/trace.h         | 77 ++++++++++++++++++++++++++++++++---
 fs/smb/client/transport.c     | 50 +++++++++++++++++++----
 11 files changed, 151 insertions(+), 48 deletions(-)
fs/smb/client/cifsglob.h +1 -0
index c444b7d81c23..6b90f17ae0fa 100644
@@ -1669,6 +1669,7 @@ struct smb_message {
 	struct cifs_credits	credits;	/* Credit requirements for this message */
 	void			*request;	/* Pointer to request message body */
 	refcount_t		ref;
+	unsigned int		debug_id;	/* Debugging ID for tracing */
 	bool			sensitive;	/* Request contains sensitive data */
 	bool			cancelled;	/* T if cancelled */
 	unsigned int		sr_flags;	/* Flags passed to send_recv() */
fs/smb/client/cifsproto.h +3 -2
index 47f78955796f..72ee5c5fd8b7 100644
@@ -82,8 +82,9 @@ char *cifs_build_path_to_root(struct smb3_fs_context *ctx,
 char *cifs_build_devname(char *nodename, const char *prepath);
 void delete_mid(struct TCP_Server_Info *server, struct smb_message *smb);
 struct smb_message *smb_message_alloc(enum smb_command_trace cmd, gfp_t gfp);
-void smb_get_message(struct smb_message *smb);
-void smb_put_message(struct smb_message *smb);
+void smb_see_message(struct smb_message *smb, enum smb_message_trace trace);
+void smb_get_message(struct smb_message *smb, enum smb_message_trace trace);
+void smb_put_message(struct smb_message *smb, enum smb_message_trace trace);
 void smb_put_messages(struct smb_message *smb);
 void __release_mid(struct TCP_Server_Info *server, struct smb_message *smb);
 void cifs_wake_up_task(struct TCP_Server_Info *server,
fs/smb/client/cifssmb.c +3 -3
index ebd95de2cd91..ca6410e1f39e 100644
@@ -744,7 +744,7 @@ CIFSSMBEcho(struct TCP_Server_Info *server)
 		cifs_dbg(FYI, "Echo request failed: %d\n", rc);
 
 	cifs_small_buf_release(req);
-	smb_put_message(smb);
+	smb_put_message(smb, smb_message_trace_put_end);
 	return rc;
 }
 
@@ -1614,7 +1614,7 @@ cifs_async_readv(struct cifs_io_subrequest *rdata)
 	if (rc == 0)
 		cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
 put_smb:
-	smb_put_message(smb);
+	smb_put_message(smb, smb_message_trace_put_end);
 	return rc;
 }
 
@@ -2022,7 +2022,7 @@ cifs_async_writev(struct cifs_io_subrequest *wdata)
 async_writev_out:
 	cifs_small_buf_release(req);
 out_put:
-	smb_put_message(smb);
+	smb_put_message(smb, smb_message_trace_put_end);
 out:
 	if (rc) {
 		add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
fs/smb/client/connect.c +7 -4
index dd092f0a0515..99b7c3f165cb 100644
@@ -364,6 +364,7 @@ cifs_abort_connection(struct TCP_Server_Info *server)
 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
 	spin_lock(&server->mid_queue_lock);
 	list_for_each_entry_safe(smb, nsmb, &server->pending_mid_q, qhead) {
+		smb_see_message(smb, smb_message_trace_see_abort_conn);
 		if (smb->mid_state == MID_REQUEST_SUBMITTED)
 			smb->mid_state = MID_RETRY_NEEDED;
 		list_move(&smb->qhead, &retry_list);
@@ -376,7 +377,7 @@ cifs_abort_connection(struct TCP_Server_Info *server)
 	list_for_each_entry_safe(smb, nsmb, &retry_list, qhead) {
 		list_del_init(&smb->qhead);
 		mid_execute_callback(server, smb);
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_abort_conn);
 	}
 }
 
@@ -821,6 +822,7 @@ static bool smb_decode_rfc1002(struct TCP_Server_Info *server, u32 rfc1002_hdr)
 			 */
 			spin_lock(&server->mid_queue_lock);
 			list_for_each_entry_safe(smb, nsmb, &server->pending_mid_q, qhead) {
+				smb_see_message(smb, smb_message_trace_see_is_smb_resp);
 				list_move(&smb->qhead, &dispose_list);
 				smb->deleted_from_q = true;
 			}
@@ -854,7 +856,7 @@ static bool smb_decode_rfc1002(struct TCP_Server_Info *server, u32 rfc1002_hdr)
 				smb->mid_rc = mid_rc;
 				smb->mid_state = MID_RC;
 				mid_execute_callback(server, smb);
-				smb_put_message(smb);
+				smb_put_message(smb, smb_message_trace_put_is_smb_resp);
 			}
 
 			/*
@@ -908,7 +910,7 @@ dequeue_mid(struct TCP_Server_Info *server, struct smb_message *smb, bool malfor
 		spin_unlock(&server->mid_queue_lock);
 		pr_warn_once("trying to dequeue a deleted mid\n");
 	} else {
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_dequeue_mid);
 		list_del_init(&smb->qhead);
 		smb->deleted_from_q = true;
 		spin_unlock(&server->mid_queue_lock);
@@ -1015,6 +1017,7 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
 		spin_lock(&server->mid_queue_lock);
 		list_for_each_entry_safe(smb, smb2, &server->pending_mid_q, qhead) {
 			cifs_dbg(FYI, "Clearing mid %llu\n", smb->mid);
+			smb_see_message(smb, smb_message_trace_see_clean_demux);
 			smb->mid_state = MID_SHUTDOWN;
 			list_move(&smb->qhead, &dispose_list);
 			smb->deleted_from_q = true;
@@ -1026,7 +1029,7 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
 			cifs_dbg(FYI, "Callback mid %llu\n", smb->mid);
 			list_del_init(&smb->qhead);
 			mid_execute_callback(server, smb);
-			smb_put_message(smb);
+			smb_put_message(smb, smb_message_trace_put_clean_demux);
 		}
 		/* 1/8th of sec is more than enough time for them to exit */
 		msleep(125);
fs/smb/client/smb1ops.c +1 -1
index fb3529a79c6b..a2a2167c0e22 100644
@@ -236,7 +236,7 @@ cifs_find_mid(struct TCP_Server_Info *server, const struct smb_hdr *shdr)
 		if (smb->mid == mid &&
 		    smb->mid_state == MID_REQUEST_SUBMITTED &&
 		    smb->command == shdr->Command) {
-			smb_get_message(smb);
+			smb_get_message(smb, smb_message_trace_get_find_mid);
 			spin_unlock(&server->mid_queue_lock);
 			return smb;
 		}
fs/smb/client/smb1transport.c +5 -5
index 1bbfa0844e6f..ba887284fadd 100644
@@ -58,7 +58,7 @@ static int allocate_mid(struct cifs_ses *ses, struct smb_message *smb)
 	}
 	spin_unlock(&ses->ses_lock);
 
-	smb_get_message(smb);
+	smb_get_message(smb, smb_message_trace_get_enqueue_sync);
 	spin_lock(&ses->server->mid_queue_lock);
 	list_add_tail(&smb->qhead, &ses->server->pending_mid_q);
 	spin_unlock(&ses->server->mid_queue_lock);
@@ -85,7 +85,7 @@ cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_message *smb
 
 	rc = cifs_sign_rqst(&smb->rqst, server, &smb->sequence_number);
 	if (rc) {
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_end);
 		return rc;
 	}
 
@@ -891,7 +891,7 @@ static void smb1_parse_one_message(struct TCP_Server_Info *server,
 			 __func__, le16_to_cpu(shdr->Mid));
 		rxq->msg_id = 0;
 	} else {
-		rxq->msg_id = 0; /* TODO: smb->debug_id */
+		rxq->msg_id = smb->debug_id;
 	}
 
 	/* No session expiry check. */
@@ -921,7 +921,7 @@ static void smb1_parse_one_message(struct TCP_Server_Info *server,
 			/* Handle multipart trans2-class messages. */
 			rc = smb1_trans2_receive(server, smb, recv, rxq);
 			if (rc == 1) {
-				smb_put_message(smb);
+				smb_put_message(smb, smb_message_trace_put_incomplete);
 				return; /* Multipart, incomplete. */
 			}
 			if (rc < 0)
@@ -995,7 +995,7 @@ static void smb1_parse_one_message(struct TCP_Server_Info *server,
 		dequeue_mid(server, smb, recv->malformed);
 		mid_execute_callback(server, smb);
 
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_delivered);
 	} else if (smb1_is_valid_oplock_break(h, recv->msg_len, server)) {
 		cifs_dbg(FYI, "Received oplock break\n");
 		smb_rxqueue_consume(server, rxq, rxq->pdu_remain);
fs/smb/client/smb2ops.c +1 -1
index 93ec64d67d54..0aaefe901fbd 100644
@@ -439,7 +439,7 @@ smb2_find_mid(struct TCP_Server_Info *server, struct smb2_hdr *shdr, bool dequeu
 		if ((smb->mid == wire_mid) &&
 		    (smb->mid_state == MID_REQUEST_SUBMITTED) &&
 		    (smb->command == shdr->Command)) {
-			smb_get_message(smb);
+			smb_get_message(smb, smb_message_trace_get_find_mid);
 			if (dequeue) {
 				list_del_init(&smb->qhead);
 				smb->deleted_from_q = true;
fs/smb/client/smb2pdu.c +5 -5
index 5919cd99dec6..2a234df2fad5 100644
@@ -4395,7 +4395,7 @@ SMB2_echo(struct TCP_Server_Info *server)
 		cifs_dbg(FYI, "Echo request failed: %d\n", rc);
 
 	cifs_small_buf_release(req);
-	smb_put_message(smb);
+	smb_put_messages(smb);
 	return rc;
 }
 
@@ -4662,8 +4662,8 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 
 			rc = smb2_verify_signature(smb, server);
 			if (rc) {
-				cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
-					      rc);
+				cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d (MSG=%x)\n",
+					 rc, smb->debug_id);
 				rdata->subreq.error = rc;
 				rdata->result = rc;
 
@@ -4887,7 +4887,7 @@ smb2_async_readv(struct cifs_io_subrequest *rdata)
 		__set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags);
 	}
 
-	smb_put_message(smb);
+	smb_put_messages(smb);
 	return rc;
 }
 
@@ -5286,7 +5286,7 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 
 async_writev_out:
 	cifs_small_buf_release(req);
-	smb_put_message(smb);
+	smb_put_messages(smb);
 out:
 	/* if the send error is retryable, let netfs know about it */
 	if (is_replayable_error(rc) &&
fs/smb/client/smb2transport.c +12 -13
index 543b26135085..404bb8b1b3c3 100644
@@ -579,13 +579,12 @@ int smb2_verify_signature(struct smb_message *smb, struct TCP_Server_Info *serve
 	if (rc)
 		return rc;
 
-	if (crypto_memneq(server_response_sig, shdr->Signature,
-			  SMB2_SIGNATURE_SIZE)) {
-		cifs_dbg(VFS, "sign fail cmd 0x%x message id 0x%llx\n",
-			shdr->Command, shdr->MessageId);
-		return -EACCES;
-	} else
+	if (crypto_memneq(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
 		return 0;
+
+	cifs_dbg(VFS, "sign fail cmd 0x%x message id 0x%llx MSG=%x\n",
+		 shdr->Command, shdr->MessageId, smb->debug_id);
+	return -EACCES;
 }
 
 /*
@@ -662,7 +661,7 @@ smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
 
 	smb2_init_mid(smb, server);
 
-	smb_get_message(smb);
+	smb_get_message(smb, smb_message_trace_get_enqueue_sync);
 	spin_lock(&server->mid_queue_lock);
 	list_add_tail(&smb->qhead, &server->pending_mid_q);
 	spin_unlock(&server->mid_queue_lock);
@@ -682,8 +681,8 @@ smb2_check_receive(struct smb_message *smb, struct TCP_Server_Info *server,
 
 		rc = smb2_verify_signature(smb, server);
 		if (rc)
-			cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
-					rc);
+			cifs_server_dbg(VFS, "SMB signature verification returned error = %d (MSG=%x)\n",
+					rc, smb->debug_id);
 	}
 
 	return smb->error;
@@ -1300,7 +1299,7 @@ static void smb2_parse_one_message(struct TCP_Server_Info *server,
 			 __func__, le64_to_cpu(shdr->MessageId));
 		rxq->msg_id = 0;
 	} else {
-		rxq->msg_id = 0; /* TODO: smb->debug_id */
+		rxq->msg_id = smb->debug_id;
 		smb->decrypted = decrypted;
 	}
 
@@ -1335,12 +1334,12 @@ static void smb2_parse_one_message(struct TCP_Server_Info *server,
 				       le64_to_cpu(shdr->MessageId));
 		cifs_dbg(FYI, "Session expired or deleted\n");
 		set_bit(SMB_SERVER_NEED_RECONNECT, &server->flags);
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_session_expired);
 		return;
 	case STATUS_PENDING:
 		smb_rxqueue_consume(server, rxq, rxq->pdu_remain);
 		smb2_status_pending(shdr, server);
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_pending);
 		return;
 	case STATUS_IO_TIMEOUT:
 		int iotimo = atomic_inc_return(&server->num_io_timeout);
@@ -1434,7 +1433,7 @@ static void smb2_parse_one_message(struct TCP_Server_Info *server,
 		dequeue_mid(server, smb, recv->malformed);
 		mid_execute_callback(server, smb);
 
-		smb_put_message(smb);
+		smb_put_message(smb, smb_message_trace_put_delivered);
 	} else if (shdr->Command == cpu_to_le32(SMB2_OPLOCK_BREAK)) {
 		smb2_is_valid_oplock_break(server, h);
 		smb2_add_credits_from_hdr(shdr, server);
fs/smb/client/trace.h +72 -5

116 lines not on this page · raw message

fs/smb/client/transport.c +41 -9

110 lines not on this page · raw message

author maintainer
#23 lore raw

RFC PATCH 22/36 cifs: Trace smb1/2_copy_to_prepped_buffers()

In reply to David Howells

Add a tracepoint to smb1/2_copy_to_prepped_buffers() so that bulk data read (such as a Read op) can be checked.

---
 fs/smb/client/smb1transport.c |  1 +
 fs/smb/client/smb2transport.c |  1 +
 fs/smb/client/trace.h         | 21 +++++++++++++++++++++
 3 files changed, 23 insertions(+)
fs/smb/client/smb1transport.c +1 -0
index ba887284fadd..598579b564ae 100644
@@ -829,6 +829,7 @@ static void smb1_copy_to_prepped_buffers(struct TCP_Server_Info *server,
 		return;
 	}
 
+	trace_smb3_copy_to_buf(smb, &dest, skip, to_copy);
 	if (!rxq->refillable) {
 		size_t got;
 
fs/smb/client/smb2transport.c +1 -0
index 404bb8b1b3c3..73db90ab8a52 100644
@@ -1236,6 +1236,7 @@ static void smb2_copy_to_prepped_buffers(struct TCP_Server_Info *server,
 		return;
 	}
 
+	trace_smb3_copy_to_buf(smb, &dest, skip, to_copy);
 	if (!rxq->refillable) {
 		size_t got;
 
fs/smb/client/trace.h +21 -0
index 2cdbd8249af0..e9abcc5cf83a 100644
@@ -2117,6 +2117,27 @@ TRACE_EVENT(smb3_message,
 		      __entry->ref)
 	    );
 
+TRACE_EVENT(smb3_copy_to_buf,
+	    TP_PROTO(const struct smb_message *smb, const struct iov_iter *dest,
+		     unsigned int msg_offset, unsigned int len),
+	    TP_ARGS(smb, dest, msg_offset, len),
+	    TP_STRUCT__entry(
+		    __field(unsigned int,	smb_message)
+		    __field(unsigned int,	dest_len)
+		    __field(unsigned int,	msg_offset)
+		    __field(unsigned int,	len)
+			     ),
+	    TP_fast_assign(
+		    __entry->smb_message = smb->debug_id;
+		    __entry->dest_len	= iov_iter_count(dest);
+		    __entry->msg_offset	= msg_offset;
+		    __entry->len	= len;
+			   ),
+	    TP_printk("MSG=%08x m=%x sl=%x dl=%x",
+		      __entry->smb_message,
+		      __entry->msg_offset, __entry->len, __entry->dest_len)
+	    );
+
 #undef EM
 #undef E_
 #endif /* _CIFS_TRACE_H */
author maintainer
#24 lore raw

RFC PATCH 23/36 cifs: Clean up mid->callback_data and kill off mid->creator

In reply to David Howells

We shouldn't really be pinning a task_struct on the mid_q_struct. Better to provide a wait queue there and have the waiter wait on it. For synchronous operations, this allows them to remove themselves from it once this is absorbed into smb_message and smb_message is generated higher up the stack. This also gets rid of one of the uses of mid->callback_data.

We can also get rid of the need for callback_data for the Echo command callback by passing a server pointer down into ->callback() from the callers (all of which have it available). This can also be used in the Read and Write callbacks.

We then only need the callback_data for the Read and Write commands - and in both cases, it points to a cifs_io_subrequest struct. So replace callback_data with a specifically typed pointer in a union that other users can be added to as the need arises.

---
 fs/smb/client/cifs_debug.c    |  3 +--
 fs/smb/client/cifsglob.h      | 11 ++++++++---
 fs/smb/client/cifssmb.c       |  7 ++-----
 fs/smb/client/smb1debug.c     |  7 ++++---
 fs/smb/client/smb2ops.c       |  5 +++--
 fs/smb/client/smb2pdu.c       |  9 ++++-----
 fs/smb/client/smb2transport.c |  6 ------
 fs/smb/client/transport.c     |  7 +++----
 8 files changed, 25 insertions(+), 30 deletions(-)
fs/smb/client/cifs_debug.c +1 -2
index 47dadab67440..0e3e790bbdbc 100644
@@ -659,11 +659,10 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
 					   chan_server->conn_id);
 				spin_lock(&chan_server->mid_queue_lock);
 				list_for_each_entry(smb, &chan_server->pending_mid_q, qhead) {
-					seq_printf(m, "\n\t\tState: %d com: %d pid: %d cbdata: %p mid %llu",
+					seq_printf(m, "\n\t\tState: %d com: %d pid: %d mid %llu",
 						   smb->mid_state,
 						   le16_to_cpu(smb->command),
 						   smb->pid,
-						   smb->callback_data,
 						   smb->mid);
 				}
 				spin_unlock(&chan_server->mid_queue_lock);
fs/smb/client/cifsglob.h +8 -3
index 6b90f17ae0fa..3a2f684cede9 100644
@@ -1636,7 +1636,8 @@ static inline void cifs_stats_bytes_read(struct cifs_tcon *tcon,
  * - it will be called by cifsd, with no locks held
  * - the mid will be removed from any lists
  */
-typedef void (*mid_callback_t)(struct TCP_Server_Info *srv, struct smb_message *smb);
+typedef void (*mid_callback_t)(struct TCP_Server_Info *server,
+			       struct smb_message *smb);
 
 /*
  * Definition of an SMB request message to be transmitted.  These may be
@@ -1668,12 +1669,18 @@ struct smb_message {
 	struct smb_message	*next;		/* Next message in compound */
 	struct cifs_credits	credits;	/* Credit requirements for this message */
 	void			*request;	/* Pointer to request message body */
+	wait_queue_head_t	waitq;		/* Wait queue for message events */
 	refcount_t		ref;
 	unsigned int		debug_id;	/* Debugging ID for tracing */
 	bool			sensitive;	/* Request contains sensitive data */
 	bool			cancelled;	/* T if cancelled */
 	unsigned int		sr_flags;	/* Flags passed to send_recv() */
 
+	/* PDU-type specific data */
+	union {
+		struct cifs_io_subrequest *subreq; /* Read/write subrequest */
+	};
+
 	/* Queue state */
 	struct list_head	qhead;		/* mids waiting on reply from this server */
 	__u64			mid;		/* multiplex id */
@@ -1687,8 +1694,6 @@ struct smb_message {
 	unsigned long		when_received;	/* when demux complete (taken off wire) */
 #endif
 	mid_callback_t		callback;	/* call completion callback */
-	void			*callback_data;	/* general purpose pointer for callback */
-	struct task_struct	*creator;
 	int			mid_state;	/* wish this were enum but can not pass to wait_event */
 	int			mid_rc;		/* rc for MID_RC */
 	unsigned int		optype;		/* operation type */
fs/smb/client/cifssmb.c +2 -5
index ca6410e1f39e..e36828359f67 100644
@@ -737,7 +737,6 @@ CIFSSMBEcho(struct TCP_Server_Info *server)
 	smb->request		= req;
 	smb->total_len		= in_len;
 	smb->callback		= cifs_echo_callback;
-	smb->callback_data	= server;
 
 	rc = cifs_call_async(server, smb, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
 	if (rc)
@@ -1428,7 +1427,7 @@ CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
 static void
 cifs_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *rdata = smb->callback_data;
+	struct cifs_io_subrequest *rdata = smb->subreq;
 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct inode *inode = &ictx->inode;
@@ -1595,7 +1594,6 @@ cifs_async_readv(struct cifs_io_subrequest *rdata)
 	smb->request		= req;
 	smb->total_len		= in_len;
 	smb->callback		= cifs_readv_callback;
-	smb->callback_data	= rdata;
 	smb->copy_to_bufs	= true;
 
 	iov_iter_bvec_queue(&smb->response_iter, ITER_DEST,
@@ -1860,7 +1858,7 @@ CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
 static void
 cifs_writev_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *wdata = smb->callback_data;
+	struct cifs_io_subrequest *wdata = smb->subreq;
 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
 	WRITE_RSP *rsp = (WRITE_RSP *)smb->response;
 	struct cifs_credits credits = {
@@ -1975,7 +1973,6 @@ cifs_async_writev(struct cifs_io_subrequest *wdata)
 	smb->total_len		= in_len + 1;
 	smb->total_len		+= wdata->subreq.len;
 	smb->callback		= cifs_writev_callback;
-	smb->callback_data	= wdata;
 
 	iov_iter_bvec_queue(&smb->rqst.rq_iter, ITER_DEST,
 			    wdata->subreq.content.bvecq, wdata->subreq.content.slot,
fs/smb/client/smb1debug.c +4 -3
index 048fe6303bc1..6eb87852bce7 100644
@@ -40,11 +40,12 @@ void cifs_dump_mids(struct TCP_Server_Info *server)
 			.data_offset	= smb->resp_data_offset,
 		};
 
-		cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d Cbdata: %p Mid %llu\n",
+		cifs_dbg(VFS, "State: %d Cmd: %x Pid: %d R=%x[%x] Mid %llu\n",
 			 smb->mid_state,
-			 le16_to_cpu(smb->command),
+			 le32_to_cpu(smb->command),
 			 smb->pid,
-			 smb->callback_data,
+			 smb->subreq ? smb->subreq->rreq->debug_id : 0,
+			 smb->subreq ? smb->subreq->subreq.debug_index : 0,
 			 smb->mid);
 #ifdef CONFIG_CIFS_STATS2
 		cifs_dbg(VFS, "IsLarge: %d buf: %p time rcv: %ld now: %ld\n",
fs/smb/client/smb2ops.c +3 -2
index 0aaefe901fbd..b655c0f626f5 100644
@@ -485,11 +485,12 @@ void smb2_dump_mids(struct TCP_Server_Info *server)
 			.data_offset	= smb->resp_data_offset,
 		};
 
-		cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d Cbdata: %p Mid %llu\n",
+		cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d R=%x[%x] Mid %llu\n",
 			 smb->mid_state,
 			 smb->command,
 			 smb->pid,
-			 smb->callback_data,
+			 smb->subreq ? smb->subreq->rreq->debug_id : 0,
+			 smb->subreq ? smb->subreq->subreq.debug_index : 0,
 			 smb->mid);
 #ifdef CONFIG_CIFS_STATS2
 		cifs_dbg(VFS, "IsLarge: %d buf: %p time rcv: %ld now: %ld\n",
fs/smb/client/smb2pdu.c +4 -5
index 2a234df2fad5..f4ea7256b09d 100644
@@ -4386,7 +4386,6 @@ SMB2_echo(struct TCP_Server_Info *server)
 	smb->request		= req;
 	smb->total_len		= total_len;
 	smb->callback		= smb2_echo_callback;
-	smb->callback_data	= server;
 
 	req->hdr.CreditRequest = cpu_to_le16(1);
 
@@ -4629,7 +4628,7 @@ smb2_new_read_req(void **buf, unsigned int *total_len,
 static void
 smb2_readv_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *rdata = smb->callback_data;
+	struct cifs_io_subrequest *rdata = smb->subreq;
 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
 	struct smb2_hdr *shdr = smb->response;
@@ -4828,7 +4827,7 @@ smb2_async_readv(struct cifs_io_subrequest *rdata)
 	smb->request		= buf;
 	smb->total_len		= total_len;
 	smb->callback		= smb2_readv_callback;
-	smb->callback_data	= rdata;
+	smb->subreq		= rdata;
 	smb->copy_to_bufs	= true;
 
 	iov_iter_bvec_queue(&smb->response_iter, ITER_DEST,
@@ -4981,7 +4980,7 @@ SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
 static void
 smb2_writev_callback(struct TCP_Server_Info *server, struct smb_message *smb)
 {
-	struct cifs_io_subrequest *wdata = smb->callback_data;
+	struct cifs_io_subrequest *wdata = smb->subreq;
 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
 	struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)smb->response;
 	struct cifs_credits credits = {
@@ -5160,7 +5159,7 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 	smb->request		= req;
 	smb->total_len		= total_len;
 	smb->callback		= smb2_writev_callback;
-	smb->callback_data	= wdata;
+	smb->subreq		= wdata;
 
 	iov_iter_bvec_queue(&smb->rqst.rq_iter, ITER_SOURCE,
 			    wdata->subreq.content.bvecq, wdata->subreq.content.slot,
fs/smb/client/smb2transport.c +0 -6
index 73db90ab8a52..14ee09b33f2c 100644
@@ -611,12 +611,6 @@ static void smb2_init_mid(struct smb_message *smb,
 
 	smb->credits_consumed = credits > 0 ? credits : 1;
 
-	/*
-	 * The default is for the mid to be synchronous, so the
-	 * default callback just wakes up the current task.
-	 */
-	smb->creator = get_task_struct(current);
-
 	atomic_inc(&mid_count);
 	trace_smb3_cmd_enter(le32_to_cpu(shdr->Id.SyncId.TreeId),
 			     le64_to_cpu(shdr->SessionId),
fs/smb/client/transport.c +3 -4
index 0a7b2e7922cf..68536de2c1a0 100644
@@ -53,8 +53,8 @@ struct smb_message *smb_message_alloc(enum smb_command_trace cmd, gfp_t gfp)
 		 * callback just wakes up the current task.
 		 */
 		smb->callback		= cifs_wake_up_task;
-		smb->callback_data	= current;
 		smb->mid_state		= MID_REQUEST_ALLOCATED;
+		init_waitqueue_head(&smb->waitq);
 		trace_smb3_message(smb->debug_id, 1, (enum smb_message_trace)cmd);
 	}
 	return smb;
@@ -122,7 +122,7 @@ cifs_wake_up_task(struct TCP_Server_Info *server, struct smb_message *smb)
 	if (smb->mid_state == MID_RESPONSE_RECEIVED)
 		smb->mid_state = MID_RESPONSE_READY;
 	smb_see_message(smb, smb_message_trace_see_wake_up_task);
-	wake_up_process(smb->callback_data);
+	wake_up_all(&smb->waitq);
 }
 
 static void smb_clear_mid(struct TCP_Server_Info *server, struct smb_message *smb)
@@ -199,7 +199,6 @@ static void smb_clear_mid(struct TCP_Server_Info *server, struct smb_message *sm
 		}
 	}
 #endif
-	put_task_struct(smb->creator);
 }
 
 static bool discard_message(struct TCP_Server_Info *server, struct smb_message *smb)
@@ -901,7 +900,7 @@ int wait_for_response(struct TCP_Server_Info *server, struct smb_message *smb)
 	if (smb->sr_flags & CIFS_INTERRUPTIBLE_WAIT)
 		sleep_state = TASK_INTERRUPTIBLE;
 
-	error = wait_event_state(server->response_q,
+	error = wait_event_state(smb->waitq,
 				 smb->mid_state != MID_REQUEST_SUBMITTED &&
 				 smb->mid_state != MID_RESPONSE_RECEIVED,
 				 (sleep_state | TASK_FREEZABLE_UNSAFE));
author maintainer
#25 lore raw

RFC PATCH 24/36 cifs: Add netmem allocation functions

In reply to David Howells

Add (stub) functions for doing netmem allocations. We want to allocate memory from the netmem buffering as that does bulk DMA and IOMMU management.

---
 fs/smb/client/cifsproto.h |  2 ++
 fs/smb/client/transport.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)
fs/smb/client/cifsproto.h +2 -0
index 72ee5c5fd8b7..3067056f8ef9 100644
@@ -508,5 +508,7 @@ int smb_rxqueue_refill(struct TCP_Server_Info *server, struct netfs_rxqueue *rxq
 		       size_t min_size);
 int smb_rxqueue_consume(struct TCP_Server_Info *server, struct netfs_rxqueue *rxq,
 			size_t amount);
+void *cifs_allocate_tx_buf(struct TCP_Server_Info *server, size_t size);
+void cifs_free_tx_buf(void *p);
 
 #endif			/* _CIFSPROTO_H */
fs/smb/client/transport.c +20 -0
index 68536de2c1a0..1599afb9e5cf 100644
@@ -33,6 +33,26 @@
 #include "smbdirect.h"
 #include "compress.h"
 
+/*
+ * Allocate transmission buffers for a socket.  This memory will be allocated
+ * from the netmem buffers.  It comes with a page ref that we need to drop.
+ * The networking layer can pin it by getting its own ref.
+ */
+void *cifs_allocate_tx_buf(struct TCP_Server_Info *server, size_t size)
+{
+	void *p;
+
+	mutex_lock(&server->tx_alloc_lock);
+	p = page_frag_alloc_align(&server->tx_alloc, size, GFP_NOFS, 8);
+	mutex_unlock(&server->tx_alloc_lock);
+	return p;
+}
+
+void cifs_free_tx_buf(void *p)
+{
+	page_frag_free(p);
+}
+
 struct smb_message *smb_message_alloc(enum smb_command_trace cmd, gfp_t gfp)
 {
 	static atomic_t debug_ids;
author maintainer
#26 lore raw

RFC PATCH 25/36 cifs: Add more pieces to smb_message

In reply to David Howells

Add more pieces to the smb_message struct to facilitate future changes.

---
 fs/smb/client/cifsglob.h      |  10 ++
 fs/smb/client/cifsproto.h     |  18 +++
 fs/smb/client/smb2misc.c      |  15 +++
 fs/smb/client/smb2ops.c       |  10 ++
 fs/smb/client/smb2pdu.c       | 202 ++++++++++++++++++++++++++++++++++
 fs/smb/client/smb2proto.h     |   1 +
 fs/smb/client/smb2transport.c | 122 ++++++++++++++------
 fs/smb/client/transport.c     |  89 +++++++++++----
 8 files changed, 413 insertions(+), 54 deletions(-)
fs/smb/client/cifsglob.h +10 -0
index 3a2f684cede9..9aa9eb72ec6b 100644
@@ -1672,6 +1672,7 @@ struct smb_message {
 	wait_queue_head_t	waitq;		/* Wait queue for message events */
 	refcount_t		ref;
 	unsigned int		debug_id;	/* Debugging ID for tracing */
+	bool			new_style;	/* New style request (not using ->rqst) */
 	bool			sensitive;	/* Request contains sensitive data */
 	bool			cancelled;	/* T if cancelled */
 	unsigned int		sr_flags;	/* Flags passed to send_recv() */
@@ -1711,7 +1712,13 @@ struct smb_message {
 	u8			command_trace;	/* enum smb_command_trace - Command trace ID */
 	__le16			command;	/* smb command code */
 	s16			pre_offset;	/* Offset of pre-headers from ->body (negative) */
+	u16			ext_offset;	/* Offset of extensions from ->body */
+	u16			latest_record;	/* Offset of latest context record (or 0) */
+	u16			offset;		/* Running offset during assembly */
+	u16			data_offset;	/* Offset of data in message (maybe in ->body) */
 	unsigned int		total_len;	/* Total length of from hdr_offset onwards */
+	struct iov_iter		data_iter;	/* Data iterator */
+	struct iov_iter		req_iter;	/* Request iterator */
 	/* Response */
 	//u32			response_pdu_len; /* Size of response PDU */
 	void			*response;	/* Protocol part of response */
@@ -1724,6 +1731,9 @@ struct smb_message {
 	struct bvecq		*response_data;	/* Storage for response data (or NULL) */
 	/* Compat with old code */
 	struct smb_rqst		rqst;
+	/* Variable-length request fragment list - must be last! */
+	struct bvecq		bvecq;		/* List of request frags (passed to socket) */
+	struct bio_vec		__bvecq_bv[3];
 };
 
 struct close_cancelled_open {
fs/smb/client/cifsproto.h +18 -0
index 3067056f8ef9..bbb59761fb9f 100644
@@ -86,6 +86,7 @@ void smb_see_message(struct smb_message *smb, enum smb_message_trace trace);
 void smb_get_message(struct smb_message *smb, enum smb_message_trace trace);
 void smb_put_message(struct smb_message *smb, enum smb_message_trace trace);
 void smb_put_messages(struct smb_message *smb);
+void smb_clear_request(struct smb_message *smb);
 void __release_mid(struct TCP_Server_Info *server, struct smb_message *smb);
 void cifs_wake_up_task(struct TCP_Server_Info *server,
 		       struct smb_message *smb);
@@ -93,6 +94,9 @@ char *smb3_fs_context_fullpath(const struct smb3_fs_context *ctx, char dirsep);
 int smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx);
 int cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs);
 bool cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs);
+int smb_send_recv_messages(const unsigned int xid, struct cifs_ses *ses,
+			   struct TCP_Server_Info *server,
+			   struct smb_message *head_smb, const int flags);
 int cifs_call_async(struct TCP_Server_Info *server, struct smb_message *smb,
 		    const int flags, const struct cifs_credits *exist_credits);
 struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses);
@@ -511,4 +515,18 @@ int smb_rxqueue_consume(struct TCP_Server_Info *server, struct netfs_rxqueue *rx
 void *cifs_allocate_tx_buf(struct TCP_Server_Info *server, size_t size);
 void cifs_free_tx_buf(void *p);
 
+/*
+ * Add a segment to a message.  This should be allocated with
+ * cifs_allocate_tx_buf() so that it can be used with MSG_SPLICE_PAGES.
+ */
+static inline void smb_add_segment_to_tx_buf(struct smb_message *smb,
+					     void *buf, size_t size)
+{
+	unsigned int nr = smb->bvecq.nr_slots;
+
+	bvec_set_virt(&smb->bvecq.bv[nr], buf, size);
+	smb->bvecq.nr_slots = nr + 1;
+	smb->total_len += size;
+}
+
 #endif			/* _CIFSPROTO_H */
fs/smb/client/smb2misc.c +15 -0
index d28812107e8f..7c9dc3ede8b8 100644
@@ -9,6 +9,7 @@
  */
 #include <crypto/sha2.h>
 #include <linux/ctype.h>
+#include <linux/iov_iter.h>
 #include "cifsglob.h"
 #include "cifsproto.h"
 #include "smb2proto.h"
@@ -836,6 +837,15 @@ smb2_handle_cancelled_mid(struct smb_message *smb, struct TCP_Server_Info *serve
 	return rc;
 }
 
+static size_t smb_sha512_step(void *iter_base, size_t progress, size_t len,
+			      void *priv, void *priv2)
+{
+	struct sha512_ctx *ctx = priv;
+
+	sha512_update(ctx, iter_base, len);
+	return 0;
+}
+
 /**
  * smb311_update_preauth_hash - update @ses hash from the message
  * @ses:	server session structure
@@ -878,6 +888,11 @@ smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,
 
 	if (hash_resp) {
 		sha512_update(&sha_ctx,  smb->response, smb->resp_len);
+	} else if (smb->new_style) {
+		struct iov_iter tmp = smb->req_iter;
+
+		iterate_and_advance_kernel(&tmp, smb->total_len,
+					   &sha_ctx, NULL, smb_sha512_step);
 	} else {
 		struct kvec *iov = smb->rqst.rq_iov;
 
fs/smb/client/smb2ops.c +10 -0
index b655c0f626f5..f9cefc47d084 100644
@@ -2729,6 +2729,16 @@ smb2_set_replay(struct TCP_Server_Info *server, struct smb_rqst *rqst)
 	shdr->Flags |= SMB2_FLAGS_REPLAY_OPERATION;
 }
 
+void smb2_set_replay_smb(struct TCP_Server_Info *server, struct smb_message *smb)
+{
+	struct smb2_hdr *shdr = smb->request;
+
+	if (server->dialect < SMB30_PROT_ID)
+		return;
+
+	shdr->Flags |= SMB2_FLAGS_REPLAY_OPERATION;
+}
+
 void
 smb2_set_related(struct smb_rqst *rqst)
 {
fs/smb/client/smb2pdu.c +202 -0

208 lines not on this page · raw message

fs/smb/client/smb2proto.h +1 -0
index bac01d15b94a..6571a6a7b147 100644
@@ -121,6 +121,7 @@ unsigned long smb_rqst_len(struct TCP_Server_Info *server,
 void smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst);
 void smb2_set_related(struct smb_rqst *rqst);
 void smb2_set_replay(struct TCP_Server_Info *server, struct smb_rqst *rqst);
+void smb2_set_replay_smb(struct TCP_Server_Info *server, struct smb_message *smb);
 bool smb2_should_replay(struct cifs_tcon *tcon, int *pretries,
 			int *pcur_sleep);
 void smb2_add_credits_from_hdr(struct smb2_hdr *shdr,
fs/smb/client/smb2transport.c +88 -34

170 lines not on this page · raw message

fs/smb/client/transport.c +69 -20

138 lines not on this page · raw message

author maintainer
#27 lore raw

RFC PATCH 26/36 cifs: Convert SMB2 Negotiate Protocol request

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 497 +++++++++++++++++++++++-----------------
 fs/smb/common/smb2pdu.h |  24 +-
 fs/smb/server/smb2pdu.c |  22 +-
 3 files changed, 310 insertions(+), 233 deletions(-)
fs/smb/client/smb2pdu.c +293 -204
index f907ee4faa3a..193dca577fa1 100644
@@ -825,144 +825,203 @@ static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
 
 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
 
-static void
-build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
+static void *cifs_begin_neg_context(struct smb_message *smb,
+				    __le16 context_type)
 {
-	pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
-	pneg_ctxt->DataLength = cpu_to_le16(38);
-	pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
-	pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
-	get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
-	pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
+	struct smb2_neg_context *neg;
+
+	neg = cifs_begin_extension(smb);
+	neg->ContextType	= context_type;
+	neg->Reserved		= 0;
+	return (void *)neg;
 }
 
-static void
-build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
+static void cifs_end_neg_context(struct smb_message *smb, void *p, size_t size)
 {
-	pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
-	pneg_ctxt->DataLength =
-		cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
-			  - sizeof(struct smb2_neg_context));
-	pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
-	pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
-	pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
-	pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
+	struct smb2_neg_context *neg = p;
+
+	neg->DataLength = cpu_to_le16(size - sizeof(*neg));
+	cifs_end_extension(smb, size);
 }
 
-static unsigned int
-build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
+static void build_preauth_ctxt(struct smb_message *smb)
+{
+	struct smb2_preauth_neg_context *preauth;
+
+	preauth = cifs_begin_neg_context(smb, SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
+	preauth->HashAlgorithmCount	= cpu_to_le16(1);
+	preauth->SaltLength		= cpu_to_le16(SMB311_SALT_SIZE);
+	preauth->HashAlgorithms		= SMB2_PREAUTH_INTEGRITY_SHA512;
+	get_random_bytes(preauth->Salt, SMB311_SALT_SIZE);
+	cifs_end_neg_context(smb, preauth, sizeof(*preauth));
+}
+
+static void build_compression_ctxt(struct smb_message *smb)
+{
+	struct smb2_compression_capabilities_context *compr;
+
+	compr = cifs_begin_neg_context(smb, SMB2_COMPRESSION_CAPABILITIES);
+	compr->CompressionAlgorithmCount = cpu_to_le16(3);
+	compr->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
+	compr->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
+	compr->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
+	cifs_end_neg_context(smb, compr, sizeof(*compr));
+}
+
+static size_t smb2_size_signing_ctxt(void)
+{
+	size_t ctxt_len = sizeof(struct smb2_signing_capabilities);
+	unsigned short num_algs = 1; /* number of signing algorithms sent */
+
+	ctxt_len += sizeof(__le16) * num_algs;
+	return ALIGN8(ctxt_len);
+}
+
+static void build_signing_ctxt(struct smb_message *smb)
 {
-	unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
+	struct smb2_signing_capabilities *scap;
 	unsigned short num_algs = 1; /* number of signing algorithms sent */
 
-	pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
+	scap = cifs_begin_neg_context(smb, SMB2_SIGNING_CAPABILITIES);
+	scap->SigningAlgorithmCount = cpu_to_le16(num_algs);
+	scap->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
+	/* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
+
 	/*
 	 * Context Data length must be rounded to multiple of 8 for some servers
 	 */
-	pneg_ctxt->DataLength = cpu_to_le16(ALIGN8(sizeof(struct smb2_signing_capabilities) -
-					    sizeof(struct smb2_neg_context) +
-					    (num_algs * sizeof(u16))));
-	pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
-	pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
-
-	ctxt_len += sizeof(__le16) * num_algs;
-	ctxt_len = ALIGN8(ctxt_len);
-	return ctxt_len;
-	/* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
+	cifs_end_neg_context(smb, scap,
+			     ALIGN8(struct_size(scap, SigningAlgorithms, num_algs)));
 }
 
-static void
-build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
+static void build_encrypt_ctxt(struct smb_message *smb)
 {
-	pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
-	if (require_gcm_256) {
-		pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
-		pneg_ctxt->CipherCount = cpu_to_le16(1);
-		pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
-	} else if (enable_gcm_256) {
-		pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
-		pneg_ctxt->CipherCount = cpu_to_le16(3);
-		pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
-		pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
-		pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
+	struct smb2_encryption_neg_context *ecap;
+	size_t count;
+
+	ecap = cifs_begin_neg_context(smb, SMB2_ENCRYPTION_CAPABILITIES);
+
+	if (READ_ONCE(require_gcm_256)) {
+		ecap->Ciphers[0]  = SMB2_ENCRYPTION_AES256_GCM;
+		count = 1;
+	} else if (READ_ONCE(enable_gcm_256)) {
+		ecap->Ciphers[0]  = SMB2_ENCRYPTION_AES128_GCM;
+		ecap->Ciphers[1]  = SMB2_ENCRYPTION_AES256_GCM;
+		ecap->Ciphers[2]  = SMB2_ENCRYPTION_AES128_CCM;
+		count = 3;
 	} else {
-		pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
-		pneg_ctxt->CipherCount = cpu_to_le16(2);
-		pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
-		pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
+		ecap->Ciphers[0]  = SMB2_ENCRYPTION_AES128_GCM;
+		ecap->Ciphers[1]  = SMB2_ENCRYPTION_AES128_CCM;
+		count = 2;
 	}
+	ecap->CipherCount = cpu_to_le16(count);
+	cifs_end_neg_context(smb, ecap, struct_size(ecap, Ciphers, count));
 }
 
-static unsigned int
-build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
+static size_t smb2_size_netname_ctxt(struct TCP_Server_Info *server)
 {
+	size_t data_len;
+
+#if 0
 	struct nls_table *cp = load_nls_default();
+	const char *hostname;
 
-	pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
+	/* Only include up to first 100 bytes of server name in the NetName
+	 * field.
+	 */
+	cifs_server_lock(pserver);
+	hostname = pserver->hostname;
+	if (hostname && hostname[0])
+		data_len = cifs_size_strtoUTF16(hostname, 100, cp);
+	cifs_server_unlock(pserver);
+#else
+	/* Now, we can't just measure the length of hostname as, unless we hold
+	 * the lock, it may change under us, so allow maximum space for it.
+	 */
+	data_len = 400;
+#endif
+	return ALIGN8(sizeof(struct smb2_neg_context) + data_len);
+}
+
+static void build_netname_ctxt(struct smb_message *smb, const char *hostname)
+{
+	struct smb2_netname_neg_context *name;
+	struct nls_table *cp = load_nls_default();
+	size_t count;
+
+	name = cifs_begin_neg_context(smb, SMB2_NETNAME_NEGOTIATE_CONTEXT_ID);
 
 	/* copy up to max of first 100 bytes of server name to NetName field */
-	pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
-	/* context size is DataLength + minimal smb2_neg_context */
-	return ALIGN8(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context));
+	count = cifs_strtoUTF16(name->NetName, hostname, 100, cp);
+	cifs_end_neg_context(smb, name, struct_size(name, NetName, count));
 }
 
-static void
-build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
+static void build_posix_ctxt(struct smb_message *smb)
 {
-	pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
-	pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
+	struct smb2_posix_neg_context *posix;
+
+	posix = cifs_begin_neg_context(smb, SMB2_POSIX_EXTENSIONS_AVAILABLE);
 	/* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
-	pneg_ctxt->Name[0] = 0x93;
-	pneg_ctxt->Name[1] = 0xAD;
-	pneg_ctxt->Name[2] = 0x25;
-	pneg_ctxt->Name[3] = 0x50;
-	pneg_ctxt->Name[4] = 0x9C;
-	pneg_ctxt->Name[5] = 0xB4;
-	pneg_ctxt->Name[6] = 0x11;
-	pneg_ctxt->Name[7] = 0xE7;
-	pneg_ctxt->Name[8] = 0xB4;
-	pneg_ctxt->Name[9] = 0x23;
-	pneg_ctxt->Name[10] = 0x83;
-	pneg_ctxt->Name[11] = 0xDE;
-	pneg_ctxt->Name[12] = 0x96;
-	pneg_ctxt->Name[13] = 0x8B;
-	pneg_ctxt->Name[14] = 0xCD;
-	pneg_ctxt->Name[15] = 0x7C;
+	posix->Name[0] = 0x93;
+	posix->Name[1] = 0xAD;
+	posix->Name[2] = 0x25;
+	posix->Name[3] = 0x50;
+	posix->Name[4] = 0x9C;
+	posix->Name[5] = 0xB4;
+	posix->Name[6] = 0x11;
+	posix->Name[7] = 0xE7;
+	posix->Name[8] = 0xB4;
+	posix->Name[9] = 0x23;
+	posix->Name[10] = 0x83;
+	posix->Name[11] = 0xDE;
+	posix->Name[12] = 0x96;
+	posix->Name[13] = 0x8B;
+	posix->Name[14] = 0xCD;
+	posix->Name[15] = 0x7C;
+	cifs_end_neg_context(smb, posix, sizeof(*posix));
 }
 
-static void
-assemble_neg_contexts(struct smb2_negotiate_req *req,
-		      struct TCP_Server_Info *server, unsigned int *total_len)
+static size_t smb2_size_neg_contexts(struct TCP_Server_Info *server,
+				     size_t offset)
 {
-	unsigned int ctxt_len, neg_context_count;
 	struct TCP_Server_Info *pserver;
-	char *pneg_ctxt;
-	char *hostname;
-
-	if (*total_len > 200) {
-		/* In case length corrupted don't want to overrun smb buffer */
-		cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
-		return;
-	}
 
 	/*
 	 * round up total_len of fixed part of SMB3 negotiate request to 8
 	 * byte boundary before adding negotiate contexts
 	 */
-	*total_len = ALIGN8(*total_len);
+	offset = ALIGN8(offset);
+	offset += ALIGN8(sizeof(struct smb2_preauth_neg_context));
+	offset += ALIGN8(sizeof(struct smb2_encryption_neg_context));
+
+	/*
+	 * secondary channels don't have the hostname field populated
+	 * use the hostname field in the primary channel instead
+	 */
+	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
+	offset += smb2_size_netname_ctxt(pserver);
+
+	offset += ALIGN8(sizeof(struct smb2_posix_neg_context));
+	if (server->compression.requested)
+		offset += ALIGN8(sizeof(struct smb2_compression_capabilities_context));
+	if (enable_negotiate_signing)
+		offset += smb2_size_signing_ctxt();
+	return offset;
+}
 
-	pneg_ctxt = (*total_len) + (char *)req;
-	req->NegotiateContextOffset = cpu_to_le32(*total_len);
+static void
+assemble_neg_contexts(struct smb_message *smb, struct TCP_Server_Info *server)
+{
+	struct smb2_negotiate_req *req = smb->request;
+	struct TCP_Server_Info *pserver;
+	const char *hostname;
+	unsigned int neg_context_count = 2;
 
-	build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
-	ctxt_len = ALIGN8(sizeof(struct smb2_preauth_neg_context));
-	*total_len += ctxt_len;
-	pneg_ctxt += ctxt_len;
+	cifs_pad_to_8(smb);
+	req->NegotiateContextOffset = cpu_to_le32(smb->offset);
 
-	build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
-	ctxt_len = ALIGN8(sizeof(struct smb2_encryption_neg_context));
-	*total_len += ctxt_len;
-	pneg_ctxt += ctxt_len;
+	build_preauth_ctxt(smb);
+	build_encrypt_ctxt(smb);
 
 	/*
 	 * secondary channels don't have the hostname field populated
@@ -971,47 +1030,35 @@ assemble_neg_contexts(struct smb2_negotiate_req *req,
 	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
 	cifs_server_lock(pserver);
 	hostname = pserver->hostname;
-	if (hostname && (hostname[0] != 0)) {
-		ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
-					      hostname);
-		*total_len += ctxt_len;
-		pneg_ctxt += ctxt_len;
-		neg_context_count = 3;
-	} else
-		neg_context_count = 2;
+	if (hostname && hostname[0]) {
+		build_netname_ctxt(smb, hostname);
+		neg_context_count++;
+	}
 	cifs_server_unlock(pserver);
 
-	build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
-	*total_len += sizeof(struct smb2_posix_neg_context);
-	pneg_ctxt += sizeof(struct smb2_posix_neg_context);
+	build_posix_ctxt(smb);
 	neg_context_count++;
 
 	if (server->compression.requested) {
-		build_compression_ctxt((struct smb2_compression_capabilities_context *)
-				pneg_ctxt);
-		ctxt_len = ALIGN8(sizeof(struct smb2_compression_capabilities_context));
-		*total_len += ctxt_len;
-		pneg_ctxt += ctxt_len;
+		build_compression_ctxt(smb);
 		neg_context_count++;
 	}
 
 	if (enable_negotiate_signing) {
-		ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
-				pneg_ctxt);
-		*total_len += ctxt_len;
-		pneg_ctxt += ctxt_len;
+		build_signing_ctxt(smb);
 		neg_context_count++;
 	}
 
 	/* check for and add transport_capabilities and signing capabilities */
 	req->NegotiateContextCount = cpu_to_le16(neg_context_count);
-
 }
 
 /* If invalid preauth context warn but use what we requested, SHA-512 */
-static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
+static void decode_preauth_context(struct smb2_neg_context *neg)
 {
-	unsigned int len = le16_to_cpu(ctxt->DataLength);
+	struct smb2_preauth_neg_context *ctxt =
+		container_of(neg, struct smb2_preauth_neg_context, neg);
+	unsigned int len = le16_to_cpu(ctxt->neg.DataLength);
 
 	/*
 	 * Caller checked that DataLength remains within SMB boundary. We still
@@ -1031,9 +1078,11 @@ static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
 }
 
 static void decode_compress_ctx(struct TCP_Server_Info *server,
-			 struct smb2_compression_capabilities_context *ctxt)
+				struct smb2_neg_context *neg)
 {
-	unsigned int len = le16_to_cpu(ctxt->DataLength);
+	struct smb2_compression_capabilities_context *ctxt =
+		container_of(neg, struct smb2_compression_capabilities_context, neg);
+	unsigned int len = le16_to_cpu(ctxt->neg.DataLength);
 	__le16 alg;
 
 	server->compression.enabled = false;
@@ -1066,9 +1115,11 @@ static void decode_compress_ctx(struct TCP_Server_Info *server,
 }
 
 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
-			      struct smb2_encryption_neg_context *ctxt)
+			      struct smb2_neg_context *neg)
 {
-	unsigned int len = le16_to_cpu(ctxt->DataLength);
+	struct smb2_encryption_neg_context *ctxt =
+		container_of(neg, struct smb2_encryption_neg_context, neg);
+	unsigned int len = le16_to_cpu(ctxt->neg.DataLength);
 
 	cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
 	/*
@@ -1118,9 +1169,11 @@ static int decode_encrypt_ctx(struct TCP_Server_Info *server,
 }
 
 static void decode_signing_ctx(struct TCP_Server_Info *server,
-			       struct smb2_signing_capabilities *pctxt)
+			       struct smb2_neg_context *neg)
 {
-	unsigned int len = le16_to_cpu(pctxt->DataLength);
+	struct smb2_signing_capabilities *pctxt =
+		container_of(neg, struct smb2_signing_capabilities, neg);
+	unsigned int len = le16_to_cpu(pctxt->neg.DataLength);
 
 	/*
 	 * Caller checked that DataLength remains within SMB boundary. We still
@@ -1147,11 +1200,12 @@ static void decode_signing_ctx(struct TCP_Server_Info *server,
 }
 
 
-static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
-				     struct TCP_Server_Info *server,
-				     unsigned int len_of_smb)
+static int smb311_decode_neg_context(struct smb_message *smb,
+				     struct TCP_Server_Info *server)
 {
+	struct smb2_negotiate_rsp *rsp = smb->response;
 	struct smb2_neg_context *pctx;
+	unsigned int len_of_smb = smb->resp_len;
 	unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
 	unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
 	unsigned int len_of_ctxts, i;
@@ -1184,23 +1238,26 @@ static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
 		if (clen > len_of_ctxts)
 			break;
 
-		if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
-			decode_preauth_context(
-				(struct smb2_preauth_neg_context *)pctx);
-		else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
-			rc = decode_encrypt_ctx(server,
-				(struct smb2_encryption_neg_context *)pctx);
-		else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
-			decode_compress_ctx(server,
-				(struct smb2_compression_capabilities_context *)pctx);
-		else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
+		switch (pctx->ContextType) {
+		case SMB2_PREAUTH_INTEGRITY_CAPABILITIES:
+			decode_preauth_context(pctx);
+			break;
+		case SMB2_ENCRYPTION_CAPABILITIES:
+			rc = decode_encrypt_ctx(server, pctx);
+			break;
+		case SMB2_COMPRESSION_CAPABILITIES:
+			decode_compress_ctx(server, pctx);
+			break;
+		case SMB2_POSIX_EXTENSIONS_AVAILABLE:
 			server->posix_ext_supported = true;
-		else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
-			decode_signing_ctx(server,
-				(struct smb2_signing_capabilities *)pctx);
-		else
+			break;
+		case SMB2_SIGNING_CAPABILITIES:
+			decode_signing_ctx(server, pctx);
+			break;
+		default:
 			cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
 				le16_to_cpu(pctx->ContextType));
+		}
 		if (rc)
 			break;
 
@@ -1284,16 +1341,15 @@ SMB2_negotiate(const unsigned int xid,
 	       struct cifs_ses *ses,
 	       struct TCP_Server_Info *server)
 {
-	struct smb_rqst rqst;
 	struct smb2_negotiate_req *req;
 	struct smb2_negotiate_rsp *rsp;
-	struct kvec iov[1];
-	struct kvec rsp_iov;
+	struct smb_message *smb;
+	const char *vs;
+	size_t estimate, num_dialects;
 	int rc;
-	int resp_buftype;
 	char *security_blob;
 	int flags = CIFS_NEG_OP;
-	unsigned int total_len;
+	enum { DEF, ANY3, SPEC } version;
 
 	cifs_dbg(FYI, "Negotiate protocol\n");
 
@@ -1302,38 +1358,71 @@ SMB2_negotiate(const unsigned int xid,
 		return smb_EIO(smb_eio_trace_null_pointers);
 	}
 
-	rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
-				 (void **) &req, &total_len);
+	rc = smb2_reconnect(SMB2_SESSION_SETUP, NULL, server, false);
 	if (rc)
 		return rc;
 
+	/* Calculate how much space we need for a Negotiate Request message.
+	 * We can't do this exactly as the hostname might change, but we don't
+	 * want to hold the lock across the allocation.
+	 */
+	vs = server->vals->version_string;
+	if (strcmp(vs, SMB3ANY_VERSION_STRING) == 0) {
+		version = ANY3;
+		num_dialects = 3;
+	} else if (strcmp(vs, SMBDEFAULT_VERSION_STRING) == 0) {
+		version = DEF;
+		num_dialects = 4;
+	} else {
+		version = SPEC;
+		num_dialects = 1;
+	}
+
+	estimate = struct_size(req, Dialects, num_dialects);
+
+	if ((server->vals->protocol_id == SMB311_PROT_ID) ||
+	    (version == ANY3) ||
+	    (version == DEF))
+		estimate = smb2_size_neg_contexts(server, estimate);
+
+	smb = smb2_create_request(SMB2_NEGOTIATE, server, NULL,
+				  sizeof(*req), estimate, 0,
+				  SMB2_REQ_HEAD);
+	if (!smb)
+		return -ENOMEM;
+
+	smb->sr_flags = flags;
+
+	/* Fill in the message. */
+	req = smb->request;
 	req->hdr.SessionId = 0;
 
 	memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
 	memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
 
-	if (strcmp(server->vals->version_string,
-		   SMB3ANY_VERSION_STRING) == 0) {
+	switch (version) {
+	case ANY3:
+		req->DialectCount = cpu_to_le16(3);
 		req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
 		req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
 		req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
-		req->DialectCount = cpu_to_le16(3);
-		total_len += 6;
-	} else if (strcmp(server->vals->version_string,
-		   SMBDEFAULT_VERSION_STRING) == 0) {
+		break;
+	case DEF:
+		req->DialectCount = cpu_to_le16(4);
 		req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
 		req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
 		req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
 		req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
-		req->DialectCount = cpu_to_le16(4);
-		total_len += 8;
-	} else {
+		break;
+	case SPEC:
 		/* otherwise send specific dialect */
-		req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
 		req->DialectCount = cpu_to_le16(1);
-		total_len += 2;
+		req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
+		break;
 	}
 
+	smb->offset += num_dialects * sizeof(__le16);
+
 	/* only one of SMB2 signing flags may be set in SMB2 request */
 	if (ses->sign)
 		req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
@@ -1346,42 +1435,46 @@ SMB2_negotiate(const unsigned int xid,
 	req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
 
 	/* ClientGUID must be zero for SMB2.02 dialect */
-	if (server->vals->protocol_id == SMB20_PROT_ID)
+	if (server->vals->protocol_id == SMB20_PROT_ID) {
 		memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
-	else {
+	} else {
 		memcpy(req->ClientGUID, server->client_guid,
 			SMB2_CLIENT_GUID_SIZE);
 		if ((server->vals->protocol_id == SMB311_PROT_ID) ||
-		    (strcmp(server->vals->version_string,
-		     SMB3ANY_VERSION_STRING) == 0) ||
-		    (strcmp(server->vals->version_string,
-		     SMBDEFAULT_VERSION_STRING) == 0))
-			assemble_neg_contexts(req, server, &total_len);
+		    (version == ANY3) ||
+		    (version == DEF))
+			assemble_neg_contexts(smb, server);
 	}
-	iov[0].iov_base = (char *)req;
-	iov[0].iov_len = total_len;
 
-	memset(&rqst, 0, sizeof(struct smb_rqst));
-	rqst.rq_iov = iov;
-	rqst.rq_nvec = 1;
+	smb->data_offset = smb->offset;
+	smb->total_len   = smb->offset;
+	smb->bvecq.bv[0].bv_len = smb->offset;
+
+	iov_iter_bvec_queue(&smb->req_iter, ITER_SOURCE, &smb->bvecq, 0, 0,
+			    smb->data_offset);
+
+	rc = smb_send_recv_messages(xid, ses, server, smb, flags);
+	smb_clear_request(smb);
 
-	rc = cifs_send_recv(xid, ses, server,
-			    &rqst, &resp_buftype, flags, &rsp_iov);
-	cifs_small_buf_release(req);
-	rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
 	/*
 	 * No tcon so can't do
 	 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
 	 */
-	if (rc == -EOPNOTSUPP) {
-		cifs_server_dbg(VFS, "Dialect not supported by server. Consider  specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
-		goto neg_exit;
-	} else if (rc != 0)
+	if (rc != 0) {
+		if (rc == -EOPNOTSUPP)
+			cifs_server_dbg(VFS, "Dialect not supported by server. Consider  specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
 		goto neg_exit;
+	}
+
+	/* ________________________________________
+	 * Decode the response.
+	 */
+	rsp = (struct smb2_negotiate_rsp *)smb->response;
 
 	u16 dialect = le16_to_cpu(rsp->DialectRevision);
-	if (strcmp(server->vals->version_string,
-		   SMB3ANY_VERSION_STRING) == 0) {
+	rc = -EIO;
+	switch (version) {
+	case ANY3:
 		switch (dialect) {
 		case SMB20_PROT_ID:
 			cifs_server_dbg(VFS,
@@ -1398,11 +1491,9 @@ SMB2_negotiate(const unsigned int xid,
 			server->ops = &smb311_operations;
 			server->vals = &smb311_values;
 			break;
-		default:
-			break;
 		}
-	} else if (strcmp(server->vals->version_string,
-			  SMBDEFAULT_VERSION_STRING) == 0) {
+		break;
+	case DEF:
 		switch (dialect) {
 		case SMB20_PROT_ID:
 			cifs_server_dbg(VFS,
@@ -1421,10 +1512,14 @@ SMB2_negotiate(const unsigned int xid,
 		default:
 			break;
 		}
-	} else if (dialect != server->vals->protocol_id) {
-		/* if requested single dialect ensure returned dialect matched */
-		cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
-				dialect);
+		break;
+	default:
+		if (dialect != server->vals->protocol_id) {
+			/* if requested single dialect ensure returned dialect matched */
+			cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
+					dialect);
+			goto neg_exit;
+		}
 		rc = smb_EIO2(smb_eio_trace_neg_unreq_dialect,
 			      dialect, server->vals->protocol_id);
 		goto neg_exit;
@@ -1490,13 +1585,8 @@ SMB2_negotiate(const unsigned int xid,
 	    (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
 		server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
 
-	struct cifs_receive recv = {
-		/* TODO: This should be returned into smb_message */
-		.response = rsp_iov.iov_base,
-		.msg_len = rsp_iov.iov_len,
-	};
+	security_blob = smb->response + smb->resp_data_offset;
 
-	security_blob = smb2_get_data_area_len(&recv);
 	/*
 	 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
 	 * for us will be
@@ -1504,7 +1594,7 @@ SMB2_negotiate(const unsigned int xid,
 	 * but for time being this is our only auth choice so doesn't matter.
 	 * We just found a server which sets blob length to zero expecting raw.
 	 */
-	if (recv.data_len == 0) {
+	if (smb->resp_data_len == 0) {
 		cifs_dbg(FYI, "missing security blob on negprot\n");
 		server->sec_ntlmssp = true;
 	}
@@ -1512,8 +1602,8 @@ SMB2_negotiate(const unsigned int xid,
 	rc = cifs_enable_signing(server, ses->sign);
 	if (rc)
 		goto neg_exit;
-	if (recv.data_len) {
-		rc = decode_negTokenInit(security_blob, recv.data_len, server);
+	if (smb->resp_data_len) {
+		rc = decode_negTokenInit(security_blob, smb->resp_data_len, server);
 		if (rc == 1)
 			rc = 0;
 		else if (rc == 0)
@@ -1522,8 +1612,7 @@ SMB2_negotiate(const unsigned int xid,
 
 	if (server->dialect == SMB311_PROT_ID) {
 		if (rsp->NegotiateContextCount)
-			rc = smb311_decode_neg_context(rsp, server,
-						       rsp_iov.iov_len);
+			rc = smb311_decode_neg_context(smb, server);
 		else
 			cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
 	}
@@ -1531,7 +1620,7 @@ SMB2_negotiate(const unsigned int xid,
 	if (server->cipher_type && !rc)
 		rc = smb3_crypto_aead_allocate(server);
 neg_exit:
-	free_rsp_buf(resp_buftype, rsp);
+	smb_put_messages(smb);
 	return rc;
 }
 
fs/smb/common/smb2pdu.h +6 -18
index 3d69daf8d7ef..d04a343c15c3 100644
@@ -477,9 +477,7 @@ struct smb2_neg_context {
 #define MIN_PREAUTH_CTXT_DATA_LEN 6
 
 struct smb2_preauth_neg_context {
-	__le16	ContextType; /* 1 */
-	__le16	DataLength;
-	__le32	Reserved;
+	struct smb2_neg_context neg;
 	__le16	HashAlgorithmCount; /* 1 */
 	__le16	SaltLength;
 	__le16	HashAlgorithms; /* HashAlgorithms[0] since only one defined */
@@ -495,9 +493,7 @@ struct smb2_preauth_neg_context {
 /* Min encrypt context data is one cipher so 2 bytes + 2 byte count field */
 #define MIN_ENCRYPT_CTXT_DATA_LEN	4
 struct smb2_encryption_neg_context {
-	__le16	ContextType; /* 2 */
-	__le16	DataLength;
-	__le32	Reserved;
+	struct smb2_neg_context neg;
 	/* CipherCount usually 2, but can be 3 when AES256-GCM enabled */
 	__le16	CipherCount; /* AES128-GCM and AES128-CCM by default */
 	__le16	Ciphers[];
@@ -519,9 +515,7 @@ struct smb2_encryption_neg_context {
 #define SMB2_COMPRESSION_CAPABILITIES_FLAG_CHAINED	cpu_to_le32(0x00000001)
 
 struct smb2_compression_capabilities_context {
-	__le16	ContextType; /* 3 */
-	__le16  DataLength;
-	__le32	Reserved;
+	struct smb2_neg_context neg;
 	__le16	CompressionAlgorithmCount;
 	__le16	Padding;
 	__le32	Flags;
@@ -535,9 +529,7 @@ struct smb2_compression_capabilities_context {
  * Its struct simply contains NetName, an array of Unicode characters
  */
 struct smb2_netname_neg_context {
-	__le16	ContextType; /* 5 */
-	__le16	DataLength;
-	__le32	Reserved;
+	struct smb2_neg_context neg;
 	__le16	NetName[]; /* hostname of target converted to UCS-2 */
 } __packed;
 
@@ -591,9 +583,7 @@ struct smb2_rdma_transform_capabilities_context {
 #define SIGNING_ALG_AES_GMAC_LE    cpu_to_le16(2)
 
 struct smb2_signing_capabilities {
-	__le16	ContextType; /* 8 */
-	__le16	DataLength;
-	__le32	Reserved;
+	struct smb2_neg_context neg;
 	__le16	SigningAlgorithmCount;
 	__le16	SigningAlgorithms[];
 	/*  Followed by padding to 8 byte boundary (required by some servers) */
@@ -601,9 +591,7 @@ struct smb2_signing_capabilities {
 
 #define POSIX_CTXT_DATA_LEN	16
 struct smb2_posix_neg_context {
-	__le16	ContextType; /* 0x100 */
-	__le16	DataLength;
-	__le32	Reserved;
+	struct smb2_neg_context neg;
 	__u8	Name[16]; /* POSIX ctxt GUID 93AD25509CB411E7B42383DE968BCD7C */
 } __packed;
 
fs/smb/server/smb2pdu.c +11 -11
index 62d4399a993d..ec60629355f7 100644
@@ -784,10 +784,10 @@ static int smb2_get_dos_mode(struct kstat *stat, int attribute)
 static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
 			       __le16 hash_id)
 {
-	pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
-	pneg_ctxt->DataLength = cpu_to_le16(38);
+	pneg_ctxt->neg.ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
+	pneg_ctxt->neg.DataLength = cpu_to_le16(38);
+	pneg_ctxt->neg.Reserved = cpu_to_le32(0);
 	pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
-	pneg_ctxt->Reserved = cpu_to_le32(0);
 	pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
 	get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
 	pneg_ctxt->HashAlgorithms = hash_id;
@@ -796,9 +796,9 @@ static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
 static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
 			       __le16 cipher_type)
 {
-	pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
-	pneg_ctxt->DataLength = cpu_to_le16(4);
-	pneg_ctxt->Reserved = cpu_to_le32(0);
+	pneg_ctxt->neg.ContextType = SMB2_ENCRYPTION_CAPABILITIES;
+	pneg_ctxt->neg.DataLength = cpu_to_le16(4);
+	pneg_ctxt->neg.Reserved = cpu_to_le32(0);
 	pneg_ctxt->CipherCount = cpu_to_le16(1);
 	pneg_ctxt->Ciphers[0] = cipher_type;
 }
@@ -806,19 +806,19 @@ static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
 static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
 				__le16 sign_algo)
 {
-	pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
-	pneg_ctxt->DataLength =
+	pneg_ctxt->neg.ContextType = SMB2_SIGNING_CAPABILITIES;
+	pneg_ctxt->neg.DataLength =
 		cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
 			- sizeof(struct smb2_neg_context));
-	pneg_ctxt->Reserved = cpu_to_le32(0);
+	pneg_ctxt->neg.Reserved = cpu_to_le32(0);
 	pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
 	pneg_ctxt->SigningAlgorithms[0] = sign_algo;
 }
 
 static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
 {
-	pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
-	pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
+	pneg_ctxt->neg.ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
+	pneg_ctxt->neg.DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
 	/* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
 	pneg_ctxt->Name[0] = 0x93;
 	pneg_ctxt->Name[1] = 0xAD;
author maintainer
#28 lore raw

RFC PATCH 27/36 cifs: Convert SMB2 Session Setup request

In reply to David Howells
---
 fs/smb/client/ntlmssp.h     |   8 +-
 fs/smb/client/sess.c        | 306 ++++++++++++++++++++----------------
 fs/smb/client/smb1session.c |   4 +-
 fs/smb/client/smb2pdu.c     | 180 +++++++++------------
 4 files changed, 253 insertions(+), 245 deletions(-)
fs/smb/client/ntlmssp.h +4 -4
index be0365f08396..58fcaa868fab 100644
@@ -123,7 +123,7 @@ typedef struct _CHALLENGE_MESSAGE {
 	   do not set the version is present flag */
 } __packed CHALLENGE_MESSAGE, *PCHALLENGE_MESSAGE;
 
-typedef struct _AUTHENTICATE_MESSAGE {
+struct ntlmssp_authenticate_message {
 	__u8 Signature[sizeof(NTLMSSP_SIGNATURE)];
 	__le32 MessageType;  /* NtLmsAuthenticate = 3 */
 	SECURITY_BUFFER LmChallengeResponse;
@@ -136,7 +136,7 @@ typedef struct _AUTHENTICATE_MESSAGE {
 	struct	ntlmssp_version Version;
 	/* SECURITY_BUFFER */
 	char UserString[];
-} __packed AUTHENTICATE_MESSAGE, *PAUTHENTICATE_MESSAGE;
+} __packed;
 
 /*
  * Size of the session key (crypto key encrypted with the password
@@ -148,11 +148,11 @@ int build_ntlmssp_negotiate_blob(unsigned char **pbuffer, u16 *buflen,
 				 struct cifs_ses *ses,
 				 struct TCP_Server_Info *server,
 				 const struct nls_table *nls_cp);
-int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer, u16 *buflen,
+int build_ntlmssp_smb3_negotiate_blob(struct smb_message *smb,
 				      struct cifs_ses *ses,
 				      struct TCP_Server_Info *server,
 				      const struct nls_table *nls_cp);
-int build_ntlmssp_auth_blob(unsigned char **pbuffer, u16 *buflen,
+int build_ntlmssp_auth_blob(struct smb_message *smb,
 			    struct cifs_ses *ses,
 			    struct TCP_Server_Info *server,
 			    const struct nls_table *nls_cp);
fs/smb/client/sess.c +173 -133

385 lines not on this page · raw message

fs/smb/client/smb1session.c +1 -3
index 83bfbf0c068e..06c3eb2fcae9 100644
@@ -835,9 +835,7 @@ sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
 	/* Build security blob before we assemble the request */
 	pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
 	smb_buf = (struct smb_hdr *)pSMB;
-	rc = build_ntlmssp_auth_blob(&ntlmsspblob,
-					&blob_len, ses, server,
-					sess_data->nls_cp);
+	rc = build_ntlmssp_auth_blob(smb, ses, server, sess_data->nls_cp);
 	if (rc)
 		goto out_free_ntlmsspblob;
 	sess_data->iov[1].iov_len = blob_len;
fs/smb/client/smb2pdu.c +75 -105

327 lines not on this page · raw message

author maintainer
#29 lore raw

RFC PATCH 28/36 cifs: Convert SMB2 Logoff request

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 36 +++++++++++++-----------------------
 1 file changed, 13 insertions(+), 23 deletions(-)
fs/smb/client/smb2pdu.c +13 -23
index 4dd4f2de39f4..082838e9c9b5 100644
@@ -2298,15 +2298,10 @@ SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
 int
 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
 {
-	struct smb_rqst rqst;
-	struct smb2_logoff_req *req; /* response is also trivial struct */
-	int rc = 0;
 	struct TCP_Server_Info *server;
-	int flags = 0;
-	unsigned int total_len;
-	struct kvec iov[1];
-	struct kvec rsp_iov;
-	int resp_buf_type;
+	struct smb2_logoff_req *req; /* response is also trivial struct */
+	struct smb_message *smb = NULL;
+	int rc = 0, flags = 0;
 
 	cifs_dbg(FYI, "disconnect session %p\n", ses);
 
@@ -2322,10 +2317,11 @@ SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
 	}
 	spin_unlock(&ses->chan_lock);
 
-	rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
-				 (void **) &req, &total_len);
-	if (rc)
-		return rc;
+	smb = smb2_create_request(SMB2_LOGOFF, server, NULL,
+				  sizeof(*req), sizeof(*req), 0, 0);
+	if (!smb)
+		return -ENOMEM;
+	req = smb->request;
 
 	 /* since no tcon, smb2_init can not do this, so do here */
 	req->hdr.SessionId = cpu_to_le64(ses->Suid);
@@ -2335,23 +2331,17 @@ SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
 	else if (server->sign)
 		req->hdr.Flags |= SMB2_FLAGS_SIGNED;
 
-	flags |= CIFS_NO_RSP_BUF;
-
-	iov[0].iov_base = (char *)req;
-	iov[0].iov_len = total_len;
-
-	memset(&rqst, 0, sizeof(struct smb_rqst));
-	rqst.rq_iov = iov;
-	rqst.rq_nvec = 1;
+	iov_iter_bvec_queue(&smb->req_iter, ITER_SOURCE, &smb->bvecq, 0, 0,
+			    smb->data_offset);
 
-	rc = cifs_send_recv(xid, ses, ses->server,
-			    &rqst, &resp_buf_type, flags, &rsp_iov);
-	cifs_small_buf_release(req);
+	smb->sr_flags = flags;
+	rc = smb_send_recv_messages(xid, ses, ses->server, smb, flags);
 	/*
 	 * No tcon so can't do
 	 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
 	 */
 
+	smb_put_messages(smb);
 smb2_session_already_dead:
 	return rc;
 }
author maintainer
#30 lore raw

RFC PATCH 29/36 cifs: Convert SMB2 Tree Connect request

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 76 ++++++++++++++++-------------------------
 1 file changed, 30 insertions(+), 46 deletions(-)
fs/smb/client/smb2pdu.c +30 -46
index 082838e9c9b5..ea4108669e6c 100644
@@ -2365,57 +2365,44 @@ int
 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 	  struct cifs_tcon *tcon, const struct nls_table *cp)
 {
-	struct smb_rqst rqst;
+	struct TCP_Server_Info *server = cifs_pick_channel(ses);
 	struct smb2_tree_connect_req *req;
 	struct smb2_tree_connect_rsp *rsp = NULL;
-	struct kvec iov[2];
-	struct kvec rsp_iov = { NULL, 0 };
-	int rc = 0;
-	int resp_buftype;
-	int unc_path_len;
-	__le16 *unc_path = NULL;
+	struct smb_message *smb = NULL;
+	int unc_path_size;
 	int flags = 0;
-	unsigned int total_len;
-	struct TCP_Server_Info *server = cifs_pick_channel(ses);
+	int rc = 0;
 
 	cifs_dbg(FYI, "TCON\n");
 
 	if (!server || !tree)
 		return smb_EIO(smb_eio_trace_null_pointers);
 
-	unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
-	if (unc_path == NULL)
-		return -ENOMEM;
-
-	unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
-	if (unc_path_len <= 0) {
-		kfree(unc_path);
-		return -EINVAL;
-	}
-	unc_path_len *= 2;
+	unc_path_size = cifs_size_strtoUTF16(tree, INT_MAX, cp);
 
 	/* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
 	tcon->tid = 0;
 	atomic_set(&tcon->num_remote_opens, 0);
-	rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
-				 (void **) &req, &total_len);
-	if (rc) {
-		kfree(unc_path);
-		return rc;
-	}
+
+	smb = smb2_create_request(SMB2_TREE_CONNECT, server, tcon,
+				  sizeof(*req), sizeof(*req) + unc_path_size, 0,
+				  SMB2_REQ_DYNAMIC);
+	if (!smb)
+		return -ENOMEM;
+	req = smb->request;
 
 	if (smb3_encryption_required(tcon))
 		flags |= CIFS_TRANSFORM_REQ;
 
-	iov[0].iov_base = (char *)req;
-	/* 1 for pad */
-	iov[0].iov_len = total_len;
-
 	/* Testing shows that buffer offset must be at location of Buffer[0] */
-	req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
-	req->PathLength = cpu_to_le16(unc_path_len);
-	iov[1].iov_base = unc_path;
-	iov[1].iov_len = unc_path_len;
+	req->PathOffset = cpu_to_le16(smb->ext_offset);
+	req->PathLength = cpu_to_le16(unc_path_size);
+
+	rc = cifs_strtoUTF16(smb->request + smb->ext_offset, tree, strlen(tree), cp);
+	if (rc <= 0) {
+		rc = -EINVAL;
+		goto tcon_exit;
+	}
 
 	/*
 	 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
@@ -2429,22 +2416,21 @@ SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 	    ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
 		req->hdr.Flags |= SMB2_FLAGS_SIGNED;
 
-	memset(&rqst, 0, sizeof(struct smb_rqst));
-	rqst.rq_iov = iov;
-	rqst.rq_nvec = 2;
-
 	/* Need 64 for max size write so ask for more in case not there yet */
 	if (server->credits >= server->max_credits)
 		req->hdr.CreditRequest = cpu_to_le16(0);
 	else
 		req->hdr.CreditRequest = cpu_to_le16(
-			min_t(int, server->max_credits -
-			      server->credits, 64));
+			min_t(int, server->max_credits - server->credits, 64));
 
-	rc = cifs_send_recv(xid, ses, server,
-			    &rqst, &resp_buftype, flags, &rsp_iov);
-	cifs_small_buf_release(req);
-	rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
+	iov_iter_bvec_queue(&smb->req_iter, ITER_SOURCE, &smb->bvecq, 0, 0,
+			    smb->data_offset);
+
+	smb->sr_flags = flags;
+	rc = smb_send_recv_messages(xid, ses, server, smb, flags);
+	smb_clear_request(smb);
+
+	rsp = (struct smb2_tree_connect_rsp *)smb->response;
 	trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
 	if ((rc != 0) || (rsp == NULL)) {
 		cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
@@ -2491,9 +2477,7 @@ SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 		if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
 			server->nosharesock = true;
 tcon_exit:
-
-	free_rsp_buf(resp_buftype, rsp);
-	kfree(unc_path);
+	smb_put_messages(smb);
 	return rc;
 
 tcon_error_exit:
author maintainer
#31 lore raw

RFC PATCH 30/36 cifs: Convert SMB2 Tree Disconnect request

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)
fs/smb/client/smb2pdu.c +14 -22
index ea4108669e6c..f0b39c77f8ae 100644
@@ -2489,16 +2489,12 @@ SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 int
 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
 {
-	struct smb_rqst rqst;
 	struct smb2_tree_disconnect_req *req; /* response is trivial */
-	int rc = 0;
+	struct smb_message *smb = NULL;
 	struct cifs_ses *ses = tcon->ses;
 	struct TCP_Server_Info *server = cifs_pick_channel(ses);
 	int flags = 0;
-	unsigned int total_len;
-	struct kvec iov[1];
-	struct kvec rsp_iov;
-	int resp_buf_type;
+	int rc = 0;
 
 	cifs_dbg(FYI, "Tree Disconnect\n");
 
@@ -2516,33 +2512,29 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
 
 	invalidate_all_cached_dirs(tcon, true);
 
-	rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
-				 (void **) &req,
-				 &total_len);
-	if (rc)
-		return rc;
+	smb = smb2_create_request(SMB2_TREE_DISCONNECT, server, tcon,
+				  sizeof(*req), sizeof(*req), 0,
+				  SMB2_REQ_DYNAMIC);
+	if (!smb)
+		return -ENOMEM;
+	req = smb->request;
 
 	if (smb3_encryption_required(tcon))
 		flags |= CIFS_TRANSFORM_REQ;
 
-	flags |= CIFS_NO_RSP_BUF;
-
-	iov[0].iov_base = (char *)req;
-	iov[0].iov_len = total_len;
-
-	memset(&rqst, 0, sizeof(struct smb_rqst));
-	rqst.rq_iov = iov;
-	rqst.rq_nvec = 1;
+	iov_iter_bvec_queue(&smb->req_iter, ITER_SOURCE, &smb->bvecq, 0, 0,
+			    smb->data_offset);
 
-	rc = cifs_send_recv(xid, ses, server,
-			    &rqst, &resp_buf_type, flags, &rsp_iov);
-	cifs_small_buf_release(req);
+	smb->sr_flags = flags;
+	rc = smb_send_recv_messages(xid, ses, server, smb, flags);
+	smb_clear_request(smb);
 	if (rc) {
 		cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
 		trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
 	}
 	trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
 
+	smb_put_messages(smb);
 	return rc;
 }
 
author maintainer
#32 lore raw

RFC PATCH 31/36 cifs: Convert SMB2 Read request

In reply to David Howells
---
 fs/smb/client/cifsglob.h |   1 -
 fs/smb/client/smb2pdu.c  | 232 +++++++++++++++++++--------------------
 2 files changed, 113 insertions(+), 120 deletions(-)
fs/smb/client/cifsglob.h +0 -1
index 9aa9eb72ec6b..ed7311637d81 100644
@@ -1455,7 +1455,6 @@ struct cifs_io_subrequest {
 	bool				replay;
 	unsigned int			retries;	/* number of retries so far */
 	unsigned int			cur_sleep;	/* time to sleep before replay */
-	struct kvec			iov[2];
 	struct TCP_Server_Info		*server;
 #ifdef CONFIG_CIFS_SMB_DIRECT
 	struct smbdirect_mr_io		*mr;
fs/smb/client/smb2pdu.c +113 -119

370 lines not on this page · raw message

author maintainer
#33 lore raw

RFC PATCH 32/36 cifs: Convert SMB2 Write request

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 69 +++++++++++++++++++++--------------------
 1 file changed, 35 insertions(+), 34 deletions(-)
fs/smb/client/smb2pdu.c +35 -34
index b2140cb1662e..b434aaf15dba 100644
@@ -5338,10 +5338,8 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 	struct smb2_hdr *shdr;
 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
 	struct TCP_Server_Info *server = wdata->server;
-	struct kvec iov[1];
-	unsigned int total_len, xid = wdata->xid;
-	int credit_request;
-	int rc = -EACCES, flags = 0;
+	unsigned int xid = wdata->xid;
+	int rc = -EACCES;
 
 	/*
 	 * in future we may get cifs_io_parms passed in from the caller,
@@ -5357,41 +5355,39 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 		.pid		= wdata->req->pid,
 	};
 
-	smb = smb_message_alloc(smb2_command_trace_write, GFP_NOFS);
+	size_t proto_len = sizeof(*req);
+
+#ifdef CONFIG_CIFS_SMB_DIRECT
+	bool rdma_offload = smb3_use_rdma_offload(&io_parms);
+
+	if (rdma_offload)
+		proto_len += sizeof(struct smbdirect_buffer_descriptor_v1);
+#endif
+
+	smb = smb2_create_request(SMB2_WRITE, server, tcon,
+				  sizeof(*req), proto_len, wdata->subreq.len,
+				  SMB2_REQ_DYNAMIC);
 	if (!smb) {
 		rc = -ENOMEM;
 		goto out;
 	}
+	req = smb->request;
+	shdr = &req->hdr;
 
-	rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
-				 (void **) &req, &total_len);
-	if (rc) {
-		mempool_free(smb, &smb_message_pool);
-		goto out;
-	}
-
-	iov[0].iov_len	= total_len;
-	iov[0].iov_base	= (char *)req;
-	total_len += wdata->subreq.len;
-
-	smb->rqst.rq_iov	= iov;
-	smb->rqst.rq_nvec	= 1;
-	smb->command		= SMB2_WRITE;
+	smb->subreq		= wdata;
+	smb->credits		= wdata->credits;
 	smb->request		= req;
-	smb->total_len		= total_len;
 	smb->callback		= smb2_writev_callback;
 	smb->subreq		= wdata;
 
-	iov_iter_bvec_queue(&smb->rqst.rq_iter, ITER_SOURCE,
+	iov_iter_bvec_queue(&smb->data_iter, ITER_SOURCE,
 			    wdata->subreq.content.bvecq, wdata->subreq.content.slot,
 			    wdata->subreq.content.offset, wdata->subreq.len);
 
 	if (smb3_encryption_required(tcon))
-		flags |= CIFS_TRANSFORM_REQ;
-
-	shdr = (struct smb2_hdr *)req;
-	shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms.pid);
+		smb->sr_flags |= CIFS_TRANSFORM_REQ;
 
+	shdr->Id.SyncId.ProcessId	= cpu_to_le32(io_parms.pid);
 	req->PersistentFileId		= io_parms.persistent_fid;
 	req->VolatileFileId		= io_parms.volatile_fid;
 	req->WriteChannelInfoOffset	= 0;
@@ -5417,7 +5413,7 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 	 * If we want to do a server RDMA read, fill in and append
 	 * smbdirect_buffer_descriptor_v1 to the end of write request
 	 */
-	if (smb3_use_rdma_offload(&io_parms)) {
+	if (rdma_offload) {
 		struct smbdirect_buffer_descriptor_v1 *v1;
 		struct iov_iter iter;
 		bool need_invalidate = server->dialect == SMB30_PROT_ID;
@@ -5467,9 +5463,12 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 		 io_parms.offset, io_parms.length, iov_iter_count(&smb->rqst.rq_iter));
 
 	if (wdata->credits.value > 0) {
-		shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->subreq.len,
-						    SMB2_MAX_BUFFER_SIZE));
-		credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
+		u32 credit_charge = DIV_ROUND_UP(wdata->subreq.len,
+						 SMB2_MAX_BUFFER_SIZE);
+		int credit_request;
+
+		shdr->CreditCharge = cpu_to_le16(credit_charge);
+		credit_request = credit_charge + 8;
 		if (server->credits >= server->max_credits)
 			shdr->CreditRequest = cpu_to_le16(0);
 		else
@@ -5481,15 +5480,18 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 		if (rc)
 			goto async_writev_out;
 
-		flags |= CIFS_HAS_CREDITS;
+		smb->sr_flags |= CIFS_HAS_CREDITS;
 	}
 
 	/* XXX: compression + encryption is unsupported for now */
-	if (((flags & CIFS_TRANSFORM_REQ) != CIFS_TRANSFORM_REQ) &&
+	if (((smb->sr_flags & CIFS_TRANSFORM_REQ) != CIFS_TRANSFORM_REQ) &&
 	    should_compress(tcon, smb))
-		flags |= CIFS_COMPRESS_REQ;
+		smb->sr_flags |= CIFS_COMPRESS_REQ;
 
-	rc = cifs_call_async(server, smb, flags, &wdata->credits);
+	iov_iter_bvec_queue(&smb->req_iter, ITER_SOURCE, &smb->bvecq, 0, 0,
+			    smb->data_offset);
+
+	rc = cifs_call_async(server, smb, smb->sr_flags, &wdata->credits);
 	/* Can't touch wdata if rc == 0 */
 	if (rc) {
 		trace_smb3_write_err(wdata->rreq->debug_id,
@@ -5505,7 +5507,6 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
 	}
 
 async_writev_out:
-	cifs_small_buf_release(req);
 	smb_put_messages(smb);
 out:
 	/* if the send error is retryable, let netfs know about it */
author maintainer
#34 lore raw

RFC PATCH 33/36 cifs: [WIP] Don't copy new-style smb_messages to a set of pages

In reply to David Howells

Currently, smb_send_rqst() copies all the data it is given into a clean set of pages and, if needed, a page fragment so that MSG_SPLICE_PAGES can be passed to sendmsg() to make sendmsg() more efficient.

With the "new style" of smb_messages, however, the protocol data is placed into its own fragment or fragments plus, optionally, a bundle of page fragments supplied from higher up carrying data (e.g. for Write). These can be passed to MSG_SPLICE_PAGES without the need for copying, so don't do that if we don't have to.

Note that we may still need to do this if encrypting or compressing and we can't modify borrowed data in place (e.g. Write), though we *can* encrypt the request in its fragment.

---
 fs/smb/client/cifsencrypt.c   |   4 +-
 fs/smb/client/cifsglob.h      |  16 ++-
 fs/smb/client/cifssmb.c       |  12 +-
 fs/smb/client/compress.c      |  21 ++-
 fs/smb/client/compress.h      |   4 +-
 fs/smb/client/smb1encrypt.c   |   4 +-
 fs/smb/client/smb1transport.c |   8 +-
 fs/smb/client/smb2misc.c      |   4 +-
 fs/smb/client/smb2ops.c       |   2 +-
 fs/smb/client/smb2pdu.c       |  63 +++------
 fs/smb/client/smb2transport.c |  69 ++++++----
 fs/smb/client/transport.c     | 249 ++++++++++++++++++++++------------
 12 files changed, 274 insertions(+), 182 deletions(-)
fs/smb/client/cifsencrypt.c +3 -1
index 34804e9842a8..eb900cecbb37 100644
@@ -87,7 +87,9 @@ int __cifs_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
 	if (rc < 0)
 		return rc;
 
-	rc = cifs_sig_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), ctx);
+	iov_iter_bvec_queue(&iter, ITER_SOURCE, rqst->rq_data, 0, 0, rqst->rq_data_len);
+
+	rc = cifs_sig_iter(&iter, iov_iter_count(&iter), ctx);
 	if (rc < 0)
 		return rc;
 
fs/smb/client/cifsglob.h +10 -6
index ed7311637d81..e5972ac80240 100644
@@ -290,8 +290,8 @@ struct cifs_open_info_data {
 struct smb_rqst {
 	struct kvec	*rq_iov;	/* array of kvecs */
 	unsigned int	rq_nvec;	/* number of kvecs in array */
-	struct iov_iter	rq_iter;	/* Data iterator */
-	struct bvecq	*rq_buffer;	/* Buffer for encryption */
+	unsigned int	rq_data_len;	/* Amount of data in ->rq_data */
+	struct bvecq	*rq_data;	/* Data content */
 };
 
 struct smb_message;
@@ -1706,6 +1706,7 @@ struct smb_message {
 	bool			decrypted:1;	/* decrypted entry */
 	bool			sig_checked:1;	/* T if sig already checked */
 	bool			copy_to_bufs:1;	/* Copy to prepared buffer in response_iter */
+	bool			req_data_nomodify:1; /* T if req_data cannot be modified */
 
 	/* Request details */
 	u8			command_trace;	/* enum smb_command_trace - Command trace ID */
@@ -1716,8 +1717,8 @@ struct smb_message {
 	u16			offset;		/* Running offset during assembly */
 	u16			data_offset;	/* Offset of data in message (maybe in ->body) */
 	unsigned int		total_len;	/* Total length of from hdr_offset onwards */
-	struct iov_iter		data_iter;	/* Data iterator */
-	struct iov_iter		req_iter;	/* Request iterator */
+	unsigned int		req_data_len;	/* Amount of data in ->data_bq */
+	struct bvecq		*req_data;	/* Data content */
 	/* Response */
 	//u32			response_pdu_len; /* Size of response PDU */
 	void			*response;	/* Protocol part of response */
@@ -1726,8 +1727,11 @@ struct smb_message {
 	u32			resp_data_offset; /* Offset of response data (or 0) */
 	__le32			status;		/* Completion status */
 	short			error;		/* Linux error */
-	struct iov_iter		response_iter;	/* Data part of response */
-	struct bvecq		*response_data;	/* Storage for response data (or NULL) */
+	struct iov_iter		resp_extra_iter;
+	struct bvecq		*resp_buffer;	/* Storage for response data */
+	unsigned int		resp_buffer_slot;
+	unsigned int		resp_buffer_offset;
+	unsigned int		resp_buffer_len; /* Capacity of resp->buffer */
 	/* Compat with old code */
 	struct smb_rqst		rqst;
 	/* Variable-length request fragment list - must be last! */
fs/smb/client/cifssmb.c +7 -5
index e36828359f67..80fc4e89f3df 100644
@@ -1595,10 +1595,10 @@ cifs_async_readv(struct cifs_io_subrequest *rdata)
 	smb->total_len		= in_len;
 	smb->callback		= cifs_readv_callback;
 	smb->copy_to_bufs	= true;
-
-	iov_iter_bvec_queue(&smb->response_iter, ITER_DEST,
-			    rdata->subreq.content.bvecq, rdata->subreq.content.slot,
-			    rdata->subreq.content.offset, rdata->subreq.len);
+	smb->resp_buffer	= bvecq_get(rdata->subreq.content.bvecq);
+	smb->resp_buffer_slot	= rdata->subreq.content.slot;
+	smb->resp_buffer_offset	= rdata->subreq.content.offset;
+	smb->resp_buffer_len	= rdata->subreq.len;
 
 	trace_smb3_read_enter(rdata->rreq->debug_id,
 			      rdata->subreq.debug_index,
@@ -1968,7 +1968,9 @@ cifs_async_writev(struct cifs_io_subrequest *wdata)
 
 	smb->rqst.rq_iov	= iov;
 	smb->rqst.rq_nvec	= 1;
-	smb->command		= SMB2_WRITE;
+	smb->req_data		= wdata->subreq.content;
+	smb->req_data_len	= wdata->len;
+	smb->req_data_nomodify	= true;
 	smb->request		= req;
 	smb->total_len		= in_len + 1;
 	smb->total_len		+= wdata->subreq.len;
fs/smb/client/compress.c +9 -12
index 36eb8a604603..67305b1e82a6 100644
@@ -329,9 +329,8 @@ static void *vmap_bvecq(struct iov_iter *iter, pgprot_t prot)
 }
 
 int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
-		 struct bvecq **bq)
+		 struct bvecq **bq, struct smb2_compression_hdr *z_hdr)
 {
-	struct smb2_compression_hdr *z_hdr;
 	struct smb2_write_req *w_hdr;
 	struct smb2_hdr *shdr;
 	struct iov_iter tmp;
@@ -363,9 +362,8 @@ int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
 	 * This is just overprovisioning, as the algorithm will error out if @dst reaches 7/8
 	 * of @slen.
 	 */
-	dlen = lz77_compressed_alloc_size(slen);
-	dlen = sizeof(*z_hdr) + slen;
-	dbq = bvecq_alloc_buffer(dlen, GFP_NOFS);
+	dlen = slen;
+	dbq = bvecq_alloc_buffer2(dlen, 1, GFP_NOFS);
 	if (!dbq) {
 		ret = -ENOMEM;
 		goto err_free;
@@ -378,16 +376,15 @@ int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
 		dst = NULL;
 		goto err_free;
 	}
-	z_hdr = dst;
-	dst  += sizeof(*z_hdr) + hlen;
-	dlen -= sizeof(*z_hdr) + hlen;
+	dst  += hlen;
+	dlen -= hlen;
 
 	ret = lz77_compress(src + hlen, slen - hlen, dst, &dlen);
 	if (ret)
 		goto err_free;
 
-	dlen += sizeof(*z_hdr) + hlen;
-	dst -= hlen;
+	dlen += hlen;
+	dst  -= hlen;
 	memcpy(dst, src, hlen);
 
 	z_hdr->ProtocolId			= SMB2_COMPRESSION_TRANSFORM_ID;
@@ -396,7 +393,7 @@ int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
 	z_hdr->Flags				= SMB2_COMPRESSION_FLAG_NONE;
 	z_hdr->Offset				= cpu_to_le32(hlen);
 
-	vunmap(z_hdr);
+	vunmap(dst);
 	vunmap(src);
 
 	dbq->bv[0] = sbq->bv[0];
@@ -409,7 +406,7 @@ int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
 	return dlen;
 
 err_free:
-	vunmap(z_hdr);
+	vunmap(dst);
 	vunmap(src);
 	bvecq_put(dbq);
 	return ret;
fs/smb/client/compress.h +2 -2
index d03f7342b4bd..ba45f0681ab1 100644
@@ -31,7 +31,7 @@ typedef int (*compress_send_fn)(struct TCP_Server_Info *, int, struct smb_rqst *
 
 
 int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
-		 struct bvecq **bq);
+		 struct bvecq **bq, struct smb2_compression_hdr *z_hdr);
 bool should_compress(const struct cifs_tcon *tcon, const struct smb_message *smb);
 bool is_compressible(const struct iov_iter *data);
 
@@ -61,7 +61,7 @@ static __always_inline int smb_compress_alg_valid(__le16 alg, bool valid_none)
 #else /* !CONFIG_CIFS_COMPRESSION */
 static inline
 int smb_compress(struct TCP_Server_Info *server, struct iov_iter *iter,
-		 struct bvecq **bq)
+		 struct bvecq **bq, struct smb2_compression_hdr *z_hdr)
 {
 	return -EOPNOTSUPP;
 }
fs/smb/client/smb1encrypt.c +3 -1
index c61202ae54c6..a9b5884fd790 100644
@@ -118,7 +118,9 @@ int cifs_verify_signature(struct smb_message *smb,
 	struct smb_rqst rqst = {
 		.rq_iov = &iov,
 		.rq_nvec = 1,
-		.rq_iter = smb->response_iter
+		.rq_data = smb->resp_buffer,
+		.rq_data_len = umin(smb->resp_buffer_len, smb->resp_data_len),
+		
 	};
 	char what_we_think_sig_should_be[20];
 	char server_response_sig[8];
fs/smb/client/smb1transport.c +7 -1
index 598579b564ae..df26eb3cf0e0 100644
@@ -799,10 +799,16 @@ static void smb1_copy_to_prepped_buffers(struct TCP_Server_Info *server,
 					 struct cifs_receive *recv)
 {
 	const union smb1_response_hdr *h = recv->response;
-	struct iov_iter dest = smb->response_iter;
+	struct iov_iter dest;
 	unsigned int to_copy, skip;
 	int rc;
 
+	iov_iter_bvec_queue(&dest, ITER_DEST,
+			    smb->resp_buffer,
+			    smb->resp_buffer_slot,
+			    smb->resp_buffer_offset,
+			    smb->resp_buffer_len);
+
 	switch (h->hdr.Command) {
 	case SMB_COM_READ_ANDX:
 		to_copy = recv->data_len;
fs/smb/client/smb2misc.c +3 -1
index 7c9dc3ede8b8..3f0f1e46bb06 100644
@@ -889,7 +889,9 @@ smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,
 	if (hash_resp) {
 		sha512_update(&sha_ctx,  smb->response, smb->resp_len);
 	} else if (smb->new_style) {
-		struct iov_iter tmp = smb->req_iter;
+		struct iov_iter tmp;
+
+		iov_iter_bvec_queue(&tmp, ITER_SOURCE, &smb->bvecq, 0, 0, smb->total_len);
 
 		iterate_and_advance_kernel(&tmp, smb->total_len,
 					   &sha_ctx, NULL, smb_sha512_step);
fs/smb/client/smb2ops.c +1 -1
index f9cefc47d084..0e212a3e883d 100644
@@ -2769,7 +2769,7 @@ smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
 
 	for (int i = 0; i < rqst->rq_nvec; i++)
 		len += rqst->rq_iov[i].iov_len;
-	len += iov_iter_count(&rqst->rq_iter);
+	len += rqst->rq_data_len;
 
 	/* SMB headers in a compound are 8 byte aligned. */
 	if (IS_ALIGNED(len, 8))
fs/smb/client/smb2pdu.c +22 -41

137 lines not on this page · raw message

fs/smb/client/smb2transport.c +44 -25

130 lines not on this page · raw message

fs/smb/client/transport.c +163 -86

374 lines not on this page · raw message

author maintainer
#35 lore raw

RFC PATCH 34/36 cifs: [WIP] Rearrange Create request subfuncs

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 355 ++++++++++++++++++++--------------------
 1 file changed, 177 insertions(+), 178 deletions(-)
fs/smb/client/smb2pdu.c +177 -178
index 01d84f9ece82..6747886024c4 100644
@@ -1267,60 +1267,6 @@ static int smb311_decode_neg_context(struct smb_message *smb,
 	return rc;
 }
 
-static struct create_posix *
-create_posix_buf(umode_t mode)
-{
-	struct create_posix *buf;
-
-	buf = kzalloc_obj(struct create_posix);
-	if (!buf)
-		return NULL;
-
-	buf->ccontext.DataOffset =
-		cpu_to_le16(offsetof(struct create_posix, Mode));
-	buf->ccontext.DataLength = cpu_to_le32(4);
-	buf->ccontext.NameOffset =
-		cpu_to_le16(offsetof(struct create_posix, Name));
-	buf->ccontext.NameLength = cpu_to_le16(16);
-
-	/* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
-	buf->Name[0] = 0x93;
-	buf->Name[1] = 0xAD;
-	buf->Name[2] = 0x25;
-	buf->Name[3] = 0x50;
-	buf->Name[4] = 0x9C;
-	buf->Name[5] = 0xB4;
-	buf->Name[6] = 0x11;
-	buf->Name[7] = 0xE7;
-	buf->Name[8] = 0xB4;
-	buf->Name[9] = 0x23;
-	buf->Name[10] = 0x83;
-	buf->Name[11] = 0xDE;
-	buf->Name[12] = 0x96;
-	buf->Name[13] = 0x8B;
-	buf->Name[14] = 0xCD;
-	buf->Name[15] = 0x7C;
-	buf->Mode = cpu_to_le32(mode);
-	cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
-	return buf;
-}
-
-static int
-add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
-{
-	unsigned int num = *num_iovec;
-
-	iov[num].iov_base = create_posix_buf(mode);
-	if (mode == ACL_NO_MODE)
-		cifs_dbg(FYI, "%s: no mode\n", __func__);
-	if (iov[num].iov_base == NULL)
-		return -ENOMEM;
-	iov[num].iov_len = sizeof(struct create_posix);
-	*num_iovec = num + 1;
-	return 0;
-}
-
-
 /*
  *
  *	SMB2 Worker functions follow:
@@ -2524,6 +2470,59 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
 	return rc;
 }
 
+static struct create_posix *
+create_posix_buf(umode_t mode)
+{
+	struct create_posix *buf;
+
+	buf = kzalloc_obj(struct create_posix);
+	if (!buf)
+		return NULL;
+
+	buf->ccontext.DataOffset =
+		cpu_to_le16(offsetof(struct create_posix, Mode));
+	buf->ccontext.DataLength = cpu_to_le32(4);
+	buf->ccontext.NameOffset =
+		cpu_to_le16(offsetof(struct create_posix, Name));
+	buf->ccontext.NameLength = cpu_to_le16(16);
+
+	/* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
+	buf->Name[0] = 0x93;
+	buf->Name[1] = 0xAD;
+	buf->Name[2] = 0x25;
+	buf->Name[3] = 0x50;
+	buf->Name[4] = 0x9C;
+	buf->Name[5] = 0xB4;
+	buf->Name[6] = 0x11;
+	buf->Name[7] = 0xE7;
+	buf->Name[8] = 0xB4;
+	buf->Name[9] = 0x23;
+	buf->Name[10] = 0x83;
+	buf->Name[11] = 0xDE;
+	buf->Name[12] = 0x96;
+	buf->Name[13] = 0x8B;
+	buf->Name[14] = 0xCD;
+	buf->Name[15] = 0x7C;
+	buf->Mode = cpu_to_le32(mode);
+	cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
+	return buf;
+}
+
+static int
+add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
+{
+	unsigned int num = *num_iovec;
+
+	iov[num].iov_base = create_posix_buf(mode);
+	if (mode == ACL_NO_MODE)
+		cifs_dbg(FYI, "%s: no mode\n", __func__);
+	if (iov[num].iov_base == NULL)
+		return -ENOMEM;
+	iov[num].iov_len = sizeof(struct create_posix);
+	*num_iovec = num + 1;
+	return 0;
+}
+
 static create_durable_req_t *
 create_durable_buf(void)
 {
@@ -2572,130 +2571,6 @@ create_reconnect_durable_buf(struct cifs_fid *fid)
 	return buf;
 }
 
-static void
-parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
-{
-	struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
-
-	cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
-		pdisk_id->DiskFileId, pdisk_id->VolumeId);
-	buf->IndexNumber = pdisk_id->DiskFileId;
-}
-
-static void
-parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
-		 struct create_posix_rsp *posix)
-{
-	int sid_len;
-	u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
-	u8 *end = beg + le32_to_cpu(cc->DataLength);
-	u8 *sid;
-
-	memset(posix, 0, sizeof(*posix));
-
-	posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
-	posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
-	posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
-
-	sid = beg + 12;
-	sid_len = posix_info_sid_size(sid, end);
-	if (sid_len < 0) {
-		cifs_dbg(VFS, "bad owner sid in posix create response\n");
-		return;
-	}
-	memcpy(&posix->owner, sid, sid_len);
-
-	sid = sid + sid_len;
-	sid_len = posix_info_sid_size(sid, end);
-	if (sid_len < 0) {
-		cifs_dbg(VFS, "bad group sid in posix create response\n");
-		return;
-	}
-	memcpy(&posix->group, sid, sid_len);
-
-	cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
-		 posix->nlink, posix->mode, posix->reparse_tag);
-}
-
-int smb2_parse_contexts(struct TCP_Server_Info *server,
-			struct kvec *rsp_iov,
-			__u16 *epoch,
-			char *lease_key, __u8 *oplock,
-			struct smb2_file_all_info *buf,
-			struct create_posix_rsp *posix)
-{
-	struct smb2_create_rsp *rsp = rsp_iov->iov_base;
-	struct create_context *cc;
-	size_t rem, off, len;
-	size_t doff, dlen;
-	size_t noff, nlen;
-	char *name;
-	static const char smb3_create_tag_posix[] = {
-		0x93, 0xAD, 0x25, 0x50, 0x9C,
-		0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
-		0xDE, 0x96, 0x8B, 0xCD, 0x7C
-	};
-
-	*oplock = 0;
-
-	off = le32_to_cpu(rsp->CreateContextsOffset);
-	rem = le32_to_cpu(rsp->CreateContextsLength);
-	if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
-		return -EINVAL;
-	cc = (struct create_context *)((u8 *)rsp + off);
-
-	/* Initialize inode number to 0 in case no valid data in qfid context */
-	if (buf)
-		buf->IndexNumber = 0;
-
-	while (rem >= sizeof(*cc)) {
-		doff = le16_to_cpu(cc->DataOffset);
-		dlen = le32_to_cpu(cc->DataLength);
-		if (check_add_overflow(doff, dlen, &len) || len > rem)
-			return -EINVAL;
-
-		noff = le16_to_cpu(cc->NameOffset);
-		nlen = le16_to_cpu(cc->NameLength);
-		if (noff + nlen > doff)
-			return -EINVAL;
-
-		name = (char *)cc + noff;
-		switch (nlen) {
-		case 4:
-			if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
-				*oplock = server->ops->parse_lease_buf(cc, epoch,
-								       lease_key);
-			} else if (buf &&
-				   !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
-				parse_query_id_ctxt(cc, buf);
-			}
-			break;
-		case 16:
-			if (posix && !memcmp(name, smb3_create_tag_posix, 16))
-				parse_posix_ctxt(cc, buf, posix);
-			break;
-		default:
-			cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
-				 __func__, nlen, dlen);
-			if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
-				cifs_dump_mem("context data: ", cc, dlen);
-			break;
-		}
-
-		off = le32_to_cpu(cc->Next);
-		if (!off)
-			break;
-		if (check_sub_overflow(rem, off, &rem))
-			return -EINVAL;
-		cc = (struct create_context *)((u8 *)cc + off);
-	}
-
-	if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
-		*oplock = rsp->OplockLevel;
-
-	return 0;
-}
-
 static int
 add_lease_context(struct TCP_Server_Info *server,
 		  struct smb2_create_req *req,
@@ -3466,6 +3341,130 @@ SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 	return 0;
 }
 
+static void
+parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
+{
+	struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
+
+	cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
+		pdisk_id->DiskFileId, pdisk_id->VolumeId);
+	buf->IndexNumber = pdisk_id->DiskFileId;
+}
+
+static void
+parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
+		 struct create_posix_rsp *posix)
+{
+	int sid_len;
+	u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
+	u8 *end = beg + le32_to_cpu(cc->DataLength);
+	u8 *sid;
+
+	memset(posix, 0, sizeof(*posix));
+
+	posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
+	posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
+	posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
+
+	sid = beg + 12;
+	sid_len = posix_info_sid_size(sid, end);
+	if (sid_len < 0) {
+		cifs_dbg(VFS, "bad owner sid in posix create response\n");
+		return;
+	}
+	memcpy(&posix->owner, sid, sid_len);
+
+	sid = sid + sid_len;
+	sid_len = posix_info_sid_size(sid, end);
+	if (sid_len < 0) {
+		cifs_dbg(VFS, "bad group sid in posix create response\n");
+		return;
+	}
+	memcpy(&posix->group, sid, sid_len);
+
+	cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
+		 posix->nlink, posix->mode, posix->reparse_tag);
+}
+
+int smb2_parse_contexts(struct TCP_Server_Info *server,
+			struct kvec *rsp_iov,
+			__u16 *epoch,
+			char *lease_key, __u8 *oplock,
+			struct smb2_file_all_info *buf,
+			struct create_posix_rsp *posix)
+{
+	struct smb2_create_rsp *rsp = rsp_iov->iov_base;
+	struct create_context *cc;
+	size_t rem, off, len;
+	size_t doff, dlen;
+	size_t noff, nlen;
+	char *name;
+	static const char smb3_create_tag_posix[] = {
+		0x93, 0xAD, 0x25, 0x50, 0x9C,
+		0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
+		0xDE, 0x96, 0x8B, 0xCD, 0x7C
+	};
+
+	*oplock = 0;
+
+	off = le32_to_cpu(rsp->CreateContextsOffset);
+	rem = le32_to_cpu(rsp->CreateContextsLength);
+	if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
+		return -EINVAL;
+	cc = (struct create_context *)((u8 *)rsp + off);
+
+	/* Initialize inode number to 0 in case no valid data in qfid context */
+	if (buf)
+		buf->IndexNumber = 0;
+
+	while (rem >= sizeof(*cc)) {
+		doff = le16_to_cpu(cc->DataOffset);
+		dlen = le32_to_cpu(cc->DataLength);
+		if (check_add_overflow(doff, dlen, &len) || len > rem)
+			return -EINVAL;
+
+		noff = le16_to_cpu(cc->NameOffset);
+		nlen = le16_to_cpu(cc->NameLength);
+		if (noff + nlen > doff)
+			return -EINVAL;
+
+		name = (char *)cc + noff;
+		switch (nlen) {
+		case 4:
+			if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
+				*oplock = server->ops->parse_lease_buf(cc, epoch,
+								       lease_key);
+			} else if (buf &&
+				   !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
+				parse_query_id_ctxt(cc, buf);
+			}
+			break;
+		case 16:
+			if (posix && !memcmp(name, smb3_create_tag_posix, 16))
+				parse_posix_ctxt(cc, buf, posix);
+			break;
+		default:
+			cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
+				 __func__, nlen, dlen);
+			if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
+				cifs_dump_mem("context data: ", cc, dlen);
+			break;
+		}
+
+		off = le32_to_cpu(cc->Next);
+		if (!off)
+			break;
+		if (check_sub_overflow(rem, off, &rem))
+			return -EINVAL;
+		cc = (struct create_context *)((u8 *)cc + off);
+	}
+
+	if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
+		*oplock = rsp->OplockLevel;
+
+	return 0;
+}
+
 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
  * All other vectors are freed by kfree().
  */
author maintainer
#36 lore raw

RFC PATCH 35/36 cifs: [WIP] Convert SMB2 Posix Mkdir request

In reply to David Howells
---
 fs/smb/client/smb2pdu.c | 247 ++++++++++++++++++++++------------------
 1 file changed, 139 insertions(+), 108 deletions(-)
fs/smb/client/smb2pdu.c +139 -108
index 6747886024c4..3eeaf027115c 100644
@@ -2470,15 +2470,8 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
 	return rc;
 }
 
-static struct create_posix *
-create_posix_buf(umode_t mode)
+static void fill_posix_buf(struct create_posix *buf, umode_t mode)
 {
-	struct create_posix *buf;
-
-	buf = kzalloc_obj(struct create_posix);
-	if (!buf)
-		return NULL;
-
 	buf->ccontext.DataOffset =
 		cpu_to_le16(offsetof(struct create_posix, Mode));
 	buf->ccontext.DataLength = cpu_to_le32(4);
@@ -2505,20 +2498,25 @@ create_posix_buf(umode_t mode)
 	buf->Name[15] = 0x7C;
 	buf->Mode = cpu_to_le32(mode);
 	cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
-	return buf;
 }
 
 static int
 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
 {
+	struct create_posix *buf;
 	unsigned int num = *num_iovec;
 
-	iov[num].iov_base = create_posix_buf(mode);
 	if (mode == ACL_NO_MODE)
 		cifs_dbg(FYI, "%s: no mode\n", __func__);
-	if (iov[num].iov_base == NULL)
+
+	buf = kzalloc_obj(struct create_posix);
+	if (!buf)
 		return -ENOMEM;
-	iov[num].iov_len = sizeof(struct create_posix);
+
+	fill_posix_buf(buf, mode);
+
+	iov[num].iov_base = buf;
+	iov[num].iov_len  = sizeof(struct create_posix);
 	*num_iovec = num + 1;
 	return 0;
 }
@@ -2986,73 +2984,141 @@ alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
 	return 0;
 }
 
+struct smb2_create_layout {
+	const struct nls_table *cp;
+	u16		offset;		/* Running offset for assembly */
+	u16		path_len;	/* Length of utf16 path (or 0) */
+	u16		treename_len;	/* Length of DFS prefix (or 0) */
+	u16		name_len;	/* Total name length */
+	u16		name_offset;	/* Offset in message of name */
+	u16		contexts_offset; /* Offset in message contexts */
+	u16		contexts_len;	/* Length of contexts (or 0) */
+	u16		posix_offset;	/* Offset in message of posix context */
+	u8		name_pad;	/* Amount of name padding needed */
+};
+
+static int smb311_posix_mkdir_layout(struct cifs_tcon *tcon,
+				     struct smb2_create_layout *lay)
+{
+	size_t offset = lay->offset;
+	size_t tmp;
+	bool has_contexts = false;
+
+	/* [MS-SMB2] 2.2.13 NameOffset: If SMB2_FLAGS_DFS_OPERATIONS
+	 * is set in the Flags field of the SMB2 header, the file name
+	 * includes a prefix that will be processed during DFS name
+	 * normalization as specified in section 3.3.5.9. Otherwise,
+	 * the file name is relative to the share that is identified
+	 * by the TreeId in the SMB2 header.
+	 */
+	lay->name_offset = offset;
+	if (tcon->share_flags & SHI1005_FLAGS_DFS) {
+		const char *treename = tcon->tree_name;
+		int treename_len;
+
+		/* skip leading "\\" */
+		if (!(treename[0] == '\\' && treename[1] == '\\'))
+			return -EINVAL;
+
+		treename_len = cifs_size_strtoUTF16(treename + 2, INT_MAX, lay->cp);
+
+		lay->treename_len = treename_len;
+		lay->name_len = treename_len;
+		if (lay->path_len)
+			lay->name_len += 2 + lay->path_len;
+	} else {
+		lay->name_len = lay->path_len;
+	}
+
+	offset += lay->name_len;
+	tmp = offset;
+	offset = ALIGN8(offset);
+	lay->name_pad = offset - tmp;
+	lay->contexts_offset = offset;
+
+	if (tcon->posix_extensions) {
+		/* resource #3: posix buf */
+		offset += sizeof(struct create_posix);
+		has_contexts = true;
+	}
+
+	if (has_contexts)
+		lay->contexts_len = offset - lay->contexts_offset;
+	else
+		lay->contexts_offset = 0;
+
+	lay->offset = offset;
+	return 0;
+}
+
 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
 			       umode_t mode, struct cifs_tcon *tcon,
 			       const char *full_path,
 			       struct cifs_sb_info *cifs_sb)
 {
-	struct smb_rqst rqst;
+	struct TCP_Server_Info *server;
 	struct smb2_create_req *req;
 	struct smb2_create_rsp *rsp = NULL;
+	struct smb_message *smb = NULL;
 	struct cifs_ses *ses = tcon->ses;
-	struct kvec iov[3]; /* make sure at least one for each open context */
-	struct kvec rsp_iov = {NULL, 0};
-	int resp_buftype;
-	int uni_path_len;
-	__le16 *copy_path = NULL;
-	int copy_size;
-	int rc = 0;
-	unsigned int n_iov = 2;
-	__u32 file_attributes = 0;
-	char *pc_buf = NULL;
-	int flags = 0;
-	unsigned int total_len;
 	__le16 *utf16_path = NULL;
 	struct TCP_Server_Info *server;
-	int retries = 0, cur_sleep = 0;
+	int retries = 0, cur_sleep = 0, path_len;
+
+	if (!ses) {
+		rc = smb_EIO(smb_eio_trace_null_pointers);
+
+	/* resource #1: path allocation */
+	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
+	if (!utf16_path)
+		return -ENOMEM;
+	path_len = UniStrlen(utf16_path);
 
 replay_again:
 	/* reinitialize for possible replay */
-	pc_buf = NULL;
 	flags = 0;
-	n_iov = 2;
 	server = cifs_pick_channel(ses);
 
 	cifs_dbg(FYI, "mkdir\n");
 
-	/* resource #1: path allocation */
-	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
-	if (!utf16_path)
-		return -ENOMEM;
-
-	if (!ses || !server) {
-		rc = smb_EIO(smb_eio_trace_null_pointers);
+	if (!server) {
+		rc = -EIO;
 		goto err_free_path;
 	}
 
+	struct smb2_create_layout layout = {
+		.offset		= sizeof(struct smb2_create_req),
+		.path_len	= path_len,
+		.cp		= cifs_sb->local_nls,
+	};
+	rc = smb311_posix_mkdir_layout(tcon, &layout);
+	if (rc < 0)
+		goto err_free_path;
+
 	/* resource #2: request */
-	rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
-				 (void **) &req, &total_len);
-	if (rc)
+	rc = -ENOMEM;
+	smb = smb2_create_request(SMB2_CREATE, server, tcon,
+				  sizeof(*req), layout.offset, 0,
+				  SMB2_REQ_DYNAMIC);
+	if (!smb)
 		goto err_free_path;
 
 
 	if (smb3_encryption_required(tcon))
 		flags |= CIFS_TRANSFORM_REQ;
 
-	req->ImpersonationLevel = IL_IMPERSONATION;
-	req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
+	req->ImpersonationLevel	= IL_IMPERSONATION;
+	req->DesiredAccess	= cpu_to_le32(FILE_WRITE_ATTRIBUTES);
 	/* File attributes ignored on open (used in create though) */
-	req->FileAttributes = cpu_to_le32(file_attributes);
-	req->ShareAccess = FILE_SHARE_ALL_LE;
-	req->CreateDisposition = cpu_to_le32(FILE_CREATE);
-	req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
-
-	iov[0].iov_base = (char *)req;
-	/* -1 since last byte is buf[0] which is sent below (path) */
-	iov[0].iov_len = total_len;
-
-	req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
+	req->FileAttributes	= cpu_to_le32(file_attributes);
+	req->ShareAccess	= FILE_SHARE_ALL_LE;
+	req->CreateDisposition	= cpu_to_le32(FILE_CREATE);
+	req->CreateOptions	= cpu_to_le32(CREATE_NOT_FILE);
+	req->NameOffset		= cpu_to_le16(layout.name_offset);
+	req->NameLength		= cpu_to_le16(layout.name_len);
+	req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
+	req->CreateContextsOffset = cpu_to_le32(layout.contexts_offset);
+	req->CreateContextsLength = cpu_to_le32(layout.contexts_len);
 
 	/* [MS-SMB2] 2.2.13 NameOffset:
 	 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
@@ -3062,61 +3128,32 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
 	 * the share that is identified by the TreeId in the SMB2
 	 * header.
 	 */
+	__le16 *name = smb->request + layout.name_offset;
+
 	if (tcon->share_flags & SHI1005_FLAGS_DFS) {
-		int name_len;
+		int tmp;
 
 		req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
-		rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
-						 &name_len,
-						 tcon->tree_name, utf16_path);
-		if (rc)
-			goto err_free_req;
 
-		req->NameLength = cpu_to_le16(name_len * 2);
-		uni_path_len = copy_size;
-		/* free before overwriting resource */
-		kfree(utf16_path);
-		utf16_path = copy_path;
-	} else {
-		uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
-		/* MUST set path len (NameLength) to 0 opening root of share */
-		req->NameLength = cpu_to_le16(uni_path_len - 2);
-		if (uni_path_len % 8 != 0) {
-			copy_size = roundup(uni_path_len, 8);
-			copy_path = kzalloc(copy_size, GFP_KERNEL);
-			if (!copy_path) {
-				rc = -ENOMEM;
-				goto err_free_req;
-			}
-			memcpy((char *)copy_path, (const char *)utf16_path,
-			       uni_path_len);
-			uni_path_len = copy_size;
-			/* free before overwriting resource */
-			kfree(utf16_path);
-			utf16_path = copy_path;
+		tmp = cifs_strtoUTF16(name, tcon->tree_name + 2, INT_MAX,
+				      layout.cp);
+		WARN_ON(tmp != layout.treename_len);
+		name += tmp;
+		if (layout.path_len) {
+			*name++ = cpu_to_le16('\\');
+			memcpy(name, utf16_path, layout.path_len);
 		}
+	} else {
+		memcpy(name, utf16_path, layout.path_len);
 	}
 
-	iov[1].iov_len = uni_path_len;
-	iov[1].iov_base = utf16_path;
-	req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
+	if (layout.name_pad)
+		memset(smb->request + layout.name_offset + layout.name_len,
+		       0, layout.name_pad);
 
-	if (tcon->posix_extensions) {
+	if (tcon->posix_extensions)
 		/* resource #3: posix buf */
-		rc = add_posix_context(iov, &n_iov, mode);
-		if (rc)
-			goto err_free_req;
-		req->CreateContextsOffset = cpu_to_le32(
-			sizeof(struct smb2_create_req) +
-			iov[1].iov_len);
-		le32_add_cpu(&req->CreateContextsLength, iov[n_iov-1].iov_len);
-		pc_buf = iov[n_iov-1].iov_base;
-	}
-
-
-	memset(&rqst, 0, sizeof(struct smb_rqst));
-	rqst.rq_iov = iov;
-	rqst.rq_nvec = n_iov;
+		fill_posix_buf(smb->request + layout.posix_offset, mode);
 
 	/* no need to inc num_remote_opens because we close it just below */
 	trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
@@ -3126,18 +3163,17 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
 		/* Back-off before retry */
 		if (cur_sleep)
 			msleep(cur_sleep);
-		smb2_set_replay(server, &rqst);
+		smb2_set_replay(server, smb);
 	}
 
 	/* resource #4: response buffer */
-	rc = cifs_send_recv(xid, ses, server,
-			    &rqst, &resp_buftype, flags, &rsp_iov);
+	rc = smb_send_recv_messages(xid, ses, server, smb, flags);
 	if (rc) {
 		cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
 		trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
 					   CREATE_NOT_FILE,
 					   FILE_WRITE_ATTRIBUTES, rc);
-		goto err_free_rsp_buf;
+		goto err_free_req;
 	}
 
 	/*
@@ -3145,10 +3181,9 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
 	 * adding check below is slightly safer long term (and quiets Coverity
 	 * warning)
 	 */
-	rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
+	rsp = (struct smb2_create_rsp *)smb->response;
 	if (rsp == NULL) {
 		rc = smb_EIO(smb_eio_trace_mkdir_no_rsp);
-		kfree(pc_buf);
 		goto err_free_req;
 	}
 
@@ -3160,18 +3195,14 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
 
 	/* Eventually save off posix specific response info and timestamps */
 
-err_free_rsp_buf:
-	free_rsp_buf(resp_buftype, rsp_iov.iov_base);
-	kfree(pc_buf);
 err_free_req:
-	cifs_small_buf_release(req);
+	smb_put_messages(smb);
 err_free_path:
-	kfree(utf16_path);
-
 	if (is_replayable_error(rc) &&
 	    smb2_should_replay(tcon, &retries, &cur_sleep))
 		goto replay_again;
 
+	kfree(utf16_path);
 	return rc;
 }
 
author maintainer
#37 lore raw

RFC PATCH 36/36 cifs: [WIP] Convert SMB2 Open request

In reply to David Howells
---
 fs/smb/client/cached_dir.c |  41 +-
 fs/smb/client/cifsglob.h   |   5 +-
 fs/smb/client/smb2inode.c  |   6 +-
 fs/smb/client/smb2ops.c    |  34 +-
 fs/smb/client/smb2pdu.c    | 880 +++++++++++++++----------------------
 fs/smb/client/smb2proto.h  |  24 +-
 6 files changed, 401 insertions(+), 589 deletions(-)
fs/smb/client/cached_dir.c +15 -26
index 88d5e9a32f28..4a4063d2c903 100644
@@ -141,9 +141,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 	struct cifs_open_parms oparms;
 	struct smb2_create_rsp *o_rsp = NULL;
 	struct smb2_query_info_rsp *qi_rsp = NULL;
-	int resp_buftype[2];
-	struct smb_rqst rqst[2];
-	struct kvec rsp_iov[2];
+	struct smb_message *smb;
 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
 	struct kvec qi_iov[1];
 	int rc, flags = 0;
@@ -261,29 +259,20 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 
 	server->ops->new_lease_key(pfid);
 
-	memset(rqst, 0, sizeof(rqst));
-	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
-	memset(rsp_iov, 0, sizeof(rsp_iov));
-
 	/* Open */
-	memset(&open_iov, 0, sizeof(open_iov));
-	rqst[0].rq_iov = open_iov;
-	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
-
 	oparms = (struct cifs_open_parms) {
-		.tcon = tcon,
-		.path = path,
-		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE),
-		.desired_access =  FILE_READ_DATA | FILE_READ_ATTRIBUTES |
-				   FILE_READ_EA,
-		.disposition = FILE_OPEN,
-		.fid = pfid,
-		.lease_flags = lease_flags,
-		.replay = !!(retries),
+		.tcon		= tcon,
+		.path		= path,
+		.create_options	= cifs_create_options(cifs_sb, CREATE_NOT_FILE),
+		.desired_access	= FILE_READ_DATA | FILE_READ_ATTRIBUTES |
+				  FILE_READ_EA,
+		.disposition	= FILE_OPEN,
+		.fid		= pfid,
+		.lease_flags	= lease_flags,
+		.replay		= !!(retries),
 	};
 
-	rc = SMB2_open_init(tcon, server,
-			    &rqst[0], &oplock, &oparms, utf16_path);
+	smb = SMB2_open_init(tcon, server, &oplock, &oparms, utf16_path);
 	if (rc)
 		goto oshr_free;
 
@@ -349,10 +338,10 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 		goto oshr_free;
 	}
 
-	rc = smb2_parse_contexts(server, rsp_iov,
-				 &oparms.fid->epoch,
-				 oparms.fid->lease_key,
-				 &oplock, NULL, NULL);
+	rc = smb2_parse_create_response(server, rsp_iov,
+					&oparms.fid->epoch,
+					oparms.fid->lease_key,
+					&oplock, NULL, NULL);
 	if (rc) {
 		spin_unlock(&cfids->cfid_list_lock);
 		goto oshr_free;
fs/smb/client/cifsglob.h +3 -2
index e5972ac80240..616eec52aa7d 100644
@@ -530,8 +530,9 @@ struct smb_version_operations {
 	/* set oplock level for the inode */
 	void (*set_oplock_level)(struct cifsInodeInfo *cinode, __u32 oplock, __u16 epoch,
 				 bool *purge_cache);
-	/* create lease context buffer for CREATE request */
-	char * (*create_lease_buf)(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 le_flags);
+	/* Fill in a lease context buffer for CREATE request */
+	void (*fill_lease_buf)(struct smb_message *smb, const u8 *lease_key, u8 oplock,
+			       const u8 *parent_lease_key, __le32 le_flags);
 	/* parse lease context buffer and return oplock/epoch info */
 	__u8 (*parse_lease_buf)(void *buf, __u16 *epoch, char *lkey);
 	ssize_t (*copychunk_range)(const unsigned int,
fs/smb/client/smb2inode.c +3 -3
index 6789049db053..73819629af19 100644
@@ -579,9 +579,9 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
 		idata->fi.DeletePending = 0; /* successful open = not delete pending */
 		idata->fi.Directory = !!(le32_to_cpu(create_rsp->FileAttributes) & ATTR_DIRECTORY);
 
-		/* smb2_parse_contexts() fills idata->fi.IndexNumber */
-		rc = smb2_parse_contexts(server, &rsp_iov[0], &oparms->fid->epoch,
-					 oparms->fid->lease_key, &oplock, &idata->fi, NULL);
+		/* smb2_parse_create_response() fills idata->fi.IndexNumber */
+		rc = smb2_parse_create_response(server, &rsp_iov[0], &oparms->fid->epoch,
+						oparms->fid->lease_key, &oplock, &idata->fi, NULL);
 		if (rc)
 			cifs_dbg(VFS, "rc: %d parsing context of compound op\n", rc);
 	}
fs/smb/client/smb2ops.c +14 -20
index 0e212a3e883d..f6598ea0a2de 100644
@@ -4248,14 +4248,11 @@ map_oplock_to_lease(u8 oplock)
 	return 0;
 }
 
-static char *
-smb2_create_lease_buf(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 flags)
+static void
+smb2_fill_lease_buf(struct smb_message *smb, const u8 *lease_key, u8 oplock,
+		    const u8 *parent_lease_key, __le32 flags)
 {
-	struct create_lease *buf;
-
-	buf = kzalloc_obj(struct create_lease);
-	if (!buf)
-		return NULL;
+	struct create_lease *buf = cifs_begin_extension(smb);
 
 	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
 	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
@@ -4271,17 +4268,14 @@ smb2_create_lease_buf(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 fla
 	buf->Name[1] = 'q';
 	buf->Name[2] = 'L';
 	buf->Name[3] = 's';
-	return (char *)buf;
+	cifs_end_extension(smb, sizeof(*buf));
 }
 
-static char *
-smb3_create_lease_buf(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 flags)
+static void
+smb3_fill_lease_buf(struct smb_message *smb, const u8 *lease_key, u8 oplock,
+		    const u8 *parent_lease_key, __le32 flags)
 {
-	struct create_lease_v2 *buf;
-
-	buf = kzalloc_obj(struct create_lease_v2);
-	if (!buf)
-		return NULL;
+	struct create_lease_v2 *buf = cifs_begin_extension(smb);
 
 	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
 	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
@@ -4300,7 +4294,7 @@ smb3_create_lease_buf(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 fla
 	buf->Name[1] = 'q';
 	buf->Name[2] = 'L';
 	buf->Name[3] = 's';
-	return (char *)buf;
+	cifs_end_extension(smb, sizeof(*buf));
 }
 
 static __u8
@@ -4628,7 +4622,7 @@ struct smb_version_operations smb20_operations = {
 	.new_lease_key = smb2_new_lease_key,
 	.is_read_op = smb2_is_read_op,
 	.set_oplock_level = smb2_set_oplock_level,
-	.create_lease_buf = smb2_create_lease_buf,
+	.fill_lease_buf = smb2_fill_lease_buf,
 	.parse_lease_buf = smb2_parse_lease_buf,
 	.copychunk_range = smb2_copychunk_range,
 	.wp_retry_size = smb2_wp_retry_size,
@@ -4719,7 +4713,7 @@ struct smb_version_operations smb21_operations = {
 	.new_lease_key = smb2_new_lease_key,
 	.is_read_op = smb21_is_read_op,
 	.set_oplock_level = smb21_set_oplock_level,
-	.create_lease_buf = smb2_create_lease_buf,
+	.fill_lease_buf = smb2_fill_lease_buf,
 	.parse_lease_buf = smb2_parse_lease_buf,
 	.copychunk_range = smb2_copychunk_range,
 	.wp_retry_size = smb2_wp_retry_size,
@@ -4817,7 +4811,7 @@ struct smb_version_operations smb30_operations = {
 	.set_integrity  = smb3_set_integrity,
 	.is_read_op = smb21_is_read_op,
 	.set_oplock_level = smb3_set_oplock_level,
-	.create_lease_buf = smb3_create_lease_buf,
+	.fill_lease_buf = smb3_fill_lease_buf,
 	.parse_lease_buf = smb3_parse_lease_buf,
 	.copychunk_range = smb2_copychunk_range,
 	.duplicate_extents = smb2_duplicate_extents,
@@ -4919,7 +4913,7 @@ struct smb_version_operations smb311_operations = {
 	.set_integrity  = smb3_set_integrity,
 	.is_read_op = smb21_is_read_op,
 	.set_oplock_level = smb3_set_oplock_level,
-	.create_lease_buf = smb3_create_lease_buf,
+	.fill_lease_buf = smb3_fill_lease_buf,
 	.parse_lease_buf = smb3_parse_lease_buf,
 	.copychunk_range = smb2_copychunk_range,
 	.duplicate_extents = smb2_duplicate_extents,
fs/smb/client/smb2pdu.c +354 -526

1,112 lines not on this page · raw message

fs/smb/client/smb2proto.h +12 -12
index 6571a6a7b147..8d3f5271bc21 100644
@@ -149,14 +149,12 @@ int SMB2_logoff(const unsigned int xid, struct cifs_ses *ses);
 int SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 	      struct cifs_tcon *tcon, const struct nls_table *cp);
 int SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon);
-int SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms,
-	      __le16 *path, __u8 *oplock, struct smb2_file_all_info *buf,
-	      struct create_posix_rsp *posix, struct kvec *err_iov,
-	      int *buftype);
-int SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
-		   struct smb_rqst *rqst, __u8 *oplock,
-		   struct cifs_open_parms *oparms, __le16 *path);
-void SMB2_open_free(struct smb_rqst *rqst);
+struct smb_message *SMB2_open_init(struct cifs_tcon *tcon,
+				   struct TCP_Server_Info *server, __u8 *oplock,
+				   struct cifs_open_parms *oparms, __le16 *path);
+int SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
+	      __u8 *oplock, struct smb2_file_all_info *buf,
+	      struct create_posix_rsp *posix, struct kvec *err_iov);
 int SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon,
 	       u64 persistent_fid, u64 volatile_fid, u32 opcode, char *in_data,
 	       u32 indatalen, u32 max_out_data_len, char **out_data,
@@ -258,10 +256,12 @@ int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon);
 
 enum securityEnum smb2_select_sectype(struct TCP_Server_Info *server,
 				      enum securityEnum requested);
-int smb2_parse_contexts(struct TCP_Server_Info *server, struct kvec *rsp_iov,
-			__u16 *epoch, char *lease_key, __u8 *oplock,
-			struct smb2_file_all_info *buf,
-			struct create_posix_rsp *posix);
+int smb2_parse_create_response(struct TCP_Server_Info *server,
+			       struct smb_message *smb,
+			       __u16 *epoch,
+			       char *lease_key, __u8 *oplock,
+			       struct smb2_file_all_info *buf,
+			       struct create_posix_rsp *posix);
 
 int smb3_encryption_required(const struct cifs_tcon *tcon);
 int smb2_validate_iov(unsigned int offset, unsigned int buffer_length,