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