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_count = 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 do { 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 (!is_retryable_error(rc)) 2990 break; 2991 usleep_range(512, 2048); 2992 } while (++retry_count < 5); 2993 2994 if (!rc && !dfs_rsp) 2995 rc = -EIO; 2996 if (rc) { 2997 if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP) 2998 cifs_tcon_dbg(VFS, "%s: ioctl error: rc=%d\n", __func__, rc); 2999 goto out; 3000 } 3001 3002 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size, 3003 num_of_nodes, target_nodes, 3004 nls_codepage, remap, search_name, 3005 true /* is_unicode */); 3006 if (rc) { 3007 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc); 3008 goto out; 3009 } 3010 3011 out: 3012 if (tcon && !tcon->ipc) { 3013 /* ipc tcons are not refcounted */ 3014 spin_lock(&cifs_tcp_ses_lock); 3015 tcon->tc_count--; 3016 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 3017 netfs_trace_tcon_ref_dec_dfs_refer); 3018 /* tc_count can never go negative */ 3019 WARN_ON(tcon->tc_count < 0); 3020 spin_unlock(&cifs_tcp_ses_lock); 3021 } 3022 kfree(utf16_path); 3023 kfree(dfs_req); 3024 kfree(dfs_rsp); 3025 return rc; 3026 } 3027 3028 static struct smb_ntsd * 3029 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb, 3030 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info) 3031 { 3032 struct smb_ntsd *pntsd = NULL; 3033 unsigned int xid; 3034 int rc = -EOPNOTSUPP; 3035 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); 3036 3037 if (IS_ERR(tlink)) 3038 return ERR_CAST(tlink); 3039 3040 xid = get_xid(); 3041 cifs_dbg(FYI, "trying to get acl\n"); 3042 3043 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid, 3044 cifsfid->volatile_fid, (void **)&pntsd, pacllen, 3045 info); 3046 free_xid(xid); 3047 3048 cifs_put_tlink(tlink); 3049 3050 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen); 3051 if (rc) 3052 return ERR_PTR(rc); 3053 return pntsd; 3054 3055 } 3056 3057 static struct smb_ntsd * 3058 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb, 3059 const char *path, u32 *pacllen, u32 info) 3060 { 3061 struct smb_ntsd *pntsd = NULL; 3062 u8 oplock = SMB2_OPLOCK_LEVEL_NONE; 3063 unsigned int xid; 3064 int rc; 3065 struct cifs_tcon *tcon; 3066 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); 3067 struct cifs_fid fid; 3068 struct cifs_open_parms oparms; 3069 __le16 *utf16_path; 3070 3071 cifs_dbg(FYI, "get smb3 acl for path %s\n", path); 3072 if (IS_ERR(tlink)) 3073 return ERR_CAST(tlink); 3074 3075 tcon = tlink_tcon(tlink); 3076 xid = get_xid(); 3077 3078 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb); 3079 if (!utf16_path) { 3080 rc = -ENOMEM; 3081 free_xid(xid); 3082 return ERR_PTR(rc); 3083 } 3084 3085 oparms = (struct cifs_open_parms) { 3086 .tcon = tcon, 3087 .path = path, 3088 .desired_access = READ_CONTROL, 3089 .disposition = FILE_OPEN, 3090 /* 3091 * When querying an ACL, even if the file is a symlink 3092 * we want to open the source not the target, and so 3093 * the protocol requires that the client specify this 3094 * flag when opening a reparse point 3095 */ 3096 .create_options = cifs_create_options(cifs_sb, 0) | 3097 OPEN_REPARSE_POINT, 3098 .fid = &fid, 3099 }; 3100 3101 if (info & SACL_SECINFO) 3102 oparms.desired_access |= SYSTEM_SECURITY; 3103 3104 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL, 3105 NULL); 3106 kfree(utf16_path); 3107 if (!rc) { 3108 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid, 3109 fid.volatile_fid, (void **)&pntsd, pacllen, 3110 info); 3111 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid); 3112 } 3113 3114 cifs_put_tlink(tlink); 3115 free_xid(xid); 3116 3117 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen); 3118 if (rc) 3119 return ERR_PTR(rc); 3120 return pntsd; 3121 } 3122 3123 static int 3124 set_smb2_acl(struct smb_ntsd *pnntsd, __u32 acllen, 3125 struct inode *inode, const char *path, int aclflag) 3126 { 3127 u8 oplock = SMB2_OPLOCK_LEVEL_NONE; 3128 unsigned int xid; 3129 int rc, access_flags = 0; 3130 struct cifs_tcon *tcon; 3131 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 3132 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); 3133 struct cifs_fid fid; 3134 struct cifs_open_parms oparms; 3135 __le16 *utf16_path; 3136 3137 cifs_dbg(FYI, "set smb3 acl for path %s\n", path); 3138 if (IS_ERR(tlink)) 3139 return PTR_ERR(tlink); 3140 3141 tcon = tlink_tcon(tlink); 3142 xid = get_xid(); 3143 3144 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP) 3145 access_flags |= WRITE_OWNER; 3146 if (aclflag & CIFS_ACL_SACL) 3147 access_flags |= SYSTEM_SECURITY; 3148 if (aclflag & CIFS_ACL_DACL) 3149 access_flags |= WRITE_DAC; 3150 3151 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb); 3152 if (!utf16_path) { 3153 rc = -ENOMEM; 3154 free_xid(xid); 3155 return rc; 3156 } 3157 3158 oparms = (struct cifs_open_parms) { 3159 .tcon = tcon, 3160 .desired_access = access_flags, 3161 .create_options = cifs_create_options(cifs_sb, 0), 3162 .disposition = FILE_OPEN, 3163 .path = path, 3164 .fid = &fid, 3165 }; 3166 3167 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, 3168 NULL, NULL); 3169 kfree(utf16_path); 3170 if (!rc) { 3171 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid, 3172 fid.volatile_fid, pnntsd, acllen, aclflag); 3173 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid); 3174 } 3175 3176 cifs_put_tlink(tlink); 3177 free_xid(xid); 3178 return rc; 3179 } 3180 3181 /* Retrieve an ACL from the server */ 3182 static struct smb_ntsd * 3183 get_smb2_acl(struct cifs_sb_info *cifs_sb, 3184 struct inode *inode, const char *path, 3185 u32 *pacllen, u32 info) 3186 { 3187 struct smb_ntsd *pntsd = NULL; 3188 struct cifsFileInfo *open_file = NULL; 3189 3190 if (inode && !(info & SACL_SECINFO)) 3191 open_file = find_readable_file(CIFS_I(inode), true); 3192 if (!open_file || (info & SACL_SECINFO)) 3193 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info); 3194 3195 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info); 3196 cifsFileInfo_put(open_file); 3197 return pntsd; 3198 } 3199 3200 static long smb3_zero_data(struct file *file, struct cifs_tcon *tcon, 3201 loff_t offset, loff_t len, unsigned int xid) 3202 { 3203 struct cifsFileInfo *cfile = file->private_data; 3204 struct file_zero_data_information fsctl_buf; 3205 3206 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len); 3207 3208 fsctl_buf.FileOffset = cpu_to_le64(offset); 3209 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len); 3210 3211 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid, 3212 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, 3213 (char *)&fsctl_buf, 3214 sizeof(struct file_zero_data_information), 3215 0, NULL, NULL); 3216 } 3217 3218 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon, 3219 unsigned long long offset, unsigned long long len, 3220 bool keep_size) 3221 { 3222 struct cifs_ses *ses = tcon->ses; 3223 struct inode *inode = file_inode(file); 3224 struct cifsInodeInfo *cifsi = CIFS_I(inode); 3225 struct cifsFileInfo *cfile = file->private_data; 3226 struct netfs_inode *ictx = netfs_inode(inode); 3227 unsigned long long i_size, new_size, remote_size; 3228 long rc; 3229 unsigned int xid; 3230 3231 xid = get_xid(); 3232 3233 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid, 3234 ses->Suid, offset, len); 3235 3236 inode_lock(inode); 3237 filemap_invalidate_lock(inode->i_mapping); 3238 3239 i_size = i_size_read(inode); 3240 remote_size = ictx->remote_i_size; 3241 if (offset + len >= remote_size && offset < i_size) { 3242 unsigned long long top = umin(offset + len, i_size); 3243 3244 rc = filemap_write_and_wait_range(inode->i_mapping, offset, top - 1); 3245 if (rc < 0) 3246 goto zero_range_exit; 3247 } 3248 3249 /* 3250 * We zero the range through ioctl, so we need remove the page caches 3251 * first, otherwise the data may be inconsistent with the server. 3252 */ 3253 truncate_pagecache_range(inode, offset, offset + len - 1); 3254 3255 /* if file not oplocked can't be sure whether asking to extend size */ 3256 rc = -EOPNOTSUPP; 3257 if (keep_size == false && !CIFS_CACHE_READ(cifsi)) 3258 goto zero_range_exit; 3259 3260 rc = smb3_zero_data(file, tcon, offset, len, xid); 3261 if (rc < 0) 3262 goto zero_range_exit; 3263 3264 /* 3265 * do we also need to change the size of the file? 3266 */ 3267 new_size = offset + len; 3268 if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) { 3269 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, 3270 cfile->fid.volatile_fid, cfile->pid, new_size); 3271 if (rc >= 0) { 3272 truncate_setsize(inode, new_size); 3273 netfs_resize_file(&cifsi->netfs, new_size, true); 3274 if (offset < cifsi->netfs.zero_point) 3275 cifsi->netfs.zero_point = offset; 3276 fscache_resize_cookie(cifs_inode_cookie(inode), new_size); 3277 } 3278 } 3279 3280 zero_range_exit: 3281 filemap_invalidate_unlock(inode->i_mapping); 3282 inode_unlock(inode); 3283 free_xid(xid); 3284 if (rc) 3285 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid, 3286 ses->Suid, offset, len, rc); 3287 else 3288 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid, 3289 ses->Suid, offset, len); 3290 return rc; 3291 } 3292 3293 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon, 3294 loff_t offset, loff_t len) 3295 { 3296 struct inode *inode = file_inode(file); 3297 struct cifsFileInfo *cfile = file->private_data; 3298 struct file_zero_data_information fsctl_buf; 3299 unsigned long long end = offset + len, i_size, remote_i_size; 3300 long rc; 3301 unsigned int xid; 3302 __u8 set_sparse = 1; 3303 3304 xid = get_xid(); 3305 3306 inode_lock(inode); 3307 /* Need to make file sparse, if not already, before freeing range. */ 3308 /* Consider adding equivalent for compressed since it could also work */ 3309 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) { 3310 rc = -EOPNOTSUPP; 3311 goto out; 3312 } 3313 3314 filemap_invalidate_lock(inode->i_mapping); 3315 /* 3316 * We implement the punch hole through ioctl, so we need remove the page 3317 * caches first, otherwise the data may be inconsistent with the server. 3318 */ 3319 truncate_pagecache_range(inode, offset, offset + len - 1); 3320 3321 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len); 3322 3323 fsctl_buf.FileOffset = cpu_to_le64(offset); 3324 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len); 3325 3326 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid, 3327 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, 3328 (char *)&fsctl_buf, 3329 sizeof(struct file_zero_data_information), 3330 CIFSMaxBufSize, NULL, NULL); 3331 3332 if (rc) 3333 goto unlock; 3334 3335 /* If there's dirty data in the buffer that would extend the EOF if it 3336 * were written, then we need to move the EOF marker over to the lower 3337 * of the high end of the hole and the proposed EOF. The problem is 3338 * that we locally hole-punch the tail of the dirty data, the proposed 3339 * EOF update will end up in the wrong place. 3340 */ 3341 i_size = i_size_read(inode); 3342 remote_i_size = netfs_inode(inode)->remote_i_size; 3343 if (end > remote_i_size && i_size > remote_i_size) { 3344 unsigned long long extend_to = umin(end, i_size); 3345 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, 3346 cfile->fid.volatile_fid, cfile->pid, extend_to); 3347 if (rc >= 0) 3348 netfs_inode(inode)->remote_i_size = extend_to; 3349 } 3350 3351 unlock: 3352 filemap_invalidate_unlock(inode->i_mapping); 3353 out: 3354 inode_unlock(inode); 3355 free_xid(xid); 3356 return rc; 3357 } 3358 3359 static int smb3_simple_fallocate_write_range(unsigned int xid, 3360 struct cifs_tcon *tcon, 3361 struct cifsFileInfo *cfile, 3362 loff_t off, loff_t len, 3363 char *buf) 3364 { 3365 struct cifs_io_parms io_parms = {0}; 3366 int nbytes; 3367 int rc = 0; 3368 struct kvec iov[2]; 3369 3370 io_parms.netfid = cfile->fid.netfid; 3371 io_parms.pid = current->tgid; 3372 io_parms.tcon = tcon; 3373 io_parms.persistent_fid = cfile->fid.persistent_fid; 3374 io_parms.volatile_fid = cfile->fid.volatile_fid; 3375 3376 while (len) { 3377 io_parms.offset = off; 3378 io_parms.length = len; 3379 if (io_parms.length > SMB2_MAX_BUFFER_SIZE) 3380 io_parms.length = SMB2_MAX_BUFFER_SIZE; 3381 /* iov[0] is reserved for smb header */ 3382 iov[1].iov_base = buf; 3383 iov[1].iov_len = io_parms.length; 3384 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1); 3385 if (rc) 3386 break; 3387 if (nbytes > len) 3388 return -EINVAL; 3389 buf += nbytes; 3390 off += nbytes; 3391 len -= nbytes; 3392 } 3393 return rc; 3394 } 3395 3396 static int smb3_simple_fallocate_range(unsigned int xid, 3397 struct cifs_tcon *tcon, 3398 struct cifsFileInfo *cfile, 3399 loff_t off, loff_t len) 3400 { 3401 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data; 3402 u32 out_data_len; 3403 char *buf = NULL; 3404 loff_t l; 3405 int rc; 3406 3407 in_data.file_offset = cpu_to_le64(off); 3408 in_data.length = cpu_to_le64(len); 3409 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid, 3410 cfile->fid.volatile_fid, 3411 FSCTL_QUERY_ALLOCATED_RANGES, 3412 (char *)&in_data, sizeof(in_data), 3413 1024 * sizeof(struct file_allocated_range_buffer), 3414 (char **)&out_data, &out_data_len); 3415 if (rc) 3416 goto out; 3417 3418 buf = kzalloc(1024 * 1024, GFP_KERNEL); 3419 if (buf == NULL) { 3420 rc = -ENOMEM; 3421 goto out; 3422 } 3423 3424 tmp_data = out_data; 3425 while (len) { 3426 /* 3427 * The rest of the region is unmapped so write it all. 3428 */ 3429 if (out_data_len == 0) { 3430 rc = smb3_simple_fallocate_write_range(xid, tcon, 3431 cfile, off, len, buf); 3432 goto out; 3433 } 3434 3435 if (out_data_len < sizeof(struct file_allocated_range_buffer)) { 3436 rc = -EINVAL; 3437 goto out; 3438 } 3439 3440 if (off < le64_to_cpu(tmp_data->file_offset)) { 3441 /* 3442 * We are at a hole. Write until the end of the region 3443 * or until the next allocated data, 3444 * whichever comes next. 3445 */ 3446 l = le64_to_cpu(tmp_data->file_offset) - off; 3447 if (len < l) 3448 l = len; 3449 rc = smb3_simple_fallocate_write_range(xid, tcon, 3450 cfile, off, l, buf); 3451 if (rc) 3452 goto out; 3453 off = off + l; 3454 len = len - l; 3455 if (len == 0) 3456 goto out; 3457 } 3458 /* 3459 * We are at a section of allocated data, just skip forward 3460 * until the end of the data or the end of the region 3461 * we are supposed to fallocate, whichever comes first. 3462 */ 3463 l = le64_to_cpu(tmp_data->length); 3464 if (len < l) 3465 l = len; 3466 off += l; 3467 len -= l; 3468 3469 tmp_data = &tmp_data[1]; 3470 out_data_len -= sizeof(struct file_allocated_range_buffer); 3471 } 3472 3473 out: 3474 kfree(out_data); 3475 kfree(buf); 3476 return rc; 3477 } 3478 3479 3480 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon, 3481 loff_t off, loff_t len, bool keep_size) 3482 { 3483 struct inode *inode; 3484 struct cifsInodeInfo *cifsi; 3485 struct cifsFileInfo *cfile = file->private_data; 3486 long rc = -EOPNOTSUPP; 3487 unsigned int xid; 3488 loff_t new_eof; 3489 3490 xid = get_xid(); 3491 3492 inode = d_inode(cfile->dentry); 3493 cifsi = CIFS_I(inode); 3494 3495 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid, 3496 tcon->ses->Suid, off, len); 3497 /* if file not oplocked can't be sure whether asking to extend size */ 3498 if (!CIFS_CACHE_READ(cifsi)) 3499 if (keep_size == false) { 3500 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, 3501 tcon->tid, tcon->ses->Suid, off, len, rc); 3502 free_xid(xid); 3503 return rc; 3504 } 3505 3506 /* 3507 * Extending the file 3508 */ 3509 if ((keep_size == false) && i_size_read(inode) < off + len) { 3510 rc = inode_newsize_ok(inode, off + len); 3511 if (rc) 3512 goto out; 3513 3514 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) 3515 smb2_set_sparse(xid, tcon, cfile, inode, false); 3516 3517 new_eof = off + len; 3518 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, 3519 cfile->fid.volatile_fid, cfile->pid, new_eof); 3520 if (rc == 0) { 3521 netfs_resize_file(&cifsi->netfs, new_eof, true); 3522 cifs_setsize(inode, new_eof); 3523 cifs_truncate_page(inode->i_mapping, inode->i_size); 3524 truncate_setsize(inode, new_eof); 3525 } 3526 goto out; 3527 } 3528 3529 /* 3530 * Files are non-sparse by default so falloc may be a no-op 3531 * Must check if file sparse. If not sparse, and since we are not 3532 * extending then no need to do anything since file already allocated 3533 */ 3534 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) { 3535 rc = 0; 3536 goto out; 3537 } 3538 3539 if (keep_size == true) { 3540 /* 3541 * We can not preallocate pages beyond the end of the file 3542 * in SMB2 3543 */ 3544 if (off >= i_size_read(inode)) { 3545 rc = 0; 3546 goto out; 3547 } 3548 /* 3549 * For fallocates that are partially beyond the end of file, 3550 * clamp len so we only fallocate up to the end of file. 3551 */ 3552 if (off + len > i_size_read(inode)) { 3553 len = i_size_read(inode) - off; 3554 } 3555 } 3556 3557 if ((keep_size == true) || (i_size_read(inode) >= off + len)) { 3558 /* 3559 * At this point, we are trying to fallocate an internal 3560 * regions of a sparse file. Since smb2 does not have a 3561 * fallocate command we have two options on how to emulate this. 3562 * We can either turn the entire file to become non-sparse 3563 * which we only do if the fallocate is for virtually 3564 * the whole file, or we can overwrite the region with zeroes 3565 * using SMB2_write, which could be prohibitevly expensive 3566 * if len is large. 3567 */ 3568 /* 3569 * We are only trying to fallocate a small region so 3570 * just write it with zero. 3571 */ 3572 if (len <= 1024 * 1024) { 3573 rc = smb3_simple_fallocate_range(xid, tcon, cfile, 3574 off, len); 3575 goto out; 3576 } 3577 3578 /* 3579 * Check if falloc starts within first few pages of file 3580 * and ends within a few pages of the end of file to 3581 * ensure that most of file is being forced to be 3582 * fallocated now. If so then setting whole file sparse 3583 * ie potentially making a few extra pages at the beginning 3584 * or end of the file non-sparse via set_sparse is harmless. 3585 */ 3586 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) { 3587 rc = -EOPNOTSUPP; 3588 goto out; 3589 } 3590 } 3591 3592 smb2_set_sparse(xid, tcon, cfile, inode, false); 3593 rc = 0; 3594 3595 out: 3596 if (rc) 3597 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid, 3598 tcon->ses->Suid, off, len, rc); 3599 else 3600 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid, 3601 tcon->ses->Suid, off, len); 3602 3603 free_xid(xid); 3604 return rc; 3605 } 3606 3607 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon, 3608 loff_t off, loff_t len) 3609 { 3610 int rc; 3611 unsigned int xid; 3612 struct inode *inode = file_inode(file); 3613 struct cifsInodeInfo *cifsi = CIFS_I(inode); 3614 struct cifsFileInfo *cfile = file->private_data; 3615 struct netfs_inode *ictx = &cifsi->netfs; 3616 loff_t old_eof, new_eof; 3617 3618 xid = get_xid(); 3619 3620 inode_lock(inode); 3621 3622 old_eof = i_size_read(inode); 3623 if ((off >= old_eof) || 3624 off + len >= old_eof) { 3625 rc = -EINVAL; 3626 goto out; 3627 } 3628 3629 filemap_invalidate_lock(inode->i_mapping); 3630 rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof - 1); 3631 if (rc < 0) 3632 goto out_2; 3633 3634 truncate_pagecache_range(inode, off, old_eof); 3635 ictx->zero_point = old_eof; 3636 3637 rc = smb2_copychunk_range(xid, cfile, cfile, off + len, 3638 old_eof - off - len, off); 3639 if (rc < 0) 3640 goto out_2; 3641 3642 new_eof = old_eof - len; 3643 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, 3644 cfile->fid.volatile_fid, cfile->pid, new_eof); 3645 if (rc < 0) 3646 goto out_2; 3647 3648 rc = 0; 3649 3650 truncate_setsize(inode, new_eof); 3651 netfs_resize_file(&cifsi->netfs, new_eof, true); 3652 ictx->zero_point = new_eof; 3653 fscache_resize_cookie(cifs_inode_cookie(inode), new_eof); 3654 out_2: 3655 filemap_invalidate_unlock(inode->i_mapping); 3656 out: 3657 inode_unlock(inode); 3658 free_xid(xid); 3659 return rc; 3660 } 3661 3662 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon, 3663 loff_t off, loff_t len) 3664 { 3665 int rc; 3666 unsigned int xid; 3667 struct cifsFileInfo *cfile = file->private_data; 3668 struct inode *inode = file_inode(file); 3669 struct cifsInodeInfo *cifsi = CIFS_I(inode); 3670 __u64 count, old_eof, new_eof; 3671 3672 xid = get_xid(); 3673 3674 inode_lock(inode); 3675 3676 old_eof = i_size_read(inode); 3677 if (off >= old_eof) { 3678 rc = -EINVAL; 3679 goto out; 3680 } 3681 3682 count = old_eof - off; 3683 new_eof = old_eof + len; 3684 3685 filemap_invalidate_lock(inode->i_mapping); 3686 rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1); 3687 if (rc < 0) 3688 goto out_2; 3689 truncate_pagecache_range(inode, off, old_eof); 3690 3691 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, 3692 cfile->fid.volatile_fid, cfile->pid, new_eof); 3693 if (rc < 0) 3694 goto out_2; 3695 3696 truncate_setsize(inode, new_eof); 3697 netfs_resize_file(&cifsi->netfs, i_size_read(inode), true); 3698 fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode)); 3699 3700 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len); 3701 if (rc < 0) 3702 goto out_2; 3703 cifsi->netfs.zero_point = new_eof; 3704 3705 rc = smb3_zero_data(file, tcon, off, len, xid); 3706 if (rc < 0) 3707 goto out_2; 3708 3709 rc = 0; 3710 out_2: 3711 filemap_invalidate_unlock(inode->i_mapping); 3712 out: 3713 inode_unlock(inode); 3714 free_xid(xid); 3715 return rc; 3716 } 3717 3718 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence) 3719 { 3720 struct cifsFileInfo *wrcfile, *cfile = file->private_data; 3721 struct cifsInodeInfo *cifsi; 3722 struct inode *inode; 3723 int rc = 0; 3724 struct file_allocated_range_buffer in_data, *out_data = NULL; 3725 u32 out_data_len; 3726 unsigned int xid; 3727 3728 if (whence != SEEK_HOLE && whence != SEEK_DATA) 3729 return generic_file_llseek(file, offset, whence); 3730 3731 inode = d_inode(cfile->dentry); 3732 cifsi = CIFS_I(inode); 3733 3734 if (offset < 0 || offset >= i_size_read(inode)) 3735 return -ENXIO; 3736 3737 xid = get_xid(); 3738 /* 3739 * We need to be sure that all dirty pages are written as they 3740 * might fill holes on the server. 3741 * Note that we also MUST flush any written pages since at least 3742 * some servers (Windows2016) will not reflect recent writes in 3743 * QUERY_ALLOCATED_RANGES until SMB2_flush is called. 3744 */ 3745 wrcfile = find_writable_file(cifsi, FIND_WR_ANY); 3746 if (wrcfile) { 3747 filemap_write_and_wait(inode->i_mapping); 3748 smb2_flush_file(xid, tcon, &wrcfile->fid); 3749 cifsFileInfo_put(wrcfile); 3750 } 3751 3752 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) { 3753 if (whence == SEEK_HOLE) 3754 offset = i_size_read(inode); 3755 goto lseek_exit; 3756 } 3757 3758 in_data.file_offset = cpu_to_le64(offset); 3759 in_data.length = cpu_to_le64(i_size_read(inode)); 3760 3761 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid, 3762 cfile->fid.volatile_fid, 3763 FSCTL_QUERY_ALLOCATED_RANGES, 3764 (char *)&in_data, sizeof(in_data), 3765 sizeof(struct file_allocated_range_buffer), 3766 (char **)&out_data, &out_data_len); 3767 if (rc == -E2BIG) 3768 rc = 0; 3769 if (rc) 3770 goto lseek_exit; 3771 3772 if (whence == SEEK_HOLE && out_data_len == 0) 3773 goto lseek_exit; 3774 3775 if (whence == SEEK_DATA && out_data_len == 0) { 3776 rc = -ENXIO; 3777 goto lseek_exit; 3778 } 3779 3780 if (out_data_len < sizeof(struct file_allocated_range_buffer)) { 3781 rc = -EINVAL; 3782 goto lseek_exit; 3783 } 3784 if (whence == SEEK_DATA) { 3785 offset = le64_to_cpu(out_data->file_offset); 3786 goto lseek_exit; 3787 } 3788 if (offset < le64_to_cpu(out_data->file_offset)) 3789 goto lseek_exit; 3790 3791 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length); 3792 3793 lseek_exit: 3794 free_xid(xid); 3795 kfree(out_data); 3796 if (!rc) 3797 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); 3798 else 3799 return rc; 3800 } 3801 3802 static int smb3_fiemap(struct cifs_tcon *tcon, 3803 struct cifsFileInfo *cfile, 3804 struct fiemap_extent_info *fei, u64 start, u64 len) 3805 { 3806 unsigned int xid; 3807 struct file_allocated_range_buffer in_data, *out_data; 3808 u32 out_data_len; 3809 int i, num, rc, flags, last_blob; 3810 u64 next; 3811 3812 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0); 3813 if (rc) 3814 return rc; 3815 3816 xid = get_xid(); 3817 again: 3818 in_data.file_offset = cpu_to_le64(start); 3819 in_data.length = cpu_to_le64(len); 3820 3821 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid, 3822 cfile->fid.volatile_fid, 3823 FSCTL_QUERY_ALLOCATED_RANGES, 3824 (char *)&in_data, sizeof(in_data), 3825 1024 * sizeof(struct file_allocated_range_buffer), 3826 (char **)&out_data, &out_data_len); 3827 if (rc == -E2BIG) { 3828 last_blob = 0; 3829 rc = 0; 3830 } else 3831 last_blob = 1; 3832 if (rc) 3833 goto out; 3834 3835 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) { 3836 rc = -EINVAL; 3837 goto out; 3838 } 3839 if (out_data_len % sizeof(struct file_allocated_range_buffer)) { 3840 rc = -EINVAL; 3841 goto out; 3842 } 3843 3844 num = out_data_len / sizeof(struct file_allocated_range_buffer); 3845 for (i = 0; i < num; i++) { 3846 flags = 0; 3847 if (i == num - 1 && last_blob) 3848 flags |= FIEMAP_EXTENT_LAST; 3849 3850 rc = fiemap_fill_next_extent(fei, 3851 le64_to_cpu(out_data[i].file_offset), 3852 le64_to_cpu(out_data[i].file_offset), 3853 le64_to_cpu(out_data[i].length), 3854 flags); 3855 if (rc < 0) 3856 goto out; 3857 if (rc == 1) { 3858 rc = 0; 3859 goto out; 3860 } 3861 } 3862 3863 if (!last_blob) { 3864 next = le64_to_cpu(out_data[num - 1].file_offset) + 3865 le64_to_cpu(out_data[num - 1].length); 3866 len = len - (next - start); 3867 start = next; 3868 goto again; 3869 } 3870 3871 out: 3872 free_xid(xid); 3873 kfree(out_data); 3874 return rc; 3875 } 3876 3877 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode, 3878 loff_t off, loff_t len) 3879 { 3880 /* KEEP_SIZE already checked for by do_fallocate */ 3881 if (mode & FALLOC_FL_PUNCH_HOLE) 3882 return smb3_punch_hole(file, tcon, off, len); 3883 else if (mode & FALLOC_FL_ZERO_RANGE) { 3884 if (mode & FALLOC_FL_KEEP_SIZE) 3885 return smb3_zero_range(file, tcon, off, len, true); 3886 return smb3_zero_range(file, tcon, off, len, false); 3887 } else if (mode == FALLOC_FL_KEEP_SIZE) 3888 return smb3_simple_falloc(file, tcon, off, len, true); 3889 else if (mode == FALLOC_FL_COLLAPSE_RANGE) 3890 return smb3_collapse_range(file, tcon, off, len); 3891 else if (mode == FALLOC_FL_INSERT_RANGE) 3892 return smb3_insert_range(file, tcon, off, len); 3893 else if (mode == 0) 3894 return smb3_simple_falloc(file, tcon, off, len, false); 3895 3896 return -EOPNOTSUPP; 3897 } 3898 3899 static void 3900 smb2_downgrade_oplock(struct TCP_Server_Info *server, 3901 struct cifsInodeInfo *cinode, __u32 oplock, 3902 unsigned int epoch, bool *purge_cache) 3903 { 3904 server->ops->set_oplock_level(cinode, oplock, 0, NULL); 3905 } 3906 3907 static void 3908 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock, 3909 unsigned int epoch, bool *purge_cache); 3910 3911 static void 3912 smb3_downgrade_oplock(struct TCP_Server_Info *server, 3913 struct cifsInodeInfo *cinode, __u32 oplock, 3914 unsigned int epoch, bool *purge_cache) 3915 { 3916 unsigned int old_state = cinode->oplock; 3917 unsigned int old_epoch = cinode->epoch; 3918 unsigned int new_state; 3919 3920 if (epoch > old_epoch) { 3921 smb21_set_oplock_level(cinode, oplock, 0, NULL); 3922 cinode->epoch = epoch; 3923 } 3924 3925 new_state = cinode->oplock; 3926 *purge_cache = false; 3927 3928 if ((old_state & CIFS_CACHE_READ_FLG) != 0 && 3929 (new_state & CIFS_CACHE_READ_FLG) == 0) 3930 *purge_cache = true; 3931 else if (old_state == new_state && (epoch - old_epoch > 1)) 3932 *purge_cache = true; 3933 } 3934 3935 static void 3936 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock, 3937 unsigned int epoch, bool *purge_cache) 3938 { 3939 oplock &= 0xFF; 3940 cinode->lease_granted = false; 3941 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE) 3942 return; 3943 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) { 3944 cinode->oplock = CIFS_CACHE_RHW_FLG; 3945 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n", 3946 &cinode->netfs.inode); 3947 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 3948 cinode->oplock = CIFS_CACHE_RW_FLG; 3949 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n", 3950 &cinode->netfs.inode); 3951 } else if (oplock == SMB2_OPLOCK_LEVEL_II) { 3952 cinode->oplock = CIFS_CACHE_READ_FLG; 3953 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n", 3954 &cinode->netfs.inode); 3955 } else 3956 cinode->oplock = 0; 3957 } 3958 3959 static void 3960 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock, 3961 unsigned int epoch, bool *purge_cache) 3962 { 3963 char message[5] = {0}; 3964 unsigned int new_oplock = 0; 3965 3966 oplock &= 0xFF; 3967 cinode->lease_granted = true; 3968 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE) 3969 return; 3970 3971 /* Check if the server granted an oplock rather than a lease */ 3972 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE) 3973 return smb2_set_oplock_level(cinode, oplock, epoch, 3974 purge_cache); 3975 3976 if (oplock & SMB2_LEASE_READ_CACHING_HE) { 3977 new_oplock |= CIFS_CACHE_READ_FLG; 3978 strcat(message, "R"); 3979 } 3980 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) { 3981 new_oplock |= CIFS_CACHE_HANDLE_FLG; 3982 strcat(message, "H"); 3983 } 3984 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) { 3985 new_oplock |= CIFS_CACHE_WRITE_FLG; 3986 strcat(message, "W"); 3987 } 3988 if (!new_oplock) 3989 strscpy(message, "None"); 3990 3991 cinode->oplock = new_oplock; 3992 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message, 3993 &cinode->netfs.inode); 3994 } 3995 3996 static void 3997 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock, 3998 unsigned int epoch, bool *purge_cache) 3999 { 4000 unsigned int old_oplock = cinode->oplock; 4001 4002 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache); 4003 4004 if (purge_cache) { 4005 *purge_cache = false; 4006 if (old_oplock == CIFS_CACHE_READ_FLG) { 4007 if (cinode->oplock == CIFS_CACHE_READ_FLG && 4008 (epoch - cinode->epoch > 0)) 4009 *purge_cache = true; 4010 else if (cinode->oplock == CIFS_CACHE_RH_FLG && 4011 (epoch - cinode->epoch > 1)) 4012 *purge_cache = true; 4013 else if (cinode->oplock == CIFS_CACHE_RHW_FLG && 4014 (epoch - cinode->epoch > 1)) 4015 *purge_cache = true; 4016 else if (cinode->oplock == 0 && 4017 (epoch - cinode->epoch > 0)) 4018 *purge_cache = true; 4019 } else if (old_oplock == CIFS_CACHE_RH_FLG) { 4020 if (cinode->oplock == CIFS_CACHE_RH_FLG && 4021 (epoch - cinode->epoch > 0)) 4022 *purge_cache = true; 4023 else if (cinode->oplock == CIFS_CACHE_RHW_FLG && 4024 (epoch - cinode->epoch > 1)) 4025 *purge_cache = true; 4026 } 4027 cinode->epoch = epoch; 4028 } 4029 } 4030 4031 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 4032 static bool 4033 smb2_is_read_op(__u32 oplock) 4034 { 4035 return oplock == SMB2_OPLOCK_LEVEL_II; 4036 } 4037 #endif /* CIFS_ALLOW_INSECURE_LEGACY */ 4038 4039 static bool 4040 smb21_is_read_op(__u32 oplock) 4041 { 4042 return (oplock & SMB2_LEASE_READ_CACHING_HE) && 4043 !(oplock & SMB2_LEASE_WRITE_CACHING_HE); 4044 } 4045 4046 static __le32 4047 map_oplock_to_lease(u8 oplock) 4048 { 4049 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) 4050 return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE; 4051 else if (oplock == SMB2_OPLOCK_LEVEL_II) 4052 return SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE; 4053 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH) 4054 return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE | 4055 SMB2_LEASE_WRITE_CACHING_LE; 4056 return 0; 4057 } 4058 4059 static char * 4060 smb2_create_lease_buf(u8 *lease_key, u8 oplock) 4061 { 4062 struct create_lease *buf; 4063 4064 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL); 4065 if (!buf) 4066 return NULL; 4067 4068 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE); 4069 buf->lcontext.LeaseState = map_oplock_to_lease(oplock); 4070 4071 buf->ccontext.DataOffset = cpu_to_le16(offsetof 4072 (struct create_lease, lcontext)); 4073 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context)); 4074 buf->ccontext.NameOffset = cpu_to_le16(offsetof 4075 (struct create_lease, Name)); 4076 buf->ccontext.NameLength = cpu_to_le16(4); 4077 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */ 4078 buf->Name[0] = 'R'; 4079 buf->Name[1] = 'q'; 4080 buf->Name[2] = 'L'; 4081 buf->Name[3] = 's'; 4082 return (char *)buf; 4083 } 4084 4085 static char * 4086 smb3_create_lease_buf(u8 *lease_key, u8 oplock) 4087 { 4088 struct create_lease_v2 *buf; 4089 4090 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL); 4091 if (!buf) 4092 return NULL; 4093 4094 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE); 4095 buf->lcontext.LeaseState = map_oplock_to_lease(oplock); 4096 4097 buf->ccontext.DataOffset = cpu_to_le16(offsetof 4098 (struct create_lease_v2, lcontext)); 4099 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2)); 4100 buf->ccontext.NameOffset = cpu_to_le16(offsetof 4101 (struct create_lease_v2, Name)); 4102 buf->ccontext.NameLength = cpu_to_le16(4); 4103 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */ 4104 buf->Name[0] = 'R'; 4105 buf->Name[1] = 'q'; 4106 buf->Name[2] = 'L'; 4107 buf->Name[3] = 's'; 4108 return (char *)buf; 4109 } 4110 4111 static __u8 4112 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key) 4113 { 4114 struct create_lease *lc = (struct create_lease *)buf; 4115 4116 *epoch = 0; /* not used */ 4117 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE) 4118 return SMB2_OPLOCK_LEVEL_NOCHANGE; 4119 return le32_to_cpu(lc->lcontext.LeaseState); 4120 } 4121 4122 static __u8 4123 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key) 4124 { 4125 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf; 4126 4127 *epoch = le16_to_cpu(lc->lcontext.Epoch); 4128 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE) 4129 return SMB2_OPLOCK_LEVEL_NOCHANGE; 4130 if (lease_key) 4131 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 4132 return le32_to_cpu(lc->lcontext.LeaseState); 4133 } 4134 4135 static unsigned int 4136 smb2_wp_retry_size(struct inode *inode) 4137 { 4138 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize, 4139 SMB2_MAX_BUFFER_SIZE); 4140 } 4141 4142 static bool 4143 smb2_dir_needs_close(struct cifsFileInfo *cfile) 4144 { 4145 return !cfile->invalidHandle; 4146 } 4147 4148 static void 4149 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len, 4150 struct smb_rqst *old_rq, __le16 cipher_type) 4151 { 4152 struct smb2_hdr *shdr = 4153 (struct smb2_hdr *)old_rq->rq_iov[0].iov_base; 4154 4155 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr)); 4156 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; 4157 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); 4158 tr_hdr->Flags = cpu_to_le16(0x01); 4159 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) || 4160 (cipher_type == SMB2_ENCRYPTION_AES256_GCM)) 4161 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); 4162 else 4163 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); 4164 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8); 4165 } 4166 4167 static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst, 4168 int num_rqst, const u8 *sig, u8 **iv, 4169 struct aead_request **req, struct sg_table *sgt, 4170 unsigned int *num_sgs, size_t *sensitive_size) 4171 { 4172 unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm); 4173 unsigned int iv_size = crypto_aead_ivsize(tfm); 4174 unsigned int len; 4175 u8 *p; 4176 4177 *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig); 4178 if (IS_ERR_VALUE((long)(int)*num_sgs)) 4179 return ERR_PTR(*num_sgs); 4180 4181 len = iv_size; 4182 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1); 4183 len = ALIGN(len, crypto_tfm_ctx_alignment()); 4184 len += req_size; 4185 len = ALIGN(len, __alignof__(struct scatterlist)); 4186 len += array_size(*num_sgs, sizeof(struct scatterlist)); 4187 *sensitive_size = len; 4188 4189 p = kvzalloc(len, GFP_NOFS); 4190 if (!p) 4191 return ERR_PTR(-ENOMEM); 4192 4193 *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1); 4194 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size, 4195 crypto_tfm_ctx_alignment()); 4196 sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size, 4197 __alignof__(struct scatterlist)); 4198 return p; 4199 } 4200 4201 static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst, 4202 int num_rqst, const u8 *sig, u8 **iv, 4203 struct aead_request **req, struct scatterlist **sgl, 4204 size_t *sensitive_size) 4205 { 4206 struct sg_table sgtable = {}; 4207 unsigned int skip, num_sgs, i, j; 4208 ssize_t rc; 4209 void *p; 4210 4211 p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable, 4212 &num_sgs, sensitive_size); 4213 if (IS_ERR(p)) 4214 return ERR_CAST(p); 4215 4216 sg_init_marker(sgtable.sgl, num_sgs); 4217 4218 /* 4219 * The first rqst has a transform header where the 4220 * first 20 bytes are not part of the encrypted blob. 4221 */ 4222 skip = 20; 4223 4224 for (i = 0; i < num_rqst; i++) { 4225 struct iov_iter *iter = &rqst[i].rq_iter; 4226 size_t count = iov_iter_count(iter); 4227 4228 for (j = 0; j < rqst[i].rq_nvec; j++) { 4229 cifs_sg_set_buf(&sgtable, 4230 rqst[i].rq_iov[j].iov_base + skip, 4231 rqst[i].rq_iov[j].iov_len - skip); 4232 4233 /* See the above comment on the 'skip' assignment */ 4234 skip = 0; 4235 } 4236 sgtable.orig_nents = sgtable.nents; 4237 4238 rc = extract_iter_to_sg(iter, count, &sgtable, 4239 num_sgs - sgtable.nents, 0); 4240 iov_iter_revert(iter, rc); 4241 sgtable.orig_nents = sgtable.nents; 4242 } 4243 4244 cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE); 4245 sg_mark_end(&sgtable.sgl[sgtable.nents - 1]); 4246 *sgl = sgtable.sgl; 4247 return p; 4248 } 4249 4250 static int 4251 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key) 4252 { 4253 struct TCP_Server_Info *pserver; 4254 struct cifs_ses *ses; 4255 u8 *ses_enc_key; 4256 4257 /* If server is a channel, select the primary channel */ 4258 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 4259 4260 spin_lock(&cifs_tcp_ses_lock); 4261 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 4262 if (ses->Suid == ses_id) { 4263 spin_lock(&ses->ses_lock); 4264 ses_enc_key = enc ? ses->smb3encryptionkey : 4265 ses->smb3decryptionkey; 4266 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE); 4267 spin_unlock(&ses->ses_lock); 4268 spin_unlock(&cifs_tcp_ses_lock); 4269 return 0; 4270 } 4271 } 4272 spin_unlock(&cifs_tcp_ses_lock); 4273 4274 trace_smb3_ses_not_found(ses_id); 4275 4276 return -EAGAIN; 4277 } 4278 /* 4279 * Encrypt or decrypt @rqst message. @rqst[0] has the following format: 4280 * iov[0] - transform header (associate data), 4281 * iov[1-N] - SMB2 header and pages - data to encrypt. 4282 * On success return encrypted data in iov[1-N] and pages, leave iov[0] 4283 * untouched. 4284 */ 4285 static int 4286 crypt_message(struct TCP_Server_Info *server, int num_rqst, 4287 struct smb_rqst *rqst, int enc, struct crypto_aead *tfm) 4288 { 4289 struct smb2_transform_hdr *tr_hdr = 4290 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base; 4291 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20; 4292 int rc = 0; 4293 struct scatterlist *sg; 4294 u8 sign[SMB2_SIGNATURE_SIZE] = {}; 4295 u8 key[SMB3_ENC_DEC_KEY_SIZE]; 4296 struct aead_request *req; 4297 u8 *iv; 4298 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize); 4299 void *creq; 4300 size_t sensitive_size; 4301 4302 rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key); 4303 if (rc) { 4304 cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__, 4305 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId)); 4306 return rc; 4307 } 4308 4309 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) || 4310 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) 4311 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE); 4312 else 4313 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE); 4314 4315 if (rc) { 4316 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc); 4317 return rc; 4318 } 4319 4320 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE); 4321 if (rc) { 4322 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc); 4323 return rc; 4324 } 4325 4326 creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg, 4327 &sensitive_size); 4328 if (IS_ERR(creq)) 4329 return PTR_ERR(creq); 4330 4331 if (!enc) { 4332 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE); 4333 crypt_len += SMB2_SIGNATURE_SIZE; 4334 } 4335 4336 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) || 4337 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) 4338 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE); 4339 else { 4340 iv[0] = 3; 4341 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE); 4342 } 4343 4344 aead_request_set_tfm(req, tfm); 4345 aead_request_set_crypt(req, sg, sg, crypt_len, iv); 4346 aead_request_set_ad(req, assoc_data_len); 4347 4348 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req); 4349 4350 if (!rc && enc) 4351 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE); 4352 4353 kvfree_sensitive(creq, sensitive_size); 4354 return rc; 4355 } 4356 4357 /* 4358 * Clear a read buffer, discarding the folios which have the 1st mark set. 4359 */ 4360 static void cifs_clear_folioq_buffer(struct folio_queue *buffer) 4361 { 4362 struct folio_queue *folioq; 4363 4364 while ((folioq = buffer)) { 4365 for (int s = 0; s < folioq_count(folioq); s++) 4366 if (folioq_is_marked(folioq, s)) 4367 folio_put(folioq_folio(folioq, s)); 4368 buffer = folioq->next; 4369 kfree(folioq); 4370 } 4371 } 4372 4373 /* 4374 * Allocate buffer space into a folio queue. 4375 */ 4376 static struct folio_queue *cifs_alloc_folioq_buffer(ssize_t size) 4377 { 4378 struct folio_queue *buffer = NULL, *tail = NULL, *p; 4379 struct folio *folio; 4380 unsigned int slot; 4381 4382 do { 4383 if (!tail || folioq_full(tail)) { 4384 p = kmalloc(sizeof(*p), GFP_NOFS); 4385 if (!p) 4386 goto nomem; 4387 folioq_init(p); 4388 if (tail) { 4389 tail->next = p; 4390 p->prev = tail; 4391 } else { 4392 buffer = p; 4393 } 4394 tail = p; 4395 } 4396 4397 folio = folio_alloc(GFP_KERNEL|__GFP_HIGHMEM, 0); 4398 if (!folio) 4399 goto nomem; 4400 4401 slot = folioq_append_mark(tail, folio); 4402 size -= folioq_folio_size(tail, slot); 4403 } while (size > 0); 4404 4405 return buffer; 4406 4407 nomem: 4408 cifs_clear_folioq_buffer(buffer); 4409 return NULL; 4410 } 4411 4412 /* 4413 * Copy data from an iterator to the folios in a folio queue buffer. 4414 */ 4415 static bool cifs_copy_iter_to_folioq(struct iov_iter *iter, size_t size, 4416 struct folio_queue *buffer) 4417 { 4418 for (; buffer; buffer = buffer->next) { 4419 for (int s = 0; s < folioq_count(buffer); s++) { 4420 struct folio *folio = folioq_folio(buffer, s); 4421 size_t part = folioq_folio_size(buffer, s); 4422 4423 part = umin(part, size); 4424 4425 if (copy_folio_from_iter(folio, 0, part, iter) != part) 4426 return false; 4427 size -= part; 4428 } 4429 } 4430 return true; 4431 } 4432 4433 void 4434 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst) 4435 { 4436 for (int i = 0; i < num_rqst; i++) 4437 cifs_clear_folioq_buffer(rqst[i].rq_buffer); 4438 } 4439 4440 /* 4441 * This function will initialize new_rq and encrypt the content. 4442 * The first entry, new_rq[0], only contains a single iov which contains 4443 * a smb2_transform_hdr and is pre-allocated by the caller. 4444 * This function then populates new_rq[1+] with the content from olq_rq[0+]. 4445 * 4446 * The end result is an array of smb_rqst structures where the first structure 4447 * only contains a single iov for the transform header which we then can pass 4448 * to crypt_message(). 4449 * 4450 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller 4451 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests 4452 */ 4453 static int 4454 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst, 4455 struct smb_rqst *new_rq, struct smb_rqst *old_rq) 4456 { 4457 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base; 4458 unsigned int orig_len = 0; 4459 int rc = -ENOMEM; 4460 4461 for (int i = 1; i < num_rqst; i++) { 4462 struct smb_rqst *old = &old_rq[i - 1]; 4463 struct smb_rqst *new = &new_rq[i]; 4464 struct folio_queue *buffer; 4465 size_t size = iov_iter_count(&old->rq_iter); 4466 4467 orig_len += smb_rqst_len(server, old); 4468 new->rq_iov = old->rq_iov; 4469 new->rq_nvec = old->rq_nvec; 4470 4471 if (size > 0) { 4472 buffer = cifs_alloc_folioq_buffer(size); 4473 if (!buffer) 4474 goto err_free; 4475 4476 new->rq_buffer = buffer; 4477 iov_iter_folio_queue(&new->rq_iter, ITER_SOURCE, 4478 buffer, 0, 0, size); 4479 4480 if (!cifs_copy_iter_to_folioq(&old->rq_iter, size, buffer)) { 4481 rc = -EIO; 4482 goto err_free; 4483 } 4484 } 4485 } 4486 4487 /* fill the 1st iov with a transform header */ 4488 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type); 4489 4490 rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc); 4491 cifs_dbg(FYI, "Encrypt message returned %d\n", rc); 4492 if (rc) 4493 goto err_free; 4494 4495 return rc; 4496 4497 err_free: 4498 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]); 4499 return rc; 4500 } 4501 4502 static int 4503 smb3_is_transform_hdr(void *buf) 4504 { 4505 struct smb2_transform_hdr *trhdr = buf; 4506 4507 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; 4508 } 4509 4510 static int 4511 decrypt_raw_data(struct TCP_Server_Info *server, char *buf, 4512 unsigned int buf_data_size, struct iov_iter *iter, 4513 bool is_offloaded) 4514 { 4515 struct crypto_aead *tfm; 4516 struct smb_rqst rqst = {NULL}; 4517 struct kvec iov[2]; 4518 size_t iter_size = 0; 4519 int rc; 4520 4521 iov[0].iov_base = buf; 4522 iov[0].iov_len = sizeof(struct smb2_transform_hdr); 4523 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr); 4524 iov[1].iov_len = buf_data_size; 4525 4526 rqst.rq_iov = iov; 4527 rqst.rq_nvec = 2; 4528 if (iter) { 4529 rqst.rq_iter = *iter; 4530 iter_size = iov_iter_count(iter); 4531 } 4532 4533 if (is_offloaded) { 4534 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) || 4535 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) 4536 tfm = crypto_alloc_aead("gcm(aes)", 0, 0); 4537 else 4538 tfm = crypto_alloc_aead("ccm(aes)", 0, 0); 4539 if (IS_ERR(tfm)) { 4540 rc = PTR_ERR(tfm); 4541 cifs_server_dbg(VFS, "%s: Failed alloc decrypt TFM, rc=%d\n", __func__, rc); 4542 4543 return rc; 4544 } 4545 } else { 4546 if (unlikely(!server->secmech.dec)) 4547 return -EIO; 4548 4549 tfm = server->secmech.dec; 4550 } 4551 4552 rc = crypt_message(server, 1, &rqst, 0, tfm); 4553 cifs_dbg(FYI, "Decrypt message returned %d\n", rc); 4554 4555 if (is_offloaded) 4556 crypto_free_aead(tfm); 4557 4558 if (rc) 4559 return rc; 4560 4561 memmove(buf, iov[1].iov_base, buf_data_size); 4562 4563 if (!is_offloaded) 4564 server->total_read = buf_data_size + iter_size; 4565 4566 return rc; 4567 } 4568 4569 static int 4570 cifs_copy_folioq_to_iter(struct folio_queue *folioq, size_t data_size, 4571 size_t skip, struct iov_iter *iter) 4572 { 4573 for (; folioq; folioq = folioq->next) { 4574 for (int s = 0; s < folioq_count(folioq); s++) { 4575 struct folio *folio = folioq_folio(folioq, s); 4576 size_t fsize = folio_size(folio); 4577 size_t n, len = umin(fsize - skip, data_size); 4578 4579 n = copy_folio_to_iter(folio, skip, len, iter); 4580 if (n != len) { 4581 cifs_dbg(VFS, "%s: something went wrong\n", __func__); 4582 return -EIO; 4583 } 4584 data_size -= n; 4585 skip = 0; 4586 } 4587 } 4588 4589 return 0; 4590 } 4591 4592 static int 4593 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid, 4594 char *buf, unsigned int buf_len, struct folio_queue *buffer, 4595 unsigned int buffer_len, bool is_offloaded) 4596 { 4597 unsigned int data_offset; 4598 unsigned int data_len; 4599 unsigned int cur_off; 4600 unsigned int cur_page_idx; 4601 unsigned int pad_len; 4602 struct cifs_io_subrequest *rdata = mid->callback_data; 4603 struct smb2_hdr *shdr = (struct smb2_hdr *)buf; 4604 int length; 4605 bool use_rdma_mr = false; 4606 4607 if (shdr->Command != SMB2_READ) { 4608 cifs_server_dbg(VFS, "only big read responses are supported\n"); 4609 return -EOPNOTSUPP; 4610 } 4611 4612 if (server->ops->is_session_expired && 4613 server->ops->is_session_expired(buf)) { 4614 if (!is_offloaded) 4615 cifs_reconnect(server, true); 4616 return -1; 4617 } 4618 4619 if (server->ops->is_status_pending && 4620 server->ops->is_status_pending(buf, server)) 4621 return -1; 4622 4623 /* set up first two iov to get credits */ 4624 rdata->iov[0].iov_base = buf; 4625 rdata->iov[0].iov_len = 0; 4626 rdata->iov[1].iov_base = buf; 4627 rdata->iov[1].iov_len = 4628 min_t(unsigned int, buf_len, server->vals->read_rsp_size); 4629 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n", 4630 rdata->iov[0].iov_base, rdata->iov[0].iov_len); 4631 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n", 4632 rdata->iov[1].iov_base, rdata->iov[1].iov_len); 4633 4634 rdata->result = server->ops->map_error(buf, true); 4635 if (rdata->result != 0) { 4636 cifs_dbg(FYI, "%s: server returned error %d\n", 4637 __func__, rdata->result); 4638 /* normal error on read response */ 4639 if (is_offloaded) 4640 mid->mid_state = MID_RESPONSE_RECEIVED; 4641 else 4642 dequeue_mid(mid, false); 4643 return 0; 4644 } 4645 4646 data_offset = server->ops->read_data_offset(buf); 4647 #ifdef CONFIG_CIFS_SMB_DIRECT 4648 use_rdma_mr = rdata->mr; 4649 #endif 4650 data_len = server->ops->read_data_length(buf, use_rdma_mr); 4651 4652 if (data_offset < server->vals->read_rsp_size) { 4653 /* 4654 * win2k8 sometimes sends an offset of 0 when the read 4655 * is beyond the EOF. Treat it as if the data starts just after 4656 * the header. 4657 */ 4658 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n", 4659 __func__, data_offset); 4660 data_offset = server->vals->read_rsp_size; 4661 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) { 4662 /* data_offset is beyond the end of smallbuf */ 4663 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n", 4664 __func__, data_offset); 4665 rdata->result = -EIO; 4666 if (is_offloaded) 4667 mid->mid_state = MID_RESPONSE_MALFORMED; 4668 else 4669 dequeue_mid(mid, rdata->result); 4670 return 0; 4671 } 4672 4673 pad_len = data_offset - server->vals->read_rsp_size; 4674 4675 if (buf_len <= data_offset) { 4676 /* read response payload is in pages */ 4677 cur_page_idx = pad_len / PAGE_SIZE; 4678 cur_off = pad_len % PAGE_SIZE; 4679 4680 if (cur_page_idx != 0) { 4681 /* data offset is beyond the 1st page of response */ 4682 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n", 4683 __func__, data_offset); 4684 rdata->result = -EIO; 4685 if (is_offloaded) 4686 mid->mid_state = MID_RESPONSE_MALFORMED; 4687 else 4688 dequeue_mid(mid, rdata->result); 4689 return 0; 4690 } 4691 4692 if (data_len > buffer_len - pad_len) { 4693 /* data_len is corrupt -- discard frame */ 4694 rdata->result = -EIO; 4695 if (is_offloaded) 4696 mid->mid_state = MID_RESPONSE_MALFORMED; 4697 else 4698 dequeue_mid(mid, rdata->result); 4699 return 0; 4700 } 4701 4702 /* Copy the data to the output I/O iterator. */ 4703 rdata->result = cifs_copy_folioq_to_iter(buffer, buffer_len, 4704 cur_off, &rdata->subreq.io_iter); 4705 if (rdata->result != 0) { 4706 if (is_offloaded) 4707 mid->mid_state = MID_RESPONSE_MALFORMED; 4708 else 4709 dequeue_mid(mid, rdata->result); 4710 return 0; 4711 } 4712 rdata->got_bytes = buffer_len; 4713 4714 } else if (buf_len >= data_offset + data_len) { 4715 /* read response payload is in buf */ 4716 WARN_ONCE(buffer, "read data can be either in buf or in buffer"); 4717 length = copy_to_iter(buf + data_offset, data_len, &rdata->subreq.io_iter); 4718 if (length < 0) 4719 return length; 4720 rdata->got_bytes = data_len; 4721 } else { 4722 /* read response payload cannot be in both buf and pages */ 4723 WARN_ONCE(1, "buf can not contain only a part of read data"); 4724 rdata->result = -EIO; 4725 if (is_offloaded) 4726 mid->mid_state = MID_RESPONSE_MALFORMED; 4727 else 4728 dequeue_mid(mid, rdata->result); 4729 return 0; 4730 } 4731 4732 if (is_offloaded) 4733 mid->mid_state = MID_RESPONSE_RECEIVED; 4734 else 4735 dequeue_mid(mid, false); 4736 return 0; 4737 } 4738 4739 struct smb2_decrypt_work { 4740 struct work_struct decrypt; 4741 struct TCP_Server_Info *server; 4742 struct folio_queue *buffer; 4743 char *buf; 4744 unsigned int len; 4745 }; 4746 4747 4748 static void smb2_decrypt_offload(struct work_struct *work) 4749 { 4750 struct smb2_decrypt_work *dw = container_of(work, 4751 struct smb2_decrypt_work, decrypt); 4752 int rc; 4753 struct mid_q_entry *mid; 4754 struct iov_iter iter; 4755 4756 iov_iter_folio_queue(&iter, ITER_DEST, dw->buffer, 0, 0, dw->len); 4757 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size, 4758 &iter, true); 4759 if (rc) { 4760 cifs_dbg(VFS, "error decrypting rc=%d\n", rc); 4761 goto free_pages; 4762 } 4763 4764 dw->server->lstrp = jiffies; 4765 mid = smb2_find_dequeue_mid(dw->server, dw->buf); 4766 if (mid == NULL) 4767 cifs_dbg(FYI, "mid not found\n"); 4768 else { 4769 mid->decrypted = true; 4770 rc = handle_read_data(dw->server, mid, dw->buf, 4771 dw->server->vals->read_rsp_size, 4772 dw->buffer, dw->len, 4773 true); 4774 if (rc >= 0) { 4775 #ifdef CONFIG_CIFS_STATS2 4776 mid->when_received = jiffies; 4777 #endif 4778 if (dw->server->ops->is_network_name_deleted) 4779 dw->server->ops->is_network_name_deleted(dw->buf, 4780 dw->server); 4781 4782 mid->callback(mid); 4783 } else { 4784 spin_lock(&dw->server->srv_lock); 4785 if (dw->server->tcpStatus == CifsNeedReconnect) { 4786 spin_lock(&dw->server->mid_lock); 4787 mid->mid_state = MID_RETRY_NEEDED; 4788 spin_unlock(&dw->server->mid_lock); 4789 spin_unlock(&dw->server->srv_lock); 4790 mid->callback(mid); 4791 } else { 4792 spin_lock(&dw->server->mid_lock); 4793 mid->mid_state = MID_REQUEST_SUBMITTED; 4794 mid->mid_flags &= ~(MID_DELETED); 4795 list_add_tail(&mid->qhead, 4796 &dw->server->pending_mid_q); 4797 spin_unlock(&dw->server->mid_lock); 4798 spin_unlock(&dw->server->srv_lock); 4799 } 4800 } 4801 release_mid(mid); 4802 } 4803 4804 free_pages: 4805 cifs_clear_folioq_buffer(dw->buffer); 4806 cifs_small_buf_release(dw->buf); 4807 kfree(dw); 4808 } 4809 4810 4811 static int 4812 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid, 4813 int *num_mids) 4814 { 4815 char *buf = server->smallbuf; 4816 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; 4817 struct iov_iter iter; 4818 unsigned int len; 4819 unsigned int buflen = server->pdu_size; 4820 int rc; 4821 struct smb2_decrypt_work *dw; 4822 4823 dw = kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL); 4824 if (!dw) 4825 return -ENOMEM; 4826 INIT_WORK(&dw->decrypt, smb2_decrypt_offload); 4827 dw->server = server; 4828 4829 *num_mids = 1; 4830 len = min_t(unsigned int, buflen, server->vals->read_rsp_size + 4831 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1; 4832 4833 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len); 4834 if (rc < 0) 4835 goto free_dw; 4836 server->total_read += rc; 4837 4838 len = le32_to_cpu(tr_hdr->OriginalMessageSize) - 4839 server->vals->read_rsp_size; 4840 dw->len = len; 4841 len = round_up(dw->len, PAGE_SIZE); 4842 4843 rc = -ENOMEM; 4844 dw->buffer = cifs_alloc_folioq_buffer(len); 4845 if (!dw->buffer) 4846 goto discard_data; 4847 4848 iov_iter_folio_queue(&iter, ITER_DEST, dw->buffer, 0, 0, len); 4849 4850 /* Read the data into the buffer and clear excess bufferage. */ 4851 rc = cifs_read_iter_from_socket(server, &iter, dw->len); 4852 if (rc < 0) 4853 goto discard_data; 4854 4855 server->total_read += rc; 4856 if (rc < len) { 4857 struct iov_iter tmp = iter; 4858 4859 iov_iter_advance(&tmp, rc); 4860 iov_iter_zero(len - rc, &tmp); 4861 } 4862 iov_iter_truncate(&iter, dw->len); 4863 4864 rc = cifs_discard_remaining_data(server); 4865 if (rc) 4866 goto free_pages; 4867 4868 /* 4869 * For large reads, offload to different thread for better performance, 4870 * use more cores decrypting which can be expensive 4871 */ 4872 4873 if ((server->min_offload) && (server->in_flight > 1) && 4874 (server->pdu_size >= server->min_offload)) { 4875 dw->buf = server->smallbuf; 4876 server->smallbuf = (char *)cifs_small_buf_get(); 4877 4878 queue_work(decrypt_wq, &dw->decrypt); 4879 *num_mids = 0; /* worker thread takes care of finding mid */ 4880 return -1; 4881 } 4882 4883 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size, 4884 &iter, false); 4885 if (rc) 4886 goto free_pages; 4887 4888 *mid = smb2_find_mid(server, buf); 4889 if (*mid == NULL) { 4890 cifs_dbg(FYI, "mid not found\n"); 4891 } else { 4892 cifs_dbg(FYI, "mid found\n"); 4893 (*mid)->decrypted = true; 4894 rc = handle_read_data(server, *mid, buf, 4895 server->vals->read_rsp_size, 4896 dw->buffer, dw->len, false); 4897 if (rc >= 0) { 4898 if (server->ops->is_network_name_deleted) { 4899 server->ops->is_network_name_deleted(buf, 4900 server); 4901 } 4902 } 4903 } 4904 4905 free_pages: 4906 cifs_clear_folioq_buffer(dw->buffer); 4907 free_dw: 4908 kfree(dw); 4909 return rc; 4910 discard_data: 4911 cifs_discard_remaining_data(server); 4912 goto free_pages; 4913 } 4914 4915 static int 4916 receive_encrypted_standard(struct TCP_Server_Info *server, 4917 struct mid_q_entry **mids, char **bufs, 4918 int *num_mids) 4919 { 4920 int ret, length; 4921 char *buf = server->smallbuf; 4922 struct smb2_hdr *shdr; 4923 unsigned int pdu_length = server->pdu_size; 4924 unsigned int buf_size; 4925 unsigned int next_cmd; 4926 struct mid_q_entry *mid_entry; 4927 int next_is_large; 4928 char *next_buffer = NULL; 4929 4930 *num_mids = 0; 4931 4932 /* switch to large buffer if too big for a small one */ 4933 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) { 4934 server->large_buf = true; 4935 memcpy(server->bigbuf, buf, server->total_read); 4936 buf = server->bigbuf; 4937 } 4938 4939 /* now read the rest */ 4940 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, 4941 pdu_length - HEADER_SIZE(server) + 1); 4942 if (length < 0) 4943 return length; 4944 server->total_read += length; 4945 4946 buf_size = pdu_length - sizeof(struct smb2_transform_hdr); 4947 length = decrypt_raw_data(server, buf, buf_size, NULL, false); 4948 if (length) 4949 return length; 4950 4951 next_is_large = server->large_buf; 4952 one_more: 4953 shdr = (struct smb2_hdr *)buf; 4954 next_cmd = le32_to_cpu(shdr->NextCommand); 4955 if (next_cmd) { 4956 if (WARN_ON_ONCE(next_cmd > pdu_length)) 4957 return -1; 4958 if (next_is_large) 4959 next_buffer = (char *)cifs_buf_get(); 4960 else 4961 next_buffer = (char *)cifs_small_buf_get(); 4962 memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd); 4963 } 4964 4965 mid_entry = smb2_find_mid(server, buf); 4966 if (mid_entry == NULL) 4967 cifs_dbg(FYI, "mid not found\n"); 4968 else { 4969 cifs_dbg(FYI, "mid found\n"); 4970 mid_entry->decrypted = true; 4971 mid_entry->resp_buf_size = server->pdu_size; 4972 } 4973 4974 if (*num_mids >= MAX_COMPOUND) { 4975 cifs_server_dbg(VFS, "too many PDUs in compound\n"); 4976 return -1; 4977 } 4978 bufs[*num_mids] = buf; 4979 mids[(*num_mids)++] = mid_entry; 4980 4981 if (mid_entry && mid_entry->handle) 4982 ret = mid_entry->handle(server, mid_entry); 4983 else 4984 ret = cifs_handle_standard(server, mid_entry); 4985 4986 if (ret == 0 && next_cmd) { 4987 pdu_length -= next_cmd; 4988 server->large_buf = next_is_large; 4989 if (next_is_large) 4990 server->bigbuf = buf = next_buffer; 4991 else 4992 server->smallbuf = buf = next_buffer; 4993 goto one_more; 4994 } else if (ret != 0) { 4995 /* 4996 * ret != 0 here means that we didn't get to handle_mid() thus 4997 * server->smallbuf and server->bigbuf are still valid. We need 4998 * to free next_buffer because it is not going to be used 4999 * anywhere. 5000 */ 5001 if (next_is_large) 5002 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer); 5003 else 5004 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer); 5005 } 5006 5007 return ret; 5008 } 5009 5010 static int 5011 smb3_receive_transform(struct TCP_Server_Info *server, 5012 struct mid_q_entry **mids, char **bufs, int *num_mids) 5013 { 5014 char *buf = server->smallbuf; 5015 unsigned int pdu_length = server->pdu_size; 5016 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; 5017 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize); 5018 5019 if (pdu_length < sizeof(struct smb2_transform_hdr) + 5020 sizeof(struct smb2_hdr)) { 5021 cifs_server_dbg(VFS, "Transform message is too small (%u)\n", 5022 pdu_length); 5023 cifs_reconnect(server, true); 5024 return -ECONNABORTED; 5025 } 5026 5027 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) { 5028 cifs_server_dbg(VFS, "Transform message is broken\n"); 5029 cifs_reconnect(server, true); 5030 return -ECONNABORTED; 5031 } 5032 5033 /* TODO: add support for compounds containing READ. */ 5034 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) { 5035 return receive_encrypted_read(server, &mids[0], num_mids); 5036 } 5037 5038 return receive_encrypted_standard(server, mids, bufs, num_mids); 5039 } 5040 5041 int 5042 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid) 5043 { 5044 char *buf = server->large_buf ? server->bigbuf : server->smallbuf; 5045 5046 return handle_read_data(server, mid, buf, server->pdu_size, 5047 NULL, 0, false); 5048 } 5049 5050 static int smb2_next_header(struct TCP_Server_Info *server, char *buf, 5051 unsigned int *noff) 5052 { 5053 struct smb2_hdr *hdr = (struct smb2_hdr *)buf; 5054 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf; 5055 5056 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) { 5057 *noff = le32_to_cpu(t_hdr->OriginalMessageSize); 5058 if (unlikely(check_add_overflow(*noff, sizeof(*t_hdr), noff))) 5059 return -EINVAL; 5060 } else { 5061 *noff = le32_to_cpu(hdr->NextCommand); 5062 } 5063 if (unlikely(*noff && *noff < MID_HEADER_SIZE(server))) 5064 return -EINVAL; 5065 return 0; 5066 } 5067 5068 int __cifs_sfu_make_node(unsigned int xid, struct inode *inode, 5069 struct dentry *dentry, struct cifs_tcon *tcon, 5070 const char *full_path, umode_t mode, dev_t dev, 5071 const char *symname) 5072 { 5073 struct TCP_Server_Info *server = tcon->ses->server; 5074 struct cifs_open_parms oparms; 5075 struct cifs_io_parms io_parms = {}; 5076 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 5077 struct cifs_fid fid; 5078 unsigned int bytes_written; 5079 u8 type[8]; 5080 int type_len = 0; 5081 struct { 5082 __le64 major; 5083 __le64 minor; 5084 } __packed pdev = {}; 5085 __le16 *symname_utf16 = NULL; 5086 u8 *data = NULL; 5087 int data_len = 0; 5088 struct kvec iov[3]; 5089 __u32 oplock = server->oplocks ? REQ_OPLOCK : 0; 5090 int rc; 5091 5092 switch (mode & S_IFMT) { 5093 case S_IFCHR: 5094 type_len = 8; 5095 memcpy(type, "IntxCHR\0", type_len); 5096 pdev.major = cpu_to_le64(MAJOR(dev)); 5097 pdev.minor = cpu_to_le64(MINOR(dev)); 5098 data = (u8 *)&pdev; 5099 data_len = sizeof(pdev); 5100 break; 5101 case S_IFBLK: 5102 type_len = 8; 5103 memcpy(type, "IntxBLK\0", type_len); 5104 pdev.major = cpu_to_le64(MAJOR(dev)); 5105 pdev.minor = cpu_to_le64(MINOR(dev)); 5106 data = (u8 *)&pdev; 5107 data_len = sizeof(pdev); 5108 break; 5109 case S_IFLNK: 5110 type_len = 8; 5111 memcpy(type, "IntxLNK\1", type_len); 5112 symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname), 5113 &data_len, cifs_sb->local_nls, 5114 NO_MAP_UNI_RSVD); 5115 if (!symname_utf16) { 5116 rc = -ENOMEM; 5117 goto out; 5118 } 5119 data_len -= 2; /* symlink is without trailing wide-nul */ 5120 data = (u8 *)symname_utf16; 5121 break; 5122 case S_IFSOCK: 5123 type_len = 8; 5124 strscpy(type, "LnxSOCK"); 5125 data = (u8 *)&pdev; 5126 data_len = sizeof(pdev); 5127 break; 5128 case S_IFIFO: 5129 type_len = 8; 5130 strscpy(type, "LnxFIFO"); 5131 data = (u8 *)&pdev; 5132 data_len = sizeof(pdev); 5133 break; 5134 default: 5135 rc = -EPERM; 5136 goto out; 5137 } 5138 5139 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, GENERIC_WRITE, 5140 FILE_CREATE, CREATE_NOT_DIR | 5141 CREATE_OPTION_SPECIAL, ACL_NO_MODE); 5142 oparms.fid = &fid; 5143 5144 rc = server->ops->open(xid, &oparms, &oplock, NULL); 5145 if (rc) 5146 goto out; 5147 5148 if (type_len + data_len > 0) { 5149 io_parms.pid = current->tgid; 5150 io_parms.tcon = tcon; 5151 io_parms.length = type_len + data_len; 5152 iov[1].iov_base = type; 5153 iov[1].iov_len = type_len; 5154 iov[2].iov_base = data; 5155 iov[2].iov_len = data_len; 5156 5157 rc = server->ops->sync_write(xid, &fid, &io_parms, 5158 &bytes_written, 5159 iov, ARRAY_SIZE(iov)-1); 5160 } 5161 5162 server->ops->close(xid, tcon, &fid); 5163 5164 out: 5165 kfree(symname_utf16); 5166 return rc; 5167 } 5168 5169 int cifs_sfu_make_node(unsigned int xid, struct inode *inode, 5170 struct dentry *dentry, struct cifs_tcon *tcon, 5171 const char *full_path, umode_t mode, dev_t dev) 5172 { 5173 struct inode *new = NULL; 5174 int rc; 5175 5176 rc = __cifs_sfu_make_node(xid, inode, dentry, tcon, 5177 full_path, mode, dev, NULL); 5178 if (rc) 5179 return rc; 5180 5181 if (tcon->posix_extensions) { 5182 rc = smb311_posix_get_inode_info(&new, full_path, NULL, 5183 inode->i_sb, xid); 5184 } else if (tcon->unix_ext) { 5185 rc = cifs_get_inode_info_unix(&new, full_path, 5186 inode->i_sb, xid); 5187 } else { 5188 rc = cifs_get_inode_info(&new, full_path, NULL, 5189 inode->i_sb, xid, NULL); 5190 } 5191 if (!rc) 5192 d_instantiate(dentry, new); 5193 return rc; 5194 } 5195 5196 static int smb2_make_node(unsigned int xid, struct inode *inode, 5197 struct dentry *dentry, struct cifs_tcon *tcon, 5198 const char *full_path, umode_t mode, dev_t dev) 5199 { 5200 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 5201 int rc; 5202 5203 /* 5204 * Check if mounted with mount parm 'sfu' mount parm. 5205 * SFU emulation should work with all servers, but only 5206 * supports block and char device, socket & fifo, 5207 * and was used by default in earlier versions of Windows 5208 */ 5209 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { 5210 rc = cifs_sfu_make_node(xid, inode, dentry, tcon, 5211 full_path, mode, dev); 5212 } else { 5213 rc = smb2_mknod_reparse(xid, inode, dentry, tcon, 5214 full_path, mode, dev); 5215 } 5216 return rc; 5217 } 5218 5219 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 5220 struct smb_version_operations smb20_operations = { 5221 .compare_fids = smb2_compare_fids, 5222 .setup_request = smb2_setup_request, 5223 .setup_async_request = smb2_setup_async_request, 5224 .check_receive = smb2_check_receive, 5225 .add_credits = smb2_add_credits, 5226 .set_credits = smb2_set_credits, 5227 .get_credits_field = smb2_get_credits_field, 5228 .get_credits = smb2_get_credits, 5229 .wait_mtu_credits = cifs_wait_mtu_credits, 5230 .get_next_mid = smb2_get_next_mid, 5231 .revert_current_mid = smb2_revert_current_mid, 5232 .read_data_offset = smb2_read_data_offset, 5233 .read_data_length = smb2_read_data_length, 5234 .map_error = map_smb2_to_linux_error, 5235 .find_mid = smb2_find_mid, 5236 .check_message = smb2_check_message, 5237 .dump_detail = smb2_dump_detail, 5238 .clear_stats = smb2_clear_stats, 5239 .print_stats = smb2_print_stats, 5240 .is_oplock_break = smb2_is_valid_oplock_break, 5241 .handle_cancelled_mid = smb2_handle_cancelled_mid, 5242 .downgrade_oplock = smb2_downgrade_oplock, 5243 .need_neg = smb2_need_neg, 5244 .negotiate = smb2_negotiate, 5245 .negotiate_wsize = smb2_negotiate_wsize, 5246 .negotiate_rsize = smb2_negotiate_rsize, 5247 .sess_setup = SMB2_sess_setup, 5248 .logoff = SMB2_logoff, 5249 .tree_connect = SMB2_tcon, 5250 .tree_disconnect = SMB2_tdis, 5251 .qfs_tcon = smb2_qfs_tcon, 5252 .is_path_accessible = smb2_is_path_accessible, 5253 .can_echo = smb2_can_echo, 5254 .echo = SMB2_echo, 5255 .query_path_info = smb2_query_path_info, 5256 .query_reparse_point = smb2_query_reparse_point, 5257 .get_srv_inum = smb2_get_srv_inum, 5258 .query_file_info = smb2_query_file_info, 5259 .set_path_size = smb2_set_path_size, 5260 .set_file_size = smb2_set_file_size, 5261 .set_file_info = smb2_set_file_info, 5262 .set_compression = smb2_set_compression, 5263 .mkdir = smb2_mkdir, 5264 .mkdir_setinfo = smb2_mkdir_setinfo, 5265 .rmdir = smb2_rmdir, 5266 .unlink = smb2_unlink, 5267 .rename = smb2_rename_path, 5268 .create_hardlink = smb2_create_hardlink, 5269 .parse_reparse_point = smb2_parse_reparse_point, 5270 .query_mf_symlink = smb3_query_mf_symlink, 5271 .create_mf_symlink = smb3_create_mf_symlink, 5272 .create_reparse_symlink = smb2_create_reparse_symlink, 5273 .open = smb2_open_file, 5274 .set_fid = smb2_set_fid, 5275 .close = smb2_close_file, 5276 .flush = smb2_flush_file, 5277 .async_readv = smb2_async_readv, 5278 .async_writev = smb2_async_writev, 5279 .sync_read = smb2_sync_read, 5280 .sync_write = smb2_sync_write, 5281 .query_dir_first = smb2_query_dir_first, 5282 .query_dir_next = smb2_query_dir_next, 5283 .close_dir = smb2_close_dir, 5284 .calc_smb_size = smb2_calc_size, 5285 .is_status_pending = smb2_is_status_pending, 5286 .is_session_expired = smb2_is_session_expired, 5287 .oplock_response = smb2_oplock_response, 5288 .queryfs = smb2_queryfs, 5289 .mand_lock = smb2_mand_lock, 5290 .mand_unlock_range = smb2_unlock_range, 5291 .push_mand_locks = smb2_push_mandatory_locks, 5292 .get_lease_key = smb2_get_lease_key, 5293 .set_lease_key = smb2_set_lease_key, 5294 .new_lease_key = smb2_new_lease_key, 5295 .calc_signature = smb2_calc_signature, 5296 .is_read_op = smb2_is_read_op, 5297 .set_oplock_level = smb2_set_oplock_level, 5298 .create_lease_buf = smb2_create_lease_buf, 5299 .parse_lease_buf = smb2_parse_lease_buf, 5300 .copychunk_range = smb2_copychunk_range, 5301 .wp_retry_size = smb2_wp_retry_size, 5302 .dir_needs_close = smb2_dir_needs_close, 5303 .get_dfs_refer = smb2_get_dfs_refer, 5304 .select_sectype = smb2_select_sectype, 5305 #ifdef CONFIG_CIFS_XATTR 5306 .query_all_EAs = smb2_query_eas, 5307 .set_EA = smb2_set_ea, 5308 #endif /* CIFS_XATTR */ 5309 .get_acl = get_smb2_acl, 5310 .get_acl_by_fid = get_smb2_acl_by_fid, 5311 .set_acl = set_smb2_acl, 5312 .next_header = smb2_next_header, 5313 .ioctl_query_info = smb2_ioctl_query_info, 5314 .make_node = smb2_make_node, 5315 .fiemap = smb3_fiemap, 5316 .llseek = smb3_llseek, 5317 .is_status_io_timeout = smb2_is_status_io_timeout, 5318 .is_network_name_deleted = smb2_is_network_name_deleted, 5319 }; 5320 #endif /* CIFS_ALLOW_INSECURE_LEGACY */ 5321 5322 struct smb_version_operations smb21_operations = { 5323 .compare_fids = smb2_compare_fids, 5324 .setup_request = smb2_setup_request, 5325 .setup_async_request = smb2_setup_async_request, 5326 .check_receive = smb2_check_receive, 5327 .add_credits = smb2_add_credits, 5328 .set_credits = smb2_set_credits, 5329 .get_credits_field = smb2_get_credits_field, 5330 .get_credits = smb2_get_credits, 5331 .wait_mtu_credits = smb2_wait_mtu_credits, 5332 .adjust_credits = smb2_adjust_credits, 5333 .get_next_mid = smb2_get_next_mid, 5334 .revert_current_mid = smb2_revert_current_mid, 5335 .read_data_offset = smb2_read_data_offset, 5336 .read_data_length = smb2_read_data_length, 5337 .map_error = map_smb2_to_linux_error, 5338 .find_mid = smb2_find_mid, 5339 .check_message = smb2_check_message, 5340 .dump_detail = smb2_dump_detail, 5341 .clear_stats = smb2_clear_stats, 5342 .print_stats = smb2_print_stats, 5343 .is_oplock_break = smb2_is_valid_oplock_break, 5344 .handle_cancelled_mid = smb2_handle_cancelled_mid, 5345 .downgrade_oplock = smb2_downgrade_oplock, 5346 .need_neg = smb2_need_neg, 5347 .negotiate = smb2_negotiate, 5348 .negotiate_wsize = smb2_negotiate_wsize, 5349 .negotiate_rsize = smb2_negotiate_rsize, 5350 .sess_setup = SMB2_sess_setup, 5351 .logoff = SMB2_logoff, 5352 .tree_connect = SMB2_tcon, 5353 .tree_disconnect = SMB2_tdis, 5354 .qfs_tcon = smb2_qfs_tcon, 5355 .is_path_accessible = smb2_is_path_accessible, 5356 .can_echo = smb2_can_echo, 5357 .echo = SMB2_echo, 5358 .query_path_info = smb2_query_path_info, 5359 .query_reparse_point = smb2_query_reparse_point, 5360 .get_srv_inum = smb2_get_srv_inum, 5361 .query_file_info = smb2_query_file_info, 5362 .set_path_size = smb2_set_path_size, 5363 .set_file_size = smb2_set_file_size, 5364 .set_file_info = smb2_set_file_info, 5365 .set_compression = smb2_set_compression, 5366 .mkdir = smb2_mkdir, 5367 .mkdir_setinfo = smb2_mkdir_setinfo, 5368 .rmdir = smb2_rmdir, 5369 .unlink = smb2_unlink, 5370 .rename = smb2_rename_path, 5371 .create_hardlink = smb2_create_hardlink, 5372 .parse_reparse_point = smb2_parse_reparse_point, 5373 .query_mf_symlink = smb3_query_mf_symlink, 5374 .create_mf_symlink = smb3_create_mf_symlink, 5375 .create_reparse_symlink = smb2_create_reparse_symlink, 5376 .open = smb2_open_file, 5377 .set_fid = smb2_set_fid, 5378 .close = smb2_close_file, 5379 .flush = smb2_flush_file, 5380 .async_readv = smb2_async_readv, 5381 .async_writev = smb2_async_writev, 5382 .sync_read = smb2_sync_read, 5383 .sync_write = smb2_sync_write, 5384 .query_dir_first = smb2_query_dir_first, 5385 .query_dir_next = smb2_query_dir_next, 5386 .close_dir = smb2_close_dir, 5387 .calc_smb_size = smb2_calc_size, 5388 .is_status_pending = smb2_is_status_pending, 5389 .is_session_expired = smb2_is_session_expired, 5390 .oplock_response = smb2_oplock_response, 5391 .queryfs = smb2_queryfs, 5392 .mand_lock = smb2_mand_lock, 5393 .mand_unlock_range = smb2_unlock_range, 5394 .push_mand_locks = smb2_push_mandatory_locks, 5395 .get_lease_key = smb2_get_lease_key, 5396 .set_lease_key = smb2_set_lease_key, 5397 .new_lease_key = smb2_new_lease_key, 5398 .calc_signature = smb2_calc_signature, 5399 .is_read_op = smb21_is_read_op, 5400 .set_oplock_level = smb21_set_oplock_level, 5401 .create_lease_buf = smb2_create_lease_buf, 5402 .parse_lease_buf = smb2_parse_lease_buf, 5403 .copychunk_range = smb2_copychunk_range, 5404 .wp_retry_size = smb2_wp_retry_size, 5405 .dir_needs_close = smb2_dir_needs_close, 5406 .enum_snapshots = smb3_enum_snapshots, 5407 .notify = smb3_notify, 5408 .get_dfs_refer = smb2_get_dfs_refer, 5409 .select_sectype = smb2_select_sectype, 5410 #ifdef CONFIG_CIFS_XATTR 5411 .query_all_EAs = smb2_query_eas, 5412 .set_EA = smb2_set_ea, 5413 #endif /* CIFS_XATTR */ 5414 .get_acl = get_smb2_acl, 5415 .get_acl_by_fid = get_smb2_acl_by_fid, 5416 .set_acl = set_smb2_acl, 5417 .next_header = smb2_next_header, 5418 .ioctl_query_info = smb2_ioctl_query_info, 5419 .make_node = smb2_make_node, 5420 .fiemap = smb3_fiemap, 5421 .llseek = smb3_llseek, 5422 .is_status_io_timeout = smb2_is_status_io_timeout, 5423 .is_network_name_deleted = smb2_is_network_name_deleted, 5424 }; 5425 5426 struct smb_version_operations smb30_operations = { 5427 .compare_fids = smb2_compare_fids, 5428 .setup_request = smb2_setup_request, 5429 .setup_async_request = smb2_setup_async_request, 5430 .check_receive = smb2_check_receive, 5431 .add_credits = smb2_add_credits, 5432 .set_credits = smb2_set_credits, 5433 .get_credits_field = smb2_get_credits_field, 5434 .get_credits = smb2_get_credits, 5435 .wait_mtu_credits = smb2_wait_mtu_credits, 5436 .adjust_credits = smb2_adjust_credits, 5437 .get_next_mid = smb2_get_next_mid, 5438 .revert_current_mid = smb2_revert_current_mid, 5439 .read_data_offset = smb2_read_data_offset, 5440 .read_data_length = smb2_read_data_length, 5441 .map_error = map_smb2_to_linux_error, 5442 .find_mid = smb2_find_mid, 5443 .check_message = smb2_check_message, 5444 .dump_detail = smb2_dump_detail, 5445 .clear_stats = smb2_clear_stats, 5446 .print_stats = smb2_print_stats, 5447 .dump_share_caps = smb2_dump_share_caps, 5448 .is_oplock_break = smb2_is_valid_oplock_break, 5449 .handle_cancelled_mid = smb2_handle_cancelled_mid, 5450 .downgrade_oplock = smb3_downgrade_oplock, 5451 .need_neg = smb2_need_neg, 5452 .negotiate = smb2_negotiate, 5453 .negotiate_wsize = smb3_negotiate_wsize, 5454 .negotiate_rsize = smb3_negotiate_rsize, 5455 .sess_setup = SMB2_sess_setup, 5456 .logoff = SMB2_logoff, 5457 .tree_connect = SMB2_tcon, 5458 .tree_disconnect = SMB2_tdis, 5459 .qfs_tcon = smb3_qfs_tcon, 5460 .query_server_interfaces = SMB3_request_interfaces, 5461 .is_path_accessible = smb2_is_path_accessible, 5462 .can_echo = smb2_can_echo, 5463 .echo = SMB2_echo, 5464 .query_path_info = smb2_query_path_info, 5465 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */ 5466 .query_reparse_point = smb2_query_reparse_point, 5467 .get_srv_inum = smb2_get_srv_inum, 5468 .query_file_info = smb2_query_file_info, 5469 .set_path_size = smb2_set_path_size, 5470 .set_file_size = smb2_set_file_size, 5471 .set_file_info = smb2_set_file_info, 5472 .set_compression = smb2_set_compression, 5473 .mkdir = smb2_mkdir, 5474 .mkdir_setinfo = smb2_mkdir_setinfo, 5475 .rmdir = smb2_rmdir, 5476 .unlink = smb2_unlink, 5477 .rename = smb2_rename_path, 5478 .create_hardlink = smb2_create_hardlink, 5479 .parse_reparse_point = smb2_parse_reparse_point, 5480 .query_mf_symlink = smb3_query_mf_symlink, 5481 .create_mf_symlink = smb3_create_mf_symlink, 5482 .create_reparse_symlink = smb2_create_reparse_symlink, 5483 .open = smb2_open_file, 5484 .set_fid = smb2_set_fid, 5485 .close = smb2_close_file, 5486 .close_getattr = smb2_close_getattr, 5487 .flush = smb2_flush_file, 5488 .async_readv = smb2_async_readv, 5489 .async_writev = smb2_async_writev, 5490 .sync_read = smb2_sync_read, 5491 .sync_write = smb2_sync_write, 5492 .query_dir_first = smb2_query_dir_first, 5493 .query_dir_next = smb2_query_dir_next, 5494 .close_dir = smb2_close_dir, 5495 .calc_smb_size = smb2_calc_size, 5496 .is_status_pending = smb2_is_status_pending, 5497 .is_session_expired = smb2_is_session_expired, 5498 .oplock_response = smb2_oplock_response, 5499 .queryfs = smb2_queryfs, 5500 .mand_lock = smb2_mand_lock, 5501 .mand_unlock_range = smb2_unlock_range, 5502 .push_mand_locks = smb2_push_mandatory_locks, 5503 .get_lease_key = smb2_get_lease_key, 5504 .set_lease_key = smb2_set_lease_key, 5505 .new_lease_key = smb2_new_lease_key, 5506 .generate_signingkey = generate_smb30signingkey, 5507 .calc_signature = smb3_calc_signature, 5508 .set_integrity = smb3_set_integrity, 5509 .is_read_op = smb21_is_read_op, 5510 .set_oplock_level = smb3_set_oplock_level, 5511 .create_lease_buf = smb3_create_lease_buf, 5512 .parse_lease_buf = smb3_parse_lease_buf, 5513 .copychunk_range = smb2_copychunk_range, 5514 .duplicate_extents = smb2_duplicate_extents, 5515 .validate_negotiate = smb3_validate_negotiate, 5516 .wp_retry_size = smb2_wp_retry_size, 5517 .dir_needs_close = smb2_dir_needs_close, 5518 .fallocate = smb3_fallocate, 5519 .enum_snapshots = smb3_enum_snapshots, 5520 .notify = smb3_notify, 5521 .init_transform_rq = smb3_init_transform_rq, 5522 .is_transform_hdr = smb3_is_transform_hdr, 5523 .receive_transform = smb3_receive_transform, 5524 .get_dfs_refer = smb2_get_dfs_refer, 5525 .select_sectype = smb2_select_sectype, 5526 #ifdef CONFIG_CIFS_XATTR 5527 .query_all_EAs = smb2_query_eas, 5528 .set_EA = smb2_set_ea, 5529 #endif /* CIFS_XATTR */ 5530 .get_acl = get_smb2_acl, 5531 .get_acl_by_fid = get_smb2_acl_by_fid, 5532 .set_acl = set_smb2_acl, 5533 .next_header = smb2_next_header, 5534 .ioctl_query_info = smb2_ioctl_query_info, 5535 .make_node = smb2_make_node, 5536 .fiemap = smb3_fiemap, 5537 .llseek = smb3_llseek, 5538 .is_status_io_timeout = smb2_is_status_io_timeout, 5539 .is_network_name_deleted = smb2_is_network_name_deleted, 5540 }; 5541 5542 struct smb_version_operations smb311_operations = { 5543 .compare_fids = smb2_compare_fids, 5544 .setup_request = smb2_setup_request, 5545 .setup_async_request = smb2_setup_async_request, 5546 .check_receive = smb2_check_receive, 5547 .add_credits = smb2_add_credits, 5548 .set_credits = smb2_set_credits, 5549 .get_credits_field = smb2_get_credits_field, 5550 .get_credits = smb2_get_credits, 5551 .wait_mtu_credits = smb2_wait_mtu_credits, 5552 .adjust_credits = smb2_adjust_credits, 5553 .get_next_mid = smb2_get_next_mid, 5554 .revert_current_mid = smb2_revert_current_mid, 5555 .read_data_offset = smb2_read_data_offset, 5556 .read_data_length = smb2_read_data_length, 5557 .map_error = map_smb2_to_linux_error, 5558 .find_mid = smb2_find_mid, 5559 .check_message = smb2_check_message, 5560 .dump_detail = smb2_dump_detail, 5561 .clear_stats = smb2_clear_stats, 5562 .print_stats = smb2_print_stats, 5563 .dump_share_caps = smb2_dump_share_caps, 5564 .is_oplock_break = smb2_is_valid_oplock_break, 5565 .handle_cancelled_mid = smb2_handle_cancelled_mid, 5566 .downgrade_oplock = smb3_downgrade_oplock, 5567 .need_neg = smb2_need_neg, 5568 .negotiate = smb2_negotiate, 5569 .negotiate_wsize = smb3_negotiate_wsize, 5570 .negotiate_rsize = smb3_negotiate_rsize, 5571 .sess_setup = SMB2_sess_setup, 5572 .logoff = SMB2_logoff, 5573 .tree_connect = SMB2_tcon, 5574 .tree_disconnect = SMB2_tdis, 5575 .qfs_tcon = smb3_qfs_tcon, 5576 .query_server_interfaces = SMB3_request_interfaces, 5577 .is_path_accessible = smb2_is_path_accessible, 5578 .can_echo = smb2_can_echo, 5579 .echo = SMB2_echo, 5580 .query_path_info = smb2_query_path_info, 5581 .query_reparse_point = smb2_query_reparse_point, 5582 .get_srv_inum = smb2_get_srv_inum, 5583 .query_file_info = smb2_query_file_info, 5584 .set_path_size = smb2_set_path_size, 5585 .set_file_size = smb2_set_file_size, 5586 .set_file_info = smb2_set_file_info, 5587 .set_compression = smb2_set_compression, 5588 .mkdir = smb2_mkdir, 5589 .mkdir_setinfo = smb2_mkdir_setinfo, 5590 .posix_mkdir = smb311_posix_mkdir, 5591 .rmdir = smb2_rmdir, 5592 .unlink = smb2_unlink, 5593 .rename = smb2_rename_path, 5594 .create_hardlink = smb2_create_hardlink, 5595 .parse_reparse_point = smb2_parse_reparse_point, 5596 .query_mf_symlink = smb3_query_mf_symlink, 5597 .create_mf_symlink = smb3_create_mf_symlink, 5598 .create_reparse_symlink = smb2_create_reparse_symlink, 5599 .open = smb2_open_file, 5600 .set_fid = smb2_set_fid, 5601 .close = smb2_close_file, 5602 .close_getattr = smb2_close_getattr, 5603 .flush = smb2_flush_file, 5604 .async_readv = smb2_async_readv, 5605 .async_writev = smb2_async_writev, 5606 .sync_read = smb2_sync_read, 5607 .sync_write = smb2_sync_write, 5608 .query_dir_first = smb2_query_dir_first, 5609 .query_dir_next = smb2_query_dir_next, 5610 .close_dir = smb2_close_dir, 5611 .calc_smb_size = smb2_calc_size, 5612 .is_status_pending = smb2_is_status_pending, 5613 .is_session_expired = smb2_is_session_expired, 5614 .oplock_response = smb2_oplock_response, 5615 .queryfs = smb311_queryfs, 5616 .mand_lock = smb2_mand_lock, 5617 .mand_unlock_range = smb2_unlock_range, 5618 .push_mand_locks = smb2_push_mandatory_locks, 5619 .get_lease_key = smb2_get_lease_key, 5620 .set_lease_key = smb2_set_lease_key, 5621 .new_lease_key = smb2_new_lease_key, 5622 .generate_signingkey = generate_smb311signingkey, 5623 .calc_signature = smb3_calc_signature, 5624 .set_integrity = smb3_set_integrity, 5625 .is_read_op = smb21_is_read_op, 5626 .set_oplock_level = smb3_set_oplock_level, 5627 .create_lease_buf = smb3_create_lease_buf, 5628 .parse_lease_buf = smb3_parse_lease_buf, 5629 .copychunk_range = smb2_copychunk_range, 5630 .duplicate_extents = smb2_duplicate_extents, 5631 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */ 5632 .wp_retry_size = smb2_wp_retry_size, 5633 .dir_needs_close = smb2_dir_needs_close, 5634 .fallocate = smb3_fallocate, 5635 .enum_snapshots = smb3_enum_snapshots, 5636 .notify = smb3_notify, 5637 .init_transform_rq = smb3_init_transform_rq, 5638 .is_transform_hdr = smb3_is_transform_hdr, 5639 .receive_transform = smb3_receive_transform, 5640 .get_dfs_refer = smb2_get_dfs_refer, 5641 .select_sectype = smb2_select_sectype, 5642 #ifdef CONFIG_CIFS_XATTR 5643 .query_all_EAs = smb2_query_eas, 5644 .set_EA = smb2_set_ea, 5645 #endif /* CIFS_XATTR */ 5646 .get_acl = get_smb2_acl, 5647 .get_acl_by_fid = get_smb2_acl_by_fid, 5648 .set_acl = set_smb2_acl, 5649 .next_header = smb2_next_header, 5650 .ioctl_query_info = smb2_ioctl_query_info, 5651 .make_node = smb2_make_node, 5652 .fiemap = smb3_fiemap, 5653 .llseek = smb3_llseek, 5654 .is_status_io_timeout = smb2_is_status_io_timeout, 5655 .is_network_name_deleted = smb2_is_network_name_deleted, 5656 }; 5657 5658 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 5659 struct smb_version_values smb20_values = { 5660 .version_string = SMB20_VERSION_STRING, 5661 .protocol_id = SMB20_PROT_ID, 5662 .req_capabilities = 0, /* MBZ */ 5663 .large_lock_type = 0, 5664 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5665 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5666 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5667 .header_size = sizeof(struct smb2_hdr), 5668 .header_preamble_size = 0, 5669 .max_header_size = MAX_SMB2_HDR_SIZE, 5670 .read_rsp_size = sizeof(struct smb2_read_rsp), 5671 .lock_cmd = SMB2_LOCK, 5672 .cap_unix = 0, 5673 .cap_nt_find = SMB2_NT_FIND, 5674 .cap_large_files = SMB2_LARGE_FILES, 5675 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5676 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5677 .create_lease_size = sizeof(struct create_lease), 5678 }; 5679 #endif /* ALLOW_INSECURE_LEGACY */ 5680 5681 struct smb_version_values smb21_values = { 5682 .version_string = SMB21_VERSION_STRING, 5683 .protocol_id = SMB21_PROT_ID, 5684 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */ 5685 .large_lock_type = 0, 5686 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5687 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5688 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5689 .header_size = sizeof(struct smb2_hdr), 5690 .header_preamble_size = 0, 5691 .max_header_size = MAX_SMB2_HDR_SIZE, 5692 .read_rsp_size = sizeof(struct smb2_read_rsp), 5693 .lock_cmd = SMB2_LOCK, 5694 .cap_unix = 0, 5695 .cap_nt_find = SMB2_NT_FIND, 5696 .cap_large_files = SMB2_LARGE_FILES, 5697 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5698 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5699 .create_lease_size = sizeof(struct create_lease), 5700 }; 5701 5702 struct smb_version_values smb3any_values = { 5703 .version_string = SMB3ANY_VERSION_STRING, 5704 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */ 5705 .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, 5706 .large_lock_type = 0, 5707 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5708 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5709 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5710 .header_size = sizeof(struct smb2_hdr), 5711 .header_preamble_size = 0, 5712 .max_header_size = MAX_SMB2_HDR_SIZE, 5713 .read_rsp_size = sizeof(struct smb2_read_rsp), 5714 .lock_cmd = SMB2_LOCK, 5715 .cap_unix = 0, 5716 .cap_nt_find = SMB2_NT_FIND, 5717 .cap_large_files = SMB2_LARGE_FILES, 5718 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5719 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5720 .create_lease_size = sizeof(struct create_lease_v2), 5721 }; 5722 5723 struct smb_version_values smbdefault_values = { 5724 .version_string = SMBDEFAULT_VERSION_STRING, 5725 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */ 5726 .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, 5727 .large_lock_type = 0, 5728 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5729 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5730 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5731 .header_size = sizeof(struct smb2_hdr), 5732 .header_preamble_size = 0, 5733 .max_header_size = MAX_SMB2_HDR_SIZE, 5734 .read_rsp_size = sizeof(struct smb2_read_rsp), 5735 .lock_cmd = SMB2_LOCK, 5736 .cap_unix = 0, 5737 .cap_nt_find = SMB2_NT_FIND, 5738 .cap_large_files = SMB2_LARGE_FILES, 5739 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5740 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5741 .create_lease_size = sizeof(struct create_lease_v2), 5742 }; 5743 5744 struct smb_version_values smb30_values = { 5745 .version_string = SMB30_VERSION_STRING, 5746 .protocol_id = SMB30_PROT_ID, 5747 .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, 5748 .large_lock_type = 0, 5749 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5750 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5751 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5752 .header_size = sizeof(struct smb2_hdr), 5753 .header_preamble_size = 0, 5754 .max_header_size = MAX_SMB2_HDR_SIZE, 5755 .read_rsp_size = sizeof(struct smb2_read_rsp), 5756 .lock_cmd = SMB2_LOCK, 5757 .cap_unix = 0, 5758 .cap_nt_find = SMB2_NT_FIND, 5759 .cap_large_files = SMB2_LARGE_FILES, 5760 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5761 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5762 .create_lease_size = sizeof(struct create_lease_v2), 5763 }; 5764 5765 struct smb_version_values smb302_values = { 5766 .version_string = SMB302_VERSION_STRING, 5767 .protocol_id = SMB302_PROT_ID, 5768 .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, 5769 .large_lock_type = 0, 5770 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5771 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5772 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5773 .header_size = sizeof(struct smb2_hdr), 5774 .header_preamble_size = 0, 5775 .max_header_size = MAX_SMB2_HDR_SIZE, 5776 .read_rsp_size = sizeof(struct smb2_read_rsp), 5777 .lock_cmd = SMB2_LOCK, 5778 .cap_unix = 0, 5779 .cap_nt_find = SMB2_NT_FIND, 5780 .cap_large_files = SMB2_LARGE_FILES, 5781 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5782 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5783 .create_lease_size = sizeof(struct create_lease_v2), 5784 }; 5785 5786 struct smb_version_values smb311_values = { 5787 .version_string = SMB311_VERSION_STRING, 5788 .protocol_id = SMB311_PROT_ID, 5789 .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, 5790 .large_lock_type = 0, 5791 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE, 5792 .shared_lock_type = SMB2_LOCKFLAG_SHARED, 5793 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK, 5794 .header_size = sizeof(struct smb2_hdr), 5795 .header_preamble_size = 0, 5796 .max_header_size = MAX_SMB2_HDR_SIZE, 5797 .read_rsp_size = sizeof(struct smb2_read_rsp), 5798 .lock_cmd = SMB2_LOCK, 5799 .cap_unix = 0, 5800 .cap_nt_find = SMB2_NT_FIND, 5801 .cap_large_files = SMB2_LARGE_FILES, 5802 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED, 5803 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED, 5804 .create_lease_size = sizeof(struct create_lease_v2), 5805 }; 5806