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