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