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