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