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