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
reset_cifs_unix_caps(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,struct smb3_fs_context * ctx)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
send_nt_cancel(struct cifs_ses * ses,struct TCP_Server_Info * server,struct smb_rqst * rqst,struct mid_q_entry * mid,unsigned int xid)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
send_lock_cancel(struct cifs_ses * ses,struct TCP_Server_Info * server,struct smb_rqst * rqst,struct mid_q_entry * mid,unsigned int xid)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
cifs_send_cancel(struct cifs_ses * ses,struct TCP_Server_Info * server,struct smb_rqst * rqst,struct mid_q_entry * mid,unsigned int xid)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
cifs_compare_fids(struct cifsFileInfo * ob1,struct cifsFileInfo * ob2)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
cifs_read_data_offset(char * buf)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
cifs_read_data_length(char * buf,bool in_remaining)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 *
cifs_find_mid(struct TCP_Server_Info * server,char * buffer)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
cifs_add_credits(struct TCP_Server_Info * server,struct cifs_credits * credits,const int optype)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
cifs_set_credits(struct TCP_Server_Info * server,const int val)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 *
cifs_get_credits_field(struct TCP_Server_Info * server,const int optype)285 cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
286 {
287 return &server->credits;
288 }
289
290 static unsigned int
cifs_get_credits(struct mid_q_entry * mid)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
cifs_get_next_mid(struct TCP_Server_Info * server)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
cifs_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)394 cifs_downgrade_oplock(struct TCP_Server_Info *server,
395 struct cifsInodeInfo *cinode, __u32 oplock,
396 __u16 epoch, bool *purge_cache)
397 {
398 lockdep_assert_held(&cinode->open_file_lock);
399 cifs_set_oplock_level(cinode, oplock);
400 }
401
402 static bool
cifs_need_neg(struct TCP_Server_Info * server)403 cifs_need_neg(struct TCP_Server_Info *server)
404 {
405 return server->maxBuf == 0;
406 }
407
408 static int
cifs_negotiate(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server)409 cifs_negotiate(const unsigned int xid,
410 struct cifs_ses *ses,
411 struct TCP_Server_Info *server)
412 {
413 int rc;
414 rc = CIFSSMBNegotiate(xid, ses, server);
415 return rc;
416 }
417
418 static unsigned int
smb1_negotiate_wsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)419 smb1_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
420 {
421 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
422 struct TCP_Server_Info *server = tcon->ses->server;
423 unsigned int wsize;
424
425 /* start with specified wsize, or default */
426 if (ctx->got_wsize)
427 wsize = ctx->vol_wsize;
428 else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
429 wsize = CIFS_DEFAULT_IOSIZE;
430 else
431 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
432
433 /* can server support 24-bit write sizes? (via UNIX extensions) */
434 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
435 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
436
437 /*
438 * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
439 * Limit it to max buffer offered by the server, minus the size of the
440 * WRITEX header, not including the 4 byte RFC1001 length.
441 */
442 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
443 (!(server->capabilities & CAP_UNIX) && server->sign))
444 wsize = min_t(unsigned int, wsize,
445 server->maxBuf - sizeof(WRITE_REQ));
446
447 /* hard limit of CIFS_MAX_WSIZE */
448 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
449
450 return wsize;
451 }
452
453 static unsigned int
smb1_negotiate_rsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)454 smb1_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
455 {
456 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
457 struct TCP_Server_Info *server = tcon->ses->server;
458 unsigned int rsize, defsize;
459
460 /*
461 * Set default value...
462 *
463 * HACK alert! Ancient servers have very small buffers. Even though
464 * MS-CIFS indicates that servers are only limited by the client's
465 * bufsize for reads, testing against win98se shows that it throws
466 * INVALID_PARAMETER errors if you try to request too large a read.
467 * OS/2 just sends back short reads.
468 *
469 * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
470 * it can't handle a read request larger than its MaxBufferSize either.
471 */
472 if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
473 defsize = CIFS_DEFAULT_IOSIZE;
474 else if (server->capabilities & CAP_LARGE_READ_X)
475 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
476 else
477 defsize = server->maxBuf - sizeof(READ_RSP);
478
479 rsize = ctx->got_rsize ? ctx->vol_rsize : defsize;
480
481 /*
482 * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
483 * the client's MaxBufferSize.
484 */
485 if (!(server->capabilities & CAP_LARGE_READ_X))
486 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
487
488 /* hard limit of CIFS_MAX_RSIZE */
489 rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
490
491 return rsize;
492 }
493
494 static void
cifs_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb)495 cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
496 struct cifs_sb_info *cifs_sb)
497 {
498 CIFSSMBQFSDeviceInfo(xid, tcon);
499 CIFSSMBQFSAttributeInfo(xid, tcon);
500 }
501
502 static int
cifs_is_path_accessible(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path)503 cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
504 struct cifs_sb_info *cifs_sb, const char *full_path)
505 {
506 int rc;
507 FILE_ALL_INFO *file_info;
508
509 file_info = kmalloc_obj(FILE_ALL_INFO);
510 if (file_info == NULL)
511 return -ENOMEM;
512
513 rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
514 0 /* not legacy */, cifs_sb->local_nls,
515 cifs_remap(cifs_sb));
516
517 if (rc == -EOPNOTSUPP || rc == -EINVAL)
518 rc = SMBQueryInformation(xid, tcon, full_path, file_info,
519 cifs_sb->local_nls, cifs_remap(cifs_sb));
520 kfree(file_info);
521 return rc;
522 }
523
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)524 static int cifs_query_path_info(const unsigned int xid,
525 struct cifs_tcon *tcon,
526 struct cifs_sb_info *cifs_sb,
527 const char *full_path,
528 struct cifs_open_info_data *data)
529 {
530 int rc = -EOPNOTSUPP;
531 FILE_ALL_INFO fi = {};
532 struct cifs_search_info search_info = {};
533 bool non_unicode_wildcard = false;
534
535 data->reparse_point = false;
536 data->adjust_tz = false;
537
538 /*
539 * First try CIFSSMBQPathInfo() function which returns more info
540 * (NumberOfLinks) than CIFSFindFirst() fallback function.
541 * Some servers like Win9x do not support SMB_QUERY_FILE_ALL_INFO over
542 * TRANS2_QUERY_PATH_INFORMATION, but supports it with filehandle over
543 * TRANS2_QUERY_FILE_INFORMATION (function CIFSSMBQFileInfo(). But SMB
544 * Open command on non-NT servers works only for files, does not work
545 * for directories. And moreover Win9x SMB server returns bogus data in
546 * SMB_QUERY_FILE_ALL_INFO Attributes field. So for non-NT servers,
547 * do not even use CIFSSMBQPathInfo() or CIFSSMBQFileInfo() function.
548 */
549 if (tcon->ses->capabilities & CAP_NT_SMBS)
550 rc = CIFSSMBQPathInfo(xid, tcon, full_path, &fi, 0 /* not legacy */,
551 cifs_sb->local_nls, cifs_remap(cifs_sb));
552
553 /*
554 * Non-UNICODE variant of fallback functions below expands wildcards,
555 * so they cannot be used for querying paths with wildcard characters.
556 */
557 if (rc && !(tcon->ses->capabilities & CAP_UNICODE) && strpbrk(full_path, "*?\"><"))
558 non_unicode_wildcard = true;
559
560 /*
561 * Then fallback to CIFSFindFirst() which works also with non-NT servers
562 * but does not does not provide NumberOfLinks.
563 */
564 if ((rc == -EOPNOTSUPP || rc == -EINVAL) &&
565 !non_unicode_wildcard) {
566 if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find))
567 search_info.info_level = SMB_FIND_FILE_INFO_STANDARD;
568 else
569 search_info.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
570 rc = CIFSFindFirst(xid, tcon, full_path, cifs_sb, NULL,
571 CIFS_SEARCH_CLOSE_ALWAYS | CIFS_SEARCH_CLOSE_AT_END,
572 &search_info, false);
573 if (rc == 0) {
574 if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find)) {
575 FIND_FILE_STANDARD_INFO *di;
576 int offset = tcon->ses->server->timeAdj;
577
578 di = (FIND_FILE_STANDARD_INFO *)search_info.srch_entries_start;
579 fi.CreationTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
580 di->CreationDate, di->CreationTime, offset)));
581 fi.LastAccessTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
582 di->LastAccessDate, di->LastAccessTime, offset)));
583 fi.LastWriteTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
584 di->LastWriteDate, di->LastWriteTime, offset)));
585 fi.ChangeTime = fi.LastWriteTime;
586 fi.Attributes = cpu_to_le32(le16_to_cpu(di->Attributes));
587 fi.AllocationSize = cpu_to_le64(le32_to_cpu(di->AllocationSize));
588 fi.EndOfFile = cpu_to_le64(le32_to_cpu(di->DataSize));
589 } else {
590 FILE_FULL_DIRECTORY_INFO *di;
591
592 di = (FILE_FULL_DIRECTORY_INFO *)search_info.srch_entries_start;
593 fi.CreationTime = di->CreationTime;
594 fi.LastAccessTime = di->LastAccessTime;
595 fi.LastWriteTime = di->LastWriteTime;
596 fi.ChangeTime = di->ChangeTime;
597 fi.Attributes = di->ExtFileAttributes;
598 fi.AllocationSize = di->AllocationSize;
599 fi.EndOfFile = di->EndOfFile;
600 fi.EASize = di->EaSize;
601 }
602 fi.NumberOfLinks = cpu_to_le32(1);
603 fi.DeletePending = 0;
604 fi.Directory = !!(le32_to_cpu(fi.Attributes) & ATTR_DIRECTORY);
605 cifs_buf_release(search_info.ntwrk_buf_start);
606 } else if (!full_path[0]) {
607 /*
608 * CIFSFindFirst() does not work on root path if the
609 * root path was exported on the server from the top
610 * level path (drive letter).
611 */
612 rc = -EOPNOTSUPP;
613 }
614 }
615
616 /*
617 * If everything failed then fallback to the legacy SMB command
618 * SMB_COM_QUERY_INFORMATION which works with all servers, but
619 * provide just few information.
620 */
621 if ((rc == -EOPNOTSUPP || rc == -EINVAL) && !non_unicode_wildcard) {
622 rc = SMBQueryInformation(xid, tcon, full_path, &fi, cifs_sb->local_nls,
623 cifs_remap(cifs_sb));
624 data->adjust_tz = true;
625 } else if ((rc == -EOPNOTSUPP || rc == -EINVAL) && non_unicode_wildcard) {
626 /* Path with non-UNICODE wildcard character cannot exist. */
627 rc = -ENOENT;
628 }
629
630 if (!rc) {
631 move_cifs_info_to_smb2(&data->fi, &fi);
632 data->reparse_point = le32_to_cpu(fi.Attributes) & ATTR_REPARSE_POINT;
633 }
634
635 #ifdef CONFIG_CIFS_XATTR
636 /*
637 * For non-symlink WSL reparse points it is required to fetch
638 * EA $LXMOD which contains in its S_DT part the mandatory file type.
639 */
640 if (!rc && data->reparse_point) {
641 struct smb2_file_full_ea_info *ea;
642 u32 next = 0;
643
644 ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
645 do {
646 ea = (void *)((u8 *)ea + next);
647 next = le32_to_cpu(ea->next_entry_offset);
648 } while (next);
649 if (le16_to_cpu(ea->ea_value_length)) {
650 ea->next_entry_offset = cpu_to_le32(ALIGN(sizeof(*ea) +
651 ea->ea_name_length + 1 +
652 le16_to_cpu(ea->ea_value_length), 4));
653 ea = (void *)((u8 *)ea + le32_to_cpu(ea->next_entry_offset));
654 }
655
656 rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_MODE,
657 &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
658 SMB2_WSL_XATTR_MODE_SIZE, cifs_sb);
659 if (rc == SMB2_WSL_XATTR_MODE_SIZE) {
660 ea->next_entry_offset = cpu_to_le32(0);
661 ea->flags = 0;
662 ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
663 ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_MODE_SIZE);
664 memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_MODE, SMB2_WSL_XATTR_NAME_LEN + 1);
665 data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
666 SMB2_WSL_XATTR_MODE_SIZE, 4);
667 rc = 0;
668 } else if (rc >= 0) {
669 /* It is an error if EA $LXMOD has wrong size. */
670 rc = -EINVAL;
671 } else {
672 /*
673 * In all other cases ignore error if fetching
674 * of EA $LXMOD failed. It is needed only for
675 * non-symlink WSL reparse points and wsl_to_fattr()
676 * handle the case when EA is missing.
677 */
678 rc = 0;
679 }
680 }
681
682 /*
683 * For WSL CHR and BLK reparse points it is required to fetch
684 * EA $LXDEV which contains major and minor device numbers.
685 */
686 if (!rc && data->reparse_point) {
687 struct smb2_file_full_ea_info *ea;
688 u32 next = 0;
689
690 ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
691 do {
692 ea = (void *)((u8 *)ea + next);
693 next = le32_to_cpu(ea->next_entry_offset);
694 } while (next);
695 if (le16_to_cpu(ea->ea_value_length)) {
696 ea->next_entry_offset = cpu_to_le32(ALIGN(sizeof(*ea) +
697 ea->ea_name_length + 1 +
698 le16_to_cpu(ea->ea_value_length), 4));
699 ea = (void *)((u8 *)ea + le32_to_cpu(ea->next_entry_offset));
700 }
701
702 rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_DEV,
703 &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
704 SMB2_WSL_XATTR_DEV_SIZE, cifs_sb);
705 if (rc == SMB2_WSL_XATTR_DEV_SIZE) {
706 ea->next_entry_offset = cpu_to_le32(0);
707 ea->flags = 0;
708 ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
709 ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_DEV_SIZE);
710 memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_DEV, SMB2_WSL_XATTR_NAME_LEN + 1);
711 data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
712 SMB2_WSL_XATTR_MODE_SIZE, 4);
713 rc = 0;
714 } else if (rc >= 0) {
715 /* It is an error if EA $LXDEV has wrong size. */
716 rc = -EINVAL;
717 } else {
718 /*
719 * In all other cases ignore error if fetching
720 * of EA $LXDEV failed. It is needed only for
721 * WSL CHR and BLK reparse points and wsl_to_fattr()
722 * handle the case when EA is missing.
723 */
724 rc = 0;
725 }
726 }
727 #endif
728
729 return rc;
730 }
731
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)732 static int cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
733 struct cifs_sb_info *cifs_sb, const char *full_path,
734 u64 *uniqueid, struct cifs_open_info_data *unused)
735 {
736 /*
737 * We can not use the IndexNumber field by default from Windows or
738 * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
739 * CIFS spec claims that this value is unique within the scope of a
740 * share, and the windows docs hint that it's actually unique
741 * per-machine.
742 *
743 * There may be higher info levels that work but are there Windows
744 * server or network appliances for which IndexNumber field is not
745 * guaranteed unique?
746 *
747 * CIFSGetSrvInodeNumber() uses SMB_QUERY_FILE_INTERNAL_INFO
748 * which is SMB PASSTHROUGH level therefore check for capability.
749 * Note that this function can be called with tcon == NULL.
750 */
751 if (tcon && !(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU))
752 return -EOPNOTSUPP;
753 return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
754 cifs_sb->local_nls,
755 cifs_remap(cifs_sb));
756 }
757
cifs_query_file_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct cifs_open_info_data * data)758 static int cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
759 struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
760 {
761 int rc;
762 FILE_ALL_INFO fi = {};
763
764 /*
765 * CIFSSMBQFileInfo() for non-NT servers returns bogus data in
766 * Attributes fields. So do not use this command for non-NT servers.
767 */
768 if (!(tcon->ses->capabilities & CAP_NT_SMBS))
769 return -EOPNOTSUPP;
770
771 if (cfile->symlink_target) {
772 data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
773 if (!data->symlink_target)
774 return -ENOMEM;
775 }
776
777 rc = CIFSSMBQFileInfo(xid, tcon, cfile->fid.netfid, &fi);
778 if (!rc)
779 move_cifs_info_to_smb2(&data->fi, &fi);
780 return rc;
781 }
782
783 static void
cifs_clear_stats(struct cifs_tcon * tcon)784 cifs_clear_stats(struct cifs_tcon *tcon)
785 {
786 atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
787 atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
788 atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
789 atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
790 atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
791 atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
792 atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
793 atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
794 atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
795 atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
796 atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
797 atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
798 atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
799 atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
800 atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
801 atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
802 atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
803 atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
804 atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
805 atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
806 atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
807 }
808
809 static void
cifs_print_stats(struct seq_file * m,struct cifs_tcon * tcon)810 cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
811 {
812 seq_printf(m, " Oplocks breaks: %d",
813 atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
814 seq_printf(m, "\nReads: %d Bytes: %llu",
815 atomic_read(&tcon->stats.cifs_stats.num_reads),
816 (long long)(tcon->bytes_read));
817 seq_printf(m, "\nWrites: %d Bytes: %llu",
818 atomic_read(&tcon->stats.cifs_stats.num_writes),
819 (long long)(tcon->bytes_written));
820 seq_printf(m, "\nFlushes: %d",
821 atomic_read(&tcon->stats.cifs_stats.num_flushes));
822 seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
823 atomic_read(&tcon->stats.cifs_stats.num_locks),
824 atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
825 atomic_read(&tcon->stats.cifs_stats.num_symlinks));
826 seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
827 atomic_read(&tcon->stats.cifs_stats.num_opens),
828 atomic_read(&tcon->stats.cifs_stats.num_closes),
829 atomic_read(&tcon->stats.cifs_stats.num_deletes));
830 seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
831 atomic_read(&tcon->stats.cifs_stats.num_posixopens),
832 atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
833 seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
834 atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
835 atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
836 seq_printf(m, "\nRenames: %d T2 Renames %d",
837 atomic_read(&tcon->stats.cifs_stats.num_renames),
838 atomic_read(&tcon->stats.cifs_stats.num_t2renames));
839 seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
840 atomic_read(&tcon->stats.cifs_stats.num_ffirst),
841 atomic_read(&tcon->stats.cifs_stats.num_fnext),
842 atomic_read(&tcon->stats.cifs_stats.num_fclose));
843 }
844
845 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)846 cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
847 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
848 const unsigned int xid)
849 {
850 FILE_BASIC_INFO info;
851 struct cifsInodeInfo *cifsInode;
852 u32 dosattrs;
853 int rc;
854
855 memset(&info, 0, sizeof(info));
856 cifsInode = CIFS_I(inode);
857 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
858 info.Attributes = cpu_to_le32(dosattrs);
859 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
860 cifs_sb);
861 if (rc == -EOPNOTSUPP || rc == -EINVAL)
862 rc = SMBSetInformation(xid, tcon, full_path,
863 info.Attributes,
864 0 /* do not change write time */,
865 cifs_sb->local_nls, cifs_sb);
866 if (rc == 0)
867 cifsInode->cifsAttrs = dosattrs;
868 }
869
cifs_open_file(const unsigned int xid,struct cifs_open_parms * oparms,__u32 * oplock,void * buf)870 static int cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock,
871 void *buf)
872 {
873 struct cifs_open_info_data *data = buf;
874 FILE_ALL_INFO fi = {};
875 int rc;
876
877 if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
878 rc = SMBLegacyOpen(xid, oparms->tcon, oparms->path,
879 oparms->disposition,
880 oparms->desired_access,
881 oparms->create_options,
882 &oparms->fid->netfid, oplock, &fi,
883 oparms->cifs_sb->local_nls,
884 cifs_remap(oparms->cifs_sb));
885 else
886 rc = CIFS_open(xid, oparms, oplock, &fi);
887
888 if (!rc && data)
889 move_cifs_info_to_smb2(&data->fi, &fi);
890
891 return rc;
892 }
893
894 static void
cifs_set_fid(struct cifsFileInfo * cfile,struct cifs_fid * fid,__u32 oplock)895 cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
896 {
897 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
898
899 lockdep_assert_held(&cinode->open_file_lock);
900
901 cfile->fid.netfid = fid->netfid;
902 cifs_set_oplock_level(cinode, oplock);
903 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
904 }
905
906 static int
cifs_close_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)907 cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
908 struct cifs_fid *fid)
909 {
910 return CIFSSMBClose(xid, tcon, fid->netfid);
911 }
912
913 static int
cifs_flush_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)914 cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
915 struct cifs_fid *fid)
916 {
917 return CIFSSMBFlush(xid, tcon, fid->netfid);
918 }
919
920 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)921 cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
922 struct cifs_io_parms *parms, unsigned int *bytes_read,
923 char **buf, int *buf_type)
924 {
925 parms->netfid = pfid->netfid;
926 return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
927 }
928
929 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)930 cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
931 struct cifs_io_parms *parms, unsigned int *written,
932 struct kvec *iov, unsigned long nr_segs)
933 {
934
935 parms->netfid = pfid->netfid;
936 return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
937 }
938
939 static int
smb_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)940 smb_set_file_info(struct inode *inode, const char *full_path,
941 FILE_BASIC_INFO *buf, const unsigned int xid)
942 {
943 int oplock = 0;
944 int rc;
945 __u32 netpid;
946 struct cifs_fid fid;
947 struct cifs_open_parms oparms;
948 struct cifsFileInfo *open_file;
949 FILE_BASIC_INFO new_buf;
950 struct cifs_open_info_data query_data;
951 __le64 write_time = buf->LastWriteTime;
952 struct cifsInodeInfo *cinode = CIFS_I(inode);
953 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
954 struct tcon_link *tlink = NULL;
955 struct cifs_tcon *tcon;
956
957 /* if the file is already open for write, just use that fileid */
958 open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
959
960 if (open_file) {
961 fid.netfid = open_file->fid.netfid;
962 netpid = open_file->pid;
963 tcon = tlink_tcon(open_file->tlink);
964 } else {
965 tlink = cifs_sb_tlink(cifs_sb);
966 if (IS_ERR(tlink)) {
967 rc = PTR_ERR(tlink);
968 tlink = NULL;
969 goto out;
970 }
971 tcon = tlink_tcon(tlink);
972 }
973
974 /*
975 * Non-NT servers interprets zero time value in SMB_SET_FILE_BASIC_INFO
976 * over TRANS2_SET_FILE_INFORMATION as a valid time value. NT servers
977 * interprets zero time value as do not change existing value on server.
978 * API of ->set_file_info() callback expects that zero time value has
979 * the NT meaning - do not change. Therefore if server is non-NT and
980 * some time values in "buf" are zero, then fetch missing time values.
981 */
982 if (!(tcon->ses->capabilities & CAP_NT_SMBS) &&
983 (!buf->CreationTime || !buf->LastAccessTime ||
984 !buf->LastWriteTime || !buf->ChangeTime)) {
985 rc = cifs_query_path_info(xid, tcon, cifs_sb, full_path, &query_data);
986 if (rc) {
987 if (open_file) {
988 cifsFileInfo_put(open_file);
989 open_file = NULL;
990 }
991 goto out;
992 }
993 /*
994 * Original write_time from buf->LastWriteTime is preserved
995 * as SMBSetInformation() interprets zero as do not change.
996 */
997 new_buf = *buf;
998 buf = &new_buf;
999 if (!buf->CreationTime)
1000 buf->CreationTime = query_data.fi.CreationTime;
1001 if (!buf->LastAccessTime)
1002 buf->LastAccessTime = query_data.fi.LastAccessTime;
1003 if (!buf->LastWriteTime)
1004 buf->LastWriteTime = query_data.fi.LastWriteTime;
1005 if (!buf->ChangeTime)
1006 buf->ChangeTime = query_data.fi.ChangeTime;
1007 }
1008
1009 if (open_file)
1010 goto set_via_filehandle;
1011
1012 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
1013 cifs_sb);
1014 if (rc == 0) {
1015 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1016 goto out;
1017 } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
1018 goto out;
1019 }
1020
1021 oparms = (struct cifs_open_parms) {
1022 .tcon = tcon,
1023 .cifs_sb = cifs_sb,
1024 .desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1025 .create_options = cifs_create_options(cifs_sb, 0),
1026 .disposition = FILE_OPEN,
1027 .path = full_path,
1028 .fid = &fid,
1029 };
1030
1031 if (S_ISDIR(inode->i_mode) && !(tcon->ses->capabilities & CAP_NT_SMBS)) {
1032 /* Opening directory path is not possible on non-NT servers. */
1033 rc = -EOPNOTSUPP;
1034 } else {
1035 /*
1036 * Use cifs_open_file() instead of CIFS_open() as the
1037 * cifs_open_file() selects the correct function which
1038 * works also on non-NT servers.
1039 */
1040 rc = cifs_open_file(xid, &oparms, &oplock, NULL);
1041 /*
1042 * Opening path for writing on non-NT servers is not
1043 * possible when the read-only attribute is already set.
1044 * Non-NT server in this case returns -EACCES. For those
1045 * servers the only possible way how to clear the read-only
1046 * bit is via SMB_COM_SETATTR command.
1047 */
1048 if (rc == -EACCES &&
1049 (cinode->cifsAttrs & ATTR_READONLY) &&
1050 le32_to_cpu(buf->Attributes) != 0 && /* 0 = do not change attrs */
1051 !(le32_to_cpu(buf->Attributes) & ATTR_READONLY) &&
1052 !(tcon->ses->capabilities & CAP_NT_SMBS))
1053 rc = -EOPNOTSUPP;
1054 }
1055
1056 /* Fallback to SMB_COM_SETATTR command when absolutely needed. */
1057 if (rc == -EOPNOTSUPP) {
1058 cifs_dbg(FYI, "calling SetInformation since SetPathInfo for attrs/times not supported by this server\n");
1059 rc = SMBSetInformation(xid, tcon, full_path,
1060 buf->Attributes != 0 ? buf->Attributes : cpu_to_le32(cinode->cifsAttrs),
1061 write_time,
1062 cifs_sb->local_nls, cifs_sb);
1063 if (rc == 0)
1064 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1065 else
1066 rc = -EACCES;
1067 goto out;
1068 }
1069
1070 if (rc != 0) {
1071 if (rc == -EIO)
1072 rc = -EINVAL;
1073 goto out;
1074 }
1075
1076 netpid = current->tgid;
1077 cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for attrs/times not supported by this server\n");
1078
1079 set_via_filehandle:
1080 rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
1081 if (!rc)
1082 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1083
1084 if (open_file == NULL)
1085 CIFSSMBClose(xid, tcon, fid.netfid);
1086 else
1087 cifsFileInfo_put(open_file);
1088
1089 /*
1090 * Setting the read-only bit is not honored on non-NT servers when done
1091 * via open-semantics. So for setting it, use SMB_COM_SETATTR command.
1092 * This command works only after the file is closed, so use it only when
1093 * operation was called without the filehandle.
1094 */
1095 if (open_file == NULL &&
1096 !(tcon->ses->capabilities & CAP_NT_SMBS) &&
1097 le32_to_cpu(buf->Attributes) & ATTR_READONLY) {
1098 SMBSetInformation(xid, tcon, full_path,
1099 buf->Attributes,
1100 0 /* do not change write time */,
1101 cifs_sb->local_nls, cifs_sb);
1102 }
1103 out:
1104 if (tlink != NULL)
1105 cifs_put_tlink(tlink);
1106 return rc;
1107 }
1108
1109 static int
cifs_set_compression(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)1110 cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1111 struct cifsFileInfo *cfile)
1112 {
1113 return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
1114 }
1115
1116 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)1117 cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1118 const char *path, struct cifs_sb_info *cifs_sb,
1119 struct cifs_fid *fid, __u16 search_flags,
1120 struct cifs_search_info *srch_inf)
1121 {
1122 int rc;
1123
1124 rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
1125 &fid->netfid, search_flags, srch_inf, true);
1126 if (rc)
1127 cifs_dbg(FYI, "find first failed=%d\n", rc);
1128 return rc;
1129 }
1130
1131 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)1132 cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1133 struct cifs_fid *fid, __u16 search_flags,
1134 struct cifs_search_info *srch_inf)
1135 {
1136 return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
1137 }
1138
1139 static int
cifs_close_dir(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1140 cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1141 struct cifs_fid *fid)
1142 {
1143 return CIFSFindClose(xid, tcon, fid->netfid);
1144 }
1145
cifs_oplock_response(struct cifs_tcon * tcon,__u64 persistent_fid,__u64 volatile_fid,__u16 net_fid,struct cifsInodeInfo * cinode,unsigned int oplock)1146 static int cifs_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
1147 __u64 volatile_fid, __u16 net_fid,
1148 struct cifsInodeInfo *cinode, unsigned int oplock)
1149 {
1150 unsigned int sbflags = CIFS_SB(cinode->netfs.inode.i_sb)->mnt_cifs_flags;
1151 __u8 op;
1152
1153 op = !!((oplock & CIFS_CACHE_READ_FLG) || (sbflags & CIFS_MOUNT_RO_CACHE));
1154 return CIFSSMBLock(0, tcon, net_fid, current->tgid, 0, 0, 0, 0,
1155 LOCKING_ANDX_OPLOCK_RELEASE, false, op);
1156 }
1157
1158 static int
cifs_queryfs(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct kstatfs * buf)1159 cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1160 const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
1161 {
1162 int rc = -EOPNOTSUPP;
1163
1164 buf->f_type = CIFS_SUPER_MAGIC;
1165
1166 /*
1167 * We could add a second check for a QFS Unix capability bit
1168 */
1169 if ((tcon->ses->capabilities & CAP_UNIX) &&
1170 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
1171 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
1172
1173 /*
1174 * Only need to call the old QFSInfo if failed on newer one,
1175 * e.g. by OS/2.
1176 **/
1177 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
1178 rc = CIFSSMBQFSInfo(xid, tcon, buf);
1179
1180 /*
1181 * Some old Windows servers also do not support level 103, retry with
1182 * older level one if old server failed the previous call or we
1183 * bypassed it because we detected that this was an older LANMAN sess
1184 */
1185 if (rc)
1186 rc = SMBOldQFSInfo(xid, tcon, buf);
1187 return rc;
1188 }
1189
1190 static int
cifs_mand_lock(const unsigned int xid,struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u32 type,int lock,int unlock,bool wait)1191 cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1192 __u64 length, __u32 type, int lock, int unlock, bool wait)
1193 {
1194 return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
1195 current->tgid, length, offset, unlock, lock,
1196 (__u8)type, wait, 0);
1197 }
1198
1199 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)1200 cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
1201 const unsigned char *searchName, char **symlinkinfo,
1202 const struct nls_table *nls_codepage)
1203 {
1204 #ifdef CONFIG_CIFS_DFS_UPCALL
1205 int rc;
1206 struct dfs_info3_param referral = {0};
1207
1208 rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
1209 0);
1210
1211 if (!rc) {
1212 *symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL);
1213 free_dfs_info_param(&referral);
1214 if (!*symlinkinfo)
1215 rc = -ENOMEM;
1216 }
1217 return rc;
1218 #else /* No DFS support */
1219 return -EREMOTE;
1220 #endif
1221 }
1222
cifs_query_symlink(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,char ** target_path)1223 static int cifs_query_symlink(const unsigned int xid,
1224 struct cifs_tcon *tcon,
1225 struct cifs_sb_info *cifs_sb,
1226 const char *full_path,
1227 char **target_path)
1228 {
1229 int rc;
1230
1231 cifs_tcon_dbg(FYI, "%s: path=%s\n", __func__, full_path);
1232
1233 if (!cap_unix(tcon->ses))
1234 return -EOPNOTSUPP;
1235
1236 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
1237 cifs_sb->local_nls, cifs_remap(cifs_sb));
1238 if (rc == -EREMOTE)
1239 rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
1240 target_path, cifs_sb->local_nls);
1241 return rc;
1242 }
1243
cifs_get_reparse_point_buffer(const struct kvec * rsp_iov,u32 * plen)1244 static struct reparse_data_buffer *cifs_get_reparse_point_buffer(const struct kvec *rsp_iov,
1245 u32 *plen)
1246 {
1247 TRANSACT_IOCTL_RSP *io = rsp_iov->iov_base;
1248 *plen = le16_to_cpu(io->ByteCount);
1249 return (struct reparse_data_buffer *)((__u8 *)&io->hdr.Protocol +
1250 le32_to_cpu(io->DataOffset));
1251 }
1252
1253 static bool
cifs_is_read_op(__u32 oplock)1254 cifs_is_read_op(__u32 oplock)
1255 {
1256 return oplock == OPLOCK_READ;
1257 }
1258
1259 static unsigned int
cifs_wp_retry_size(struct inode * inode)1260 cifs_wp_retry_size(struct inode *inode)
1261 {
1262 return CIFS_SB(inode->i_sb)->ctx->wsize;
1263 }
1264
1265 static bool
cifs_dir_needs_close(struct cifsFileInfo * cfile)1266 cifs_dir_needs_close(struct cifsFileInfo *cfile)
1267 {
1268 return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1269 }
1270
1271 static bool
cifs_can_echo(struct TCP_Server_Info * server)1272 cifs_can_echo(struct TCP_Server_Info *server)
1273 {
1274 if (server->tcpStatus == CifsGood)
1275 return true;
1276
1277 return false;
1278 }
1279
1280 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)1281 cifs_make_node(unsigned int xid, struct inode *inode,
1282 struct dentry *dentry, struct cifs_tcon *tcon,
1283 const char *full_path, umode_t mode, dev_t dev)
1284 {
1285 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1286 struct inode *newinode = NULL;
1287 int rc;
1288
1289 if (tcon->unix_ext) {
1290 /*
1291 * SMB1 Unix Extensions: requires server support but
1292 * works with all special files
1293 */
1294 struct cifs_unix_set_info_args args = {
1295 .mode = mode & ~current_umask(),
1296 .ctime = NO_CHANGE_64,
1297 .atime = NO_CHANGE_64,
1298 .mtime = NO_CHANGE_64,
1299 .device = dev,
1300 };
1301 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1302 args.uid = current_fsuid();
1303 args.gid = current_fsgid();
1304 } else {
1305 args.uid = INVALID_UID; /* no change */
1306 args.gid = INVALID_GID; /* no change */
1307 }
1308 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1309 cifs_sb->local_nls,
1310 cifs_remap(cifs_sb));
1311 if (rc)
1312 return rc;
1313
1314 rc = cifs_get_inode_info_unix(&newinode, full_path,
1315 inode->i_sb, xid);
1316
1317 if (rc == 0)
1318 d_instantiate(dentry, newinode);
1319 return rc;
1320 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1321 /*
1322 * Check if mounted with mount parm 'sfu' mount parm.
1323 * SFU emulation should work with all servers
1324 * and was used by default in earlier versions of Windows.
1325 */
1326 return cifs_sfu_make_node(xid, inode, dentry, tcon,
1327 full_path, mode, dev);
1328 } else if (CIFS_REPARSE_SUPPORT(tcon)) {
1329 /*
1330 * mknod via reparse points requires server support for
1331 * storing reparse points, which is available since
1332 * Windows 2000, but was not widely used until release
1333 * of Windows Server 2012 by the Windows NFS server.
1334 */
1335 return mknod_reparse(xid, inode, dentry, tcon,
1336 full_path, mode, dev);
1337 } else {
1338 return -EOPNOTSUPP;
1339 }
1340 }
1341
1342 static bool
cifs_is_network_name_deleted(char * buf,struct TCP_Server_Info * server)1343 cifs_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
1344 {
1345 struct smb_hdr *shdr = (struct smb_hdr *)buf;
1346 struct TCP_Server_Info *pserver;
1347 struct cifs_ses *ses;
1348 struct cifs_tcon *tcon;
1349
1350 if (shdr->Flags2 & SMBFLG2_ERR_STATUS) {
1351 if (shdr->Status.CifsError != cpu_to_le32(NT_STATUS_NETWORK_NAME_DELETED))
1352 return false;
1353 } else {
1354 if (shdr->Status.DosError.ErrorClass != ERRSRV ||
1355 shdr->Status.DosError.Error != cpu_to_le16(ERRinvtid))
1356 return false;
1357 }
1358
1359 /* If server is a channel, select the primary channel */
1360 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
1361
1362 spin_lock(&cifs_tcp_ses_lock);
1363 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
1364 if (cifs_ses_exiting(ses))
1365 continue;
1366 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1367 if (tcon->tid == shdr->Tid) {
1368 spin_lock(&tcon->tc_lock);
1369 tcon->need_reconnect = true;
1370 spin_unlock(&tcon->tc_lock);
1371 spin_unlock(&cifs_tcp_ses_lock);
1372 pr_warn_once("Server share %s deleted.\n",
1373 tcon->tree_name);
1374 return true;
1375 }
1376 }
1377 }
1378 spin_unlock(&cifs_tcp_ses_lock);
1379
1380 return false;
1381 }
1382
1383 struct smb_version_operations smb1_operations = {
1384 .send_cancel = cifs_send_cancel,
1385 .compare_fids = cifs_compare_fids,
1386 .setup_request = cifs_setup_request,
1387 .setup_async_request = cifs_setup_async_request,
1388 .check_receive = cifs_check_receive,
1389 .add_credits = cifs_add_credits,
1390 .set_credits = cifs_set_credits,
1391 .get_credits_field = cifs_get_credits_field,
1392 .get_credits = cifs_get_credits,
1393 .wait_mtu_credits = cifs_wait_mtu_credits,
1394 .get_next_mid = cifs_get_next_mid,
1395 .read_data_offset = cifs_read_data_offset,
1396 .read_data_length = cifs_read_data_length,
1397 .map_error = map_smb_to_linux_error,
1398 .find_mid = cifs_find_mid,
1399 .check_message = checkSMB,
1400 .dump_detail = cifs_dump_detail,
1401 .clear_stats = cifs_clear_stats,
1402 .print_stats = cifs_print_stats,
1403 .is_oplock_break = is_valid_oplock_break,
1404 .downgrade_oplock = cifs_downgrade_oplock,
1405 .check_trans2 = cifs_check_trans2,
1406 .need_neg = cifs_need_neg,
1407 .negotiate = cifs_negotiate,
1408 .negotiate_wsize = smb1_negotiate_wsize,
1409 .negotiate_rsize = smb1_negotiate_rsize,
1410 .sess_setup = CIFS_SessSetup,
1411 .logoff = CIFSSMBLogoff,
1412 .tree_connect = CIFSTCon,
1413 .tree_disconnect = CIFSSMBTDis,
1414 .get_dfs_refer = CIFSGetDFSRefer,
1415 .qfs_tcon = cifs_qfs_tcon,
1416 .is_path_accessible = cifs_is_path_accessible,
1417 .can_echo = cifs_can_echo,
1418 .query_path_info = cifs_query_path_info,
1419 .query_reparse_point = cifs_query_reparse_point,
1420 .query_file_info = cifs_query_file_info,
1421 .get_srv_inum = cifs_get_srv_inum,
1422 .set_path_size = CIFSSMBSetEOF,
1423 .set_file_size = CIFSSMBSetFileSize,
1424 .set_file_info = smb_set_file_info,
1425 .set_compression = cifs_set_compression,
1426 .echo = CIFSSMBEcho,
1427 .mkdir = CIFSSMBMkDir,
1428 .mkdir_setinfo = cifs_mkdir_setinfo,
1429 .rmdir = CIFSSMBRmDir,
1430 .unlink = CIFSSMBDelFile,
1431 .rename_pending_delete = cifs_rename_pending_delete,
1432 .rename = CIFSSMBRename,
1433 .create_hardlink = CIFSCreateHardLink,
1434 .query_symlink = cifs_query_symlink,
1435 .get_reparse_point_buffer = cifs_get_reparse_point_buffer,
1436 .create_reparse_inode = cifs_create_reparse_inode,
1437 .open = cifs_open_file,
1438 .set_fid = cifs_set_fid,
1439 .close = cifs_close_file,
1440 .flush = cifs_flush_file,
1441 .async_readv = cifs_async_readv,
1442 .async_writev = cifs_async_writev,
1443 .sync_read = cifs_sync_read,
1444 .sync_write = cifs_sync_write,
1445 .query_dir_first = cifs_query_dir_first,
1446 .query_dir_next = cifs_query_dir_next,
1447 .close_dir = cifs_close_dir,
1448 .calc_smb_size = smbCalcSize,
1449 .oplock_response = cifs_oplock_response,
1450 .queryfs = cifs_queryfs,
1451 .mand_lock = cifs_mand_lock,
1452 .mand_unlock_range = cifs_unlock_range,
1453 .push_mand_locks = cifs_push_mandatory_locks,
1454 .query_mf_symlink = cifs_query_mf_symlink,
1455 .create_mf_symlink = cifs_create_mf_symlink,
1456 .is_read_op = cifs_is_read_op,
1457 .wp_retry_size = cifs_wp_retry_size,
1458 .dir_needs_close = cifs_dir_needs_close,
1459 .select_sectype = cifs_select_sectype,
1460 #ifdef CONFIG_CIFS_XATTR
1461 .query_all_EAs = CIFSSMBQAllEAs,
1462 .set_EA = CIFSSMBSetEA,
1463 #endif /* CIFS_XATTR */
1464 .get_acl = get_cifs_acl,
1465 .get_acl_by_fid = get_cifs_acl_by_fid,
1466 .set_acl = set_cifs_acl,
1467 .make_node = cifs_make_node,
1468 .is_network_name_deleted = cifs_is_network_name_deleted,
1469 };
1470
1471 struct smb_version_values smb1_values = {
1472 .version_string = SMB1_VERSION_STRING,
1473 .protocol_id = SMB10_PROT_ID,
1474 .large_lock_type = LOCKING_ANDX_LARGE_FILES,
1475 .exclusive_lock_type = 0,
1476 .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1477 .unlock_lock_type = 0,
1478 .header_size = sizeof(struct smb_hdr),
1479 .max_header_size = MAX_CIFS_HDR_SIZE,
1480 .read_rsp_size = sizeof(READ_RSP),
1481 .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
1482 .cap_unix = CAP_UNIX,
1483 .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1484 .cap_large_files = CAP_LARGE_FILES,
1485 .cap_unicode = CAP_UNICODE,
1486 .signing_enabled = SECMODE_SIGN_ENABLED,
1487 .signing_required = SECMODE_SIGN_REQUIRED,
1488 };
1489