]> git.ipfire.org Git - thirdparty/linux.git/blob - fs/smb/client/smb2ops.c
cifs: Pass unbyteswapped eof value into SMB2_set_eof()
[thirdparty/linux.git] / fs / smb / client / smb2ops.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SMB2 version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 */
7
8 #include <linux/pagemap.h>
9 #include <linux/vfs.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <linux/sort.h>
14 #include <crypto/aead.h>
15 #include <linux/fiemap.h>
16 #include <uapi/linux/magic.h>
17 #include "cifsfs.h"
18 #include "cifsglob.h"
19 #include "smb2pdu.h"
20 #include "smb2proto.h"
21 #include "cifsproto.h"
22 #include "cifs_debug.h"
23 #include "cifs_unicode.h"
24 #include "smb2status.h"
25 #include "smb2glob.h"
26 #include "cifs_ioctl.h"
27 #include "smbdirect.h"
28 #include "fscache.h"
29 #include "fs_context.h"
30 #include "cached_dir.h"
31
32 /* Change credits for different ops and return the total number of credits */
33 static int
34 change_conf(struct TCP_Server_Info *server)
35 {
36 server->credits += server->echo_credits + server->oplock_credits;
37 if (server->credits > server->max_credits)
38 server->credits = server->max_credits;
39 server->oplock_credits = server->echo_credits = 0;
40 switch (server->credits) {
41 case 0:
42 return 0;
43 case 1:
44 server->echoes = false;
45 server->oplocks = false;
46 break;
47 case 2:
48 server->echoes = true;
49 server->oplocks = false;
50 server->echo_credits = 1;
51 break;
52 default:
53 server->echoes = true;
54 if (enable_oplocks) {
55 server->oplocks = true;
56 server->oplock_credits = 1;
57 } else
58 server->oplocks = false;
59
60 server->echo_credits = 1;
61 }
62 server->credits -= server->echo_credits + server->oplock_credits;
63 return server->credits + server->echo_credits + server->oplock_credits;
64 }
65
66 static void
67 smb2_add_credits(struct TCP_Server_Info *server,
68 const struct cifs_credits *credits, const int optype)
69 {
70 int *val, rc = -1;
71 int scredits, in_flight;
72 unsigned int add = credits->value;
73 unsigned int instance = credits->instance;
74 bool reconnect_detected = false;
75 bool reconnect_with_invalid_credits = false;
76
77 spin_lock(&server->req_lock);
78 val = server->ops->get_credits_field(server, optype);
79
80 /* eg found case where write overlapping reconnect messed up credits */
81 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
82 reconnect_with_invalid_credits = true;
83
84 if ((instance == 0) || (instance == server->reconnect_instance))
85 *val += add;
86 else
87 reconnect_detected = true;
88
89 if (*val > 65000) {
90 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
91 pr_warn_once("server overflowed SMB3 credits\n");
92 trace_smb3_overflow_credits(server->CurrentMid,
93 server->conn_id, server->hostname, *val,
94 add, server->in_flight);
95 }
96 WARN_ON_ONCE(server->in_flight == 0);
97 server->in_flight--;
98 if (server->in_flight == 0 &&
99 ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
100 ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
101 rc = change_conf(server);
102 /*
103 * Sometimes server returns 0 credits on oplock break ack - we need to
104 * rebalance credits in this case.
105 */
106 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
107 server->oplocks) {
108 if (server->credits > 1) {
109 server->credits--;
110 server->oplock_credits++;
111 }
112 } else if ((server->in_flight > 0) && (server->oplock_credits > 3) &&
113 ((optype & CIFS_OP_MASK) == CIFS_OBREAK_OP))
114 /* if now have too many oplock credits, rebalance so don't starve normal ops */
115 change_conf(server);
116
117 scredits = *val;
118 in_flight = server->in_flight;
119 spin_unlock(&server->req_lock);
120 wake_up(&server->request_q);
121
122 if (reconnect_detected) {
123 trace_smb3_reconnect_detected(server->CurrentMid,
124 server->conn_id, server->hostname, scredits, add, in_flight);
125
126 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
127 add, instance);
128 }
129
130 if (reconnect_with_invalid_credits) {
131 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
132 server->conn_id, server->hostname, scredits, add, in_flight);
133 cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
134 optype, scredits, add);
135 }
136
137 spin_lock(&server->srv_lock);
138 if (server->tcpStatus == CifsNeedReconnect
139 || server->tcpStatus == CifsExiting) {
140 spin_unlock(&server->srv_lock);
141 return;
142 }
143 spin_unlock(&server->srv_lock);
144
145 switch (rc) {
146 case -1:
147 /* change_conf hasn't been executed */
148 break;
149 case 0:
150 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
151 break;
152 case 1:
153 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
154 break;
155 case 2:
156 cifs_dbg(FYI, "disabling oplocks\n");
157 break;
158 default:
159 /* change_conf rebalanced credits for different types */
160 break;
161 }
162
163 trace_smb3_add_credits(server->CurrentMid,
164 server->conn_id, server->hostname, scredits, add, in_flight);
165 cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
166 }
167
168 static void
169 smb2_set_credits(struct TCP_Server_Info *server, const int val)
170 {
171 int scredits, in_flight;
172
173 spin_lock(&server->req_lock);
174 server->credits = val;
175 if (val == 1) {
176 server->reconnect_instance++;
177 /*
178 * ChannelSequence updated for all channels in primary channel so that consistent
179 * across SMB3 requests sent on any channel. See MS-SMB2 3.2.4.1 and 3.2.7.1
180 */
181 if (SERVER_IS_CHAN(server))
182 server->primary_server->channel_sequence_num++;
183 else
184 server->channel_sequence_num++;
185 }
186 scredits = server->credits;
187 in_flight = server->in_flight;
188 spin_unlock(&server->req_lock);
189
190 trace_smb3_set_credits(server->CurrentMid,
191 server->conn_id, server->hostname, scredits, val, in_flight);
192 cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
193
194 /* don't log while holding the lock */
195 if (val == 1)
196 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
197 }
198
199 static int *
200 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
201 {
202 switch (optype) {
203 case CIFS_ECHO_OP:
204 return &server->echo_credits;
205 case CIFS_OBREAK_OP:
206 return &server->oplock_credits;
207 default:
208 return &server->credits;
209 }
210 }
211
212 static unsigned int
213 smb2_get_credits(struct mid_q_entry *mid)
214 {
215 return mid->credits_received;
216 }
217
218 static int
219 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
220 unsigned int *num, struct cifs_credits *credits)
221 {
222 int rc = 0;
223 unsigned int scredits, in_flight;
224
225 spin_lock(&server->req_lock);
226 while (1) {
227 spin_unlock(&server->req_lock);
228
229 spin_lock(&server->srv_lock);
230 if (server->tcpStatus == CifsExiting) {
231 spin_unlock(&server->srv_lock);
232 return -ENOENT;
233 }
234 spin_unlock(&server->srv_lock);
235
236 spin_lock(&server->req_lock);
237 if (server->credits <= 0) {
238 spin_unlock(&server->req_lock);
239 cifs_num_waiters_inc(server);
240 rc = wait_event_killable(server->request_q,
241 has_credits(server, &server->credits, 1));
242 cifs_num_waiters_dec(server);
243 if (rc)
244 return rc;
245 spin_lock(&server->req_lock);
246 } else {
247 scredits = server->credits;
248 /* can deadlock with reopen */
249 if (scredits <= 8) {
250 *num = SMB2_MAX_BUFFER_SIZE;
251 credits->value = 0;
252 credits->instance = 0;
253 break;
254 }
255
256 /* leave some credits for reopen and other ops */
257 scredits -= 8;
258 *num = min_t(unsigned int, size,
259 scredits * SMB2_MAX_BUFFER_SIZE);
260
261 credits->value =
262 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
263 credits->instance = server->reconnect_instance;
264 server->credits -= credits->value;
265 server->in_flight++;
266 if (server->in_flight > server->max_in_flight)
267 server->max_in_flight = server->in_flight;
268 break;
269 }
270 }
271 scredits = server->credits;
272 in_flight = server->in_flight;
273 spin_unlock(&server->req_lock);
274
275 trace_smb3_wait_credits(server->CurrentMid,
276 server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
277 cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
278 __func__, credits->value, scredits);
279
280 return rc;
281 }
282
283 static int
284 smb2_adjust_credits(struct TCP_Server_Info *server,
285 struct cifs_credits *credits,
286 const unsigned int payload_size)
287 {
288 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
289 int scredits, in_flight;
290
291 if (!credits->value || credits->value == new_val)
292 return 0;
293
294 if (credits->value < new_val) {
295 trace_smb3_too_many_credits(server->CurrentMid,
296 server->conn_id, server->hostname, 0, credits->value - new_val, 0);
297 cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
298 credits->value, new_val);
299
300 return -EOPNOTSUPP;
301 }
302
303 spin_lock(&server->req_lock);
304
305 if (server->reconnect_instance != credits->instance) {
306 scredits = server->credits;
307 in_flight = server->in_flight;
308 spin_unlock(&server->req_lock);
309
310 trace_smb3_reconnect_detected(server->CurrentMid,
311 server->conn_id, server->hostname, scredits,
312 credits->value - new_val, in_flight);
313 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
314 credits->value - new_val);
315 return -EAGAIN;
316 }
317
318 server->credits += credits->value - new_val;
319 scredits = server->credits;
320 in_flight = server->in_flight;
321 spin_unlock(&server->req_lock);
322 wake_up(&server->request_q);
323
324 trace_smb3_adj_credits(server->CurrentMid,
325 server->conn_id, server->hostname, scredits,
326 credits->value - new_val, in_flight);
327 cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
328 __func__, credits->value - new_val, scredits);
329
330 credits->value = new_val;
331
332 return 0;
333 }
334
335 static __u64
336 smb2_get_next_mid(struct TCP_Server_Info *server)
337 {
338 __u64 mid;
339 /* for SMB2 we need the current value */
340 spin_lock(&server->mid_lock);
341 mid = server->CurrentMid++;
342 spin_unlock(&server->mid_lock);
343 return mid;
344 }
345
346 static void
347 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
348 {
349 spin_lock(&server->mid_lock);
350 if (server->CurrentMid >= val)
351 server->CurrentMid -= val;
352 spin_unlock(&server->mid_lock);
353 }
354
355 static struct mid_q_entry *
356 __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
357 {
358 struct mid_q_entry *mid;
359 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
360 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
361
362 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
363 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
364 return NULL;
365 }
366
367 spin_lock(&server->mid_lock);
368 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
369 if ((mid->mid == wire_mid) &&
370 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
371 (mid->command == shdr->Command)) {
372 kref_get(&mid->refcount);
373 if (dequeue) {
374 list_del_init(&mid->qhead);
375 mid->mid_flags |= MID_DELETED;
376 }
377 spin_unlock(&server->mid_lock);
378 return mid;
379 }
380 }
381 spin_unlock(&server->mid_lock);
382 return NULL;
383 }
384
385 static struct mid_q_entry *
386 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
387 {
388 return __smb2_find_mid(server, buf, false);
389 }
390
391 static struct mid_q_entry *
392 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
393 {
394 return __smb2_find_mid(server, buf, true);
395 }
396
397 static void
398 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
399 {
400 #ifdef CONFIG_CIFS_DEBUG2
401 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
402
403 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
404 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
405 shdr->Id.SyncId.ProcessId);
406 if (!server->ops->check_message(buf, server->total_read, server)) {
407 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
408 server->ops->calc_smb_size(buf));
409 }
410 #endif
411 }
412
413 static bool
414 smb2_need_neg(struct TCP_Server_Info *server)
415 {
416 return server->max_read == 0;
417 }
418
419 static int
420 smb2_negotiate(const unsigned int xid,
421 struct cifs_ses *ses,
422 struct TCP_Server_Info *server)
423 {
424 int rc;
425
426 spin_lock(&server->mid_lock);
427 server->CurrentMid = 0;
428 spin_unlock(&server->mid_lock);
429 rc = SMB2_negotiate(xid, ses, server);
430 /* BB we probably don't need to retry with modern servers */
431 if (rc == -EAGAIN)
432 rc = -EHOSTDOWN;
433 return rc;
434 }
435
436 static unsigned int
437 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
438 {
439 struct TCP_Server_Info *server = tcon->ses->server;
440 unsigned int wsize;
441
442 /* start with specified wsize, or default */
443 wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
444 wsize = min_t(unsigned int, wsize, server->max_write);
445 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
446 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
447
448 return wsize;
449 }
450
451 static unsigned int
452 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
453 {
454 struct TCP_Server_Info *server = tcon->ses->server;
455 unsigned int wsize;
456
457 /* start with specified wsize, or default */
458 wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
459 wsize = min_t(unsigned int, wsize, server->max_write);
460 #ifdef CONFIG_CIFS_SMB_DIRECT
461 if (server->rdma) {
462 if (server->sign)
463 /*
464 * Account for SMB2 data transfer packet header and
465 * possible encryption header
466 */
467 wsize = min_t(unsigned int,
468 wsize,
469 server->smbd_conn->max_fragmented_send_size -
470 SMB2_READWRITE_PDU_HEADER_SIZE -
471 sizeof(struct smb2_transform_hdr));
472 else
473 wsize = min_t(unsigned int,
474 wsize, server->smbd_conn->max_readwrite_size);
475 }
476 #endif
477 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
478 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
479
480 return wsize;
481 }
482
483 static unsigned int
484 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
485 {
486 struct TCP_Server_Info *server = tcon->ses->server;
487 unsigned int rsize;
488
489 /* start with specified rsize, or default */
490 rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
491 rsize = min_t(unsigned int, rsize, server->max_read);
492
493 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
494 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
495
496 return rsize;
497 }
498
499 static unsigned int
500 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
501 {
502 struct TCP_Server_Info *server = tcon->ses->server;
503 unsigned int rsize;
504
505 /* start with specified rsize, or default */
506 rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
507 rsize = min_t(unsigned int, rsize, server->max_read);
508 #ifdef CONFIG_CIFS_SMB_DIRECT
509 if (server->rdma) {
510 if (server->sign)
511 /*
512 * Account for SMB2 data transfer packet header and
513 * possible encryption header
514 */
515 rsize = min_t(unsigned int,
516 rsize,
517 server->smbd_conn->max_fragmented_recv_size -
518 SMB2_READWRITE_PDU_HEADER_SIZE -
519 sizeof(struct smb2_transform_hdr));
520 else
521 rsize = min_t(unsigned int,
522 rsize, server->smbd_conn->max_readwrite_size);
523 }
524 #endif
525
526 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
527 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
528
529 return rsize;
530 }
531
532 /*
533 * compare two interfaces a and b
534 * return 0 if everything matches.
535 * return 1 if a is rdma capable, or rss capable, or has higher link speed
536 * return -1 otherwise.
537 */
538 static int
539 iface_cmp(struct cifs_server_iface *a, struct cifs_server_iface *b)
540 {
541 int cmp_ret = 0;
542
543 WARN_ON(!a || !b);
544 if (a->rdma_capable == b->rdma_capable) {
545 if (a->rss_capable == b->rss_capable) {
546 if (a->speed == b->speed) {
547 cmp_ret = cifs_ipaddr_cmp((struct sockaddr *) &a->sockaddr,
548 (struct sockaddr *) &b->sockaddr);
549 if (!cmp_ret)
550 return 0;
551 else if (cmp_ret > 0)
552 return 1;
553 else
554 return -1;
555 } else if (a->speed > b->speed)
556 return 1;
557 else
558 return -1;
559 } else if (a->rss_capable > b->rss_capable)
560 return 1;
561 else
562 return -1;
563 } else if (a->rdma_capable > b->rdma_capable)
564 return 1;
565 else
566 return -1;
567 }
568
569 static int
570 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
571 size_t buf_len, struct cifs_ses *ses, bool in_mount)
572 {
573 struct network_interface_info_ioctl_rsp *p;
574 struct sockaddr_in *addr4;
575 struct sockaddr_in6 *addr6;
576 struct iface_info_ipv4 *p4;
577 struct iface_info_ipv6 *p6;
578 struct cifs_server_iface *info = NULL, *iface = NULL, *niface = NULL;
579 struct cifs_server_iface tmp_iface;
580 ssize_t bytes_left;
581 size_t next = 0;
582 int nb_iface = 0;
583 int rc = 0, ret = 0;
584
585 bytes_left = buf_len;
586 p = buf;
587
588 spin_lock(&ses->iface_lock);
589 /* do not query too frequently, this time with lock held */
590 if (ses->iface_last_update &&
591 time_before(jiffies, ses->iface_last_update +
592 (SMB_INTERFACE_POLL_INTERVAL * HZ))) {
593 spin_unlock(&ses->iface_lock);
594 return 0;
595 }
596
597 /*
598 * Go through iface_list and mark them as inactive
599 */
600 list_for_each_entry_safe(iface, niface, &ses->iface_list,
601 iface_head)
602 iface->is_active = 0;
603
604 spin_unlock(&ses->iface_lock);
605
606 /*
607 * Samba server e.g. can return an empty interface list in some cases,
608 * which would only be a problem if we were requesting multichannel
609 */
610 if (bytes_left == 0) {
611 /* avoid spamming logs every 10 minutes, so log only in mount */
612 if ((ses->chan_max > 1) && in_mount)
613 cifs_dbg(VFS,
614 "multichannel not available\n"
615 "Empty network interface list returned by server %s\n",
616 ses->server->hostname);
617 rc = -EINVAL;
618 goto out;
619 }
620
621 while (bytes_left >= sizeof(*p)) {
622 memset(&tmp_iface, 0, sizeof(tmp_iface));
623 tmp_iface.speed = le64_to_cpu(p->LinkSpeed);
624 tmp_iface.rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
625 tmp_iface.rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
626
627 switch (p->Family) {
628 /*
629 * The kernel and wire socket structures have the same
630 * layout and use network byte order but make the
631 * conversion explicit in case either one changes.
632 */
633 case INTERNETWORK:
634 addr4 = (struct sockaddr_in *)&tmp_iface.sockaddr;
635 p4 = (struct iface_info_ipv4 *)p->Buffer;
636 addr4->sin_family = AF_INET;
637 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
638
639 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
640 addr4->sin_port = cpu_to_be16(CIFS_PORT);
641
642 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
643 &addr4->sin_addr);
644 break;
645 case INTERNETWORKV6:
646 addr6 = (struct sockaddr_in6 *)&tmp_iface.sockaddr;
647 p6 = (struct iface_info_ipv6 *)p->Buffer;
648 addr6->sin6_family = AF_INET6;
649 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
650
651 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
652 addr6->sin6_flowinfo = 0;
653 addr6->sin6_scope_id = 0;
654 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
655
656 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
657 &addr6->sin6_addr);
658 break;
659 default:
660 cifs_dbg(VFS,
661 "%s: skipping unsupported socket family\n",
662 __func__);
663 goto next_iface;
664 }
665
666 /*
667 * The iface_list is assumed to be sorted by speed.
668 * Check if the new interface exists in that list.
669 * NEVER change iface. it could be in use.
670 * Add a new one instead
671 */
672 spin_lock(&ses->iface_lock);
673 list_for_each_entry_safe(iface, niface, &ses->iface_list,
674 iface_head) {
675 ret = iface_cmp(iface, &tmp_iface);
676 if (!ret) {
677 iface->is_active = 1;
678 spin_unlock(&ses->iface_lock);
679 goto next_iface;
680 } else if (ret < 0) {
681 /* all remaining ifaces are slower */
682 kref_get(&iface->refcount);
683 break;
684 }
685 }
686 spin_unlock(&ses->iface_lock);
687
688 /* no match. insert the entry in the list */
689 info = kmalloc(sizeof(struct cifs_server_iface),
690 GFP_KERNEL);
691 if (!info) {
692 rc = -ENOMEM;
693 goto out;
694 }
695 memcpy(info, &tmp_iface, sizeof(tmp_iface));
696
697 /* add this new entry to the list */
698 kref_init(&info->refcount);
699 info->is_active = 1;
700
701 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, ses->iface_count);
702 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
703 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
704 le32_to_cpu(p->Capability));
705
706 spin_lock(&ses->iface_lock);
707 if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
708 list_add_tail(&info->iface_head, &iface->iface_head);
709 kref_put(&iface->refcount, release_iface);
710 } else
711 list_add_tail(&info->iface_head, &ses->iface_list);
712
713 ses->iface_count++;
714 spin_unlock(&ses->iface_lock);
715 ses->iface_last_update = jiffies;
716 next_iface:
717 nb_iface++;
718 next = le32_to_cpu(p->Next);
719 if (!next) {
720 bytes_left -= sizeof(*p);
721 break;
722 }
723 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
724 bytes_left -= next;
725 }
726
727 if (!nb_iface) {
728 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
729 rc = -EINVAL;
730 goto out;
731 }
732
733 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
734 if ((bytes_left > 8) || p->Next)
735 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
736
737
738 if (!ses->iface_count) {
739 rc = -EINVAL;
740 goto out;
741 }
742
743 out:
744 /*
745 * Go through the list again and put the inactive entries
746 */
747 spin_lock(&ses->iface_lock);
748 list_for_each_entry_safe(iface, niface, &ses->iface_list,
749 iface_head) {
750 if (!iface->is_active) {
751 list_del(&iface->iface_head);
752 kref_put(&iface->refcount, release_iface);
753 ses->iface_count--;
754 }
755 }
756 spin_unlock(&ses->iface_lock);
757
758 return rc;
759 }
760
761 int
762 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon, bool in_mount)
763 {
764 int rc;
765 unsigned int ret_data_len = 0;
766 struct network_interface_info_ioctl_rsp *out_buf = NULL;
767 struct cifs_ses *ses = tcon->ses;
768 struct TCP_Server_Info *pserver;
769
770 /* do not query too frequently */
771 if (ses->iface_last_update &&
772 time_before(jiffies, ses->iface_last_update +
773 (SMB_INTERFACE_POLL_INTERVAL * HZ)))
774 return 0;
775
776 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
777 FSCTL_QUERY_NETWORK_INTERFACE_INFO,
778 NULL /* no data input */, 0 /* no data input */,
779 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
780 if (rc == -EOPNOTSUPP) {
781 cifs_dbg(FYI,
782 "server does not support query network interfaces\n");
783 ret_data_len = 0;
784 } else if (rc != 0) {
785 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
786 goto out;
787 }
788
789 rc = parse_server_interfaces(out_buf, ret_data_len, ses, in_mount);
790 if (rc)
791 goto out;
792
793 /* check if iface is still active */
794 spin_lock(&ses->chan_lock);
795 pserver = ses->chans[0].server;
796 if (pserver && !cifs_chan_is_iface_active(ses, pserver)) {
797 spin_unlock(&ses->chan_lock);
798 cifs_chan_update_iface(ses, pserver);
799 spin_lock(&ses->chan_lock);
800 }
801 spin_unlock(&ses->chan_lock);
802
803 out:
804 kfree(out_buf);
805 return rc;
806 }
807
808 static void
809 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
810 struct cifs_sb_info *cifs_sb)
811 {
812 int rc;
813 __le16 srch_path = 0; /* Null - open root of share */
814 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
815 struct cifs_open_parms oparms;
816 struct cifs_fid fid;
817 struct cached_fid *cfid = NULL;
818
819 oparms = (struct cifs_open_parms) {
820 .tcon = tcon,
821 .path = "",
822 .desired_access = FILE_READ_ATTRIBUTES,
823 .disposition = FILE_OPEN,
824 .create_options = cifs_create_options(cifs_sb, 0),
825 .fid = &fid,
826 };
827
828 rc = open_cached_dir(xid, tcon, "", cifs_sb, false, &cfid);
829 if (rc == 0)
830 memcpy(&fid, &cfid->fid, sizeof(struct cifs_fid));
831 else
832 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
833 NULL, NULL);
834 if (rc)
835 return;
836
837 SMB3_request_interfaces(xid, tcon, true /* called during mount */);
838
839 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
840 FS_ATTRIBUTE_INFORMATION);
841 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
842 FS_DEVICE_INFORMATION);
843 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
844 FS_VOLUME_INFORMATION);
845 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
846 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
847 if (cfid == NULL)
848 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
849 else
850 close_cached_dir(cfid);
851 }
852
853 static void
854 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
855 struct cifs_sb_info *cifs_sb)
856 {
857 int rc;
858 __le16 srch_path = 0; /* Null - open root of share */
859 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
860 struct cifs_open_parms oparms;
861 struct cifs_fid fid;
862
863 oparms = (struct cifs_open_parms) {
864 .tcon = tcon,
865 .path = "",
866 .desired_access = FILE_READ_ATTRIBUTES,
867 .disposition = FILE_OPEN,
868 .create_options = cifs_create_options(cifs_sb, 0),
869 .fid = &fid,
870 };
871
872 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
873 NULL, NULL);
874 if (rc)
875 return;
876
877 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
878 FS_ATTRIBUTE_INFORMATION);
879 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
880 FS_DEVICE_INFORMATION);
881 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
882 }
883
884 static int
885 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
886 struct cifs_sb_info *cifs_sb, const char *full_path)
887 {
888 __le16 *utf16_path;
889 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
890 int err_buftype = CIFS_NO_BUFFER;
891 struct cifs_open_parms oparms;
892 struct kvec err_iov = {};
893 struct cifs_fid fid;
894 struct cached_fid *cfid;
895 bool islink;
896 int rc, rc2;
897
898 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid);
899 if (!rc) {
900 if (cfid->has_lease) {
901 close_cached_dir(cfid);
902 return 0;
903 }
904 close_cached_dir(cfid);
905 }
906
907 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
908 if (!utf16_path)
909 return -ENOMEM;
910
911 oparms = (struct cifs_open_parms) {
912 .tcon = tcon,
913 .path = full_path,
914 .desired_access = FILE_READ_ATTRIBUTES,
915 .disposition = FILE_OPEN,
916 .create_options = cifs_create_options(cifs_sb, 0),
917 .fid = &fid,
918 };
919
920 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
921 &err_iov, &err_buftype);
922 if (rc) {
923 struct smb2_hdr *hdr = err_iov.iov_base;
924
925 if (unlikely(!hdr || err_buftype == CIFS_NO_BUFFER))
926 goto out;
927
928 if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
929 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
930 full_path, &islink);
931 if (rc2) {
932 rc = rc2;
933 goto out;
934 }
935 if (islink)
936 rc = -EREMOTE;
937 }
938 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb &&
939 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
940 rc = -EOPNOTSUPP;
941 goto out;
942 }
943
944 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
945
946 out:
947 free_rsp_buf(err_buftype, err_iov.iov_base);
948 kfree(utf16_path);
949 return rc;
950 }
951
952 static int smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
953 struct cifs_sb_info *cifs_sb, const char *full_path,
954 u64 *uniqueid, struct cifs_open_info_data *data)
955 {
956 *uniqueid = le64_to_cpu(data->fi.IndexNumber);
957 return 0;
958 }
959
960 static int smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
961 struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
962 {
963 struct cifs_fid *fid = &cfile->fid;
964
965 if (cfile->symlink_target) {
966 data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
967 if (!data->symlink_target)
968 return -ENOMEM;
969 }
970 return SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid, &data->fi);
971 }
972
973 #ifdef CONFIG_CIFS_XATTR
974 static ssize_t
975 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
976 struct smb2_file_full_ea_info *src, size_t src_size,
977 const unsigned char *ea_name)
978 {
979 int rc = 0;
980 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
981 char *name, *value;
982 size_t buf_size = dst_size;
983 size_t name_len, value_len, user_name_len;
984
985 while (src_size > 0) {
986 name_len = (size_t)src->ea_name_length;
987 value_len = (size_t)le16_to_cpu(src->ea_value_length);
988
989 if (name_len == 0)
990 break;
991
992 if (src_size < 8 + name_len + 1 + value_len) {
993 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
994 rc = -EIO;
995 goto out;
996 }
997
998 name = &src->ea_data[0];
999 value = &src->ea_data[src->ea_name_length + 1];
1000
1001 if (ea_name) {
1002 if (ea_name_len == name_len &&
1003 memcmp(ea_name, name, name_len) == 0) {
1004 rc = value_len;
1005 if (dst_size == 0)
1006 goto out;
1007 if (dst_size < value_len) {
1008 rc = -ERANGE;
1009 goto out;
1010 }
1011 memcpy(dst, value, value_len);
1012 goto out;
1013 }
1014 } else {
1015 /* 'user.' plus a terminating null */
1016 user_name_len = 5 + 1 + name_len;
1017
1018 if (buf_size == 0) {
1019 /* skip copy - calc size only */
1020 rc += user_name_len;
1021 } else if (dst_size >= user_name_len) {
1022 dst_size -= user_name_len;
1023 memcpy(dst, "user.", 5);
1024 dst += 5;
1025 memcpy(dst, src->ea_data, name_len);
1026 dst += name_len;
1027 *dst = 0;
1028 ++dst;
1029 rc += user_name_len;
1030 } else {
1031 /* stop before overrun buffer */
1032 rc = -ERANGE;
1033 break;
1034 }
1035 }
1036
1037 if (!src->next_entry_offset)
1038 break;
1039
1040 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1041 /* stop before overrun buffer */
1042 rc = -ERANGE;
1043 break;
1044 }
1045 src_size -= le32_to_cpu(src->next_entry_offset);
1046 src = (void *)((char *)src +
1047 le32_to_cpu(src->next_entry_offset));
1048 }
1049
1050 /* didn't find the named attribute */
1051 if (ea_name)
1052 rc = -ENODATA;
1053
1054 out:
1055 return (ssize_t)rc;
1056 }
1057
1058 static ssize_t
1059 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1060 const unsigned char *path, const unsigned char *ea_name,
1061 char *ea_data, size_t buf_size,
1062 struct cifs_sb_info *cifs_sb)
1063 {
1064 int rc;
1065 struct kvec rsp_iov = {NULL, 0};
1066 int buftype = CIFS_NO_BUFFER;
1067 struct smb2_query_info_rsp *rsp;
1068 struct smb2_file_full_ea_info *info = NULL;
1069
1070 rc = smb2_query_info_compound(xid, tcon, path,
1071 FILE_READ_EA,
1072 FILE_FULL_EA_INFORMATION,
1073 SMB2_O_INFO_FILE,
1074 CIFSMaxBufSize -
1075 MAX_SMB2_CREATE_RESPONSE_SIZE -
1076 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1077 &rsp_iov, &buftype, cifs_sb);
1078 if (rc) {
1079 /*
1080 * If ea_name is NULL (listxattr) and there are no EAs,
1081 * return 0 as it's not an error. Otherwise, the specified
1082 * ea_name was not found.
1083 */
1084 if (!ea_name && rc == -ENODATA)
1085 rc = 0;
1086 goto qeas_exit;
1087 }
1088
1089 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1090 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1091 le32_to_cpu(rsp->OutputBufferLength),
1092 &rsp_iov,
1093 sizeof(struct smb2_file_full_ea_info));
1094 if (rc)
1095 goto qeas_exit;
1096
1097 info = (struct smb2_file_full_ea_info *)(
1098 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1099 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1100 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1101
1102 qeas_exit:
1103 free_rsp_buf(buftype, rsp_iov.iov_base);
1104 return rc;
1105 }
1106
1107 static int
1108 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1109 const char *path, const char *ea_name, const void *ea_value,
1110 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1111 struct cifs_sb_info *cifs_sb)
1112 {
1113 struct smb2_compound_vars *vars;
1114 struct cifs_ses *ses = tcon->ses;
1115 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1116 struct smb_rqst *rqst;
1117 struct kvec *rsp_iov;
1118 __le16 *utf16_path = NULL;
1119 int ea_name_len = strlen(ea_name);
1120 int flags = CIFS_CP_CREATE_CLOSE_OP;
1121 int len;
1122 int resp_buftype[3];
1123 struct cifs_open_parms oparms;
1124 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1125 struct cifs_fid fid;
1126 unsigned int size[1];
1127 void *data[1];
1128 struct smb2_file_full_ea_info *ea = NULL;
1129 struct smb2_query_info_rsp *rsp;
1130 int rc, used_len = 0;
1131
1132 if (smb3_encryption_required(tcon))
1133 flags |= CIFS_TRANSFORM_REQ;
1134
1135 if (ea_name_len > 255)
1136 return -EINVAL;
1137
1138 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1139 if (!utf16_path)
1140 return -ENOMEM;
1141
1142 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1143 vars = kzalloc(sizeof(*vars), GFP_KERNEL);
1144 if (!vars) {
1145 rc = -ENOMEM;
1146 goto out_free_path;
1147 }
1148 rqst = vars->rqst;
1149 rsp_iov = vars->rsp_iov;
1150
1151 if (ses->server->ops->query_all_EAs) {
1152 if (!ea_value) {
1153 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1154 ea_name, NULL, 0,
1155 cifs_sb);
1156 if (rc == -ENODATA)
1157 goto sea_exit;
1158 } else {
1159 /* If we are adding a attribute we should first check
1160 * if there will be enough space available to store
1161 * the new EA. If not we should not add it since we
1162 * would not be able to even read the EAs back.
1163 */
1164 rc = smb2_query_info_compound(xid, tcon, path,
1165 FILE_READ_EA,
1166 FILE_FULL_EA_INFORMATION,
1167 SMB2_O_INFO_FILE,
1168 CIFSMaxBufSize -
1169 MAX_SMB2_CREATE_RESPONSE_SIZE -
1170 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1171 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1172 if (rc == 0) {
1173 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1174 used_len = le32_to_cpu(rsp->OutputBufferLength);
1175 }
1176 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1177 resp_buftype[1] = CIFS_NO_BUFFER;
1178 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1179 rc = 0;
1180
1181 /* Use a fudge factor of 256 bytes in case we collide
1182 * with a different set_EAs command.
1183 */
1184 if (CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1185 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1186 used_len + ea_name_len + ea_value_len + 1) {
1187 rc = -ENOSPC;
1188 goto sea_exit;
1189 }
1190 }
1191 }
1192
1193 /* Open */
1194 rqst[0].rq_iov = vars->open_iov;
1195 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1196
1197 oparms = (struct cifs_open_parms) {
1198 .tcon = tcon,
1199 .path = path,
1200 .desired_access = FILE_WRITE_EA,
1201 .disposition = FILE_OPEN,
1202 .create_options = cifs_create_options(cifs_sb, 0),
1203 .fid = &fid,
1204 };
1205
1206 rc = SMB2_open_init(tcon, server,
1207 &rqst[0], &oplock, &oparms, utf16_path);
1208 if (rc)
1209 goto sea_exit;
1210 smb2_set_next_command(tcon, &rqst[0]);
1211
1212
1213 /* Set Info */
1214 rqst[1].rq_iov = vars->si_iov;
1215 rqst[1].rq_nvec = 1;
1216
1217 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1218 ea = kzalloc(len, GFP_KERNEL);
1219 if (ea == NULL) {
1220 rc = -ENOMEM;
1221 goto sea_exit;
1222 }
1223
1224 ea->ea_name_length = ea_name_len;
1225 ea->ea_value_length = cpu_to_le16(ea_value_len);
1226 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1227 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1228
1229 size[0] = len;
1230 data[0] = ea;
1231
1232 rc = SMB2_set_info_init(tcon, server,
1233 &rqst[1], COMPOUND_FID,
1234 COMPOUND_FID, current->tgid,
1235 FILE_FULL_EA_INFORMATION,
1236 SMB2_O_INFO_FILE, 0, data, size);
1237 if (rc)
1238 goto sea_exit;
1239 smb2_set_next_command(tcon, &rqst[1]);
1240 smb2_set_related(&rqst[1]);
1241
1242 /* Close */
1243 rqst[2].rq_iov = &vars->close_iov;
1244 rqst[2].rq_nvec = 1;
1245 rc = SMB2_close_init(tcon, server,
1246 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1247 if (rc)
1248 goto sea_exit;
1249 smb2_set_related(&rqst[2]);
1250
1251 rc = compound_send_recv(xid, ses, server,
1252 flags, 3, rqst,
1253 resp_buftype, rsp_iov);
1254 /* no need to bump num_remote_opens because handle immediately closed */
1255
1256 sea_exit:
1257 kfree(ea);
1258 SMB2_open_free(&rqst[0]);
1259 SMB2_set_info_free(&rqst[1]);
1260 SMB2_close_free(&rqst[2]);
1261 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1262 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1263 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1264 kfree(vars);
1265 out_free_path:
1266 kfree(utf16_path);
1267 return rc;
1268 }
1269 #endif
1270
1271 static bool
1272 smb2_can_echo(struct TCP_Server_Info *server)
1273 {
1274 return server->echoes;
1275 }
1276
1277 static void
1278 smb2_clear_stats(struct cifs_tcon *tcon)
1279 {
1280 int i;
1281
1282 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1283 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1284 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1285 }
1286 }
1287
1288 static void
1289 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1290 {
1291 seq_puts(m, "\n\tShare Capabilities:");
1292 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1293 seq_puts(m, " DFS,");
1294 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1295 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1296 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1297 seq_puts(m, " SCALEOUT,");
1298 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1299 seq_puts(m, " CLUSTER,");
1300 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1301 seq_puts(m, " ASYMMETRIC,");
1302 if (tcon->capabilities == 0)
1303 seq_puts(m, " None");
1304 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1305 seq_puts(m, " Aligned,");
1306 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1307 seq_puts(m, " Partition Aligned,");
1308 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1309 seq_puts(m, " SSD,");
1310 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1311 seq_puts(m, " TRIM-support,");
1312
1313 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1314 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1315 if (tcon->perf_sector_size)
1316 seq_printf(m, "\tOptimal sector size: 0x%x",
1317 tcon->perf_sector_size);
1318 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1319 }
1320
1321 static void
1322 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1323 {
1324 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1325 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1326
1327 /*
1328 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1329 * totals (requests sent) since those SMBs are per-session not per tcon
1330 */
1331 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1332 (long long)(tcon->bytes_read),
1333 (long long)(tcon->bytes_written));
1334 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1335 atomic_read(&tcon->num_local_opens),
1336 atomic_read(&tcon->num_remote_opens));
1337 seq_printf(m, "\nTreeConnects: %d total %d failed",
1338 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1339 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1340 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1341 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1342 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1343 seq_printf(m, "\nCreates: %d total %d failed",
1344 atomic_read(&sent[SMB2_CREATE_HE]),
1345 atomic_read(&failed[SMB2_CREATE_HE]));
1346 seq_printf(m, "\nCloses: %d total %d failed",
1347 atomic_read(&sent[SMB2_CLOSE_HE]),
1348 atomic_read(&failed[SMB2_CLOSE_HE]));
1349 seq_printf(m, "\nFlushes: %d total %d failed",
1350 atomic_read(&sent[SMB2_FLUSH_HE]),
1351 atomic_read(&failed[SMB2_FLUSH_HE]));
1352 seq_printf(m, "\nReads: %d total %d failed",
1353 atomic_read(&sent[SMB2_READ_HE]),
1354 atomic_read(&failed[SMB2_READ_HE]));
1355 seq_printf(m, "\nWrites: %d total %d failed",
1356 atomic_read(&sent[SMB2_WRITE_HE]),
1357 atomic_read(&failed[SMB2_WRITE_HE]));
1358 seq_printf(m, "\nLocks: %d total %d failed",
1359 atomic_read(&sent[SMB2_LOCK_HE]),
1360 atomic_read(&failed[SMB2_LOCK_HE]));
1361 seq_printf(m, "\nIOCTLs: %d total %d failed",
1362 atomic_read(&sent[SMB2_IOCTL_HE]),
1363 atomic_read(&failed[SMB2_IOCTL_HE]));
1364 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1365 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1366 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1367 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1368 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1369 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1370 seq_printf(m, "\nQueryInfos: %d total %d failed",
1371 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1372 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1373 seq_printf(m, "\nSetInfos: %d total %d failed",
1374 atomic_read(&sent[SMB2_SET_INFO_HE]),
1375 atomic_read(&failed[SMB2_SET_INFO_HE]));
1376 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1377 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1378 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1379 }
1380
1381 static void
1382 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1383 {
1384 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1385 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1386
1387 cfile->fid.persistent_fid = fid->persistent_fid;
1388 cfile->fid.volatile_fid = fid->volatile_fid;
1389 cfile->fid.access = fid->access;
1390 #ifdef CONFIG_CIFS_DEBUG2
1391 cfile->fid.mid = fid->mid;
1392 #endif /* CIFS_DEBUG2 */
1393 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1394 &fid->purge_cache);
1395 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1396 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1397 }
1398
1399 static void
1400 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1401 struct cifs_fid *fid)
1402 {
1403 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1404 }
1405
1406 static void
1407 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1408 struct cifsFileInfo *cfile)
1409 {
1410 struct smb2_file_network_open_info file_inf;
1411 struct inode *inode;
1412 int rc;
1413
1414 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1415 cfile->fid.volatile_fid, &file_inf);
1416 if (rc)
1417 return;
1418
1419 inode = d_inode(cfile->dentry);
1420
1421 spin_lock(&inode->i_lock);
1422 CIFS_I(inode)->time = jiffies;
1423
1424 /* Creation time should not need to be updated on close */
1425 if (file_inf.LastWriteTime)
1426 inode_set_mtime_to_ts(inode,
1427 cifs_NTtimeToUnix(file_inf.LastWriteTime));
1428 if (file_inf.ChangeTime)
1429 inode_set_ctime_to_ts(inode,
1430 cifs_NTtimeToUnix(file_inf.ChangeTime));
1431 if (file_inf.LastAccessTime)
1432 inode_set_atime_to_ts(inode,
1433 cifs_NTtimeToUnix(file_inf.LastAccessTime));
1434
1435 /*
1436 * i_blocks is not related to (i_size / i_blksize),
1437 * but instead 512 byte (2**9) size is required for
1438 * calculating num blocks.
1439 */
1440 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1441 inode->i_blocks =
1442 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1443
1444 /* End of file and Attributes should not have to be updated on close */
1445 spin_unlock(&inode->i_lock);
1446 }
1447
1448 static int
1449 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1450 u64 persistent_fid, u64 volatile_fid,
1451 struct copychunk_ioctl *pcchunk)
1452 {
1453 int rc;
1454 unsigned int ret_data_len;
1455 struct resume_key_req *res_key;
1456
1457 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1458 FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
1459 CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
1460
1461 if (rc == -EOPNOTSUPP) {
1462 pr_warn_once("Server share %s does not support copy range\n", tcon->tree_name);
1463 goto req_res_key_exit;
1464 } else if (rc) {
1465 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1466 goto req_res_key_exit;
1467 }
1468 if (ret_data_len < sizeof(struct resume_key_req)) {
1469 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1470 rc = -EINVAL;
1471 goto req_res_key_exit;
1472 }
1473 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1474
1475 req_res_key_exit:
1476 kfree(res_key);
1477 return rc;
1478 }
1479
1480 static int
1481 smb2_ioctl_query_info(const unsigned int xid,
1482 struct cifs_tcon *tcon,
1483 struct cifs_sb_info *cifs_sb,
1484 __le16 *path, int is_dir,
1485 unsigned long p)
1486 {
1487 struct smb2_compound_vars *vars;
1488 struct smb_rqst *rqst;
1489 struct kvec *rsp_iov;
1490 struct cifs_ses *ses = tcon->ses;
1491 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1492 char __user *arg = (char __user *)p;
1493 struct smb_query_info qi;
1494 struct smb_query_info __user *pqi;
1495 int rc = 0;
1496 int flags = CIFS_CP_CREATE_CLOSE_OP;
1497 struct smb2_query_info_rsp *qi_rsp = NULL;
1498 struct smb2_ioctl_rsp *io_rsp = NULL;
1499 void *buffer = NULL;
1500 int resp_buftype[3];
1501 struct cifs_open_parms oparms;
1502 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1503 struct cifs_fid fid;
1504 unsigned int size[2];
1505 void *data[2];
1506 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1507 void (*free_req1_func)(struct smb_rqst *r);
1508
1509 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1510 if (vars == NULL)
1511 return -ENOMEM;
1512 rqst = &vars->rqst[0];
1513 rsp_iov = &vars->rsp_iov[0];
1514
1515 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1516
1517 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1518 rc = -EFAULT;
1519 goto free_vars;
1520 }
1521 if (qi.output_buffer_length > 1024) {
1522 rc = -EINVAL;
1523 goto free_vars;
1524 }
1525
1526 if (!ses || !server) {
1527 rc = -EIO;
1528 goto free_vars;
1529 }
1530
1531 if (smb3_encryption_required(tcon))
1532 flags |= CIFS_TRANSFORM_REQ;
1533
1534 if (qi.output_buffer_length) {
1535 buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1536 if (IS_ERR(buffer)) {
1537 rc = PTR_ERR(buffer);
1538 goto free_vars;
1539 }
1540 }
1541
1542 /* Open */
1543 rqst[0].rq_iov = &vars->open_iov[0];
1544 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1545
1546 oparms = (struct cifs_open_parms) {
1547 .tcon = tcon,
1548 .disposition = FILE_OPEN,
1549 .create_options = cifs_create_options(cifs_sb, create_options),
1550 .fid = &fid,
1551 };
1552
1553 if (qi.flags & PASSTHRU_FSCTL) {
1554 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1555 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1556 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1557 break;
1558 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1559 oparms.desired_access = GENERIC_ALL;
1560 break;
1561 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1562 oparms.desired_access = GENERIC_READ;
1563 break;
1564 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1565 oparms.desired_access = GENERIC_WRITE;
1566 break;
1567 }
1568 } else if (qi.flags & PASSTHRU_SET_INFO) {
1569 oparms.desired_access = GENERIC_WRITE;
1570 } else {
1571 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1572 }
1573
1574 rc = SMB2_open_init(tcon, server,
1575 &rqst[0], &oplock, &oparms, path);
1576 if (rc)
1577 goto free_output_buffer;
1578 smb2_set_next_command(tcon, &rqst[0]);
1579
1580 /* Query */
1581 if (qi.flags & PASSTHRU_FSCTL) {
1582 /* Can eventually relax perm check since server enforces too */
1583 if (!capable(CAP_SYS_ADMIN)) {
1584 rc = -EPERM;
1585 goto free_open_req;
1586 }
1587 rqst[1].rq_iov = &vars->io_iov[0];
1588 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1589
1590 rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1591 qi.info_type, buffer, qi.output_buffer_length,
1592 CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1593 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1594 free_req1_func = SMB2_ioctl_free;
1595 } else if (qi.flags == PASSTHRU_SET_INFO) {
1596 /* Can eventually relax perm check since server enforces too */
1597 if (!capable(CAP_SYS_ADMIN)) {
1598 rc = -EPERM;
1599 goto free_open_req;
1600 }
1601 if (qi.output_buffer_length < 8) {
1602 rc = -EINVAL;
1603 goto free_open_req;
1604 }
1605 rqst[1].rq_iov = vars->si_iov;
1606 rqst[1].rq_nvec = 1;
1607
1608 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1609 size[0] = 8;
1610 data[0] = buffer;
1611
1612 rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1613 current->tgid, FILE_END_OF_FILE_INFORMATION,
1614 SMB2_O_INFO_FILE, 0, data, size);
1615 free_req1_func = SMB2_set_info_free;
1616 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1617 rqst[1].rq_iov = &vars->qi_iov;
1618 rqst[1].rq_nvec = 1;
1619
1620 rc = SMB2_query_info_init(tcon, server,
1621 &rqst[1], COMPOUND_FID,
1622 COMPOUND_FID, qi.file_info_class,
1623 qi.info_type, qi.additional_information,
1624 qi.input_buffer_length,
1625 qi.output_buffer_length, buffer);
1626 free_req1_func = SMB2_query_info_free;
1627 } else { /* unknown flags */
1628 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1629 qi.flags);
1630 rc = -EINVAL;
1631 }
1632
1633 if (rc)
1634 goto free_open_req;
1635 smb2_set_next_command(tcon, &rqst[1]);
1636 smb2_set_related(&rqst[1]);
1637
1638 /* Close */
1639 rqst[2].rq_iov = &vars->close_iov;
1640 rqst[2].rq_nvec = 1;
1641
1642 rc = SMB2_close_init(tcon, server,
1643 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1644 if (rc)
1645 goto free_req_1;
1646 smb2_set_related(&rqst[2]);
1647
1648 rc = compound_send_recv(xid, ses, server,
1649 flags, 3, rqst,
1650 resp_buftype, rsp_iov);
1651 if (rc)
1652 goto out;
1653
1654 /* No need to bump num_remote_opens since handle immediately closed */
1655 if (qi.flags & PASSTHRU_FSCTL) {
1656 pqi = (struct smb_query_info __user *)arg;
1657 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1658 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1659 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1660 if (qi.input_buffer_length > 0 &&
1661 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1662 > rsp_iov[1].iov_len) {
1663 rc = -EFAULT;
1664 goto out;
1665 }
1666
1667 if (copy_to_user(&pqi->input_buffer_length,
1668 &qi.input_buffer_length,
1669 sizeof(qi.input_buffer_length))) {
1670 rc = -EFAULT;
1671 goto out;
1672 }
1673
1674 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1675 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1676 qi.input_buffer_length))
1677 rc = -EFAULT;
1678 } else {
1679 pqi = (struct smb_query_info __user *)arg;
1680 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1681 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1682 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1683 if (copy_to_user(&pqi->input_buffer_length,
1684 &qi.input_buffer_length,
1685 sizeof(qi.input_buffer_length))) {
1686 rc = -EFAULT;
1687 goto out;
1688 }
1689
1690 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1691 qi.input_buffer_length))
1692 rc = -EFAULT;
1693 }
1694
1695 out:
1696 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1697 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1698 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1699 SMB2_close_free(&rqst[2]);
1700 free_req_1:
1701 free_req1_func(&rqst[1]);
1702 free_open_req:
1703 SMB2_open_free(&rqst[0]);
1704 free_output_buffer:
1705 kfree(buffer);
1706 free_vars:
1707 kfree(vars);
1708 return rc;
1709 }
1710
1711 static ssize_t
1712 smb2_copychunk_range(const unsigned int xid,
1713 struct cifsFileInfo *srcfile,
1714 struct cifsFileInfo *trgtfile, u64 src_off,
1715 u64 len, u64 dest_off)
1716 {
1717 int rc;
1718 unsigned int ret_data_len;
1719 struct copychunk_ioctl *pcchunk;
1720 struct copychunk_ioctl_rsp *retbuf = NULL;
1721 struct cifs_tcon *tcon;
1722 int chunks_copied = 0;
1723 bool chunk_sizes_updated = false;
1724 ssize_t bytes_written, total_bytes_written = 0;
1725
1726 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1727 if (pcchunk == NULL)
1728 return -ENOMEM;
1729
1730 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1731 /* Request a key from the server to identify the source of the copy */
1732 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1733 srcfile->fid.persistent_fid,
1734 srcfile->fid.volatile_fid, pcchunk);
1735
1736 /* Note: request_res_key sets res_key null only if rc !=0 */
1737 if (rc)
1738 goto cchunk_out;
1739
1740 /* For now array only one chunk long, will make more flexible later */
1741 pcchunk->ChunkCount = cpu_to_le32(1);
1742 pcchunk->Reserved = 0;
1743 pcchunk->Reserved2 = 0;
1744
1745 tcon = tlink_tcon(trgtfile->tlink);
1746
1747 while (len > 0) {
1748 pcchunk->SourceOffset = cpu_to_le64(src_off);
1749 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1750 pcchunk->Length =
1751 cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
1752
1753 /* Request server copy to target from src identified by key */
1754 kfree(retbuf);
1755 retbuf = NULL;
1756 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1757 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1758 (char *)pcchunk, sizeof(struct copychunk_ioctl),
1759 CIFSMaxBufSize, (char **)&retbuf, &ret_data_len);
1760 if (rc == 0) {
1761 if (ret_data_len !=
1762 sizeof(struct copychunk_ioctl_rsp)) {
1763 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1764 rc = -EIO;
1765 goto cchunk_out;
1766 }
1767 if (retbuf->TotalBytesWritten == 0) {
1768 cifs_dbg(FYI, "no bytes copied\n");
1769 rc = -EIO;
1770 goto cchunk_out;
1771 }
1772 /*
1773 * Check if server claimed to write more than we asked
1774 */
1775 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1776 le32_to_cpu(pcchunk->Length)) {
1777 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1778 rc = -EIO;
1779 goto cchunk_out;
1780 }
1781 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1782 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1783 rc = -EIO;
1784 goto cchunk_out;
1785 }
1786 chunks_copied++;
1787
1788 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1789 src_off += bytes_written;
1790 dest_off += bytes_written;
1791 len -= bytes_written;
1792 total_bytes_written += bytes_written;
1793
1794 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1795 le32_to_cpu(retbuf->ChunksWritten),
1796 le32_to_cpu(retbuf->ChunkBytesWritten),
1797 bytes_written);
1798 } else if (rc == -EINVAL) {
1799 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1800 goto cchunk_out;
1801
1802 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1803 le32_to_cpu(retbuf->ChunksWritten),
1804 le32_to_cpu(retbuf->ChunkBytesWritten),
1805 le32_to_cpu(retbuf->TotalBytesWritten));
1806
1807 /*
1808 * Check if this is the first request using these sizes,
1809 * (ie check if copy succeed once with original sizes
1810 * and check if the server gave us different sizes after
1811 * we already updated max sizes on previous request).
1812 * if not then why is the server returning an error now
1813 */
1814 if ((chunks_copied != 0) || chunk_sizes_updated)
1815 goto cchunk_out;
1816
1817 /* Check that server is not asking us to grow size */
1818 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1819 tcon->max_bytes_chunk)
1820 tcon->max_bytes_chunk =
1821 le32_to_cpu(retbuf->ChunkBytesWritten);
1822 else
1823 goto cchunk_out; /* server gave us bogus size */
1824
1825 /* No need to change MaxChunks since already set to 1 */
1826 chunk_sizes_updated = true;
1827 } else
1828 goto cchunk_out;
1829 }
1830
1831 cchunk_out:
1832 kfree(pcchunk);
1833 kfree(retbuf);
1834 if (rc)
1835 return rc;
1836 else
1837 return total_bytes_written;
1838 }
1839
1840 static int
1841 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1842 struct cifs_fid *fid)
1843 {
1844 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1845 }
1846
1847 static unsigned int
1848 smb2_read_data_offset(char *buf)
1849 {
1850 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1851
1852 return rsp->DataOffset;
1853 }
1854
1855 static unsigned int
1856 smb2_read_data_length(char *buf, bool in_remaining)
1857 {
1858 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1859
1860 if (in_remaining)
1861 return le32_to_cpu(rsp->DataRemaining);
1862
1863 return le32_to_cpu(rsp->DataLength);
1864 }
1865
1866
1867 static int
1868 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1869 struct cifs_io_parms *parms, unsigned int *bytes_read,
1870 char **buf, int *buf_type)
1871 {
1872 parms->persistent_fid = pfid->persistent_fid;
1873 parms->volatile_fid = pfid->volatile_fid;
1874 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1875 }
1876
1877 static int
1878 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1879 struct cifs_io_parms *parms, unsigned int *written,
1880 struct kvec *iov, unsigned long nr_segs)
1881 {
1882
1883 parms->persistent_fid = pfid->persistent_fid;
1884 parms->volatile_fid = pfid->volatile_fid;
1885 return SMB2_write(xid, parms, written, iov, nr_segs);
1886 }
1887
1888 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1889 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1890 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1891 {
1892 struct cifsInodeInfo *cifsi;
1893 int rc;
1894
1895 cifsi = CIFS_I(inode);
1896
1897 /* if file already sparse don't bother setting sparse again */
1898 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1899 return true; /* already sparse */
1900
1901 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1902 return true; /* already not sparse */
1903
1904 /*
1905 * Can't check for sparse support on share the usual way via the
1906 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1907 * since Samba server doesn't set the flag on the share, yet
1908 * supports the set sparse FSCTL and returns sparse correctly
1909 * in the file attributes. If we fail setting sparse though we
1910 * mark that server does not support sparse files for this share
1911 * to avoid repeatedly sending the unsupported fsctl to server
1912 * if the file is repeatedly extended.
1913 */
1914 if (tcon->broken_sparse_sup)
1915 return false;
1916
1917 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1918 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1919 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1920 if (rc) {
1921 tcon->broken_sparse_sup = true;
1922 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1923 return false;
1924 }
1925
1926 if (setsparse)
1927 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1928 else
1929 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1930
1931 return true;
1932 }
1933
1934 static int
1935 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1936 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1937 {
1938 struct inode *inode;
1939
1940 /*
1941 * If extending file more than one page make sparse. Many Linux fs
1942 * make files sparse by default when extending via ftruncate
1943 */
1944 inode = d_inode(cfile->dentry);
1945
1946 if (!set_alloc && (size > inode->i_size + 8192)) {
1947 __u8 set_sparse = 1;
1948
1949 /* whether set sparse succeeds or not, extend the file */
1950 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1951 }
1952
1953 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1954 cfile->fid.volatile_fid, cfile->pid, size);
1955 }
1956
1957 static int
1958 smb2_duplicate_extents(const unsigned int xid,
1959 struct cifsFileInfo *srcfile,
1960 struct cifsFileInfo *trgtfile, u64 src_off,
1961 u64 len, u64 dest_off)
1962 {
1963 int rc;
1964 unsigned int ret_data_len;
1965 struct inode *inode;
1966 struct duplicate_extents_to_file dup_ext_buf;
1967 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1968
1969 /* server fileays advertise duplicate extent support with this flag */
1970 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1971 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1972 return -EOPNOTSUPP;
1973
1974 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1975 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1976 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1977 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1978 dup_ext_buf.ByteCount = cpu_to_le64(len);
1979 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1980 src_off, dest_off, len);
1981
1982 inode = d_inode(trgtfile->dentry);
1983 if (inode->i_size < dest_off + len) {
1984 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1985 if (rc)
1986 goto duplicate_extents_out;
1987
1988 /*
1989 * Although also could set plausible allocation size (i_blocks)
1990 * here in addition to setting the file size, in reflink
1991 * it is likely that the target file is sparse. Its allocation
1992 * size will be queried on next revalidate, but it is important
1993 * to make sure that file's cached size is updated immediately
1994 */
1995 cifs_setsize(inode, dest_off + len);
1996 }
1997 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1998 trgtfile->fid.volatile_fid,
1999 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2000 (char *)&dup_ext_buf,
2001 sizeof(struct duplicate_extents_to_file),
2002 CIFSMaxBufSize, NULL,
2003 &ret_data_len);
2004
2005 if (ret_data_len > 0)
2006 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2007
2008 duplicate_extents_out:
2009 return rc;
2010 }
2011
2012 static int
2013 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2014 struct cifsFileInfo *cfile)
2015 {
2016 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2017 cfile->fid.volatile_fid);
2018 }
2019
2020 static int
2021 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2022 struct cifsFileInfo *cfile)
2023 {
2024 struct fsctl_set_integrity_information_req integr_info;
2025 unsigned int ret_data_len;
2026
2027 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2028 integr_info.Flags = 0;
2029 integr_info.Reserved = 0;
2030
2031 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2032 cfile->fid.volatile_fid,
2033 FSCTL_SET_INTEGRITY_INFORMATION,
2034 (char *)&integr_info,
2035 sizeof(struct fsctl_set_integrity_information_req),
2036 CIFSMaxBufSize, NULL,
2037 &ret_data_len);
2038
2039 }
2040
2041 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2042 #define GMT_TOKEN_SIZE 50
2043
2044 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2045
2046 /*
2047 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2048 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2049 */
2050 static int
2051 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2052 struct cifsFileInfo *cfile, void __user *ioc_buf)
2053 {
2054 char *retbuf = NULL;
2055 unsigned int ret_data_len = 0;
2056 int rc;
2057 u32 max_response_size;
2058 struct smb_snapshot_array snapshot_in;
2059
2060 /*
2061 * On the first query to enumerate the list of snapshots available
2062 * for this volume the buffer begins with 0 (number of snapshots
2063 * which can be returned is zero since at that point we do not know
2064 * how big the buffer needs to be). On the second query,
2065 * it (ret_data_len) is set to number of snapshots so we can
2066 * know to set the maximum response size larger (see below).
2067 */
2068 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2069 return -EFAULT;
2070
2071 /*
2072 * Note that for snapshot queries that servers like Azure expect that
2073 * the first query be minimal size (and just used to get the number/size
2074 * of previous versions) so response size must be specified as EXACTLY
2075 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2076 * of eight bytes.
2077 */
2078 if (ret_data_len == 0)
2079 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2080 else
2081 max_response_size = CIFSMaxBufSize;
2082
2083 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2084 cfile->fid.volatile_fid,
2085 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2086 NULL, 0 /* no input data */, max_response_size,
2087 (char **)&retbuf,
2088 &ret_data_len);
2089 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2090 rc, ret_data_len);
2091 if (rc)
2092 return rc;
2093
2094 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2095 /* Fixup buffer */
2096 if (copy_from_user(&snapshot_in, ioc_buf,
2097 sizeof(struct smb_snapshot_array))) {
2098 rc = -EFAULT;
2099 kfree(retbuf);
2100 return rc;
2101 }
2102
2103 /*
2104 * Check for min size, ie not large enough to fit even one GMT
2105 * token (snapshot). On the first ioctl some users may pass in
2106 * smaller size (or zero) to simply get the size of the array
2107 * so the user space caller can allocate sufficient memory
2108 * and retry the ioctl again with larger array size sufficient
2109 * to hold all of the snapshot GMT tokens on the second try.
2110 */
2111 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2112 ret_data_len = sizeof(struct smb_snapshot_array);
2113
2114 /*
2115 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2116 * the snapshot array (of 50 byte GMT tokens) each
2117 * representing an available previous version of the data
2118 */
2119 if (ret_data_len > (snapshot_in.snapshot_array_size +
2120 sizeof(struct smb_snapshot_array)))
2121 ret_data_len = snapshot_in.snapshot_array_size +
2122 sizeof(struct smb_snapshot_array);
2123
2124 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2125 rc = -EFAULT;
2126 }
2127
2128 kfree(retbuf);
2129 return rc;
2130 }
2131
2132
2133
2134 static int
2135 smb3_notify(const unsigned int xid, struct file *pfile,
2136 void __user *ioc_buf, bool return_changes)
2137 {
2138 struct smb3_notify_info notify;
2139 struct smb3_notify_info __user *pnotify_buf;
2140 struct dentry *dentry = pfile->f_path.dentry;
2141 struct inode *inode = file_inode(pfile);
2142 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2143 struct cifs_open_parms oparms;
2144 struct cifs_fid fid;
2145 struct cifs_tcon *tcon;
2146 const unsigned char *path;
2147 char *returned_ioctl_info = NULL;
2148 void *page = alloc_dentry_path();
2149 __le16 *utf16_path = NULL;
2150 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2151 int rc = 0;
2152 __u32 ret_len = 0;
2153
2154 path = build_path_from_dentry(dentry, page);
2155 if (IS_ERR(path)) {
2156 rc = PTR_ERR(path);
2157 goto notify_exit;
2158 }
2159
2160 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2161 if (utf16_path == NULL) {
2162 rc = -ENOMEM;
2163 goto notify_exit;
2164 }
2165
2166 if (return_changes) {
2167 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify_info))) {
2168 rc = -EFAULT;
2169 goto notify_exit;
2170 }
2171 } else {
2172 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2173 rc = -EFAULT;
2174 goto notify_exit;
2175 }
2176 notify.data_len = 0;
2177 }
2178
2179 tcon = cifs_sb_master_tcon(cifs_sb);
2180 oparms = (struct cifs_open_parms) {
2181 .tcon = tcon,
2182 .path = path,
2183 .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2184 .disposition = FILE_OPEN,
2185 .create_options = cifs_create_options(cifs_sb, 0),
2186 .fid = &fid,
2187 };
2188
2189 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2190 NULL);
2191 if (rc)
2192 goto notify_exit;
2193
2194 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2195 notify.watch_tree, notify.completion_filter,
2196 notify.data_len, &returned_ioctl_info, &ret_len);
2197
2198 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2199
2200 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2201 if (return_changes && (ret_len > 0) && (notify.data_len > 0)) {
2202 if (ret_len > notify.data_len)
2203 ret_len = notify.data_len;
2204 pnotify_buf = (struct smb3_notify_info __user *)ioc_buf;
2205 if (copy_to_user(pnotify_buf->notify_data, returned_ioctl_info, ret_len))
2206 rc = -EFAULT;
2207 else if (copy_to_user(&pnotify_buf->data_len, &ret_len, sizeof(ret_len)))
2208 rc = -EFAULT;
2209 }
2210 kfree(returned_ioctl_info);
2211 notify_exit:
2212 free_dentry_path(page);
2213 kfree(utf16_path);
2214 return rc;
2215 }
2216
2217 static int
2218 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2219 const char *path, struct cifs_sb_info *cifs_sb,
2220 struct cifs_fid *fid, __u16 search_flags,
2221 struct cifs_search_info *srch_inf)
2222 {
2223 __le16 *utf16_path;
2224 struct smb_rqst rqst[2];
2225 struct kvec rsp_iov[2];
2226 int resp_buftype[2];
2227 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2228 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2229 int rc, flags = 0;
2230 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2231 struct cifs_open_parms oparms;
2232 struct smb2_query_directory_rsp *qd_rsp = NULL;
2233 struct smb2_create_rsp *op_rsp = NULL;
2234 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2235 int retry_count = 0;
2236
2237 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2238 if (!utf16_path)
2239 return -ENOMEM;
2240
2241 if (smb3_encryption_required(tcon))
2242 flags |= CIFS_TRANSFORM_REQ;
2243
2244 memset(rqst, 0, sizeof(rqst));
2245 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2246 memset(rsp_iov, 0, sizeof(rsp_iov));
2247
2248 /* Open */
2249 memset(&open_iov, 0, sizeof(open_iov));
2250 rqst[0].rq_iov = open_iov;
2251 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2252
2253 oparms = (struct cifs_open_parms) {
2254 .tcon = tcon,
2255 .path = path,
2256 .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2257 .disposition = FILE_OPEN,
2258 .create_options = cifs_create_options(cifs_sb, 0),
2259 .fid = fid,
2260 };
2261
2262 rc = SMB2_open_init(tcon, server,
2263 &rqst[0], &oplock, &oparms, utf16_path);
2264 if (rc)
2265 goto qdf_free;
2266 smb2_set_next_command(tcon, &rqst[0]);
2267
2268 /* Query directory */
2269 srch_inf->entries_in_buffer = 0;
2270 srch_inf->index_of_last_entry = 2;
2271
2272 memset(&qd_iov, 0, sizeof(qd_iov));
2273 rqst[1].rq_iov = qd_iov;
2274 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2275
2276 rc = SMB2_query_directory_init(xid, tcon, server,
2277 &rqst[1],
2278 COMPOUND_FID, COMPOUND_FID,
2279 0, srch_inf->info_level);
2280 if (rc)
2281 goto qdf_free;
2282
2283 smb2_set_related(&rqst[1]);
2284
2285 again:
2286 rc = compound_send_recv(xid, tcon->ses, server,
2287 flags, 2, rqst,
2288 resp_buftype, rsp_iov);
2289
2290 if (rc == -EAGAIN && retry_count++ < 10)
2291 goto again;
2292
2293 /* If the open failed there is nothing to do */
2294 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2295 if (op_rsp == NULL || op_rsp->hdr.Status != STATUS_SUCCESS) {
2296 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2297 goto qdf_free;
2298 }
2299 fid->persistent_fid = op_rsp->PersistentFileId;
2300 fid->volatile_fid = op_rsp->VolatileFileId;
2301
2302 /* Anything else than ENODATA means a genuine error */
2303 if (rc && rc != -ENODATA) {
2304 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2305 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2306 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2307 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2308 goto qdf_free;
2309 }
2310
2311 atomic_inc(&tcon->num_remote_opens);
2312
2313 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2314 if (qd_rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2315 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2316 tcon->tid, tcon->ses->Suid, 0, 0);
2317 srch_inf->endOfSearch = true;
2318 rc = 0;
2319 goto qdf_free;
2320 }
2321
2322 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2323 srch_inf);
2324 if (rc) {
2325 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2326 tcon->ses->Suid, 0, 0, rc);
2327 goto qdf_free;
2328 }
2329 resp_buftype[1] = CIFS_NO_BUFFER;
2330
2331 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2332 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2333
2334 qdf_free:
2335 kfree(utf16_path);
2336 SMB2_open_free(&rqst[0]);
2337 SMB2_query_directory_free(&rqst[1]);
2338 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2339 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2340 return rc;
2341 }
2342
2343 static int
2344 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2345 struct cifs_fid *fid, __u16 search_flags,
2346 struct cifs_search_info *srch_inf)
2347 {
2348 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2349 fid->volatile_fid, 0, srch_inf);
2350 }
2351
2352 static int
2353 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2354 struct cifs_fid *fid)
2355 {
2356 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2357 }
2358
2359 /*
2360 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2361 * the number of credits and return true. Otherwise - return false.
2362 */
2363 static bool
2364 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2365 {
2366 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2367 int scredits, in_flight;
2368
2369 if (shdr->Status != STATUS_PENDING)
2370 return false;
2371
2372 if (shdr->CreditRequest) {
2373 spin_lock(&server->req_lock);
2374 server->credits += le16_to_cpu(shdr->CreditRequest);
2375 scredits = server->credits;
2376 in_flight = server->in_flight;
2377 spin_unlock(&server->req_lock);
2378 wake_up(&server->request_q);
2379
2380 trace_smb3_pend_credits(server->CurrentMid,
2381 server->conn_id, server->hostname, scredits,
2382 le16_to_cpu(shdr->CreditRequest), in_flight);
2383 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2384 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
2385 }
2386
2387 return true;
2388 }
2389
2390 static bool
2391 smb2_is_session_expired(char *buf)
2392 {
2393 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2394
2395 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2396 shdr->Status != STATUS_USER_SESSION_DELETED)
2397 return false;
2398
2399 trace_smb3_ses_expired(le32_to_cpu(shdr->Id.SyncId.TreeId),
2400 le64_to_cpu(shdr->SessionId),
2401 le16_to_cpu(shdr->Command),
2402 le64_to_cpu(shdr->MessageId));
2403 cifs_dbg(FYI, "Session expired or deleted\n");
2404
2405 return true;
2406 }
2407
2408 static bool
2409 smb2_is_status_io_timeout(char *buf)
2410 {
2411 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2412
2413 if (shdr->Status == STATUS_IO_TIMEOUT)
2414 return true;
2415 else
2416 return false;
2417 }
2418
2419 static bool
2420 smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2421 {
2422 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2423 struct TCP_Server_Info *pserver;
2424 struct cifs_ses *ses;
2425 struct cifs_tcon *tcon;
2426
2427 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2428 return false;
2429
2430 /* If server is a channel, select the primary channel */
2431 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
2432
2433 spin_lock(&cifs_tcp_ses_lock);
2434 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
2435 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2436 if (tcon->tid == le32_to_cpu(shdr->Id.SyncId.TreeId)) {
2437 spin_lock(&tcon->tc_lock);
2438 tcon->need_reconnect = true;
2439 spin_unlock(&tcon->tc_lock);
2440 spin_unlock(&cifs_tcp_ses_lock);
2441 pr_warn_once("Server share %s deleted.\n",
2442 tcon->tree_name);
2443 return true;
2444 }
2445 }
2446 }
2447 spin_unlock(&cifs_tcp_ses_lock);
2448
2449 return false;
2450 }
2451
2452 static int
2453 smb2_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
2454 __u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
2455 {
2456 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2457 return SMB2_lease_break(0, tcon, cinode->lease_key,
2458 smb2_get_lease_state(cinode));
2459
2460 return SMB2_oplock_break(0, tcon, persistent_fid, volatile_fid,
2461 CIFS_CACHE_READ(cinode) ? 1 : 0);
2462 }
2463
2464 void
2465 smb2_set_related(struct smb_rqst *rqst)
2466 {
2467 struct smb2_hdr *shdr;
2468
2469 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2470 if (shdr == NULL) {
2471 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2472 return;
2473 }
2474 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2475 }
2476
2477 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2478
2479 void
2480 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2481 {
2482 struct smb2_hdr *shdr;
2483 struct cifs_ses *ses = tcon->ses;
2484 struct TCP_Server_Info *server = ses->server;
2485 unsigned long len = smb_rqst_len(server, rqst);
2486 int i, num_padding;
2487
2488 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2489 if (shdr == NULL) {
2490 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2491 return;
2492 }
2493
2494 /* SMB headers in a compound are 8 byte aligned. */
2495
2496 /* No padding needed */
2497 if (!(len & 7))
2498 goto finished;
2499
2500 num_padding = 8 - (len & 7);
2501 if (!smb3_encryption_required(tcon)) {
2502 /*
2503 * If we do not have encryption then we can just add an extra
2504 * iov for the padding.
2505 */
2506 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2507 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2508 rqst->rq_nvec++;
2509 len += num_padding;
2510 } else {
2511 /*
2512 * We can not add a small padding iov for the encryption case
2513 * because the encryption framework can not handle the padding
2514 * iovs.
2515 * We have to flatten this into a single buffer and add
2516 * the padding to it.
2517 */
2518 for (i = 1; i < rqst->rq_nvec; i++) {
2519 memcpy(rqst->rq_iov[0].iov_base +
2520 rqst->rq_iov[0].iov_len,
2521 rqst->rq_iov[i].iov_base,
2522 rqst->rq_iov[i].iov_len);
2523 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2524 }
2525 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2526 0, num_padding);
2527 rqst->rq_iov[0].iov_len += num_padding;
2528 len += num_padding;
2529 rqst->rq_nvec = 1;
2530 }
2531
2532 finished:
2533 shdr->NextCommand = cpu_to_le32(len);
2534 }
2535
2536 /*
2537 * Passes the query info response back to the caller on success.
2538 * Caller need to free this with free_rsp_buf().
2539 */
2540 int
2541 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2542 const char *path, u32 desired_access,
2543 u32 class, u32 type, u32 output_len,
2544 struct kvec *rsp, int *buftype,
2545 struct cifs_sb_info *cifs_sb)
2546 {
2547 struct smb2_compound_vars *vars;
2548 struct cifs_ses *ses = tcon->ses;
2549 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2550 int flags = CIFS_CP_CREATE_CLOSE_OP;
2551 struct smb_rqst *rqst;
2552 int resp_buftype[3];
2553 struct kvec *rsp_iov;
2554 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2555 struct cifs_open_parms oparms;
2556 struct cifs_fid fid;
2557 int rc;
2558 __le16 *utf16_path;
2559 struct cached_fid *cfid = NULL;
2560
2561 if (!path)
2562 path = "";
2563 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2564 if (!utf16_path)
2565 return -ENOMEM;
2566
2567 if (smb3_encryption_required(tcon))
2568 flags |= CIFS_TRANSFORM_REQ;
2569
2570 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2571 vars = kzalloc(sizeof(*vars), GFP_KERNEL);
2572 if (!vars) {
2573 rc = -ENOMEM;
2574 goto out_free_path;
2575 }
2576 rqst = vars->rqst;
2577 rsp_iov = vars->rsp_iov;
2578
2579 /*
2580 * We can only call this for things we know are directories.
2581 */
2582 if (!strcmp(path, ""))
2583 open_cached_dir(xid, tcon, path, cifs_sb, false,
2584 &cfid); /* cfid null if open dir failed */
2585
2586 rqst[0].rq_iov = vars->open_iov;
2587 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2588
2589 oparms = (struct cifs_open_parms) {
2590 .tcon = tcon,
2591 .path = path,
2592 .desired_access = desired_access,
2593 .disposition = FILE_OPEN,
2594 .create_options = cifs_create_options(cifs_sb, 0),
2595 .fid = &fid,
2596 };
2597
2598 rc = SMB2_open_init(tcon, server,
2599 &rqst[0], &oplock, &oparms, utf16_path);
2600 if (rc)
2601 goto qic_exit;
2602 smb2_set_next_command(tcon, &rqst[0]);
2603
2604 rqst[1].rq_iov = &vars->qi_iov;
2605 rqst[1].rq_nvec = 1;
2606
2607 if (cfid) {
2608 rc = SMB2_query_info_init(tcon, server,
2609 &rqst[1],
2610 cfid->fid.persistent_fid,
2611 cfid->fid.volatile_fid,
2612 class, type, 0,
2613 output_len, 0,
2614 NULL);
2615 } else {
2616 rc = SMB2_query_info_init(tcon, server,
2617 &rqst[1],
2618 COMPOUND_FID,
2619 COMPOUND_FID,
2620 class, type, 0,
2621 output_len, 0,
2622 NULL);
2623 }
2624 if (rc)
2625 goto qic_exit;
2626 if (!cfid) {
2627 smb2_set_next_command(tcon, &rqst[1]);
2628 smb2_set_related(&rqst[1]);
2629 }
2630
2631 rqst[2].rq_iov = &vars->close_iov;
2632 rqst[2].rq_nvec = 1;
2633
2634 rc = SMB2_close_init(tcon, server,
2635 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2636 if (rc)
2637 goto qic_exit;
2638 smb2_set_related(&rqst[2]);
2639
2640 if (cfid) {
2641 rc = compound_send_recv(xid, ses, server,
2642 flags, 1, &rqst[1],
2643 &resp_buftype[1], &rsp_iov[1]);
2644 } else {
2645 rc = compound_send_recv(xid, ses, server,
2646 flags, 3, rqst,
2647 resp_buftype, rsp_iov);
2648 }
2649 if (rc) {
2650 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2651 if (rc == -EREMCHG) {
2652 tcon->need_reconnect = true;
2653 pr_warn_once("server share %s deleted\n",
2654 tcon->tree_name);
2655 }
2656 goto qic_exit;
2657 }
2658 *rsp = rsp_iov[1];
2659 *buftype = resp_buftype[1];
2660
2661 qic_exit:
2662 SMB2_open_free(&rqst[0]);
2663 SMB2_query_info_free(&rqst[1]);
2664 SMB2_close_free(&rqst[2]);
2665 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2666 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2667 if (cfid)
2668 close_cached_dir(cfid);
2669 kfree(vars);
2670 out_free_path:
2671 kfree(utf16_path);
2672 return rc;
2673 }
2674
2675 static int
2676 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2677 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2678 {
2679 struct smb2_query_info_rsp *rsp;
2680 struct smb2_fs_full_size_info *info = NULL;
2681 struct kvec rsp_iov = {NULL, 0};
2682 int buftype = CIFS_NO_BUFFER;
2683 int rc;
2684
2685
2686 rc = smb2_query_info_compound(xid, tcon, "",
2687 FILE_READ_ATTRIBUTES,
2688 FS_FULL_SIZE_INFORMATION,
2689 SMB2_O_INFO_FILESYSTEM,
2690 sizeof(struct smb2_fs_full_size_info),
2691 &rsp_iov, &buftype, cifs_sb);
2692 if (rc)
2693 goto qfs_exit;
2694
2695 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2696 buf->f_type = SMB2_SUPER_MAGIC;
2697 info = (struct smb2_fs_full_size_info *)(
2698 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2699 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2700 le32_to_cpu(rsp->OutputBufferLength),
2701 &rsp_iov,
2702 sizeof(struct smb2_fs_full_size_info));
2703 if (!rc)
2704 smb2_copy_fs_info_to_kstatfs(info, buf);
2705
2706 qfs_exit:
2707 trace_smb3_qfs_done(xid, tcon->tid, tcon->ses->Suid, tcon->tree_name, rc);
2708 free_rsp_buf(buftype, rsp_iov.iov_base);
2709 return rc;
2710 }
2711
2712 static int
2713 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2714 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2715 {
2716 int rc;
2717 __le16 srch_path = 0; /* Null - open root of share */
2718 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2719 struct cifs_open_parms oparms;
2720 struct cifs_fid fid;
2721
2722 if (!tcon->posix_extensions)
2723 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2724
2725 oparms = (struct cifs_open_parms) {
2726 .tcon = tcon,
2727 .path = "",
2728 .desired_access = FILE_READ_ATTRIBUTES,
2729 .disposition = FILE_OPEN,
2730 .create_options = cifs_create_options(cifs_sb, 0),
2731 .fid = &fid,
2732 };
2733
2734 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2735 NULL, NULL);
2736 if (rc)
2737 return rc;
2738
2739 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2740 fid.volatile_fid, buf);
2741 buf->f_type = SMB2_SUPER_MAGIC;
2742 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2743 return rc;
2744 }
2745
2746 static bool
2747 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2748 {
2749 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2750 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2751 }
2752
2753 static int
2754 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2755 __u64 length, __u32 type, int lock, int unlock, bool wait)
2756 {
2757 if (unlock && !lock)
2758 type = SMB2_LOCKFLAG_UNLOCK;
2759 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2760 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2761 current->tgid, length, offset, type, wait);
2762 }
2763
2764 static void
2765 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2766 {
2767 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2768 }
2769
2770 static void
2771 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2772 {
2773 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2774 }
2775
2776 static void
2777 smb2_new_lease_key(struct cifs_fid *fid)
2778 {
2779 generate_random_uuid(fid->lease_key);
2780 }
2781
2782 static int
2783 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2784 const char *search_name,
2785 struct dfs_info3_param **target_nodes,
2786 unsigned int *num_of_nodes,
2787 const struct nls_table *nls_codepage, int remap)
2788 {
2789 int rc;
2790 __le16 *utf16_path = NULL;
2791 int utf16_path_len = 0;
2792 struct cifs_tcon *tcon;
2793 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2794 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2795 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2796 int retry_count = 0;
2797
2798 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2799
2800 /*
2801 * Try to use the IPC tcon, otherwise just use any
2802 */
2803 tcon = ses->tcon_ipc;
2804 if (tcon == NULL) {
2805 spin_lock(&cifs_tcp_ses_lock);
2806 tcon = list_first_entry_or_null(&ses->tcon_list,
2807 struct cifs_tcon,
2808 tcon_list);
2809 if (tcon)
2810 tcon->tc_count++;
2811 spin_unlock(&cifs_tcp_ses_lock);
2812 }
2813
2814 if (tcon == NULL) {
2815 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2816 ses);
2817 rc = -ENOTCONN;
2818 goto out;
2819 }
2820
2821 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2822 &utf16_path_len,
2823 nls_codepage, remap);
2824 if (!utf16_path) {
2825 rc = -ENOMEM;
2826 goto out;
2827 }
2828
2829 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2830 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2831 if (!dfs_req) {
2832 rc = -ENOMEM;
2833 goto out;
2834 }
2835
2836 /* Highest DFS referral version understood */
2837 dfs_req->MaxReferralLevel = DFS_VERSION;
2838
2839 /* Path to resolve in an UTF-16 null-terminated string */
2840 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2841
2842 do {
2843 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2844 FSCTL_DFS_GET_REFERRALS,
2845 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2846 (char **)&dfs_rsp, &dfs_rsp_size);
2847 if (!is_retryable_error(rc))
2848 break;
2849 usleep_range(512, 2048);
2850 } while (++retry_count < 5);
2851
2852 if (!rc && !dfs_rsp)
2853 rc = -EIO;
2854 if (rc) {
2855 if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP)
2856 cifs_tcon_dbg(VFS, "%s: ioctl error: rc=%d\n", __func__, rc);
2857 goto out;
2858 }
2859
2860 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2861 num_of_nodes, target_nodes,
2862 nls_codepage, remap, search_name,
2863 true /* is_unicode */);
2864 if (rc) {
2865 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2866 goto out;
2867 }
2868
2869 out:
2870 if (tcon && !tcon->ipc) {
2871 /* ipc tcons are not refcounted */
2872 spin_lock(&cifs_tcp_ses_lock);
2873 tcon->tc_count--;
2874 /* tc_count can never go negative */
2875 WARN_ON(tcon->tc_count < 0);
2876 spin_unlock(&cifs_tcp_ses_lock);
2877 }
2878 kfree(utf16_path);
2879 kfree(dfs_req);
2880 kfree(dfs_rsp);
2881 return rc;
2882 }
2883
2884 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2885 static int parse_reparse_posix(struct reparse_posix_data *buf,
2886 struct cifs_sb_info *cifs_sb,
2887 struct cifs_open_info_data *data)
2888 {
2889 unsigned int len;
2890 u64 type;
2891
2892 switch ((type = le64_to_cpu(buf->InodeType))) {
2893 case NFS_SPECFILE_LNK:
2894 len = le16_to_cpu(buf->ReparseDataLength);
2895 data->symlink_target = cifs_strndup_from_utf16(buf->DataBuffer,
2896 len, true,
2897 cifs_sb->local_nls);
2898 if (!data->symlink_target)
2899 return -ENOMEM;
2900 convert_delimiter(data->symlink_target, '/');
2901 cifs_dbg(FYI, "%s: target path: %s\n",
2902 __func__, data->symlink_target);
2903 break;
2904 case NFS_SPECFILE_CHR:
2905 case NFS_SPECFILE_BLK:
2906 case NFS_SPECFILE_FIFO:
2907 case NFS_SPECFILE_SOCK:
2908 break;
2909 default:
2910 cifs_dbg(VFS, "%s: unhandled inode type: 0x%llx\n",
2911 __func__, type);
2912 return -EOPNOTSUPP;
2913 }
2914 return 0;
2915 }
2916
2917 static int parse_reparse_symlink(struct reparse_symlink_data_buffer *sym,
2918 u32 plen, bool unicode,
2919 struct cifs_sb_info *cifs_sb,
2920 struct cifs_open_info_data *data)
2921 {
2922 unsigned int len;
2923 unsigned int offs;
2924
2925 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2926
2927 offs = le16_to_cpu(sym->SubstituteNameOffset);
2928 len = le16_to_cpu(sym->SubstituteNameLength);
2929 if (offs + 20 > plen || offs + len + 20 > plen) {
2930 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2931 return -EIO;
2932 }
2933
2934 data->symlink_target = cifs_strndup_from_utf16(sym->PathBuffer + offs,
2935 len, unicode,
2936 cifs_sb->local_nls);
2937 if (!data->symlink_target)
2938 return -ENOMEM;
2939
2940 convert_delimiter(data->symlink_target, '/');
2941 cifs_dbg(FYI, "%s: target path: %s\n", __func__, data->symlink_target);
2942
2943 return 0;
2944 }
2945
2946 int parse_reparse_point(struct reparse_data_buffer *buf,
2947 u32 plen, struct cifs_sb_info *cifs_sb,
2948 bool unicode, struct cifs_open_info_data *data)
2949 {
2950 if (plen < sizeof(*buf)) {
2951 cifs_dbg(VFS, "%s: reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2952 __func__, plen);
2953 return -EIO;
2954 }
2955
2956 if (plen < le16_to_cpu(buf->ReparseDataLength) + sizeof(*buf)) {
2957 cifs_dbg(VFS, "%s: invalid reparse buf length: %d\n",
2958 __func__, plen);
2959 return -EIO;
2960 }
2961
2962 data->reparse.buf = buf;
2963
2964 /* See MS-FSCC 2.1.2 */
2965 switch (le32_to_cpu(buf->ReparseTag)) {
2966 case IO_REPARSE_TAG_NFS:
2967 return parse_reparse_posix((struct reparse_posix_data *)buf,
2968 cifs_sb, data);
2969 case IO_REPARSE_TAG_SYMLINK:
2970 return parse_reparse_symlink(
2971 (struct reparse_symlink_data_buffer *)buf,
2972 plen, unicode, cifs_sb, data);
2973 case IO_REPARSE_TAG_LX_SYMLINK:
2974 case IO_REPARSE_TAG_AF_UNIX:
2975 case IO_REPARSE_TAG_LX_FIFO:
2976 case IO_REPARSE_TAG_LX_CHR:
2977 case IO_REPARSE_TAG_LX_BLK:
2978 return 0;
2979 default:
2980 cifs_dbg(VFS, "%s: unhandled reparse tag: 0x%08x\n",
2981 __func__, le32_to_cpu(buf->ReparseTag));
2982 return -EOPNOTSUPP;
2983 }
2984 }
2985
2986 static int smb2_parse_reparse_point(struct cifs_sb_info *cifs_sb,
2987 struct kvec *rsp_iov,
2988 struct cifs_open_info_data *data)
2989 {
2990 struct reparse_data_buffer *buf;
2991 struct smb2_ioctl_rsp *io = rsp_iov->iov_base;
2992 u32 plen = le32_to_cpu(io->OutputCount);
2993
2994 buf = (struct reparse_data_buffer *)((u8 *)io +
2995 le32_to_cpu(io->OutputOffset));
2996 return parse_reparse_point(buf, plen, cifs_sb, true, data);
2997 }
2998
2999 static struct cifs_ntsd *
3000 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3001 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
3002 {
3003 struct cifs_ntsd *pntsd = NULL;
3004 unsigned int xid;
3005 int rc = -EOPNOTSUPP;
3006 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3007
3008 if (IS_ERR(tlink))
3009 return ERR_CAST(tlink);
3010
3011 xid = get_xid();
3012 cifs_dbg(FYI, "trying to get acl\n");
3013
3014 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3015 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3016 info);
3017 free_xid(xid);
3018
3019 cifs_put_tlink(tlink);
3020
3021 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3022 if (rc)
3023 return ERR_PTR(rc);
3024 return pntsd;
3025
3026 }
3027
3028 static struct cifs_ntsd *
3029 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3030 const char *path, u32 *pacllen, u32 info)
3031 {
3032 struct cifs_ntsd *pntsd = NULL;
3033 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3034 unsigned int xid;
3035 int rc;
3036 struct cifs_tcon *tcon;
3037 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3038 struct cifs_fid fid;
3039 struct cifs_open_parms oparms;
3040 __le16 *utf16_path;
3041
3042 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3043 if (IS_ERR(tlink))
3044 return ERR_CAST(tlink);
3045
3046 tcon = tlink_tcon(tlink);
3047 xid = get_xid();
3048
3049 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3050 if (!utf16_path) {
3051 rc = -ENOMEM;
3052 free_xid(xid);
3053 return ERR_PTR(rc);
3054 }
3055
3056 oparms = (struct cifs_open_parms) {
3057 .tcon = tcon,
3058 .path = path,
3059 .desired_access = READ_CONTROL,
3060 .disposition = FILE_OPEN,
3061 /*
3062 * When querying an ACL, even if the file is a symlink
3063 * we want to open the source not the target, and so
3064 * the protocol requires that the client specify this
3065 * flag when opening a reparse point
3066 */
3067 .create_options = cifs_create_options(cifs_sb, 0) |
3068 OPEN_REPARSE_POINT,
3069 .fid = &fid,
3070 };
3071
3072 if (info & SACL_SECINFO)
3073 oparms.desired_access |= SYSTEM_SECURITY;
3074
3075 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3076 NULL);
3077 kfree(utf16_path);
3078 if (!rc) {
3079 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3080 fid.volatile_fid, (void **)&pntsd, pacllen,
3081 info);
3082 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3083 }
3084
3085 cifs_put_tlink(tlink);
3086 free_xid(xid);
3087
3088 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3089 if (rc)
3090 return ERR_PTR(rc);
3091 return pntsd;
3092 }
3093
3094 static int
3095 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3096 struct inode *inode, const char *path, int aclflag)
3097 {
3098 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3099 unsigned int xid;
3100 int rc, access_flags = 0;
3101 struct cifs_tcon *tcon;
3102 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3103 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3104 struct cifs_fid fid;
3105 struct cifs_open_parms oparms;
3106 __le16 *utf16_path;
3107
3108 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3109 if (IS_ERR(tlink))
3110 return PTR_ERR(tlink);
3111
3112 tcon = tlink_tcon(tlink);
3113 xid = get_xid();
3114
3115 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3116 access_flags |= WRITE_OWNER;
3117 if (aclflag & CIFS_ACL_SACL)
3118 access_flags |= SYSTEM_SECURITY;
3119 if (aclflag & CIFS_ACL_DACL)
3120 access_flags |= WRITE_DAC;
3121
3122 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3123 if (!utf16_path) {
3124 rc = -ENOMEM;
3125 free_xid(xid);
3126 return rc;
3127 }
3128
3129 oparms = (struct cifs_open_parms) {
3130 .tcon = tcon,
3131 .desired_access = access_flags,
3132 .create_options = cifs_create_options(cifs_sb, 0),
3133 .disposition = FILE_OPEN,
3134 .path = path,
3135 .fid = &fid,
3136 };
3137
3138 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3139 NULL, NULL);
3140 kfree(utf16_path);
3141 if (!rc) {
3142 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3143 fid.volatile_fid, pnntsd, acllen, aclflag);
3144 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3145 }
3146
3147 cifs_put_tlink(tlink);
3148 free_xid(xid);
3149 return rc;
3150 }
3151
3152 /* Retrieve an ACL from the server */
3153 static struct cifs_ntsd *
3154 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3155 struct inode *inode, const char *path,
3156 u32 *pacllen, u32 info)
3157 {
3158 struct cifs_ntsd *pntsd = NULL;
3159 struct cifsFileInfo *open_file = NULL;
3160
3161 if (inode && !(info & SACL_SECINFO))
3162 open_file = find_readable_file(CIFS_I(inode), true);
3163 if (!open_file || (info & SACL_SECINFO))
3164 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3165
3166 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3167 cifsFileInfo_put(open_file);
3168 return pntsd;
3169 }
3170
3171 static long smb3_zero_data(struct file *file, struct cifs_tcon *tcon,
3172 loff_t offset, loff_t len, unsigned int xid)
3173 {
3174 struct cifsFileInfo *cfile = file->private_data;
3175 struct file_zero_data_information fsctl_buf;
3176
3177 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3178
3179 fsctl_buf.FileOffset = cpu_to_le64(offset);
3180 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3181
3182 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3183 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3184 (char *)&fsctl_buf,
3185 sizeof(struct file_zero_data_information),
3186 0, NULL, NULL);
3187 }
3188
3189 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3190 loff_t offset, loff_t len, bool keep_size)
3191 {
3192 struct cifs_ses *ses = tcon->ses;
3193 struct inode *inode = file_inode(file);
3194 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3195 struct cifsFileInfo *cfile = file->private_data;
3196 unsigned long long new_size;
3197 long rc;
3198 unsigned int xid;
3199
3200 xid = get_xid();
3201
3202 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3203 ses->Suid, offset, len);
3204
3205 inode_lock(inode);
3206 filemap_invalidate_lock(inode->i_mapping);
3207
3208 /*
3209 * We zero the range through ioctl, so we need remove the page caches
3210 * first, otherwise the data may be inconsistent with the server.
3211 */
3212 truncate_pagecache_range(inode, offset, offset + len - 1);
3213
3214 /* if file not oplocked can't be sure whether asking to extend size */
3215 rc = -EOPNOTSUPP;
3216 if (keep_size == false && !CIFS_CACHE_READ(cifsi))
3217 goto zero_range_exit;
3218
3219 rc = smb3_zero_data(file, tcon, offset, len, xid);
3220 if (rc < 0)
3221 goto zero_range_exit;
3222
3223 /*
3224 * do we also need to change the size of the file?
3225 */
3226 new_size = offset + len;
3227 if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) {
3228 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3229 cfile->fid.volatile_fid, cfile->pid, new_size);
3230 if (rc >= 0) {
3231 truncate_setsize(inode, new_size);
3232 fscache_resize_cookie(cifs_inode_cookie(inode), new_size);
3233 }
3234 }
3235
3236 zero_range_exit:
3237 filemap_invalidate_unlock(inode->i_mapping);
3238 inode_unlock(inode);
3239 free_xid(xid);
3240 if (rc)
3241 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3242 ses->Suid, offset, len, rc);
3243 else
3244 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3245 ses->Suid, offset, len);
3246 return rc;
3247 }
3248
3249 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3250 loff_t offset, loff_t len)
3251 {
3252 struct inode *inode = file_inode(file);
3253 struct cifsFileInfo *cfile = file->private_data;
3254 struct file_zero_data_information fsctl_buf;
3255 long rc;
3256 unsigned int xid;
3257 __u8 set_sparse = 1;
3258
3259 xid = get_xid();
3260
3261 inode_lock(inode);
3262 /* Need to make file sparse, if not already, before freeing range. */
3263 /* Consider adding equivalent for compressed since it could also work */
3264 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3265 rc = -EOPNOTSUPP;
3266 goto out;
3267 }
3268
3269 filemap_invalidate_lock(inode->i_mapping);
3270 /*
3271 * We implement the punch hole through ioctl, so we need remove the page
3272 * caches first, otherwise the data may be inconsistent with the server.
3273 */
3274 truncate_pagecache_range(inode, offset, offset + len - 1);
3275
3276 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3277
3278 fsctl_buf.FileOffset = cpu_to_le64(offset);
3279 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3280
3281 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3282 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3283 (char *)&fsctl_buf,
3284 sizeof(struct file_zero_data_information),
3285 CIFSMaxBufSize, NULL, NULL);
3286 filemap_invalidate_unlock(inode->i_mapping);
3287 out:
3288 inode_unlock(inode);
3289 free_xid(xid);
3290 return rc;
3291 }
3292
3293 static int smb3_simple_fallocate_write_range(unsigned int xid,
3294 struct cifs_tcon *tcon,
3295 struct cifsFileInfo *cfile,
3296 loff_t off, loff_t len,
3297 char *buf)
3298 {
3299 struct cifs_io_parms io_parms = {0};
3300 int nbytes;
3301 int rc = 0;
3302 struct kvec iov[2];
3303
3304 io_parms.netfid = cfile->fid.netfid;
3305 io_parms.pid = current->tgid;
3306 io_parms.tcon = tcon;
3307 io_parms.persistent_fid = cfile->fid.persistent_fid;
3308 io_parms.volatile_fid = cfile->fid.volatile_fid;
3309
3310 while (len) {
3311 io_parms.offset = off;
3312 io_parms.length = len;
3313 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3314 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3315 /* iov[0] is reserved for smb header */
3316 iov[1].iov_base = buf;
3317 iov[1].iov_len = io_parms.length;
3318 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3319 if (rc)
3320 break;
3321 if (nbytes > len)
3322 return -EINVAL;
3323 buf += nbytes;
3324 off += nbytes;
3325 len -= nbytes;
3326 }
3327 return rc;
3328 }
3329
3330 static int smb3_simple_fallocate_range(unsigned int xid,
3331 struct cifs_tcon *tcon,
3332 struct cifsFileInfo *cfile,
3333 loff_t off, loff_t len)
3334 {
3335 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3336 u32 out_data_len;
3337 char *buf = NULL;
3338 loff_t l;
3339 int rc;
3340
3341 in_data.file_offset = cpu_to_le64(off);
3342 in_data.length = cpu_to_le64(len);
3343 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3344 cfile->fid.volatile_fid,
3345 FSCTL_QUERY_ALLOCATED_RANGES,
3346 (char *)&in_data, sizeof(in_data),
3347 1024 * sizeof(struct file_allocated_range_buffer),
3348 (char **)&out_data, &out_data_len);
3349 if (rc)
3350 goto out;
3351
3352 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3353 if (buf == NULL) {
3354 rc = -ENOMEM;
3355 goto out;
3356 }
3357
3358 tmp_data = out_data;
3359 while (len) {
3360 /*
3361 * The rest of the region is unmapped so write it all.
3362 */
3363 if (out_data_len == 0) {
3364 rc = smb3_simple_fallocate_write_range(xid, tcon,
3365 cfile, off, len, buf);
3366 goto out;
3367 }
3368
3369 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3370 rc = -EINVAL;
3371 goto out;
3372 }
3373
3374 if (off < le64_to_cpu(tmp_data->file_offset)) {
3375 /*
3376 * We are at a hole. Write until the end of the region
3377 * or until the next allocated data,
3378 * whichever comes next.
3379 */
3380 l = le64_to_cpu(tmp_data->file_offset) - off;
3381 if (len < l)
3382 l = len;
3383 rc = smb3_simple_fallocate_write_range(xid, tcon,
3384 cfile, off, l, buf);
3385 if (rc)
3386 goto out;
3387 off = off + l;
3388 len = len - l;
3389 if (len == 0)
3390 goto out;
3391 }
3392 /*
3393 * We are at a section of allocated data, just skip forward
3394 * until the end of the data or the end of the region
3395 * we are supposed to fallocate, whichever comes first.
3396 */
3397 l = le64_to_cpu(tmp_data->length);
3398 if (len < l)
3399 l = len;
3400 off += l;
3401 len -= l;
3402
3403 tmp_data = &tmp_data[1];
3404 out_data_len -= sizeof(struct file_allocated_range_buffer);
3405 }
3406
3407 out:
3408 kfree(out_data);
3409 kfree(buf);
3410 return rc;
3411 }
3412
3413
3414 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3415 loff_t off, loff_t len, bool keep_size)
3416 {
3417 struct inode *inode;
3418 struct cifsInodeInfo *cifsi;
3419 struct cifsFileInfo *cfile = file->private_data;
3420 long rc = -EOPNOTSUPP;
3421 unsigned int xid;
3422 loff_t new_eof;
3423
3424 xid = get_xid();
3425
3426 inode = d_inode(cfile->dentry);
3427 cifsi = CIFS_I(inode);
3428
3429 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3430 tcon->ses->Suid, off, len);
3431 /* if file not oplocked can't be sure whether asking to extend size */
3432 if (!CIFS_CACHE_READ(cifsi))
3433 if (keep_size == false) {
3434 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3435 tcon->tid, tcon->ses->Suid, off, len, rc);
3436 free_xid(xid);
3437 return rc;
3438 }
3439
3440 /*
3441 * Extending the file
3442 */
3443 if ((keep_size == false) && i_size_read(inode) < off + len) {
3444 rc = inode_newsize_ok(inode, off + len);
3445 if (rc)
3446 goto out;
3447
3448 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
3449 smb2_set_sparse(xid, tcon, cfile, inode, false);
3450
3451 new_eof = off + len;
3452 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3453 cfile->fid.volatile_fid, cfile->pid, new_eof);
3454 if (rc == 0) {
3455 cifsi->server_eof = new_eof;
3456 cifs_setsize(inode, new_eof);
3457 cifs_truncate_page(inode->i_mapping, inode->i_size);
3458 truncate_setsize(inode, new_eof);
3459 }
3460 goto out;
3461 }
3462
3463 /*
3464 * Files are non-sparse by default so falloc may be a no-op
3465 * Must check if file sparse. If not sparse, and since we are not
3466 * extending then no need to do anything since file already allocated
3467 */
3468 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3469 rc = 0;
3470 goto out;
3471 }
3472
3473 if (keep_size == true) {
3474 /*
3475 * We can not preallocate pages beyond the end of the file
3476 * in SMB2
3477 */
3478 if (off >= i_size_read(inode)) {
3479 rc = 0;
3480 goto out;
3481 }
3482 /*
3483 * For fallocates that are partially beyond the end of file,
3484 * clamp len so we only fallocate up to the end of file.
3485 */
3486 if (off + len > i_size_read(inode)) {
3487 len = i_size_read(inode) - off;
3488 }
3489 }
3490
3491 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3492 /*
3493 * At this point, we are trying to fallocate an internal
3494 * regions of a sparse file. Since smb2 does not have a
3495 * fallocate command we have two otions on how to emulate this.
3496 * We can either turn the entire file to become non-sparse
3497 * which we only do if the fallocate is for virtually
3498 * the whole file, or we can overwrite the region with zeroes
3499 * using SMB2_write, which could be prohibitevly expensive
3500 * if len is large.
3501 */
3502 /*
3503 * We are only trying to fallocate a small region so
3504 * just write it with zero.
3505 */
3506 if (len <= 1024 * 1024) {
3507 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3508 off, len);
3509 goto out;
3510 }
3511
3512 /*
3513 * Check if falloc starts within first few pages of file
3514 * and ends within a few pages of the end of file to
3515 * ensure that most of file is being forced to be
3516 * fallocated now. If so then setting whole file sparse
3517 * ie potentially making a few extra pages at the beginning
3518 * or end of the file non-sparse via set_sparse is harmless.
3519 */
3520 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3521 rc = -EOPNOTSUPP;
3522 goto out;
3523 }
3524 }
3525
3526 smb2_set_sparse(xid, tcon, cfile, inode, false);
3527 rc = 0;
3528
3529 out:
3530 if (rc)
3531 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3532 tcon->ses->Suid, off, len, rc);
3533 else
3534 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3535 tcon->ses->Suid, off, len);
3536
3537 free_xid(xid);
3538 return rc;
3539 }
3540
3541 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3542 loff_t off, loff_t len)
3543 {
3544 int rc;
3545 unsigned int xid;
3546 struct inode *inode = file_inode(file);
3547 struct cifsFileInfo *cfile = file->private_data;
3548 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3549 loff_t old_eof, new_eof;
3550
3551 xid = get_xid();
3552
3553 inode_lock(inode);
3554
3555 old_eof = i_size_read(inode);
3556 if ((off >= old_eof) ||
3557 off + len >= old_eof) {
3558 rc = -EINVAL;
3559 goto out;
3560 }
3561
3562 filemap_invalidate_lock(inode->i_mapping);
3563 rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof - 1);
3564 if (rc < 0)
3565 goto out_2;
3566
3567 truncate_pagecache_range(inode, off, old_eof);
3568
3569 rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3570 old_eof - off - len, off);
3571 if (rc < 0)
3572 goto out_2;
3573
3574 new_eof = old_eof - len;
3575 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3576 cfile->fid.volatile_fid, cfile->pid, new_eof);
3577 if (rc < 0)
3578 goto out_2;
3579
3580 rc = 0;
3581
3582 cifsi->server_eof = i_size_read(inode) - len;
3583 truncate_setsize(inode, cifsi->server_eof);
3584 fscache_resize_cookie(cifs_inode_cookie(inode), cifsi->server_eof);
3585 out_2:
3586 filemap_invalidate_unlock(inode->i_mapping);
3587 out:
3588 inode_unlock(inode);
3589 free_xid(xid);
3590 return rc;
3591 }
3592
3593 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
3594 loff_t off, loff_t len)
3595 {
3596 int rc;
3597 unsigned int xid;
3598 struct cifsFileInfo *cfile = file->private_data;
3599 struct inode *inode = file_inode(file);
3600 __u64 count, old_eof, new_eof;
3601
3602 xid = get_xid();
3603
3604 inode_lock(inode);
3605
3606 old_eof = i_size_read(inode);
3607 if (off >= old_eof) {
3608 rc = -EINVAL;
3609 goto out;
3610 }
3611
3612 count = old_eof - off;
3613 new_eof = old_eof + len;
3614
3615 filemap_invalidate_lock(inode->i_mapping);
3616 rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1);
3617 if (rc < 0)
3618 goto out_2;
3619 truncate_pagecache_range(inode, off, old_eof);
3620
3621 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3622 cfile->fid.volatile_fid, cfile->pid, new_eof);
3623 if (rc < 0)
3624 goto out_2;
3625
3626 truncate_setsize(inode, new_eof);
3627 fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode));
3628
3629 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
3630 if (rc < 0)
3631 goto out_2;
3632
3633 rc = smb3_zero_data(file, tcon, off, len, xid);
3634 if (rc < 0)
3635 goto out_2;
3636
3637 rc = 0;
3638 out_2:
3639 filemap_invalidate_unlock(inode->i_mapping);
3640 out:
3641 inode_unlock(inode);
3642 free_xid(xid);
3643 return rc;
3644 }
3645
3646 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3647 {
3648 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3649 struct cifsInodeInfo *cifsi;
3650 struct inode *inode;
3651 int rc = 0;
3652 struct file_allocated_range_buffer in_data, *out_data = NULL;
3653 u32 out_data_len;
3654 unsigned int xid;
3655
3656 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3657 return generic_file_llseek(file, offset, whence);
3658
3659 inode = d_inode(cfile->dentry);
3660 cifsi = CIFS_I(inode);
3661
3662 if (offset < 0 || offset >= i_size_read(inode))
3663 return -ENXIO;
3664
3665 xid = get_xid();
3666 /*
3667 * We need to be sure that all dirty pages are written as they
3668 * might fill holes on the server.
3669 * Note that we also MUST flush any written pages since at least
3670 * some servers (Windows2016) will not reflect recent writes in
3671 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3672 */
3673 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3674 if (wrcfile) {
3675 filemap_write_and_wait(inode->i_mapping);
3676 smb2_flush_file(xid, tcon, &wrcfile->fid);
3677 cifsFileInfo_put(wrcfile);
3678 }
3679
3680 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3681 if (whence == SEEK_HOLE)
3682 offset = i_size_read(inode);
3683 goto lseek_exit;
3684 }
3685
3686 in_data.file_offset = cpu_to_le64(offset);
3687 in_data.length = cpu_to_le64(i_size_read(inode));
3688
3689 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3690 cfile->fid.volatile_fid,
3691 FSCTL_QUERY_ALLOCATED_RANGES,
3692 (char *)&in_data, sizeof(in_data),
3693 sizeof(struct file_allocated_range_buffer),
3694 (char **)&out_data, &out_data_len);
3695 if (rc == -E2BIG)
3696 rc = 0;
3697 if (rc)
3698 goto lseek_exit;
3699
3700 if (whence == SEEK_HOLE && out_data_len == 0)
3701 goto lseek_exit;
3702
3703 if (whence == SEEK_DATA && out_data_len == 0) {
3704 rc = -ENXIO;
3705 goto lseek_exit;
3706 }
3707
3708 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3709 rc = -EINVAL;
3710 goto lseek_exit;
3711 }
3712 if (whence == SEEK_DATA) {
3713 offset = le64_to_cpu(out_data->file_offset);
3714 goto lseek_exit;
3715 }
3716 if (offset < le64_to_cpu(out_data->file_offset))
3717 goto lseek_exit;
3718
3719 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3720
3721 lseek_exit:
3722 free_xid(xid);
3723 kfree(out_data);
3724 if (!rc)
3725 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3726 else
3727 return rc;
3728 }
3729
3730 static int smb3_fiemap(struct cifs_tcon *tcon,
3731 struct cifsFileInfo *cfile,
3732 struct fiemap_extent_info *fei, u64 start, u64 len)
3733 {
3734 unsigned int xid;
3735 struct file_allocated_range_buffer in_data, *out_data;
3736 u32 out_data_len;
3737 int i, num, rc, flags, last_blob;
3738 u64 next;
3739
3740 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3741 if (rc)
3742 return rc;
3743
3744 xid = get_xid();
3745 again:
3746 in_data.file_offset = cpu_to_le64(start);
3747 in_data.length = cpu_to_le64(len);
3748
3749 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3750 cfile->fid.volatile_fid,
3751 FSCTL_QUERY_ALLOCATED_RANGES,
3752 (char *)&in_data, sizeof(in_data),
3753 1024 * sizeof(struct file_allocated_range_buffer),
3754 (char **)&out_data, &out_data_len);
3755 if (rc == -E2BIG) {
3756 last_blob = 0;
3757 rc = 0;
3758 } else
3759 last_blob = 1;
3760 if (rc)
3761 goto out;
3762
3763 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3764 rc = -EINVAL;
3765 goto out;
3766 }
3767 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3768 rc = -EINVAL;
3769 goto out;
3770 }
3771
3772 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3773 for (i = 0; i < num; i++) {
3774 flags = 0;
3775 if (i == num - 1 && last_blob)
3776 flags |= FIEMAP_EXTENT_LAST;
3777
3778 rc = fiemap_fill_next_extent(fei,
3779 le64_to_cpu(out_data[i].file_offset),
3780 le64_to_cpu(out_data[i].file_offset),
3781 le64_to_cpu(out_data[i].length),
3782 flags);
3783 if (rc < 0)
3784 goto out;
3785 if (rc == 1) {
3786 rc = 0;
3787 goto out;
3788 }
3789 }
3790
3791 if (!last_blob) {
3792 next = le64_to_cpu(out_data[num - 1].file_offset) +
3793 le64_to_cpu(out_data[num - 1].length);
3794 len = len - (next - start);
3795 start = next;
3796 goto again;
3797 }
3798
3799 out:
3800 free_xid(xid);
3801 kfree(out_data);
3802 return rc;
3803 }
3804
3805 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3806 loff_t off, loff_t len)
3807 {
3808 /* KEEP_SIZE already checked for by do_fallocate */
3809 if (mode & FALLOC_FL_PUNCH_HOLE)
3810 return smb3_punch_hole(file, tcon, off, len);
3811 else if (mode & FALLOC_FL_ZERO_RANGE) {
3812 if (mode & FALLOC_FL_KEEP_SIZE)
3813 return smb3_zero_range(file, tcon, off, len, true);
3814 return smb3_zero_range(file, tcon, off, len, false);
3815 } else if (mode == FALLOC_FL_KEEP_SIZE)
3816 return smb3_simple_falloc(file, tcon, off, len, true);
3817 else if (mode == FALLOC_FL_COLLAPSE_RANGE)
3818 return smb3_collapse_range(file, tcon, off, len);
3819 else if (mode == FALLOC_FL_INSERT_RANGE)
3820 return smb3_insert_range(file, tcon, off, len);
3821 else if (mode == 0)
3822 return smb3_simple_falloc(file, tcon, off, len, false);
3823
3824 return -EOPNOTSUPP;
3825 }
3826
3827 static void
3828 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3829 struct cifsInodeInfo *cinode, __u32 oplock,
3830 unsigned int epoch, bool *purge_cache)
3831 {
3832 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3833 }
3834
3835 static void
3836 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3837 unsigned int epoch, bool *purge_cache);
3838
3839 static void
3840 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3841 struct cifsInodeInfo *cinode, __u32 oplock,
3842 unsigned int epoch, bool *purge_cache)
3843 {
3844 unsigned int old_state = cinode->oplock;
3845 unsigned int old_epoch = cinode->epoch;
3846 unsigned int new_state;
3847
3848 if (epoch > old_epoch) {
3849 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3850 cinode->epoch = epoch;
3851 }
3852
3853 new_state = cinode->oplock;
3854 *purge_cache = false;
3855
3856 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3857 (new_state & CIFS_CACHE_READ_FLG) == 0)
3858 *purge_cache = true;
3859 else if (old_state == new_state && (epoch - old_epoch > 1))
3860 *purge_cache = true;
3861 }
3862
3863 static void
3864 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3865 unsigned int epoch, bool *purge_cache)
3866 {
3867 oplock &= 0xFF;
3868 cinode->lease_granted = false;
3869 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3870 return;
3871 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3872 cinode->oplock = CIFS_CACHE_RHW_FLG;
3873 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3874 &cinode->netfs.inode);
3875 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3876 cinode->oplock = CIFS_CACHE_RW_FLG;
3877 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3878 &cinode->netfs.inode);
3879 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3880 cinode->oplock = CIFS_CACHE_READ_FLG;
3881 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3882 &cinode->netfs.inode);
3883 } else
3884 cinode->oplock = 0;
3885 }
3886
3887 static void
3888 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3889 unsigned int epoch, bool *purge_cache)
3890 {
3891 char message[5] = {0};
3892 unsigned int new_oplock = 0;
3893
3894 oplock &= 0xFF;
3895 cinode->lease_granted = true;
3896 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3897 return;
3898
3899 /* Check if the server granted an oplock rather than a lease */
3900 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3901 return smb2_set_oplock_level(cinode, oplock, epoch,
3902 purge_cache);
3903
3904 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3905 new_oplock |= CIFS_CACHE_READ_FLG;
3906 strcat(message, "R");
3907 }
3908 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3909 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3910 strcat(message, "H");
3911 }
3912 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3913 new_oplock |= CIFS_CACHE_WRITE_FLG;
3914 strcat(message, "W");
3915 }
3916 if (!new_oplock)
3917 strncpy(message, "None", sizeof(message));
3918
3919 cinode->oplock = new_oplock;
3920 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3921 &cinode->netfs.inode);
3922 }
3923
3924 static void
3925 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3926 unsigned int epoch, bool *purge_cache)
3927 {
3928 unsigned int old_oplock = cinode->oplock;
3929
3930 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3931
3932 if (purge_cache) {
3933 *purge_cache = false;
3934 if (old_oplock == CIFS_CACHE_READ_FLG) {
3935 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3936 (epoch - cinode->epoch > 0))
3937 *purge_cache = true;
3938 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3939 (epoch - cinode->epoch > 1))
3940 *purge_cache = true;
3941 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3942 (epoch - cinode->epoch > 1))
3943 *purge_cache = true;
3944 else if (cinode->oplock == 0 &&
3945 (epoch - cinode->epoch > 0))
3946 *purge_cache = true;
3947 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3948 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3949 (epoch - cinode->epoch > 0))
3950 *purge_cache = true;
3951 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3952 (epoch - cinode->epoch > 1))
3953 *purge_cache = true;
3954 }
3955 cinode->epoch = epoch;
3956 }
3957 }
3958
3959 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3960 static bool
3961 smb2_is_read_op(__u32 oplock)
3962 {
3963 return oplock == SMB2_OPLOCK_LEVEL_II;
3964 }
3965 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
3966
3967 static bool
3968 smb21_is_read_op(__u32 oplock)
3969 {
3970 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3971 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3972 }
3973
3974 static __le32
3975 map_oplock_to_lease(u8 oplock)
3976 {
3977 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3978 return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE;
3979 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3980 return SMB2_LEASE_READ_CACHING_LE;
3981 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3982 return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE |
3983 SMB2_LEASE_WRITE_CACHING_LE;
3984 return 0;
3985 }
3986
3987 static char *
3988 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3989 {
3990 struct create_lease *buf;
3991
3992 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3993 if (!buf)
3994 return NULL;
3995
3996 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3997 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3998
3999 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4000 (struct create_lease, lcontext));
4001 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4002 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4003 (struct create_lease, Name));
4004 buf->ccontext.NameLength = cpu_to_le16(4);
4005 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4006 buf->Name[0] = 'R';
4007 buf->Name[1] = 'q';
4008 buf->Name[2] = 'L';
4009 buf->Name[3] = 's';
4010 return (char *)buf;
4011 }
4012
4013 static char *
4014 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4015 {
4016 struct create_lease_v2 *buf;
4017
4018 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4019 if (!buf)
4020 return NULL;
4021
4022 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4023 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4024
4025 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4026 (struct create_lease_v2, lcontext));
4027 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4028 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4029 (struct create_lease_v2, Name));
4030 buf->ccontext.NameLength = cpu_to_le16(4);
4031 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4032 buf->Name[0] = 'R';
4033 buf->Name[1] = 'q';
4034 buf->Name[2] = 'L';
4035 buf->Name[3] = 's';
4036 return (char *)buf;
4037 }
4038
4039 static __u8
4040 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4041 {
4042 struct create_lease *lc = (struct create_lease *)buf;
4043
4044 *epoch = 0; /* not used */
4045 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4046 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4047 return le32_to_cpu(lc->lcontext.LeaseState);
4048 }
4049
4050 static __u8
4051 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4052 {
4053 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4054
4055 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4056 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4057 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4058 if (lease_key)
4059 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4060 return le32_to_cpu(lc->lcontext.LeaseState);
4061 }
4062
4063 static unsigned int
4064 smb2_wp_retry_size(struct inode *inode)
4065 {
4066 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4067 SMB2_MAX_BUFFER_SIZE);
4068 }
4069
4070 static bool
4071 smb2_dir_needs_close(struct cifsFileInfo *cfile)
4072 {
4073 return !cfile->invalidHandle;
4074 }
4075
4076 static void
4077 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4078 struct smb_rqst *old_rq, __le16 cipher_type)
4079 {
4080 struct smb2_hdr *shdr =
4081 (struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
4082
4083 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4084 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4085 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4086 tr_hdr->Flags = cpu_to_le16(0x01);
4087 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4088 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4089 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4090 else
4091 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4092 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4093 }
4094
4095 static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
4096 int num_rqst, const u8 *sig, u8 **iv,
4097 struct aead_request **req, struct sg_table *sgt,
4098 unsigned int *num_sgs, size_t *sensitive_size)
4099 {
4100 unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
4101 unsigned int iv_size = crypto_aead_ivsize(tfm);
4102 unsigned int len;
4103 u8 *p;
4104
4105 *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
4106 if (IS_ERR_VALUE((long)(int)*num_sgs))
4107 return ERR_PTR(*num_sgs);
4108
4109 len = iv_size;
4110 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
4111 len = ALIGN(len, crypto_tfm_ctx_alignment());
4112 len += req_size;
4113 len = ALIGN(len, __alignof__(struct scatterlist));
4114 len += array_size(*num_sgs, sizeof(struct scatterlist));
4115 *sensitive_size = len;
4116
4117 p = kvzalloc(len, GFP_NOFS);
4118 if (!p)
4119 return ERR_PTR(-ENOMEM);
4120
4121 *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
4122 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
4123 crypto_tfm_ctx_alignment());
4124 sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
4125 __alignof__(struct scatterlist));
4126 return p;
4127 }
4128
4129 static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
4130 int num_rqst, const u8 *sig, u8 **iv,
4131 struct aead_request **req, struct scatterlist **sgl,
4132 size_t *sensitive_size)
4133 {
4134 struct sg_table sgtable = {};
4135 unsigned int skip, num_sgs, i, j;
4136 ssize_t rc;
4137 void *p;
4138
4139 p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable,
4140 &num_sgs, sensitive_size);
4141 if (IS_ERR(p))
4142 return ERR_CAST(p);
4143
4144 sg_init_marker(sgtable.sgl, num_sgs);
4145
4146 /*
4147 * The first rqst has a transform header where the
4148 * first 20 bytes are not part of the encrypted blob.
4149 */
4150 skip = 20;
4151
4152 for (i = 0; i < num_rqst; i++) {
4153 struct iov_iter *iter = &rqst[i].rq_iter;
4154 size_t count = iov_iter_count(iter);
4155
4156 for (j = 0; j < rqst[i].rq_nvec; j++) {
4157 cifs_sg_set_buf(&sgtable,
4158 rqst[i].rq_iov[j].iov_base + skip,
4159 rqst[i].rq_iov[j].iov_len - skip);
4160
4161 /* See the above comment on the 'skip' assignment */
4162 skip = 0;
4163 }
4164 sgtable.orig_nents = sgtable.nents;
4165
4166 rc = extract_iter_to_sg(iter, count, &sgtable,
4167 num_sgs - sgtable.nents, 0);
4168 iov_iter_revert(iter, rc);
4169 sgtable.orig_nents = sgtable.nents;
4170 }
4171
4172 cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
4173 sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
4174 *sgl = sgtable.sgl;
4175 return p;
4176 }
4177
4178 static int
4179 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4180 {
4181 struct TCP_Server_Info *pserver;
4182 struct cifs_ses *ses;
4183 u8 *ses_enc_key;
4184
4185 /* If server is a channel, select the primary channel */
4186 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4187
4188 spin_lock(&cifs_tcp_ses_lock);
4189 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4190 if (ses->Suid == ses_id) {
4191 spin_lock(&ses->ses_lock);
4192 ses_enc_key = enc ? ses->smb3encryptionkey :
4193 ses->smb3decryptionkey;
4194 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4195 spin_unlock(&ses->ses_lock);
4196 spin_unlock(&cifs_tcp_ses_lock);
4197 return 0;
4198 }
4199 }
4200 spin_unlock(&cifs_tcp_ses_lock);
4201
4202 trace_smb3_ses_not_found(ses_id);
4203
4204 return -EAGAIN;
4205 }
4206 /*
4207 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4208 * iov[0] - transform header (associate data),
4209 * iov[1-N] - SMB2 header and pages - data to encrypt.
4210 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4211 * untouched.
4212 */
4213 static int
4214 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4215 struct smb_rqst *rqst, int enc)
4216 {
4217 struct smb2_transform_hdr *tr_hdr =
4218 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4219 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4220 int rc = 0;
4221 struct scatterlist *sg;
4222 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4223 u8 key[SMB3_ENC_DEC_KEY_SIZE];
4224 struct aead_request *req;
4225 u8 *iv;
4226 DECLARE_CRYPTO_WAIT(wait);
4227 struct crypto_aead *tfm;
4228 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4229 void *creq;
4230 size_t sensitive_size;
4231
4232 rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
4233 if (rc) {
4234 cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
4235 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
4236 return rc;
4237 }
4238
4239 rc = smb3_crypto_aead_allocate(server);
4240 if (rc) {
4241 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4242 return rc;
4243 }
4244
4245 tfm = enc ? server->secmech.enc : server->secmech.dec;
4246
4247 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4248 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4249 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4250 else
4251 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4252
4253 if (rc) {
4254 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4255 return rc;
4256 }
4257
4258 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4259 if (rc) {
4260 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4261 return rc;
4262 }
4263
4264 creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg,
4265 &sensitive_size);
4266 if (IS_ERR(creq))
4267 return PTR_ERR(creq);
4268
4269 if (!enc) {
4270 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4271 crypt_len += SMB2_SIGNATURE_SIZE;
4272 }
4273
4274 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4275 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4276 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4277 else {
4278 iv[0] = 3;
4279 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4280 }
4281
4282 aead_request_set_tfm(req, tfm);
4283 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4284 aead_request_set_ad(req, assoc_data_len);
4285
4286 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4287 crypto_req_done, &wait);
4288
4289 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4290 : crypto_aead_decrypt(req), &wait);
4291
4292 if (!rc && enc)
4293 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4294
4295 kvfree_sensitive(creq, sensitive_size);
4296 return rc;
4297 }
4298
4299 /*
4300 * Clear a read buffer, discarding the folios which have XA_MARK_0 set.
4301 */
4302 static void cifs_clear_xarray_buffer(struct xarray *buffer)
4303 {
4304 struct folio *folio;
4305
4306 XA_STATE(xas, buffer, 0);
4307
4308 rcu_read_lock();
4309 xas_for_each_marked(&xas, folio, ULONG_MAX, XA_MARK_0) {
4310 folio_put(folio);
4311 }
4312 rcu_read_unlock();
4313 xa_destroy(buffer);
4314 }
4315
4316 void
4317 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4318 {
4319 int i;
4320
4321 for (i = 0; i < num_rqst; i++)
4322 if (!xa_empty(&rqst[i].rq_buffer))
4323 cifs_clear_xarray_buffer(&rqst[i].rq_buffer);
4324 }
4325
4326 /*
4327 * This function will initialize new_rq and encrypt the content.
4328 * The first entry, new_rq[0], only contains a single iov which contains
4329 * a smb2_transform_hdr and is pre-allocated by the caller.
4330 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4331 *
4332 * The end result is an array of smb_rqst structures where the first structure
4333 * only contains a single iov for the transform header which we then can pass
4334 * to crypt_message().
4335 *
4336 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4337 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4338 */
4339 static int
4340 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4341 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4342 {
4343 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4344 struct page *page;
4345 unsigned int orig_len = 0;
4346 int i, j;
4347 int rc = -ENOMEM;
4348
4349 for (i = 1; i < num_rqst; i++) {
4350 struct smb_rqst *old = &old_rq[i - 1];
4351 struct smb_rqst *new = &new_rq[i];
4352 struct xarray *buffer = &new->rq_buffer;
4353 size_t size = iov_iter_count(&old->rq_iter), seg, copied = 0;
4354
4355 orig_len += smb_rqst_len(server, old);
4356 new->rq_iov = old->rq_iov;
4357 new->rq_nvec = old->rq_nvec;
4358
4359 xa_init(buffer);
4360
4361 if (size > 0) {
4362 unsigned int npages = DIV_ROUND_UP(size, PAGE_SIZE);
4363
4364 for (j = 0; j < npages; j++) {
4365 void *o;
4366
4367 rc = -ENOMEM;
4368 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4369 if (!page)
4370 goto err_free;
4371 page->index = j;
4372 o = xa_store(buffer, j, page, GFP_KERNEL);
4373 if (xa_is_err(o)) {
4374 rc = xa_err(o);
4375 put_page(page);
4376 goto err_free;
4377 }
4378
4379 xa_set_mark(buffer, j, XA_MARK_0);
4380
4381 seg = min_t(size_t, size - copied, PAGE_SIZE);
4382 if (copy_page_from_iter(page, 0, seg, &old->rq_iter) != seg) {
4383 rc = -EFAULT;
4384 goto err_free;
4385 }
4386 copied += seg;
4387 }
4388 iov_iter_xarray(&new->rq_iter, ITER_SOURCE,
4389 buffer, 0, size);
4390 new->rq_iter_size = size;
4391 }
4392 }
4393
4394 /* fill the 1st iov with a transform header */
4395 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4396
4397 rc = crypt_message(server, num_rqst, new_rq, 1);
4398 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4399 if (rc)
4400 goto err_free;
4401
4402 return rc;
4403
4404 err_free:
4405 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4406 return rc;
4407 }
4408
4409 static int
4410 smb3_is_transform_hdr(void *buf)
4411 {
4412 struct smb2_transform_hdr *trhdr = buf;
4413
4414 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4415 }
4416
4417 static int
4418 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4419 unsigned int buf_data_size, struct iov_iter *iter,
4420 bool is_offloaded)
4421 {
4422 struct kvec iov[2];
4423 struct smb_rqst rqst = {NULL};
4424 size_t iter_size = 0;
4425 int rc;
4426
4427 iov[0].iov_base = buf;
4428 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4429 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4430 iov[1].iov_len = buf_data_size;
4431
4432 rqst.rq_iov = iov;
4433 rqst.rq_nvec = 2;
4434 if (iter) {
4435 rqst.rq_iter = *iter;
4436 rqst.rq_iter_size = iov_iter_count(iter);
4437 iter_size = iov_iter_count(iter);
4438 }
4439
4440 rc = crypt_message(server, 1, &rqst, 0);
4441 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4442
4443 if (rc)
4444 return rc;
4445
4446 memmove(buf, iov[1].iov_base, buf_data_size);
4447
4448 if (!is_offloaded)
4449 server->total_read = buf_data_size + iter_size;
4450
4451 return rc;
4452 }
4453
4454 static int
4455 cifs_copy_pages_to_iter(struct xarray *pages, unsigned int data_size,
4456 unsigned int skip, struct iov_iter *iter)
4457 {
4458 struct page *page;
4459 unsigned long index;
4460
4461 xa_for_each(pages, index, page) {
4462 size_t n, len = min_t(unsigned int, PAGE_SIZE - skip, data_size);
4463
4464 n = copy_page_to_iter(page, skip, len, iter);
4465 if (n != len) {
4466 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4467 return -EIO;
4468 }
4469 data_size -= n;
4470 skip = 0;
4471 }
4472
4473 return 0;
4474 }
4475
4476 static int
4477 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4478 char *buf, unsigned int buf_len, struct xarray *pages,
4479 unsigned int pages_len, bool is_offloaded)
4480 {
4481 unsigned int data_offset;
4482 unsigned int data_len;
4483 unsigned int cur_off;
4484 unsigned int cur_page_idx;
4485 unsigned int pad_len;
4486 struct cifs_readdata *rdata = mid->callback_data;
4487 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
4488 int length;
4489 bool use_rdma_mr = false;
4490
4491 if (shdr->Command != SMB2_READ) {
4492 cifs_server_dbg(VFS, "only big read responses are supported\n");
4493 return -EOPNOTSUPP;
4494 }
4495
4496 if (server->ops->is_session_expired &&
4497 server->ops->is_session_expired(buf)) {
4498 if (!is_offloaded)
4499 cifs_reconnect(server, true);
4500 return -1;
4501 }
4502
4503 if (server->ops->is_status_pending &&
4504 server->ops->is_status_pending(buf, server))
4505 return -1;
4506
4507 /* set up first two iov to get credits */
4508 rdata->iov[0].iov_base = buf;
4509 rdata->iov[0].iov_len = 0;
4510 rdata->iov[1].iov_base = buf;
4511 rdata->iov[1].iov_len =
4512 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4513 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4514 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4515 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4516 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4517
4518 rdata->result = server->ops->map_error(buf, true);
4519 if (rdata->result != 0) {
4520 cifs_dbg(FYI, "%s: server returned error %d\n",
4521 __func__, rdata->result);
4522 /* normal error on read response */
4523 if (is_offloaded)
4524 mid->mid_state = MID_RESPONSE_RECEIVED;
4525 else
4526 dequeue_mid(mid, false);
4527 return 0;
4528 }
4529
4530 data_offset = server->ops->read_data_offset(buf);
4531 #ifdef CONFIG_CIFS_SMB_DIRECT
4532 use_rdma_mr = rdata->mr;
4533 #endif
4534 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4535
4536 if (data_offset < server->vals->read_rsp_size) {
4537 /*
4538 * win2k8 sometimes sends an offset of 0 when the read
4539 * is beyond the EOF. Treat it as if the data starts just after
4540 * the header.
4541 */
4542 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4543 __func__, data_offset);
4544 data_offset = server->vals->read_rsp_size;
4545 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4546 /* data_offset is beyond the end of smallbuf */
4547 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4548 __func__, data_offset);
4549 rdata->result = -EIO;
4550 if (is_offloaded)
4551 mid->mid_state = MID_RESPONSE_MALFORMED;
4552 else
4553 dequeue_mid(mid, rdata->result);
4554 return 0;
4555 }
4556
4557 pad_len = data_offset - server->vals->read_rsp_size;
4558
4559 if (buf_len <= data_offset) {
4560 /* read response payload is in pages */
4561 cur_page_idx = pad_len / PAGE_SIZE;
4562 cur_off = pad_len % PAGE_SIZE;
4563
4564 if (cur_page_idx != 0) {
4565 /* data offset is beyond the 1st page of response */
4566 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4567 __func__, data_offset);
4568 rdata->result = -EIO;
4569 if (is_offloaded)
4570 mid->mid_state = MID_RESPONSE_MALFORMED;
4571 else
4572 dequeue_mid(mid, rdata->result);
4573 return 0;
4574 }
4575
4576 if (data_len > pages_len - pad_len) {
4577 /* data_len is corrupt -- discard frame */
4578 rdata->result = -EIO;
4579 if (is_offloaded)
4580 mid->mid_state = MID_RESPONSE_MALFORMED;
4581 else
4582 dequeue_mid(mid, rdata->result);
4583 return 0;
4584 }
4585
4586 /* Copy the data to the output I/O iterator. */
4587 rdata->result = cifs_copy_pages_to_iter(pages, pages_len,
4588 cur_off, &rdata->iter);
4589 if (rdata->result != 0) {
4590 if (is_offloaded)
4591 mid->mid_state = MID_RESPONSE_MALFORMED;
4592 else
4593 dequeue_mid(mid, rdata->result);
4594 return 0;
4595 }
4596 rdata->got_bytes = pages_len;
4597
4598 } else if (buf_len >= data_offset + data_len) {
4599 /* read response payload is in buf */
4600 WARN_ONCE(pages && !xa_empty(pages),
4601 "read data can be either in buf or in pages");
4602 length = copy_to_iter(buf + data_offset, data_len, &rdata->iter);
4603 if (length < 0)
4604 return length;
4605 rdata->got_bytes = data_len;
4606 } else {
4607 /* read response payload cannot be in both buf and pages */
4608 WARN_ONCE(1, "buf can not contain only a part of read data");
4609 rdata->result = -EIO;
4610 if (is_offloaded)
4611 mid->mid_state = MID_RESPONSE_MALFORMED;
4612 else
4613 dequeue_mid(mid, rdata->result);
4614 return 0;
4615 }
4616
4617 if (is_offloaded)
4618 mid->mid_state = MID_RESPONSE_RECEIVED;
4619 else
4620 dequeue_mid(mid, false);
4621 return 0;
4622 }
4623
4624 struct smb2_decrypt_work {
4625 struct work_struct decrypt;
4626 struct TCP_Server_Info *server;
4627 struct xarray buffer;
4628 char *buf;
4629 unsigned int len;
4630 };
4631
4632
4633 static void smb2_decrypt_offload(struct work_struct *work)
4634 {
4635 struct smb2_decrypt_work *dw = container_of(work,
4636 struct smb2_decrypt_work, decrypt);
4637 int rc;
4638 struct mid_q_entry *mid;
4639 struct iov_iter iter;
4640
4641 iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, dw->len);
4642 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4643 &iter, true);
4644 if (rc) {
4645 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4646 goto free_pages;
4647 }
4648
4649 dw->server->lstrp = jiffies;
4650 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4651 if (mid == NULL)
4652 cifs_dbg(FYI, "mid not found\n");
4653 else {
4654 mid->decrypted = true;
4655 rc = handle_read_data(dw->server, mid, dw->buf,
4656 dw->server->vals->read_rsp_size,
4657 &dw->buffer, dw->len,
4658 true);
4659 if (rc >= 0) {
4660 #ifdef CONFIG_CIFS_STATS2
4661 mid->when_received = jiffies;
4662 #endif
4663 if (dw->server->ops->is_network_name_deleted)
4664 dw->server->ops->is_network_name_deleted(dw->buf,
4665 dw->server);
4666
4667 mid->callback(mid);
4668 } else {
4669 spin_lock(&dw->server->srv_lock);
4670 if (dw->server->tcpStatus == CifsNeedReconnect) {
4671 spin_lock(&dw->server->mid_lock);
4672 mid->mid_state = MID_RETRY_NEEDED;
4673 spin_unlock(&dw->server->mid_lock);
4674 spin_unlock(&dw->server->srv_lock);
4675 mid->callback(mid);
4676 } else {
4677 spin_lock(&dw->server->mid_lock);
4678 mid->mid_state = MID_REQUEST_SUBMITTED;
4679 mid->mid_flags &= ~(MID_DELETED);
4680 list_add_tail(&mid->qhead,
4681 &dw->server->pending_mid_q);
4682 spin_unlock(&dw->server->mid_lock);
4683 spin_unlock(&dw->server->srv_lock);
4684 }
4685 }
4686 release_mid(mid);
4687 }
4688
4689 free_pages:
4690 cifs_clear_xarray_buffer(&dw->buffer);
4691 cifs_small_buf_release(dw->buf);
4692 kfree(dw);
4693 }
4694
4695
4696 static int
4697 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4698 int *num_mids)
4699 {
4700 struct page *page;
4701 char *buf = server->smallbuf;
4702 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4703 struct iov_iter iter;
4704 unsigned int len, npages;
4705 unsigned int buflen = server->pdu_size;
4706 int rc;
4707 int i = 0;
4708 struct smb2_decrypt_work *dw;
4709
4710 dw = kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4711 if (!dw)
4712 return -ENOMEM;
4713 xa_init(&dw->buffer);
4714 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4715 dw->server = server;
4716
4717 *num_mids = 1;
4718 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4719 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4720
4721 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4722 if (rc < 0)
4723 goto free_dw;
4724 server->total_read += rc;
4725
4726 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4727 server->vals->read_rsp_size;
4728 dw->len = len;
4729 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4730
4731 rc = -ENOMEM;
4732 for (; i < npages; i++) {
4733 void *old;
4734
4735 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4736 if (!page)
4737 goto discard_data;
4738 page->index = i;
4739 old = xa_store(&dw->buffer, i, page, GFP_KERNEL);
4740 if (xa_is_err(old)) {
4741 rc = xa_err(old);
4742 put_page(page);
4743 goto discard_data;
4744 }
4745 xa_set_mark(&dw->buffer, i, XA_MARK_0);
4746 }
4747
4748 iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, npages * PAGE_SIZE);
4749
4750 /* Read the data into the buffer and clear excess bufferage. */
4751 rc = cifs_read_iter_from_socket(server, &iter, dw->len);
4752 if (rc < 0)
4753 goto discard_data;
4754
4755 server->total_read += rc;
4756 if (rc < npages * PAGE_SIZE)
4757 iov_iter_zero(npages * PAGE_SIZE - rc, &iter);
4758 iov_iter_revert(&iter, npages * PAGE_SIZE);
4759 iov_iter_truncate(&iter, dw->len);
4760
4761 rc = cifs_discard_remaining_data(server);
4762 if (rc)
4763 goto free_pages;
4764
4765 /*
4766 * For large reads, offload to different thread for better performance,
4767 * use more cores decrypting which can be expensive
4768 */
4769
4770 if ((server->min_offload) && (server->in_flight > 1) &&
4771 (server->pdu_size >= server->min_offload)) {
4772 dw->buf = server->smallbuf;
4773 server->smallbuf = (char *)cifs_small_buf_get();
4774
4775 queue_work(decrypt_wq, &dw->decrypt);
4776 *num_mids = 0; /* worker thread takes care of finding mid */
4777 return -1;
4778 }
4779
4780 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4781 &iter, false);
4782 if (rc)
4783 goto free_pages;
4784
4785 *mid = smb2_find_mid(server, buf);
4786 if (*mid == NULL) {
4787 cifs_dbg(FYI, "mid not found\n");
4788 } else {
4789 cifs_dbg(FYI, "mid found\n");
4790 (*mid)->decrypted = true;
4791 rc = handle_read_data(server, *mid, buf,
4792 server->vals->read_rsp_size,
4793 &dw->buffer, dw->len, false);
4794 if (rc >= 0) {
4795 if (server->ops->is_network_name_deleted) {
4796 server->ops->is_network_name_deleted(buf,
4797 server);
4798 }
4799 }
4800 }
4801
4802 free_pages:
4803 cifs_clear_xarray_buffer(&dw->buffer);
4804 free_dw:
4805 kfree(dw);
4806 return rc;
4807 discard_data:
4808 cifs_discard_remaining_data(server);
4809 goto free_pages;
4810 }
4811
4812 static int
4813 receive_encrypted_standard(struct TCP_Server_Info *server,
4814 struct mid_q_entry **mids, char **bufs,
4815 int *num_mids)
4816 {
4817 int ret, length;
4818 char *buf = server->smallbuf;
4819 struct smb2_hdr *shdr;
4820 unsigned int pdu_length = server->pdu_size;
4821 unsigned int buf_size;
4822 unsigned int next_cmd;
4823 struct mid_q_entry *mid_entry;
4824 int next_is_large;
4825 char *next_buffer = NULL;
4826
4827 *num_mids = 0;
4828
4829 /* switch to large buffer if too big for a small one */
4830 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4831 server->large_buf = true;
4832 memcpy(server->bigbuf, buf, server->total_read);
4833 buf = server->bigbuf;
4834 }
4835
4836 /* now read the rest */
4837 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4838 pdu_length - HEADER_SIZE(server) + 1);
4839 if (length < 0)
4840 return length;
4841 server->total_read += length;
4842
4843 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4844 length = decrypt_raw_data(server, buf, buf_size, NULL, false);
4845 if (length)
4846 return length;
4847
4848 next_is_large = server->large_buf;
4849 one_more:
4850 shdr = (struct smb2_hdr *)buf;
4851 next_cmd = le32_to_cpu(shdr->NextCommand);
4852 if (next_cmd) {
4853 if (WARN_ON_ONCE(next_cmd > pdu_length))
4854 return -1;
4855 if (next_is_large)
4856 next_buffer = (char *)cifs_buf_get();
4857 else
4858 next_buffer = (char *)cifs_small_buf_get();
4859 memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd);
4860 }
4861
4862 mid_entry = smb2_find_mid(server, buf);
4863 if (mid_entry == NULL)
4864 cifs_dbg(FYI, "mid not found\n");
4865 else {
4866 cifs_dbg(FYI, "mid found\n");
4867 mid_entry->decrypted = true;
4868 mid_entry->resp_buf_size = server->pdu_size;
4869 }
4870
4871 if (*num_mids >= MAX_COMPOUND) {
4872 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4873 return -1;
4874 }
4875 bufs[*num_mids] = buf;
4876 mids[(*num_mids)++] = mid_entry;
4877
4878 if (mid_entry && mid_entry->handle)
4879 ret = mid_entry->handle(server, mid_entry);
4880 else
4881 ret = cifs_handle_standard(server, mid_entry);
4882
4883 if (ret == 0 && next_cmd) {
4884 pdu_length -= next_cmd;
4885 server->large_buf = next_is_large;
4886 if (next_is_large)
4887 server->bigbuf = buf = next_buffer;
4888 else
4889 server->smallbuf = buf = next_buffer;
4890 goto one_more;
4891 } else if (ret != 0) {
4892 /*
4893 * ret != 0 here means that we didn't get to handle_mid() thus
4894 * server->smallbuf and server->bigbuf are still valid. We need
4895 * to free next_buffer because it is not going to be used
4896 * anywhere.
4897 */
4898 if (next_is_large)
4899 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4900 else
4901 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4902 }
4903
4904 return ret;
4905 }
4906
4907 static int
4908 smb3_receive_transform(struct TCP_Server_Info *server,
4909 struct mid_q_entry **mids, char **bufs, int *num_mids)
4910 {
4911 char *buf = server->smallbuf;
4912 unsigned int pdu_length = server->pdu_size;
4913 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4914 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4915
4916 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4917 sizeof(struct smb2_hdr)) {
4918 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4919 pdu_length);
4920 cifs_reconnect(server, true);
4921 return -ECONNABORTED;
4922 }
4923
4924 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4925 cifs_server_dbg(VFS, "Transform message is broken\n");
4926 cifs_reconnect(server, true);
4927 return -ECONNABORTED;
4928 }
4929
4930 /* TODO: add support for compounds containing READ. */
4931 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4932 return receive_encrypted_read(server, &mids[0], num_mids);
4933 }
4934
4935 return receive_encrypted_standard(server, mids, bufs, num_mids);
4936 }
4937
4938 int
4939 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4940 {
4941 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4942
4943 return handle_read_data(server, mid, buf, server->pdu_size,
4944 NULL, 0, false);
4945 }
4946
4947 static int smb2_next_header(struct TCP_Server_Info *server, char *buf,
4948 unsigned int *noff)
4949 {
4950 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
4951 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4952
4953 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
4954 *noff = le32_to_cpu(t_hdr->OriginalMessageSize);
4955 if (unlikely(check_add_overflow(*noff, sizeof(*t_hdr), noff)))
4956 return -EINVAL;
4957 } else {
4958 *noff = le32_to_cpu(hdr->NextCommand);
4959 }
4960 if (unlikely(*noff && *noff < MID_HEADER_SIZE(server)))
4961 return -EINVAL;
4962 return 0;
4963 }
4964
4965 int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
4966 struct dentry *dentry, struct cifs_tcon *tcon,
4967 const char *full_path, umode_t mode, dev_t dev)
4968 {
4969 struct cifs_open_info_data buf = {};
4970 struct TCP_Server_Info *server = tcon->ses->server;
4971 struct cifs_open_parms oparms;
4972 struct cifs_io_parms io_parms = {};
4973 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4974 struct cifs_fid fid;
4975 unsigned int bytes_written;
4976 struct win_dev *pdev;
4977 struct kvec iov[2];
4978 __u32 oplock = server->oplocks ? REQ_OPLOCK : 0;
4979 int rc;
4980
4981 if (!S_ISCHR(mode) && !S_ISBLK(mode) && !S_ISFIFO(mode))
4982 return -EPERM;
4983
4984 oparms = (struct cifs_open_parms) {
4985 .tcon = tcon,
4986 .cifs_sb = cifs_sb,
4987 .desired_access = GENERIC_WRITE,
4988 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4989 CREATE_OPTION_SPECIAL),
4990 .disposition = FILE_CREATE,
4991 .path = full_path,
4992 .fid = &fid,
4993 };
4994
4995 rc = server->ops->open(xid, &oparms, &oplock, &buf);
4996 if (rc)
4997 return rc;
4998
4999 /*
5000 * BB Do not bother to decode buf since no local inode yet to put
5001 * timestamps in, but we can reuse it safely.
5002 */
5003 pdev = (struct win_dev *)&buf.fi;
5004 io_parms.pid = current->tgid;
5005 io_parms.tcon = tcon;
5006 io_parms.length = sizeof(*pdev);
5007 iov[1].iov_base = pdev;
5008 iov[1].iov_len = sizeof(*pdev);
5009 if (S_ISCHR(mode)) {
5010 memcpy(pdev->type, "IntxCHR", 8);
5011 pdev->major = cpu_to_le64(MAJOR(dev));
5012 pdev->minor = cpu_to_le64(MINOR(dev));
5013 } else if (S_ISBLK(mode)) {
5014 memcpy(pdev->type, "IntxBLK", 8);
5015 pdev->major = cpu_to_le64(MAJOR(dev));
5016 pdev->minor = cpu_to_le64(MINOR(dev));
5017 } else if (S_ISFIFO(mode)) {
5018 memcpy(pdev->type, "LnxFIFO", 8);
5019 }
5020
5021 rc = server->ops->sync_write(xid, &fid, &io_parms,
5022 &bytes_written, iov, 1);
5023 server->ops->close(xid, tcon, &fid);
5024 d_drop(dentry);
5025 /* FIXME: add code here to set EAs */
5026 cifs_free_open_info(&buf);
5027 return rc;
5028 }
5029
5030 static inline u64 mode_nfs_type(mode_t mode)
5031 {
5032 switch (mode & S_IFMT) {
5033 case S_IFBLK: return NFS_SPECFILE_BLK;
5034 case S_IFCHR: return NFS_SPECFILE_CHR;
5035 case S_IFIFO: return NFS_SPECFILE_FIFO;
5036 case S_IFSOCK: return NFS_SPECFILE_SOCK;
5037 }
5038 return 0;
5039 }
5040
5041 static int nfs_set_reparse_buf(struct reparse_posix_data *buf,
5042 mode_t mode, dev_t dev,
5043 struct kvec *iov)
5044 {
5045 u64 type;
5046 u16 len, dlen;
5047
5048 len = sizeof(*buf);
5049
5050 switch ((type = mode_nfs_type(mode))) {
5051 case NFS_SPECFILE_BLK:
5052 case NFS_SPECFILE_CHR:
5053 dlen = sizeof(__le64);
5054 break;
5055 case NFS_SPECFILE_FIFO:
5056 case NFS_SPECFILE_SOCK:
5057 dlen = 0;
5058 break;
5059 default:
5060 return -EOPNOTSUPP;
5061 }
5062
5063 buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_NFS);
5064 buf->Reserved = 0;
5065 buf->InodeType = cpu_to_le64(type);
5066 buf->ReparseDataLength = cpu_to_le16(len + dlen -
5067 sizeof(struct reparse_data_buffer));
5068 *(__le64 *)buf->DataBuffer = cpu_to_le64(((u64)MAJOR(dev) << 32) |
5069 MINOR(dev));
5070 iov->iov_base = buf;
5071 iov->iov_len = len + dlen;
5072 return 0;
5073 }
5074
5075 static int nfs_make_node(unsigned int xid, struct inode *inode,
5076 struct dentry *dentry, struct cifs_tcon *tcon,
5077 const char *full_path, umode_t mode, dev_t dev)
5078 {
5079 struct cifs_open_info_data data;
5080 struct reparse_posix_data *p;
5081 struct inode *new;
5082 struct kvec iov;
5083 __u8 buf[sizeof(*p) + sizeof(__le64)];
5084 int rc;
5085
5086 p = (struct reparse_posix_data *)buf;
5087 rc = nfs_set_reparse_buf(p, mode, dev, &iov);
5088 if (rc)
5089 return rc;
5090
5091 data = (struct cifs_open_info_data) {
5092 .reparse_point = true,
5093 .reparse = { .tag = IO_REPARSE_TAG_NFS, .posix = p, },
5094 };
5095
5096 new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
5097 tcon, full_path, &iov);
5098 if (!IS_ERR(new))
5099 d_instantiate(dentry, new);
5100 else
5101 rc = PTR_ERR(new);
5102 cifs_free_open_info(&data);
5103 return rc;
5104 }
5105
5106 static int smb2_create_reparse_symlink(const unsigned int xid,
5107 struct inode *inode,
5108 struct dentry *dentry,
5109 struct cifs_tcon *tcon,
5110 const char *full_path,
5111 const char *symname)
5112 {
5113 struct reparse_symlink_data_buffer *buf = NULL;
5114 struct cifs_open_info_data data;
5115 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5116 struct inode *new;
5117 struct kvec iov;
5118 __le16 *path;
5119 char *sym;
5120 u16 len, plen;
5121 int rc = 0;
5122
5123 sym = kstrdup(symname, GFP_KERNEL);
5124 if (!sym)
5125 return -ENOMEM;
5126
5127 data = (struct cifs_open_info_data) {
5128 .reparse_point = true,
5129 .reparse = { .tag = IO_REPARSE_TAG_SYMLINK, },
5130 .symlink_target = sym,
5131 };
5132
5133 path = cifs_convert_path_to_utf16(symname, cifs_sb);
5134 if (!path) {
5135 rc = -ENOMEM;
5136 goto out;
5137 }
5138
5139 plen = 2 * UniStrnlen((wchar_t *)path, PATH_MAX);
5140 len = sizeof(*buf) + plen * 2;
5141 buf = kzalloc(len, GFP_KERNEL);
5142 if (!buf) {
5143 rc = -ENOMEM;
5144 goto out;
5145 }
5146
5147 buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_SYMLINK);
5148 buf->ReparseDataLength = cpu_to_le16(len - sizeof(struct reparse_data_buffer));
5149 buf->SubstituteNameOffset = cpu_to_le16(plen);
5150 buf->SubstituteNameLength = cpu_to_le16(plen);
5151 memcpy(&buf->PathBuffer[plen], path, plen);
5152 buf->PrintNameOffset = 0;
5153 buf->PrintNameLength = cpu_to_le16(plen);
5154 memcpy(buf->PathBuffer, path, plen);
5155 buf->Flags = cpu_to_le32(*symname != '/' ? SYMLINK_FLAG_RELATIVE : 0);
5156
5157 iov.iov_base = buf;
5158 iov.iov_len = len;
5159 new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
5160 tcon, full_path, &iov);
5161 if (!IS_ERR(new))
5162 d_instantiate(dentry, new);
5163 else
5164 rc = PTR_ERR(new);
5165 out:
5166 kfree(path);
5167 cifs_free_open_info(&data);
5168 kfree(buf);
5169 return rc;
5170 }
5171
5172 static int smb2_make_node(unsigned int xid, struct inode *inode,
5173 struct dentry *dentry, struct cifs_tcon *tcon,
5174 const char *full_path, umode_t mode, dev_t dev)
5175 {
5176 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5177 int rc;
5178
5179 /*
5180 * Check if mounted with mount parm 'sfu' mount parm.
5181 * SFU emulation should work with all servers, but only
5182 * supports block and char device (no socket & fifo),
5183 * and was used by default in earlier versions of Windows
5184 */
5185 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
5186 rc = cifs_sfu_make_node(xid, inode, dentry, tcon,
5187 full_path, mode, dev);
5188 } else {
5189 rc = nfs_make_node(xid, inode, dentry, tcon,
5190 full_path, mode, dev);
5191 }
5192 return rc;
5193 }
5194
5195 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5196 struct smb_version_operations smb20_operations = {
5197 .compare_fids = smb2_compare_fids,
5198 .setup_request = smb2_setup_request,
5199 .setup_async_request = smb2_setup_async_request,
5200 .check_receive = smb2_check_receive,
5201 .add_credits = smb2_add_credits,
5202 .set_credits = smb2_set_credits,
5203 .get_credits_field = smb2_get_credits_field,
5204 .get_credits = smb2_get_credits,
5205 .wait_mtu_credits = cifs_wait_mtu_credits,
5206 .get_next_mid = smb2_get_next_mid,
5207 .revert_current_mid = smb2_revert_current_mid,
5208 .read_data_offset = smb2_read_data_offset,
5209 .read_data_length = smb2_read_data_length,
5210 .map_error = map_smb2_to_linux_error,
5211 .find_mid = smb2_find_mid,
5212 .check_message = smb2_check_message,
5213 .dump_detail = smb2_dump_detail,
5214 .clear_stats = smb2_clear_stats,
5215 .print_stats = smb2_print_stats,
5216 .is_oplock_break = smb2_is_valid_oplock_break,
5217 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5218 .downgrade_oplock = smb2_downgrade_oplock,
5219 .need_neg = smb2_need_neg,
5220 .negotiate = smb2_negotiate,
5221 .negotiate_wsize = smb2_negotiate_wsize,
5222 .negotiate_rsize = smb2_negotiate_rsize,
5223 .sess_setup = SMB2_sess_setup,
5224 .logoff = SMB2_logoff,
5225 .tree_connect = SMB2_tcon,
5226 .tree_disconnect = SMB2_tdis,
5227 .qfs_tcon = smb2_qfs_tcon,
5228 .is_path_accessible = smb2_is_path_accessible,
5229 .can_echo = smb2_can_echo,
5230 .echo = SMB2_echo,
5231 .query_path_info = smb2_query_path_info,
5232 .query_reparse_point = smb2_query_reparse_point,
5233 .get_srv_inum = smb2_get_srv_inum,
5234 .query_file_info = smb2_query_file_info,
5235 .set_path_size = smb2_set_path_size,
5236 .set_file_size = smb2_set_file_size,
5237 .set_file_info = smb2_set_file_info,
5238 .set_compression = smb2_set_compression,
5239 .mkdir = smb2_mkdir,
5240 .mkdir_setinfo = smb2_mkdir_setinfo,
5241 .rmdir = smb2_rmdir,
5242 .unlink = smb2_unlink,
5243 .rename = smb2_rename_path,
5244 .create_hardlink = smb2_create_hardlink,
5245 .parse_reparse_point = smb2_parse_reparse_point,
5246 .query_mf_symlink = smb3_query_mf_symlink,
5247 .create_mf_symlink = smb3_create_mf_symlink,
5248 .create_reparse_symlink = smb2_create_reparse_symlink,
5249 .open = smb2_open_file,
5250 .set_fid = smb2_set_fid,
5251 .close = smb2_close_file,
5252 .flush = smb2_flush_file,
5253 .async_readv = smb2_async_readv,
5254 .async_writev = smb2_async_writev,
5255 .sync_read = smb2_sync_read,
5256 .sync_write = smb2_sync_write,
5257 .query_dir_first = smb2_query_dir_first,
5258 .query_dir_next = smb2_query_dir_next,
5259 .close_dir = smb2_close_dir,
5260 .calc_smb_size = smb2_calc_size,
5261 .is_status_pending = smb2_is_status_pending,
5262 .is_session_expired = smb2_is_session_expired,
5263 .oplock_response = smb2_oplock_response,
5264 .queryfs = smb2_queryfs,
5265 .mand_lock = smb2_mand_lock,
5266 .mand_unlock_range = smb2_unlock_range,
5267 .push_mand_locks = smb2_push_mandatory_locks,
5268 .get_lease_key = smb2_get_lease_key,
5269 .set_lease_key = smb2_set_lease_key,
5270 .new_lease_key = smb2_new_lease_key,
5271 .calc_signature = smb2_calc_signature,
5272 .is_read_op = smb2_is_read_op,
5273 .set_oplock_level = smb2_set_oplock_level,
5274 .create_lease_buf = smb2_create_lease_buf,
5275 .parse_lease_buf = smb2_parse_lease_buf,
5276 .copychunk_range = smb2_copychunk_range,
5277 .wp_retry_size = smb2_wp_retry_size,
5278 .dir_needs_close = smb2_dir_needs_close,
5279 .get_dfs_refer = smb2_get_dfs_refer,
5280 .select_sectype = smb2_select_sectype,
5281 #ifdef CONFIG_CIFS_XATTR
5282 .query_all_EAs = smb2_query_eas,
5283 .set_EA = smb2_set_ea,
5284 #endif /* CIFS_XATTR */
5285 .get_acl = get_smb2_acl,
5286 .get_acl_by_fid = get_smb2_acl_by_fid,
5287 .set_acl = set_smb2_acl,
5288 .next_header = smb2_next_header,
5289 .ioctl_query_info = smb2_ioctl_query_info,
5290 .make_node = smb2_make_node,
5291 .fiemap = smb3_fiemap,
5292 .llseek = smb3_llseek,
5293 .is_status_io_timeout = smb2_is_status_io_timeout,
5294 .is_network_name_deleted = smb2_is_network_name_deleted,
5295 };
5296 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5297
5298 struct smb_version_operations smb21_operations = {
5299 .compare_fids = smb2_compare_fids,
5300 .setup_request = smb2_setup_request,
5301 .setup_async_request = smb2_setup_async_request,
5302 .check_receive = smb2_check_receive,
5303 .add_credits = smb2_add_credits,
5304 .set_credits = smb2_set_credits,
5305 .get_credits_field = smb2_get_credits_field,
5306 .get_credits = smb2_get_credits,
5307 .wait_mtu_credits = smb2_wait_mtu_credits,
5308 .adjust_credits = smb2_adjust_credits,
5309 .get_next_mid = smb2_get_next_mid,
5310 .revert_current_mid = smb2_revert_current_mid,
5311 .read_data_offset = smb2_read_data_offset,
5312 .read_data_length = smb2_read_data_length,
5313 .map_error = map_smb2_to_linux_error,
5314 .find_mid = smb2_find_mid,
5315 .check_message = smb2_check_message,
5316 .dump_detail = smb2_dump_detail,
5317 .clear_stats = smb2_clear_stats,
5318 .print_stats = smb2_print_stats,
5319 .is_oplock_break = smb2_is_valid_oplock_break,
5320 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5321 .downgrade_oplock = smb2_downgrade_oplock,
5322 .need_neg = smb2_need_neg,
5323 .negotiate = smb2_negotiate,
5324 .negotiate_wsize = smb2_negotiate_wsize,
5325 .negotiate_rsize = smb2_negotiate_rsize,
5326 .sess_setup = SMB2_sess_setup,
5327 .logoff = SMB2_logoff,
5328 .tree_connect = SMB2_tcon,
5329 .tree_disconnect = SMB2_tdis,
5330 .qfs_tcon = smb2_qfs_tcon,
5331 .is_path_accessible = smb2_is_path_accessible,
5332 .can_echo = smb2_can_echo,
5333 .echo = SMB2_echo,
5334 .query_path_info = smb2_query_path_info,
5335 .query_reparse_point = smb2_query_reparse_point,
5336 .get_srv_inum = smb2_get_srv_inum,
5337 .query_file_info = smb2_query_file_info,
5338 .set_path_size = smb2_set_path_size,
5339 .set_file_size = smb2_set_file_size,
5340 .set_file_info = smb2_set_file_info,
5341 .set_compression = smb2_set_compression,
5342 .mkdir = smb2_mkdir,
5343 .mkdir_setinfo = smb2_mkdir_setinfo,
5344 .rmdir = smb2_rmdir,
5345 .unlink = smb2_unlink,
5346 .rename = smb2_rename_path,
5347 .create_hardlink = smb2_create_hardlink,
5348 .parse_reparse_point = smb2_parse_reparse_point,
5349 .query_mf_symlink = smb3_query_mf_symlink,
5350 .create_mf_symlink = smb3_create_mf_symlink,
5351 .create_reparse_symlink = smb2_create_reparse_symlink,
5352 .open = smb2_open_file,
5353 .set_fid = smb2_set_fid,
5354 .close = smb2_close_file,
5355 .flush = smb2_flush_file,
5356 .async_readv = smb2_async_readv,
5357 .async_writev = smb2_async_writev,
5358 .sync_read = smb2_sync_read,
5359 .sync_write = smb2_sync_write,
5360 .query_dir_first = smb2_query_dir_first,
5361 .query_dir_next = smb2_query_dir_next,
5362 .close_dir = smb2_close_dir,
5363 .calc_smb_size = smb2_calc_size,
5364 .is_status_pending = smb2_is_status_pending,
5365 .is_session_expired = smb2_is_session_expired,
5366 .oplock_response = smb2_oplock_response,
5367 .queryfs = smb2_queryfs,
5368 .mand_lock = smb2_mand_lock,
5369 .mand_unlock_range = smb2_unlock_range,
5370 .push_mand_locks = smb2_push_mandatory_locks,
5371 .get_lease_key = smb2_get_lease_key,
5372 .set_lease_key = smb2_set_lease_key,
5373 .new_lease_key = smb2_new_lease_key,
5374 .calc_signature = smb2_calc_signature,
5375 .is_read_op = smb21_is_read_op,
5376 .set_oplock_level = smb21_set_oplock_level,
5377 .create_lease_buf = smb2_create_lease_buf,
5378 .parse_lease_buf = smb2_parse_lease_buf,
5379 .copychunk_range = smb2_copychunk_range,
5380 .wp_retry_size = smb2_wp_retry_size,
5381 .dir_needs_close = smb2_dir_needs_close,
5382 .enum_snapshots = smb3_enum_snapshots,
5383 .notify = smb3_notify,
5384 .get_dfs_refer = smb2_get_dfs_refer,
5385 .select_sectype = smb2_select_sectype,
5386 #ifdef CONFIG_CIFS_XATTR
5387 .query_all_EAs = smb2_query_eas,
5388 .set_EA = smb2_set_ea,
5389 #endif /* CIFS_XATTR */
5390 .get_acl = get_smb2_acl,
5391 .get_acl_by_fid = get_smb2_acl_by_fid,
5392 .set_acl = set_smb2_acl,
5393 .next_header = smb2_next_header,
5394 .ioctl_query_info = smb2_ioctl_query_info,
5395 .make_node = smb2_make_node,
5396 .fiemap = smb3_fiemap,
5397 .llseek = smb3_llseek,
5398 .is_status_io_timeout = smb2_is_status_io_timeout,
5399 .is_network_name_deleted = smb2_is_network_name_deleted,
5400 };
5401
5402 struct smb_version_operations smb30_operations = {
5403 .compare_fids = smb2_compare_fids,
5404 .setup_request = smb2_setup_request,
5405 .setup_async_request = smb2_setup_async_request,
5406 .check_receive = smb2_check_receive,
5407 .add_credits = smb2_add_credits,
5408 .set_credits = smb2_set_credits,
5409 .get_credits_field = smb2_get_credits_field,
5410 .get_credits = smb2_get_credits,
5411 .wait_mtu_credits = smb2_wait_mtu_credits,
5412 .adjust_credits = smb2_adjust_credits,
5413 .get_next_mid = smb2_get_next_mid,
5414 .revert_current_mid = smb2_revert_current_mid,
5415 .read_data_offset = smb2_read_data_offset,
5416 .read_data_length = smb2_read_data_length,
5417 .map_error = map_smb2_to_linux_error,
5418 .find_mid = smb2_find_mid,
5419 .check_message = smb2_check_message,
5420 .dump_detail = smb2_dump_detail,
5421 .clear_stats = smb2_clear_stats,
5422 .print_stats = smb2_print_stats,
5423 .dump_share_caps = smb2_dump_share_caps,
5424 .is_oplock_break = smb2_is_valid_oplock_break,
5425 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5426 .downgrade_oplock = smb3_downgrade_oplock,
5427 .need_neg = smb2_need_neg,
5428 .negotiate = smb2_negotiate,
5429 .negotiate_wsize = smb3_negotiate_wsize,
5430 .negotiate_rsize = smb3_negotiate_rsize,
5431 .sess_setup = SMB2_sess_setup,
5432 .logoff = SMB2_logoff,
5433 .tree_connect = SMB2_tcon,
5434 .tree_disconnect = SMB2_tdis,
5435 .qfs_tcon = smb3_qfs_tcon,
5436 .is_path_accessible = smb2_is_path_accessible,
5437 .can_echo = smb2_can_echo,
5438 .echo = SMB2_echo,
5439 .query_path_info = smb2_query_path_info,
5440 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5441 .query_reparse_point = smb2_query_reparse_point,
5442 .get_srv_inum = smb2_get_srv_inum,
5443 .query_file_info = smb2_query_file_info,
5444 .set_path_size = smb2_set_path_size,
5445 .set_file_size = smb2_set_file_size,
5446 .set_file_info = smb2_set_file_info,
5447 .set_compression = smb2_set_compression,
5448 .mkdir = smb2_mkdir,
5449 .mkdir_setinfo = smb2_mkdir_setinfo,
5450 .rmdir = smb2_rmdir,
5451 .unlink = smb2_unlink,
5452 .rename = smb2_rename_path,
5453 .create_hardlink = smb2_create_hardlink,
5454 .parse_reparse_point = smb2_parse_reparse_point,
5455 .query_mf_symlink = smb3_query_mf_symlink,
5456 .create_mf_symlink = smb3_create_mf_symlink,
5457 .create_reparse_symlink = smb2_create_reparse_symlink,
5458 .open = smb2_open_file,
5459 .set_fid = smb2_set_fid,
5460 .close = smb2_close_file,
5461 .close_getattr = smb2_close_getattr,
5462 .flush = smb2_flush_file,
5463 .async_readv = smb2_async_readv,
5464 .async_writev = smb2_async_writev,
5465 .sync_read = smb2_sync_read,
5466 .sync_write = smb2_sync_write,
5467 .query_dir_first = smb2_query_dir_first,
5468 .query_dir_next = smb2_query_dir_next,
5469 .close_dir = smb2_close_dir,
5470 .calc_smb_size = smb2_calc_size,
5471 .is_status_pending = smb2_is_status_pending,
5472 .is_session_expired = smb2_is_session_expired,
5473 .oplock_response = smb2_oplock_response,
5474 .queryfs = smb2_queryfs,
5475 .mand_lock = smb2_mand_lock,
5476 .mand_unlock_range = smb2_unlock_range,
5477 .push_mand_locks = smb2_push_mandatory_locks,
5478 .get_lease_key = smb2_get_lease_key,
5479 .set_lease_key = smb2_set_lease_key,
5480 .new_lease_key = smb2_new_lease_key,
5481 .generate_signingkey = generate_smb30signingkey,
5482 .calc_signature = smb3_calc_signature,
5483 .set_integrity = smb3_set_integrity,
5484 .is_read_op = smb21_is_read_op,
5485 .set_oplock_level = smb3_set_oplock_level,
5486 .create_lease_buf = smb3_create_lease_buf,
5487 .parse_lease_buf = smb3_parse_lease_buf,
5488 .copychunk_range = smb2_copychunk_range,
5489 .duplicate_extents = smb2_duplicate_extents,
5490 .validate_negotiate = smb3_validate_negotiate,
5491 .wp_retry_size = smb2_wp_retry_size,
5492 .dir_needs_close = smb2_dir_needs_close,
5493 .fallocate = smb3_fallocate,
5494 .enum_snapshots = smb3_enum_snapshots,
5495 .notify = smb3_notify,
5496 .init_transform_rq = smb3_init_transform_rq,
5497 .is_transform_hdr = smb3_is_transform_hdr,
5498 .receive_transform = smb3_receive_transform,
5499 .get_dfs_refer = smb2_get_dfs_refer,
5500 .select_sectype = smb2_select_sectype,
5501 #ifdef CONFIG_CIFS_XATTR
5502 .query_all_EAs = smb2_query_eas,
5503 .set_EA = smb2_set_ea,
5504 #endif /* CIFS_XATTR */
5505 .get_acl = get_smb2_acl,
5506 .get_acl_by_fid = get_smb2_acl_by_fid,
5507 .set_acl = set_smb2_acl,
5508 .next_header = smb2_next_header,
5509 .ioctl_query_info = smb2_ioctl_query_info,
5510 .make_node = smb2_make_node,
5511 .fiemap = smb3_fiemap,
5512 .llseek = smb3_llseek,
5513 .is_status_io_timeout = smb2_is_status_io_timeout,
5514 .is_network_name_deleted = smb2_is_network_name_deleted,
5515 };
5516
5517 struct smb_version_operations smb311_operations = {
5518 .compare_fids = smb2_compare_fids,
5519 .setup_request = smb2_setup_request,
5520 .setup_async_request = smb2_setup_async_request,
5521 .check_receive = smb2_check_receive,
5522 .add_credits = smb2_add_credits,
5523 .set_credits = smb2_set_credits,
5524 .get_credits_field = smb2_get_credits_field,
5525 .get_credits = smb2_get_credits,
5526 .wait_mtu_credits = smb2_wait_mtu_credits,
5527 .adjust_credits = smb2_adjust_credits,
5528 .get_next_mid = smb2_get_next_mid,
5529 .revert_current_mid = smb2_revert_current_mid,
5530 .read_data_offset = smb2_read_data_offset,
5531 .read_data_length = smb2_read_data_length,
5532 .map_error = map_smb2_to_linux_error,
5533 .find_mid = smb2_find_mid,
5534 .check_message = smb2_check_message,
5535 .dump_detail = smb2_dump_detail,
5536 .clear_stats = smb2_clear_stats,
5537 .print_stats = smb2_print_stats,
5538 .dump_share_caps = smb2_dump_share_caps,
5539 .is_oplock_break = smb2_is_valid_oplock_break,
5540 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5541 .downgrade_oplock = smb3_downgrade_oplock,
5542 .need_neg = smb2_need_neg,
5543 .negotiate = smb2_negotiate,
5544 .negotiate_wsize = smb3_negotiate_wsize,
5545 .negotiate_rsize = smb3_negotiate_rsize,
5546 .sess_setup = SMB2_sess_setup,
5547 .logoff = SMB2_logoff,
5548 .tree_connect = SMB2_tcon,
5549 .tree_disconnect = SMB2_tdis,
5550 .qfs_tcon = smb3_qfs_tcon,
5551 .is_path_accessible = smb2_is_path_accessible,
5552 .can_echo = smb2_can_echo,
5553 .echo = SMB2_echo,
5554 .query_path_info = smb2_query_path_info,
5555 .query_reparse_point = smb2_query_reparse_point,
5556 .get_srv_inum = smb2_get_srv_inum,
5557 .query_file_info = smb2_query_file_info,
5558 .set_path_size = smb2_set_path_size,
5559 .set_file_size = smb2_set_file_size,
5560 .set_file_info = smb2_set_file_info,
5561 .set_compression = smb2_set_compression,
5562 .mkdir = smb2_mkdir,
5563 .mkdir_setinfo = smb2_mkdir_setinfo,
5564 .posix_mkdir = smb311_posix_mkdir,
5565 .rmdir = smb2_rmdir,
5566 .unlink = smb2_unlink,
5567 .rename = smb2_rename_path,
5568 .create_hardlink = smb2_create_hardlink,
5569 .parse_reparse_point = smb2_parse_reparse_point,
5570 .query_mf_symlink = smb3_query_mf_symlink,
5571 .create_mf_symlink = smb3_create_mf_symlink,
5572 .create_reparse_symlink = smb2_create_reparse_symlink,
5573 .open = smb2_open_file,
5574 .set_fid = smb2_set_fid,
5575 .close = smb2_close_file,
5576 .close_getattr = smb2_close_getattr,
5577 .flush = smb2_flush_file,
5578 .async_readv = smb2_async_readv,
5579 .async_writev = smb2_async_writev,
5580 .sync_read = smb2_sync_read,
5581 .sync_write = smb2_sync_write,
5582 .query_dir_first = smb2_query_dir_first,
5583 .query_dir_next = smb2_query_dir_next,
5584 .close_dir = smb2_close_dir,
5585 .calc_smb_size = smb2_calc_size,
5586 .is_status_pending = smb2_is_status_pending,
5587 .is_session_expired = smb2_is_session_expired,
5588 .oplock_response = smb2_oplock_response,
5589 .queryfs = smb311_queryfs,
5590 .mand_lock = smb2_mand_lock,
5591 .mand_unlock_range = smb2_unlock_range,
5592 .push_mand_locks = smb2_push_mandatory_locks,
5593 .get_lease_key = smb2_get_lease_key,
5594 .set_lease_key = smb2_set_lease_key,
5595 .new_lease_key = smb2_new_lease_key,
5596 .generate_signingkey = generate_smb311signingkey,
5597 .calc_signature = smb3_calc_signature,
5598 .set_integrity = smb3_set_integrity,
5599 .is_read_op = smb21_is_read_op,
5600 .set_oplock_level = smb3_set_oplock_level,
5601 .create_lease_buf = smb3_create_lease_buf,
5602 .parse_lease_buf = smb3_parse_lease_buf,
5603 .copychunk_range = smb2_copychunk_range,
5604 .duplicate_extents = smb2_duplicate_extents,
5605 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5606 .wp_retry_size = smb2_wp_retry_size,
5607 .dir_needs_close = smb2_dir_needs_close,
5608 .fallocate = smb3_fallocate,
5609 .enum_snapshots = smb3_enum_snapshots,
5610 .notify = smb3_notify,
5611 .init_transform_rq = smb3_init_transform_rq,
5612 .is_transform_hdr = smb3_is_transform_hdr,
5613 .receive_transform = smb3_receive_transform,
5614 .get_dfs_refer = smb2_get_dfs_refer,
5615 .select_sectype = smb2_select_sectype,
5616 #ifdef CONFIG_CIFS_XATTR
5617 .query_all_EAs = smb2_query_eas,
5618 .set_EA = smb2_set_ea,
5619 #endif /* CIFS_XATTR */
5620 .get_acl = get_smb2_acl,
5621 .get_acl_by_fid = get_smb2_acl_by_fid,
5622 .set_acl = set_smb2_acl,
5623 .next_header = smb2_next_header,
5624 .ioctl_query_info = smb2_ioctl_query_info,
5625 .make_node = smb2_make_node,
5626 .fiemap = smb3_fiemap,
5627 .llseek = smb3_llseek,
5628 .is_status_io_timeout = smb2_is_status_io_timeout,
5629 .is_network_name_deleted = smb2_is_network_name_deleted,
5630 };
5631
5632 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5633 struct smb_version_values smb20_values = {
5634 .version_string = SMB20_VERSION_STRING,
5635 .protocol_id = SMB20_PROT_ID,
5636 .req_capabilities = 0, /* MBZ */
5637 .large_lock_type = 0,
5638 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5639 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5640 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5641 .header_size = sizeof(struct smb2_hdr),
5642 .header_preamble_size = 0,
5643 .max_header_size = MAX_SMB2_HDR_SIZE,
5644 .read_rsp_size = sizeof(struct smb2_read_rsp),
5645 .lock_cmd = SMB2_LOCK,
5646 .cap_unix = 0,
5647 .cap_nt_find = SMB2_NT_FIND,
5648 .cap_large_files = SMB2_LARGE_FILES,
5649 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5650 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5651 .create_lease_size = sizeof(struct create_lease),
5652 };
5653 #endif /* ALLOW_INSECURE_LEGACY */
5654
5655 struct smb_version_values smb21_values = {
5656 .version_string = SMB21_VERSION_STRING,
5657 .protocol_id = SMB21_PROT_ID,
5658 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5659 .large_lock_type = 0,
5660 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5661 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5662 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5663 .header_size = sizeof(struct smb2_hdr),
5664 .header_preamble_size = 0,
5665 .max_header_size = MAX_SMB2_HDR_SIZE,
5666 .read_rsp_size = sizeof(struct smb2_read_rsp),
5667 .lock_cmd = SMB2_LOCK,
5668 .cap_unix = 0,
5669 .cap_nt_find = SMB2_NT_FIND,
5670 .cap_large_files = SMB2_LARGE_FILES,
5671 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5672 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5673 .create_lease_size = sizeof(struct create_lease),
5674 };
5675
5676 struct smb_version_values smb3any_values = {
5677 .version_string = SMB3ANY_VERSION_STRING,
5678 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5679 .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,
5680 .large_lock_type = 0,
5681 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5682 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5683 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5684 .header_size = sizeof(struct smb2_hdr),
5685 .header_preamble_size = 0,
5686 .max_header_size = MAX_SMB2_HDR_SIZE,
5687 .read_rsp_size = sizeof(struct smb2_read_rsp),
5688 .lock_cmd = SMB2_LOCK,
5689 .cap_unix = 0,
5690 .cap_nt_find = SMB2_NT_FIND,
5691 .cap_large_files = SMB2_LARGE_FILES,
5692 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5693 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5694 .create_lease_size = sizeof(struct create_lease_v2),
5695 };
5696
5697 struct smb_version_values smbdefault_values = {
5698 .version_string = SMBDEFAULT_VERSION_STRING,
5699 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5700 .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,
5701 .large_lock_type = 0,
5702 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5703 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5704 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5705 .header_size = sizeof(struct smb2_hdr),
5706 .header_preamble_size = 0,
5707 .max_header_size = MAX_SMB2_HDR_SIZE,
5708 .read_rsp_size = sizeof(struct smb2_read_rsp),
5709 .lock_cmd = SMB2_LOCK,
5710 .cap_unix = 0,
5711 .cap_nt_find = SMB2_NT_FIND,
5712 .cap_large_files = SMB2_LARGE_FILES,
5713 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5714 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5715 .create_lease_size = sizeof(struct create_lease_v2),
5716 };
5717
5718 struct smb_version_values smb30_values = {
5719 .version_string = SMB30_VERSION_STRING,
5720 .protocol_id = SMB30_PROT_ID,
5721 .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,
5722 .large_lock_type = 0,
5723 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5724 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5725 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5726 .header_size = sizeof(struct smb2_hdr),
5727 .header_preamble_size = 0,
5728 .max_header_size = MAX_SMB2_HDR_SIZE,
5729 .read_rsp_size = sizeof(struct smb2_read_rsp),
5730 .lock_cmd = SMB2_LOCK,
5731 .cap_unix = 0,
5732 .cap_nt_find = SMB2_NT_FIND,
5733 .cap_large_files = SMB2_LARGE_FILES,
5734 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5735 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5736 .create_lease_size = sizeof(struct create_lease_v2),
5737 };
5738
5739 struct smb_version_values smb302_values = {
5740 .version_string = SMB302_VERSION_STRING,
5741 .protocol_id = SMB302_PROT_ID,
5742 .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,
5743 .large_lock_type = 0,
5744 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5745 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5746 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5747 .header_size = sizeof(struct smb2_hdr),
5748 .header_preamble_size = 0,
5749 .max_header_size = MAX_SMB2_HDR_SIZE,
5750 .read_rsp_size = sizeof(struct smb2_read_rsp),
5751 .lock_cmd = SMB2_LOCK,
5752 .cap_unix = 0,
5753 .cap_nt_find = SMB2_NT_FIND,
5754 .cap_large_files = SMB2_LARGE_FILES,
5755 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5756 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5757 .create_lease_size = sizeof(struct create_lease_v2),
5758 };
5759
5760 struct smb_version_values smb311_values = {
5761 .version_string = SMB311_VERSION_STRING,
5762 .protocol_id = SMB311_PROT_ID,
5763 .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,
5764 .large_lock_type = 0,
5765 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5766 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5767 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5768 .header_size = sizeof(struct smb2_hdr),
5769 .header_preamble_size = 0,
5770 .max_header_size = MAX_SMB2_HDR_SIZE,
5771 .read_rsp_size = sizeof(struct smb2_read_rsp),
5772 .lock_cmd = SMB2_LOCK,
5773 .cap_unix = 0,
5774 .cap_nt_find = SMB2_NT_FIND,
5775 .cap_large_files = SMB2_LARGE_FILES,
5776 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5777 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5778 .create_lease_size = sizeof(struct create_lease_v2),
5779 };