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