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