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