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