xref: /linux/fs/smb/client/smb1ops.c (revision c5ec7f49b480db0dfc83f395755b1c2a7c979920)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  SMB1 (CIFS) version specific operations
4  *
5  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6  */
7 
8 #include <linux/pagemap.h>
9 #include <linux/vfs.h>
10 #include <uapi/linux/magic.h>
11 #include "cifsglob.h"
12 #include "cifsproto.h"
13 #include "cifs_debug.h"
14 #include "cifspdu.h"
15 #include "cifs_unicode.h"
16 #include "fs_context.h"
17 #include "nterr.h"
18 #include "smberr.h"
19 #include "reparse.h"
20 
21 /*
22  * An NT cancel request header looks just like the original request except:
23  *
24  * The Command is SMB_COM_NT_CANCEL
25  * The WordCount is zeroed out
26  * The ByteCount is zeroed out
27  *
28  * This function mangles an existing request buffer into a
29  * SMB_COM_NT_CANCEL request and then sends it.
30  */
31 static int
32 send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
33 	       struct mid_q_entry *mid)
34 {
35 	int rc = 0;
36 	struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
37 
38 	/* -4 for RFC1001 length and +2 for BCC field */
39 	in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4  + 2);
40 	in_buf->Command = SMB_COM_NT_CANCEL;
41 	in_buf->WordCount = 0;
42 	put_bcc(0, in_buf);
43 
44 	cifs_server_lock(server);
45 	rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
46 	if (rc) {
47 		cifs_server_unlock(server);
48 		return rc;
49 	}
50 
51 	/*
52 	 * The response to this call was already factored into the sequence
53 	 * number when the call went out, so we must adjust it back downward
54 	 * after signing here.
55 	 */
56 	--server->sequence_number;
57 	rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
58 	if (rc < 0)
59 		server->sequence_number--;
60 
61 	cifs_server_unlock(server);
62 
63 	cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
64 		 get_mid(in_buf), rc);
65 
66 	return rc;
67 }
68 
69 static bool
70 cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
71 {
72 	return ob1->fid.netfid == ob2->fid.netfid;
73 }
74 
75 static unsigned int
76 cifs_read_data_offset(char *buf)
77 {
78 	READ_RSP *rsp = (READ_RSP *)buf;
79 	return le16_to_cpu(rsp->DataOffset);
80 }
81 
82 static unsigned int
83 cifs_read_data_length(char *buf, bool in_remaining)
84 {
85 	READ_RSP *rsp = (READ_RSP *)buf;
86 	/* It's a bug reading remaining data for SMB1 packets */
87 	WARN_ON(in_remaining);
88 	return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
89 	       le16_to_cpu(rsp->DataLength);
90 }
91 
92 static struct mid_q_entry *
93 cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
94 {
95 	struct smb_hdr *buf = (struct smb_hdr *)buffer;
96 	struct mid_q_entry *mid;
97 
98 	spin_lock(&server->mid_lock);
99 	list_for_each_entry(mid, &server->pending_mid_q, qhead) {
100 		if (compare_mid(mid->mid, buf) &&
101 		    mid->mid_state == MID_REQUEST_SUBMITTED &&
102 		    le16_to_cpu(mid->command) == buf->Command) {
103 			kref_get(&mid->refcount);
104 			spin_unlock(&server->mid_lock);
105 			return mid;
106 		}
107 	}
108 	spin_unlock(&server->mid_lock);
109 	return NULL;
110 }
111 
112 static void
113 cifs_add_credits(struct TCP_Server_Info *server,
114 		 struct cifs_credits *credits, const int optype)
115 {
116 	spin_lock(&server->req_lock);
117 	server->credits += credits->value;
118 	server->in_flight--;
119 	spin_unlock(&server->req_lock);
120 	wake_up(&server->request_q);
121 }
122 
123 static void
124 cifs_set_credits(struct TCP_Server_Info *server, const int val)
125 {
126 	spin_lock(&server->req_lock);
127 	server->credits = val;
128 	server->oplocks = val > 1 ? enable_oplocks : false;
129 	spin_unlock(&server->req_lock);
130 }
131 
132 static int *
133 cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
134 {
135 	return &server->credits;
136 }
137 
138 static unsigned int
139 cifs_get_credits(struct mid_q_entry *mid)
140 {
141 	return 1;
142 }
143 
144 /*
145  * Find a free multiplex id (SMB mid). Otherwise there could be
146  * mid collisions which might cause problems, demultiplexing the
147  * wrong response to this request. Multiplex ids could collide if
148  * one of a series requests takes much longer than the others, or
149  * if a very large number of long lived requests (byte range
150  * locks or FindNotify requests) are pending. No more than
151  * 64K-1 requests can be outstanding at one time. If no
152  * mids are available, return zero. A future optimization
153  * could make the combination of mids and uid the key we use
154  * to demultiplex on (rather than mid alone).
155  * In addition to the above check, the cifs demultiplex
156  * code already used the command code as a secondary
157  * check of the frame and if signing is negotiated the
158  * response would be discarded if the mid were the same
159  * but the signature was wrong. Since the mid is not put in the
160  * pending queue until later (when it is about to be dispatched)
161  * we do have to limit the number of outstanding requests
162  * to somewhat less than 64K-1 although it is hard to imagine
163  * so many threads being in the vfs at one time.
164  */
165 static __u64
166 cifs_get_next_mid(struct TCP_Server_Info *server)
167 {
168 	__u64 mid = 0;
169 	__u16 last_mid, cur_mid;
170 	bool collision, reconnect = false;
171 
172 	spin_lock(&server->mid_lock);
173 
174 	/* mid is 16 bit only for CIFS/SMB */
175 	cur_mid = (__u16)((server->CurrentMid) & 0xffff);
176 	/* we do not want to loop forever */
177 	last_mid = cur_mid;
178 	cur_mid++;
179 	/* avoid 0xFFFF MID */
180 	if (cur_mid == 0xffff)
181 		cur_mid++;
182 
183 	/*
184 	 * This nested loop looks more expensive than it is.
185 	 * In practice the list of pending requests is short,
186 	 * fewer than 50, and the mids are likely to be unique
187 	 * on the first pass through the loop unless some request
188 	 * takes longer than the 64 thousand requests before it
189 	 * (and it would also have to have been a request that
190 	 * did not time out).
191 	 */
192 	while (cur_mid != last_mid) {
193 		struct mid_q_entry *mid_entry;
194 		unsigned int num_mids;
195 
196 		collision = false;
197 		if (cur_mid == 0)
198 			cur_mid++;
199 
200 		num_mids = 0;
201 		list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
202 			++num_mids;
203 			if (mid_entry->mid == cur_mid &&
204 			    mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
205 				/* This mid is in use, try a different one */
206 				collision = true;
207 				break;
208 			}
209 		}
210 
211 		/*
212 		 * if we have more than 32k mids in the list, then something
213 		 * is very wrong. Possibly a local user is trying to DoS the
214 		 * box by issuing long-running calls and SIGKILL'ing them. If
215 		 * we get to 2^16 mids then we're in big trouble as this
216 		 * function could loop forever.
217 		 *
218 		 * Go ahead and assign out the mid in this situation, but force
219 		 * an eventual reconnect to clean out the pending_mid_q.
220 		 */
221 		if (num_mids > 32768)
222 			reconnect = true;
223 
224 		if (!collision) {
225 			mid = (__u64)cur_mid;
226 			server->CurrentMid = mid;
227 			break;
228 		}
229 		cur_mid++;
230 	}
231 	spin_unlock(&server->mid_lock);
232 
233 	if (reconnect) {
234 		cifs_signal_cifsd_for_reconnect(server, false);
235 	}
236 
237 	return mid;
238 }
239 
240 /*
241 	return codes:
242 		0	not a transact2, or all data present
243 		>0	transact2 with that much data missing
244 		-EINVAL	invalid transact2
245  */
246 static int
247 check2ndT2(char *buf)
248 {
249 	struct smb_hdr *pSMB = (struct smb_hdr *)buf;
250 	struct smb_t2_rsp *pSMBt;
251 	int remaining;
252 	__u16 total_data_size, data_in_this_rsp;
253 
254 	if (pSMB->Command != SMB_COM_TRANSACTION2)
255 		return 0;
256 
257 	/* check for plausible wct, bcc and t2 data and parm sizes */
258 	/* check for parm and data offset going beyond end of smb */
259 	if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
260 		cifs_dbg(FYI, "Invalid transact2 word count\n");
261 		return -EINVAL;
262 	}
263 
264 	pSMBt = (struct smb_t2_rsp *)pSMB;
265 
266 	total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
267 	data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
268 
269 	if (total_data_size == data_in_this_rsp)
270 		return 0;
271 	else if (total_data_size < data_in_this_rsp) {
272 		cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
273 			 total_data_size, data_in_this_rsp);
274 		return -EINVAL;
275 	}
276 
277 	remaining = total_data_size - data_in_this_rsp;
278 
279 	cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
280 		 remaining);
281 	if (total_data_size > CIFSMaxBufSize) {
282 		cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
283 			 total_data_size, CIFSMaxBufSize);
284 		return -EINVAL;
285 	}
286 	return remaining;
287 }
288 
289 static int
290 coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
291 {
292 	struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
293 	struct smb_t2_rsp *pSMBt  = (struct smb_t2_rsp *)target_hdr;
294 	char *data_area_of_tgt;
295 	char *data_area_of_src;
296 	int remaining;
297 	unsigned int byte_count, total_in_tgt;
298 	__u16 tgt_total_cnt, src_total_cnt, total_in_src;
299 
300 	src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
301 	tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
302 
303 	if (tgt_total_cnt != src_total_cnt)
304 		cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
305 			 src_total_cnt, tgt_total_cnt);
306 
307 	total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
308 
309 	remaining = tgt_total_cnt - total_in_tgt;
310 
311 	if (remaining < 0) {
312 		cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
313 			 tgt_total_cnt, total_in_tgt);
314 		return -EPROTO;
315 	}
316 
317 	if (remaining == 0) {
318 		/* nothing to do, ignore */
319 		cifs_dbg(FYI, "no more data remains\n");
320 		return 0;
321 	}
322 
323 	total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
324 	if (remaining < total_in_src)
325 		cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
326 
327 	/* find end of first SMB data area */
328 	data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
329 				get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
330 
331 	/* validate target area */
332 	data_area_of_src = (char *)&pSMBs->hdr.Protocol +
333 				get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
334 
335 	data_area_of_tgt += total_in_tgt;
336 
337 	total_in_tgt += total_in_src;
338 	/* is the result too big for the field? */
339 	if (total_in_tgt > USHRT_MAX) {
340 		cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
341 			 total_in_tgt);
342 		return -EPROTO;
343 	}
344 	put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
345 
346 	/* fix up the BCC */
347 	byte_count = get_bcc(target_hdr);
348 	byte_count += total_in_src;
349 	/* is the result too big for the field? */
350 	if (byte_count > USHRT_MAX) {
351 		cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
352 		return -EPROTO;
353 	}
354 	put_bcc(byte_count, target_hdr);
355 
356 	byte_count = be32_to_cpu(target_hdr->smb_buf_length);
357 	byte_count += total_in_src;
358 	/* don't allow buffer to overflow */
359 	if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
360 		cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
361 			 byte_count);
362 		return -ENOBUFS;
363 	}
364 	target_hdr->smb_buf_length = cpu_to_be32(byte_count);
365 
366 	/* copy second buffer into end of first buffer */
367 	memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
368 
369 	if (remaining != total_in_src) {
370 		/* more responses to go */
371 		cifs_dbg(FYI, "waiting for more secondary responses\n");
372 		return 1;
373 	}
374 
375 	/* we are done */
376 	cifs_dbg(FYI, "found the last secondary response\n");
377 	return 0;
378 }
379 
380 static void
381 cifs_downgrade_oplock(struct TCP_Server_Info *server,
382 		      struct cifsInodeInfo *cinode, __u32 oplock,
383 		      __u16 epoch, bool *purge_cache)
384 {
385 	cifs_set_oplock_level(cinode, oplock);
386 }
387 
388 static bool
389 cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
390 		  char *buf, int malformed)
391 {
392 	if (malformed)
393 		return false;
394 	if (check2ndT2(buf) <= 0)
395 		return false;
396 	mid->multiRsp = true;
397 	if (mid->resp_buf) {
398 		/* merge response - fix up 1st*/
399 		malformed = coalesce_t2(buf, mid->resp_buf);
400 		if (malformed > 0)
401 			return true;
402 		/* All parts received or packet is malformed. */
403 		mid->multiEnd = true;
404 		dequeue_mid(mid, malformed);
405 		return true;
406 	}
407 	if (!server->large_buf) {
408 		/*FIXME: switch to already allocated largebuf?*/
409 		cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n");
410 	} else {
411 		/* Have first buffer */
412 		mid->resp_buf = buf;
413 		mid->large_buf = true;
414 		server->bigbuf = NULL;
415 	}
416 	return true;
417 }
418 
419 static bool
420 cifs_need_neg(struct TCP_Server_Info *server)
421 {
422 	return server->maxBuf == 0;
423 }
424 
425 static int
426 cifs_negotiate(const unsigned int xid,
427 	       struct cifs_ses *ses,
428 	       struct TCP_Server_Info *server)
429 {
430 	int rc;
431 	rc = CIFSSMBNegotiate(xid, ses, server);
432 	return rc;
433 }
434 
435 static unsigned int
436 smb1_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
437 {
438 	__u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
439 	struct TCP_Server_Info *server = tcon->ses->server;
440 	unsigned int wsize;
441 
442 	/* start with specified wsize, or default */
443 	if (ctx->got_wsize)
444 		wsize = ctx->vol_wsize;
445 	else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
446 		wsize = CIFS_DEFAULT_IOSIZE;
447 	else
448 		wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
449 
450 	/* can server support 24-bit write sizes? (via UNIX extensions) */
451 	if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
452 		wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
453 
454 	/*
455 	 * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
456 	 * Limit it to max buffer offered by the server, minus the size of the
457 	 * WRITEX header, not including the 4 byte RFC1001 length.
458 	 */
459 	if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
460 	    (!(server->capabilities & CAP_UNIX) && server->sign))
461 		wsize = min_t(unsigned int, wsize,
462 				server->maxBuf - sizeof(WRITE_REQ) + 4);
463 
464 	/* hard limit of CIFS_MAX_WSIZE */
465 	wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
466 
467 	return wsize;
468 }
469 
470 static unsigned int
471 smb1_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
472 {
473 	__u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
474 	struct TCP_Server_Info *server = tcon->ses->server;
475 	unsigned int rsize, defsize;
476 
477 	/*
478 	 * Set default value...
479 	 *
480 	 * HACK alert! Ancient servers have very small buffers. Even though
481 	 * MS-CIFS indicates that servers are only limited by the client's
482 	 * bufsize for reads, testing against win98se shows that it throws
483 	 * INVALID_PARAMETER errors if you try to request too large a read.
484 	 * OS/2 just sends back short reads.
485 	 *
486 	 * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
487 	 * it can't handle a read request larger than its MaxBufferSize either.
488 	 */
489 	if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
490 		defsize = CIFS_DEFAULT_IOSIZE;
491 	else if (server->capabilities & CAP_LARGE_READ_X)
492 		defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
493 	else
494 		defsize = server->maxBuf - sizeof(READ_RSP);
495 
496 	rsize = ctx->got_rsize ? ctx->vol_rsize : defsize;
497 
498 	/*
499 	 * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
500 	 * the client's MaxBufferSize.
501 	 */
502 	if (!(server->capabilities & CAP_LARGE_READ_X))
503 		rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
504 
505 	/* hard limit of CIFS_MAX_RSIZE */
506 	rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
507 
508 	return rsize;
509 }
510 
511 static void
512 cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
513 	      struct cifs_sb_info *cifs_sb)
514 {
515 	CIFSSMBQFSDeviceInfo(xid, tcon);
516 	CIFSSMBQFSAttributeInfo(xid, tcon);
517 }
518 
519 static int
520 cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
521 			struct cifs_sb_info *cifs_sb, const char *full_path)
522 {
523 	int rc;
524 	FILE_ALL_INFO *file_info;
525 
526 	file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
527 	if (file_info == NULL)
528 		return -ENOMEM;
529 
530 	rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
531 			      0 /* not legacy */, cifs_sb->local_nls,
532 			      cifs_remap(cifs_sb));
533 
534 	if (rc == -EOPNOTSUPP || rc == -EINVAL)
535 		rc = SMBQueryInformation(xid, tcon, full_path, file_info,
536 				cifs_sb->local_nls, cifs_remap(cifs_sb));
537 	kfree(file_info);
538 	return rc;
539 }
540 
541 static int cifs_query_path_info(const unsigned int xid,
542 				struct cifs_tcon *tcon,
543 				struct cifs_sb_info *cifs_sb,
544 				const char *full_path,
545 				struct cifs_open_info_data *data)
546 {
547 	int rc = -EOPNOTSUPP;
548 	FILE_ALL_INFO fi = {};
549 	struct cifs_search_info search_info = {};
550 	bool non_unicode_wildcard = false;
551 
552 	data->reparse_point = false;
553 	data->adjust_tz = false;
554 
555 	/*
556 	 * First try CIFSSMBQPathInfo() function which returns more info
557 	 * (NumberOfLinks) than CIFSFindFirst() fallback function.
558 	 * Some servers like Win9x do not support SMB_QUERY_FILE_ALL_INFO over
559 	 * TRANS2_QUERY_PATH_INFORMATION, but supports it with filehandle over
560 	 * TRANS2_QUERY_FILE_INFORMATION (function CIFSSMBQFileInfo(). But SMB
561 	 * Open command on non-NT servers works only for files, does not work
562 	 * for directories. And moreover Win9x SMB server returns bogus data in
563 	 * SMB_QUERY_FILE_ALL_INFO Attributes field. So for non-NT servers,
564 	 * do not even use CIFSSMBQPathInfo() or CIFSSMBQFileInfo() function.
565 	 */
566 	if (tcon->ses->capabilities & CAP_NT_SMBS)
567 		rc = CIFSSMBQPathInfo(xid, tcon, full_path, &fi, 0 /* not legacy */,
568 				      cifs_sb->local_nls, cifs_remap(cifs_sb));
569 
570 	/*
571 	 * Non-UNICODE variant of fallback functions below expands wildcards,
572 	 * so they cannot be used for querying paths with wildcard characters.
573 	 */
574 	if (rc && !(tcon->ses->capabilities & CAP_UNICODE) && strpbrk(full_path, "*?\"><"))
575 		non_unicode_wildcard = true;
576 
577 	/*
578 	 * Then fallback to CIFSFindFirst() which works also with non-NT servers
579 	 * but does not does not provide NumberOfLinks.
580 	 */
581 	if ((rc == -EOPNOTSUPP || rc == -EINVAL) &&
582 	    !non_unicode_wildcard) {
583 		if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find))
584 			search_info.info_level = SMB_FIND_FILE_INFO_STANDARD;
585 		else
586 			search_info.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
587 		rc = CIFSFindFirst(xid, tcon, full_path, cifs_sb, NULL,
588 				   CIFS_SEARCH_CLOSE_ALWAYS | CIFS_SEARCH_CLOSE_AT_END,
589 				   &search_info, false);
590 		if (rc == 0) {
591 			if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find)) {
592 				FIND_FILE_STANDARD_INFO *di;
593 				int offset = tcon->ses->server->timeAdj;
594 
595 				di = (FIND_FILE_STANDARD_INFO *)search_info.srch_entries_start;
596 				fi.CreationTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
597 						di->CreationDate, di->CreationTime, offset)));
598 				fi.LastAccessTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
599 						di->LastAccessDate, di->LastAccessTime, offset)));
600 				fi.LastWriteTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
601 						di->LastWriteDate, di->LastWriteTime, offset)));
602 				fi.ChangeTime = fi.LastWriteTime;
603 				fi.Attributes = cpu_to_le32(le16_to_cpu(di->Attributes));
604 				fi.AllocationSize = cpu_to_le64(le32_to_cpu(di->AllocationSize));
605 				fi.EndOfFile = cpu_to_le64(le32_to_cpu(di->DataSize));
606 			} else {
607 				FILE_FULL_DIRECTORY_INFO *di;
608 
609 				di = (FILE_FULL_DIRECTORY_INFO *)search_info.srch_entries_start;
610 				fi.CreationTime = di->CreationTime;
611 				fi.LastAccessTime = di->LastAccessTime;
612 				fi.LastWriteTime = di->LastWriteTime;
613 				fi.ChangeTime = di->ChangeTime;
614 				fi.Attributes = di->ExtFileAttributes;
615 				fi.AllocationSize = di->AllocationSize;
616 				fi.EndOfFile = di->EndOfFile;
617 				fi.EASize = di->EaSize;
618 			}
619 			fi.NumberOfLinks = cpu_to_le32(1);
620 			fi.DeletePending = 0;
621 			fi.Directory = !!(le32_to_cpu(fi.Attributes) & ATTR_DIRECTORY);
622 			cifs_buf_release(search_info.ntwrk_buf_start);
623 		} else if (!full_path[0]) {
624 			/*
625 			 * CIFSFindFirst() does not work on root path if the
626 			 * root path was exported on the server from the top
627 			 * level path (drive letter).
628 			 */
629 			rc = -EOPNOTSUPP;
630 		}
631 	}
632 
633 	/*
634 	 * If everything failed then fallback to the legacy SMB command
635 	 * SMB_COM_QUERY_INFORMATION which works with all servers, but
636 	 * provide just few information.
637 	 */
638 	if ((rc == -EOPNOTSUPP || rc == -EINVAL) && !non_unicode_wildcard) {
639 		rc = SMBQueryInformation(xid, tcon, full_path, &fi, cifs_sb->local_nls,
640 					 cifs_remap(cifs_sb));
641 		data->adjust_tz = true;
642 	} else if ((rc == -EOPNOTSUPP || rc == -EINVAL) && non_unicode_wildcard) {
643 		/* Path with non-UNICODE wildcard character cannot exist. */
644 		rc = -ENOENT;
645 	}
646 
647 	if (!rc) {
648 		move_cifs_info_to_smb2(&data->fi, &fi);
649 		data->reparse_point = le32_to_cpu(fi.Attributes) & ATTR_REPARSE;
650 	}
651 
652 #ifdef CONFIG_CIFS_XATTR
653 	/*
654 	 * For WSL CHR and BLK reparse points it is required to fetch
655 	 * EA $LXDEV which contains major and minor device numbers.
656 	 */
657 	if (!rc && data->reparse_point) {
658 		struct smb2_file_full_ea_info *ea;
659 
660 		ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
661 		rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_DEV,
662 				    &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
663 				    SMB2_WSL_XATTR_DEV_SIZE, cifs_sb);
664 		if (rc == SMB2_WSL_XATTR_DEV_SIZE) {
665 			ea->next_entry_offset = cpu_to_le32(0);
666 			ea->flags = 0;
667 			ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
668 			ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_DEV_SIZE);
669 			memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_DEV, SMB2_WSL_XATTR_NAME_LEN + 1);
670 			data->wsl.eas_len = sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
671 					    SMB2_WSL_XATTR_DEV_SIZE;
672 			rc = 0;
673 		} else if (rc >= 0) {
674 			/* It is an error if EA $LXDEV has wrong size. */
675 			rc = -EINVAL;
676 		} else {
677 			/*
678 			 * In all other cases ignore error if fetching
679 			 * of EA $LXDEV failed. It is needed only for
680 			 * WSL CHR and BLK reparse points and wsl_to_fattr()
681 			 * handle the case when EA is missing.
682 			 */
683 			rc = 0;
684 		}
685 	}
686 #endif
687 
688 	return rc;
689 }
690 
691 static int cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
692 			     struct cifs_sb_info *cifs_sb, const char *full_path,
693 			     u64 *uniqueid, struct cifs_open_info_data *unused)
694 {
695 	/*
696 	 * We can not use the IndexNumber field by default from Windows or
697 	 * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
698 	 * CIFS spec claims that this value is unique within the scope of a
699 	 * share, and the windows docs hint that it's actually unique
700 	 * per-machine.
701 	 *
702 	 * There may be higher info levels that work but are there Windows
703 	 * server or network appliances for which IndexNumber field is not
704 	 * guaranteed unique?
705 	 *
706 	 * CIFSGetSrvInodeNumber() uses SMB_QUERY_FILE_INTERNAL_INFO
707 	 * which is SMB PASSTHROUGH level therefore check for capability.
708 	 * Note that this function can be called with tcon == NULL.
709 	 */
710 	if (tcon && !(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU))
711 		return -EOPNOTSUPP;
712 	return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
713 				     cifs_sb->local_nls,
714 				     cifs_remap(cifs_sb));
715 }
716 
717 static int cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
718 				struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
719 {
720 	int rc;
721 	FILE_ALL_INFO fi = {};
722 
723 	/*
724 	 * CIFSSMBQFileInfo() for non-NT servers returns bogus data in
725 	 * Attributes fields. So do not use this command for non-NT servers.
726 	 */
727 	if (!(tcon->ses->capabilities & CAP_NT_SMBS))
728 		return -EOPNOTSUPP;
729 
730 	if (cfile->symlink_target) {
731 		data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
732 		if (!data->symlink_target)
733 			return -ENOMEM;
734 	}
735 
736 	rc = CIFSSMBQFileInfo(xid, tcon, cfile->fid.netfid, &fi);
737 	if (!rc)
738 		move_cifs_info_to_smb2(&data->fi, &fi);
739 	return rc;
740 }
741 
742 static void
743 cifs_clear_stats(struct cifs_tcon *tcon)
744 {
745 	atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
746 	atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
747 	atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
748 	atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
749 	atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
750 	atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
751 	atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
752 	atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
753 	atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
754 	atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
755 	atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
756 	atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
757 	atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
758 	atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
759 	atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
760 	atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
761 	atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
762 	atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
763 	atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
764 	atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
765 	atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
766 }
767 
768 static void
769 cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
770 {
771 	seq_printf(m, " Oplocks breaks: %d",
772 		   atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
773 	seq_printf(m, "\nReads:  %d Bytes: %llu",
774 		   atomic_read(&tcon->stats.cifs_stats.num_reads),
775 		   (long long)(tcon->bytes_read));
776 	seq_printf(m, "\nWrites: %d Bytes: %llu",
777 		   atomic_read(&tcon->stats.cifs_stats.num_writes),
778 		   (long long)(tcon->bytes_written));
779 	seq_printf(m, "\nFlushes: %d",
780 		   atomic_read(&tcon->stats.cifs_stats.num_flushes));
781 	seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
782 		   atomic_read(&tcon->stats.cifs_stats.num_locks),
783 		   atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
784 		   atomic_read(&tcon->stats.cifs_stats.num_symlinks));
785 	seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
786 		   atomic_read(&tcon->stats.cifs_stats.num_opens),
787 		   atomic_read(&tcon->stats.cifs_stats.num_closes),
788 		   atomic_read(&tcon->stats.cifs_stats.num_deletes));
789 	seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
790 		   atomic_read(&tcon->stats.cifs_stats.num_posixopens),
791 		   atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
792 	seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
793 		   atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
794 		   atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
795 	seq_printf(m, "\nRenames: %d T2 Renames %d",
796 		   atomic_read(&tcon->stats.cifs_stats.num_renames),
797 		   atomic_read(&tcon->stats.cifs_stats.num_t2renames));
798 	seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
799 		   atomic_read(&tcon->stats.cifs_stats.num_ffirst),
800 		   atomic_read(&tcon->stats.cifs_stats.num_fnext),
801 		   atomic_read(&tcon->stats.cifs_stats.num_fclose));
802 }
803 
804 static void
805 cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
806 		   struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
807 		   const unsigned int xid)
808 {
809 	FILE_BASIC_INFO info;
810 	struct cifsInodeInfo *cifsInode;
811 	u32 dosattrs;
812 	int rc;
813 
814 	memset(&info, 0, sizeof(info));
815 	cifsInode = CIFS_I(inode);
816 	dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
817 	info.Attributes = cpu_to_le32(dosattrs);
818 	rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
819 				cifs_sb);
820 	if (rc == 0)
821 		cifsInode->cifsAttrs = dosattrs;
822 }
823 
824 static int cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock,
825 			  void *buf)
826 {
827 	struct cifs_open_info_data *data = buf;
828 	FILE_ALL_INFO fi = {};
829 	int rc;
830 
831 	if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
832 		rc = SMBLegacyOpen(xid, oparms->tcon, oparms->path,
833 				   oparms->disposition,
834 				   oparms->desired_access,
835 				   oparms->create_options,
836 				   &oparms->fid->netfid, oplock, &fi,
837 				   oparms->cifs_sb->local_nls,
838 				   cifs_remap(oparms->cifs_sb));
839 	else
840 		rc = CIFS_open(xid, oparms, oplock, &fi);
841 
842 	if (!rc && data)
843 		move_cifs_info_to_smb2(&data->fi, &fi);
844 
845 	return rc;
846 }
847 
848 static void
849 cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
850 {
851 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
852 	cfile->fid.netfid = fid->netfid;
853 	cifs_set_oplock_level(cinode, oplock);
854 	cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
855 }
856 
857 static int
858 cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
859 		struct cifs_fid *fid)
860 {
861 	return CIFSSMBClose(xid, tcon, fid->netfid);
862 }
863 
864 static int
865 cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
866 		struct cifs_fid *fid)
867 {
868 	return CIFSSMBFlush(xid, tcon, fid->netfid);
869 }
870 
871 static int
872 cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
873 	       struct cifs_io_parms *parms, unsigned int *bytes_read,
874 	       char **buf, int *buf_type)
875 {
876 	parms->netfid = pfid->netfid;
877 	return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
878 }
879 
880 static int
881 cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
882 		struct cifs_io_parms *parms, unsigned int *written,
883 		struct kvec *iov, unsigned long nr_segs)
884 {
885 
886 	parms->netfid = pfid->netfid;
887 	return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
888 }
889 
890 static int
891 smb_set_file_info(struct inode *inode, const char *full_path,
892 		  FILE_BASIC_INFO *buf, const unsigned int xid)
893 {
894 	int oplock = 0;
895 	int rc;
896 	__u32 netpid;
897 	struct cifs_fid fid;
898 	struct cifs_open_parms oparms;
899 	struct cifsFileInfo *open_file;
900 	FILE_BASIC_INFO new_buf;
901 	struct cifs_open_info_data query_data;
902 	__le64 write_time = buf->LastWriteTime;
903 	struct cifsInodeInfo *cinode = CIFS_I(inode);
904 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
905 	struct tcon_link *tlink = NULL;
906 	struct cifs_tcon *tcon;
907 
908 	/* if the file is already open for write, just use that fileid */
909 	open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
910 
911 	if (open_file) {
912 		fid.netfid = open_file->fid.netfid;
913 		netpid = open_file->pid;
914 		tcon = tlink_tcon(open_file->tlink);
915 	} else {
916 		tlink = cifs_sb_tlink(cifs_sb);
917 		if (IS_ERR(tlink)) {
918 			rc = PTR_ERR(tlink);
919 			tlink = NULL;
920 			goto out;
921 		}
922 		tcon = tlink_tcon(tlink);
923 	}
924 
925 	/*
926 	 * Non-NT servers interprets zero time value in SMB_SET_FILE_BASIC_INFO
927 	 * over TRANS2_SET_FILE_INFORMATION as a valid time value. NT servers
928 	 * interprets zero time value as do not change existing value on server.
929 	 * API of ->set_file_info() callback expects that zero time value has
930 	 * the NT meaning - do not change. Therefore if server is non-NT and
931 	 * some time values in "buf" are zero, then fetch missing time values.
932 	 */
933 	if (!(tcon->ses->capabilities & CAP_NT_SMBS) &&
934 	    (!buf->CreationTime || !buf->LastAccessTime ||
935 	     !buf->LastWriteTime || !buf->ChangeTime)) {
936 		rc = cifs_query_path_info(xid, tcon, cifs_sb, full_path, &query_data);
937 		if (rc) {
938 			if (open_file) {
939 				cifsFileInfo_put(open_file);
940 				open_file = NULL;
941 			}
942 			goto out;
943 		}
944 		/*
945 		 * Original write_time from buf->LastWriteTime is preserved
946 		 * as SMBSetInformation() interprets zero as do not change.
947 		 */
948 		new_buf = *buf;
949 		buf = &new_buf;
950 		if (!buf->CreationTime)
951 			buf->CreationTime = query_data.fi.CreationTime;
952 		if (!buf->LastAccessTime)
953 			buf->LastAccessTime = query_data.fi.LastAccessTime;
954 		if (!buf->LastWriteTime)
955 			buf->LastWriteTime = query_data.fi.LastWriteTime;
956 		if (!buf->ChangeTime)
957 			buf->ChangeTime = query_data.fi.ChangeTime;
958 	}
959 
960 	if (open_file)
961 		goto set_via_filehandle;
962 
963 	rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
964 				cifs_sb);
965 	if (rc == 0) {
966 		cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
967 		goto out;
968 	} else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
969 		goto out;
970 	}
971 
972 	oparms = (struct cifs_open_parms) {
973 		.tcon = tcon,
974 		.cifs_sb = cifs_sb,
975 		.desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
976 		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
977 		.disposition = FILE_OPEN,
978 		.path = full_path,
979 		.fid = &fid,
980 	};
981 
982 	if (S_ISDIR(inode->i_mode) && !(tcon->ses->capabilities & CAP_NT_SMBS)) {
983 		/* Opening directory path is not possible on non-NT servers. */
984 		rc = -EOPNOTSUPP;
985 	} else {
986 		/*
987 		 * Use cifs_open_file() instead of CIFS_open() as the
988 		 * cifs_open_file() selects the correct function which
989 		 * works also on non-NT servers.
990 		 */
991 		rc = cifs_open_file(xid, &oparms, &oplock, NULL);
992 		/*
993 		 * Opening path for writing on non-NT servers is not
994 		 * possible when the read-only attribute is already set.
995 		 * Non-NT server in this case returns -EACCES. For those
996 		 * servers the only possible way how to clear the read-only
997 		 * bit is via SMB_COM_SETATTR command.
998 		 */
999 		if (rc == -EACCES &&
1000 		    (cinode->cifsAttrs & ATTR_READONLY) &&
1001 		     le32_to_cpu(buf->Attributes) != 0 && /* 0 = do not change attrs */
1002 		     !(le32_to_cpu(buf->Attributes) & ATTR_READONLY) &&
1003 		     !(tcon->ses->capabilities & CAP_NT_SMBS))
1004 			rc = -EOPNOTSUPP;
1005 	}
1006 
1007 	/* Fallback to SMB_COM_SETATTR command when absolutelty needed. */
1008 	if (rc == -EOPNOTSUPP) {
1009 		cifs_dbg(FYI, "calling SetInformation since SetPathInfo for attrs/times not supported by this server\n");
1010 		rc = SMBSetInformation(xid, tcon, full_path,
1011 				       buf->Attributes != 0 ? buf->Attributes : cpu_to_le32(cinode->cifsAttrs),
1012 				       write_time,
1013 				       cifs_sb->local_nls, cifs_sb);
1014 		if (rc == 0)
1015 			cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1016 		else
1017 			rc = -EACCES;
1018 		goto out;
1019 	}
1020 
1021 	if (rc != 0) {
1022 		if (rc == -EIO)
1023 			rc = -EINVAL;
1024 		goto out;
1025 	}
1026 
1027 	netpid = current->tgid;
1028 	cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for attrs/times not supported by this server\n");
1029 
1030 set_via_filehandle:
1031 	rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
1032 	if (!rc)
1033 		cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1034 
1035 	if (open_file == NULL)
1036 		CIFSSMBClose(xid, tcon, fid.netfid);
1037 	else
1038 		cifsFileInfo_put(open_file);
1039 
1040 	/*
1041 	 * Setting the read-only bit is not honered on non-NT servers when done
1042 	 * via open-semantics. So for setting it, use SMB_COM_SETATTR command.
1043 	 * This command works only after the file is closed, so use it only when
1044 	 * operation was called without the filehandle.
1045 	 */
1046 	if (open_file == NULL &&
1047 	    !(tcon->ses->capabilities & CAP_NT_SMBS) &&
1048 	    le32_to_cpu(buf->Attributes) & ATTR_READONLY) {
1049 		SMBSetInformation(xid, tcon, full_path,
1050 				  buf->Attributes,
1051 				  0 /* do not change write time */,
1052 				  cifs_sb->local_nls, cifs_sb);
1053 	}
1054 out:
1055 	if (tlink != NULL)
1056 		cifs_put_tlink(tlink);
1057 	return rc;
1058 }
1059 
1060 static int
1061 cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1062 		   struct cifsFileInfo *cfile)
1063 {
1064 	return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
1065 }
1066 
1067 static int
1068 cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1069 		     const char *path, struct cifs_sb_info *cifs_sb,
1070 		     struct cifs_fid *fid, __u16 search_flags,
1071 		     struct cifs_search_info *srch_inf)
1072 {
1073 	int rc;
1074 
1075 	rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
1076 			   &fid->netfid, search_flags, srch_inf, true);
1077 	if (rc)
1078 		cifs_dbg(FYI, "find first failed=%d\n", rc);
1079 	return rc;
1080 }
1081 
1082 static int
1083 cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1084 		    struct cifs_fid *fid, __u16 search_flags,
1085 		    struct cifs_search_info *srch_inf)
1086 {
1087 	return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
1088 }
1089 
1090 static int
1091 cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1092 	       struct cifs_fid *fid)
1093 {
1094 	return CIFSFindClose(xid, tcon, fid->netfid);
1095 }
1096 
1097 static int
1098 cifs_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
1099 		__u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
1100 {
1101 	return CIFSSMBLock(0, tcon, net_fid, current->tgid, 0, 0, 0, 0,
1102 			   LOCKING_ANDX_OPLOCK_RELEASE, false, CIFS_CACHE_READ(cinode) ? 1 : 0);
1103 }
1104 
1105 static int
1106 cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1107 	     const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
1108 {
1109 	int rc = -EOPNOTSUPP;
1110 
1111 	buf->f_type = CIFS_SUPER_MAGIC;
1112 
1113 	/*
1114 	 * We could add a second check for a QFS Unix capability bit
1115 	 */
1116 	if ((tcon->ses->capabilities & CAP_UNIX) &&
1117 	    (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
1118 		rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
1119 
1120 	/*
1121 	 * Only need to call the old QFSInfo if failed on newer one,
1122 	 * e.g. by OS/2.
1123 	 **/
1124 	if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
1125 		rc = CIFSSMBQFSInfo(xid, tcon, buf);
1126 
1127 	/*
1128 	 * Some old Windows servers also do not support level 103, retry with
1129 	 * older level one if old server failed the previous call or we
1130 	 * bypassed it because we detected that this was an older LANMAN sess
1131 	 */
1132 	if (rc)
1133 		rc = SMBOldQFSInfo(xid, tcon, buf);
1134 	return rc;
1135 }
1136 
1137 static int
1138 cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1139 	       __u64 length, __u32 type, int lock, int unlock, bool wait)
1140 {
1141 	return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
1142 			   current->tgid, length, offset, unlock, lock,
1143 			   (__u8)type, wait, 0);
1144 }
1145 
1146 static int
1147 cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
1148 		       const unsigned char *searchName, char **symlinkinfo,
1149 		       const struct nls_table *nls_codepage)
1150 {
1151 #ifdef CONFIG_CIFS_DFS_UPCALL
1152 	int rc;
1153 	struct dfs_info3_param referral = {0};
1154 
1155 	rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
1156 			  0);
1157 
1158 	if (!rc) {
1159 		*symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL);
1160 		free_dfs_info_param(&referral);
1161 		if (!*symlinkinfo)
1162 			rc = -ENOMEM;
1163 	}
1164 	return rc;
1165 #else /* No DFS support */
1166 	return -EREMOTE;
1167 #endif
1168 }
1169 
1170 static int cifs_query_symlink(const unsigned int xid,
1171 			      struct cifs_tcon *tcon,
1172 			      struct cifs_sb_info *cifs_sb,
1173 			      const char *full_path,
1174 			      char **target_path)
1175 {
1176 	int rc;
1177 
1178 	cifs_tcon_dbg(FYI, "%s: path=%s\n", __func__, full_path);
1179 
1180 	if (!cap_unix(tcon->ses))
1181 		return -EOPNOTSUPP;
1182 
1183 	rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
1184 				     cifs_sb->local_nls, cifs_remap(cifs_sb));
1185 	if (rc == -EREMOTE)
1186 		rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
1187 					    target_path, cifs_sb->local_nls);
1188 	return rc;
1189 }
1190 
1191 static struct reparse_data_buffer *cifs_get_reparse_point_buffer(const struct kvec *rsp_iov,
1192 								 u32 *plen)
1193 {
1194 	TRANSACT_IOCTL_RSP *io = rsp_iov->iov_base;
1195 	*plen = le16_to_cpu(io->ByteCount);
1196 	return (struct reparse_data_buffer *)((__u8 *)&io->hdr.Protocol +
1197 					      le32_to_cpu(io->DataOffset));
1198 }
1199 
1200 static bool
1201 cifs_is_read_op(__u32 oplock)
1202 {
1203 	return oplock == OPLOCK_READ;
1204 }
1205 
1206 static unsigned int
1207 cifs_wp_retry_size(struct inode *inode)
1208 {
1209 	return CIFS_SB(inode->i_sb)->ctx->wsize;
1210 }
1211 
1212 static bool
1213 cifs_dir_needs_close(struct cifsFileInfo *cfile)
1214 {
1215 	return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1216 }
1217 
1218 static bool
1219 cifs_can_echo(struct TCP_Server_Info *server)
1220 {
1221 	if (server->tcpStatus == CifsGood)
1222 		return true;
1223 
1224 	return false;
1225 }
1226 
1227 static int
1228 cifs_make_node(unsigned int xid, struct inode *inode,
1229 	       struct dentry *dentry, struct cifs_tcon *tcon,
1230 	       const char *full_path, umode_t mode, dev_t dev)
1231 {
1232 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1233 	struct inode *newinode = NULL;
1234 	int rc;
1235 
1236 	if (tcon->unix_ext) {
1237 		/*
1238 		 * SMB1 Unix Extensions: requires server support but
1239 		 * works with all special files
1240 		 */
1241 		struct cifs_unix_set_info_args args = {
1242 			.mode	= mode & ~current_umask(),
1243 			.ctime	= NO_CHANGE_64,
1244 			.atime	= NO_CHANGE_64,
1245 			.mtime	= NO_CHANGE_64,
1246 			.device	= dev,
1247 		};
1248 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1249 			args.uid = current_fsuid();
1250 			args.gid = current_fsgid();
1251 		} else {
1252 			args.uid = INVALID_UID; /* no change */
1253 			args.gid = INVALID_GID; /* no change */
1254 		}
1255 		rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1256 					    cifs_sb->local_nls,
1257 					    cifs_remap(cifs_sb));
1258 		if (rc)
1259 			return rc;
1260 
1261 		rc = cifs_get_inode_info_unix(&newinode, full_path,
1262 					      inode->i_sb, xid);
1263 
1264 		if (rc == 0)
1265 			d_instantiate(dentry, newinode);
1266 		return rc;
1267 	} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1268 		/*
1269 		 * Check if mounted with mount parm 'sfu' mount parm.
1270 		 * SFU emulation should work with all servers
1271 		 * and was used by default in earlier versions of Windows.
1272 		 */
1273 		return cifs_sfu_make_node(xid, inode, dentry, tcon,
1274 					  full_path, mode, dev);
1275 	} else if (le32_to_cpu(tcon->fsAttrInfo.Attributes) & FILE_SUPPORTS_REPARSE_POINTS) {
1276 		/*
1277 		 * mknod via reparse points requires server support for
1278 		 * storing reparse points, which is available since
1279 		 * Windows 2000, but was not widely used until release
1280 		 * of Windows Server 2012 by the Windows NFS server.
1281 		 */
1282 		return mknod_reparse(xid, inode, dentry, tcon,
1283 				     full_path, mode, dev);
1284 	} else {
1285 		return -EOPNOTSUPP;
1286 	}
1287 }
1288 
1289 static bool
1290 cifs_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
1291 {
1292 	struct smb_hdr *shdr = (struct smb_hdr *)buf;
1293 	struct TCP_Server_Info *pserver;
1294 	struct cifs_ses *ses;
1295 	struct cifs_tcon *tcon;
1296 
1297 	if (shdr->Flags2 & SMBFLG2_ERR_STATUS) {
1298 		if (shdr->Status.CifsError != cpu_to_le32(NT_STATUS_NETWORK_NAME_DELETED))
1299 			return false;
1300 	} else {
1301 		if (shdr->Status.DosError.ErrorClass != ERRSRV ||
1302 		    shdr->Status.DosError.Error != cpu_to_le16(ERRinvtid))
1303 			return false;
1304 	}
1305 
1306 	/* If server is a channel, select the primary channel */
1307 	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
1308 
1309 	spin_lock(&cifs_tcp_ses_lock);
1310 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
1311 		if (cifs_ses_exiting(ses))
1312 			continue;
1313 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1314 			if (tcon->tid == shdr->Tid) {
1315 				spin_lock(&tcon->tc_lock);
1316 				tcon->need_reconnect = true;
1317 				spin_unlock(&tcon->tc_lock);
1318 				spin_unlock(&cifs_tcp_ses_lock);
1319 				pr_warn_once("Server share %s deleted.\n",
1320 					     tcon->tree_name);
1321 				return true;
1322 			}
1323 		}
1324 	}
1325 	spin_unlock(&cifs_tcp_ses_lock);
1326 
1327 	return false;
1328 }
1329 
1330 struct smb_version_operations smb1_operations = {
1331 	.send_cancel = send_nt_cancel,
1332 	.compare_fids = cifs_compare_fids,
1333 	.setup_request = cifs_setup_request,
1334 	.setup_async_request = cifs_setup_async_request,
1335 	.check_receive = cifs_check_receive,
1336 	.add_credits = cifs_add_credits,
1337 	.set_credits = cifs_set_credits,
1338 	.get_credits_field = cifs_get_credits_field,
1339 	.get_credits = cifs_get_credits,
1340 	.wait_mtu_credits = cifs_wait_mtu_credits,
1341 	.get_next_mid = cifs_get_next_mid,
1342 	.read_data_offset = cifs_read_data_offset,
1343 	.read_data_length = cifs_read_data_length,
1344 	.map_error = map_smb_to_linux_error,
1345 	.find_mid = cifs_find_mid,
1346 	.check_message = checkSMB,
1347 	.dump_detail = cifs_dump_detail,
1348 	.clear_stats = cifs_clear_stats,
1349 	.print_stats = cifs_print_stats,
1350 	.is_oplock_break = is_valid_oplock_break,
1351 	.downgrade_oplock = cifs_downgrade_oplock,
1352 	.check_trans2 = cifs_check_trans2,
1353 	.need_neg = cifs_need_neg,
1354 	.negotiate = cifs_negotiate,
1355 	.negotiate_wsize = smb1_negotiate_wsize,
1356 	.negotiate_rsize = smb1_negotiate_rsize,
1357 	.sess_setup = CIFS_SessSetup,
1358 	.logoff = CIFSSMBLogoff,
1359 	.tree_connect = CIFSTCon,
1360 	.tree_disconnect = CIFSSMBTDis,
1361 	.get_dfs_refer = CIFSGetDFSRefer,
1362 	.qfs_tcon = cifs_qfs_tcon,
1363 	.is_path_accessible = cifs_is_path_accessible,
1364 	.can_echo = cifs_can_echo,
1365 	.query_path_info = cifs_query_path_info,
1366 	.query_reparse_point = cifs_query_reparse_point,
1367 	.query_file_info = cifs_query_file_info,
1368 	.get_srv_inum = cifs_get_srv_inum,
1369 	.set_path_size = CIFSSMBSetEOF,
1370 	.set_file_size = CIFSSMBSetFileSize,
1371 	.set_file_info = smb_set_file_info,
1372 	.set_compression = cifs_set_compression,
1373 	.echo = CIFSSMBEcho,
1374 	.mkdir = CIFSSMBMkDir,
1375 	.mkdir_setinfo = cifs_mkdir_setinfo,
1376 	.rmdir = CIFSSMBRmDir,
1377 	.unlink = CIFSSMBDelFile,
1378 	.rename_pending_delete = cifs_rename_pending_delete,
1379 	.rename = CIFSSMBRename,
1380 	.create_hardlink = CIFSCreateHardLink,
1381 	.query_symlink = cifs_query_symlink,
1382 	.get_reparse_point_buffer = cifs_get_reparse_point_buffer,
1383 	.create_reparse_inode = cifs_create_reparse_inode,
1384 	.open = cifs_open_file,
1385 	.set_fid = cifs_set_fid,
1386 	.close = cifs_close_file,
1387 	.flush = cifs_flush_file,
1388 	.async_readv = cifs_async_readv,
1389 	.async_writev = cifs_async_writev,
1390 	.sync_read = cifs_sync_read,
1391 	.sync_write = cifs_sync_write,
1392 	.query_dir_first = cifs_query_dir_first,
1393 	.query_dir_next = cifs_query_dir_next,
1394 	.close_dir = cifs_close_dir,
1395 	.calc_smb_size = smbCalcSize,
1396 	.oplock_response = cifs_oplock_response,
1397 	.queryfs = cifs_queryfs,
1398 	.mand_lock = cifs_mand_lock,
1399 	.mand_unlock_range = cifs_unlock_range,
1400 	.push_mand_locks = cifs_push_mandatory_locks,
1401 	.query_mf_symlink = cifs_query_mf_symlink,
1402 	.create_mf_symlink = cifs_create_mf_symlink,
1403 	.is_read_op = cifs_is_read_op,
1404 	.wp_retry_size = cifs_wp_retry_size,
1405 	.dir_needs_close = cifs_dir_needs_close,
1406 	.select_sectype = cifs_select_sectype,
1407 #ifdef CONFIG_CIFS_XATTR
1408 	.query_all_EAs = CIFSSMBQAllEAs,
1409 	.set_EA = CIFSSMBSetEA,
1410 #endif /* CIFS_XATTR */
1411 	.get_acl = get_cifs_acl,
1412 	.get_acl_by_fid = get_cifs_acl_by_fid,
1413 	.set_acl = set_cifs_acl,
1414 	.make_node = cifs_make_node,
1415 	.is_network_name_deleted = cifs_is_network_name_deleted,
1416 };
1417 
1418 struct smb_version_values smb1_values = {
1419 	.version_string = SMB1_VERSION_STRING,
1420 	.protocol_id = SMB10_PROT_ID,
1421 	.large_lock_type = LOCKING_ANDX_LARGE_FILES,
1422 	.exclusive_lock_type = 0,
1423 	.shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1424 	.unlock_lock_type = 0,
1425 	.header_preamble_size = 4,
1426 	.header_size = sizeof(struct smb_hdr),
1427 	.max_header_size = MAX_CIFS_HDR_SIZE,
1428 	.read_rsp_size = sizeof(READ_RSP),
1429 	.lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
1430 	.cap_unix = CAP_UNIX,
1431 	.cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1432 	.cap_large_files = CAP_LARGE_FILES,
1433 	.cap_unicode = CAP_UNICODE,
1434 	.signing_enabled = SECMODE_SIGN_ENABLED,
1435 	.signing_required = SECMODE_SIGN_REQUIRED,
1436 };
1437