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 #include <linux/err.h> 9 10 #include "glob.h" 11 #include "oplock.h" 12 13 #include "smb_common.h" 14 #include "../common/smb2status.h" 15 #include "connection.h" 16 #include "mgmt/user_session.h" 17 #include "mgmt/share_config.h" 18 #include "mgmt/tree_connect.h" 19 #include "server.h" 20 21 static LIST_HEAD(lease_table_list); 22 static DEFINE_RWLOCK(lease_list_lock); 23 24 #define SMB2_LEASE_STATE_MASK_LE (SMB2_LEASE_READ_CACHING_LE | \ 25 SMB2_LEASE_HANDLE_CACHING_LE | \ 26 SMB2_LEASE_WRITE_CACHING_LE) 27 28 static bool lease_state_valid(__le32 state) 29 { 30 return !(state & ~SMB2_LEASE_STATE_MASK_LE); 31 } 32 33 static __le32 lease_state_grantable(__le32 state) 34 { 35 if (state == SMB2_LEASE_READ_CACHING_LE || 36 state == (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE) || 37 state == (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_WRITE_CACHING_LE) || 38 state == SMB2_LEASE_STATE_MASK_LE) 39 return state; 40 41 return 0; 42 } 43 44 static bool lease_v2_flags_valid(__le32 flags) 45 { 46 return !(flags & ~SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE); 47 } 48 49 static bool lease_has_parent_key(struct lease *lease) 50 { 51 return lease->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; 52 } 53 54 static bool lease_break_in_progress(struct lease *lease) 55 { 56 struct oplock_info *opinfo; 57 bool ret = false; 58 59 spin_lock(&lease->lock); 60 list_for_each_entry(opinfo, &lease->open_list, lease_entry) { 61 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 62 ret = true; 63 break; 64 } 65 } 66 spin_unlock(&lease->lock); 67 68 return ret; 69 } 70 71 /** 72 * alloc_opinfo() - allocate a new opinfo object for oplock info 73 * @work: smb work 74 * @id: fid of open file 75 * @Tid: tree id of connection 76 * 77 * Return: allocated opinfo object on success, otherwise NULL 78 */ 79 static struct oplock_info *alloc_opinfo(struct ksmbd_work *work, 80 u64 id, __u16 Tid) 81 { 82 struct ksmbd_session *sess = work->sess; 83 struct oplock_info *opinfo; 84 85 opinfo = kzalloc_obj(struct oplock_info, KSMBD_DEFAULT_GFP); 86 if (!opinfo) 87 return NULL; 88 89 opinfo->sess = sess; 90 opinfo->conn = ksmbd_conn_get(work->conn); 91 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 92 opinfo->op_state = OPLOCK_STATE_NONE; 93 spin_lock_init(&opinfo->state_lock); 94 opinfo->pending_break = 0; 95 opinfo->fid = id; 96 opinfo->Tid = Tid; 97 INIT_LIST_HEAD(&opinfo->op_entry); 98 INIT_LIST_HEAD(&opinfo->lease_entry); 99 init_waitqueue_head(&opinfo->oplock_q); 100 init_waitqueue_head(&opinfo->oplock_brk); 101 atomic_set(&opinfo->refcount, 1); 102 atomic_set(&opinfo->breaking_cnt, 0); 103 104 return opinfo; 105 } 106 107 static void lease_get(struct lease *lease) 108 { 109 atomic_inc(&lease->refcount); 110 } 111 112 static void lease_put(struct lease *lease) 113 { 114 if (lease && atomic_dec_and_test(&lease->refcount)) 115 kfree(lease); 116 } 117 118 static void lease_add_table(struct lease *lease, struct lease_table *lb) 119 { 120 lease_get(lease); 121 lease->l_lb = lb; 122 spin_lock(&lb->lb_lock); 123 list_add_rcu(&lease->l_entry, &lb->lease_list); 124 spin_unlock(&lb->lb_lock); 125 } 126 127 static void lease_del_table(struct lease *lease) 128 { 129 struct lease_table *lb = lease->l_lb; 130 131 if (!lb) 132 return; 133 134 spin_lock(&lb->lb_lock); 135 if (list_empty(&lease->l_entry)) { 136 spin_unlock(&lb->lb_lock); 137 return; 138 } 139 140 list_del_init(&lease->l_entry); 141 lease->l_lb = NULL; 142 spin_unlock(&lb->lb_lock); 143 144 lease_put(lease); 145 } 146 147 static struct lease_table *alloc_lease_table(struct oplock_info *opinfo) 148 { 149 struct lease_table *lb; 150 151 lb = kmalloc_obj(struct lease_table, KSMBD_DEFAULT_GFP); 152 if (!lb) 153 return NULL; 154 155 memcpy(lb->client_guid, opinfo->conn->ClientGUID, 156 SMB2_CLIENT_GUID_SIZE); 157 lb->conn = ksmbd_conn_get(opinfo->conn); 158 INIT_LIST_HEAD(&lb->lease_list); 159 spin_lock_init(&lb->lb_lock); 160 return lb; 161 } 162 163 static void free_lease_table(struct lease_table *lb) 164 { 165 if (!lb) 166 return; 167 168 ksmbd_conn_put(lb->conn); 169 kfree(lb); 170 } 171 172 static struct lease *alloc_lease(struct lease_ctx_info *lctx, 173 struct ksmbd_inode *ci) 174 { 175 struct lease *lease; 176 177 lease = kmalloc_obj(struct lease, KSMBD_DEFAULT_GFP); 178 if (!lease) 179 return NULL; 180 181 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 182 lease->state = lctx->req_state; 183 lease->new_state = 0; 184 lease->flags = lctx->flags; 185 lease->duration = lctx->duration; 186 lease->is_dir = lctx->is_dir; 187 memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE); 188 lease->version = lctx->version; 189 lease->epoch = lctx->version == 2 ? le16_to_cpu(lctx->epoch) + 1 : 0; 190 lease->ci = ci; 191 lease->reuse_epoch = false; 192 lease->l_lb = NULL; 193 INIT_LIST_HEAD(&lease->l_entry); 194 INIT_LIST_HEAD(&lease->open_list); 195 spin_lock_init(&lease->lock); 196 atomic_set(&lease->refcount, 1); 197 198 return lease; 199 } 200 201 static void lease_add_open(struct lease *lease, struct oplock_info *opinfo) 202 { 203 spin_lock(&lease->lock); 204 list_add(&opinfo->lease_entry, &lease->open_list); 205 spin_unlock(&lease->lock); 206 } 207 208 static void lease_del_open(struct oplock_info *opinfo) 209 { 210 struct lease *lease = opinfo->o_lease; 211 bool remove_table = false; 212 213 if (!lease) 214 return; 215 216 spin_lock(&lease->lock); 217 if (!list_empty(&opinfo->lease_entry)) { 218 list_del_init(&opinfo->lease_entry); 219 remove_table = list_empty(&lease->open_list); 220 } 221 spin_unlock(&lease->lock); 222 223 if (remove_table) { 224 write_lock(&lease_list_lock); 225 lease_del_table(lease); 226 write_unlock(&lease_list_lock); 227 } 228 } 229 230 static void free_lease(struct oplock_info *opinfo) 231 { 232 lease_put(opinfo->o_lease); 233 } 234 235 static void __free_opinfo(struct oplock_info *opinfo) 236 { 237 if (opinfo->is_lease) 238 free_lease(opinfo); 239 ksmbd_conn_put(opinfo->conn); 240 kfree(opinfo); 241 } 242 243 static void free_opinfo_rcu(struct rcu_head *rcu) 244 { 245 struct oplock_info *opinfo = container_of(rcu, struct oplock_info, rcu); 246 247 __free_opinfo(opinfo); 248 } 249 250 static void free_opinfo(struct oplock_info *opinfo) 251 { 252 call_rcu(&opinfo->rcu, free_opinfo_rcu); 253 } 254 255 void lease_update_oplock_levels(struct lease *lease) 256 { 257 struct oplock_info *opinfo; 258 __u8 level; 259 260 if (!lease) 261 return; 262 263 level = smb2_map_lease_to_oplock(lease->state); 264 spin_lock(&lease->lock); 265 list_for_each_entry(opinfo, &lease->open_list, lease_entry) 266 opinfo->level = level; 267 spin_unlock(&lease->lock); 268 } 269 270 struct oplock_info *opinfo_get(struct ksmbd_file *fp) 271 { 272 struct oplock_info *opinfo; 273 274 rcu_read_lock(); 275 opinfo = rcu_dereference(fp->f_opinfo); 276 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount)) 277 opinfo = NULL; 278 rcu_read_unlock(); 279 280 return opinfo; 281 } 282 283 struct oplock_snapshot { 284 bool durable_open; 285 bool durable_detached; 286 unsigned long long fid; 287 }; 288 289 static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci, 290 struct ksmbd_file *skip_fp, 291 struct oplock_snapshot *snapshot) 292 { 293 struct oplock_info *opinfo; 294 295 if (snapshot) { 296 snapshot->durable_open = false; 297 snapshot->durable_detached = false; 298 snapshot->fid = KSMBD_NO_FID; 299 } 300 301 down_read(&ci->m_lock); 302 opinfo = list_first_entry_or_null(&ci->m_op_list, struct oplock_info, 303 op_entry); 304 if (opinfo) { 305 if (opinfo->conn == NULL || 306 !atomic_inc_not_zero(&opinfo->refcount)) 307 opinfo = NULL; 308 else { 309 if (ksmbd_conn_releasing(opinfo->conn)) { 310 atomic_dec(&opinfo->refcount); 311 opinfo = NULL; 312 } 313 } 314 315 if (opinfo && snapshot && opinfo->o_fp && 316 opinfo->o_fp != skip_fp && 317 READ_ONCE(opinfo->o_fp->is_durable)) { 318 snapshot->durable_open = true; 319 snapshot->durable_detached = 320 !READ_ONCE(opinfo->o_fp->conn) || 321 !READ_ONCE(opinfo->o_fp->tcon); 322 snapshot->fid = opinfo->fid; 323 } 324 } 325 up_read(&ci->m_lock); 326 327 return opinfo; 328 } 329 330 void opinfo_put(struct oplock_info *opinfo) 331 { 332 if (!opinfo) 333 return; 334 335 if (!atomic_dec_and_test(&opinfo->refcount)) 336 return; 337 338 free_opinfo(opinfo); 339 } 340 341 static bool ksmbd_inode_has_lease(struct ksmbd_inode *ci) 342 { 343 struct oplock_info *opinfo = opinfo_get_list(ci, NULL, NULL); 344 bool is_lease; 345 346 if (!opinfo) 347 return false; 348 is_lease = opinfo->is_lease; 349 opinfo_put(opinfo); 350 return is_lease; 351 } 352 353 static void opinfo_add(struct oplock_info *opinfo, struct ksmbd_file *fp) 354 { 355 struct ksmbd_inode *ci = fp->f_ci; 356 357 down_write(&ci->m_lock); 358 list_add(&opinfo->op_entry, &ci->m_op_list); 359 up_write(&ci->m_lock); 360 } 361 362 static void opinfo_del(struct oplock_info *opinfo) 363 { 364 struct ksmbd_inode *ci = opinfo->o_fp->f_ci; 365 366 if (opinfo->is_lease) 367 lease_del_open(opinfo); 368 369 down_write(&ci->m_lock); 370 list_del(&opinfo->op_entry); 371 up_write(&ci->m_lock); 372 } 373 374 static unsigned long opinfo_count(struct ksmbd_file *fp) 375 { 376 if (ksmbd_stream_fd(fp)) 377 return atomic_read(&fp->f_ci->sop_count); 378 else 379 return atomic_read(&fp->f_ci->op_count); 380 } 381 382 static void opinfo_count_inc(struct ksmbd_file *fp) 383 { 384 if (ksmbd_stream_fd(fp)) 385 return atomic_inc(&fp->f_ci->sop_count); 386 else 387 return atomic_inc(&fp->f_ci->op_count); 388 } 389 390 static void opinfo_count_dec(struct ksmbd_file *fp) 391 { 392 if (ksmbd_stream_fd(fp)) 393 return atomic_dec(&fp->f_ci->sop_count); 394 else 395 return atomic_dec(&fp->f_ci->op_count); 396 } 397 398 /** 399 * opinfo_write_to_read() - convert a write oplock to read oplock 400 * @opinfo: current oplock info 401 * 402 * Return: 0 on success, otherwise -EINVAL 403 */ 404 int opinfo_write_to_read(struct oplock_info *opinfo) 405 { 406 struct lease *lease = opinfo->o_lease; 407 408 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 409 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { 410 pr_err("bad oplock(0x%x)\n", opinfo->level); 411 if (opinfo->is_lease) 412 pr_err("lease state(0x%x)\n", lease->state); 413 return -EINVAL; 414 } 415 opinfo->level = SMB2_OPLOCK_LEVEL_II; 416 417 if (opinfo->is_lease) { 418 lease->state = lease->new_state; 419 lease_update_oplock_levels(lease); 420 } 421 return 0; 422 } 423 424 /** 425 * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock 426 * @opinfo: current oplock info 427 * 428 * Return: 0 on success, otherwise -EINVAL 429 */ 430 int opinfo_read_handle_to_read(struct oplock_info *opinfo) 431 { 432 struct lease *lease = opinfo->o_lease; 433 434 lease->state = lease->new_state; 435 lease_update_oplock_levels(lease); 436 return 0; 437 } 438 439 /** 440 * opinfo_write_to_none() - convert a write oplock to none 441 * @opinfo: current oplock info 442 * 443 * Return: 0 on success, otherwise -EINVAL 444 */ 445 int opinfo_write_to_none(struct oplock_info *opinfo) 446 { 447 struct lease *lease = opinfo->o_lease; 448 449 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 450 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { 451 pr_err("bad oplock(0x%x)\n", opinfo->level); 452 if (opinfo->is_lease) 453 pr_err("lease state(0x%x)\n", lease->state); 454 return -EINVAL; 455 } 456 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 457 if (opinfo->is_lease) { 458 lease->state = lease->new_state; 459 lease_update_oplock_levels(lease); 460 } 461 return 0; 462 } 463 464 /** 465 * opinfo_read_to_none() - convert a write read to none 466 * @opinfo: current oplock info 467 * 468 * Return: 0 on success, otherwise -EINVAL 469 */ 470 int opinfo_read_to_none(struct oplock_info *opinfo) 471 { 472 struct lease *lease = opinfo->o_lease; 473 474 if (opinfo->level != SMB2_OPLOCK_LEVEL_II) { 475 pr_err("bad oplock(0x%x)\n", opinfo->level); 476 if (opinfo->is_lease) 477 pr_err("lease state(0x%x)\n", lease->state); 478 return -EINVAL; 479 } 480 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 481 if (opinfo->is_lease) { 482 lease->state = lease->new_state; 483 lease_update_oplock_levels(lease); 484 } 485 return 0; 486 } 487 488 /** 489 * lease_read_to_write() - upgrade lease state from read to write 490 * @opinfo: current lease info 491 * 492 * Return: 0 on success, otherwise -EINVAL 493 */ 494 int lease_read_to_write(struct oplock_info *opinfo) 495 { 496 struct lease *lease = opinfo->o_lease; 497 498 if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) { 499 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); 500 return -EINVAL; 501 } 502 503 lease->new_state = SMB2_LEASE_NONE_LE; 504 lease->state |= SMB2_LEASE_WRITE_CACHING_LE; 505 lease_update_oplock_levels(lease); 506 return 0; 507 } 508 509 /** 510 * lease_none_upgrade() - upgrade lease state from none 511 * @opinfo: current lease info 512 * @new_state: new lease state 513 * 514 * Return: 0 on success, otherwise -EINVAL 515 */ 516 static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state) 517 { 518 struct lease *lease = opinfo->o_lease; 519 520 if (!(lease->state == SMB2_LEASE_NONE_LE)) { 521 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); 522 return -EINVAL; 523 } 524 525 lease->new_state = SMB2_LEASE_NONE_LE; 526 lease->state = new_state; 527 lease_update_oplock_levels(lease); 528 529 return 0; 530 } 531 532 /** 533 * close_id_del_oplock() - release oplock object at file close time 534 * @fp: ksmbd file pointer 535 */ 536 void close_id_del_oplock(struct ksmbd_file *fp) 537 { 538 struct oplock_info *opinfo; 539 540 if (fp->reserve_lease_break) 541 smb_lazy_parent_lease_break_close(fp); 542 543 opinfo = opinfo_get(fp); 544 if (!opinfo) 545 return; 546 547 opinfo_del(opinfo); 548 549 rcu_assign_pointer(fp->f_opinfo, NULL); 550 spin_lock(&opinfo->state_lock); 551 if (opinfo->op_state == OPLOCK_ACK_WAIT && opinfo->is_lease) 552 atomic_set(&opinfo->breaking_cnt, 0); 553 /* 554 * An opinfo that has been removed from the inode list is terminal. Keep 555 * this transition and releasing pending_break under state_lock. a breaker 556 * takes the same lock before it acquires pending_break or sets ACK_WAIT. 557 */ 558 opinfo->op_state = OPLOCK_CLOSING; 559 clear_bit_unlock(0, &opinfo->pending_break); 560 spin_unlock(&opinfo->state_lock); 561 wake_up_interruptible_all(&opinfo->oplock_q); 562 if (opinfo->is_lease) 563 wake_up_interruptible_all(&opinfo->oplock_brk); 564 /* memory barrier is needed for wake_up_bit() */ 565 smp_mb__after_atomic(); 566 wake_up_bit(&opinfo->pending_break, 0); 567 568 opinfo_count_dec(fp); 569 atomic_dec(&opinfo->refcount); 570 opinfo_put(opinfo); 571 } 572 573 /** 574 * grant_write_oplock() - grant exclusive/batch oplock or write lease 575 * @opinfo_new: new oplock info object 576 * @req_oplock: request oplock 577 * @lctx: lease context information 578 * 579 * Return: 0 580 */ 581 static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock, 582 struct lease_ctx_info *lctx) 583 { 584 struct lease *lease = opinfo_new->o_lease; 585 586 if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH) 587 opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH; 588 else 589 opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; 590 591 if (lctx) { 592 lease->state = lctx->req_state; 593 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 594 } 595 } 596 597 /** 598 * grant_read_oplock() - grant level2 oplock or read lease 599 * @opinfo_new: new oplock info object 600 * @lctx: lease context information 601 * 602 * Return: 0 603 */ 604 static void grant_read_oplock(struct oplock_info *opinfo_new, 605 struct lease_ctx_info *lctx) 606 { 607 struct lease *lease = opinfo_new->o_lease; 608 609 opinfo_new->level = SMB2_OPLOCK_LEVEL_II; 610 611 if (lctx) { 612 lease->state = SMB2_LEASE_READ_CACHING_LE; 613 if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE) 614 lease->state |= SMB2_LEASE_HANDLE_CACHING_LE; 615 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 616 } 617 } 618 619 /** 620 * grant_none_oplock() - grant none oplock or none lease 621 * @opinfo_new: new oplock info object 622 * @lctx: lease context information 623 * 624 * Return: 0 625 */ 626 static void grant_none_oplock(struct oplock_info *opinfo_new, 627 struct lease_ctx_info *lctx) 628 { 629 struct lease *lease = opinfo_new->o_lease; 630 631 opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE; 632 633 if (lctx) { 634 lease->state = 0; 635 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 636 } 637 } 638 639 static inline int compare_guid_key(struct oplock_info *opinfo, 640 const char *guid1, const char *key1) 641 { 642 const char *guid2, *key2; 643 struct ksmbd_conn *conn; 644 645 conn = READ_ONCE(opinfo->conn); 646 if (!conn) 647 return 0; 648 guid2 = conn->ClientGUID; 649 key2 = opinfo->o_lease->lease_key; 650 if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) && 651 !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE)) 652 return 1; 653 654 return 0; 655 } 656 657 /** 658 * same_client_has_lease() - check whether current lease request is 659 * from lease owner of file 660 * @ci: master file pointer 661 * @client_guid: Client GUID 662 * @lctx: lease context information 663 * 664 * Return: oplock(lease) object on success, otherwise NULL 665 */ 666 static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci, 667 const char *client_guid, 668 struct lease_ctx_info *lctx) 669 { 670 int ret; 671 struct lease *lease; 672 struct oplock_info *opinfo; 673 struct oplock_info *m_opinfo = NULL; 674 675 if (!lctx) 676 return NULL; 677 678 /* 679 * Compare lease key and client_guid to know request from same owner 680 * of same client 681 */ 682 down_read(&ci->m_lock); 683 list_for_each_entry(opinfo, &ci->m_op_list, op_entry) { 684 if (!opinfo->is_lease || !opinfo->conn) 685 continue; 686 lease = opinfo->o_lease; 687 688 ret = compare_guid_key(opinfo, client_guid, lctx->lease_key); 689 if (ret) { 690 if (!atomic_inc_not_zero(&opinfo->refcount)) 691 continue; 692 if (m_opinfo) 693 opinfo_put(m_opinfo); 694 m_opinfo = opinfo; 695 696 /* skip upgrading lease about breaking lease */ 697 if (atomic_read(&opinfo->breaking_cnt)) 698 continue; 699 700 /* upgrading lease */ 701 if ((atomic_read(&ci->op_count) + 702 atomic_read(&ci->sop_count)) == 1) { 703 if (lease->state != SMB2_LEASE_NONE_LE && 704 lease->state == (lctx->req_state & lease->state)) { 705 lease->epoch++; 706 lease->state |= lctx->req_state; 707 if (lctx->req_state & 708 SMB2_LEASE_WRITE_CACHING_LE) 709 lease_read_to_write(opinfo); 710 711 } 712 } else if ((atomic_read(&ci->op_count) + 713 atomic_read(&ci->sop_count)) > 1) { 714 if (lctx->req_state == 715 (SMB2_LEASE_READ_CACHING_LE | 716 SMB2_LEASE_HANDLE_CACHING_LE)) { 717 if (lease->state != lctx->req_state) { 718 lease->epoch++; 719 lease->state = lctx->req_state; 720 lease_update_oplock_levels(lease); 721 } 722 } 723 } 724 725 if (lctx->req_state && lease->state == 726 SMB2_LEASE_NONE_LE) { 727 lease->epoch++; 728 lease_none_upgrade(opinfo, lctx->req_state); 729 } 730 } 731 } 732 up_read(&ci->m_lock); 733 734 return m_opinfo; 735 } 736 737 static bool wait_for_break_ack(struct oplock_info *opinfo) 738 { 739 int rc = 0; 740 741 rc = wait_event_interruptible_timeout(opinfo->oplock_q, 742 opinfo->op_state == OPLOCK_STATE_NONE || 743 opinfo->op_state == OPLOCK_CLOSING, 744 OPLOCK_WAIT_TIME); 745 746 /* is this a timeout ? */ 747 if (!rc) { 748 spin_lock(&opinfo->state_lock); 749 if (opinfo->op_state == OPLOCK_CLOSING) { 750 spin_unlock(&opinfo->state_lock); 751 return false; 752 } 753 if (opinfo->is_lease) { 754 opinfo->o_lease->state = SMB2_LEASE_NONE_LE; 755 lease_update_oplock_levels(opinfo->o_lease); 756 } 757 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 758 opinfo->op_state = OPLOCK_STATE_NONE; 759 spin_unlock(&opinfo->state_lock); 760 return true; 761 } 762 763 return false; 764 } 765 766 static void wake_up_oplock_break(struct oplock_info *opinfo) 767 { 768 clear_bit_unlock(0, &opinfo->pending_break); 769 /* memory barrier is needed for wake_up_bit() */ 770 smp_mb__after_atomic(); 771 wake_up_bit(&opinfo->pending_break, 0); 772 } 773 774 static bool oplock_break_set_ack_wait(struct oplock_info *opinfo) 775 { 776 bool ret = false; 777 778 spin_lock(&opinfo->state_lock); 779 if (opinfo->op_state != OPLOCK_CLOSING) { 780 opinfo->op_state = OPLOCK_ACK_WAIT; 781 ret = true; 782 } 783 spin_unlock(&opinfo->state_lock); 784 785 return ret; 786 } 787 788 static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level) 789 { 790 for (;;) { 791 bool closing; 792 793 spin_lock(&opinfo->state_lock); 794 closing = opinfo->op_state == OPLOCK_CLOSING; 795 if (!closing && !test_and_set_bit(0, &opinfo->pending_break)) { 796 spin_unlock(&opinfo->state_lock); 797 break; 798 } 799 spin_unlock(&opinfo->state_lock); 800 if (closing) 801 return -ENOENT; 802 803 if (opinfo->is_lease) 804 opinfo->o_lease->reuse_epoch = true; 805 806 wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE); 807 808 /* Not immediately break to none. */ 809 opinfo->open_trunc = 0; 810 811 spin_lock(&opinfo->state_lock); 812 closing = opinfo->op_state == OPLOCK_CLOSING; 813 spin_unlock(&opinfo->state_lock); 814 if (closing) 815 return -ENOENT; 816 if (opinfo->level <= req_op_level) { 817 if (opinfo->is_lease == false) 818 return 1; 819 820 if (opinfo->o_lease->state != 821 (SMB2_LEASE_HANDLE_CACHING_LE | 822 SMB2_LEASE_READ_CACHING_LE)) 823 return 1; 824 } 825 } 826 827 if (opinfo->level <= req_op_level) { 828 if (opinfo->is_lease == false) { 829 wake_up_oplock_break(opinfo); 830 return 1; 831 } 832 if (opinfo->o_lease->state != 833 (SMB2_LEASE_HANDLE_CACHING_LE | 834 SMB2_LEASE_READ_CACHING_LE)) { 835 wake_up_oplock_break(opinfo); 836 return 1; 837 } 838 } 839 return 0; 840 } 841 842 static bool lease_break_needed(struct oplock_info *opinfo, int req_op_level, 843 bool open_trunc) 844 { 845 struct lease *lease = opinfo->o_lease; 846 847 if (open_trunc) 848 return lease->state != SMB2_LEASE_NONE_LE; 849 850 return opinfo->level > req_op_level; 851 } 852 853 /** 854 * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn 855 * to client 856 * @wk: smb work object 857 * 858 * There are two ways this function can be called. 1- while file open we break 859 * from exclusive/batch lock to levelII oplock and 2- while file write/truncate 860 * we break from levelII oplock no oplock. 861 * work->request_buf contains oplock_info. 862 */ 863 static void __smb2_oplock_break_noti(struct work_struct *wk) 864 { 865 struct smb2_oplock_break *rsp = NULL; 866 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work); 867 struct ksmbd_conn *conn = work->conn; 868 struct oplock_break_info *br_info = work->request_buf; 869 struct smb2_hdr *rsp_hdr; 870 struct ksmbd_file *fp; 871 872 fp = ksmbd_lookup_global_fd(br_info->fid); 873 if (!fp) 874 goto out; 875 876 if (allocate_interim_rsp_buf(work)) { 877 pr_err("smb2_allocate_rsp_buf failed! "); 878 ksmbd_fd_put(work, fp); 879 goto out; 880 } 881 882 rsp_hdr = smb_get_msg(work->response_buf); 883 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 884 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; 885 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 886 rsp_hdr->CreditRequest = cpu_to_le16(0); 887 rsp_hdr->Command = SMB2_OPLOCK_BREAK; 888 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); 889 rsp_hdr->NextCommand = 0; 890 rsp_hdr->MessageId = cpu_to_le64(-1); 891 rsp_hdr->Id.SyncId.ProcessId = 0; 892 rsp_hdr->Id.SyncId.TreeId = 0; 893 rsp_hdr->SessionId = 0; 894 memset(rsp_hdr->Signature, 0, 16); 895 896 rsp = smb_get_msg(work->response_buf); 897 898 rsp->StructureSize = cpu_to_le16(24); 899 if (!br_info->open_trunc && 900 (br_info->level == SMB2_OPLOCK_LEVEL_BATCH || 901 br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) 902 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II; 903 else 904 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; 905 rsp->Reserved = 0; 906 rsp->Reserved2 = 0; 907 rsp->PersistentFid = fp->persistent_id; 908 rsp->VolatileFid = fp->volatile_id; 909 910 ksmbd_fd_put(work, fp); 911 if (ksmbd_iov_pin_rsp(work, (void *)rsp, 912 sizeof(struct smb2_oplock_break))) 913 goto out; 914 915 ksmbd_debug(OPLOCK, 916 "sending oplock break v_id %llu p_id = %llu lock level = %d\n", 917 rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel); 918 919 ksmbd_conn_write(work); 920 921 out: 922 ksmbd_free_work_struct(work); 923 ksmbd_conn_r_count_dec(conn); 924 ksmbd_conn_put(conn); 925 } 926 927 /** 928 * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock 929 * break command from server to client 930 * @opinfo: oplock info object 931 * 932 * Return: 0 on success, otherwise error 933 */ 934 static int smb2_oplock_break_noti(struct oplock_info *opinfo) 935 { 936 struct ksmbd_conn *conn; 937 struct oplock_break_info *br_info; 938 int ret = 0; 939 struct ksmbd_work *work; 940 941 conn = READ_ONCE(opinfo->conn); 942 if (!conn) 943 return ksmbd_invalidate_durable_fd(opinfo->fid); 944 945 work = ksmbd_alloc_work_struct(); 946 if (!work) 947 return -ENOMEM; 948 949 br_info = kmalloc_obj(struct oplock_break_info, KSMBD_DEFAULT_GFP); 950 if (!br_info) { 951 ksmbd_free_work_struct(work); 952 return -ENOMEM; 953 } 954 955 br_info->level = opinfo->level; 956 br_info->fid = opinfo->fid; 957 br_info->open_trunc = opinfo->open_trunc; 958 959 work->request_buf = (char *)br_info; 960 work->conn = ksmbd_conn_get(conn); 961 work->sess = opinfo->sess; 962 963 ksmbd_conn_r_count_inc(conn); 964 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 965 INIT_WORK(&work->work, __smb2_oplock_break_noti); 966 ksmbd_queue_work(work); 967 968 if (wait_for_break_ack(opinfo)) 969 ret = ksmbd_invalidate_durable_fd(opinfo->fid); 970 } else { 971 __smb2_oplock_break_noti(&work->work); 972 if (opinfo->level == SMB2_OPLOCK_LEVEL_II) 973 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 974 } 975 return ret; 976 } 977 978 /** 979 * __smb2_lease_break_noti() - send lease break command from server 980 * to client 981 * @wk: smb work object 982 */ 983 static void __smb2_lease_break_noti(struct work_struct *wk) 984 { 985 struct smb2_lease_break *rsp = NULL; 986 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work); 987 struct ksmbd_conn *conn = work->conn; 988 struct lease_break_info *br_info = work->request_buf; 989 struct smb2_hdr *rsp_hdr; 990 991 if (allocate_interim_rsp_buf(work)) { 992 ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! "); 993 goto out; 994 } 995 996 rsp_hdr = smb_get_msg(work->response_buf); 997 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 998 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; 999 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 1000 rsp_hdr->CreditRequest = cpu_to_le16(0); 1001 rsp_hdr->Command = SMB2_OPLOCK_BREAK; 1002 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); 1003 rsp_hdr->NextCommand = 0; 1004 rsp_hdr->MessageId = cpu_to_le64(-1); 1005 rsp_hdr->Id.SyncId.ProcessId = 0; 1006 rsp_hdr->Id.SyncId.TreeId = 0; 1007 rsp_hdr->SessionId = 0; 1008 memset(rsp_hdr->Signature, 0, 16); 1009 1010 rsp = smb_get_msg(work->response_buf); 1011 rsp->StructureSize = cpu_to_le16(44); 1012 rsp->Epoch = br_info->epoch; 1013 rsp->Flags = 0; 1014 1015 if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE | 1016 SMB2_LEASE_HANDLE_CACHING_LE)) 1017 rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED; 1018 1019 memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE); 1020 rsp->CurrentLeaseState = br_info->curr_state; 1021 rsp->NewLeaseState = br_info->new_state; 1022 rsp->BreakReason = 0; 1023 rsp->AccessMaskHint = 0; 1024 rsp->ShareMaskHint = 0; 1025 1026 if (ksmbd_iov_pin_rsp(work, (void *)rsp, 1027 sizeof(struct smb2_lease_break))) 1028 goto out; 1029 1030 ksmbd_conn_write(work); 1031 1032 out: 1033 ksmbd_free_work_struct(work); 1034 ksmbd_conn_r_count_dec(conn); 1035 ksmbd_conn_put(conn); 1036 } 1037 1038 /* 1039 * Select and pin the connection used for a lease break before doing any 1040 * allocations which may sleep. opinfo->conn is cleared under ci->m_lock, 1041 * while lease->l_lb and the lease table lifetime are protected by 1042 * lease_list_lock. 1043 */ 1044 static struct ksmbd_conn *smb2_lease_break_conn_get(struct oplock_info *opinfo) 1045 { 1046 struct lease *lease = opinfo->o_lease; 1047 struct lease_table *lb; 1048 struct ksmbd_conn *conn; 1049 1050 /* Keep the connection which owns the open, when it is still active. */ 1051 down_read(&lease->ci->m_lock); 1052 conn = READ_ONCE(opinfo->conn); 1053 if (conn && !ksmbd_conn_releasing(conn)) 1054 conn = ksmbd_conn_get(conn); 1055 else 1056 conn = NULL; 1057 up_read(&lease->ci->m_lock); 1058 1059 if (conn || lease->version != 2) 1060 return conn; 1061 1062 /* Otherwise route v2 lease breaks through the shared lease channel. */ 1063 read_lock(&lease_list_lock); 1064 lb = lease->l_lb; 1065 if (lb && lb->conn && !ksmbd_conn_releasing(lb->conn)) 1066 conn = ksmbd_conn_get(lb->conn); 1067 read_unlock(&lease_list_lock); 1068 1069 return conn; 1070 } 1071 1072 /** 1073 * smb2_lease_break_noti() - break lease when a new client request 1074 * write lease 1075 * @opinfo: contains lease state information 1076 * @sync: send the lease break notification synchronously 1077 * @inc_epoch: increment the lease epoch before sending the break 1078 * 1079 * Return: 0 on success, otherwise error 1080 */ 1081 static int smb2_lease_break_noti(struct oplock_info *opinfo, bool sync, 1082 bool inc_epoch) 1083 { 1084 struct ksmbd_conn *conn; 1085 struct ksmbd_work *work; 1086 struct lease_break_info *br_info; 1087 struct lease *lease = opinfo->o_lease; 1088 1089 conn = smb2_lease_break_conn_get(opinfo); 1090 if (!conn) 1091 return ksmbd_invalidate_durable_fd(opinfo->fid); 1092 1093 work = ksmbd_alloc_work_struct(); 1094 if (!work) { 1095 ksmbd_conn_put(conn); 1096 return -ENOMEM; 1097 } 1098 1099 br_info = kmalloc_obj(struct lease_break_info, KSMBD_DEFAULT_GFP); 1100 if (!br_info) { 1101 ksmbd_free_work_struct(work); 1102 ksmbd_conn_put(conn); 1103 return -ENOMEM; 1104 } 1105 1106 br_info->curr_state = lease->state; 1107 br_info->new_state = lease->new_state; 1108 if (lease->version == 2) { 1109 if (inc_epoch) 1110 lease->epoch++; 1111 br_info->epoch = cpu_to_le16(lease->epoch); 1112 } else { 1113 br_info->epoch = 0; 1114 } 1115 memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE); 1116 1117 work->request_buf = (char *)br_info; 1118 /* Transfer the reference acquired by smb2_lease_break_conn_get(). */ 1119 work->conn = conn; 1120 work->sess = opinfo->sess; 1121 1122 ksmbd_conn_r_count_inc(conn); 1123 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 1124 if (sync) { 1125 __smb2_lease_break_noti(&work->work); 1126 } else { 1127 INIT_WORK(&work->work, __smb2_lease_break_noti); 1128 ksmbd_queue_work(work); 1129 } 1130 } else { 1131 __smb2_lease_break_noti(&work->work); 1132 if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) { 1133 opinfo->o_lease->state = SMB2_LEASE_NONE_LE; 1134 lease_update_oplock_levels(opinfo->o_lease); 1135 } 1136 } 1137 return 0; 1138 } 1139 1140 static void wait_lease_breaking(struct oplock_info *opinfo) 1141 { 1142 if (!opinfo->is_lease) 1143 return; 1144 1145 wake_up_interruptible_all(&opinfo->oplock_brk); 1146 if (atomic_read(&opinfo->breaking_cnt)) { 1147 int ret = 0; 1148 1149 ret = wait_event_interruptible_timeout(opinfo->oplock_brk, 1150 atomic_read(&opinfo->breaking_cnt) == 0, 1151 HZ); 1152 if (!ret) 1153 atomic_set(&opinfo->breaking_cnt, 0); 1154 } 1155 } 1156 1157 static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, 1158 struct ksmbd_work *in_work, bool share_break, 1159 bool sync_lease_break) 1160 { 1161 int err = 0; 1162 bool sent_interim = false; 1163 1164 /* Need to break exclusive/batch oplock, write lease or overwrite_if */ 1165 ksmbd_debug(OPLOCK, 1166 "request to send oplock(level : 0x%x) break notification\n", 1167 brk_opinfo->level); 1168 1169 if (brk_opinfo->is_lease) { 1170 struct lease *lease = brk_opinfo->o_lease; 1171 bool open_trunc = brk_opinfo->open_trunc; 1172 bool was_pending = test_bit(0, &brk_opinfo->pending_break); 1173 bool wait_ack; 1174 bool inc_epoch = true; 1175 1176 if (in_work && was_pending) { 1177 setup_async_work(in_work, NULL, NULL); 1178 smb2_send_interim_resp(in_work, STATUS_PENDING); 1179 release_async_work(in_work); 1180 sent_interim = true; 1181 } 1182 1183 err = oplock_break_pending(brk_opinfo, req_op_level); 1184 if (err) 1185 return err < 0 ? err : 0; 1186 if (was_pending) 1187 open_trunc = brk_opinfo->open_trunc; 1188 1189 again: 1190 atomic_inc(&brk_opinfo->breaking_cnt); 1191 if (open_trunc) { 1192 /* 1193 * Create overwrite break trigger the lease break to 1194 * none. 1195 */ 1196 lease->new_state = SMB2_LEASE_NONE_LE; 1197 } else if (share_break && 1198 lease->state & SMB2_LEASE_HANDLE_CACHING_LE) { 1199 lease->new_state = 1200 lease->state & ~SMB2_LEASE_HANDLE_CACHING_LE; 1201 } else { 1202 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) { 1203 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 1204 lease->new_state = 1205 SMB2_LEASE_READ_CACHING_LE | 1206 SMB2_LEASE_HANDLE_CACHING_LE; 1207 else 1208 lease->new_state = 1209 SMB2_LEASE_READ_CACHING_LE; 1210 } else { 1211 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE && 1212 !lease->is_dir) 1213 lease->new_state = 1214 SMB2_LEASE_READ_CACHING_LE; 1215 else 1216 lease->new_state = SMB2_LEASE_NONE_LE; 1217 } 1218 } 1219 1220 if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE | 1221 SMB2_LEASE_HANDLE_CACHING_LE)) { 1222 if (!oplock_break_set_ack_wait(brk_opinfo)) { 1223 atomic_dec_if_positive(&brk_opinfo->breaking_cnt); 1224 wake_up_oplock_break(brk_opinfo); 1225 return -ENOENT; 1226 } 1227 } else 1228 atomic_dec(&brk_opinfo->breaking_cnt); 1229 1230 wait_ack = !(open_trunc && 1231 lease->state == (SMB2_LEASE_READ_CACHING_LE | 1232 SMB2_LEASE_HANDLE_CACHING_LE)); 1233 if (lease->reuse_epoch) { 1234 inc_epoch = false; 1235 lease->reuse_epoch = false; 1236 } 1237 err = smb2_lease_break_noti(brk_opinfo, sync_lease_break, inc_epoch); 1238 inc_epoch = false; 1239 if (in_work && !sent_interim) { 1240 setup_async_work(in_work, NULL, NULL); 1241 smb2_send_interim_resp(in_work, STATUS_PENDING); 1242 release_async_work(in_work); 1243 sent_interim = true; 1244 } 1245 if (wait_ack && !err && wait_for_break_ack(brk_opinfo)) 1246 err = ksmbd_invalidate_durable_fd(brk_opinfo->fid); 1247 1248 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level); 1249 if (brk_opinfo->op_state == OPLOCK_CLOSING) 1250 err = -ENOENT; 1251 1252 if (wait_ack) 1253 wait_lease_breaking(brk_opinfo); 1254 /* 1255 * A share-mode conflict break only drops the conflicting 1256 * caching bit; the triggering open fails with a sharing 1257 * violation, so keep it to a single break. 1258 * 1259 * Otherwise chain another break while the lease is still 1260 * incompatible with this open (req_op_level), or while a 1261 * truncating waiter that arrived during the break still needs 1262 * the lease dropped to none. open_trunc snapshotted for this 1263 * break stays cleared, so the next state is computed from the 1264 * lease state and the cascade steps down (e.g. RH->R->none) 1265 * instead of collapsing straight to none. 1266 */ 1267 if (wait_ack && !err && !share_break && 1268 (lease_break_needed(brk_opinfo, req_op_level, open_trunc) || 1269 (brk_opinfo->open_trunc && 1270 lease->state != SMB2_LEASE_NONE_LE))) 1271 goto again; 1272 1273 wake_up_oplock_break(brk_opinfo); 1274 return err; 1275 } else { 1276 err = oplock_break_pending(brk_opinfo, req_op_level); 1277 if (err) 1278 return err < 0 ? err : 0; 1279 1280 if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 1281 brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1282 if (!oplock_break_set_ack_wait(brk_opinfo)) { 1283 wake_up_oplock_break(brk_opinfo); 1284 return -ENOENT; 1285 } 1286 } 1287 1288 /* 1289 * Keep a conflicting CREATE asynchronous while waiting for an 1290 * oplock-break acknowledgement. Besides avoiding a blocked client 1291 * request, this lets a replay arrive while the original CREATE is 1292 * still pending and be rejected with FILE_NOT_AVAILABLE. 1293 */ 1294 if (in_work) { 1295 setup_async_work(in_work, NULL, NULL); 1296 smb2_send_interim_resp(in_work, STATUS_PENDING); 1297 release_async_work(in_work); 1298 } 1299 } 1300 1301 err = smb2_oplock_break_noti(brk_opinfo); 1302 1303 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level); 1304 if (brk_opinfo->op_state == OPLOCK_CLOSING) 1305 err = -EAGAIN; 1306 wake_up_oplock_break(brk_opinfo); 1307 1308 return err; 1309 } 1310 1311 struct oplock_break_entry { 1312 struct list_head list; 1313 struct oplock_info *opinfo; 1314 }; 1315 1316 static int oplock_break_add(struct list_head *head, struct oplock_info *opinfo) 1317 { 1318 struct oplock_break_entry *ent; 1319 1320 ent = kmalloc_obj(struct oplock_break_entry, KSMBD_DEFAULT_GFP); 1321 if (!ent) 1322 return -ENOMEM; 1323 1324 ent->opinfo = opinfo; 1325 list_add_tail(&ent->list, head); 1326 return 0; 1327 } 1328 1329 static void oplock_break_drain_none(struct list_head *head) 1330 { 1331 struct oplock_break_entry *ent, *tmp; 1332 1333 list_for_each_entry_safe(ent, tmp, head, list) { 1334 oplock_break(ent->opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL, false, 1335 false); 1336 list_del(&ent->list); 1337 opinfo_put(ent->opinfo); 1338 kfree(ent); 1339 } 1340 } 1341 1342 void destroy_lease_table(struct ksmbd_conn *conn) 1343 { 1344 struct lease_table *lb, *lbtmp; 1345 struct lease *lease, *ltmp; 1346 1347 write_lock(&lease_list_lock); 1348 if (list_empty(&lease_table_list)) { 1349 write_unlock(&lease_list_lock); 1350 return; 1351 } 1352 1353 list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) { 1354 if (conn && memcmp(lb->client_guid, conn->ClientGUID, 1355 SMB2_CLIENT_GUID_SIZE)) 1356 continue; 1357 list_for_each_entry_safe(lease, ltmp, &lb->lease_list, l_entry) 1358 lease_del_table(lease); 1359 list_del(&lb->l_entry); 1360 free_lease_table(lb); 1361 } 1362 write_unlock(&lease_list_lock); 1363 } 1364 1365 int find_same_lease_key(struct ksmbd_conn *conn, struct ksmbd_inode *ci, 1366 struct lease_ctx_info *lctx) 1367 { 1368 struct lease *lease; 1369 int err = 0; 1370 struct lease_table *lb; 1371 1372 if (!lctx) 1373 return err; 1374 1375 read_lock(&lease_list_lock); 1376 if (list_empty(&lease_table_list)) { 1377 read_unlock(&lease_list_lock); 1378 return 0; 1379 } 1380 1381 list_for_each_entry(lb, &lease_table_list, l_entry) { 1382 if (!memcmp(lb->client_guid, conn->ClientGUID, 1383 SMB2_CLIENT_GUID_SIZE)) 1384 goto found; 1385 } 1386 read_unlock(&lease_list_lock); 1387 1388 return 0; 1389 1390 found: 1391 list_for_each_entry(lease, &lb->lease_list, l_entry) { 1392 if (lease->ci == ci) 1393 continue; 1394 if (!memcmp(lease->lease_key, lctx->lease_key, 1395 SMB2_LEASE_KEY_SIZE)) { 1396 err = -EINVAL; 1397 ksmbd_debug(OPLOCK, 1398 "found same lease key is already used in other files\n"); 1399 goto out; 1400 } 1401 } 1402 1403 out: 1404 read_unlock(&lease_list_lock); 1405 return err; 1406 } 1407 1408 static void add_lease_global_list(struct lease *lease, struct ksmbd_conn *conn, 1409 struct lease_table *new_lb) 1410 { 1411 struct lease_table *lb; 1412 1413 write_lock(&lease_list_lock); 1414 list_for_each_entry(lb, &lease_table_list, l_entry) { 1415 if (!memcmp(lb->client_guid, conn->ClientGUID, 1416 SMB2_CLIENT_GUID_SIZE)) { 1417 lease_add_table(lease, lb); 1418 write_unlock(&lease_list_lock); 1419 free_lease_table(new_lb); 1420 return; 1421 } 1422 } 1423 1424 lease_add_table(lease, new_lb); 1425 list_add(&new_lb->l_entry, &lease_table_list); 1426 write_unlock(&lease_list_lock); 1427 } 1428 1429 static void set_oplock_level(struct oplock_info *opinfo, int level, 1430 struct lease_ctx_info *lctx) 1431 { 1432 switch (level) { 1433 case SMB2_OPLOCK_LEVEL_BATCH: 1434 case SMB2_OPLOCK_LEVEL_EXCLUSIVE: 1435 grant_write_oplock(opinfo, level, lctx); 1436 break; 1437 case SMB2_OPLOCK_LEVEL_II: 1438 grant_read_oplock(opinfo, lctx); 1439 break; 1440 default: 1441 grant_none_oplock(opinfo, lctx); 1442 break; 1443 } 1444 } 1445 1446 void smb_send_parent_lease_break_noti(struct ksmbd_file *fp, 1447 struct lease_ctx_info *lctx) 1448 { 1449 struct oplock_info *opinfo; 1450 struct ksmbd_inode *p_ci = NULL; 1451 LIST_HEAD(brk_list); 1452 1453 if (lctx && lctx->version != 2) 1454 return; 1455 1456 p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent); 1457 if (!p_ci) 1458 return; 1459 1460 down_read(&p_ci->m_lock); 1461 list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) { 1462 if (opinfo->conn == NULL || !opinfo->is_lease) 1463 continue; 1464 1465 if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE && 1466 (!lctx || 1467 (!(lctx->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) || 1468 !compare_guid_key(opinfo, fp->conn->ClientGUID, 1469 lctx->parent_lease_key)))) { 1470 if (!atomic_inc_not_zero(&opinfo->refcount)) 1471 continue; 1472 1473 if (ksmbd_conn_releasing(opinfo->conn)) { 1474 opinfo_put(opinfo); 1475 continue; 1476 } 1477 1478 if (oplock_break_add(&brk_list, opinfo)) 1479 opinfo_put(opinfo); 1480 } 1481 } 1482 up_read(&p_ci->m_lock); 1483 1484 oplock_break_drain_none(&brk_list); 1485 1486 ksmbd_inode_put(p_ci); 1487 } 1488 1489 void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp) 1490 { 1491 struct oplock_info *opinfo; 1492 struct ksmbd_inode *p_ci = NULL; 1493 LIST_HEAD(brk_list); 1494 1495 rcu_read_lock(); 1496 opinfo = rcu_dereference(fp->f_opinfo); 1497 1498 if (!opinfo || !opinfo->is_lease || opinfo->o_lease->version != 2) { 1499 rcu_read_unlock(); 1500 return; 1501 } 1502 rcu_read_unlock(); 1503 1504 p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent); 1505 if (!p_ci) 1506 return; 1507 1508 down_read(&p_ci->m_lock); 1509 list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) { 1510 if (opinfo->conn == NULL || !opinfo->is_lease) 1511 continue; 1512 1513 if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE) { 1514 if (!atomic_inc_not_zero(&opinfo->refcount)) 1515 continue; 1516 1517 if (ksmbd_conn_releasing(opinfo->conn)) { 1518 opinfo_put(opinfo); 1519 continue; 1520 } 1521 1522 if (oplock_break_add(&brk_list, opinfo)) 1523 opinfo_put(opinfo); 1524 } 1525 } 1526 up_read(&p_ci->m_lock); 1527 1528 oplock_break_drain_none(&brk_list); 1529 1530 ksmbd_inode_put(p_ci); 1531 } 1532 1533 /** 1534 * smb_grant_oplock() - handle oplock/lease request on file open 1535 * @work: smb work 1536 * @req_op_level: oplock level 1537 * @pid: id of open file 1538 * @fp: ksmbd file pointer 1539 * @tid: Tree id of connection 1540 * @lctx: lease context information on file open 1541 * @share_ret: share mode 1542 * @replay: whether this is a replayed CREATE request 1543 * 1544 * Return: 0 on success, otherwise error 1545 */ 1546 int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, 1547 struct ksmbd_file *fp, __u16 tid, 1548 struct lease_ctx_info *lctx, int share_ret, bool replay) 1549 { 1550 int err = 0; 1551 int break_level = SMB2_OPLOCK_LEVEL_II; 1552 struct oplock_info *opinfo = NULL, *prev_opinfo = NULL; 1553 struct ksmbd_inode *ci = fp->f_ci; 1554 struct lease_table *new_lb = NULL; 1555 struct oplock_snapshot prev_op_snapshot; 1556 bool prev_op_has_lease; 1557 bool prev_durable_open = false; 1558 bool prev_durable_detached = false; 1559 unsigned long long prev_fid = KSMBD_NO_FID; 1560 bool new_lease = false; 1561 bool break_needed; 1562 __le32 prev_op_state = 0; 1563 1564 /* Only v2 leases handle the directory */ 1565 if (S_ISDIR(file_inode(fp->filp)->i_mode)) { 1566 if (!lctx || lctx->version != 2) 1567 return 0; 1568 } 1569 1570 opinfo = alloc_opinfo(work, pid, tid); 1571 if (!opinfo) 1572 return -ENOMEM; 1573 1574 if (lctx) { 1575 opinfo->o_lease = alloc_lease(lctx, ci); 1576 if (!opinfo->o_lease) { 1577 err = -ENOMEM; 1578 goto err_out; 1579 } 1580 opinfo->is_lease = 1; 1581 new_lease = true; 1582 } 1583 1584 /* ci does not have any oplock */ 1585 if (!opinfo_count(fp)) 1586 goto set_lev; 1587 1588 /* 1589 * A stat open that only requests metadata access must not break the 1590 * existing caching state. READ_CONTROL (reading the security 1591 * descriptor) does not conflict with a lease, but it does conflict 1592 * with an oplock, so only treat a read-control-only open as a stat 1593 * open when the existing holder is a lease. 1594 */ 1595 if (fp->cdoption != FILE_OVERWRITE_IF_LE && 1596 fp->cdoption != FILE_OVERWRITE_LE && 1597 fp->cdoption != FILE_SUPERSEDE_LE && 1598 (fp->attrib_only || 1599 (!(fp->daccess & ~(FILE_READ_ATTRIBUTES_LE | 1600 FILE_WRITE_ATTRIBUTES_LE | 1601 FILE_SYNCHRONIZE_LE | 1602 FILE_READ_CONTROL_LE)) && 1603 ksmbd_inode_has_lease(ci)))) { 1604 req_op_level = SMB2_OPLOCK_LEVEL_NONE; 1605 goto set_lev; 1606 } 1607 1608 if (lctx) { 1609 struct oplock_info *m_opinfo; 1610 1611 /* is lease already granted ? */ 1612 m_opinfo = same_client_has_lease(ci, work->conn->ClientGUID, 1613 lctx); 1614 if (m_opinfo) { 1615 lease_put(opinfo->o_lease); 1616 lease_get(m_opinfo->o_lease); 1617 opinfo->o_lease = m_opinfo->o_lease; 1618 opinfo->level = m_opinfo->level; 1619 new_lease = false; 1620 opinfo_put(m_opinfo); 1621 goto out; 1622 } 1623 } 1624 prev_opinfo = opinfo_get_list(ci, fp, &prev_op_snapshot); 1625 if (!prev_opinfo || 1626 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx)) { 1627 opinfo_put(prev_opinfo); 1628 goto set_lev; 1629 } 1630 prev_op_has_lease = prev_opinfo->is_lease; 1631 if (prev_op_has_lease) 1632 prev_op_state = prev_opinfo->o_lease->state; 1633 /* 1634 * A replay received while this open is waiting for an oplock or lease 1635 * break must not observe an intermediate level and proceed as a new 1636 * open. This check has to precede break_needed. an oplock may already 1637 * have been downgraded from Batch to II while its acknowledgement is 1638 * still pending. 1639 */ 1640 if (replay && 1641 (test_bit(0, &prev_opinfo->pending_break) || 1642 prev_opinfo->op_state == OPLOCK_ACK_WAIT)) { 1643 err = -EINPROGRESS; 1644 opinfo_put(prev_opinfo); 1645 goto err_out; 1646 } 1647 1648 if (share_ret < 0 && 1649 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1650 err = share_ret; 1651 opinfo_put(prev_opinfo); 1652 goto err_out; 1653 } 1654 1655 break_needed = prev_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 1656 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || 1657 (share_ret < 0 && prev_op_has_lease && 1658 (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)); 1659 if (!break_needed) { 1660 opinfo_put(prev_opinfo); 1661 goto op_break_not_needed; 1662 } 1663 1664 prev_durable_open = prev_op_snapshot.durable_open; 1665 prev_durable_detached = prev_op_snapshot.durable_detached; 1666 prev_fid = prev_op_snapshot.fid; 1667 1668 err = oplock_break(prev_opinfo, break_level, work, 1669 share_ret < 0 && prev_opinfo->is_lease, false); 1670 if (prev_durable_detached || (prev_durable_open && err == -ENOENT)) 1671 ksmbd_invalidate_durable_fd(prev_fid); 1672 opinfo_put(prev_opinfo); 1673 if (err == -EAGAIN) { 1674 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); 1675 if (share_ret < 0) { 1676 err = share_ret; 1677 goto err_out; 1678 } 1679 goto set_lev; 1680 } 1681 if (err == -ENOENT) { 1682 /* 1683 * A pending durable CREATE can lose the previous oplock when 1684 * its holder closes the file. In that case grant the original 1685 * request its full caching state. Other opens still need the 1686 * normal shared-open downgrade below. 1687 */ 1688 if (!prev_durable_open && 1689 req_op_level != SMB2_OPLOCK_LEVEL_NONE) 1690 req_op_level = SMB2_OPLOCK_LEVEL_II; 1691 goto set_lev; 1692 } 1693 /* Check all oplock was freed by close */ 1694 else if (err < 0) 1695 goto err_out; 1696 1697 op_break_not_needed: 1698 if (share_ret < 0) { 1699 err = share_ret; 1700 goto err_out; 1701 } 1702 1703 if (req_op_level != SMB2_OPLOCK_LEVEL_NONE) 1704 req_op_level = SMB2_OPLOCK_LEVEL_II; 1705 1706 /* grant fixed oplock on stacked locking between lease and oplock */ 1707 if (prev_op_has_lease && !lctx) 1708 if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE) 1709 req_op_level = SMB2_OPLOCK_LEVEL_NONE; 1710 1711 if (!prev_op_has_lease && lctx) { 1712 req_op_level = SMB2_OPLOCK_LEVEL_II; 1713 lctx->req_state = SMB2_LEASE_READ_CACHING_LE; 1714 } 1715 1716 set_lev: 1717 set_oplock_level(opinfo, req_op_level, lctx); 1718 1719 out: 1720 /* 1721 * Keep the original publication order so concurrent opens can 1722 * still observe the in-flight grant via ci->m_op_list, but make 1723 * everything after opinfo_add() no-fail by preallocating any new 1724 * lease_table first. 1725 */ 1726 opinfo->o_fp = fp; 1727 if (new_lease) { 1728 new_lb = alloc_lease_table(opinfo); 1729 if (!new_lb) { 1730 err = -ENOMEM; 1731 goto err_out; 1732 } 1733 } 1734 1735 opinfo_count_inc(fp); 1736 opinfo_add(opinfo, fp); 1737 1738 if (new_lease) 1739 add_lease_global_list(opinfo->o_lease, opinfo->conn, new_lb); 1740 if (opinfo->is_lease) 1741 lease_add_open(opinfo->o_lease, opinfo); 1742 1743 rcu_assign_pointer(fp->f_opinfo, opinfo); 1744 1745 return 0; 1746 err_out: 1747 kfree(new_lb); 1748 opinfo_put(opinfo); 1749 return err; 1750 } 1751 1752 /** 1753 * smb_break_all_write_oplock() - break batch/exclusive oplock to level2 1754 * @work: smb work 1755 * @fp: ksmbd file pointer 1756 * @is_trunc: truncate on open 1757 */ 1758 static bool smb_break_all_write_oplock(struct ksmbd_work *work, 1759 struct ksmbd_file *fp, int is_trunc) 1760 { 1761 struct oplock_info *brk_opinfo; 1762 bool sent_break = false; 1763 1764 brk_opinfo = opinfo_get_list(fp->f_ci, NULL, NULL); 1765 if (!brk_opinfo) 1766 return false; 1767 if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH && 1768 brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1769 opinfo_put(brk_opinfo); 1770 return false; 1771 } 1772 1773 brk_opinfo->open_trunc = is_trunc; 1774 oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II, work, false, false); 1775 sent_break = true; 1776 opinfo_put(brk_opinfo); 1777 1778 return sent_break; 1779 } 1780 1781 /** 1782 * __smb_break_all_levII_oplock() - send level2 oplock or read lease break command 1783 * from server to client 1784 * @work: smb work 1785 * @fp: ksmbd file pointer 1786 * @is_trunc: truncate on open 1787 * @send_interim: send interim response to the client 1788 * @send_oplock_break: send oplock break notification to the client 1789 * @sync_lease_break: send the lease break notification synchronously 1790 */ 1791 static void __smb_break_all_levII_oplock(struct ksmbd_work *work, 1792 struct ksmbd_file *fp, int is_trunc, 1793 bool send_interim, bool send_oplock_break, 1794 bool sync_lease_break) 1795 { 1796 struct oplock_info *op, *brk_op; 1797 struct oplock_break_entry *ent, *tmp; 1798 struct ksmbd_inode *ci; 1799 struct ksmbd_conn *conn = work->conn; 1800 bool sent_interim = false; 1801 LIST_HEAD(brk_list); 1802 1803 if (!test_share_config_flag(work->tcon->share_conf, 1804 KSMBD_SHARE_FLAG_OPLOCKS)) 1805 return; 1806 1807 ci = fp->f_ci; 1808 op = opinfo_get(fp); 1809 1810 down_read(&ci->m_lock); 1811 list_for_each_entry(brk_op, &ci->m_op_list, op_entry) { 1812 if (brk_op->conn == NULL) 1813 continue; 1814 1815 if (!atomic_inc_not_zero(&brk_op->refcount)) 1816 continue; 1817 1818 if (ksmbd_conn_releasing(brk_op->conn)) { 1819 opinfo_put(brk_op); 1820 continue; 1821 } 1822 1823 if (!brk_op->is_lease && 1824 brk_op->level != SMB2_OPLOCK_LEVEL_II) { 1825 ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n", 1826 brk_op->level); 1827 goto next; 1828 } 1829 1830 /* Skip oplock being break to none */ 1831 if (brk_op->is_lease && 1832 brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE && 1833 atomic_read(&brk_op->breaking_cnt)) 1834 goto next; 1835 1836 if (op && op->is_lease && brk_op->is_lease && 1837 !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID, 1838 SMB2_CLIENT_GUID_SIZE) && 1839 !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key, 1840 SMB2_LEASE_KEY_SIZE)) 1841 goto next; 1842 brk_op->open_trunc = is_trunc; 1843 1844 /* 1845 * Defer the break until ci->m_lock is released: oplock_break() 1846 * may block waiting for the lease break acknowledgment, and the 1847 * close that wakes that wait needs ci->m_lock for write. 1848 */ 1849 if (!oplock_break_add(&brk_list, brk_op)) 1850 continue; 1851 next: 1852 opinfo_put(brk_op); 1853 } 1854 up_read(&ci->m_lock); 1855 1856 list_for_each_entry_safe(ent, tmp, &brk_list, list) { 1857 brk_op = ent->opinfo; 1858 1859 if (!brk_op->is_lease && !send_oplock_break) { 1860 brk_op->level = SMB2_OPLOCK_LEVEL_NONE; 1861 spin_lock(&brk_op->state_lock); 1862 if (brk_op->op_state != OPLOCK_CLOSING) 1863 brk_op->op_state = OPLOCK_STATE_NONE; 1864 spin_unlock(&brk_op->state_lock); 1865 } else { 1866 oplock_break(brk_op, 1867 brk_op->is_lease && !is_trunc ? 1868 SMB2_OPLOCK_LEVEL_II : SMB2_OPLOCK_LEVEL_NONE, 1869 send_interim && !sent_interim ? work : NULL, 1870 false, sync_lease_break); 1871 } 1872 sent_interim = true; 1873 list_del(&ent->list); 1874 opinfo_put(brk_op); 1875 kfree(ent); 1876 } 1877 1878 if (op) 1879 opinfo_put(op); 1880 } 1881 1882 void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp, 1883 int is_trunc) 1884 { 1885 __smb_break_all_levII_oplock(work, fp, is_trunc, true, true, false); 1886 } 1887 1888 void smb_break_all_levII_oplock_rename(struct ksmbd_work *work, struct ksmbd_file *fp) 1889 { 1890 __smb_break_all_levII_oplock(work, fp, 0, true, true, true); 1891 } 1892 1893 void smb_break_all_levII_oplock_no_interim(struct ksmbd_work *work, 1894 struct ksmbd_file *fp, int is_trunc) 1895 { 1896 __smb_break_all_levII_oplock(work, fp, is_trunc, false, true, false); 1897 } 1898 1899 void smb_break_all_levII_oplock_for_delete(struct ksmbd_work *work, 1900 struct ksmbd_file *fp) 1901 { 1902 __smb_break_all_levII_oplock(work, fp, 0, false, false, false); 1903 } 1904 1905 /** 1906 * smb_break_all_oplock() - break both batch/exclusive and level2 oplock 1907 * @work: smb work 1908 * @fp: ksmbd file pointer 1909 */ 1910 void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp) 1911 { 1912 bool sent_break; 1913 1914 if (!test_share_config_flag(work->tcon->share_conf, 1915 KSMBD_SHARE_FLAG_OPLOCKS)) 1916 return; 1917 1918 sent_break = smb_break_all_write_oplock(work, fp, 1); 1919 __smb_break_all_levII_oplock(work, fp, 1, !sent_break, true, false); 1920 } 1921 1922 /** 1923 * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type 1924 * @lease_state: lease type 1925 * 1926 * Return: 0 if no mapping, otherwise corresponding oplock type 1927 */ 1928 __u8 smb2_map_lease_to_oplock(__le32 lease_state) 1929 { 1930 if ((lease_state & SMB2_LEASE_WRITE_CACHING_LE) && 1931 (lease_state & SMB2_LEASE_HANDLE_CACHING_LE)) { 1932 return SMB2_OPLOCK_LEVEL_BATCH; 1933 } else if (lease_state & SMB2_LEASE_WRITE_CACHING_LE) { 1934 return SMB2_OPLOCK_LEVEL_EXCLUSIVE; 1935 } else if (lease_state & (SMB2_LEASE_READ_CACHING_LE | 1936 SMB2_LEASE_HANDLE_CACHING_LE)) { 1937 return SMB2_OPLOCK_LEVEL_II; 1938 } 1939 return 0; 1940 } 1941 1942 /** 1943 * create_lease_buf() - create lease context for open cmd response 1944 * @rbuf: buffer to create lease context response 1945 * @lease: buffer to stored parsed lease state information 1946 */ 1947 void create_lease_buf(u8 *rbuf, struct lease *lease) 1948 { 1949 if (lease->version == 2) { 1950 struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf; 1951 __le32 flags = 0; 1952 1953 memset(buf, 0, sizeof(struct create_lease_v2)); 1954 memcpy(buf->lcontext.LeaseKey, lease->lease_key, 1955 SMB2_LEASE_KEY_SIZE); 1956 if (lease_has_parent_key(lease)) 1957 flags |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; 1958 if (lease_break_in_progress(lease)) 1959 flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE; 1960 buf->lcontext.LeaseFlags = flags; 1961 buf->lcontext.Epoch = cpu_to_le16(lease->epoch); 1962 buf->lcontext.LeaseState = lease->state; 1963 if (lease_has_parent_key(lease)) 1964 memcpy(buf->lcontext.ParentLeaseKey, lease->parent_lease_key, 1965 SMB2_LEASE_KEY_SIZE); 1966 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1967 (struct create_lease_v2, lcontext)); 1968 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2)); 1969 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1970 (struct create_lease_v2, Name)); 1971 buf->ccontext.NameLength = cpu_to_le16(4); 1972 buf->Name[0] = 'R'; 1973 buf->Name[1] = 'q'; 1974 buf->Name[2] = 'L'; 1975 buf->Name[3] = 's'; 1976 } else { 1977 struct create_lease *buf = (struct create_lease *)rbuf; 1978 1979 memset(buf, 0, sizeof(struct create_lease)); 1980 memcpy(buf->lcontext.LeaseKey, lease->lease_key, SMB2_LEASE_KEY_SIZE); 1981 if (lease_break_in_progress(lease)) 1982 buf->lcontext.LeaseFlags = 1983 SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE; 1984 buf->lcontext.LeaseState = lease->state; 1985 buf->ccontext.DataOffset = cpu_to_le16(offsetof 1986 (struct create_lease, lcontext)); 1987 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context)); 1988 buf->ccontext.NameOffset = cpu_to_le16(offsetof 1989 (struct create_lease, Name)); 1990 buf->ccontext.NameLength = cpu_to_le16(4); 1991 buf->Name[0] = 'R'; 1992 buf->Name[1] = 'q'; 1993 buf->Name[2] = 'L'; 1994 buf->Name[3] = 's'; 1995 } 1996 } 1997 1998 /** 1999 * parse_lease_state() - parse lease context contained in file open request 2000 * @open_req: buffer containing smb2 file open(create) request 2001 * 2002 * Return: allocated lease context object on success, otherwise NULL 2003 */ 2004 struct lease_ctx_info *parse_lease_state(void *open_req) 2005 { 2006 struct create_context *cc; 2007 struct smb2_create_req *req = (struct smb2_create_req *)open_req; 2008 struct lease_ctx_info *lreq; 2009 2010 cc = smb2_find_context_vals(req, SMB2_CREATE_REQUEST_LEASE, 4); 2011 if (IS_ERR(cc)) 2012 return ERR_CAST(cc); 2013 if (!cc) 2014 return NULL; 2015 2016 lreq = kzalloc_obj(struct lease_ctx_info, KSMBD_DEFAULT_GFP); 2017 if (!lreq) 2018 return ERR_PTR(-ENOMEM); 2019 2020 if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) { 2021 struct create_lease_v2 *lc = (struct create_lease_v2 *)cc; 2022 2023 if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) < 2024 sizeof(struct create_lease_v2) - 4) 2025 goto err_out; 2026 2027 memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 2028 lreq->req_state = lc->lcontext.LeaseState; 2029 lreq->flags = lc->lcontext.LeaseFlags; 2030 lreq->epoch = lc->lcontext.Epoch; 2031 lreq->duration = lc->lcontext.LeaseDuration; 2032 if (!lease_state_valid(lreq->req_state) || 2033 !lease_v2_flags_valid(lreq->flags)) 2034 goto err_out; 2035 lreq->req_state = lease_state_grantable(lreq->req_state); 2036 if (lreq->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) 2037 memcpy(lreq->parent_lease_key, lc->lcontext.ParentLeaseKey, 2038 SMB2_LEASE_KEY_SIZE); 2039 lreq->version = 2; 2040 } else if (sizeof(struct lease_context) == le32_to_cpu(cc->DataLength)) { 2041 struct create_lease *lc = (struct create_lease *)cc; 2042 2043 if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) < 2044 sizeof(struct create_lease)) 2045 goto err_out; 2046 2047 memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 2048 lreq->req_state = lc->lcontext.LeaseState; 2049 lreq->flags = 0; 2050 lreq->duration = lc->lcontext.LeaseDuration; 2051 if (!lease_state_valid(lreq->req_state)) 2052 goto err_out; 2053 lreq->req_state = lease_state_grantable(lreq->req_state); 2054 lreq->version = 1; 2055 } else 2056 goto err_out; 2057 return lreq; 2058 err_out: 2059 kfree(lreq); 2060 return ERR_PTR(-EINVAL); 2061 } 2062 2063 /** 2064 * smb2_find_context_vals() - find a particular context info in open request 2065 * @open_req: buffer containing smb2 file open(create) request 2066 * @tag: context name to search for 2067 * @tag_len: the length of tag 2068 * 2069 * Return: pointer to requested context, NULL if @str context not found 2070 * or error pointer if name length is invalid. 2071 */ 2072 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len) 2073 { 2074 struct create_context *cc; 2075 unsigned int next = 0; 2076 char *name; 2077 struct smb2_create_req *req = (struct smb2_create_req *)open_req; 2078 unsigned int remain_len, name_off, name_len, value_off, value_len, 2079 cc_len; 2080 2081 /* 2082 * CreateContextsOffset and CreateContextsLength are guaranteed to 2083 * be valid because of ksmbd_smb2_check_message(). 2084 */ 2085 if (!req->CreateContextsOffset || !req->CreateContextsLength) 2086 return NULL; 2087 2088 cc = (struct create_context *)((char *)req + 2089 le32_to_cpu(req->CreateContextsOffset)); 2090 remain_len = le32_to_cpu(req->CreateContextsLength); 2091 do { 2092 cc = (struct create_context *)((char *)cc + next); 2093 if (remain_len < offsetof(struct create_context, Buffer)) 2094 return ERR_PTR(-EINVAL); 2095 2096 next = le32_to_cpu(cc->Next); 2097 name_off = le16_to_cpu(cc->NameOffset); 2098 name_len = le16_to_cpu(cc->NameLength); 2099 value_off = le16_to_cpu(cc->DataOffset); 2100 value_len = le32_to_cpu(cc->DataLength); 2101 cc_len = next ? next : remain_len; 2102 2103 if ((next & 0x7) != 0 || 2104 next > remain_len || 2105 name_off != offsetof(struct create_context, Buffer) || 2106 name_len < 4 || 2107 name_off + name_len > cc_len || 2108 (value_off & 0x7) != 0 || 2109 (value_len && value_off < name_off + (name_len < 8 ? 8 : name_len)) || 2110 ((u64)value_off + value_len > cc_len)) 2111 return ERR_PTR(-EINVAL); 2112 2113 name = (char *)cc + name_off; 2114 if (name_len == tag_len && !memcmp(name, tag, name_len)) 2115 return cc; 2116 2117 remain_len -= next; 2118 } while (next != 0); 2119 2120 return NULL; 2121 } 2122 2123 /** 2124 * create_durable_rsp_buf() - create durable handle context 2125 * @cc: buffer to create durable context response 2126 */ 2127 void create_durable_rsp_buf(char *cc) 2128 { 2129 struct create_durable_rsp *buf; 2130 2131 buf = (struct create_durable_rsp *)cc; 2132 memset(buf, 0, sizeof(struct create_durable_rsp)); 2133 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2134 (struct create_durable_rsp, Data)); 2135 buf->ccontext.DataLength = cpu_to_le32(8); 2136 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2137 (struct create_durable_rsp, Name)); 2138 buf->ccontext.NameLength = cpu_to_le16(4); 2139 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */ 2140 buf->Name[0] = 'D'; 2141 buf->Name[1] = 'H'; 2142 buf->Name[2] = 'n'; 2143 buf->Name[3] = 'Q'; 2144 } 2145 2146 /** 2147 * create_durable_v2_rsp_buf() - create durable handle v2 context 2148 * @cc: buffer to create durable context response 2149 * @fp: ksmbd file pointer 2150 */ 2151 void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp) 2152 { 2153 struct create_durable_rsp_v2 *buf; 2154 2155 buf = (struct create_durable_rsp_v2 *)cc; 2156 memset(buf, 0, sizeof(*buf)); 2157 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2158 (struct create_durable_rsp_v2, dcontext)); 2159 buf->ccontext.DataLength = cpu_to_le32(8); 2160 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2161 (struct create_durable_rsp_v2, Name)); 2162 buf->ccontext.NameLength = cpu_to_le16(4); 2163 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */ 2164 buf->Name[0] = 'D'; 2165 buf->Name[1] = 'H'; 2166 buf->Name[2] = '2'; 2167 buf->Name[3] = 'Q'; 2168 2169 buf->dcontext.Timeout = cpu_to_le32(fp->durable_timeout); 2170 if (fp->is_persistent) 2171 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); 2172 } 2173 2174 /** 2175 * create_mxac_rsp_buf() - create query maximal access context 2176 * @cc: buffer to create maximal access context response 2177 * @maximal_access: maximal access 2178 */ 2179 void create_mxac_rsp_buf(char *cc, int maximal_access) 2180 { 2181 struct create_mxac_rsp *buf; 2182 2183 buf = (struct create_mxac_rsp *)cc; 2184 memset(buf, 0, sizeof(struct create_mxac_rsp)); 2185 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2186 (struct create_mxac_rsp, QueryStatus)); 2187 buf->ccontext.DataLength = cpu_to_le32(8); 2188 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2189 (struct create_mxac_rsp, Name)); 2190 buf->ccontext.NameLength = cpu_to_le16(4); 2191 /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */ 2192 buf->Name[0] = 'M'; 2193 buf->Name[1] = 'x'; 2194 buf->Name[2] = 'A'; 2195 buf->Name[3] = 'c'; 2196 2197 buf->QueryStatus = STATUS_SUCCESS; 2198 buf->MaximalAccess = cpu_to_le32(maximal_access); 2199 } 2200 2201 void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id) 2202 { 2203 struct create_disk_id_rsp *buf; 2204 2205 buf = (struct create_disk_id_rsp *)cc; 2206 memset(buf, 0, sizeof(struct create_disk_id_rsp)); 2207 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2208 (struct create_disk_id_rsp, DiskFileId)); 2209 buf->ccontext.DataLength = cpu_to_le32(32); 2210 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2211 (struct create_mxac_rsp, Name)); 2212 buf->ccontext.NameLength = cpu_to_le16(4); 2213 /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */ 2214 buf->Name[0] = 'Q'; 2215 buf->Name[1] = 'F'; 2216 buf->Name[2] = 'i'; 2217 buf->Name[3] = 'd'; 2218 2219 buf->DiskFileId = cpu_to_le64(file_id); 2220 buf->VolumeId = cpu_to_le64(vol_id); 2221 } 2222 2223 /** 2224 * create_posix_rsp_buf() - create posix extension context 2225 * @cc: buffer to create posix on posix response 2226 * @fp: ksmbd file pointer 2227 */ 2228 void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp) 2229 { 2230 struct create_posix_rsp *buf; 2231 struct inode *inode = file_inode(fp->filp); 2232 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 2233 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 2234 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 2235 2236 buf = (struct create_posix_rsp *)cc; 2237 memset(buf, 0, sizeof(struct create_posix_rsp)); 2238 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2239 (struct create_posix_rsp, nlink)); 2240 /* 2241 * DataLength = nlink(4) + reparse_tag(4) + mode(4) + 2242 * domain sid(28) + unix group sid(16). 2243 */ 2244 buf->ccontext.DataLength = cpu_to_le32(56); 2245 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2246 (struct create_posix_rsp, Name)); 2247 buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); 2248 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ 2249 buf->Name[0] = 0x93; 2250 buf->Name[1] = 0xAD; 2251 buf->Name[2] = 0x25; 2252 buf->Name[3] = 0x50; 2253 buf->Name[4] = 0x9C; 2254 buf->Name[5] = 0xB4; 2255 buf->Name[6] = 0x11; 2256 buf->Name[7] = 0xE7; 2257 buf->Name[8] = 0xB4; 2258 buf->Name[9] = 0x23; 2259 buf->Name[10] = 0x83; 2260 buf->Name[11] = 0xDE; 2261 buf->Name[12] = 0x96; 2262 buf->Name[13] = 0x8B; 2263 buf->Name[14] = 0xCD; 2264 buf->Name[15] = 0x7C; 2265 2266 buf->nlink = cpu_to_le32(inode->i_nlink); 2267 buf->reparse_tag = cpu_to_le32(fp->volatile_id); 2268 buf->mode = cpu_to_le32(inode->i_mode & 0777); 2269 /* 2270 * SidBuffer(44) contain two sids(Domain sid(28), UNIX group sid(16)). 2271 * Domain sid(28) = revision(1) + num_subauth(1) + authority(6) + 2272 * sub_auth(4 * 4(num_subauth)) + RID(4). 2273 * UNIX group id(16) = revision(1) + num_subauth(1) + authority(6) + 2274 * sub_auth(4 * 1(num_subauth)) + RID(4). 2275 */ 2276 id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)), 2277 SIDOWNER, (struct smb_sid *)&buf->SidBuffer[0]); 2278 id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)), 2279 SIDUNIX_GROUP, (struct smb_sid *)&buf->SidBuffer[28]); 2280 } 2281 2282 /** 2283 * create_aapl_rsp_buf() - build AAPL kAAPL_SERVER_QUERY response 2284 * @cc: buffer to write the create context into (AAPL_RSP_MAX_SIZE bytes) 2285 * @vol_caps: volume capability flags (SMB2_CRTCTX_AAPL_* volume bits) 2286 * @req_bitmap: the client's request bitmap, echoed back in reply_bitmap 2287 * 2288 * Response format follows the layout observed from macOS's own smbd, and 2289 * matches the client-side parsing in AAPL's published public client kernel 2290 * source (public client behavior reference, kAAPL_SERVER_QUERY 2291 * case): reply_bitmap, then server_caps/vol_caps/model-info fields present 2292 * only when their reply_bitmap bit is set: 2293 * reply_bitmap = req_bitmap masked to the fields we support 2294 * server_caps = AAPL_SERVER_CAPS_KSMBD when requested 2295 * vol_caps = caller-supplied 2296 * model string = server_conf.aapl_model (default "Xserve") in UTF-16LE, 2297 * when SMB2_CRTCTX_AAPL_MODEL_INFO requested 2298 * 2299 * Sending reply_bitmap with MODEL_INFO set but no model string causes 2300 * smbfs.kext to enter a broken disconnect path requiring a macOS reboot. 2301 * @readdir_attr_v2: advertise SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2 2302 * instead of the V1 bit 2303 */ 2304 void create_aapl_rsp_buf(char *cc, __u64 vol_caps, __u64 req_bitmap, 2305 bool readdir_attr_v2) 2306 { 2307 struct create_aapl_rsp *buf; 2308 u64 reply_bitmap; 2309 u64 server_caps; 2310 u32 data_len; 2311 2312 buf = (struct create_aapl_rsp *)cc; 2313 memset(buf, 0, AAPL_RSP_MAX_SIZE); 2314 2315 reply_bitmap = req_bitmap & (SMB2_CRTCTX_AAPL_SERVER_CAPS | 2316 SMB2_CRTCTX_AAPL_VOLUME_CAPS | 2317 SMB2_CRTCTX_AAPL_MODEL_INFO); 2318 2319 /* base data: cmd(4)+reserved(4)+reply_bitmap(8)+server_caps(8)+vol_caps(8) */ 2320 data_len = 32; 2321 if (reply_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) 2322 data_len += 4 + 4 + AAPL_MODEL_UTF16_BYTES; /* pad2+model_bytes+string */ 2323 2324 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct create_aapl_rsp, cmd)); 2325 buf->ccontext.DataLength = cpu_to_le32(data_len); 2326 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct create_aapl_rsp, Name)); 2327 buf->ccontext.NameLength = cpu_to_le16(SMB2_CREATE_AAPL_LEN); 2328 buf->Name[0] = 'A'; 2329 buf->Name[1] = 'A'; 2330 buf->Name[2] = 'P'; 2331 buf->Name[3] = 'L'; 2332 2333 buf->cmd = cpu_to_le32(SMB2_CRTCTX_AAPL_SERVER_QUERY); 2334 buf->reply_bitmap = cpu_to_le64(reply_bitmap); 2335 server_caps = AAPL_SERVER_CAPS_KSMBD; 2336 if (readdir_attr_v2) 2337 server_caps = (server_caps & ~SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) | 2338 SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2; 2339 buf->server_caps = (reply_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) ? 2340 cpu_to_le64(server_caps) : 0; 2341 buf->vol_caps = (reply_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) ? 2342 cpu_to_le64(vol_caps) : 0; 2343 2344 if (reply_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) { 2345 __le32 *p = (__le32 *)((u8 *)buf + sizeof(*buf)); 2346 __le16 *model_str = (__le16 *)(p + 2); 2347 const char *src = server_conf.aapl_model[0] ? 2348 server_conf.aapl_model : "Xserve"; 2349 int i, model_bytes = 0; 2350 2351 /* Convert ASCII model string to UTF-16LE in-place */ 2352 for (i = 0; src[i] && i < AAPL_MODEL_MAX_CHARS; i++) { 2353 model_str[i] = cpu_to_le16((unsigned char)src[i]); 2354 model_bytes += 2; 2355 } 2356 2357 p[0] = 0; /* pad2 */ 2358 p[1] = cpu_to_le32(model_bytes); 2359 2360 /* Update DataLength to reflect actual model string size */ 2361 buf->ccontext.DataLength = 2362 cpu_to_le32(data_len - AAPL_MODEL_UTF16_BYTES + model_bytes); 2363 } 2364 } 2365 2366 /* 2367 * Find lease object(opinfo) for given lease key/fid from lease 2368 * break/file close path. 2369 */ 2370 /** 2371 * lookup_lease_in_table() - find a matching lease info object 2372 * @conn: connection instance 2373 * @lease_key: lease key to be searched for 2374 * 2375 * Return: opinfo if found matching opinfo, otherwise NULL 2376 */ 2377 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn, 2378 char *lease_key) 2379 { 2380 struct oplock_info *opinfo = NULL, *ret_op = NULL; 2381 struct lease *lease; 2382 struct lease_table *lt; 2383 2384 read_lock(&lease_list_lock); 2385 list_for_each_entry(lt, &lease_table_list, l_entry) { 2386 if (!memcmp(lt->client_guid, conn->ClientGUID, 2387 SMB2_CLIENT_GUID_SIZE)) 2388 goto found; 2389 } 2390 2391 read_unlock(&lease_list_lock); 2392 return NULL; 2393 2394 found: 2395 list_for_each_entry(lease, <->lease_list, l_entry) { 2396 if (memcmp(lease->lease_key, lease_key, SMB2_LEASE_KEY_SIZE)) 2397 continue; 2398 if (!(lease->state & (SMB2_LEASE_HANDLE_CACHING_LE | 2399 SMB2_LEASE_WRITE_CACHING_LE))) 2400 break; 2401 2402 spin_lock(&lease->lock); 2403 list_for_each_entry(opinfo, &lease->open_list, lease_entry) { 2404 if (!opinfo->op_state || 2405 opinfo->op_state == OPLOCK_CLOSING) 2406 continue; 2407 if (!atomic_inc_not_zero(&opinfo->refcount)) 2408 continue; 2409 ret_op = opinfo; 2410 } 2411 spin_unlock(&lease->lock); 2412 if (ret_op) { 2413 ksmbd_debug(OPLOCK, "found opinfo\n"); 2414 goto out; 2415 } 2416 break; 2417 } 2418 2419 out: 2420 read_unlock(&lease_list_lock); 2421 return ret_op; 2422 } 2423 2424 int smb2_check_durable_oplock(struct ksmbd_conn *conn, 2425 struct ksmbd_share_config *share, 2426 struct ksmbd_file *fp, 2427 struct lease_ctx_info *lctx, 2428 struct ksmbd_user *user, 2429 char *name) 2430 { 2431 struct oplock_info *opinfo = opinfo_get(fp); 2432 int ret = 0; 2433 2434 if (!opinfo) 2435 return 0; 2436 2437 if (ksmbd_has_other_active_fd(fp)) { 2438 ksmbd_debug(SMB, "Durable handle reconnect failed: competing open\n"); 2439 ret = -EBADF; 2440 goto out; 2441 } 2442 2443 if (ksmbd_vfs_compare_durable_owner(fp, user) == false) { 2444 ksmbd_debug(SMB, "Durable handle reconnect failed: owner mismatch\n"); 2445 ret = -EBADF; 2446 goto out; 2447 } 2448 2449 if (opinfo->is_lease == false) { 2450 if (lctx) { 2451 pr_err("create context include lease\n"); 2452 ret = -EBADF; 2453 goto out; 2454 } 2455 2456 if (opinfo->level != SMB2_OPLOCK_LEVEL_BATCH) { 2457 pr_err("oplock level is not equal to SMB2_OPLOCK_LEVEL_BATCH\n"); 2458 ret = -EBADF; 2459 } 2460 2461 goto out; 2462 } 2463 2464 if (memcmp(conn->ClientGUID, fp->client_guid, 2465 SMB2_CLIENT_GUID_SIZE)) { 2466 ksmbd_debug(SMB, "Client guid of fp is not equal to the one of connection\n"); 2467 ret = -EBADF; 2468 goto out; 2469 } 2470 2471 if (!lctx) { 2472 ksmbd_debug(SMB, "create context does not include lease\n"); 2473 ret = -EBADF; 2474 goto out; 2475 } 2476 2477 if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key, 2478 SMB2_LEASE_KEY_SIZE)) { 2479 ksmbd_debug(SMB, 2480 "lease key of fp does not match lease key in create context\n"); 2481 ret = -EBADF; 2482 goto out; 2483 } 2484 2485 if (!(opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)) { 2486 ksmbd_debug(SMB, "lease state does not contain SMB2_LEASE_HANDLE_CACHING\n"); 2487 ret = -EBADF; 2488 goto out; 2489 } 2490 2491 if (opinfo->o_lease->version != lctx->version) { 2492 ksmbd_debug(SMB, 2493 "lease version of fp does not match the one in create context\n"); 2494 ret = -EBADF; 2495 goto out; 2496 } 2497 2498 if (!ksmbd_inode_pending_delete(fp)) 2499 ret = ksmbd_validate_name_reconnect(share, fp, name); 2500 out: 2501 opinfo_put(opinfo); 2502 return ret; 2503 } 2504