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