1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 6 7 #include <linux/moduleparam.h> 8 9 #include "glob.h" 10 #include "oplock.h" 11 12 #include "smb_common.h" 13 #include "../common/smb2status.h" 14 #include "connection.h" 15 #include "mgmt/user_session.h" 16 #include "mgmt/share_config.h" 17 #include "mgmt/tree_connect.h" 18 19 static LIST_HEAD(lease_table_list); 20 static DEFINE_RWLOCK(lease_list_lock); 21 22 /** 23 * alloc_opinfo() - allocate a new opinfo object for oplock info 24 * @work: smb work 25 * @id: fid of open file 26 * @Tid: tree id of connection 27 * 28 * Return: allocated opinfo object on success, otherwise NULL 29 */ 30 static struct oplock_info *alloc_opinfo(struct ksmbd_work *work, 31 u64 id, __u16 Tid) 32 { 33 struct ksmbd_session *sess = work->sess; 34 struct oplock_info *opinfo; 35 36 opinfo = kzalloc_obj(struct oplock_info, KSMBD_DEFAULT_GFP); 37 if (!opinfo) 38 return NULL; 39 40 opinfo->sess = sess; 41 opinfo->conn = ksmbd_conn_get(work->conn); 42 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 43 opinfo->op_state = OPLOCK_STATE_NONE; 44 opinfo->pending_break = 0; 45 opinfo->fid = id; 46 opinfo->Tid = Tid; 47 INIT_LIST_HEAD(&opinfo->op_entry); 48 init_waitqueue_head(&opinfo->oplock_q); 49 init_waitqueue_head(&opinfo->oplock_brk); 50 atomic_set(&opinfo->refcount, 1); 51 atomic_set(&opinfo->breaking_cnt, 0); 52 53 return opinfo; 54 } 55 56 static void lease_add_list(struct oplock_info *opinfo) 57 { 58 struct lease_table *lb = opinfo->o_lease->l_lb; 59 60 spin_lock(&lb->lb_lock); 61 list_add_rcu(&opinfo->lease_entry, &lb->lease_list); 62 spin_unlock(&lb->lb_lock); 63 } 64 65 static void lease_del_list(struct oplock_info *opinfo) 66 { 67 struct lease_table *lb = opinfo->o_lease->l_lb; 68 69 if (!lb) 70 return; 71 72 spin_lock(&lb->lb_lock); 73 if (list_empty(&opinfo->lease_entry)) { 74 spin_unlock(&lb->lb_lock); 75 return; 76 } 77 78 list_del_init(&opinfo->lease_entry); 79 opinfo->o_lease->l_lb = NULL; 80 spin_unlock(&lb->lb_lock); 81 } 82 83 static struct lease_table *alloc_lease_table(struct oplock_info *opinfo) 84 { 85 struct lease_table *lb; 86 87 lb = kmalloc_obj(struct lease_table, KSMBD_DEFAULT_GFP); 88 if (!lb) 89 return NULL; 90 91 memcpy(lb->client_guid, opinfo->conn->ClientGUID, 92 SMB2_CLIENT_GUID_SIZE); 93 INIT_LIST_HEAD(&lb->lease_list); 94 spin_lock_init(&lb->lb_lock); 95 return lb; 96 } 97 98 static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx) 99 { 100 struct lease *lease; 101 102 lease = kmalloc_obj(struct lease, KSMBD_DEFAULT_GFP); 103 if (!lease) 104 return -ENOMEM; 105 106 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 107 lease->state = lctx->req_state; 108 lease->new_state = 0; 109 lease->flags = lctx->flags; 110 lease->duration = lctx->duration; 111 lease->is_dir = lctx->is_dir; 112 memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE); 113 lease->version = lctx->version; 114 lease->epoch = le16_to_cpu(lctx->epoch) + 1; 115 INIT_LIST_HEAD(&opinfo->lease_entry); 116 opinfo->o_lease = lease; 117 118 return 0; 119 } 120 121 static void free_lease(struct oplock_info *opinfo) 122 { 123 struct lease *lease; 124 125 lease = opinfo->o_lease; 126 kfree(lease); 127 } 128 129 static void __free_opinfo(struct oplock_info *opinfo) 130 { 131 if (opinfo->is_lease) 132 free_lease(opinfo); 133 ksmbd_conn_put(opinfo->conn); 134 kfree(opinfo); 135 } 136 137 static void free_opinfo_rcu(struct rcu_head *rcu) 138 { 139 struct oplock_info *opinfo = container_of(rcu, struct oplock_info, rcu); 140 141 __free_opinfo(opinfo); 142 } 143 144 static void free_opinfo(struct oplock_info *opinfo) 145 { 146 call_rcu(&opinfo->rcu, free_opinfo_rcu); 147 } 148 149 struct oplock_info *opinfo_get(struct ksmbd_file *fp) 150 { 151 struct oplock_info *opinfo; 152 153 rcu_read_lock(); 154 opinfo = rcu_dereference(fp->f_opinfo); 155 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount)) 156 opinfo = NULL; 157 rcu_read_unlock(); 158 159 return opinfo; 160 } 161 162 static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci) 163 { 164 struct oplock_info *opinfo; 165 166 down_read(&ci->m_lock); 167 opinfo = list_first_entry_or_null(&ci->m_op_list, struct oplock_info, 168 op_entry); 169 if (opinfo) { 170 if (opinfo->conn == NULL || 171 !atomic_inc_not_zero(&opinfo->refcount)) 172 opinfo = NULL; 173 else { 174 if (ksmbd_conn_releasing(opinfo->conn)) { 175 atomic_dec(&opinfo->refcount); 176 opinfo = NULL; 177 } 178 } 179 } 180 up_read(&ci->m_lock); 181 182 return opinfo; 183 } 184 185 void opinfo_put(struct oplock_info *opinfo) 186 { 187 if (!opinfo) 188 return; 189 190 if (!atomic_dec_and_test(&opinfo->refcount)) 191 return; 192 193 free_opinfo(opinfo); 194 } 195 196 static void opinfo_add(struct oplock_info *opinfo, struct ksmbd_file *fp) 197 { 198 struct ksmbd_inode *ci = fp->f_ci; 199 200 down_write(&ci->m_lock); 201 list_add(&opinfo->op_entry, &ci->m_op_list); 202 up_write(&ci->m_lock); 203 } 204 205 static void opinfo_del(struct oplock_info *opinfo) 206 { 207 struct ksmbd_inode *ci = opinfo->o_fp->f_ci; 208 209 if (opinfo->is_lease) { 210 write_lock(&lease_list_lock); 211 lease_del_list(opinfo); 212 write_unlock(&lease_list_lock); 213 } 214 down_write(&ci->m_lock); 215 list_del(&opinfo->op_entry); 216 up_write(&ci->m_lock); 217 } 218 219 static unsigned long opinfo_count(struct ksmbd_file *fp) 220 { 221 if (ksmbd_stream_fd(fp)) 222 return atomic_read(&fp->f_ci->sop_count); 223 else 224 return atomic_read(&fp->f_ci->op_count); 225 } 226 227 static void opinfo_count_inc(struct ksmbd_file *fp) 228 { 229 if (ksmbd_stream_fd(fp)) 230 return atomic_inc(&fp->f_ci->sop_count); 231 else 232 return atomic_inc(&fp->f_ci->op_count); 233 } 234 235 static void opinfo_count_dec(struct ksmbd_file *fp) 236 { 237 if (ksmbd_stream_fd(fp)) 238 return atomic_dec(&fp->f_ci->sop_count); 239 else 240 return atomic_dec(&fp->f_ci->op_count); 241 } 242 243 /** 244 * opinfo_write_to_read() - convert a write oplock to read oplock 245 * @opinfo: current oplock info 246 * 247 * Return: 0 on success, otherwise -EINVAL 248 */ 249 int opinfo_write_to_read(struct oplock_info *opinfo) 250 { 251 struct lease *lease = opinfo->o_lease; 252 253 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 254 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { 255 pr_err("bad oplock(0x%x)\n", opinfo->level); 256 if (opinfo->is_lease) 257 pr_err("lease state(0x%x)\n", lease->state); 258 return -EINVAL; 259 } 260 opinfo->level = SMB2_OPLOCK_LEVEL_II; 261 262 if (opinfo->is_lease) 263 lease->state = lease->new_state; 264 return 0; 265 } 266 267 /** 268 * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock 269 * @opinfo: current oplock info 270 * 271 * Return: 0 on success, otherwise -EINVAL 272 */ 273 int opinfo_read_handle_to_read(struct oplock_info *opinfo) 274 { 275 struct lease *lease = opinfo->o_lease; 276 277 lease->state = lease->new_state; 278 opinfo->level = SMB2_OPLOCK_LEVEL_II; 279 return 0; 280 } 281 282 /** 283 * opinfo_write_to_none() - convert a write oplock to none 284 * @opinfo: current oplock info 285 * 286 * Return: 0 on success, otherwise -EINVAL 287 */ 288 int opinfo_write_to_none(struct oplock_info *opinfo) 289 { 290 struct lease *lease = opinfo->o_lease; 291 292 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 293 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { 294 pr_err("bad oplock(0x%x)\n", opinfo->level); 295 if (opinfo->is_lease) 296 pr_err("lease state(0x%x)\n", lease->state); 297 return -EINVAL; 298 } 299 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 300 if (opinfo->is_lease) 301 lease->state = lease->new_state; 302 return 0; 303 } 304 305 /** 306 * opinfo_read_to_none() - convert a write read to none 307 * @opinfo: current oplock info 308 * 309 * Return: 0 on success, otherwise -EINVAL 310 */ 311 int opinfo_read_to_none(struct oplock_info *opinfo) 312 { 313 struct lease *lease = opinfo->o_lease; 314 315 if (opinfo->level != SMB2_OPLOCK_LEVEL_II) { 316 pr_err("bad oplock(0x%x)\n", opinfo->level); 317 if (opinfo->is_lease) 318 pr_err("lease state(0x%x)\n", lease->state); 319 return -EINVAL; 320 } 321 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 322 if (opinfo->is_lease) 323 lease->state = lease->new_state; 324 return 0; 325 } 326 327 /** 328 * lease_read_to_write() - upgrade lease state from read to write 329 * @opinfo: current lease info 330 * 331 * Return: 0 on success, otherwise -EINVAL 332 */ 333 int lease_read_to_write(struct oplock_info *opinfo) 334 { 335 struct lease *lease = opinfo->o_lease; 336 337 if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) { 338 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); 339 return -EINVAL; 340 } 341 342 lease->new_state = SMB2_LEASE_NONE_LE; 343 lease->state |= SMB2_LEASE_WRITE_CACHING_LE; 344 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 345 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH; 346 else 347 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; 348 return 0; 349 } 350 351 /** 352 * lease_none_upgrade() - upgrade lease state from none 353 * @opinfo: current lease info 354 * @new_state: new lease state 355 * 356 * Return: 0 on success, otherwise -EINVAL 357 */ 358 static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state) 359 { 360 struct lease *lease = opinfo->o_lease; 361 362 if (!(lease->state == SMB2_LEASE_NONE_LE)) { 363 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); 364 return -EINVAL; 365 } 366 367 lease->new_state = SMB2_LEASE_NONE_LE; 368 lease->state = new_state; 369 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 370 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) 371 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH; 372 else 373 opinfo->level = SMB2_OPLOCK_LEVEL_II; 374 else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) 375 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; 376 else if (lease->state & SMB2_LEASE_READ_CACHING_LE) 377 opinfo->level = SMB2_OPLOCK_LEVEL_II; 378 379 return 0; 380 } 381 382 /** 383 * close_id_del_oplock() - release oplock object at file close time 384 * @fp: ksmbd file pointer 385 */ 386 void close_id_del_oplock(struct ksmbd_file *fp) 387 { 388 struct oplock_info *opinfo; 389 390 if (fp->reserve_lease_break) 391 smb_lazy_parent_lease_break_close(fp); 392 393 opinfo = opinfo_get(fp); 394 if (!opinfo) 395 return; 396 397 opinfo_del(opinfo); 398 399 rcu_assign_pointer(fp->f_opinfo, NULL); 400 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 401 opinfo->op_state = OPLOCK_CLOSING; 402 wake_up_interruptible_all(&opinfo->oplock_q); 403 if (opinfo->is_lease) { 404 atomic_set(&opinfo->breaking_cnt, 0); 405 wake_up_interruptible_all(&opinfo->oplock_brk); 406 } 407 } 408 409 opinfo_count_dec(fp); 410 atomic_dec(&opinfo->refcount); 411 opinfo_put(opinfo); 412 } 413 414 /** 415 * grant_write_oplock() - grant exclusive/batch oplock or write lease 416 * @opinfo_new: new oplock info object 417 * @req_oplock: request oplock 418 * @lctx: lease context information 419 * 420 * Return: 0 421 */ 422 static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock, 423 struct lease_ctx_info *lctx) 424 { 425 struct lease *lease = opinfo_new->o_lease; 426 427 if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH) 428 opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH; 429 else 430 opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; 431 432 if (lctx) { 433 lease->state = lctx->req_state; 434 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 435 } 436 } 437 438 /** 439 * grant_read_oplock() - grant level2 oplock or read lease 440 * @opinfo_new: new oplock info object 441 * @lctx: lease context information 442 * 443 * Return: 0 444 */ 445 static void grant_read_oplock(struct oplock_info *opinfo_new, 446 struct lease_ctx_info *lctx) 447 { 448 struct lease *lease = opinfo_new->o_lease; 449 450 opinfo_new->level = SMB2_OPLOCK_LEVEL_II; 451 452 if (lctx) { 453 lease->state = SMB2_LEASE_READ_CACHING_LE; 454 if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE) 455 lease->state |= SMB2_LEASE_HANDLE_CACHING_LE; 456 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 457 } 458 } 459 460 /** 461 * grant_none_oplock() - grant none oplock or none lease 462 * @opinfo_new: new oplock info object 463 * @lctx: lease context information 464 * 465 * Return: 0 466 */ 467 static void grant_none_oplock(struct oplock_info *opinfo_new, 468 struct lease_ctx_info *lctx) 469 { 470 struct lease *lease = opinfo_new->o_lease; 471 472 opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE; 473 474 if (lctx) { 475 lease->state = 0; 476 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 477 } 478 } 479 480 static inline int compare_guid_key(struct oplock_info *opinfo, 481 const char *guid1, const char *key1) 482 { 483 const char *guid2, *key2; 484 struct ksmbd_conn *conn; 485 486 conn = READ_ONCE(opinfo->conn); 487 if (!conn) 488 return 0; 489 guid2 = conn->ClientGUID; 490 key2 = opinfo->o_lease->lease_key; 491 if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) && 492 !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE)) 493 return 1; 494 495 return 0; 496 } 497 498 /** 499 * same_client_has_lease() - check whether current lease request is 500 * from lease owner of file 501 * @ci: master file pointer 502 * @client_guid: Client GUID 503 * @lctx: lease context information 504 * 505 * Return: oplock(lease) object on success, otherwise NULL 506 */ 507 static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci, 508 char *client_guid, 509 struct lease_ctx_info *lctx) 510 { 511 int ret; 512 struct lease *lease; 513 struct oplock_info *opinfo; 514 struct oplock_info *m_opinfo = NULL; 515 516 if (!lctx) 517 return NULL; 518 519 /* 520 * Compare lease key and client_guid to know request from same owner 521 * of same client 522 */ 523 down_read(&ci->m_lock); 524 list_for_each_entry(opinfo, &ci->m_op_list, op_entry) { 525 if (!opinfo->is_lease || !opinfo->conn) 526 continue; 527 lease = opinfo->o_lease; 528 529 ret = compare_guid_key(opinfo, client_guid, lctx->lease_key); 530 if (ret) { 531 m_opinfo = opinfo; 532 /* skip upgrading lease about breaking lease */ 533 if (atomic_read(&opinfo->breaking_cnt)) 534 continue; 535 536 /* upgrading lease */ 537 if ((atomic_read(&ci->op_count) + 538 atomic_read(&ci->sop_count)) == 1) { 539 if (lease->state != SMB2_LEASE_NONE_LE && 540 lease->state == (lctx->req_state & lease->state)) { 541 lease->epoch++; 542 lease->state |= lctx->req_state; 543 if (lctx->req_state & 544 SMB2_LEASE_WRITE_CACHING_LE) 545 lease_read_to_write(opinfo); 546 547 } 548 } else if ((atomic_read(&ci->op_count) + 549 atomic_read(&ci->sop_count)) > 1) { 550 if (lctx->req_state == 551 (SMB2_LEASE_READ_CACHING_LE | 552 SMB2_LEASE_HANDLE_CACHING_LE)) { 553 lease->epoch++; 554 lease->state = lctx->req_state; 555 } 556 } 557 558 if (lctx->req_state && lease->state == 559 SMB2_LEASE_NONE_LE) { 560 lease->epoch++; 561 lease_none_upgrade(opinfo, lctx->req_state); 562 } 563 } 564 } 565 up_read(&ci->m_lock); 566 567 return m_opinfo; 568 } 569 570 static void wait_for_break_ack(struct oplock_info *opinfo) 571 { 572 int rc = 0; 573 574 rc = wait_event_interruptible_timeout(opinfo->oplock_q, 575 opinfo->op_state == OPLOCK_STATE_NONE || 576 opinfo->op_state == OPLOCK_CLOSING, 577 OPLOCK_WAIT_TIME); 578 579 /* is this a timeout ? */ 580 if (!rc) { 581 if (opinfo->is_lease) 582 opinfo->o_lease->state = SMB2_LEASE_NONE_LE; 583 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 584 opinfo->op_state = OPLOCK_STATE_NONE; 585 } 586 } 587 588 static void wake_up_oplock_break(struct oplock_info *opinfo) 589 { 590 clear_bit_unlock(0, &opinfo->pending_break); 591 /* memory barrier is needed for wake_up_bit() */ 592 smp_mb__after_atomic(); 593 wake_up_bit(&opinfo->pending_break, 0); 594 } 595 596 static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level) 597 { 598 while (test_and_set_bit(0, &opinfo->pending_break)) { 599 wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE); 600 601 /* Not immediately break to none. */ 602 opinfo->open_trunc = 0; 603 604 if (opinfo->op_state == OPLOCK_CLOSING) 605 return -ENOENT; 606 else if (opinfo->level <= req_op_level) { 607 if (opinfo->is_lease == false) 608 return 1; 609 610 if (opinfo->o_lease->state != 611 (SMB2_LEASE_HANDLE_CACHING_LE | 612 SMB2_LEASE_READ_CACHING_LE)) 613 return 1; 614 } 615 } 616 617 if (opinfo->level <= req_op_level) { 618 if (opinfo->is_lease == false) { 619 wake_up_oplock_break(opinfo); 620 return 1; 621 } 622 if (opinfo->o_lease->state != 623 (SMB2_LEASE_HANDLE_CACHING_LE | 624 SMB2_LEASE_READ_CACHING_LE)) { 625 wake_up_oplock_break(opinfo); 626 return 1; 627 } 628 } 629 return 0; 630 } 631 632 /** 633 * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn 634 * to client 635 * @wk: smb work object 636 * 637 * There are two ways this function can be called. 1- while file open we break 638 * from exclusive/batch lock to levelII oplock and 2- while file write/truncate 639 * we break from levelII oplock no oplock. 640 * work->request_buf contains oplock_info. 641 */ 642 static void __smb2_oplock_break_noti(struct work_struct *wk) 643 { 644 struct smb2_oplock_break *rsp = NULL; 645 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work); 646 struct ksmbd_conn *conn = work->conn; 647 struct oplock_break_info *br_info = work->request_buf; 648 struct smb2_hdr *rsp_hdr; 649 struct ksmbd_file *fp; 650 651 fp = ksmbd_lookup_global_fd(br_info->fid); 652 if (!fp) 653 goto out; 654 655 if (allocate_interim_rsp_buf(work)) { 656 pr_err("smb2_allocate_rsp_buf failed! "); 657 ksmbd_fd_put(work, fp); 658 goto out; 659 } 660 661 rsp_hdr = smb_get_msg(work->response_buf); 662 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 663 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; 664 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 665 rsp_hdr->CreditRequest = cpu_to_le16(0); 666 rsp_hdr->Command = SMB2_OPLOCK_BREAK; 667 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); 668 rsp_hdr->NextCommand = 0; 669 rsp_hdr->MessageId = cpu_to_le64(-1); 670 rsp_hdr->Id.SyncId.ProcessId = 0; 671 rsp_hdr->Id.SyncId.TreeId = 0; 672 rsp_hdr->SessionId = 0; 673 memset(rsp_hdr->Signature, 0, 16); 674 675 rsp = smb_get_msg(work->response_buf); 676 677 rsp->StructureSize = cpu_to_le16(24); 678 if (!br_info->open_trunc && 679 (br_info->level == SMB2_OPLOCK_LEVEL_BATCH || 680 br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) 681 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II; 682 else 683 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; 684 rsp->Reserved = 0; 685 rsp->Reserved2 = 0; 686 rsp->PersistentFid = fp->persistent_id; 687 rsp->VolatileFid = fp->volatile_id; 688 689 ksmbd_fd_put(work, fp); 690 if (ksmbd_iov_pin_rsp(work, (void *)rsp, 691 sizeof(struct smb2_oplock_break))) 692 goto out; 693 694 ksmbd_debug(OPLOCK, 695 "sending oplock break v_id %llu p_id = %llu lock level = %d\n", 696 rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel); 697 698 ksmbd_conn_write(work); 699 700 out: 701 ksmbd_free_work_struct(work); 702 ksmbd_conn_r_count_dec(conn); 703 } 704 705 /** 706 * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock 707 * break command from server to client 708 * @opinfo: oplock info object 709 * 710 * Return: 0 on success, otherwise error 711 */ 712 static int smb2_oplock_break_noti(struct oplock_info *opinfo) 713 { 714 struct ksmbd_conn *conn = opinfo->conn; 715 struct oplock_break_info *br_info; 716 int ret = 0; 717 struct ksmbd_work *work = ksmbd_alloc_work_struct(); 718 719 if (!work) 720 return -ENOMEM; 721 722 br_info = kmalloc_obj(struct oplock_break_info, KSMBD_DEFAULT_GFP); 723 if (!br_info) { 724 ksmbd_free_work_struct(work); 725 return -ENOMEM; 726 } 727 728 br_info->level = opinfo->level; 729 br_info->fid = opinfo->fid; 730 br_info->open_trunc = opinfo->open_trunc; 731 732 work->request_buf = (char *)br_info; 733 work->conn = conn; 734 work->sess = opinfo->sess; 735 736 ksmbd_conn_r_count_inc(conn); 737 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 738 INIT_WORK(&work->work, __smb2_oplock_break_noti); 739 ksmbd_queue_work(work); 740 741 wait_for_break_ack(opinfo); 742 } else { 743 __smb2_oplock_break_noti(&work->work); 744 if (opinfo->level == SMB2_OPLOCK_LEVEL_II) 745 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 746 } 747 return ret; 748 } 749 750 /** 751 * __smb2_lease_break_noti() - send lease break command from server 752 * to client 753 * @wk: smb work object 754 */ 755 static void __smb2_lease_break_noti(struct work_struct *wk) 756 { 757 struct smb2_lease_break *rsp = NULL; 758 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work); 759 struct ksmbd_conn *conn = work->conn; 760 struct lease_break_info *br_info = work->request_buf; 761 struct smb2_hdr *rsp_hdr; 762 763 if (allocate_interim_rsp_buf(work)) { 764 ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! "); 765 goto out; 766 } 767 768 rsp_hdr = smb_get_msg(work->response_buf); 769 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 770 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; 771 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 772 rsp_hdr->CreditRequest = cpu_to_le16(0); 773 rsp_hdr->Command = SMB2_OPLOCK_BREAK; 774 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); 775 rsp_hdr->NextCommand = 0; 776 rsp_hdr->MessageId = cpu_to_le64(-1); 777 rsp_hdr->Id.SyncId.ProcessId = 0; 778 rsp_hdr->Id.SyncId.TreeId = 0; 779 rsp_hdr->SessionId = 0; 780 memset(rsp_hdr->Signature, 0, 16); 781 782 rsp = smb_get_msg(work->response_buf); 783 rsp->StructureSize = cpu_to_le16(44); 784 rsp->Epoch = br_info->epoch; 785 rsp->Flags = 0; 786 787 if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE | 788 SMB2_LEASE_HANDLE_CACHING_LE)) 789 rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED; 790 791 memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE); 792 rsp->CurrentLeaseState = br_info->curr_state; 793 rsp->NewLeaseState = br_info->new_state; 794 rsp->BreakReason = 0; 795 rsp->AccessMaskHint = 0; 796 rsp->ShareMaskHint = 0; 797 798 if (ksmbd_iov_pin_rsp(work, (void *)rsp, 799 sizeof(struct smb2_lease_break))) 800 goto out; 801 802 ksmbd_conn_write(work); 803 804 out: 805 ksmbd_free_work_struct(work); 806 ksmbd_conn_r_count_dec(conn); 807 } 808 809 /** 810 * smb2_lease_break_noti() - break lease when a new client request 811 * write lease 812 * @opinfo: contains lease state information 813 * 814 * Return: 0 on success, otherwise error 815 */ 816 static int smb2_lease_break_noti(struct oplock_info *opinfo) 817 { 818 struct ksmbd_conn *conn = opinfo->conn; 819 struct ksmbd_work *work; 820 struct lease_break_info *br_info; 821 struct lease *lease = opinfo->o_lease; 822 823 work = ksmbd_alloc_work_struct(); 824 if (!work) 825 return -ENOMEM; 826 827 br_info = kmalloc_obj(struct lease_break_info, KSMBD_DEFAULT_GFP); 828 if (!br_info) { 829 ksmbd_free_work_struct(work); 830 return -ENOMEM; 831 } 832 833 br_info->curr_state = lease->state; 834 br_info->new_state = lease->new_state; 835 if (lease->version == 2) 836 br_info->epoch = cpu_to_le16(++lease->epoch); 837 else 838 br_info->epoch = 0; 839 memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE); 840 841 work->request_buf = (char *)br_info; 842 work->conn = conn; 843 work->sess = opinfo->sess; 844 845 ksmbd_conn_r_count_inc(conn); 846 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 847 INIT_WORK(&work->work, __smb2_lease_break_noti); 848 ksmbd_queue_work(work); 849 wait_for_break_ack(opinfo); 850 } else { 851 __smb2_lease_break_noti(&work->work); 852 if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) { 853 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 854 opinfo->o_lease->state = SMB2_LEASE_NONE_LE; 855 } 856 } 857 return 0; 858 } 859 860 static void wait_lease_breaking(struct oplock_info *opinfo) 861 { 862 if (!opinfo->is_lease) 863 return; 864 865 wake_up_interruptible_all(&opinfo->oplock_brk); 866 if (atomic_read(&opinfo->breaking_cnt)) { 867 int ret = 0; 868 869 ret = wait_event_interruptible_timeout(opinfo->oplock_brk, 870 atomic_read(&opinfo->breaking_cnt) == 0, 871 HZ); 872 if (!ret) 873 atomic_set(&opinfo->breaking_cnt, 0); 874 } 875 } 876 877 static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, 878 struct ksmbd_work *in_work) 879 { 880 int err = 0; 881 882 /* Need to break exclusive/batch oplock, write lease or overwrite_if */ 883 ksmbd_debug(OPLOCK, 884 "request to send oplock(level : 0x%x) break notification\n", 885 brk_opinfo->level); 886 887 if (brk_opinfo->is_lease) { 888 struct lease *lease = brk_opinfo->o_lease; 889 890 atomic_inc(&brk_opinfo->breaking_cnt); 891 err = oplock_break_pending(brk_opinfo, req_op_level); 892 if (err) 893 return err < 0 ? err : 0; 894 895 if (brk_opinfo->open_trunc) { 896 /* 897 * Create overwrite break trigger the lease break to 898 * none. 899 */ 900 lease->new_state = SMB2_LEASE_NONE_LE; 901 } else { 902 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) { 903 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 904 lease->new_state = 905 SMB2_LEASE_READ_CACHING_LE | 906 SMB2_LEASE_HANDLE_CACHING_LE; 907 else 908 lease->new_state = 909 SMB2_LEASE_READ_CACHING_LE; 910 } else { 911 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE && 912 !lease->is_dir) 913 lease->new_state = 914 SMB2_LEASE_READ_CACHING_LE; 915 else 916 lease->new_state = SMB2_LEASE_NONE_LE; 917 } 918 } 919 920 if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE | 921 SMB2_LEASE_HANDLE_CACHING_LE)) { 922 if (in_work) { 923 setup_async_work(in_work, NULL, NULL); 924 smb2_send_interim_resp(in_work, STATUS_PENDING); 925 release_async_work(in_work); 926 } 927 928 brk_opinfo->op_state = OPLOCK_ACK_WAIT; 929 } else 930 atomic_dec(&brk_opinfo->breaking_cnt); 931 } else { 932 err = oplock_break_pending(brk_opinfo, req_op_level); 933 if (err) 934 return err < 0 ? err : 0; 935 936 if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 937 brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) 938 brk_opinfo->op_state = OPLOCK_ACK_WAIT; 939 } 940 941 if (brk_opinfo->is_lease) 942 err = smb2_lease_break_noti(brk_opinfo); 943 else 944 err = smb2_oplock_break_noti(brk_opinfo); 945 946 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level); 947 if (brk_opinfo->op_state == OPLOCK_CLOSING) 948 err = -ENOENT; 949 wake_up_oplock_break(brk_opinfo); 950 951 wait_lease_breaking(brk_opinfo); 952 953 return err; 954 } 955 956 void destroy_lease_table(struct ksmbd_conn *conn) 957 { 958 struct lease_table *lb, *lbtmp; 959 struct oplock_info *opinfo; 960 961 write_lock(&lease_list_lock); 962 if (list_empty(&lease_table_list)) { 963 write_unlock(&lease_list_lock); 964 return; 965 } 966 967 list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) { 968 if (conn && memcmp(lb->client_guid, conn->ClientGUID, 969 SMB2_CLIENT_GUID_SIZE)) 970 continue; 971 again: 972 rcu_read_lock(); 973 list_for_each_entry_rcu(opinfo, &lb->lease_list, 974 lease_entry) { 975 rcu_read_unlock(); 976 lease_del_list(opinfo); 977 goto again; 978 } 979 rcu_read_unlock(); 980 list_del(&lb->l_entry); 981 kfree(lb); 982 } 983 write_unlock(&lease_list_lock); 984 } 985 986 int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci, 987 struct lease_ctx_info *lctx) 988 { 989 struct oplock_info *opinfo; 990 int err = 0; 991 struct lease_table *lb; 992 993 if (!lctx) 994 return err; 995 996 read_lock(&lease_list_lock); 997 if (list_empty(&lease_table_list)) { 998 read_unlock(&lease_list_lock); 999 return 0; 1000 } 1001 1002 list_for_each_entry(lb, &lease_table_list, l_entry) { 1003 if (!memcmp(lb->client_guid, sess->ClientGUID, 1004 SMB2_CLIENT_GUID_SIZE)) 1005 goto found; 1006 } 1007 read_unlock(&lease_list_lock); 1008 1009 return 0; 1010 1011 found: 1012 rcu_read_lock(); 1013 list_for_each_entry_rcu(opinfo, &lb->lease_list, lease_entry) { 1014 if (!atomic_inc_not_zero(&opinfo->refcount)) 1015 continue; 1016 rcu_read_unlock(); 1017 if (opinfo->o_fp->f_ci == ci) 1018 goto op_next; 1019 err = compare_guid_key(opinfo, sess->ClientGUID, 1020 lctx->lease_key); 1021 if (err) { 1022 err = -EINVAL; 1023 ksmbd_debug(OPLOCK, 1024 "found same lease key is already used in other files\n"); 1025 opinfo_put(opinfo); 1026 goto out; 1027 } 1028 op_next: 1029 opinfo_put(opinfo); 1030 rcu_read_lock(); 1031 } 1032 rcu_read_unlock(); 1033 1034 out: 1035 read_unlock(&lease_list_lock); 1036 return err; 1037 } 1038 1039 static void copy_lease(struct oplock_info *op1, struct oplock_info *op2) 1040 { 1041 struct lease *lease1 = op1->o_lease; 1042 struct lease *lease2 = op2->o_lease; 1043 1044 op2->level = op1->level; 1045 lease2->state = lease1->state; 1046 memcpy(lease2->lease_key, lease1->lease_key, 1047 SMB2_LEASE_KEY_SIZE); 1048 lease2->duration = lease1->duration; 1049 lease2->flags = lease1->flags; 1050 lease2->epoch = lease1->epoch; 1051 lease2->version = lease1->version; 1052 } 1053 1054 static void add_lease_global_list(struct oplock_info *opinfo, 1055 struct lease_table *new_lb) 1056 { 1057 struct lease_table *lb; 1058 1059 write_lock(&lease_list_lock); 1060 list_for_each_entry(lb, &lease_table_list, l_entry) { 1061 if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID, 1062 SMB2_CLIENT_GUID_SIZE)) { 1063 opinfo->o_lease->l_lb = lb; 1064 lease_add_list(opinfo); 1065 write_unlock(&lease_list_lock); 1066 kfree(new_lb); 1067 return; 1068 } 1069 } 1070 1071 opinfo->o_lease->l_lb = new_lb; 1072 lease_add_list(opinfo); 1073 list_add(&new_lb->l_entry, &lease_table_list); 1074 write_unlock(&lease_list_lock); 1075 } 1076 1077 static void set_oplock_level(struct oplock_info *opinfo, int level, 1078 struct lease_ctx_info *lctx) 1079 { 1080 switch (level) { 1081 case SMB2_OPLOCK_LEVEL_BATCH: 1082 case SMB2_OPLOCK_LEVEL_EXCLUSIVE: 1083 grant_write_oplock(opinfo, level, lctx); 1084 break; 1085 case SMB2_OPLOCK_LEVEL_II: 1086 grant_read_oplock(opinfo, lctx); 1087 break; 1088 default: 1089 grant_none_oplock(opinfo, lctx); 1090 break; 1091 } 1092 } 1093 1094 void smb_send_parent_lease_break_noti(struct ksmbd_file *fp, 1095 struct lease_ctx_info *lctx) 1096 { 1097 struct oplock_info *opinfo; 1098 struct ksmbd_inode *p_ci = NULL; 1099 1100 if (lctx->version != 2) 1101 return; 1102 1103 p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent); 1104 if (!p_ci) 1105 return; 1106 1107 down_read(&p_ci->m_lock); 1108 list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) { 1109 if (opinfo->conn == NULL || !opinfo->is_lease) 1110 continue; 1111 1112 if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE && 1113 (!(lctx->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) || 1114 !compare_guid_key(opinfo, fp->conn->ClientGUID, 1115 lctx->parent_lease_key))) { 1116 if (!atomic_inc_not_zero(&opinfo->refcount)) 1117 continue; 1118 1119 if (ksmbd_conn_releasing(opinfo->conn)) { 1120 opinfo_put(opinfo); 1121 continue; 1122 } 1123 1124 oplock_break(opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL); 1125 opinfo_put(opinfo); 1126 } 1127 } 1128 up_read(&p_ci->m_lock); 1129 1130 ksmbd_inode_put(p_ci); 1131 } 1132 1133 void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp) 1134 { 1135 struct oplock_info *opinfo; 1136 struct ksmbd_inode *p_ci = NULL; 1137 1138 rcu_read_lock(); 1139 opinfo = rcu_dereference(fp->f_opinfo); 1140 1141 if (!opinfo || !opinfo->is_lease || opinfo->o_lease->version != 2) { 1142 rcu_read_unlock(); 1143 return; 1144 } 1145 rcu_read_unlock(); 1146 1147 p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent); 1148 if (!p_ci) 1149 return; 1150 1151 down_read(&p_ci->m_lock); 1152 list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) { 1153 if (opinfo->conn == NULL || !opinfo->is_lease) 1154 continue; 1155 1156 if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE) { 1157 if (!atomic_inc_not_zero(&opinfo->refcount)) 1158 continue; 1159 1160 if (ksmbd_conn_releasing(opinfo->conn)) { 1161 opinfo_put(opinfo); 1162 continue; 1163 } 1164 1165 oplock_break(opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL); 1166 opinfo_put(opinfo); 1167 } 1168 } 1169 up_read(&p_ci->m_lock); 1170 1171 ksmbd_inode_put(p_ci); 1172 } 1173 1174 /** 1175 * smb_grant_oplock() - handle oplock/lease request on file open 1176 * @work: smb work 1177 * @req_op_level: oplock level 1178 * @pid: id of open file 1179 * @fp: ksmbd file pointer 1180 * @tid: Tree id of connection 1181 * @lctx: lease context information on file open 1182 * @share_ret: share mode 1183 * 1184 * Return: 0 on success, otherwise error 1185 */ 1186 int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, 1187 struct ksmbd_file *fp, __u16 tid, 1188 struct lease_ctx_info *lctx, int share_ret) 1189 { 1190 struct ksmbd_session *sess = work->sess; 1191 int err = 0; 1192 struct oplock_info *opinfo = NULL, *prev_opinfo = NULL; 1193 struct ksmbd_inode *ci = fp->f_ci; 1194 struct lease_table *new_lb = NULL; 1195 bool prev_op_has_lease; 1196 __le32 prev_op_state = 0; 1197 1198 /* Only v2 leases handle the directory */ 1199 if (S_ISDIR(file_inode(fp->filp)->i_mode)) { 1200 if (!lctx || lctx->version != 2 || 1201 (lctx->flags != SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE && 1202 !lctx->epoch)) 1203 return 0; 1204 } 1205 1206 opinfo = alloc_opinfo(work, pid, tid); 1207 if (!opinfo) 1208 return -ENOMEM; 1209 1210 if (lctx) { 1211 err = alloc_lease(opinfo, lctx); 1212 if (err) 1213 goto err_out; 1214 opinfo->is_lease = 1; 1215 } 1216 1217 /* ci does not have any oplock */ 1218 if (!opinfo_count(fp)) 1219 goto set_lev; 1220 1221 /* grant none-oplock if second open is trunc */ 1222 if (fp->attrib_only && fp->cdoption != FILE_OVERWRITE_IF_LE && 1223 fp->cdoption != FILE_OVERWRITE_LE && 1224 fp->cdoption != FILE_SUPERSEDE_LE) { 1225 req_op_level = SMB2_OPLOCK_LEVEL_NONE; 1226 goto set_lev; 1227 } 1228 1229 if (lctx) { 1230 struct oplock_info *m_opinfo; 1231 1232 /* is lease already granted ? */ 1233 m_opinfo = same_client_has_lease(ci, sess->ClientGUID, 1234 lctx); 1235 if (m_opinfo) { 1236 copy_lease(m_opinfo, opinfo); 1237 if (atomic_read(&m_opinfo->breaking_cnt)) 1238 opinfo->o_lease->flags = 1239 SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE; 1240 goto out; 1241 } 1242 } 1243 prev_opinfo = opinfo_get_list(ci); 1244 if (!prev_opinfo || 1245 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx)) { 1246 opinfo_put(prev_opinfo); 1247 goto set_lev; 1248 } 1249 prev_op_has_lease = prev_opinfo->is_lease; 1250 if (prev_op_has_lease) 1251 prev_op_state = prev_opinfo->o_lease->state; 1252 1253 if (share_ret < 0 && 1254 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1255 err = share_ret; 1256 opinfo_put(prev_opinfo); 1257 goto err_out; 1258 } 1259 1260 if (prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH && 1261 prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1262 opinfo_put(prev_opinfo); 1263 goto op_break_not_needed; 1264 } 1265 1266 err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II, work); 1267 opinfo_put(prev_opinfo); 1268 if (err == -ENOENT) 1269 goto set_lev; 1270 /* Check all oplock was freed by close */ 1271 else if (err < 0) 1272 goto err_out; 1273 1274 op_break_not_needed: 1275 if (share_ret < 0) { 1276 err = share_ret; 1277 goto err_out; 1278 } 1279 1280 if (req_op_level != SMB2_OPLOCK_LEVEL_NONE) 1281 req_op_level = SMB2_OPLOCK_LEVEL_II; 1282 1283 /* grant fixed oplock on stacked locking between lease and oplock */ 1284 if (prev_op_has_lease && !lctx) 1285 if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE) 1286 req_op_level = SMB2_OPLOCK_LEVEL_NONE; 1287 1288 if (!prev_op_has_lease && lctx) { 1289 req_op_level = SMB2_OPLOCK_LEVEL_II; 1290 lctx->req_state = SMB2_LEASE_READ_CACHING_LE; 1291 } 1292 1293 set_lev: 1294 set_oplock_level(opinfo, req_op_level, lctx); 1295 1296 out: 1297 /* 1298 * Set o_fp before any publication so that concurrent readers 1299 * (e.g. find_same_lease_key() on the lease list) that 1300 * dereference opinfo->o_fp don't hit a NULL pointer. 1301 * 1302 * Keep the original publication order so concurrent opens can 1303 * still observe the in-flight grant via ci->m_op_list, but make 1304 * everything after opinfo_add() no-fail by preallocating any new 1305 * lease_table first. 1306 */ 1307 opinfo->o_fp = fp; 1308 if (opinfo->is_lease) { 1309 new_lb = alloc_lease_table(opinfo); 1310 if (!new_lb) { 1311 err = -ENOMEM; 1312 goto err_out; 1313 } 1314 } 1315 1316 opinfo_count_inc(fp); 1317 opinfo_add(opinfo, fp); 1318 1319 if (opinfo->is_lease) 1320 add_lease_global_list(opinfo, new_lb); 1321 1322 rcu_assign_pointer(fp->f_opinfo, opinfo); 1323 1324 return 0; 1325 err_out: 1326 kfree(new_lb); 1327 opinfo_put(opinfo); 1328 return err; 1329 } 1330 1331 /** 1332 * smb_break_all_write_oplock() - break batch/exclusive oplock to level2 1333 * @work: smb work 1334 * @fp: ksmbd file pointer 1335 * @is_trunc: truncate on open 1336 */ 1337 static void smb_break_all_write_oplock(struct ksmbd_work *work, 1338 struct ksmbd_file *fp, int is_trunc) 1339 { 1340 struct oplock_info *brk_opinfo; 1341 1342 brk_opinfo = opinfo_get_list(fp->f_ci); 1343 if (!brk_opinfo) 1344 return; 1345 if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH && 1346 brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1347 opinfo_put(brk_opinfo); 1348 return; 1349 } 1350 1351 brk_opinfo->open_trunc = is_trunc; 1352 oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II, work); 1353 opinfo_put(brk_opinfo); 1354 } 1355 1356 /** 1357 * smb_break_all_levII_oplock() - send level2 oplock or read lease break command 1358 * from server to client 1359 * @work: smb work 1360 * @fp: ksmbd file pointer 1361 * @is_trunc: truncate on open 1362 */ 1363 void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp, 1364 int is_trunc) 1365 { 1366 struct oplock_info *op, *brk_op; 1367 struct ksmbd_inode *ci; 1368 struct ksmbd_conn *conn = work->conn; 1369 1370 if (!test_share_config_flag(work->tcon->share_conf, 1371 KSMBD_SHARE_FLAG_OPLOCKS)) 1372 return; 1373 1374 ci = fp->f_ci; 1375 op = opinfo_get(fp); 1376 1377 down_read(&ci->m_lock); 1378 list_for_each_entry(brk_op, &ci->m_op_list, op_entry) { 1379 if (brk_op->conn == NULL) 1380 continue; 1381 1382 if (!atomic_inc_not_zero(&brk_op->refcount)) 1383 continue; 1384 1385 if (ksmbd_conn_releasing(brk_op->conn)) { 1386 opinfo_put(brk_op); 1387 continue; 1388 } 1389 1390 if (brk_op->is_lease && (brk_op->o_lease->state & 1391 (~(SMB2_LEASE_READ_CACHING_LE | 1392 SMB2_LEASE_HANDLE_CACHING_LE)))) { 1393 ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n", 1394 brk_op->o_lease->state); 1395 goto next; 1396 } else if (brk_op->level != 1397 SMB2_OPLOCK_LEVEL_II) { 1398 ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n", 1399 brk_op->level); 1400 goto next; 1401 } 1402 1403 /* Skip oplock being break to none */ 1404 if (brk_op->is_lease && 1405 brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE && 1406 atomic_read(&brk_op->breaking_cnt)) 1407 goto next; 1408 1409 if (op && op->is_lease && brk_op->is_lease && 1410 !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID, 1411 SMB2_CLIENT_GUID_SIZE) && 1412 !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key, 1413 SMB2_LEASE_KEY_SIZE)) 1414 goto next; 1415 brk_op->open_trunc = is_trunc; 1416 oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE, NULL); 1417 next: 1418 opinfo_put(brk_op); 1419 } 1420 up_read(&ci->m_lock); 1421 1422 if (op) 1423 opinfo_put(op); 1424 } 1425 1426 /** 1427 * smb_break_all_oplock() - break both batch/exclusive and level2 oplock 1428 * @work: smb work 1429 * @fp: ksmbd file pointer 1430 */ 1431 void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp) 1432 { 1433 if (!test_share_config_flag(work->tcon->share_conf, 1434 KSMBD_SHARE_FLAG_OPLOCKS)) 1435 return; 1436 1437 smb_break_all_write_oplock(work, fp, 1); 1438 smb_break_all_levII_oplock(work, fp, 1); 1439 } 1440 1441 /** 1442 * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type 1443 * @lease_state: lease type 1444 * 1445 * Return: 0 if no mapping, otherwise corresponding oplock type 1446 */ 1447 __u8 smb2_map_lease_to_oplock(__le32 lease_state) 1448 { 1449 if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE | 1450 SMB2_LEASE_READ_CACHING_LE | 1451 SMB2_LEASE_WRITE_CACHING_LE)) { 1452 return SMB2_OPLOCK_LEVEL_BATCH; 1453 } else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE && 1454 lease_state & SMB2_LEASE_WRITE_CACHING_LE) { 1455 if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE)) 1456 return SMB2_OPLOCK_LEVEL_EXCLUSIVE; 1457 } else if (lease_state & SMB2_LEASE_READ_CACHING_LE) { 1458 return SMB2_OPLOCK_LEVEL_II; 1459 } 1460 return 0; 1461 } 1462 1463 /** 1464 * create_lease_buf() - create lease context for open cmd response 1465 * @rbuf: buffer to create lease context response 1466 * @lease: buffer to stored parsed lease state information 1467 */ 1468 void create_lease_buf(u8 *rbuf, struct lease *lease) 1469 { 1470 if (lease->version == 2) { 1471 struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf; 1472 1473 memset(buf, 0, sizeof(struct create_lease_v2)); 1474 memcpy(buf->lcontext.LeaseKey, lease->lease_key, 1475 SMB2_LEASE_KEY_SIZE); 1476 buf->lcontext.LeaseFlags = lease->flags; 1477 buf->lcontext.Epoch = cpu_to_le16(lease->epoch); 1478 buf->lcontext.LeaseState = lease->state; 1479 if (lease->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) 1480 memcpy(buf->lcontext.ParentLeaseKey, lease->parent_lease_key, 1481 SMB2_LEASE_KEY_SIZE); 1482 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1483 (struct create_lease_v2, lcontext)); 1484 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2)); 1485 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1486 (struct create_lease_v2, Name)); 1487 buf->ccontext.NameLength = cpu_to_le16(4); 1488 buf->Name[0] = 'R'; 1489 buf->Name[1] = 'q'; 1490 buf->Name[2] = 'L'; 1491 buf->Name[3] = 's'; 1492 } else { 1493 struct create_lease *buf = (struct create_lease *)rbuf; 1494 1495 memset(buf, 0, sizeof(struct create_lease)); 1496 memcpy(buf->lcontext.LeaseKey, lease->lease_key, SMB2_LEASE_KEY_SIZE); 1497 buf->lcontext.LeaseFlags = lease->flags; 1498 buf->lcontext.LeaseState = lease->state; 1499 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1500 (struct create_lease, lcontext)); 1501 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context)); 1502 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1503 (struct create_lease, Name)); 1504 buf->ccontext.NameLength = cpu_to_le16(4); 1505 buf->Name[0] = 'R'; 1506 buf->Name[1] = 'q'; 1507 buf->Name[2] = 'L'; 1508 buf->Name[3] = 's'; 1509 } 1510 } 1511 1512 /** 1513 * parse_lease_state() - parse lease context contained in file open request 1514 * @open_req: buffer containing smb2 file open(create) request 1515 * 1516 * Return: allocated lease context object on success, otherwise NULL 1517 */ 1518 struct lease_ctx_info *parse_lease_state(void *open_req) 1519 { 1520 struct create_context *cc; 1521 struct smb2_create_req *req = (struct smb2_create_req *)open_req; 1522 struct lease_ctx_info *lreq; 1523 1524 cc = smb2_find_context_vals(req, SMB2_CREATE_REQUEST_LEASE, 4); 1525 if (IS_ERR_OR_NULL(cc)) 1526 return NULL; 1527 1528 lreq = kzalloc_obj(struct lease_ctx_info, KSMBD_DEFAULT_GFP); 1529 if (!lreq) 1530 return NULL; 1531 1532 if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) { 1533 struct create_lease_v2 *lc = (struct create_lease_v2 *)cc; 1534 1535 if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) < 1536 sizeof(struct create_lease_v2) - 4) 1537 goto err_out; 1538 1539 memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 1540 lreq->req_state = lc->lcontext.LeaseState; 1541 lreq->flags = lc->lcontext.LeaseFlags; 1542 lreq->epoch = lc->lcontext.Epoch; 1543 lreq->duration = lc->lcontext.LeaseDuration; 1544 if (lreq->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) 1545 memcpy(lreq->parent_lease_key, lc->lcontext.ParentLeaseKey, 1546 SMB2_LEASE_KEY_SIZE); 1547 lreq->version = 2; 1548 } else { 1549 struct create_lease *lc = (struct create_lease *)cc; 1550 1551 if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) < 1552 sizeof(struct create_lease)) 1553 goto err_out; 1554 1555 memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 1556 lreq->req_state = lc->lcontext.LeaseState; 1557 lreq->flags = lc->lcontext.LeaseFlags; 1558 lreq->duration = lc->lcontext.LeaseDuration; 1559 lreq->version = 1; 1560 } 1561 return lreq; 1562 err_out: 1563 kfree(lreq); 1564 return NULL; 1565 } 1566 1567 /** 1568 * smb2_find_context_vals() - find a particular context info in open request 1569 * @open_req: buffer containing smb2 file open(create) request 1570 * @tag: context name to search for 1571 * @tag_len: the length of tag 1572 * 1573 * Return: pointer to requested context, NULL if @str context not found 1574 * or error pointer if name length is invalid. 1575 */ 1576 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len) 1577 { 1578 struct create_context *cc; 1579 unsigned int next = 0; 1580 char *name; 1581 struct smb2_create_req *req = (struct smb2_create_req *)open_req; 1582 unsigned int remain_len, name_off, name_len, value_off, value_len, 1583 cc_len; 1584 1585 /* 1586 * CreateContextsOffset and CreateContextsLength are guaranteed to 1587 * be valid because of ksmbd_smb2_check_message(). 1588 */ 1589 cc = (struct create_context *)((char *)req + 1590 le32_to_cpu(req->CreateContextsOffset)); 1591 remain_len = le32_to_cpu(req->CreateContextsLength); 1592 do { 1593 cc = (struct create_context *)((char *)cc + next); 1594 if (remain_len < offsetof(struct create_context, Buffer)) 1595 return ERR_PTR(-EINVAL); 1596 1597 next = le32_to_cpu(cc->Next); 1598 name_off = le16_to_cpu(cc->NameOffset); 1599 name_len = le16_to_cpu(cc->NameLength); 1600 value_off = le16_to_cpu(cc->DataOffset); 1601 value_len = le32_to_cpu(cc->DataLength); 1602 cc_len = next ? next : remain_len; 1603 1604 if ((next & 0x7) != 0 || 1605 next > remain_len || 1606 name_off != offsetof(struct create_context, Buffer) || 1607 name_len < 4 || 1608 name_off + name_len > cc_len || 1609 (value_off & 0x7) != 0 || 1610 (value_len && value_off < name_off + (name_len < 8 ? 8 : name_len)) || 1611 ((u64)value_off + value_len > cc_len)) 1612 return ERR_PTR(-EINVAL); 1613 1614 name = (char *)cc + name_off; 1615 if (name_len == tag_len && !memcmp(name, tag, name_len)) 1616 return cc; 1617 1618 remain_len -= next; 1619 } while (next != 0); 1620 1621 return NULL; 1622 } 1623 1624 /** 1625 * create_durable_rsp_buf() - create durable handle context 1626 * @cc: buffer to create durable context response 1627 */ 1628 void create_durable_rsp_buf(char *cc) 1629 { 1630 struct create_durable_rsp *buf; 1631 1632 buf = (struct create_durable_rsp *)cc; 1633 memset(buf, 0, sizeof(struct create_durable_rsp)); 1634 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1635 (struct create_durable_rsp, Data)); 1636 buf->ccontext.DataLength = cpu_to_le32(8); 1637 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1638 (struct create_durable_rsp, Name)); 1639 buf->ccontext.NameLength = cpu_to_le16(4); 1640 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */ 1641 buf->Name[0] = 'D'; 1642 buf->Name[1] = 'H'; 1643 buf->Name[2] = 'n'; 1644 buf->Name[3] = 'Q'; 1645 } 1646 1647 /** 1648 * create_durable_v2_rsp_buf() - create durable handle v2 context 1649 * @cc: buffer to create durable context response 1650 * @fp: ksmbd file pointer 1651 */ 1652 void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp) 1653 { 1654 struct create_durable_rsp_v2 *buf; 1655 1656 buf = (struct create_durable_rsp_v2 *)cc; 1657 memset(buf, 0, sizeof(struct create_durable_rsp)); 1658 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1659 (struct create_durable_rsp, Data)); 1660 buf->ccontext.DataLength = cpu_to_le32(8); 1661 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1662 (struct create_durable_rsp, Name)); 1663 buf->ccontext.NameLength = cpu_to_le16(4); 1664 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */ 1665 buf->Name[0] = 'D'; 1666 buf->Name[1] = 'H'; 1667 buf->Name[2] = '2'; 1668 buf->Name[3] = 'Q'; 1669 1670 buf->dcontext.Timeout = cpu_to_le32(fp->durable_timeout); 1671 if (fp->is_persistent) 1672 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); 1673 } 1674 1675 /** 1676 * create_mxac_rsp_buf() - create query maximal access context 1677 * @cc: buffer to create maximal access context response 1678 * @maximal_access: maximal access 1679 */ 1680 void create_mxac_rsp_buf(char *cc, int maximal_access) 1681 { 1682 struct create_mxac_rsp *buf; 1683 1684 buf = (struct create_mxac_rsp *)cc; 1685 memset(buf, 0, sizeof(struct create_mxac_rsp)); 1686 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1687 (struct create_mxac_rsp, QueryStatus)); 1688 buf->ccontext.DataLength = cpu_to_le32(8); 1689 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1690 (struct create_mxac_rsp, Name)); 1691 buf->ccontext.NameLength = cpu_to_le16(4); 1692 /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */ 1693 buf->Name[0] = 'M'; 1694 buf->Name[1] = 'x'; 1695 buf->Name[2] = 'A'; 1696 buf->Name[3] = 'c'; 1697 1698 buf->QueryStatus = STATUS_SUCCESS; 1699 buf->MaximalAccess = cpu_to_le32(maximal_access); 1700 } 1701 1702 void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id) 1703 { 1704 struct create_disk_id_rsp *buf; 1705 1706 buf = (struct create_disk_id_rsp *)cc; 1707 memset(buf, 0, sizeof(struct create_disk_id_rsp)); 1708 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1709 (struct create_disk_id_rsp, DiskFileId)); 1710 buf->ccontext.DataLength = cpu_to_le32(32); 1711 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1712 (struct create_mxac_rsp, Name)); 1713 buf->ccontext.NameLength = cpu_to_le16(4); 1714 /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */ 1715 buf->Name[0] = 'Q'; 1716 buf->Name[1] = 'F'; 1717 buf->Name[2] = 'i'; 1718 buf->Name[3] = 'd'; 1719 1720 buf->DiskFileId = cpu_to_le64(file_id); 1721 buf->VolumeId = cpu_to_le64(vol_id); 1722 } 1723 1724 /** 1725 * create_posix_rsp_buf() - create posix extension context 1726 * @cc: buffer to create posix on posix response 1727 * @fp: ksmbd file pointer 1728 */ 1729 void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp) 1730 { 1731 struct create_posix_rsp *buf; 1732 struct inode *inode = file_inode(fp->filp); 1733 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 1734 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 1735 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 1736 1737 buf = (struct create_posix_rsp *)cc; 1738 memset(buf, 0, sizeof(struct create_posix_rsp)); 1739 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1740 (struct create_posix_rsp, nlink)); 1741 /* 1742 * DataLength = nlink(4) + reparse_tag(4) + mode(4) + 1743 * domain sid(28) + unix group sid(16). 1744 */ 1745 buf->ccontext.DataLength = cpu_to_le32(56); 1746 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1747 (struct create_posix_rsp, Name)); 1748 buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); 1749 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ 1750 buf->Name[0] = 0x93; 1751 buf->Name[1] = 0xAD; 1752 buf->Name[2] = 0x25; 1753 buf->Name[3] = 0x50; 1754 buf->Name[4] = 0x9C; 1755 buf->Name[5] = 0xB4; 1756 buf->Name[6] = 0x11; 1757 buf->Name[7] = 0xE7; 1758 buf->Name[8] = 0xB4; 1759 buf->Name[9] = 0x23; 1760 buf->Name[10] = 0x83; 1761 buf->Name[11] = 0xDE; 1762 buf->Name[12] = 0x96; 1763 buf->Name[13] = 0x8B; 1764 buf->Name[14] = 0xCD; 1765 buf->Name[15] = 0x7C; 1766 1767 buf->nlink = cpu_to_le32(inode->i_nlink); 1768 buf->reparse_tag = cpu_to_le32(fp->volatile_id); 1769 buf->mode = cpu_to_le32(inode->i_mode & 0777); 1770 /* 1771 * SidBuffer(44) contain two sids(Domain sid(28), UNIX group sid(16)). 1772 * Domain sid(28) = revision(1) + num_subauth(1) + authority(6) + 1773 * sub_auth(4 * 4(num_subauth)) + RID(4). 1774 * UNIX group id(16) = revision(1) + num_subauth(1) + authority(6) + 1775 * sub_auth(4 * 1(num_subauth)) + RID(4). 1776 */ 1777 id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)), 1778 SIDOWNER, (struct smb_sid *)&buf->SidBuffer[0]); 1779 id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)), 1780 SIDUNIX_GROUP, (struct smb_sid *)&buf->SidBuffer[28]); 1781 } 1782 1783 /* 1784 * Find lease object(opinfo) for given lease key/fid from lease 1785 * break/file close path. 1786 */ 1787 /** 1788 * lookup_lease_in_table() - find a matching lease info object 1789 * @conn: connection instance 1790 * @lease_key: lease key to be searched for 1791 * 1792 * Return: opinfo if found matching opinfo, otherwise NULL 1793 */ 1794 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn, 1795 char *lease_key) 1796 { 1797 struct oplock_info *opinfo = NULL, *ret_op = NULL; 1798 struct lease_table *lt; 1799 int ret; 1800 1801 read_lock(&lease_list_lock); 1802 list_for_each_entry(lt, &lease_table_list, l_entry) { 1803 if (!memcmp(lt->client_guid, conn->ClientGUID, 1804 SMB2_CLIENT_GUID_SIZE)) 1805 goto found; 1806 } 1807 1808 read_unlock(&lease_list_lock); 1809 return NULL; 1810 1811 found: 1812 rcu_read_lock(); 1813 list_for_each_entry_rcu(opinfo, <->lease_list, lease_entry) { 1814 if (!atomic_inc_not_zero(&opinfo->refcount)) 1815 continue; 1816 rcu_read_unlock(); 1817 if (!opinfo->op_state || opinfo->op_state == OPLOCK_CLOSING) 1818 goto op_next; 1819 if (!(opinfo->o_lease->state & 1820 (SMB2_LEASE_HANDLE_CACHING_LE | 1821 SMB2_LEASE_WRITE_CACHING_LE))) 1822 goto op_next; 1823 ret = compare_guid_key(opinfo, conn->ClientGUID, 1824 lease_key); 1825 if (ret) { 1826 ksmbd_debug(OPLOCK, "found opinfo\n"); 1827 ret_op = opinfo; 1828 goto out; 1829 } 1830 op_next: 1831 opinfo_put(opinfo); 1832 rcu_read_lock(); 1833 } 1834 rcu_read_unlock(); 1835 1836 out: 1837 read_unlock(&lease_list_lock); 1838 return ret_op; 1839 } 1840 1841 int smb2_check_durable_oplock(struct ksmbd_conn *conn, 1842 struct ksmbd_share_config *share, 1843 struct ksmbd_file *fp, 1844 struct lease_ctx_info *lctx, 1845 struct ksmbd_user *user, 1846 char *name) 1847 { 1848 struct oplock_info *opinfo = opinfo_get(fp); 1849 int ret = 0; 1850 1851 if (!opinfo) 1852 return 0; 1853 1854 if (ksmbd_vfs_compare_durable_owner(fp, user) == false) { 1855 ksmbd_debug(SMB, "Durable handle reconnect failed: owner mismatch\n"); 1856 ret = -EBADF; 1857 goto out; 1858 } 1859 1860 if (opinfo->is_lease == false) { 1861 if (lctx) { 1862 pr_err("create context include lease\n"); 1863 ret = -EBADF; 1864 goto out; 1865 } 1866 1867 if (opinfo->level != SMB2_OPLOCK_LEVEL_BATCH) { 1868 pr_err("oplock level is not equal to SMB2_OPLOCK_LEVEL_BATCH\n"); 1869 ret = -EBADF; 1870 } 1871 1872 goto out; 1873 } 1874 1875 if (memcmp(conn->ClientGUID, fp->client_guid, 1876 SMB2_CLIENT_GUID_SIZE)) { 1877 ksmbd_debug(SMB, "Client guid of fp is not equal to the one of connection\n"); 1878 ret = -EBADF; 1879 goto out; 1880 } 1881 1882 if (!lctx) { 1883 ksmbd_debug(SMB, "create context does not include lease\n"); 1884 ret = -EBADF; 1885 goto out; 1886 } 1887 1888 if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key, 1889 SMB2_LEASE_KEY_SIZE)) { 1890 ksmbd_debug(SMB, 1891 "lease key of fp does not match lease key in create context\n"); 1892 ret = -EBADF; 1893 goto out; 1894 } 1895 1896 if (!(opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)) { 1897 ksmbd_debug(SMB, "lease state does not contain SMB2_LEASE_HANDLE_CACHING\n"); 1898 ret = -EBADF; 1899 goto out; 1900 } 1901 1902 if (opinfo->o_lease->version != lctx->version) { 1903 ksmbd_debug(SMB, 1904 "lease version of fp does not match the one in create context\n"); 1905 ret = -EBADF; 1906 goto out; 1907 } 1908 1909 if (!ksmbd_inode_pending_delete(fp)) 1910 ret = ksmbd_validate_name_reconnect(share, fp, name); 1911 out: 1912 opinfo_put(opinfo); 1913 return ret; 1914 } 1915