xref: /OK3568_Linux_fs/kernel/fs/cifs/smb2ops.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  SMB2 version specific operations
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/pagemap.h>
9*4882a593Smuzhiyun #include <linux/vfs.h>
10*4882a593Smuzhiyun #include <linux/falloc.h>
11*4882a593Smuzhiyun #include <linux/scatterlist.h>
12*4882a593Smuzhiyun #include <linux/uuid.h>
13*4882a593Smuzhiyun #include <linux/sort.h>
14*4882a593Smuzhiyun #include <crypto/aead.h>
15*4882a593Smuzhiyun #include <linux/fiemap.h>
16*4882a593Smuzhiyun #include "cifsfs.h"
17*4882a593Smuzhiyun #include "cifsglob.h"
18*4882a593Smuzhiyun #include "smb2pdu.h"
19*4882a593Smuzhiyun #include "smb2proto.h"
20*4882a593Smuzhiyun #include "cifsproto.h"
21*4882a593Smuzhiyun #include "cifs_debug.h"
22*4882a593Smuzhiyun #include "cifs_unicode.h"
23*4882a593Smuzhiyun #include "smb2status.h"
24*4882a593Smuzhiyun #include "smb2glob.h"
25*4882a593Smuzhiyun #include "cifs_ioctl.h"
26*4882a593Smuzhiyun #include "smbdirect.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Change credits for different ops and return the total number of credits */
29*4882a593Smuzhiyun static int
change_conf(struct TCP_Server_Info * server)30*4882a593Smuzhiyun change_conf(struct TCP_Server_Info *server)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	server->credits += server->echo_credits + server->oplock_credits;
33*4882a593Smuzhiyun 	server->oplock_credits = server->echo_credits = 0;
34*4882a593Smuzhiyun 	switch (server->credits) {
35*4882a593Smuzhiyun 	case 0:
36*4882a593Smuzhiyun 		return 0;
37*4882a593Smuzhiyun 	case 1:
38*4882a593Smuzhiyun 		server->echoes = false;
39*4882a593Smuzhiyun 		server->oplocks = false;
40*4882a593Smuzhiyun 		break;
41*4882a593Smuzhiyun 	case 2:
42*4882a593Smuzhiyun 		server->echoes = true;
43*4882a593Smuzhiyun 		server->oplocks = false;
44*4882a593Smuzhiyun 		server->echo_credits = 1;
45*4882a593Smuzhiyun 		break;
46*4882a593Smuzhiyun 	default:
47*4882a593Smuzhiyun 		server->echoes = true;
48*4882a593Smuzhiyun 		if (enable_oplocks) {
49*4882a593Smuzhiyun 			server->oplocks = true;
50*4882a593Smuzhiyun 			server->oplock_credits = 1;
51*4882a593Smuzhiyun 		} else
52*4882a593Smuzhiyun 			server->oplocks = false;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 		server->echo_credits = 1;
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 	server->credits -= server->echo_credits + server->oplock_credits;
57*4882a593Smuzhiyun 	return server->credits + server->echo_credits + server->oplock_credits;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun static void
smb2_add_credits(struct TCP_Server_Info * server,const struct cifs_credits * credits,const int optype)61*4882a593Smuzhiyun smb2_add_credits(struct TCP_Server_Info *server,
62*4882a593Smuzhiyun 		 const struct cifs_credits *credits, const int optype)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	int *val, rc = -1;
65*4882a593Smuzhiyun 	unsigned int add = credits->value;
66*4882a593Smuzhiyun 	unsigned int instance = credits->instance;
67*4882a593Smuzhiyun 	bool reconnect_detected = false;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	spin_lock(&server->req_lock);
70*4882a593Smuzhiyun 	val = server->ops->get_credits_field(server, optype);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/* eg found case where write overlapping reconnect messed up credits */
73*4882a593Smuzhiyun 	if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
74*4882a593Smuzhiyun 		trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
75*4882a593Smuzhiyun 			server->hostname, *val, add);
76*4882a593Smuzhiyun 	if ((instance == 0) || (instance == server->reconnect_instance))
77*4882a593Smuzhiyun 		*val += add;
78*4882a593Smuzhiyun 	else
79*4882a593Smuzhiyun 		reconnect_detected = true;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if (*val > 65000) {
82*4882a593Smuzhiyun 		*val = 65000; /* Don't get near 64K credits, avoid srv bugs */
83*4882a593Smuzhiyun 		pr_warn_once("server overflowed SMB3 credits\n");
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 	server->in_flight--;
86*4882a593Smuzhiyun 	if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
87*4882a593Smuzhiyun 		rc = change_conf(server);
88*4882a593Smuzhiyun 	/*
89*4882a593Smuzhiyun 	 * Sometimes server returns 0 credits on oplock break ack - we need to
90*4882a593Smuzhiyun 	 * rebalance credits in this case.
91*4882a593Smuzhiyun 	 */
92*4882a593Smuzhiyun 	else if (server->in_flight > 0 && server->oplock_credits == 0 &&
93*4882a593Smuzhiyun 		 server->oplocks) {
94*4882a593Smuzhiyun 		if (server->credits > 1) {
95*4882a593Smuzhiyun 			server->credits--;
96*4882a593Smuzhiyun 			server->oplock_credits++;
97*4882a593Smuzhiyun 		}
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 	spin_unlock(&server->req_lock);
100*4882a593Smuzhiyun 	wake_up(&server->request_q);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	if (reconnect_detected)
103*4882a593Smuzhiyun 		cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
104*4882a593Smuzhiyun 			 add, instance);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (server->tcpStatus == CifsNeedReconnect
107*4882a593Smuzhiyun 	    || server->tcpStatus == CifsExiting)
108*4882a593Smuzhiyun 		return;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	switch (rc) {
111*4882a593Smuzhiyun 	case -1:
112*4882a593Smuzhiyun 		/* change_conf hasn't been executed */
113*4882a593Smuzhiyun 		break;
114*4882a593Smuzhiyun 	case 0:
115*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
116*4882a593Smuzhiyun 		break;
117*4882a593Smuzhiyun 	case 1:
118*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
119*4882a593Smuzhiyun 		break;
120*4882a593Smuzhiyun 	case 2:
121*4882a593Smuzhiyun 		cifs_dbg(FYI, "disabling oplocks\n");
122*4882a593Smuzhiyun 		break;
123*4882a593Smuzhiyun 	default:
124*4882a593Smuzhiyun 		trace_smb3_add_credits(server->CurrentMid,
125*4882a593Smuzhiyun 			server->hostname, rc, add);
126*4882a593Smuzhiyun 		cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun static void
smb2_set_credits(struct TCP_Server_Info * server,const int val)131*4882a593Smuzhiyun smb2_set_credits(struct TCP_Server_Info *server, const int val)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	spin_lock(&server->req_lock);
134*4882a593Smuzhiyun 	server->credits = val;
135*4882a593Smuzhiyun 	if (val == 1)
136*4882a593Smuzhiyun 		server->reconnect_instance++;
137*4882a593Smuzhiyun 	spin_unlock(&server->req_lock);
138*4882a593Smuzhiyun 	/* don't log while holding the lock */
139*4882a593Smuzhiyun 	if (val == 1)
140*4882a593Smuzhiyun 		cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun static int *
smb2_get_credits_field(struct TCP_Server_Info * server,const int optype)144*4882a593Smuzhiyun smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	switch (optype) {
147*4882a593Smuzhiyun 	case CIFS_ECHO_OP:
148*4882a593Smuzhiyun 		return &server->echo_credits;
149*4882a593Smuzhiyun 	case CIFS_OBREAK_OP:
150*4882a593Smuzhiyun 		return &server->oplock_credits;
151*4882a593Smuzhiyun 	default:
152*4882a593Smuzhiyun 		return &server->credits;
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun static unsigned int
smb2_get_credits(struct mid_q_entry * mid)157*4882a593Smuzhiyun smb2_get_credits(struct mid_q_entry *mid)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	return mid->credits_received;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static int
smb2_wait_mtu_credits(struct TCP_Server_Info * server,unsigned int size,unsigned int * num,struct cifs_credits * credits)163*4882a593Smuzhiyun smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
164*4882a593Smuzhiyun 		      unsigned int *num, struct cifs_credits *credits)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	int rc = 0;
167*4882a593Smuzhiyun 	unsigned int scredits;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	spin_lock(&server->req_lock);
170*4882a593Smuzhiyun 	while (1) {
171*4882a593Smuzhiyun 		if (server->credits <= 0) {
172*4882a593Smuzhiyun 			spin_unlock(&server->req_lock);
173*4882a593Smuzhiyun 			cifs_num_waiters_inc(server);
174*4882a593Smuzhiyun 			rc = wait_event_killable(server->request_q,
175*4882a593Smuzhiyun 				has_credits(server, &server->credits, 1));
176*4882a593Smuzhiyun 			cifs_num_waiters_dec(server);
177*4882a593Smuzhiyun 			if (rc)
178*4882a593Smuzhiyun 				return rc;
179*4882a593Smuzhiyun 			spin_lock(&server->req_lock);
180*4882a593Smuzhiyun 		} else {
181*4882a593Smuzhiyun 			if (server->tcpStatus == CifsExiting) {
182*4882a593Smuzhiyun 				spin_unlock(&server->req_lock);
183*4882a593Smuzhiyun 				return -ENOENT;
184*4882a593Smuzhiyun 			}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 			scredits = server->credits;
187*4882a593Smuzhiyun 			/* can deadlock with reopen */
188*4882a593Smuzhiyun 			if (scredits <= 8) {
189*4882a593Smuzhiyun 				*num = SMB2_MAX_BUFFER_SIZE;
190*4882a593Smuzhiyun 				credits->value = 0;
191*4882a593Smuzhiyun 				credits->instance = 0;
192*4882a593Smuzhiyun 				break;
193*4882a593Smuzhiyun 			}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 			/* leave some credits for reopen and other ops */
196*4882a593Smuzhiyun 			scredits -= 8;
197*4882a593Smuzhiyun 			*num = min_t(unsigned int, size,
198*4882a593Smuzhiyun 				     scredits * SMB2_MAX_BUFFER_SIZE);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 			credits->value =
201*4882a593Smuzhiyun 				DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
202*4882a593Smuzhiyun 			credits->instance = server->reconnect_instance;
203*4882a593Smuzhiyun 			server->credits -= credits->value;
204*4882a593Smuzhiyun 			server->in_flight++;
205*4882a593Smuzhiyun 			if (server->in_flight > server->max_in_flight)
206*4882a593Smuzhiyun 				server->max_in_flight = server->in_flight;
207*4882a593Smuzhiyun 			break;
208*4882a593Smuzhiyun 		}
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 	spin_unlock(&server->req_lock);
211*4882a593Smuzhiyun 	return rc;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun static int
smb2_adjust_credits(struct TCP_Server_Info * server,struct cifs_credits * credits,const unsigned int payload_size)215*4882a593Smuzhiyun smb2_adjust_credits(struct TCP_Server_Info *server,
216*4882a593Smuzhiyun 		    struct cifs_credits *credits,
217*4882a593Smuzhiyun 		    const unsigned int payload_size)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (!credits->value || credits->value == new_val)
222*4882a593Smuzhiyun 		return 0;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	if (credits->value < new_val) {
225*4882a593Smuzhiyun 		WARN_ONCE(1, "request has less credits (%d) than required (%d)",
226*4882a593Smuzhiyun 			  credits->value, new_val);
227*4882a593Smuzhiyun 		return -ENOTSUPP;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	spin_lock(&server->req_lock);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (server->reconnect_instance != credits->instance) {
233*4882a593Smuzhiyun 		spin_unlock(&server->req_lock);
234*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
235*4882a593Smuzhiyun 			 credits->value - new_val);
236*4882a593Smuzhiyun 		return -EAGAIN;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	server->credits += credits->value - new_val;
240*4882a593Smuzhiyun 	spin_unlock(&server->req_lock);
241*4882a593Smuzhiyun 	wake_up(&server->request_q);
242*4882a593Smuzhiyun 	credits->value = new_val;
243*4882a593Smuzhiyun 	return 0;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun static __u64
smb2_get_next_mid(struct TCP_Server_Info * server)247*4882a593Smuzhiyun smb2_get_next_mid(struct TCP_Server_Info *server)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	__u64 mid;
250*4882a593Smuzhiyun 	/* for SMB2 we need the current value */
251*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
252*4882a593Smuzhiyun 	mid = server->CurrentMid++;
253*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
254*4882a593Smuzhiyun 	return mid;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun static void
smb2_revert_current_mid(struct TCP_Server_Info * server,const unsigned int val)258*4882a593Smuzhiyun smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
261*4882a593Smuzhiyun 	if (server->CurrentMid >= val)
262*4882a593Smuzhiyun 		server->CurrentMid -= val;
263*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun static struct mid_q_entry *
__smb2_find_mid(struct TCP_Server_Info * server,char * buf,bool dequeue)267*4882a593Smuzhiyun __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	struct mid_q_entry *mid;
270*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
271*4882a593Smuzhiyun 	__u64 wire_mid = le64_to_cpu(shdr->MessageId);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
274*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
275*4882a593Smuzhiyun 		return NULL;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
279*4882a593Smuzhiyun 	list_for_each_entry(mid, &server->pending_mid_q, qhead) {
280*4882a593Smuzhiyun 		if ((mid->mid == wire_mid) &&
281*4882a593Smuzhiyun 		    (mid->mid_state == MID_REQUEST_SUBMITTED) &&
282*4882a593Smuzhiyun 		    (mid->command == shdr->Command)) {
283*4882a593Smuzhiyun 			kref_get(&mid->refcount);
284*4882a593Smuzhiyun 			if (dequeue) {
285*4882a593Smuzhiyun 				list_del_init(&mid->qhead);
286*4882a593Smuzhiyun 				mid->mid_flags |= MID_DELETED;
287*4882a593Smuzhiyun 			}
288*4882a593Smuzhiyun 			spin_unlock(&GlobalMid_Lock);
289*4882a593Smuzhiyun 			return mid;
290*4882a593Smuzhiyun 		}
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
293*4882a593Smuzhiyun 	return NULL;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun static struct mid_q_entry *
smb2_find_mid(struct TCP_Server_Info * server,char * buf)297*4882a593Smuzhiyun smb2_find_mid(struct TCP_Server_Info *server, char *buf)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	return __smb2_find_mid(server, buf, false);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun static struct mid_q_entry *
smb2_find_dequeue_mid(struct TCP_Server_Info * server,char * buf)303*4882a593Smuzhiyun smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	return __smb2_find_mid(server, buf, true);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun static void
smb2_dump_detail(void * buf,struct TCP_Server_Info * server)309*4882a593Smuzhiyun smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DEBUG2
312*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
315*4882a593Smuzhiyun 		 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
316*4882a593Smuzhiyun 		 shdr->ProcessId);
317*4882a593Smuzhiyun 	cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
318*4882a593Smuzhiyun 		 server->ops->calc_smb_size(buf, server));
319*4882a593Smuzhiyun #endif
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun static bool
smb2_need_neg(struct TCP_Server_Info * server)323*4882a593Smuzhiyun smb2_need_neg(struct TCP_Server_Info *server)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	return server->max_read == 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun static int
smb2_negotiate(const unsigned int xid,struct cifs_ses * ses)329*4882a593Smuzhiyun smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	int rc;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	cifs_ses_server(ses)->CurrentMid = 0;
334*4882a593Smuzhiyun 	rc = SMB2_negotiate(xid, ses);
335*4882a593Smuzhiyun 	/* BB we probably don't need to retry with modern servers */
336*4882a593Smuzhiyun 	if (rc == -EAGAIN)
337*4882a593Smuzhiyun 		rc = -EHOSTDOWN;
338*4882a593Smuzhiyun 	return rc;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun static unsigned int
smb2_negotiate_wsize(struct cifs_tcon * tcon,struct smb_vol * volume_info)342*4882a593Smuzhiyun smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tcon->ses->server;
345*4882a593Smuzhiyun 	unsigned int wsize;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* start with specified wsize, or default */
348*4882a593Smuzhiyun 	wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
349*4882a593Smuzhiyun 	wsize = min_t(unsigned int, wsize, server->max_write);
350*4882a593Smuzhiyun 	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
351*4882a593Smuzhiyun 		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	return wsize;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun static unsigned int
smb3_negotiate_wsize(struct cifs_tcon * tcon,struct smb_vol * volume_info)357*4882a593Smuzhiyun smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tcon->ses->server;
360*4882a593Smuzhiyun 	unsigned int wsize;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	/* start with specified wsize, or default */
363*4882a593Smuzhiyun 	wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
364*4882a593Smuzhiyun 	wsize = min_t(unsigned int, wsize, server->max_write);
365*4882a593Smuzhiyun #ifdef CONFIG_CIFS_SMB_DIRECT
366*4882a593Smuzhiyun 	if (server->rdma) {
367*4882a593Smuzhiyun 		if (server->sign)
368*4882a593Smuzhiyun 			/*
369*4882a593Smuzhiyun 			 * Account for SMB2 data transfer packet header and
370*4882a593Smuzhiyun 			 * possible encryption header
371*4882a593Smuzhiyun 			 */
372*4882a593Smuzhiyun 			wsize = min_t(unsigned int,
373*4882a593Smuzhiyun 				wsize,
374*4882a593Smuzhiyun 				server->smbd_conn->max_fragmented_send_size -
375*4882a593Smuzhiyun 					SMB2_READWRITE_PDU_HEADER_SIZE -
376*4882a593Smuzhiyun 					sizeof(struct smb2_transform_hdr));
377*4882a593Smuzhiyun 		else
378*4882a593Smuzhiyun 			wsize = min_t(unsigned int,
379*4882a593Smuzhiyun 				wsize, server->smbd_conn->max_readwrite_size);
380*4882a593Smuzhiyun 	}
381*4882a593Smuzhiyun #endif
382*4882a593Smuzhiyun 	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
383*4882a593Smuzhiyun 		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	return wsize;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun static unsigned int
smb2_negotiate_rsize(struct cifs_tcon * tcon,struct smb_vol * volume_info)389*4882a593Smuzhiyun smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tcon->ses->server;
392*4882a593Smuzhiyun 	unsigned int rsize;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* start with specified rsize, or default */
395*4882a593Smuzhiyun 	rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
396*4882a593Smuzhiyun 	rsize = min_t(unsigned int, rsize, server->max_read);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
399*4882a593Smuzhiyun 		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return rsize;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun static unsigned int
smb3_negotiate_rsize(struct cifs_tcon * tcon,struct smb_vol * volume_info)405*4882a593Smuzhiyun smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tcon->ses->server;
408*4882a593Smuzhiyun 	unsigned int rsize;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	/* start with specified rsize, or default */
411*4882a593Smuzhiyun 	rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
412*4882a593Smuzhiyun 	rsize = min_t(unsigned int, rsize, server->max_read);
413*4882a593Smuzhiyun #ifdef CONFIG_CIFS_SMB_DIRECT
414*4882a593Smuzhiyun 	if (server->rdma) {
415*4882a593Smuzhiyun 		if (server->sign)
416*4882a593Smuzhiyun 			/*
417*4882a593Smuzhiyun 			 * Account for SMB2 data transfer packet header and
418*4882a593Smuzhiyun 			 * possible encryption header
419*4882a593Smuzhiyun 			 */
420*4882a593Smuzhiyun 			rsize = min_t(unsigned int,
421*4882a593Smuzhiyun 				rsize,
422*4882a593Smuzhiyun 				server->smbd_conn->max_fragmented_recv_size -
423*4882a593Smuzhiyun 					SMB2_READWRITE_PDU_HEADER_SIZE -
424*4882a593Smuzhiyun 					sizeof(struct smb2_transform_hdr));
425*4882a593Smuzhiyun 		else
426*4882a593Smuzhiyun 			rsize = min_t(unsigned int,
427*4882a593Smuzhiyun 				rsize, server->smbd_conn->max_readwrite_size);
428*4882a593Smuzhiyun 	}
429*4882a593Smuzhiyun #endif
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
432*4882a593Smuzhiyun 		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	return rsize;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun static int
parse_server_interfaces(struct network_interface_info_ioctl_rsp * buf,size_t buf_len,struct cifs_server_iface ** iface_list,size_t * iface_count)438*4882a593Smuzhiyun parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
439*4882a593Smuzhiyun 			size_t buf_len,
440*4882a593Smuzhiyun 			struct cifs_server_iface **iface_list,
441*4882a593Smuzhiyun 			size_t *iface_count)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct network_interface_info_ioctl_rsp *p;
444*4882a593Smuzhiyun 	struct sockaddr_in *addr4;
445*4882a593Smuzhiyun 	struct sockaddr_in6 *addr6;
446*4882a593Smuzhiyun 	struct iface_info_ipv4 *p4;
447*4882a593Smuzhiyun 	struct iface_info_ipv6 *p6;
448*4882a593Smuzhiyun 	struct cifs_server_iface *info;
449*4882a593Smuzhiyun 	ssize_t bytes_left;
450*4882a593Smuzhiyun 	size_t next = 0;
451*4882a593Smuzhiyun 	int nb_iface = 0;
452*4882a593Smuzhiyun 	int rc = 0;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	*iface_list = NULL;
455*4882a593Smuzhiyun 	*iface_count = 0;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/*
458*4882a593Smuzhiyun 	 * Fist pass: count and sanity check
459*4882a593Smuzhiyun 	 */
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	bytes_left = buf_len;
462*4882a593Smuzhiyun 	p = buf;
463*4882a593Smuzhiyun 	while (bytes_left >= sizeof(*p)) {
464*4882a593Smuzhiyun 		nb_iface++;
465*4882a593Smuzhiyun 		next = le32_to_cpu(p->Next);
466*4882a593Smuzhiyun 		if (!next) {
467*4882a593Smuzhiyun 			bytes_left -= sizeof(*p);
468*4882a593Smuzhiyun 			break;
469*4882a593Smuzhiyun 		}
470*4882a593Smuzhiyun 		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
471*4882a593Smuzhiyun 		bytes_left -= next;
472*4882a593Smuzhiyun 	}
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	if (!nb_iface) {
475*4882a593Smuzhiyun 		cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
476*4882a593Smuzhiyun 		rc = -EINVAL;
477*4882a593Smuzhiyun 		goto out;
478*4882a593Smuzhiyun 	}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	/* Azure rounds the buffer size up 8, to a 16 byte boundary */
481*4882a593Smuzhiyun 	if ((bytes_left > 8) || p->Next)
482*4882a593Smuzhiyun 		cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/*
486*4882a593Smuzhiyun 	 * Second pass: extract info to internal structure
487*4882a593Smuzhiyun 	 */
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	*iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
490*4882a593Smuzhiyun 	if (!*iface_list) {
491*4882a593Smuzhiyun 		rc = -ENOMEM;
492*4882a593Smuzhiyun 		goto out;
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	info = *iface_list;
496*4882a593Smuzhiyun 	bytes_left = buf_len;
497*4882a593Smuzhiyun 	p = buf;
498*4882a593Smuzhiyun 	while (bytes_left >= sizeof(*p)) {
499*4882a593Smuzhiyun 		info->speed = le64_to_cpu(p->LinkSpeed);
500*4882a593Smuzhiyun 		info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
501*4882a593Smuzhiyun 		info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
504*4882a593Smuzhiyun 		cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
505*4882a593Smuzhiyun 		cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
506*4882a593Smuzhiyun 			 le32_to_cpu(p->Capability));
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 		switch (p->Family) {
509*4882a593Smuzhiyun 		/*
510*4882a593Smuzhiyun 		 * The kernel and wire socket structures have the same
511*4882a593Smuzhiyun 		 * layout and use network byte order but make the
512*4882a593Smuzhiyun 		 * conversion explicit in case either one changes.
513*4882a593Smuzhiyun 		 */
514*4882a593Smuzhiyun 		case INTERNETWORK:
515*4882a593Smuzhiyun 			addr4 = (struct sockaddr_in *)&info->sockaddr;
516*4882a593Smuzhiyun 			p4 = (struct iface_info_ipv4 *)p->Buffer;
517*4882a593Smuzhiyun 			addr4->sin_family = AF_INET;
518*4882a593Smuzhiyun 			memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 			/* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
521*4882a593Smuzhiyun 			addr4->sin_port = cpu_to_be16(CIFS_PORT);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 			cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
524*4882a593Smuzhiyun 				 &addr4->sin_addr);
525*4882a593Smuzhiyun 			break;
526*4882a593Smuzhiyun 		case INTERNETWORKV6:
527*4882a593Smuzhiyun 			addr6 =	(struct sockaddr_in6 *)&info->sockaddr;
528*4882a593Smuzhiyun 			p6 = (struct iface_info_ipv6 *)p->Buffer;
529*4882a593Smuzhiyun 			addr6->sin6_family = AF_INET6;
530*4882a593Smuzhiyun 			memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 			/* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
533*4882a593Smuzhiyun 			addr6->sin6_flowinfo = 0;
534*4882a593Smuzhiyun 			addr6->sin6_scope_id = 0;
535*4882a593Smuzhiyun 			addr6->sin6_port = cpu_to_be16(CIFS_PORT);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 			cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
538*4882a593Smuzhiyun 				 &addr6->sin6_addr);
539*4882a593Smuzhiyun 			break;
540*4882a593Smuzhiyun 		default:
541*4882a593Smuzhiyun 			cifs_dbg(VFS,
542*4882a593Smuzhiyun 				 "%s: skipping unsupported socket family\n",
543*4882a593Smuzhiyun 				 __func__);
544*4882a593Smuzhiyun 			goto next_iface;
545*4882a593Smuzhiyun 		}
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 		(*iface_count)++;
548*4882a593Smuzhiyun 		info++;
549*4882a593Smuzhiyun next_iface:
550*4882a593Smuzhiyun 		next = le32_to_cpu(p->Next);
551*4882a593Smuzhiyun 		if (!next)
552*4882a593Smuzhiyun 			break;
553*4882a593Smuzhiyun 		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
554*4882a593Smuzhiyun 		bytes_left -= next;
555*4882a593Smuzhiyun 	}
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	if (!*iface_count) {
558*4882a593Smuzhiyun 		rc = -EINVAL;
559*4882a593Smuzhiyun 		goto out;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun out:
563*4882a593Smuzhiyun 	if (rc) {
564*4882a593Smuzhiyun 		kfree(*iface_list);
565*4882a593Smuzhiyun 		*iface_count = 0;
566*4882a593Smuzhiyun 		*iface_list = NULL;
567*4882a593Smuzhiyun 	}
568*4882a593Smuzhiyun 	return rc;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
compare_iface(const void * ia,const void * ib)571*4882a593Smuzhiyun static int compare_iface(const void *ia, const void *ib)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
574*4882a593Smuzhiyun 	const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun static int
SMB3_request_interfaces(const unsigned int xid,struct cifs_tcon * tcon)580*4882a593Smuzhiyun SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	int rc;
583*4882a593Smuzhiyun 	unsigned int ret_data_len = 0;
584*4882a593Smuzhiyun 	struct network_interface_info_ioctl_rsp *out_buf = NULL;
585*4882a593Smuzhiyun 	struct cifs_server_iface *iface_list;
586*4882a593Smuzhiyun 	size_t iface_count;
587*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
590*4882a593Smuzhiyun 			FSCTL_QUERY_NETWORK_INTERFACE_INFO,
591*4882a593Smuzhiyun 			NULL /* no data input */, 0 /* no data input */,
592*4882a593Smuzhiyun 			CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
593*4882a593Smuzhiyun 	if (rc == -EOPNOTSUPP) {
594*4882a593Smuzhiyun 		cifs_dbg(FYI,
595*4882a593Smuzhiyun 			 "server does not support query network interfaces\n");
596*4882a593Smuzhiyun 		goto out;
597*4882a593Smuzhiyun 	} else if (rc != 0) {
598*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
599*4882a593Smuzhiyun 		goto out;
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	rc = parse_server_interfaces(out_buf, ret_data_len,
603*4882a593Smuzhiyun 				     &iface_list, &iface_count);
604*4882a593Smuzhiyun 	if (rc)
605*4882a593Smuzhiyun 		goto out;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	/* sort interfaces from fastest to slowest */
608*4882a593Smuzhiyun 	sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	spin_lock(&ses->iface_lock);
611*4882a593Smuzhiyun 	kfree(ses->iface_list);
612*4882a593Smuzhiyun 	ses->iface_list = iface_list;
613*4882a593Smuzhiyun 	ses->iface_count = iface_count;
614*4882a593Smuzhiyun 	ses->iface_last_update = jiffies;
615*4882a593Smuzhiyun 	spin_unlock(&ses->iface_lock);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun out:
618*4882a593Smuzhiyun 	kfree(out_buf);
619*4882a593Smuzhiyun 	return rc;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun static void
smb2_close_cached_fid(struct kref * ref)623*4882a593Smuzhiyun smb2_close_cached_fid(struct kref *ref)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun 	struct cached_fid *cfid = container_of(ref, struct cached_fid,
626*4882a593Smuzhiyun 					       refcount);
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	if (cfid->is_valid) {
629*4882a593Smuzhiyun 		cifs_dbg(FYI, "clear cached root file handle\n");
630*4882a593Smuzhiyun 		SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
631*4882a593Smuzhiyun 			   cfid->fid->volatile_fid);
632*4882a593Smuzhiyun 		cfid->is_valid = false;
633*4882a593Smuzhiyun 		cfid->file_all_info_is_valid = false;
634*4882a593Smuzhiyun 		cfid->has_lease = false;
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun 
close_shroot(struct cached_fid * cfid)638*4882a593Smuzhiyun void close_shroot(struct cached_fid *cfid)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	mutex_lock(&cfid->fid_mutex);
641*4882a593Smuzhiyun 	kref_put(&cfid->refcount, smb2_close_cached_fid);
642*4882a593Smuzhiyun 	mutex_unlock(&cfid->fid_mutex);
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
close_shroot_lease_locked(struct cached_fid * cfid)645*4882a593Smuzhiyun void close_shroot_lease_locked(struct cached_fid *cfid)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun 	if (cfid->has_lease) {
648*4882a593Smuzhiyun 		cfid->has_lease = false;
649*4882a593Smuzhiyun 		kref_put(&cfid->refcount, smb2_close_cached_fid);
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
close_shroot_lease(struct cached_fid * cfid)653*4882a593Smuzhiyun void close_shroot_lease(struct cached_fid *cfid)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	mutex_lock(&cfid->fid_mutex);
656*4882a593Smuzhiyun 	close_shroot_lease_locked(cfid);
657*4882a593Smuzhiyun 	mutex_unlock(&cfid->fid_mutex);
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun void
smb2_cached_lease_break(struct work_struct * work)661*4882a593Smuzhiyun smb2_cached_lease_break(struct work_struct *work)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun 	struct cached_fid *cfid = container_of(work,
664*4882a593Smuzhiyun 				struct cached_fid, lease_break);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	close_shroot_lease(cfid);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun /*
670*4882a593Smuzhiyun  * Open the directory at the root of a share
671*4882a593Smuzhiyun  */
open_shroot(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,struct cached_fid ** cfid)672*4882a593Smuzhiyun int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
673*4882a593Smuzhiyun 		struct cifs_sb_info *cifs_sb,
674*4882a593Smuzhiyun 		struct cached_fid **cfid)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
677*4882a593Smuzhiyun 	struct TCP_Server_Info *server = ses->server;
678*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
679*4882a593Smuzhiyun 	struct smb2_create_rsp *o_rsp = NULL;
680*4882a593Smuzhiyun 	struct smb2_query_info_rsp *qi_rsp = NULL;
681*4882a593Smuzhiyun 	int resp_buftype[2];
682*4882a593Smuzhiyun 	struct smb_rqst rqst[2];
683*4882a593Smuzhiyun 	struct kvec rsp_iov[2];
684*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
685*4882a593Smuzhiyun 	struct kvec qi_iov[1];
686*4882a593Smuzhiyun 	int rc, flags = 0;
687*4882a593Smuzhiyun 	__le16 utf16_path = 0; /* Null - since an open of top of share */
688*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_II;
689*4882a593Smuzhiyun 	struct cifs_fid *pfid;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	mutex_lock(&tcon->crfid.fid_mutex);
692*4882a593Smuzhiyun 	if (tcon->crfid.is_valid) {
693*4882a593Smuzhiyun 		cifs_dbg(FYI, "found a cached root file handle\n");
694*4882a593Smuzhiyun 		*cfid = &tcon->crfid;
695*4882a593Smuzhiyun 		kref_get(&tcon->crfid.refcount);
696*4882a593Smuzhiyun 		mutex_unlock(&tcon->crfid.fid_mutex);
697*4882a593Smuzhiyun 		return 0;
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	/*
701*4882a593Smuzhiyun 	 * We do not hold the lock for the open because in case
702*4882a593Smuzhiyun 	 * SMB2_open needs to reconnect, it will end up calling
703*4882a593Smuzhiyun 	 * cifs_mark_open_files_invalid() which takes the lock again
704*4882a593Smuzhiyun 	 * thus causing a deadlock
705*4882a593Smuzhiyun 	 */
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	mutex_unlock(&tcon->crfid.fid_mutex);
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
710*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	if (!server->ops->new_lease_key)
713*4882a593Smuzhiyun 		return -EIO;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	pfid = tcon->crfid.fid;
716*4882a593Smuzhiyun 	server->ops->new_lease_key(pfid);
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	memset(rqst, 0, sizeof(rqst));
719*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
720*4882a593Smuzhiyun 	memset(rsp_iov, 0, sizeof(rsp_iov));
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	/* Open */
723*4882a593Smuzhiyun 	memset(&open_iov, 0, sizeof(open_iov));
724*4882a593Smuzhiyun 	rqst[0].rq_iov = open_iov;
725*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	oparms.tcon = tcon;
728*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
729*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
730*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
731*4882a593Smuzhiyun 	oparms.fid = pfid;
732*4882a593Smuzhiyun 	oparms.reconnect = false;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
735*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, &utf16_path);
736*4882a593Smuzhiyun 	if (rc)
737*4882a593Smuzhiyun 		goto oshr_free;
738*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	memset(&qi_iov, 0, sizeof(qi_iov));
741*4882a593Smuzhiyun 	rqst[1].rq_iov = qi_iov;
742*4882a593Smuzhiyun 	rqst[1].rq_nvec = 1;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	rc = SMB2_query_info_init(tcon, server,
745*4882a593Smuzhiyun 				  &rqst[1], COMPOUND_FID,
746*4882a593Smuzhiyun 				  COMPOUND_FID, FILE_ALL_INFORMATION,
747*4882a593Smuzhiyun 				  SMB2_O_INFO_FILE, 0,
748*4882a593Smuzhiyun 				  sizeof(struct smb2_file_all_info) +
749*4882a593Smuzhiyun 				  PATH_MAX * 2, 0, NULL);
750*4882a593Smuzhiyun 	if (rc)
751*4882a593Smuzhiyun 		goto oshr_free;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	rc = compound_send_recv(xid, ses, server,
756*4882a593Smuzhiyun 				flags, 2, rqst,
757*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
758*4882a593Smuzhiyun 	mutex_lock(&tcon->crfid.fid_mutex);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/*
761*4882a593Smuzhiyun 	 * Now we need to check again as the cached root might have
762*4882a593Smuzhiyun 	 * been successfully re-opened from a concurrent process
763*4882a593Smuzhiyun 	 */
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	if (tcon->crfid.is_valid) {
766*4882a593Smuzhiyun 		/* work was already done */
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 		/* stash fids for close() later */
769*4882a593Smuzhiyun 		struct cifs_fid fid = {
770*4882a593Smuzhiyun 			.persistent_fid = pfid->persistent_fid,
771*4882a593Smuzhiyun 			.volatile_fid = pfid->volatile_fid,
772*4882a593Smuzhiyun 		};
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		/*
775*4882a593Smuzhiyun 		 * caller expects this func to set pfid to a valid
776*4882a593Smuzhiyun 		 * cached root, so we copy the existing one and get a
777*4882a593Smuzhiyun 		 * reference.
778*4882a593Smuzhiyun 		 */
779*4882a593Smuzhiyun 		memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
780*4882a593Smuzhiyun 		kref_get(&tcon->crfid.refcount);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 		mutex_unlock(&tcon->crfid.fid_mutex);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 		if (rc == 0) {
785*4882a593Smuzhiyun 			/* close extra handle outside of crit sec */
786*4882a593Smuzhiyun 			SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
787*4882a593Smuzhiyun 		}
788*4882a593Smuzhiyun 		rc = 0;
789*4882a593Smuzhiyun 		goto oshr_free;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* Cached root is still invalid, continue normaly */
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	if (rc) {
795*4882a593Smuzhiyun 		if (rc == -EREMCHG) {
796*4882a593Smuzhiyun 			tcon->need_reconnect = true;
797*4882a593Smuzhiyun 			pr_warn_once("server share %s deleted\n",
798*4882a593Smuzhiyun 				     tcon->treeName);
799*4882a593Smuzhiyun 		}
800*4882a593Smuzhiyun 		goto oshr_exit;
801*4882a593Smuzhiyun 	}
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	atomic_inc(&tcon->num_remote_opens);
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
806*4882a593Smuzhiyun 	oparms.fid->persistent_fid = o_rsp->PersistentFileId;
807*4882a593Smuzhiyun 	oparms.fid->volatile_fid = o_rsp->VolatileFileId;
808*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DEBUG2
809*4882a593Smuzhiyun 	oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
810*4882a593Smuzhiyun #endif /* CIFS_DEBUG2 */
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
813*4882a593Smuzhiyun 	tcon->crfid.tcon = tcon;
814*4882a593Smuzhiyun 	tcon->crfid.is_valid = true;
815*4882a593Smuzhiyun 	kref_init(&tcon->crfid.refcount);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	/* BB TBD check to see if oplock level check can be removed below */
818*4882a593Smuzhiyun 	if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
819*4882a593Smuzhiyun 		kref_get(&tcon->crfid.refcount);
820*4882a593Smuzhiyun 		tcon->crfid.has_lease = true;
821*4882a593Smuzhiyun 		smb2_parse_contexts(server, o_rsp,
822*4882a593Smuzhiyun 				&oparms.fid->epoch,
823*4882a593Smuzhiyun 				    oparms.fid->lease_key, &oplock,
824*4882a593Smuzhiyun 				    NULL, NULL);
825*4882a593Smuzhiyun 	} else
826*4882a593Smuzhiyun 		goto oshr_exit;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
829*4882a593Smuzhiyun 	if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
830*4882a593Smuzhiyun 		goto oshr_exit;
831*4882a593Smuzhiyun 	if (!smb2_validate_and_copy_iov(
832*4882a593Smuzhiyun 				le16_to_cpu(qi_rsp->OutputBufferOffset),
833*4882a593Smuzhiyun 				sizeof(struct smb2_file_all_info),
834*4882a593Smuzhiyun 				&rsp_iov[1], sizeof(struct smb2_file_all_info),
835*4882a593Smuzhiyun 				(char *)&tcon->crfid.file_all_info))
836*4882a593Smuzhiyun 		tcon->crfid.file_all_info_is_valid = true;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun oshr_exit:
839*4882a593Smuzhiyun 	mutex_unlock(&tcon->crfid.fid_mutex);
840*4882a593Smuzhiyun oshr_free:
841*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
842*4882a593Smuzhiyun 	SMB2_query_info_free(&rqst[1]);
843*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
844*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
845*4882a593Smuzhiyun 	if (rc == 0)
846*4882a593Smuzhiyun 		*cfid = &tcon->crfid;
847*4882a593Smuzhiyun 	return rc;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun static void
smb3_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb)851*4882a593Smuzhiyun smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
852*4882a593Smuzhiyun 	      struct cifs_sb_info *cifs_sb)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	int rc;
855*4882a593Smuzhiyun 	__le16 srch_path = 0; /* Null - open root of share */
856*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
857*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
858*4882a593Smuzhiyun 	struct cifs_fid fid;
859*4882a593Smuzhiyun 	bool no_cached_open = tcon->nohandlecache;
860*4882a593Smuzhiyun 	struct cached_fid *cfid = NULL;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	oparms.tcon = tcon;
863*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
864*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
865*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
866*4882a593Smuzhiyun 	oparms.fid = &fid;
867*4882a593Smuzhiyun 	oparms.reconnect = false;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	if (no_cached_open) {
870*4882a593Smuzhiyun 		rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
871*4882a593Smuzhiyun 			       NULL, NULL);
872*4882a593Smuzhiyun 	} else {
873*4882a593Smuzhiyun 		rc = open_shroot(xid, tcon, cifs_sb, &cfid);
874*4882a593Smuzhiyun 		if (rc == 0)
875*4882a593Smuzhiyun 			memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
876*4882a593Smuzhiyun 	}
877*4882a593Smuzhiyun 	if (rc)
878*4882a593Smuzhiyun 		return;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	SMB3_request_interfaces(xid, tcon);
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
883*4882a593Smuzhiyun 			FS_ATTRIBUTE_INFORMATION);
884*4882a593Smuzhiyun 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
885*4882a593Smuzhiyun 			FS_DEVICE_INFORMATION);
886*4882a593Smuzhiyun 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
887*4882a593Smuzhiyun 			FS_VOLUME_INFORMATION);
888*4882a593Smuzhiyun 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
889*4882a593Smuzhiyun 			FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
890*4882a593Smuzhiyun 	if (no_cached_open)
891*4882a593Smuzhiyun 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
892*4882a593Smuzhiyun 	else
893*4882a593Smuzhiyun 		close_shroot(cfid);
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun static void
smb2_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb)897*4882a593Smuzhiyun smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
898*4882a593Smuzhiyun 	      struct cifs_sb_info *cifs_sb)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	int rc;
901*4882a593Smuzhiyun 	__le16 srch_path = 0; /* Null - open root of share */
902*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
903*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
904*4882a593Smuzhiyun 	struct cifs_fid fid;
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	oparms.tcon = tcon;
907*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
908*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
909*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
910*4882a593Smuzhiyun 	oparms.fid = &fid;
911*4882a593Smuzhiyun 	oparms.reconnect = false;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
914*4882a593Smuzhiyun 		       NULL, NULL);
915*4882a593Smuzhiyun 	if (rc)
916*4882a593Smuzhiyun 		return;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
919*4882a593Smuzhiyun 			FS_ATTRIBUTE_INFORMATION);
920*4882a593Smuzhiyun 	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
921*4882a593Smuzhiyun 			FS_DEVICE_INFORMATION);
922*4882a593Smuzhiyun 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun static int
smb2_is_path_accessible(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path)926*4882a593Smuzhiyun smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
927*4882a593Smuzhiyun 			struct cifs_sb_info *cifs_sb, const char *full_path)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun 	int rc;
930*4882a593Smuzhiyun 	__le16 *utf16_path;
931*4882a593Smuzhiyun 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
932*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
933*4882a593Smuzhiyun 	struct cifs_fid fid;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	if ((*full_path == 0) && tcon->crfid.is_valid)
936*4882a593Smuzhiyun 		return 0;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
939*4882a593Smuzhiyun 	if (!utf16_path)
940*4882a593Smuzhiyun 		return -ENOMEM;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	oparms.tcon = tcon;
943*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
944*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
945*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
946*4882a593Smuzhiyun 	oparms.fid = &fid;
947*4882a593Smuzhiyun 	oparms.reconnect = false;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
950*4882a593Smuzhiyun 		       NULL);
951*4882a593Smuzhiyun 	if (rc) {
952*4882a593Smuzhiyun 		kfree(utf16_path);
953*4882a593Smuzhiyun 		return rc;
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
957*4882a593Smuzhiyun 	kfree(utf16_path);
958*4882a593Smuzhiyun 	return rc;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun static int
smb2_get_srv_inum(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,u64 * uniqueid,FILE_ALL_INFO * data)962*4882a593Smuzhiyun smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
963*4882a593Smuzhiyun 		  struct cifs_sb_info *cifs_sb, const char *full_path,
964*4882a593Smuzhiyun 		  u64 *uniqueid, FILE_ALL_INFO *data)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	*uniqueid = le64_to_cpu(data->IndexNumber);
967*4882a593Smuzhiyun 	return 0;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun static int
smb2_query_file_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid,FILE_ALL_INFO * data)971*4882a593Smuzhiyun smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
972*4882a593Smuzhiyun 		     struct cifs_fid *fid, FILE_ALL_INFO *data)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun 	int rc;
975*4882a593Smuzhiyun 	struct smb2_file_all_info *smb2_data;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
978*4882a593Smuzhiyun 			    GFP_KERNEL);
979*4882a593Smuzhiyun 	if (smb2_data == NULL)
980*4882a593Smuzhiyun 		return -ENOMEM;
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
983*4882a593Smuzhiyun 			     smb2_data);
984*4882a593Smuzhiyun 	if (!rc)
985*4882a593Smuzhiyun 		move_smb2_info_to_cifs(data, smb2_data);
986*4882a593Smuzhiyun 	kfree(smb2_data);
987*4882a593Smuzhiyun 	return rc;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun #ifdef CONFIG_CIFS_XATTR
991*4882a593Smuzhiyun static ssize_t
move_smb2_ea_to_cifs(char * dst,size_t dst_size,struct smb2_file_full_ea_info * src,size_t src_size,const unsigned char * ea_name)992*4882a593Smuzhiyun move_smb2_ea_to_cifs(char *dst, size_t dst_size,
993*4882a593Smuzhiyun 		     struct smb2_file_full_ea_info *src, size_t src_size,
994*4882a593Smuzhiyun 		     const unsigned char *ea_name)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun 	int rc = 0;
997*4882a593Smuzhiyun 	unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
998*4882a593Smuzhiyun 	char *name, *value;
999*4882a593Smuzhiyun 	size_t buf_size = dst_size;
1000*4882a593Smuzhiyun 	size_t name_len, value_len, user_name_len;
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	while (src_size > 0) {
1003*4882a593Smuzhiyun 		name_len = (size_t)src->ea_name_length;
1004*4882a593Smuzhiyun 		value_len = (size_t)le16_to_cpu(src->ea_value_length);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 		if (name_len == 0)
1007*4882a593Smuzhiyun 			break;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 		if (src_size < 8 + name_len + 1 + value_len) {
1010*4882a593Smuzhiyun 			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1011*4882a593Smuzhiyun 			rc = -EIO;
1012*4882a593Smuzhiyun 			goto out;
1013*4882a593Smuzhiyun 		}
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 		name = &src->ea_data[0];
1016*4882a593Smuzhiyun 		value = &src->ea_data[src->ea_name_length + 1];
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 		if (ea_name) {
1019*4882a593Smuzhiyun 			if (ea_name_len == name_len &&
1020*4882a593Smuzhiyun 			    memcmp(ea_name, name, name_len) == 0) {
1021*4882a593Smuzhiyun 				rc = value_len;
1022*4882a593Smuzhiyun 				if (dst_size == 0)
1023*4882a593Smuzhiyun 					goto out;
1024*4882a593Smuzhiyun 				if (dst_size < value_len) {
1025*4882a593Smuzhiyun 					rc = -ERANGE;
1026*4882a593Smuzhiyun 					goto out;
1027*4882a593Smuzhiyun 				}
1028*4882a593Smuzhiyun 				memcpy(dst, value, value_len);
1029*4882a593Smuzhiyun 				goto out;
1030*4882a593Smuzhiyun 			}
1031*4882a593Smuzhiyun 		} else {
1032*4882a593Smuzhiyun 			/* 'user.' plus a terminating null */
1033*4882a593Smuzhiyun 			user_name_len = 5 + 1 + name_len;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 			if (buf_size == 0) {
1036*4882a593Smuzhiyun 				/* skip copy - calc size only */
1037*4882a593Smuzhiyun 				rc += user_name_len;
1038*4882a593Smuzhiyun 			} else if (dst_size >= user_name_len) {
1039*4882a593Smuzhiyun 				dst_size -= user_name_len;
1040*4882a593Smuzhiyun 				memcpy(dst, "user.", 5);
1041*4882a593Smuzhiyun 				dst += 5;
1042*4882a593Smuzhiyun 				memcpy(dst, src->ea_data, name_len);
1043*4882a593Smuzhiyun 				dst += name_len;
1044*4882a593Smuzhiyun 				*dst = 0;
1045*4882a593Smuzhiyun 				++dst;
1046*4882a593Smuzhiyun 				rc += user_name_len;
1047*4882a593Smuzhiyun 			} else {
1048*4882a593Smuzhiyun 				/* stop before overrun buffer */
1049*4882a593Smuzhiyun 				rc = -ERANGE;
1050*4882a593Smuzhiyun 				break;
1051*4882a593Smuzhiyun 			}
1052*4882a593Smuzhiyun 		}
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 		if (!src->next_entry_offset)
1055*4882a593Smuzhiyun 			break;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 		if (src_size < le32_to_cpu(src->next_entry_offset)) {
1058*4882a593Smuzhiyun 			/* stop before overrun buffer */
1059*4882a593Smuzhiyun 			rc = -ERANGE;
1060*4882a593Smuzhiyun 			break;
1061*4882a593Smuzhiyun 		}
1062*4882a593Smuzhiyun 		src_size -= le32_to_cpu(src->next_entry_offset);
1063*4882a593Smuzhiyun 		src = (void *)((char *)src +
1064*4882a593Smuzhiyun 			       le32_to_cpu(src->next_entry_offset));
1065*4882a593Smuzhiyun 	}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	/* didn't find the named attribute */
1068*4882a593Smuzhiyun 	if (ea_name)
1069*4882a593Smuzhiyun 		rc = -ENODATA;
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun out:
1072*4882a593Smuzhiyun 	return (ssize_t)rc;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun static ssize_t
smb2_query_eas(const unsigned int xid,struct cifs_tcon * tcon,const unsigned char * path,const unsigned char * ea_name,char * ea_data,size_t buf_size,struct cifs_sb_info * cifs_sb)1076*4882a593Smuzhiyun smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1077*4882a593Smuzhiyun 	       const unsigned char *path, const unsigned char *ea_name,
1078*4882a593Smuzhiyun 	       char *ea_data, size_t buf_size,
1079*4882a593Smuzhiyun 	       struct cifs_sb_info *cifs_sb)
1080*4882a593Smuzhiyun {
1081*4882a593Smuzhiyun 	int rc;
1082*4882a593Smuzhiyun 	__le16 *utf16_path;
1083*4882a593Smuzhiyun 	struct kvec rsp_iov = {NULL, 0};
1084*4882a593Smuzhiyun 	int buftype = CIFS_NO_BUFFER;
1085*4882a593Smuzhiyun 	struct smb2_query_info_rsp *rsp;
1086*4882a593Smuzhiyun 	struct smb2_file_full_ea_info *info = NULL;
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1089*4882a593Smuzhiyun 	if (!utf16_path)
1090*4882a593Smuzhiyun 		return -ENOMEM;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	rc = smb2_query_info_compound(xid, tcon, utf16_path,
1093*4882a593Smuzhiyun 				      FILE_READ_EA,
1094*4882a593Smuzhiyun 				      FILE_FULL_EA_INFORMATION,
1095*4882a593Smuzhiyun 				      SMB2_O_INFO_FILE,
1096*4882a593Smuzhiyun 				      CIFSMaxBufSize -
1097*4882a593Smuzhiyun 				      MAX_SMB2_CREATE_RESPONSE_SIZE -
1098*4882a593Smuzhiyun 				      MAX_SMB2_CLOSE_RESPONSE_SIZE,
1099*4882a593Smuzhiyun 				      &rsp_iov, &buftype, cifs_sb);
1100*4882a593Smuzhiyun 	if (rc) {
1101*4882a593Smuzhiyun 		/*
1102*4882a593Smuzhiyun 		 * If ea_name is NULL (listxattr) and there are no EAs,
1103*4882a593Smuzhiyun 		 * return 0 as it's not an error. Otherwise, the specified
1104*4882a593Smuzhiyun 		 * ea_name was not found.
1105*4882a593Smuzhiyun 		 */
1106*4882a593Smuzhiyun 		if (!ea_name && rc == -ENODATA)
1107*4882a593Smuzhiyun 			rc = 0;
1108*4882a593Smuzhiyun 		goto qeas_exit;
1109*4882a593Smuzhiyun 	}
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1112*4882a593Smuzhiyun 	rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1113*4882a593Smuzhiyun 			       le32_to_cpu(rsp->OutputBufferLength),
1114*4882a593Smuzhiyun 			       &rsp_iov,
1115*4882a593Smuzhiyun 			       sizeof(struct smb2_file_full_ea_info));
1116*4882a593Smuzhiyun 	if (rc)
1117*4882a593Smuzhiyun 		goto qeas_exit;
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	info = (struct smb2_file_full_ea_info *)(
1120*4882a593Smuzhiyun 			le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1121*4882a593Smuzhiyun 	rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1122*4882a593Smuzhiyun 			le32_to_cpu(rsp->OutputBufferLength), ea_name);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun  qeas_exit:
1125*4882a593Smuzhiyun 	kfree(utf16_path);
1126*4882a593Smuzhiyun 	free_rsp_buf(buftype, rsp_iov.iov_base);
1127*4882a593Smuzhiyun 	return rc;
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun static int
smb2_set_ea(const unsigned int xid,struct cifs_tcon * tcon,const char * path,const char * ea_name,const void * ea_value,const __u16 ea_value_len,const struct nls_table * nls_codepage,struct cifs_sb_info * cifs_sb)1132*4882a593Smuzhiyun smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1133*4882a593Smuzhiyun 	    const char *path, const char *ea_name, const void *ea_value,
1134*4882a593Smuzhiyun 	    const __u16 ea_value_len, const struct nls_table *nls_codepage,
1135*4882a593Smuzhiyun 	    struct cifs_sb_info *cifs_sb)
1136*4882a593Smuzhiyun {
1137*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
1138*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_pick_channel(ses);
1139*4882a593Smuzhiyun 	__le16 *utf16_path = NULL;
1140*4882a593Smuzhiyun 	int ea_name_len = strlen(ea_name);
1141*4882a593Smuzhiyun 	int flags = CIFS_CP_CREATE_CLOSE_OP;
1142*4882a593Smuzhiyun 	int len;
1143*4882a593Smuzhiyun 	struct smb_rqst rqst[3];
1144*4882a593Smuzhiyun 	int resp_buftype[3];
1145*4882a593Smuzhiyun 	struct kvec rsp_iov[3];
1146*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1147*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
1148*4882a593Smuzhiyun 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1149*4882a593Smuzhiyun 	struct cifs_fid fid;
1150*4882a593Smuzhiyun 	struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1151*4882a593Smuzhiyun 	unsigned int size[1];
1152*4882a593Smuzhiyun 	void *data[1];
1153*4882a593Smuzhiyun 	struct smb2_file_full_ea_info *ea = NULL;
1154*4882a593Smuzhiyun 	struct kvec close_iov[1];
1155*4882a593Smuzhiyun 	struct smb2_query_info_rsp *rsp;
1156*4882a593Smuzhiyun 	int rc, used_len = 0;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
1159*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	if (ea_name_len > 255)
1162*4882a593Smuzhiyun 		return -EINVAL;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1165*4882a593Smuzhiyun 	if (!utf16_path)
1166*4882a593Smuzhiyun 		return -ENOMEM;
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	memset(rqst, 0, sizeof(rqst));
1169*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1170*4882a593Smuzhiyun 	memset(rsp_iov, 0, sizeof(rsp_iov));
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	if (ses->server->ops->query_all_EAs) {
1173*4882a593Smuzhiyun 		if (!ea_value) {
1174*4882a593Smuzhiyun 			rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1175*4882a593Smuzhiyun 							     ea_name, NULL, 0,
1176*4882a593Smuzhiyun 							     cifs_sb);
1177*4882a593Smuzhiyun 			if (rc == -ENODATA)
1178*4882a593Smuzhiyun 				goto sea_exit;
1179*4882a593Smuzhiyun 		} else {
1180*4882a593Smuzhiyun 			/* If we are adding a attribute we should first check
1181*4882a593Smuzhiyun 			 * if there will be enough space available to store
1182*4882a593Smuzhiyun 			 * the new EA. If not we should not add it since we
1183*4882a593Smuzhiyun 			 * would not be able to even read the EAs back.
1184*4882a593Smuzhiyun 			 */
1185*4882a593Smuzhiyun 			rc = smb2_query_info_compound(xid, tcon, utf16_path,
1186*4882a593Smuzhiyun 				      FILE_READ_EA,
1187*4882a593Smuzhiyun 				      FILE_FULL_EA_INFORMATION,
1188*4882a593Smuzhiyun 				      SMB2_O_INFO_FILE,
1189*4882a593Smuzhiyun 				      CIFSMaxBufSize -
1190*4882a593Smuzhiyun 				      MAX_SMB2_CREATE_RESPONSE_SIZE -
1191*4882a593Smuzhiyun 				      MAX_SMB2_CLOSE_RESPONSE_SIZE,
1192*4882a593Smuzhiyun 				      &rsp_iov[1], &resp_buftype[1], cifs_sb);
1193*4882a593Smuzhiyun 			if (rc == 0) {
1194*4882a593Smuzhiyun 				rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1195*4882a593Smuzhiyun 				used_len = le32_to_cpu(rsp->OutputBufferLength);
1196*4882a593Smuzhiyun 			}
1197*4882a593Smuzhiyun 			free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1198*4882a593Smuzhiyun 			resp_buftype[1] = CIFS_NO_BUFFER;
1199*4882a593Smuzhiyun 			memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1200*4882a593Smuzhiyun 			rc = 0;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 			/* Use a fudge factor of 256 bytes in case we collide
1203*4882a593Smuzhiyun 			 * with a different set_EAs command.
1204*4882a593Smuzhiyun 			 */
1205*4882a593Smuzhiyun 			if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1206*4882a593Smuzhiyun 			   MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1207*4882a593Smuzhiyun 			   used_len + ea_name_len + ea_value_len + 1) {
1208*4882a593Smuzhiyun 				rc = -ENOSPC;
1209*4882a593Smuzhiyun 				goto sea_exit;
1210*4882a593Smuzhiyun 			}
1211*4882a593Smuzhiyun 		}
1212*4882a593Smuzhiyun 	}
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	/* Open */
1215*4882a593Smuzhiyun 	memset(&open_iov, 0, sizeof(open_iov));
1216*4882a593Smuzhiyun 	rqst[0].rq_iov = open_iov;
1217*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	memset(&oparms, 0, sizeof(oparms));
1220*4882a593Smuzhiyun 	oparms.tcon = tcon;
1221*4882a593Smuzhiyun 	oparms.desired_access = FILE_WRITE_EA;
1222*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
1223*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
1224*4882a593Smuzhiyun 	oparms.fid = &fid;
1225*4882a593Smuzhiyun 	oparms.reconnect = false;
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
1228*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, utf16_path);
1229*4882a593Smuzhiyun 	if (rc)
1230*4882a593Smuzhiyun 		goto sea_exit;
1231*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/* Set Info */
1235*4882a593Smuzhiyun 	memset(&si_iov, 0, sizeof(si_iov));
1236*4882a593Smuzhiyun 	rqst[1].rq_iov = si_iov;
1237*4882a593Smuzhiyun 	rqst[1].rq_nvec = 1;
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1240*4882a593Smuzhiyun 	ea = kzalloc(len, GFP_KERNEL);
1241*4882a593Smuzhiyun 	if (ea == NULL) {
1242*4882a593Smuzhiyun 		rc = -ENOMEM;
1243*4882a593Smuzhiyun 		goto sea_exit;
1244*4882a593Smuzhiyun 	}
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	ea->ea_name_length = ea_name_len;
1247*4882a593Smuzhiyun 	ea->ea_value_length = cpu_to_le16(ea_value_len);
1248*4882a593Smuzhiyun 	memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1249*4882a593Smuzhiyun 	memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	size[0] = len;
1252*4882a593Smuzhiyun 	data[0] = ea;
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	rc = SMB2_set_info_init(tcon, server,
1255*4882a593Smuzhiyun 				&rqst[1], COMPOUND_FID,
1256*4882a593Smuzhiyun 				COMPOUND_FID, current->tgid,
1257*4882a593Smuzhiyun 				FILE_FULL_EA_INFORMATION,
1258*4882a593Smuzhiyun 				SMB2_O_INFO_FILE, 0, data, size);
1259*4882a593Smuzhiyun 	if (rc)
1260*4882a593Smuzhiyun 		goto sea_exit;
1261*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[1]);
1262*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	/* Close */
1266*4882a593Smuzhiyun 	memset(&close_iov, 0, sizeof(close_iov));
1267*4882a593Smuzhiyun 	rqst[2].rq_iov = close_iov;
1268*4882a593Smuzhiyun 	rqst[2].rq_nvec = 1;
1269*4882a593Smuzhiyun 	rc = SMB2_close_init(tcon, server,
1270*4882a593Smuzhiyun 			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1271*4882a593Smuzhiyun 	if (rc)
1272*4882a593Smuzhiyun 		goto sea_exit;
1273*4882a593Smuzhiyun 	smb2_set_related(&rqst[2]);
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	rc = compound_send_recv(xid, ses, server,
1276*4882a593Smuzhiyun 				flags, 3, rqst,
1277*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
1278*4882a593Smuzhiyun 	/* no need to bump num_remote_opens because handle immediately closed */
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun  sea_exit:
1281*4882a593Smuzhiyun 	kfree(ea);
1282*4882a593Smuzhiyun 	kfree(utf16_path);
1283*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
1284*4882a593Smuzhiyun 	SMB2_set_info_free(&rqst[1]);
1285*4882a593Smuzhiyun 	SMB2_close_free(&rqst[2]);
1286*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1287*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1288*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1289*4882a593Smuzhiyun 	return rc;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun #endif
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun static bool
smb2_can_echo(struct TCP_Server_Info * server)1294*4882a593Smuzhiyun smb2_can_echo(struct TCP_Server_Info *server)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	return server->echoes;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun static void
smb2_clear_stats(struct cifs_tcon * tcon)1300*4882a593Smuzhiyun smb2_clear_stats(struct cifs_tcon *tcon)
1301*4882a593Smuzhiyun {
1302*4882a593Smuzhiyun 	int i;
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1305*4882a593Smuzhiyun 		atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1306*4882a593Smuzhiyun 		atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1307*4882a593Smuzhiyun 	}
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun static void
smb2_dump_share_caps(struct seq_file * m,struct cifs_tcon * tcon)1311*4882a593Smuzhiyun smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	seq_puts(m, "\n\tShare Capabilities:");
1314*4882a593Smuzhiyun 	if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1315*4882a593Smuzhiyun 		seq_puts(m, " DFS,");
1316*4882a593Smuzhiyun 	if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1317*4882a593Smuzhiyun 		seq_puts(m, " CONTINUOUS AVAILABILITY,");
1318*4882a593Smuzhiyun 	if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1319*4882a593Smuzhiyun 		seq_puts(m, " SCALEOUT,");
1320*4882a593Smuzhiyun 	if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1321*4882a593Smuzhiyun 		seq_puts(m, " CLUSTER,");
1322*4882a593Smuzhiyun 	if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1323*4882a593Smuzhiyun 		seq_puts(m, " ASYMMETRIC,");
1324*4882a593Smuzhiyun 	if (tcon->capabilities == 0)
1325*4882a593Smuzhiyun 		seq_puts(m, " None");
1326*4882a593Smuzhiyun 	if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1327*4882a593Smuzhiyun 		seq_puts(m, " Aligned,");
1328*4882a593Smuzhiyun 	if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1329*4882a593Smuzhiyun 		seq_puts(m, " Partition Aligned,");
1330*4882a593Smuzhiyun 	if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1331*4882a593Smuzhiyun 		seq_puts(m, " SSD,");
1332*4882a593Smuzhiyun 	if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1333*4882a593Smuzhiyun 		seq_puts(m, " TRIM-support,");
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1336*4882a593Smuzhiyun 	seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1337*4882a593Smuzhiyun 	if (tcon->perf_sector_size)
1338*4882a593Smuzhiyun 		seq_printf(m, "\tOptimal sector size: 0x%x",
1339*4882a593Smuzhiyun 			   tcon->perf_sector_size);
1340*4882a593Smuzhiyun 	seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun static void
smb2_print_stats(struct seq_file * m,struct cifs_tcon * tcon)1344*4882a593Smuzhiyun smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1345*4882a593Smuzhiyun {
1346*4882a593Smuzhiyun 	atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1347*4882a593Smuzhiyun 	atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	/*
1350*4882a593Smuzhiyun 	 *  Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1351*4882a593Smuzhiyun 	 *  totals (requests sent) since those SMBs are per-session not per tcon
1352*4882a593Smuzhiyun 	 */
1353*4882a593Smuzhiyun 	seq_printf(m, "\nBytes read: %llu  Bytes written: %llu",
1354*4882a593Smuzhiyun 		   (long long)(tcon->bytes_read),
1355*4882a593Smuzhiyun 		   (long long)(tcon->bytes_written));
1356*4882a593Smuzhiyun 	seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1357*4882a593Smuzhiyun 		   atomic_read(&tcon->num_local_opens),
1358*4882a593Smuzhiyun 		   atomic_read(&tcon->num_remote_opens));
1359*4882a593Smuzhiyun 	seq_printf(m, "\nTreeConnects: %d total %d failed",
1360*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1361*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1362*4882a593Smuzhiyun 	seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1363*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1364*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1365*4882a593Smuzhiyun 	seq_printf(m, "\nCreates: %d total %d failed",
1366*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_CREATE_HE]),
1367*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_CREATE_HE]));
1368*4882a593Smuzhiyun 	seq_printf(m, "\nCloses: %d total %d failed",
1369*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_CLOSE_HE]),
1370*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_CLOSE_HE]));
1371*4882a593Smuzhiyun 	seq_printf(m, "\nFlushes: %d total %d failed",
1372*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_FLUSH_HE]),
1373*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_FLUSH_HE]));
1374*4882a593Smuzhiyun 	seq_printf(m, "\nReads: %d total %d failed",
1375*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_READ_HE]),
1376*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_READ_HE]));
1377*4882a593Smuzhiyun 	seq_printf(m, "\nWrites: %d total %d failed",
1378*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_WRITE_HE]),
1379*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_WRITE_HE]));
1380*4882a593Smuzhiyun 	seq_printf(m, "\nLocks: %d total %d failed",
1381*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_LOCK_HE]),
1382*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_LOCK_HE]));
1383*4882a593Smuzhiyun 	seq_printf(m, "\nIOCTLs: %d total %d failed",
1384*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_IOCTL_HE]),
1385*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_IOCTL_HE]));
1386*4882a593Smuzhiyun 	seq_printf(m, "\nQueryDirectories: %d total %d failed",
1387*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1388*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1389*4882a593Smuzhiyun 	seq_printf(m, "\nChangeNotifies: %d total %d failed",
1390*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1391*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1392*4882a593Smuzhiyun 	seq_printf(m, "\nQueryInfos: %d total %d failed",
1393*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1394*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1395*4882a593Smuzhiyun 	seq_printf(m, "\nSetInfos: %d total %d failed",
1396*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_SET_INFO_HE]),
1397*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_SET_INFO_HE]));
1398*4882a593Smuzhiyun 	seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1399*4882a593Smuzhiyun 		   atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1400*4882a593Smuzhiyun 		   atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1401*4882a593Smuzhiyun }
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun static void
smb2_set_fid(struct cifsFileInfo * cfile,struct cifs_fid * fid,__u32 oplock)1404*4882a593Smuzhiyun smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1407*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	cfile->fid.persistent_fid = fid->persistent_fid;
1410*4882a593Smuzhiyun 	cfile->fid.volatile_fid = fid->volatile_fid;
1411*4882a593Smuzhiyun 	cfile->fid.access = fid->access;
1412*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DEBUG2
1413*4882a593Smuzhiyun 	cfile->fid.mid = fid->mid;
1414*4882a593Smuzhiyun #endif /* CIFS_DEBUG2 */
1415*4882a593Smuzhiyun 	server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1416*4882a593Smuzhiyun 				      &fid->purge_cache);
1417*4882a593Smuzhiyun 	cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1418*4882a593Smuzhiyun 	memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun static void
smb2_close_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1422*4882a593Smuzhiyun smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1423*4882a593Smuzhiyun 		struct cifs_fid *fid)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1426*4882a593Smuzhiyun }
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun static void
smb2_close_getattr(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)1429*4882a593Smuzhiyun smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1430*4882a593Smuzhiyun 		   struct cifsFileInfo *cfile)
1431*4882a593Smuzhiyun {
1432*4882a593Smuzhiyun 	struct smb2_file_network_open_info file_inf;
1433*4882a593Smuzhiyun 	struct inode *inode;
1434*4882a593Smuzhiyun 	int rc;
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 	rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1437*4882a593Smuzhiyun 		   cfile->fid.volatile_fid, &file_inf);
1438*4882a593Smuzhiyun 	if (rc)
1439*4882a593Smuzhiyun 		return;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	inode = d_inode(cfile->dentry);
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	spin_lock(&inode->i_lock);
1444*4882a593Smuzhiyun 	CIFS_I(inode)->time = jiffies;
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 	/* Creation time should not need to be updated on close */
1447*4882a593Smuzhiyun 	if (file_inf.LastWriteTime)
1448*4882a593Smuzhiyun 		inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1449*4882a593Smuzhiyun 	if (file_inf.ChangeTime)
1450*4882a593Smuzhiyun 		inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1451*4882a593Smuzhiyun 	if (file_inf.LastAccessTime)
1452*4882a593Smuzhiyun 		inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 	/*
1455*4882a593Smuzhiyun 	 * i_blocks is not related to (i_size / i_blksize),
1456*4882a593Smuzhiyun 	 * but instead 512 byte (2**9) size is required for
1457*4882a593Smuzhiyun 	 * calculating num blocks.
1458*4882a593Smuzhiyun 	 */
1459*4882a593Smuzhiyun 	if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1460*4882a593Smuzhiyun 		inode->i_blocks =
1461*4882a593Smuzhiyun 			(512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	/* End of file and Attributes should not have to be updated on close */
1464*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun static int
SMB2_request_res_key(const unsigned int xid,struct cifs_tcon * tcon,u64 persistent_fid,u64 volatile_fid,struct copychunk_ioctl * pcchunk)1468*4882a593Smuzhiyun SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1469*4882a593Smuzhiyun 		     u64 persistent_fid, u64 volatile_fid,
1470*4882a593Smuzhiyun 		     struct copychunk_ioctl *pcchunk)
1471*4882a593Smuzhiyun {
1472*4882a593Smuzhiyun 	int rc;
1473*4882a593Smuzhiyun 	unsigned int ret_data_len;
1474*4882a593Smuzhiyun 	struct resume_key_req *res_key;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1477*4882a593Smuzhiyun 			FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
1478*4882a593Smuzhiyun 			CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	if (rc) {
1481*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1482*4882a593Smuzhiyun 		goto req_res_key_exit;
1483*4882a593Smuzhiyun 	}
1484*4882a593Smuzhiyun 	if (ret_data_len < sizeof(struct resume_key_req)) {
1485*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1486*4882a593Smuzhiyun 		rc = -EINVAL;
1487*4882a593Smuzhiyun 		goto req_res_key_exit;
1488*4882a593Smuzhiyun 	}
1489*4882a593Smuzhiyun 	memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun req_res_key_exit:
1492*4882a593Smuzhiyun 	kfree(res_key);
1493*4882a593Smuzhiyun 	return rc;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun struct iqi_vars {
1497*4882a593Smuzhiyun 	struct smb_rqst rqst[3];
1498*4882a593Smuzhiyun 	struct kvec rsp_iov[3];
1499*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1500*4882a593Smuzhiyun 	struct kvec qi_iov[1];
1501*4882a593Smuzhiyun 	struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1502*4882a593Smuzhiyun 	struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1503*4882a593Smuzhiyun 	struct kvec close_iov[1];
1504*4882a593Smuzhiyun };
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun static int
smb2_ioctl_query_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,__le16 * path,int is_dir,unsigned long p)1507*4882a593Smuzhiyun smb2_ioctl_query_info(const unsigned int xid,
1508*4882a593Smuzhiyun 		      struct cifs_tcon *tcon,
1509*4882a593Smuzhiyun 		      struct cifs_sb_info *cifs_sb,
1510*4882a593Smuzhiyun 		      __le16 *path, int is_dir,
1511*4882a593Smuzhiyun 		      unsigned long p)
1512*4882a593Smuzhiyun {
1513*4882a593Smuzhiyun 	struct iqi_vars *vars;
1514*4882a593Smuzhiyun 	struct smb_rqst *rqst;
1515*4882a593Smuzhiyun 	struct kvec *rsp_iov;
1516*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
1517*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_pick_channel(ses);
1518*4882a593Smuzhiyun 	char __user *arg = (char __user *)p;
1519*4882a593Smuzhiyun 	struct smb_query_info qi;
1520*4882a593Smuzhiyun 	struct smb_query_info __user *pqi;
1521*4882a593Smuzhiyun 	int rc = 0;
1522*4882a593Smuzhiyun 	int flags = CIFS_CP_CREATE_CLOSE_OP;
1523*4882a593Smuzhiyun 	struct smb2_query_info_rsp *qi_rsp = NULL;
1524*4882a593Smuzhiyun 	struct smb2_ioctl_rsp *io_rsp = NULL;
1525*4882a593Smuzhiyun 	void *buffer = NULL;
1526*4882a593Smuzhiyun 	int resp_buftype[3];
1527*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
1528*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1529*4882a593Smuzhiyun 	struct cifs_fid fid;
1530*4882a593Smuzhiyun 	unsigned int size[2];
1531*4882a593Smuzhiyun 	void *data[2];
1532*4882a593Smuzhiyun 	int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1533*4882a593Smuzhiyun 	void (*free_req1_func)(struct smb_rqst *r);
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun 	vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1536*4882a593Smuzhiyun 	if (vars == NULL)
1537*4882a593Smuzhiyun 		return -ENOMEM;
1538*4882a593Smuzhiyun 	rqst = &vars->rqst[0];
1539*4882a593Smuzhiyun 	rsp_iov = &vars->rsp_iov[0];
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1544*4882a593Smuzhiyun 		rc = -EFAULT;
1545*4882a593Smuzhiyun 		goto free_vars;
1546*4882a593Smuzhiyun 	}
1547*4882a593Smuzhiyun 	if (qi.output_buffer_length > 1024) {
1548*4882a593Smuzhiyun 		rc = -EINVAL;
1549*4882a593Smuzhiyun 		goto free_vars;
1550*4882a593Smuzhiyun 	}
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 	if (!ses || !server) {
1553*4882a593Smuzhiyun 		rc = -EIO;
1554*4882a593Smuzhiyun 		goto free_vars;
1555*4882a593Smuzhiyun 	}
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
1558*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	if (qi.output_buffer_length) {
1561*4882a593Smuzhiyun 		buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1562*4882a593Smuzhiyun 		if (IS_ERR(buffer)) {
1563*4882a593Smuzhiyun 			rc = PTR_ERR(buffer);
1564*4882a593Smuzhiyun 			goto free_vars;
1565*4882a593Smuzhiyun 		}
1566*4882a593Smuzhiyun 	}
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	/* Open */
1569*4882a593Smuzhiyun 	rqst[0].rq_iov = &vars->open_iov[0];
1570*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 	memset(&oparms, 0, sizeof(oparms));
1573*4882a593Smuzhiyun 	oparms.tcon = tcon;
1574*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
1575*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, create_options);
1576*4882a593Smuzhiyun 	oparms.fid = &fid;
1577*4882a593Smuzhiyun 	oparms.reconnect = false;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	if (qi.flags & PASSTHRU_FSCTL) {
1580*4882a593Smuzhiyun 		switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1581*4882a593Smuzhiyun 		case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1582*4882a593Smuzhiyun 			oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1583*4882a593Smuzhiyun 			break;
1584*4882a593Smuzhiyun 		case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1585*4882a593Smuzhiyun 			oparms.desired_access = GENERIC_ALL;
1586*4882a593Smuzhiyun 			break;
1587*4882a593Smuzhiyun 		case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1588*4882a593Smuzhiyun 			oparms.desired_access = GENERIC_READ;
1589*4882a593Smuzhiyun 			break;
1590*4882a593Smuzhiyun 		case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1591*4882a593Smuzhiyun 			oparms.desired_access = GENERIC_WRITE;
1592*4882a593Smuzhiyun 			break;
1593*4882a593Smuzhiyun 		}
1594*4882a593Smuzhiyun 	} else if (qi.flags & PASSTHRU_SET_INFO) {
1595*4882a593Smuzhiyun 		oparms.desired_access = GENERIC_WRITE;
1596*4882a593Smuzhiyun 	} else {
1597*4882a593Smuzhiyun 		oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1598*4882a593Smuzhiyun 	}
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
1601*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, path);
1602*4882a593Smuzhiyun 	if (rc)
1603*4882a593Smuzhiyun 		goto free_output_buffer;
1604*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	/* Query */
1607*4882a593Smuzhiyun 	if (qi.flags & PASSTHRU_FSCTL) {
1608*4882a593Smuzhiyun 		/* Can eventually relax perm check since server enforces too */
1609*4882a593Smuzhiyun 		if (!capable(CAP_SYS_ADMIN)) {
1610*4882a593Smuzhiyun 			rc = -EPERM;
1611*4882a593Smuzhiyun 			goto free_open_req;
1612*4882a593Smuzhiyun 		}
1613*4882a593Smuzhiyun 		rqst[1].rq_iov = &vars->io_iov[0];
1614*4882a593Smuzhiyun 		rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 		rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1617*4882a593Smuzhiyun 				     qi.info_type, buffer, qi.output_buffer_length,
1618*4882a593Smuzhiyun 				     CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1619*4882a593Smuzhiyun 				     MAX_SMB2_CLOSE_RESPONSE_SIZE);
1620*4882a593Smuzhiyun 		free_req1_func = SMB2_ioctl_free;
1621*4882a593Smuzhiyun 	} else if (qi.flags == PASSTHRU_SET_INFO) {
1622*4882a593Smuzhiyun 		/* Can eventually relax perm check since server enforces too */
1623*4882a593Smuzhiyun 		if (!capable(CAP_SYS_ADMIN)) {
1624*4882a593Smuzhiyun 			rc = -EPERM;
1625*4882a593Smuzhiyun 			goto free_open_req;
1626*4882a593Smuzhiyun 		}
1627*4882a593Smuzhiyun 		if (qi.output_buffer_length < 8) {
1628*4882a593Smuzhiyun 			rc = -EINVAL;
1629*4882a593Smuzhiyun 			goto free_open_req;
1630*4882a593Smuzhiyun 		}
1631*4882a593Smuzhiyun 		rqst[1].rq_iov = &vars->si_iov[0];
1632*4882a593Smuzhiyun 		rqst[1].rq_nvec = 1;
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 		/* MS-FSCC 2.4.13 FileEndOfFileInformation */
1635*4882a593Smuzhiyun 		size[0] = 8;
1636*4882a593Smuzhiyun 		data[0] = buffer;
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 		rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1639*4882a593Smuzhiyun 					current->tgid, FILE_END_OF_FILE_INFORMATION,
1640*4882a593Smuzhiyun 					SMB2_O_INFO_FILE, 0, data, size);
1641*4882a593Smuzhiyun 		free_req1_func = SMB2_set_info_free;
1642*4882a593Smuzhiyun 	} else if (qi.flags == PASSTHRU_QUERY_INFO) {
1643*4882a593Smuzhiyun 		rqst[1].rq_iov = &vars->qi_iov[0];
1644*4882a593Smuzhiyun 		rqst[1].rq_nvec = 1;
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 		rc = SMB2_query_info_init(tcon, server,
1647*4882a593Smuzhiyun 				  &rqst[1], COMPOUND_FID,
1648*4882a593Smuzhiyun 				  COMPOUND_FID, qi.file_info_class,
1649*4882a593Smuzhiyun 				  qi.info_type, qi.additional_information,
1650*4882a593Smuzhiyun 				  qi.input_buffer_length,
1651*4882a593Smuzhiyun 				  qi.output_buffer_length, buffer);
1652*4882a593Smuzhiyun 		free_req1_func = SMB2_query_info_free;
1653*4882a593Smuzhiyun 	} else { /* unknown flags */
1654*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1655*4882a593Smuzhiyun 			      qi.flags);
1656*4882a593Smuzhiyun 		rc = -EINVAL;
1657*4882a593Smuzhiyun 	}
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun 	if (rc)
1660*4882a593Smuzhiyun 		goto free_open_req;
1661*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[1]);
1662*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	/* Close */
1665*4882a593Smuzhiyun 	rqst[2].rq_iov = &vars->close_iov[0];
1666*4882a593Smuzhiyun 	rqst[2].rq_nvec = 1;
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun 	rc = SMB2_close_init(tcon, server,
1669*4882a593Smuzhiyun 			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1670*4882a593Smuzhiyun 	if (rc)
1671*4882a593Smuzhiyun 		goto free_req_1;
1672*4882a593Smuzhiyun 	smb2_set_related(&rqst[2]);
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	rc = compound_send_recv(xid, ses, server,
1675*4882a593Smuzhiyun 				flags, 3, rqst,
1676*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
1677*4882a593Smuzhiyun 	if (rc)
1678*4882a593Smuzhiyun 		goto out;
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	/* No need to bump num_remote_opens since handle immediately closed */
1681*4882a593Smuzhiyun 	if (qi.flags & PASSTHRU_FSCTL) {
1682*4882a593Smuzhiyun 		pqi = (struct smb_query_info __user *)arg;
1683*4882a593Smuzhiyun 		io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1684*4882a593Smuzhiyun 		if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1685*4882a593Smuzhiyun 			qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1686*4882a593Smuzhiyun 		if (qi.input_buffer_length > 0 &&
1687*4882a593Smuzhiyun 		    le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1688*4882a593Smuzhiyun 		    > rsp_iov[1].iov_len) {
1689*4882a593Smuzhiyun 			rc = -EFAULT;
1690*4882a593Smuzhiyun 			goto out;
1691*4882a593Smuzhiyun 		}
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 		if (copy_to_user(&pqi->input_buffer_length,
1694*4882a593Smuzhiyun 				 &qi.input_buffer_length,
1695*4882a593Smuzhiyun 				 sizeof(qi.input_buffer_length))) {
1696*4882a593Smuzhiyun 			rc = -EFAULT;
1697*4882a593Smuzhiyun 			goto out;
1698*4882a593Smuzhiyun 		}
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 		if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1701*4882a593Smuzhiyun 				 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1702*4882a593Smuzhiyun 				 qi.input_buffer_length))
1703*4882a593Smuzhiyun 			rc = -EFAULT;
1704*4882a593Smuzhiyun 	} else {
1705*4882a593Smuzhiyun 		pqi = (struct smb_query_info __user *)arg;
1706*4882a593Smuzhiyun 		qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1707*4882a593Smuzhiyun 		if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1708*4882a593Smuzhiyun 			qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1709*4882a593Smuzhiyun 		if (copy_to_user(&pqi->input_buffer_length,
1710*4882a593Smuzhiyun 				 &qi.input_buffer_length,
1711*4882a593Smuzhiyun 				 sizeof(qi.input_buffer_length))) {
1712*4882a593Smuzhiyun 			rc = -EFAULT;
1713*4882a593Smuzhiyun 			goto out;
1714*4882a593Smuzhiyun 		}
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 		if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1717*4882a593Smuzhiyun 				 qi.input_buffer_length))
1718*4882a593Smuzhiyun 			rc = -EFAULT;
1719*4882a593Smuzhiyun 	}
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun out:
1722*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1723*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1724*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1725*4882a593Smuzhiyun 	SMB2_close_free(&rqst[2]);
1726*4882a593Smuzhiyun free_req_1:
1727*4882a593Smuzhiyun 	free_req1_func(&rqst[1]);
1728*4882a593Smuzhiyun free_open_req:
1729*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
1730*4882a593Smuzhiyun free_output_buffer:
1731*4882a593Smuzhiyun 	kfree(buffer);
1732*4882a593Smuzhiyun free_vars:
1733*4882a593Smuzhiyun 	kfree(vars);
1734*4882a593Smuzhiyun 	return rc;
1735*4882a593Smuzhiyun }
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun static ssize_t
smb2_copychunk_range(const unsigned int xid,struct cifsFileInfo * srcfile,struct cifsFileInfo * trgtfile,u64 src_off,u64 len,u64 dest_off)1738*4882a593Smuzhiyun smb2_copychunk_range(const unsigned int xid,
1739*4882a593Smuzhiyun 			struct cifsFileInfo *srcfile,
1740*4882a593Smuzhiyun 			struct cifsFileInfo *trgtfile, u64 src_off,
1741*4882a593Smuzhiyun 			u64 len, u64 dest_off)
1742*4882a593Smuzhiyun {
1743*4882a593Smuzhiyun 	int rc;
1744*4882a593Smuzhiyun 	unsigned int ret_data_len;
1745*4882a593Smuzhiyun 	struct copychunk_ioctl *pcchunk;
1746*4882a593Smuzhiyun 	struct copychunk_ioctl_rsp *retbuf = NULL;
1747*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
1748*4882a593Smuzhiyun 	int chunks_copied = 0;
1749*4882a593Smuzhiyun 	bool chunk_sizes_updated = false;
1750*4882a593Smuzhiyun 	ssize_t bytes_written, total_bytes_written = 0;
1751*4882a593Smuzhiyun 	struct inode *inode;
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 	pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	/*
1756*4882a593Smuzhiyun 	 * We need to flush all unwritten data before we can send the
1757*4882a593Smuzhiyun 	 * copychunk ioctl to the server.
1758*4882a593Smuzhiyun 	 */
1759*4882a593Smuzhiyun 	inode = d_inode(trgtfile->dentry);
1760*4882a593Smuzhiyun 	filemap_write_and_wait(inode->i_mapping);
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 	if (pcchunk == NULL)
1763*4882a593Smuzhiyun 		return -ENOMEM;
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1766*4882a593Smuzhiyun 	/* Request a key from the server to identify the source of the copy */
1767*4882a593Smuzhiyun 	rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1768*4882a593Smuzhiyun 				srcfile->fid.persistent_fid,
1769*4882a593Smuzhiyun 				srcfile->fid.volatile_fid, pcchunk);
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun 	/* Note: request_res_key sets res_key null only if rc !=0 */
1772*4882a593Smuzhiyun 	if (rc)
1773*4882a593Smuzhiyun 		goto cchunk_out;
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 	/* For now array only one chunk long, will make more flexible later */
1776*4882a593Smuzhiyun 	pcchunk->ChunkCount = cpu_to_le32(1);
1777*4882a593Smuzhiyun 	pcchunk->Reserved = 0;
1778*4882a593Smuzhiyun 	pcchunk->Reserved2 = 0;
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun 	tcon = tlink_tcon(trgtfile->tlink);
1781*4882a593Smuzhiyun 
1782*4882a593Smuzhiyun 	while (len > 0) {
1783*4882a593Smuzhiyun 		pcchunk->SourceOffset = cpu_to_le64(src_off);
1784*4882a593Smuzhiyun 		pcchunk->TargetOffset = cpu_to_le64(dest_off);
1785*4882a593Smuzhiyun 		pcchunk->Length =
1786*4882a593Smuzhiyun 			cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun 		/* Request server copy to target from src identified by key */
1789*4882a593Smuzhiyun 		kfree(retbuf);
1790*4882a593Smuzhiyun 		retbuf = NULL;
1791*4882a593Smuzhiyun 		rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1792*4882a593Smuzhiyun 			trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1793*4882a593Smuzhiyun 			(char *)pcchunk, sizeof(struct copychunk_ioctl),
1794*4882a593Smuzhiyun 			CIFSMaxBufSize, (char **)&retbuf, &ret_data_len);
1795*4882a593Smuzhiyun 		if (rc == 0) {
1796*4882a593Smuzhiyun 			if (ret_data_len !=
1797*4882a593Smuzhiyun 					sizeof(struct copychunk_ioctl_rsp)) {
1798*4882a593Smuzhiyun 				cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1799*4882a593Smuzhiyun 				rc = -EIO;
1800*4882a593Smuzhiyun 				goto cchunk_out;
1801*4882a593Smuzhiyun 			}
1802*4882a593Smuzhiyun 			if (retbuf->TotalBytesWritten == 0) {
1803*4882a593Smuzhiyun 				cifs_dbg(FYI, "no bytes copied\n");
1804*4882a593Smuzhiyun 				rc = -EIO;
1805*4882a593Smuzhiyun 				goto cchunk_out;
1806*4882a593Smuzhiyun 			}
1807*4882a593Smuzhiyun 			/*
1808*4882a593Smuzhiyun 			 * Check if server claimed to write more than we asked
1809*4882a593Smuzhiyun 			 */
1810*4882a593Smuzhiyun 			if (le32_to_cpu(retbuf->TotalBytesWritten) >
1811*4882a593Smuzhiyun 			    le32_to_cpu(pcchunk->Length)) {
1812*4882a593Smuzhiyun 				cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1813*4882a593Smuzhiyun 				rc = -EIO;
1814*4882a593Smuzhiyun 				goto cchunk_out;
1815*4882a593Smuzhiyun 			}
1816*4882a593Smuzhiyun 			if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1817*4882a593Smuzhiyun 				cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1818*4882a593Smuzhiyun 				rc = -EIO;
1819*4882a593Smuzhiyun 				goto cchunk_out;
1820*4882a593Smuzhiyun 			}
1821*4882a593Smuzhiyun 			chunks_copied++;
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun 			bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1824*4882a593Smuzhiyun 			src_off += bytes_written;
1825*4882a593Smuzhiyun 			dest_off += bytes_written;
1826*4882a593Smuzhiyun 			len -= bytes_written;
1827*4882a593Smuzhiyun 			total_bytes_written += bytes_written;
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 			cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1830*4882a593Smuzhiyun 				le32_to_cpu(retbuf->ChunksWritten),
1831*4882a593Smuzhiyun 				le32_to_cpu(retbuf->ChunkBytesWritten),
1832*4882a593Smuzhiyun 				bytes_written);
1833*4882a593Smuzhiyun 		} else if (rc == -EINVAL) {
1834*4882a593Smuzhiyun 			if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1835*4882a593Smuzhiyun 				goto cchunk_out;
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 			cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1838*4882a593Smuzhiyun 				le32_to_cpu(retbuf->ChunksWritten),
1839*4882a593Smuzhiyun 				le32_to_cpu(retbuf->ChunkBytesWritten),
1840*4882a593Smuzhiyun 				le32_to_cpu(retbuf->TotalBytesWritten));
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 			/*
1843*4882a593Smuzhiyun 			 * Check if this is the first request using these sizes,
1844*4882a593Smuzhiyun 			 * (ie check if copy succeed once with original sizes
1845*4882a593Smuzhiyun 			 * and check if the server gave us different sizes after
1846*4882a593Smuzhiyun 			 * we already updated max sizes on previous request).
1847*4882a593Smuzhiyun 			 * if not then why is the server returning an error now
1848*4882a593Smuzhiyun 			 */
1849*4882a593Smuzhiyun 			if ((chunks_copied != 0) || chunk_sizes_updated)
1850*4882a593Smuzhiyun 				goto cchunk_out;
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun 			/* Check that server is not asking us to grow size */
1853*4882a593Smuzhiyun 			if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1854*4882a593Smuzhiyun 					tcon->max_bytes_chunk)
1855*4882a593Smuzhiyun 				tcon->max_bytes_chunk =
1856*4882a593Smuzhiyun 					le32_to_cpu(retbuf->ChunkBytesWritten);
1857*4882a593Smuzhiyun 			else
1858*4882a593Smuzhiyun 				goto cchunk_out; /* server gave us bogus size */
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 			/* No need to change MaxChunks since already set to 1 */
1861*4882a593Smuzhiyun 			chunk_sizes_updated = true;
1862*4882a593Smuzhiyun 		} else
1863*4882a593Smuzhiyun 			goto cchunk_out;
1864*4882a593Smuzhiyun 	}
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun cchunk_out:
1867*4882a593Smuzhiyun 	kfree(pcchunk);
1868*4882a593Smuzhiyun 	kfree(retbuf);
1869*4882a593Smuzhiyun 	if (rc)
1870*4882a593Smuzhiyun 		return rc;
1871*4882a593Smuzhiyun 	else
1872*4882a593Smuzhiyun 		return total_bytes_written;
1873*4882a593Smuzhiyun }
1874*4882a593Smuzhiyun 
1875*4882a593Smuzhiyun static int
smb2_flush_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1876*4882a593Smuzhiyun smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1877*4882a593Smuzhiyun 		struct cifs_fid *fid)
1878*4882a593Smuzhiyun {
1879*4882a593Smuzhiyun 	return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun static unsigned int
smb2_read_data_offset(char * buf)1883*4882a593Smuzhiyun smb2_read_data_offset(char *buf)
1884*4882a593Smuzhiyun {
1885*4882a593Smuzhiyun 	struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1886*4882a593Smuzhiyun 
1887*4882a593Smuzhiyun 	return rsp->DataOffset;
1888*4882a593Smuzhiyun }
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun static unsigned int
smb2_read_data_length(char * buf,bool in_remaining)1891*4882a593Smuzhiyun smb2_read_data_length(char *buf, bool in_remaining)
1892*4882a593Smuzhiyun {
1893*4882a593Smuzhiyun 	struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	if (in_remaining)
1896*4882a593Smuzhiyun 		return le32_to_cpu(rsp->DataRemaining);
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 	return le32_to_cpu(rsp->DataLength);
1899*4882a593Smuzhiyun }
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun static int
smb2_sync_read(const unsigned int xid,struct cifs_fid * pfid,struct cifs_io_parms * parms,unsigned int * bytes_read,char ** buf,int * buf_type)1903*4882a593Smuzhiyun smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1904*4882a593Smuzhiyun 	       struct cifs_io_parms *parms, unsigned int *bytes_read,
1905*4882a593Smuzhiyun 	       char **buf, int *buf_type)
1906*4882a593Smuzhiyun {
1907*4882a593Smuzhiyun 	parms->persistent_fid = pfid->persistent_fid;
1908*4882a593Smuzhiyun 	parms->volatile_fid = pfid->volatile_fid;
1909*4882a593Smuzhiyun 	return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun static int
smb2_sync_write(const unsigned int xid,struct cifs_fid * pfid,struct cifs_io_parms * parms,unsigned int * written,struct kvec * iov,unsigned long nr_segs)1913*4882a593Smuzhiyun smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1914*4882a593Smuzhiyun 		struct cifs_io_parms *parms, unsigned int *written,
1915*4882a593Smuzhiyun 		struct kvec *iov, unsigned long nr_segs)
1916*4882a593Smuzhiyun {
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	parms->persistent_fid = pfid->persistent_fid;
1919*4882a593Smuzhiyun 	parms->volatile_fid = pfid->volatile_fid;
1920*4882a593Smuzhiyun 	return SMB2_write(xid, parms, written, iov, nr_segs);
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
smb2_set_sparse(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct inode * inode,__u8 setsparse)1924*4882a593Smuzhiyun static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1925*4882a593Smuzhiyun 		struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1926*4882a593Smuzhiyun {
1927*4882a593Smuzhiyun 	struct cifsInodeInfo *cifsi;
1928*4882a593Smuzhiyun 	int rc;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	cifsi = CIFS_I(inode);
1931*4882a593Smuzhiyun 
1932*4882a593Smuzhiyun 	/* if file already sparse don't bother setting sparse again */
1933*4882a593Smuzhiyun 	if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1934*4882a593Smuzhiyun 		return true; /* already sparse */
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1937*4882a593Smuzhiyun 		return true; /* already not sparse */
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	/*
1940*4882a593Smuzhiyun 	 * Can't check for sparse support on share the usual way via the
1941*4882a593Smuzhiyun 	 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1942*4882a593Smuzhiyun 	 * since Samba server doesn't set the flag on the share, yet
1943*4882a593Smuzhiyun 	 * supports the set sparse FSCTL and returns sparse correctly
1944*4882a593Smuzhiyun 	 * in the file attributes. If we fail setting sparse though we
1945*4882a593Smuzhiyun 	 * mark that server does not support sparse files for this share
1946*4882a593Smuzhiyun 	 * to avoid repeatedly sending the unsupported fsctl to server
1947*4882a593Smuzhiyun 	 * if the file is repeatedly extended.
1948*4882a593Smuzhiyun 	 */
1949*4882a593Smuzhiyun 	if (tcon->broken_sparse_sup)
1950*4882a593Smuzhiyun 		return false;
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1953*4882a593Smuzhiyun 			cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1954*4882a593Smuzhiyun 			&setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1955*4882a593Smuzhiyun 	if (rc) {
1956*4882a593Smuzhiyun 		tcon->broken_sparse_sup = true;
1957*4882a593Smuzhiyun 		cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1958*4882a593Smuzhiyun 		return false;
1959*4882a593Smuzhiyun 	}
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 	if (setsparse)
1962*4882a593Smuzhiyun 		cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1963*4882a593Smuzhiyun 	else
1964*4882a593Smuzhiyun 		cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 	return true;
1967*4882a593Smuzhiyun }
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun static int
smb2_set_file_size(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,__u64 size,bool set_alloc)1970*4882a593Smuzhiyun smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1971*4882a593Smuzhiyun 		   struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1972*4882a593Smuzhiyun {
1973*4882a593Smuzhiyun 	__le64 eof = cpu_to_le64(size);
1974*4882a593Smuzhiyun 	struct inode *inode;
1975*4882a593Smuzhiyun 
1976*4882a593Smuzhiyun 	/*
1977*4882a593Smuzhiyun 	 * If extending file more than one page make sparse. Many Linux fs
1978*4882a593Smuzhiyun 	 * make files sparse by default when extending via ftruncate
1979*4882a593Smuzhiyun 	 */
1980*4882a593Smuzhiyun 	inode = d_inode(cfile->dentry);
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun 	if (!set_alloc && (size > inode->i_size + 8192)) {
1983*4882a593Smuzhiyun 		__u8 set_sparse = 1;
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun 		/* whether set sparse succeeds or not, extend the file */
1986*4882a593Smuzhiyun 		smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1987*4882a593Smuzhiyun 	}
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1990*4882a593Smuzhiyun 			    cfile->fid.volatile_fid, cfile->pid, &eof);
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun static int
smb2_duplicate_extents(const unsigned int xid,struct cifsFileInfo * srcfile,struct cifsFileInfo * trgtfile,u64 src_off,u64 len,u64 dest_off)1994*4882a593Smuzhiyun smb2_duplicate_extents(const unsigned int xid,
1995*4882a593Smuzhiyun 			struct cifsFileInfo *srcfile,
1996*4882a593Smuzhiyun 			struct cifsFileInfo *trgtfile, u64 src_off,
1997*4882a593Smuzhiyun 			u64 len, u64 dest_off)
1998*4882a593Smuzhiyun {
1999*4882a593Smuzhiyun 	int rc;
2000*4882a593Smuzhiyun 	unsigned int ret_data_len;
2001*4882a593Smuzhiyun 	struct inode *inode;
2002*4882a593Smuzhiyun 	struct duplicate_extents_to_file dup_ext_buf;
2003*4882a593Smuzhiyun 	struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	/* server fileays advertise duplicate extent support with this flag */
2006*4882a593Smuzhiyun 	if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2007*4882a593Smuzhiyun 	     FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2008*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2011*4882a593Smuzhiyun 	dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2012*4882a593Smuzhiyun 	dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2013*4882a593Smuzhiyun 	dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2014*4882a593Smuzhiyun 	dup_ext_buf.ByteCount = cpu_to_le64(len);
2015*4882a593Smuzhiyun 	cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2016*4882a593Smuzhiyun 		src_off, dest_off, len);
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun 	inode = d_inode(trgtfile->dentry);
2019*4882a593Smuzhiyun 	if (inode->i_size < dest_off + len) {
2020*4882a593Smuzhiyun 		rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2021*4882a593Smuzhiyun 		if (rc)
2022*4882a593Smuzhiyun 			goto duplicate_extents_out;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 		/*
2025*4882a593Smuzhiyun 		 * Although also could set plausible allocation size (i_blocks)
2026*4882a593Smuzhiyun 		 * here in addition to setting the file size, in reflink
2027*4882a593Smuzhiyun 		 * it is likely that the target file is sparse. Its allocation
2028*4882a593Smuzhiyun 		 * size will be queried on next revalidate, but it is important
2029*4882a593Smuzhiyun 		 * to make sure that file's cached size is updated immediately
2030*4882a593Smuzhiyun 		 */
2031*4882a593Smuzhiyun 		cifs_setsize(inode, dest_off + len);
2032*4882a593Smuzhiyun 	}
2033*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2034*4882a593Smuzhiyun 			trgtfile->fid.volatile_fid,
2035*4882a593Smuzhiyun 			FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2036*4882a593Smuzhiyun 			(char *)&dup_ext_buf,
2037*4882a593Smuzhiyun 			sizeof(struct duplicate_extents_to_file),
2038*4882a593Smuzhiyun 			CIFSMaxBufSize, NULL,
2039*4882a593Smuzhiyun 			&ret_data_len);
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun 	if (ret_data_len > 0)
2042*4882a593Smuzhiyun 		cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun duplicate_extents_out:
2045*4882a593Smuzhiyun 	return rc;
2046*4882a593Smuzhiyun }
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun static int
smb2_set_compression(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)2049*4882a593Smuzhiyun smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2050*4882a593Smuzhiyun 		   struct cifsFileInfo *cfile)
2051*4882a593Smuzhiyun {
2052*4882a593Smuzhiyun 	return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2053*4882a593Smuzhiyun 			    cfile->fid.volatile_fid);
2054*4882a593Smuzhiyun }
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun static int
smb3_set_integrity(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)2057*4882a593Smuzhiyun smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2058*4882a593Smuzhiyun 		   struct cifsFileInfo *cfile)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun 	struct fsctl_set_integrity_information_req integr_info;
2061*4882a593Smuzhiyun 	unsigned int ret_data_len;
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 	integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2064*4882a593Smuzhiyun 	integr_info.Flags = 0;
2065*4882a593Smuzhiyun 	integr_info.Reserved = 0;
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2068*4882a593Smuzhiyun 			cfile->fid.volatile_fid,
2069*4882a593Smuzhiyun 			FSCTL_SET_INTEGRITY_INFORMATION,
2070*4882a593Smuzhiyun 			(char *)&integr_info,
2071*4882a593Smuzhiyun 			sizeof(struct fsctl_set_integrity_information_req),
2072*4882a593Smuzhiyun 			CIFSMaxBufSize, NULL,
2073*4882a593Smuzhiyun 			&ret_data_len);
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun }
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2078*4882a593Smuzhiyun #define GMT_TOKEN_SIZE 50
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun /*
2083*4882a593Smuzhiyun  * Input buffer contains (empty) struct smb_snapshot array with size filled in
2084*4882a593Smuzhiyun  * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2085*4882a593Smuzhiyun  */
2086*4882a593Smuzhiyun static int
smb3_enum_snapshots(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,void __user * ioc_buf)2087*4882a593Smuzhiyun smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2088*4882a593Smuzhiyun 		   struct cifsFileInfo *cfile, void __user *ioc_buf)
2089*4882a593Smuzhiyun {
2090*4882a593Smuzhiyun 	char *retbuf = NULL;
2091*4882a593Smuzhiyun 	unsigned int ret_data_len = 0;
2092*4882a593Smuzhiyun 	int rc;
2093*4882a593Smuzhiyun 	u32 max_response_size;
2094*4882a593Smuzhiyun 	struct smb_snapshot_array snapshot_in;
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 	/*
2097*4882a593Smuzhiyun 	 * On the first query to enumerate the list of snapshots available
2098*4882a593Smuzhiyun 	 * for this volume the buffer begins with 0 (number of snapshots
2099*4882a593Smuzhiyun 	 * which can be returned is zero since at that point we do not know
2100*4882a593Smuzhiyun 	 * how big the buffer needs to be). On the second query,
2101*4882a593Smuzhiyun 	 * it (ret_data_len) is set to number of snapshots so we can
2102*4882a593Smuzhiyun 	 * know to set the maximum response size larger (see below).
2103*4882a593Smuzhiyun 	 */
2104*4882a593Smuzhiyun 	if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2105*4882a593Smuzhiyun 		return -EFAULT;
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	/*
2108*4882a593Smuzhiyun 	 * Note that for snapshot queries that servers like Azure expect that
2109*4882a593Smuzhiyun 	 * the first query be minimal size (and just used to get the number/size
2110*4882a593Smuzhiyun 	 * of previous versions) so response size must be specified as EXACTLY
2111*4882a593Smuzhiyun 	 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2112*4882a593Smuzhiyun 	 * of eight bytes.
2113*4882a593Smuzhiyun 	 */
2114*4882a593Smuzhiyun 	if (ret_data_len == 0)
2115*4882a593Smuzhiyun 		max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2116*4882a593Smuzhiyun 	else
2117*4882a593Smuzhiyun 		max_response_size = CIFSMaxBufSize;
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2120*4882a593Smuzhiyun 			cfile->fid.volatile_fid,
2121*4882a593Smuzhiyun 			FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2122*4882a593Smuzhiyun 			NULL, 0 /* no input data */, max_response_size,
2123*4882a593Smuzhiyun 			(char **)&retbuf,
2124*4882a593Smuzhiyun 			&ret_data_len);
2125*4882a593Smuzhiyun 	cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2126*4882a593Smuzhiyun 			rc, ret_data_len);
2127*4882a593Smuzhiyun 	if (rc)
2128*4882a593Smuzhiyun 		return rc;
2129*4882a593Smuzhiyun 
2130*4882a593Smuzhiyun 	if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2131*4882a593Smuzhiyun 		/* Fixup buffer */
2132*4882a593Smuzhiyun 		if (copy_from_user(&snapshot_in, ioc_buf,
2133*4882a593Smuzhiyun 		    sizeof(struct smb_snapshot_array))) {
2134*4882a593Smuzhiyun 			rc = -EFAULT;
2135*4882a593Smuzhiyun 			kfree(retbuf);
2136*4882a593Smuzhiyun 			return rc;
2137*4882a593Smuzhiyun 		}
2138*4882a593Smuzhiyun 
2139*4882a593Smuzhiyun 		/*
2140*4882a593Smuzhiyun 		 * Check for min size, ie not large enough to fit even one GMT
2141*4882a593Smuzhiyun 		 * token (snapshot).  On the first ioctl some users may pass in
2142*4882a593Smuzhiyun 		 * smaller size (or zero) to simply get the size of the array
2143*4882a593Smuzhiyun 		 * so the user space caller can allocate sufficient memory
2144*4882a593Smuzhiyun 		 * and retry the ioctl again with larger array size sufficient
2145*4882a593Smuzhiyun 		 * to hold all of the snapshot GMT tokens on the second try.
2146*4882a593Smuzhiyun 		 */
2147*4882a593Smuzhiyun 		if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2148*4882a593Smuzhiyun 			ret_data_len = sizeof(struct smb_snapshot_array);
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 		/*
2151*4882a593Smuzhiyun 		 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2152*4882a593Smuzhiyun 		 * the snapshot array (of 50 byte GMT tokens) each
2153*4882a593Smuzhiyun 		 * representing an available previous version of the data
2154*4882a593Smuzhiyun 		 */
2155*4882a593Smuzhiyun 		if (ret_data_len > (snapshot_in.snapshot_array_size +
2156*4882a593Smuzhiyun 					sizeof(struct smb_snapshot_array)))
2157*4882a593Smuzhiyun 			ret_data_len = snapshot_in.snapshot_array_size +
2158*4882a593Smuzhiyun 					sizeof(struct smb_snapshot_array);
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 		if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2161*4882a593Smuzhiyun 			rc = -EFAULT;
2162*4882a593Smuzhiyun 	}
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	kfree(retbuf);
2165*4882a593Smuzhiyun 	return rc;
2166*4882a593Smuzhiyun }
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 
2170*4882a593Smuzhiyun static int
smb3_notify(const unsigned int xid,struct file * pfile,void __user * ioc_buf)2171*4882a593Smuzhiyun smb3_notify(const unsigned int xid, struct file *pfile,
2172*4882a593Smuzhiyun 	    void __user *ioc_buf)
2173*4882a593Smuzhiyun {
2174*4882a593Smuzhiyun 	struct smb3_notify notify;
2175*4882a593Smuzhiyun 	struct dentry *dentry = pfile->f_path.dentry;
2176*4882a593Smuzhiyun 	struct inode *inode = file_inode(pfile);
2177*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb;
2178*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
2179*4882a593Smuzhiyun 	struct cifs_fid fid;
2180*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
2181*4882a593Smuzhiyun 	unsigned char *path = NULL;
2182*4882a593Smuzhiyun 	__le16 *utf16_path = NULL;
2183*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2184*4882a593Smuzhiyun 	int rc = 0;
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	path = build_path_from_dentry(dentry);
2187*4882a593Smuzhiyun 	if (path == NULL)
2188*4882a593Smuzhiyun 		return -ENOMEM;
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun 	cifs_sb = CIFS_SB(inode->i_sb);
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2193*4882a593Smuzhiyun 	if (utf16_path == NULL) {
2194*4882a593Smuzhiyun 		rc = -ENOMEM;
2195*4882a593Smuzhiyun 		goto notify_exit;
2196*4882a593Smuzhiyun 	}
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun 	if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2199*4882a593Smuzhiyun 		rc = -EFAULT;
2200*4882a593Smuzhiyun 		goto notify_exit;
2201*4882a593Smuzhiyun 	}
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun 	tcon = cifs_sb_master_tcon(cifs_sb);
2204*4882a593Smuzhiyun 	oparms.tcon = tcon;
2205*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2206*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
2207*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
2208*4882a593Smuzhiyun 	oparms.fid = &fid;
2209*4882a593Smuzhiyun 	oparms.reconnect = false;
2210*4882a593Smuzhiyun 
2211*4882a593Smuzhiyun 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2212*4882a593Smuzhiyun 		       NULL);
2213*4882a593Smuzhiyun 	if (rc)
2214*4882a593Smuzhiyun 		goto notify_exit;
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2217*4882a593Smuzhiyun 				notify.watch_tree, notify.completion_filter);
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun 	cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun notify_exit:
2224*4882a593Smuzhiyun 	kfree(path);
2225*4882a593Smuzhiyun 	kfree(utf16_path);
2226*4882a593Smuzhiyun 	return rc;
2227*4882a593Smuzhiyun }
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun static int
smb2_query_dir_first(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct cifs_fid * fid,__u16 search_flags,struct cifs_search_info * srch_inf)2230*4882a593Smuzhiyun smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2231*4882a593Smuzhiyun 		     const char *path, struct cifs_sb_info *cifs_sb,
2232*4882a593Smuzhiyun 		     struct cifs_fid *fid, __u16 search_flags,
2233*4882a593Smuzhiyun 		     struct cifs_search_info *srch_inf)
2234*4882a593Smuzhiyun {
2235*4882a593Smuzhiyun 	__le16 *utf16_path;
2236*4882a593Smuzhiyun 	struct smb_rqst rqst[2];
2237*4882a593Smuzhiyun 	struct kvec rsp_iov[2];
2238*4882a593Smuzhiyun 	int resp_buftype[2];
2239*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2240*4882a593Smuzhiyun 	struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2241*4882a593Smuzhiyun 	int rc, flags = 0;
2242*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2243*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
2244*4882a593Smuzhiyun 	struct smb2_query_directory_rsp *qd_rsp = NULL;
2245*4882a593Smuzhiyun 	struct smb2_create_rsp *op_rsp = NULL;
2246*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2249*4882a593Smuzhiyun 	if (!utf16_path)
2250*4882a593Smuzhiyun 		return -ENOMEM;
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
2253*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 	memset(rqst, 0, sizeof(rqst));
2256*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2257*4882a593Smuzhiyun 	memset(rsp_iov, 0, sizeof(rsp_iov));
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun 	/* Open */
2260*4882a593Smuzhiyun 	memset(&open_iov, 0, sizeof(open_iov));
2261*4882a593Smuzhiyun 	rqst[0].rq_iov = open_iov;
2262*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun 	oparms.tcon = tcon;
2265*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2266*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
2267*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
2268*4882a593Smuzhiyun 	oparms.fid = fid;
2269*4882a593Smuzhiyun 	oparms.reconnect = false;
2270*4882a593Smuzhiyun 
2271*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
2272*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, utf16_path);
2273*4882a593Smuzhiyun 	if (rc)
2274*4882a593Smuzhiyun 		goto qdf_free;
2275*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
2276*4882a593Smuzhiyun 
2277*4882a593Smuzhiyun 	/* Query directory */
2278*4882a593Smuzhiyun 	srch_inf->entries_in_buffer = 0;
2279*4882a593Smuzhiyun 	srch_inf->index_of_last_entry = 2;
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun 	memset(&qd_iov, 0, sizeof(qd_iov));
2282*4882a593Smuzhiyun 	rqst[1].rq_iov = qd_iov;
2283*4882a593Smuzhiyun 	rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2284*4882a593Smuzhiyun 
2285*4882a593Smuzhiyun 	rc = SMB2_query_directory_init(xid, tcon, server,
2286*4882a593Smuzhiyun 				       &rqst[1],
2287*4882a593Smuzhiyun 				       COMPOUND_FID, COMPOUND_FID,
2288*4882a593Smuzhiyun 				       0, srch_inf->info_level);
2289*4882a593Smuzhiyun 	if (rc)
2290*4882a593Smuzhiyun 		goto qdf_free;
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun 	rc = compound_send_recv(xid, tcon->ses, server,
2295*4882a593Smuzhiyun 				flags, 2, rqst,
2296*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun 	/* If the open failed there is nothing to do */
2299*4882a593Smuzhiyun 	op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2300*4882a593Smuzhiyun 	if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2301*4882a593Smuzhiyun 		cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2302*4882a593Smuzhiyun 		goto qdf_free;
2303*4882a593Smuzhiyun 	}
2304*4882a593Smuzhiyun 	fid->persistent_fid = op_rsp->PersistentFileId;
2305*4882a593Smuzhiyun 	fid->volatile_fid = op_rsp->VolatileFileId;
2306*4882a593Smuzhiyun 
2307*4882a593Smuzhiyun 	/* Anything else than ENODATA means a genuine error */
2308*4882a593Smuzhiyun 	if (rc && rc != -ENODATA) {
2309*4882a593Smuzhiyun 		SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2310*4882a593Smuzhiyun 		cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2311*4882a593Smuzhiyun 		trace_smb3_query_dir_err(xid, fid->persistent_fid,
2312*4882a593Smuzhiyun 					 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2313*4882a593Smuzhiyun 		goto qdf_free;
2314*4882a593Smuzhiyun 	}
2315*4882a593Smuzhiyun 
2316*4882a593Smuzhiyun 	atomic_inc(&tcon->num_remote_opens);
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun 	qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2319*4882a593Smuzhiyun 	if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2320*4882a593Smuzhiyun 		trace_smb3_query_dir_done(xid, fid->persistent_fid,
2321*4882a593Smuzhiyun 					  tcon->tid, tcon->ses->Suid, 0, 0);
2322*4882a593Smuzhiyun 		srch_inf->endOfSearch = true;
2323*4882a593Smuzhiyun 		rc = 0;
2324*4882a593Smuzhiyun 		goto qdf_free;
2325*4882a593Smuzhiyun 	}
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun 	rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2328*4882a593Smuzhiyun 					srch_inf);
2329*4882a593Smuzhiyun 	if (rc) {
2330*4882a593Smuzhiyun 		trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2331*4882a593Smuzhiyun 			tcon->ses->Suid, 0, 0, rc);
2332*4882a593Smuzhiyun 		goto qdf_free;
2333*4882a593Smuzhiyun 	}
2334*4882a593Smuzhiyun 	resp_buftype[1] = CIFS_NO_BUFFER;
2335*4882a593Smuzhiyun 
2336*4882a593Smuzhiyun 	trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2337*4882a593Smuzhiyun 			tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun  qdf_free:
2340*4882a593Smuzhiyun 	kfree(utf16_path);
2341*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
2342*4882a593Smuzhiyun 	SMB2_query_directory_free(&rqst[1]);
2343*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2344*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2345*4882a593Smuzhiyun 	return rc;
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun 
2348*4882a593Smuzhiyun static int
smb2_query_dir_next(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid,__u16 search_flags,struct cifs_search_info * srch_inf)2349*4882a593Smuzhiyun smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2350*4882a593Smuzhiyun 		    struct cifs_fid *fid, __u16 search_flags,
2351*4882a593Smuzhiyun 		    struct cifs_search_info *srch_inf)
2352*4882a593Smuzhiyun {
2353*4882a593Smuzhiyun 	return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2354*4882a593Smuzhiyun 				    fid->volatile_fid, 0, srch_inf);
2355*4882a593Smuzhiyun }
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun static int
smb2_close_dir(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)2358*4882a593Smuzhiyun smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2359*4882a593Smuzhiyun 	       struct cifs_fid *fid)
2360*4882a593Smuzhiyun {
2361*4882a593Smuzhiyun 	return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2362*4882a593Smuzhiyun }
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun /*
2365*4882a593Smuzhiyun  * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2366*4882a593Smuzhiyun  * the number of credits and return true. Otherwise - return false.
2367*4882a593Smuzhiyun  */
2368*4882a593Smuzhiyun static bool
smb2_is_status_pending(char * buf,struct TCP_Server_Info * server)2369*4882a593Smuzhiyun smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2370*4882a593Smuzhiyun {
2371*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun 	if (shdr->Status != STATUS_PENDING)
2374*4882a593Smuzhiyun 		return false;
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	if (shdr->CreditRequest) {
2377*4882a593Smuzhiyun 		spin_lock(&server->req_lock);
2378*4882a593Smuzhiyun 		server->credits += le16_to_cpu(shdr->CreditRequest);
2379*4882a593Smuzhiyun 		spin_unlock(&server->req_lock);
2380*4882a593Smuzhiyun 		wake_up(&server->request_q);
2381*4882a593Smuzhiyun 	}
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 	return true;
2384*4882a593Smuzhiyun }
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun static bool
smb2_is_session_expired(char * buf)2387*4882a593Smuzhiyun smb2_is_session_expired(char *buf)
2388*4882a593Smuzhiyun {
2389*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 	if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2392*4882a593Smuzhiyun 	    shdr->Status != STATUS_USER_SESSION_DELETED)
2393*4882a593Smuzhiyun 		return false;
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun 	trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2396*4882a593Smuzhiyun 			       le16_to_cpu(shdr->Command),
2397*4882a593Smuzhiyun 			       le64_to_cpu(shdr->MessageId));
2398*4882a593Smuzhiyun 	cifs_dbg(FYI, "Session expired or deleted\n");
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 	return true;
2401*4882a593Smuzhiyun }
2402*4882a593Smuzhiyun 
2403*4882a593Smuzhiyun static bool
smb2_is_status_io_timeout(char * buf)2404*4882a593Smuzhiyun smb2_is_status_io_timeout(char *buf)
2405*4882a593Smuzhiyun {
2406*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2407*4882a593Smuzhiyun 
2408*4882a593Smuzhiyun 	if (shdr->Status == STATUS_IO_TIMEOUT)
2409*4882a593Smuzhiyun 		return true;
2410*4882a593Smuzhiyun 	else
2411*4882a593Smuzhiyun 		return false;
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun static int
smb2_oplock_response(struct cifs_tcon * tcon,struct cifs_fid * fid,struct cifsInodeInfo * cinode)2415*4882a593Smuzhiyun smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2416*4882a593Smuzhiyun 		     struct cifsInodeInfo *cinode)
2417*4882a593Smuzhiyun {
2418*4882a593Smuzhiyun 	if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2419*4882a593Smuzhiyun 		return SMB2_lease_break(0, tcon, cinode->lease_key,
2420*4882a593Smuzhiyun 					smb2_get_lease_state(cinode));
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun 	return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2423*4882a593Smuzhiyun 				 fid->volatile_fid,
2424*4882a593Smuzhiyun 				 CIFS_CACHE_READ(cinode) ? 1 : 0);
2425*4882a593Smuzhiyun }
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun void
smb2_set_related(struct smb_rqst * rqst)2428*4882a593Smuzhiyun smb2_set_related(struct smb_rqst *rqst)
2429*4882a593Smuzhiyun {
2430*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr;
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 	shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2433*4882a593Smuzhiyun 	if (shdr == NULL) {
2434*4882a593Smuzhiyun 		cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2435*4882a593Smuzhiyun 		return;
2436*4882a593Smuzhiyun 	}
2437*4882a593Smuzhiyun 	shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2438*4882a593Smuzhiyun }
2439*4882a593Smuzhiyun 
2440*4882a593Smuzhiyun char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun void
smb2_set_next_command(struct cifs_tcon * tcon,struct smb_rqst * rqst)2443*4882a593Smuzhiyun smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2444*4882a593Smuzhiyun {
2445*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr;
2446*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
2447*4882a593Smuzhiyun 	struct TCP_Server_Info *server = ses->server;
2448*4882a593Smuzhiyun 	unsigned long len = smb_rqst_len(server, rqst);
2449*4882a593Smuzhiyun 	int i, num_padding;
2450*4882a593Smuzhiyun 
2451*4882a593Smuzhiyun 	shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2452*4882a593Smuzhiyun 	if (shdr == NULL) {
2453*4882a593Smuzhiyun 		cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2454*4882a593Smuzhiyun 		return;
2455*4882a593Smuzhiyun 	}
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 	/* SMB headers in a compound are 8 byte aligned. */
2458*4882a593Smuzhiyun 
2459*4882a593Smuzhiyun 	/* No padding needed */
2460*4882a593Smuzhiyun 	if (!(len & 7))
2461*4882a593Smuzhiyun 		goto finished;
2462*4882a593Smuzhiyun 
2463*4882a593Smuzhiyun 	num_padding = 8 - (len & 7);
2464*4882a593Smuzhiyun 	if (!smb3_encryption_required(tcon)) {
2465*4882a593Smuzhiyun 		/*
2466*4882a593Smuzhiyun 		 * If we do not have encryption then we can just add an extra
2467*4882a593Smuzhiyun 		 * iov for the padding.
2468*4882a593Smuzhiyun 		 */
2469*4882a593Smuzhiyun 		rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2470*4882a593Smuzhiyun 		rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2471*4882a593Smuzhiyun 		rqst->rq_nvec++;
2472*4882a593Smuzhiyun 		len += num_padding;
2473*4882a593Smuzhiyun 	} else {
2474*4882a593Smuzhiyun 		/*
2475*4882a593Smuzhiyun 		 * We can not add a small padding iov for the encryption case
2476*4882a593Smuzhiyun 		 * because the encryption framework can not handle the padding
2477*4882a593Smuzhiyun 		 * iovs.
2478*4882a593Smuzhiyun 		 * We have to flatten this into a single buffer and add
2479*4882a593Smuzhiyun 		 * the padding to it.
2480*4882a593Smuzhiyun 		 */
2481*4882a593Smuzhiyun 		for (i = 1; i < rqst->rq_nvec; i++) {
2482*4882a593Smuzhiyun 			memcpy(rqst->rq_iov[0].iov_base +
2483*4882a593Smuzhiyun 			       rqst->rq_iov[0].iov_len,
2484*4882a593Smuzhiyun 			       rqst->rq_iov[i].iov_base,
2485*4882a593Smuzhiyun 			       rqst->rq_iov[i].iov_len);
2486*4882a593Smuzhiyun 			rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2487*4882a593Smuzhiyun 		}
2488*4882a593Smuzhiyun 		memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2489*4882a593Smuzhiyun 		       0, num_padding);
2490*4882a593Smuzhiyun 		rqst->rq_iov[0].iov_len += num_padding;
2491*4882a593Smuzhiyun 		len += num_padding;
2492*4882a593Smuzhiyun 		rqst->rq_nvec = 1;
2493*4882a593Smuzhiyun 	}
2494*4882a593Smuzhiyun 
2495*4882a593Smuzhiyun  finished:
2496*4882a593Smuzhiyun 	shdr->NextCommand = cpu_to_le32(len);
2497*4882a593Smuzhiyun }
2498*4882a593Smuzhiyun 
2499*4882a593Smuzhiyun /*
2500*4882a593Smuzhiyun  * Passes the query info response back to the caller on success.
2501*4882a593Smuzhiyun  * Caller need to free this with free_rsp_buf().
2502*4882a593Smuzhiyun  */
2503*4882a593Smuzhiyun int
smb2_query_info_compound(const unsigned int xid,struct cifs_tcon * tcon,__le16 * utf16_path,u32 desired_access,u32 class,u32 type,u32 output_len,struct kvec * rsp,int * buftype,struct cifs_sb_info * cifs_sb)2504*4882a593Smuzhiyun smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2505*4882a593Smuzhiyun 			 __le16 *utf16_path, u32 desired_access,
2506*4882a593Smuzhiyun 			 u32 class, u32 type, u32 output_len,
2507*4882a593Smuzhiyun 			 struct kvec *rsp, int *buftype,
2508*4882a593Smuzhiyun 			 struct cifs_sb_info *cifs_sb)
2509*4882a593Smuzhiyun {
2510*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
2511*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_pick_channel(ses);
2512*4882a593Smuzhiyun 	int flags = CIFS_CP_CREATE_CLOSE_OP;
2513*4882a593Smuzhiyun 	struct smb_rqst rqst[3];
2514*4882a593Smuzhiyun 	int resp_buftype[3];
2515*4882a593Smuzhiyun 	struct kvec rsp_iov[3];
2516*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2517*4882a593Smuzhiyun 	struct kvec qi_iov[1];
2518*4882a593Smuzhiyun 	struct kvec close_iov[1];
2519*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2520*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
2521*4882a593Smuzhiyun 	struct cifs_fid fid;
2522*4882a593Smuzhiyun 	int rc;
2523*4882a593Smuzhiyun 
2524*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
2525*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	memset(rqst, 0, sizeof(rqst));
2528*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2529*4882a593Smuzhiyun 	memset(rsp_iov, 0, sizeof(rsp_iov));
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 	memset(&open_iov, 0, sizeof(open_iov));
2532*4882a593Smuzhiyun 	rqst[0].rq_iov = open_iov;
2533*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2534*4882a593Smuzhiyun 
2535*4882a593Smuzhiyun 	oparms.tcon = tcon;
2536*4882a593Smuzhiyun 	oparms.desired_access = desired_access;
2537*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
2538*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
2539*4882a593Smuzhiyun 	oparms.fid = &fid;
2540*4882a593Smuzhiyun 	oparms.reconnect = false;
2541*4882a593Smuzhiyun 
2542*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
2543*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, utf16_path);
2544*4882a593Smuzhiyun 	if (rc)
2545*4882a593Smuzhiyun 		goto qic_exit;
2546*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
2547*4882a593Smuzhiyun 
2548*4882a593Smuzhiyun 	memset(&qi_iov, 0, sizeof(qi_iov));
2549*4882a593Smuzhiyun 	rqst[1].rq_iov = qi_iov;
2550*4882a593Smuzhiyun 	rqst[1].rq_nvec = 1;
2551*4882a593Smuzhiyun 
2552*4882a593Smuzhiyun 	rc = SMB2_query_info_init(tcon, server,
2553*4882a593Smuzhiyun 				  &rqst[1], COMPOUND_FID, COMPOUND_FID,
2554*4882a593Smuzhiyun 				  class, type, 0,
2555*4882a593Smuzhiyun 				  output_len, 0,
2556*4882a593Smuzhiyun 				  NULL);
2557*4882a593Smuzhiyun 	if (rc)
2558*4882a593Smuzhiyun 		goto qic_exit;
2559*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[1]);
2560*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
2561*4882a593Smuzhiyun 
2562*4882a593Smuzhiyun 	memset(&close_iov, 0, sizeof(close_iov));
2563*4882a593Smuzhiyun 	rqst[2].rq_iov = close_iov;
2564*4882a593Smuzhiyun 	rqst[2].rq_nvec = 1;
2565*4882a593Smuzhiyun 
2566*4882a593Smuzhiyun 	rc = SMB2_close_init(tcon, server,
2567*4882a593Smuzhiyun 			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2568*4882a593Smuzhiyun 	if (rc)
2569*4882a593Smuzhiyun 		goto qic_exit;
2570*4882a593Smuzhiyun 	smb2_set_related(&rqst[2]);
2571*4882a593Smuzhiyun 
2572*4882a593Smuzhiyun 	rc = compound_send_recv(xid, ses, server,
2573*4882a593Smuzhiyun 				flags, 3, rqst,
2574*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
2575*4882a593Smuzhiyun 	if (rc) {
2576*4882a593Smuzhiyun 		free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2577*4882a593Smuzhiyun 		if (rc == -EREMCHG) {
2578*4882a593Smuzhiyun 			tcon->need_reconnect = true;
2579*4882a593Smuzhiyun 			pr_warn_once("server share %s deleted\n",
2580*4882a593Smuzhiyun 				     tcon->treeName);
2581*4882a593Smuzhiyun 		}
2582*4882a593Smuzhiyun 		goto qic_exit;
2583*4882a593Smuzhiyun 	}
2584*4882a593Smuzhiyun 	*rsp = rsp_iov[1];
2585*4882a593Smuzhiyun 	*buftype = resp_buftype[1];
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun  qic_exit:
2588*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
2589*4882a593Smuzhiyun 	SMB2_query_info_free(&rqst[1]);
2590*4882a593Smuzhiyun 	SMB2_close_free(&rqst[2]);
2591*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2592*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2593*4882a593Smuzhiyun 	return rc;
2594*4882a593Smuzhiyun }
2595*4882a593Smuzhiyun 
2596*4882a593Smuzhiyun static int
smb2_queryfs(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,struct kstatfs * buf)2597*4882a593Smuzhiyun smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2598*4882a593Smuzhiyun 	     struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2599*4882a593Smuzhiyun {
2600*4882a593Smuzhiyun 	struct smb2_query_info_rsp *rsp;
2601*4882a593Smuzhiyun 	struct smb2_fs_full_size_info *info = NULL;
2602*4882a593Smuzhiyun 	__le16 utf16_path = 0; /* Null - open root of share */
2603*4882a593Smuzhiyun 	struct kvec rsp_iov = {NULL, 0};
2604*4882a593Smuzhiyun 	int buftype = CIFS_NO_BUFFER;
2605*4882a593Smuzhiyun 	int rc;
2606*4882a593Smuzhiyun 
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 	rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2609*4882a593Smuzhiyun 				      FILE_READ_ATTRIBUTES,
2610*4882a593Smuzhiyun 				      FS_FULL_SIZE_INFORMATION,
2611*4882a593Smuzhiyun 				      SMB2_O_INFO_FILESYSTEM,
2612*4882a593Smuzhiyun 				      sizeof(struct smb2_fs_full_size_info),
2613*4882a593Smuzhiyun 				      &rsp_iov, &buftype, cifs_sb);
2614*4882a593Smuzhiyun 	if (rc)
2615*4882a593Smuzhiyun 		goto qfs_exit;
2616*4882a593Smuzhiyun 
2617*4882a593Smuzhiyun 	rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2618*4882a593Smuzhiyun 	buf->f_type = SMB2_MAGIC_NUMBER;
2619*4882a593Smuzhiyun 	info = (struct smb2_fs_full_size_info *)(
2620*4882a593Smuzhiyun 		le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2621*4882a593Smuzhiyun 	rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2622*4882a593Smuzhiyun 			       le32_to_cpu(rsp->OutputBufferLength),
2623*4882a593Smuzhiyun 			       &rsp_iov,
2624*4882a593Smuzhiyun 			       sizeof(struct smb2_fs_full_size_info));
2625*4882a593Smuzhiyun 	if (!rc)
2626*4882a593Smuzhiyun 		smb2_copy_fs_info_to_kstatfs(info, buf);
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun qfs_exit:
2629*4882a593Smuzhiyun 	free_rsp_buf(buftype, rsp_iov.iov_base);
2630*4882a593Smuzhiyun 	return rc;
2631*4882a593Smuzhiyun }
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun static int
smb311_queryfs(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,struct kstatfs * buf)2634*4882a593Smuzhiyun smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2635*4882a593Smuzhiyun 	       struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2636*4882a593Smuzhiyun {
2637*4882a593Smuzhiyun 	int rc;
2638*4882a593Smuzhiyun 	__le16 srch_path = 0; /* Null - open root of share */
2639*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2640*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
2641*4882a593Smuzhiyun 	struct cifs_fid fid;
2642*4882a593Smuzhiyun 
2643*4882a593Smuzhiyun 	if (!tcon->posix_extensions)
2644*4882a593Smuzhiyun 		return smb2_queryfs(xid, tcon, cifs_sb, buf);
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	oparms.tcon = tcon;
2647*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
2648*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
2649*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
2650*4882a593Smuzhiyun 	oparms.fid = &fid;
2651*4882a593Smuzhiyun 	oparms.reconnect = false;
2652*4882a593Smuzhiyun 
2653*4882a593Smuzhiyun 	rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2654*4882a593Smuzhiyun 		       NULL, NULL);
2655*4882a593Smuzhiyun 	if (rc)
2656*4882a593Smuzhiyun 		return rc;
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun 	rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2659*4882a593Smuzhiyun 				   fid.volatile_fid, buf);
2660*4882a593Smuzhiyun 	buf->f_type = SMB2_MAGIC_NUMBER;
2661*4882a593Smuzhiyun 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2662*4882a593Smuzhiyun 	return rc;
2663*4882a593Smuzhiyun }
2664*4882a593Smuzhiyun 
2665*4882a593Smuzhiyun static bool
smb2_compare_fids(struct cifsFileInfo * ob1,struct cifsFileInfo * ob2)2666*4882a593Smuzhiyun smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2667*4882a593Smuzhiyun {
2668*4882a593Smuzhiyun 	return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2669*4882a593Smuzhiyun 	       ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2670*4882a593Smuzhiyun }
2671*4882a593Smuzhiyun 
2672*4882a593Smuzhiyun static int
smb2_mand_lock(const unsigned int xid,struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u32 type,int lock,int unlock,bool wait)2673*4882a593Smuzhiyun smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2674*4882a593Smuzhiyun 	       __u64 length, __u32 type, int lock, int unlock, bool wait)
2675*4882a593Smuzhiyun {
2676*4882a593Smuzhiyun 	if (unlock && !lock)
2677*4882a593Smuzhiyun 		type = SMB2_LOCKFLAG_UNLOCK;
2678*4882a593Smuzhiyun 	return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2679*4882a593Smuzhiyun 			 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2680*4882a593Smuzhiyun 			 current->tgid, length, offset, type, wait);
2681*4882a593Smuzhiyun }
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun static void
smb2_get_lease_key(struct inode * inode,struct cifs_fid * fid)2684*4882a593Smuzhiyun smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2685*4882a593Smuzhiyun {
2686*4882a593Smuzhiyun 	memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2687*4882a593Smuzhiyun }
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun static void
smb2_set_lease_key(struct inode * inode,struct cifs_fid * fid)2690*4882a593Smuzhiyun smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2691*4882a593Smuzhiyun {
2692*4882a593Smuzhiyun 	memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2693*4882a593Smuzhiyun }
2694*4882a593Smuzhiyun 
2695*4882a593Smuzhiyun static void
smb2_new_lease_key(struct cifs_fid * fid)2696*4882a593Smuzhiyun smb2_new_lease_key(struct cifs_fid *fid)
2697*4882a593Smuzhiyun {
2698*4882a593Smuzhiyun 	generate_random_uuid(fid->lease_key);
2699*4882a593Smuzhiyun }
2700*4882a593Smuzhiyun 
2701*4882a593Smuzhiyun static int
smb2_get_dfs_refer(const unsigned int xid,struct cifs_ses * ses,const char * search_name,struct dfs_info3_param ** target_nodes,unsigned int * num_of_nodes,const struct nls_table * nls_codepage,int remap)2702*4882a593Smuzhiyun smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2703*4882a593Smuzhiyun 		   const char *search_name,
2704*4882a593Smuzhiyun 		   struct dfs_info3_param **target_nodes,
2705*4882a593Smuzhiyun 		   unsigned int *num_of_nodes,
2706*4882a593Smuzhiyun 		   const struct nls_table *nls_codepage, int remap)
2707*4882a593Smuzhiyun {
2708*4882a593Smuzhiyun 	int rc;
2709*4882a593Smuzhiyun 	__le16 *utf16_path = NULL;
2710*4882a593Smuzhiyun 	int utf16_path_len = 0;
2711*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
2712*4882a593Smuzhiyun 	struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2713*4882a593Smuzhiyun 	struct get_dfs_referral_rsp *dfs_rsp = NULL;
2714*4882a593Smuzhiyun 	u32 dfs_req_size = 0, dfs_rsp_size = 0;
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 	/*
2719*4882a593Smuzhiyun 	 * Try to use the IPC tcon, otherwise just use any
2720*4882a593Smuzhiyun 	 */
2721*4882a593Smuzhiyun 	tcon = ses->tcon_ipc;
2722*4882a593Smuzhiyun 	if (tcon == NULL) {
2723*4882a593Smuzhiyun 		spin_lock(&cifs_tcp_ses_lock);
2724*4882a593Smuzhiyun 		tcon = list_first_entry_or_null(&ses->tcon_list,
2725*4882a593Smuzhiyun 						struct cifs_tcon,
2726*4882a593Smuzhiyun 						tcon_list);
2727*4882a593Smuzhiyun 		if (tcon)
2728*4882a593Smuzhiyun 			tcon->tc_count++;
2729*4882a593Smuzhiyun 		spin_unlock(&cifs_tcp_ses_lock);
2730*4882a593Smuzhiyun 	}
2731*4882a593Smuzhiyun 
2732*4882a593Smuzhiyun 	if (tcon == NULL) {
2733*4882a593Smuzhiyun 		cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2734*4882a593Smuzhiyun 			 ses);
2735*4882a593Smuzhiyun 		rc = -ENOTCONN;
2736*4882a593Smuzhiyun 		goto out;
2737*4882a593Smuzhiyun 	}
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun 	utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2740*4882a593Smuzhiyun 					   &utf16_path_len,
2741*4882a593Smuzhiyun 					   nls_codepage, remap);
2742*4882a593Smuzhiyun 	if (!utf16_path) {
2743*4882a593Smuzhiyun 		rc = -ENOMEM;
2744*4882a593Smuzhiyun 		goto out;
2745*4882a593Smuzhiyun 	}
2746*4882a593Smuzhiyun 
2747*4882a593Smuzhiyun 	dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2748*4882a593Smuzhiyun 	dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2749*4882a593Smuzhiyun 	if (!dfs_req) {
2750*4882a593Smuzhiyun 		rc = -ENOMEM;
2751*4882a593Smuzhiyun 		goto out;
2752*4882a593Smuzhiyun 	}
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun 	/* Highest DFS referral version understood */
2755*4882a593Smuzhiyun 	dfs_req->MaxReferralLevel = DFS_VERSION;
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun 	/* Path to resolve in an UTF-16 null-terminated string */
2758*4882a593Smuzhiyun 	memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun 	do {
2761*4882a593Smuzhiyun 		rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2762*4882a593Smuzhiyun 				FSCTL_DFS_GET_REFERRALS,
2763*4882a593Smuzhiyun 				(char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2764*4882a593Smuzhiyun 				(char **)&dfs_rsp, &dfs_rsp_size);
2765*4882a593Smuzhiyun 	} while (rc == -EAGAIN);
2766*4882a593Smuzhiyun 
2767*4882a593Smuzhiyun 	if (rc) {
2768*4882a593Smuzhiyun 		if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2769*4882a593Smuzhiyun 			cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2770*4882a593Smuzhiyun 		goto out;
2771*4882a593Smuzhiyun 	}
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun 	rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2774*4882a593Smuzhiyun 				 num_of_nodes, target_nodes,
2775*4882a593Smuzhiyun 				 nls_codepage, remap, search_name,
2776*4882a593Smuzhiyun 				 true /* is_unicode */);
2777*4882a593Smuzhiyun 	if (rc) {
2778*4882a593Smuzhiyun 		cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2779*4882a593Smuzhiyun 		goto out;
2780*4882a593Smuzhiyun 	}
2781*4882a593Smuzhiyun 
2782*4882a593Smuzhiyun  out:
2783*4882a593Smuzhiyun 	if (tcon && !tcon->ipc) {
2784*4882a593Smuzhiyun 		/* ipc tcons are not refcounted */
2785*4882a593Smuzhiyun 		spin_lock(&cifs_tcp_ses_lock);
2786*4882a593Smuzhiyun 		tcon->tc_count--;
2787*4882a593Smuzhiyun 		spin_unlock(&cifs_tcp_ses_lock);
2788*4882a593Smuzhiyun 	}
2789*4882a593Smuzhiyun 	kfree(utf16_path);
2790*4882a593Smuzhiyun 	kfree(dfs_req);
2791*4882a593Smuzhiyun 	kfree(dfs_rsp);
2792*4882a593Smuzhiyun 	return rc;
2793*4882a593Smuzhiyun }
2794*4882a593Smuzhiyun 
2795*4882a593Smuzhiyun static int
parse_reparse_posix(struct reparse_posix_data * symlink_buf,u32 plen,char ** target_path,struct cifs_sb_info * cifs_sb)2796*4882a593Smuzhiyun parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2797*4882a593Smuzhiyun 		      u32 plen, char **target_path,
2798*4882a593Smuzhiyun 		      struct cifs_sb_info *cifs_sb)
2799*4882a593Smuzhiyun {
2800*4882a593Smuzhiyun 	unsigned int len;
2801*4882a593Smuzhiyun 
2802*4882a593Smuzhiyun 	/* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2803*4882a593Smuzhiyun 	len = le16_to_cpu(symlink_buf->ReparseDataLength);
2804*4882a593Smuzhiyun 
2805*4882a593Smuzhiyun 	if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2806*4882a593Smuzhiyun 		cifs_dbg(VFS, "%lld not a supported symlink type\n",
2807*4882a593Smuzhiyun 			le64_to_cpu(symlink_buf->InodeType));
2808*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2809*4882a593Smuzhiyun 	}
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun 	*target_path = cifs_strndup_from_utf16(
2812*4882a593Smuzhiyun 				symlink_buf->PathBuffer,
2813*4882a593Smuzhiyun 				len, true, cifs_sb->local_nls);
2814*4882a593Smuzhiyun 	if (!(*target_path))
2815*4882a593Smuzhiyun 		return -ENOMEM;
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun 	convert_delimiter(*target_path, '/');
2818*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun 	return 0;
2821*4882a593Smuzhiyun }
2822*4882a593Smuzhiyun 
2823*4882a593Smuzhiyun static int
parse_reparse_symlink(struct reparse_symlink_data_buffer * symlink_buf,u32 plen,char ** target_path,struct cifs_sb_info * cifs_sb)2824*4882a593Smuzhiyun parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2825*4882a593Smuzhiyun 		      u32 plen, char **target_path,
2826*4882a593Smuzhiyun 		      struct cifs_sb_info *cifs_sb)
2827*4882a593Smuzhiyun {
2828*4882a593Smuzhiyun 	unsigned int sub_len;
2829*4882a593Smuzhiyun 	unsigned int sub_offset;
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun 	/* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2832*4882a593Smuzhiyun 
2833*4882a593Smuzhiyun 	sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2834*4882a593Smuzhiyun 	sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2835*4882a593Smuzhiyun 	if (sub_offset + 20 > plen ||
2836*4882a593Smuzhiyun 	    sub_offset + sub_len + 20 > plen) {
2837*4882a593Smuzhiyun 		cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2838*4882a593Smuzhiyun 		return -EIO;
2839*4882a593Smuzhiyun 	}
2840*4882a593Smuzhiyun 
2841*4882a593Smuzhiyun 	*target_path = cifs_strndup_from_utf16(
2842*4882a593Smuzhiyun 				symlink_buf->PathBuffer + sub_offset,
2843*4882a593Smuzhiyun 				sub_len, true, cifs_sb->local_nls);
2844*4882a593Smuzhiyun 	if (!(*target_path))
2845*4882a593Smuzhiyun 		return -ENOMEM;
2846*4882a593Smuzhiyun 
2847*4882a593Smuzhiyun 	convert_delimiter(*target_path, '/');
2848*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2849*4882a593Smuzhiyun 
2850*4882a593Smuzhiyun 	return 0;
2851*4882a593Smuzhiyun }
2852*4882a593Smuzhiyun 
2853*4882a593Smuzhiyun static int
parse_reparse_point(struct reparse_data_buffer * buf,u32 plen,char ** target_path,struct cifs_sb_info * cifs_sb)2854*4882a593Smuzhiyun parse_reparse_point(struct reparse_data_buffer *buf,
2855*4882a593Smuzhiyun 		    u32 plen, char **target_path,
2856*4882a593Smuzhiyun 		    struct cifs_sb_info *cifs_sb)
2857*4882a593Smuzhiyun {
2858*4882a593Smuzhiyun 	if (plen < sizeof(struct reparse_data_buffer)) {
2859*4882a593Smuzhiyun 		cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2860*4882a593Smuzhiyun 			 plen);
2861*4882a593Smuzhiyun 		return -EIO;
2862*4882a593Smuzhiyun 	}
2863*4882a593Smuzhiyun 
2864*4882a593Smuzhiyun 	if (plen < le16_to_cpu(buf->ReparseDataLength) +
2865*4882a593Smuzhiyun 	    sizeof(struct reparse_data_buffer)) {
2866*4882a593Smuzhiyun 		cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2867*4882a593Smuzhiyun 			 plen);
2868*4882a593Smuzhiyun 		return -EIO;
2869*4882a593Smuzhiyun 	}
2870*4882a593Smuzhiyun 
2871*4882a593Smuzhiyun 	/* See MS-FSCC 2.1.2 */
2872*4882a593Smuzhiyun 	switch (le32_to_cpu(buf->ReparseTag)) {
2873*4882a593Smuzhiyun 	case IO_REPARSE_TAG_NFS:
2874*4882a593Smuzhiyun 		return parse_reparse_posix(
2875*4882a593Smuzhiyun 			(struct reparse_posix_data *)buf,
2876*4882a593Smuzhiyun 			plen, target_path, cifs_sb);
2877*4882a593Smuzhiyun 	case IO_REPARSE_TAG_SYMLINK:
2878*4882a593Smuzhiyun 		return parse_reparse_symlink(
2879*4882a593Smuzhiyun 			(struct reparse_symlink_data_buffer *)buf,
2880*4882a593Smuzhiyun 			plen, target_path, cifs_sb);
2881*4882a593Smuzhiyun 	default:
2882*4882a593Smuzhiyun 		cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2883*4882a593Smuzhiyun 			 le32_to_cpu(buf->ReparseTag));
2884*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2885*4882a593Smuzhiyun 	}
2886*4882a593Smuzhiyun }
2887*4882a593Smuzhiyun 
2888*4882a593Smuzhiyun #define SMB2_SYMLINK_STRUCT_SIZE \
2889*4882a593Smuzhiyun 	(sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun static int
smb2_query_symlink(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,char ** target_path,bool is_reparse_point)2892*4882a593Smuzhiyun smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2893*4882a593Smuzhiyun 		   struct cifs_sb_info *cifs_sb, const char *full_path,
2894*4882a593Smuzhiyun 		   char **target_path, bool is_reparse_point)
2895*4882a593Smuzhiyun {
2896*4882a593Smuzhiyun 	int rc;
2897*4882a593Smuzhiyun 	__le16 *utf16_path = NULL;
2898*4882a593Smuzhiyun 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2899*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
2900*4882a593Smuzhiyun 	struct cifs_fid fid;
2901*4882a593Smuzhiyun 	struct kvec err_iov = {NULL, 0};
2902*4882a593Smuzhiyun 	struct smb2_err_rsp *err_buf = NULL;
2903*4882a593Smuzhiyun 	struct smb2_symlink_err_rsp *symlink;
2904*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2905*4882a593Smuzhiyun 	unsigned int sub_len;
2906*4882a593Smuzhiyun 	unsigned int sub_offset;
2907*4882a593Smuzhiyun 	unsigned int print_len;
2908*4882a593Smuzhiyun 	unsigned int print_offset;
2909*4882a593Smuzhiyun 	int flags = CIFS_CP_CREATE_CLOSE_OP;
2910*4882a593Smuzhiyun 	struct smb_rqst rqst[3];
2911*4882a593Smuzhiyun 	int resp_buftype[3];
2912*4882a593Smuzhiyun 	struct kvec rsp_iov[3];
2913*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2914*4882a593Smuzhiyun 	struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2915*4882a593Smuzhiyun 	struct kvec close_iov[1];
2916*4882a593Smuzhiyun 	struct smb2_create_rsp *create_rsp;
2917*4882a593Smuzhiyun 	struct smb2_ioctl_rsp *ioctl_rsp;
2918*4882a593Smuzhiyun 	struct reparse_data_buffer *reparse_buf;
2919*4882a593Smuzhiyun 	int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2920*4882a593Smuzhiyun 	u32 plen;
2921*4882a593Smuzhiyun 
2922*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2923*4882a593Smuzhiyun 
2924*4882a593Smuzhiyun 	*target_path = NULL;
2925*4882a593Smuzhiyun 
2926*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
2927*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
2928*4882a593Smuzhiyun 
2929*4882a593Smuzhiyun 	memset(rqst, 0, sizeof(rqst));
2930*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2931*4882a593Smuzhiyun 	memset(rsp_iov, 0, sizeof(rsp_iov));
2932*4882a593Smuzhiyun 
2933*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2934*4882a593Smuzhiyun 	if (!utf16_path)
2935*4882a593Smuzhiyun 		return -ENOMEM;
2936*4882a593Smuzhiyun 
2937*4882a593Smuzhiyun 	/* Open */
2938*4882a593Smuzhiyun 	memset(&open_iov, 0, sizeof(open_iov));
2939*4882a593Smuzhiyun 	rqst[0].rq_iov = open_iov;
2940*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2941*4882a593Smuzhiyun 
2942*4882a593Smuzhiyun 	memset(&oparms, 0, sizeof(oparms));
2943*4882a593Smuzhiyun 	oparms.tcon = tcon;
2944*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
2945*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
2946*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, create_options);
2947*4882a593Smuzhiyun 	oparms.fid = &fid;
2948*4882a593Smuzhiyun 	oparms.reconnect = false;
2949*4882a593Smuzhiyun 
2950*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
2951*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, utf16_path);
2952*4882a593Smuzhiyun 	if (rc)
2953*4882a593Smuzhiyun 		goto querty_exit;
2954*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
2955*4882a593Smuzhiyun 
2956*4882a593Smuzhiyun 
2957*4882a593Smuzhiyun 	/* IOCTL */
2958*4882a593Smuzhiyun 	memset(&io_iov, 0, sizeof(io_iov));
2959*4882a593Smuzhiyun 	rqst[1].rq_iov = io_iov;
2960*4882a593Smuzhiyun 	rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2961*4882a593Smuzhiyun 
2962*4882a593Smuzhiyun 	rc = SMB2_ioctl_init(tcon, server,
2963*4882a593Smuzhiyun 			     &rqst[1], fid.persistent_fid,
2964*4882a593Smuzhiyun 			     fid.volatile_fid, FSCTL_GET_REPARSE_POINT, NULL, 0,
2965*4882a593Smuzhiyun 			     CIFSMaxBufSize -
2966*4882a593Smuzhiyun 			     MAX_SMB2_CREATE_RESPONSE_SIZE -
2967*4882a593Smuzhiyun 			     MAX_SMB2_CLOSE_RESPONSE_SIZE);
2968*4882a593Smuzhiyun 	if (rc)
2969*4882a593Smuzhiyun 		goto querty_exit;
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[1]);
2972*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
2973*4882a593Smuzhiyun 
2974*4882a593Smuzhiyun 
2975*4882a593Smuzhiyun 	/* Close */
2976*4882a593Smuzhiyun 	memset(&close_iov, 0, sizeof(close_iov));
2977*4882a593Smuzhiyun 	rqst[2].rq_iov = close_iov;
2978*4882a593Smuzhiyun 	rqst[2].rq_nvec = 1;
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun 	rc = SMB2_close_init(tcon, server,
2981*4882a593Smuzhiyun 			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2982*4882a593Smuzhiyun 	if (rc)
2983*4882a593Smuzhiyun 		goto querty_exit;
2984*4882a593Smuzhiyun 
2985*4882a593Smuzhiyun 	smb2_set_related(&rqst[2]);
2986*4882a593Smuzhiyun 
2987*4882a593Smuzhiyun 	rc = compound_send_recv(xid, tcon->ses, server,
2988*4882a593Smuzhiyun 				flags, 3, rqst,
2989*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
2990*4882a593Smuzhiyun 
2991*4882a593Smuzhiyun 	create_rsp = rsp_iov[0].iov_base;
2992*4882a593Smuzhiyun 	if (create_rsp && create_rsp->sync_hdr.Status)
2993*4882a593Smuzhiyun 		err_iov = rsp_iov[0];
2994*4882a593Smuzhiyun 	ioctl_rsp = rsp_iov[1].iov_base;
2995*4882a593Smuzhiyun 
2996*4882a593Smuzhiyun 	/*
2997*4882a593Smuzhiyun 	 * Open was successful and we got an ioctl response.
2998*4882a593Smuzhiyun 	 */
2999*4882a593Smuzhiyun 	if ((rc == 0) && (is_reparse_point)) {
3000*4882a593Smuzhiyun 		/* See MS-FSCC 2.3.23 */
3001*4882a593Smuzhiyun 
3002*4882a593Smuzhiyun 		reparse_buf = (struct reparse_data_buffer *)
3003*4882a593Smuzhiyun 			((char *)ioctl_rsp +
3004*4882a593Smuzhiyun 			 le32_to_cpu(ioctl_rsp->OutputOffset));
3005*4882a593Smuzhiyun 		plen = le32_to_cpu(ioctl_rsp->OutputCount);
3006*4882a593Smuzhiyun 
3007*4882a593Smuzhiyun 		if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3008*4882a593Smuzhiyun 		    rsp_iov[1].iov_len) {
3009*4882a593Smuzhiyun 			cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
3010*4882a593Smuzhiyun 				 plen);
3011*4882a593Smuzhiyun 			rc = -EIO;
3012*4882a593Smuzhiyun 			goto querty_exit;
3013*4882a593Smuzhiyun 		}
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun 		rc = parse_reparse_point(reparse_buf, plen, target_path,
3016*4882a593Smuzhiyun 					 cifs_sb);
3017*4882a593Smuzhiyun 		goto querty_exit;
3018*4882a593Smuzhiyun 	}
3019*4882a593Smuzhiyun 
3020*4882a593Smuzhiyun 	if (!rc || !err_iov.iov_base) {
3021*4882a593Smuzhiyun 		rc = -ENOENT;
3022*4882a593Smuzhiyun 		goto querty_exit;
3023*4882a593Smuzhiyun 	}
3024*4882a593Smuzhiyun 
3025*4882a593Smuzhiyun 	err_buf = err_iov.iov_base;
3026*4882a593Smuzhiyun 	if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3027*4882a593Smuzhiyun 	    err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
3028*4882a593Smuzhiyun 		rc = -EINVAL;
3029*4882a593Smuzhiyun 		goto querty_exit;
3030*4882a593Smuzhiyun 	}
3031*4882a593Smuzhiyun 
3032*4882a593Smuzhiyun 	symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3033*4882a593Smuzhiyun 	if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3034*4882a593Smuzhiyun 	    le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3035*4882a593Smuzhiyun 		rc = -EINVAL;
3036*4882a593Smuzhiyun 		goto querty_exit;
3037*4882a593Smuzhiyun 	}
3038*4882a593Smuzhiyun 
3039*4882a593Smuzhiyun 	/* open must fail on symlink - reset rc */
3040*4882a593Smuzhiyun 	rc = 0;
3041*4882a593Smuzhiyun 	sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3042*4882a593Smuzhiyun 	sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3043*4882a593Smuzhiyun 	print_len = le16_to_cpu(symlink->PrintNameLength);
3044*4882a593Smuzhiyun 	print_offset = le16_to_cpu(symlink->PrintNameOffset);
3045*4882a593Smuzhiyun 
3046*4882a593Smuzhiyun 	if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3047*4882a593Smuzhiyun 		rc = -EINVAL;
3048*4882a593Smuzhiyun 		goto querty_exit;
3049*4882a593Smuzhiyun 	}
3050*4882a593Smuzhiyun 
3051*4882a593Smuzhiyun 	if (err_iov.iov_len <
3052*4882a593Smuzhiyun 	    SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3053*4882a593Smuzhiyun 		rc = -EINVAL;
3054*4882a593Smuzhiyun 		goto querty_exit;
3055*4882a593Smuzhiyun 	}
3056*4882a593Smuzhiyun 
3057*4882a593Smuzhiyun 	*target_path = cifs_strndup_from_utf16(
3058*4882a593Smuzhiyun 				(char *)symlink->PathBuffer + sub_offset,
3059*4882a593Smuzhiyun 				sub_len, true, cifs_sb->local_nls);
3060*4882a593Smuzhiyun 	if (!(*target_path)) {
3061*4882a593Smuzhiyun 		rc = -ENOMEM;
3062*4882a593Smuzhiyun 		goto querty_exit;
3063*4882a593Smuzhiyun 	}
3064*4882a593Smuzhiyun 	convert_delimiter(*target_path, '/');
3065*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3066*4882a593Smuzhiyun 
3067*4882a593Smuzhiyun  querty_exit:
3068*4882a593Smuzhiyun 	cifs_dbg(FYI, "query symlink rc %d\n", rc);
3069*4882a593Smuzhiyun 	kfree(utf16_path);
3070*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
3071*4882a593Smuzhiyun 	SMB2_ioctl_free(&rqst[1]);
3072*4882a593Smuzhiyun 	SMB2_close_free(&rqst[2]);
3073*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3074*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3075*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3076*4882a593Smuzhiyun 	return rc;
3077*4882a593Smuzhiyun }
3078*4882a593Smuzhiyun 
3079*4882a593Smuzhiyun int
smb2_query_reparse_tag(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,__u32 * tag)3080*4882a593Smuzhiyun smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3081*4882a593Smuzhiyun 		   struct cifs_sb_info *cifs_sb, const char *full_path,
3082*4882a593Smuzhiyun 		   __u32 *tag)
3083*4882a593Smuzhiyun {
3084*4882a593Smuzhiyun 	int rc;
3085*4882a593Smuzhiyun 	__le16 *utf16_path = NULL;
3086*4882a593Smuzhiyun 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3087*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
3088*4882a593Smuzhiyun 	struct cifs_fid fid;
3089*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3090*4882a593Smuzhiyun 	int flags = CIFS_CP_CREATE_CLOSE_OP;
3091*4882a593Smuzhiyun 	struct smb_rqst rqst[3];
3092*4882a593Smuzhiyun 	int resp_buftype[3];
3093*4882a593Smuzhiyun 	struct kvec rsp_iov[3];
3094*4882a593Smuzhiyun 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3095*4882a593Smuzhiyun 	struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3096*4882a593Smuzhiyun 	struct kvec close_iov[1];
3097*4882a593Smuzhiyun 	struct smb2_ioctl_rsp *ioctl_rsp;
3098*4882a593Smuzhiyun 	struct reparse_data_buffer *reparse_buf;
3099*4882a593Smuzhiyun 	u32 plen;
3100*4882a593Smuzhiyun 
3101*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3102*4882a593Smuzhiyun 
3103*4882a593Smuzhiyun 	if (smb3_encryption_required(tcon))
3104*4882a593Smuzhiyun 		flags |= CIFS_TRANSFORM_REQ;
3105*4882a593Smuzhiyun 
3106*4882a593Smuzhiyun 	memset(rqst, 0, sizeof(rqst));
3107*4882a593Smuzhiyun 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3108*4882a593Smuzhiyun 	memset(rsp_iov, 0, sizeof(rsp_iov));
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3111*4882a593Smuzhiyun 	if (!utf16_path)
3112*4882a593Smuzhiyun 		return -ENOMEM;
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun 	/*
3115*4882a593Smuzhiyun 	 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3116*4882a593Smuzhiyun 	 * to see if there is a handle already open that we can use
3117*4882a593Smuzhiyun 	 */
3118*4882a593Smuzhiyun 	memset(&open_iov, 0, sizeof(open_iov));
3119*4882a593Smuzhiyun 	rqst[0].rq_iov = open_iov;
3120*4882a593Smuzhiyun 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3121*4882a593Smuzhiyun 
3122*4882a593Smuzhiyun 	memset(&oparms, 0, sizeof(oparms));
3123*4882a593Smuzhiyun 	oparms.tcon = tcon;
3124*4882a593Smuzhiyun 	oparms.desired_access = FILE_READ_ATTRIBUTES;
3125*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
3126*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3127*4882a593Smuzhiyun 	oparms.fid = &fid;
3128*4882a593Smuzhiyun 	oparms.reconnect = false;
3129*4882a593Smuzhiyun 
3130*4882a593Smuzhiyun 	rc = SMB2_open_init(tcon, server,
3131*4882a593Smuzhiyun 			    &rqst[0], &oplock, &oparms, utf16_path);
3132*4882a593Smuzhiyun 	if (rc)
3133*4882a593Smuzhiyun 		goto query_rp_exit;
3134*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[0]);
3135*4882a593Smuzhiyun 
3136*4882a593Smuzhiyun 
3137*4882a593Smuzhiyun 	/* IOCTL */
3138*4882a593Smuzhiyun 	memset(&io_iov, 0, sizeof(io_iov));
3139*4882a593Smuzhiyun 	rqst[1].rq_iov = io_iov;
3140*4882a593Smuzhiyun 	rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3141*4882a593Smuzhiyun 
3142*4882a593Smuzhiyun 	rc = SMB2_ioctl_init(tcon, server,
3143*4882a593Smuzhiyun 			     &rqst[1], COMPOUND_FID,
3144*4882a593Smuzhiyun 			     COMPOUND_FID, FSCTL_GET_REPARSE_POINT, NULL, 0,
3145*4882a593Smuzhiyun 			     CIFSMaxBufSize -
3146*4882a593Smuzhiyun 			     MAX_SMB2_CREATE_RESPONSE_SIZE -
3147*4882a593Smuzhiyun 			     MAX_SMB2_CLOSE_RESPONSE_SIZE);
3148*4882a593Smuzhiyun 	if (rc)
3149*4882a593Smuzhiyun 		goto query_rp_exit;
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun 	smb2_set_next_command(tcon, &rqst[1]);
3152*4882a593Smuzhiyun 	smb2_set_related(&rqst[1]);
3153*4882a593Smuzhiyun 
3154*4882a593Smuzhiyun 
3155*4882a593Smuzhiyun 	/* Close */
3156*4882a593Smuzhiyun 	memset(&close_iov, 0, sizeof(close_iov));
3157*4882a593Smuzhiyun 	rqst[2].rq_iov = close_iov;
3158*4882a593Smuzhiyun 	rqst[2].rq_nvec = 1;
3159*4882a593Smuzhiyun 
3160*4882a593Smuzhiyun 	rc = SMB2_close_init(tcon, server,
3161*4882a593Smuzhiyun 			     &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3162*4882a593Smuzhiyun 	if (rc)
3163*4882a593Smuzhiyun 		goto query_rp_exit;
3164*4882a593Smuzhiyun 
3165*4882a593Smuzhiyun 	smb2_set_related(&rqst[2]);
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 	rc = compound_send_recv(xid, tcon->ses, server,
3168*4882a593Smuzhiyun 				flags, 3, rqst,
3169*4882a593Smuzhiyun 				resp_buftype, rsp_iov);
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 	ioctl_rsp = rsp_iov[1].iov_base;
3172*4882a593Smuzhiyun 
3173*4882a593Smuzhiyun 	/*
3174*4882a593Smuzhiyun 	 * Open was successful and we got an ioctl response.
3175*4882a593Smuzhiyun 	 */
3176*4882a593Smuzhiyun 	if (rc == 0) {
3177*4882a593Smuzhiyun 		/* See MS-FSCC 2.3.23 */
3178*4882a593Smuzhiyun 
3179*4882a593Smuzhiyun 		reparse_buf = (struct reparse_data_buffer *)
3180*4882a593Smuzhiyun 			((char *)ioctl_rsp +
3181*4882a593Smuzhiyun 			 le32_to_cpu(ioctl_rsp->OutputOffset));
3182*4882a593Smuzhiyun 		plen = le32_to_cpu(ioctl_rsp->OutputCount);
3183*4882a593Smuzhiyun 
3184*4882a593Smuzhiyun 		if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3185*4882a593Smuzhiyun 		    rsp_iov[1].iov_len) {
3186*4882a593Smuzhiyun 			cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3187*4882a593Smuzhiyun 				 plen);
3188*4882a593Smuzhiyun 			rc = -EIO;
3189*4882a593Smuzhiyun 			goto query_rp_exit;
3190*4882a593Smuzhiyun 		}
3191*4882a593Smuzhiyun 		*tag = le32_to_cpu(reparse_buf->ReparseTag);
3192*4882a593Smuzhiyun 	}
3193*4882a593Smuzhiyun 
3194*4882a593Smuzhiyun  query_rp_exit:
3195*4882a593Smuzhiyun 	kfree(utf16_path);
3196*4882a593Smuzhiyun 	SMB2_open_free(&rqst[0]);
3197*4882a593Smuzhiyun 	SMB2_ioctl_free(&rqst[1]);
3198*4882a593Smuzhiyun 	SMB2_close_free(&rqst[2]);
3199*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3200*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3201*4882a593Smuzhiyun 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3202*4882a593Smuzhiyun 	return rc;
3203*4882a593Smuzhiyun }
3204*4882a593Smuzhiyun 
3205*4882a593Smuzhiyun static struct cifs_ntsd *
get_smb2_acl_by_fid(struct cifs_sb_info * cifs_sb,const struct cifs_fid * cifsfid,u32 * pacllen)3206*4882a593Smuzhiyun get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3207*4882a593Smuzhiyun 		const struct cifs_fid *cifsfid, u32 *pacllen)
3208*4882a593Smuzhiyun {
3209*4882a593Smuzhiyun 	struct cifs_ntsd *pntsd = NULL;
3210*4882a593Smuzhiyun 	unsigned int xid;
3211*4882a593Smuzhiyun 	int rc = -EOPNOTSUPP;
3212*4882a593Smuzhiyun 	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3213*4882a593Smuzhiyun 
3214*4882a593Smuzhiyun 	if (IS_ERR(tlink))
3215*4882a593Smuzhiyun 		return ERR_CAST(tlink);
3216*4882a593Smuzhiyun 
3217*4882a593Smuzhiyun 	xid = get_xid();
3218*4882a593Smuzhiyun 	cifs_dbg(FYI, "trying to get acl\n");
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun 	rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3221*4882a593Smuzhiyun 			    cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3222*4882a593Smuzhiyun 	free_xid(xid);
3223*4882a593Smuzhiyun 
3224*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
3225*4882a593Smuzhiyun 
3226*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3227*4882a593Smuzhiyun 	if (rc)
3228*4882a593Smuzhiyun 		return ERR_PTR(rc);
3229*4882a593Smuzhiyun 	return pntsd;
3230*4882a593Smuzhiyun 
3231*4882a593Smuzhiyun }
3232*4882a593Smuzhiyun 
3233*4882a593Smuzhiyun static struct cifs_ntsd *
get_smb2_acl_by_path(struct cifs_sb_info * cifs_sb,const char * path,u32 * pacllen)3234*4882a593Smuzhiyun get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3235*4882a593Smuzhiyun 		const char *path, u32 *pacllen)
3236*4882a593Smuzhiyun {
3237*4882a593Smuzhiyun 	struct cifs_ntsd *pntsd = NULL;
3238*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3239*4882a593Smuzhiyun 	unsigned int xid;
3240*4882a593Smuzhiyun 	int rc;
3241*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
3242*4882a593Smuzhiyun 	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3243*4882a593Smuzhiyun 	struct cifs_fid fid;
3244*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
3245*4882a593Smuzhiyun 	__le16 *utf16_path;
3246*4882a593Smuzhiyun 
3247*4882a593Smuzhiyun 	cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3248*4882a593Smuzhiyun 	if (IS_ERR(tlink))
3249*4882a593Smuzhiyun 		return ERR_CAST(tlink);
3250*4882a593Smuzhiyun 
3251*4882a593Smuzhiyun 	tcon = tlink_tcon(tlink);
3252*4882a593Smuzhiyun 	xid = get_xid();
3253*4882a593Smuzhiyun 
3254*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3255*4882a593Smuzhiyun 	if (!utf16_path) {
3256*4882a593Smuzhiyun 		rc = -ENOMEM;
3257*4882a593Smuzhiyun 		free_xid(xid);
3258*4882a593Smuzhiyun 		return ERR_PTR(rc);
3259*4882a593Smuzhiyun 	}
3260*4882a593Smuzhiyun 
3261*4882a593Smuzhiyun 	oparms.tcon = tcon;
3262*4882a593Smuzhiyun 	oparms.desired_access = READ_CONTROL;
3263*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
3264*4882a593Smuzhiyun 	/*
3265*4882a593Smuzhiyun 	 * When querying an ACL, even if the file is a symlink we want to open
3266*4882a593Smuzhiyun 	 * the source not the target, and so the protocol requires that the
3267*4882a593Smuzhiyun 	 * client specify this flag when opening a reparse point
3268*4882a593Smuzhiyun 	 */
3269*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3270*4882a593Smuzhiyun 	oparms.fid = &fid;
3271*4882a593Smuzhiyun 	oparms.reconnect = false;
3272*4882a593Smuzhiyun 
3273*4882a593Smuzhiyun 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3274*4882a593Smuzhiyun 		       NULL);
3275*4882a593Smuzhiyun 	kfree(utf16_path);
3276*4882a593Smuzhiyun 	if (!rc) {
3277*4882a593Smuzhiyun 		rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3278*4882a593Smuzhiyun 			    fid.volatile_fid, (void **)&pntsd, pacllen);
3279*4882a593Smuzhiyun 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3280*4882a593Smuzhiyun 	}
3281*4882a593Smuzhiyun 
3282*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
3283*4882a593Smuzhiyun 	free_xid(xid);
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3286*4882a593Smuzhiyun 	if (rc)
3287*4882a593Smuzhiyun 		return ERR_PTR(rc);
3288*4882a593Smuzhiyun 	return pntsd;
3289*4882a593Smuzhiyun }
3290*4882a593Smuzhiyun 
3291*4882a593Smuzhiyun static int
set_smb2_acl(struct cifs_ntsd * pnntsd,__u32 acllen,struct inode * inode,const char * path,int aclflag)3292*4882a593Smuzhiyun set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3293*4882a593Smuzhiyun 		struct inode *inode, const char *path, int aclflag)
3294*4882a593Smuzhiyun {
3295*4882a593Smuzhiyun 	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3296*4882a593Smuzhiyun 	unsigned int xid;
3297*4882a593Smuzhiyun 	int rc, access_flags = 0;
3298*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
3299*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3300*4882a593Smuzhiyun 	struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3301*4882a593Smuzhiyun 	struct cifs_fid fid;
3302*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
3303*4882a593Smuzhiyun 	__le16 *utf16_path;
3304*4882a593Smuzhiyun 
3305*4882a593Smuzhiyun 	cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3306*4882a593Smuzhiyun 	if (IS_ERR(tlink))
3307*4882a593Smuzhiyun 		return PTR_ERR(tlink);
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 	tcon = tlink_tcon(tlink);
3310*4882a593Smuzhiyun 	xid = get_xid();
3311*4882a593Smuzhiyun 
3312*4882a593Smuzhiyun 	if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3313*4882a593Smuzhiyun 		access_flags = WRITE_OWNER;
3314*4882a593Smuzhiyun 	else
3315*4882a593Smuzhiyun 		access_flags = WRITE_DAC;
3316*4882a593Smuzhiyun 
3317*4882a593Smuzhiyun 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3318*4882a593Smuzhiyun 	if (!utf16_path) {
3319*4882a593Smuzhiyun 		rc = -ENOMEM;
3320*4882a593Smuzhiyun 		free_xid(xid);
3321*4882a593Smuzhiyun 		return rc;
3322*4882a593Smuzhiyun 	}
3323*4882a593Smuzhiyun 
3324*4882a593Smuzhiyun 	oparms.tcon = tcon;
3325*4882a593Smuzhiyun 	oparms.desired_access = access_flags;
3326*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, 0);
3327*4882a593Smuzhiyun 	oparms.disposition = FILE_OPEN;
3328*4882a593Smuzhiyun 	oparms.path = path;
3329*4882a593Smuzhiyun 	oparms.fid = &fid;
3330*4882a593Smuzhiyun 	oparms.reconnect = false;
3331*4882a593Smuzhiyun 
3332*4882a593Smuzhiyun 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3333*4882a593Smuzhiyun 		       NULL, NULL);
3334*4882a593Smuzhiyun 	kfree(utf16_path);
3335*4882a593Smuzhiyun 	if (!rc) {
3336*4882a593Smuzhiyun 		rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3337*4882a593Smuzhiyun 			    fid.volatile_fid, pnntsd, acllen, aclflag);
3338*4882a593Smuzhiyun 		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3339*4882a593Smuzhiyun 	}
3340*4882a593Smuzhiyun 
3341*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
3342*4882a593Smuzhiyun 	free_xid(xid);
3343*4882a593Smuzhiyun 	return rc;
3344*4882a593Smuzhiyun }
3345*4882a593Smuzhiyun 
3346*4882a593Smuzhiyun /* Retrieve an ACL from the server */
3347*4882a593Smuzhiyun static struct cifs_ntsd *
get_smb2_acl(struct cifs_sb_info * cifs_sb,struct inode * inode,const char * path,u32 * pacllen)3348*4882a593Smuzhiyun get_smb2_acl(struct cifs_sb_info *cifs_sb,
3349*4882a593Smuzhiyun 				      struct inode *inode, const char *path,
3350*4882a593Smuzhiyun 				      u32 *pacllen)
3351*4882a593Smuzhiyun {
3352*4882a593Smuzhiyun 	struct cifs_ntsd *pntsd = NULL;
3353*4882a593Smuzhiyun 	struct cifsFileInfo *open_file = NULL;
3354*4882a593Smuzhiyun 
3355*4882a593Smuzhiyun 	if (inode)
3356*4882a593Smuzhiyun 		open_file = find_readable_file(CIFS_I(inode), true);
3357*4882a593Smuzhiyun 	if (!open_file)
3358*4882a593Smuzhiyun 		return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3359*4882a593Smuzhiyun 
3360*4882a593Smuzhiyun 	pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3361*4882a593Smuzhiyun 	cifsFileInfo_put(open_file);
3362*4882a593Smuzhiyun 	return pntsd;
3363*4882a593Smuzhiyun }
3364*4882a593Smuzhiyun 
smb3_zero_range(struct file * file,struct cifs_tcon * tcon,loff_t offset,loff_t len,bool keep_size)3365*4882a593Smuzhiyun static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3366*4882a593Smuzhiyun 			    loff_t offset, loff_t len, bool keep_size)
3367*4882a593Smuzhiyun {
3368*4882a593Smuzhiyun 	struct cifs_ses *ses = tcon->ses;
3369*4882a593Smuzhiyun 	struct inode *inode;
3370*4882a593Smuzhiyun 	struct cifsInodeInfo *cifsi;
3371*4882a593Smuzhiyun 	struct cifsFileInfo *cfile = file->private_data;
3372*4882a593Smuzhiyun 	struct file_zero_data_information fsctl_buf;
3373*4882a593Smuzhiyun 	long rc;
3374*4882a593Smuzhiyun 	unsigned int xid;
3375*4882a593Smuzhiyun 	__le64 eof;
3376*4882a593Smuzhiyun 
3377*4882a593Smuzhiyun 	xid = get_xid();
3378*4882a593Smuzhiyun 
3379*4882a593Smuzhiyun 	inode = d_inode(cfile->dentry);
3380*4882a593Smuzhiyun 	cifsi = CIFS_I(inode);
3381*4882a593Smuzhiyun 
3382*4882a593Smuzhiyun 	trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3383*4882a593Smuzhiyun 			      ses->Suid, offset, len);
3384*4882a593Smuzhiyun 
3385*4882a593Smuzhiyun 	/*
3386*4882a593Smuzhiyun 	 * We zero the range through ioctl, so we need remove the page caches
3387*4882a593Smuzhiyun 	 * first, otherwise the data may be inconsistent with the server.
3388*4882a593Smuzhiyun 	 */
3389*4882a593Smuzhiyun 	truncate_pagecache_range(inode, offset, offset + len - 1);
3390*4882a593Smuzhiyun 
3391*4882a593Smuzhiyun 	/* if file not oplocked can't be sure whether asking to extend size */
3392*4882a593Smuzhiyun 	if (!CIFS_CACHE_READ(cifsi))
3393*4882a593Smuzhiyun 		if (keep_size == false) {
3394*4882a593Smuzhiyun 			rc = -EOPNOTSUPP;
3395*4882a593Smuzhiyun 			trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3396*4882a593Smuzhiyun 				tcon->tid, ses->Suid, offset, len, rc);
3397*4882a593Smuzhiyun 			free_xid(xid);
3398*4882a593Smuzhiyun 			return rc;
3399*4882a593Smuzhiyun 		}
3400*4882a593Smuzhiyun 
3401*4882a593Smuzhiyun 	cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3402*4882a593Smuzhiyun 
3403*4882a593Smuzhiyun 	fsctl_buf.FileOffset = cpu_to_le64(offset);
3404*4882a593Smuzhiyun 	fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3405*4882a593Smuzhiyun 
3406*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3407*4882a593Smuzhiyun 			cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3408*4882a593Smuzhiyun 			(char *)&fsctl_buf,
3409*4882a593Smuzhiyun 			sizeof(struct file_zero_data_information),
3410*4882a593Smuzhiyun 			0, NULL, NULL);
3411*4882a593Smuzhiyun 	if (rc)
3412*4882a593Smuzhiyun 		goto zero_range_exit;
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun 	/*
3415*4882a593Smuzhiyun 	 * do we also need to change the size of the file?
3416*4882a593Smuzhiyun 	 */
3417*4882a593Smuzhiyun 	if (keep_size == false && i_size_read(inode) < offset + len) {
3418*4882a593Smuzhiyun 		eof = cpu_to_le64(offset + len);
3419*4882a593Smuzhiyun 		rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3420*4882a593Smuzhiyun 				  cfile->fid.volatile_fid, cfile->pid, &eof);
3421*4882a593Smuzhiyun 	}
3422*4882a593Smuzhiyun 
3423*4882a593Smuzhiyun  zero_range_exit:
3424*4882a593Smuzhiyun 	free_xid(xid);
3425*4882a593Smuzhiyun 	if (rc)
3426*4882a593Smuzhiyun 		trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3427*4882a593Smuzhiyun 			      ses->Suid, offset, len, rc);
3428*4882a593Smuzhiyun 	else
3429*4882a593Smuzhiyun 		trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3430*4882a593Smuzhiyun 			      ses->Suid, offset, len);
3431*4882a593Smuzhiyun 	return rc;
3432*4882a593Smuzhiyun }
3433*4882a593Smuzhiyun 
smb3_punch_hole(struct file * file,struct cifs_tcon * tcon,loff_t offset,loff_t len)3434*4882a593Smuzhiyun static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3435*4882a593Smuzhiyun 			    loff_t offset, loff_t len)
3436*4882a593Smuzhiyun {
3437*4882a593Smuzhiyun 	struct inode *inode = file_inode(file);
3438*4882a593Smuzhiyun 	struct cifsFileInfo *cfile = file->private_data;
3439*4882a593Smuzhiyun 	struct file_zero_data_information fsctl_buf;
3440*4882a593Smuzhiyun 	long rc;
3441*4882a593Smuzhiyun 	unsigned int xid;
3442*4882a593Smuzhiyun 	__u8 set_sparse = 1;
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 	xid = get_xid();
3445*4882a593Smuzhiyun 
3446*4882a593Smuzhiyun 	inode_lock(inode);
3447*4882a593Smuzhiyun 	/* Need to make file sparse, if not already, before freeing range. */
3448*4882a593Smuzhiyun 	/* Consider adding equivalent for compressed since it could also work */
3449*4882a593Smuzhiyun 	if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3450*4882a593Smuzhiyun 		rc = -EOPNOTSUPP;
3451*4882a593Smuzhiyun 		goto out;
3452*4882a593Smuzhiyun 	}
3453*4882a593Smuzhiyun 
3454*4882a593Smuzhiyun 	/*
3455*4882a593Smuzhiyun 	 * We implement the punch hole through ioctl, so we need remove the page
3456*4882a593Smuzhiyun 	 * caches first, otherwise the data may be inconsistent with the server.
3457*4882a593Smuzhiyun 	 */
3458*4882a593Smuzhiyun 	truncate_pagecache_range(inode, offset, offset + len - 1);
3459*4882a593Smuzhiyun 
3460*4882a593Smuzhiyun 	cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3461*4882a593Smuzhiyun 
3462*4882a593Smuzhiyun 	fsctl_buf.FileOffset = cpu_to_le64(offset);
3463*4882a593Smuzhiyun 	fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3464*4882a593Smuzhiyun 
3465*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3466*4882a593Smuzhiyun 			cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3467*4882a593Smuzhiyun 			(char *)&fsctl_buf,
3468*4882a593Smuzhiyun 			sizeof(struct file_zero_data_information),
3469*4882a593Smuzhiyun 			CIFSMaxBufSize, NULL, NULL);
3470*4882a593Smuzhiyun out:
3471*4882a593Smuzhiyun 	inode_unlock(inode);
3472*4882a593Smuzhiyun 	free_xid(xid);
3473*4882a593Smuzhiyun 	return rc;
3474*4882a593Smuzhiyun }
3475*4882a593Smuzhiyun 
smb3_simple_fallocate_write_range(unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,loff_t off,loff_t len,char * buf)3476*4882a593Smuzhiyun static int smb3_simple_fallocate_write_range(unsigned int xid,
3477*4882a593Smuzhiyun 					     struct cifs_tcon *tcon,
3478*4882a593Smuzhiyun 					     struct cifsFileInfo *cfile,
3479*4882a593Smuzhiyun 					     loff_t off, loff_t len,
3480*4882a593Smuzhiyun 					     char *buf)
3481*4882a593Smuzhiyun {
3482*4882a593Smuzhiyun 	struct cifs_io_parms io_parms = {0};
3483*4882a593Smuzhiyun 	int nbytes;
3484*4882a593Smuzhiyun 	int rc = 0;
3485*4882a593Smuzhiyun 	struct kvec iov[2];
3486*4882a593Smuzhiyun 
3487*4882a593Smuzhiyun 	io_parms.netfid = cfile->fid.netfid;
3488*4882a593Smuzhiyun 	io_parms.pid = current->tgid;
3489*4882a593Smuzhiyun 	io_parms.tcon = tcon;
3490*4882a593Smuzhiyun 	io_parms.persistent_fid = cfile->fid.persistent_fid;
3491*4882a593Smuzhiyun 	io_parms.volatile_fid = cfile->fid.volatile_fid;
3492*4882a593Smuzhiyun 
3493*4882a593Smuzhiyun 	while (len) {
3494*4882a593Smuzhiyun 		io_parms.offset = off;
3495*4882a593Smuzhiyun 		io_parms.length = len;
3496*4882a593Smuzhiyun 		if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3497*4882a593Smuzhiyun 			io_parms.length = SMB2_MAX_BUFFER_SIZE;
3498*4882a593Smuzhiyun 		/* iov[0] is reserved for smb header */
3499*4882a593Smuzhiyun 		iov[1].iov_base = buf;
3500*4882a593Smuzhiyun 		iov[1].iov_len = io_parms.length;
3501*4882a593Smuzhiyun 		rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3502*4882a593Smuzhiyun 		if (rc)
3503*4882a593Smuzhiyun 			break;
3504*4882a593Smuzhiyun 		if (nbytes > len)
3505*4882a593Smuzhiyun 			return -EINVAL;
3506*4882a593Smuzhiyun 		buf += nbytes;
3507*4882a593Smuzhiyun 		off += nbytes;
3508*4882a593Smuzhiyun 		len -= nbytes;
3509*4882a593Smuzhiyun 	}
3510*4882a593Smuzhiyun 	return rc;
3511*4882a593Smuzhiyun }
3512*4882a593Smuzhiyun 
smb3_simple_fallocate_range(unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,loff_t off,loff_t len)3513*4882a593Smuzhiyun static int smb3_simple_fallocate_range(unsigned int xid,
3514*4882a593Smuzhiyun 				       struct cifs_tcon *tcon,
3515*4882a593Smuzhiyun 				       struct cifsFileInfo *cfile,
3516*4882a593Smuzhiyun 				       loff_t off, loff_t len)
3517*4882a593Smuzhiyun {
3518*4882a593Smuzhiyun 	struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3519*4882a593Smuzhiyun 	u32 out_data_len;
3520*4882a593Smuzhiyun 	char *buf = NULL;
3521*4882a593Smuzhiyun 	loff_t l;
3522*4882a593Smuzhiyun 	int rc;
3523*4882a593Smuzhiyun 
3524*4882a593Smuzhiyun 	in_data.file_offset = cpu_to_le64(off);
3525*4882a593Smuzhiyun 	in_data.length = cpu_to_le64(len);
3526*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3527*4882a593Smuzhiyun 			cfile->fid.volatile_fid,
3528*4882a593Smuzhiyun 			FSCTL_QUERY_ALLOCATED_RANGES,
3529*4882a593Smuzhiyun 			(char *)&in_data, sizeof(in_data),
3530*4882a593Smuzhiyun 			1024 * sizeof(struct file_allocated_range_buffer),
3531*4882a593Smuzhiyun 			(char **)&out_data, &out_data_len);
3532*4882a593Smuzhiyun 	if (rc)
3533*4882a593Smuzhiyun 		goto out;
3534*4882a593Smuzhiyun 
3535*4882a593Smuzhiyun 	buf = kzalloc(1024 * 1024, GFP_KERNEL);
3536*4882a593Smuzhiyun 	if (buf == NULL) {
3537*4882a593Smuzhiyun 		rc = -ENOMEM;
3538*4882a593Smuzhiyun 		goto out;
3539*4882a593Smuzhiyun 	}
3540*4882a593Smuzhiyun 
3541*4882a593Smuzhiyun 	tmp_data = out_data;
3542*4882a593Smuzhiyun 	while (len) {
3543*4882a593Smuzhiyun 		/*
3544*4882a593Smuzhiyun 		 * The rest of the region is unmapped so write it all.
3545*4882a593Smuzhiyun 		 */
3546*4882a593Smuzhiyun 		if (out_data_len == 0) {
3547*4882a593Smuzhiyun 			rc = smb3_simple_fallocate_write_range(xid, tcon,
3548*4882a593Smuzhiyun 					       cfile, off, len, buf);
3549*4882a593Smuzhiyun 			goto out;
3550*4882a593Smuzhiyun 		}
3551*4882a593Smuzhiyun 
3552*4882a593Smuzhiyun 		if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3553*4882a593Smuzhiyun 			rc = -EINVAL;
3554*4882a593Smuzhiyun 			goto out;
3555*4882a593Smuzhiyun 		}
3556*4882a593Smuzhiyun 
3557*4882a593Smuzhiyun 		if (off < le64_to_cpu(tmp_data->file_offset)) {
3558*4882a593Smuzhiyun 			/*
3559*4882a593Smuzhiyun 			 * We are at a hole. Write until the end of the region
3560*4882a593Smuzhiyun 			 * or until the next allocated data,
3561*4882a593Smuzhiyun 			 * whichever comes next.
3562*4882a593Smuzhiyun 			 */
3563*4882a593Smuzhiyun 			l = le64_to_cpu(tmp_data->file_offset) - off;
3564*4882a593Smuzhiyun 			if (len < l)
3565*4882a593Smuzhiyun 				l = len;
3566*4882a593Smuzhiyun 			rc = smb3_simple_fallocate_write_range(xid, tcon,
3567*4882a593Smuzhiyun 					       cfile, off, l, buf);
3568*4882a593Smuzhiyun 			if (rc)
3569*4882a593Smuzhiyun 				goto out;
3570*4882a593Smuzhiyun 			off = off + l;
3571*4882a593Smuzhiyun 			len = len - l;
3572*4882a593Smuzhiyun 			if (len == 0)
3573*4882a593Smuzhiyun 				goto out;
3574*4882a593Smuzhiyun 		}
3575*4882a593Smuzhiyun 		/*
3576*4882a593Smuzhiyun 		 * We are at a section of allocated data, just skip forward
3577*4882a593Smuzhiyun 		 * until the end of the data or the end of the region
3578*4882a593Smuzhiyun 		 * we are supposed to fallocate, whichever comes first.
3579*4882a593Smuzhiyun 		 */
3580*4882a593Smuzhiyun 		l = le64_to_cpu(tmp_data->length);
3581*4882a593Smuzhiyun 		if (len < l)
3582*4882a593Smuzhiyun 			l = len;
3583*4882a593Smuzhiyun 		off += l;
3584*4882a593Smuzhiyun 		len -= l;
3585*4882a593Smuzhiyun 
3586*4882a593Smuzhiyun 		tmp_data = &tmp_data[1];
3587*4882a593Smuzhiyun 		out_data_len -= sizeof(struct file_allocated_range_buffer);
3588*4882a593Smuzhiyun 	}
3589*4882a593Smuzhiyun 
3590*4882a593Smuzhiyun  out:
3591*4882a593Smuzhiyun 	kfree(out_data);
3592*4882a593Smuzhiyun 	kfree(buf);
3593*4882a593Smuzhiyun 	return rc;
3594*4882a593Smuzhiyun }
3595*4882a593Smuzhiyun 
3596*4882a593Smuzhiyun 
smb3_simple_falloc(struct file * file,struct cifs_tcon * tcon,loff_t off,loff_t len,bool keep_size)3597*4882a593Smuzhiyun static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3598*4882a593Smuzhiyun 			    loff_t off, loff_t len, bool keep_size)
3599*4882a593Smuzhiyun {
3600*4882a593Smuzhiyun 	struct inode *inode;
3601*4882a593Smuzhiyun 	struct cifsInodeInfo *cifsi;
3602*4882a593Smuzhiyun 	struct cifsFileInfo *cfile = file->private_data;
3603*4882a593Smuzhiyun 	long rc = -EOPNOTSUPP;
3604*4882a593Smuzhiyun 	unsigned int xid;
3605*4882a593Smuzhiyun 	__le64 eof;
3606*4882a593Smuzhiyun 
3607*4882a593Smuzhiyun 	xid = get_xid();
3608*4882a593Smuzhiyun 
3609*4882a593Smuzhiyun 	inode = d_inode(cfile->dentry);
3610*4882a593Smuzhiyun 	cifsi = CIFS_I(inode);
3611*4882a593Smuzhiyun 
3612*4882a593Smuzhiyun 	trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3613*4882a593Smuzhiyun 				tcon->ses->Suid, off, len);
3614*4882a593Smuzhiyun 	/* if file not oplocked can't be sure whether asking to extend size */
3615*4882a593Smuzhiyun 	if (!CIFS_CACHE_READ(cifsi))
3616*4882a593Smuzhiyun 		if (keep_size == false) {
3617*4882a593Smuzhiyun 			trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3618*4882a593Smuzhiyun 				tcon->tid, tcon->ses->Suid, off, len, rc);
3619*4882a593Smuzhiyun 			free_xid(xid);
3620*4882a593Smuzhiyun 			return rc;
3621*4882a593Smuzhiyun 		}
3622*4882a593Smuzhiyun 
3623*4882a593Smuzhiyun 	/*
3624*4882a593Smuzhiyun 	 * Extending the file
3625*4882a593Smuzhiyun 	 */
3626*4882a593Smuzhiyun 	if ((keep_size == false) && i_size_read(inode) < off + len) {
3627*4882a593Smuzhiyun 		rc = inode_newsize_ok(inode, off + len);
3628*4882a593Smuzhiyun 		if (rc)
3629*4882a593Smuzhiyun 			goto out;
3630*4882a593Smuzhiyun 
3631*4882a593Smuzhiyun 		if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
3632*4882a593Smuzhiyun 			smb2_set_sparse(xid, tcon, cfile, inode, false);
3633*4882a593Smuzhiyun 
3634*4882a593Smuzhiyun 		eof = cpu_to_le64(off + len);
3635*4882a593Smuzhiyun 		rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3636*4882a593Smuzhiyun 				  cfile->fid.volatile_fid, cfile->pid, &eof);
3637*4882a593Smuzhiyun 		if (rc == 0) {
3638*4882a593Smuzhiyun 			cifsi->server_eof = off + len;
3639*4882a593Smuzhiyun 			cifs_setsize(inode, off + len);
3640*4882a593Smuzhiyun 			cifs_truncate_page(inode->i_mapping, inode->i_size);
3641*4882a593Smuzhiyun 			truncate_setsize(inode, off + len);
3642*4882a593Smuzhiyun 		}
3643*4882a593Smuzhiyun 		goto out;
3644*4882a593Smuzhiyun 	}
3645*4882a593Smuzhiyun 
3646*4882a593Smuzhiyun 	/*
3647*4882a593Smuzhiyun 	 * Files are non-sparse by default so falloc may be a no-op
3648*4882a593Smuzhiyun 	 * Must check if file sparse. If not sparse, and since we are not
3649*4882a593Smuzhiyun 	 * extending then no need to do anything since file already allocated
3650*4882a593Smuzhiyun 	 */
3651*4882a593Smuzhiyun 	if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3652*4882a593Smuzhiyun 		rc = 0;
3653*4882a593Smuzhiyun 		goto out;
3654*4882a593Smuzhiyun 	}
3655*4882a593Smuzhiyun 
3656*4882a593Smuzhiyun 	if (keep_size == true) {
3657*4882a593Smuzhiyun 		/*
3658*4882a593Smuzhiyun 		 * We can not preallocate pages beyond the end of the file
3659*4882a593Smuzhiyun 		 * in SMB2
3660*4882a593Smuzhiyun 		 */
3661*4882a593Smuzhiyun 		if (off >= i_size_read(inode)) {
3662*4882a593Smuzhiyun 			rc = 0;
3663*4882a593Smuzhiyun 			goto out;
3664*4882a593Smuzhiyun 		}
3665*4882a593Smuzhiyun 		/*
3666*4882a593Smuzhiyun 		 * For fallocates that are partially beyond the end of file,
3667*4882a593Smuzhiyun 		 * clamp len so we only fallocate up to the end of file.
3668*4882a593Smuzhiyun 		 */
3669*4882a593Smuzhiyun 		if (off + len > i_size_read(inode)) {
3670*4882a593Smuzhiyun 			len = i_size_read(inode) - off;
3671*4882a593Smuzhiyun 		}
3672*4882a593Smuzhiyun 	}
3673*4882a593Smuzhiyun 
3674*4882a593Smuzhiyun 	if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3675*4882a593Smuzhiyun 		/*
3676*4882a593Smuzhiyun 		 * At this point, we are trying to fallocate an internal
3677*4882a593Smuzhiyun 		 * regions of a sparse file. Since smb2 does not have a
3678*4882a593Smuzhiyun 		 * fallocate command we have two otions on how to emulate this.
3679*4882a593Smuzhiyun 		 * We can either turn the entire file to become non-sparse
3680*4882a593Smuzhiyun 		 * which we only do if the fallocate is for virtually
3681*4882a593Smuzhiyun 		 * the whole file,  or we can overwrite the region with zeroes
3682*4882a593Smuzhiyun 		 * using SMB2_write, which could be prohibitevly expensive
3683*4882a593Smuzhiyun 		 * if len is large.
3684*4882a593Smuzhiyun 		 */
3685*4882a593Smuzhiyun 		/*
3686*4882a593Smuzhiyun 		 * We are only trying to fallocate a small region so
3687*4882a593Smuzhiyun 		 * just write it with zero.
3688*4882a593Smuzhiyun 		 */
3689*4882a593Smuzhiyun 		if (len <= 1024 * 1024) {
3690*4882a593Smuzhiyun 			rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3691*4882a593Smuzhiyun 							 off, len);
3692*4882a593Smuzhiyun 			goto out;
3693*4882a593Smuzhiyun 		}
3694*4882a593Smuzhiyun 
3695*4882a593Smuzhiyun 		/*
3696*4882a593Smuzhiyun 		 * Check if falloc starts within first few pages of file
3697*4882a593Smuzhiyun 		 * and ends within a few pages of the end of file to
3698*4882a593Smuzhiyun 		 * ensure that most of file is being forced to be
3699*4882a593Smuzhiyun 		 * fallocated now. If so then setting whole file sparse
3700*4882a593Smuzhiyun 		 * ie potentially making a few extra pages at the beginning
3701*4882a593Smuzhiyun 		 * or end of the file non-sparse via set_sparse is harmless.
3702*4882a593Smuzhiyun 		 */
3703*4882a593Smuzhiyun 		if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3704*4882a593Smuzhiyun 			rc = -EOPNOTSUPP;
3705*4882a593Smuzhiyun 			goto out;
3706*4882a593Smuzhiyun 		}
3707*4882a593Smuzhiyun 	}
3708*4882a593Smuzhiyun 
3709*4882a593Smuzhiyun 	smb2_set_sparse(xid, tcon, cfile, inode, false);
3710*4882a593Smuzhiyun 	rc = 0;
3711*4882a593Smuzhiyun 
3712*4882a593Smuzhiyun out:
3713*4882a593Smuzhiyun 	if (rc)
3714*4882a593Smuzhiyun 		trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3715*4882a593Smuzhiyun 				tcon->ses->Suid, off, len, rc);
3716*4882a593Smuzhiyun 	else
3717*4882a593Smuzhiyun 		trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3718*4882a593Smuzhiyun 				tcon->ses->Suid, off, len);
3719*4882a593Smuzhiyun 
3720*4882a593Smuzhiyun 	free_xid(xid);
3721*4882a593Smuzhiyun 	return rc;
3722*4882a593Smuzhiyun }
3723*4882a593Smuzhiyun 
smb3_llseek(struct file * file,struct cifs_tcon * tcon,loff_t offset,int whence)3724*4882a593Smuzhiyun static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3725*4882a593Smuzhiyun {
3726*4882a593Smuzhiyun 	struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3727*4882a593Smuzhiyun 	struct cifsInodeInfo *cifsi;
3728*4882a593Smuzhiyun 	struct inode *inode;
3729*4882a593Smuzhiyun 	int rc = 0;
3730*4882a593Smuzhiyun 	struct file_allocated_range_buffer in_data, *out_data = NULL;
3731*4882a593Smuzhiyun 	u32 out_data_len;
3732*4882a593Smuzhiyun 	unsigned int xid;
3733*4882a593Smuzhiyun 
3734*4882a593Smuzhiyun 	if (whence != SEEK_HOLE && whence != SEEK_DATA)
3735*4882a593Smuzhiyun 		return generic_file_llseek(file, offset, whence);
3736*4882a593Smuzhiyun 
3737*4882a593Smuzhiyun 	inode = d_inode(cfile->dentry);
3738*4882a593Smuzhiyun 	cifsi = CIFS_I(inode);
3739*4882a593Smuzhiyun 
3740*4882a593Smuzhiyun 	if (offset < 0 || offset >= i_size_read(inode))
3741*4882a593Smuzhiyun 		return -ENXIO;
3742*4882a593Smuzhiyun 
3743*4882a593Smuzhiyun 	xid = get_xid();
3744*4882a593Smuzhiyun 	/*
3745*4882a593Smuzhiyun 	 * We need to be sure that all dirty pages are written as they
3746*4882a593Smuzhiyun 	 * might fill holes on the server.
3747*4882a593Smuzhiyun 	 * Note that we also MUST flush any written pages since at least
3748*4882a593Smuzhiyun 	 * some servers (Windows2016) will not reflect recent writes in
3749*4882a593Smuzhiyun 	 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3750*4882a593Smuzhiyun 	 */
3751*4882a593Smuzhiyun 	wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3752*4882a593Smuzhiyun 	if (wrcfile) {
3753*4882a593Smuzhiyun 		filemap_write_and_wait(inode->i_mapping);
3754*4882a593Smuzhiyun 		smb2_flush_file(xid, tcon, &wrcfile->fid);
3755*4882a593Smuzhiyun 		cifsFileInfo_put(wrcfile);
3756*4882a593Smuzhiyun 	}
3757*4882a593Smuzhiyun 
3758*4882a593Smuzhiyun 	if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3759*4882a593Smuzhiyun 		if (whence == SEEK_HOLE)
3760*4882a593Smuzhiyun 			offset = i_size_read(inode);
3761*4882a593Smuzhiyun 		goto lseek_exit;
3762*4882a593Smuzhiyun 	}
3763*4882a593Smuzhiyun 
3764*4882a593Smuzhiyun 	in_data.file_offset = cpu_to_le64(offset);
3765*4882a593Smuzhiyun 	in_data.length = cpu_to_le64(i_size_read(inode));
3766*4882a593Smuzhiyun 
3767*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3768*4882a593Smuzhiyun 			cfile->fid.volatile_fid,
3769*4882a593Smuzhiyun 			FSCTL_QUERY_ALLOCATED_RANGES,
3770*4882a593Smuzhiyun 			(char *)&in_data, sizeof(in_data),
3771*4882a593Smuzhiyun 			sizeof(struct file_allocated_range_buffer),
3772*4882a593Smuzhiyun 			(char **)&out_data, &out_data_len);
3773*4882a593Smuzhiyun 	if (rc == -E2BIG)
3774*4882a593Smuzhiyun 		rc = 0;
3775*4882a593Smuzhiyun 	if (rc)
3776*4882a593Smuzhiyun 		goto lseek_exit;
3777*4882a593Smuzhiyun 
3778*4882a593Smuzhiyun 	if (whence == SEEK_HOLE && out_data_len == 0)
3779*4882a593Smuzhiyun 		goto lseek_exit;
3780*4882a593Smuzhiyun 
3781*4882a593Smuzhiyun 	if (whence == SEEK_DATA && out_data_len == 0) {
3782*4882a593Smuzhiyun 		rc = -ENXIO;
3783*4882a593Smuzhiyun 		goto lseek_exit;
3784*4882a593Smuzhiyun 	}
3785*4882a593Smuzhiyun 
3786*4882a593Smuzhiyun 	if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3787*4882a593Smuzhiyun 		rc = -EINVAL;
3788*4882a593Smuzhiyun 		goto lseek_exit;
3789*4882a593Smuzhiyun 	}
3790*4882a593Smuzhiyun 	if (whence == SEEK_DATA) {
3791*4882a593Smuzhiyun 		offset = le64_to_cpu(out_data->file_offset);
3792*4882a593Smuzhiyun 		goto lseek_exit;
3793*4882a593Smuzhiyun 	}
3794*4882a593Smuzhiyun 	if (offset < le64_to_cpu(out_data->file_offset))
3795*4882a593Smuzhiyun 		goto lseek_exit;
3796*4882a593Smuzhiyun 
3797*4882a593Smuzhiyun 	offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3798*4882a593Smuzhiyun 
3799*4882a593Smuzhiyun  lseek_exit:
3800*4882a593Smuzhiyun 	free_xid(xid);
3801*4882a593Smuzhiyun 	kfree(out_data);
3802*4882a593Smuzhiyun 	if (!rc)
3803*4882a593Smuzhiyun 		return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3804*4882a593Smuzhiyun 	else
3805*4882a593Smuzhiyun 		return rc;
3806*4882a593Smuzhiyun }
3807*4882a593Smuzhiyun 
smb3_fiemap(struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct fiemap_extent_info * fei,u64 start,u64 len)3808*4882a593Smuzhiyun static int smb3_fiemap(struct cifs_tcon *tcon,
3809*4882a593Smuzhiyun 		       struct cifsFileInfo *cfile,
3810*4882a593Smuzhiyun 		       struct fiemap_extent_info *fei, u64 start, u64 len)
3811*4882a593Smuzhiyun {
3812*4882a593Smuzhiyun 	unsigned int xid;
3813*4882a593Smuzhiyun 	struct file_allocated_range_buffer in_data, *out_data;
3814*4882a593Smuzhiyun 	u32 out_data_len;
3815*4882a593Smuzhiyun 	int i, num, rc, flags, last_blob;
3816*4882a593Smuzhiyun 	u64 next;
3817*4882a593Smuzhiyun 
3818*4882a593Smuzhiyun 	rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3819*4882a593Smuzhiyun 	if (rc)
3820*4882a593Smuzhiyun 		return rc;
3821*4882a593Smuzhiyun 
3822*4882a593Smuzhiyun 	xid = get_xid();
3823*4882a593Smuzhiyun  again:
3824*4882a593Smuzhiyun 	in_data.file_offset = cpu_to_le64(start);
3825*4882a593Smuzhiyun 	in_data.length = cpu_to_le64(len);
3826*4882a593Smuzhiyun 
3827*4882a593Smuzhiyun 	rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3828*4882a593Smuzhiyun 			cfile->fid.volatile_fid,
3829*4882a593Smuzhiyun 			FSCTL_QUERY_ALLOCATED_RANGES,
3830*4882a593Smuzhiyun 			(char *)&in_data, sizeof(in_data),
3831*4882a593Smuzhiyun 			1024 * sizeof(struct file_allocated_range_buffer),
3832*4882a593Smuzhiyun 			(char **)&out_data, &out_data_len);
3833*4882a593Smuzhiyun 	if (rc == -E2BIG) {
3834*4882a593Smuzhiyun 		last_blob = 0;
3835*4882a593Smuzhiyun 		rc = 0;
3836*4882a593Smuzhiyun 	} else
3837*4882a593Smuzhiyun 		last_blob = 1;
3838*4882a593Smuzhiyun 	if (rc)
3839*4882a593Smuzhiyun 		goto out;
3840*4882a593Smuzhiyun 
3841*4882a593Smuzhiyun 	if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3842*4882a593Smuzhiyun 		rc = -EINVAL;
3843*4882a593Smuzhiyun 		goto out;
3844*4882a593Smuzhiyun 	}
3845*4882a593Smuzhiyun 	if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3846*4882a593Smuzhiyun 		rc = -EINVAL;
3847*4882a593Smuzhiyun 		goto out;
3848*4882a593Smuzhiyun 	}
3849*4882a593Smuzhiyun 
3850*4882a593Smuzhiyun 	num = out_data_len / sizeof(struct file_allocated_range_buffer);
3851*4882a593Smuzhiyun 	for (i = 0; i < num; i++) {
3852*4882a593Smuzhiyun 		flags = 0;
3853*4882a593Smuzhiyun 		if (i == num - 1 && last_blob)
3854*4882a593Smuzhiyun 			flags |= FIEMAP_EXTENT_LAST;
3855*4882a593Smuzhiyun 
3856*4882a593Smuzhiyun 		rc = fiemap_fill_next_extent(fei,
3857*4882a593Smuzhiyun 				le64_to_cpu(out_data[i].file_offset),
3858*4882a593Smuzhiyun 				le64_to_cpu(out_data[i].file_offset),
3859*4882a593Smuzhiyun 				le64_to_cpu(out_data[i].length),
3860*4882a593Smuzhiyun 				flags);
3861*4882a593Smuzhiyun 		if (rc < 0)
3862*4882a593Smuzhiyun 			goto out;
3863*4882a593Smuzhiyun 		if (rc == 1) {
3864*4882a593Smuzhiyun 			rc = 0;
3865*4882a593Smuzhiyun 			goto out;
3866*4882a593Smuzhiyun 		}
3867*4882a593Smuzhiyun 	}
3868*4882a593Smuzhiyun 
3869*4882a593Smuzhiyun 	if (!last_blob) {
3870*4882a593Smuzhiyun 		next = le64_to_cpu(out_data[num - 1].file_offset) +
3871*4882a593Smuzhiyun 		  le64_to_cpu(out_data[num - 1].length);
3872*4882a593Smuzhiyun 		len = len - (next - start);
3873*4882a593Smuzhiyun 		start = next;
3874*4882a593Smuzhiyun 		goto again;
3875*4882a593Smuzhiyun 	}
3876*4882a593Smuzhiyun 
3877*4882a593Smuzhiyun  out:
3878*4882a593Smuzhiyun 	free_xid(xid);
3879*4882a593Smuzhiyun 	kfree(out_data);
3880*4882a593Smuzhiyun 	return rc;
3881*4882a593Smuzhiyun }
3882*4882a593Smuzhiyun 
smb3_fallocate(struct file * file,struct cifs_tcon * tcon,int mode,loff_t off,loff_t len)3883*4882a593Smuzhiyun static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3884*4882a593Smuzhiyun 			   loff_t off, loff_t len)
3885*4882a593Smuzhiyun {
3886*4882a593Smuzhiyun 	/* KEEP_SIZE already checked for by do_fallocate */
3887*4882a593Smuzhiyun 	if (mode & FALLOC_FL_PUNCH_HOLE)
3888*4882a593Smuzhiyun 		return smb3_punch_hole(file, tcon, off, len);
3889*4882a593Smuzhiyun 	else if (mode & FALLOC_FL_ZERO_RANGE) {
3890*4882a593Smuzhiyun 		if (mode & FALLOC_FL_KEEP_SIZE)
3891*4882a593Smuzhiyun 			return smb3_zero_range(file, tcon, off, len, true);
3892*4882a593Smuzhiyun 		return smb3_zero_range(file, tcon, off, len, false);
3893*4882a593Smuzhiyun 	} else if (mode == FALLOC_FL_KEEP_SIZE)
3894*4882a593Smuzhiyun 		return smb3_simple_falloc(file, tcon, off, len, true);
3895*4882a593Smuzhiyun 	else if (mode == 0)
3896*4882a593Smuzhiyun 		return smb3_simple_falloc(file, tcon, off, len, false);
3897*4882a593Smuzhiyun 
3898*4882a593Smuzhiyun 	return -EOPNOTSUPP;
3899*4882a593Smuzhiyun }
3900*4882a593Smuzhiyun 
3901*4882a593Smuzhiyun static void
smb2_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)3902*4882a593Smuzhiyun smb2_downgrade_oplock(struct TCP_Server_Info *server,
3903*4882a593Smuzhiyun 		      struct cifsInodeInfo *cinode, __u32 oplock,
3904*4882a593Smuzhiyun 		      unsigned int epoch, bool *purge_cache)
3905*4882a593Smuzhiyun {
3906*4882a593Smuzhiyun 	server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3907*4882a593Smuzhiyun }
3908*4882a593Smuzhiyun 
3909*4882a593Smuzhiyun static void
3910*4882a593Smuzhiyun smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3911*4882a593Smuzhiyun 		       unsigned int epoch, bool *purge_cache);
3912*4882a593Smuzhiyun 
3913*4882a593Smuzhiyun static void
smb3_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)3914*4882a593Smuzhiyun smb3_downgrade_oplock(struct TCP_Server_Info *server,
3915*4882a593Smuzhiyun 		       struct cifsInodeInfo *cinode, __u32 oplock,
3916*4882a593Smuzhiyun 		       unsigned int epoch, bool *purge_cache)
3917*4882a593Smuzhiyun {
3918*4882a593Smuzhiyun 	unsigned int old_state = cinode->oplock;
3919*4882a593Smuzhiyun 	unsigned int old_epoch = cinode->epoch;
3920*4882a593Smuzhiyun 	unsigned int new_state;
3921*4882a593Smuzhiyun 
3922*4882a593Smuzhiyun 	if (epoch > old_epoch) {
3923*4882a593Smuzhiyun 		smb21_set_oplock_level(cinode, oplock, 0, NULL);
3924*4882a593Smuzhiyun 		cinode->epoch = epoch;
3925*4882a593Smuzhiyun 	}
3926*4882a593Smuzhiyun 
3927*4882a593Smuzhiyun 	new_state = cinode->oplock;
3928*4882a593Smuzhiyun 	*purge_cache = false;
3929*4882a593Smuzhiyun 
3930*4882a593Smuzhiyun 	if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3931*4882a593Smuzhiyun 	    (new_state & CIFS_CACHE_READ_FLG) == 0)
3932*4882a593Smuzhiyun 		*purge_cache = true;
3933*4882a593Smuzhiyun 	else if (old_state == new_state && (epoch - old_epoch > 1))
3934*4882a593Smuzhiyun 		*purge_cache = true;
3935*4882a593Smuzhiyun }
3936*4882a593Smuzhiyun 
3937*4882a593Smuzhiyun static void
smb2_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)3938*4882a593Smuzhiyun smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3939*4882a593Smuzhiyun 		      unsigned int epoch, bool *purge_cache)
3940*4882a593Smuzhiyun {
3941*4882a593Smuzhiyun 	oplock &= 0xFF;
3942*4882a593Smuzhiyun 	if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3943*4882a593Smuzhiyun 		return;
3944*4882a593Smuzhiyun 	if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3945*4882a593Smuzhiyun 		cinode->oplock = CIFS_CACHE_RHW_FLG;
3946*4882a593Smuzhiyun 		cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3947*4882a593Smuzhiyun 			 &cinode->vfs_inode);
3948*4882a593Smuzhiyun 	} else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3949*4882a593Smuzhiyun 		cinode->oplock = CIFS_CACHE_RW_FLG;
3950*4882a593Smuzhiyun 		cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3951*4882a593Smuzhiyun 			 &cinode->vfs_inode);
3952*4882a593Smuzhiyun 	} else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3953*4882a593Smuzhiyun 		cinode->oplock = CIFS_CACHE_READ_FLG;
3954*4882a593Smuzhiyun 		cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3955*4882a593Smuzhiyun 			 &cinode->vfs_inode);
3956*4882a593Smuzhiyun 	} else
3957*4882a593Smuzhiyun 		cinode->oplock = 0;
3958*4882a593Smuzhiyun }
3959*4882a593Smuzhiyun 
3960*4882a593Smuzhiyun static void
smb21_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)3961*4882a593Smuzhiyun smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3962*4882a593Smuzhiyun 		       unsigned int epoch, bool *purge_cache)
3963*4882a593Smuzhiyun {
3964*4882a593Smuzhiyun 	char message[5] = {0};
3965*4882a593Smuzhiyun 	unsigned int new_oplock = 0;
3966*4882a593Smuzhiyun 
3967*4882a593Smuzhiyun 	oplock &= 0xFF;
3968*4882a593Smuzhiyun 	if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3969*4882a593Smuzhiyun 		return;
3970*4882a593Smuzhiyun 
3971*4882a593Smuzhiyun 	/* Check if the server granted an oplock rather than a lease */
3972*4882a593Smuzhiyun 	if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3973*4882a593Smuzhiyun 		return smb2_set_oplock_level(cinode, oplock, epoch,
3974*4882a593Smuzhiyun 					     purge_cache);
3975*4882a593Smuzhiyun 
3976*4882a593Smuzhiyun 	if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3977*4882a593Smuzhiyun 		new_oplock |= CIFS_CACHE_READ_FLG;
3978*4882a593Smuzhiyun 		strcat(message, "R");
3979*4882a593Smuzhiyun 	}
3980*4882a593Smuzhiyun 	if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3981*4882a593Smuzhiyun 		new_oplock |= CIFS_CACHE_HANDLE_FLG;
3982*4882a593Smuzhiyun 		strcat(message, "H");
3983*4882a593Smuzhiyun 	}
3984*4882a593Smuzhiyun 	if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3985*4882a593Smuzhiyun 		new_oplock |= CIFS_CACHE_WRITE_FLG;
3986*4882a593Smuzhiyun 		strcat(message, "W");
3987*4882a593Smuzhiyun 	}
3988*4882a593Smuzhiyun 	if (!new_oplock)
3989*4882a593Smuzhiyun 		strncpy(message, "None", sizeof(message));
3990*4882a593Smuzhiyun 
3991*4882a593Smuzhiyun 	cinode->oplock = new_oplock;
3992*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3993*4882a593Smuzhiyun 		 &cinode->vfs_inode);
3994*4882a593Smuzhiyun }
3995*4882a593Smuzhiyun 
3996*4882a593Smuzhiyun static void
smb3_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,unsigned int epoch,bool * purge_cache)3997*4882a593Smuzhiyun smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3998*4882a593Smuzhiyun 		      unsigned int epoch, bool *purge_cache)
3999*4882a593Smuzhiyun {
4000*4882a593Smuzhiyun 	unsigned int old_oplock = cinode->oplock;
4001*4882a593Smuzhiyun 
4002*4882a593Smuzhiyun 	smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
4003*4882a593Smuzhiyun 
4004*4882a593Smuzhiyun 	if (purge_cache) {
4005*4882a593Smuzhiyun 		*purge_cache = false;
4006*4882a593Smuzhiyun 		if (old_oplock == CIFS_CACHE_READ_FLG) {
4007*4882a593Smuzhiyun 			if (cinode->oplock == CIFS_CACHE_READ_FLG &&
4008*4882a593Smuzhiyun 			    (epoch - cinode->epoch > 0))
4009*4882a593Smuzhiyun 				*purge_cache = true;
4010*4882a593Smuzhiyun 			else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4011*4882a593Smuzhiyun 				 (epoch - cinode->epoch > 1))
4012*4882a593Smuzhiyun 				*purge_cache = true;
4013*4882a593Smuzhiyun 			else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4014*4882a593Smuzhiyun 				 (epoch - cinode->epoch > 1))
4015*4882a593Smuzhiyun 				*purge_cache = true;
4016*4882a593Smuzhiyun 			else if (cinode->oplock == 0 &&
4017*4882a593Smuzhiyun 				 (epoch - cinode->epoch > 0))
4018*4882a593Smuzhiyun 				*purge_cache = true;
4019*4882a593Smuzhiyun 		} else if (old_oplock == CIFS_CACHE_RH_FLG) {
4020*4882a593Smuzhiyun 			if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4021*4882a593Smuzhiyun 			    (epoch - cinode->epoch > 0))
4022*4882a593Smuzhiyun 				*purge_cache = true;
4023*4882a593Smuzhiyun 			else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4024*4882a593Smuzhiyun 				 (epoch - cinode->epoch > 1))
4025*4882a593Smuzhiyun 				*purge_cache = true;
4026*4882a593Smuzhiyun 		}
4027*4882a593Smuzhiyun 		cinode->epoch = epoch;
4028*4882a593Smuzhiyun 	}
4029*4882a593Smuzhiyun }
4030*4882a593Smuzhiyun 
4031*4882a593Smuzhiyun #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4032*4882a593Smuzhiyun static bool
smb2_is_read_op(__u32 oplock)4033*4882a593Smuzhiyun smb2_is_read_op(__u32 oplock)
4034*4882a593Smuzhiyun {
4035*4882a593Smuzhiyun 	return oplock == SMB2_OPLOCK_LEVEL_II;
4036*4882a593Smuzhiyun }
4037*4882a593Smuzhiyun #endif /* CIFS_ALLOW_INSECURE_LEGACY */
4038*4882a593Smuzhiyun 
4039*4882a593Smuzhiyun static bool
smb21_is_read_op(__u32 oplock)4040*4882a593Smuzhiyun smb21_is_read_op(__u32 oplock)
4041*4882a593Smuzhiyun {
4042*4882a593Smuzhiyun 	return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4043*4882a593Smuzhiyun 	       !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4044*4882a593Smuzhiyun }
4045*4882a593Smuzhiyun 
4046*4882a593Smuzhiyun static __le32
map_oplock_to_lease(u8 oplock)4047*4882a593Smuzhiyun map_oplock_to_lease(u8 oplock)
4048*4882a593Smuzhiyun {
4049*4882a593Smuzhiyun 	if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4050*4882a593Smuzhiyun 		return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
4051*4882a593Smuzhiyun 	else if (oplock == SMB2_OPLOCK_LEVEL_II)
4052*4882a593Smuzhiyun 		return SMB2_LEASE_READ_CACHING;
4053*4882a593Smuzhiyun 	else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4054*4882a593Smuzhiyun 		return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
4055*4882a593Smuzhiyun 		       SMB2_LEASE_WRITE_CACHING;
4056*4882a593Smuzhiyun 	return 0;
4057*4882a593Smuzhiyun }
4058*4882a593Smuzhiyun 
4059*4882a593Smuzhiyun static char *
smb2_create_lease_buf(u8 * lease_key,u8 oplock)4060*4882a593Smuzhiyun smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4061*4882a593Smuzhiyun {
4062*4882a593Smuzhiyun 	struct create_lease *buf;
4063*4882a593Smuzhiyun 
4064*4882a593Smuzhiyun 	buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4065*4882a593Smuzhiyun 	if (!buf)
4066*4882a593Smuzhiyun 		return NULL;
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4069*4882a593Smuzhiyun 	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4070*4882a593Smuzhiyun 
4071*4882a593Smuzhiyun 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
4072*4882a593Smuzhiyun 					(struct create_lease, lcontext));
4073*4882a593Smuzhiyun 	buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4074*4882a593Smuzhiyun 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
4075*4882a593Smuzhiyun 				(struct create_lease, Name));
4076*4882a593Smuzhiyun 	buf->ccontext.NameLength = cpu_to_le16(4);
4077*4882a593Smuzhiyun 	/* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4078*4882a593Smuzhiyun 	buf->Name[0] = 'R';
4079*4882a593Smuzhiyun 	buf->Name[1] = 'q';
4080*4882a593Smuzhiyun 	buf->Name[2] = 'L';
4081*4882a593Smuzhiyun 	buf->Name[3] = 's';
4082*4882a593Smuzhiyun 	return (char *)buf;
4083*4882a593Smuzhiyun }
4084*4882a593Smuzhiyun 
4085*4882a593Smuzhiyun static char *
smb3_create_lease_buf(u8 * lease_key,u8 oplock)4086*4882a593Smuzhiyun smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4087*4882a593Smuzhiyun {
4088*4882a593Smuzhiyun 	struct create_lease_v2 *buf;
4089*4882a593Smuzhiyun 
4090*4882a593Smuzhiyun 	buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4091*4882a593Smuzhiyun 	if (!buf)
4092*4882a593Smuzhiyun 		return NULL;
4093*4882a593Smuzhiyun 
4094*4882a593Smuzhiyun 	memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4095*4882a593Smuzhiyun 	buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4096*4882a593Smuzhiyun 
4097*4882a593Smuzhiyun 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
4098*4882a593Smuzhiyun 					(struct create_lease_v2, lcontext));
4099*4882a593Smuzhiyun 	buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4100*4882a593Smuzhiyun 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
4101*4882a593Smuzhiyun 				(struct create_lease_v2, Name));
4102*4882a593Smuzhiyun 	buf->ccontext.NameLength = cpu_to_le16(4);
4103*4882a593Smuzhiyun 	/* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4104*4882a593Smuzhiyun 	buf->Name[0] = 'R';
4105*4882a593Smuzhiyun 	buf->Name[1] = 'q';
4106*4882a593Smuzhiyun 	buf->Name[2] = 'L';
4107*4882a593Smuzhiyun 	buf->Name[3] = 's';
4108*4882a593Smuzhiyun 	return (char *)buf;
4109*4882a593Smuzhiyun }
4110*4882a593Smuzhiyun 
4111*4882a593Smuzhiyun static __u8
smb2_parse_lease_buf(void * buf,unsigned int * epoch,char * lease_key)4112*4882a593Smuzhiyun smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4113*4882a593Smuzhiyun {
4114*4882a593Smuzhiyun 	struct create_lease *lc = (struct create_lease *)buf;
4115*4882a593Smuzhiyun 
4116*4882a593Smuzhiyun 	*epoch = 0; /* not used */
4117*4882a593Smuzhiyun 	if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4118*4882a593Smuzhiyun 		return SMB2_OPLOCK_LEVEL_NOCHANGE;
4119*4882a593Smuzhiyun 	return le32_to_cpu(lc->lcontext.LeaseState);
4120*4882a593Smuzhiyun }
4121*4882a593Smuzhiyun 
4122*4882a593Smuzhiyun static __u8
smb3_parse_lease_buf(void * buf,unsigned int * epoch,char * lease_key)4123*4882a593Smuzhiyun smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4124*4882a593Smuzhiyun {
4125*4882a593Smuzhiyun 	struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4126*4882a593Smuzhiyun 
4127*4882a593Smuzhiyun 	*epoch = le16_to_cpu(lc->lcontext.Epoch);
4128*4882a593Smuzhiyun 	if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4129*4882a593Smuzhiyun 		return SMB2_OPLOCK_LEVEL_NOCHANGE;
4130*4882a593Smuzhiyun 	if (lease_key)
4131*4882a593Smuzhiyun 		memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4132*4882a593Smuzhiyun 	return le32_to_cpu(lc->lcontext.LeaseState);
4133*4882a593Smuzhiyun }
4134*4882a593Smuzhiyun 
4135*4882a593Smuzhiyun static unsigned int
smb2_wp_retry_size(struct inode * inode)4136*4882a593Smuzhiyun smb2_wp_retry_size(struct inode *inode)
4137*4882a593Smuzhiyun {
4138*4882a593Smuzhiyun 	return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
4139*4882a593Smuzhiyun 		     SMB2_MAX_BUFFER_SIZE);
4140*4882a593Smuzhiyun }
4141*4882a593Smuzhiyun 
4142*4882a593Smuzhiyun static bool
smb2_dir_needs_close(struct cifsFileInfo * cfile)4143*4882a593Smuzhiyun smb2_dir_needs_close(struct cifsFileInfo *cfile)
4144*4882a593Smuzhiyun {
4145*4882a593Smuzhiyun 	return !cfile->invalidHandle;
4146*4882a593Smuzhiyun }
4147*4882a593Smuzhiyun 
4148*4882a593Smuzhiyun static void
fill_transform_hdr(struct smb2_transform_hdr * tr_hdr,unsigned int orig_len,struct smb_rqst * old_rq,__le16 cipher_type)4149*4882a593Smuzhiyun fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4150*4882a593Smuzhiyun 		   struct smb_rqst *old_rq, __le16 cipher_type)
4151*4882a593Smuzhiyun {
4152*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr =
4153*4882a593Smuzhiyun 			(struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
4154*4882a593Smuzhiyun 
4155*4882a593Smuzhiyun 	memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4156*4882a593Smuzhiyun 	tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4157*4882a593Smuzhiyun 	tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4158*4882a593Smuzhiyun 	tr_hdr->Flags = cpu_to_le16(0x01);
4159*4882a593Smuzhiyun 	if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4160*4882a593Smuzhiyun 	    (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4161*4882a593Smuzhiyun 		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4162*4882a593Smuzhiyun 	else
4163*4882a593Smuzhiyun 		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4164*4882a593Smuzhiyun 	memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4165*4882a593Smuzhiyun }
4166*4882a593Smuzhiyun 
4167*4882a593Smuzhiyun /* We can not use the normal sg_set_buf() as we will sometimes pass a
4168*4882a593Smuzhiyun  * stack object as buf.
4169*4882a593Smuzhiyun  */
smb2_sg_set_buf(struct scatterlist * sg,const void * buf,unsigned int buflen)4170*4882a593Smuzhiyun static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4171*4882a593Smuzhiyun 				   unsigned int buflen)
4172*4882a593Smuzhiyun {
4173*4882a593Smuzhiyun 	void *addr;
4174*4882a593Smuzhiyun 	/*
4175*4882a593Smuzhiyun 	 * VMAP_STACK (at least) puts stack into the vmalloc address space
4176*4882a593Smuzhiyun 	 */
4177*4882a593Smuzhiyun 	if (is_vmalloc_addr(buf))
4178*4882a593Smuzhiyun 		addr = vmalloc_to_page(buf);
4179*4882a593Smuzhiyun 	else
4180*4882a593Smuzhiyun 		addr = virt_to_page(buf);
4181*4882a593Smuzhiyun 	sg_set_page(sg, addr, buflen, offset_in_page(buf));
4182*4882a593Smuzhiyun }
4183*4882a593Smuzhiyun 
4184*4882a593Smuzhiyun /* Assumes the first rqst has a transform header as the first iov.
4185*4882a593Smuzhiyun  * I.e.
4186*4882a593Smuzhiyun  * rqst[0].rq_iov[0]  is transform header
4187*4882a593Smuzhiyun  * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4188*4882a593Smuzhiyun  * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4189*4882a593Smuzhiyun  */
4190*4882a593Smuzhiyun static struct scatterlist *
init_sg(int num_rqst,struct smb_rqst * rqst,u8 * sign)4191*4882a593Smuzhiyun init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4192*4882a593Smuzhiyun {
4193*4882a593Smuzhiyun 	unsigned int sg_len;
4194*4882a593Smuzhiyun 	struct scatterlist *sg;
4195*4882a593Smuzhiyun 	unsigned int i;
4196*4882a593Smuzhiyun 	unsigned int j;
4197*4882a593Smuzhiyun 	unsigned int idx = 0;
4198*4882a593Smuzhiyun 	int skip;
4199*4882a593Smuzhiyun 
4200*4882a593Smuzhiyun 	sg_len = 1;
4201*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++)
4202*4882a593Smuzhiyun 		sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4203*4882a593Smuzhiyun 
4204*4882a593Smuzhiyun 	sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4205*4882a593Smuzhiyun 	if (!sg)
4206*4882a593Smuzhiyun 		return NULL;
4207*4882a593Smuzhiyun 
4208*4882a593Smuzhiyun 	sg_init_table(sg, sg_len);
4209*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
4210*4882a593Smuzhiyun 		for (j = 0; j < rqst[i].rq_nvec; j++) {
4211*4882a593Smuzhiyun 			/*
4212*4882a593Smuzhiyun 			 * The first rqst has a transform header where the
4213*4882a593Smuzhiyun 			 * first 20 bytes are not part of the encrypted blob
4214*4882a593Smuzhiyun 			 */
4215*4882a593Smuzhiyun 			skip = (i == 0) && (j == 0) ? 20 : 0;
4216*4882a593Smuzhiyun 			smb2_sg_set_buf(&sg[idx++],
4217*4882a593Smuzhiyun 					rqst[i].rq_iov[j].iov_base + skip,
4218*4882a593Smuzhiyun 					rqst[i].rq_iov[j].iov_len - skip);
4219*4882a593Smuzhiyun 			}
4220*4882a593Smuzhiyun 
4221*4882a593Smuzhiyun 		for (j = 0; j < rqst[i].rq_npages; j++) {
4222*4882a593Smuzhiyun 			unsigned int len, offset;
4223*4882a593Smuzhiyun 
4224*4882a593Smuzhiyun 			rqst_page_get_length(&rqst[i], j, &len, &offset);
4225*4882a593Smuzhiyun 			sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4226*4882a593Smuzhiyun 		}
4227*4882a593Smuzhiyun 	}
4228*4882a593Smuzhiyun 	smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4229*4882a593Smuzhiyun 	return sg;
4230*4882a593Smuzhiyun }
4231*4882a593Smuzhiyun 
4232*4882a593Smuzhiyun static int
smb2_get_enc_key(struct TCP_Server_Info * server,__u64 ses_id,int enc,u8 * key)4233*4882a593Smuzhiyun smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4234*4882a593Smuzhiyun {
4235*4882a593Smuzhiyun 	struct cifs_ses *ses;
4236*4882a593Smuzhiyun 	u8 *ses_enc_key;
4237*4882a593Smuzhiyun 
4238*4882a593Smuzhiyun 	spin_lock(&cifs_tcp_ses_lock);
4239*4882a593Smuzhiyun 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4240*4882a593Smuzhiyun 		list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4241*4882a593Smuzhiyun 			if (ses->Suid == ses_id) {
4242*4882a593Smuzhiyun 				ses_enc_key = enc ? ses->smb3encryptionkey :
4243*4882a593Smuzhiyun 					ses->smb3decryptionkey;
4244*4882a593Smuzhiyun 				memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4245*4882a593Smuzhiyun 				spin_unlock(&cifs_tcp_ses_lock);
4246*4882a593Smuzhiyun 				return 0;
4247*4882a593Smuzhiyun 			}
4248*4882a593Smuzhiyun 		}
4249*4882a593Smuzhiyun 	}
4250*4882a593Smuzhiyun 	spin_unlock(&cifs_tcp_ses_lock);
4251*4882a593Smuzhiyun 
4252*4882a593Smuzhiyun 	return -EAGAIN;
4253*4882a593Smuzhiyun }
4254*4882a593Smuzhiyun /*
4255*4882a593Smuzhiyun  * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4256*4882a593Smuzhiyun  * iov[0]   - transform header (associate data),
4257*4882a593Smuzhiyun  * iov[1-N] - SMB2 header and pages - data to encrypt.
4258*4882a593Smuzhiyun  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4259*4882a593Smuzhiyun  * untouched.
4260*4882a593Smuzhiyun  */
4261*4882a593Smuzhiyun static int
crypt_message(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst,int enc)4262*4882a593Smuzhiyun crypt_message(struct TCP_Server_Info *server, int num_rqst,
4263*4882a593Smuzhiyun 	      struct smb_rqst *rqst, int enc)
4264*4882a593Smuzhiyun {
4265*4882a593Smuzhiyun 	struct smb2_transform_hdr *tr_hdr =
4266*4882a593Smuzhiyun 		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4267*4882a593Smuzhiyun 	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4268*4882a593Smuzhiyun 	int rc = 0;
4269*4882a593Smuzhiyun 	struct scatterlist *sg;
4270*4882a593Smuzhiyun 	u8 sign[SMB2_SIGNATURE_SIZE] = {};
4271*4882a593Smuzhiyun 	u8 key[SMB3_ENC_DEC_KEY_SIZE];
4272*4882a593Smuzhiyun 	struct aead_request *req;
4273*4882a593Smuzhiyun 	char *iv;
4274*4882a593Smuzhiyun 	unsigned int iv_len;
4275*4882a593Smuzhiyun 	DECLARE_CRYPTO_WAIT(wait);
4276*4882a593Smuzhiyun 	struct crypto_aead *tfm;
4277*4882a593Smuzhiyun 	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4278*4882a593Smuzhiyun 
4279*4882a593Smuzhiyun 	rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4280*4882a593Smuzhiyun 	if (rc) {
4281*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4282*4882a593Smuzhiyun 			 enc ? "en" : "de");
4283*4882a593Smuzhiyun 		return rc;
4284*4882a593Smuzhiyun 	}
4285*4882a593Smuzhiyun 
4286*4882a593Smuzhiyun 	rc = smb3_crypto_aead_allocate(server);
4287*4882a593Smuzhiyun 	if (rc) {
4288*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4289*4882a593Smuzhiyun 		return rc;
4290*4882a593Smuzhiyun 	}
4291*4882a593Smuzhiyun 
4292*4882a593Smuzhiyun 	tfm = enc ? server->secmech.ccmaesencrypt :
4293*4882a593Smuzhiyun 						server->secmech.ccmaesdecrypt;
4294*4882a593Smuzhiyun 
4295*4882a593Smuzhiyun 	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4296*4882a593Smuzhiyun 		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4297*4882a593Smuzhiyun 		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4298*4882a593Smuzhiyun 	else
4299*4882a593Smuzhiyun 		rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4300*4882a593Smuzhiyun 
4301*4882a593Smuzhiyun 	if (rc) {
4302*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4303*4882a593Smuzhiyun 		return rc;
4304*4882a593Smuzhiyun 	}
4305*4882a593Smuzhiyun 
4306*4882a593Smuzhiyun 	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4307*4882a593Smuzhiyun 	if (rc) {
4308*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4309*4882a593Smuzhiyun 		return rc;
4310*4882a593Smuzhiyun 	}
4311*4882a593Smuzhiyun 
4312*4882a593Smuzhiyun 	req = aead_request_alloc(tfm, GFP_KERNEL);
4313*4882a593Smuzhiyun 	if (!req) {
4314*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4315*4882a593Smuzhiyun 		return -ENOMEM;
4316*4882a593Smuzhiyun 	}
4317*4882a593Smuzhiyun 
4318*4882a593Smuzhiyun 	if (!enc) {
4319*4882a593Smuzhiyun 		memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4320*4882a593Smuzhiyun 		crypt_len += SMB2_SIGNATURE_SIZE;
4321*4882a593Smuzhiyun 	}
4322*4882a593Smuzhiyun 
4323*4882a593Smuzhiyun 	sg = init_sg(num_rqst, rqst, sign);
4324*4882a593Smuzhiyun 	if (!sg) {
4325*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4326*4882a593Smuzhiyun 		rc = -ENOMEM;
4327*4882a593Smuzhiyun 		goto free_req;
4328*4882a593Smuzhiyun 	}
4329*4882a593Smuzhiyun 
4330*4882a593Smuzhiyun 	iv_len = crypto_aead_ivsize(tfm);
4331*4882a593Smuzhiyun 	iv = kzalloc(iv_len, GFP_KERNEL);
4332*4882a593Smuzhiyun 	if (!iv) {
4333*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4334*4882a593Smuzhiyun 		rc = -ENOMEM;
4335*4882a593Smuzhiyun 		goto free_sg;
4336*4882a593Smuzhiyun 	}
4337*4882a593Smuzhiyun 
4338*4882a593Smuzhiyun 	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4339*4882a593Smuzhiyun 	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4340*4882a593Smuzhiyun 		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4341*4882a593Smuzhiyun 	else {
4342*4882a593Smuzhiyun 		iv[0] = 3;
4343*4882a593Smuzhiyun 		memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4344*4882a593Smuzhiyun 	}
4345*4882a593Smuzhiyun 
4346*4882a593Smuzhiyun 	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4347*4882a593Smuzhiyun 	aead_request_set_ad(req, assoc_data_len);
4348*4882a593Smuzhiyun 
4349*4882a593Smuzhiyun 	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4350*4882a593Smuzhiyun 				  crypto_req_done, &wait);
4351*4882a593Smuzhiyun 
4352*4882a593Smuzhiyun 	rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4353*4882a593Smuzhiyun 				: crypto_aead_decrypt(req), &wait);
4354*4882a593Smuzhiyun 
4355*4882a593Smuzhiyun 	if (!rc && enc)
4356*4882a593Smuzhiyun 		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4357*4882a593Smuzhiyun 
4358*4882a593Smuzhiyun 	kfree(iv);
4359*4882a593Smuzhiyun free_sg:
4360*4882a593Smuzhiyun 	kfree(sg);
4361*4882a593Smuzhiyun free_req:
4362*4882a593Smuzhiyun 	kfree(req);
4363*4882a593Smuzhiyun 	return rc;
4364*4882a593Smuzhiyun }
4365*4882a593Smuzhiyun 
4366*4882a593Smuzhiyun void
smb3_free_compound_rqst(int num_rqst,struct smb_rqst * rqst)4367*4882a593Smuzhiyun smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4368*4882a593Smuzhiyun {
4369*4882a593Smuzhiyun 	int i, j;
4370*4882a593Smuzhiyun 
4371*4882a593Smuzhiyun 	for (i = 0; i < num_rqst; i++) {
4372*4882a593Smuzhiyun 		if (rqst[i].rq_pages) {
4373*4882a593Smuzhiyun 			for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4374*4882a593Smuzhiyun 				put_page(rqst[i].rq_pages[j]);
4375*4882a593Smuzhiyun 			kfree(rqst[i].rq_pages);
4376*4882a593Smuzhiyun 		}
4377*4882a593Smuzhiyun 	}
4378*4882a593Smuzhiyun }
4379*4882a593Smuzhiyun 
4380*4882a593Smuzhiyun /*
4381*4882a593Smuzhiyun  * This function will initialize new_rq and encrypt the content.
4382*4882a593Smuzhiyun  * The first entry, new_rq[0], only contains a single iov which contains
4383*4882a593Smuzhiyun  * a smb2_transform_hdr and is pre-allocated by the caller.
4384*4882a593Smuzhiyun  * This function then populates new_rq[1+] with the content from olq_rq[0+].
4385*4882a593Smuzhiyun  *
4386*4882a593Smuzhiyun  * The end result is an array of smb_rqst structures where the first structure
4387*4882a593Smuzhiyun  * only contains a single iov for the transform header which we then can pass
4388*4882a593Smuzhiyun  * to crypt_message().
4389*4882a593Smuzhiyun  *
4390*4882a593Smuzhiyun  * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
4391*4882a593Smuzhiyun  * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4392*4882a593Smuzhiyun  */
4393*4882a593Smuzhiyun static int
smb3_init_transform_rq(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * new_rq,struct smb_rqst * old_rq)4394*4882a593Smuzhiyun smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4395*4882a593Smuzhiyun 		       struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4396*4882a593Smuzhiyun {
4397*4882a593Smuzhiyun 	struct page **pages;
4398*4882a593Smuzhiyun 	struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4399*4882a593Smuzhiyun 	unsigned int npages;
4400*4882a593Smuzhiyun 	unsigned int orig_len = 0;
4401*4882a593Smuzhiyun 	int i, j;
4402*4882a593Smuzhiyun 	int rc = -ENOMEM;
4403*4882a593Smuzhiyun 
4404*4882a593Smuzhiyun 	for (i = 1; i < num_rqst; i++) {
4405*4882a593Smuzhiyun 		npages = old_rq[i - 1].rq_npages;
4406*4882a593Smuzhiyun 		pages = kmalloc_array(npages, sizeof(struct page *),
4407*4882a593Smuzhiyun 				      GFP_KERNEL);
4408*4882a593Smuzhiyun 		if (!pages)
4409*4882a593Smuzhiyun 			goto err_free;
4410*4882a593Smuzhiyun 
4411*4882a593Smuzhiyun 		new_rq[i].rq_pages = pages;
4412*4882a593Smuzhiyun 		new_rq[i].rq_npages = npages;
4413*4882a593Smuzhiyun 		new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4414*4882a593Smuzhiyun 		new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4415*4882a593Smuzhiyun 		new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4416*4882a593Smuzhiyun 		new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4417*4882a593Smuzhiyun 		new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4418*4882a593Smuzhiyun 
4419*4882a593Smuzhiyun 		orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4420*4882a593Smuzhiyun 
4421*4882a593Smuzhiyun 		for (j = 0; j < npages; j++) {
4422*4882a593Smuzhiyun 			pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4423*4882a593Smuzhiyun 			if (!pages[j])
4424*4882a593Smuzhiyun 				goto err_free;
4425*4882a593Smuzhiyun 		}
4426*4882a593Smuzhiyun 
4427*4882a593Smuzhiyun 		/* copy pages form the old */
4428*4882a593Smuzhiyun 		for (j = 0; j < npages; j++) {
4429*4882a593Smuzhiyun 			char *dst, *src;
4430*4882a593Smuzhiyun 			unsigned int offset, len;
4431*4882a593Smuzhiyun 
4432*4882a593Smuzhiyun 			rqst_page_get_length(&new_rq[i], j, &len, &offset);
4433*4882a593Smuzhiyun 
4434*4882a593Smuzhiyun 			dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4435*4882a593Smuzhiyun 			src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4436*4882a593Smuzhiyun 
4437*4882a593Smuzhiyun 			memcpy(dst, src, len);
4438*4882a593Smuzhiyun 			kunmap(new_rq[i].rq_pages[j]);
4439*4882a593Smuzhiyun 			kunmap(old_rq[i - 1].rq_pages[j]);
4440*4882a593Smuzhiyun 		}
4441*4882a593Smuzhiyun 	}
4442*4882a593Smuzhiyun 
4443*4882a593Smuzhiyun 	/* fill the 1st iov with a transform header */
4444*4882a593Smuzhiyun 	fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4445*4882a593Smuzhiyun 
4446*4882a593Smuzhiyun 	rc = crypt_message(server, num_rqst, new_rq, 1);
4447*4882a593Smuzhiyun 	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4448*4882a593Smuzhiyun 	if (rc)
4449*4882a593Smuzhiyun 		goto err_free;
4450*4882a593Smuzhiyun 
4451*4882a593Smuzhiyun 	return rc;
4452*4882a593Smuzhiyun 
4453*4882a593Smuzhiyun err_free:
4454*4882a593Smuzhiyun 	smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4455*4882a593Smuzhiyun 	return rc;
4456*4882a593Smuzhiyun }
4457*4882a593Smuzhiyun 
4458*4882a593Smuzhiyun static int
smb3_is_transform_hdr(void * buf)4459*4882a593Smuzhiyun smb3_is_transform_hdr(void *buf)
4460*4882a593Smuzhiyun {
4461*4882a593Smuzhiyun 	struct smb2_transform_hdr *trhdr = buf;
4462*4882a593Smuzhiyun 
4463*4882a593Smuzhiyun 	return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4464*4882a593Smuzhiyun }
4465*4882a593Smuzhiyun 
4466*4882a593Smuzhiyun static int
decrypt_raw_data(struct TCP_Server_Info * server,char * buf,unsigned int buf_data_size,struct page ** pages,unsigned int npages,unsigned int page_data_size,bool is_offloaded)4467*4882a593Smuzhiyun decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4468*4882a593Smuzhiyun 		 unsigned int buf_data_size, struct page **pages,
4469*4882a593Smuzhiyun 		 unsigned int npages, unsigned int page_data_size,
4470*4882a593Smuzhiyun 		 bool is_offloaded)
4471*4882a593Smuzhiyun {
4472*4882a593Smuzhiyun 	struct kvec iov[2];
4473*4882a593Smuzhiyun 	struct smb_rqst rqst = {NULL};
4474*4882a593Smuzhiyun 	int rc;
4475*4882a593Smuzhiyun 
4476*4882a593Smuzhiyun 	iov[0].iov_base = buf;
4477*4882a593Smuzhiyun 	iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4478*4882a593Smuzhiyun 	iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4479*4882a593Smuzhiyun 	iov[1].iov_len = buf_data_size;
4480*4882a593Smuzhiyun 
4481*4882a593Smuzhiyun 	rqst.rq_iov = iov;
4482*4882a593Smuzhiyun 	rqst.rq_nvec = 2;
4483*4882a593Smuzhiyun 	rqst.rq_pages = pages;
4484*4882a593Smuzhiyun 	rqst.rq_npages = npages;
4485*4882a593Smuzhiyun 	rqst.rq_pagesz = PAGE_SIZE;
4486*4882a593Smuzhiyun 	rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4487*4882a593Smuzhiyun 
4488*4882a593Smuzhiyun 	rc = crypt_message(server, 1, &rqst, 0);
4489*4882a593Smuzhiyun 	cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4490*4882a593Smuzhiyun 
4491*4882a593Smuzhiyun 	if (rc)
4492*4882a593Smuzhiyun 		return rc;
4493*4882a593Smuzhiyun 
4494*4882a593Smuzhiyun 	memmove(buf, iov[1].iov_base, buf_data_size);
4495*4882a593Smuzhiyun 
4496*4882a593Smuzhiyun 	if (!is_offloaded)
4497*4882a593Smuzhiyun 		server->total_read = buf_data_size + page_data_size;
4498*4882a593Smuzhiyun 
4499*4882a593Smuzhiyun 	return rc;
4500*4882a593Smuzhiyun }
4501*4882a593Smuzhiyun 
4502*4882a593Smuzhiyun static int
read_data_into_pages(struct TCP_Server_Info * server,struct page ** pages,unsigned int npages,unsigned int len)4503*4882a593Smuzhiyun read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4504*4882a593Smuzhiyun 		     unsigned int npages, unsigned int len)
4505*4882a593Smuzhiyun {
4506*4882a593Smuzhiyun 	int i;
4507*4882a593Smuzhiyun 	int length;
4508*4882a593Smuzhiyun 
4509*4882a593Smuzhiyun 	for (i = 0; i < npages; i++) {
4510*4882a593Smuzhiyun 		struct page *page = pages[i];
4511*4882a593Smuzhiyun 		size_t n;
4512*4882a593Smuzhiyun 
4513*4882a593Smuzhiyun 		n = len;
4514*4882a593Smuzhiyun 		if (len >= PAGE_SIZE) {
4515*4882a593Smuzhiyun 			/* enough data to fill the page */
4516*4882a593Smuzhiyun 			n = PAGE_SIZE;
4517*4882a593Smuzhiyun 			len -= n;
4518*4882a593Smuzhiyun 		} else {
4519*4882a593Smuzhiyun 			zero_user(page, len, PAGE_SIZE - len);
4520*4882a593Smuzhiyun 			len = 0;
4521*4882a593Smuzhiyun 		}
4522*4882a593Smuzhiyun 		length = cifs_read_page_from_socket(server, page, 0, n);
4523*4882a593Smuzhiyun 		if (length < 0)
4524*4882a593Smuzhiyun 			return length;
4525*4882a593Smuzhiyun 		server->total_read += length;
4526*4882a593Smuzhiyun 	}
4527*4882a593Smuzhiyun 
4528*4882a593Smuzhiyun 	return 0;
4529*4882a593Smuzhiyun }
4530*4882a593Smuzhiyun 
4531*4882a593Smuzhiyun static int
init_read_bvec(struct page ** pages,unsigned int npages,unsigned int data_size,unsigned int cur_off,struct bio_vec ** page_vec)4532*4882a593Smuzhiyun init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4533*4882a593Smuzhiyun 	       unsigned int cur_off, struct bio_vec **page_vec)
4534*4882a593Smuzhiyun {
4535*4882a593Smuzhiyun 	struct bio_vec *bvec;
4536*4882a593Smuzhiyun 	int i;
4537*4882a593Smuzhiyun 
4538*4882a593Smuzhiyun 	bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4539*4882a593Smuzhiyun 	if (!bvec)
4540*4882a593Smuzhiyun 		return -ENOMEM;
4541*4882a593Smuzhiyun 
4542*4882a593Smuzhiyun 	for (i = 0; i < npages; i++) {
4543*4882a593Smuzhiyun 		bvec[i].bv_page = pages[i];
4544*4882a593Smuzhiyun 		bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4545*4882a593Smuzhiyun 		bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4546*4882a593Smuzhiyun 		data_size -= bvec[i].bv_len;
4547*4882a593Smuzhiyun 	}
4548*4882a593Smuzhiyun 
4549*4882a593Smuzhiyun 	if (data_size != 0) {
4550*4882a593Smuzhiyun 		cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4551*4882a593Smuzhiyun 		kfree(bvec);
4552*4882a593Smuzhiyun 		return -EIO;
4553*4882a593Smuzhiyun 	}
4554*4882a593Smuzhiyun 
4555*4882a593Smuzhiyun 	*page_vec = bvec;
4556*4882a593Smuzhiyun 	return 0;
4557*4882a593Smuzhiyun }
4558*4882a593Smuzhiyun 
4559*4882a593Smuzhiyun static int
handle_read_data(struct TCP_Server_Info * server,struct mid_q_entry * mid,char * buf,unsigned int buf_len,struct page ** pages,unsigned int npages,unsigned int page_data_size,bool is_offloaded)4560*4882a593Smuzhiyun handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4561*4882a593Smuzhiyun 		 char *buf, unsigned int buf_len, struct page **pages,
4562*4882a593Smuzhiyun 		 unsigned int npages, unsigned int page_data_size,
4563*4882a593Smuzhiyun 		 bool is_offloaded)
4564*4882a593Smuzhiyun {
4565*4882a593Smuzhiyun 	unsigned int data_offset;
4566*4882a593Smuzhiyun 	unsigned int data_len;
4567*4882a593Smuzhiyun 	unsigned int cur_off;
4568*4882a593Smuzhiyun 	unsigned int cur_page_idx;
4569*4882a593Smuzhiyun 	unsigned int pad_len;
4570*4882a593Smuzhiyun 	struct cifs_readdata *rdata = mid->callback_data;
4571*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4572*4882a593Smuzhiyun 	struct bio_vec *bvec = NULL;
4573*4882a593Smuzhiyun 	struct iov_iter iter;
4574*4882a593Smuzhiyun 	struct kvec iov;
4575*4882a593Smuzhiyun 	int length;
4576*4882a593Smuzhiyun 	bool use_rdma_mr = false;
4577*4882a593Smuzhiyun 
4578*4882a593Smuzhiyun 	if (shdr->Command != SMB2_READ) {
4579*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "only big read responses are supported\n");
4580*4882a593Smuzhiyun 		return -ENOTSUPP;
4581*4882a593Smuzhiyun 	}
4582*4882a593Smuzhiyun 
4583*4882a593Smuzhiyun 	if (server->ops->is_session_expired &&
4584*4882a593Smuzhiyun 	    server->ops->is_session_expired(buf)) {
4585*4882a593Smuzhiyun 		if (!is_offloaded)
4586*4882a593Smuzhiyun 			cifs_reconnect(server);
4587*4882a593Smuzhiyun 		return -1;
4588*4882a593Smuzhiyun 	}
4589*4882a593Smuzhiyun 
4590*4882a593Smuzhiyun 	if (server->ops->is_status_pending &&
4591*4882a593Smuzhiyun 			server->ops->is_status_pending(buf, server))
4592*4882a593Smuzhiyun 		return -1;
4593*4882a593Smuzhiyun 
4594*4882a593Smuzhiyun 	/* set up first two iov to get credits */
4595*4882a593Smuzhiyun 	rdata->iov[0].iov_base = buf;
4596*4882a593Smuzhiyun 	rdata->iov[0].iov_len = 0;
4597*4882a593Smuzhiyun 	rdata->iov[1].iov_base = buf;
4598*4882a593Smuzhiyun 	rdata->iov[1].iov_len =
4599*4882a593Smuzhiyun 		min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4600*4882a593Smuzhiyun 	cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4601*4882a593Smuzhiyun 		 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4602*4882a593Smuzhiyun 	cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4603*4882a593Smuzhiyun 		 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4604*4882a593Smuzhiyun 
4605*4882a593Smuzhiyun 	rdata->result = server->ops->map_error(buf, true);
4606*4882a593Smuzhiyun 	if (rdata->result != 0) {
4607*4882a593Smuzhiyun 		cifs_dbg(FYI, "%s: server returned error %d\n",
4608*4882a593Smuzhiyun 			 __func__, rdata->result);
4609*4882a593Smuzhiyun 		/* normal error on read response */
4610*4882a593Smuzhiyun 		if (is_offloaded)
4611*4882a593Smuzhiyun 			mid->mid_state = MID_RESPONSE_RECEIVED;
4612*4882a593Smuzhiyun 		else
4613*4882a593Smuzhiyun 			dequeue_mid(mid, false);
4614*4882a593Smuzhiyun 		return 0;
4615*4882a593Smuzhiyun 	}
4616*4882a593Smuzhiyun 
4617*4882a593Smuzhiyun 	data_offset = server->ops->read_data_offset(buf);
4618*4882a593Smuzhiyun #ifdef CONFIG_CIFS_SMB_DIRECT
4619*4882a593Smuzhiyun 	use_rdma_mr = rdata->mr;
4620*4882a593Smuzhiyun #endif
4621*4882a593Smuzhiyun 	data_len = server->ops->read_data_length(buf, use_rdma_mr);
4622*4882a593Smuzhiyun 
4623*4882a593Smuzhiyun 	if (data_offset < server->vals->read_rsp_size) {
4624*4882a593Smuzhiyun 		/*
4625*4882a593Smuzhiyun 		 * win2k8 sometimes sends an offset of 0 when the read
4626*4882a593Smuzhiyun 		 * is beyond the EOF. Treat it as if the data starts just after
4627*4882a593Smuzhiyun 		 * the header.
4628*4882a593Smuzhiyun 		 */
4629*4882a593Smuzhiyun 		cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4630*4882a593Smuzhiyun 			 __func__, data_offset);
4631*4882a593Smuzhiyun 		data_offset = server->vals->read_rsp_size;
4632*4882a593Smuzhiyun 	} else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4633*4882a593Smuzhiyun 		/* data_offset is beyond the end of smallbuf */
4634*4882a593Smuzhiyun 		cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4635*4882a593Smuzhiyun 			 __func__, data_offset);
4636*4882a593Smuzhiyun 		rdata->result = -EIO;
4637*4882a593Smuzhiyun 		if (is_offloaded)
4638*4882a593Smuzhiyun 			mid->mid_state = MID_RESPONSE_MALFORMED;
4639*4882a593Smuzhiyun 		else
4640*4882a593Smuzhiyun 			dequeue_mid(mid, rdata->result);
4641*4882a593Smuzhiyun 		return 0;
4642*4882a593Smuzhiyun 	}
4643*4882a593Smuzhiyun 
4644*4882a593Smuzhiyun 	pad_len = data_offset - server->vals->read_rsp_size;
4645*4882a593Smuzhiyun 
4646*4882a593Smuzhiyun 	if (buf_len <= data_offset) {
4647*4882a593Smuzhiyun 		/* read response payload is in pages */
4648*4882a593Smuzhiyun 		cur_page_idx = pad_len / PAGE_SIZE;
4649*4882a593Smuzhiyun 		cur_off = pad_len % PAGE_SIZE;
4650*4882a593Smuzhiyun 
4651*4882a593Smuzhiyun 		if (cur_page_idx != 0) {
4652*4882a593Smuzhiyun 			/* data offset is beyond the 1st page of response */
4653*4882a593Smuzhiyun 			cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4654*4882a593Smuzhiyun 				 __func__, data_offset);
4655*4882a593Smuzhiyun 			rdata->result = -EIO;
4656*4882a593Smuzhiyun 			if (is_offloaded)
4657*4882a593Smuzhiyun 				mid->mid_state = MID_RESPONSE_MALFORMED;
4658*4882a593Smuzhiyun 			else
4659*4882a593Smuzhiyun 				dequeue_mid(mid, rdata->result);
4660*4882a593Smuzhiyun 			return 0;
4661*4882a593Smuzhiyun 		}
4662*4882a593Smuzhiyun 
4663*4882a593Smuzhiyun 		if (data_len > page_data_size - pad_len) {
4664*4882a593Smuzhiyun 			/* data_len is corrupt -- discard frame */
4665*4882a593Smuzhiyun 			rdata->result = -EIO;
4666*4882a593Smuzhiyun 			if (is_offloaded)
4667*4882a593Smuzhiyun 				mid->mid_state = MID_RESPONSE_MALFORMED;
4668*4882a593Smuzhiyun 			else
4669*4882a593Smuzhiyun 				dequeue_mid(mid, rdata->result);
4670*4882a593Smuzhiyun 			return 0;
4671*4882a593Smuzhiyun 		}
4672*4882a593Smuzhiyun 
4673*4882a593Smuzhiyun 		rdata->result = init_read_bvec(pages, npages, page_data_size,
4674*4882a593Smuzhiyun 					       cur_off, &bvec);
4675*4882a593Smuzhiyun 		if (rdata->result != 0) {
4676*4882a593Smuzhiyun 			if (is_offloaded)
4677*4882a593Smuzhiyun 				mid->mid_state = MID_RESPONSE_MALFORMED;
4678*4882a593Smuzhiyun 			else
4679*4882a593Smuzhiyun 				dequeue_mid(mid, rdata->result);
4680*4882a593Smuzhiyun 			return 0;
4681*4882a593Smuzhiyun 		}
4682*4882a593Smuzhiyun 
4683*4882a593Smuzhiyun 		iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4684*4882a593Smuzhiyun 	} else if (buf_len >= data_offset + data_len) {
4685*4882a593Smuzhiyun 		/* read response payload is in buf */
4686*4882a593Smuzhiyun 		WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4687*4882a593Smuzhiyun 		iov.iov_base = buf + data_offset;
4688*4882a593Smuzhiyun 		iov.iov_len = data_len;
4689*4882a593Smuzhiyun 		iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4690*4882a593Smuzhiyun 	} else {
4691*4882a593Smuzhiyun 		/* read response payload cannot be in both buf and pages */
4692*4882a593Smuzhiyun 		WARN_ONCE(1, "buf can not contain only a part of read data");
4693*4882a593Smuzhiyun 		rdata->result = -EIO;
4694*4882a593Smuzhiyun 		if (is_offloaded)
4695*4882a593Smuzhiyun 			mid->mid_state = MID_RESPONSE_MALFORMED;
4696*4882a593Smuzhiyun 		else
4697*4882a593Smuzhiyun 			dequeue_mid(mid, rdata->result);
4698*4882a593Smuzhiyun 		return 0;
4699*4882a593Smuzhiyun 	}
4700*4882a593Smuzhiyun 
4701*4882a593Smuzhiyun 	length = rdata->copy_into_pages(server, rdata, &iter);
4702*4882a593Smuzhiyun 
4703*4882a593Smuzhiyun 	kfree(bvec);
4704*4882a593Smuzhiyun 
4705*4882a593Smuzhiyun 	if (length < 0)
4706*4882a593Smuzhiyun 		return length;
4707*4882a593Smuzhiyun 
4708*4882a593Smuzhiyun 	if (is_offloaded)
4709*4882a593Smuzhiyun 		mid->mid_state = MID_RESPONSE_RECEIVED;
4710*4882a593Smuzhiyun 	else
4711*4882a593Smuzhiyun 		dequeue_mid(mid, false);
4712*4882a593Smuzhiyun 	return length;
4713*4882a593Smuzhiyun }
4714*4882a593Smuzhiyun 
4715*4882a593Smuzhiyun struct smb2_decrypt_work {
4716*4882a593Smuzhiyun 	struct work_struct decrypt;
4717*4882a593Smuzhiyun 	struct TCP_Server_Info *server;
4718*4882a593Smuzhiyun 	struct page **ppages;
4719*4882a593Smuzhiyun 	char *buf;
4720*4882a593Smuzhiyun 	unsigned int npages;
4721*4882a593Smuzhiyun 	unsigned int len;
4722*4882a593Smuzhiyun };
4723*4882a593Smuzhiyun 
4724*4882a593Smuzhiyun 
smb2_decrypt_offload(struct work_struct * work)4725*4882a593Smuzhiyun static void smb2_decrypt_offload(struct work_struct *work)
4726*4882a593Smuzhiyun {
4727*4882a593Smuzhiyun 	struct smb2_decrypt_work *dw = container_of(work,
4728*4882a593Smuzhiyun 				struct smb2_decrypt_work, decrypt);
4729*4882a593Smuzhiyun 	int i, rc;
4730*4882a593Smuzhiyun 	struct mid_q_entry *mid;
4731*4882a593Smuzhiyun 
4732*4882a593Smuzhiyun 	rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4733*4882a593Smuzhiyun 			      dw->ppages, dw->npages, dw->len, true);
4734*4882a593Smuzhiyun 	if (rc) {
4735*4882a593Smuzhiyun 		cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4736*4882a593Smuzhiyun 		goto free_pages;
4737*4882a593Smuzhiyun 	}
4738*4882a593Smuzhiyun 
4739*4882a593Smuzhiyun 	dw->server->lstrp = jiffies;
4740*4882a593Smuzhiyun 	mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4741*4882a593Smuzhiyun 	if (mid == NULL)
4742*4882a593Smuzhiyun 		cifs_dbg(FYI, "mid not found\n");
4743*4882a593Smuzhiyun 	else {
4744*4882a593Smuzhiyun 		mid->decrypted = true;
4745*4882a593Smuzhiyun 		rc = handle_read_data(dw->server, mid, dw->buf,
4746*4882a593Smuzhiyun 				      dw->server->vals->read_rsp_size,
4747*4882a593Smuzhiyun 				      dw->ppages, dw->npages, dw->len,
4748*4882a593Smuzhiyun 				      true);
4749*4882a593Smuzhiyun 		if (rc >= 0) {
4750*4882a593Smuzhiyun #ifdef CONFIG_CIFS_STATS2
4751*4882a593Smuzhiyun 			mid->when_received = jiffies;
4752*4882a593Smuzhiyun #endif
4753*4882a593Smuzhiyun 			mid->callback(mid);
4754*4882a593Smuzhiyun 		} else {
4755*4882a593Smuzhiyun 			spin_lock(&GlobalMid_Lock);
4756*4882a593Smuzhiyun 			if (dw->server->tcpStatus == CifsNeedReconnect) {
4757*4882a593Smuzhiyun 				mid->mid_state = MID_RETRY_NEEDED;
4758*4882a593Smuzhiyun 				spin_unlock(&GlobalMid_Lock);
4759*4882a593Smuzhiyun 				mid->callback(mid);
4760*4882a593Smuzhiyun 			} else {
4761*4882a593Smuzhiyun 				mid->mid_state = MID_REQUEST_SUBMITTED;
4762*4882a593Smuzhiyun 				mid->mid_flags &= ~(MID_DELETED);
4763*4882a593Smuzhiyun 				list_add_tail(&mid->qhead,
4764*4882a593Smuzhiyun 					&dw->server->pending_mid_q);
4765*4882a593Smuzhiyun 				spin_unlock(&GlobalMid_Lock);
4766*4882a593Smuzhiyun 			}
4767*4882a593Smuzhiyun 		}
4768*4882a593Smuzhiyun 		cifs_mid_q_entry_release(mid);
4769*4882a593Smuzhiyun 	}
4770*4882a593Smuzhiyun 
4771*4882a593Smuzhiyun free_pages:
4772*4882a593Smuzhiyun 	for (i = dw->npages-1; i >= 0; i--)
4773*4882a593Smuzhiyun 		put_page(dw->ppages[i]);
4774*4882a593Smuzhiyun 
4775*4882a593Smuzhiyun 	kfree(dw->ppages);
4776*4882a593Smuzhiyun 	cifs_small_buf_release(dw->buf);
4777*4882a593Smuzhiyun 	kfree(dw);
4778*4882a593Smuzhiyun }
4779*4882a593Smuzhiyun 
4780*4882a593Smuzhiyun 
4781*4882a593Smuzhiyun static int
receive_encrypted_read(struct TCP_Server_Info * server,struct mid_q_entry ** mid,int * num_mids)4782*4882a593Smuzhiyun receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4783*4882a593Smuzhiyun 		       int *num_mids)
4784*4882a593Smuzhiyun {
4785*4882a593Smuzhiyun 	char *buf = server->smallbuf;
4786*4882a593Smuzhiyun 	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4787*4882a593Smuzhiyun 	unsigned int npages;
4788*4882a593Smuzhiyun 	struct page **pages;
4789*4882a593Smuzhiyun 	unsigned int len;
4790*4882a593Smuzhiyun 	unsigned int buflen = server->pdu_size;
4791*4882a593Smuzhiyun 	int rc;
4792*4882a593Smuzhiyun 	int i = 0;
4793*4882a593Smuzhiyun 	struct smb2_decrypt_work *dw;
4794*4882a593Smuzhiyun 
4795*4882a593Smuzhiyun 	*num_mids = 1;
4796*4882a593Smuzhiyun 	len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4797*4882a593Smuzhiyun 		sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4798*4882a593Smuzhiyun 
4799*4882a593Smuzhiyun 	rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4800*4882a593Smuzhiyun 	if (rc < 0)
4801*4882a593Smuzhiyun 		return rc;
4802*4882a593Smuzhiyun 	server->total_read += rc;
4803*4882a593Smuzhiyun 
4804*4882a593Smuzhiyun 	len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4805*4882a593Smuzhiyun 		server->vals->read_rsp_size;
4806*4882a593Smuzhiyun 	npages = DIV_ROUND_UP(len, PAGE_SIZE);
4807*4882a593Smuzhiyun 
4808*4882a593Smuzhiyun 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4809*4882a593Smuzhiyun 	if (!pages) {
4810*4882a593Smuzhiyun 		rc = -ENOMEM;
4811*4882a593Smuzhiyun 		goto discard_data;
4812*4882a593Smuzhiyun 	}
4813*4882a593Smuzhiyun 
4814*4882a593Smuzhiyun 	for (; i < npages; i++) {
4815*4882a593Smuzhiyun 		pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4816*4882a593Smuzhiyun 		if (!pages[i]) {
4817*4882a593Smuzhiyun 			rc = -ENOMEM;
4818*4882a593Smuzhiyun 			goto discard_data;
4819*4882a593Smuzhiyun 		}
4820*4882a593Smuzhiyun 	}
4821*4882a593Smuzhiyun 
4822*4882a593Smuzhiyun 	/* read read data into pages */
4823*4882a593Smuzhiyun 	rc = read_data_into_pages(server, pages, npages, len);
4824*4882a593Smuzhiyun 	if (rc)
4825*4882a593Smuzhiyun 		goto free_pages;
4826*4882a593Smuzhiyun 
4827*4882a593Smuzhiyun 	rc = cifs_discard_remaining_data(server);
4828*4882a593Smuzhiyun 	if (rc)
4829*4882a593Smuzhiyun 		goto free_pages;
4830*4882a593Smuzhiyun 
4831*4882a593Smuzhiyun 	/*
4832*4882a593Smuzhiyun 	 * For large reads, offload to different thread for better performance,
4833*4882a593Smuzhiyun 	 * use more cores decrypting which can be expensive
4834*4882a593Smuzhiyun 	 */
4835*4882a593Smuzhiyun 
4836*4882a593Smuzhiyun 	if ((server->min_offload) && (server->in_flight > 1) &&
4837*4882a593Smuzhiyun 	    (server->pdu_size >= server->min_offload)) {
4838*4882a593Smuzhiyun 		dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4839*4882a593Smuzhiyun 		if (dw == NULL)
4840*4882a593Smuzhiyun 			goto non_offloaded_decrypt;
4841*4882a593Smuzhiyun 
4842*4882a593Smuzhiyun 		dw->buf = server->smallbuf;
4843*4882a593Smuzhiyun 		server->smallbuf = (char *)cifs_small_buf_get();
4844*4882a593Smuzhiyun 
4845*4882a593Smuzhiyun 		INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4846*4882a593Smuzhiyun 
4847*4882a593Smuzhiyun 		dw->npages = npages;
4848*4882a593Smuzhiyun 		dw->server = server;
4849*4882a593Smuzhiyun 		dw->ppages = pages;
4850*4882a593Smuzhiyun 		dw->len = len;
4851*4882a593Smuzhiyun 		queue_work(decrypt_wq, &dw->decrypt);
4852*4882a593Smuzhiyun 		*num_mids = 0; /* worker thread takes care of finding mid */
4853*4882a593Smuzhiyun 		return -1;
4854*4882a593Smuzhiyun 	}
4855*4882a593Smuzhiyun 
4856*4882a593Smuzhiyun non_offloaded_decrypt:
4857*4882a593Smuzhiyun 	rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4858*4882a593Smuzhiyun 			      pages, npages, len, false);
4859*4882a593Smuzhiyun 	if (rc)
4860*4882a593Smuzhiyun 		goto free_pages;
4861*4882a593Smuzhiyun 
4862*4882a593Smuzhiyun 	*mid = smb2_find_mid(server, buf);
4863*4882a593Smuzhiyun 	if (*mid == NULL)
4864*4882a593Smuzhiyun 		cifs_dbg(FYI, "mid not found\n");
4865*4882a593Smuzhiyun 	else {
4866*4882a593Smuzhiyun 		cifs_dbg(FYI, "mid found\n");
4867*4882a593Smuzhiyun 		(*mid)->decrypted = true;
4868*4882a593Smuzhiyun 		rc = handle_read_data(server, *mid, buf,
4869*4882a593Smuzhiyun 				      server->vals->read_rsp_size,
4870*4882a593Smuzhiyun 				      pages, npages, len, false);
4871*4882a593Smuzhiyun 	}
4872*4882a593Smuzhiyun 
4873*4882a593Smuzhiyun free_pages:
4874*4882a593Smuzhiyun 	for (i = i - 1; i >= 0; i--)
4875*4882a593Smuzhiyun 		put_page(pages[i]);
4876*4882a593Smuzhiyun 	kfree(pages);
4877*4882a593Smuzhiyun 	return rc;
4878*4882a593Smuzhiyun discard_data:
4879*4882a593Smuzhiyun 	cifs_discard_remaining_data(server);
4880*4882a593Smuzhiyun 	goto free_pages;
4881*4882a593Smuzhiyun }
4882*4882a593Smuzhiyun 
4883*4882a593Smuzhiyun static int
receive_encrypted_standard(struct TCP_Server_Info * server,struct mid_q_entry ** mids,char ** bufs,int * num_mids)4884*4882a593Smuzhiyun receive_encrypted_standard(struct TCP_Server_Info *server,
4885*4882a593Smuzhiyun 			   struct mid_q_entry **mids, char **bufs,
4886*4882a593Smuzhiyun 			   int *num_mids)
4887*4882a593Smuzhiyun {
4888*4882a593Smuzhiyun 	int ret, length;
4889*4882a593Smuzhiyun 	char *buf = server->smallbuf;
4890*4882a593Smuzhiyun 	struct smb2_sync_hdr *shdr;
4891*4882a593Smuzhiyun 	unsigned int pdu_length = server->pdu_size;
4892*4882a593Smuzhiyun 	unsigned int buf_size;
4893*4882a593Smuzhiyun 	struct mid_q_entry *mid_entry;
4894*4882a593Smuzhiyun 	int next_is_large;
4895*4882a593Smuzhiyun 	char *next_buffer = NULL;
4896*4882a593Smuzhiyun 
4897*4882a593Smuzhiyun 	*num_mids = 0;
4898*4882a593Smuzhiyun 
4899*4882a593Smuzhiyun 	/* switch to large buffer if too big for a small one */
4900*4882a593Smuzhiyun 	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4901*4882a593Smuzhiyun 		server->large_buf = true;
4902*4882a593Smuzhiyun 		memcpy(server->bigbuf, buf, server->total_read);
4903*4882a593Smuzhiyun 		buf = server->bigbuf;
4904*4882a593Smuzhiyun 	}
4905*4882a593Smuzhiyun 
4906*4882a593Smuzhiyun 	/* now read the rest */
4907*4882a593Smuzhiyun 	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4908*4882a593Smuzhiyun 				pdu_length - HEADER_SIZE(server) + 1);
4909*4882a593Smuzhiyun 	if (length < 0)
4910*4882a593Smuzhiyun 		return length;
4911*4882a593Smuzhiyun 	server->total_read += length;
4912*4882a593Smuzhiyun 
4913*4882a593Smuzhiyun 	buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4914*4882a593Smuzhiyun 	length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
4915*4882a593Smuzhiyun 	if (length)
4916*4882a593Smuzhiyun 		return length;
4917*4882a593Smuzhiyun 
4918*4882a593Smuzhiyun 	next_is_large = server->large_buf;
4919*4882a593Smuzhiyun one_more:
4920*4882a593Smuzhiyun 	shdr = (struct smb2_sync_hdr *)buf;
4921*4882a593Smuzhiyun 	if (shdr->NextCommand) {
4922*4882a593Smuzhiyun 		if (next_is_large)
4923*4882a593Smuzhiyun 			next_buffer = (char *)cifs_buf_get();
4924*4882a593Smuzhiyun 		else
4925*4882a593Smuzhiyun 			next_buffer = (char *)cifs_small_buf_get();
4926*4882a593Smuzhiyun 		memcpy(next_buffer,
4927*4882a593Smuzhiyun 		       buf + le32_to_cpu(shdr->NextCommand),
4928*4882a593Smuzhiyun 		       pdu_length - le32_to_cpu(shdr->NextCommand));
4929*4882a593Smuzhiyun 	}
4930*4882a593Smuzhiyun 
4931*4882a593Smuzhiyun 	mid_entry = smb2_find_mid(server, buf);
4932*4882a593Smuzhiyun 	if (mid_entry == NULL)
4933*4882a593Smuzhiyun 		cifs_dbg(FYI, "mid not found\n");
4934*4882a593Smuzhiyun 	else {
4935*4882a593Smuzhiyun 		cifs_dbg(FYI, "mid found\n");
4936*4882a593Smuzhiyun 		mid_entry->decrypted = true;
4937*4882a593Smuzhiyun 		mid_entry->resp_buf_size = server->pdu_size;
4938*4882a593Smuzhiyun 	}
4939*4882a593Smuzhiyun 
4940*4882a593Smuzhiyun 	if (*num_mids >= MAX_COMPOUND) {
4941*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "too many PDUs in compound\n");
4942*4882a593Smuzhiyun 		return -1;
4943*4882a593Smuzhiyun 	}
4944*4882a593Smuzhiyun 	bufs[*num_mids] = buf;
4945*4882a593Smuzhiyun 	mids[(*num_mids)++] = mid_entry;
4946*4882a593Smuzhiyun 
4947*4882a593Smuzhiyun 	if (mid_entry && mid_entry->handle)
4948*4882a593Smuzhiyun 		ret = mid_entry->handle(server, mid_entry);
4949*4882a593Smuzhiyun 	else
4950*4882a593Smuzhiyun 		ret = cifs_handle_standard(server, mid_entry);
4951*4882a593Smuzhiyun 
4952*4882a593Smuzhiyun 	if (ret == 0 && shdr->NextCommand) {
4953*4882a593Smuzhiyun 		pdu_length -= le32_to_cpu(shdr->NextCommand);
4954*4882a593Smuzhiyun 		server->large_buf = next_is_large;
4955*4882a593Smuzhiyun 		if (next_is_large)
4956*4882a593Smuzhiyun 			server->bigbuf = buf = next_buffer;
4957*4882a593Smuzhiyun 		else
4958*4882a593Smuzhiyun 			server->smallbuf = buf = next_buffer;
4959*4882a593Smuzhiyun 		goto one_more;
4960*4882a593Smuzhiyun 	} else if (ret != 0) {
4961*4882a593Smuzhiyun 		/*
4962*4882a593Smuzhiyun 		 * ret != 0 here means that we didn't get to handle_mid() thus
4963*4882a593Smuzhiyun 		 * server->smallbuf and server->bigbuf are still valid. We need
4964*4882a593Smuzhiyun 		 * to free next_buffer because it is not going to be used
4965*4882a593Smuzhiyun 		 * anywhere.
4966*4882a593Smuzhiyun 		 */
4967*4882a593Smuzhiyun 		if (next_is_large)
4968*4882a593Smuzhiyun 			free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4969*4882a593Smuzhiyun 		else
4970*4882a593Smuzhiyun 			free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4971*4882a593Smuzhiyun 	}
4972*4882a593Smuzhiyun 
4973*4882a593Smuzhiyun 	return ret;
4974*4882a593Smuzhiyun }
4975*4882a593Smuzhiyun 
4976*4882a593Smuzhiyun static int
smb3_receive_transform(struct TCP_Server_Info * server,struct mid_q_entry ** mids,char ** bufs,int * num_mids)4977*4882a593Smuzhiyun smb3_receive_transform(struct TCP_Server_Info *server,
4978*4882a593Smuzhiyun 		       struct mid_q_entry **mids, char **bufs, int *num_mids)
4979*4882a593Smuzhiyun {
4980*4882a593Smuzhiyun 	char *buf = server->smallbuf;
4981*4882a593Smuzhiyun 	unsigned int pdu_length = server->pdu_size;
4982*4882a593Smuzhiyun 	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4983*4882a593Smuzhiyun 	unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4984*4882a593Smuzhiyun 
4985*4882a593Smuzhiyun 	if (pdu_length < sizeof(struct smb2_transform_hdr) +
4986*4882a593Smuzhiyun 						sizeof(struct smb2_sync_hdr)) {
4987*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4988*4882a593Smuzhiyun 			 pdu_length);
4989*4882a593Smuzhiyun 		cifs_reconnect(server);
4990*4882a593Smuzhiyun 		return -ECONNABORTED;
4991*4882a593Smuzhiyun 	}
4992*4882a593Smuzhiyun 
4993*4882a593Smuzhiyun 	if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4994*4882a593Smuzhiyun 		cifs_server_dbg(VFS, "Transform message is broken\n");
4995*4882a593Smuzhiyun 		cifs_reconnect(server);
4996*4882a593Smuzhiyun 		return -ECONNABORTED;
4997*4882a593Smuzhiyun 	}
4998*4882a593Smuzhiyun 
4999*4882a593Smuzhiyun 	/* TODO: add support for compounds containing READ. */
5000*4882a593Smuzhiyun 	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
5001*4882a593Smuzhiyun 		return receive_encrypted_read(server, &mids[0], num_mids);
5002*4882a593Smuzhiyun 	}
5003*4882a593Smuzhiyun 
5004*4882a593Smuzhiyun 	return receive_encrypted_standard(server, mids, bufs, num_mids);
5005*4882a593Smuzhiyun }
5006*4882a593Smuzhiyun 
5007*4882a593Smuzhiyun int
smb3_handle_read_data(struct TCP_Server_Info * server,struct mid_q_entry * mid)5008*4882a593Smuzhiyun smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
5009*4882a593Smuzhiyun {
5010*4882a593Smuzhiyun 	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
5011*4882a593Smuzhiyun 
5012*4882a593Smuzhiyun 	return handle_read_data(server, mid, buf, server->pdu_size,
5013*4882a593Smuzhiyun 				NULL, 0, 0, false);
5014*4882a593Smuzhiyun }
5015*4882a593Smuzhiyun 
5016*4882a593Smuzhiyun static int
smb2_next_header(char * buf)5017*4882a593Smuzhiyun smb2_next_header(char *buf)
5018*4882a593Smuzhiyun {
5019*4882a593Smuzhiyun 	struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
5020*4882a593Smuzhiyun 	struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5021*4882a593Smuzhiyun 
5022*4882a593Smuzhiyun 	if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
5023*4882a593Smuzhiyun 		return sizeof(struct smb2_transform_hdr) +
5024*4882a593Smuzhiyun 		  le32_to_cpu(t_hdr->OriginalMessageSize);
5025*4882a593Smuzhiyun 
5026*4882a593Smuzhiyun 	return le32_to_cpu(hdr->NextCommand);
5027*4882a593Smuzhiyun }
5028*4882a593Smuzhiyun 
5029*4882a593Smuzhiyun static int
smb2_make_node(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,char * full_path,umode_t mode,dev_t dev)5030*4882a593Smuzhiyun smb2_make_node(unsigned int xid, struct inode *inode,
5031*4882a593Smuzhiyun 	       struct dentry *dentry, struct cifs_tcon *tcon,
5032*4882a593Smuzhiyun 	       char *full_path, umode_t mode, dev_t dev)
5033*4882a593Smuzhiyun {
5034*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5035*4882a593Smuzhiyun 	int rc = -EPERM;
5036*4882a593Smuzhiyun 	FILE_ALL_INFO *buf = NULL;
5037*4882a593Smuzhiyun 	struct cifs_io_parms io_parms = {0};
5038*4882a593Smuzhiyun 	__u32 oplock = 0;
5039*4882a593Smuzhiyun 	struct cifs_fid fid;
5040*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
5041*4882a593Smuzhiyun 	unsigned int bytes_written;
5042*4882a593Smuzhiyun 	struct win_dev *pdev;
5043*4882a593Smuzhiyun 	struct kvec iov[2];
5044*4882a593Smuzhiyun 
5045*4882a593Smuzhiyun 	/*
5046*4882a593Smuzhiyun 	 * Check if mounted with mount parm 'sfu' mount parm.
5047*4882a593Smuzhiyun 	 * SFU emulation should work with all servers, but only
5048*4882a593Smuzhiyun 	 * supports block and char device (no socket & fifo),
5049*4882a593Smuzhiyun 	 * and was used by default in earlier versions of Windows
5050*4882a593Smuzhiyun 	 */
5051*4882a593Smuzhiyun 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
5052*4882a593Smuzhiyun 		goto out;
5053*4882a593Smuzhiyun 
5054*4882a593Smuzhiyun 	/*
5055*4882a593Smuzhiyun 	 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5056*4882a593Smuzhiyun 	 * their current NFS server) uses this approach to expose special files
5057*4882a593Smuzhiyun 	 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5058*4882a593Smuzhiyun 	 */
5059*4882a593Smuzhiyun 
5060*4882a593Smuzhiyun 	if (!S_ISCHR(mode) && !S_ISBLK(mode))
5061*4882a593Smuzhiyun 		goto out;
5062*4882a593Smuzhiyun 
5063*4882a593Smuzhiyun 	cifs_dbg(FYI, "sfu compat create special file\n");
5064*4882a593Smuzhiyun 
5065*4882a593Smuzhiyun 	buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5066*4882a593Smuzhiyun 	if (buf == NULL) {
5067*4882a593Smuzhiyun 		rc = -ENOMEM;
5068*4882a593Smuzhiyun 		goto out;
5069*4882a593Smuzhiyun 	}
5070*4882a593Smuzhiyun 
5071*4882a593Smuzhiyun 	oparms.tcon = tcon;
5072*4882a593Smuzhiyun 	oparms.cifs_sb = cifs_sb;
5073*4882a593Smuzhiyun 	oparms.desired_access = GENERIC_WRITE;
5074*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5075*4882a593Smuzhiyun 						    CREATE_OPTION_SPECIAL);
5076*4882a593Smuzhiyun 	oparms.disposition = FILE_CREATE;
5077*4882a593Smuzhiyun 	oparms.path = full_path;
5078*4882a593Smuzhiyun 	oparms.fid = &fid;
5079*4882a593Smuzhiyun 	oparms.reconnect = false;
5080*4882a593Smuzhiyun 
5081*4882a593Smuzhiyun 	if (tcon->ses->server->oplocks)
5082*4882a593Smuzhiyun 		oplock = REQ_OPLOCK;
5083*4882a593Smuzhiyun 	else
5084*4882a593Smuzhiyun 		oplock = 0;
5085*4882a593Smuzhiyun 	rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5086*4882a593Smuzhiyun 	if (rc)
5087*4882a593Smuzhiyun 		goto out;
5088*4882a593Smuzhiyun 
5089*4882a593Smuzhiyun 	/*
5090*4882a593Smuzhiyun 	 * BB Do not bother to decode buf since no local inode yet to put
5091*4882a593Smuzhiyun 	 * timestamps in, but we can reuse it safely.
5092*4882a593Smuzhiyun 	 */
5093*4882a593Smuzhiyun 
5094*4882a593Smuzhiyun 	pdev = (struct win_dev *)buf;
5095*4882a593Smuzhiyun 	io_parms.pid = current->tgid;
5096*4882a593Smuzhiyun 	io_parms.tcon = tcon;
5097*4882a593Smuzhiyun 	io_parms.offset = 0;
5098*4882a593Smuzhiyun 	io_parms.length = sizeof(struct win_dev);
5099*4882a593Smuzhiyun 	iov[1].iov_base = buf;
5100*4882a593Smuzhiyun 	iov[1].iov_len = sizeof(struct win_dev);
5101*4882a593Smuzhiyun 	if (S_ISCHR(mode)) {
5102*4882a593Smuzhiyun 		memcpy(pdev->type, "IntxCHR", 8);
5103*4882a593Smuzhiyun 		pdev->major = cpu_to_le64(MAJOR(dev));
5104*4882a593Smuzhiyun 		pdev->minor = cpu_to_le64(MINOR(dev));
5105*4882a593Smuzhiyun 		rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5106*4882a593Smuzhiyun 							&bytes_written, iov, 1);
5107*4882a593Smuzhiyun 	} else if (S_ISBLK(mode)) {
5108*4882a593Smuzhiyun 		memcpy(pdev->type, "IntxBLK", 8);
5109*4882a593Smuzhiyun 		pdev->major = cpu_to_le64(MAJOR(dev));
5110*4882a593Smuzhiyun 		pdev->minor = cpu_to_le64(MINOR(dev));
5111*4882a593Smuzhiyun 		rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5112*4882a593Smuzhiyun 							&bytes_written, iov, 1);
5113*4882a593Smuzhiyun 	}
5114*4882a593Smuzhiyun 	tcon->ses->server->ops->close(xid, tcon, &fid);
5115*4882a593Smuzhiyun 	d_drop(dentry);
5116*4882a593Smuzhiyun 
5117*4882a593Smuzhiyun 	/* FIXME: add code here to set EAs */
5118*4882a593Smuzhiyun out:
5119*4882a593Smuzhiyun 	kfree(buf);
5120*4882a593Smuzhiyun 	return rc;
5121*4882a593Smuzhiyun }
5122*4882a593Smuzhiyun 
5123*4882a593Smuzhiyun #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5124*4882a593Smuzhiyun struct smb_version_operations smb20_operations = {
5125*4882a593Smuzhiyun 	.compare_fids = smb2_compare_fids,
5126*4882a593Smuzhiyun 	.setup_request = smb2_setup_request,
5127*4882a593Smuzhiyun 	.setup_async_request = smb2_setup_async_request,
5128*4882a593Smuzhiyun 	.check_receive = smb2_check_receive,
5129*4882a593Smuzhiyun 	.add_credits = smb2_add_credits,
5130*4882a593Smuzhiyun 	.set_credits = smb2_set_credits,
5131*4882a593Smuzhiyun 	.get_credits_field = smb2_get_credits_field,
5132*4882a593Smuzhiyun 	.get_credits = smb2_get_credits,
5133*4882a593Smuzhiyun 	.wait_mtu_credits = cifs_wait_mtu_credits,
5134*4882a593Smuzhiyun 	.get_next_mid = smb2_get_next_mid,
5135*4882a593Smuzhiyun 	.revert_current_mid = smb2_revert_current_mid,
5136*4882a593Smuzhiyun 	.read_data_offset = smb2_read_data_offset,
5137*4882a593Smuzhiyun 	.read_data_length = smb2_read_data_length,
5138*4882a593Smuzhiyun 	.map_error = map_smb2_to_linux_error,
5139*4882a593Smuzhiyun 	.find_mid = smb2_find_mid,
5140*4882a593Smuzhiyun 	.check_message = smb2_check_message,
5141*4882a593Smuzhiyun 	.dump_detail = smb2_dump_detail,
5142*4882a593Smuzhiyun 	.clear_stats = smb2_clear_stats,
5143*4882a593Smuzhiyun 	.print_stats = smb2_print_stats,
5144*4882a593Smuzhiyun 	.is_oplock_break = smb2_is_valid_oplock_break,
5145*4882a593Smuzhiyun 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5146*4882a593Smuzhiyun 	.downgrade_oplock = smb2_downgrade_oplock,
5147*4882a593Smuzhiyun 	.need_neg = smb2_need_neg,
5148*4882a593Smuzhiyun 	.negotiate = smb2_negotiate,
5149*4882a593Smuzhiyun 	.negotiate_wsize = smb2_negotiate_wsize,
5150*4882a593Smuzhiyun 	.negotiate_rsize = smb2_negotiate_rsize,
5151*4882a593Smuzhiyun 	.sess_setup = SMB2_sess_setup,
5152*4882a593Smuzhiyun 	.logoff = SMB2_logoff,
5153*4882a593Smuzhiyun 	.tree_connect = SMB2_tcon,
5154*4882a593Smuzhiyun 	.tree_disconnect = SMB2_tdis,
5155*4882a593Smuzhiyun 	.qfs_tcon = smb2_qfs_tcon,
5156*4882a593Smuzhiyun 	.is_path_accessible = smb2_is_path_accessible,
5157*4882a593Smuzhiyun 	.can_echo = smb2_can_echo,
5158*4882a593Smuzhiyun 	.echo = SMB2_echo,
5159*4882a593Smuzhiyun 	.query_path_info = smb2_query_path_info,
5160*4882a593Smuzhiyun 	.get_srv_inum = smb2_get_srv_inum,
5161*4882a593Smuzhiyun 	.query_file_info = smb2_query_file_info,
5162*4882a593Smuzhiyun 	.set_path_size = smb2_set_path_size,
5163*4882a593Smuzhiyun 	.set_file_size = smb2_set_file_size,
5164*4882a593Smuzhiyun 	.set_file_info = smb2_set_file_info,
5165*4882a593Smuzhiyun 	.set_compression = smb2_set_compression,
5166*4882a593Smuzhiyun 	.mkdir = smb2_mkdir,
5167*4882a593Smuzhiyun 	.mkdir_setinfo = smb2_mkdir_setinfo,
5168*4882a593Smuzhiyun 	.rmdir = smb2_rmdir,
5169*4882a593Smuzhiyun 	.unlink = smb2_unlink,
5170*4882a593Smuzhiyun 	.rename = smb2_rename_path,
5171*4882a593Smuzhiyun 	.create_hardlink = smb2_create_hardlink,
5172*4882a593Smuzhiyun 	.query_symlink = smb2_query_symlink,
5173*4882a593Smuzhiyun 	.query_mf_symlink = smb3_query_mf_symlink,
5174*4882a593Smuzhiyun 	.create_mf_symlink = smb3_create_mf_symlink,
5175*4882a593Smuzhiyun 	.open = smb2_open_file,
5176*4882a593Smuzhiyun 	.set_fid = smb2_set_fid,
5177*4882a593Smuzhiyun 	.close = smb2_close_file,
5178*4882a593Smuzhiyun 	.flush = smb2_flush_file,
5179*4882a593Smuzhiyun 	.async_readv = smb2_async_readv,
5180*4882a593Smuzhiyun 	.async_writev = smb2_async_writev,
5181*4882a593Smuzhiyun 	.sync_read = smb2_sync_read,
5182*4882a593Smuzhiyun 	.sync_write = smb2_sync_write,
5183*4882a593Smuzhiyun 	.query_dir_first = smb2_query_dir_first,
5184*4882a593Smuzhiyun 	.query_dir_next = smb2_query_dir_next,
5185*4882a593Smuzhiyun 	.close_dir = smb2_close_dir,
5186*4882a593Smuzhiyun 	.calc_smb_size = smb2_calc_size,
5187*4882a593Smuzhiyun 	.is_status_pending = smb2_is_status_pending,
5188*4882a593Smuzhiyun 	.is_session_expired = smb2_is_session_expired,
5189*4882a593Smuzhiyun 	.oplock_response = smb2_oplock_response,
5190*4882a593Smuzhiyun 	.queryfs = smb2_queryfs,
5191*4882a593Smuzhiyun 	.mand_lock = smb2_mand_lock,
5192*4882a593Smuzhiyun 	.mand_unlock_range = smb2_unlock_range,
5193*4882a593Smuzhiyun 	.push_mand_locks = smb2_push_mandatory_locks,
5194*4882a593Smuzhiyun 	.get_lease_key = smb2_get_lease_key,
5195*4882a593Smuzhiyun 	.set_lease_key = smb2_set_lease_key,
5196*4882a593Smuzhiyun 	.new_lease_key = smb2_new_lease_key,
5197*4882a593Smuzhiyun 	.calc_signature = smb2_calc_signature,
5198*4882a593Smuzhiyun 	.is_read_op = smb2_is_read_op,
5199*4882a593Smuzhiyun 	.set_oplock_level = smb2_set_oplock_level,
5200*4882a593Smuzhiyun 	.create_lease_buf = smb2_create_lease_buf,
5201*4882a593Smuzhiyun 	.parse_lease_buf = smb2_parse_lease_buf,
5202*4882a593Smuzhiyun 	.copychunk_range = smb2_copychunk_range,
5203*4882a593Smuzhiyun 	.wp_retry_size = smb2_wp_retry_size,
5204*4882a593Smuzhiyun 	.dir_needs_close = smb2_dir_needs_close,
5205*4882a593Smuzhiyun 	.get_dfs_refer = smb2_get_dfs_refer,
5206*4882a593Smuzhiyun 	.select_sectype = smb2_select_sectype,
5207*4882a593Smuzhiyun #ifdef CONFIG_CIFS_XATTR
5208*4882a593Smuzhiyun 	.query_all_EAs = smb2_query_eas,
5209*4882a593Smuzhiyun 	.set_EA = smb2_set_ea,
5210*4882a593Smuzhiyun #endif /* CIFS_XATTR */
5211*4882a593Smuzhiyun 	.get_acl = get_smb2_acl,
5212*4882a593Smuzhiyun 	.get_acl_by_fid = get_smb2_acl_by_fid,
5213*4882a593Smuzhiyun 	.set_acl = set_smb2_acl,
5214*4882a593Smuzhiyun 	.next_header = smb2_next_header,
5215*4882a593Smuzhiyun 	.ioctl_query_info = smb2_ioctl_query_info,
5216*4882a593Smuzhiyun 	.make_node = smb2_make_node,
5217*4882a593Smuzhiyun 	.fiemap = smb3_fiemap,
5218*4882a593Smuzhiyun 	.llseek = smb3_llseek,
5219*4882a593Smuzhiyun 	.is_status_io_timeout = smb2_is_status_io_timeout,
5220*4882a593Smuzhiyun };
5221*4882a593Smuzhiyun #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5222*4882a593Smuzhiyun 
5223*4882a593Smuzhiyun struct smb_version_operations smb21_operations = {
5224*4882a593Smuzhiyun 	.compare_fids = smb2_compare_fids,
5225*4882a593Smuzhiyun 	.setup_request = smb2_setup_request,
5226*4882a593Smuzhiyun 	.setup_async_request = smb2_setup_async_request,
5227*4882a593Smuzhiyun 	.check_receive = smb2_check_receive,
5228*4882a593Smuzhiyun 	.add_credits = smb2_add_credits,
5229*4882a593Smuzhiyun 	.set_credits = smb2_set_credits,
5230*4882a593Smuzhiyun 	.get_credits_field = smb2_get_credits_field,
5231*4882a593Smuzhiyun 	.get_credits = smb2_get_credits,
5232*4882a593Smuzhiyun 	.wait_mtu_credits = smb2_wait_mtu_credits,
5233*4882a593Smuzhiyun 	.adjust_credits = smb2_adjust_credits,
5234*4882a593Smuzhiyun 	.get_next_mid = smb2_get_next_mid,
5235*4882a593Smuzhiyun 	.revert_current_mid = smb2_revert_current_mid,
5236*4882a593Smuzhiyun 	.read_data_offset = smb2_read_data_offset,
5237*4882a593Smuzhiyun 	.read_data_length = smb2_read_data_length,
5238*4882a593Smuzhiyun 	.map_error = map_smb2_to_linux_error,
5239*4882a593Smuzhiyun 	.find_mid = smb2_find_mid,
5240*4882a593Smuzhiyun 	.check_message = smb2_check_message,
5241*4882a593Smuzhiyun 	.dump_detail = smb2_dump_detail,
5242*4882a593Smuzhiyun 	.clear_stats = smb2_clear_stats,
5243*4882a593Smuzhiyun 	.print_stats = smb2_print_stats,
5244*4882a593Smuzhiyun 	.is_oplock_break = smb2_is_valid_oplock_break,
5245*4882a593Smuzhiyun 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5246*4882a593Smuzhiyun 	.downgrade_oplock = smb2_downgrade_oplock,
5247*4882a593Smuzhiyun 	.need_neg = smb2_need_neg,
5248*4882a593Smuzhiyun 	.negotiate = smb2_negotiate,
5249*4882a593Smuzhiyun 	.negotiate_wsize = smb2_negotiate_wsize,
5250*4882a593Smuzhiyun 	.negotiate_rsize = smb2_negotiate_rsize,
5251*4882a593Smuzhiyun 	.sess_setup = SMB2_sess_setup,
5252*4882a593Smuzhiyun 	.logoff = SMB2_logoff,
5253*4882a593Smuzhiyun 	.tree_connect = SMB2_tcon,
5254*4882a593Smuzhiyun 	.tree_disconnect = SMB2_tdis,
5255*4882a593Smuzhiyun 	.qfs_tcon = smb2_qfs_tcon,
5256*4882a593Smuzhiyun 	.is_path_accessible = smb2_is_path_accessible,
5257*4882a593Smuzhiyun 	.can_echo = smb2_can_echo,
5258*4882a593Smuzhiyun 	.echo = SMB2_echo,
5259*4882a593Smuzhiyun 	.query_path_info = smb2_query_path_info,
5260*4882a593Smuzhiyun 	.get_srv_inum = smb2_get_srv_inum,
5261*4882a593Smuzhiyun 	.query_file_info = smb2_query_file_info,
5262*4882a593Smuzhiyun 	.set_path_size = smb2_set_path_size,
5263*4882a593Smuzhiyun 	.set_file_size = smb2_set_file_size,
5264*4882a593Smuzhiyun 	.set_file_info = smb2_set_file_info,
5265*4882a593Smuzhiyun 	.set_compression = smb2_set_compression,
5266*4882a593Smuzhiyun 	.mkdir = smb2_mkdir,
5267*4882a593Smuzhiyun 	.mkdir_setinfo = smb2_mkdir_setinfo,
5268*4882a593Smuzhiyun 	.rmdir = smb2_rmdir,
5269*4882a593Smuzhiyun 	.unlink = smb2_unlink,
5270*4882a593Smuzhiyun 	.rename = smb2_rename_path,
5271*4882a593Smuzhiyun 	.create_hardlink = smb2_create_hardlink,
5272*4882a593Smuzhiyun 	.query_symlink = smb2_query_symlink,
5273*4882a593Smuzhiyun 	.query_mf_symlink = smb3_query_mf_symlink,
5274*4882a593Smuzhiyun 	.create_mf_symlink = smb3_create_mf_symlink,
5275*4882a593Smuzhiyun 	.open = smb2_open_file,
5276*4882a593Smuzhiyun 	.set_fid = smb2_set_fid,
5277*4882a593Smuzhiyun 	.close = smb2_close_file,
5278*4882a593Smuzhiyun 	.flush = smb2_flush_file,
5279*4882a593Smuzhiyun 	.async_readv = smb2_async_readv,
5280*4882a593Smuzhiyun 	.async_writev = smb2_async_writev,
5281*4882a593Smuzhiyun 	.sync_read = smb2_sync_read,
5282*4882a593Smuzhiyun 	.sync_write = smb2_sync_write,
5283*4882a593Smuzhiyun 	.query_dir_first = smb2_query_dir_first,
5284*4882a593Smuzhiyun 	.query_dir_next = smb2_query_dir_next,
5285*4882a593Smuzhiyun 	.close_dir = smb2_close_dir,
5286*4882a593Smuzhiyun 	.calc_smb_size = smb2_calc_size,
5287*4882a593Smuzhiyun 	.is_status_pending = smb2_is_status_pending,
5288*4882a593Smuzhiyun 	.is_session_expired = smb2_is_session_expired,
5289*4882a593Smuzhiyun 	.oplock_response = smb2_oplock_response,
5290*4882a593Smuzhiyun 	.queryfs = smb2_queryfs,
5291*4882a593Smuzhiyun 	.mand_lock = smb2_mand_lock,
5292*4882a593Smuzhiyun 	.mand_unlock_range = smb2_unlock_range,
5293*4882a593Smuzhiyun 	.push_mand_locks = smb2_push_mandatory_locks,
5294*4882a593Smuzhiyun 	.get_lease_key = smb2_get_lease_key,
5295*4882a593Smuzhiyun 	.set_lease_key = smb2_set_lease_key,
5296*4882a593Smuzhiyun 	.new_lease_key = smb2_new_lease_key,
5297*4882a593Smuzhiyun 	.calc_signature = smb2_calc_signature,
5298*4882a593Smuzhiyun 	.is_read_op = smb21_is_read_op,
5299*4882a593Smuzhiyun 	.set_oplock_level = smb21_set_oplock_level,
5300*4882a593Smuzhiyun 	.create_lease_buf = smb2_create_lease_buf,
5301*4882a593Smuzhiyun 	.parse_lease_buf = smb2_parse_lease_buf,
5302*4882a593Smuzhiyun 	.copychunk_range = smb2_copychunk_range,
5303*4882a593Smuzhiyun 	.wp_retry_size = smb2_wp_retry_size,
5304*4882a593Smuzhiyun 	.dir_needs_close = smb2_dir_needs_close,
5305*4882a593Smuzhiyun 	.enum_snapshots = smb3_enum_snapshots,
5306*4882a593Smuzhiyun 	.notify = smb3_notify,
5307*4882a593Smuzhiyun 	.get_dfs_refer = smb2_get_dfs_refer,
5308*4882a593Smuzhiyun 	.select_sectype = smb2_select_sectype,
5309*4882a593Smuzhiyun #ifdef CONFIG_CIFS_XATTR
5310*4882a593Smuzhiyun 	.query_all_EAs = smb2_query_eas,
5311*4882a593Smuzhiyun 	.set_EA = smb2_set_ea,
5312*4882a593Smuzhiyun #endif /* CIFS_XATTR */
5313*4882a593Smuzhiyun 	.get_acl = get_smb2_acl,
5314*4882a593Smuzhiyun 	.get_acl_by_fid = get_smb2_acl_by_fid,
5315*4882a593Smuzhiyun 	.set_acl = set_smb2_acl,
5316*4882a593Smuzhiyun 	.next_header = smb2_next_header,
5317*4882a593Smuzhiyun 	.ioctl_query_info = smb2_ioctl_query_info,
5318*4882a593Smuzhiyun 	.make_node = smb2_make_node,
5319*4882a593Smuzhiyun 	.fiemap = smb3_fiemap,
5320*4882a593Smuzhiyun 	.llseek = smb3_llseek,
5321*4882a593Smuzhiyun 	.is_status_io_timeout = smb2_is_status_io_timeout,
5322*4882a593Smuzhiyun };
5323*4882a593Smuzhiyun 
5324*4882a593Smuzhiyun struct smb_version_operations smb30_operations = {
5325*4882a593Smuzhiyun 	.compare_fids = smb2_compare_fids,
5326*4882a593Smuzhiyun 	.setup_request = smb2_setup_request,
5327*4882a593Smuzhiyun 	.setup_async_request = smb2_setup_async_request,
5328*4882a593Smuzhiyun 	.check_receive = smb2_check_receive,
5329*4882a593Smuzhiyun 	.add_credits = smb2_add_credits,
5330*4882a593Smuzhiyun 	.set_credits = smb2_set_credits,
5331*4882a593Smuzhiyun 	.get_credits_field = smb2_get_credits_field,
5332*4882a593Smuzhiyun 	.get_credits = smb2_get_credits,
5333*4882a593Smuzhiyun 	.wait_mtu_credits = smb2_wait_mtu_credits,
5334*4882a593Smuzhiyun 	.adjust_credits = smb2_adjust_credits,
5335*4882a593Smuzhiyun 	.get_next_mid = smb2_get_next_mid,
5336*4882a593Smuzhiyun 	.revert_current_mid = smb2_revert_current_mid,
5337*4882a593Smuzhiyun 	.read_data_offset = smb2_read_data_offset,
5338*4882a593Smuzhiyun 	.read_data_length = smb2_read_data_length,
5339*4882a593Smuzhiyun 	.map_error = map_smb2_to_linux_error,
5340*4882a593Smuzhiyun 	.find_mid = smb2_find_mid,
5341*4882a593Smuzhiyun 	.check_message = smb2_check_message,
5342*4882a593Smuzhiyun 	.dump_detail = smb2_dump_detail,
5343*4882a593Smuzhiyun 	.clear_stats = smb2_clear_stats,
5344*4882a593Smuzhiyun 	.print_stats = smb2_print_stats,
5345*4882a593Smuzhiyun 	.dump_share_caps = smb2_dump_share_caps,
5346*4882a593Smuzhiyun 	.is_oplock_break = smb2_is_valid_oplock_break,
5347*4882a593Smuzhiyun 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5348*4882a593Smuzhiyun 	.downgrade_oplock = smb3_downgrade_oplock,
5349*4882a593Smuzhiyun 	.need_neg = smb2_need_neg,
5350*4882a593Smuzhiyun 	.negotiate = smb2_negotiate,
5351*4882a593Smuzhiyun 	.negotiate_wsize = smb3_negotiate_wsize,
5352*4882a593Smuzhiyun 	.negotiate_rsize = smb3_negotiate_rsize,
5353*4882a593Smuzhiyun 	.sess_setup = SMB2_sess_setup,
5354*4882a593Smuzhiyun 	.logoff = SMB2_logoff,
5355*4882a593Smuzhiyun 	.tree_connect = SMB2_tcon,
5356*4882a593Smuzhiyun 	.tree_disconnect = SMB2_tdis,
5357*4882a593Smuzhiyun 	.qfs_tcon = smb3_qfs_tcon,
5358*4882a593Smuzhiyun 	.is_path_accessible = smb2_is_path_accessible,
5359*4882a593Smuzhiyun 	.can_echo = smb2_can_echo,
5360*4882a593Smuzhiyun 	.echo = SMB2_echo,
5361*4882a593Smuzhiyun 	.query_path_info = smb2_query_path_info,
5362*4882a593Smuzhiyun 	/* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5363*4882a593Smuzhiyun 	.query_reparse_tag = smb2_query_reparse_tag,
5364*4882a593Smuzhiyun 	.get_srv_inum = smb2_get_srv_inum,
5365*4882a593Smuzhiyun 	.query_file_info = smb2_query_file_info,
5366*4882a593Smuzhiyun 	.set_path_size = smb2_set_path_size,
5367*4882a593Smuzhiyun 	.set_file_size = smb2_set_file_size,
5368*4882a593Smuzhiyun 	.set_file_info = smb2_set_file_info,
5369*4882a593Smuzhiyun 	.set_compression = smb2_set_compression,
5370*4882a593Smuzhiyun 	.mkdir = smb2_mkdir,
5371*4882a593Smuzhiyun 	.mkdir_setinfo = smb2_mkdir_setinfo,
5372*4882a593Smuzhiyun 	.rmdir = smb2_rmdir,
5373*4882a593Smuzhiyun 	.unlink = smb2_unlink,
5374*4882a593Smuzhiyun 	.rename = smb2_rename_path,
5375*4882a593Smuzhiyun 	.create_hardlink = smb2_create_hardlink,
5376*4882a593Smuzhiyun 	.query_symlink = smb2_query_symlink,
5377*4882a593Smuzhiyun 	.query_mf_symlink = smb3_query_mf_symlink,
5378*4882a593Smuzhiyun 	.create_mf_symlink = smb3_create_mf_symlink,
5379*4882a593Smuzhiyun 	.open = smb2_open_file,
5380*4882a593Smuzhiyun 	.set_fid = smb2_set_fid,
5381*4882a593Smuzhiyun 	.close = smb2_close_file,
5382*4882a593Smuzhiyun 	.close_getattr = smb2_close_getattr,
5383*4882a593Smuzhiyun 	.flush = smb2_flush_file,
5384*4882a593Smuzhiyun 	.async_readv = smb2_async_readv,
5385*4882a593Smuzhiyun 	.async_writev = smb2_async_writev,
5386*4882a593Smuzhiyun 	.sync_read = smb2_sync_read,
5387*4882a593Smuzhiyun 	.sync_write = smb2_sync_write,
5388*4882a593Smuzhiyun 	.query_dir_first = smb2_query_dir_first,
5389*4882a593Smuzhiyun 	.query_dir_next = smb2_query_dir_next,
5390*4882a593Smuzhiyun 	.close_dir = smb2_close_dir,
5391*4882a593Smuzhiyun 	.calc_smb_size = smb2_calc_size,
5392*4882a593Smuzhiyun 	.is_status_pending = smb2_is_status_pending,
5393*4882a593Smuzhiyun 	.is_session_expired = smb2_is_session_expired,
5394*4882a593Smuzhiyun 	.oplock_response = smb2_oplock_response,
5395*4882a593Smuzhiyun 	.queryfs = smb2_queryfs,
5396*4882a593Smuzhiyun 	.mand_lock = smb2_mand_lock,
5397*4882a593Smuzhiyun 	.mand_unlock_range = smb2_unlock_range,
5398*4882a593Smuzhiyun 	.push_mand_locks = smb2_push_mandatory_locks,
5399*4882a593Smuzhiyun 	.get_lease_key = smb2_get_lease_key,
5400*4882a593Smuzhiyun 	.set_lease_key = smb2_set_lease_key,
5401*4882a593Smuzhiyun 	.new_lease_key = smb2_new_lease_key,
5402*4882a593Smuzhiyun 	.generate_signingkey = generate_smb30signingkey,
5403*4882a593Smuzhiyun 	.calc_signature = smb3_calc_signature,
5404*4882a593Smuzhiyun 	.set_integrity  = smb3_set_integrity,
5405*4882a593Smuzhiyun 	.is_read_op = smb21_is_read_op,
5406*4882a593Smuzhiyun 	.set_oplock_level = smb3_set_oplock_level,
5407*4882a593Smuzhiyun 	.create_lease_buf = smb3_create_lease_buf,
5408*4882a593Smuzhiyun 	.parse_lease_buf = smb3_parse_lease_buf,
5409*4882a593Smuzhiyun 	.copychunk_range = smb2_copychunk_range,
5410*4882a593Smuzhiyun 	.duplicate_extents = smb2_duplicate_extents,
5411*4882a593Smuzhiyun 	.validate_negotiate = smb3_validate_negotiate,
5412*4882a593Smuzhiyun 	.wp_retry_size = smb2_wp_retry_size,
5413*4882a593Smuzhiyun 	.dir_needs_close = smb2_dir_needs_close,
5414*4882a593Smuzhiyun 	.fallocate = smb3_fallocate,
5415*4882a593Smuzhiyun 	.enum_snapshots = smb3_enum_snapshots,
5416*4882a593Smuzhiyun 	.notify = smb3_notify,
5417*4882a593Smuzhiyun 	.init_transform_rq = smb3_init_transform_rq,
5418*4882a593Smuzhiyun 	.is_transform_hdr = smb3_is_transform_hdr,
5419*4882a593Smuzhiyun 	.receive_transform = smb3_receive_transform,
5420*4882a593Smuzhiyun 	.get_dfs_refer = smb2_get_dfs_refer,
5421*4882a593Smuzhiyun 	.select_sectype = smb2_select_sectype,
5422*4882a593Smuzhiyun #ifdef CONFIG_CIFS_XATTR
5423*4882a593Smuzhiyun 	.query_all_EAs = smb2_query_eas,
5424*4882a593Smuzhiyun 	.set_EA = smb2_set_ea,
5425*4882a593Smuzhiyun #endif /* CIFS_XATTR */
5426*4882a593Smuzhiyun 	.get_acl = get_smb2_acl,
5427*4882a593Smuzhiyun 	.get_acl_by_fid = get_smb2_acl_by_fid,
5428*4882a593Smuzhiyun 	.set_acl = set_smb2_acl,
5429*4882a593Smuzhiyun 	.next_header = smb2_next_header,
5430*4882a593Smuzhiyun 	.ioctl_query_info = smb2_ioctl_query_info,
5431*4882a593Smuzhiyun 	.make_node = smb2_make_node,
5432*4882a593Smuzhiyun 	.fiemap = smb3_fiemap,
5433*4882a593Smuzhiyun 	.llseek = smb3_llseek,
5434*4882a593Smuzhiyun 	.is_status_io_timeout = smb2_is_status_io_timeout,
5435*4882a593Smuzhiyun };
5436*4882a593Smuzhiyun 
5437*4882a593Smuzhiyun struct smb_version_operations smb311_operations = {
5438*4882a593Smuzhiyun 	.compare_fids = smb2_compare_fids,
5439*4882a593Smuzhiyun 	.setup_request = smb2_setup_request,
5440*4882a593Smuzhiyun 	.setup_async_request = smb2_setup_async_request,
5441*4882a593Smuzhiyun 	.check_receive = smb2_check_receive,
5442*4882a593Smuzhiyun 	.add_credits = smb2_add_credits,
5443*4882a593Smuzhiyun 	.set_credits = smb2_set_credits,
5444*4882a593Smuzhiyun 	.get_credits_field = smb2_get_credits_field,
5445*4882a593Smuzhiyun 	.get_credits = smb2_get_credits,
5446*4882a593Smuzhiyun 	.wait_mtu_credits = smb2_wait_mtu_credits,
5447*4882a593Smuzhiyun 	.adjust_credits = smb2_adjust_credits,
5448*4882a593Smuzhiyun 	.get_next_mid = smb2_get_next_mid,
5449*4882a593Smuzhiyun 	.revert_current_mid = smb2_revert_current_mid,
5450*4882a593Smuzhiyun 	.read_data_offset = smb2_read_data_offset,
5451*4882a593Smuzhiyun 	.read_data_length = smb2_read_data_length,
5452*4882a593Smuzhiyun 	.map_error = map_smb2_to_linux_error,
5453*4882a593Smuzhiyun 	.find_mid = smb2_find_mid,
5454*4882a593Smuzhiyun 	.check_message = smb2_check_message,
5455*4882a593Smuzhiyun 	.dump_detail = smb2_dump_detail,
5456*4882a593Smuzhiyun 	.clear_stats = smb2_clear_stats,
5457*4882a593Smuzhiyun 	.print_stats = smb2_print_stats,
5458*4882a593Smuzhiyun 	.dump_share_caps = smb2_dump_share_caps,
5459*4882a593Smuzhiyun 	.is_oplock_break = smb2_is_valid_oplock_break,
5460*4882a593Smuzhiyun 	.handle_cancelled_mid = smb2_handle_cancelled_mid,
5461*4882a593Smuzhiyun 	.downgrade_oplock = smb3_downgrade_oplock,
5462*4882a593Smuzhiyun 	.need_neg = smb2_need_neg,
5463*4882a593Smuzhiyun 	.negotiate = smb2_negotiate,
5464*4882a593Smuzhiyun 	.negotiate_wsize = smb3_negotiate_wsize,
5465*4882a593Smuzhiyun 	.negotiate_rsize = smb3_negotiate_rsize,
5466*4882a593Smuzhiyun 	.sess_setup = SMB2_sess_setup,
5467*4882a593Smuzhiyun 	.logoff = SMB2_logoff,
5468*4882a593Smuzhiyun 	.tree_connect = SMB2_tcon,
5469*4882a593Smuzhiyun 	.tree_disconnect = SMB2_tdis,
5470*4882a593Smuzhiyun 	.qfs_tcon = smb3_qfs_tcon,
5471*4882a593Smuzhiyun 	.is_path_accessible = smb2_is_path_accessible,
5472*4882a593Smuzhiyun 	.can_echo = smb2_can_echo,
5473*4882a593Smuzhiyun 	.echo = SMB2_echo,
5474*4882a593Smuzhiyun 	.query_path_info = smb2_query_path_info,
5475*4882a593Smuzhiyun 	.query_reparse_tag = smb2_query_reparse_tag,
5476*4882a593Smuzhiyun 	.get_srv_inum = smb2_get_srv_inum,
5477*4882a593Smuzhiyun 	.query_file_info = smb2_query_file_info,
5478*4882a593Smuzhiyun 	.set_path_size = smb2_set_path_size,
5479*4882a593Smuzhiyun 	.set_file_size = smb2_set_file_size,
5480*4882a593Smuzhiyun 	.set_file_info = smb2_set_file_info,
5481*4882a593Smuzhiyun 	.set_compression = smb2_set_compression,
5482*4882a593Smuzhiyun 	.mkdir = smb2_mkdir,
5483*4882a593Smuzhiyun 	.mkdir_setinfo = smb2_mkdir_setinfo,
5484*4882a593Smuzhiyun 	.posix_mkdir = smb311_posix_mkdir,
5485*4882a593Smuzhiyun 	.rmdir = smb2_rmdir,
5486*4882a593Smuzhiyun 	.unlink = smb2_unlink,
5487*4882a593Smuzhiyun 	.rename = smb2_rename_path,
5488*4882a593Smuzhiyun 	.create_hardlink = smb2_create_hardlink,
5489*4882a593Smuzhiyun 	.query_symlink = smb2_query_symlink,
5490*4882a593Smuzhiyun 	.query_mf_symlink = smb3_query_mf_symlink,
5491*4882a593Smuzhiyun 	.create_mf_symlink = smb3_create_mf_symlink,
5492*4882a593Smuzhiyun 	.open = smb2_open_file,
5493*4882a593Smuzhiyun 	.set_fid = smb2_set_fid,
5494*4882a593Smuzhiyun 	.close = smb2_close_file,
5495*4882a593Smuzhiyun 	.close_getattr = smb2_close_getattr,
5496*4882a593Smuzhiyun 	.flush = smb2_flush_file,
5497*4882a593Smuzhiyun 	.async_readv = smb2_async_readv,
5498*4882a593Smuzhiyun 	.async_writev = smb2_async_writev,
5499*4882a593Smuzhiyun 	.sync_read = smb2_sync_read,
5500*4882a593Smuzhiyun 	.sync_write = smb2_sync_write,
5501*4882a593Smuzhiyun 	.query_dir_first = smb2_query_dir_first,
5502*4882a593Smuzhiyun 	.query_dir_next = smb2_query_dir_next,
5503*4882a593Smuzhiyun 	.close_dir = smb2_close_dir,
5504*4882a593Smuzhiyun 	.calc_smb_size = smb2_calc_size,
5505*4882a593Smuzhiyun 	.is_status_pending = smb2_is_status_pending,
5506*4882a593Smuzhiyun 	.is_session_expired = smb2_is_session_expired,
5507*4882a593Smuzhiyun 	.oplock_response = smb2_oplock_response,
5508*4882a593Smuzhiyun 	.queryfs = smb311_queryfs,
5509*4882a593Smuzhiyun 	.mand_lock = smb2_mand_lock,
5510*4882a593Smuzhiyun 	.mand_unlock_range = smb2_unlock_range,
5511*4882a593Smuzhiyun 	.push_mand_locks = smb2_push_mandatory_locks,
5512*4882a593Smuzhiyun 	.get_lease_key = smb2_get_lease_key,
5513*4882a593Smuzhiyun 	.set_lease_key = smb2_set_lease_key,
5514*4882a593Smuzhiyun 	.new_lease_key = smb2_new_lease_key,
5515*4882a593Smuzhiyun 	.generate_signingkey = generate_smb311signingkey,
5516*4882a593Smuzhiyun 	.calc_signature = smb3_calc_signature,
5517*4882a593Smuzhiyun 	.set_integrity  = smb3_set_integrity,
5518*4882a593Smuzhiyun 	.is_read_op = smb21_is_read_op,
5519*4882a593Smuzhiyun 	.set_oplock_level = smb3_set_oplock_level,
5520*4882a593Smuzhiyun 	.create_lease_buf = smb3_create_lease_buf,
5521*4882a593Smuzhiyun 	.parse_lease_buf = smb3_parse_lease_buf,
5522*4882a593Smuzhiyun 	.copychunk_range = smb2_copychunk_range,
5523*4882a593Smuzhiyun 	.duplicate_extents = smb2_duplicate_extents,
5524*4882a593Smuzhiyun /*	.validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5525*4882a593Smuzhiyun 	.wp_retry_size = smb2_wp_retry_size,
5526*4882a593Smuzhiyun 	.dir_needs_close = smb2_dir_needs_close,
5527*4882a593Smuzhiyun 	.fallocate = smb3_fallocate,
5528*4882a593Smuzhiyun 	.enum_snapshots = smb3_enum_snapshots,
5529*4882a593Smuzhiyun 	.notify = smb3_notify,
5530*4882a593Smuzhiyun 	.init_transform_rq = smb3_init_transform_rq,
5531*4882a593Smuzhiyun 	.is_transform_hdr = smb3_is_transform_hdr,
5532*4882a593Smuzhiyun 	.receive_transform = smb3_receive_transform,
5533*4882a593Smuzhiyun 	.get_dfs_refer = smb2_get_dfs_refer,
5534*4882a593Smuzhiyun 	.select_sectype = smb2_select_sectype,
5535*4882a593Smuzhiyun #ifdef CONFIG_CIFS_XATTR
5536*4882a593Smuzhiyun 	.query_all_EAs = smb2_query_eas,
5537*4882a593Smuzhiyun 	.set_EA = smb2_set_ea,
5538*4882a593Smuzhiyun #endif /* CIFS_XATTR */
5539*4882a593Smuzhiyun 	.get_acl = get_smb2_acl,
5540*4882a593Smuzhiyun 	.get_acl_by_fid = get_smb2_acl_by_fid,
5541*4882a593Smuzhiyun 	.set_acl = set_smb2_acl,
5542*4882a593Smuzhiyun 	.next_header = smb2_next_header,
5543*4882a593Smuzhiyun 	.ioctl_query_info = smb2_ioctl_query_info,
5544*4882a593Smuzhiyun 	.make_node = smb2_make_node,
5545*4882a593Smuzhiyun 	.fiemap = smb3_fiemap,
5546*4882a593Smuzhiyun 	.llseek = smb3_llseek,
5547*4882a593Smuzhiyun 	.is_status_io_timeout = smb2_is_status_io_timeout,
5548*4882a593Smuzhiyun };
5549*4882a593Smuzhiyun 
5550*4882a593Smuzhiyun #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5551*4882a593Smuzhiyun struct smb_version_values smb20_values = {
5552*4882a593Smuzhiyun 	.version_string = SMB20_VERSION_STRING,
5553*4882a593Smuzhiyun 	.protocol_id = SMB20_PROT_ID,
5554*4882a593Smuzhiyun 	.req_capabilities = 0, /* MBZ */
5555*4882a593Smuzhiyun 	.large_lock_type = 0,
5556*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5557*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5558*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5559*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5560*4882a593Smuzhiyun 	.header_preamble_size = 0,
5561*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5562*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5563*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5564*4882a593Smuzhiyun 	.cap_unix = 0,
5565*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5566*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5567*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5568*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5569*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease),
5570*4882a593Smuzhiyun };
5571*4882a593Smuzhiyun #endif /* ALLOW_INSECURE_LEGACY */
5572*4882a593Smuzhiyun 
5573*4882a593Smuzhiyun struct smb_version_values smb21_values = {
5574*4882a593Smuzhiyun 	.version_string = SMB21_VERSION_STRING,
5575*4882a593Smuzhiyun 	.protocol_id = SMB21_PROT_ID,
5576*4882a593Smuzhiyun 	.req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5577*4882a593Smuzhiyun 	.large_lock_type = 0,
5578*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5579*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5580*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5581*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5582*4882a593Smuzhiyun 	.header_preamble_size = 0,
5583*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5584*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5585*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5586*4882a593Smuzhiyun 	.cap_unix = 0,
5587*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5588*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5589*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5590*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5591*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease),
5592*4882a593Smuzhiyun };
5593*4882a593Smuzhiyun 
5594*4882a593Smuzhiyun struct smb_version_values smb3any_values = {
5595*4882a593Smuzhiyun 	.version_string = SMB3ANY_VERSION_STRING,
5596*4882a593Smuzhiyun 	.protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5597*4882a593Smuzhiyun 	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5598*4882a593Smuzhiyun 	.large_lock_type = 0,
5599*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5600*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5601*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5602*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5603*4882a593Smuzhiyun 	.header_preamble_size = 0,
5604*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5605*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5606*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5607*4882a593Smuzhiyun 	.cap_unix = 0,
5608*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5609*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5610*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5611*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5612*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease_v2),
5613*4882a593Smuzhiyun };
5614*4882a593Smuzhiyun 
5615*4882a593Smuzhiyun struct smb_version_values smbdefault_values = {
5616*4882a593Smuzhiyun 	.version_string = SMBDEFAULT_VERSION_STRING,
5617*4882a593Smuzhiyun 	.protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5618*4882a593Smuzhiyun 	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5619*4882a593Smuzhiyun 	.large_lock_type = 0,
5620*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5621*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5622*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5623*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5624*4882a593Smuzhiyun 	.header_preamble_size = 0,
5625*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5626*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5627*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5628*4882a593Smuzhiyun 	.cap_unix = 0,
5629*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5630*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5631*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5632*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5633*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease_v2),
5634*4882a593Smuzhiyun };
5635*4882a593Smuzhiyun 
5636*4882a593Smuzhiyun struct smb_version_values smb30_values = {
5637*4882a593Smuzhiyun 	.version_string = SMB30_VERSION_STRING,
5638*4882a593Smuzhiyun 	.protocol_id = SMB30_PROT_ID,
5639*4882a593Smuzhiyun 	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5640*4882a593Smuzhiyun 	.large_lock_type = 0,
5641*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5642*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5643*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5644*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5645*4882a593Smuzhiyun 	.header_preamble_size = 0,
5646*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5647*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5648*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5649*4882a593Smuzhiyun 	.cap_unix = 0,
5650*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5651*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5652*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5653*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5654*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease_v2),
5655*4882a593Smuzhiyun };
5656*4882a593Smuzhiyun 
5657*4882a593Smuzhiyun struct smb_version_values smb302_values = {
5658*4882a593Smuzhiyun 	.version_string = SMB302_VERSION_STRING,
5659*4882a593Smuzhiyun 	.protocol_id = SMB302_PROT_ID,
5660*4882a593Smuzhiyun 	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5661*4882a593Smuzhiyun 	.large_lock_type = 0,
5662*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5663*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5664*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5665*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5666*4882a593Smuzhiyun 	.header_preamble_size = 0,
5667*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5668*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5669*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5670*4882a593Smuzhiyun 	.cap_unix = 0,
5671*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5672*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5673*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5674*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5675*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease_v2),
5676*4882a593Smuzhiyun };
5677*4882a593Smuzhiyun 
5678*4882a593Smuzhiyun struct smb_version_values smb311_values = {
5679*4882a593Smuzhiyun 	.version_string = SMB311_VERSION_STRING,
5680*4882a593Smuzhiyun 	.protocol_id = SMB311_PROT_ID,
5681*4882a593Smuzhiyun 	.req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5682*4882a593Smuzhiyun 	.large_lock_type = 0,
5683*4882a593Smuzhiyun 	.exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5684*4882a593Smuzhiyun 	.shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5685*4882a593Smuzhiyun 	.unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5686*4882a593Smuzhiyun 	.header_size = sizeof(struct smb2_sync_hdr),
5687*4882a593Smuzhiyun 	.header_preamble_size = 0,
5688*4882a593Smuzhiyun 	.max_header_size = MAX_SMB2_HDR_SIZE,
5689*4882a593Smuzhiyun 	.read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5690*4882a593Smuzhiyun 	.lock_cmd = SMB2_LOCK,
5691*4882a593Smuzhiyun 	.cap_unix = 0,
5692*4882a593Smuzhiyun 	.cap_nt_find = SMB2_NT_FIND,
5693*4882a593Smuzhiyun 	.cap_large_files = SMB2_LARGE_FILES,
5694*4882a593Smuzhiyun 	.signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5695*4882a593Smuzhiyun 	.signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5696*4882a593Smuzhiyun 	.create_lease_size = sizeof(struct create_lease_v2),
5697*4882a593Smuzhiyun };
5698