xref: /linux/fs/smb/client/cifssmb.c (revision 1d1981b7910c48649f957e0f1d4299ea05c9a072)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2010
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  *   Contains the routines for constructing the SMB PDUs themselves
8  *
9  */
10 
11  /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c   */
12  /* These are mostly routines that operate on a pathname, or on a tree id     */
13  /* (mounted volume), but there are eight handle based routines which must be */
14  /* treated slightly differently for reconnection purposes since we never     */
15  /* want to reuse a stale file handle and only the caller knows the file info */
16 
17 #include <linux/fs.h>
18 #include <linux/filelock.h>
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/slab.h>
22 #include <linux/posix_acl_xattr.h>
23 #include <linux/pagemap.h>
24 #include <linux/swap.h>
25 #include <linux/task_io_accounting_ops.h>
26 #include <linux/uaccess.h>
27 #include <linux/netfs.h>
28 #include <trace/events/netfs.h>
29 #include "cifsglob.h"
30 #include "cifsproto.h"
31 #include "smb1proto.h"
32 #include "../common/smbfsctl.h"
33 #include "cifsfs.h"
34 #include "cifsacl.h"
35 #include "cifs_unicode.h"
36 #include "cifs_debug.h"
37 #include "fscache.h"
38 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
41 #endif
42 
43 #ifdef CONFIG_CIFS_POSIX
44 static struct {
45 	int index;
46 	char *name;
47 } protocols[] = {
48 	{CIFS_PROT, "\2NT LM 0.12"},
49 	{POSIX_PROT, "\2POSIX 2"},
50 	{BAD_PROT, "\2"}
51 };
52 #else
53 static struct {
54 	int index;
55 	char *name;
56 } protocols[] = {
57 	{CIFS_PROT, "\2NT LM 0.12"},
58 	{BAD_PROT, "\2"}
59 };
60 #endif
61 
62 /* define the number of elements in the cifs dialect array */
63 #ifdef CONFIG_CIFS_POSIX
64 #define CIFS_NUM_PROT 2
65 #else /* not posix */
66 #define CIFS_NUM_PROT 1
67 #endif /* CIFS_POSIX */
68 
69 
70 /* reconnect the socket, tcon, and smb session if needed */
71 static int
72 cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
73 {
74 	struct TCP_Server_Info *server;
75 	struct cifs_ses *ses;
76 	int aborts;
77 	int rc;
78 
79 	/*
80 	 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
81 	 * tcp and smb session status done differently for those three - in the
82 	 * calling routine
83 	 */
84 	if (!tcon)
85 		return 0;
86 
87 	aborts = atomic_read(&tcon->umount_aborts);
88 
89 	ses = tcon->ses;
90 	server = ses->server;
91 
92 	/*
93 	 * only tree disconnect, open, and write, (and ulogoff which does not
94 	 * have tcon) are allowed as we start umount
95 	 */
96 	spin_lock(&tcon->tc_lock);
97 	if (tcon->status == TID_EXITING) {
98 		if (smb_command != SMB_COM_TREE_DISCONNECT) {
99 			spin_unlock(&tcon->tc_lock);
100 			cifs_dbg(FYI, "can not send cmd %d while umounting\n",
101 				 smb_command);
102 			return -ENODEV;
103 		}
104 	}
105 	spin_unlock(&tcon->tc_lock);
106 
107 again:
108 	rc = cifs_wait_for_server_reconnect(server, tcon, aborts, false);
109 	if (rc)
110 		return rc;
111 
112 	spin_lock(&ses->chan_lock);
113 	if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
114 		spin_unlock(&ses->chan_lock);
115 		return 0;
116 	}
117 	spin_unlock(&ses->chan_lock);
118 
119 	mutex_lock(&ses->session_mutex);
120 	/*
121 	 * Handle the case where a concurrent thread failed to negotiate or
122 	 * killed a channel.
123 	 */
124 	spin_lock(&server->srv_lock);
125 	switch (server->tcpStatus) {
126 	case CifsExiting:
127 		spin_unlock(&server->srv_lock);
128 		mutex_unlock(&ses->session_mutex);
129 		return -EHOSTDOWN;
130 	case CifsNeedReconnect:
131 		spin_unlock(&server->srv_lock);
132 		mutex_unlock(&ses->session_mutex);
133 		if (!cifs_tcon_retry(tcon, aborts))
134 			return -EHOSTDOWN;
135 		goto again;
136 	default:
137 		break;
138 	}
139 	spin_unlock(&server->srv_lock);
140 
141 	/*
142 	 * need to prevent multiple threads trying to simultaneously
143 	 * reconnect the same SMB session
144 	 */
145 	spin_lock(&ses->ses_lock);
146 	spin_lock(&ses->chan_lock);
147 	if (!cifs_chan_needs_reconnect(ses, server) &&
148 	    ses->ses_status == SES_GOOD) {
149 		spin_unlock(&ses->chan_lock);
150 		spin_unlock(&ses->ses_lock);
151 
152 		/* this means that we only need to tree connect */
153 		if (tcon->need_reconnect)
154 			goto skip_sess_setup;
155 
156 		mutex_unlock(&ses->session_mutex);
157 		goto out;
158 	}
159 	spin_unlock(&ses->chan_lock);
160 	spin_unlock(&ses->ses_lock);
161 
162 	rc = cifs_negotiate_protocol(0, ses, server);
163 	if (rc) {
164 		mutex_unlock(&ses->session_mutex);
165 		if (!cifs_tcon_retry(tcon, aborts))
166 			return -EHOSTDOWN;
167 		goto again;
168 	}
169 	rc = cifs_setup_session(0, ses, server);
170 	if ((rc == -EACCES) || (rc == -EHOSTDOWN) || (rc == -EKEYREVOKED)) {
171 		/*
172 		 * Try alternate password for next reconnect if an alternate
173 		 * password is available.
174 		 */
175 		if (ses->password2)
176 			swap(ses->password2, ses->password);
177 	}
178 
179 	/* do we need to reconnect tcon? */
180 	if (rc || !tcon->need_reconnect) {
181 		mutex_unlock(&ses->session_mutex);
182 		goto out;
183 	}
184 
185 skip_sess_setup:
186 	cifs_mark_open_files_invalid(tcon);
187 	rc = cifs_tree_connect(0, tcon);
188 	mutex_unlock(&ses->session_mutex);
189 	cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
190 
191 	if (rc) {
192 		pr_warn_once("reconnect tcon failed rc = %d\n", rc);
193 		goto out;
194 	}
195 
196 	atomic_inc(&tconInfoReconnectCount);
197 
198 	/* tell server Unix caps we support */
199 	if (cap_unix(ses))
200 		reset_cifs_unix_caps(0, tcon, NULL, NULL);
201 
202 	/*
203 	 * Removed call to reopen open files here. It is safer (and faster) to
204 	 * reopen files one at a time as needed in read and write.
205 	 *
206 	 * FIXME: what about file locks? don't we need to reclaim them ASAP?
207 	 */
208 
209 out:
210 	/*
211 	 * Check if handle based operation so we know whether we can continue
212 	 * or not without returning to caller to reset file handle
213 	 */
214 	switch (smb_command) {
215 	case SMB_COM_READ_ANDX:
216 	case SMB_COM_WRITE_ANDX:
217 	case SMB_COM_CLOSE:
218 	case SMB_COM_FIND_CLOSE2:
219 	case SMB_COM_LOCKING_ANDX:
220 		rc = -EAGAIN;
221 	}
222 
223 	return rc;
224 }
225 
226 /* Allocate and return pointer to an SMB request buffer, and set basic
227    SMB information in the SMB header.  If the return code is zero, this
228    function must have filled in request_buf pointer */
229 static int
230 small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
231 		void **request_buf)
232 {
233 	unsigned int in_len;
234 	int rc;
235 
236 	rc = cifs_reconnect_tcon(tcon, smb_command);
237 	if (rc)
238 		return rc;
239 
240 	*request_buf = cifs_small_buf_get();
241 	if (*request_buf == NULL) {
242 		/* BB should we add a retry in here if not a writepage? */
243 		return -ENOMEM;
244 	}
245 
246 	in_len = header_assemble((struct smb_hdr *) *request_buf, smb_command,
247 				 tcon, wct);
248 
249 	if (tcon != NULL)
250 		cifs_stats_inc(&tcon->num_smbs_sent);
251 
252 	return in_len;
253 }
254 
255 int
256 small_smb_init_no_tc(const int smb_command, const int wct,
257 		     struct cifs_ses *ses, void **request_buf)
258 {
259 	int rc;
260 	struct smb_hdr *buffer;
261 
262 	rc = small_smb_init(smb_command, wct, NULL, request_buf);
263 	if (rc < 0)
264 		return rc;
265 
266 	buffer = (struct smb_hdr *)*request_buf;
267 	buffer->Mid = get_next_mid(ses->server);
268 	if (ses->capabilities & CAP_UNICODE)
269 		buffer->Flags2 |= SMBFLG2_UNICODE;
270 	if (ses->capabilities & CAP_STATUS32)
271 		buffer->Flags2 |= SMBFLG2_ERR_STATUS;
272 
273 	/* uid, tid can stay at zero as set in header assemble */
274 
275 	/* BB add support for turning on the signing when
276 	this function is used after 1st of session setup requests */
277 
278 	return rc;
279 }
280 
281 /* If the return code is zero, this function must fill in request_buf pointer */
282 static int
283 __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
284 			void **request_buf, void **response_buf)
285 {
286 	unsigned int in_len;
287 
288 	*request_buf = cifs_buf_get();
289 	if (*request_buf == NULL) {
290 		/* BB should we add a retry in here if not a writepage? */
291 		return -ENOMEM;
292 	}
293     /* Although the original thought was we needed the response buf for  */
294     /* potential retries of smb operations it turns out we can determine */
295     /* from the mid flags when the request buffer can be resent without  */
296     /* having to use a second distinct buffer for the response */
297 	if (response_buf)
298 		*response_buf = *request_buf;
299 
300 	in_len = header_assemble((struct smb_hdr *)*request_buf, smb_command, tcon,
301 				 wct);
302 
303 	if (tcon != NULL)
304 		cifs_stats_inc(&tcon->num_smbs_sent);
305 
306 	return in_len;
307 }
308 
309 /* If the return code is zero, this function must fill in request_buf pointer */
310 static int
311 smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
312 	 void **request_buf, void **response_buf)
313 {
314 	int rc;
315 
316 	rc = cifs_reconnect_tcon(tcon, smb_command);
317 	if (rc)
318 		return rc;
319 
320 	return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
321 }
322 
323 static int
324 smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
325 			void **request_buf, void **response_buf)
326 {
327 	spin_lock(&tcon->ses->chan_lock);
328 	if (cifs_chan_needs_reconnect(tcon->ses, tcon->ses->server) ||
329 	    tcon->need_reconnect) {
330 		spin_unlock(&tcon->ses->chan_lock);
331 		return -EHOSTDOWN;
332 	}
333 	spin_unlock(&tcon->ses->chan_lock);
334 
335 	return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
336 }
337 
338 static int validate_t2(struct smb_t2_rsp *pSMB)
339 {
340 	unsigned int total_size;
341 
342 	/* check for plausible wct */
343 	if (pSMB->hdr.WordCount < 10)
344 		goto vt2_err;
345 
346 	/* check for parm and data offset going beyond end of smb */
347 	if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
348 	    get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
349 		goto vt2_err;
350 
351 	total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
352 	if (total_size >= 512)
353 		goto vt2_err;
354 
355 	/* check that bcc is at least as big as parms + data, and that it is
356 	 * less than negotiated smb buffer
357 	 */
358 	total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
359 	if (total_size > get_bcc(&pSMB->hdr) ||
360 	    total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
361 		goto vt2_err;
362 
363 	return 0;
364 vt2_err:
365 	cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
366 		sizeof(struct smb_t2_rsp) + 16);
367 	return -EINVAL;
368 }
369 
370 static int
371 decode_ext_sec_blob(struct cifs_ses *ses, SMB_NEGOTIATE_RSP *pSMBr)
372 {
373 	int	rc = 0;
374 	u16	count;
375 	char	*guid = pSMBr->u.extended_response.GUID;
376 	struct TCP_Server_Info *server = ses->server;
377 
378 	count = get_bcc(&pSMBr->hdr);
379 	if (count < SMB1_CLIENT_GUID_SIZE)
380 		return smb_EIO2(smb_eio_trace_neg_sec_blob_too_small,
381 				count, SMB1_CLIENT_GUID_SIZE);
382 
383 	spin_lock(&cifs_tcp_ses_lock);
384 	if (server->srv_count > 1) {
385 		spin_unlock(&cifs_tcp_ses_lock);
386 		if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
387 			cifs_dbg(FYI, "server UID changed\n");
388 			memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
389 		}
390 	} else {
391 		spin_unlock(&cifs_tcp_ses_lock);
392 		memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
393 	}
394 
395 	if (count == SMB1_CLIENT_GUID_SIZE) {
396 		server->sec_ntlmssp = true;
397 	} else {
398 		count -= SMB1_CLIENT_GUID_SIZE;
399 		rc = decode_negTokenInit(
400 			pSMBr->u.extended_response.SecurityBlob, count, server);
401 		if (rc != 1)
402 			return -EINVAL;
403 	}
404 
405 	return 0;
406 }
407 
408 static bool
409 should_set_ext_sec_flag(enum securityEnum sectype)
410 {
411 	switch (sectype) {
412 	case RawNTLMSSP:
413 	case Kerberos:
414 		return true;
415 	case Unspecified:
416 		if (global_secflags &
417 		    (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
418 			return true;
419 		fallthrough;
420 	default:
421 		return false;
422 	}
423 }
424 
425 int
426 CIFSSMBNegotiate(const unsigned int xid,
427 		 struct cifs_ses *ses,
428 		 struct TCP_Server_Info *server)
429 {
430 	SMB_NEGOTIATE_REQ *pSMB;
431 	SMB_NEGOTIATE_RSP *pSMBr;
432 	unsigned int in_len;
433 	int rc = 0;
434 	int bytes_returned;
435 	int i;
436 	u16 count;
437 
438 	if (!server) {
439 		WARN(1, "%s: server is NULL!\n", __func__);
440 		return smb_EIO(smb_eio_trace_null_pointers);
441 	}
442 
443 	rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
444 		      (void **) &pSMB, (void **) &pSMBr);
445 	if (rc < 0)
446 		return rc;
447 	in_len = rc;
448 
449 	pSMB->hdr.Mid = get_next_mid(server);
450 	pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
451 
452 	if (ses->unicode != 0)
453 		pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
454 
455 	if (should_set_ext_sec_flag(ses->sectype)) {
456 		cifs_dbg(FYI, "Requesting extended security\n");
457 		pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
458 	}
459 
460 	count = 0;
461 	/*
462 	 * We know that all the name entries in the protocols array
463 	 * are short (< 16 bytes anyway) and are NUL terminated.
464 	 */
465 	for (i = 0; i < CIFS_NUM_PROT; i++) {
466 		size_t len = strlen(protocols[i].name) + 1;
467 
468 		memcpy(&pSMB->DialectsArray[count], protocols[i].name, len);
469 		count += len;
470 	}
471 	in_len += count;
472 	pSMB->ByteCount = cpu_to_le16(count);
473 
474 	rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB, in_len,
475 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
476 	if (rc != 0)
477 		goto neg_err_exit;
478 
479 	server->dialect = le16_to_cpu(pSMBr->DialectIndex);
480 	cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
481 	/* Check wct = 1 error case */
482 	if ((pSMBr->hdr.WordCount <= 13) || (server->dialect == BAD_PROT)) {
483 		/* core returns wct = 1, but we do not ask for core - otherwise
484 		small wct just comes when dialect index is -1 indicating we
485 		could not negotiate a common dialect */
486 		rc = -EOPNOTSUPP;
487 		goto neg_err_exit;
488 	} else if (pSMBr->hdr.WordCount != 17) {
489 		/* unknown wct */
490 		rc = -EOPNOTSUPP;
491 		goto neg_err_exit;
492 	}
493 	/* else wct == 17, NTLM or better */
494 
495 	server->sec_mode = pSMBr->SecurityMode;
496 	if ((server->sec_mode & SECMODE_USER) == 0)
497 		cifs_dbg(FYI, "share mode security\n");
498 
499 	/* one byte, so no need to convert this or EncryptionKeyLen from
500 	   little endian */
501 	server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
502 			       cifs_max_pending);
503 	set_credits(server, server->maxReq);
504 	/* probably no need to store and check maxvcs */
505 	server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
506 	/* set up max_read for readahead check */
507 	server->max_read = server->maxBuf;
508 	cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
509 	server->capabilities = le32_to_cpu(pSMBr->Capabilities);
510 	server->session_key_id = pSMBr->SessionKey;
511 	server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
512 	server->timeAdj *= 60;
513 
514 	if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
515 		server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
516 		memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
517 		       CIFS_CRYPTO_KEY_SIZE);
518 	} else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
519 			server->capabilities & CAP_EXTENDED_SECURITY) {
520 		server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
521 		rc = decode_ext_sec_blob(ses, pSMBr);
522 	} else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
523 		/* no crypt key only if plain text pwd */
524 		rc = smb_EIO(smb_eio_trace_neg_no_crypt_key);
525 	} else {
526 		server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
527 		server->capabilities &= ~CAP_EXTENDED_SECURITY;
528 	}
529 
530 	if (!rc)
531 		rc = cifs_enable_signing(server, ses->sign);
532 neg_err_exit:
533 	cifs_buf_release(pSMB);
534 
535 	cifs_dbg(FYI, "negprot rc %d\n", rc);
536 	return rc;
537 }
538 
539 /*
540  * Issue a TREE_CONNECT request.
541  */
542 int CIFSTCon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
543 	     struct cifs_tcon *tcon)
544 {
545 	struct smb_hdr *smb_buffer;
546 	struct smb_hdr *smb_buffer_response;
547 	TCONX_REQ *pSMB;
548 	TCONX_RSP *pSMBr;
549 	unsigned char *bcc_ptr;
550 	int rc = 0;
551 	int length, in_len;
552 	__u16 bytes_left, count;
553 
554 	if (ses == NULL)
555 		return smb_EIO(smb_eio_trace_null_pointers);
556 
557 	smb_buffer = cifs_buf_get();
558 	if (smb_buffer == NULL)
559 		return -ENOMEM;
560 
561 	smb_buffer_response = smb_buffer;
562 
563 	in_len = header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
564 				 NULL /*no tid */, 4 /*wct */);
565 
566 	smb_buffer->Mid = get_next_mid(ses->server);
567 	smb_buffer->Uid = ses->Suid;
568 	pSMB = (TCONX_REQ *) smb_buffer;
569 	pSMBr = (TCONX_RSP *) smb_buffer_response;
570 
571 	pSMB->AndXCommand = 0xFF;
572 	pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
573 	bcc_ptr = &pSMB->Password[0];
574 
575 	pSMB->PasswordLength = cpu_to_le16(1);	/* minimum */
576 	*bcc_ptr = 0; /* password is null byte */
577 	bcc_ptr++;              /* skip password */
578 	/* already aligned so no need to do it below */
579 
580 	if (ses->server->sign)
581 		smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
582 
583 	if (ses->capabilities & CAP_STATUS32)
584 		smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
585 
586 	if (ses->capabilities & CAP_DFS)
587 		smb_buffer->Flags2 |= SMBFLG2_DFS;
588 
589 	if (ses->capabilities & CAP_UNICODE) {
590 		smb_buffer->Flags2 |= SMBFLG2_UNICODE;
591 		length =
592 		    cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
593 			6 /* max utf8 char length in bytes */ *
594 			(/* server len*/ + 256 /* share len */),
595 			ses->local_nls);
596 		bcc_ptr += 2 * length;	/* convert num 16 bit words to bytes */
597 		bcc_ptr += 2;	/* skip trailing null */
598 	} else {		/* ASCII */
599 		strcpy(bcc_ptr, tree);
600 		bcc_ptr += strlen(tree) + 1;
601 	}
602 	strcpy(bcc_ptr, "?????");
603 	bcc_ptr += strlen("?????");
604 	bcc_ptr += 1;
605 	count = bcc_ptr - &pSMB->Password[0];
606 	in_len += count;
607 	pSMB->ByteCount = cpu_to_le16(count);
608 
609 	rc = SendReceive(xid, ses, smb_buffer, in_len, smb_buffer_response,
610 			 &length, 0);
611 
612 	/* above now done in SendReceive */
613 	if (rc == 0) {
614 		bool is_unicode;
615 
616 		tcon->tid = smb_buffer_response->Tid;
617 		bcc_ptr = pByteArea(smb_buffer_response);
618 		bytes_left = get_bcc(smb_buffer_response);
619 		if (bytes_left < 2) {
620 			rc = smb_EIO2(smb_eio_trace_tcon_bcc_too_small,
621 				      bytes_left, 2);
622 			goto out;
623 		}
624 		length = strnlen(bcc_ptr, bytes_left - 2);
625 		if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
626 			is_unicode = true;
627 		else
628 			is_unicode = false;
629 
630 
631 		/* skip service field (NB: this field is always ASCII) */
632 		if (length == 3) {
633 			if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
634 			    (bcc_ptr[2] == 'C')) {
635 				cifs_dbg(FYI, "IPC connection\n");
636 				tcon->ipc = true;
637 				tcon->pipe = true;
638 			}
639 		} else if (length == 2) {
640 			if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
641 				/* the most common case */
642 				cifs_dbg(FYI, "disk share connection\n");
643 			}
644 		}
645 		bcc_ptr += length + 1;
646 		bytes_left -= (length + 1);
647 		strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
648 
649 		/* mostly informational -- no need to fail on error here */
650 		kfree(tcon->nativeFileSystem);
651 		tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
652 						      bytes_left, is_unicode,
653 						      ses->local_nls);
654 
655 		cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
656 
657 		if ((smb_buffer_response->WordCount == 3) ||
658 			 (smb_buffer_response->WordCount == 7))
659 			/* field is in same location */
660 			tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
661 		else
662 			tcon->Flags = 0;
663 		cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
664 
665 		/*
666 		 * reset_cifs_unix_caps calls QFSInfo which requires
667 		 * need_reconnect to be false, but we would not need to call
668 		 * reset_caps if this were not a reconnect case so must check
669 		 * need_reconnect flag here.  The caller will also clear
670 		 * need_reconnect when tcon was successful but needed to be
671 		 * cleared earlier in the case of unix extensions reconnect
672 		 */
673 		if (tcon->need_reconnect && tcon->unix_ext) {
674 			cifs_dbg(FYI, "resetting caps for %s\n", tcon->tree_name);
675 			tcon->need_reconnect = false;
676 			reset_cifs_unix_caps(xid, tcon, NULL, NULL);
677 		}
678 	}
679 out:
680 	cifs_buf_release(smb_buffer);
681 	return rc;
682 }
683 
684 int
685 CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
686 {
687 	struct smb_hdr *smb_buffer;
688 	unsigned int in_len;
689 	int rc = 0;
690 
691 	cifs_dbg(FYI, "In tree disconnect\n");
692 
693 	/* BB: do we need to check this? These should never be NULL. */
694 	if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
695 		return smb_EIO(smb_eio_trace_null_pointers);
696 
697 	/*
698 	 * No need to return error on this operation if tid invalidated and
699 	 * closed on server already e.g. due to tcp session crashing. Also,
700 	 * the tcon is no longer on the list, so no need to take lock before
701 	 * checking this.
702 	 */
703 	spin_lock(&tcon->ses->chan_lock);
704 	if ((tcon->need_reconnect) || CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses)) {
705 		spin_unlock(&tcon->ses->chan_lock);
706 		return smb_EIO(smb_eio_trace_tdis_in_reconnect);
707 	}
708 	spin_unlock(&tcon->ses->chan_lock);
709 
710 	rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
711 			    (void **)&smb_buffer);
712 	if (rc < 0)
713 		return rc;
714 	in_len = rc;
715 
716 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, in_len, 0);
717 	cifs_small_buf_release(smb_buffer);
718 	if (rc)
719 		cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
720 
721 	/* No need to return error on this operation if tid invalidated and
722 	   closed on server already e.g. due to tcp session crashing */
723 	if (rc == -EAGAIN)
724 		rc = 0;
725 
726 	return rc;
727 }
728 
729 /*
730  * This is a no-op for now. We're not really interested in the reply, but
731  * rather in the fact that the server sent one and that server->lstrp
732  * gets updated.
733  *
734  * FIXME: maybe we should consider checking that the reply matches request?
735  */
736 static void
737 cifs_echo_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
738 {
739 	struct cifs_credits credits = { .value = 1, .instance = 0 };
740 
741 	release_mid(server, mid);
742 	add_credits(server, &credits, CIFS_ECHO_OP);
743 }
744 
745 int
746 CIFSSMBEcho(struct TCP_Server_Info *server)
747 {
748 	ECHO_REQ *smb;
749 	int rc = 0;
750 	struct kvec iov[1];
751 	struct smb_rqst rqst = {
752 		.rq_iov = iov,
753 		.rq_nvec = ARRAY_SIZE(iov),
754 	};
755 	unsigned int in_len;
756 
757 	cifs_dbg(FYI, "In echo request\n");
758 
759 	rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
760 	if (rc < 0)
761 		return rc;
762 	in_len = rc;
763 
764 	if (server->capabilities & CAP_UNICODE)
765 		smb->hdr.Flags2 |= SMBFLG2_UNICODE;
766 
767 	/* set up echo request */
768 	smb->hdr.Tid = 0xffff;
769 	smb->hdr.WordCount = 1;
770 	put_unaligned_le16(1, &smb->EchoCount);
771 	put_bcc(1, &smb->hdr);
772 	smb->Data[0] = 'a';
773 	in_len += 3;
774 
775 	iov[0].iov_len = in_len;
776 	iov[0].iov_base = smb;
777 
778 	rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
779 			     server, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
780 	if (rc)
781 		cifs_dbg(FYI, "Echo request failed: %d\n", rc);
782 
783 	cifs_small_buf_release(smb);
784 
785 	return rc;
786 }
787 
788 int
789 CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
790 {
791 	LOGOFF_ANDX_REQ *pSMB;
792 	unsigned int in_len;
793 	int rc = 0;
794 
795 	cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
796 
797 	/*
798 	 * BB: do we need to check validity of ses and server? They should
799 	 * always be valid since we have an active reference. If not, that
800 	 * should probably be a BUG()
801 	 */
802 	if (!ses || !ses->server)
803 		return smb_EIO(smb_eio_trace_null_pointers);
804 
805 	mutex_lock(&ses->session_mutex);
806 	spin_lock(&ses->chan_lock);
807 	if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
808 		spin_unlock(&ses->chan_lock);
809 		goto session_already_dead; /* no need to send SMBlogoff if uid
810 					      already closed due to reconnect */
811 	}
812 	spin_unlock(&ses->chan_lock);
813 
814 	rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
815 	if (rc < 0) {
816 		mutex_unlock(&ses->session_mutex);
817 		return rc;
818 	}
819 	in_len = rc;
820 
821 	pSMB->hdr.Mid = get_next_mid(ses->server);
822 
823 	if (ses->server->sign)
824 		pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
825 
826 	pSMB->hdr.Uid = ses->Suid;
827 
828 	pSMB->AndXCommand = 0xFF;
829 	rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, in_len, 0);
830 	cifs_small_buf_release(pSMB);
831 session_already_dead:
832 	mutex_unlock(&ses->session_mutex);
833 
834 	/* if session dead then we do not need to do ulogoff,
835 		since server closed smb session, no sense reporting
836 		error */
837 	if (rc == -EAGAIN)
838 		rc = 0;
839 	return rc;
840 }
841 
842 int CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
843 		     const char *fileName, __u16 type, int remap)
844 {
845 	TRANSACTION2_SPI_REQ *pSMB = NULL;
846 	TRANSACTION2_SPI_RSP *pSMBr = NULL;
847 	struct unlink_psx_rq *pRqD;
848 	unsigned int in_len;
849 	int name_len;
850 	int rc = 0;
851 	int bytes_returned = 0;
852 	__u16 params, param_offset, offset, byte_count;
853 
854 	cifs_dbg(FYI, "In POSIX delete\n");
855 PsxDelete:
856 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
857 		      (void **) &pSMBr);
858 	if (rc < 0)
859 		return rc;
860 	in_len = rc;
861 
862 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
863 		name_len =
864 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
865 				       PATH_MAX, tcon->ses->local_nls, remap);
866 		name_len++;	/* trailing null */
867 		name_len *= 2;
868 	} else {
869 		name_len = copy_path_name(pSMB->FileName, fileName);
870 	}
871 
872 	params = 6 + name_len;
873 	pSMB->MaxParameterCount = cpu_to_le16(2);
874 	pSMB->MaxDataCount = 0; /* BB double check this with jra */
875 	pSMB->MaxSetupCount = 0;
876 	pSMB->Reserved = 0;
877 	pSMB->Flags = 0;
878 	pSMB->Timeout = 0;
879 	pSMB->Reserved2 = 0;
880 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
881 				InformationLevel);
882 	offset = param_offset + params;
883 
884 	/* Setup pointer to Request Data (inode type). */
885 	pRqD = (struct unlink_psx_rq *)((char *)(pSMB) + offset);
886 	pRqD->type = cpu_to_le16(type);
887 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
888 	pSMB->DataOffset = cpu_to_le16(offset);
889 	pSMB->SetupCount = 1;
890 	pSMB->Reserved3 = 0;
891 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
892 	byte_count = 3 /* pad */  + params + sizeof(struct unlink_psx_rq);
893 
894 	pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
895 	pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
896 	pSMB->ParameterCount = cpu_to_le16(params);
897 	pSMB->TotalParameterCount = pSMB->ParameterCount;
898 	pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
899 	pSMB->Reserved4 = 0;
900 	in_len += byte_count;
901 	pSMB->ByteCount = cpu_to_le16(byte_count);
902 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
903 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
904 	if (rc)
905 		cifs_dbg(FYI, "Posix delete returned %d\n", rc);
906 	cifs_buf_release(pSMB);
907 
908 	cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
909 
910 	if (rc == -EAGAIN)
911 		goto PsxDelete;
912 
913 	return rc;
914 }
915 
916 int
917 CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
918 	       struct cifs_sb_info *cifs_sb, struct dentry *dentry)
919 {
920 	DELETE_FILE_REQ *pSMB = NULL;
921 	DELETE_FILE_RSP *pSMBr = NULL;
922 	unsigned int in_len;
923 	int rc = 0;
924 	int bytes_returned;
925 	int name_len;
926 	int remap = cifs_remap(cifs_sb);
927 
928 DelFileRetry:
929 	rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
930 		      (void **) &pSMBr);
931 	if (rc < 0)
932 		return rc;
933 	in_len = rc;
934 
935 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
936 		name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
937 					      PATH_MAX, cifs_sb->local_nls,
938 					      remap);
939 		name_len++;	/* trailing null */
940 		name_len *= 2;
941 	} else {
942 		name_len = copy_path_name(pSMB->fileName, name);
943 	}
944 	pSMB->SearchAttributes =
945 	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
946 	pSMB->BufferFormat = 0x04;
947 	in_len += name_len + 1;
948 	pSMB->ByteCount = cpu_to_le16(name_len + 1);
949 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
950 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
951 	cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
952 	if (rc)
953 		cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
954 
955 	cifs_buf_release(pSMB);
956 	if (rc == -EAGAIN)
957 		goto DelFileRetry;
958 
959 	return rc;
960 }
961 
962 int
963 CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
964 	     struct cifs_sb_info *cifs_sb)
965 {
966 	DELETE_DIRECTORY_REQ *pSMB = NULL;
967 	DELETE_DIRECTORY_RSP *pSMBr = NULL;
968 	unsigned int in_len;
969 	int rc = 0;
970 	int bytes_returned;
971 	int name_len;
972 	int remap = cifs_remap(cifs_sb);
973 
974 	cifs_dbg(FYI, "In CIFSSMBRmDir\n");
975 RmDirRetry:
976 	rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
977 		      (void **) &pSMBr);
978 	if (rc < 0)
979 		return rc;
980 	in_len = rc;
981 
982 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
983 		name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
984 					      PATH_MAX, cifs_sb->local_nls,
985 					      remap);
986 		name_len++;	/* trailing null */
987 		name_len *= 2;
988 	} else {
989 		name_len = copy_path_name(pSMB->DirName, name);
990 	}
991 
992 	pSMB->BufferFormat = 0x04;
993 	in_len += name_len + 1;
994 	pSMB->ByteCount = cpu_to_le16(name_len + 1);
995 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
996 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
997 	cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
998 	if (rc)
999 		cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
1000 
1001 	cifs_buf_release(pSMB);
1002 	if (rc == -EAGAIN)
1003 		goto RmDirRetry;
1004 	return rc;
1005 }
1006 
1007 int
1008 CIFSSMBMkDir(const unsigned int xid, struct inode *inode, umode_t mode,
1009 	     struct cifs_tcon *tcon, const char *name,
1010 	     struct cifs_sb_info *cifs_sb)
1011 {
1012 	int rc = 0;
1013 	CREATE_DIRECTORY_REQ *pSMB = NULL;
1014 	CREATE_DIRECTORY_RSP *pSMBr = NULL;
1015 	unsigned int in_len;
1016 	int bytes_returned;
1017 	int name_len;
1018 	int remap = cifs_remap(cifs_sb);
1019 
1020 	cifs_dbg(FYI, "In CIFSSMBMkDir\n");
1021 MkDirRetry:
1022 	rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1023 		      (void **) &pSMBr);
1024 	if (rc < 0)
1025 		return rc;
1026 	in_len = rc;
1027 
1028 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1029 		name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1030 					      PATH_MAX, cifs_sb->local_nls,
1031 					      remap);
1032 		name_len++;	/* trailing null */
1033 		name_len *= 2;
1034 	} else {
1035 		name_len = copy_path_name(pSMB->DirName, name);
1036 	}
1037 
1038 	pSMB->BufferFormat = 0x04;
1039 	in_len += name_len + 1;
1040 	pSMB->ByteCount = cpu_to_le16(name_len + 1);
1041 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
1042 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1043 	cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
1044 	if (rc)
1045 		cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
1046 
1047 	cifs_buf_release(pSMB);
1048 	if (rc == -EAGAIN)
1049 		goto MkDirRetry;
1050 	return rc;
1051 }
1052 
1053 int CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1054 		    __u32 posix_flags, __u64 mode, __u16 *netfid,
1055 		    FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1056 		    const char *name, int remap)
1057 {
1058 	TRANSACTION2_SPI_REQ *pSMB = NULL;
1059 	TRANSACTION2_SPI_RSP *pSMBr = NULL;
1060 	unsigned int in_len;
1061 	int name_len;
1062 	int rc = 0;
1063 	int bytes_returned = 0;
1064 	__u16 params, param_offset, offset, byte_count, count;
1065 	OPEN_PSX_REQ *pdata;
1066 	OPEN_PSX_RSP *psx_rsp;
1067 
1068 	cifs_dbg(FYI, "In POSIX Create\n");
1069 PsxCreat:
1070 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1071 		      (void **) &pSMBr);
1072 	if (rc < 0)
1073 		return rc;
1074 	in_len = rc;
1075 
1076 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1077 		name_len =
1078 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1079 				       PATH_MAX, tcon->ses->local_nls, remap);
1080 		name_len++;	/* trailing null */
1081 		name_len *= 2;
1082 	} else {
1083 		name_len = copy_path_name(pSMB->FileName, name);
1084 	}
1085 
1086 	params = 6 + name_len;
1087 	count = sizeof(OPEN_PSX_REQ);
1088 	pSMB->MaxParameterCount = cpu_to_le16(2);
1089 	pSMB->MaxDataCount = cpu_to_le16(1000);	/* large enough */
1090 	pSMB->MaxSetupCount = 0;
1091 	pSMB->Reserved = 0;
1092 	pSMB->Flags = 0;
1093 	pSMB->Timeout = 0;
1094 	pSMB->Reserved2 = 0;
1095 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
1096 				InformationLevel);
1097 	offset = param_offset + params;
1098 	pdata = (OPEN_PSX_REQ *)((char *)(pSMB) + offset);
1099 	pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1100 	pdata->Permissions = cpu_to_le64(mode);
1101 	pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1102 	pdata->OpenFlags =  cpu_to_le32(*pOplock);
1103 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
1104 	pSMB->DataOffset = cpu_to_le16(offset);
1105 	pSMB->SetupCount = 1;
1106 	pSMB->Reserved3 = 0;
1107 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1108 	byte_count = 3 /* pad */  + params + count;
1109 
1110 	pSMB->DataCount = cpu_to_le16(count);
1111 	pSMB->ParameterCount = cpu_to_le16(params);
1112 	pSMB->TotalDataCount = pSMB->DataCount;
1113 	pSMB->TotalParameterCount = pSMB->ParameterCount;
1114 	pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1115 	pSMB->Reserved4 = 0;
1116 	in_len += byte_count;
1117 	pSMB->ByteCount = cpu_to_le16(byte_count);
1118 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
1119 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1120 	if (rc) {
1121 		cifs_dbg(FYI, "Posix create returned %d\n", rc);
1122 		goto psx_create_err;
1123 	}
1124 
1125 	cifs_dbg(FYI, "copying inode info\n");
1126 	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1127 
1128 	if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1129 		rc = smb_EIO2(smb_eio_trace_create_rsp_too_small,
1130 			      get_bcc(&pSMBr->hdr), sizeof(OPEN_PSX_RSP));
1131 		goto psx_create_err;
1132 	}
1133 
1134 	/* copy return information to pRetData */
1135 	psx_rsp = (OPEN_PSX_RSP *)
1136 		((char *)pSMBr + le16_to_cpu(pSMBr->t2.DataOffset));
1137 
1138 	*pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1139 	if (netfid)
1140 		*netfid = psx_rsp->Fid;   /* cifs fid stays in le */
1141 	/* Let caller know file was created so we can set the mode. */
1142 	/* Do we care about the CreateAction in any other cases? */
1143 	if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1144 		*pOplock |= CIFS_CREATE_ACTION;
1145 	/* check to make sure response data is there */
1146 	if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1147 		pRetData->Type = cpu_to_le32(-1); /* unknown */
1148 		cifs_dbg(NOISY, "unknown type\n");
1149 	} else {
1150 		if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1151 					+ sizeof(FILE_UNIX_BASIC_INFO)) {
1152 			cifs_dbg(VFS, "Open response data too small\n");
1153 			pRetData->Type = cpu_to_le32(-1);
1154 			goto psx_create_err;
1155 		}
1156 		memcpy(pRetData,
1157 		       (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1158 		       sizeof(*pRetData));
1159 	}
1160 
1161 psx_create_err:
1162 	cifs_buf_release(pSMB);
1163 
1164 	if (posix_flags & SMB_O_DIRECTORY)
1165 		cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
1166 	else
1167 		cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
1168 
1169 	if (rc == -EAGAIN)
1170 		goto PsxCreat;
1171 
1172 	return rc;
1173 }
1174 
1175 static __u16 convert_disposition(int disposition)
1176 {
1177 	__u16 ofun = 0;
1178 
1179 	switch (disposition) {
1180 		case FILE_SUPERSEDE:
1181 			ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1182 			break;
1183 		case FILE_OPEN:
1184 			ofun = SMBOPEN_OAPPEND;
1185 			break;
1186 		case FILE_CREATE:
1187 			ofun = SMBOPEN_OCREATE;
1188 			break;
1189 		case FILE_OPEN_IF:
1190 			ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1191 			break;
1192 		case FILE_OVERWRITE:
1193 			ofun = SMBOPEN_OTRUNC;
1194 			break;
1195 		case FILE_OVERWRITE_IF:
1196 			ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1197 			break;
1198 		default:
1199 			cifs_dbg(FYI, "unknown disposition %d\n", disposition);
1200 			ofun =  SMBOPEN_OAPPEND; /* regular open */
1201 	}
1202 	return ofun;
1203 }
1204 
1205 static int
1206 access_flags_to_smbopen_mode(const int access_flags)
1207 {
1208 	/*
1209 	 * SYSTEM_SECURITY grants both read and write access to SACL, treat is as read/write.
1210 	 * MAXIMUM_ALLOWED grants as many access as possible, so treat it as read/write too.
1211 	 * SYNCHRONIZE as is does not grant any specific access, so do not check its mask.
1212 	 * If only SYNCHRONIZE bit is specified then fallback to read access.
1213 	 */
1214 	bool with_write_flags = access_flags & (FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA |
1215 						FILE_DELETE_CHILD | FILE_WRITE_ATTRIBUTES | DELETE |
1216 						WRITE_DAC | WRITE_OWNER | SYSTEM_SECURITY |
1217 						MAXIMUM_ALLOWED | GENERIC_WRITE | GENERIC_ALL);
1218 	bool with_read_flags = access_flags & (FILE_READ_DATA | FILE_READ_EA | FILE_EXECUTE |
1219 						FILE_READ_ATTRIBUTES | READ_CONTROL |
1220 						SYSTEM_SECURITY | MAXIMUM_ALLOWED | GENERIC_ALL |
1221 						GENERIC_EXECUTE | GENERIC_READ);
1222 	bool with_execute_flags = access_flags & (FILE_EXECUTE | MAXIMUM_ALLOWED | GENERIC_ALL |
1223 						GENERIC_EXECUTE);
1224 
1225 	if (with_write_flags && with_read_flags)
1226 		return SMBOPEN_READWRITE;
1227 	else if (with_write_flags)
1228 		return SMBOPEN_WRITE;
1229 	else if (with_execute_flags)
1230 		return SMBOPEN_EXECUTE;
1231 	else
1232 		return SMBOPEN_READ;
1233 }
1234 
1235 int SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1236 		  const char *fileName, const int openDisposition,
1237 		  const int access_flags, const int create_options,
1238 		  __u16 *netfid, int *pOplock, FILE_ALL_INFO *pfile_info,
1239 		  int remap)
1240 {
1241 	int rc;
1242 	OPENX_REQ *pSMB = NULL;
1243 	OPENX_RSP *pSMBr = NULL;
1244 	unsigned int in_len;
1245 	int bytes_returned;
1246 	int name_len;
1247 	__u16 count;
1248 
1249 OldOpenRetry:
1250 	rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1251 		      (void **) &pSMBr);
1252 	if (rc < 0)
1253 		return rc;
1254 	in_len = rc;
1255 
1256 	pSMB->AndXCommand = 0xFF;       /* none */
1257 
1258 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1259 		count = 1;      /* account for one byte pad to word boundary */
1260 		name_len =
1261 		   cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1262 				      fileName, PATH_MAX,
1263 				      tcon->ses->local_nls, remap);
1264 		name_len++;     /* trailing null */
1265 		name_len *= 2;
1266 	} else {
1267 		count = 0;      /* no pad */
1268 		name_len = copy_path_name(pSMB->fileName, fileName);
1269 	}
1270 	if (*pOplock & REQ_OPLOCK)
1271 		pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1272 	else if (*pOplock & REQ_BATCHOPLOCK)
1273 		pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1274 
1275 	pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1276 	pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1277 	pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1278 	/* set file as system file if special file such as fifo,
1279 	 * socket, char or block and server expecting SFU style and
1280 	   no Unix extensions */
1281 
1282 	if (create_options & CREATE_OPTION_SPECIAL)
1283 		pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1284 	else /* BB FIXME BB */
1285 		pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1286 
1287 	if (create_options & CREATE_OPTION_READONLY)
1288 		pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1289 
1290 	/* BB FIXME BB */
1291 /*	pSMB->CreateOptions = cpu_to_le32(create_options &
1292 						 CREATE_OPTIONS_MASK); */
1293 	/* BB FIXME END BB */
1294 
1295 	pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1296 	pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1297 	count += name_len;
1298 	in_len += count;
1299 
1300 	pSMB->ByteCount = cpu_to_le16(count);
1301 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
1302 			(struct smb_hdr *)pSMBr, &bytes_returned, 0);
1303 	cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1304 	if (rc) {
1305 		cifs_dbg(FYI, "Error in Open = %d\n", rc);
1306 	} else {
1307 	/* BB verify if wct == 15 */
1308 		*netfid = pSMBr->Fid;   /* cifs fid stays in le */
1309 		/* Let caller know file was created so we can set the mode. */
1310 		/* Do we care about the CreateAction in any other cases? */
1311 	/* BB FIXME BB */
1312 /*		if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1313 			*pOplock |= CIFS_CREATE_ACTION; */
1314 	/* BB FIXME END */
1315 
1316 		if (pfile_info) {
1317 			pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1318 			pfile_info->LastAccessTime = 0; /* BB fixme */
1319 			pfile_info->LastWriteTime = 0; /* BB fixme */
1320 			pfile_info->ChangeTime = 0;  /* BB fixme */
1321 			pfile_info->Attributes =
1322 				cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1323 			/* the file_info buf is endian converted by caller */
1324 			pfile_info->AllocationSize =
1325 				cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1326 			pfile_info->EndOfFile = pfile_info->AllocationSize;
1327 			pfile_info->NumberOfLinks = cpu_to_le32(1);
1328 			pfile_info->DeletePending = 0; /* successful open = not delete pending */
1329 		}
1330 	}
1331 
1332 	cifs_buf_release(pSMB);
1333 	if (rc == -EAGAIN)
1334 		goto OldOpenRetry;
1335 	return rc;
1336 }
1337 
1338 int
1339 CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1340 	  FILE_ALL_INFO *buf)
1341 {
1342 	int rc;
1343 	OPEN_REQ *req = NULL;
1344 	OPEN_RSP *rsp = NULL;
1345 	int bytes_returned;
1346 	int name_len;
1347 	__u16 count;
1348 	struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1349 	struct cifs_tcon *tcon = oparms->tcon;
1350 	int remap = cifs_remap(cifs_sb);
1351 	const struct nls_table *nls = cifs_sb->local_nls;
1352 	int create_options = oparms->create_options;
1353 	int desired_access = oparms->desired_access;
1354 	int disposition = oparms->disposition;
1355 	const char *path = oparms->path;
1356 	unsigned int in_len;
1357 
1358 openRetry:
1359 	rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1360 		      (void **)&rsp);
1361 	if (rc < 0)
1362 		return rc;
1363 	in_len = rc;
1364 
1365 	/* no commands go after this */
1366 	req->AndXCommand = 0xFF;
1367 
1368 	if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1369 		/* account for one byte pad to word boundary */
1370 		count = 1;
1371 		name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1372 					      path, PATH_MAX, nls, remap);
1373 		/* trailing null */
1374 		name_len++;
1375 		name_len *= 2;
1376 		req->NameLength = cpu_to_le16(name_len);
1377 	} else {
1378 		/* BB improve check for buffer overruns BB */
1379 		/* no pad */
1380 		count = 0;
1381 		name_len = copy_path_name(req->fileName, path);
1382 		req->NameLength = cpu_to_le16(name_len);
1383 	}
1384 
1385 	if (*oplock & REQ_OPLOCK)
1386 		req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1387 	else if (*oplock & REQ_BATCHOPLOCK)
1388 		req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1389 
1390 	req->DesiredAccess = cpu_to_le32(desired_access);
1391 	req->AllocationSize = 0;
1392 
1393 	/*
1394 	 * Set file as system file if special file such as fifo, socket, char
1395 	 * or block and server expecting SFU style and no Unix extensions.
1396 	 */
1397 	if (create_options & CREATE_OPTION_SPECIAL)
1398 		req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1399 	else
1400 		req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1401 
1402 	/*
1403 	 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1404 	 * sensitive checks for other servers such as Samba.
1405 	 */
1406 	if (tcon->ses->capabilities & CAP_UNIX)
1407 		req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1408 
1409 	if (create_options & CREATE_OPTION_READONLY)
1410 		req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1411 
1412 	req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1413 	req->CreateDisposition = cpu_to_le32(disposition);
1414 	req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1415 
1416 	/* BB Experiment with various impersonation levels and verify */
1417 	req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1418 	req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
1419 
1420 	count += name_len;
1421 	in_len += count;
1422 
1423 	req->ByteCount = cpu_to_le16(count);
1424 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req, in_len,
1425 			 (struct smb_hdr *)rsp, &bytes_returned, 0);
1426 	cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1427 	if (rc) {
1428 		cifs_dbg(FYI, "Error in Open = %d\n", rc);
1429 		cifs_buf_release(req);
1430 		if (rc == -EAGAIN)
1431 			goto openRetry;
1432 		return rc;
1433 	}
1434 
1435 	/* 1 byte no need to le_to_cpu */
1436 	*oplock = rsp->OplockLevel;
1437 	/* cifs fid stays in le */
1438 	oparms->fid->netfid = rsp->Fid;
1439 	oparms->fid->access = desired_access;
1440 
1441 	/* Let caller know file was created so we can set the mode. */
1442 	/* Do we care about the CreateAction in any other cases? */
1443 	if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1444 		*oplock |= CIFS_CREATE_ACTION;
1445 
1446 	if (buf) {
1447 		/* copy commonly used attributes */
1448 		memcpy(&buf->common_attributes,
1449 		       &rsp->common_attributes,
1450 		       sizeof(buf->common_attributes));
1451 		/* the file_info buf is endian converted by caller */
1452 		buf->AllocationSize = rsp->AllocationSize;
1453 		buf->EndOfFile = rsp->EndOfFile;
1454 		buf->NumberOfLinks = cpu_to_le32(1);
1455 		buf->DeletePending = 0; /* successful open = not delete pending */
1456 	}
1457 
1458 	cifs_buf_release(req);
1459 	return rc;
1460 }
1461 
1462 static void
1463 cifs_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1464 {
1465 	struct cifs_io_subrequest *rdata = mid->callback_data;
1466 	struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
1467 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
1468 	struct inode *inode = &ictx->inode;
1469 	struct smb_rqst rqst = { .rq_iov = rdata->iov,
1470 				 .rq_nvec = 1,
1471 				 .rq_iter = rdata->subreq.io_iter };
1472 	struct cifs_credits credits = {
1473 		.value = 1,
1474 		.instance = 0,
1475 		.rreq_debug_id = rdata->rreq->debug_id,
1476 		.rreq_debug_index = rdata->subreq.debug_index,
1477 	};
1478 	unsigned int rreq_debug_id = rdata->rreq->debug_id;
1479 	unsigned int subreq_debug_index = rdata->subreq.debug_index;
1480 
1481 	cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%zu\n",
1482 		 __func__, mid->mid, mid->mid_state, rdata->result,
1483 		 rdata->subreq.len);
1484 
1485 	switch (mid->mid_state) {
1486 	case MID_RESPONSE_RECEIVED:
1487 		/* result already set, check signature */
1488 		if (server->sign) {
1489 			int rc = 0;
1490 
1491 			iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
1492 			rc = cifs_verify_signature(&rqst, server,
1493 						  mid->sequence_number);
1494 			if (rc)
1495 				cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1496 					 rc);
1497 		}
1498 		/* FIXME: should this be counted toward the initiating task? */
1499 		task_io_account_read(rdata->got_bytes);
1500 		cifs_stats_bytes_read(tcon, rdata->got_bytes);
1501 		break;
1502 	case MID_REQUEST_SUBMITTED:
1503 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_req_submitted);
1504 		goto do_retry;
1505 	case MID_RETRY_NEEDED:
1506 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_retry_needed);
1507 do_retry:
1508 		__set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags);
1509 		rdata->result = -EAGAIN;
1510 		if (server->sign && rdata->got_bytes)
1511 			/* reset bytes number since we can not check a sign */
1512 			rdata->got_bytes = 0;
1513 		/* FIXME: should this be counted toward the initiating task? */
1514 		task_io_account_read(rdata->got_bytes);
1515 		cifs_stats_bytes_read(tcon, rdata->got_bytes);
1516 		break;
1517 	case MID_RESPONSE_MALFORMED:
1518 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_malformed);
1519 		rdata->result = smb_EIO(smb_eio_trace_read_rsp_malformed);
1520 		break;
1521 	default:
1522 		trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_unknown);
1523 		rdata->result = smb_EIO1(smb_eio_trace_read_mid_state_unknown,
1524 					 mid->mid_state);
1525 		break;
1526 	}
1527 
1528 	if (rdata->result == -ENODATA) {
1529 		rdata->result = 0;
1530 		__set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
1531 		trace_smb3_read_err(rdata->rreq->debug_id,
1532 				    rdata->subreq.debug_index,
1533 				    rdata->xid,
1534 				    rdata->req->cfile->fid.persistent_fid,
1535 				    tcon->tid, tcon->ses->Suid,
1536 				    rdata->subreq.start + rdata->subreq.transferred,
1537 				    rdata->subreq.len   - rdata->subreq.transferred,
1538 				    rdata->result);
1539 	} else {
1540 		size_t trans = rdata->subreq.transferred + rdata->got_bytes;
1541 		if (trans < rdata->subreq.len &&
1542 		    rdata->subreq.start + trans >= netfs_read_remote_i_size(inode)) {
1543 			rdata->result = 0;
1544 			__set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
1545 		} else if (rdata->got_bytes > 0) {
1546 			__set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags);
1547 		}
1548 		if (rdata->got_bytes)
1549 			__set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags);
1550 		trace_smb3_read_done(rdata->rreq->debug_id,
1551 				     rdata->subreq.debug_index,
1552 				     rdata->xid,
1553 				     rdata->req->cfile->fid.persistent_fid,
1554 				     tcon->tid, tcon->ses->Suid,
1555 				     rdata->subreq.start + rdata->subreq.transferred,
1556 				     rdata->got_bytes);
1557 	}
1558 
1559 	trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value,
1560 			      server->credits, server->in_flight,
1561 			      0, cifs_trace_rw_credits_read_response_clear);
1562 	rdata->credits.value = 0;
1563 	rdata->subreq.error = rdata->result;
1564 	rdata->subreq.transferred += rdata->got_bytes;
1565 	trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
1566 	netfs_read_subreq_terminated(&rdata->subreq);
1567 	release_mid(server, mid);
1568 	add_credits(server, &credits, 0);
1569 	trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
1570 			      server->credits, server->in_flight,
1571 			      credits.value, cifs_trace_rw_credits_read_response_add);
1572 }
1573 
1574 /* cifs_async_readv - send an async write, and set up mid to handle result */
1575 int
1576 cifs_async_readv(struct cifs_io_subrequest *rdata)
1577 {
1578 	int rc;
1579 	READ_REQ *smb = NULL;
1580 	int wct;
1581 	struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
1582 	struct smb_rqst rqst = { .rq_iov = rdata->iov,
1583 				 .rq_nvec = 1 };
1584 	unsigned int in_len;
1585 
1586 	cifs_dbg(FYI, "%s: offset=%llu bytes=%zu\n",
1587 		 __func__, rdata->subreq.start, rdata->subreq.len);
1588 
1589 	if (tcon->ses->capabilities & CAP_LARGE_FILES)
1590 		wct = 12;
1591 	else {
1592 		wct = 10; /* old style read */
1593 		if ((rdata->subreq.start >> 32) > 0)  {
1594 			/* can not handle this big offset for old */
1595 			return smb_EIO(smb_eio_trace_read_too_far);
1596 		}
1597 	}
1598 
1599 	rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1600 	if (rc < 0)
1601 		return rc;
1602 	in_len = rc;
1603 
1604 	smb->hdr.Pid = cpu_to_le16((__u16)rdata->req->pid);
1605 	smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->req->pid >> 16));
1606 
1607 	smb->AndXCommand = 0xFF;	/* none */
1608 	smb->Fid = rdata->req->cfile->fid.netfid;
1609 	smb->OffsetLow = cpu_to_le32(rdata->subreq.start & 0xFFFFFFFF);
1610 	if (wct == 12)
1611 		smb->OffsetHigh = cpu_to_le32(rdata->subreq.start >> 32);
1612 	smb->Remaining = 0;
1613 	smb->MaxCount = cpu_to_le16(rdata->subreq.len & 0xFFFF);
1614 	smb->MaxCountHigh = cpu_to_le32(rdata->subreq.len >> 16);
1615 	if (wct == 12)
1616 		smb->ByteCount = 0;
1617 	else {
1618 		/* old style read */
1619 		struct smb_com_readx_req *smbr =
1620 			(struct smb_com_readx_req *)smb;
1621 		smbr->ByteCount = 0;
1622 	}
1623 
1624 	/* 4 for RFC1001 length + 1 for BCC */
1625 	rdata->iov[0].iov_base = smb;
1626 	rdata->iov[0].iov_len = in_len;
1627 
1628 	trace_smb3_read_enter(rdata->rreq->debug_id,
1629 			      rdata->subreq.debug_index,
1630 			      rdata->xid,
1631 			      rdata->req->cfile->fid.netfid,
1632 			      tcon->tid, tcon->ses->Suid,
1633 			      rdata->subreq.start, rdata->subreq.len);
1634 
1635 	rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1636 			     cifs_readv_callback, NULL, rdata, 0, NULL);
1637 
1638 	if (rc == 0)
1639 		cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1640 	cifs_small_buf_release(smb);
1641 	return rc;
1642 }
1643 
1644 int
1645 CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1646 	    unsigned int *nbytes, char **buf, int *pbuf_type)
1647 {
1648 	int rc = -EACCES;
1649 	READ_REQ *pSMB = NULL;
1650 	READ_RSP *pSMBr = NULL;
1651 	char *pReadData = NULL;
1652 	int wct;
1653 	int resp_buf_type = 0;
1654 	struct kvec iov[1];
1655 	struct kvec rsp_iov;
1656 	__u32 pid = io_parms->pid;
1657 	__u16 netfid = io_parms->netfid;
1658 	__u64 offset = io_parms->offset;
1659 	struct cifs_tcon *tcon = io_parms->tcon;
1660 	unsigned int in_len;
1661 	unsigned int count = io_parms->length;
1662 
1663 	cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
1664 	if (tcon->ses->capabilities & CAP_LARGE_FILES)
1665 		wct = 12;
1666 	else {
1667 		wct = 10; /* old style read */
1668 		if ((offset >> 32) > 0)  {
1669 			/* can not handle this big offset for old */
1670 			return smb_EIO(smb_eio_trace_read_too_far);
1671 		}
1672 	}
1673 
1674 	*nbytes = 0;
1675 	rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1676 	if (rc < 0)
1677 		return rc;
1678 	in_len = rc;
1679 
1680 	pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1681 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1682 
1683 	/* tcon and ses pointer are checked in smb_init */
1684 	if (!tcon->ses->server) {
1685 		cifs_small_buf_release(pSMB);
1686 		return -ECONNABORTED;
1687 	}
1688 
1689 	pSMB->AndXCommand = 0xFF;       /* none */
1690 	pSMB->Fid = netfid;
1691 	pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1692 	if (wct == 12)
1693 		pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1694 
1695 	pSMB->Remaining = 0;
1696 	pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1697 	pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1698 	if (wct == 12)
1699 		pSMB->ByteCount = 0;  /* no need to do le conversion since 0 */
1700 	else {
1701 		/* old style read */
1702 		struct smb_com_readx_req *pSMBW =
1703 			(struct smb_com_readx_req *)pSMB;
1704 		pSMBW->ByteCount = 0;
1705 	}
1706 
1707 	iov[0].iov_base = (char *)pSMB;
1708 	iov[0].iov_len = in_len;
1709 	rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1710 			  CIFS_LOG_ERROR, &rsp_iov);
1711 	cifs_small_buf_release(pSMB);
1712 	cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1713 	pSMBr = (READ_RSP *)rsp_iov.iov_base;
1714 	if (rc) {
1715 		cifs_dbg(VFS, "Send error in read = %d\n", rc);
1716 	} else if (rsp_iov.iov_len < tcon->ses->server->vals->read_rsp_size) {
1717 		/* check that the received response can hold a whole READ_RSP */
1718 		cifs_dbg(FYI, "%s: server returned short header. got=%zu expected=%zu\n",
1719 			 __func__, rsp_iov.iov_len,
1720 			 tcon->ses->server->vals->read_rsp_size);
1721 		rc = smb_EIO2(smb_eio_trace_read_rsp_short,
1722 			      rsp_iov.iov_len, tcon->ses->server->vals->read_rsp_size);
1723 		*nbytes = 0;
1724 	} else {
1725 		unsigned int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1726 		__u16 data_offset = le16_to_cpu(pSMBr->DataOffset);
1727 		data_length = data_length << 16;
1728 		data_length += le16_to_cpu(pSMBr->DataLength);
1729 		*nbytes = data_length;
1730 
1731 		/*check that DataLength would not go beyond end of SMB */
1732 		if ((data_length > CIFSMaxBufSize)
1733 				|| (data_length > count)) {
1734 			cifs_dbg(FYI, "%s: bad length %u for count %u\n",
1735 				 __func__, data_length, count);
1736 			rc = smb_EIO2(smb_eio_trace_read_overlarge,
1737 				      data_length, count);
1738 			*nbytes = 0;
1739 		} else if (data_offset < sizeof(*pSMBr) ||
1740 			   (size_t)data_offset + data_length > rsp_iov.iov_len) {
1741 			/* check that the data lies within the received response */
1742 			cifs_dbg(FYI, "%s: bad data offset %u length %u for response of %zu\n",
1743 				 __func__, data_offset, data_length, rsp_iov.iov_len);
1744 			rc = smb_EIO2(smb_eio_trace_read_bad_offset,
1745 				      data_offset, data_length);
1746 			*nbytes = 0;
1747 		} else {
1748 			pReadData = (char *) (&pSMBr->hdr.Protocol) + data_offset;
1749 /*			if (rc = copy_to_user(buf, pReadData, data_length)) {
1750 				cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1751 				rc = -EFAULT;
1752 			}*/ /* can not use copy_to_user when using page cache*/
1753 			if (*buf)
1754 				memcpy(*buf, pReadData, data_length);
1755 		}
1756 	}
1757 
1758 	if (*buf) {
1759 		free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1760 	} else if (resp_buf_type != CIFS_NO_BUFFER) {
1761 		/* return buffer to caller to free */
1762 		*buf = rsp_iov.iov_base;
1763 		if (resp_buf_type == CIFS_SMALL_BUFFER)
1764 			*pbuf_type = CIFS_SMALL_BUFFER;
1765 		else if (resp_buf_type == CIFS_LARGE_BUFFER)
1766 			*pbuf_type = CIFS_LARGE_BUFFER;
1767 	} /* else no valid buffer on return - leave as null */
1768 
1769 	/* Note: On -EAGAIN error only caller can retry on handle based calls
1770 		since file handle passed in no longer valid */
1771 	return rc;
1772 }
1773 
1774 
1775 int
1776 CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1777 	     unsigned int *nbytes, const char *buf)
1778 {
1779 	int rc = -EACCES;
1780 	WRITE_REQ *pSMB = NULL;
1781 	WRITE_RSP *pSMBr = NULL;
1782 	int bytes_returned, wct;
1783 	__u32 bytes_sent;
1784 	__u16 byte_count;
1785 	__u32 pid = io_parms->pid;
1786 	__u16 netfid = io_parms->netfid;
1787 	__u64 offset = io_parms->offset;
1788 	struct cifs_tcon *tcon = io_parms->tcon;
1789 	unsigned int count = io_parms->length, in_len;
1790 
1791 	*nbytes = 0;
1792 
1793 	if (tcon->ses == NULL)
1794 		return -ECONNABORTED;
1795 
1796 	if (tcon->ses->capabilities & CAP_LARGE_FILES)
1797 		wct = 14;
1798 	else {
1799 		wct = 12;
1800 		if ((offset >> 32) > 0) {
1801 			/* can not handle big offset for old srv */
1802 			return smb_EIO(smb_eio_trace_write_too_far);
1803 		}
1804 	}
1805 
1806 	rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1807 		      (void **) &pSMBr);
1808 	if (rc < 0)
1809 		return rc;
1810 	in_len = rc;
1811 
1812 	pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1813 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1814 
1815 	/* tcon and ses pointer are checked in smb_init */
1816 	if (!tcon->ses->server) {
1817 		cifs_buf_release(pSMB);
1818 		return -ECONNABORTED;
1819 	}
1820 
1821 	pSMB->AndXCommand = 0xFF;	/* none */
1822 	pSMB->Fid = netfid;
1823 	pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1824 	if (wct == 14)
1825 		pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1826 
1827 	pSMB->Reserved = 0xFFFFFFFF;
1828 	pSMB->WriteMode = 0;
1829 	pSMB->Remaining = 0;
1830 
1831 	/* Can increase buffer size if buffer is big enough in some cases ie we
1832 	can send more if LARGE_WRITE_X capability returned by the server and if
1833 	our buffer is big enough or if we convert to iovecs on socket writes
1834 	and eliminate the copy to the CIFS buffer */
1835 	if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1836 		bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1837 	} else {
1838 		bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1839 			 & ~0xFF;
1840 	}
1841 
1842 	if (bytes_sent > count)
1843 		bytes_sent = count;
1844 	pSMB->DataOffset =
1845 		cpu_to_le16(offsetof(struct smb_com_write_req, Data));
1846 	if (buf)
1847 		memcpy(pSMB->Data, buf, bytes_sent);
1848 	else if (count != 0) {
1849 		/* No buffer */
1850 		cifs_buf_release(pSMB);
1851 		return -EINVAL;
1852 	} /* else setting file size with write of zero bytes */
1853 	if (wct == 14)
1854 		byte_count = bytes_sent + 1; /* pad */
1855 	else /* wct == 12 */
1856 		byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1857 
1858 	pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1859 	pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1860 	in_len += byte_count;
1861 
1862 	if (wct == 14)
1863 		pSMB->ByteCount = cpu_to_le16(byte_count);
1864 	else { /* old style write has byte count 4 bytes earlier
1865 		  so 4 bytes pad  */
1866 		struct smb_com_writex_req *pSMBW =
1867 			(struct smb_com_writex_req *)pSMB;
1868 		pSMBW->ByteCount = cpu_to_le16(byte_count);
1869 	}
1870 
1871 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
1872 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1873 	cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1874 	if (rc) {
1875 		cifs_dbg(FYI, "Send error in write = %d\n", rc);
1876 	} else {
1877 		*nbytes = le16_to_cpu(pSMBr->CountHigh);
1878 		*nbytes = (*nbytes) << 16;
1879 		*nbytes += le16_to_cpu(pSMBr->Count);
1880 
1881 		/*
1882 		 * Mask off high 16 bits when bytes written as returned by the
1883 		 * server is greater than bytes requested by the client. Some
1884 		 * OS/2 servers are known to set incorrect CountHigh values.
1885 		 */
1886 		if (*nbytes > count)
1887 			*nbytes &= 0xFFFF;
1888 	}
1889 
1890 	cifs_buf_release(pSMB);
1891 
1892 	/* Note: On -EAGAIN error only caller can retry on handle based calls
1893 		since file handle passed in no longer valid */
1894 
1895 	return rc;
1896 }
1897 
1898 /*
1899  * Check the mid_state and signature on received buffer (if any), and queue the
1900  * workqueue completion task.
1901  */
1902 static void
1903 cifs_writev_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1904 {
1905 	struct cifs_io_subrequest *wdata = mid->callback_data;
1906 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
1907 	WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1908 	struct cifs_credits credits = {
1909 		.value = 1,
1910 		.instance = 0,
1911 		.rreq_debug_id = wdata->rreq->debug_id,
1912 		.rreq_debug_index = wdata->subreq.debug_index,
1913 	};
1914 	ssize_t result;
1915 	size_t written;
1916 
1917 	switch (mid->mid_state) {
1918 	case MID_RESPONSE_RECEIVED:
1919 		result = cifs_check_receive(mid, tcon->ses->server, 0);
1920 		if (result != 0)
1921 			break;
1922 
1923 		written = le16_to_cpu(smb->CountHigh);
1924 		written <<= 16;
1925 		written += le16_to_cpu(smb->Count);
1926 		/*
1927 		 * Mask off high 16 bits when bytes written as returned
1928 		 * by the server is greater than bytes requested by the
1929 		 * client. OS/2 servers are known to set incorrect
1930 		 * CountHigh values.
1931 		 */
1932 		if (written > wdata->subreq.len)
1933 			written &= 0xFFFF;
1934 
1935 		if (written < wdata->subreq.len) {
1936 			result = -ENOSPC;
1937 		} else {
1938 			result = written;
1939 			if (written > 0)
1940 				__set_bit(NETFS_SREQ_MADE_PROGRESS, &wdata->subreq.flags);
1941 		}
1942 		break;
1943 	case MID_REQUEST_SUBMITTED:
1944 		trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_req_submitted);
1945 		if (wdata->rreq->origin != NETFS_WRITEBACK)
1946 			__set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags);
1947 		result = -EAGAIN;
1948 		break;
1949 	case MID_RETRY_NEEDED:
1950 		trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_retry_needed);
1951 		if (wdata->rreq->origin != NETFS_WRITEBACK)
1952 			__set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags);
1953 		result = -EAGAIN;
1954 		break;
1955 	case MID_RESPONSE_MALFORMED:
1956 		trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_malformed);
1957 		result = smb_EIO(smb_eio_trace_write_rsp_malformed);
1958 		break;
1959 	default:
1960 		trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_unknown);
1961 		result = smb_EIO1(smb_eio_trace_write_mid_state_unknown,
1962 				  mid->mid_state);
1963 		break;
1964 	}
1965 
1966 	trace_smb3_rw_credits(credits.rreq_debug_id, credits.rreq_debug_index,
1967 			      wdata->credits.value,
1968 			      server->credits, server->in_flight,
1969 			      0, cifs_trace_rw_credits_write_response_clear);
1970 	wdata->credits.value = 0;
1971 	cifs_write_subrequest_terminated(wdata, result);
1972 	release_mid(server, mid);
1973 	trace_smb3_rw_credits(credits.rreq_debug_id, credits.rreq_debug_index, 0,
1974 			      server->credits, server->in_flight,
1975 			      credits.value, cifs_trace_rw_credits_write_response_add);
1976 	add_credits(tcon->ses->server, &credits, 0);
1977 }
1978 
1979 /* cifs_async_writev - send an async write, and set up mid to handle result */
1980 void
1981 cifs_async_writev(struct cifs_io_subrequest *wdata)
1982 {
1983 	int rc = -EACCES;
1984 	WRITE_REQ *req = NULL;
1985 	int wct;
1986 	struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
1987 	struct kvec iov[1];
1988 	struct smb_rqst rqst = { };
1989 	unsigned int in_len;
1990 
1991 	if (tcon->ses->capabilities & CAP_LARGE_FILES) {
1992 		wct = 14;
1993 	} else {
1994 		wct = 12;
1995 		if (wdata->subreq.start >> 32 > 0) {
1996 			/* can not handle big offset for old srv */
1997 			rc = smb_EIO(smb_eio_trace_write_too_far);
1998 			goto out;
1999 		}
2000 	}
2001 
2002 	rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&req);
2003 	if (rc < 0)
2004 		goto async_writev_out;
2005 	in_len = rc;
2006 
2007 	req->hdr.Pid = cpu_to_le16((__u16)wdata->req->pid);
2008 	req->hdr.PidHigh = cpu_to_le16((__u16)(wdata->req->pid >> 16));
2009 
2010 	req->AndXCommand = 0xFF;	/* none */
2011 	req->Fid = wdata->req->cfile->fid.netfid;
2012 	req->OffsetLow = cpu_to_le32(wdata->subreq.start & 0xFFFFFFFF);
2013 	if (wct == 14)
2014 		req->OffsetHigh = cpu_to_le32(wdata->subreq.start >> 32);
2015 	req->Reserved = 0xFFFFFFFF;
2016 	req->WriteMode = 0;
2017 	req->Remaining = 0;
2018 
2019 	req->DataOffset =
2020 	    cpu_to_le16(offsetof(struct smb_com_write_req, Data));
2021 
2022 	iov[0].iov_base = req;
2023 	iov[0].iov_len = in_len + 1; /* +1 for BCC */
2024 
2025 	rqst.rq_iov = iov;
2026 	rqst.rq_nvec = 1;
2027 	rqst.rq_iter = wdata->subreq.io_iter;
2028 
2029 	cifs_dbg(FYI, "async write at %llu %zu bytes\n",
2030 		 wdata->subreq.start, wdata->subreq.len);
2031 
2032 	req->DataLengthLow = cpu_to_le16(wdata->subreq.len & 0xFFFF);
2033 	req->DataLengthHigh = cpu_to_le16(wdata->subreq.len >> 16);
2034 
2035 	if (wct == 14) {
2036 		in_len += wdata->subreq.len + 1;
2037 		put_bcc(wdata->subreq.len + 1, &req->hdr);
2038 	} else {
2039 		/* wct == 12 */
2040 		struct smb_com_writex_req *reqw =
2041 				(struct smb_com_writex_req *)req;
2042 		in_len += wdata->subreq.len + 5;
2043 		put_bcc(wdata->subreq.len + 5, &reqw->hdr);
2044 		iov[0].iov_len += 4; /* pad bigger by four bytes */
2045 	}
2046 
2047 	rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2048 			     cifs_writev_callback, NULL, wdata, 0, NULL);
2049 	/* Can't touch wdata if rc == 0 */
2050 	if (rc == 0)
2051 		cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2052 
2053 async_writev_out:
2054 	cifs_small_buf_release(req);
2055 out:
2056 	if (rc) {
2057 		add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
2058 		cifs_write_subrequest_terminated(wdata, rc);
2059 	}
2060 }
2061 
2062 int
2063 CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
2064 	      unsigned int *nbytes, struct kvec *iov, int n_vec)
2065 {
2066 	int rc;
2067 	WRITE_REQ *pSMB = NULL;
2068 	int wct;
2069 	int smb_hdr_len;
2070 	int resp_buf_type = 0;
2071 	__u32 pid = io_parms->pid;
2072 	__u16 netfid = io_parms->netfid;
2073 	__u64 offset = io_parms->offset;
2074 	struct cifs_tcon *tcon = io_parms->tcon;
2075 	unsigned int count = io_parms->length;
2076 	struct kvec rsp_iov;
2077 	unsigned int in_len;
2078 
2079 	*nbytes = 0;
2080 
2081 	cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
2082 
2083 	if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2084 		wct = 14;
2085 	} else {
2086 		wct = 12;
2087 		if ((offset >> 32) > 0) {
2088 			/* can not handle big offset for old srv */
2089 			return smb_EIO(smb_eio_trace_write_too_far);
2090 		}
2091 	}
2092 	rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
2093 	if (rc < 0)
2094 		return rc;
2095 	in_len = rc;
2096 
2097 	pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2098 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2099 
2100 	/* tcon and ses pointer are checked in smb_init */
2101 	if (!tcon->ses->server) {
2102 		cifs_small_buf_release(pSMB);
2103 		return -ECONNABORTED;
2104 	}
2105 
2106 	pSMB->AndXCommand = 0xFF;	/* none */
2107 	pSMB->Fid = netfid;
2108 	pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2109 	if (wct == 14)
2110 		pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
2111 	pSMB->Reserved = 0xFFFFFFFF;
2112 	pSMB->WriteMode = 0;
2113 	pSMB->Remaining = 0;
2114 
2115 	pSMB->DataOffset =
2116 	    cpu_to_le16(offsetof(struct smb_com_write_req, Data));
2117 
2118 	pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2119 	pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2120 	/* header + 1 byte pad */
2121 	smb_hdr_len = in_len + 1;
2122 	if (wct == 14)
2123 		in_len += count + 1;
2124 	else /* wct == 12 */
2125 		in_len += count + 5; /* smb data starts later */
2126 	if (wct == 14)
2127 		pSMB->ByteCount = cpu_to_le16(count + 1);
2128 	else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2129 		struct smb_com_writex_req *pSMBW =
2130 				(struct smb_com_writex_req *)pSMB;
2131 		pSMBW->ByteCount = cpu_to_le16(count + 5);
2132 	}
2133 	iov[0].iov_base = pSMB;
2134 	if (wct == 14)
2135 		iov[0].iov_len = smb_hdr_len + 4;
2136 	else /* wct == 12 pad bigger by four bytes */
2137 		iov[0].iov_len = smb_hdr_len + 8;
2138 
2139 	rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2140 			  &rsp_iov);
2141 	cifs_small_buf_release(pSMB);
2142 	cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
2143 	if (rc) {
2144 		cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
2145 	} else if (resp_buf_type == 0) {
2146 		/* presumably this can not happen, but best to be safe */
2147 		rc = smb_EIO1(smb_eio_trace_write_bad_buf_type, resp_buf_type);
2148 	} else {
2149 		WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
2150 		*nbytes = le16_to_cpu(pSMBr->CountHigh);
2151 		*nbytes = (*nbytes) << 16;
2152 		*nbytes += le16_to_cpu(pSMBr->Count);
2153 
2154 		/*
2155 		 * Mask off high 16 bits when bytes written as returned by the
2156 		 * server is greater than bytes requested by the client. OS/2
2157 		 * servers are known to set incorrect CountHigh values.
2158 		 */
2159 		if (*nbytes > count)
2160 			*nbytes &= 0xFFFF;
2161 	}
2162 
2163 	free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2164 
2165 	/* Note: On -EAGAIN error only caller can retry on handle based calls
2166 		since file handle passed in no longer valid */
2167 
2168 	return rc;
2169 }
2170 
2171 int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2172 	       const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
2173 	       const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2174 {
2175 	int rc = 0;
2176 	LOCK_REQ *pSMB = NULL;
2177 	struct kvec iov[2];
2178 	struct kvec rsp_iov;
2179 	unsigned int in_len;
2180 	int resp_buf_type;
2181 	__u16 count;
2182 
2183 	cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2184 		 num_lock, num_unlock);
2185 
2186 	rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2187 	if (rc < 0)
2188 		return rc;
2189 	in_len = rc;
2190 
2191 	pSMB->Timeout = 0;
2192 	pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2193 	pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2194 	pSMB->LockType = lock_type;
2195 	pSMB->AndXCommand = 0xFF; /* none */
2196 	pSMB->Fid = netfid; /* netfid stays le */
2197 
2198 	count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2199 	in_len += count;
2200 	pSMB->ByteCount = cpu_to_le16(count);
2201 
2202 	iov[0].iov_base = (char *)pSMB;
2203 	iov[0].iov_len = in_len -
2204 			 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2205 	iov[1].iov_base = (char *)buf;
2206 	iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2207 
2208 	cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2209 	rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type,
2210 			  CIFS_NO_RSP_BUF, &rsp_iov);
2211 	cifs_small_buf_release(pSMB);
2212 	if (rc)
2213 		cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
2214 
2215 	return rc;
2216 }
2217 
2218 int
2219 CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
2220 	    const __u16 smb_file_id, const __u32 netpid, const __u64 len,
2221 	    const __u64 offset, const __u32 numUnlock,
2222 	    const __u32 numLock, const __u8 lockType,
2223 	    const bool waitFlag, const __u8 oplock_level)
2224 {
2225 	int rc = 0;
2226 	LOCK_REQ *pSMB = NULL;
2227 	unsigned int in_len;
2228 	int bytes_returned;
2229 	int flags = CIFS_WINDOWS_LOCK | CIFS_INTERRUPTIBLE_WAIT;
2230 	__u16 count;
2231 
2232 	cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2233 		 (int)waitFlag, numLock);
2234 	rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2235 
2236 	if (rc < 0)
2237 		return rc;
2238 	in_len = rc;
2239 
2240 	if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2241 		/* no response expected */
2242 		flags = CIFS_NO_SRV_RSP | CIFS_NON_BLOCKING | CIFS_OBREAK_OP;
2243 		pSMB->Timeout = 0;
2244 	} else if (waitFlag) {
2245 		flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2246 		pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2247 	} else {
2248 		pSMB->Timeout = 0;
2249 	}
2250 
2251 	pSMB->NumberOfLocks = cpu_to_le16(numLock);
2252 	pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2253 	pSMB->LockType = lockType;
2254 	pSMB->OplockLevel = oplock_level;
2255 	pSMB->AndXCommand = 0xFF;	/* none */
2256 	pSMB->Fid = smb_file_id; /* netfid stays le */
2257 
2258 	if ((numLock != 0) || (numUnlock != 0)) {
2259 		pSMB->Locks[0].Pid = cpu_to_le16(netpid);
2260 		/* BB where to store pid high? */
2261 		pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2262 		pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2263 		pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2264 		pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2265 		count = sizeof(LOCKING_ANDX_RANGE);
2266 	} else {
2267 		/* oplock break */
2268 		count = 0;
2269 	}
2270 	in_len += count;
2271 	pSMB->ByteCount = cpu_to_le16(count);
2272 
2273 	if (waitFlag)
2274 		rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2275 				 (struct smb_hdr *) pSMB, &bytes_returned,
2276 				 flags);
2277 	else
2278 		rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, in_len, flags);
2279 	cifs_small_buf_release(pSMB);
2280 	cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2281 	if (rc)
2282 		cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
2283 
2284 	/* Note: On -EAGAIN error only caller can retry on handle based calls
2285 	since file handle passed in no longer valid */
2286 	return rc;
2287 }
2288 
2289 int
2290 CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
2291 		const __u16 smb_file_id, const __u32 netpid,
2292 		const loff_t start_offset, const __u64 len,
2293 		struct file_lock *pLockData, const __u16 lock_type,
2294 		const bool waitFlag)
2295 {
2296 	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
2297 	struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2298 	struct cifs_posix_lock *parm_data;
2299 	unsigned int in_len;
2300 	int rc = 0;
2301 	int sr_flags = CIFS_INTERRUPTIBLE_WAIT;
2302 	int bytes_returned = 0;
2303 	int resp_buf_type = 0;
2304 	__u16 params, param_offset, offset, byte_count, count;
2305 	struct kvec iov[1];
2306 	struct kvec rsp_iov;
2307 
2308 	cifs_dbg(FYI, "Posix Lock\n");
2309 
2310 	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2311 	if (rc < 0)
2312 		return rc;
2313 	in_len = rc;
2314 
2315 	pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2316 
2317 	params = 6;
2318 	pSMB->MaxSetupCount = 0;
2319 	pSMB->Reserved = 0;
2320 	pSMB->Flags = 0;
2321 	pSMB->Reserved2 = 0;
2322 	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid);
2323 	offset = param_offset + params;
2324 
2325 	count = sizeof(struct cifs_posix_lock);
2326 	pSMB->MaxParameterCount = cpu_to_le16(2);
2327 	pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2328 	pSMB->SetupCount = 1;
2329 	pSMB->Reserved3 = 0;
2330 	if (pLockData)
2331 		pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2332 	else
2333 		pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2334 	byte_count = 3 /* pad */  + params + count;
2335 	pSMB->DataCount = cpu_to_le16(count);
2336 	pSMB->ParameterCount = cpu_to_le16(params);
2337 	pSMB->TotalDataCount = pSMB->DataCount;
2338 	pSMB->TotalParameterCount = pSMB->ParameterCount;
2339 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
2340 	parm_data = (struct cifs_posix_lock *)(((char *)pSMB) + offset);
2341 
2342 	parm_data->lock_type = cpu_to_le16(lock_type);
2343 	if (waitFlag) {
2344 		sr_flags |= CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2345 		parm_data->lock_flags = cpu_to_le16(1);
2346 		pSMB->Timeout = cpu_to_le32(-1);
2347 	} else
2348 		pSMB->Timeout = 0;
2349 
2350 	parm_data->pid = cpu_to_le32(netpid);
2351 	parm_data->start = cpu_to_le64(start_offset);
2352 	parm_data->length = cpu_to_le64(len);  /* normalize negative numbers */
2353 
2354 	pSMB->DataOffset = cpu_to_le16(offset);
2355 	pSMB->Fid = smb_file_id;
2356 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2357 	pSMB->Reserved4 = 0;
2358 	in_len += byte_count;
2359 	pSMB->ByteCount = cpu_to_le16(byte_count);
2360 	if (waitFlag) {
2361 		rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2362 				 (struct smb_hdr *) pSMBr, &bytes_returned,
2363 				 sr_flags);
2364 	} else {
2365 		iov[0].iov_base = (char *)pSMB;
2366 		iov[0].iov_len = in_len;
2367 		rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2368 				&resp_buf_type, sr_flags, &rsp_iov);
2369 		pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
2370 	}
2371 	cifs_small_buf_release(pSMB);
2372 
2373 	if (rc) {
2374 		cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
2375 	} else if (pLockData) {
2376 		/* lock structure can be returned on get */
2377 		unsigned int rsp_len;
2378 		__u16 data_offset;
2379 		__u16 data_count;
2380 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2381 
2382 		if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2383 			rc = smb_EIO2(smb_eio_trace_lock_bcc_too_small,
2384 				      get_bcc(&pSMBr->hdr), sizeof(*parm_data));
2385 			goto plk_err_exit;
2386 		}
2387 		data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2388 		data_count  = le16_to_cpu(pSMBr->t2.DataCount);
2389 		rsp_len = smbCalcSize(&pSMBr->hdr);
2390 		if (data_count < sizeof(struct cifs_posix_lock)) {
2391 			rc = smb_EIO2(smb_eio_trace_lock_data_too_small,
2392 				      data_count, sizeof(struct cifs_posix_lock));
2393 			goto plk_err_exit;
2394 		}
2395 		if (data_offset > rsp_len ||
2396 		    data_count > rsp_len - data_offset) {
2397 			rc = smb_EIO2(smb_eio_trace_lock_data_area,
2398 				      data_offset, data_count);
2399 			goto plk_err_exit;
2400 		}
2401 		parm_data = (struct cifs_posix_lock *)
2402 			((char *)&pSMBr->hdr.Protocol + data_offset);
2403 		if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
2404 			pLockData->c.flc_type = F_UNLCK;
2405 		else {
2406 			if (parm_data->lock_type ==
2407 					cpu_to_le16(CIFS_RDLCK))
2408 				pLockData->c.flc_type = F_RDLCK;
2409 			else if (parm_data->lock_type ==
2410 					cpu_to_le16(CIFS_WRLCK))
2411 				pLockData->c.flc_type = F_WRLCK;
2412 
2413 			pLockData->fl_start = le64_to_cpu(parm_data->start);
2414 			pLockData->fl_end = pLockData->fl_start +
2415 				(le64_to_cpu(parm_data->length) ?
2416 				 le64_to_cpu(parm_data->length) - 1 : 0);
2417 			pLockData->c.flc_pid = -le32_to_cpu(parm_data->pid);
2418 		}
2419 	}
2420 
2421 plk_err_exit:
2422 	free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
2423 
2424 	/* Note: On -EAGAIN error only caller can retry on handle based calls
2425 	   since file handle passed in no longer valid */
2426 
2427 	return rc;
2428 }
2429 
2430 
2431 int
2432 CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2433 {
2434 	int rc = 0;
2435 	CLOSE_REQ *pSMB = NULL;
2436 	unsigned int in_len;
2437 
2438 	cifs_dbg(FYI, "In CIFSSMBClose\n");
2439 
2440 /* do not retry on dead session on close */
2441 	rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2442 	if (rc == -EAGAIN)
2443 		return 0;
2444 	if (rc < 0)
2445 		return rc;
2446 	in_len = rc;
2447 
2448 	pSMB->FileID = (__u16) smb_file_id;
2449 	pSMB->LastWriteTime = 0xFFFFFFFF;
2450 	pSMB->ByteCount = 0;
2451 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
2452 	cifs_small_buf_release(pSMB);
2453 	cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
2454 	if (rc) {
2455 		if (rc != -EINTR) {
2456 			/* EINTR is expected when user ctl-c to kill app */
2457 			cifs_dbg(VFS, "Send error in Close = %d\n", rc);
2458 		}
2459 	}
2460 
2461 	/* Since session is dead, file will be closed on server already */
2462 	if (rc == -EAGAIN)
2463 		rc = 0;
2464 
2465 	return rc;
2466 }
2467 
2468 int
2469 CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2470 {
2471 	int rc = 0;
2472 	FLUSH_REQ *pSMB = NULL;
2473 	unsigned int in_len;
2474 
2475 	cifs_dbg(FYI, "In CIFSSMBFlush\n");
2476 
2477 	rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2478 	if (rc < 0)
2479 		return rc;
2480 	in_len = rc;
2481 
2482 	pSMB->FileID = (__u16) smb_file_id;
2483 	pSMB->ByteCount = 0;
2484 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
2485 	cifs_small_buf_release(pSMB);
2486 	cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2487 	if (rc)
2488 		cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
2489 
2490 	return rc;
2491 }
2492 
2493 int CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2494 		  struct dentry *source_dentry,
2495 		  const char *from_name, const char *to_name,
2496 		  struct cifs_sb_info *cifs_sb)
2497 {
2498 	int rc = 0;
2499 	RENAME_REQ *pSMB = NULL;
2500 	RENAME_RSP *pSMBr = NULL;
2501 	unsigned int in_len;
2502 	int bytes_returned;
2503 	int name_len, name_len2;
2504 	__u16 count;
2505 	int remap = cifs_remap(cifs_sb);
2506 
2507 	cifs_dbg(FYI, "In CIFSSMBRename\n");
2508 renameRetry:
2509 	rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2510 		      (void **) &pSMBr);
2511 	if (rc < 0)
2512 		return rc;
2513 	in_len = rc;
2514 
2515 	pSMB->BufferFormat = 0x04;
2516 	pSMB->SearchAttributes =
2517 	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2518 			ATTR_DIRECTORY);
2519 
2520 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2521 		name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2522 					      from_name, PATH_MAX,
2523 					      cifs_sb->local_nls, remap);
2524 		name_len++;	/* trailing null */
2525 		name_len *= 2;
2526 		pSMB->OldFileName[name_len] = 0x04;	/* pad */
2527 	/* protocol requires ASCII signature byte on Unicode string */
2528 		pSMB->OldFileName[name_len + 1] = 0x00;
2529 		name_len2 =
2530 		    cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2531 				       to_name, PATH_MAX, cifs_sb->local_nls,
2532 				       remap);
2533 		name_len2 += 1 /* trailing null */  + 1 /* Signature word */ ;
2534 		name_len2 *= 2;	/* convert to bytes */
2535 	} else {
2536 		name_len = copy_path_name(pSMB->OldFileName, from_name);
2537 		name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
2538 		pSMB->OldFileName[name_len] = 0x04;  /* 2nd buffer format */
2539 		name_len2++;	/* signature byte */
2540 	}
2541 
2542 	count = 1 /* 1st signature byte */  + name_len + name_len2;
2543 	in_len += count;
2544 	pSMB->ByteCount = cpu_to_le16(count);
2545 
2546 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2547 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2548 	cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2549 	if (rc)
2550 		cifs_dbg(FYI, "Send error in rename = %d\n", rc);
2551 
2552 	cifs_buf_release(pSMB);
2553 
2554 	if (rc == -EAGAIN)
2555 		goto renameRetry;
2556 
2557 	return rc;
2558 }
2559 
2560 int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2561 			  int netfid, const char *target_name, int remap)
2562 {
2563 	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
2564 	struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2565 	struct set_file_rename *rename_info;
2566 	unsigned int in_len;
2567 	char *data_offset;
2568 	char dummy_string[30];
2569 	int rc = 0;
2570 	int bytes_returned = 0;
2571 	int len_of_str;
2572 	__u16 params, param_offset, offset, count, byte_count;
2573 
2574 	cifs_dbg(FYI, "Rename to File by handle\n");
2575 	rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2576 			(void **) &pSMBr);
2577 	if (rc < 0)
2578 		return rc;
2579 	in_len = rc;
2580 
2581 	params = 6;
2582 	pSMB->MaxSetupCount = 0;
2583 	pSMB->Reserved = 0;
2584 	pSMB->Flags = 0;
2585 	pSMB->Timeout = 0;
2586 	pSMB->Reserved2 = 0;
2587 	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid);
2588 	offset = param_offset + params;
2589 
2590 	data_offset = (char *)(pSMB) + offset;
2591 	rename_info = (struct set_file_rename *) data_offset;
2592 	pSMB->MaxParameterCount = cpu_to_le16(2);
2593 	pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2594 	pSMB->SetupCount = 1;
2595 	pSMB->Reserved3 = 0;
2596 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2597 	byte_count = 3 /* pad */  + params;
2598 	pSMB->ParameterCount = cpu_to_le16(params);
2599 	pSMB->TotalParameterCount = pSMB->ParameterCount;
2600 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
2601 	pSMB->DataOffset = cpu_to_le16(offset);
2602 	/* construct random name ".cifs_tmp<inodenum><mid>" */
2603 	rename_info->overwrite = cpu_to_le32(1);
2604 	rename_info->root_fid  = 0;
2605 	/* unicode only call */
2606 	if (target_name == NULL) {
2607 		sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2608 		len_of_str =
2609 			cifsConvertToUTF16((__le16 *)rename_info->target_name,
2610 					dummy_string, 24,
2611 					pTcon->ses->local_nls, remap);
2612 	} else {
2613 		len_of_str =
2614 			cifsConvertToUTF16((__le16 *)rename_info->target_name,
2615 					target_name, PATH_MAX,
2616 					pTcon->ses->local_nls, remap);
2617 	}
2618 	rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2619 	count = sizeof(struct set_file_rename) + (2 * len_of_str);
2620 	byte_count += count;
2621 	pSMB->DataCount = cpu_to_le16(count);
2622 	pSMB->TotalDataCount = pSMB->DataCount;
2623 	pSMB->Fid = netfid;
2624 	pSMB->InformationLevel =
2625 		cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2626 	pSMB->Reserved4 = 0;
2627 	in_len += byte_count;
2628 	pSMB->ByteCount = cpu_to_le16(byte_count);
2629 	rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB, in_len,
2630 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2631 	cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2632 	if (rc)
2633 		cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2634 			 rc);
2635 
2636 	cifs_buf_release(pSMB);
2637 
2638 	/* Note: On -EAGAIN error only caller can retry on handle based calls
2639 		since file handle passed in no longer valid */
2640 
2641 	return rc;
2642 }
2643 
2644 int
2645 CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
2646 		      const char *fromName, const char *toName,
2647 		      const struct nls_table *nls_codepage, int remap)
2648 {
2649 	TRANSACTION2_SPI_REQ *pSMB = NULL;
2650 	TRANSACTION2_SPI_RSP *pSMBr = NULL;
2651 	unsigned int in_len;
2652 	char *data_offset;
2653 	int name_len;
2654 	int name_len_target;
2655 	int rc = 0;
2656 	int bytes_returned = 0;
2657 	__u16 params, param_offset, offset, byte_count;
2658 
2659 	cifs_dbg(FYI, "In Symlink Unix style\n");
2660 createSymLinkRetry:
2661 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2662 		      (void **) &pSMBr);
2663 	if (rc < 0)
2664 		return rc;
2665 	in_len = rc;
2666 
2667 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2668 		name_len =
2669 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2670 				/* find define for this maxpathcomponent */
2671 					PATH_MAX, nls_codepage, remap);
2672 		name_len++;	/* trailing null */
2673 		name_len *= 2;
2674 
2675 	} else {
2676 		name_len = copy_path_name(pSMB->FileName, fromName);
2677 	}
2678 	params = 6 + name_len;
2679 	pSMB->MaxSetupCount = 0;
2680 	pSMB->Reserved = 0;
2681 	pSMB->Flags = 0;
2682 	pSMB->Timeout = 0;
2683 	pSMB->Reserved2 = 0;
2684 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
2685 				InformationLevel);
2686 	offset = param_offset + params;
2687 
2688 	data_offset = (char *)pSMB + offset;
2689 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2690 		name_len_target =
2691 		    cifsConvertToUTF16((__le16 *) data_offset, toName,
2692 				/* find define for this maxpathcomponent */
2693 					PATH_MAX, nls_codepage, remap);
2694 		name_len_target++;	/* trailing null */
2695 		name_len_target *= 2;
2696 	} else {
2697 		name_len_target = copy_path_name(data_offset, toName);
2698 	}
2699 
2700 	pSMB->MaxParameterCount = cpu_to_le16(2);
2701 	/* BB find exact max on data count below from sess */
2702 	pSMB->MaxDataCount = cpu_to_le16(1000);
2703 	pSMB->SetupCount = 1;
2704 	pSMB->Reserved3 = 0;
2705 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2706 	byte_count = 3 /* pad */  + params + name_len_target;
2707 	pSMB->DataCount = cpu_to_le16(name_len_target);
2708 	pSMB->ParameterCount = cpu_to_le16(params);
2709 	pSMB->TotalDataCount = pSMB->DataCount;
2710 	pSMB->TotalParameterCount = pSMB->ParameterCount;
2711 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
2712 	pSMB->DataOffset = cpu_to_le16(offset);
2713 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2714 	pSMB->Reserved4 = 0;
2715 	in_len += byte_count;
2716 	pSMB->ByteCount = cpu_to_le16(byte_count);
2717 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2718 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2719 	cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2720 	if (rc)
2721 		cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2722 			 rc);
2723 
2724 	cifs_buf_release(pSMB);
2725 
2726 	if (rc == -EAGAIN)
2727 		goto createSymLinkRetry;
2728 
2729 	return rc;
2730 }
2731 
2732 int
2733 CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2734 		       const char *fromName, const char *toName,
2735 		       const struct nls_table *nls_codepage, int remap)
2736 {
2737 	TRANSACTION2_SPI_REQ *pSMB = NULL;
2738 	TRANSACTION2_SPI_RSP *pSMBr = NULL;
2739 	unsigned int in_len;
2740 	char *data_offset;
2741 	int name_len;
2742 	int name_len_target;
2743 	int rc = 0;
2744 	int bytes_returned = 0;
2745 	__u16 params, param_offset, offset, byte_count;
2746 
2747 	cifs_dbg(FYI, "In Create Hard link Unix style\n");
2748 createHardLinkRetry:
2749 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2750 		      (void **) &pSMBr);
2751 	if (rc < 0)
2752 		return rc;
2753 	in_len = rc;
2754 
2755 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2756 		name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2757 					      PATH_MAX, nls_codepage, remap);
2758 		name_len++;	/* trailing null */
2759 		name_len *= 2;
2760 
2761 	} else {
2762 		name_len = copy_path_name(pSMB->FileName, toName);
2763 	}
2764 	params = 6 + name_len;
2765 	pSMB->MaxSetupCount = 0;
2766 	pSMB->Reserved = 0;
2767 	pSMB->Flags = 0;
2768 	pSMB->Timeout = 0;
2769 	pSMB->Reserved2 = 0;
2770 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
2771 				InformationLevel);
2772 	offset = param_offset + params;
2773 
2774 	data_offset = (char *)pSMB + offset;
2775 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2776 		name_len_target =
2777 		    cifsConvertToUTF16((__le16 *) data_offset, fromName,
2778 				       PATH_MAX, nls_codepage, remap);
2779 		name_len_target++;	/* trailing null */
2780 		name_len_target *= 2;
2781 	} else {
2782 		name_len_target = copy_path_name(data_offset, fromName);
2783 	}
2784 
2785 	pSMB->MaxParameterCount = cpu_to_le16(2);
2786 	/* BB find exact max on data count below from sess*/
2787 	pSMB->MaxDataCount = cpu_to_le16(1000);
2788 	pSMB->SetupCount = 1;
2789 	pSMB->Reserved3 = 0;
2790 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2791 	byte_count = 3 /* pad */  + params + name_len_target;
2792 	pSMB->ParameterCount = cpu_to_le16(params);
2793 	pSMB->TotalParameterCount = pSMB->ParameterCount;
2794 	pSMB->DataCount = cpu_to_le16(name_len_target);
2795 	pSMB->TotalDataCount = pSMB->DataCount;
2796 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
2797 	pSMB->DataOffset = cpu_to_le16(offset);
2798 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2799 	pSMB->Reserved4 = 0;
2800 	in_len += byte_count;
2801 	pSMB->ByteCount = cpu_to_le16(byte_count);
2802 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2803 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2804 	cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2805 	if (rc)
2806 		cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2807 			 rc);
2808 
2809 	cifs_buf_release(pSMB);
2810 	if (rc == -EAGAIN)
2811 		goto createHardLinkRetry;
2812 
2813 	return rc;
2814 }
2815 
2816 int CIFSCreateHardLink(const unsigned int xid,
2817 		       struct cifs_tcon *tcon,
2818 		       struct dentry *source_dentry,
2819 		       const char *from_name, const char *to_name,
2820 		       struct cifs_sb_info *cifs_sb)
2821 {
2822 	int rc = 0;
2823 	NT_RENAME_REQ *pSMB = NULL;
2824 	RENAME_RSP *pSMBr = NULL;
2825 	unsigned int in_len;
2826 	int bytes_returned;
2827 	int name_len, name_len2;
2828 	__u16 count;
2829 	int remap = cifs_remap(cifs_sb);
2830 
2831 	cifs_dbg(FYI, "In CIFSCreateHardLink\n");
2832 winCreateHardLinkRetry:
2833 
2834 	rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2835 		      (void **) &pSMBr);
2836 	if (rc < 0)
2837 		return rc;
2838 	in_len = rc;
2839 
2840 	pSMB->SearchAttributes =
2841 	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2842 			ATTR_DIRECTORY);
2843 	pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2844 	pSMB->ClusterCount = 0;
2845 
2846 	pSMB->BufferFormat = 0x04;
2847 
2848 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2849 		name_len =
2850 		    cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2851 				       PATH_MAX, cifs_sb->local_nls, remap);
2852 		name_len++;	/* trailing null */
2853 		name_len *= 2;
2854 
2855 		/* protocol specifies ASCII buffer format (0x04) for unicode */
2856 		pSMB->OldFileName[name_len] = 0x04;
2857 		pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
2858 		name_len2 =
2859 		    cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2860 				       to_name, PATH_MAX, cifs_sb->local_nls,
2861 				       remap);
2862 		name_len2 += 1 /* trailing null */  + 1 /* Signature word */ ;
2863 		name_len2 *= 2;	/* convert to bytes */
2864 	} else {
2865 		name_len = copy_path_name(pSMB->OldFileName, from_name);
2866 		pSMB->OldFileName[name_len] = 0x04;	/* 2nd buffer format */
2867 		name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
2868 		name_len2++;	/* signature byte */
2869 	}
2870 
2871 	count = 1 /* string type byte */  + name_len + name_len2;
2872 	in_len += count;
2873 	pSMB->ByteCount = cpu_to_le16(count);
2874 
2875 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2876 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2877 	cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2878 	if (rc)
2879 		cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
2880 
2881 	cifs_buf_release(pSMB);
2882 	if (rc == -EAGAIN)
2883 		goto winCreateHardLinkRetry;
2884 
2885 	return rc;
2886 }
2887 
2888 int
2889 CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
2890 			const unsigned char *searchName, char **symlinkinfo,
2891 			const struct nls_table *nls_codepage, int remap)
2892 {
2893 /* SMB_QUERY_FILE_UNIX_LINK */
2894 	TRANSACTION2_QPI_REQ *pSMB = NULL;
2895 	TRANSACTION2_QPI_RSP *pSMBr = NULL;
2896 	unsigned int in_len;
2897 	int rc = 0;
2898 	int bytes_returned;
2899 	int name_len;
2900 	__u16 params, byte_count;
2901 	char *data_start;
2902 
2903 	cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
2904 
2905 querySymLinkRetry:
2906 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2907 		      (void **) &pSMBr);
2908 	if (rc < 0)
2909 		return rc;
2910 	in_len = rc;
2911 
2912 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2913 		name_len =
2914 			cifsConvertToUTF16((__le16 *) pSMB->FileName,
2915 					   searchName, PATH_MAX, nls_codepage,
2916 					   remap);
2917 		name_len++;	/* trailing null */
2918 		name_len *= 2;
2919 	} else {
2920 		name_len = copy_path_name(pSMB->FileName, searchName);
2921 	}
2922 
2923 	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
2924 	pSMB->TotalDataCount = 0;
2925 	pSMB->MaxParameterCount = cpu_to_le16(2);
2926 	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
2927 	pSMB->MaxSetupCount = 0;
2928 	pSMB->Reserved = 0;
2929 	pSMB->Flags = 0;
2930 	pSMB->Timeout = 0;
2931 	pSMB->Reserved2 = 0;
2932 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
2933 		struct smb_com_transaction2_qpi_req, InformationLevel));
2934 	pSMB->DataCount = 0;
2935 	pSMB->DataOffset = 0;
2936 	pSMB->SetupCount = 1;
2937 	pSMB->Reserved3 = 0;
2938 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2939 	byte_count = params + 1 /* pad */ ;
2940 	pSMB->TotalParameterCount = cpu_to_le16(params);
2941 	pSMB->ParameterCount = pSMB->TotalParameterCount;
2942 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
2943 	pSMB->Reserved4 = 0;
2944 	in_len += byte_count;
2945 	pSMB->ByteCount = cpu_to_le16(byte_count);
2946 
2947 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
2948 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2949 	if (rc) {
2950 		cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
2951 	} else {
2952 		/* decode response */
2953 
2954 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2955 		/* BB also check enough total bytes returned */
2956 		if (rc || get_bcc(&pSMBr->hdr) < 2)
2957 			rc = smb_EIO2(smb_eio_trace_qsym_bcc_too_small,
2958 				      get_bcc(&pSMBr->hdr), 2);
2959 		else {
2960 			bool is_unicode;
2961 			u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2962 
2963 			data_start = ((char *) &pSMBr->hdr.Protocol) +
2964 					   le16_to_cpu(pSMBr->t2.DataOffset);
2965 
2966 			if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
2967 				is_unicode = true;
2968 			else
2969 				is_unicode = false;
2970 
2971 			/* BB FIXME investigate remapping reserved chars here */
2972 			*symlinkinfo = cifs_strndup_from_utf16(data_start,
2973 					count, is_unicode, nls_codepage);
2974 			if (!*symlinkinfo)
2975 				rc = -ENOMEM;
2976 		}
2977 	}
2978 	cifs_buf_release(pSMB);
2979 	if (rc == -EAGAIN)
2980 		goto querySymLinkRetry;
2981 	return rc;
2982 }
2983 
2984 int cifs_query_reparse_point(const unsigned int xid,
2985 			     struct cifs_tcon *tcon,
2986 			     struct cifs_sb_info *cifs_sb,
2987 			     const char *full_path,
2988 			     u32 *tag, struct kvec *rsp,
2989 			     int *rsp_buftype)
2990 {
2991 	struct reparse_data_buffer *buf;
2992 	struct cifs_open_parms oparms;
2993 	TRANSACT_IOCTL_REQ *io_req = NULL;
2994 	TRANSACT_IOCTL_RSP *io_rsp = NULL;
2995 	struct cifs_fid fid;
2996 	unsigned int in_len;
2997 	__u32 data_offset, data_count, len;
2998 	__u8 *start, *end;
2999 	int io_rsp_len;
3000 	int oplock = 0;
3001 	int rc;
3002 
3003 	cifs_tcon_dbg(FYI, "%s: path=%s\n", __func__, full_path);
3004 
3005 	if (cap_unix(tcon->ses))
3006 		return -EOPNOTSUPP;
3007 
3008 	if (!CIFS_REPARSE_SUPPORT(tcon))
3009 		return -EOPNOTSUPP;
3010 
3011 	oparms = (struct cifs_open_parms) {
3012 		.tcon = tcon,
3013 		.cifs_sb = cifs_sb,
3014 		.desired_access = FILE_READ_ATTRIBUTES,
3015 		.create_options = cifs_create_options(cifs_sb,
3016 						      OPEN_REPARSE_POINT),
3017 		.disposition = FILE_OPEN,
3018 		.path = full_path,
3019 		.fid = &fid,
3020 	};
3021 
3022 	rc = CIFS_open(xid, &oparms, &oplock, NULL);
3023 	if (rc)
3024 		return rc;
3025 
3026 	rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon,
3027 		      (void **)&io_req, (void **)&io_rsp);
3028 	if (rc < 0)
3029 		goto error;
3030 	in_len = rc;
3031 
3032 	io_req->TotalParameterCount = 0;
3033 	io_req->TotalDataCount = 0;
3034 	io_req->MaxParameterCount = cpu_to_le32(0);
3035 	/* BB find exact data count max from sess structure BB */
3036 	io_req->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3037 	io_req->MaxSetupCount = 1;
3038 	io_req->Reserved = 0;
3039 	io_req->ParameterOffset = 0;
3040 	io_req->DataCount = 0;
3041 	io_req->DataOffset = 0;
3042 	io_req->SetupCount = 4;
3043 	io_req->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3044 	io_req->ParameterCount = io_req->TotalParameterCount;
3045 	io_req->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3046 	io_req->IsFsctl = 1;
3047 	io_req->IsRootFlag = 0;
3048 	io_req->Fid = fid.netfid;
3049 	io_req->ByteCount = 0;
3050 
3051 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)io_req, in_len,
3052 			 (struct smb_hdr *)io_rsp, &io_rsp_len, 0);
3053 	if (rc)
3054 		goto error;
3055 
3056 	data_offset = le32_to_cpu(io_rsp->DataOffset);
3057 	data_count = le32_to_cpu(io_rsp->DataCount);
3058 	if (get_bcc(&io_rsp->hdr) < 2 || data_offset > 512 ||
3059 	    !data_count || data_count > 2048) {
3060 		rc = smb_EIO2(smb_eio_trace_qreparse_sizes_wrong,
3061 			      get_bcc(&io_rsp->hdr), data_count);
3062 		goto error;
3063 	}
3064 
3065 	/* SetupCount must be 1, otherwise offset to ByteCount is incorrect. */
3066 	if (io_rsp->SetupCount != 1) {
3067 		rc = smb_EIO2(smb_eio_trace_qreparse_setup_count,
3068 			      io_rsp->SetupCount, 1);
3069 		goto error;
3070 	}
3071 
3072 	/*
3073 	 * ReturnedDataLen is output length of executed IOCTL.
3074 	 * DataCount is output length transferred over network.
3075 	 * Check that we have full FSCTL_GET_REPARSE_POINT buffer.
3076 	 */
3077 	if (data_count != le16_to_cpu(io_rsp->ReturnedDataLen)) {
3078 		rc = smb_EIO2(smb_eio_trace_qreparse_ret_datalen,
3079 			      data_count, le16_to_cpu(io_rsp->ReturnedDataLen));
3080 		goto error;
3081 	}
3082 
3083 	end = 2 + get_bcc(&io_rsp->hdr) + (__u8 *)&io_rsp->ByteCount;
3084 	start = (__u8 *)&io_rsp->hdr.Protocol + data_offset;
3085 	if (start >= end || (size_t)(end - start) < sizeof(*buf)) {
3086 		rc = smb_EIO2(smb_eio_trace_qreparse_data_area,
3087 			      (unsigned long)start - (unsigned long)io_rsp,
3088 			      (unsigned long)end - (unsigned long)io_rsp);
3089 		goto error;
3090 	}
3091 
3092 	data_count = le16_to_cpu(io_rsp->ByteCount);
3093 	buf = (struct reparse_data_buffer *)start;
3094 	len = sizeof(*buf);
3095 	if (data_count < len ||
3096 	    data_count < le16_to_cpu(buf->ReparseDataLength) + len) {
3097 		rc = smb_EIO2(smb_eio_trace_qreparse_rep_datalen,
3098 			      data_count, le16_to_cpu(buf->ReparseDataLength) + len);
3099 		goto error;
3100 	}
3101 
3102 	*tag = le32_to_cpu(buf->ReparseTag);
3103 	rsp->iov_base = io_rsp;
3104 	rsp->iov_len = io_rsp_len;
3105 	*rsp_buftype = CIFS_LARGE_BUFFER;
3106 	CIFSSMBClose(xid, tcon, fid.netfid);
3107 	return 0;
3108 
3109 error:
3110 	cifs_buf_release(io_req);
3111 	CIFSSMBClose(xid, tcon, fid.netfid);
3112 	return rc;
3113 }
3114 
3115 struct inode *cifs_create_reparse_inode(struct cifs_open_info_data *data,
3116 					struct super_block *sb,
3117 					const unsigned int xid,
3118 					struct cifs_tcon *tcon,
3119 					const char *full_path,
3120 					bool directory,
3121 					struct kvec *reparse_iov,
3122 					struct kvec *xattr_iov)
3123 {
3124 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
3125 	struct cifs_open_parms oparms;
3126 	TRANSACT_IOCTL_REQ *io_req;
3127 	struct inode *new = NULL;
3128 	struct kvec in_iov[2];
3129 	struct kvec out_iov;
3130 	struct cifs_fid fid;
3131 	unsigned int in_len;
3132 	int oplock = 0;
3133 	int buf_type = 0;
3134 	int rc;
3135 
3136 	cifs_tcon_dbg(FYI, "%s: path=%s\n", __func__, full_path);
3137 
3138 	/*
3139 	 * If server filesystem does not support reparse points then do not
3140 	 * attempt to create reparse point. This will prevent creating unusable
3141 	 * empty object on the server.
3142 	 */
3143 	if (!CIFS_REPARSE_SUPPORT(tcon))
3144 		return ERR_PTR(-EOPNOTSUPP);
3145 
3146 #ifndef CONFIG_CIFS_XATTR
3147 	if (xattr_iov)
3148 		return ERR_PTR(-EOPNOTSUPP);
3149 #endif
3150 
3151 	oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
3152 			     FILE_READ_ATTRIBUTES | FILE_WRITE_DATA | FILE_WRITE_EA,
3153 			     FILE_CREATE,
3154 			     (directory ? CREATE_NOT_FILE : CREATE_NOT_DIR) | OPEN_REPARSE_POINT,
3155 			     ACL_NO_MODE);
3156 	oparms.fid = &fid;
3157 
3158 	rc = CIFS_open(xid, &oparms, &oplock, NULL);
3159 	if (rc)
3160 		return ERR_PTR(rc);
3161 
3162 #ifdef CONFIG_CIFS_XATTR
3163 	if (xattr_iov) {
3164 		struct smb2_file_full_ea_info *ea;
3165 
3166 		ea = &((struct smb2_create_ea_ctx *)xattr_iov->iov_base)->ea;
3167 		while (1) {
3168 			rc = CIFSSMBSetEA(xid,
3169 					  tcon,
3170 					  full_path,
3171 					  &ea->ea_data[0],
3172 					  &ea->ea_data[ea->ea_name_length+1],
3173 					  le16_to_cpu(ea->ea_value_length),
3174 					  cifs_sb);
3175 			if (rc)
3176 				goto out_close;
3177 			if (le32_to_cpu(ea->next_entry_offset) == 0)
3178 				break;
3179 			ea = (struct smb2_file_full_ea_info *)((u8 *)ea +
3180 				le32_to_cpu(ea->next_entry_offset));
3181 		}
3182 	}
3183 #endif
3184 
3185 	rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **)&io_req, NULL);
3186 	if (rc < 0)
3187 		goto out_close;
3188 	in_len = rc;
3189 	in_len += sizeof(io_req->Pad);
3190 
3191 	/* NT IOCTL response contains one-word long output setup buffer with size of output data. */
3192 	io_req->MaxSetupCount = 1;
3193 	/* NT IOCTL response does not contain output parameters. */
3194 	io_req->MaxParameterCount = cpu_to_le32(0);
3195 	/* FSCTL_SET_REPARSE_POINT response contains empty output data. */
3196 	io_req->MaxDataCount = cpu_to_le32(0);
3197 
3198 	io_req->TotalParameterCount = cpu_to_le32(0);
3199 	io_req->TotalDataCount = cpu_to_le32(reparse_iov->iov_len);
3200 	io_req->ParameterCount = io_req->TotalParameterCount;
3201 	io_req->ParameterOffset = cpu_to_le32(0);
3202 	io_req->DataCount = io_req->TotalDataCount;
3203 	io_req->DataOffset = cpu_to_le32(offsetof(typeof(*io_req), Data));
3204 	io_req->SetupCount = 4;
3205 	io_req->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3206 	io_req->FunctionCode = cpu_to_le32(FSCTL_SET_REPARSE_POINT);
3207 	io_req->Fid = fid.netfid;
3208 	io_req->IsFsctl = 1;
3209 	io_req->IsRootFlag = 0;
3210 	io_req->ByteCount = cpu_to_le16(le32_to_cpu(io_req->DataCount) + sizeof(io_req->Pad));
3211 
3212 	in_iov[0].iov_base = (char *)io_req;
3213 	in_iov[0].iov_len = in_len;
3214 	in_iov[1] = *reparse_iov;
3215 	rc = SendReceive2(xid, tcon->ses, in_iov, ARRAY_SIZE(in_iov), &buf_type,
3216 			  CIFS_NO_RSP_BUF, &out_iov);
3217 
3218 	cifs_buf_release(io_req);
3219 
3220 	if (!rc)
3221 		rc = cifs_get_inode_info(&new, full_path, data, sb, xid, NULL);
3222 
3223 out_close:
3224 	CIFSSMBClose(xid, tcon, fid.netfid);
3225 
3226 	/*
3227 	 * If CREATE was successful but FSCTL_SET_REPARSE_POINT failed then
3228 	 * remove the intermediate object created by CREATE. Otherwise
3229 	 * empty object stay on the server when reparse call failed.
3230 	 */
3231 	if (rc)
3232 		CIFSSMBDelFile(xid, tcon, full_path, cifs_sb, NULL);
3233 
3234 	return rc ? ERR_PTR(rc) : new;
3235 }
3236 
3237 int
3238 CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3239 		    __u16 fid, __u16 compression_state)
3240 {
3241 	int rc = 0;
3242 	int bytes_returned;
3243 	struct smb_com_transaction_compr_ioctl_req *pSMB;
3244 	struct smb_com_transaction_ioctl_rsp *pSMBr;
3245 	unsigned int in_len;
3246 
3247 	cifs_dbg(FYI, "Set compression for %u\n", fid);
3248 	rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3249 		      (void **) &pSMBr);
3250 	if (rc < 0)
3251 		return rc;
3252 	in_len = rc;
3253 
3254 	pSMB->compression_state = cpu_to_le16(compression_state);
3255 
3256 	pSMB->TotalParameterCount = 0;
3257 	pSMB->TotalDataCount = cpu_to_le32(2);
3258 	pSMB->MaxParameterCount = 0;
3259 	pSMB->MaxDataCount = 0;
3260 	pSMB->MaxSetupCount = 4;
3261 	pSMB->Reserved = 0;
3262 	pSMB->ParameterOffset = 0;
3263 	pSMB->DataCount = cpu_to_le32(2);
3264 	pSMB->DataOffset =
3265 		cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3266 				     compression_state));  /* 84 */
3267 	pSMB->SetupCount = 4;
3268 	pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3269 	pSMB->ParameterCount = 0;
3270 	pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
3271 	pSMB->IsFsctl = 1; /* FSCTL */
3272 	pSMB->IsRootFlag = 0;
3273 	pSMB->Fid = fid; /* file handle always le */
3274 	/* 3 byte pad, followed by 2 byte compress state */
3275 	pSMB->ByteCount = cpu_to_le16(5);
3276 	in_len += 5;
3277 
3278 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
3279 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3280 	if (rc)
3281 		cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3282 
3283 	cifs_buf_release(pSMB);
3284 
3285 	/*
3286 	 * Note: On -EAGAIN error only caller can retry on handle based calls
3287 	 * since file handle passed in no longer valid.
3288 	 */
3289 	return rc;
3290 }
3291 
3292 
3293 #ifdef CONFIG_CIFS_POSIX
3294 
3295 #ifdef CONFIG_FS_POSIX_ACL
3296 /**
3297  * cifs_init_posix_acl - convert ACL from cifs to POSIX ACL format
3298  * @ace: POSIX ACL entry to store converted ACL into
3299  * @cifs_ace: ACL in cifs format
3300  *
3301  * Convert an Access Control Entry from wire format to local POSIX xattr
3302  * format.
3303  *
3304  * Note that the @cifs_uid member is used to store both {g,u}id_t.
3305  */
3306 static void cifs_init_posix_acl(struct posix_acl_entry *ace,
3307 				struct cifs_posix_ace *cifs_ace)
3308 {
3309 	/* u8 cifs fields do not need le conversion */
3310 	ace->e_perm = cifs_ace->cifs_e_perm;
3311 	ace->e_tag = cifs_ace->cifs_e_tag;
3312 
3313 	switch (ace->e_tag) {
3314 	case ACL_USER:
3315 		ace->e_uid = make_kuid(&init_user_ns,
3316 				       le64_to_cpu(cifs_ace->cifs_uid));
3317 		break;
3318 	case ACL_GROUP:
3319 		ace->e_gid = make_kgid(&init_user_ns,
3320 				       le64_to_cpu(cifs_ace->cifs_uid));
3321 		break;
3322 	}
3323 	return;
3324 }
3325 
3326 /**
3327  * cifs_to_posix_acl - copy cifs ACL format to POSIX ACL format
3328  * @acl: ACLs returned in POSIX ACL format
3329  * @src: ACLs in cifs format
3330  * @acl_type: type of POSIX ACL requested
3331  * @size_of_data_area: size of SMB we got
3332  *
3333  * This function converts ACLs from cifs format to POSIX ACL format.
3334  * If @acl is NULL then the size of the buffer required to store POSIX ACLs in
3335  * their uapi format is returned.
3336  */
3337 static int cifs_to_posix_acl(struct posix_acl **acl, char *src,
3338 			     const int acl_type, const int size_of_data_area)
3339 {
3340 	int size =  0;
3341 	__u16 count;
3342 	struct cifs_posix_ace *pACE;
3343 	struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3344 	struct posix_acl *kacl = NULL;
3345 	struct posix_acl_entry *pa, *pe;
3346 
3347 	if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3348 		return -EOPNOTSUPP;
3349 
3350 	if (acl_type == ACL_TYPE_ACCESS) {
3351 		count = le16_to_cpu(cifs_acl->access_entry_count);
3352 		pACE = &cifs_acl->ace_array[0];
3353 		size = sizeof(struct cifs_posix_acl);
3354 		size += sizeof(struct cifs_posix_ace) * count;
3355 		/* check if we would go beyond end of SMB */
3356 		if (size_of_data_area < size) {
3357 			cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3358 				 size_of_data_area, size);
3359 			return -EINVAL;
3360 		}
3361 	} else if (acl_type == ACL_TYPE_DEFAULT) {
3362 		count = le16_to_cpu(cifs_acl->access_entry_count);
3363 		size = sizeof(struct cifs_posix_acl);
3364 		size += sizeof(struct cifs_posix_ace) * count;
3365 		/* skip past access ACEs to get to default ACEs */
3366 		pACE = &cifs_acl->ace_array[count];
3367 		count = le16_to_cpu(cifs_acl->default_entry_count);
3368 		size += sizeof(struct cifs_posix_ace) * count;
3369 		/* check if we would go beyond end of SMB */
3370 		if (size_of_data_area < size)
3371 			return -EINVAL;
3372 	} else {
3373 		/* illegal type */
3374 		return -EINVAL;
3375 	}
3376 
3377 	/* Allocate number of POSIX ACLs to store in VFS format. */
3378 	kacl = posix_acl_alloc(count, GFP_NOFS);
3379 	if (!kacl)
3380 		return -ENOMEM;
3381 
3382 	FOREACH_ACL_ENTRY(pa, kacl, pe) {
3383 		cifs_init_posix_acl(pa, pACE);
3384 		pACE++;
3385 	}
3386 
3387 	*acl = kacl;
3388 	return 0;
3389 }
3390 
3391 /**
3392  * cifs_init_ace - convert ACL entry from POSIX ACL to cifs format
3393  * @cifs_ace: the cifs ACL entry to store into
3394  * @local_ace: the POSIX ACL entry to convert
3395  */
3396 static void cifs_init_ace(struct cifs_posix_ace *cifs_ace,
3397 			  const struct posix_acl_entry *local_ace)
3398 {
3399 	cifs_ace->cifs_e_perm = local_ace->e_perm;
3400 	cifs_ace->cifs_e_tag =  local_ace->e_tag;
3401 
3402 	switch (local_ace->e_tag) {
3403 	case ACL_USER:
3404 		cifs_ace->cifs_uid =
3405 			cpu_to_le64(from_kuid(&init_user_ns, local_ace->e_uid));
3406 		break;
3407 	case ACL_GROUP:
3408 		cifs_ace->cifs_uid =
3409 			cpu_to_le64(from_kgid(&init_user_ns, local_ace->e_gid));
3410 		break;
3411 	default:
3412 		cifs_ace->cifs_uid = cpu_to_le64(-1);
3413 	}
3414 }
3415 
3416 /**
3417  * posix_acl_to_cifs - convert ACLs from POSIX ACL to cifs format
3418  * @parm_data: ACLs in cifs format to convert to
3419  * @acl: ACLs in POSIX ACL format to convert from
3420  * @acl_type: the type of POSIX ACLs stored in @acl
3421  *
3422  * Return: the number cifs ACL entries after conversion
3423  */
3424 static __u16 posix_acl_to_cifs(char *parm_data, const struct posix_acl *acl,
3425 			       const int acl_type)
3426 {
3427 	__u16 rc = 0;
3428 	struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3429 	const struct posix_acl_entry *pa, *pe;
3430 	int count;
3431 	int i = 0;
3432 
3433 	if ((acl == NULL) || (cifs_acl == NULL))
3434 		return 0;
3435 
3436 	count = acl->a_count;
3437 	cifs_dbg(FYI, "setting acl with %d entries\n", count);
3438 
3439 	/*
3440 	 * Note that the uapi POSIX ACL version is verified by the VFS and is
3441 	 * independent of the cifs ACL version. Changing the POSIX ACL version
3442 	 * is a uapi change and if it's changed we will pass down the POSIX ACL
3443 	 * version in struct posix_acl from the VFS. For now there's really
3444 	 * only one that all filesystems know how to deal with.
3445 	 */
3446 	cifs_acl->version = cpu_to_le16(1);
3447 	if (acl_type == ACL_TYPE_ACCESS) {
3448 		cifs_acl->access_entry_count = cpu_to_le16(count);
3449 		cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
3450 	} else if (acl_type == ACL_TYPE_DEFAULT) {
3451 		cifs_acl->default_entry_count = cpu_to_le16(count);
3452 		cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
3453 	} else {
3454 		cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
3455 		return 0;
3456 	}
3457 	FOREACH_ACL_ENTRY(pa, acl, pe) {
3458 		cifs_init_ace(&cifs_acl->ace_array[i++], pa);
3459 	}
3460 	if (rc == 0) {
3461 		rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3462 		rc += sizeof(struct cifs_posix_acl);
3463 		/* BB add check to make sure ACL does not overflow SMB */
3464 	}
3465 	return rc;
3466 }
3467 
3468 int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
3469 		    const unsigned char *searchName, struct posix_acl **acl,
3470 		    const int acl_type, const struct nls_table *nls_codepage,
3471 		    int remap)
3472 {
3473 /* SMB_QUERY_POSIX_ACL */
3474 	TRANSACTION2_QPI_REQ *pSMB = NULL;
3475 	TRANSACTION2_QPI_RSP *pSMBr = NULL;
3476 	unsigned int in_len;
3477 	int rc = 0;
3478 	int bytes_returned;
3479 	int name_len;
3480 	__u16 params, byte_count;
3481 
3482 	cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
3483 
3484 queryAclRetry:
3485 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3486 		(void **) &pSMBr);
3487 	if (rc < 0)
3488 		return rc;
3489 	in_len = rc;
3490 
3491 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3492 		name_len =
3493 			cifsConvertToUTF16((__le16 *) pSMB->FileName,
3494 					   searchName, PATH_MAX, nls_codepage,
3495 					   remap);
3496 		name_len++;     /* trailing null */
3497 		name_len *= 2;
3498 		pSMB->FileName[name_len] = 0;
3499 		pSMB->FileName[name_len+1] = 0;
3500 	} else {
3501 		name_len = copy_path_name(pSMB->FileName, searchName);
3502 	}
3503 
3504 	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
3505 	pSMB->TotalDataCount = 0;
3506 	pSMB->MaxParameterCount = cpu_to_le16(2);
3507 	/* BB find exact max data count below from sess structure BB */
3508 	pSMB->MaxDataCount = cpu_to_le16(4000);
3509 	pSMB->MaxSetupCount = 0;
3510 	pSMB->Reserved = 0;
3511 	pSMB->Flags = 0;
3512 	pSMB->Timeout = 0;
3513 	pSMB->Reserved2 = 0;
3514 	pSMB->ParameterOffset = cpu_to_le16(
3515 		offsetof(struct smb_com_transaction2_qpi_req,
3516 			 InformationLevel));
3517 	pSMB->DataCount = 0;
3518 	pSMB->DataOffset = 0;
3519 	pSMB->SetupCount = 1;
3520 	pSMB->Reserved3 = 0;
3521 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3522 	byte_count = params + 1 /* pad */ ;
3523 	pSMB->TotalParameterCount = cpu_to_le16(params);
3524 	pSMB->ParameterCount = pSMB->TotalParameterCount;
3525 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3526 	pSMB->Reserved4 = 0;
3527 	in_len += byte_count;
3528 	pSMB->ByteCount = cpu_to_le16(byte_count);
3529 
3530 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
3531 		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
3532 	cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3533 	if (rc) {
3534 		cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
3535 	} else {
3536 		/* decode response */
3537 
3538 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3539 		/* BB also check enough total bytes returned */
3540 		if (rc || get_bcc(&pSMBr->hdr) < 2)
3541 			rc = smb_EIO2(smb_eio_trace_getacl_bcc_too_small,
3542 				      get_bcc(&pSMBr->hdr), 2);
3543 		else {
3544 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3545 			__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3546 			rc = cifs_to_posix_acl(acl,
3547 				(char *)&pSMBr->hdr.Protocol+data_offset,
3548 				acl_type, count);
3549 		}
3550 	}
3551 	cifs_buf_release(pSMB);
3552 	/*
3553 	 * The else branch after SendReceive() doesn't return EAGAIN so if we
3554 	 * allocated @acl in cifs_to_posix_acl() we are guaranteed to return
3555 	 * here and don't leak POSIX ACLs.
3556 	 */
3557 	if (rc == -EAGAIN)
3558 		goto queryAclRetry;
3559 	return rc;
3560 }
3561 
3562 int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3563 		    const unsigned char *fileName, const struct posix_acl *acl,
3564 		    const int acl_type, const struct nls_table *nls_codepage,
3565 		    int remap)
3566 {
3567 	struct smb_com_transaction2_spi_req *pSMB = NULL;
3568 	struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3569 	unsigned int in_len;
3570 	char *parm_data;
3571 	int name_len;
3572 	int rc = 0;
3573 	int bytes_returned = 0;
3574 	__u16 params, byte_count, data_count, param_offset, offset;
3575 	size_t cifs_acl_size, bytes_available;
3576 
3577 	cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
3578 setAclRetry:
3579 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3580 		      (void **) &pSMBr);
3581 	if (rc < 0)
3582 		return rc;
3583 	in_len = rc;
3584 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3585 		name_len =
3586 			cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3587 					   PATH_MAX, nls_codepage, remap);
3588 		name_len++;     /* trailing null */
3589 		name_len *= 2;
3590 	} else {
3591 		name_len = copy_path_name(pSMB->FileName, fileName);
3592 	}
3593 	params = 6 + name_len;
3594 	pSMB->MaxParameterCount = cpu_to_le16(2);
3595 	pSMB->MaxDataCount = cpu_to_le16(min_t(unsigned int, CIFSMaxBufSize, USHRT_MAX));
3596 	pSMB->MaxSetupCount = 0;
3597 	pSMB->Reserved = 0;
3598 	pSMB->Flags = 0;
3599 	pSMB->Timeout = 0;
3600 	pSMB->Reserved2 = 0;
3601 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
3602 				InformationLevel);
3603 	offset = param_offset + params;
3604 	parm_data = ((char *)pSMB) + offset;
3605 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
3606 
3607 	/* make sure we can fit the larger cifs_posix_aces in the buffer */
3608 	cifs_acl_size = sizeof(struct cifs_posix_acl) +
3609 		       (acl->a_count * sizeof(struct cifs_posix_ace));
3610 	bytes_available = (CIFSMaxBufSize + MAX_HEADER_SIZE(tcon->ses->server)) - offset;
3611 	if (cifs_acl_size > bytes_available || cifs_acl_size > USHRT_MAX) {
3612 		rc = -E2BIG;
3613 		goto setACLerrorExit;
3614 	}
3615 
3616 	/* convert to on the wire format for POSIX ACL */
3617 	data_count = posix_acl_to_cifs(parm_data, acl, acl_type);
3618 
3619 	if (data_count == 0) {
3620 		rc = -EOPNOTSUPP;
3621 		goto setACLerrorExit;
3622 	}
3623 	pSMB->DataOffset = cpu_to_le16(offset);
3624 	pSMB->SetupCount = 1;
3625 	pSMB->Reserved3 = 0;
3626 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3627 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3628 	byte_count = 3 /* pad */  + params + data_count;
3629 	pSMB->DataCount = cpu_to_le16(data_count);
3630 	pSMB->TotalDataCount = pSMB->DataCount;
3631 	pSMB->ParameterCount = cpu_to_le16(params);
3632 	pSMB->TotalParameterCount = pSMB->ParameterCount;
3633 	pSMB->Reserved4 = 0;
3634 	in_len += byte_count;
3635 	pSMB->ByteCount = cpu_to_le16(byte_count);
3636 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
3637 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3638 	if (rc)
3639 		cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
3640 
3641 setACLerrorExit:
3642 	cifs_buf_release(pSMB);
3643 	if (rc == -EAGAIN)
3644 		goto setAclRetry;
3645 	return rc;
3646 }
3647 #else
3648 int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
3649 		    const unsigned char *searchName, struct posix_acl **acl,
3650 		    const int acl_type, const struct nls_table *nls_codepage,
3651 		    int remap)
3652 {
3653 	return -EOPNOTSUPP;
3654 }
3655 
3656 int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3657 		    const unsigned char *fileName, const struct posix_acl *acl,
3658 		    const int acl_type, const struct nls_table *nls_codepage,
3659 		    int remap)
3660 {
3661 	return -EOPNOTSUPP;
3662 }
3663 #endif /* CONFIG_FS_POSIX_ACL */
3664 
3665 int
3666 CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3667 	       const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3668 {
3669 	int rc = 0;
3670 	struct smb_t2_qfi_req *pSMB = NULL;
3671 	struct smb_t2_qfi_rsp *pSMBr = NULL;
3672 	unsigned int in_len;
3673 	int bytes_returned;
3674 	__u16 params, byte_count;
3675 
3676 	cifs_dbg(FYI, "In GetExtAttr\n");
3677 	if (tcon == NULL)
3678 		return -ENODEV;
3679 
3680 GetExtAttrRetry:
3681 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3682 		      (void **) &pSMBr);
3683 	if (rc < 0)
3684 		return rc;
3685 	in_len = rc;
3686 
3687 	params = 2 /* level */ + 2 /* fid */;
3688 	pSMB->t2.TotalDataCount = 0;
3689 	pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3690 	/* BB find exact max data count below from sess structure BB */
3691 	pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3692 	pSMB->t2.MaxSetupCount = 0;
3693 	pSMB->t2.Reserved = 0;
3694 	pSMB->t2.Flags = 0;
3695 	pSMB->t2.Timeout = 0;
3696 	pSMB->t2.Reserved2 = 0;
3697 	pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3698 					       Fid));
3699 	pSMB->t2.DataCount = 0;
3700 	pSMB->t2.DataOffset = 0;
3701 	pSMB->t2.SetupCount = 1;
3702 	pSMB->t2.Reserved3 = 0;
3703 	pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3704 	byte_count = params + 1 /* pad */ ;
3705 	pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3706 	pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3707 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3708 	pSMB->Pad = 0;
3709 	pSMB->Fid = netfid;
3710 	in_len += byte_count;
3711 	pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3712 
3713 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
3714 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3715 	if (rc) {
3716 		cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
3717 	} else {
3718 		/* decode response */
3719 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3720 		/* BB also check enough total bytes returned */
3721 		if (rc || get_bcc(&pSMBr->hdr) < 2)
3722 			/* If rc should we check for EOPNOSUPP and
3723 			   disable the srvino flag? or in caller? */
3724 			rc = smb_EIO2(smb_eio_trace_getextattr_bcc_too_small,
3725 				      get_bcc(&pSMBr->hdr), 2);
3726 		else {
3727 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3728 			__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3729 			struct file_chattr_info *pfinfo;
3730 
3731 			if (count != 16) {
3732 				cifs_dbg(FYI, "Invalid size ret in GetExtAttr\n");
3733 				rc = smb_EIO2(smb_eio_trace_getextattr_inv_size,
3734 					      count, 16);
3735 				goto GetExtAttrOut;
3736 			}
3737 			pfinfo = (struct file_chattr_info *)
3738 				 (data_offset + (char *) &pSMBr->hdr.Protocol);
3739 			*pExtAttrBits = le64_to_cpu(pfinfo->mode);
3740 			*pMask = le64_to_cpu(pfinfo->mask);
3741 		}
3742 	}
3743 GetExtAttrOut:
3744 	cifs_buf_release(pSMB);
3745 	if (rc == -EAGAIN)
3746 		goto GetExtAttrRetry;
3747 	return rc;
3748 }
3749 
3750 #endif /* CONFIG_POSIX */
3751 
3752 /*
3753  * Initialize NT TRANSACT SMB into small smb request buffer.  This assumes that
3754  * all NT TRANSACTS that we init here have total parm and data under about 400
3755  * bytes (to fit in small cifs buffer size), which is the case so far, it
3756  * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3757  * returned setup area) and MaxParameterCount (returned parms size) must be set
3758  * by caller
3759  */
3760 static int
3761 smb_init_nttransact(const __u16 sub_command, const int setup_count,
3762 		   const int parm_len, struct cifs_tcon *tcon,
3763 		   void **ret_buf)
3764 {
3765 	int rc;
3766 	__u32 temp_offset;
3767 	struct smb_com_ntransact_req *pSMB;
3768 	unsigned int in_len;
3769 
3770 	rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3771 				(void **)&pSMB);
3772 	if (rc < 0)
3773 		return rc;
3774 	in_len = rc;
3775 	*ret_buf = (void *)pSMB;
3776 	pSMB->Reserved = 0;
3777 	pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3778 	pSMB->TotalDataCount  = 0;
3779 	pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3780 	pSMB->ParameterCount = pSMB->TotalParameterCount;
3781 	pSMB->DataCount  = pSMB->TotalDataCount;
3782 	temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3783 		(setup_count * 2);
3784 	pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3785 	pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3786 	pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3787 	pSMB->SubCommand = cpu_to_le16(sub_command);
3788 	return in_len;
3789 }
3790 
3791 static int
3792 validate_ntransact(char *buf, char **ppparm, char **ppdata,
3793 		   __u32 *pparmlen, __u32 *pdatalen)
3794 {
3795 	__u32 data_count, data_offset, parm_count, parm_offset;
3796 	struct smb_com_ntransact_rsp *pSMBr;
3797 	size_t buflen;
3798 	char *end_buf;
3799 	u16 bcc;
3800 
3801 	*pdatalen = 0;
3802 	*pparmlen = 0;
3803 
3804 	if (buf == NULL)
3805 		return -EINVAL;
3806 
3807 	pSMBr = (struct smb_com_ntransact_rsp *)buf;
3808 
3809 	if (pSMBr->hdr.WordCount != 18) {
3810 		cifs_dbg(FYI, "invalid NT transaction WordCount %u\n",
3811 			 pSMBr->hdr.WordCount);
3812 		return -EINVAL;
3813 	}
3814 
3815 	bcc = get_bcc(&pSMBr->hdr);
3816 	end_buf = 2 /* sizeof byte count */ + bcc + (char *)&pSMBr->ByteCount;
3817 	buflen = end_buf - buf;
3818 
3819 	data_offset = le32_to_cpu(pSMBr->DataOffset);
3820 	data_count = le32_to_cpu(pSMBr->DataCount);
3821 	parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3822 	parm_count = le32_to_cpu(pSMBr->ParameterCount);
3823 
3824 	/* should we also check that parm and data areas do not overlap? */
3825 	if (parm_offset > buflen) {
3826 		cifs_dbg(FYI, "parms start after end of smb\n");
3827 		return -EINVAL;
3828 	} else if (parm_count > buflen - parm_offset) {
3829 		cifs_dbg(FYI, "parm end after end of smb\n");
3830 		return -EINVAL;
3831 	} else if (data_offset > buflen) {
3832 		cifs_dbg(FYI, "data starts after end of smb\n");
3833 		return -EINVAL;
3834 	} else if (data_count > buflen - data_offset) {
3835 		cifs_dbg(FYI, "data offset %u + count %u past smb length %zu\n",
3836 			 data_offset, data_count, buflen);
3837 		return -EINVAL;
3838 	} else if (parm_count + data_count > bcc) {
3839 		cifs_dbg(FYI, "parm count and data count larger than SMB\n");
3840 		return -EINVAL;
3841 	}
3842 
3843 	*ppparm = buf + parm_offset;
3844 	*ppdata = buf + data_offset;
3845 	*pdatalen = data_count;
3846 	*pparmlen = parm_count;
3847 	return 0;
3848 }
3849 
3850 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3851 int
3852 CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3853 		  struct smb_ntsd **acl_inf, __u32 *pbuflen, __u32 info)
3854 {
3855 	int rc = 0;
3856 	int buf_type = 0;
3857 	QUERY_SEC_DESC_REQ *pSMB;
3858 	struct kvec iov[1];
3859 	struct kvec rsp_iov;
3860 	unsigned int in_len;
3861 
3862 	cifs_dbg(FYI, "GetCifsACL\n");
3863 
3864 	*pbuflen = 0;
3865 	*acl_inf = NULL;
3866 
3867 	rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3868 			8 /* parm len */, tcon, (void **) &pSMB);
3869 	if (rc < 0)
3870 		return rc;
3871 	in_len = rc;
3872 
3873 	pSMB->MaxParameterCount = cpu_to_le32(4);
3874 	/* BB TEST with big acls that might need to be e.g. larger than 16K */
3875 	pSMB->MaxSetupCount = 0;
3876 	pSMB->Fid = fid; /* file handle always le */
3877 	pSMB->AclFlags = cpu_to_le32(info);
3878 	pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3879 	in_len += 11;
3880 	iov[0].iov_base = (char *)pSMB;
3881 	iov[0].iov_len = in_len;
3882 
3883 	rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3884 			  0, &rsp_iov);
3885 	cifs_small_buf_release(pSMB);
3886 	cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3887 	if (rc) {
3888 		cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
3889 	} else {                /* decode response */
3890 		__le32 *parm;
3891 		__u32 parm_len;
3892 		__u32 acl_len;
3893 		struct smb_com_ntransact_rsp *pSMBr;
3894 		char *pdata;
3895 
3896 /* validate_nttransact */
3897 		rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
3898 					&pdata, &parm_len, pbuflen);
3899 		if (rc)
3900 			goto qsec_out;
3901 		pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
3902 
3903 		cifs_dbg(FYI, "smb %p parm %p data %p\n",
3904 			 pSMBr, parm, *acl_inf);
3905 
3906 		if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3907 			rc = smb_EIO2(smb_eio_trace_getcifsacl_param_count,
3908 				      le32_to_cpu(pSMBr->ParameterCount), 4);
3909 			*pbuflen = 0;
3910 			goto qsec_out;
3911 		}
3912 
3913 /* BB check that data area is minimum length and as big as acl_len */
3914 
3915 		acl_len = le32_to_cpu(*parm);
3916 		if (acl_len != *pbuflen) {
3917 			cifs_dbg(VFS, "acl length %d does not match %d\n",
3918 				 acl_len, *pbuflen);
3919 			if (*pbuflen > acl_len)
3920 				*pbuflen = acl_len;
3921 		}
3922 
3923 		/* check if buffer is big enough for the acl
3924 		   header followed by the smallest SID */
3925 		if ((*pbuflen < sizeof(struct smb_ntsd) + 8) ||
3926 		    (*pbuflen >= 64 * 1024)) {
3927 			cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
3928 			rc = -EINVAL;
3929 			*pbuflen = 0;
3930 		} else {
3931 			*acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
3932 			if (*acl_inf == NULL) {
3933 				*pbuflen = 0;
3934 				rc = -ENOMEM;
3935 			}
3936 		}
3937 	}
3938 qsec_out:
3939 	free_rsp_buf(buf_type, rsp_iov.iov_base);
3940 	return rc;
3941 }
3942 
3943 int
3944 CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3945 			struct smb_ntsd *pntsd, __u32 acllen, int aclflag)
3946 {
3947 	__u16 byte_count, param_count, data_count, param_offset, data_offset;
3948 	int rc = 0;
3949 	int bytes_returned = 0;
3950 	SET_SEC_DESC_REQ *pSMB = NULL;
3951 	unsigned int in_len;
3952 	void *pSMBr;
3953 
3954 setCifsAclRetry:
3955 	rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3956 	if (rc < 0)
3957 		return rc;
3958 	in_len = rc;
3959 
3960 	pSMB->MaxSetupCount = 0;
3961 	pSMB->Reserved = 0;
3962 
3963 	param_count = 8;
3964 	param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid);
3965 	data_count = acllen;
3966 	data_offset = param_offset + param_count;
3967 	byte_count = 3 /* pad */  + param_count;
3968 
3969 	pSMB->DataCount = cpu_to_le32(data_count);
3970 	pSMB->TotalDataCount = pSMB->DataCount;
3971 	pSMB->MaxParameterCount = cpu_to_le32(4);
3972 	pSMB->MaxDataCount = cpu_to_le32(16384);
3973 	pSMB->ParameterCount = cpu_to_le32(param_count);
3974 	pSMB->ParameterOffset = cpu_to_le32(param_offset);
3975 	pSMB->TotalParameterCount = pSMB->ParameterCount;
3976 	pSMB->DataOffset = cpu_to_le32(data_offset);
3977 	pSMB->SetupCount = 0;
3978 	pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3979 	pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3980 
3981 	pSMB->Fid = fid; /* file handle always le */
3982 	pSMB->Reserved2 = 0;
3983 	pSMB->AclFlags = cpu_to_le32(aclflag);
3984 
3985 	if (pntsd && acllen) {
3986 		memcpy((char *)pSMBr + data_offset, pntsd, acllen);
3987 		in_len += byte_count + data_count;
3988 	} else
3989 		in_len += byte_count;
3990 
3991 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
3992 		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
3993 
3994 	cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3995 		 bytes_returned, rc);
3996 	if (rc)
3997 		cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
3998 	cifs_buf_release(pSMB);
3999 
4000 	if (rc == -EAGAIN)
4001 		goto setCifsAclRetry;
4002 
4003 	return (rc);
4004 }
4005 
4006 
4007 /* Legacy Query Path Information call for lookup to old servers such
4008    as Win9x/WinME */
4009 int
4010 SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4011 		    const char *search_name, FILE_ALL_INFO *data,
4012 		    const struct nls_table *nls_codepage, int remap)
4013 {
4014 	QUERY_INFORMATION_REQ *pSMB;
4015 	QUERY_INFORMATION_RSP *pSMBr;
4016 	unsigned int in_len;
4017 	int rc = 0;
4018 	int bytes_returned;
4019 	int name_len;
4020 
4021 	cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
4022 QInfRetry:
4023 	rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
4024 		      (void **) &pSMBr);
4025 	if (rc < 0)
4026 		return rc;
4027 	in_len = rc;
4028 
4029 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4030 		name_len =
4031 			cifsConvertToUTF16((__le16 *) pSMB->FileName,
4032 					   search_name, PATH_MAX, nls_codepage,
4033 					   remap);
4034 		name_len++;     /* trailing null */
4035 		name_len *= 2;
4036 	} else {
4037 		name_len = copy_path_name(pSMB->FileName, search_name);
4038 	}
4039 	pSMB->BufferFormat = 0x04;
4040 	name_len++; /* account for buffer type byte */
4041 	in_len += name_len;
4042 	pSMB->ByteCount = cpu_to_le16(name_len);
4043 
4044 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4045 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4046 	if (rc) {
4047 		cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
4048 	} else if (data) {
4049 		struct timespec64 ts;
4050 		__u32 time = le32_to_cpu(pSMBr->last_write_time);
4051 
4052 		/* decode response */
4053 		/* BB FIXME - add time zone adjustment BB */
4054 		memset(data, 0, sizeof(FILE_ALL_INFO));
4055 		ts.tv_nsec = 0;
4056 		ts.tv_sec = time;
4057 		/* decode time fields */
4058 		data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4059 		data->LastWriteTime = data->ChangeTime;
4060 		data->LastAccessTime = 0;
4061 		data->AllocationSize =
4062 			cpu_to_le64(le32_to_cpu(pSMBr->size));
4063 		data->EndOfFile = data->AllocationSize;
4064 		data->Attributes =
4065 			cpu_to_le32(le16_to_cpu(pSMBr->attr));
4066 	} else {
4067 		/* bad buffer passed in */
4068 		rc = smb_EIO(smb_eio_trace_null_pointers);
4069 	}
4070 
4071 	cifs_buf_release(pSMB);
4072 
4073 	if (rc == -EAGAIN)
4074 		goto QInfRetry;
4075 
4076 	return rc;
4077 }
4078 
4079 int
4080 CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4081 		 u16 netfid, FILE_ALL_INFO *pFindData)
4082 {
4083 	struct smb_t2_qfi_req *pSMB = NULL;
4084 	struct smb_t2_qfi_rsp *pSMBr = NULL;
4085 	unsigned int in_len;
4086 	int rc = 0;
4087 	int bytes_returned;
4088 	__u16 params, byte_count;
4089 
4090 QFileInfoRetry:
4091 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4092 		      (void **) &pSMBr);
4093 	if (rc < 0)
4094 		return rc;
4095 	in_len = rc;
4096 
4097 	params = 2 /* level */ + 2 /* fid */;
4098 	pSMB->t2.TotalDataCount = 0;
4099 	pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4100 	/* BB find exact max data count below from sess structure BB */
4101 	pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4102 	pSMB->t2.MaxSetupCount = 0;
4103 	pSMB->t2.Reserved = 0;
4104 	pSMB->t2.Flags = 0;
4105 	pSMB->t2.Timeout = 0;
4106 	pSMB->t2.Reserved2 = 0;
4107 	pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4108 					       Fid));
4109 	pSMB->t2.DataCount = 0;
4110 	pSMB->t2.DataOffset = 0;
4111 	pSMB->t2.SetupCount = 1;
4112 	pSMB->t2.Reserved3 = 0;
4113 	pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4114 	byte_count = params + 1 /* pad */ ;
4115 	pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4116 	pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4117 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4118 	pSMB->Pad = 0;
4119 	pSMB->Fid = netfid;
4120 	in_len += byte_count;
4121 	pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4122 
4123 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4124 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4125 	if (rc) {
4126 		cifs_dbg(FYI, "Send error in QFileInfo = %d\n", rc);
4127 	} else {		/* decode response */
4128 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4129 
4130 		if (rc) /* BB add auto retry on EOPNOTSUPP? */
4131 			rc = smb_EIO2(smb_eio_trace_qfileinfo_invalid,
4132 				      get_bcc(&pSMBr->hdr), 40);
4133 		else if (get_bcc(&pSMBr->hdr) < 40)
4134 			rc = smb_EIO2(smb_eio_trace_qfileinfo_bcc_too_small,
4135 				      get_bcc(&pSMBr->hdr), 40);
4136 		else if (pFindData) {
4137 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4138 			memcpy((char *) pFindData,
4139 			       (char *) &pSMBr->hdr.Protocol +
4140 			       data_offset, sizeof(FILE_ALL_INFO));
4141 		} else
4142 		    rc = -ENOMEM;
4143 	}
4144 	cifs_buf_release(pSMB);
4145 	if (rc == -EAGAIN)
4146 		goto QFileInfoRetry;
4147 
4148 	return rc;
4149 }
4150 
4151 int
4152 CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4153 		 const char *search_name, FILE_ALL_INFO *data,
4154 		 int legacy /* old style infolevel */,
4155 		 const struct nls_table *nls_codepage, int remap)
4156 {
4157 	/* level 263 SMB_QUERY_FILE_ALL_INFO */
4158 	TRANSACTION2_QPI_REQ *pSMB = NULL;
4159 	TRANSACTION2_QPI_RSP *pSMBr = NULL;
4160 	unsigned int in_len;
4161 	int rc = 0;
4162 	int bytes_returned;
4163 	int name_len;
4164 	__u16 params, byte_count;
4165 
4166 QPathInfoRetry:
4167 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4168 		      (void **) &pSMBr);
4169 	if (rc < 0)
4170 		return rc;
4171 	in_len = rc;
4172 
4173 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4174 		name_len =
4175 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
4176 				       PATH_MAX, nls_codepage, remap);
4177 		name_len++;	/* trailing null */
4178 		name_len *= 2;
4179 	} else {
4180 		name_len = copy_path_name(pSMB->FileName, search_name);
4181 	}
4182 
4183 	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4184 	pSMB->TotalDataCount = 0;
4185 	pSMB->MaxParameterCount = cpu_to_le16(2);
4186 	/* BB find exact max SMB PDU from sess structure BB */
4187 	pSMB->MaxDataCount = cpu_to_le16(4000);
4188 	pSMB->MaxSetupCount = 0;
4189 	pSMB->Reserved = 0;
4190 	pSMB->Flags = 0;
4191 	pSMB->Timeout = 0;
4192 	pSMB->Reserved2 = 0;
4193 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4194 		struct smb_com_transaction2_qpi_req, InformationLevel));
4195 	pSMB->DataCount = 0;
4196 	pSMB->DataOffset = 0;
4197 	pSMB->SetupCount = 1;
4198 	pSMB->Reserved3 = 0;
4199 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4200 	byte_count = params + 1 /* pad */ ;
4201 	pSMB->TotalParameterCount = cpu_to_le16(params);
4202 	pSMB->ParameterCount = pSMB->TotalParameterCount;
4203 	if (legacy)
4204 		pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4205 	else
4206 		pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4207 	pSMB->Reserved4 = 0;
4208 	in_len += byte_count;
4209 	pSMB->ByteCount = cpu_to_le16(byte_count);
4210 
4211 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4212 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4213 	if (rc) {
4214 		cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4215 	} else {		/* decode response */
4216 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4217 
4218 		if (rc) /* BB add auto retry on EOPNOTSUPP? */
4219 			rc = smb_EIO2(smb_eio_trace_qpathinfo_invalid,
4220 				      get_bcc(&pSMBr->hdr), 40);
4221 		else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
4222 			rc = smb_EIO2(smb_eio_trace_qpathinfo_bcc_too_small,
4223 				      get_bcc(&pSMBr->hdr), 40);
4224 		else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4225 			/* 24 or 26 expected but we do not read last field */
4226 			rc = smb_EIO2(smb_eio_trace_qpathinfo_bcc_too_small,
4227 				      get_bcc(&pSMBr->hdr), 24);
4228 		else if (data) {
4229 			int size;
4230 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4231 
4232 			/*
4233 			 * On legacy responses we do not read the last field,
4234 			 * EAsize, fortunately since it varies by subdialect and
4235 			 * also note it differs on Set vs Get, ie two bytes or 4
4236 			 * bytes depending but we don't care here.
4237 			 */
4238 			if (legacy)
4239 				size = sizeof(FILE_INFO_STANDARD);
4240 			else
4241 				size = sizeof(FILE_ALL_INFO);
4242 			memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
4243 			       data_offset, size);
4244 		} else
4245 		    rc = -ENOMEM;
4246 	}
4247 	cifs_buf_release(pSMB);
4248 	if (rc == -EAGAIN)
4249 		goto QPathInfoRetry;
4250 
4251 	return rc;
4252 }
4253 
4254 int
4255 CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4256 		 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4257 {
4258 	struct smb_t2_qfi_req *pSMB = NULL;
4259 	struct smb_t2_qfi_rsp *pSMBr = NULL;
4260 	unsigned int in_len;
4261 	int rc = 0;
4262 	int bytes_returned;
4263 	__u16 params, byte_count;
4264 
4265 UnixQFileInfoRetry:
4266 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4267 		      (void **) &pSMBr);
4268 	if (rc < 0)
4269 		return rc;
4270 	in_len = rc;
4271 
4272 	params = 2 /* level */ + 2 /* fid */;
4273 	pSMB->t2.TotalDataCount = 0;
4274 	pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4275 	/* BB find exact max data count below from sess structure BB */
4276 	pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4277 	pSMB->t2.MaxSetupCount = 0;
4278 	pSMB->t2.Reserved = 0;
4279 	pSMB->t2.Flags = 0;
4280 	pSMB->t2.Timeout = 0;
4281 	pSMB->t2.Reserved2 = 0;
4282 	pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4283 					       Fid));
4284 	pSMB->t2.DataCount = 0;
4285 	pSMB->t2.DataOffset = 0;
4286 	pSMB->t2.SetupCount = 1;
4287 	pSMB->t2.Reserved3 = 0;
4288 	pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4289 	byte_count = params + 1 /* pad */ ;
4290 	pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4291 	pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4292 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4293 	pSMB->Pad = 0;
4294 	pSMB->Fid = netfid;
4295 	in_len += byte_count;
4296 	pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4297 
4298 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4299 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4300 	if (rc) {
4301 		cifs_dbg(FYI, "Send error in UnixQFileInfo = %d\n", rc);
4302 	} else {		/* decode response */
4303 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4304 
4305 		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4306 			cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4307 			rc = smb_EIO2(smb_eio_trace_unixqfileinfo_bcc_too_small,
4308 				      get_bcc(&pSMBr->hdr), sizeof(FILE_UNIX_BASIC_INFO));
4309 		} else {
4310 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4311 			memcpy((char *) pFindData,
4312 			       (char *) &pSMBr->hdr.Protocol +
4313 			       data_offset,
4314 			       sizeof(FILE_UNIX_BASIC_INFO));
4315 		}
4316 	}
4317 
4318 	cifs_buf_release(pSMB);
4319 	if (rc == -EAGAIN)
4320 		goto UnixQFileInfoRetry;
4321 
4322 	return rc;
4323 }
4324 
4325 int
4326 CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4327 		     const unsigned char *searchName,
4328 		     FILE_UNIX_BASIC_INFO *pFindData,
4329 		     const struct nls_table *nls_codepage, int remap)
4330 {
4331 /* SMB_QUERY_FILE_UNIX_BASIC */
4332 	TRANSACTION2_QPI_REQ *pSMB = NULL;
4333 	TRANSACTION2_QPI_RSP *pSMBr = NULL;
4334 	unsigned int in_len;
4335 	int rc = 0;
4336 	int bytes_returned = 0;
4337 	int name_len;
4338 	__u16 params, byte_count;
4339 
4340 	cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
4341 UnixQPathInfoRetry:
4342 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4343 		      (void **) &pSMBr);
4344 	if (rc < 0)
4345 		return rc;
4346 	in_len = rc;
4347 
4348 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4349 		name_len =
4350 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4351 				       PATH_MAX, nls_codepage, remap);
4352 		name_len++;	/* trailing null */
4353 		name_len *= 2;
4354 	} else {
4355 		name_len = copy_path_name(pSMB->FileName, searchName);
4356 	}
4357 
4358 	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
4359 	pSMB->TotalDataCount = 0;
4360 	pSMB->MaxParameterCount = cpu_to_le16(2);
4361 	/* BB find exact max SMB PDU from sess structure BB */
4362 	pSMB->MaxDataCount = cpu_to_le16(4000);
4363 	pSMB->MaxSetupCount = 0;
4364 	pSMB->Reserved = 0;
4365 	pSMB->Flags = 0;
4366 	pSMB->Timeout = 0;
4367 	pSMB->Reserved2 = 0;
4368 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4369 		struct smb_com_transaction2_qpi_req, InformationLevel));
4370 	pSMB->DataCount = 0;
4371 	pSMB->DataOffset = 0;
4372 	pSMB->SetupCount = 1;
4373 	pSMB->Reserved3 = 0;
4374 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4375 	byte_count = params + 1 /* pad */ ;
4376 	pSMB->TotalParameterCount = cpu_to_le16(params);
4377 	pSMB->ParameterCount = pSMB->TotalParameterCount;
4378 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4379 	pSMB->Reserved4 = 0;
4380 	in_len += byte_count;
4381 	pSMB->ByteCount = cpu_to_le16(byte_count);
4382 
4383 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4384 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4385 	if (rc) {
4386 		cifs_dbg(FYI, "Send error in UnixQPathInfo = %d\n", rc);
4387 	} else {		/* decode response */
4388 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4389 
4390 		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4391 			cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4392 			rc = smb_EIO2(smb_eio_trace_unixqpathinfo_bcc_too_small,
4393 				      get_bcc(&pSMBr->hdr), sizeof(FILE_UNIX_BASIC_INFO));
4394 		} else {
4395 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4396 			memcpy((char *) pFindData,
4397 			       (char *) &pSMBr->hdr.Protocol +
4398 			       data_offset,
4399 			       sizeof(FILE_UNIX_BASIC_INFO));
4400 		}
4401 	}
4402 	cifs_buf_release(pSMB);
4403 	if (rc == -EAGAIN)
4404 		goto UnixQPathInfoRetry;
4405 
4406 	return rc;
4407 }
4408 
4409 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4410 int
4411 CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
4412 	      const char *searchName, struct cifs_sb_info *cifs_sb,
4413 	      __u16 *pnetfid, __u16 search_flags,
4414 	      struct cifs_search_info *psrch_inf, bool msearch)
4415 {
4416 /* level 257 SMB_ */
4417 	unsigned int parm_off, parm_count, data_off, data_count;
4418 	TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4419 	TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4420 	struct nls_table *nls_codepage;
4421 	T2_FFIRST_RSP_PARMS *parms;
4422 	unsigned int in_len, lnoff;
4423 	__u16 params, byte_count;
4424 	int bytes_returned = 0;
4425 	int name_len, remap;
4426 	int rc = 0;
4427 
4428 	cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
4429 
4430 findFirstRetry:
4431 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4432 		      (void **) &pSMBr);
4433 	if (rc < 0)
4434 		return rc;
4435 	in_len = rc;
4436 
4437 	nls_codepage = cifs_sb->local_nls;
4438 	remap = cifs_remap(cifs_sb);
4439 
4440 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4441 		name_len =
4442 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4443 				       PATH_MAX, nls_codepage, remap);
4444 		/* We can not add the asterisk earlier in case
4445 		it got remapped to 0xF03A as if it were part of the
4446 		directory name instead of a wildcard */
4447 		name_len *= 2;
4448 		if (msearch) {
4449 			pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4450 			pSMB->FileName[name_len+1] = 0;
4451 			pSMB->FileName[name_len+2] = '*';
4452 			pSMB->FileName[name_len+3] = 0;
4453 			name_len += 4; /* now the trailing null */
4454 			/* null terminate just in case */
4455 			pSMB->FileName[name_len] = 0;
4456 			pSMB->FileName[name_len+1] = 0;
4457 			name_len += 2;
4458 		} else if (!searchName[0]) {
4459 			pSMB->FileName[0] = CIFS_DIR_SEP(cifs_sb);
4460 			pSMB->FileName[1] = 0;
4461 			pSMB->FileName[2] = 0;
4462 			pSMB->FileName[3] = 0;
4463 			name_len = 4;
4464 		}
4465 	} else {
4466 		name_len = copy_path_name(pSMB->FileName, searchName);
4467 		if (msearch) {
4468 			if (WARN_ON_ONCE(name_len > PATH_MAX-2))
4469 				name_len = PATH_MAX-2;
4470 			/* overwrite nul byte */
4471 			pSMB->FileName[name_len-1] = CIFS_DIR_SEP(cifs_sb);
4472 			pSMB->FileName[name_len] = '*';
4473 			pSMB->FileName[name_len+1] = 0;
4474 			name_len += 2;
4475 		} else if (!searchName[0]) {
4476 			pSMB->FileName[0] = CIFS_DIR_SEP(cifs_sb);
4477 			pSMB->FileName[1] = 0;
4478 			name_len = 2;
4479 		}
4480 	}
4481 
4482 	params = 12 + name_len /* includes null */ ;
4483 	pSMB->TotalDataCount = 0;	/* no EAs */
4484 	pSMB->MaxParameterCount = cpu_to_le16(10);
4485 	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4486 	pSMB->MaxSetupCount = 0;
4487 	pSMB->Reserved = 0;
4488 	pSMB->Flags = 0;
4489 	pSMB->Timeout = 0;
4490 	pSMB->Reserved2 = 0;
4491 	byte_count = params + 1 /* pad */ ;
4492 	pSMB->TotalParameterCount = cpu_to_le16(params);
4493 	pSMB->ParameterCount = pSMB->TotalParameterCount;
4494 	pSMB->ParameterOffset = cpu_to_le16(
4495 	      offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes));
4496 	pSMB->DataCount = 0;
4497 	pSMB->DataOffset = 0;
4498 	pSMB->SetupCount = 1;	/* one byte, no need to make endian neutral */
4499 	pSMB->Reserved3 = 0;
4500 	pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4501 	pSMB->SearchAttributes =
4502 	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4503 			ATTR_DIRECTORY);
4504 	pSMB->SearchCount = cpu_to_le16(msearch ? CIFSMaxBufSize/sizeof(FILE_UNIX_INFO) : 1);
4505 	pSMB->SearchFlags = cpu_to_le16(search_flags);
4506 	pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4507 
4508 	/* BB what should we set StorageType to? Does it matter? BB */
4509 	pSMB->SearchStorageType = 0;
4510 	in_len += byte_count;
4511 	pSMB->ByteCount = cpu_to_le16(byte_count);
4512 
4513 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4514 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4515 	cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
4516 
4517 	if (rc) {
4518 		/*
4519 		 * BB: add logic to retry regular search if Unix search rejected
4520 		 * unexpectedly by server.
4521 		 */
4522 		/* BB: add code to handle unsupported level rc */
4523 		cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
4524 		cifs_buf_release(pSMB);
4525 		/*
4526 		 * BB: eventually could optimize out free and realloc of buf for
4527 		 * this case.
4528 		 */
4529 		if (rc == -EAGAIN)
4530 			goto findFirstRetry;
4531 		return rc;
4532 	}
4533 	/* decode response */
4534 	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4535 	if (rc) {
4536 		cifs_buf_release(pSMB);
4537 		return rc;
4538 	}
4539 
4540 	parm_off = le16_to_cpu(pSMBr->t2.ParameterOffset);
4541 	parm_count = le16_to_cpu(pSMBr->t2.ParameterCount);
4542 	if (parm_count < sizeof(*parms) ||
4543 	    parm_off + parm_count > (unsigned int)bytes_returned) {
4544 		cifs_dbg(FYI, "%s: bad parameter area offset %u count %u for response of %d\n",
4545 			 __func__, parm_off, parm_count, bytes_returned);
4546 		rc = smb_EIO2(smb_eio_trace_ffirst_param_area,
4547 			      parm_off, parm_count);
4548 		cifs_buf_release(pSMB);
4549 		return rc;
4550 	}
4551 
4552 	data_off = le16_to_cpu(pSMBr->t2.DataOffset);
4553 	data_count = le16_to_cpu(pSMBr->t2.DataCount);
4554 	if (data_off + data_count > (unsigned int)bytes_returned) {
4555 		cifs_dbg(VFS, "%s: data area offset %u count %u past response length %d\n",
4556 			 __func__, data_off, data_count, bytes_returned);
4557 		cifs_buf_release(pSMB);
4558 		return -EINVAL;
4559 	}
4560 
4561 	psrch_inf->unicode = !!(pSMBr->hdr.Flags2 & SMBFLG2_UNICODE);
4562 	psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4563 	psrch_inf->smallBuf = false;
4564 	psrch_inf->srch_entries_start = (char *)&pSMBr->hdr.Protocol + data_off;
4565 
4566 	parms = (T2_FFIRST_RSP_PARMS *)((char *)&pSMBr->hdr.Protocol + parm_off);
4567 	psrch_inf->endOfSearch = !!parms->EndofSearch;
4568 
4569 	psrch_inf->entries_in_buffer = le16_to_cpu(parms->SearchCount);
4570 	psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
4571 		psrch_inf->entries_in_buffer;
4572 	psrch_inf->presume_name = NULL;
4573 	psrch_inf->resume_name_len = 0;
4574 	psrch_inf->resume_key = 0;
4575 	if (pnetfid)
4576 		*pnetfid = parms->SearchHandle;
4577 	lnoff = le16_to_cpu(parms->LastNameOffset);
4578 	if (!psrch_inf->entries_in_buffer) {
4579 		psrch_inf->last_entry = NULL;
4580 	} else if (psrch_inf->srch_entries_start + lnoff >=
4581 		   psrch_inf->ntwrk_buf_start + bytes_returned) {
4582 		cifs_dbg(VFS, "ignoring corrupt resume name\n");
4583 		psrch_inf->last_entry = NULL;
4584 	} else {
4585 		psrch_inf->last_entry = psrch_inf->srch_entries_start + lnoff;
4586 	}
4587 	return 0;
4588 }
4589 
4590 int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4591 		 __u16 searchHandle, __u16 search_flags,
4592 		 struct cifs_search_info *psrch_inf)
4593 {
4594 	unsigned int parm_off, parm_count, data_off, data_count;
4595 	TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4596 	TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4597 	unsigned int name_len, in_len;
4598 	T2_FNEXT_RSP_PARMS *parms;
4599 	unsigned int lnoff;
4600 	__u16 params, byte_count;
4601 	char *response_data;
4602 	int bytes_returned;
4603 	int rc = 0;
4604 
4605 	cifs_dbg(FYI, "In FindNext\n");
4606 
4607 	if (psrch_inf->endOfSearch)
4608 		return -ENOENT;
4609 
4610 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4611 		(void **) &pSMBr);
4612 	if (rc < 0)
4613 		return rc;
4614 	in_len = rc;
4615 
4616 	params = 14; /* includes 2 bytes of null string, converted to LE below*/
4617 	byte_count = 0;
4618 	pSMB->TotalDataCount = 0;       /* no EAs */
4619 	pSMB->MaxParameterCount = cpu_to_le16(8);
4620 	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
4621 	pSMB->MaxSetupCount = 0;
4622 	pSMB->Reserved = 0;
4623 	pSMB->Flags = 0;
4624 	pSMB->Timeout = 0;
4625 	pSMB->Reserved2 = 0;
4626 	pSMB->ParameterOffset =  cpu_to_le16(
4627 	      offsetof(struct smb_com_transaction2_fnext_req, SearchHandle));
4628 	pSMB->DataCount = 0;
4629 	pSMB->DataOffset = 0;
4630 	pSMB->SetupCount = 1;
4631 	pSMB->Reserved3 = 0;
4632 	pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4633 	pSMB->SearchHandle = searchHandle;      /* always kept as le */
4634 	pSMB->SearchCount =
4635 		cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
4636 	pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4637 	pSMB->ResumeKey = psrch_inf->resume_key;
4638 	pSMB->SearchFlags = cpu_to_le16(search_flags);
4639 
4640 	name_len = psrch_inf->resume_name_len;
4641 	params += name_len;
4642 	if (name_len < PATH_MAX) {
4643 		if (name_len) {
4644 			memcpy(pSMB->ResumeFileName,
4645 			       psrch_inf->presume_name, name_len);
4646 		}
4647 		byte_count += name_len;
4648 		/* 14 byte parm len above enough for 2 byte null terminator */
4649 		pSMB->ResumeFileName[name_len] = 0;
4650 		pSMB->ResumeFileName[name_len+1] = 0;
4651 	} else {
4652 		cifs_buf_release(pSMB);
4653 		return -EINVAL;
4654 	}
4655 	byte_count = params + 1 /* pad */ ;
4656 	pSMB->TotalParameterCount = cpu_to_le16(params);
4657 	pSMB->ParameterCount = pSMB->TotalParameterCount;
4658 	in_len += byte_count;
4659 	pSMB->ByteCount = cpu_to_le16(byte_count);
4660 
4661 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4662 			(struct smb_hdr *) pSMBr, &bytes_returned, 0);
4663 	cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
4664 
4665 	if (rc) {
4666 		cifs_buf_release(pSMB);
4667 		if (rc == -EBADF) {
4668 			psrch_inf->endOfSearch = true;
4669 			rc = 0; /* search probably was closed at end of search*/
4670 		} else {
4671 			cifs_dbg(FYI, "FindNext returned = %d\n", rc);
4672 		}
4673 		return rc;
4674 	}
4675 
4676 	/* decode response */
4677 	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4678 	if (rc) {
4679 		cifs_buf_release(pSMB);
4680 		return rc;
4681 	}
4682 
4683 	parm_off = le16_to_cpu(pSMBr->t2.ParameterOffset);
4684 	parm_count = le16_to_cpu(pSMBr->t2.ParameterCount);
4685 	if (parm_count < sizeof(*parms) ||
4686 	    parm_off + parm_count > (unsigned int)bytes_returned) {
4687 		cifs_dbg(FYI, "%s: bad parameter area offset %u count %u for response of %d\n",
4688 			 __func__, parm_off, parm_count, bytes_returned);
4689 		rc = smb_EIO2(smb_eio_trace_fnext_param_area,
4690 			      parm_off, parm_count);
4691 		cifs_buf_release(pSMB);
4692 		return rc;
4693 	}
4694 
4695 	data_off = le16_to_cpu(pSMBr->t2.DataOffset);
4696 	data_count = le16_to_cpu(pSMBr->t2.DataCount);
4697 	if (data_off + data_count > (unsigned int)bytes_returned) {
4698 		cifs_dbg(VFS, "%s: data area offset %u count %u past response length %d\n",
4699 			 __func__, data_off, data_count, bytes_returned);
4700 		cifs_buf_release(pSMB);
4701 		return -EINVAL;
4702 	}
4703 
4704 	/* BB fixme add lock for file (srch_info) struct here */
4705 	psrch_inf->unicode = !!(pSMBr->hdr.Flags2 & SMBFLG2_UNICODE);
4706 	response_data = (char *)&pSMBr->hdr.Protocol + parm_off;
4707 	parms = (T2_FNEXT_RSP_PARMS *)response_data;
4708 	response_data = (char *)&pSMBr->hdr.Protocol + data_off;
4709 
4710 	if (psrch_inf->smallBuf)
4711 		cifs_small_buf_release(psrch_inf->ntwrk_buf_start);
4712 	else
4713 		cifs_buf_release(psrch_inf->ntwrk_buf_start);
4714 
4715 	psrch_inf->srch_entries_start = response_data;
4716 	psrch_inf->ntwrk_buf_start = (char *)pSMB;
4717 	psrch_inf->smallBuf = false;
4718 	psrch_inf->endOfSearch = !!parms->EndofSearch;
4719 	psrch_inf->entries_in_buffer = le16_to_cpu(parms->SearchCount);
4720 	psrch_inf->index_of_last_entry += psrch_inf->entries_in_buffer;
4721 	psrch_inf->presume_name = NULL;
4722 	psrch_inf->resume_name_len = 0;
4723 	psrch_inf->resume_key = 0;
4724 	lnoff = le16_to_cpu(parms->LastNameOffset);
4725 	if (!psrch_inf->entries_in_buffer) {
4726 		psrch_inf->last_entry = NULL;
4727 	} else if (psrch_inf->srch_entries_start + lnoff >=
4728 		   psrch_inf->ntwrk_buf_start + bytes_returned) {
4729 		cifs_dbg(VFS, "ignoring corrupt resume name\n");
4730 		psrch_inf->last_entry = NULL;
4731 	} else {
4732 		psrch_inf->last_entry =
4733 			psrch_inf->srch_entries_start + lnoff;
4734 	}
4735 	/* BB fixme add unlock here */
4736 
4737 	/*
4738 	 * BB: On error, should we leave previous search buf
4739 	 * (and count and last entry fields) intact or free the previous one?
4740 	 *
4741 	 * Note: On -EAGAIN error only caller can retry on handle based calls
4742 	 * since file handle passed in no longer valid.
4743 	 */
4744 	return 0;
4745 }
4746 
4747 int
4748 CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4749 	      const __u16 searchHandle)
4750 {
4751 	int rc = 0;
4752 	FINDCLOSE_REQ *pSMB = NULL;
4753 	unsigned int in_len;
4754 
4755 	cifs_dbg(FYI, "In CIFSSMBFindClose\n");
4756 	rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4757 
4758 	/* no sense returning error if session restarted
4759 		as file handle has been closed */
4760 	if (rc == -EAGAIN)
4761 		return 0;
4762 	if (rc < 0)
4763 		return rc;
4764 	in_len = rc;
4765 
4766 	pSMB->FileID = searchHandle;
4767 	pSMB->ByteCount = 0;
4768 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
4769 	cifs_small_buf_release(pSMB);
4770 	if (rc)
4771 		cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
4772 
4773 	cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
4774 
4775 	/* Since session is dead, search handle closed on server already */
4776 	if (rc == -EAGAIN)
4777 		rc = 0;
4778 
4779 	return rc;
4780 }
4781 
4782 int
4783 CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4784 		      const char *search_name, __u64 *inode_number,
4785 		      const struct nls_table *nls_codepage, int remap)
4786 {
4787 	int rc = 0;
4788 	TRANSACTION2_QPI_REQ *pSMB = NULL;
4789 	TRANSACTION2_QPI_RSP *pSMBr = NULL;
4790 	unsigned int in_len;
4791 	int name_len, bytes_returned;
4792 	__u16 params, byte_count;
4793 
4794 	cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
4795 	if (tcon == NULL)
4796 		return -ENODEV;
4797 
4798 GetInodeNumberRetry:
4799 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4800 		      (void **) &pSMBr);
4801 	if (rc < 0)
4802 		return rc;
4803 	in_len = rc;
4804 
4805 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4806 		name_len =
4807 			cifsConvertToUTF16((__le16 *) pSMB->FileName,
4808 					   search_name, PATH_MAX, nls_codepage,
4809 					   remap);
4810 		name_len++;     /* trailing null */
4811 		name_len *= 2;
4812 	} else {
4813 		name_len = copy_path_name(pSMB->FileName, search_name);
4814 	}
4815 
4816 	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
4817 	pSMB->TotalDataCount = 0;
4818 	pSMB->MaxParameterCount = cpu_to_le16(2);
4819 	/* BB find exact max data count below from sess structure BB */
4820 	pSMB->MaxDataCount = cpu_to_le16(4000);
4821 	pSMB->MaxSetupCount = 0;
4822 	pSMB->Reserved = 0;
4823 	pSMB->Flags = 0;
4824 	pSMB->Timeout = 0;
4825 	pSMB->Reserved2 = 0;
4826 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4827 		struct smb_com_transaction2_qpi_req, InformationLevel));
4828 	pSMB->DataCount = 0;
4829 	pSMB->DataOffset = 0;
4830 	pSMB->SetupCount = 1;
4831 	pSMB->Reserved3 = 0;
4832 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4833 	byte_count = params + 1 /* pad */ ;
4834 	pSMB->TotalParameterCount = cpu_to_le16(params);
4835 	pSMB->ParameterCount = pSMB->TotalParameterCount;
4836 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4837 	pSMB->Reserved4 = 0;
4838 	in_len += byte_count;
4839 	pSMB->ByteCount = cpu_to_le16(byte_count);
4840 
4841 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
4842 		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
4843 	if (rc) {
4844 		cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
4845 	} else {
4846 		/* decode response */
4847 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4848 		/* BB also check enough total bytes returned */
4849 		if (rc || get_bcc(&pSMBr->hdr) < 2)
4850 			/* If rc should we check for EOPNOSUPP and
4851 			disable the srvino flag? or in caller? */
4852 			rc = smb_EIO2(smb_eio_trace_getsrvinonum_bcc_too_small,
4853 				      get_bcc(&pSMBr->hdr), 2);
4854 		else {
4855 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4856 			__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4857 			struct file_internal_info *pfinfo;
4858 			/* BB Do we need a cast or hash here ? */
4859 			if (count < 8) {
4860 				cifs_dbg(FYI, "Invalid size ret in QryIntrnlInf\n");
4861 				rc = smb_EIO2(smb_eio_trace_getsrvinonum_size,
4862 					      count, 8);
4863 				goto GetInodeNumOut;
4864 			}
4865 			pfinfo = (struct file_internal_info *)
4866 				(data_offset + (char *) &pSMBr->hdr.Protocol);
4867 			*inode_number = le64_to_cpu(pfinfo->UniqueId);
4868 		}
4869 	}
4870 GetInodeNumOut:
4871 	cifs_buf_release(pSMB);
4872 	if (rc == -EAGAIN)
4873 		goto GetInodeNumberRetry;
4874 	return rc;
4875 }
4876 
4877 int
4878 CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4879 		const char *search_name, struct dfs_info3_param **target_nodes,
4880 		unsigned int *num_of_nodes,
4881 		const struct nls_table *nls_codepage, int remap)
4882 {
4883 /* TRANS2_GET_DFS_REFERRAL */
4884 	TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4885 	TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
4886 	unsigned int in_len;
4887 	int rc = 0;
4888 	int bytes_returned;
4889 	int name_len;
4890 	__u16 params, byte_count;
4891 	*num_of_nodes = 0;
4892 	*target_nodes = NULL;
4893 
4894 	cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
4895 	if (ses == NULL || ses->tcon_ipc == NULL)
4896 		return -ENODEV;
4897 
4898 getDFSRetry:
4899 	/*
4900 	 * Use smb_init_no_reconnect() instead of smb_init() as
4901 	 * CIFSGetDFSRefer() may be called from cifs_reconnect_tcon() and thus
4902 	 * causing an infinite recursion.
4903 	 */
4904 	rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc,
4905 		      (void **)&pSMB, (void **)&pSMBr);
4906 	if (rc < 0)
4907 		return rc;
4908 	in_len = rc;
4909 
4910 	/* server pointer checked in called function,
4911 	but should never be null here anyway */
4912 	pSMB->hdr.Mid = get_next_mid(ses->server);
4913 	pSMB->hdr.Tid = ses->tcon_ipc->tid;
4914 	pSMB->hdr.Uid = ses->Suid;
4915 	if (ses->capabilities & CAP_STATUS32)
4916 		pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4917 	if (ses->capabilities & CAP_DFS)
4918 		pSMB->hdr.Flags2 |= SMBFLG2_DFS;
4919 
4920 	if (ses->capabilities & CAP_UNICODE) {
4921 		pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4922 		name_len =
4923 		    cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4924 				       search_name, PATH_MAX, nls_codepage,
4925 				       remap);
4926 		name_len++;	/* trailing null */
4927 		name_len *= 2;
4928 	} else {	/* BB improve the check for buffer overruns BB */
4929 		name_len = copy_path_name(pSMB->RequestFileName, search_name);
4930 	}
4931 
4932 	if (ses->server->sign)
4933 		pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4934 
4935 	pSMB->hdr.Uid = ses->Suid;
4936 
4937 	params = 2 /* level */  + name_len /*includes null */ ;
4938 	pSMB->TotalDataCount = 0;
4939 	pSMB->DataCount = 0;
4940 	pSMB->DataOffset = 0;
4941 	pSMB->MaxParameterCount = 0;
4942 	/* BB find exact max SMB PDU from sess structure BB */
4943 	pSMB->MaxDataCount = cpu_to_le16(4000);
4944 	pSMB->MaxSetupCount = 0;
4945 	pSMB->Reserved = 0;
4946 	pSMB->Flags = 0;
4947 	pSMB->Timeout = 0;
4948 	pSMB->Reserved2 = 0;
4949 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4950 	  struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel));
4951 	pSMB->SetupCount = 1;
4952 	pSMB->Reserved3 = 0;
4953 	pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4954 	byte_count = params + 3 /* pad */ ;
4955 	pSMB->ParameterCount = cpu_to_le16(params);
4956 	pSMB->TotalParameterCount = pSMB->ParameterCount;
4957 	pSMB->MaxReferralLevel = cpu_to_le16(3);
4958 	in_len += byte_count;
4959 	pSMB->ByteCount = cpu_to_le16(byte_count);
4960 
4961 	rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB, in_len,
4962 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4963 	if (rc) {
4964 		cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
4965 		goto GetDFSRefExit;
4966 	}
4967 	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4968 
4969 	/* BB Also check if enough total bytes returned? */
4970 	if (rc || get_bcc(&pSMBr->hdr) < 17) {
4971 		rc = smb_EIO2(smb_eio_trace_getdfsrefer_bcc_too_small,
4972 			      get_bcc(&pSMBr->hdr), 17);
4973 		goto GetDFSRefExit;
4974 	}
4975 
4976 	cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d  Offset %d\n",
4977 		 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
4978 
4979 	/* parse returned result into more usable form */
4980 	rc = parse_dfs_referrals(&pSMBr->dfs_data,
4981 				 le16_to_cpu(pSMBr->t2.DataCount),
4982 				 num_of_nodes, target_nodes, nls_codepage,
4983 				 remap, search_name,
4984 				 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
4985 
4986 GetDFSRefExit:
4987 	cifs_buf_release(pSMB);
4988 
4989 	if (rc == -EAGAIN)
4990 		goto getDFSRetry;
4991 
4992 	return rc;
4993 }
4994 
4995 /* Query File System Info such as free space to old servers such as Win 9x */
4996 int
4997 SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4998 	      struct kstatfs *FSData)
4999 {
5000 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5001 	TRANSACTION2_QFSI_REQ *pSMB = NULL;
5002 	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5003 	FILE_SYSTEM_ALLOC_INFO *response_data;
5004 	unsigned int in_len;
5005 	int rc = 0;
5006 	int bytes_returned = 0;
5007 	__u16 params, byte_count;
5008 
5009 	cifs_dbg(FYI, "OldQFSInfo\n");
5010 oldQFSInfoRetry:
5011 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5012 		(void **) &pSMBr);
5013 	if (rc < 0)
5014 		return rc;
5015 	in_len = rc;
5016 
5017 	params = 2;     /* level */
5018 	pSMB->TotalDataCount = 0;
5019 	pSMB->MaxParameterCount = cpu_to_le16(2);
5020 	pSMB->MaxDataCount = cpu_to_le16(1000);
5021 	pSMB->MaxSetupCount = 0;
5022 	pSMB->Reserved = 0;
5023 	pSMB->Flags = 0;
5024 	pSMB->Timeout = 0;
5025 	pSMB->Reserved2 = 0;
5026 	byte_count = params + 1 /* pad */ ;
5027 	pSMB->TotalParameterCount = cpu_to_le16(params);
5028 	pSMB->ParameterCount = pSMB->TotalParameterCount;
5029 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5030 		struct smb_com_transaction2_qfsi_req, InformationLevel));
5031 	pSMB->DataCount = 0;
5032 	pSMB->DataOffset = 0;
5033 	pSMB->SetupCount = 1;
5034 	pSMB->Reserved3 = 0;
5035 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5036 	pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
5037 	in_len += byte_count;
5038 	pSMB->ByteCount = cpu_to_le16(byte_count);
5039 
5040 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5041 		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
5042 	if (rc) {
5043 		cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
5044 	} else {                /* decode response */
5045 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5046 
5047 		if (rc || get_bcc(&pSMBr->hdr) < 18)
5048 			rc = smb_EIO2(smb_eio_trace_oldqfsinfo_bcc_too_small,
5049 				      get_bcc(&pSMBr->hdr), 18);
5050 		else {
5051 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5052 			cifs_dbg(FYI, "qfsinf resp BCC: %d  Offset %d\n",
5053 				 get_bcc(&pSMBr->hdr), data_offset);
5054 
5055 			response_data = (FILE_SYSTEM_ALLOC_INFO *)
5056 				(((char *) &pSMBr->hdr.Protocol) + data_offset);
5057 			FSData->f_bsize =
5058 				le16_to_cpu(response_data->BytesPerSector) *
5059 				le32_to_cpu(response_data->
5060 					SectorsPerAllocationUnit);
5061 			/*
5062 			 * much prefer larger but if server doesn't report
5063 			 * a valid size than 4K is a reasonable minimum
5064 			 */
5065 			if (FSData->f_bsize < 512)
5066 				FSData->f_bsize = 4096;
5067 
5068 			FSData->f_blocks =
5069 			       le32_to_cpu(response_data->TotalAllocationUnits);
5070 			FSData->f_bfree = FSData->f_bavail =
5071 				le32_to_cpu(response_data->FreeAllocationUnits);
5072 			cifs_dbg(FYI, "Blocks: %lld  Free: %lld Block size %ld\n",
5073 				 (unsigned long long)FSData->f_blocks,
5074 				 (unsigned long long)FSData->f_bfree,
5075 				 FSData->f_bsize);
5076 		}
5077 	}
5078 	cifs_buf_release(pSMB);
5079 
5080 	if (rc == -EAGAIN)
5081 		goto oldQFSInfoRetry;
5082 
5083 	return rc;
5084 }
5085 
5086 int
5087 CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5088 	       struct kstatfs *FSData)
5089 {
5090 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5091 	TRANSACTION2_QFSI_REQ *pSMB = NULL;
5092 	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5093 	FILE_SYSTEM_SIZE_INFO *response_data;
5094 	unsigned int in_len;
5095 	int rc = 0;
5096 	int bytes_returned = 0;
5097 	__u16 params, byte_count;
5098 
5099 	cifs_dbg(FYI, "In QFSInfo\n");
5100 QFSInfoRetry:
5101 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5102 		      (void **) &pSMBr);
5103 	if (rc < 0)
5104 		return rc;
5105 	in_len = rc;
5106 
5107 	params = 2;	/* level */
5108 	pSMB->TotalDataCount = 0;
5109 	pSMB->MaxParameterCount = cpu_to_le16(2);
5110 	pSMB->MaxDataCount = cpu_to_le16(1000);
5111 	pSMB->MaxSetupCount = 0;
5112 	pSMB->Reserved = 0;
5113 	pSMB->Flags = 0;
5114 	pSMB->Timeout = 0;
5115 	pSMB->Reserved2 = 0;
5116 	byte_count = params + 1 /* pad */ ;
5117 	pSMB->TotalParameterCount = cpu_to_le16(params);
5118 	pSMB->ParameterCount = pSMB->TotalParameterCount;
5119 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5120 		struct smb_com_transaction2_qfsi_req, InformationLevel));
5121 	pSMB->DataCount = 0;
5122 	pSMB->DataOffset = 0;
5123 	pSMB->SetupCount = 1;
5124 	pSMB->Reserved3 = 0;
5125 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5126 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
5127 	in_len += byte_count;
5128 	pSMB->ByteCount = cpu_to_le16(byte_count);
5129 
5130 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5131 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5132 	if (rc) {
5133 		cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
5134 	} else {		/* decode response */
5135 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5136 
5137 		if (rc || get_bcc(&pSMBr->hdr) < 24)
5138 			rc = smb_EIO2(smb_eio_trace_qfsinfo_bcc_too_small,
5139 				      get_bcc(&pSMBr->hdr), 24);
5140 		else {
5141 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5142 
5143 			response_data =
5144 			    (FILE_SYSTEM_SIZE_INFO
5145 			     *) (((char *) &pSMBr->hdr.Protocol) +
5146 				 data_offset);
5147 			FSData->f_bsize =
5148 			    le32_to_cpu(response_data->BytesPerSector) *
5149 			    le32_to_cpu(response_data->
5150 					SectorsPerAllocationUnit);
5151 			/*
5152 			 * much prefer larger but if server doesn't report
5153 			 * a valid size than 4K is a reasonable minimum
5154 			 */
5155 			if (FSData->f_bsize < 512)
5156 				FSData->f_bsize = 4096;
5157 
5158 			FSData->f_blocks =
5159 			    le64_to_cpu(response_data->TotalAllocationUnits);
5160 			FSData->f_bfree = FSData->f_bavail =
5161 			    le64_to_cpu(response_data->AvailableAllocationUnits);
5162 			cifs_dbg(FYI, "Blocks: %lld  Free: %lld Block size %ld\n",
5163 				 (unsigned long long)FSData->f_blocks,
5164 				 (unsigned long long)FSData->f_bfree,
5165 				 FSData->f_bsize);
5166 		}
5167 	}
5168 	cifs_buf_release(pSMB);
5169 
5170 	if (rc == -EAGAIN)
5171 		goto QFSInfoRetry;
5172 
5173 	return rc;
5174 }
5175 
5176 int
5177 CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
5178 {
5179 /* level 0x105  SMB_QUERY_FILE_SYSTEM_INFO */
5180 	TRANSACTION2_QFSI_REQ *pSMB = NULL;
5181 	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5182 	FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5183 	unsigned int in_len;
5184 	int rc = 0;
5185 	int bytes_returned = 0;
5186 	__u16 params, byte_count;
5187 
5188 	cifs_dbg(FYI, "In QFSAttributeInfo\n");
5189 QFSAttributeRetry:
5190 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5191 		      (void **) &pSMBr);
5192 	if (rc < 0)
5193 		return rc;
5194 	in_len = rc;
5195 
5196 	params = 2;	/* level */
5197 	pSMB->TotalDataCount = 0;
5198 	pSMB->MaxParameterCount = cpu_to_le16(2);
5199 	/* BB find exact max SMB PDU from sess structure BB */
5200 	pSMB->MaxDataCount = cpu_to_le16(1000);
5201 	pSMB->MaxSetupCount = 0;
5202 	pSMB->Reserved = 0;
5203 	pSMB->Flags = 0;
5204 	pSMB->Timeout = 0;
5205 	pSMB->Reserved2 = 0;
5206 	byte_count = params + 1 /* pad */ ;
5207 	pSMB->TotalParameterCount = cpu_to_le16(params);
5208 	pSMB->ParameterCount = pSMB->TotalParameterCount;
5209 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5210 		struct smb_com_transaction2_qfsi_req, InformationLevel));
5211 	pSMB->DataCount = 0;
5212 	pSMB->DataOffset = 0;
5213 	pSMB->SetupCount = 1;
5214 	pSMB->Reserved3 = 0;
5215 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5216 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5217 	in_len += byte_count;
5218 	pSMB->ByteCount = cpu_to_le16(byte_count);
5219 
5220 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5221 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5222 	if (rc) {
5223 		cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
5224 	} else {		/* decode response */
5225 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5226 
5227 		if (rc || get_bcc(&pSMBr->hdr) < 13) {
5228 			/* BB also check if enough bytes returned */
5229 			rc = smb_EIO2(smb_eio_trace_qfsattrinfo_bcc_too_small,
5230 				      get_bcc(&pSMBr->hdr), 13);
5231 		} else {
5232 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5233 			response_data =
5234 			    (FILE_SYSTEM_ATTRIBUTE_INFO
5235 			     *) (((char *) &pSMBr->hdr.Protocol) +
5236 				 data_offset);
5237 			memcpy(&tcon->fsAttrInfo, response_data,
5238 			       sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
5239 		}
5240 	}
5241 	cifs_buf_release(pSMB);
5242 
5243 	if (rc == -EAGAIN)
5244 		goto QFSAttributeRetry;
5245 
5246 	return rc;
5247 }
5248 
5249 int
5250 CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
5251 {
5252 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5253 	TRANSACTION2_QFSI_REQ *pSMB = NULL;
5254 	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5255 	FILE_SYSTEM_DEVICE_INFO *response_data;
5256 	unsigned int in_len;
5257 	int rc = 0;
5258 	int bytes_returned = 0;
5259 	__u16 params, byte_count;
5260 
5261 	cifs_dbg(FYI, "In QFSDeviceInfo\n");
5262 QFSDeviceRetry:
5263 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5264 		      (void **) &pSMBr);
5265 	if (rc < 0)
5266 		return rc;
5267 	in_len = rc;
5268 
5269 	params = 2;	/* level */
5270 	pSMB->TotalDataCount = 0;
5271 	pSMB->MaxParameterCount = cpu_to_le16(2);
5272 	/* BB find exact max SMB PDU from sess structure BB */
5273 	pSMB->MaxDataCount = cpu_to_le16(1000);
5274 	pSMB->MaxSetupCount = 0;
5275 	pSMB->Reserved = 0;
5276 	pSMB->Flags = 0;
5277 	pSMB->Timeout = 0;
5278 	pSMB->Reserved2 = 0;
5279 	byte_count = params + 1 /* pad */ ;
5280 	pSMB->TotalParameterCount = cpu_to_le16(params);
5281 	pSMB->ParameterCount = pSMB->TotalParameterCount;
5282 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5283 		struct smb_com_transaction2_qfsi_req, InformationLevel));
5284 
5285 	pSMB->DataCount = 0;
5286 	pSMB->DataOffset = 0;
5287 	pSMB->SetupCount = 1;
5288 	pSMB->Reserved3 = 0;
5289 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5290 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5291 	in_len += byte_count;
5292 	pSMB->ByteCount = cpu_to_le16(byte_count);
5293 
5294 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5295 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5296 	if (rc) {
5297 		cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
5298 	} else {		/* decode response */
5299 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5300 
5301 		if (rc || get_bcc(&pSMBr->hdr) <
5302 			  sizeof(FILE_SYSTEM_DEVICE_INFO))
5303 			rc = smb_EIO2(smb_eio_trace_qfsdevinfo_bcc_too_small,
5304 				      get_bcc(&pSMBr->hdr),
5305 				      sizeof(FILE_SYSTEM_DEVICE_INFO));
5306 		else {
5307 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5308 			response_data =
5309 			    (FILE_SYSTEM_DEVICE_INFO *)
5310 				(((char *) &pSMBr->hdr.Protocol) +
5311 				 data_offset);
5312 			memcpy(&tcon->fsDevInfo, response_data,
5313 			       sizeof(FILE_SYSTEM_DEVICE_INFO));
5314 		}
5315 	}
5316 	cifs_buf_release(pSMB);
5317 
5318 	if (rc == -EAGAIN)
5319 		goto QFSDeviceRetry;
5320 
5321 	return rc;
5322 }
5323 
5324 int
5325 CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
5326 {
5327 /* level 0x200  SMB_QUERY_CIFS_UNIX_INFO */
5328 	TRANSACTION2_QFSI_REQ *pSMB = NULL;
5329 	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5330 	FILE_SYSTEM_UNIX_INFO *response_data;
5331 	unsigned int in_len;
5332 	int rc = 0;
5333 	int bytes_returned = 0;
5334 	__u16 params, byte_count;
5335 
5336 	cifs_dbg(FYI, "In QFSUnixInfo\n");
5337 QFSUnixRetry:
5338 	rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5339 				   (void **) &pSMB, (void **) &pSMBr);
5340 	if (rc < 0)
5341 		return rc;
5342 	in_len = rc;
5343 
5344 	params = 2;	/* level */
5345 	pSMB->TotalDataCount = 0;
5346 	pSMB->DataCount = 0;
5347 	pSMB->DataOffset = 0;
5348 	pSMB->MaxParameterCount = cpu_to_le16(2);
5349 	/* BB find exact max SMB PDU from sess structure BB */
5350 	pSMB->MaxDataCount = cpu_to_le16(100);
5351 	pSMB->MaxSetupCount = 0;
5352 	pSMB->Reserved = 0;
5353 	pSMB->Flags = 0;
5354 	pSMB->Timeout = 0;
5355 	pSMB->Reserved2 = 0;
5356 	byte_count = params + 1 /* pad */ ;
5357 	pSMB->ParameterCount = cpu_to_le16(params);
5358 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5359 	pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5360 			smb_com_transaction2_qfsi_req, InformationLevel));
5361 	pSMB->SetupCount = 1;
5362 	pSMB->Reserved3 = 0;
5363 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5364 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5365 	in_len += byte_count;
5366 	pSMB->ByteCount = cpu_to_le16(byte_count);
5367 
5368 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5369 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5370 	if (rc) {
5371 		cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
5372 	} else {		/* decode response */
5373 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5374 
5375 		if (rc || get_bcc(&pSMBr->hdr) < 13) {
5376 			rc = smb_EIO2(smb_eio_trace_qfsunixinfo_bcc_too_small,
5377 				      get_bcc(&pSMBr->hdr), 13);
5378 		} else {
5379 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5380 			response_data =
5381 			    (FILE_SYSTEM_UNIX_INFO
5382 			     *) (((char *) &pSMBr->hdr.Protocol) +
5383 				 data_offset);
5384 			memcpy(&tcon->fsUnixInfo, response_data,
5385 			       sizeof(FILE_SYSTEM_UNIX_INFO));
5386 		}
5387 	}
5388 	cifs_buf_release(pSMB);
5389 
5390 	if (rc == -EAGAIN)
5391 		goto QFSUnixRetry;
5392 
5393 
5394 	return rc;
5395 }
5396 
5397 int
5398 CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
5399 {
5400 /* level 0x200  SMB_SET_CIFS_UNIX_INFO */
5401 	TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5402 	TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5403 	unsigned int in_len;
5404 	int rc = 0;
5405 	int bytes_returned = 0;
5406 	__u16 params, param_offset, offset, byte_count;
5407 
5408 	cifs_dbg(FYI, "In SETFSUnixInfo\n");
5409 SETFSUnixRetry:
5410 	/* BB switch to small buf init to save memory */
5411 	rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5412 					(void **) &pSMB, (void **) &pSMBr);
5413 	if (rc < 0)
5414 		return rc;
5415 	in_len = rc;
5416 
5417 	params = 4;	/* 2 bytes zero followed by info level. */
5418 	pSMB->MaxSetupCount = 0;
5419 	pSMB->Reserved = 0;
5420 	pSMB->Flags = 0;
5421 	pSMB->Timeout = 0;
5422 	pSMB->Reserved2 = 0;
5423 	param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum);
5424 	offset = param_offset + params;
5425 
5426 	pSMB->MaxParameterCount = cpu_to_le16(4);
5427 	/* BB find exact max SMB PDU from sess structure BB */
5428 	pSMB->MaxDataCount = cpu_to_le16(100);
5429 	pSMB->SetupCount = 1;
5430 	pSMB->Reserved3 = 0;
5431 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5432 	byte_count = 1 /* pad */ + params + 12;
5433 
5434 	pSMB->DataCount = cpu_to_le16(12);
5435 	pSMB->ParameterCount = cpu_to_le16(params);
5436 	pSMB->TotalDataCount = pSMB->DataCount;
5437 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5438 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
5439 	pSMB->DataOffset = cpu_to_le16(offset);
5440 
5441 	/* Params. */
5442 	pSMB->FileNum = 0;
5443 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5444 
5445 	/* Data. */
5446 	pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5447 	pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5448 	pSMB->ClientUnixCap = cpu_to_le64(cap);
5449 
5450 	in_len += byte_count;
5451 	pSMB->ByteCount = cpu_to_le16(byte_count);
5452 
5453 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5454 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5455 	if (rc) {
5456 		cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
5457 	} else {		/* decode response */
5458 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5459 		if (rc)
5460 			rc = -EIO;	/* bad smb */
5461 	}
5462 	cifs_buf_release(pSMB);
5463 
5464 	if (rc == -EAGAIN)
5465 		goto SETFSUnixRetry;
5466 
5467 	return rc;
5468 }
5469 
5470 
5471 
5472 int
5473 CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
5474 		   struct kstatfs *FSData)
5475 {
5476 /* level 0x201  SMB_QUERY_CIFS_POSIX_INFO */
5477 	TRANSACTION2_QFSI_REQ *pSMB = NULL;
5478 	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5479 	FILE_SYSTEM_POSIX_INFO *response_data;
5480 	unsigned int in_len;
5481 	int rc = 0;
5482 	int bytes_returned = 0;
5483 	__u16 params, byte_count;
5484 
5485 	cifs_dbg(FYI, "In QFSPosixInfo\n");
5486 QFSPosixRetry:
5487 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5488 		      (void **) &pSMBr);
5489 	if (rc < 0)
5490 		return rc;
5491 	in_len = rc;
5492 
5493 	params = 2;	/* level */
5494 	pSMB->TotalDataCount = 0;
5495 	pSMB->DataCount = 0;
5496 	pSMB->DataOffset = 0;
5497 	pSMB->MaxParameterCount = cpu_to_le16(2);
5498 	/* BB find exact max SMB PDU from sess structure BB */
5499 	pSMB->MaxDataCount = cpu_to_le16(100);
5500 	pSMB->MaxSetupCount = 0;
5501 	pSMB->Reserved = 0;
5502 	pSMB->Flags = 0;
5503 	pSMB->Timeout = 0;
5504 	pSMB->Reserved2 = 0;
5505 	byte_count = params + 1 /* pad */ ;
5506 	pSMB->ParameterCount = cpu_to_le16(params);
5507 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5508 	pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5509 			smb_com_transaction2_qfsi_req, InformationLevel));
5510 	pSMB->SetupCount = 1;
5511 	pSMB->Reserved3 = 0;
5512 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5513 	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5514 	in_len += byte_count;
5515 	pSMB->ByteCount = cpu_to_le16(byte_count);
5516 
5517 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5518 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5519 	if (rc) {
5520 		cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
5521 	} else {		/* decode response */
5522 		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5523 
5524 		if (rc || get_bcc(&pSMBr->hdr) < 13) {
5525 			rc = smb_EIO2(smb_eio_trace_qfsposixinfo_bcc_too_small,
5526 				      get_bcc(&pSMBr->hdr), 13);
5527 		} else {
5528 			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5529 			response_data =
5530 			    (FILE_SYSTEM_POSIX_INFO
5531 			     *) (((char *) &pSMBr->hdr.Protocol) +
5532 				 data_offset);
5533 			FSData->f_bsize =
5534 					le32_to_cpu(response_data->BlockSize);
5535 			/*
5536 			 * much prefer larger but if server doesn't report
5537 			 * a valid size than 4K is a reasonable minimum
5538 			 */
5539 			if (FSData->f_bsize < 512)
5540 				FSData->f_bsize = 4096;
5541 
5542 			FSData->f_blocks =
5543 					le64_to_cpu(response_data->TotalBlocks);
5544 			FSData->f_bfree =
5545 			    le64_to_cpu(response_data->BlocksAvail);
5546 			if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
5547 				FSData->f_bavail = FSData->f_bfree;
5548 			} else {
5549 				FSData->f_bavail =
5550 				    le64_to_cpu(response_data->UserBlocksAvail);
5551 			}
5552 			if (response_data->TotalFileNodes != cpu_to_le64(-1))
5553 				FSData->f_files =
5554 				     le64_to_cpu(response_data->TotalFileNodes);
5555 			if (response_data->FreeFileNodes != cpu_to_le64(-1))
5556 				FSData->f_ffree =
5557 				      le64_to_cpu(response_data->FreeFileNodes);
5558 		}
5559 	}
5560 	cifs_buf_release(pSMB);
5561 
5562 	if (rc == -EAGAIN)
5563 		goto QFSPosixRetry;
5564 
5565 	return rc;
5566 }
5567 
5568 
5569 /*
5570  * We can not use write of zero bytes trick to set file size due to need for
5571  * large file support. Also note that this SetPathInfo is preferred to
5572  * SetFileInfo based method in next routine which is only needed to work around
5573  * a sharing violation bugin Samba which this routine can run into.
5574  */
5575 int
5576 CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5577 	      const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5578 	      bool set_allocation, struct dentry *dentry)
5579 {
5580 	struct smb_com_transaction2_spi_req *pSMB = NULL;
5581 	struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5582 	struct file_end_of_file_info *parm_data;
5583 	unsigned int in_len;
5584 	int name_len;
5585 	int rc = 0;
5586 	int bytes_returned = 0;
5587 	int remap = cifs_remap(cifs_sb);
5588 
5589 	__u16 params, byte_count, data_count, param_offset, offset;
5590 
5591 	cifs_dbg(FYI, "In SetEOF\n");
5592 SetEOFRetry:
5593 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5594 		      (void **) &pSMBr);
5595 	if (rc < 0)
5596 		return rc;
5597 	in_len = rc;
5598 
5599 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5600 		name_len =
5601 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5602 				       PATH_MAX, cifs_sb->local_nls, remap);
5603 		name_len++;	/* trailing null */
5604 		name_len *= 2;
5605 	} else {
5606 		name_len = copy_path_name(pSMB->FileName, file_name);
5607 	}
5608 	params = 6 + name_len;
5609 	data_count = sizeof(struct file_end_of_file_info);
5610 	pSMB->MaxParameterCount = cpu_to_le16(2);
5611 	pSMB->MaxDataCount = cpu_to_le16(4100);
5612 	pSMB->MaxSetupCount = 0;
5613 	pSMB->Reserved = 0;
5614 	pSMB->Flags = 0;
5615 	pSMB->Timeout = 0;
5616 	pSMB->Reserved2 = 0;
5617 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
5618 				InformationLevel);
5619 	offset = param_offset + params;
5620 	if (set_allocation) {
5621 		if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5622 			pSMB->InformationLevel =
5623 				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5624 		else
5625 			pSMB->InformationLevel =
5626 				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5627 	} else /* Set File Size */  {
5628 	    if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5629 		    pSMB->InformationLevel =
5630 				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5631 	    else
5632 		    pSMB->InformationLevel =
5633 				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5634 	}
5635 
5636 	parm_data =
5637 	    (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5638 				       offset);
5639 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
5640 	pSMB->DataOffset = cpu_to_le16(offset);
5641 	pSMB->SetupCount = 1;
5642 	pSMB->Reserved3 = 0;
5643 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5644 	byte_count = 3 /* pad */  + params + data_count;
5645 	pSMB->DataCount = cpu_to_le16(data_count);
5646 	pSMB->TotalDataCount = pSMB->DataCount;
5647 	pSMB->ParameterCount = cpu_to_le16(params);
5648 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5649 	pSMB->Reserved4 = 0;
5650 	in_len += byte_count;
5651 	parm_data->FileSize = cpu_to_le64(size);
5652 	pSMB->ByteCount = cpu_to_le16(byte_count);
5653 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5654 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5655 	if (rc)
5656 		cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
5657 
5658 	cifs_buf_release(pSMB);
5659 
5660 	if (rc == -EAGAIN)
5661 		goto SetEOFRetry;
5662 
5663 	return rc;
5664 }
5665 
5666 int
5667 CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5668 		   struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
5669 {
5670 	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
5671 	struct file_end_of_file_info *parm_data;
5672 	unsigned int in_len;
5673 	int rc = 0;
5674 	__u16 params, param_offset, offset, byte_count, count;
5675 
5676 	cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5677 		 (long long)size);
5678 	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5679 	if (rc < 0)
5680 		return rc;
5681 	in_len = rc;
5682 
5683 	pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5684 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5685 
5686 	params = 6;
5687 	pSMB->MaxSetupCount = 0;
5688 	pSMB->Reserved = 0;
5689 	pSMB->Flags = 0;
5690 	pSMB->Timeout = 0;
5691 	pSMB->Reserved2 = 0;
5692 	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid);
5693 	offset = param_offset + params;
5694 
5695 	count = sizeof(struct file_end_of_file_info);
5696 	pSMB->MaxParameterCount = cpu_to_le16(2);
5697 	/* BB find exact max SMB PDU from sess structure BB */
5698 	pSMB->MaxDataCount = cpu_to_le16(1000);
5699 	pSMB->SetupCount = 1;
5700 	pSMB->Reserved3 = 0;
5701 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5702 	byte_count = 3 /* pad */  + params + count;
5703 	pSMB->DataCount = cpu_to_le16(count);
5704 	pSMB->ParameterCount = cpu_to_le16(params);
5705 	pSMB->TotalDataCount = pSMB->DataCount;
5706 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5707 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
5708 	parm_data =
5709 		(struct file_end_of_file_info *)(((char *)pSMB) + offset);
5710 	pSMB->DataOffset = cpu_to_le16(offset);
5711 	parm_data->FileSize = cpu_to_le64(size);
5712 	pSMB->Fid = cfile->fid.netfid;
5713 	if (set_allocation) {
5714 		if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5715 			pSMB->InformationLevel =
5716 				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5717 		else
5718 			pSMB->InformationLevel =
5719 				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5720 	} else /* Set File Size */  {
5721 	    if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5722 		    pSMB->InformationLevel =
5723 				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
5724 	    else
5725 		    pSMB->InformationLevel =
5726 				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
5727 	}
5728 	pSMB->Reserved4 = 0;
5729 	in_len += byte_count;
5730 	pSMB->ByteCount = cpu_to_le16(byte_count);
5731 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
5732 	cifs_small_buf_release(pSMB);
5733 	if (rc) {
5734 		cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5735 			 rc);
5736 	}
5737 
5738 	/* Note: On -EAGAIN error only caller can retry on handle based calls
5739 		since file handle passed in no longer valid */
5740 
5741 	return rc;
5742 }
5743 
5744 int SMBSetInformation(const unsigned int xid, struct cifs_tcon *tcon,
5745 		      const char *fileName, __le32 attributes,
5746 		      __le64 write_time, struct cifs_sb_info *cifs_sb)
5747 {
5748 	SETATTR_REQ *pSMB;
5749 	SETATTR_RSP *pSMBr;
5750 	struct timespec64 ts;
5751 	unsigned int in_len;
5752 	int bytes_returned;
5753 	int name_len;
5754 	int rc;
5755 
5756 	cifs_dbg(FYI, "In %s path %s\n", __func__, fileName);
5757 
5758 retry:
5759 	rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5760 		      (void **) &pSMBr);
5761 	if (rc < 0)
5762 		return rc;
5763 	in_len = rc;
5764 
5765 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5766 		name_len =
5767 			cifsConvertToUTF16((__le16 *) pSMB->fileName,
5768 					   fileName, PATH_MAX,
5769 					   tcon->ses->local_nls,
5770 					   cifs_remap(cifs_sb));
5771 		name_len++;     /* trailing null */
5772 		name_len *= 2;
5773 	} else {
5774 		name_len = copy_path_name(pSMB->fileName, fileName);
5775 	}
5776 	/* Only few attributes can be set by this command, others are not accepted by Win9x. */
5777 	pSMB->attr = cpu_to_le16(le32_to_cpu(attributes) &
5778 			(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_ARCHIVE));
5779 	/* Zero write time value (in both NT and SETATTR formats) means to not change it. */
5780 	if (le64_to_cpu(write_time) != 0) {
5781 		ts = cifs_NTtimeToUnix(write_time);
5782 		pSMB->last_write_time = cpu_to_le32(ts.tv_sec);
5783 	}
5784 	pSMB->BufferFormat = 0x04;
5785 	name_len++; /* account for buffer type byte */
5786 	in_len += name_len;
5787 	pSMB->ByteCount = cpu_to_le16(name_len);
5788 
5789 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5790 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5791 	if (rc)
5792 		cifs_dbg(FYI, "Send error in %s = %d\n", __func__, rc);
5793 
5794 	cifs_buf_release(pSMB);
5795 
5796 	if (rc == -EAGAIN)
5797 		goto retry;
5798 
5799 	return rc;
5800 }
5801 
5802 /* Some legacy servers such as NT4 require that the file times be set on
5803    an open handle, rather than by pathname - this is awkward due to
5804    potential access conflicts on the open, but it is unavoidable for these
5805    old servers since the only other choice is to go from 100 nanosecond DCE
5806    time and resort to the original setpathinfo level which takes the ancient
5807    DOS time format with 2 second granularity */
5808 int
5809 CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5810 		    const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
5811 {
5812 	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
5813 	unsigned int in_len;
5814 	char *data_offset;
5815 	int rc = 0;
5816 	__u16 params, param_offset, offset, byte_count, count;
5817 
5818 	cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
5819 	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5820 	if (rc < 0)
5821 		return rc;
5822 	in_len = rc;
5823 
5824 	pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5825 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5826 
5827 	params = 6;
5828 	pSMB->MaxSetupCount = 0;
5829 	pSMB->Reserved = 0;
5830 	pSMB->Flags = 0;
5831 	pSMB->Timeout = 0;
5832 	pSMB->Reserved2 = 0;
5833 	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid);
5834 	offset = param_offset + params;
5835 
5836 	data_offset = (char *)pSMB + offset;
5837 
5838 	count = sizeof(FILE_BASIC_INFO);
5839 	pSMB->MaxParameterCount = cpu_to_le16(2);
5840 	/* BB find max SMB PDU from sess */
5841 	pSMB->MaxDataCount = cpu_to_le16(1000);
5842 	pSMB->SetupCount = 1;
5843 	pSMB->Reserved3 = 0;
5844 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5845 	byte_count = 3 /* pad */  + params + count;
5846 	pSMB->DataCount = cpu_to_le16(count);
5847 	pSMB->ParameterCount = cpu_to_le16(params);
5848 	pSMB->TotalDataCount = pSMB->DataCount;
5849 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5850 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
5851 	pSMB->DataOffset = cpu_to_le16(offset);
5852 	pSMB->Fid = fid;
5853 	if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5854 		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5855 	else
5856 		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5857 	pSMB->Reserved4 = 0;
5858 	in_len += byte_count;
5859 	pSMB->ByteCount = cpu_to_le16(byte_count);
5860 	memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5861 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
5862 	cifs_small_buf_release(pSMB);
5863 	if (rc)
5864 		cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5865 			 rc);
5866 
5867 	/* Note: On -EAGAIN error only caller can retry on handle based calls
5868 		since file handle passed in no longer valid */
5869 
5870 	return rc;
5871 }
5872 
5873 int
5874 CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5875 			  bool delete_file, __u16 fid, __u32 pid_of_opener)
5876 {
5877 	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
5878 	unsigned int in_len;
5879 	char *data_offset;
5880 	int rc = 0;
5881 	__u16 params, param_offset, offset, byte_count, count;
5882 
5883 	cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
5884 	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5885 	if (rc < 0)
5886 		return rc;
5887 	in_len = rc;
5888 
5889 	pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5890 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5891 
5892 	params = 6;
5893 	pSMB->MaxSetupCount = 0;
5894 	pSMB->Reserved = 0;
5895 	pSMB->Flags = 0;
5896 	pSMB->Timeout = 0;
5897 	pSMB->Reserved2 = 0;
5898 	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid);
5899 	offset = param_offset + params;
5900 	data_offset = (char *)(pSMB) + offset;
5901 
5902 	count = 1;
5903 	pSMB->MaxParameterCount = cpu_to_le16(2);
5904 	/* BB find max SMB PDU from sess */
5905 	pSMB->MaxDataCount = cpu_to_le16(1000);
5906 	pSMB->SetupCount = 1;
5907 	pSMB->Reserved3 = 0;
5908 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5909 	byte_count = 3 /* pad */  + params + count;
5910 	pSMB->DataCount = cpu_to_le16(count);
5911 	pSMB->ParameterCount = cpu_to_le16(params);
5912 	pSMB->TotalDataCount = pSMB->DataCount;
5913 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5914 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
5915 	pSMB->DataOffset = cpu_to_le16(offset);
5916 	pSMB->Fid = fid;
5917 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5918 	pSMB->Reserved4 = 0;
5919 	in_len += byte_count;
5920 	pSMB->ByteCount = cpu_to_le16(byte_count);
5921 	*data_offset = delete_file ? 1 : 0;
5922 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
5923 	cifs_small_buf_release(pSMB);
5924 	if (rc)
5925 		cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
5926 
5927 	return rc;
5928 }
5929 
5930 int CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5931 		       const char *fileName, const FILE_BASIC_INFO *data,
5932 		       struct cifs_sb_info *cifs_sb)
5933 {
5934 	TRANSACTION2_SPI_REQ *pSMB = NULL;
5935 	TRANSACTION2_SPI_RSP *pSMBr = NULL;
5936 	unsigned int in_len;
5937 	int name_len;
5938 	int rc = 0;
5939 	int bytes_returned = 0;
5940 	char *data_offset;
5941 	__u16 params, param_offset, offset, byte_count, count;
5942 	int remap = cifs_remap(cifs_sb);
5943 
5944 	cifs_dbg(FYI, "In SetTimes\n");
5945 
5946 SetTimesRetry:
5947 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5948 		      (void **) &pSMBr);
5949 	if (rc < 0)
5950 		return rc;
5951 	in_len = rc;
5952 
5953 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5954 		name_len =
5955 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5956 				       PATH_MAX, tcon->ses->local_nls, remap);
5957 		name_len++;	/* trailing null */
5958 		name_len *= 2;
5959 	} else {
5960 		name_len = copy_path_name(pSMB->FileName, fileName);
5961 	}
5962 
5963 	params = 6 + name_len;
5964 	count = sizeof(FILE_BASIC_INFO);
5965 	pSMB->MaxParameterCount = cpu_to_le16(2);
5966 	/* BB find max SMB PDU from sess structure BB */
5967 	pSMB->MaxDataCount = cpu_to_le16(1000);
5968 	pSMB->MaxSetupCount = 0;
5969 	pSMB->Reserved = 0;
5970 	pSMB->Flags = 0;
5971 	pSMB->Timeout = 0;
5972 	pSMB->Reserved2 = 0;
5973 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
5974 				InformationLevel);
5975 	offset = param_offset + params;
5976 	data_offset = (char *)pSMB + offsetof(typeof(*pSMB), hdr.Protocol) + offset;
5977 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
5978 	pSMB->DataOffset = cpu_to_le16(offset);
5979 	pSMB->SetupCount = 1;
5980 	pSMB->Reserved3 = 0;
5981 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5982 	byte_count = 3 /* pad */  + params + count;
5983 
5984 	pSMB->DataCount = cpu_to_le16(count);
5985 	pSMB->ParameterCount = cpu_to_le16(params);
5986 	pSMB->TotalDataCount = pSMB->DataCount;
5987 	pSMB->TotalParameterCount = pSMB->ParameterCount;
5988 	if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5989 		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5990 	else
5991 		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5992 	pSMB->Reserved4 = 0;
5993 	in_len += byte_count;
5994 	memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5995 	pSMB->ByteCount = cpu_to_le16(byte_count);
5996 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
5997 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5998 	if (rc)
5999 		cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
6000 
6001 	cifs_buf_release(pSMB);
6002 
6003 	if (rc == -EAGAIN)
6004 		goto SetTimesRetry;
6005 
6006 	return rc;
6007 }
6008 
6009 static void
6010 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6011 			const struct cifs_unix_set_info_args *args)
6012 {
6013 	u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
6014 	u64 mode = args->mode;
6015 
6016 	if (uid_valid(args->uid))
6017 		uid = from_kuid(&init_user_ns, args->uid);
6018 	if (gid_valid(args->gid))
6019 		gid = from_kgid(&init_user_ns, args->gid);
6020 
6021 	/*
6022 	 * Samba server ignores set of file size to zero due to bugs in some
6023 	 * older clients, but we should be precise - we use SetFileSize to
6024 	 * set file size and do not want to truncate file size to zero
6025 	 * accidentally as happened on one Samba server beta by putting
6026 	 * zero instead of -1 here
6027 	 */
6028 	data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6029 	data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6030 	data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6031 	data_offset->LastAccessTime = cpu_to_le64(args->atime);
6032 	data_offset->LastModificationTime = cpu_to_le64(args->mtime);
6033 	data_offset->Uid = cpu_to_le64(uid);
6034 	data_offset->Gid = cpu_to_le64(gid);
6035 	/* better to leave device as zero when it is  */
6036 	data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6037 	data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6038 	data_offset->Permissions = cpu_to_le64(mode);
6039 
6040 	if (S_ISREG(mode))
6041 		data_offset->Type = cpu_to_le32(UNIX_FILE);
6042 	else if (S_ISDIR(mode))
6043 		data_offset->Type = cpu_to_le32(UNIX_DIR);
6044 	else if (S_ISLNK(mode))
6045 		data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6046 	else if (S_ISCHR(mode))
6047 		data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6048 	else if (S_ISBLK(mode))
6049 		data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6050 	else if (S_ISFIFO(mode))
6051 		data_offset->Type = cpu_to_le32(UNIX_FIFO);
6052 	else if (S_ISSOCK(mode))
6053 		data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6054 }
6055 
6056 int
6057 CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
6058 		       const struct cifs_unix_set_info_args *args,
6059 		       u16 fid, u32 pid_of_opener)
6060 {
6061 	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
6062 	unsigned int in_len;
6063 	char *data_offset;
6064 	int rc = 0;
6065 	u16 params, param_offset, offset, byte_count, count;
6066 
6067 	cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
6068 	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6069 	if (rc < 0)
6070 		return rc;
6071 	in_len = rc;
6072 
6073 	pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6074 	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6075 
6076 	params = 6;
6077 	pSMB->MaxSetupCount = 0;
6078 	pSMB->Reserved = 0;
6079 	pSMB->Flags = 0;
6080 	pSMB->Timeout = 0;
6081 	pSMB->Reserved2 = 0;
6082 	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid);
6083 	offset = param_offset + params;
6084 
6085 	data_offset = (char *)pSMB + offset;
6086 
6087 	count = sizeof(FILE_UNIX_BASIC_INFO);
6088 
6089 	pSMB->MaxParameterCount = cpu_to_le16(2);
6090 	/* BB find max SMB PDU from sess */
6091 	pSMB->MaxDataCount = cpu_to_le16(1000);
6092 	pSMB->SetupCount = 1;
6093 	pSMB->Reserved3 = 0;
6094 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6095 	byte_count = 3 /* pad */  + params + count;
6096 	pSMB->DataCount = cpu_to_le16(count);
6097 	pSMB->ParameterCount = cpu_to_le16(params);
6098 	pSMB->TotalDataCount = pSMB->DataCount;
6099 	pSMB->TotalParameterCount = pSMB->ParameterCount;
6100 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
6101 	pSMB->DataOffset = cpu_to_le16(offset);
6102 	pSMB->Fid = fid;
6103 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6104 	pSMB->Reserved4 = 0;
6105 	in_len += byte_count;
6106 	pSMB->ByteCount = cpu_to_le16(byte_count);
6107 
6108 	cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
6109 
6110 	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, in_len, 0);
6111 	cifs_small_buf_release(pSMB);
6112 	if (rc)
6113 		cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6114 			 rc);
6115 
6116 	/* Note: On -EAGAIN error only caller can retry on handle based calls
6117 		since file handle passed in no longer valid */
6118 
6119 	return rc;
6120 }
6121 
6122 int
6123 CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
6124 		       const char *file_name,
6125 		       const struct cifs_unix_set_info_args *args,
6126 		       const struct nls_table *nls_codepage, int remap)
6127 {
6128 	TRANSACTION2_SPI_REQ *pSMB = NULL;
6129 	TRANSACTION2_SPI_RSP *pSMBr = NULL;
6130 	unsigned int in_len;
6131 	int name_len;
6132 	int rc = 0;
6133 	int bytes_returned = 0;
6134 	FILE_UNIX_BASIC_INFO *data_offset;
6135 	__u16 params, param_offset, offset, count, byte_count;
6136 
6137 	cifs_dbg(FYI, "In SetUID/GID/Mode\n");
6138 setPermsRetry:
6139 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6140 		      (void **) &pSMBr);
6141 	if (rc < 0)
6142 		return rc;
6143 	in_len = rc;
6144 
6145 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6146 		name_len =
6147 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
6148 				       PATH_MAX, nls_codepage, remap);
6149 		name_len++;	/* trailing null */
6150 		name_len *= 2;
6151 	} else {
6152 		name_len = copy_path_name(pSMB->FileName, file_name);
6153 	}
6154 
6155 	params = 6 + name_len;
6156 	count = sizeof(FILE_UNIX_BASIC_INFO);
6157 	pSMB->MaxParameterCount = cpu_to_le16(2);
6158 	/* BB find max SMB PDU from sess structure BB */
6159 	pSMB->MaxDataCount = cpu_to_le16(1000);
6160 	pSMB->MaxSetupCount = 0;
6161 	pSMB->Reserved = 0;
6162 	pSMB->Flags = 0;
6163 	pSMB->Timeout = 0;
6164 	pSMB->Reserved2 = 0;
6165 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
6166 				InformationLevel);
6167 	offset = param_offset + params;
6168 	data_offset = (FILE_UNIX_BASIC_INFO *)((char *) pSMB + offset);
6169 	memset(data_offset, 0, count);
6170 	pSMB->DataOffset = cpu_to_le16(offset);
6171 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
6172 	pSMB->SetupCount = 1;
6173 	pSMB->Reserved3 = 0;
6174 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6175 	byte_count = 3 /* pad */  + params + count;
6176 	pSMB->ParameterCount = cpu_to_le16(params);
6177 	pSMB->DataCount = cpu_to_le16(count);
6178 	pSMB->TotalParameterCount = pSMB->ParameterCount;
6179 	pSMB->TotalDataCount = pSMB->DataCount;
6180 	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6181 	pSMB->Reserved4 = 0;
6182 	in_len += byte_count;
6183 
6184 	cifs_fill_unix_set_info(data_offset, args);
6185 
6186 	pSMB->ByteCount = cpu_to_le16(byte_count);
6187 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
6188 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6189 	if (rc)
6190 		cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
6191 
6192 	cifs_buf_release(pSMB);
6193 	if (rc == -EAGAIN)
6194 		goto setPermsRetry;
6195 	return rc;
6196 }
6197 
6198 #ifdef CONFIG_CIFS_XATTR
6199 /*
6200  * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6201  * function used by listxattr and getxattr type calls. When ea_name is set,
6202  * it looks for that attribute name and stuffs that value into the EAData
6203  * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6204  * buffer. In both cases, the return value is either the length of the
6205  * resulting data or a negative error code. If EAData is a NULL pointer then
6206  * the data isn't copied to it, but the length is returned.
6207  */
6208 ssize_t
6209 CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
6210 		const unsigned char *searchName, const unsigned char *ea_name,
6211 		char *EAData, size_t buf_size,
6212 		struct cifs_sb_info *cifs_sb)
6213 {
6214 		/* BB assumes one setup word */
6215 	TRANSACTION2_QPI_REQ *pSMB = NULL;
6216 	TRANSACTION2_QPI_RSP *pSMBr = NULL;
6217 	int remap = cifs_remap(cifs_sb);
6218 	struct nls_table *nls_codepage = cifs_sb->local_nls;
6219 	unsigned int in_len;
6220 	int rc = 0;
6221 	int bytes_returned;
6222 	int list_len;
6223 	u32 ea_list_len;
6224 	struct fealist *ea_response_data;
6225 	struct fea *temp_fea;
6226 	char *temp_ptr;
6227 	char *byte_area;
6228 	char *end_of_smb;
6229 	__u16 params, byte_count, data_offset;
6230 	unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
6231 
6232 	cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
6233 QAllEAsRetry:
6234 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6235 		      (void **) &pSMBr);
6236 	if (rc < 0)
6237 		return rc;
6238 	in_len = rc;
6239 
6240 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6241 		list_len =
6242 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6243 				       PATH_MAX, nls_codepage, remap);
6244 		list_len++;	/* trailing null */
6245 		list_len *= 2;
6246 	} else {
6247 		list_len = copy_path_name(pSMB->FileName, searchName);
6248 	}
6249 
6250 	params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
6251 	pSMB->TotalDataCount = 0;
6252 	pSMB->MaxParameterCount = cpu_to_le16(2);
6253 	/* BB find exact max SMB PDU from sess structure BB */
6254 	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
6255 	pSMB->MaxSetupCount = 0;
6256 	pSMB->Reserved = 0;
6257 	pSMB->Flags = 0;
6258 	pSMB->Timeout = 0;
6259 	pSMB->Reserved2 = 0;
6260 	pSMB->ParameterOffset = cpu_to_le16(offsetof(
6261 		struct smb_com_transaction2_qpi_req, InformationLevel));
6262 	pSMB->DataCount = 0;
6263 	pSMB->DataOffset = 0;
6264 	pSMB->SetupCount = 1;
6265 	pSMB->Reserved3 = 0;
6266 	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6267 	byte_count = params + 1 /* pad */ ;
6268 	pSMB->TotalParameterCount = cpu_to_le16(params);
6269 	pSMB->ParameterCount = pSMB->TotalParameterCount;
6270 	pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6271 	pSMB->Reserved4 = 0;
6272 	in_len += byte_count;
6273 	pSMB->ByteCount = cpu_to_le16(byte_count);
6274 
6275 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
6276 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6277 	if (rc) {
6278 		cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
6279 		goto QAllEAsOut;
6280 	}
6281 
6282 
6283 	/* BB also check enough total bytes returned */
6284 	/* BB we need to improve the validity checking
6285 	of these trans2 responses */
6286 
6287 	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6288 	if (rc || get_bcc(&pSMBr->hdr) < 4) {
6289 		rc = smb_EIO2(smb_eio_trace_qalleas_bcc_too_small,
6290 			      get_bcc(&pSMBr->hdr), 4);
6291 		goto QAllEAsOut;
6292 	}
6293 
6294 	/* check that length of list is not more than bcc */
6295 	/* check that each entry does not go beyond length
6296 	   of list */
6297 	/* check that each element of each entry does not
6298 	   go beyond end of list */
6299 	/* validate_trans2_offsets() */
6300 	/* BB check if start of smb + data_offset > &bcc+ bcc */
6301 
6302 	data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6303 	ea_response_data = (struct fealist *)
6304 				(((char *) &pSMBr->hdr.Protocol) + data_offset);
6305 
6306 	byte_area = pByteArea(&pSMBr->hdr);
6307 	end_of_smb = byte_area + get_bcc(&pSMBr->hdr);
6308 	if ((char *)ea_response_data < byte_area ||
6309 	    (char *)ea_response_data >
6310 	    end_of_smb - sizeof(ea_response_data->list_len)) {
6311 		rc = smb_EIO2(smb_eio_trace_qalleas_overlong,
6312 			      data_offset, get_bcc(&pSMBr->hdr));
6313 		goto QAllEAsOut;
6314 	}
6315 
6316 	ea_list_len = le32_to_cpu(ea_response_data->list_len);
6317 	cifs_dbg(FYI, "%s: ea length: %u\n", __func__, ea_list_len);
6318 	if (ea_list_len <= 8) {
6319 		cifs_dbg(FYI, "empty EA list returned from server\n");
6320 		/* didn't find the named attribute */
6321 		if (ea_name)
6322 			rc = -ENODATA;
6323 		goto QAllEAsOut;
6324 	}
6325 
6326 	/* make sure list_len doesn't go past end of SMB */
6327 	if (ea_list_len > end_of_smb - (char *)ea_response_data) {
6328 		cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
6329 		rc = smb_EIO2(smb_eio_trace_qalleas_overlong,
6330 			      ea_list_len,
6331 			      end_of_smb - (char *)ea_response_data);
6332 		goto QAllEAsOut;
6333 	}
6334 	list_len = ea_list_len;
6335 
6336 	/* account for ea list len */
6337 	list_len -= 4;
6338 	temp_fea = &ea_response_data->list;
6339 	temp_ptr = (char *)temp_fea;
6340 	while (list_len > 0) {
6341 		unsigned int name_len;
6342 		__u16 value_len;
6343 
6344 		list_len -= 4;
6345 		temp_ptr += 4;
6346 		/* make sure we can read name_len and value_len */
6347 		if (list_len < 0) {
6348 			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6349 			rc = smb_EIO1(smb_eio_trace_qalleas_ea_overlong, list_len);
6350 			goto QAllEAsOut;
6351 		}
6352 
6353 		name_len = temp_fea->name_len;
6354 		value_len = le16_to_cpu(temp_fea->value_len);
6355 		list_len -= name_len + 1 + value_len;
6356 		if (list_len < 0) {
6357 			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6358 			rc = smb_EIO1(smb_eio_trace_qalleas_ea_overlong, list_len);
6359 			goto QAllEAsOut;
6360 		}
6361 
6362 		if (ea_name) {
6363 			if (ea_name_len == name_len &&
6364 			    memcmp(ea_name, temp_ptr, name_len) == 0) {
6365 				temp_ptr += name_len + 1;
6366 				rc = value_len;
6367 				if (buf_size == 0)
6368 					goto QAllEAsOut;
6369 				if ((size_t)value_len > buf_size) {
6370 					rc = -ERANGE;
6371 					goto QAllEAsOut;
6372 				}
6373 				memcpy(EAData, temp_ptr, value_len);
6374 				goto QAllEAsOut;
6375 			}
6376 		} else {
6377 			/* account for prefix user. and trailing null */
6378 			rc += (5 + 1 + name_len);
6379 			if (rc < (int) buf_size) {
6380 				memcpy(EAData, "user.", 5);
6381 				EAData += 5;
6382 				memcpy(EAData, temp_ptr, name_len);
6383 				EAData += name_len;
6384 				/* null terminate name */
6385 				*EAData = 0;
6386 				++EAData;
6387 			} else if (buf_size == 0) {
6388 				/* skip copy - calc size only */
6389 			} else {
6390 				/* stop before overrun buffer */
6391 				rc = -ERANGE;
6392 				break;
6393 			}
6394 		}
6395 		temp_ptr += name_len + 1 + value_len;
6396 		temp_fea = (struct fea *)temp_ptr;
6397 	}
6398 
6399 	/* didn't find the named attribute */
6400 	if (ea_name)
6401 		rc = -ENODATA;
6402 
6403 QAllEAsOut:
6404 	cifs_buf_release(pSMB);
6405 	if (rc == -EAGAIN)
6406 		goto QAllEAsRetry;
6407 
6408 	return (ssize_t)rc;
6409 }
6410 
6411 int CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6412 		 const char *fileName, const char *ea_name,
6413 		 const void *ea_value, const __u16 ea_value_len,
6414 		 struct cifs_sb_info *cifs_sb)
6415 {
6416 	struct smb_com_transaction2_spi_req *pSMB = NULL;
6417 	struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6418 	struct fealist *parm_data;
6419 	unsigned int in_len;
6420 	int name_len;
6421 	int rc = 0;
6422 	int bytes_returned = 0;
6423 	__u16 params, param_offset;
6424 	unsigned int byte_count, offset, count;
6425 	int remap = cifs_remap(cifs_sb);
6426 	unsigned int total_len;
6427 
6428 	cifs_dbg(FYI, "In SetEA\n");
6429 SetEARetry:
6430 	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6431 		      (void **) &pSMBr);
6432 	if (rc < 0)
6433 		return rc;
6434 	in_len = rc;
6435 
6436 	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6437 		name_len =
6438 		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6439 				       PATH_MAX, tcon->ses->local_nls, remap);
6440 		name_len++;	/* trailing null */
6441 		name_len *= 2;
6442 	} else {
6443 		name_len = copy_path_name(pSMB->FileName, fileName);
6444 	}
6445 
6446 	params = 6 + name_len;
6447 
6448 	/* done calculating parms using name_len of file name,
6449 	now use name_len to calculate length of ea name
6450 	we are going to create in the inode xattrs */
6451 	if (ea_name == NULL)
6452 		name_len = 0;
6453 	else
6454 		name_len = strnlen(ea_name, 255);
6455 
6456 	count = sizeof(*parm_data) + 1 + ea_value_len + name_len;
6457 	pSMB->MaxParameterCount = cpu_to_le16(2);
6458 	/* BB find max SMB PDU from sess */
6459 	pSMB->MaxDataCount = cpu_to_le16(1000);
6460 	pSMB->MaxSetupCount = 0;
6461 	pSMB->Reserved = 0;
6462 	pSMB->Flags = 0;
6463 	pSMB->Timeout = 0;
6464 	pSMB->Reserved2 = 0;
6465 	param_offset = offsetof(struct smb_com_transaction2_spi_req,
6466 				InformationLevel);
6467 	offset = param_offset + params;
6468 	pSMB->InformationLevel =
6469 		cpu_to_le16(SMB_SET_FILE_EA);
6470 
6471 	parm_data = (void *)pSMB + offset;
6472 	pSMB->ParameterOffset = cpu_to_le16(param_offset);
6473 	pSMB->DataOffset = cpu_to_le16(offset);
6474 	pSMB->SetupCount = 1;
6475 	pSMB->Reserved3 = 0;
6476 	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6477 	byte_count = 3 /* pad */  + params + count;
6478 	if (check_add_overflow(in_len, byte_count, &total_len) ||
6479 	    byte_count > U16_MAX ||
6480 	    total_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
6481 		cifs_dbg(VFS, "EA request too large: %u bytes\n", total_len);
6482 		cifs_buf_release(pSMB);
6483 		return -E2BIG;
6484 	}
6485 	pSMB->DataCount = cpu_to_le16(count);
6486 	parm_data->list_len = cpu_to_le32(count);
6487 	parm_data->list.EA_flags = 0;
6488 	/* we checked above that name len is less than 255 */
6489 	parm_data->list.name_len = (__u8)name_len;
6490 	/* EA names are always ASCII and NUL-terminated */
6491 	strscpy(parm_data->list.name, ea_name ?: "", name_len + 1);
6492 	parm_data->list.value_len = cpu_to_le16(ea_value_len);
6493 	/* caller ensures that ea_value_len is less than 64K but
6494 	we need to ensure that it fits within the smb */
6495 
6496 	/*BB add length check to see if it would fit in
6497 	     negotiated SMB buffer size BB */
6498 	/* if (ea_value_len > buffer_size - 512 (enough for header)) */
6499 	if (ea_value_len)
6500 		memcpy(parm_data->list.name + name_len + 1,
6501 		       ea_value, ea_value_len);
6502 
6503 	pSMB->TotalDataCount = pSMB->DataCount;
6504 	pSMB->ParameterCount = cpu_to_le16(params);
6505 	pSMB->TotalParameterCount = pSMB->ParameterCount;
6506 	pSMB->Reserved4 = 0;
6507 	in_len += byte_count;
6508 	pSMB->ByteCount = cpu_to_le16(byte_count);
6509 	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB, in_len,
6510 			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6511 	if (rc)
6512 		cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
6513 
6514 	cifs_buf_release(pSMB);
6515 
6516 	if (rc == -EAGAIN)
6517 		goto SetEARetry;
6518 
6519 	return rc;
6520 }
6521 #endif
6522