]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - fs/cifs/smb2ops.c
CIFS: fix deadlock in cached root handling
[thirdparty/kernel/stable.git] / fs / cifs / smb2ops.c
1 /*
2 * SMB2 version specific operations
3 *
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include <linux/falloc.h>
23 #include <linux/scatterlist.h>
24 #include <linux/uuid.h>
25 #include <crypto/aead.h>
26 #include "cifsglob.h"
27 #include "smb2pdu.h"
28 #include "smb2proto.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_unicode.h"
32 #include "smb2status.h"
33 #include "smb2glob.h"
34 #include "cifs_ioctl.h"
35 #include "smbdirect.h"
36
37 /* Change credits for different ops and return the total number of credits */
38 static int
39 change_conf(struct TCP_Server_Info *server)
40 {
41 server->credits += server->echo_credits + server->oplock_credits;
42 server->oplock_credits = server->echo_credits = 0;
43 switch (server->credits) {
44 case 0:
45 return 0;
46 case 1:
47 server->echoes = false;
48 server->oplocks = false;
49 break;
50 case 2:
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
54 break;
55 default:
56 server->echoes = true;
57 if (enable_oplocks) {
58 server->oplocks = true;
59 server->oplock_credits = 1;
60 } else
61 server->oplocks = false;
62
63 server->echo_credits = 1;
64 }
65 server->credits -= server->echo_credits + server->oplock_credits;
66 return server->credits + server->echo_credits + server->oplock_credits;
67 }
68
69 static void
70 smb2_add_credits(struct TCP_Server_Info *server,
71 const struct cifs_credits *credits, const int optype)
72 {
73 int *val, rc = -1;
74 unsigned int add = credits->value;
75 unsigned int instance = credits->instance;
76 bool reconnect_detected = false;
77
78 spin_lock(&server->req_lock);
79 val = server->ops->get_credits_field(server, optype);
80
81 /* eg found case where write overlapping reconnect messed up credits */
82 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
83 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
84 server->hostname, *val);
85 if ((instance == 0) || (instance == server->reconnect_instance))
86 *val += add;
87 else
88 reconnect_detected = true;
89
90 if (*val > 65000) {
91 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
92 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
93 }
94 server->in_flight--;
95 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
96 rc = change_conf(server);
97 /*
98 * Sometimes server returns 0 credits on oplock break ack - we need to
99 * rebalance credits in this case.
100 */
101 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
102 server->oplocks) {
103 if (server->credits > 1) {
104 server->credits--;
105 server->oplock_credits++;
106 }
107 }
108 spin_unlock(&server->req_lock);
109 wake_up(&server->request_q);
110
111 if (reconnect_detected)
112 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
113 add, instance);
114
115 if (server->tcpStatus == CifsNeedReconnect
116 || server->tcpStatus == CifsExiting)
117 return;
118
119 switch (rc) {
120 case -1:
121 /* change_conf hasn't been executed */
122 break;
123 case 0:
124 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
125 break;
126 case 1:
127 cifs_dbg(VFS, "disabling echoes and oplocks\n");
128 break;
129 case 2:
130 cifs_dbg(FYI, "disabling oplocks\n");
131 break;
132 default:
133 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
134 }
135 }
136
137 static void
138 smb2_set_credits(struct TCP_Server_Info *server, const int val)
139 {
140 spin_lock(&server->req_lock);
141 server->credits = val;
142 if (val == 1)
143 server->reconnect_instance++;
144 spin_unlock(&server->req_lock);
145 /* don't log while holding the lock */
146 if (val == 1)
147 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
148 }
149
150 static int *
151 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
152 {
153 switch (optype) {
154 case CIFS_ECHO_OP:
155 return &server->echo_credits;
156 case CIFS_OBREAK_OP:
157 return &server->oplock_credits;
158 default:
159 return &server->credits;
160 }
161 }
162
163 static unsigned int
164 smb2_get_credits(struct mid_q_entry *mid)
165 {
166 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
167
168 if (mid->mid_state == MID_RESPONSE_RECEIVED
169 || mid->mid_state == MID_RESPONSE_MALFORMED)
170 return le16_to_cpu(shdr->CreditRequest);
171
172 return 0;
173 }
174
175 static int
176 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
177 unsigned int *num, struct cifs_credits *credits)
178 {
179 int rc = 0;
180 unsigned int scredits;
181
182 spin_lock(&server->req_lock);
183 while (1) {
184 if (server->credits <= 0) {
185 spin_unlock(&server->req_lock);
186 cifs_num_waiters_inc(server);
187 rc = wait_event_killable(server->request_q,
188 has_credits(server, &server->credits, 1));
189 cifs_num_waiters_dec(server);
190 if (rc)
191 return rc;
192 spin_lock(&server->req_lock);
193 } else {
194 if (server->tcpStatus == CifsExiting) {
195 spin_unlock(&server->req_lock);
196 return -ENOENT;
197 }
198
199 scredits = server->credits;
200 /* can deadlock with reopen */
201 if (scredits <= 8) {
202 *num = SMB2_MAX_BUFFER_SIZE;
203 credits->value = 0;
204 credits->instance = 0;
205 break;
206 }
207
208 /* leave some credits for reopen and other ops */
209 scredits -= 8;
210 *num = min_t(unsigned int, size,
211 scredits * SMB2_MAX_BUFFER_SIZE);
212
213 credits->value =
214 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
215 credits->instance = server->reconnect_instance;
216 server->credits -= credits->value;
217 server->in_flight++;
218 break;
219 }
220 }
221 spin_unlock(&server->req_lock);
222 return rc;
223 }
224
225 static int
226 smb2_adjust_credits(struct TCP_Server_Info *server,
227 struct cifs_credits *credits,
228 const unsigned int payload_size)
229 {
230 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
231
232 if (!credits->value || credits->value == new_val)
233 return 0;
234
235 if (credits->value < new_val) {
236 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
237 credits->value, new_val);
238 return -ENOTSUPP;
239 }
240
241 spin_lock(&server->req_lock);
242
243 if (server->reconnect_instance != credits->instance) {
244 spin_unlock(&server->req_lock);
245 cifs_dbg(VFS, "trying to return %d credits to old session\n",
246 credits->value - new_val);
247 return -EAGAIN;
248 }
249
250 server->credits += credits->value - new_val;
251 spin_unlock(&server->req_lock);
252 wake_up(&server->request_q);
253 credits->value = new_val;
254 return 0;
255 }
256
257 static __u64
258 smb2_get_next_mid(struct TCP_Server_Info *server)
259 {
260 __u64 mid;
261 /* for SMB2 we need the current value */
262 spin_lock(&GlobalMid_Lock);
263 mid = server->CurrentMid++;
264 spin_unlock(&GlobalMid_Lock);
265 return mid;
266 }
267
268 static void
269 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
270 {
271 spin_lock(&GlobalMid_Lock);
272 if (server->CurrentMid >= val)
273 server->CurrentMid -= val;
274 spin_unlock(&GlobalMid_Lock);
275 }
276
277 static struct mid_q_entry *
278 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
279 {
280 struct mid_q_entry *mid;
281 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
282 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
283
284 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
285 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
286 return NULL;
287 }
288
289 spin_lock(&GlobalMid_Lock);
290 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
291 if ((mid->mid == wire_mid) &&
292 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
293 (mid->command == shdr->Command)) {
294 kref_get(&mid->refcount);
295 spin_unlock(&GlobalMid_Lock);
296 return mid;
297 }
298 }
299 spin_unlock(&GlobalMid_Lock);
300 return NULL;
301 }
302
303 static void
304 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
305 {
306 #ifdef CONFIG_CIFS_DEBUG2
307 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
308
309 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
310 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
311 shdr->ProcessId);
312 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
313 server->ops->calc_smb_size(buf, server));
314 #endif
315 }
316
317 static bool
318 smb2_need_neg(struct TCP_Server_Info *server)
319 {
320 return server->max_read == 0;
321 }
322
323 static int
324 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
325 {
326 int rc;
327 ses->server->CurrentMid = 0;
328 rc = SMB2_negotiate(xid, ses);
329 /* BB we probably don't need to retry with modern servers */
330 if (rc == -EAGAIN)
331 rc = -EHOSTDOWN;
332 return rc;
333 }
334
335 static unsigned int
336 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
337 {
338 struct TCP_Server_Info *server = tcon->ses->server;
339 unsigned int wsize;
340
341 /* start with specified wsize, or default */
342 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
343 wsize = min_t(unsigned int, wsize, server->max_write);
344 #ifdef CONFIG_CIFS_SMB_DIRECT
345 if (server->rdma) {
346 if (server->sign)
347 wsize = min_t(unsigned int,
348 wsize, server->smbd_conn->max_fragmented_send_size);
349 else
350 wsize = min_t(unsigned int,
351 wsize, server->smbd_conn->max_readwrite_size);
352 }
353 #endif
354 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
355 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
356
357 return wsize;
358 }
359
360 static unsigned int
361 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
362 {
363 struct TCP_Server_Info *server = tcon->ses->server;
364 unsigned int wsize;
365
366 /* start with specified wsize, or default */
367 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
368 wsize = min_t(unsigned int, wsize, server->max_write);
369 #ifdef CONFIG_CIFS_SMB_DIRECT
370 if (server->rdma) {
371 if (server->sign)
372 wsize = min_t(unsigned int,
373 wsize, server->smbd_conn->max_fragmented_send_size);
374 else
375 wsize = min_t(unsigned int,
376 wsize, server->smbd_conn->max_readwrite_size);
377 }
378 #endif
379 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
380 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
381
382 return wsize;
383 }
384
385 static unsigned int
386 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
387 {
388 struct TCP_Server_Info *server = tcon->ses->server;
389 unsigned int rsize;
390
391 /* start with specified rsize, or default */
392 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
393 rsize = min_t(unsigned int, rsize, server->max_read);
394 #ifdef CONFIG_CIFS_SMB_DIRECT
395 if (server->rdma) {
396 if (server->sign)
397 rsize = min_t(unsigned int,
398 rsize, server->smbd_conn->max_fragmented_recv_size);
399 else
400 rsize = min_t(unsigned int,
401 rsize, server->smbd_conn->max_readwrite_size);
402 }
403 #endif
404
405 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
406 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
407
408 return rsize;
409 }
410
411 static unsigned int
412 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
413 {
414 struct TCP_Server_Info *server = tcon->ses->server;
415 unsigned int rsize;
416
417 /* start with specified rsize, or default */
418 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
419 rsize = min_t(unsigned int, rsize, server->max_read);
420 #ifdef CONFIG_CIFS_SMB_DIRECT
421 if (server->rdma) {
422 if (server->sign)
423 rsize = min_t(unsigned int,
424 rsize, server->smbd_conn->max_fragmented_recv_size);
425 else
426 rsize = min_t(unsigned int,
427 rsize, server->smbd_conn->max_readwrite_size);
428 }
429 #endif
430
431 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
432 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
433
434 return rsize;
435 }
436
437 static int
438 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
439 size_t buf_len,
440 struct cifs_server_iface **iface_list,
441 size_t *iface_count)
442 {
443 struct network_interface_info_ioctl_rsp *p;
444 struct sockaddr_in *addr4;
445 struct sockaddr_in6 *addr6;
446 struct iface_info_ipv4 *p4;
447 struct iface_info_ipv6 *p6;
448 struct cifs_server_iface *info;
449 ssize_t bytes_left;
450 size_t next = 0;
451 int nb_iface = 0;
452 int rc = 0;
453
454 *iface_list = NULL;
455 *iface_count = 0;
456
457 /*
458 * Fist pass: count and sanity check
459 */
460
461 bytes_left = buf_len;
462 p = buf;
463 while (bytes_left >= sizeof(*p)) {
464 nb_iface++;
465 next = le32_to_cpu(p->Next);
466 if (!next) {
467 bytes_left -= sizeof(*p);
468 break;
469 }
470 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
471 bytes_left -= next;
472 }
473
474 if (!nb_iface) {
475 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
476 rc = -EINVAL;
477 goto out;
478 }
479
480 if (bytes_left || p->Next)
481 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
482
483
484 /*
485 * Second pass: extract info to internal structure
486 */
487
488 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
489 if (!*iface_list) {
490 rc = -ENOMEM;
491 goto out;
492 }
493
494 info = *iface_list;
495 bytes_left = buf_len;
496 p = buf;
497 while (bytes_left >= sizeof(*p)) {
498 info->speed = le64_to_cpu(p->LinkSpeed);
499 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
500 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
501
502 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
503 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
504 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
505 le32_to_cpu(p->Capability));
506
507 switch (p->Family) {
508 /*
509 * The kernel and wire socket structures have the same
510 * layout and use network byte order but make the
511 * conversion explicit in case either one changes.
512 */
513 case INTERNETWORK:
514 addr4 = (struct sockaddr_in *)&info->sockaddr;
515 p4 = (struct iface_info_ipv4 *)p->Buffer;
516 addr4->sin_family = AF_INET;
517 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
518
519 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
520 addr4->sin_port = cpu_to_be16(CIFS_PORT);
521
522 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
523 &addr4->sin_addr);
524 break;
525 case INTERNETWORKV6:
526 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
527 p6 = (struct iface_info_ipv6 *)p->Buffer;
528 addr6->sin6_family = AF_INET6;
529 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
530
531 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
532 addr6->sin6_flowinfo = 0;
533 addr6->sin6_scope_id = 0;
534 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
535
536 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
537 &addr6->sin6_addr);
538 break;
539 default:
540 cifs_dbg(VFS,
541 "%s: skipping unsupported socket family\n",
542 __func__);
543 goto next_iface;
544 }
545
546 (*iface_count)++;
547 info++;
548 next_iface:
549 next = le32_to_cpu(p->Next);
550 if (!next)
551 break;
552 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
553 bytes_left -= next;
554 }
555
556 if (!*iface_count) {
557 rc = -EINVAL;
558 goto out;
559 }
560
561 out:
562 if (rc) {
563 kfree(*iface_list);
564 *iface_count = 0;
565 *iface_list = NULL;
566 }
567 return rc;
568 }
569
570
571 static int
572 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
573 {
574 int rc;
575 unsigned int ret_data_len = 0;
576 struct network_interface_info_ioctl_rsp *out_buf = NULL;
577 struct cifs_server_iface *iface_list;
578 size_t iface_count;
579 struct cifs_ses *ses = tcon->ses;
580
581 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
582 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
583 NULL /* no data input */, 0 /* no data input */,
584 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
585 if (rc == -EOPNOTSUPP) {
586 cifs_dbg(FYI,
587 "server does not support query network interfaces\n");
588 goto out;
589 } else if (rc != 0) {
590 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
591 goto out;
592 }
593
594 rc = parse_server_interfaces(out_buf, ret_data_len,
595 &iface_list, &iface_count);
596 if (rc)
597 goto out;
598
599 spin_lock(&ses->iface_lock);
600 kfree(ses->iface_list);
601 ses->iface_list = iface_list;
602 ses->iface_count = iface_count;
603 ses->iface_last_update = jiffies;
604 spin_unlock(&ses->iface_lock);
605
606 out:
607 kfree(out_buf);
608 return rc;
609 }
610
611 static void
612 smb2_close_cached_fid(struct kref *ref)
613 {
614 struct cached_fid *cfid = container_of(ref, struct cached_fid,
615 refcount);
616
617 if (cfid->is_valid) {
618 cifs_dbg(FYI, "clear cached root file handle\n");
619 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
620 cfid->fid->volatile_fid);
621 cfid->is_valid = false;
622 cfid->file_all_info_is_valid = false;
623 }
624 }
625
626 void close_shroot(struct cached_fid *cfid)
627 {
628 mutex_lock(&cfid->fid_mutex);
629 kref_put(&cfid->refcount, smb2_close_cached_fid);
630 mutex_unlock(&cfid->fid_mutex);
631 }
632
633 void
634 smb2_cached_lease_break(struct work_struct *work)
635 {
636 struct cached_fid *cfid = container_of(work,
637 struct cached_fid, lease_break);
638
639 close_shroot(cfid);
640 }
641
642 /*
643 * Open the directory at the root of a share
644 */
645 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
646 {
647 struct cifs_ses *ses = tcon->ses;
648 struct TCP_Server_Info *server = ses->server;
649 struct cifs_open_parms oparms;
650 struct smb2_create_rsp *o_rsp = NULL;
651 struct smb2_query_info_rsp *qi_rsp = NULL;
652 int resp_buftype[2];
653 struct smb_rqst rqst[2];
654 struct kvec rsp_iov[2];
655 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
656 struct kvec qi_iov[1];
657 int rc, flags = 0;
658 __le16 utf16_path = 0; /* Null - since an open of top of share */
659 u8 oplock = SMB2_OPLOCK_LEVEL_II;
660
661 mutex_lock(&tcon->crfid.fid_mutex);
662 if (tcon->crfid.is_valid) {
663 cifs_dbg(FYI, "found a cached root file handle\n");
664 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
665 kref_get(&tcon->crfid.refcount);
666 mutex_unlock(&tcon->crfid.fid_mutex);
667 return 0;
668 }
669
670 if (smb3_encryption_required(tcon))
671 flags |= CIFS_TRANSFORM_REQ;
672
673 memset(rqst, 0, sizeof(rqst));
674 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
675 memset(rsp_iov, 0, sizeof(rsp_iov));
676
677 /* Open */
678 memset(&open_iov, 0, sizeof(open_iov));
679 rqst[0].rq_iov = open_iov;
680 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
681
682 oparms.tcon = tcon;
683 oparms.create_options = 0;
684 oparms.desired_access = FILE_READ_ATTRIBUTES;
685 oparms.disposition = FILE_OPEN;
686 oparms.fid = pfid;
687 oparms.reconnect = false;
688
689 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
690 if (rc)
691 goto oshr_exit;
692 smb2_set_next_command(tcon, &rqst[0]);
693
694 memset(&qi_iov, 0, sizeof(qi_iov));
695 rqst[1].rq_iov = qi_iov;
696 rqst[1].rq_nvec = 1;
697
698 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
699 COMPOUND_FID, FILE_ALL_INFORMATION,
700 SMB2_O_INFO_FILE, 0,
701 sizeof(struct smb2_file_all_info) +
702 PATH_MAX * 2, 0, NULL);
703 if (rc)
704 goto oshr_exit;
705
706 smb2_set_related(&rqst[1]);
707
708 /*
709 * We do not hold the lock for the open because in case
710 * SMB2_open needs to reconnect, it will end up calling
711 * cifs_mark_open_files_invalid() which takes the lock again
712 * thus causing a deadlock
713 */
714
715 mutex_unlock(&tcon->crfid.fid_mutex);
716 rc = compound_send_recv(xid, ses, flags, 2, rqst,
717 resp_buftype, rsp_iov);
718 mutex_lock(&tcon->crfid.fid_mutex);
719
720 /*
721 * Now we need to check again as the cached root might have
722 * been successfully re-opened from a concurrent process
723 */
724
725 if (tcon->crfid.is_valid) {
726 /* work was already done */
727
728 /* stash fids for close() later */
729 struct cifs_fid fid = {
730 .persistent_fid = pfid->persistent_fid,
731 .volatile_fid = pfid->volatile_fid,
732 };
733
734 /*
735 * caller expects this func to set pfid to a valid
736 * cached root, so we copy the existing one and get a
737 * reference.
738 */
739 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
740 kref_get(&tcon->crfid.refcount);
741
742 mutex_unlock(&tcon->crfid.fid_mutex);
743
744 if (rc == 0) {
745 /* close extra handle outside of crit sec */
746 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
747 }
748 goto oshr_free;
749 }
750
751 /* Cached root is still invalid, continue normaly */
752
753 if (rc)
754 goto oshr_exit;
755
756 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
757 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
758 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
759 #ifdef CONFIG_CIFS_DEBUG2
760 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
761 #endif /* CIFS_DEBUG2 */
762
763 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
764 tcon->crfid.tcon = tcon;
765 tcon->crfid.is_valid = true;
766 kref_init(&tcon->crfid.refcount);
767
768 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
769 kref_get(&tcon->crfid.refcount);
770 oplock = smb2_parse_lease_state(server, o_rsp,
771 &oparms.fid->epoch,
772 oparms.fid->lease_key);
773 } else
774 goto oshr_exit;
775
776 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
777 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
778 goto oshr_exit;
779 if (!smb2_validate_and_copy_iov(
780 le16_to_cpu(qi_rsp->OutputBufferOffset),
781 sizeof(struct smb2_file_all_info),
782 &rsp_iov[1], sizeof(struct smb2_file_all_info),
783 (char *)&tcon->crfid.file_all_info))
784 tcon->crfid.file_all_info_is_valid = 1;
785
786 oshr_exit:
787 mutex_unlock(&tcon->crfid.fid_mutex);
788 oshr_free:
789 SMB2_open_free(&rqst[0]);
790 SMB2_query_info_free(&rqst[1]);
791 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
792 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
793 return rc;
794 }
795
796 static void
797 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
798 {
799 int rc;
800 __le16 srch_path = 0; /* Null - open root of share */
801 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
802 struct cifs_open_parms oparms;
803 struct cifs_fid fid;
804 bool no_cached_open = tcon->nohandlecache;
805
806 oparms.tcon = tcon;
807 oparms.desired_access = FILE_READ_ATTRIBUTES;
808 oparms.disposition = FILE_OPEN;
809 oparms.create_options = 0;
810 oparms.fid = &fid;
811 oparms.reconnect = false;
812
813 if (no_cached_open)
814 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
815 NULL);
816 else
817 rc = open_shroot(xid, tcon, &fid);
818
819 if (rc)
820 return;
821
822 SMB3_request_interfaces(xid, tcon);
823
824 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
825 FS_ATTRIBUTE_INFORMATION);
826 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
827 FS_DEVICE_INFORMATION);
828 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
829 FS_VOLUME_INFORMATION);
830 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
831 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
832 if (no_cached_open)
833 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
834 else
835 close_shroot(&tcon->crfid);
836
837 return;
838 }
839
840 static void
841 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
842 {
843 int rc;
844 __le16 srch_path = 0; /* Null - open root of share */
845 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
846 struct cifs_open_parms oparms;
847 struct cifs_fid fid;
848
849 oparms.tcon = tcon;
850 oparms.desired_access = FILE_READ_ATTRIBUTES;
851 oparms.disposition = FILE_OPEN;
852 oparms.create_options = 0;
853 oparms.fid = &fid;
854 oparms.reconnect = false;
855
856 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
857 if (rc)
858 return;
859
860 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
861 FS_ATTRIBUTE_INFORMATION);
862 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
863 FS_DEVICE_INFORMATION);
864 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
865 return;
866 }
867
868 static int
869 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
870 struct cifs_sb_info *cifs_sb, const char *full_path)
871 {
872 int rc;
873 __le16 *utf16_path;
874 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
875 struct cifs_open_parms oparms;
876 struct cifs_fid fid;
877
878 if ((*full_path == 0) && tcon->crfid.is_valid)
879 return 0;
880
881 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
882 if (!utf16_path)
883 return -ENOMEM;
884
885 oparms.tcon = tcon;
886 oparms.desired_access = FILE_READ_ATTRIBUTES;
887 oparms.disposition = FILE_OPEN;
888 if (backup_cred(cifs_sb))
889 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
890 else
891 oparms.create_options = 0;
892 oparms.fid = &fid;
893 oparms.reconnect = false;
894
895 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
896 if (rc) {
897 kfree(utf16_path);
898 return rc;
899 }
900
901 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
902 kfree(utf16_path);
903 return rc;
904 }
905
906 static int
907 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
908 struct cifs_sb_info *cifs_sb, const char *full_path,
909 u64 *uniqueid, FILE_ALL_INFO *data)
910 {
911 *uniqueid = le64_to_cpu(data->IndexNumber);
912 return 0;
913 }
914
915 static int
916 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
917 struct cifs_fid *fid, FILE_ALL_INFO *data)
918 {
919 int rc;
920 struct smb2_file_all_info *smb2_data;
921
922 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
923 GFP_KERNEL);
924 if (smb2_data == NULL)
925 return -ENOMEM;
926
927 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
928 smb2_data);
929 if (!rc)
930 move_smb2_info_to_cifs(data, smb2_data);
931 kfree(smb2_data);
932 return rc;
933 }
934
935 #ifdef CONFIG_CIFS_XATTR
936 static ssize_t
937 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
938 struct smb2_file_full_ea_info *src, size_t src_size,
939 const unsigned char *ea_name)
940 {
941 int rc = 0;
942 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
943 char *name, *value;
944 size_t buf_size = dst_size;
945 size_t name_len, value_len, user_name_len;
946
947 while (src_size > 0) {
948 name = &src->ea_data[0];
949 name_len = (size_t)src->ea_name_length;
950 value = &src->ea_data[src->ea_name_length + 1];
951 value_len = (size_t)le16_to_cpu(src->ea_value_length);
952
953 if (name_len == 0) {
954 break;
955 }
956
957 if (src_size < 8 + name_len + 1 + value_len) {
958 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
959 rc = -EIO;
960 goto out;
961 }
962
963 if (ea_name) {
964 if (ea_name_len == name_len &&
965 memcmp(ea_name, name, name_len) == 0) {
966 rc = value_len;
967 if (dst_size == 0)
968 goto out;
969 if (dst_size < value_len) {
970 rc = -ERANGE;
971 goto out;
972 }
973 memcpy(dst, value, value_len);
974 goto out;
975 }
976 } else {
977 /* 'user.' plus a terminating null */
978 user_name_len = 5 + 1 + name_len;
979
980 if (buf_size == 0) {
981 /* skip copy - calc size only */
982 rc += user_name_len;
983 } else if (dst_size >= user_name_len) {
984 dst_size -= user_name_len;
985 memcpy(dst, "user.", 5);
986 dst += 5;
987 memcpy(dst, src->ea_data, name_len);
988 dst += name_len;
989 *dst = 0;
990 ++dst;
991 rc += user_name_len;
992 } else {
993 /* stop before overrun buffer */
994 rc = -ERANGE;
995 break;
996 }
997 }
998
999 if (!src->next_entry_offset)
1000 break;
1001
1002 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1003 /* stop before overrun buffer */
1004 rc = -ERANGE;
1005 break;
1006 }
1007 src_size -= le32_to_cpu(src->next_entry_offset);
1008 src = (void *)((char *)src +
1009 le32_to_cpu(src->next_entry_offset));
1010 }
1011
1012 /* didn't find the named attribute */
1013 if (ea_name)
1014 rc = -ENODATA;
1015
1016 out:
1017 return (ssize_t)rc;
1018 }
1019
1020 static ssize_t
1021 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1022 const unsigned char *path, const unsigned char *ea_name,
1023 char *ea_data, size_t buf_size,
1024 struct cifs_sb_info *cifs_sb)
1025 {
1026 int rc;
1027 __le16 *utf16_path;
1028 struct kvec rsp_iov = {NULL, 0};
1029 int buftype = CIFS_NO_BUFFER;
1030 struct smb2_query_info_rsp *rsp;
1031 struct smb2_file_full_ea_info *info = NULL;
1032
1033 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1034 if (!utf16_path)
1035 return -ENOMEM;
1036
1037 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1038 FILE_READ_EA,
1039 FILE_FULL_EA_INFORMATION,
1040 SMB2_O_INFO_FILE,
1041 CIFSMaxBufSize -
1042 MAX_SMB2_CREATE_RESPONSE_SIZE -
1043 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1044 &rsp_iov, &buftype, cifs_sb);
1045 if (rc) {
1046 /*
1047 * If ea_name is NULL (listxattr) and there are no EAs,
1048 * return 0 as it's not an error. Otherwise, the specified
1049 * ea_name was not found.
1050 */
1051 if (!ea_name && rc == -ENODATA)
1052 rc = 0;
1053 goto qeas_exit;
1054 }
1055
1056 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1057 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1058 le32_to_cpu(rsp->OutputBufferLength),
1059 &rsp_iov,
1060 sizeof(struct smb2_file_full_ea_info));
1061 if (rc)
1062 goto qeas_exit;
1063
1064 info = (struct smb2_file_full_ea_info *)(
1065 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1066 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1067 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1068
1069 qeas_exit:
1070 kfree(utf16_path);
1071 free_rsp_buf(buftype, rsp_iov.iov_base);
1072 return rc;
1073 }
1074
1075
1076 static int
1077 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1078 const char *path, const char *ea_name, const void *ea_value,
1079 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1080 struct cifs_sb_info *cifs_sb)
1081 {
1082 struct cifs_ses *ses = tcon->ses;
1083 __le16 *utf16_path = NULL;
1084 int ea_name_len = strlen(ea_name);
1085 int flags = 0;
1086 int len;
1087 struct smb_rqst rqst[3];
1088 int resp_buftype[3];
1089 struct kvec rsp_iov[3];
1090 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1091 struct cifs_open_parms oparms;
1092 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1093 struct cifs_fid fid;
1094 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1095 unsigned int size[1];
1096 void *data[1];
1097 struct smb2_file_full_ea_info *ea = NULL;
1098 struct kvec close_iov[1];
1099 int rc;
1100
1101 if (smb3_encryption_required(tcon))
1102 flags |= CIFS_TRANSFORM_REQ;
1103
1104 if (ea_name_len > 255)
1105 return -EINVAL;
1106
1107 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1108 if (!utf16_path)
1109 return -ENOMEM;
1110
1111 memset(rqst, 0, sizeof(rqst));
1112 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1113 memset(rsp_iov, 0, sizeof(rsp_iov));
1114
1115 if (ses->server->ops->query_all_EAs) {
1116 if (!ea_value) {
1117 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1118 ea_name, NULL, 0,
1119 cifs_sb);
1120 if (rc == -ENODATA)
1121 goto sea_exit;
1122 }
1123 }
1124
1125 /* Open */
1126 memset(&open_iov, 0, sizeof(open_iov));
1127 rqst[0].rq_iov = open_iov;
1128 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1129
1130 memset(&oparms, 0, sizeof(oparms));
1131 oparms.tcon = tcon;
1132 oparms.desired_access = FILE_WRITE_EA;
1133 oparms.disposition = FILE_OPEN;
1134 if (backup_cred(cifs_sb))
1135 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1136 else
1137 oparms.create_options = 0;
1138 oparms.fid = &fid;
1139 oparms.reconnect = false;
1140
1141 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1142 if (rc)
1143 goto sea_exit;
1144 smb2_set_next_command(tcon, &rqst[0]);
1145
1146
1147 /* Set Info */
1148 memset(&si_iov, 0, sizeof(si_iov));
1149 rqst[1].rq_iov = si_iov;
1150 rqst[1].rq_nvec = 1;
1151
1152 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1153 ea = kzalloc(len, GFP_KERNEL);
1154 if (ea == NULL) {
1155 rc = -ENOMEM;
1156 goto sea_exit;
1157 }
1158
1159 ea->ea_name_length = ea_name_len;
1160 ea->ea_value_length = cpu_to_le16(ea_value_len);
1161 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1162 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1163
1164 size[0] = len;
1165 data[0] = ea;
1166
1167 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1168 COMPOUND_FID, current->tgid,
1169 FILE_FULL_EA_INFORMATION,
1170 SMB2_O_INFO_FILE, 0, data, size);
1171 smb2_set_next_command(tcon, &rqst[1]);
1172 smb2_set_related(&rqst[1]);
1173
1174
1175 /* Close */
1176 memset(&close_iov, 0, sizeof(close_iov));
1177 rqst[2].rq_iov = close_iov;
1178 rqst[2].rq_nvec = 1;
1179 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1180 smb2_set_related(&rqst[2]);
1181
1182 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1183 resp_buftype, rsp_iov);
1184
1185 sea_exit:
1186 kfree(ea);
1187 kfree(utf16_path);
1188 SMB2_open_free(&rqst[0]);
1189 SMB2_set_info_free(&rqst[1]);
1190 SMB2_close_free(&rqst[2]);
1191 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1192 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1193 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1194 return rc;
1195 }
1196 #endif
1197
1198 static bool
1199 smb2_can_echo(struct TCP_Server_Info *server)
1200 {
1201 return server->echoes;
1202 }
1203
1204 static void
1205 smb2_clear_stats(struct cifs_tcon *tcon)
1206 {
1207 int i;
1208 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1209 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1210 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1211 }
1212 }
1213
1214 static void
1215 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1216 {
1217 seq_puts(m, "\n\tShare Capabilities:");
1218 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1219 seq_puts(m, " DFS,");
1220 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1221 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1222 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1223 seq_puts(m, " SCALEOUT,");
1224 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1225 seq_puts(m, " CLUSTER,");
1226 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1227 seq_puts(m, " ASYMMETRIC,");
1228 if (tcon->capabilities == 0)
1229 seq_puts(m, " None");
1230 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1231 seq_puts(m, " Aligned,");
1232 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1233 seq_puts(m, " Partition Aligned,");
1234 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1235 seq_puts(m, " SSD,");
1236 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1237 seq_puts(m, " TRIM-support,");
1238
1239 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1240 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1241 if (tcon->perf_sector_size)
1242 seq_printf(m, "\tOptimal sector size: 0x%x",
1243 tcon->perf_sector_size);
1244 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1245 }
1246
1247 static void
1248 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1249 {
1250 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1251 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1252
1253 /*
1254 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1255 * totals (requests sent) since those SMBs are per-session not per tcon
1256 */
1257 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1258 (long long)(tcon->bytes_read),
1259 (long long)(tcon->bytes_written));
1260 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1261 atomic_read(&tcon->num_local_opens),
1262 atomic_read(&tcon->num_remote_opens));
1263 seq_printf(m, "\nTreeConnects: %d total %d failed",
1264 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1265 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1266 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1267 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1268 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1269 seq_printf(m, "\nCreates: %d total %d failed",
1270 atomic_read(&sent[SMB2_CREATE_HE]),
1271 atomic_read(&failed[SMB2_CREATE_HE]));
1272 seq_printf(m, "\nCloses: %d total %d failed",
1273 atomic_read(&sent[SMB2_CLOSE_HE]),
1274 atomic_read(&failed[SMB2_CLOSE_HE]));
1275 seq_printf(m, "\nFlushes: %d total %d failed",
1276 atomic_read(&sent[SMB2_FLUSH_HE]),
1277 atomic_read(&failed[SMB2_FLUSH_HE]));
1278 seq_printf(m, "\nReads: %d total %d failed",
1279 atomic_read(&sent[SMB2_READ_HE]),
1280 atomic_read(&failed[SMB2_READ_HE]));
1281 seq_printf(m, "\nWrites: %d total %d failed",
1282 atomic_read(&sent[SMB2_WRITE_HE]),
1283 atomic_read(&failed[SMB2_WRITE_HE]));
1284 seq_printf(m, "\nLocks: %d total %d failed",
1285 atomic_read(&sent[SMB2_LOCK_HE]),
1286 atomic_read(&failed[SMB2_LOCK_HE]));
1287 seq_printf(m, "\nIOCTLs: %d total %d failed",
1288 atomic_read(&sent[SMB2_IOCTL_HE]),
1289 atomic_read(&failed[SMB2_IOCTL_HE]));
1290 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1291 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1292 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1293 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1294 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1295 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1296 seq_printf(m, "\nQueryInfos: %d total %d failed",
1297 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1298 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1299 seq_printf(m, "\nSetInfos: %d total %d failed",
1300 atomic_read(&sent[SMB2_SET_INFO_HE]),
1301 atomic_read(&failed[SMB2_SET_INFO_HE]));
1302 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1303 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1304 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1305 }
1306
1307 static void
1308 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1309 {
1310 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1311 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1312
1313 cfile->fid.persistent_fid = fid->persistent_fid;
1314 cfile->fid.volatile_fid = fid->volatile_fid;
1315 #ifdef CONFIG_CIFS_DEBUG2
1316 cfile->fid.mid = fid->mid;
1317 #endif /* CIFS_DEBUG2 */
1318 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1319 &fid->purge_cache);
1320 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1321 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1322 }
1323
1324 static void
1325 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1326 struct cifs_fid *fid)
1327 {
1328 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1329 }
1330
1331 static int
1332 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1333 u64 persistent_fid, u64 volatile_fid,
1334 struct copychunk_ioctl *pcchunk)
1335 {
1336 int rc;
1337 unsigned int ret_data_len;
1338 struct resume_key_req *res_key;
1339
1340 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1341 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1342 NULL, 0 /* no input */, CIFSMaxBufSize,
1343 (char **)&res_key, &ret_data_len);
1344
1345 if (rc) {
1346 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1347 goto req_res_key_exit;
1348 }
1349 if (ret_data_len < sizeof(struct resume_key_req)) {
1350 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1351 rc = -EINVAL;
1352 goto req_res_key_exit;
1353 }
1354 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1355
1356 req_res_key_exit:
1357 kfree(res_key);
1358 return rc;
1359 }
1360
1361 static int
1362 smb2_ioctl_query_info(const unsigned int xid,
1363 struct cifs_tcon *tcon,
1364 __le16 *path, int is_dir,
1365 unsigned long p)
1366 {
1367 struct cifs_ses *ses = tcon->ses;
1368 char __user *arg = (char __user *)p;
1369 struct smb_query_info qi;
1370 struct smb_query_info __user *pqi;
1371 int rc = 0;
1372 int flags = 0;
1373 struct smb2_query_info_rsp *qi_rsp = NULL;
1374 struct smb2_ioctl_rsp *io_rsp = NULL;
1375 void *buffer = NULL;
1376 struct smb_rqst rqst[3];
1377 int resp_buftype[3];
1378 struct kvec rsp_iov[3];
1379 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1380 struct cifs_open_parms oparms;
1381 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1382 struct cifs_fid fid;
1383 struct kvec qi_iov[1];
1384 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1385 struct kvec close_iov[1];
1386
1387 memset(rqst, 0, sizeof(rqst));
1388 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1389 memset(rsp_iov, 0, sizeof(rsp_iov));
1390
1391 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1392 return -EFAULT;
1393
1394 if (qi.output_buffer_length > 1024)
1395 return -EINVAL;
1396
1397 if (!ses || !(ses->server))
1398 return -EIO;
1399
1400 if (smb3_encryption_required(tcon))
1401 flags |= CIFS_TRANSFORM_REQ;
1402
1403 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1404 if (buffer == NULL)
1405 return -ENOMEM;
1406
1407 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1408 qi.output_buffer_length)) {
1409 rc = -EFAULT;
1410 goto iqinf_exit;
1411 }
1412
1413 /* Open */
1414 memset(&open_iov, 0, sizeof(open_iov));
1415 rqst[0].rq_iov = open_iov;
1416 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1417
1418 memset(&oparms, 0, sizeof(oparms));
1419 oparms.tcon = tcon;
1420 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1421 oparms.disposition = FILE_OPEN;
1422 if (is_dir)
1423 oparms.create_options = CREATE_NOT_FILE;
1424 else
1425 oparms.create_options = CREATE_NOT_DIR;
1426 oparms.fid = &fid;
1427 oparms.reconnect = false;
1428
1429 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1430 if (rc)
1431 goto iqinf_exit;
1432 smb2_set_next_command(tcon, &rqst[0]);
1433
1434 /* Query */
1435 if (qi.flags & PASSTHRU_FSCTL) {
1436 /* Can eventually relax perm check since server enforces too */
1437 if (!capable(CAP_SYS_ADMIN))
1438 rc = -EPERM;
1439 else {
1440 memset(&io_iov, 0, sizeof(io_iov));
1441 rqst[1].rq_iov = io_iov;
1442 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1443
1444 rc = SMB2_ioctl_init(tcon, &rqst[1],
1445 COMPOUND_FID, COMPOUND_FID,
1446 qi.info_type, true, NULL,
1447 0, CIFSMaxBufSize);
1448 }
1449 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1450 memset(&qi_iov, 0, sizeof(qi_iov));
1451 rqst[1].rq_iov = qi_iov;
1452 rqst[1].rq_nvec = 1;
1453
1454 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1455 COMPOUND_FID, qi.file_info_class,
1456 qi.info_type, qi.additional_information,
1457 qi.input_buffer_length,
1458 qi.output_buffer_length, buffer);
1459 } else { /* unknown flags */
1460 cifs_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1461 rc = -EINVAL;
1462 }
1463
1464 if (rc)
1465 goto iqinf_exit;
1466 smb2_set_next_command(tcon, &rqst[1]);
1467 smb2_set_related(&rqst[1]);
1468
1469 /* Close */
1470 memset(&close_iov, 0, sizeof(close_iov));
1471 rqst[2].rq_iov = close_iov;
1472 rqst[2].rq_nvec = 1;
1473
1474 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1475 if (rc)
1476 goto iqinf_exit;
1477 smb2_set_related(&rqst[2]);
1478
1479 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1480 resp_buftype, rsp_iov);
1481 if (rc)
1482 goto iqinf_exit;
1483 if (qi.flags & PASSTHRU_FSCTL) {
1484 pqi = (struct smb_query_info __user *)arg;
1485 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1486 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1487 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1488 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1489 sizeof(qi.input_buffer_length))) {
1490 rc = -EFAULT;
1491 goto iqinf_exit;
1492 }
1493 if (copy_to_user(pqi + 1, &io_rsp[1], qi.input_buffer_length)) {
1494 rc = -EFAULT;
1495 goto iqinf_exit;
1496 }
1497 } else {
1498 pqi = (struct smb_query_info __user *)arg;
1499 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1500 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1501 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1502 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1503 sizeof(qi.input_buffer_length))) {
1504 rc = -EFAULT;
1505 goto iqinf_exit;
1506 }
1507 if (copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)) {
1508 rc = -EFAULT;
1509 goto iqinf_exit;
1510 }
1511 }
1512
1513 iqinf_exit:
1514 kfree(buffer);
1515 SMB2_open_free(&rqst[0]);
1516 if (qi.flags & PASSTHRU_FSCTL)
1517 SMB2_ioctl_free(&rqst[1]);
1518 else
1519 SMB2_query_info_free(&rqst[1]);
1520
1521 SMB2_close_free(&rqst[2]);
1522 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1523 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1524 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1525 return rc;
1526 }
1527
1528 static ssize_t
1529 smb2_copychunk_range(const unsigned int xid,
1530 struct cifsFileInfo *srcfile,
1531 struct cifsFileInfo *trgtfile, u64 src_off,
1532 u64 len, u64 dest_off)
1533 {
1534 int rc;
1535 unsigned int ret_data_len;
1536 struct copychunk_ioctl *pcchunk;
1537 struct copychunk_ioctl_rsp *retbuf = NULL;
1538 struct cifs_tcon *tcon;
1539 int chunks_copied = 0;
1540 bool chunk_sizes_updated = false;
1541 ssize_t bytes_written, total_bytes_written = 0;
1542
1543 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1544
1545 if (pcchunk == NULL)
1546 return -ENOMEM;
1547
1548 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1549 /* Request a key from the server to identify the source of the copy */
1550 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1551 srcfile->fid.persistent_fid,
1552 srcfile->fid.volatile_fid, pcchunk);
1553
1554 /* Note: request_res_key sets res_key null only if rc !=0 */
1555 if (rc)
1556 goto cchunk_out;
1557
1558 /* For now array only one chunk long, will make more flexible later */
1559 pcchunk->ChunkCount = cpu_to_le32(1);
1560 pcchunk->Reserved = 0;
1561 pcchunk->Reserved2 = 0;
1562
1563 tcon = tlink_tcon(trgtfile->tlink);
1564
1565 while (len > 0) {
1566 pcchunk->SourceOffset = cpu_to_le64(src_off);
1567 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1568 pcchunk->Length =
1569 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1570
1571 /* Request server copy to target from src identified by key */
1572 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1573 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1574 true /* is_fsctl */, (char *)pcchunk,
1575 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1576 (char **)&retbuf, &ret_data_len);
1577 if (rc == 0) {
1578 if (ret_data_len !=
1579 sizeof(struct copychunk_ioctl_rsp)) {
1580 cifs_dbg(VFS, "invalid cchunk response size\n");
1581 rc = -EIO;
1582 goto cchunk_out;
1583 }
1584 if (retbuf->TotalBytesWritten == 0) {
1585 cifs_dbg(FYI, "no bytes copied\n");
1586 rc = -EIO;
1587 goto cchunk_out;
1588 }
1589 /*
1590 * Check if server claimed to write more than we asked
1591 */
1592 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1593 le32_to_cpu(pcchunk->Length)) {
1594 cifs_dbg(VFS, "invalid copy chunk response\n");
1595 rc = -EIO;
1596 goto cchunk_out;
1597 }
1598 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1599 cifs_dbg(VFS, "invalid num chunks written\n");
1600 rc = -EIO;
1601 goto cchunk_out;
1602 }
1603 chunks_copied++;
1604
1605 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1606 src_off += bytes_written;
1607 dest_off += bytes_written;
1608 len -= bytes_written;
1609 total_bytes_written += bytes_written;
1610
1611 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1612 le32_to_cpu(retbuf->ChunksWritten),
1613 le32_to_cpu(retbuf->ChunkBytesWritten),
1614 bytes_written);
1615 } else if (rc == -EINVAL) {
1616 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1617 goto cchunk_out;
1618
1619 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1620 le32_to_cpu(retbuf->ChunksWritten),
1621 le32_to_cpu(retbuf->ChunkBytesWritten),
1622 le32_to_cpu(retbuf->TotalBytesWritten));
1623
1624 /*
1625 * Check if this is the first request using these sizes,
1626 * (ie check if copy succeed once with original sizes
1627 * and check if the server gave us different sizes after
1628 * we already updated max sizes on previous request).
1629 * if not then why is the server returning an error now
1630 */
1631 if ((chunks_copied != 0) || chunk_sizes_updated)
1632 goto cchunk_out;
1633
1634 /* Check that server is not asking us to grow size */
1635 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1636 tcon->max_bytes_chunk)
1637 tcon->max_bytes_chunk =
1638 le32_to_cpu(retbuf->ChunkBytesWritten);
1639 else
1640 goto cchunk_out; /* server gave us bogus size */
1641
1642 /* No need to change MaxChunks since already set to 1 */
1643 chunk_sizes_updated = true;
1644 } else
1645 goto cchunk_out;
1646 }
1647
1648 cchunk_out:
1649 kfree(pcchunk);
1650 kfree(retbuf);
1651 if (rc)
1652 return rc;
1653 else
1654 return total_bytes_written;
1655 }
1656
1657 static int
1658 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1659 struct cifs_fid *fid)
1660 {
1661 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1662 }
1663
1664 static unsigned int
1665 smb2_read_data_offset(char *buf)
1666 {
1667 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1668 return rsp->DataOffset;
1669 }
1670
1671 static unsigned int
1672 smb2_read_data_length(char *buf, bool in_remaining)
1673 {
1674 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1675
1676 if (in_remaining)
1677 return le32_to_cpu(rsp->DataRemaining);
1678
1679 return le32_to_cpu(rsp->DataLength);
1680 }
1681
1682
1683 static int
1684 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1685 struct cifs_io_parms *parms, unsigned int *bytes_read,
1686 char **buf, int *buf_type)
1687 {
1688 parms->persistent_fid = pfid->persistent_fid;
1689 parms->volatile_fid = pfid->volatile_fid;
1690 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1691 }
1692
1693 static int
1694 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1695 struct cifs_io_parms *parms, unsigned int *written,
1696 struct kvec *iov, unsigned long nr_segs)
1697 {
1698
1699 parms->persistent_fid = pfid->persistent_fid;
1700 parms->volatile_fid = pfid->volatile_fid;
1701 return SMB2_write(xid, parms, written, iov, nr_segs);
1702 }
1703
1704 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1705 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1706 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1707 {
1708 struct cifsInodeInfo *cifsi;
1709 int rc;
1710
1711 cifsi = CIFS_I(inode);
1712
1713 /* if file already sparse don't bother setting sparse again */
1714 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1715 return true; /* already sparse */
1716
1717 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1718 return true; /* already not sparse */
1719
1720 /*
1721 * Can't check for sparse support on share the usual way via the
1722 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1723 * since Samba server doesn't set the flag on the share, yet
1724 * supports the set sparse FSCTL and returns sparse correctly
1725 * in the file attributes. If we fail setting sparse though we
1726 * mark that server does not support sparse files for this share
1727 * to avoid repeatedly sending the unsupported fsctl to server
1728 * if the file is repeatedly extended.
1729 */
1730 if (tcon->broken_sparse_sup)
1731 return false;
1732
1733 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1734 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1735 true /* is_fctl */,
1736 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1737 if (rc) {
1738 tcon->broken_sparse_sup = true;
1739 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1740 return false;
1741 }
1742
1743 if (setsparse)
1744 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1745 else
1746 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1747
1748 return true;
1749 }
1750
1751 static int
1752 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1753 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1754 {
1755 __le64 eof = cpu_to_le64(size);
1756 struct inode *inode;
1757
1758 /*
1759 * If extending file more than one page make sparse. Many Linux fs
1760 * make files sparse by default when extending via ftruncate
1761 */
1762 inode = d_inode(cfile->dentry);
1763
1764 if (!set_alloc && (size > inode->i_size + 8192)) {
1765 __u8 set_sparse = 1;
1766
1767 /* whether set sparse succeeds or not, extend the file */
1768 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1769 }
1770
1771 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1772 cfile->fid.volatile_fid, cfile->pid, &eof);
1773 }
1774
1775 static int
1776 smb2_duplicate_extents(const unsigned int xid,
1777 struct cifsFileInfo *srcfile,
1778 struct cifsFileInfo *trgtfile, u64 src_off,
1779 u64 len, u64 dest_off)
1780 {
1781 int rc;
1782 unsigned int ret_data_len;
1783 struct duplicate_extents_to_file dup_ext_buf;
1784 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1785
1786 /* server fileays advertise duplicate extent support with this flag */
1787 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1788 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1789 return -EOPNOTSUPP;
1790
1791 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1792 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1793 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1794 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1795 dup_ext_buf.ByteCount = cpu_to_le64(len);
1796 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1797 src_off, dest_off, len);
1798
1799 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1800 if (rc)
1801 goto duplicate_extents_out;
1802
1803 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1804 trgtfile->fid.volatile_fid,
1805 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1806 true /* is_fsctl */,
1807 (char *)&dup_ext_buf,
1808 sizeof(struct duplicate_extents_to_file),
1809 CIFSMaxBufSize, NULL,
1810 &ret_data_len);
1811
1812 if (ret_data_len > 0)
1813 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1814
1815 duplicate_extents_out:
1816 return rc;
1817 }
1818
1819 static int
1820 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1821 struct cifsFileInfo *cfile)
1822 {
1823 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1824 cfile->fid.volatile_fid);
1825 }
1826
1827 static int
1828 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1829 struct cifsFileInfo *cfile)
1830 {
1831 struct fsctl_set_integrity_information_req integr_info;
1832 unsigned int ret_data_len;
1833
1834 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1835 integr_info.Flags = 0;
1836 integr_info.Reserved = 0;
1837
1838 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1839 cfile->fid.volatile_fid,
1840 FSCTL_SET_INTEGRITY_INFORMATION,
1841 true /* is_fsctl */,
1842 (char *)&integr_info,
1843 sizeof(struct fsctl_set_integrity_information_req),
1844 CIFSMaxBufSize, NULL,
1845 &ret_data_len);
1846
1847 }
1848
1849 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1850 #define GMT_TOKEN_SIZE 50
1851
1852 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1853
1854 /*
1855 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1856 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1857 */
1858 static int
1859 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1860 struct cifsFileInfo *cfile, void __user *ioc_buf)
1861 {
1862 char *retbuf = NULL;
1863 unsigned int ret_data_len = 0;
1864 int rc;
1865 u32 max_response_size;
1866 struct smb_snapshot_array snapshot_in;
1867
1868 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1869 return -EFAULT;
1870
1871 /*
1872 * Note that for snapshot queries that servers like Azure expect that
1873 * the first query be minimal size (and just used to get the number/size
1874 * of previous versions) so response size must be specified as EXACTLY
1875 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1876 * of eight bytes.
1877 */
1878 if (ret_data_len == 0)
1879 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1880 else
1881 max_response_size = CIFSMaxBufSize;
1882
1883 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1884 cfile->fid.volatile_fid,
1885 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1886 true /* is_fsctl */,
1887 NULL, 0 /* no input data */, max_response_size,
1888 (char **)&retbuf,
1889 &ret_data_len);
1890 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1891 rc, ret_data_len);
1892 if (rc)
1893 return rc;
1894
1895 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1896 /* Fixup buffer */
1897 if (copy_from_user(&snapshot_in, ioc_buf,
1898 sizeof(struct smb_snapshot_array))) {
1899 rc = -EFAULT;
1900 kfree(retbuf);
1901 return rc;
1902 }
1903
1904 /*
1905 * Check for min size, ie not large enough to fit even one GMT
1906 * token (snapshot). On the first ioctl some users may pass in
1907 * smaller size (or zero) to simply get the size of the array
1908 * so the user space caller can allocate sufficient memory
1909 * and retry the ioctl again with larger array size sufficient
1910 * to hold all of the snapshot GMT tokens on the second try.
1911 */
1912 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1913 ret_data_len = sizeof(struct smb_snapshot_array);
1914
1915 /*
1916 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1917 * the snapshot array (of 50 byte GMT tokens) each
1918 * representing an available previous version of the data
1919 */
1920 if (ret_data_len > (snapshot_in.snapshot_array_size +
1921 sizeof(struct smb_snapshot_array)))
1922 ret_data_len = snapshot_in.snapshot_array_size +
1923 sizeof(struct smb_snapshot_array);
1924
1925 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1926 rc = -EFAULT;
1927 }
1928
1929 kfree(retbuf);
1930 return rc;
1931 }
1932
1933 static int
1934 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1935 const char *path, struct cifs_sb_info *cifs_sb,
1936 struct cifs_fid *fid, __u16 search_flags,
1937 struct cifs_search_info *srch_inf)
1938 {
1939 __le16 *utf16_path;
1940 int rc;
1941 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1942 struct cifs_open_parms oparms;
1943
1944 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1945 if (!utf16_path)
1946 return -ENOMEM;
1947
1948 oparms.tcon = tcon;
1949 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1950 oparms.disposition = FILE_OPEN;
1951 if (backup_cred(cifs_sb))
1952 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1953 else
1954 oparms.create_options = 0;
1955 oparms.fid = fid;
1956 oparms.reconnect = false;
1957
1958 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1959 kfree(utf16_path);
1960 if (rc) {
1961 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1962 return rc;
1963 }
1964
1965 srch_inf->entries_in_buffer = 0;
1966 srch_inf->index_of_last_entry = 2;
1967
1968 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1969 fid->volatile_fid, 0, srch_inf);
1970 if (rc) {
1971 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1972 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1973 }
1974 return rc;
1975 }
1976
1977 static int
1978 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1979 struct cifs_fid *fid, __u16 search_flags,
1980 struct cifs_search_info *srch_inf)
1981 {
1982 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1983 fid->volatile_fid, 0, srch_inf);
1984 }
1985
1986 static int
1987 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1988 struct cifs_fid *fid)
1989 {
1990 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1991 }
1992
1993 /*
1994 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1995 * the number of credits and return true. Otherwise - return false.
1996 */
1997 static bool
1998 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
1999 {
2000 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2001
2002 if (shdr->Status != STATUS_PENDING)
2003 return false;
2004
2005 if (shdr->CreditRequest) {
2006 spin_lock(&server->req_lock);
2007 server->credits += le16_to_cpu(shdr->CreditRequest);
2008 spin_unlock(&server->req_lock);
2009 wake_up(&server->request_q);
2010 }
2011
2012 return true;
2013 }
2014
2015 static bool
2016 smb2_is_session_expired(char *buf)
2017 {
2018 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2019
2020 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2021 shdr->Status != STATUS_USER_SESSION_DELETED)
2022 return false;
2023
2024 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2025 le16_to_cpu(shdr->Command),
2026 le64_to_cpu(shdr->MessageId));
2027 cifs_dbg(FYI, "Session expired or deleted\n");
2028
2029 return true;
2030 }
2031
2032 static int
2033 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2034 struct cifsInodeInfo *cinode)
2035 {
2036 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2037 return SMB2_lease_break(0, tcon, cinode->lease_key,
2038 smb2_get_lease_state(cinode));
2039
2040 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2041 fid->volatile_fid,
2042 CIFS_CACHE_READ(cinode) ? 1 : 0);
2043 }
2044
2045 void
2046 smb2_set_related(struct smb_rqst *rqst)
2047 {
2048 struct smb2_sync_hdr *shdr;
2049
2050 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2051 if (shdr == NULL) {
2052 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2053 return;
2054 }
2055 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2056 }
2057
2058 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2059
2060 void
2061 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2062 {
2063 struct smb2_sync_hdr *shdr;
2064 struct cifs_ses *ses = tcon->ses;
2065 struct TCP_Server_Info *server = ses->server;
2066 unsigned long len = smb_rqst_len(server, rqst);
2067 int i, num_padding;
2068
2069 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2070 if (shdr == NULL) {
2071 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2072 return;
2073 }
2074
2075 /* SMB headers in a compound are 8 byte aligned. */
2076
2077 /* No padding needed */
2078 if (!(len & 7))
2079 goto finished;
2080
2081 num_padding = 8 - (len & 7);
2082 if (!smb3_encryption_required(tcon)) {
2083 /*
2084 * If we do not have encryption then we can just add an extra
2085 * iov for the padding.
2086 */
2087 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2088 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2089 rqst->rq_nvec++;
2090 len += num_padding;
2091 } else {
2092 /*
2093 * We can not add a small padding iov for the encryption case
2094 * because the encryption framework can not handle the padding
2095 * iovs.
2096 * We have to flatten this into a single buffer and add
2097 * the padding to it.
2098 */
2099 for (i = 1; i < rqst->rq_nvec; i++) {
2100 memcpy(rqst->rq_iov[0].iov_base +
2101 rqst->rq_iov[0].iov_len,
2102 rqst->rq_iov[i].iov_base,
2103 rqst->rq_iov[i].iov_len);
2104 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2105 }
2106 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2107 0, num_padding);
2108 rqst->rq_iov[0].iov_len += num_padding;
2109 len += num_padding;
2110 rqst->rq_nvec = 1;
2111 }
2112
2113 finished:
2114 shdr->NextCommand = cpu_to_le32(len);
2115 }
2116
2117 /*
2118 * Passes the query info response back to the caller on success.
2119 * Caller need to free this with free_rsp_buf().
2120 */
2121 int
2122 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2123 __le16 *utf16_path, u32 desired_access,
2124 u32 class, u32 type, u32 output_len,
2125 struct kvec *rsp, int *buftype,
2126 struct cifs_sb_info *cifs_sb)
2127 {
2128 struct cifs_ses *ses = tcon->ses;
2129 int flags = 0;
2130 struct smb_rqst rqst[3];
2131 int resp_buftype[3];
2132 struct kvec rsp_iov[3];
2133 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2134 struct kvec qi_iov[1];
2135 struct kvec close_iov[1];
2136 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2137 struct cifs_open_parms oparms;
2138 struct cifs_fid fid;
2139 int rc;
2140
2141 if (smb3_encryption_required(tcon))
2142 flags |= CIFS_TRANSFORM_REQ;
2143
2144 memset(rqst, 0, sizeof(rqst));
2145 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2146 memset(rsp_iov, 0, sizeof(rsp_iov));
2147
2148 memset(&open_iov, 0, sizeof(open_iov));
2149 rqst[0].rq_iov = open_iov;
2150 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2151
2152 oparms.tcon = tcon;
2153 oparms.desired_access = desired_access;
2154 oparms.disposition = FILE_OPEN;
2155 if (cifs_sb && backup_cred(cifs_sb))
2156 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2157 else
2158 oparms.create_options = 0;
2159 oparms.fid = &fid;
2160 oparms.reconnect = false;
2161
2162 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2163 if (rc)
2164 goto qic_exit;
2165 smb2_set_next_command(tcon, &rqst[0]);
2166
2167 memset(&qi_iov, 0, sizeof(qi_iov));
2168 rqst[1].rq_iov = qi_iov;
2169 rqst[1].rq_nvec = 1;
2170
2171 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2172 class, type, 0,
2173 output_len, 0,
2174 NULL);
2175 if (rc)
2176 goto qic_exit;
2177 smb2_set_next_command(tcon, &rqst[1]);
2178 smb2_set_related(&rqst[1]);
2179
2180 memset(&close_iov, 0, sizeof(close_iov));
2181 rqst[2].rq_iov = close_iov;
2182 rqst[2].rq_nvec = 1;
2183
2184 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2185 if (rc)
2186 goto qic_exit;
2187 smb2_set_related(&rqst[2]);
2188
2189 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2190 resp_buftype, rsp_iov);
2191 if (rc) {
2192 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2193 goto qic_exit;
2194 }
2195 *rsp = rsp_iov[1];
2196 *buftype = resp_buftype[1];
2197
2198 qic_exit:
2199 SMB2_open_free(&rqst[0]);
2200 SMB2_query_info_free(&rqst[1]);
2201 SMB2_close_free(&rqst[2]);
2202 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2203 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2204 return rc;
2205 }
2206
2207 static int
2208 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2209 struct kstatfs *buf)
2210 {
2211 struct smb2_query_info_rsp *rsp;
2212 struct smb2_fs_full_size_info *info = NULL;
2213 __le16 utf16_path = 0; /* Null - open root of share */
2214 struct kvec rsp_iov = {NULL, 0};
2215 int buftype = CIFS_NO_BUFFER;
2216 int rc;
2217
2218
2219 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2220 FILE_READ_ATTRIBUTES,
2221 FS_FULL_SIZE_INFORMATION,
2222 SMB2_O_INFO_FILESYSTEM,
2223 sizeof(struct smb2_fs_full_size_info),
2224 &rsp_iov, &buftype, NULL);
2225 if (rc)
2226 goto qfs_exit;
2227
2228 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2229 buf->f_type = SMB2_MAGIC_NUMBER;
2230 info = (struct smb2_fs_full_size_info *)(
2231 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2232 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2233 le32_to_cpu(rsp->OutputBufferLength),
2234 &rsp_iov,
2235 sizeof(struct smb2_fs_full_size_info));
2236 if (!rc)
2237 smb2_copy_fs_info_to_kstatfs(info, buf);
2238
2239 qfs_exit:
2240 free_rsp_buf(buftype, rsp_iov.iov_base);
2241 return rc;
2242 }
2243
2244 static int
2245 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2246 struct kstatfs *buf)
2247 {
2248 int rc;
2249 __le16 srch_path = 0; /* Null - open root of share */
2250 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2251 struct cifs_open_parms oparms;
2252 struct cifs_fid fid;
2253
2254 if (!tcon->posix_extensions)
2255 return smb2_queryfs(xid, tcon, buf);
2256
2257 oparms.tcon = tcon;
2258 oparms.desired_access = FILE_READ_ATTRIBUTES;
2259 oparms.disposition = FILE_OPEN;
2260 oparms.create_options = 0;
2261 oparms.fid = &fid;
2262 oparms.reconnect = false;
2263
2264 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2265 if (rc)
2266 return rc;
2267
2268 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2269 fid.volatile_fid, buf);
2270 buf->f_type = SMB2_MAGIC_NUMBER;
2271 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2272 return rc;
2273 }
2274
2275 static bool
2276 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2277 {
2278 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2279 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2280 }
2281
2282 static int
2283 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2284 __u64 length, __u32 type, int lock, int unlock, bool wait)
2285 {
2286 if (unlock && !lock)
2287 type = SMB2_LOCKFLAG_UNLOCK;
2288 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2289 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2290 current->tgid, length, offset, type, wait);
2291 }
2292
2293 static void
2294 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2295 {
2296 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2297 }
2298
2299 static void
2300 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2301 {
2302 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2303 }
2304
2305 static void
2306 smb2_new_lease_key(struct cifs_fid *fid)
2307 {
2308 generate_random_uuid(fid->lease_key);
2309 }
2310
2311 static int
2312 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2313 const char *search_name,
2314 struct dfs_info3_param **target_nodes,
2315 unsigned int *num_of_nodes,
2316 const struct nls_table *nls_codepage, int remap)
2317 {
2318 int rc;
2319 __le16 *utf16_path = NULL;
2320 int utf16_path_len = 0;
2321 struct cifs_tcon *tcon;
2322 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2323 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2324 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2325
2326 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2327
2328 /*
2329 * Try to use the IPC tcon, otherwise just use any
2330 */
2331 tcon = ses->tcon_ipc;
2332 if (tcon == NULL) {
2333 spin_lock(&cifs_tcp_ses_lock);
2334 tcon = list_first_entry_or_null(&ses->tcon_list,
2335 struct cifs_tcon,
2336 tcon_list);
2337 if (tcon)
2338 tcon->tc_count++;
2339 spin_unlock(&cifs_tcp_ses_lock);
2340 }
2341
2342 if (tcon == NULL) {
2343 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2344 ses);
2345 rc = -ENOTCONN;
2346 goto out;
2347 }
2348
2349 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2350 &utf16_path_len,
2351 nls_codepage, remap);
2352 if (!utf16_path) {
2353 rc = -ENOMEM;
2354 goto out;
2355 }
2356
2357 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2358 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2359 if (!dfs_req) {
2360 rc = -ENOMEM;
2361 goto out;
2362 }
2363
2364 /* Highest DFS referral version understood */
2365 dfs_req->MaxReferralLevel = DFS_VERSION;
2366
2367 /* Path to resolve in an UTF-16 null-terminated string */
2368 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2369
2370 do {
2371 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2372 FSCTL_DFS_GET_REFERRALS,
2373 true /* is_fsctl */,
2374 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2375 (char **)&dfs_rsp, &dfs_rsp_size);
2376 } while (rc == -EAGAIN);
2377
2378 if (rc) {
2379 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2380 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
2381 goto out;
2382 }
2383
2384 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2385 num_of_nodes, target_nodes,
2386 nls_codepage, remap, search_name,
2387 true /* is_unicode */);
2388 if (rc) {
2389 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2390 goto out;
2391 }
2392
2393 out:
2394 if (tcon && !tcon->ipc) {
2395 /* ipc tcons are not refcounted */
2396 spin_lock(&cifs_tcp_ses_lock);
2397 tcon->tc_count--;
2398 spin_unlock(&cifs_tcp_ses_lock);
2399 }
2400 kfree(utf16_path);
2401 kfree(dfs_req);
2402 kfree(dfs_rsp);
2403 return rc;
2404 }
2405 #define SMB2_SYMLINK_STRUCT_SIZE \
2406 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2407
2408 static int
2409 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2410 const char *full_path, char **target_path,
2411 struct cifs_sb_info *cifs_sb)
2412 {
2413 int rc;
2414 __le16 *utf16_path;
2415 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2416 struct cifs_open_parms oparms;
2417 struct cifs_fid fid;
2418 struct kvec err_iov = {NULL, 0};
2419 struct smb2_err_rsp *err_buf = NULL;
2420 int resp_buftype;
2421 struct smb2_symlink_err_rsp *symlink;
2422 unsigned int sub_len;
2423 unsigned int sub_offset;
2424 unsigned int print_len;
2425 unsigned int print_offset;
2426
2427 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2428
2429 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2430 if (!utf16_path)
2431 return -ENOMEM;
2432
2433 oparms.tcon = tcon;
2434 oparms.desired_access = FILE_READ_ATTRIBUTES;
2435 oparms.disposition = FILE_OPEN;
2436 if (backup_cred(cifs_sb))
2437 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2438 else
2439 oparms.create_options = 0;
2440 oparms.fid = &fid;
2441 oparms.reconnect = false;
2442
2443 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2444 &resp_buftype);
2445 if (!rc)
2446 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2447 if (!rc || !err_iov.iov_base) {
2448 rc = -ENOENT;
2449 goto free_path;
2450 }
2451
2452 err_buf = err_iov.iov_base;
2453 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2454 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2455 rc = -ENOENT;
2456 goto querty_exit;
2457 }
2458
2459 /* open must fail on symlink - reset rc */
2460 rc = 0;
2461 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2462 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2463 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2464 print_len = le16_to_cpu(symlink->PrintNameLength);
2465 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2466
2467 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2468 rc = -ENOENT;
2469 goto querty_exit;
2470 }
2471
2472 if (err_iov.iov_len <
2473 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2474 rc = -ENOENT;
2475 goto querty_exit;
2476 }
2477
2478 *target_path = cifs_strndup_from_utf16(
2479 (char *)symlink->PathBuffer + sub_offset,
2480 sub_len, true, cifs_sb->local_nls);
2481 if (!(*target_path)) {
2482 rc = -ENOMEM;
2483 goto querty_exit;
2484 }
2485 convert_delimiter(*target_path, '/');
2486 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2487
2488 querty_exit:
2489 free_rsp_buf(resp_buftype, err_buf);
2490 free_path:
2491 kfree(utf16_path);
2492 return rc;
2493 }
2494
2495 #ifdef CONFIG_CIFS_ACL
2496 static struct cifs_ntsd *
2497 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2498 const struct cifs_fid *cifsfid, u32 *pacllen)
2499 {
2500 struct cifs_ntsd *pntsd = NULL;
2501 unsigned int xid;
2502 int rc = -EOPNOTSUPP;
2503 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2504
2505 if (IS_ERR(tlink))
2506 return ERR_CAST(tlink);
2507
2508 xid = get_xid();
2509 cifs_dbg(FYI, "trying to get acl\n");
2510
2511 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2512 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2513 free_xid(xid);
2514
2515 cifs_put_tlink(tlink);
2516
2517 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2518 if (rc)
2519 return ERR_PTR(rc);
2520 return pntsd;
2521
2522 }
2523
2524 static struct cifs_ntsd *
2525 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2526 const char *path, u32 *pacllen)
2527 {
2528 struct cifs_ntsd *pntsd = NULL;
2529 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2530 unsigned int xid;
2531 int rc;
2532 struct cifs_tcon *tcon;
2533 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2534 struct cifs_fid fid;
2535 struct cifs_open_parms oparms;
2536 __le16 *utf16_path;
2537
2538 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2539 if (IS_ERR(tlink))
2540 return ERR_CAST(tlink);
2541
2542 tcon = tlink_tcon(tlink);
2543 xid = get_xid();
2544
2545 if (backup_cred(cifs_sb))
2546 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2547 else
2548 oparms.create_options = 0;
2549
2550 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2551 if (!utf16_path) {
2552 rc = -ENOMEM;
2553 free_xid(xid);
2554 return ERR_PTR(rc);
2555 }
2556
2557 oparms.tcon = tcon;
2558 oparms.desired_access = READ_CONTROL;
2559 oparms.disposition = FILE_OPEN;
2560 oparms.fid = &fid;
2561 oparms.reconnect = false;
2562
2563 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2564 kfree(utf16_path);
2565 if (!rc) {
2566 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2567 fid.volatile_fid, (void **)&pntsd, pacllen);
2568 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2569 }
2570
2571 cifs_put_tlink(tlink);
2572 free_xid(xid);
2573
2574 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2575 if (rc)
2576 return ERR_PTR(rc);
2577 return pntsd;
2578 }
2579
2580 #ifdef CONFIG_CIFS_ACL
2581 static int
2582 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2583 struct inode *inode, const char *path, int aclflag)
2584 {
2585 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2586 unsigned int xid;
2587 int rc, access_flags = 0;
2588 struct cifs_tcon *tcon;
2589 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2590 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2591 struct cifs_fid fid;
2592 struct cifs_open_parms oparms;
2593 __le16 *utf16_path;
2594
2595 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2596 if (IS_ERR(tlink))
2597 return PTR_ERR(tlink);
2598
2599 tcon = tlink_tcon(tlink);
2600 xid = get_xid();
2601
2602 if (backup_cred(cifs_sb))
2603 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2604 else
2605 oparms.create_options = 0;
2606
2607 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2608 access_flags = WRITE_OWNER;
2609 else
2610 access_flags = WRITE_DAC;
2611
2612 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2613 if (!utf16_path) {
2614 rc = -ENOMEM;
2615 free_xid(xid);
2616 return rc;
2617 }
2618
2619 oparms.tcon = tcon;
2620 oparms.desired_access = access_flags;
2621 oparms.disposition = FILE_OPEN;
2622 oparms.path = path;
2623 oparms.fid = &fid;
2624 oparms.reconnect = false;
2625
2626 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2627 kfree(utf16_path);
2628 if (!rc) {
2629 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2630 fid.volatile_fid, pnntsd, acllen, aclflag);
2631 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2632 }
2633
2634 cifs_put_tlink(tlink);
2635 free_xid(xid);
2636 return rc;
2637 }
2638 #endif /* CIFS_ACL */
2639
2640 /* Retrieve an ACL from the server */
2641 static struct cifs_ntsd *
2642 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2643 struct inode *inode, const char *path,
2644 u32 *pacllen)
2645 {
2646 struct cifs_ntsd *pntsd = NULL;
2647 struct cifsFileInfo *open_file = NULL;
2648
2649 if (inode)
2650 open_file = find_readable_file(CIFS_I(inode), true);
2651 if (!open_file)
2652 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2653
2654 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2655 cifsFileInfo_put(open_file);
2656 return pntsd;
2657 }
2658 #endif
2659
2660 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2661 loff_t offset, loff_t len, bool keep_size)
2662 {
2663 struct cifs_ses *ses = tcon->ses;
2664 struct inode *inode;
2665 struct cifsInodeInfo *cifsi;
2666 struct cifsFileInfo *cfile = file->private_data;
2667 struct file_zero_data_information fsctl_buf;
2668 struct smb_rqst rqst[2];
2669 int resp_buftype[2];
2670 struct kvec rsp_iov[2];
2671 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2672 struct kvec si_iov[1];
2673 unsigned int size[1];
2674 void *data[1];
2675 long rc;
2676 unsigned int xid;
2677 int num = 0, flags = 0;
2678 __le64 eof;
2679
2680 xid = get_xid();
2681
2682 inode = d_inode(cfile->dentry);
2683 cifsi = CIFS_I(inode);
2684
2685 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2686 ses->Suid, offset, len);
2687
2688
2689 /* if file not oplocked can't be sure whether asking to extend size */
2690 if (!CIFS_CACHE_READ(cifsi))
2691 if (keep_size == false) {
2692 rc = -EOPNOTSUPP;
2693 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2694 tcon->tid, ses->Suid, offset, len, rc);
2695 free_xid(xid);
2696 return rc;
2697 }
2698
2699 /*
2700 * Must check if file sparse since fallocate -z (zero range) assumes
2701 * non-sparse allocation
2702 */
2703 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2704 rc = -EOPNOTSUPP;
2705 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2706 ses->Suid, offset, len, rc);
2707 free_xid(xid);
2708 return rc;
2709 }
2710
2711 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2712
2713 fsctl_buf.FileOffset = cpu_to_le64(offset);
2714 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2715
2716 if (smb3_encryption_required(tcon))
2717 flags |= CIFS_TRANSFORM_REQ;
2718
2719 memset(rqst, 0, sizeof(rqst));
2720 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2721 memset(rsp_iov, 0, sizeof(rsp_iov));
2722
2723
2724 memset(&io_iov, 0, sizeof(io_iov));
2725 rqst[num].rq_iov = io_iov;
2726 rqst[num].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2727 rc = SMB2_ioctl_init(tcon, &rqst[num++], cfile->fid.persistent_fid,
2728 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2729 true /* is_fctl */, (char *)&fsctl_buf,
2730 sizeof(struct file_zero_data_information),
2731 CIFSMaxBufSize);
2732 if (rc)
2733 goto zero_range_exit;
2734
2735 /*
2736 * do we also need to change the size of the file?
2737 */
2738 if (keep_size == false && i_size_read(inode) < offset + len) {
2739 smb2_set_next_command(tcon, &rqst[0]);
2740
2741 memset(&si_iov, 0, sizeof(si_iov));
2742 rqst[num].rq_iov = si_iov;
2743 rqst[num].rq_nvec = 1;
2744
2745 eof = cpu_to_le64(offset + len);
2746 size[0] = 8; /* sizeof __le64 */
2747 data[0] = &eof;
2748
2749 rc = SMB2_set_info_init(tcon, &rqst[num++],
2750 cfile->fid.persistent_fid,
2751 cfile->fid.persistent_fid,
2752 current->tgid,
2753 FILE_END_OF_FILE_INFORMATION,
2754 SMB2_O_INFO_FILE, 0, data, size);
2755 smb2_set_related(&rqst[1]);
2756 }
2757
2758 rc = compound_send_recv(xid, ses, flags, num, rqst,
2759 resp_buftype, rsp_iov);
2760
2761 zero_range_exit:
2762 SMB2_ioctl_free(&rqst[0]);
2763 SMB2_set_info_free(&rqst[1]);
2764 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2765 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2766 free_xid(xid);
2767 if (rc)
2768 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2769 ses->Suid, offset, len, rc);
2770 else
2771 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
2772 ses->Suid, offset, len);
2773 return rc;
2774 }
2775
2776 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2777 loff_t offset, loff_t len)
2778 {
2779 struct inode *inode;
2780 struct cifsInodeInfo *cifsi;
2781 struct cifsFileInfo *cfile = file->private_data;
2782 struct file_zero_data_information fsctl_buf;
2783 long rc;
2784 unsigned int xid;
2785 __u8 set_sparse = 1;
2786
2787 xid = get_xid();
2788
2789 inode = d_inode(cfile->dentry);
2790 cifsi = CIFS_I(inode);
2791
2792 /* Need to make file sparse, if not already, before freeing range. */
2793 /* Consider adding equivalent for compressed since it could also work */
2794 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2795 rc = -EOPNOTSUPP;
2796 free_xid(xid);
2797 return rc;
2798 }
2799
2800 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2801
2802 fsctl_buf.FileOffset = cpu_to_le64(offset);
2803 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2804
2805 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2806 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2807 true /* is_fctl */, (char *)&fsctl_buf,
2808 sizeof(struct file_zero_data_information),
2809 CIFSMaxBufSize, NULL, NULL);
2810 free_xid(xid);
2811 return rc;
2812 }
2813
2814 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2815 loff_t off, loff_t len, bool keep_size)
2816 {
2817 struct inode *inode;
2818 struct cifsInodeInfo *cifsi;
2819 struct cifsFileInfo *cfile = file->private_data;
2820 long rc = -EOPNOTSUPP;
2821 unsigned int xid;
2822 __le64 eof;
2823
2824 xid = get_xid();
2825
2826 inode = d_inode(cfile->dentry);
2827 cifsi = CIFS_I(inode);
2828
2829 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2830 tcon->ses->Suid, off, len);
2831 /* if file not oplocked can't be sure whether asking to extend size */
2832 if (!CIFS_CACHE_READ(cifsi))
2833 if (keep_size == false) {
2834 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2835 tcon->tid, tcon->ses->Suid, off, len, rc);
2836 free_xid(xid);
2837 return rc;
2838 }
2839
2840 /*
2841 * Files are non-sparse by default so falloc may be a no-op
2842 * Must check if file sparse. If not sparse, and not extending
2843 * then no need to do anything since file already allocated
2844 */
2845 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2846 if (keep_size == true)
2847 rc = 0;
2848 /* check if extending file */
2849 else if (i_size_read(inode) >= off + len)
2850 /* not extending file and already not sparse */
2851 rc = 0;
2852 /* BB: in future add else clause to extend file */
2853 else
2854 rc = -EOPNOTSUPP;
2855 if (rc)
2856 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2857 tcon->tid, tcon->ses->Suid, off, len, rc);
2858 else
2859 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
2860 tcon->tid, tcon->ses->Suid, off, len);
2861 free_xid(xid);
2862 return rc;
2863 }
2864
2865 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2866 /*
2867 * Check if falloc starts within first few pages of file
2868 * and ends within a few pages of the end of file to
2869 * ensure that most of file is being forced to be
2870 * fallocated now. If so then setting whole file sparse
2871 * ie potentially making a few extra pages at the beginning
2872 * or end of the file non-sparse via set_sparse is harmless.
2873 */
2874 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2875 rc = -EOPNOTSUPP;
2876 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2877 tcon->tid, tcon->ses->Suid, off, len, rc);
2878 free_xid(xid);
2879 return rc;
2880 }
2881
2882 smb2_set_sparse(xid, tcon, cfile, inode, false);
2883 rc = 0;
2884 } else {
2885 smb2_set_sparse(xid, tcon, cfile, inode, false);
2886 rc = 0;
2887 if (i_size_read(inode) < off + len) {
2888 eof = cpu_to_le64(off + len);
2889 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2890 cfile->fid.volatile_fid, cfile->pid,
2891 &eof);
2892 }
2893 }
2894
2895 if (rc)
2896 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
2897 tcon->ses->Suid, off, len, rc);
2898 else
2899 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
2900 tcon->ses->Suid, off, len);
2901
2902 free_xid(xid);
2903 return rc;
2904 }
2905
2906
2907 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2908 loff_t off, loff_t len)
2909 {
2910 /* KEEP_SIZE already checked for by do_fallocate */
2911 if (mode & FALLOC_FL_PUNCH_HOLE)
2912 return smb3_punch_hole(file, tcon, off, len);
2913 else if (mode & FALLOC_FL_ZERO_RANGE) {
2914 if (mode & FALLOC_FL_KEEP_SIZE)
2915 return smb3_zero_range(file, tcon, off, len, true);
2916 return smb3_zero_range(file, tcon, off, len, false);
2917 } else if (mode == FALLOC_FL_KEEP_SIZE)
2918 return smb3_simple_falloc(file, tcon, off, len, true);
2919 else if (mode == 0)
2920 return smb3_simple_falloc(file, tcon, off, len, false);
2921
2922 return -EOPNOTSUPP;
2923 }
2924
2925 static void
2926 smb2_downgrade_oplock(struct TCP_Server_Info *server,
2927 struct cifsInodeInfo *cinode, bool set_level2)
2928 {
2929 if (set_level2)
2930 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2931 0, NULL);
2932 else
2933 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2934 }
2935
2936 static void
2937 smb21_downgrade_oplock(struct TCP_Server_Info *server,
2938 struct cifsInodeInfo *cinode, bool set_level2)
2939 {
2940 server->ops->set_oplock_level(cinode,
2941 set_level2 ? SMB2_LEASE_READ_CACHING_HE :
2942 0, 0, NULL);
2943 }
2944
2945 static void
2946 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2947 unsigned int epoch, bool *purge_cache)
2948 {
2949 oplock &= 0xFF;
2950 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2951 return;
2952 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2953 cinode->oplock = CIFS_CACHE_RHW_FLG;
2954 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2955 &cinode->vfs_inode);
2956 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
2957 cinode->oplock = CIFS_CACHE_RW_FLG;
2958 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2959 &cinode->vfs_inode);
2960 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2961 cinode->oplock = CIFS_CACHE_READ_FLG;
2962 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2963 &cinode->vfs_inode);
2964 } else
2965 cinode->oplock = 0;
2966 }
2967
2968 static void
2969 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2970 unsigned int epoch, bool *purge_cache)
2971 {
2972 char message[5] = {0};
2973 unsigned int new_oplock = 0;
2974
2975 oplock &= 0xFF;
2976 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2977 return;
2978
2979 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2980 new_oplock |= CIFS_CACHE_READ_FLG;
2981 strcat(message, "R");
2982 }
2983 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2984 new_oplock |= CIFS_CACHE_HANDLE_FLG;
2985 strcat(message, "H");
2986 }
2987 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2988 new_oplock |= CIFS_CACHE_WRITE_FLG;
2989 strcat(message, "W");
2990 }
2991 if (!new_oplock)
2992 strncpy(message, "None", sizeof(message));
2993
2994 cinode->oplock = new_oplock;
2995 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2996 &cinode->vfs_inode);
2997 }
2998
2999 static void
3000 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3001 unsigned int epoch, bool *purge_cache)
3002 {
3003 unsigned int old_oplock = cinode->oplock;
3004
3005 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3006
3007 if (purge_cache) {
3008 *purge_cache = false;
3009 if (old_oplock == CIFS_CACHE_READ_FLG) {
3010 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3011 (epoch - cinode->epoch > 0))
3012 *purge_cache = true;
3013 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3014 (epoch - cinode->epoch > 1))
3015 *purge_cache = true;
3016 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3017 (epoch - cinode->epoch > 1))
3018 *purge_cache = true;
3019 else if (cinode->oplock == 0 &&
3020 (epoch - cinode->epoch > 0))
3021 *purge_cache = true;
3022 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3023 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3024 (epoch - cinode->epoch > 0))
3025 *purge_cache = true;
3026 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3027 (epoch - cinode->epoch > 1))
3028 *purge_cache = true;
3029 }
3030 cinode->epoch = epoch;
3031 }
3032 }
3033
3034 static bool
3035 smb2_is_read_op(__u32 oplock)
3036 {
3037 return oplock == SMB2_OPLOCK_LEVEL_II;
3038 }
3039
3040 static bool
3041 smb21_is_read_op(__u32 oplock)
3042 {
3043 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3044 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3045 }
3046
3047 static __le32
3048 map_oplock_to_lease(u8 oplock)
3049 {
3050 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3051 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3052 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3053 return SMB2_LEASE_READ_CACHING;
3054 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3055 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3056 SMB2_LEASE_WRITE_CACHING;
3057 return 0;
3058 }
3059
3060 static char *
3061 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3062 {
3063 struct create_lease *buf;
3064
3065 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3066 if (!buf)
3067 return NULL;
3068
3069 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3070 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3071
3072 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3073 (struct create_lease, lcontext));
3074 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3075 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3076 (struct create_lease, Name));
3077 buf->ccontext.NameLength = cpu_to_le16(4);
3078 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3079 buf->Name[0] = 'R';
3080 buf->Name[1] = 'q';
3081 buf->Name[2] = 'L';
3082 buf->Name[3] = 's';
3083 return (char *)buf;
3084 }
3085
3086 static char *
3087 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3088 {
3089 struct create_lease_v2 *buf;
3090
3091 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3092 if (!buf)
3093 return NULL;
3094
3095 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3096 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3097
3098 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3099 (struct create_lease_v2, lcontext));
3100 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3101 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3102 (struct create_lease_v2, Name));
3103 buf->ccontext.NameLength = cpu_to_le16(4);
3104 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3105 buf->Name[0] = 'R';
3106 buf->Name[1] = 'q';
3107 buf->Name[2] = 'L';
3108 buf->Name[3] = 's';
3109 return (char *)buf;
3110 }
3111
3112 static __u8
3113 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3114 {
3115 struct create_lease *lc = (struct create_lease *)buf;
3116
3117 *epoch = 0; /* not used */
3118 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3119 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3120 return le32_to_cpu(lc->lcontext.LeaseState);
3121 }
3122
3123 static __u8
3124 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3125 {
3126 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3127
3128 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3129 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3130 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3131 if (lease_key)
3132 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3133 return le32_to_cpu(lc->lcontext.LeaseState);
3134 }
3135
3136 static unsigned int
3137 smb2_wp_retry_size(struct inode *inode)
3138 {
3139 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3140 SMB2_MAX_BUFFER_SIZE);
3141 }
3142
3143 static bool
3144 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3145 {
3146 return !cfile->invalidHandle;
3147 }
3148
3149 static void
3150 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3151 struct smb_rqst *old_rq)
3152 {
3153 struct smb2_sync_hdr *shdr =
3154 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3155
3156 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3157 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3158 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3159 tr_hdr->Flags = cpu_to_le16(0x01);
3160 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3161 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3162 }
3163
3164 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3165 * stack object as buf.
3166 */
3167 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3168 unsigned int buflen)
3169 {
3170 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
3171 }
3172
3173 /* Assumes the first rqst has a transform header as the first iov.
3174 * I.e.
3175 * rqst[0].rq_iov[0] is transform header
3176 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3177 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3178 */
3179 static struct scatterlist *
3180 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3181 {
3182 unsigned int sg_len;
3183 struct scatterlist *sg;
3184 unsigned int i;
3185 unsigned int j;
3186 unsigned int idx = 0;
3187 int skip;
3188
3189 sg_len = 1;
3190 for (i = 0; i < num_rqst; i++)
3191 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3192
3193 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3194 if (!sg)
3195 return NULL;
3196
3197 sg_init_table(sg, sg_len);
3198 for (i = 0; i < num_rqst; i++) {
3199 for (j = 0; j < rqst[i].rq_nvec; j++) {
3200 /*
3201 * The first rqst has a transform header where the
3202 * first 20 bytes are not part of the encrypted blob
3203 */
3204 skip = (i == 0) && (j == 0) ? 20 : 0;
3205 smb2_sg_set_buf(&sg[idx++],
3206 rqst[i].rq_iov[j].iov_base + skip,
3207 rqst[i].rq_iov[j].iov_len - skip);
3208 }
3209
3210 for (j = 0; j < rqst[i].rq_npages; j++) {
3211 unsigned int len, offset;
3212
3213 rqst_page_get_length(&rqst[i], j, &len, &offset);
3214 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3215 }
3216 }
3217 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3218 return sg;
3219 }
3220
3221 static int
3222 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3223 {
3224 struct cifs_ses *ses;
3225 u8 *ses_enc_key;
3226
3227 spin_lock(&cifs_tcp_ses_lock);
3228 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3229 if (ses->Suid != ses_id)
3230 continue;
3231 ses_enc_key = enc ? ses->smb3encryptionkey :
3232 ses->smb3decryptionkey;
3233 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3234 spin_unlock(&cifs_tcp_ses_lock);
3235 return 0;
3236 }
3237 spin_unlock(&cifs_tcp_ses_lock);
3238
3239 return 1;
3240 }
3241 /*
3242 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3243 * iov[0] - transform header (associate data),
3244 * iov[1-N] - SMB2 header and pages - data to encrypt.
3245 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3246 * untouched.
3247 */
3248 static int
3249 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3250 struct smb_rqst *rqst, int enc)
3251 {
3252 struct smb2_transform_hdr *tr_hdr =
3253 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3254 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3255 int rc = 0;
3256 struct scatterlist *sg;
3257 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3258 u8 key[SMB3_SIGN_KEY_SIZE];
3259 struct aead_request *req;
3260 char *iv;
3261 unsigned int iv_len;
3262 DECLARE_CRYPTO_WAIT(wait);
3263 struct crypto_aead *tfm;
3264 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3265
3266 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3267 if (rc) {
3268 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3269 enc ? "en" : "de");
3270 return 0;
3271 }
3272
3273 rc = smb3_crypto_aead_allocate(server);
3274 if (rc) {
3275 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3276 return rc;
3277 }
3278
3279 tfm = enc ? server->secmech.ccmaesencrypt :
3280 server->secmech.ccmaesdecrypt;
3281 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3282 if (rc) {
3283 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3284 return rc;
3285 }
3286
3287 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3288 if (rc) {
3289 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3290 return rc;
3291 }
3292
3293 req = aead_request_alloc(tfm, GFP_KERNEL);
3294 if (!req) {
3295 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
3296 return -ENOMEM;
3297 }
3298
3299 if (!enc) {
3300 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3301 crypt_len += SMB2_SIGNATURE_SIZE;
3302 }
3303
3304 sg = init_sg(num_rqst, rqst, sign);
3305 if (!sg) {
3306 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
3307 rc = -ENOMEM;
3308 goto free_req;
3309 }
3310
3311 iv_len = crypto_aead_ivsize(tfm);
3312 iv = kzalloc(iv_len, GFP_KERNEL);
3313 if (!iv) {
3314 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
3315 rc = -ENOMEM;
3316 goto free_sg;
3317 }
3318 iv[0] = 3;
3319 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3320
3321 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3322 aead_request_set_ad(req, assoc_data_len);
3323
3324 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3325 crypto_req_done, &wait);
3326
3327 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3328 : crypto_aead_decrypt(req), &wait);
3329
3330 if (!rc && enc)
3331 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3332
3333 kfree(iv);
3334 free_sg:
3335 kfree(sg);
3336 free_req:
3337 kfree(req);
3338 return rc;
3339 }
3340
3341 void
3342 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3343 {
3344 int i, j;
3345
3346 for (i = 0; i < num_rqst; i++) {
3347 if (rqst[i].rq_pages) {
3348 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3349 put_page(rqst[i].rq_pages[j]);
3350 kfree(rqst[i].rq_pages);
3351 }
3352 }
3353 }
3354
3355 /*
3356 * This function will initialize new_rq and encrypt the content.
3357 * The first entry, new_rq[0], only contains a single iov which contains
3358 * a smb2_transform_hdr and is pre-allocated by the caller.
3359 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3360 *
3361 * The end result is an array of smb_rqst structures where the first structure
3362 * only contains a single iov for the transform header which we then can pass
3363 * to crypt_message().
3364 *
3365 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3366 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3367 */
3368 static int
3369 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3370 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3371 {
3372 struct page **pages;
3373 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3374 unsigned int npages;
3375 unsigned int orig_len = 0;
3376 int i, j;
3377 int rc = -ENOMEM;
3378
3379 for (i = 1; i < num_rqst; i++) {
3380 npages = old_rq[i - 1].rq_npages;
3381 pages = kmalloc_array(npages, sizeof(struct page *),
3382 GFP_KERNEL);
3383 if (!pages)
3384 goto err_free;
3385
3386 new_rq[i].rq_pages = pages;
3387 new_rq[i].rq_npages = npages;
3388 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3389 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3390 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3391 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3392 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3393
3394 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3395
3396 for (j = 0; j < npages; j++) {
3397 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3398 if (!pages[j])
3399 goto err_free;
3400 }
3401
3402 /* copy pages form the old */
3403 for (j = 0; j < npages; j++) {
3404 char *dst, *src;
3405 unsigned int offset, len;
3406
3407 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3408
3409 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3410 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3411
3412 memcpy(dst, src, len);
3413 kunmap(new_rq[i].rq_pages[j]);
3414 kunmap(old_rq[i - 1].rq_pages[j]);
3415 }
3416 }
3417
3418 /* fill the 1st iov with a transform header */
3419 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3420
3421 rc = crypt_message(server, num_rqst, new_rq, 1);
3422 cifs_dbg(FYI, "encrypt message returned %d", rc);
3423 if (rc)
3424 goto err_free;
3425
3426 return rc;
3427
3428 err_free:
3429 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3430 return rc;
3431 }
3432
3433 static int
3434 smb3_is_transform_hdr(void *buf)
3435 {
3436 struct smb2_transform_hdr *trhdr = buf;
3437
3438 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3439 }
3440
3441 static int
3442 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3443 unsigned int buf_data_size, struct page **pages,
3444 unsigned int npages, unsigned int page_data_size)
3445 {
3446 struct kvec iov[2];
3447 struct smb_rqst rqst = {NULL};
3448 int rc;
3449
3450 iov[0].iov_base = buf;
3451 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3452 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3453 iov[1].iov_len = buf_data_size;
3454
3455 rqst.rq_iov = iov;
3456 rqst.rq_nvec = 2;
3457 rqst.rq_pages = pages;
3458 rqst.rq_npages = npages;
3459 rqst.rq_pagesz = PAGE_SIZE;
3460 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3461
3462 rc = crypt_message(server, 1, &rqst, 0);
3463 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3464
3465 if (rc)
3466 return rc;
3467
3468 memmove(buf, iov[1].iov_base, buf_data_size);
3469
3470 server->total_read = buf_data_size + page_data_size;
3471
3472 return rc;
3473 }
3474
3475 static int
3476 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3477 unsigned int npages, unsigned int len)
3478 {
3479 int i;
3480 int length;
3481
3482 for (i = 0; i < npages; i++) {
3483 struct page *page = pages[i];
3484 size_t n;
3485
3486 n = len;
3487 if (len >= PAGE_SIZE) {
3488 /* enough data to fill the page */
3489 n = PAGE_SIZE;
3490 len -= n;
3491 } else {
3492 zero_user(page, len, PAGE_SIZE - len);
3493 len = 0;
3494 }
3495 length = cifs_read_page_from_socket(server, page, 0, n);
3496 if (length < 0)
3497 return length;
3498 server->total_read += length;
3499 }
3500
3501 return 0;
3502 }
3503
3504 static int
3505 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3506 unsigned int cur_off, struct bio_vec **page_vec)
3507 {
3508 struct bio_vec *bvec;
3509 int i;
3510
3511 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3512 if (!bvec)
3513 return -ENOMEM;
3514
3515 for (i = 0; i < npages; i++) {
3516 bvec[i].bv_page = pages[i];
3517 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3518 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3519 data_size -= bvec[i].bv_len;
3520 }
3521
3522 if (data_size != 0) {
3523 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3524 kfree(bvec);
3525 return -EIO;
3526 }
3527
3528 *page_vec = bvec;
3529 return 0;
3530 }
3531
3532 static int
3533 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3534 char *buf, unsigned int buf_len, struct page **pages,
3535 unsigned int npages, unsigned int page_data_size)
3536 {
3537 unsigned int data_offset;
3538 unsigned int data_len;
3539 unsigned int cur_off;
3540 unsigned int cur_page_idx;
3541 unsigned int pad_len;
3542 struct cifs_readdata *rdata = mid->callback_data;
3543 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3544 struct bio_vec *bvec = NULL;
3545 struct iov_iter iter;
3546 struct kvec iov;
3547 int length;
3548 bool use_rdma_mr = false;
3549
3550 if (shdr->Command != SMB2_READ) {
3551 cifs_dbg(VFS, "only big read responses are supported\n");
3552 return -ENOTSUPP;
3553 }
3554
3555 if (server->ops->is_session_expired &&
3556 server->ops->is_session_expired(buf)) {
3557 cifs_reconnect(server);
3558 wake_up(&server->response_q);
3559 return -1;
3560 }
3561
3562 if (server->ops->is_status_pending &&
3563 server->ops->is_status_pending(buf, server))
3564 return -1;
3565
3566 /* set up first two iov to get credits */
3567 rdata->iov[0].iov_base = buf;
3568 rdata->iov[0].iov_len = 0;
3569 rdata->iov[1].iov_base = buf;
3570 rdata->iov[1].iov_len =
3571 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
3572 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3573 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3574 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3575 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3576
3577 rdata->result = server->ops->map_error(buf, true);
3578 if (rdata->result != 0) {
3579 cifs_dbg(FYI, "%s: server returned error %d\n",
3580 __func__, rdata->result);
3581 /* normal error on read response */
3582 dequeue_mid(mid, false);
3583 return 0;
3584 }
3585
3586 data_offset = server->ops->read_data_offset(buf);
3587 #ifdef CONFIG_CIFS_SMB_DIRECT
3588 use_rdma_mr = rdata->mr;
3589 #endif
3590 data_len = server->ops->read_data_length(buf, use_rdma_mr);
3591
3592 if (data_offset < server->vals->read_rsp_size) {
3593 /*
3594 * win2k8 sometimes sends an offset of 0 when the read
3595 * is beyond the EOF. Treat it as if the data starts just after
3596 * the header.
3597 */
3598 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3599 __func__, data_offset);
3600 data_offset = server->vals->read_rsp_size;
3601 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3602 /* data_offset is beyond the end of smallbuf */
3603 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3604 __func__, data_offset);
3605 rdata->result = -EIO;
3606 dequeue_mid(mid, rdata->result);
3607 return 0;
3608 }
3609
3610 pad_len = data_offset - server->vals->read_rsp_size;
3611
3612 if (buf_len <= data_offset) {
3613 /* read response payload is in pages */
3614 cur_page_idx = pad_len / PAGE_SIZE;
3615 cur_off = pad_len % PAGE_SIZE;
3616
3617 if (cur_page_idx != 0) {
3618 /* data offset is beyond the 1st page of response */
3619 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3620 __func__, data_offset);
3621 rdata->result = -EIO;
3622 dequeue_mid(mid, rdata->result);
3623 return 0;
3624 }
3625
3626 if (data_len > page_data_size - pad_len) {
3627 /* data_len is corrupt -- discard frame */
3628 rdata->result = -EIO;
3629 dequeue_mid(mid, rdata->result);
3630 return 0;
3631 }
3632
3633 rdata->result = init_read_bvec(pages, npages, page_data_size,
3634 cur_off, &bvec);
3635 if (rdata->result != 0) {
3636 dequeue_mid(mid, rdata->result);
3637 return 0;
3638 }
3639
3640 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3641 } else if (buf_len >= data_offset + data_len) {
3642 /* read response payload is in buf */
3643 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3644 iov.iov_base = buf + data_offset;
3645 iov.iov_len = data_len;
3646 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3647 } else {
3648 /* read response payload cannot be in both buf and pages */
3649 WARN_ONCE(1, "buf can not contain only a part of read data");
3650 rdata->result = -EIO;
3651 dequeue_mid(mid, rdata->result);
3652 return 0;
3653 }
3654
3655 length = rdata->copy_into_pages(server, rdata, &iter);
3656
3657 kfree(bvec);
3658
3659 if (length < 0)
3660 return length;
3661
3662 dequeue_mid(mid, false);
3663 return length;
3664 }
3665
3666 static int
3667 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3668 {
3669 char *buf = server->smallbuf;
3670 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3671 unsigned int npages;
3672 struct page **pages;
3673 unsigned int len;
3674 unsigned int buflen = server->pdu_size;
3675 int rc;
3676 int i = 0;
3677
3678 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3679 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3680
3681 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3682 if (rc < 0)
3683 return rc;
3684 server->total_read += rc;
3685
3686 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3687 server->vals->read_rsp_size;
3688 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3689
3690 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3691 if (!pages) {
3692 rc = -ENOMEM;
3693 goto discard_data;
3694 }
3695
3696 for (; i < npages; i++) {
3697 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3698 if (!pages[i]) {
3699 rc = -ENOMEM;
3700 goto discard_data;
3701 }
3702 }
3703
3704 /* read read data into pages */
3705 rc = read_data_into_pages(server, pages, npages, len);
3706 if (rc)
3707 goto free_pages;
3708
3709 rc = cifs_discard_remaining_data(server);
3710 if (rc)
3711 goto free_pages;
3712
3713 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3714 pages, npages, len);
3715 if (rc)
3716 goto free_pages;
3717
3718 *mid = smb2_find_mid(server, buf);
3719 if (*mid == NULL)
3720 cifs_dbg(FYI, "mid not found\n");
3721 else {
3722 cifs_dbg(FYI, "mid found\n");
3723 (*mid)->decrypted = true;
3724 rc = handle_read_data(server, *mid, buf,
3725 server->vals->read_rsp_size,
3726 pages, npages, len);
3727 }
3728
3729 free_pages:
3730 for (i = i - 1; i >= 0; i--)
3731 put_page(pages[i]);
3732 kfree(pages);
3733 return rc;
3734 discard_data:
3735 cifs_discard_remaining_data(server);
3736 goto free_pages;
3737 }
3738
3739 static int
3740 receive_encrypted_standard(struct TCP_Server_Info *server,
3741 struct mid_q_entry **mids, char **bufs,
3742 int *num_mids)
3743 {
3744 int ret, length;
3745 char *buf = server->smallbuf;
3746 char *tmpbuf;
3747 struct smb2_sync_hdr *shdr;
3748 unsigned int pdu_length = server->pdu_size;
3749 unsigned int buf_size;
3750 struct mid_q_entry *mid_entry;
3751 int next_is_large;
3752 char *next_buffer = NULL;
3753
3754 *num_mids = 0;
3755
3756 /* switch to large buffer if too big for a small one */
3757 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3758 server->large_buf = true;
3759 memcpy(server->bigbuf, buf, server->total_read);
3760 buf = server->bigbuf;
3761 }
3762
3763 /* now read the rest */
3764 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3765 pdu_length - HEADER_SIZE(server) + 1);
3766 if (length < 0)
3767 return length;
3768 server->total_read += length;
3769
3770 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3771 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3772 if (length)
3773 return length;
3774
3775 next_is_large = server->large_buf;
3776 one_more:
3777 shdr = (struct smb2_sync_hdr *)buf;
3778 if (shdr->NextCommand) {
3779 if (next_is_large) {
3780 tmpbuf = server->bigbuf;
3781 next_buffer = (char *)cifs_buf_get();
3782 } else {
3783 tmpbuf = server->smallbuf;
3784 next_buffer = (char *)cifs_small_buf_get();
3785 }
3786 memcpy(next_buffer,
3787 tmpbuf + le32_to_cpu(shdr->NextCommand),
3788 pdu_length - le32_to_cpu(shdr->NextCommand));
3789 }
3790
3791 mid_entry = smb2_find_mid(server, buf);
3792 if (mid_entry == NULL)
3793 cifs_dbg(FYI, "mid not found\n");
3794 else {
3795 cifs_dbg(FYI, "mid found\n");
3796 mid_entry->decrypted = true;
3797 mid_entry->resp_buf_size = server->pdu_size;
3798 }
3799
3800 if (*num_mids >= MAX_COMPOUND) {
3801 cifs_dbg(VFS, "too many PDUs in compound\n");
3802 return -1;
3803 }
3804 bufs[*num_mids] = buf;
3805 mids[(*num_mids)++] = mid_entry;
3806
3807 if (mid_entry && mid_entry->handle)
3808 ret = mid_entry->handle(server, mid_entry);
3809 else
3810 ret = cifs_handle_standard(server, mid_entry);
3811
3812 if (ret == 0 && shdr->NextCommand) {
3813 pdu_length -= le32_to_cpu(shdr->NextCommand);
3814 server->large_buf = next_is_large;
3815 if (next_is_large)
3816 server->bigbuf = next_buffer;
3817 else
3818 server->smallbuf = next_buffer;
3819
3820 buf += le32_to_cpu(shdr->NextCommand);
3821 goto one_more;
3822 }
3823
3824 return ret;
3825 }
3826
3827 static int
3828 smb3_receive_transform(struct TCP_Server_Info *server,
3829 struct mid_q_entry **mids, char **bufs, int *num_mids)
3830 {
3831 char *buf = server->smallbuf;
3832 unsigned int pdu_length = server->pdu_size;
3833 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3834 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3835
3836 if (pdu_length < sizeof(struct smb2_transform_hdr) +
3837 sizeof(struct smb2_sync_hdr)) {
3838 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3839 pdu_length);
3840 cifs_reconnect(server);
3841 wake_up(&server->response_q);
3842 return -ECONNABORTED;
3843 }
3844
3845 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3846 cifs_dbg(VFS, "Transform message is broken\n");
3847 cifs_reconnect(server);
3848 wake_up(&server->response_q);
3849 return -ECONNABORTED;
3850 }
3851
3852 /* TODO: add support for compounds containing READ. */
3853 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3854 *num_mids = 1;
3855 return receive_encrypted_read(server, &mids[0]);
3856 }
3857
3858 return receive_encrypted_standard(server, mids, bufs, num_mids);
3859 }
3860
3861 int
3862 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3863 {
3864 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3865
3866 return handle_read_data(server, mid, buf, server->pdu_size,
3867 NULL, 0, 0);
3868 }
3869
3870 static int
3871 smb2_next_header(char *buf)
3872 {
3873 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3874 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3875
3876 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3877 return sizeof(struct smb2_transform_hdr) +
3878 le32_to_cpu(t_hdr->OriginalMessageSize);
3879
3880 return le32_to_cpu(hdr->NextCommand);
3881 }
3882
3883 static int
3884 smb2_make_node(unsigned int xid, struct inode *inode,
3885 struct dentry *dentry, struct cifs_tcon *tcon,
3886 char *full_path, umode_t mode, dev_t dev)
3887 {
3888 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3889 int rc = -EPERM;
3890 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
3891 FILE_ALL_INFO *buf = NULL;
3892 struct cifs_io_parms io_parms;
3893 __u32 oplock = 0;
3894 struct cifs_fid fid;
3895 struct cifs_open_parms oparms;
3896 unsigned int bytes_written;
3897 struct win_dev *pdev;
3898 struct kvec iov[2];
3899
3900 /*
3901 * Check if mounted with mount parm 'sfu' mount parm.
3902 * SFU emulation should work with all servers, but only
3903 * supports block and char device (no socket & fifo),
3904 * and was used by default in earlier versions of Windows
3905 */
3906 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
3907 goto out;
3908
3909 /*
3910 * TODO: Add ability to create instead via reparse point. Windows (e.g.
3911 * their current NFS server) uses this approach to expose special files
3912 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
3913 */
3914
3915 if (!S_ISCHR(mode) && !S_ISBLK(mode))
3916 goto out;
3917
3918 cifs_dbg(FYI, "sfu compat create special file\n");
3919
3920 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
3921 if (buf == NULL) {
3922 rc = -ENOMEM;
3923 goto out;
3924 }
3925
3926 if (backup_cred(cifs_sb))
3927 create_options |= CREATE_OPEN_BACKUP_INTENT;
3928
3929 oparms.tcon = tcon;
3930 oparms.cifs_sb = cifs_sb;
3931 oparms.desired_access = GENERIC_WRITE;
3932 oparms.create_options = create_options;
3933 oparms.disposition = FILE_CREATE;
3934 oparms.path = full_path;
3935 oparms.fid = &fid;
3936 oparms.reconnect = false;
3937
3938 if (tcon->ses->server->oplocks)
3939 oplock = REQ_OPLOCK;
3940 else
3941 oplock = 0;
3942 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
3943 if (rc)
3944 goto out;
3945
3946 /*
3947 * BB Do not bother to decode buf since no local inode yet to put
3948 * timestamps in, but we can reuse it safely.
3949 */
3950
3951 pdev = (struct win_dev *)buf;
3952 io_parms.pid = current->tgid;
3953 io_parms.tcon = tcon;
3954 io_parms.offset = 0;
3955 io_parms.length = sizeof(struct win_dev);
3956 iov[1].iov_base = buf;
3957 iov[1].iov_len = sizeof(struct win_dev);
3958 if (S_ISCHR(mode)) {
3959 memcpy(pdev->type, "IntxCHR", 8);
3960 pdev->major = cpu_to_le64(MAJOR(dev));
3961 pdev->minor = cpu_to_le64(MINOR(dev));
3962 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
3963 &bytes_written, iov, 1);
3964 } else if (S_ISBLK(mode)) {
3965 memcpy(pdev->type, "IntxBLK", 8);
3966 pdev->major = cpu_to_le64(MAJOR(dev));
3967 pdev->minor = cpu_to_le64(MINOR(dev));
3968 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
3969 &bytes_written, iov, 1);
3970 }
3971 tcon->ses->server->ops->close(xid, tcon, &fid);
3972 d_drop(dentry);
3973
3974 /* FIXME: add code here to set EAs */
3975 out:
3976 kfree(buf);
3977 return rc;
3978 }
3979
3980
3981 struct smb_version_operations smb20_operations = {
3982 .compare_fids = smb2_compare_fids,
3983 .setup_request = smb2_setup_request,
3984 .setup_async_request = smb2_setup_async_request,
3985 .check_receive = smb2_check_receive,
3986 .add_credits = smb2_add_credits,
3987 .set_credits = smb2_set_credits,
3988 .get_credits_field = smb2_get_credits_field,
3989 .get_credits = smb2_get_credits,
3990 .wait_mtu_credits = cifs_wait_mtu_credits,
3991 .get_next_mid = smb2_get_next_mid,
3992 .revert_current_mid = smb2_revert_current_mid,
3993 .read_data_offset = smb2_read_data_offset,
3994 .read_data_length = smb2_read_data_length,
3995 .map_error = map_smb2_to_linux_error,
3996 .find_mid = smb2_find_mid,
3997 .check_message = smb2_check_message,
3998 .dump_detail = smb2_dump_detail,
3999 .clear_stats = smb2_clear_stats,
4000 .print_stats = smb2_print_stats,
4001 .is_oplock_break = smb2_is_valid_oplock_break,
4002 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4003 .downgrade_oplock = smb2_downgrade_oplock,
4004 .need_neg = smb2_need_neg,
4005 .negotiate = smb2_negotiate,
4006 .negotiate_wsize = smb2_negotiate_wsize,
4007 .negotiate_rsize = smb2_negotiate_rsize,
4008 .sess_setup = SMB2_sess_setup,
4009 .logoff = SMB2_logoff,
4010 .tree_connect = SMB2_tcon,
4011 .tree_disconnect = SMB2_tdis,
4012 .qfs_tcon = smb2_qfs_tcon,
4013 .is_path_accessible = smb2_is_path_accessible,
4014 .can_echo = smb2_can_echo,
4015 .echo = SMB2_echo,
4016 .query_path_info = smb2_query_path_info,
4017 .get_srv_inum = smb2_get_srv_inum,
4018 .query_file_info = smb2_query_file_info,
4019 .set_path_size = smb2_set_path_size,
4020 .set_file_size = smb2_set_file_size,
4021 .set_file_info = smb2_set_file_info,
4022 .set_compression = smb2_set_compression,
4023 .mkdir = smb2_mkdir,
4024 .mkdir_setinfo = smb2_mkdir_setinfo,
4025 .rmdir = smb2_rmdir,
4026 .unlink = smb2_unlink,
4027 .rename = smb2_rename_path,
4028 .create_hardlink = smb2_create_hardlink,
4029 .query_symlink = smb2_query_symlink,
4030 .query_mf_symlink = smb3_query_mf_symlink,
4031 .create_mf_symlink = smb3_create_mf_symlink,
4032 .open = smb2_open_file,
4033 .set_fid = smb2_set_fid,
4034 .close = smb2_close_file,
4035 .flush = smb2_flush_file,
4036 .async_readv = smb2_async_readv,
4037 .async_writev = smb2_async_writev,
4038 .sync_read = smb2_sync_read,
4039 .sync_write = smb2_sync_write,
4040 .query_dir_first = smb2_query_dir_first,
4041 .query_dir_next = smb2_query_dir_next,
4042 .close_dir = smb2_close_dir,
4043 .calc_smb_size = smb2_calc_size,
4044 .is_status_pending = smb2_is_status_pending,
4045 .is_session_expired = smb2_is_session_expired,
4046 .oplock_response = smb2_oplock_response,
4047 .queryfs = smb2_queryfs,
4048 .mand_lock = smb2_mand_lock,
4049 .mand_unlock_range = smb2_unlock_range,
4050 .push_mand_locks = smb2_push_mandatory_locks,
4051 .get_lease_key = smb2_get_lease_key,
4052 .set_lease_key = smb2_set_lease_key,
4053 .new_lease_key = smb2_new_lease_key,
4054 .calc_signature = smb2_calc_signature,
4055 .is_read_op = smb2_is_read_op,
4056 .set_oplock_level = smb2_set_oplock_level,
4057 .create_lease_buf = smb2_create_lease_buf,
4058 .parse_lease_buf = smb2_parse_lease_buf,
4059 .copychunk_range = smb2_copychunk_range,
4060 .wp_retry_size = smb2_wp_retry_size,
4061 .dir_needs_close = smb2_dir_needs_close,
4062 .get_dfs_refer = smb2_get_dfs_refer,
4063 .select_sectype = smb2_select_sectype,
4064 #ifdef CONFIG_CIFS_XATTR
4065 .query_all_EAs = smb2_query_eas,
4066 .set_EA = smb2_set_ea,
4067 #endif /* CIFS_XATTR */
4068 #ifdef CONFIG_CIFS_ACL
4069 .get_acl = get_smb2_acl,
4070 .get_acl_by_fid = get_smb2_acl_by_fid,
4071 .set_acl = set_smb2_acl,
4072 #endif /* CIFS_ACL */
4073 .next_header = smb2_next_header,
4074 .ioctl_query_info = smb2_ioctl_query_info,
4075 .make_node = smb2_make_node,
4076 };
4077
4078 struct smb_version_operations smb21_operations = {
4079 .compare_fids = smb2_compare_fids,
4080 .setup_request = smb2_setup_request,
4081 .setup_async_request = smb2_setup_async_request,
4082 .check_receive = smb2_check_receive,
4083 .add_credits = smb2_add_credits,
4084 .set_credits = smb2_set_credits,
4085 .get_credits_field = smb2_get_credits_field,
4086 .get_credits = smb2_get_credits,
4087 .wait_mtu_credits = smb2_wait_mtu_credits,
4088 .adjust_credits = smb2_adjust_credits,
4089 .get_next_mid = smb2_get_next_mid,
4090 .revert_current_mid = smb2_revert_current_mid,
4091 .read_data_offset = smb2_read_data_offset,
4092 .read_data_length = smb2_read_data_length,
4093 .map_error = map_smb2_to_linux_error,
4094 .find_mid = smb2_find_mid,
4095 .check_message = smb2_check_message,
4096 .dump_detail = smb2_dump_detail,
4097 .clear_stats = smb2_clear_stats,
4098 .print_stats = smb2_print_stats,
4099 .is_oplock_break = smb2_is_valid_oplock_break,
4100 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4101 .downgrade_oplock = smb21_downgrade_oplock,
4102 .need_neg = smb2_need_neg,
4103 .negotiate = smb2_negotiate,
4104 .negotiate_wsize = smb2_negotiate_wsize,
4105 .negotiate_rsize = smb2_negotiate_rsize,
4106 .sess_setup = SMB2_sess_setup,
4107 .logoff = SMB2_logoff,
4108 .tree_connect = SMB2_tcon,
4109 .tree_disconnect = SMB2_tdis,
4110 .qfs_tcon = smb2_qfs_tcon,
4111 .is_path_accessible = smb2_is_path_accessible,
4112 .can_echo = smb2_can_echo,
4113 .echo = SMB2_echo,
4114 .query_path_info = smb2_query_path_info,
4115 .get_srv_inum = smb2_get_srv_inum,
4116 .query_file_info = smb2_query_file_info,
4117 .set_path_size = smb2_set_path_size,
4118 .set_file_size = smb2_set_file_size,
4119 .set_file_info = smb2_set_file_info,
4120 .set_compression = smb2_set_compression,
4121 .mkdir = smb2_mkdir,
4122 .mkdir_setinfo = smb2_mkdir_setinfo,
4123 .rmdir = smb2_rmdir,
4124 .unlink = smb2_unlink,
4125 .rename = smb2_rename_path,
4126 .create_hardlink = smb2_create_hardlink,
4127 .query_symlink = smb2_query_symlink,
4128 .query_mf_symlink = smb3_query_mf_symlink,
4129 .create_mf_symlink = smb3_create_mf_symlink,
4130 .open = smb2_open_file,
4131 .set_fid = smb2_set_fid,
4132 .close = smb2_close_file,
4133 .flush = smb2_flush_file,
4134 .async_readv = smb2_async_readv,
4135 .async_writev = smb2_async_writev,
4136 .sync_read = smb2_sync_read,
4137 .sync_write = smb2_sync_write,
4138 .query_dir_first = smb2_query_dir_first,
4139 .query_dir_next = smb2_query_dir_next,
4140 .close_dir = smb2_close_dir,
4141 .calc_smb_size = smb2_calc_size,
4142 .is_status_pending = smb2_is_status_pending,
4143 .is_session_expired = smb2_is_session_expired,
4144 .oplock_response = smb2_oplock_response,
4145 .queryfs = smb2_queryfs,
4146 .mand_lock = smb2_mand_lock,
4147 .mand_unlock_range = smb2_unlock_range,
4148 .push_mand_locks = smb2_push_mandatory_locks,
4149 .get_lease_key = smb2_get_lease_key,
4150 .set_lease_key = smb2_set_lease_key,
4151 .new_lease_key = smb2_new_lease_key,
4152 .calc_signature = smb2_calc_signature,
4153 .is_read_op = smb21_is_read_op,
4154 .set_oplock_level = smb21_set_oplock_level,
4155 .create_lease_buf = smb2_create_lease_buf,
4156 .parse_lease_buf = smb2_parse_lease_buf,
4157 .copychunk_range = smb2_copychunk_range,
4158 .wp_retry_size = smb2_wp_retry_size,
4159 .dir_needs_close = smb2_dir_needs_close,
4160 .enum_snapshots = smb3_enum_snapshots,
4161 .get_dfs_refer = smb2_get_dfs_refer,
4162 .select_sectype = smb2_select_sectype,
4163 #ifdef CONFIG_CIFS_XATTR
4164 .query_all_EAs = smb2_query_eas,
4165 .set_EA = smb2_set_ea,
4166 #endif /* CIFS_XATTR */
4167 #ifdef CONFIG_CIFS_ACL
4168 .get_acl = get_smb2_acl,
4169 .get_acl_by_fid = get_smb2_acl_by_fid,
4170 .set_acl = set_smb2_acl,
4171 #endif /* CIFS_ACL */
4172 .next_header = smb2_next_header,
4173 .ioctl_query_info = smb2_ioctl_query_info,
4174 .make_node = smb2_make_node,
4175 };
4176
4177 struct smb_version_operations smb30_operations = {
4178 .compare_fids = smb2_compare_fids,
4179 .setup_request = smb2_setup_request,
4180 .setup_async_request = smb2_setup_async_request,
4181 .check_receive = smb2_check_receive,
4182 .add_credits = smb2_add_credits,
4183 .set_credits = smb2_set_credits,
4184 .get_credits_field = smb2_get_credits_field,
4185 .get_credits = smb2_get_credits,
4186 .wait_mtu_credits = smb2_wait_mtu_credits,
4187 .adjust_credits = smb2_adjust_credits,
4188 .get_next_mid = smb2_get_next_mid,
4189 .revert_current_mid = smb2_revert_current_mid,
4190 .read_data_offset = smb2_read_data_offset,
4191 .read_data_length = smb2_read_data_length,
4192 .map_error = map_smb2_to_linux_error,
4193 .find_mid = smb2_find_mid,
4194 .check_message = smb2_check_message,
4195 .dump_detail = smb2_dump_detail,
4196 .clear_stats = smb2_clear_stats,
4197 .print_stats = smb2_print_stats,
4198 .dump_share_caps = smb2_dump_share_caps,
4199 .is_oplock_break = smb2_is_valid_oplock_break,
4200 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4201 .downgrade_oplock = smb21_downgrade_oplock,
4202 .need_neg = smb2_need_neg,
4203 .negotiate = smb2_negotiate,
4204 .negotiate_wsize = smb3_negotiate_wsize,
4205 .negotiate_rsize = smb3_negotiate_rsize,
4206 .sess_setup = SMB2_sess_setup,
4207 .logoff = SMB2_logoff,
4208 .tree_connect = SMB2_tcon,
4209 .tree_disconnect = SMB2_tdis,
4210 .qfs_tcon = smb3_qfs_tcon,
4211 .is_path_accessible = smb2_is_path_accessible,
4212 .can_echo = smb2_can_echo,
4213 .echo = SMB2_echo,
4214 .query_path_info = smb2_query_path_info,
4215 .get_srv_inum = smb2_get_srv_inum,
4216 .query_file_info = smb2_query_file_info,
4217 .set_path_size = smb2_set_path_size,
4218 .set_file_size = smb2_set_file_size,
4219 .set_file_info = smb2_set_file_info,
4220 .set_compression = smb2_set_compression,
4221 .mkdir = smb2_mkdir,
4222 .mkdir_setinfo = smb2_mkdir_setinfo,
4223 .rmdir = smb2_rmdir,
4224 .unlink = smb2_unlink,
4225 .rename = smb2_rename_path,
4226 .create_hardlink = smb2_create_hardlink,
4227 .query_symlink = smb2_query_symlink,
4228 .query_mf_symlink = smb3_query_mf_symlink,
4229 .create_mf_symlink = smb3_create_mf_symlink,
4230 .open = smb2_open_file,
4231 .set_fid = smb2_set_fid,
4232 .close = smb2_close_file,
4233 .flush = smb2_flush_file,
4234 .async_readv = smb2_async_readv,
4235 .async_writev = smb2_async_writev,
4236 .sync_read = smb2_sync_read,
4237 .sync_write = smb2_sync_write,
4238 .query_dir_first = smb2_query_dir_first,
4239 .query_dir_next = smb2_query_dir_next,
4240 .close_dir = smb2_close_dir,
4241 .calc_smb_size = smb2_calc_size,
4242 .is_status_pending = smb2_is_status_pending,
4243 .is_session_expired = smb2_is_session_expired,
4244 .oplock_response = smb2_oplock_response,
4245 .queryfs = smb2_queryfs,
4246 .mand_lock = smb2_mand_lock,
4247 .mand_unlock_range = smb2_unlock_range,
4248 .push_mand_locks = smb2_push_mandatory_locks,
4249 .get_lease_key = smb2_get_lease_key,
4250 .set_lease_key = smb2_set_lease_key,
4251 .new_lease_key = smb2_new_lease_key,
4252 .generate_signingkey = generate_smb30signingkey,
4253 .calc_signature = smb3_calc_signature,
4254 .set_integrity = smb3_set_integrity,
4255 .is_read_op = smb21_is_read_op,
4256 .set_oplock_level = smb3_set_oplock_level,
4257 .create_lease_buf = smb3_create_lease_buf,
4258 .parse_lease_buf = smb3_parse_lease_buf,
4259 .copychunk_range = smb2_copychunk_range,
4260 .duplicate_extents = smb2_duplicate_extents,
4261 .validate_negotiate = smb3_validate_negotiate,
4262 .wp_retry_size = smb2_wp_retry_size,
4263 .dir_needs_close = smb2_dir_needs_close,
4264 .fallocate = smb3_fallocate,
4265 .enum_snapshots = smb3_enum_snapshots,
4266 .init_transform_rq = smb3_init_transform_rq,
4267 .is_transform_hdr = smb3_is_transform_hdr,
4268 .receive_transform = smb3_receive_transform,
4269 .get_dfs_refer = smb2_get_dfs_refer,
4270 .select_sectype = smb2_select_sectype,
4271 #ifdef CONFIG_CIFS_XATTR
4272 .query_all_EAs = smb2_query_eas,
4273 .set_EA = smb2_set_ea,
4274 #endif /* CIFS_XATTR */
4275 #ifdef CONFIG_CIFS_ACL
4276 .get_acl = get_smb2_acl,
4277 .get_acl_by_fid = get_smb2_acl_by_fid,
4278 .set_acl = set_smb2_acl,
4279 #endif /* CIFS_ACL */
4280 .next_header = smb2_next_header,
4281 .ioctl_query_info = smb2_ioctl_query_info,
4282 .make_node = smb2_make_node,
4283 };
4284
4285 struct smb_version_operations smb311_operations = {
4286 .compare_fids = smb2_compare_fids,
4287 .setup_request = smb2_setup_request,
4288 .setup_async_request = smb2_setup_async_request,
4289 .check_receive = smb2_check_receive,
4290 .add_credits = smb2_add_credits,
4291 .set_credits = smb2_set_credits,
4292 .get_credits_field = smb2_get_credits_field,
4293 .get_credits = smb2_get_credits,
4294 .wait_mtu_credits = smb2_wait_mtu_credits,
4295 .adjust_credits = smb2_adjust_credits,
4296 .get_next_mid = smb2_get_next_mid,
4297 .revert_current_mid = smb2_revert_current_mid,
4298 .read_data_offset = smb2_read_data_offset,
4299 .read_data_length = smb2_read_data_length,
4300 .map_error = map_smb2_to_linux_error,
4301 .find_mid = smb2_find_mid,
4302 .check_message = smb2_check_message,
4303 .dump_detail = smb2_dump_detail,
4304 .clear_stats = smb2_clear_stats,
4305 .print_stats = smb2_print_stats,
4306 .dump_share_caps = smb2_dump_share_caps,
4307 .is_oplock_break = smb2_is_valid_oplock_break,
4308 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4309 .downgrade_oplock = smb21_downgrade_oplock,
4310 .need_neg = smb2_need_neg,
4311 .negotiate = smb2_negotiate,
4312 .negotiate_wsize = smb3_negotiate_wsize,
4313 .negotiate_rsize = smb3_negotiate_rsize,
4314 .sess_setup = SMB2_sess_setup,
4315 .logoff = SMB2_logoff,
4316 .tree_connect = SMB2_tcon,
4317 .tree_disconnect = SMB2_tdis,
4318 .qfs_tcon = smb3_qfs_tcon,
4319 .is_path_accessible = smb2_is_path_accessible,
4320 .can_echo = smb2_can_echo,
4321 .echo = SMB2_echo,
4322 .query_path_info = smb2_query_path_info,
4323 .get_srv_inum = smb2_get_srv_inum,
4324 .query_file_info = smb2_query_file_info,
4325 .set_path_size = smb2_set_path_size,
4326 .set_file_size = smb2_set_file_size,
4327 .set_file_info = smb2_set_file_info,
4328 .set_compression = smb2_set_compression,
4329 .mkdir = smb2_mkdir,
4330 .mkdir_setinfo = smb2_mkdir_setinfo,
4331 .posix_mkdir = smb311_posix_mkdir,
4332 .rmdir = smb2_rmdir,
4333 .unlink = smb2_unlink,
4334 .rename = smb2_rename_path,
4335 .create_hardlink = smb2_create_hardlink,
4336 .query_symlink = smb2_query_symlink,
4337 .query_mf_symlink = smb3_query_mf_symlink,
4338 .create_mf_symlink = smb3_create_mf_symlink,
4339 .open = smb2_open_file,
4340 .set_fid = smb2_set_fid,
4341 .close = smb2_close_file,
4342 .flush = smb2_flush_file,
4343 .async_readv = smb2_async_readv,
4344 .async_writev = smb2_async_writev,
4345 .sync_read = smb2_sync_read,
4346 .sync_write = smb2_sync_write,
4347 .query_dir_first = smb2_query_dir_first,
4348 .query_dir_next = smb2_query_dir_next,
4349 .close_dir = smb2_close_dir,
4350 .calc_smb_size = smb2_calc_size,
4351 .is_status_pending = smb2_is_status_pending,
4352 .is_session_expired = smb2_is_session_expired,
4353 .oplock_response = smb2_oplock_response,
4354 .queryfs = smb311_queryfs,
4355 .mand_lock = smb2_mand_lock,
4356 .mand_unlock_range = smb2_unlock_range,
4357 .push_mand_locks = smb2_push_mandatory_locks,
4358 .get_lease_key = smb2_get_lease_key,
4359 .set_lease_key = smb2_set_lease_key,
4360 .new_lease_key = smb2_new_lease_key,
4361 .generate_signingkey = generate_smb311signingkey,
4362 .calc_signature = smb3_calc_signature,
4363 .set_integrity = smb3_set_integrity,
4364 .is_read_op = smb21_is_read_op,
4365 .set_oplock_level = smb3_set_oplock_level,
4366 .create_lease_buf = smb3_create_lease_buf,
4367 .parse_lease_buf = smb3_parse_lease_buf,
4368 .copychunk_range = smb2_copychunk_range,
4369 .duplicate_extents = smb2_duplicate_extents,
4370 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4371 .wp_retry_size = smb2_wp_retry_size,
4372 .dir_needs_close = smb2_dir_needs_close,
4373 .fallocate = smb3_fallocate,
4374 .enum_snapshots = smb3_enum_snapshots,
4375 .init_transform_rq = smb3_init_transform_rq,
4376 .is_transform_hdr = smb3_is_transform_hdr,
4377 .receive_transform = smb3_receive_transform,
4378 .get_dfs_refer = smb2_get_dfs_refer,
4379 .select_sectype = smb2_select_sectype,
4380 #ifdef CONFIG_CIFS_XATTR
4381 .query_all_EAs = smb2_query_eas,
4382 .set_EA = smb2_set_ea,
4383 #endif /* CIFS_XATTR */
4384 #ifdef CONFIG_CIFS_ACL
4385 .get_acl = get_smb2_acl,
4386 .get_acl_by_fid = get_smb2_acl_by_fid,
4387 .set_acl = set_smb2_acl,
4388 #endif /* CIFS_ACL */
4389 .next_header = smb2_next_header,
4390 .ioctl_query_info = smb2_ioctl_query_info,
4391 .make_node = smb2_make_node,
4392 };
4393
4394 struct smb_version_values smb20_values = {
4395 .version_string = SMB20_VERSION_STRING,
4396 .protocol_id = SMB20_PROT_ID,
4397 .req_capabilities = 0, /* MBZ */
4398 .large_lock_type = 0,
4399 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4400 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4401 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4402 .header_size = sizeof(struct smb2_sync_hdr),
4403 .header_preamble_size = 0,
4404 .max_header_size = MAX_SMB2_HDR_SIZE,
4405 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4406 .lock_cmd = SMB2_LOCK,
4407 .cap_unix = 0,
4408 .cap_nt_find = SMB2_NT_FIND,
4409 .cap_large_files = SMB2_LARGE_FILES,
4410 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4411 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4412 .create_lease_size = sizeof(struct create_lease),
4413 };
4414
4415 struct smb_version_values smb21_values = {
4416 .version_string = SMB21_VERSION_STRING,
4417 .protocol_id = SMB21_PROT_ID,
4418 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4419 .large_lock_type = 0,
4420 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4421 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4422 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4423 .header_size = sizeof(struct smb2_sync_hdr),
4424 .header_preamble_size = 0,
4425 .max_header_size = MAX_SMB2_HDR_SIZE,
4426 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4427 .lock_cmd = SMB2_LOCK,
4428 .cap_unix = 0,
4429 .cap_nt_find = SMB2_NT_FIND,
4430 .cap_large_files = SMB2_LARGE_FILES,
4431 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4432 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4433 .create_lease_size = sizeof(struct create_lease),
4434 };
4435
4436 struct smb_version_values smb3any_values = {
4437 .version_string = SMB3ANY_VERSION_STRING,
4438 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4439 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4440 .large_lock_type = 0,
4441 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4442 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4443 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4444 .header_size = sizeof(struct smb2_sync_hdr),
4445 .header_preamble_size = 0,
4446 .max_header_size = MAX_SMB2_HDR_SIZE,
4447 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4448 .lock_cmd = SMB2_LOCK,
4449 .cap_unix = 0,
4450 .cap_nt_find = SMB2_NT_FIND,
4451 .cap_large_files = SMB2_LARGE_FILES,
4452 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4453 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4454 .create_lease_size = sizeof(struct create_lease_v2),
4455 };
4456
4457 struct smb_version_values smbdefault_values = {
4458 .version_string = SMBDEFAULT_VERSION_STRING,
4459 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4460 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4461 .large_lock_type = 0,
4462 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4463 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4464 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4465 .header_size = sizeof(struct smb2_sync_hdr),
4466 .header_preamble_size = 0,
4467 .max_header_size = MAX_SMB2_HDR_SIZE,
4468 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4469 .lock_cmd = SMB2_LOCK,
4470 .cap_unix = 0,
4471 .cap_nt_find = SMB2_NT_FIND,
4472 .cap_large_files = SMB2_LARGE_FILES,
4473 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4474 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4475 .create_lease_size = sizeof(struct create_lease_v2),
4476 };
4477
4478 struct smb_version_values smb30_values = {
4479 .version_string = SMB30_VERSION_STRING,
4480 .protocol_id = SMB30_PROT_ID,
4481 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4482 .large_lock_type = 0,
4483 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4484 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4485 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4486 .header_size = sizeof(struct smb2_sync_hdr),
4487 .header_preamble_size = 0,
4488 .max_header_size = MAX_SMB2_HDR_SIZE,
4489 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4490 .lock_cmd = SMB2_LOCK,
4491 .cap_unix = 0,
4492 .cap_nt_find = SMB2_NT_FIND,
4493 .cap_large_files = SMB2_LARGE_FILES,
4494 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4495 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4496 .create_lease_size = sizeof(struct create_lease_v2),
4497 };
4498
4499 struct smb_version_values smb302_values = {
4500 .version_string = SMB302_VERSION_STRING,
4501 .protocol_id = SMB302_PROT_ID,
4502 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4503 .large_lock_type = 0,
4504 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4505 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4506 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4507 .header_size = sizeof(struct smb2_sync_hdr),
4508 .header_preamble_size = 0,
4509 .max_header_size = MAX_SMB2_HDR_SIZE,
4510 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4511 .lock_cmd = SMB2_LOCK,
4512 .cap_unix = 0,
4513 .cap_nt_find = SMB2_NT_FIND,
4514 .cap_large_files = SMB2_LARGE_FILES,
4515 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4516 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4517 .create_lease_size = sizeof(struct create_lease_v2),
4518 };
4519
4520 struct smb_version_values smb311_values = {
4521 .version_string = SMB311_VERSION_STRING,
4522 .protocol_id = SMB311_PROT_ID,
4523 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4524 .large_lock_type = 0,
4525 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4526 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4527 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4528 .header_size = sizeof(struct smb2_sync_hdr),
4529 .header_preamble_size = 0,
4530 .max_header_size = MAX_SMB2_HDR_SIZE,
4531 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4532 .lock_cmd = SMB2_LOCK,
4533 .cap_unix = 0,
4534 .cap_nt_find = SMB2_NT_FIND,
4535 .cap_large_files = SMB2_LARGE_FILES,
4536 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4537 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4538 .create_lease_size = sizeof(struct create_lease_v2),
4539 };