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