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