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