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