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