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