1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2011-2020 Tintri by DDN, Inc. All rights reserved. 24 * Copyright 2016 Syneto S.R.L. All rights reserved. 25 * Copyright (c) 2016 by Delphix. All rights reserved. 26 * Copyright 2021 RackTop Systems, Inc. 27 */ 28 29 /* 30 * General Structures Layout 31 * ------------------------- 32 * 33 * This is a simplified diagram showing the relationship between most of the 34 * main structures. 35 * 36 * +-------------------+ 37 * | SMB_INFO | 38 * +-------------------+ 39 * | 40 * | 41 * v 42 * +-------------------+ +-------------------+ +-------------------+ 43 * | SESSION |<----->| SESSION |......| SESSION | 44 * +-------------------+ +-------------------+ +-------------------+ 45 * | | 46 * | | 47 * | v 48 * | +-------------------+ +-------------------+ +-------------------+ 49 * | | USER |<--->| USER |...| USER | 50 * | +-------------------+ +-------------------+ +-------------------+ 51 * | 52 * | 53 * v 54 * +-------------------+ +-------------------+ +-------------------+ 55 * | TREE |<----->| TREE |......| TREE | 56 * +-------------------+ +-------------------+ +-------------------+ 57 * | | 58 * | | 59 * | v 60 * | +-------+ +-------+ +-------+ 61 * | | OFILE |<----->| OFILE |......| OFILE | 62 * | +-------+ +-------+ +-------+ 63 * | 64 * | 65 * v 66 * +-------+ +------+ +------+ 67 * | ODIR |<----->| ODIR |......| ODIR | 68 * +-------+ +------+ +------+ 69 * 70 * 71 * Ofile State Machine 72 * ------------------ 73 * 74 * +-------------------------+ T0 75 * | SMB_OFILE_STATE_OPEN |<--+-------- Creation/Allocation 76 * +-------------------------+ | 77 * | | | T5 78 * | | +---------------------------+ 79 * | | | SMB_OFILE_STATE_RECONNECT | 80 * | | +---------------------------+ 81 * | | ^ 82 * | v | 83 * | +---------------+ | 84 * | | STATE_SAVE_DH | | 85 * | | STATE_SAVING | | 86 * | +---------------+ | 87 * | | | T4 88 * | T1 | T3 +--------------------------+ 89 * | +------>| SMB_OFILE_STATE_ORPHANED | 90 * v +--------------------------+ 91 * +-------------------------+ | | 92 * | SMB_OFILE_STATE_CLOSING |<--+ T6 | T7 93 * +-------------------------+ | 94 * | ^ v 95 * | T2 | T8 +-------------------------+ 96 * | +-------| SMB_OFILE_STATE_EXPIRED | 97 * v +-------------------------+ 98 * +-------------------------+ 99 * | SMB_OFILE_STATE_CLOSED |----------> Deletion/Free 100 * +-------------------------+ T9 101 * 102 * SMB_OFILE_STATE_OPEN 103 * 104 * While in this state: 105 * - The ofile is queued in the list of ofiles of its tree. 106 * - References will be given out if the ofile is looked up. 107 * 108 * SMB_OFILE_STATE_SAVE_DH 109 * 110 * Similar to state _CLOSING, but instead of deleting the ofile, 111 * it leaves the ofile in state _ORPHANED (for later reclaim). 112 * Will move to _SAVING after last ref, then _ORPHANED. 113 * 114 * While in this state: 115 * - The ofile has been marked for preservation during a 116 * walk of the tree ofile list to close multiple files. 117 * - References will not be given out if the ofile is looked up, 118 * except for oplock break processing. 119 * - Still affects Sharing Violation rules 120 * 121 * SMB_OFILE_STATE_SAVING 122 * 123 * Transient state used to keep oplock break processing out 124 * while the ofile moves to state _ORPHANED. 125 * 126 * While in this state: 127 * - References will not be given out if the ofile is looked up, 128 * except for oplock break processing. 129 * - Still affects Sharing Violation rules 130 * 131 * SMB_OFILE_STATE_CLOSING 132 * 133 * Close has been requested. Stay in this state until the last 134 * ref. is gone, then move to state _CLOSED 135 * 136 * While in this state: 137 * - The ofile is queued in the list of ofiles of its tree. 138 * - References will not be given out if the ofile is looked up. 139 * - The file is closed and the locks held are being released. 140 * - The resources associated with the ofile remain. 141 * 142 * SMB_OFILE_STATE_CLOSED 143 * 144 * While in this state: 145 * - The ofile is queued in the list of ofiles of its tree. 146 * - References will not be given out if the ofile is looked up. 147 * - The resources associated with the ofile remain. 148 * 149 * SMB_OFILE_STATE_ORPHANED 150 * 151 * While in this state: 152 * - The ofile is queued in the list of ofiles of its tree. 153 * - Can be reclaimed by the original owner 154 * - References will not be given out if the ofile is looked up. 155 * - All the tree, user, and session "up" pointers are NULL! 156 * - Will eventually be "expired" if not reclaimed 157 * - Can be closed if its oplock is broken 158 * - Still affects Sharing Violation rules 159 * 160 * SMB_OFILE_STATE_EXPIRED 161 * 162 * While in this state: 163 * - The ofile is queued in the list of ofiles of its tree. 164 * - References will not be given out if the ofile is looked up. 165 * - The ofile has not been reclaimed and will soon be closed, 166 * due to, for example, the durable handle timer expiring, or its 167 * oplock being broken. 168 * - Cannot be reclaimed at this point 169 * 170 * SMB_OFILE_STATE_RECONNECT 171 * 172 * Transient state used to keep oplock break processing out 173 * while the ofile moves from state _ORPHANED to _OPEN. 174 * 175 * While in this state: 176 * - The ofile is being reclaimed; do not touch it. 177 * - References will not be given out if the ofile is looked up. 178 * - Still affects Sharing Violation rules 179 * - see smb2_dh_reconnect() for which members need to be avoided 180 * 181 * Transition T0 182 * 183 * This transition occurs in smb_ofile_open(). A new ofile is created and 184 * added to the list of ofiles of a tree. 185 * 186 * Transition T1 187 * 188 * This transition occurs in smb_ofile_close(). Note that this only happens 189 * when we determine that an ofile should be closed in spite of its durable 190 * handle properties. 191 * 192 * Transition T2 193 * 194 * This transition occurs in smb_ofile_release(). The resources associated 195 * with the ofile are freed as well as the ofile structure. For the 196 * transition to occur, the ofile must be in the SMB_OFILE_STATE_CLOSED 197 * state and the reference count be zero. 198 * 199 * Transition T3 200 * 201 * This transition occurs in smb_ofile_orphan_dh(). It happens during an 202 * smb2 logoff, or during a session disconnect when certain conditions are 203 * met. The ofile and structures above it will be kept around until the ofile 204 * either gets reclaimed, expires after f_timeout_offset nanoseconds, or its 205 * oplock is broken. 206 * 207 * Transition T4 208 * 209 * This transition occurs in smb2_dh_reconnect(). An smb2 create request 210 * with a DURABLE_HANDLE_RECONNECT(_V2) create context has been 211 * recieved from the original owner. If leases are supported or it's 212 * RECONNECT_V2, reconnect is subject to additional conditions. The ofile 213 * will be unwired from the old, disconnected session, tree, and user, 214 * and wired up to its new context. 215 * 216 * Transition T5 217 * 218 * This transition occurs in smb2_dh_reconnect(). The ofile has been 219 * successfully reclaimed. 220 * 221 * Transition T6 222 * 223 * This transition occurs in smb_ofile_close(). The ofile has been orphaned 224 * while some thread was blocked, and that thread closes the ofile. Can only 225 * happen when the ofile is orphaned due to an SMB2 LOGOFF request. 226 * 227 * Transition T7 228 * 229 * This transition occurs in smb_session_durable_timers() and 230 * smb_oplock_send_brk(). The ofile will soon be closed. 231 * In the former case, f_timeout_offset nanoseconds have passed since 232 * the ofile was orphaned. In the latter, an oplock break occured 233 * on the ofile while it was orphaned. 234 * 235 * Transition T8 236 * 237 * This transition occurs in smb_ofile_close(). 238 * 239 * Transition T9 240 * 241 * This transition occurs in smb_ofile_delete(). 242 * 243 * Comments 244 * -------- 245 * 246 * The state machine of the ofile structures is controlled by 3 elements: 247 * - The list of ofiles of the tree it belongs to. 248 * - The mutex embedded in the structure itself. 249 * - The reference count. 250 * 251 * There's a mutex embedded in the ofile structure used to protect its fields 252 * and there's a lock embedded in the list of ofiles of a tree. To 253 * increment or to decrement the reference count the mutex must be entered. 254 * To insert the ofile into the list of ofiles of the tree and to remove 255 * the ofile from it, the lock must be entered in RW_WRITER mode. 256 * 257 * Rules of access to a ofile structure: 258 * 259 * 1) In order to avoid deadlocks, when both (mutex and lock of the ofile 260 * list) have to be entered, the lock must be entered first. Additionally, 261 * f_mutex must not be held when removing the ofile from sv_persistid_ht. 262 * 263 * 2) All actions applied to an ofile require a reference count. 264 * 265 * 3) There are 2 ways of getting a reference count. One is when the ofile 266 * is opened. The other one when the ofile is looked up. This translates 267 * into 2 functions: smb_ofile_open() and smb_ofile_lookup_by_fid(). 268 * 269 * It should be noted that the reference count of an ofile registers the 270 * number of references to the ofile in other structures (such as an smb 271 * request). The reference count is not incremented in these 2 instances: 272 * 273 * 1) The ofile is open. An ofile is anchored by its state. If there's 274 * no activity involving an ofile currently open, the reference count 275 * of that ofile is zero. 276 * 277 * 2) The ofile is queued in the list of ofiles of its tree. The fact of 278 * being queued in that list is NOT registered by incrementing the 279 * reference count. 280 */ 281 #include <smbsrv/smb2_kproto.h> 282 #include <smbsrv/smb_fsops.h> 283 #include <sys/time.h> 284 #include <sys/random.h> 285 286 static boolean_t smb_ofile_is_open_locked(smb_ofile_t *); 287 static void smb_ofile_delete(void *arg); 288 static void smb_ofile_save_dh(void *arg); 289 290 static int smb_ofile_netinfo_encode(smb_ofile_t *, uint8_t *, size_t, 291 uint32_t *); 292 static int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *); 293 static void smb_ofile_netinfo_fini(smb_netfileinfo_t *); 294 295 /* 296 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile 297 * which is used to uniquely identify open instances for the 298 * VFS share reservation and POSIX locks. 299 */ 300 static volatile uint32_t smb_fids = 0; 301 #define SMB_UNIQ_FID() atomic_inc_32_nv(&smb_fids) 302 303 /* 304 * smb_ofile_alloc 305 * Allocate an ofile and fill in it's "up" pointers, but 306 * do NOT link it into the tree's list of ofiles or the 307 * node's list of ofiles. An ofile in this state is a 308 * "proposed" open passed to the oplock break code. 309 * 310 * If we don't get as far as smb_ofile_open with this OF, 311 * call smb_ofile_free() to free this object. 312 * 313 * Note: The following sr members may be null during 314 * persistent handle import: session, uid_usr, tid_tree 315 */ 316 smb_ofile_t * 317 smb_ofile_alloc( 318 smb_request_t *sr, 319 smb_arg_open_t *op, 320 smb_node_t *node, /* optional (may be NULL) */ 321 uint16_t ftype, 322 uint16_t tree_fid) 323 { 324 smb_user_t *user = sr->uid_user; /* optional */ 325 smb_tree_t *tree = sr->tid_tree; /* optional */ 326 smb_ofile_t *of; 327 328 of = kmem_cache_alloc(smb_cache_ofile, KM_SLEEP); 329 bzero(of, sizeof (smb_ofile_t)); 330 of->f_magic = SMB_OFILE_MAGIC; 331 332 mutex_init(&of->f_mutex, NULL, MUTEX_DEFAULT, NULL); 333 list_create(&of->f_notify.nc_waiters, sizeof (smb_request_t), 334 offsetof(smb_request_t, sr_waiters)); 335 mutex_init(&of->dh_nvlock, NULL, MUTEX_DEFAULT, NULL); 336 337 of->f_state = SMB_OFILE_STATE_ALLOC; 338 of->f_refcnt = 1; 339 of->f_ftype = ftype; 340 of->f_fid = tree_fid; 341 /* of->f_persistid see smb2_create */ 342 of->f_uniqid = SMB_UNIQ_FID(); 343 of->f_opened_by_pid = sr->smb_pid; 344 of->f_granted_access = op->desired_access; 345 of->f_share_access = op->share_access; 346 of->f_create_options = op->create_options; 347 if (user != NULL) { 348 if ((op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) != 0) 349 of->f_cr = smb_user_getprivcred(user); 350 else 351 of->f_cr = user->u_cred; 352 crhold(of->f_cr); 353 } 354 of->f_server = sr->sr_server; 355 of->f_session = sr->session; /* may be NULL */ 356 357 (void) memset(of->f_lock_seq, -1, SMB_OFILE_LSEQ_MAX); 358 359 of->f_mode = smb_fsop_amask_to_omode(of->f_granted_access); 360 if ((of->f_granted_access & FILE_DATA_ALL) == FILE_EXECUTE) 361 of->f_flags |= SMB_OFLAGS_EXECONLY; 362 363 /* 364 * In case a lease is requested, copy the lease keys now so 365 * any oplock breaks during open don't break those on our 366 * other handles that might have the same lease. 367 */ 368 bcopy(op->lease_key, of->TargetOplockKey, SMB_LEASE_KEY_SZ); 369 bcopy(op->parent_lease_key, of->ParentOplockKey, SMB_LEASE_KEY_SZ); 370 371 /* 372 * grab a ref for of->f_user and of->f_tree 373 * We know the user and tree must be "live" because 374 * this SR holds references to them. The node ref. is 375 * held by our caller, until smb_ofile_open puts this 376 * ofile on the node ofile list with smb_node_add_ofile. 377 */ 378 if (user != NULL) { 379 smb_user_hold_internal(user); 380 of->f_user = user; 381 } 382 if (tree != NULL) { 383 smb_tree_hold_internal(tree); 384 of->f_tree = tree; 385 } 386 of->f_node = node; /* may be NULL */ 387 388 return (of); 389 } 390 391 /* 392 * smb_ofile_open 393 * 394 * Complete an open on an ofile that was previously allocated by 395 * smb_ofile_alloc, by putting it on the tree ofile list and 396 * (if it's a file) the node ofile list. 397 */ 398 void 399 smb_ofile_open( 400 smb_request_t *sr, 401 smb_arg_open_t *op, 402 smb_ofile_t *of) 403 { 404 smb_tree_t *tree = sr->tid_tree; 405 smb_node_t *node = of->f_node; 406 407 ASSERT(of->f_state == SMB_OFILE_STATE_ALLOC); 408 ASSERT(of->f_fid != 0); 409 410 of->f_state = SMB_OFILE_STATE_OPEN; 411 412 switch (of->f_ftype) { 413 case SMB_FTYPE_BYTE_PIPE: 414 case SMB_FTYPE_MESG_PIPE: 415 /* See smb_opipe_open. */ 416 of->f_pipe = op->pipe; 417 smb_server_inc_pipes(of->f_server); 418 break; 419 case SMB_FTYPE_DISK: 420 case SMB_FTYPE_PRINTER: 421 /* Regular file, not a pipe */ 422 ASSERT(node != NULL); 423 424 smb_node_inc_open_ofiles(node); 425 smb_node_add_ofile(node, of); 426 smb_node_ref(node); 427 smb_server_inc_files(of->f_server); 428 break; 429 default: 430 ASSERT(0); 431 } 432 smb_llist_enter(&tree->t_ofile_list, RW_WRITER); 433 smb_llist_insert_tail(&tree->t_ofile_list, of); 434 smb_llist_exit(&tree->t_ofile_list); 435 atomic_inc_32(&tree->t_open_files); 436 atomic_inc_32(&of->f_session->s_file_cnt); 437 438 } 439 440 /* 441 * smb_ofile_close 442 * 443 * Incoming states: (where from) 444 * SMB_OFILE_STATE_OPEN protocol close, smb_ofile_drop 445 * SMB_OFILE_STATE_EXPIRED called via smb2_dh_expire 446 * SMB_OFILE_STATE_ORPHANED smb2_dh_shutdown 447 */ 448 void 449 smb_ofile_close(smb_ofile_t *of, int32_t mtime_sec) 450 { 451 smb_attr_t *pa; 452 453 SMB_OFILE_VALID(of); 454 455 if (of->f_ftype == SMB_FTYPE_DISK) { 456 smb_node_t *node = of->f_node; 457 458 smb_llist_enter(&node->n_ofile_list, RW_READER); 459 mutex_enter(&node->n_oplock.ol_mutex); 460 461 if (of->f_oplock_closing == B_FALSE) { 462 of->f_oplock_closing = B_TRUE; 463 464 if (of->f_lease != NULL) 465 smb2_lease_ofile_close(of); 466 smb_oplock_break_CLOSE(node, of); 467 } 468 469 mutex_exit(&node->n_oplock.ol_mutex); 470 smb_llist_exit(&node->n_ofile_list); 471 } 472 473 mutex_enter(&of->f_mutex); 474 ASSERT(of->f_refcnt); 475 476 switch (of->f_state) { 477 case SMB_OFILE_STATE_OPEN: 478 case SMB_OFILE_STATE_ORPHANED: 479 case SMB_OFILE_STATE_EXPIRED: 480 of->f_state = SMB_OFILE_STATE_CLOSING; 481 mutex_exit(&of->f_mutex); 482 break; 483 default: 484 mutex_exit(&of->f_mutex); 485 return; 486 } 487 488 /* 489 * Only one thread here (the one that that set f_state closing) 490 */ 491 switch (of->f_ftype) { 492 case SMB_FTYPE_BYTE_PIPE: 493 case SMB_FTYPE_MESG_PIPE: 494 smb_opipe_close(of); 495 smb_server_dec_pipes(of->f_server); 496 break; 497 498 case SMB_FTYPE_DISK: 499 if (of->dh_persist) 500 smb2_dh_close_persistent(of); 501 if (of->f_persistid != 0) 502 smb_ofile_del_persistid(of); 503 /* FALLTHROUGH */ 504 505 case SMB_FTYPE_PRINTER: /* or FTYPE_DISK */ 506 /* 507 * In here we make changes to of->f_pending_attr 508 * while not holding of->f_mutex. This is OK 509 * because we've changed f_state to CLOSING, 510 * so no more threads will take this path. 511 */ 512 pa = &of->f_pending_attr; 513 if (mtime_sec != 0) { 514 pa->sa_vattr.va_mtime.tv_sec = mtime_sec; 515 pa->sa_mask |= SMB_AT_MTIME; 516 } 517 518 if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) { 519 /* We delete using the on-disk name. */ 520 uint32_t flags = SMB_CASE_SENSITIVE; 521 (void) smb_node_set_delete_on_close(of->f_node, 522 of->f_cr, flags); 523 } 524 smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid); 525 smb_node_destroy_lock_by_ofile(of->f_node, of); 526 527 if (smb_node_is_file(of->f_node)) { 528 (void) smb_fsop_close(of->f_node, of->f_mode, 529 of->f_cr); 530 } else { 531 /* 532 * If there was an odir, close it. 533 */ 534 if (of->f_odir != NULL) 535 smb_odir_close(of->f_odir); 536 /* 537 * Cancel any notify change requests that 538 * might be watching this open file (dir), 539 * and unsubscribe it from node events. 540 * 541 * Can't hold f_mutex when calling smb_notify_ofile. 542 * Don't really need it when unsubscribing, but 543 * harmless, and consistent with subscribing. 544 */ 545 if (of->f_notify.nc_subscribed) 546 smb_notify_ofile(of, 547 FILE_ACTION_HANDLE_CLOSED, NULL); 548 mutex_enter(&of->f_mutex); 549 if (of->f_notify.nc_subscribed) { 550 of->f_notify.nc_subscribed = B_FALSE; 551 smb_node_fcn_unsubscribe(of->f_node); 552 of->f_notify.nc_filter = 0; 553 } 554 mutex_exit(&of->f_mutex); 555 } 556 if (smb_node_dec_open_ofiles(of->f_node) == 0) { 557 /* 558 * Last close. If we're not deleting 559 * the file, apply any pending attrs. 560 * Leave allocsz zero when no open files, 561 * just to avoid confusion, because it's 562 * only updated when there are opens. 563 * XXX: Just do this on _every_ close. 564 */ 565 mutex_enter(&of->f_node->n_mutex); 566 if (of->f_node->flags & NODE_FLAGS_DELETE_ON_CLOSE) { 567 smb_node_delete_on_close(of->f_node); 568 pa->sa_mask = 0; 569 } 570 of->f_node->n_allocsz = 0; 571 mutex_exit(&of->f_node->n_mutex); 572 } 573 if (pa->sa_mask != 0) { 574 /* 575 * Commit any pending attributes from 576 * the ofile we're closing. Note that 577 * we pass NULL as the ofile to setattr 578 * so it will write to the file system 579 * and not keep anything on the ofile. 580 */ 581 (void) smb_node_setattr(NULL, of->f_node, 582 of->f_cr, NULL, pa); 583 } 584 585 smb_server_dec_files(of->f_server); 586 break; 587 } 588 589 /* 590 * Keep f_state == SMB_OFILE_STATE_CLOSING 591 * until the last ref. is dropped, in 592 * smb_ofile_release() 593 */ 594 } 595 596 /* 597 * "Destructor" function for smb_ofile_close_all, and 598 * smb_ofile_close_all_by_pid, called after the llist lock 599 * for tree list has been exited. Our job is to either 600 * close this ofile, or (if durable) set state _SAVE_DH. 601 * 602 * The next interesting thing happens when the last ref. 603 * on this ofile calls smb_ofile_release(), where we 604 * eihter delete the ofile, or (if durable) leave it 605 * in the persistid hash table for possible reclaim. 606 * 607 * This is run via smb_llist_post (after smb_llist_exit) 608 * because smb_ofile_close can block, and we'd rather not 609 * block while holding the ofile list as reader. 610 */ 611 static void 612 smb_ofile_drop(void *arg) 613 { 614 smb_ofile_t *of = arg; 615 616 SMB_OFILE_VALID(of); 617 618 mutex_enter(&of->f_mutex); 619 switch (of->f_state) { 620 case SMB_OFILE_STATE_OPEN: 621 /* DH checks under mutex. */ 622 if (of->f_ftype == SMB_FTYPE_DISK && 623 of->dh_vers != SMB2_NOT_DURABLE && 624 smb_dh_should_save(of)) { 625 /* 626 * Tell smb_ofile_release() to 627 * make this an _ORPHANED DH. 628 */ 629 of->f_state = SMB_OFILE_STATE_SAVE_DH; 630 mutex_exit(&of->f_mutex); 631 break; 632 } 633 /* OK close it. */ 634 mutex_exit(&of->f_mutex); 635 smb_ofile_close(of, 0); 636 break; 637 638 default: 639 /* Something else closed it already. */ 640 mutex_exit(&of->f_mutex); 641 break; 642 } 643 644 /* 645 * Release the ref acquired during the traversal loop. 646 * Note that on the last ref, this ofile will be 647 * removed from the tree list etc. 648 * See: smb_llist_post, smb_ofile_delete 649 */ 650 smb_ofile_release(of); 651 } 652 653 /* 654 * smb_ofile_close_all 655 * 656 * 657 */ 658 void 659 smb_ofile_close_all( 660 smb_tree_t *tree, 661 uint32_t pid) 662 { 663 smb_ofile_t *of; 664 smb_llist_t *ll; 665 666 ASSERT(tree); 667 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 668 669 ll = &tree->t_ofile_list; 670 671 smb_llist_enter(ll, RW_READER); 672 for (of = smb_llist_head(ll); 673 of != NULL; 674 of = smb_llist_next(ll, of)) { 675 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 676 ASSERT(of->f_tree == tree); 677 if (pid != 0 && of->f_opened_by_pid != pid) 678 continue; 679 if (smb_ofile_hold(of)) { 680 smb_llist_post(ll, of, smb_ofile_drop); 681 } 682 } 683 684 /* 685 * Drop the lock and process the llist dtor queue. 686 * Calls smb_ofile_drop on ofiles that were open. 687 */ 688 smb_llist_exit(ll); 689 } 690 691 /* 692 * If the enumeration request is for ofile data, handle it here. 693 * Otherwise, return. 694 * 695 * This function should be called with a hold on the ofile. 696 */ 697 int 698 smb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum) 699 { 700 uint8_t *pb; 701 uint_t nbytes; 702 int rc; 703 704 ASSERT(of); 705 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 706 ASSERT(of->f_refcnt); 707 708 if (svcenum->se_type != SMB_SVCENUM_TYPE_FILE) 709 return (0); 710 711 if (svcenum->se_nskip > 0) { 712 svcenum->se_nskip--; 713 return (0); 714 } 715 716 if (svcenum->se_nitems >= svcenum->se_nlimit) { 717 svcenum->se_nitems = svcenum->se_nlimit; 718 return (0); 719 } 720 721 pb = &svcenum->se_buf[svcenum->se_bused]; 722 723 rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail, 724 &nbytes); 725 if (rc == 0) { 726 svcenum->se_bavail -= nbytes; 727 svcenum->se_bused += nbytes; 728 svcenum->se_nitems++; 729 } 730 731 return (rc); 732 } 733 734 /* 735 * Take a reference on an open file, in any of the states: 736 * RECONNECT, SAVE_DH, OPEN, ORPHANED. 737 * Return TRUE if ref taken. Used for oplock breaks. 738 * 739 * Note: When the oplock break code calls this, it holds the 740 * node ofile list lock and node oplock mutex. When we see 741 * an ofile in states RECONNECT or SAVING, we know the ofile 742 * is gaining or losing it's tree, and that happens quickly, 743 * so we just wait for that work to finish. However, the 744 * waiting for state transitions here means we have to be 745 * careful not to re-enter the node list lock or otherwise 746 * block on things that could cause a deadlock. Waiting 747 * just on of->f_mutex here is OK. 748 */ 749 boolean_t 750 smb_ofile_hold_olbrk(smb_ofile_t *of) 751 { 752 boolean_t ret = B_FALSE; 753 754 ASSERT(of); 755 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 756 757 mutex_enter(&of->f_mutex); 758 759 again: 760 switch (of->f_state) { 761 case SMB_OFILE_STATE_RECONNECT: 762 case SMB_OFILE_STATE_SAVING: 763 cv_wait(&of->f_cv, &of->f_mutex); 764 goto again; 765 766 case SMB_OFILE_STATE_OPEN: 767 case SMB_OFILE_STATE_ORPHANED: 768 case SMB_OFILE_STATE_SAVE_DH: 769 of->f_refcnt++; 770 ret = B_TRUE; 771 break; 772 773 default: 774 break; 775 } 776 mutex_exit(&of->f_mutex); 777 778 return (ret); 779 } 780 781 /* 782 * Take a reference on an open file. 783 */ 784 boolean_t 785 smb_ofile_hold(smb_ofile_t *of) 786 { 787 ASSERT(of); 788 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 789 790 mutex_enter(&of->f_mutex); 791 792 if (of->f_state != SMB_OFILE_STATE_OPEN) { 793 mutex_exit(&of->f_mutex); 794 return (B_FALSE); 795 } 796 of->f_refcnt++; 797 798 mutex_exit(&of->f_mutex); 799 return (B_TRUE); 800 } 801 802 /* 803 * Void arg variant of smb_ofile_release for use with smb_llist_post. 804 * This is needed because smb_ofile_release may need to enter the 805 * smb_llist as writer when it drops the last reference, so when 806 * we're in the llist as reader, use smb_llist_post with this 807 * function to arrange for the release call at llist_exit. 808 */ 809 void 810 smb_ofile_release_LL(void *arg) 811 { 812 smb_ofile_t *of = arg; 813 814 SMB_OFILE_VALID(of); 815 smb_ofile_release(of); 816 } 817 818 /* 819 * Release a reference on a file. If the reference count falls to 820 * zero and the file has been closed, post the object for deletion. 821 * Object deletion is deferred to avoid modifying a list while an 822 * iteration may be in progress. 823 * 824 * We're careful to avoid dropping f_session etc. until the last 825 * reference goes away. The oplock break code depends on that 826 * not changing while it holds a ref. on an ofile. 827 */ 828 void 829 smb_ofile_release(smb_ofile_t *of) 830 { 831 smb_tree_t *tree = of->f_tree; 832 boolean_t delete = B_FALSE; 833 834 SMB_OFILE_VALID(of); 835 836 mutex_enter(&of->f_mutex); 837 ASSERT(of->f_refcnt > 0); 838 of->f_refcnt--; 839 840 switch (of->f_state) { 841 case SMB_OFILE_STATE_OPEN: 842 case SMB_OFILE_STATE_ORPHANED: 843 case SMB_OFILE_STATE_EXPIRED: 844 break; 845 846 case SMB_OFILE_STATE_SAVE_DH: 847 ASSERT(tree != NULL); 848 if (of->f_refcnt == 0) { 849 of->f_state = SMB_OFILE_STATE_SAVING; 850 smb_llist_post(&tree->t_ofile_list, of, 851 smb_ofile_save_dh); 852 } 853 break; 854 855 case SMB_OFILE_STATE_CLOSING: 856 /* Note, tree == NULL on _ORPHANED */ 857 if (of->f_refcnt == 0) { 858 of->f_state = SMB_OFILE_STATE_CLOSED; 859 if (tree == NULL) { 860 /* Skip smb_llist_post */ 861 delete = B_TRUE; 862 break; 863 } 864 smb_llist_post(&tree->t_ofile_list, of, 865 smb_ofile_delete); 866 } 867 break; 868 869 default: 870 ASSERT(0); 871 break; 872 } 873 mutex_exit(&of->f_mutex); 874 875 /* 876 * When we drop the last ref. on an expired DH, it's no longer 877 * in any tree, so skip the smb_llist_post and just call 878 * smb_ofile_delete directly. 879 */ 880 if (delete) { 881 smb_ofile_delete(of); 882 } 883 } 884 885 /* 886 * smb_ofile_lookup_by_fid 887 * 888 * Find the open file whose fid matches the one specified in the request. 889 * If we can't find the fid or the shares (trees) don't match, we have a 890 * bad fid. 891 */ 892 smb_ofile_t * 893 smb_ofile_lookup_by_fid( 894 smb_request_t *sr, 895 uint16_t fid) 896 { 897 smb_tree_t *tree = sr->tid_tree; 898 smb_llist_t *of_list; 899 smb_ofile_t *of; 900 901 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 902 903 of_list = &tree->t_ofile_list; 904 905 smb_llist_enter(of_list, RW_READER); 906 of = smb_llist_head(of_list); 907 while (of) { 908 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 909 ASSERT(of->f_tree == tree); 910 if (of->f_fid == fid) 911 break; 912 of = smb_llist_next(of_list, of); 913 } 914 if (of == NULL) 915 goto out; 916 917 /* 918 * Only allow use of a given FID with the same UID that 919 * was used to open it. MS-CIFS 3.3.5.14 920 */ 921 if (of->f_user != sr->uid_user) { 922 of = NULL; 923 goto out; 924 } 925 926 /* inline smb_ofile_hold() */ 927 mutex_enter(&of->f_mutex); 928 if (of->f_state != SMB_OFILE_STATE_OPEN) { 929 mutex_exit(&of->f_mutex); 930 of = NULL; 931 goto out; 932 } 933 of->f_refcnt++; 934 mutex_exit(&of->f_mutex); 935 936 out: 937 smb_llist_exit(of_list); 938 return (of); 939 } 940 941 /* 942 * smb_ofile_lookup_by_uniqid 943 * 944 * Find the open file whose uniqid matches the one specified in the request. 945 */ 946 smb_ofile_t * 947 smb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid) 948 { 949 smb_llist_t *of_list; 950 smb_ofile_t *of; 951 952 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 953 954 of_list = &tree->t_ofile_list; 955 smb_llist_enter(of_list, RW_READER); 956 of = smb_llist_head(of_list); 957 958 while (of) { 959 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 960 ASSERT(of->f_tree == tree); 961 962 if (of->f_uniqid == uniqid) { 963 if (smb_ofile_hold(of)) { 964 smb_llist_exit(of_list); 965 return (of); 966 } 967 } 968 969 of = smb_llist_next(of_list, of); 970 } 971 972 smb_llist_exit(of_list); 973 return (NULL); 974 } 975 976 /* 977 * Durable ID (or persistent ID) 978 */ 979 980 static smb_ofile_t * 981 smb_ofile_hold_cb(smb_ofile_t *of) 982 { 983 smb_ofile_t *ret = of; 984 985 mutex_enter(&of->f_mutex); 986 if (of->f_state == SMB_OFILE_STATE_ORPHANED) 987 /* inline smb_ofile_hold() */ 988 of->f_refcnt++; 989 else 990 ret = NULL; 991 992 mutex_exit(&of->f_mutex); 993 return (ret); 994 } 995 996 /* 997 * Lookup an ofile by persistent ID, and return ONLY if in state ORPHANED 998 * This is used by SMB2 create "reclaim". 999 */ 1000 smb_ofile_t * 1001 smb_ofile_lookup_by_persistid(smb_request_t *sr, uint64_t persistid) 1002 { 1003 smb_hash_t *hash; 1004 smb_bucket_t *bucket; 1005 smb_llist_t *ll; 1006 smb_ofile_t *of; 1007 uint_t idx; 1008 1009 if (persistid == 0) 1010 return (NULL); 1011 1012 hash = sr->sr_server->sv_persistid_ht; 1013 idx = smb_hash_uint64(hash, persistid); 1014 bucket = &hash->buckets[idx]; 1015 ll = &bucket->b_list; 1016 1017 smb_llist_enter(ll, RW_READER); 1018 of = smb_llist_head(ll); 1019 while (of != NULL) { 1020 if (of->f_persistid == persistid) 1021 break; 1022 of = smb_llist_next(ll, of); 1023 } 1024 if (of != NULL) 1025 of = smb_ofile_hold_cb(of); 1026 smb_llist_exit(ll); 1027 1028 return (of); 1029 } 1030 1031 /* 1032 * Create a (unique) durable/persistent ID for a new ofile, 1033 * and add this ofile to the persistid hash table. This ID 1034 * is referred to as the persistent ID in the protocol spec, 1035 * so that's what we call it too, though the persistence may 1036 * vary. "Durable" handles are persistent across reconnects 1037 * but not server reboots. Persistent handles are persistent 1038 * across server reboots too. 1039 * 1040 * Note that persistent IDs need to be unique for the lifetime of 1041 * any given ofile. For normal (non-persistent) ofiles we can just 1042 * use a persistent ID derived from the ofile memory address, as 1043 * these don't ever live beyond the current OS boot lifetime. 1044 * 1045 * Persistent handles are re-imported after server restart, and 1046 * generally have a different memory address after import than 1047 * they had in the previous OS boot lifetime, so for these we 1048 * use a randomly assigned value that won't conflict with any 1049 * non-persistent (durable) handles. Ensuring that a randomly 1050 * generated ID is unique requires a search of the ofiles in one 1051 * hash bucket, which we'd rather avoid for non-persistent opens. 1052 * 1053 * The solution used here is to divide the persistent ID space 1054 * in half (odd and even values) where durable opens use an ID 1055 * derived from the ofile address (which is always even), and 1056 * persistent opens use an ID generated randomly (always odd). 1057 * 1058 * smb_ofile_set_persistid_dh() sets a durable handle ID and 1059 * smb_ofile_set_persistid_ph() sets a persistent handle ID. 1060 */ 1061 void 1062 smb_ofile_set_persistid_dh(smb_ofile_t *of) 1063 { 1064 smb_hash_t *hash = of->f_server->sv_persistid_ht; 1065 smb_bucket_t *bucket; 1066 smb_llist_t *ll; 1067 uint64_t persistid; 1068 uint_t idx; 1069 1070 persistid = (uintptr_t)of; 1071 /* Avoid showing object addresses */ 1072 persistid ^= ((uintptr_t)&smb_cache_ofile); 1073 /* make sure it's even */ 1074 persistid &= ~((uint64_t)1); 1075 1076 idx = smb_hash_uint64(hash, persistid); 1077 bucket = &hash->buckets[idx]; 1078 ll = &bucket->b_list; 1079 smb_llist_enter(ll, RW_WRITER); 1080 if (of->f_persistid == 0) { 1081 of->f_persistid = persistid; 1082 smb_llist_insert_tail(ll, of); 1083 } 1084 smb_llist_exit(ll); 1085 } 1086 1087 void 1088 smb_ofile_set_persistid_ph(smb_ofile_t *of) 1089 { 1090 uint64_t persistid; 1091 int rc; 1092 1093 top: 1094 (void) random_get_pseudo_bytes((uint8_t *)&persistid, 1095 sizeof (persistid)); 1096 if (persistid == 0) { 1097 cmn_err(CE_NOTE, "random gave all zeros!"); 1098 goto top; 1099 } 1100 /* make sure it's odd */ 1101 persistid |= (uint64_t)1; 1102 1103 /* 1104 * Try inserting with this persistent ID. 1105 */ 1106 rc = smb_ofile_insert_persistid(of, persistid); 1107 if (rc == EEXIST) 1108 goto top; 1109 if (rc != 0) { 1110 cmn_err(CE_NOTE, "set persistid rc=%d", rc); 1111 } 1112 } 1113 1114 /* 1115 * Insert an ofile into the persistid hash table. 1116 * If the persistent ID is in use, error. 1117 */ 1118 int 1119 smb_ofile_insert_persistid(smb_ofile_t *new_of, uint64_t persistid) 1120 { 1121 smb_hash_t *hash = new_of->f_server->sv_persistid_ht; 1122 smb_bucket_t *bucket; 1123 smb_llist_t *ll; 1124 smb_ofile_t *of; 1125 uint_t idx; 1126 1127 ASSERT(persistid != 0); 1128 1129 /* 1130 * Look to see if this key alreay exists. 1131 */ 1132 idx = smb_hash_uint64(hash, persistid); 1133 bucket = &hash->buckets[idx]; 1134 ll = &bucket->b_list; 1135 1136 smb_llist_enter(ll, RW_WRITER); 1137 of = smb_llist_head(ll); 1138 while (of != NULL) { 1139 if (of->f_persistid == persistid) { 1140 /* already in use */ 1141 smb_llist_exit(ll); 1142 return (EEXIST); 1143 } 1144 of = smb_llist_next(ll, of); 1145 } 1146 1147 /* Not found, so OK to insert. */ 1148 if (new_of->f_persistid == 0) { 1149 new_of->f_persistid = persistid; 1150 smb_llist_insert_tail(ll, new_of); 1151 } 1152 smb_llist_exit(ll); 1153 1154 return (0); 1155 } 1156 1157 void 1158 smb_ofile_del_persistid(smb_ofile_t *of) 1159 { 1160 smb_hash_t *hash = of->f_server->sv_persistid_ht; 1161 smb_bucket_t *bucket; 1162 smb_llist_t *ll; 1163 uint_t idx; 1164 1165 idx = smb_hash_uint64(hash, of->f_persistid); 1166 bucket = &hash->buckets[idx]; 1167 ll = &bucket->b_list; 1168 smb_llist_enter(ll, RW_WRITER); 1169 if (of->f_persistid != 0) { 1170 smb_llist_remove(ll, of); 1171 of->f_persistid = 0; 1172 } 1173 smb_llist_exit(ll); 1174 } 1175 1176 /* 1177 * Disallow NetFileClose on certain ofiles to avoid side-effects. 1178 * Closing a tree root is not allowed: use NetSessionDel or NetShareDel. 1179 * Closing SRVSVC connections is not allowed because this NetFileClose 1180 * request may depend on this ofile. 1181 */ 1182 boolean_t 1183 smb_ofile_disallow_fclose(smb_ofile_t *of) 1184 { 1185 ASSERT(of); 1186 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1187 ASSERT(of->f_refcnt); 1188 1189 switch (of->f_ftype) { 1190 case SMB_FTYPE_DISK: 1191 ASSERT(of->f_tree); 1192 return (of->f_node == of->f_tree->t_snode); 1193 1194 case SMB_FTYPE_MESG_PIPE: 1195 ASSERT(of->f_pipe); 1196 if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0) 1197 return (B_TRUE); 1198 break; 1199 default: 1200 break; 1201 } 1202 1203 return (B_FALSE); 1204 } 1205 1206 /* 1207 * smb_ofile_set_flags 1208 * 1209 * Return value: 1210 * 1211 * Current flags value 1212 * 1213 */ 1214 void 1215 smb_ofile_set_flags( 1216 smb_ofile_t *of, 1217 uint32_t flags) 1218 { 1219 ASSERT(of); 1220 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1221 ASSERT(of->f_refcnt); 1222 1223 mutex_enter(&of->f_mutex); 1224 of->f_flags |= flags; 1225 mutex_exit(&of->f_mutex); 1226 } 1227 1228 /* 1229 * smb_ofile_seek 1230 * 1231 * Return value: 1232 * 1233 * 0 Success 1234 * EINVAL Unknown mode 1235 * EOVERFLOW offset too big 1236 * 1237 */ 1238 int 1239 smb_ofile_seek( 1240 smb_ofile_t *of, 1241 ushort_t mode, 1242 int32_t off, 1243 uint32_t *retoff) 1244 { 1245 u_offset_t newoff = 0; 1246 int rc = 0; 1247 smb_attr_t attr; 1248 1249 ASSERT(of); 1250 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1251 ASSERT(of->f_refcnt); 1252 1253 mutex_enter(&of->f_mutex); 1254 switch (mode) { 1255 case SMB_SEEK_SET: 1256 if (off < 0) 1257 newoff = 0; 1258 else 1259 newoff = (u_offset_t)off; 1260 break; 1261 1262 case SMB_SEEK_CUR: 1263 if (off < 0 && (-off) > of->f_seek_pos) 1264 newoff = 0; 1265 else 1266 newoff = of->f_seek_pos + (u_offset_t)off; 1267 break; 1268 1269 case SMB_SEEK_END: 1270 bzero(&attr, sizeof (smb_attr_t)); 1271 attr.sa_mask |= SMB_AT_SIZE; 1272 rc = smb_fsop_getattr(NULL, zone_kcred(), of->f_node, &attr); 1273 if (rc != 0) { 1274 mutex_exit(&of->f_mutex); 1275 return (rc); 1276 } 1277 if (off < 0 && (-off) > attr.sa_vattr.va_size) 1278 newoff = 0; 1279 else 1280 newoff = attr.sa_vattr.va_size + (u_offset_t)off; 1281 break; 1282 1283 default: 1284 mutex_exit(&of->f_mutex); 1285 return (EINVAL); 1286 } 1287 1288 /* 1289 * See comments at the beginning of smb_seek.c. 1290 * If the offset is greater than UINT_MAX, we will return an error. 1291 */ 1292 1293 if (newoff > UINT_MAX) { 1294 rc = EOVERFLOW; 1295 } else { 1296 of->f_seek_pos = newoff; 1297 *retoff = (uint32_t)newoff; 1298 } 1299 mutex_exit(&of->f_mutex); 1300 return (rc); 1301 } 1302 1303 /* 1304 * smb_ofile_flush 1305 * 1306 * If writes on this file are not synchronous, flush it using the NFSv3 1307 * commit interface. 1308 * 1309 * XXX - todo: Flush named pipe should drain writes. 1310 */ 1311 void 1312 smb_ofile_flush(struct smb_request *sr, struct smb_ofile *of) 1313 { 1314 switch (of->f_ftype) { 1315 case SMB_FTYPE_DISK: 1316 if ((of->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0) 1317 (void) smb_fsop_commit(sr, of->f_cr, of->f_node); 1318 break; 1319 default: 1320 break; 1321 } 1322 } 1323 1324 /* 1325 * smb_ofile_is_open 1326 */ 1327 boolean_t 1328 smb_ofile_is_open(smb_ofile_t *of) 1329 { 1330 boolean_t rc; 1331 1332 SMB_OFILE_VALID(of); 1333 1334 mutex_enter(&of->f_mutex); 1335 rc = smb_ofile_is_open_locked(of); 1336 mutex_exit(&of->f_mutex); 1337 return (rc); 1338 } 1339 1340 /* *************************** Static Functions ***************************** */ 1341 1342 /* 1343 * Determine whether or not an ofile is open. 1344 * This function must be called with the mutex held. 1345 */ 1346 static boolean_t 1347 smb_ofile_is_open_locked(smb_ofile_t *of) 1348 { 1349 ASSERT(MUTEX_HELD(&of->f_mutex)); 1350 1351 switch (of->f_state) { 1352 case SMB_OFILE_STATE_OPEN: 1353 case SMB_OFILE_STATE_SAVE_DH: 1354 case SMB_OFILE_STATE_SAVING: 1355 case SMB_OFILE_STATE_ORPHANED: 1356 case SMB_OFILE_STATE_RECONNECT: 1357 return (B_TRUE); 1358 1359 case SMB_OFILE_STATE_CLOSING: 1360 case SMB_OFILE_STATE_CLOSED: 1361 case SMB_OFILE_STATE_EXPIRED: 1362 return (B_FALSE); 1363 1364 default: 1365 ASSERT(0); 1366 return (B_FALSE); 1367 } 1368 } 1369 1370 /* 1371 * smb_ofile_save_dh 1372 * 1373 * Called via smb_llist_post (after smb_llist_exit) when the last ref. 1374 * on this ofile has gone, and this ofile is a "durable handle" (DH) 1375 * that has state we've decided to save. 1376 * 1377 * This does parts of what smb_ofile_delete would do, including: 1378 * remove the ofile from the tree ofile list and related. 1379 * 1380 * We leave the ofile in state ORPHANED, ready for reconnect 1381 * or expiration via smb2_dh_expire (see smb_ofile_delete). 1382 */ 1383 static void 1384 smb_ofile_save_dh(void *arg) 1385 { 1386 smb_ofile_t *of = (smb_ofile_t *)arg; 1387 smb_tree_t *tree = of->f_tree; 1388 1389 SMB_OFILE_VALID(of); 1390 ASSERT(of->f_refcnt == 0); 1391 ASSERT(of->f_ftype == SMB_FTYPE_DISK); 1392 ASSERT(of->f_state == SMB_OFILE_STATE_SAVING); 1393 1394 atomic_dec_32(&of->f_session->s_file_cnt); 1395 atomic_dec_32(&of->f_tree->t_open_files); 1396 smb_llist_enter(&tree->t_ofile_list, RW_WRITER); 1397 smb_llist_remove(&tree->t_ofile_list, of); 1398 smb_llist_exit(&tree->t_ofile_list); 1399 1400 /* 1401 * This ofile is no longer on t_ofile_list, however... 1402 * 1403 * This is called via smb_llist_post, which means it may run 1404 * BEFORE smb_ofile_release drops f_mutex (if another thread 1405 * flushes the delete queue before we do). Synchronize. 1406 */ 1407 mutex_enter(&of->f_mutex); 1408 DTRACE_PROBE1(ofile__exit, smb_ofile_t, of); 1409 mutex_exit(&of->f_mutex); 1410 1411 /* 1412 * Keep f_notify state, lease, and 1413 * keep on node ofile list. 1414 * Keep of->f_cr until reclaim. 1415 */ 1416 1417 ASSERT(of->f_fid != 0); 1418 smb_idpool_free(&tree->t_fid_pool, of->f_fid); 1419 of->f_fid = 0; 1420 smb_tree_release(of->f_tree); 1421 of->f_tree = NULL; 1422 smb_user_release(of->f_user); 1423 of->f_user = NULL; 1424 of->f_session = NULL; 1425 1426 /* 1427 * Make it "orphaned" so it can now be reclaimed. 1428 * Note that smb_ofile_hold_olbrk() may have blocked 1429 * for state SMB_OFILE_STATE_SAVING, so wake it. 1430 */ 1431 mutex_enter(&of->f_mutex); 1432 of->dh_expire_time = gethrtime() + of->dh_timeout_offset; 1433 of->f_state = SMB_OFILE_STATE_ORPHANED; 1434 cv_broadcast(&of->f_cv); 1435 mutex_exit(&of->f_mutex); 1436 } 1437 1438 /* 1439 * Delete an ofile. 1440 * 1441 * Approximately the inverse of smb_ofile_alloc() 1442 * Called via smb_llist_post (after smb_llist_exit) 1443 * when the last ref. on this ofile has gone. 1444 * 1445 * Normally,this removes the ofile from the tree list and 1446 * then frees resources held on the ofile. However, when 1447 * we're expiring an orphaned durable handle, the linkage 1448 * into the tree lists etc. have already been destroyed. 1449 * This case is distinguished by of->f_tree == NULL. 1450 */ 1451 static void 1452 smb_ofile_delete(void *arg) 1453 { 1454 smb_ofile_t *of = (smb_ofile_t *)arg; 1455 smb_tree_t *tree = of->f_tree; 1456 1457 SMB_OFILE_VALID(of); 1458 ASSERT(of->f_refcnt == 0); 1459 ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED); 1460 1461 if (tree != NULL) { 1462 ASSERT(of->f_user != NULL); 1463 ASSERT(of->f_session != NULL); 1464 atomic_dec_32(&of->f_session->s_file_cnt); 1465 atomic_dec_32(&of->f_tree->t_open_files); 1466 smb_llist_enter(&tree->t_ofile_list, RW_WRITER); 1467 smb_llist_remove(&tree->t_ofile_list, of); 1468 smb_llist_exit(&tree->t_ofile_list); 1469 } 1470 1471 /* 1472 * Remove this ofile from the node's n_ofile_list so it 1473 * can't be found by list walkers like notify or oplock. 1474 * Keep the node ref. until later in this function so 1475 * of->f_node remains valid while we destroy the ofile. 1476 */ 1477 if (of->f_ftype == SMB_FTYPE_DISK || 1478 of->f_ftype == SMB_FTYPE_PRINTER) { 1479 smb_node_t *node = of->f_node; 1480 1481 /* 1482 * Oplock cleanup should have made sure that 1483 * excl_open does not point to this ofile. 1484 */ 1485 VERIFY(node->n_oplock.excl_open != of); 1486 1487 /* 1488 * Note smb_ofile_close did smb_node_dec_open_ofiles() 1489 */ 1490 smb_node_rem_ofile(node, of); 1491 } 1492 1493 /* 1494 * This ofile is no longer on any lists, however... 1495 * 1496 * This is called via smb_llist_post, which means it may run 1497 * BEFORE smb_ofile_release drops f_mutex (if another thread 1498 * flushes the delete queue before we do). Synchronize. 1499 */ 1500 mutex_enter(&of->f_mutex); 1501 of->f_state = SMB_OFILE_STATE_ALLOC; 1502 DTRACE_PROBE1(ofile__exit, smb_ofile_t, of); 1503 mutex_exit(&of->f_mutex); 1504 1505 switch (of->f_ftype) { 1506 case SMB_FTYPE_BYTE_PIPE: 1507 case SMB_FTYPE_MESG_PIPE: 1508 smb_opipe_dealloc(of->f_pipe); 1509 of->f_pipe = NULL; 1510 break; 1511 case SMB_FTYPE_DISK: 1512 ASSERT(of->f_notify.nc_subscribed == B_FALSE); 1513 MBC_FLUSH(&of->f_notify.nc_buffer); 1514 if (of->f_odir != NULL) 1515 smb_odir_release(of->f_odir); 1516 if (of->f_lease != NULL) { 1517 smb2_lease_rele(of->f_lease); 1518 of->f_lease = NULL; 1519 } 1520 /* FALLTHROUGH */ 1521 case SMB_FTYPE_PRINTER: 1522 /* 1523 * Did smb_node_rem_ofile above. 1524 */ 1525 ASSERT(of->f_node != NULL); 1526 smb_node_release(of->f_node); 1527 break; 1528 default: 1529 ASSERT(!"f_ftype"); 1530 break; 1531 } 1532 1533 smb_ofile_free(of); 1534 } 1535 1536 void 1537 smb_ofile_free(smb_ofile_t *of) 1538 { 1539 smb_tree_t *tree = of->f_tree; 1540 1541 ASSERT(of->f_state == SMB_OFILE_STATE_ALLOC); 1542 1543 /* Make sure it's not in the persistid hash. */ 1544 ASSERT(of->f_persistid == 0); 1545 1546 if (tree != NULL) { 1547 if (of->f_fid != 0) 1548 smb_idpool_free(&tree->t_fid_pool, of->f_fid); 1549 smb_tree_release(of->f_tree); 1550 smb_user_release(of->f_user); 1551 } 1552 1553 if (of->f_cr != NULL) 1554 crfree(of->f_cr); 1555 1556 of->f_magic = (uint32_t)~SMB_OFILE_MAGIC; 1557 list_destroy(&of->f_notify.nc_waiters); 1558 mutex_destroy(&of->dh_nvlock); 1559 mutex_destroy(&of->f_mutex); 1560 kmem_cache_free(smb_cache_ofile, of); 1561 } 1562 1563 /* 1564 * smb_ofile_access 1565 * 1566 * This function will check to see if the access requested is granted. 1567 * Returns NT status codes. 1568 */ 1569 uint32_t 1570 smb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access) 1571 { 1572 1573 if ((of == NULL) || (cr == zone_kcred())) 1574 return (NT_STATUS_SUCCESS); 1575 1576 /* 1577 * If the request is for something 1578 * I don't grant it is an error 1579 */ 1580 if (~(of->f_granted_access) & access) { 1581 if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) && 1582 (access & ACCESS_SYSTEM_SECURITY)) { 1583 return (NT_STATUS_PRIVILEGE_NOT_HELD); 1584 } 1585 return (NT_STATUS_ACCESS_DENIED); 1586 } 1587 1588 return (NT_STATUS_SUCCESS); 1589 } 1590 1591 /* 1592 * smb_ofile_share_check 1593 * 1594 * Check if ofile was opened with share access NONE (0). 1595 * Returns: B_TRUE - share access non-zero 1596 * B_FALSE - share access NONE 1597 */ 1598 boolean_t 1599 smb_ofile_share_check(smb_ofile_t *of) 1600 { 1601 return (!SMB_DENY_ALL(of->f_share_access)); 1602 } 1603 1604 /* 1605 * check file sharing rules for current open request 1606 * against existing open instances of the same file 1607 * 1608 * Returns NT_STATUS_SHARING_VIOLATION if there is any 1609 * sharing conflict, otherwise returns NT_STATUS_SUCCESS. 1610 */ 1611 uint32_t 1612 smb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access, 1613 uint32_t share_access) 1614 { 1615 uint32_t ret; 1616 1617 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1618 1619 mutex_enter(&of->f_mutex); 1620 1621 if (!smb_ofile_is_open_locked(of)) { 1622 ret = NT_STATUS_INVALID_HANDLE; 1623 goto out; 1624 } 1625 1626 /* if it's just meta data */ 1627 if ((of->f_granted_access & FILE_DATA_ALL) == 0) { 1628 ret = NT_STATUS_SUCCESS; 1629 goto out; 1630 } 1631 1632 /* 1633 * Check requested share access against the 1634 * open granted (desired) access 1635 */ 1636 if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) { 1637 ret = NT_STATUS_SHARING_VIOLATION; 1638 goto out; 1639 } 1640 1641 if (SMB_DENY_READ(share_access) && 1642 (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) { 1643 ret = NT_STATUS_SHARING_VIOLATION; 1644 goto out; 1645 } 1646 1647 if (SMB_DENY_WRITE(share_access) && 1648 (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) { 1649 ret = NT_STATUS_SHARING_VIOLATION; 1650 goto out; 1651 } 1652 1653 /* check requested desired access against the open share access */ 1654 if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) { 1655 ret = NT_STATUS_SHARING_VIOLATION; 1656 goto out; 1657 } 1658 1659 if (SMB_DENY_READ(of->f_share_access) && 1660 (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) { 1661 ret = NT_STATUS_SHARING_VIOLATION; 1662 goto out; 1663 } 1664 1665 if (SMB_DENY_WRITE(of->f_share_access) && 1666 (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) { 1667 ret = NT_STATUS_SHARING_VIOLATION; 1668 goto out; 1669 } 1670 1671 ret = NT_STATUS_SUCCESS; 1672 out: 1673 mutex_exit(&of->f_mutex); 1674 return (ret); 1675 } 1676 1677 /* 1678 * smb_ofile_rename_check 1679 * 1680 * This does the work described in MS-FSA 2.1.5.1.2.2 (Algorithm 1681 * to Check Sharing Access to an Existing Stream or Directory), 1682 * where the "open in-progress" has DesiredAccess = DELETE and 1683 * SharingMode = SHARE_READ | SHARE_WRITE | SHARE_DELETE. 1684 */ 1685 1686 uint32_t 1687 smb_ofile_rename_check(smb_ofile_t *of) 1688 { 1689 uint32_t ret; 1690 1691 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1692 1693 mutex_enter(&of->f_mutex); 1694 1695 if (!smb_ofile_is_open_locked(of)) { 1696 ret = NT_STATUS_INVALID_HANDLE; 1697 goto out; 1698 } 1699 1700 if ((of->f_granted_access & FILE_DATA_ALL) == 0) { 1701 ret = NT_STATUS_SUCCESS; 1702 goto out; 1703 } 1704 1705 if ((of->f_share_access & FILE_SHARE_DELETE) == 0) { 1706 ret = NT_STATUS_SHARING_VIOLATION; 1707 goto out; 1708 } 1709 1710 ret = NT_STATUS_SUCCESS; 1711 out: 1712 mutex_exit(&of->f_mutex); 1713 return (ret); 1714 } 1715 1716 /* 1717 * smb_ofile_delete_check 1718 * 1719 * An open file can be deleted only if opened for 1720 * accessing meta data. Share modes aren't important 1721 * in this case. 1722 * 1723 * NOTE: there is another mechanism for deleting an 1724 * open file that NT clients usually use. 1725 * That's setting "Delete on close" flag for an open 1726 * file. In this way the file will be deleted after 1727 * last close. This flag can be set by SmbTrans2SetFileInfo 1728 * with FILE_DISPOSITION_INFO information level. 1729 * For setting this flag, the file should be opened by 1730 * DELETE access in the FID that is passed in the Trans2 1731 * request. 1732 */ 1733 1734 uint32_t 1735 smb_ofile_delete_check(smb_ofile_t *of) 1736 { 1737 uint32_t ret; 1738 1739 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1740 1741 mutex_enter(&of->f_mutex); 1742 1743 if (!smb_ofile_is_open_locked(of)) { 1744 ret = NT_STATUS_INVALID_HANDLE; 1745 goto out; 1746 } 1747 1748 if (of->f_granted_access & 1749 (FILE_READ_DATA | FILE_WRITE_DATA | 1750 FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) { 1751 ret = NT_STATUS_SHARING_VIOLATION; 1752 goto out; 1753 } 1754 1755 ret = NT_STATUS_SUCCESS; 1756 out: 1757 mutex_exit(&of->f_mutex); 1758 return (ret); 1759 } 1760 1761 cred_t * 1762 smb_ofile_getcred(smb_ofile_t *of) 1763 { 1764 return (of->f_cr); 1765 } 1766 1767 /* 1768 * smb_ofile_set_delete_on_close 1769 * 1770 * Set the DeleteOnClose flag on the smb file. When the file is closed, 1771 * the flag will be transferred to the smb node, which will commit the 1772 * delete operation and inhibit subsequent open requests. 1773 * 1774 * When DeleteOnClose is set on an smb_node, the common open code will 1775 * reject subsequent open requests for the file. Observation of Windows 1776 * 2000 indicates that subsequent opens should be allowed (assuming 1777 * there would be no sharing violation) until the file is closed using 1778 * the fid on which the DeleteOnClose was requested. 1779 */ 1780 void 1781 smb_ofile_set_delete_on_close(smb_request_t *sr, smb_ofile_t *of) 1782 { 1783 uint32_t status; 1784 1785 /* 1786 * Break any oplock handle caching. 1787 */ 1788 status = smb_oplock_break_SETINFO(of->f_node, of, 1789 FileDispositionInformation); 1790 if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) { 1791 if (sr->session->dialect >= SMB_VERS_2_BASE) 1792 (void) smb2sr_go_async(sr); 1793 (void) smb_oplock_wait_break(sr, of->f_node, 0); 1794 } 1795 1796 mutex_enter(&of->f_mutex); 1797 of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE; 1798 mutex_exit(&of->f_mutex); 1799 } 1800 1801 /* 1802 * Encode open file information into a buffer; needed in user space to 1803 * support RPC requests. 1804 */ 1805 static int 1806 smb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen, 1807 uint32_t *nbytes) 1808 { 1809 smb_netfileinfo_t fi; 1810 int rc; 1811 1812 rc = smb_ofile_netinfo_init(of, &fi); 1813 if (rc == 0) { 1814 rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes); 1815 smb_ofile_netinfo_fini(&fi); 1816 } 1817 1818 return (rc); 1819 } 1820 1821 static int 1822 smb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi) 1823 { 1824 smb_user_t *user; 1825 smb_tree_t *tree; 1826 smb_node_t *node; 1827 char *path; 1828 char *buf; 1829 int rc; 1830 1831 ASSERT(of); 1832 user = of->f_user; 1833 tree = of->f_tree; 1834 ASSERT(user); 1835 ASSERT(tree); 1836 1837 buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1838 1839 switch (of->f_ftype) { 1840 case SMB_FTYPE_DISK: 1841 node = of->f_node; 1842 ASSERT(node); 1843 1844 fi->fi_permissions = of->f_granted_access; 1845 fi->fi_numlocks = smb_lock_get_lock_count(node, of); 1846 1847 path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1848 1849 if (node != tree->t_snode) { 1850 rc = smb_node_getshrpath(node, tree, path, MAXPATHLEN); 1851 if (rc != 0) 1852 (void) strlcpy(path, node->od_name, MAXPATHLEN); 1853 } 1854 1855 (void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename, 1856 path); 1857 kmem_free(path, MAXPATHLEN); 1858 break; 1859 1860 case SMB_FTYPE_MESG_PIPE: 1861 ASSERT(of->f_pipe); 1862 1863 fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA | 1864 FILE_EXECUTE; 1865 fi->fi_numlocks = 0; 1866 (void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s", 1867 of->f_pipe->p_name); 1868 break; 1869 1870 default: 1871 kmem_free(buf, MAXPATHLEN); 1872 return (-1); 1873 } 1874 1875 fi->fi_fid = of->f_fid; 1876 fi->fi_uniqid = of->f_uniqid; 1877 fi->fi_pathlen = strlen(buf) + 1; 1878 fi->fi_path = smb_mem_strdup(buf); 1879 kmem_free(buf, MAXPATHLEN); 1880 1881 fi->fi_namelen = user->u_domain_len + user->u_name_len + 2; 1882 fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP); 1883 (void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s", 1884 user->u_domain, user->u_name); 1885 return (0); 1886 } 1887 1888 static void 1889 smb_ofile_netinfo_fini(smb_netfileinfo_t *fi) 1890 { 1891 if (fi == NULL) 1892 return; 1893 1894 if (fi->fi_path) 1895 smb_mem_free(fi->fi_path); 1896 if (fi->fi_username) 1897 kmem_free(fi->fi_username, fi->fi_namelen); 1898 1899 bzero(fi, sizeof (smb_netfileinfo_t)); 1900 } 1901 1902 /* 1903 * A query of user and group quotas may span multiple requests. 1904 * f_quota_resume is used to determine where the query should 1905 * be resumed, in a subsequent request. f_quota_resume contains 1906 * the SID of the last quota entry returned to the client. 1907 */ 1908 void 1909 smb_ofile_set_quota_resume(smb_ofile_t *ofile, char *resume) 1910 { 1911 ASSERT(ofile); 1912 mutex_enter(&ofile->f_mutex); 1913 if (resume == NULL) 1914 bzero(ofile->f_quota_resume, SMB_SID_STRSZ); 1915 else 1916 (void) strlcpy(ofile->f_quota_resume, resume, SMB_SID_STRSZ); 1917 mutex_exit(&ofile->f_mutex); 1918 } 1919 1920 void 1921 smb_ofile_get_quota_resume(smb_ofile_t *ofile, char *buf, int bufsize) 1922 { 1923 ASSERT(ofile); 1924 mutex_enter(&ofile->f_mutex); 1925 (void) strlcpy(buf, ofile->f_quota_resume, bufsize); 1926 mutex_exit(&ofile->f_mutex); 1927 } 1928