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