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