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