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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * General Structures Layout 28 * ------------------------- 29 * 30 * This is a simplified diagram showing the relationship between most of the 31 * main structures. 32 * 33 * +-------------------+ 34 * | SMB_INFO | 35 * +-------------------+ 36 * | 37 * | 38 * v 39 * +-------------------+ +-------------------+ +-------------------+ 40 * | SESSION |<----->| SESSION |......| SESSION | 41 * +-------------------+ +-------------------+ +-------------------+ 42 * | 43 * | 44 * v 45 * +-------------------+ +-------------------+ +-------------------+ 46 * | USER |<----->| USER |......| USER | 47 * +-------------------+ +-------------------+ +-------------------+ 48 * | 49 * | 50 * v 51 * +-------------------+ +-------------------+ +-------------------+ 52 * | TREE |<----->| TREE |......| TREE | 53 * +-------------------+ +-------------------+ +-------------------+ 54 * | | 55 * | | 56 * | v 57 * | +-------+ +-------+ +-------+ 58 * | | OFILE |<----->| OFILE |......| OFILE | 59 * | +-------+ +-------+ +-------+ 60 * | 61 * | 62 * v 63 * +-------+ +------+ +------+ 64 * | ODIR |<----->| ODIR |......| ODIR | 65 * +-------+ +------+ +------+ 66 * 67 * 68 * Odir State Machine 69 * ------------------ 70 * 71 * +-------------------------+ 72 * | SMB_ODIR_STATE_OPEN |<----------- open / creation 73 * +-------------------------+ 74 * | ^ 75 * | (first) | (last) 76 * | lookup | release 77 * v | 78 * +-------------------------+ 79 * | SMB_ODIR_STATE_IN_USE |---- 80 * +-------------------------+ | lookup / release / read 81 * | ^------- 82 * | close 83 * | 84 * v 85 * +-------------------------+ 86 * | SMB_ODIR_STATE_CLOSING |---- 87 * +-------------------------+ | close / release / read 88 * | ^------- 89 * | (last) release 90 * | 91 * v 92 * +-------------------------+ 93 * | SMB_ODIR_STATE_CLOSED |----------> deletion 94 * +-------------------------+ 95 * 96 * 97 * SMB_ODIR_STATE_OPEN 98 * - the odir exists in the list of odirs of its tree 99 * - lookup is valid in this state. It will place a hold on the odir 100 * by incrementing the reference count and the odir will transition 101 * to SMB_ODIR_STATE_IN_USE 102 * - read/close/release not valid in this state 103 * 104 * SMB_ODIR_STATE_IN_USE 105 * - the odir exists in the list of odirs of its tree. 106 * - lookup is valid in this state. It will place a hold on the odir 107 * by incrementing the reference count. 108 * - if the last hold is released the odir will transition 109 * back to SMB_ODIR_STATE_OPEN 110 * - if a close is received the odir will transition to 111 * SMB_ODIR_STATE_CLOSING. 112 * 113 * SMB_ODIR_STATE_CLOSING 114 * - the odir exists in the list of odirs of its tree. 115 * - lookup will fail in this state. 116 * - when the last hold is released the odir will transition 117 * to SMB_ODIR_STATE_CLOSED. 118 * 119 * SMB_ODIR_STATE_CLOSED 120 * - the odir exists in the list of odirs of its tree. 121 * - there are no users of the odir (refcnt == 0) 122 * - the odir is being removed from the tree's list and deleted. 123 * - lookup will fail in this state. 124 * - read/close/release not valid in this state 125 * 126 * Comments 127 * -------- 128 * The state machine of the odir structures is controlled by 3 elements: 129 * - The list of odirs of the tree it belongs to. 130 * - The mutex embedded in the structure itself. 131 * - The reference count. 132 * 133 * There's a mutex embedded in the odir structure used to protect its fields 134 * and there's a lock embedded in the list of odirs of a tree. To 135 * increment or to decrement the reference count the mutex must be entered. 136 * To insert the odir into the list of odirs of the tree and to remove 137 * the odir from it, the lock must be entered in RW_WRITER mode. 138 * 139 * In order to avoid deadlocks, when both (mutex and lock of the odir 140 * list) have to be entered, the lock must be entered first. 141 * 142 * 143 * Odir Interface 144 * --------------- 145 * odid = smb_odir_open(pathname) 146 * Create an odir representing the directory specified in pathname and 147 * add it into the tree's list of odirs. 148 * Return an identifier (odid) uniquely identifying the created odir. 149 * 150 * smb_odir_openat(smb_node_t *unode) 151 * Create an odir representing the extended attribute directory 152 * associated with the file (or directory) represented by unode 153 * and add it into the tree's list of odirs. 154 * Return an identifier (odid) uniquely identifying the created odir. 155 * 156 * smb_odir_t *odir = smb_tree_lookup_odir(odid) 157 * Find the odir corresponding to the specified odid in the tree's 158 * list of odirs. Place a hold on the odir. 159 * 160 * smb_odir_read(..., smb_odirent_t *odirent) 161 * Find the next directory entry in the odir and return it in odirent. 162 * 163 * smb_odir_read_fileinfo(..., smb_fileinfo_t *) 164 * Find the next directory entry in the odir. Return the details of 165 * the directory entry in smb_fileinfo_t. (See odir internals below) 166 * 167 * smb_odir_read_stream_info(..., smb_streaminfo_t *) 168 * Find the next named stream entry in the odir. Return the details of 169 * the named stream in smb_streaminfo_t. 170 * 171 * smb_odir_close(smb_odir_t *odir) 172 * Close the odir. 173 * The caller of close must have a hold on the odir being closed. 174 * The hold should be released after closing. 175 * 176 * smb_odir_release(smb_odir_t *odir) 177 * Release the hold on the odir, obtained by lookup. 178 * 179 * 180 * Odir Internals 181 * -------------- 182 * The odir object represent an open directory search. Each read operation 183 * provides the caller with a structure containing information pertaining 184 * to the next directory entry that matches the search criteria, namely 185 * the filename or match pattern and, in the case of smb_odir_read_fileinfo(), 186 * the search attributes. 187 * 188 * The odir maintains a buffer (d_buf) of directory entries read from 189 * the filesystem via a vop_readdir. The buffer is populated when a read 190 * request (smb_odir_next_odirent) finds that the buffer is empty or that 191 * the end of the buffer has been reached, and also when a new client request 192 * (find next) begins. 193 * 194 * The data in d_buf (that which is returned from the file system) can 195 * be in one of two formats. If the file system supports extended directory 196 * entries we request that the data be returned as edirent_t structures. If 197 * it does not the data will be returned as dirent64_t structures. For 198 * convenience, when the next directory entry is read from d_buf by 199 * smb_odir_next_odirent it is translated into an smb_odirent_t. 200 * 201 * smb_odir_read_fileinfo 202 * The processing required to obtain the information to populate the caller's 203 * smb_fileinfo_t differs depending upon whether the directory search is for a 204 * single specified filename or for multiple files matching a search pattern. 205 * Thus smb_odir_read_fileinfo uses two static functions: 206 * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single 207 * filename as specified in smb_odir_open request. 208 * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename 209 * returned from the smb_odir_next_odirent. This is called in a loop until 210 * an entry matching the search criteria is found or no more entries exist. 211 * 212 * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t 213 * is the name of the directory entry but the attributes are the attribites 214 * of the file that is the target of the link. If the link target cannot 215 * be found the attributes returned are the attributes of the link itself. 216 * 217 * smb_odir_read_stream_info 218 * In order for an odir to provide information about stream files it 219 * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can 220 * then be used to obtain the name and size of named stream files. 221 * 222 * Resuming a Search 223 * ----------------- 224 * A directory search often consists of multiple client requests: an initial 225 * find_first request followed by zero or more find_next requests and a 226 * find_close request. 227 * The find_first request will open and lookup the odir, read its desired 228 * number of entries from the odir, then release the odir and return. 229 * A find_next request will lookup the odir and read its desired number of 230 * entries from the odir, then release the odir and return. 231 * At the end of the search the find_close request will close the odir. 232 * 233 * In order to be able to resume a directory search (find_next) the odir 234 * provides the capability for the caller to save one or more resume points 235 * (cookies) at the end of a request, and to specify which resume point 236 * (cookie) to restart from at the beginning of the next search. 237 * smb_odir_save_cookie(..., cookie) 238 * smb_odir_resume_at(smb_odir_resume_t *resume) 239 * A search can be resumed at a specified resume point (cookie), the resume 240 * point (cookie) stored at a specified index in the d_cookies array, or 241 * a specified filename. The latter (specified filename) is not yet supported. 242 * 243 * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details 244 */ 245 246 #include <smbsrv/smb_incl.h> 247 #include <smbsrv/smb_kproto.h> 248 #include <smbsrv/smb_fsops.h> 249 #include <sys/extdirent.h> 250 251 /* static functions */ 252 static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *, 253 char *, uint16_t, cred_t *); 254 static void smb_odir_delete(smb_odir_t *); 255 static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *, 256 smb_fileinfo_t *); 257 static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *, 258 smb_odirent_t *, smb_fileinfo_t *); 259 static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *); 260 static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *, char *, 261 smb_node_t **, smb_attr_t *); 262 263 264 /* 265 * smb_odir_open 266 * 267 * Create an odir representing the directory specified in pathname. 268 * 269 * Returns: 270 * odid - Unique identifier of newly created odir. 271 * 0 - error, error details set in sr. 272 */ 273 uint16_t 274 smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr, uint32_t flags) 275 { 276 int rc; 277 smb_tree_t *tree; 278 smb_node_t *dnode; 279 char pattern[MAXNAMELEN]; 280 smb_odir_t *od; 281 cred_t *cr; 282 283 ASSERT(sr); 284 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 285 ASSERT(sr->tid_tree); 286 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 287 288 tree = sr->tid_tree; 289 290 rc = smb_pathname_reduce(sr, sr->user_cr, path, 291 tree->t_snode, tree->t_snode, &dnode, pattern); 292 if (rc != 0) { 293 smbsr_errno(sr, rc); 294 return (0); 295 } 296 297 if (dnode->vp->v_type != VDIR) { 298 smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND, 299 ERRDOS, ERROR_PATH_NOT_FOUND); 300 smb_node_release(dnode); 301 return (0); 302 } 303 304 if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) { 305 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 306 ERRDOS, ERROR_ACCESS_DENIED); 307 smb_node_release(dnode); 308 return (0); 309 } 310 311 if (flags & SMB_ODIR_OPENF_BACKUP_INTENT) 312 cr = smb_user_getprivcred(tree->t_user); 313 else 314 cr = tree->t_user->u_cred; 315 316 od = smb_odir_create(sr, dnode, pattern, sattr, cr); 317 smb_node_release(dnode); 318 return (od ? od->d_odid : 0); 319 } 320 321 /* 322 * smb_odir_openat 323 * 324 * Create an odir representing the extended attribute directory 325 * associated with the file (or directory) represented by unode. 326 * 327 * Returns: 328 * odid - Unique identifier of newly created odir. 329 * 0 - error, error details set in sr. 330 */ 331 uint16_t 332 smb_odir_openat(smb_request_t *sr, smb_node_t *unode) 333 { 334 int rc; 335 vnode_t *xattr_dvp; 336 smb_odir_t *od; 337 cred_t *cr; 338 char pattern[SMB_STREAM_PREFIX_LEN + 2]; 339 340 smb_node_t *xattr_dnode; 341 smb_attr_t tmp_attr; 342 343 ASSERT(sr); 344 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 345 ASSERT(unode); 346 ASSERT(unode->n_magic == SMB_NODE_MAGIC); 347 348 if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 || 349 SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) { 350 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 351 ERRDOS, ERROR_ACCESS_DENIED); 352 return (0); 353 } 354 cr = kcred; 355 356 /* find the xattrdir vnode */ 357 rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr); 358 if (rc != 0) { 359 smbsr_errno(sr, rc); 360 return (0); 361 } 362 363 /* lookup the xattrdir's smb_node */ 364 xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR, 365 unode, NULL, &tmp_attr); 366 VN_RELE(xattr_dvp); 367 if (xattr_dnode == NULL) { 368 smbsr_error(sr, NT_STATUS_NO_MEMORY, 369 ERRDOS, ERROR_NOT_ENOUGH_MEMORY); 370 return (0); 371 } 372 373 (void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX); 374 od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES, 375 cr); 376 smb_node_release(xattr_dnode); 377 if (od == NULL) 378 return (0); 379 380 od->d_flags |= SMB_ODIR_FLAG_XATTR; 381 return (od->d_odid); 382 } 383 384 /* 385 * smb_odir_hold 386 * 387 * A hold will only be granted if the odir is open or in_use. 388 */ 389 boolean_t 390 smb_odir_hold(smb_odir_t *od) 391 { 392 ASSERT(od); 393 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 394 395 mutex_enter(&od->d_mutex); 396 397 switch (od->d_state) { 398 case SMB_ODIR_STATE_OPEN: 399 od->d_refcnt++; 400 od->d_state = SMB_ODIR_STATE_IN_USE; 401 break; 402 case SMB_ODIR_STATE_IN_USE: 403 od->d_refcnt++; 404 break; 405 case SMB_ODIR_STATE_CLOSING: 406 case SMB_ODIR_STATE_CLOSED: 407 default: 408 mutex_exit(&od->d_mutex); 409 return (B_FALSE); 410 } 411 412 mutex_exit(&od->d_mutex); 413 return (B_TRUE); 414 } 415 416 /* 417 * smb_odir_release 418 * 419 * If the odir is in SMB_ODIR_STATE_CLOSING and this release 420 * results in a refcnt of 0, the odir may be removed from 421 * the tree's list of odirs and deleted. The odir's state is 422 * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and 423 * deleting the odir. 424 */ 425 void 426 smb_odir_release(smb_odir_t *od) 427 { 428 ASSERT(od); 429 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 430 431 mutex_enter(&od->d_mutex); 432 ASSERT(od->d_refcnt > 0); 433 434 switch (od->d_state) { 435 case SMB_ODIR_STATE_OPEN: 436 break; 437 case SMB_ODIR_STATE_IN_USE: 438 od->d_refcnt--; 439 if (od->d_refcnt == 0) 440 od->d_state = SMB_ODIR_STATE_OPEN; 441 break; 442 case SMB_ODIR_STATE_CLOSING: 443 od->d_refcnt--; 444 if (od->d_refcnt == 0) { 445 od->d_state = SMB_ODIR_STATE_CLOSED; 446 mutex_exit(&od->d_mutex); 447 smb_odir_delete(od); 448 return; 449 } 450 break; 451 case SMB_ODIR_STATE_CLOSED: 452 default: 453 break; 454 } 455 456 mutex_exit(&od->d_mutex); 457 } 458 459 /* 460 * smb_odir_close 461 */ 462 void 463 smb_odir_close(smb_odir_t *od) 464 { 465 ASSERT(od); 466 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 467 468 mutex_enter(&od->d_mutex); 469 ASSERT(od->d_refcnt > 0); 470 switch (od->d_state) { 471 case SMB_ODIR_STATE_OPEN: 472 break; 473 case SMB_ODIR_STATE_IN_USE: 474 od->d_state = SMB_ODIR_STATE_CLOSING; 475 break; 476 case SMB_ODIR_STATE_CLOSING: 477 case SMB_ODIR_STATE_CLOSED: 478 default: 479 break; 480 } 481 mutex_exit(&od->d_mutex); 482 } 483 484 /* 485 * smb_odir_read 486 * 487 * Find the next directory entry matching the search pattern. 488 * No search attribute matching is performed. 489 * 490 * Returns: 491 * 0 - success. 492 * - If a matching entry was found eof will be B_FALSE and 493 * odirent will be populated. 494 * - If there are no matching entries eof will be B_TRUE. 495 * -1 - error, error details set in sr. 496 */ 497 int 498 smb_odir_read(smb_request_t *sr, smb_odir_t *od, 499 smb_odirent_t *odirent, boolean_t *eof) 500 { 501 int rc; 502 boolean_t ignore_case; 503 504 ASSERT(sr); 505 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 506 ASSERT(od); 507 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 508 ASSERT(odirent); 509 510 mutex_enter(&od->d_mutex); 511 ASSERT(od->d_refcnt > 0); 512 513 switch (od->d_state) { 514 case SMB_ODIR_STATE_IN_USE: 515 case SMB_ODIR_STATE_CLOSING: 516 break; 517 case SMB_ODIR_STATE_OPEN: 518 case SMB_ODIR_STATE_CLOSED: 519 default: 520 mutex_exit(&od->d_mutex); 521 return (-1); 522 } 523 524 ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE); 525 526 for (;;) { 527 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 528 break; 529 if (smb_match_name(odirent->od_ino, odirent->od_name, 530 od->d_pattern, ignore_case)) 531 break; 532 } 533 534 mutex_exit(&od->d_mutex); 535 536 switch (rc) { 537 case 0: 538 *eof = B_FALSE; 539 return (0); 540 case ENOENT: 541 *eof = B_TRUE; 542 return (0); 543 default: 544 smbsr_errno(sr, rc); 545 return (-1); 546 } 547 } 548 549 /* 550 * smb_odir_read_fileinfo 551 * 552 * Find the next directory entry matching the search pattern 553 * and attributes: od->d_pattern and od->d_sattr. 554 * 555 * If the search pattern specifies a single filename call 556 * smb_odir_single_fileinfo to get the file attributes and 557 * populate the caller's smb_fileinfo_t. 558 * 559 * If the search pattern contains wildcards call smb_odir_next_odirent 560 * to get the next directory entry then. Repeat until a matching 561 * filename is found. Call smb_odir_wildcard_fileinfo to get the 562 * file attributes and populate the caller's smb_fileinfo_t. 563 * This is repeated until a file matching the search criteria is found. 564 * 565 * Returns: 566 * 0 - success. 567 * - If a matching entry was found eof will be B_FALSE and 568 * fileinfo will be populated. 569 * - If there are no matching entries eof will be B_TRUE. 570 * -1 - error, error details set in sr. 571 */ 572 int 573 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od, 574 smb_fileinfo_t *fileinfo, boolean_t *eof) 575 { 576 int rc; 577 smb_odirent_t *odirent; 578 boolean_t ignore_case; 579 580 ASSERT(sr); 581 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 582 ASSERT(od); 583 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 584 ASSERT(fileinfo); 585 586 mutex_enter(&od->d_mutex); 587 ASSERT(od->d_refcnt > 0); 588 589 switch (od->d_state) { 590 case SMB_ODIR_STATE_IN_USE: 591 case SMB_ODIR_STATE_CLOSING: 592 break; 593 case SMB_ODIR_STATE_OPEN: 594 case SMB_ODIR_STATE_CLOSED: 595 default: 596 mutex_exit(&od->d_mutex); 597 return (-1); 598 } 599 600 ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE); 601 602 if (!(od->d_flags & SMB_ODIR_FLAG_WILDCARDS)) { 603 if (od->d_eof) 604 rc = ENOENT; 605 else 606 rc = smb_odir_single_fileinfo(sr, od, fileinfo); 607 od->d_eof = B_TRUE; 608 } else { 609 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 610 for (;;) { 611 bzero(fileinfo, sizeof (smb_fileinfo_t)); 612 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 613 break; 614 615 if (!smb_match_name(odirent->od_ino, odirent->od_name, 616 od->d_pattern, ignore_case)) 617 continue; 618 619 rc = smb_odir_wildcard_fileinfo(sr, od, odirent, 620 fileinfo); 621 if (rc == 0) 622 break; 623 } 624 kmem_free(odirent, sizeof (smb_odirent_t)); 625 } 626 mutex_exit(&od->d_mutex); 627 628 switch (rc) { 629 case 0: 630 *eof = B_FALSE; 631 return (0); 632 case ENOENT: 633 *eof = B_TRUE; 634 return (0); 635 default: 636 smbsr_errno(sr, rc); 637 return (-1); 638 } 639 } 640 641 642 /* 643 * smb_odir_read_streaminfo 644 * 645 * Find the next directory entry whose name begins with SMB_STREAM_PREFIX, 646 * and thus represents an NTFS named stream. 647 * No search attribute matching is performed. 648 * No case conflict name mangling is required for NTFS named stream names. 649 * 650 * Returns: 651 * 0 - success. 652 * - If a matching entry was found eof will be B_FALSE and 653 * sinfo will be populated. 654 * - If there are no matching entries eof will be B_TRUE. 655 * -1 - error, error details set in sr. 656 */ 657 int 658 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od, 659 smb_streaminfo_t *sinfo, boolean_t *eof) 660 { 661 int rc; 662 smb_odirent_t *odirent; 663 vnode_t *vp; 664 smb_attr_t attr; 665 int tmpflg; 666 667 ASSERT(sr); 668 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 669 ASSERT(od); 670 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 671 ASSERT(sinfo); 672 673 mutex_enter(&od->d_mutex); 674 ASSERT(od->d_refcnt > 0); 675 676 switch (od->d_state) { 677 case SMB_ODIR_STATE_IN_USE: 678 case SMB_ODIR_STATE_CLOSING: 679 break; 680 case SMB_ODIR_STATE_OPEN: 681 case SMB_ODIR_STATE_CLOSED: 682 default: 683 mutex_exit(&od->d_mutex); 684 return (-1); 685 } 686 687 /* Check that odir represents an xattr directory */ 688 if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) { 689 *eof = B_TRUE; 690 mutex_exit(&od->d_mutex); 691 return (0); 692 } 693 694 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 695 696 for (;;) { 697 bzero(sinfo, sizeof (smb_streaminfo_t)); 698 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 699 break; 700 701 if (strncmp(odirent->od_name, SMB_STREAM_PREFIX, 702 SMB_STREAM_PREFIX_LEN)) { 703 continue; 704 } 705 706 /* 707 * since we only care about the size attribute we don't need to 708 * pass the vp of the unnamed stream file to smb_vop_getattr 709 */ 710 rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &vp, 711 NULL, 0, &tmpflg, od->d_tree->t_snode->vp, od->d_cred); 712 if (rc == 0) { 713 rc = smb_vop_getattr(vp, NULL, &attr, 0, od->d_cred); 714 VN_RELE(vp); 715 } 716 717 if (rc == 0) { 718 (void) strlcpy(sinfo->si_name, 719 odirent->od_name + SMB_STREAM_PREFIX_LEN, 720 sizeof (sinfo->si_name)); 721 sinfo->si_size = attr.sa_vattr.va_size; 722 break; 723 } 724 } 725 mutex_exit(&od->d_mutex); 726 727 kmem_free(odirent, sizeof (smb_odirent_t)); 728 729 switch (rc) { 730 case 0: 731 *eof = B_FALSE; 732 return (0); 733 case ENOENT: 734 *eof = B_TRUE; 735 return (0); 736 default: 737 smbsr_errno(sr, rc); 738 return (-1); 739 } 740 } 741 742 /* 743 * smb_odir_save_cookie 744 * 745 * Callers can save up to SMB_MAX_SEARCH cookies in the odir 746 * to be used as resume points for a 'find next' request. 747 */ 748 void 749 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie) 750 { 751 ASSERT(od); 752 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 753 ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH); 754 755 mutex_enter(&od->d_mutex); 756 od->d_cookies[idx] = cookie; 757 mutex_exit(&od->d_mutex); 758 } 759 760 /* 761 * smb_odir_resume_at 762 * 763 * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single 764 * file and should not be resumed. 765 * 766 * Wildcard searching can be resumed from: 767 * - the cookie saved at a specified index (SMBsearch, SMBfind). 768 * - a specified cookie (SMB_trans2_find) 769 * - a specified filename (SMB_trans2_find) - NOT SUPPORTED. 770 * Defaults to continuing from where the last search ended. 771 * 772 * Continuation from where the last search ended (SMB_trans2_find) 773 * is implemented by saving the last cookie at a specific index (0) 774 * smb_odir_resume_at indicates a new request, so reset od->d_bufptr 775 * and d_eof to force a vop_readdir. 776 */ 777 void 778 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume) 779 { 780 ASSERT(od); 781 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 782 ASSERT(resume); 783 784 mutex_enter(&od->d_mutex); 785 786 if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) { 787 od->d_eof = B_TRUE; 788 mutex_exit(&od->d_mutex); 789 return; 790 } 791 792 switch (resume->or_type) { 793 case SMB_ODIR_RESUME_IDX: 794 ASSERT(resume->or_idx >= 0); 795 ASSERT(resume->or_idx < SMB_MAX_SEARCH); 796 797 if ((resume->or_idx < 0) || 798 (resume->or_idx >= SMB_MAX_SEARCH)) { 799 resume->or_idx = 0; 800 } 801 od->d_offset = od->d_cookies[resume->or_idx]; 802 break; 803 case SMB_ODIR_RESUME_COOKIE: 804 od->d_offset = resume->or_cookie; 805 break; 806 case SMB_ODIR_RESUME_FNAME: 807 default: 808 od->d_offset = od->d_cookies[0]; 809 break; 810 } 811 812 /* Force a vop_readdir to refresh d_buf */ 813 od->d_bufptr = NULL; 814 od->d_eof = B_FALSE; 815 816 mutex_exit(&od->d_mutex); 817 } 818 819 820 /* *** static functions *** */ 821 822 /* 823 * smb_odir_create 824 * Allocate and populate an odir obect and add it to the tree's list. 825 */ 826 static smb_odir_t * 827 smb_odir_create(smb_request_t *sr, smb_node_t *dnode, 828 char *pattern, uint16_t sattr, cred_t *cr) 829 { 830 smb_odir_t *od; 831 smb_tree_t *tree; 832 uint16_t odid; 833 834 ASSERT(sr); 835 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 836 ASSERT(sr->tid_tree); 837 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 838 ASSERT(dnode); 839 ASSERT(dnode->n_magic == SMB_NODE_MAGIC); 840 841 tree = sr->tid_tree; 842 843 if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) { 844 smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES, 845 ERRDOS, ERROR_TOO_MANY_OPEN_FILES); 846 return (NULL); 847 } 848 849 od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP); 850 bzero(od, sizeof (smb_odir_t)); 851 852 mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL); 853 od->d_refcnt = 0; 854 od->d_state = SMB_ODIR_STATE_OPEN; 855 od->d_magic = SMB_ODIR_MAGIC; 856 od->d_opened_by_pid = sr->smb_pid; 857 od->d_session = tree->t_session; 858 od->d_cred = cr; 859 od->d_tree = tree; 860 od->d_dnode = dnode; 861 smb_node_ref(dnode); 862 od->d_odid = odid; 863 od->d_sattr = sattr; 864 (void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern)); 865 od->d_flags = 0; 866 if (smb_convert_wildcards(od->d_pattern) != 0) 867 od->d_flags |= SMB_ODIR_FLAG_WILDCARDS; 868 if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS)) 869 od->d_flags |= SMB_ODIR_FLAG_EDIRENT; 870 if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE)) 871 od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE; 872 if (SMB_TREE_SUPPORTS_CATIA(sr)) 873 od->d_flags |= SMB_ODIR_FLAG_CATIA; 874 od->d_eof = B_FALSE; 875 876 smb_llist_enter(&tree->t_odir_list, RW_WRITER); 877 smb_llist_insert_tail(&tree->t_odir_list, od); 878 smb_llist_exit(&tree->t_odir_list); 879 880 atomic_inc_32(&tree->t_session->s_dir_cnt); 881 return (od); 882 } 883 884 /* 885 * smb_odir_delete 886 * 887 * Removal of the odir from the tree's list of odirs must be 888 * done before any resources associated with the odir are 889 * released. 890 */ 891 static void 892 smb_odir_delete(smb_odir_t *od) 893 { 894 ASSERT(od); 895 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 896 ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED); 897 898 smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER); 899 smb_llist_remove(&od->d_tree->t_odir_list, od); 900 smb_llist_exit(&od->d_tree->t_odir_list); 901 902 od->d_magic = 0; 903 atomic_dec_32(&od->d_tree->t_session->s_dir_cnt); 904 smb_node_release(od->d_dnode); 905 smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid); 906 mutex_destroy(&od->d_mutex); 907 kmem_cache_free(od->d_tree->t_server->si_cache_odir, od); 908 } 909 910 /* 911 * smb_odir_next_odirent 912 * 913 * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer 914 * is empty or we've reached the end of it), read the next set of 915 * entries from the file system (vop_readdir). 916 * 917 * File systems which support VFSFT_EDIRENT_FLAGS will return the 918 * directory entries as a buffer of edirent_t structure. Others will 919 * return a buffer of dirent64_t structures. For simplicity translate 920 * the data into an smb_odirent_t structure. 921 * The ed_name/d_name in d_buf is NULL terminated by the file system. 922 * 923 * Some file systems can have directories larger than SMB_MAXDIRSIZE. 924 * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof 925 * to true to stop subsequent calls to smb_vop_readdir. 926 * 927 * Returns: 928 * 0 - success. odirent is populated with the next directory entry 929 * ENOENT - no more directory entries 930 * errno - error 931 */ 932 static int 933 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent) 934 { 935 int rc; 936 int reclen; 937 int eof; 938 dirent64_t *dp; 939 edirent_t *edp; 940 char *np; 941 942 ASSERT(MUTEX_HELD(&od->d_mutex)); 943 944 bzero(odirent, sizeof (smb_odirent_t)); 945 946 if (od->d_bufptr != NULL) { 947 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 948 reclen = od->d_edp->ed_reclen; 949 else 950 reclen = od->d_dp->d_reclen; 951 952 if (reclen == 0) { 953 od->d_bufptr = NULL; 954 } else { 955 od->d_bufptr += reclen; 956 if (od->d_bufptr >= od->d_buf + od->d_bufsize) 957 od->d_bufptr = NULL; 958 } 959 } 960 961 if (od->d_bufptr == NULL) { 962 if (od->d_eof) 963 return (ENOENT); 964 965 od->d_bufsize = sizeof (od->d_buf); 966 967 rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset, 968 od->d_buf, &od->d_bufsize, &eof, od->d_cred); 969 970 if ((rc == 0) && (od->d_bufsize == 0)) 971 rc = ENOENT; 972 973 if (rc != 0) { 974 od->d_bufptr = NULL; 975 od->d_bufsize = 0; 976 return (rc); 977 } 978 979 od->d_eof = (eof != 0); 980 od->d_bufptr = od->d_buf; 981 } 982 983 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 984 od->d_offset = od->d_edp->ed_off; 985 else 986 od->d_offset = od->d_dp->d_off; 987 988 if (od->d_offset >= SMB_MAXDIRSIZE) { 989 od->d_bufptr = NULL; 990 od->d_bufsize = 0; 991 od->d_eof = B_TRUE; 992 return (ENOENT); 993 } 994 995 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) { 996 edp = od->d_edp; 997 odirent->od_ino = edp->ed_ino; 998 odirent->od_eflags = edp->ed_eflags; 999 np = edp->ed_name; 1000 } else { 1001 dp = od->d_dp; 1002 odirent->od_ino = dp->d_ino; 1003 odirent->od_eflags = 0; 1004 np = dp->d_name; 1005 } 1006 1007 if ((od->d_flags & SMB_ODIR_FLAG_CATIA) && 1008 ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) { 1009 smb_vop_catia_v4tov5(np, odirent->od_name, 1010 sizeof (odirent->od_name)); 1011 } else { 1012 (void) strlcpy(odirent->od_name, np, 1013 sizeof (odirent->od_name)); 1014 } 1015 1016 return (0); 1017 } 1018 1019 /* 1020 * smb_odir_single_fileinfo 1021 * 1022 * Lookup the file identified by od->d_pattern. 1023 * 1024 * If the looked up file is a link, we attempt to lookup the link target 1025 * to use its attributes in place of those of the files's. 1026 * If we fail to lookup the target of the link we use the original 1027 * file's attributes. 1028 * Check if the attributes match the search attributes. 1029 * 1030 * Returns: 0 - success 1031 * ENOENT - no match 1032 * errno - error 1033 */ 1034 static int 1035 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od, 1036 smb_fileinfo_t *fileinfo) 1037 { 1038 int rc; 1039 smb_node_t *fnode, *tgt_node; 1040 smb_attr_t attr, tgt_attr, *fattr; 1041 ino64_t ino; 1042 char *name; 1043 uint32_t dosattr; 1044 boolean_t case_conflict = B_FALSE; 1045 int lookup_flags, flags = 0; 1046 vnode_t *vp; 1047 1048 ASSERT(sr); 1049 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1050 ASSERT(od); 1051 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1052 1053 ASSERT(MUTEX_HELD(&od->d_mutex)); 1054 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1055 1056 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1057 od->d_dnode, od->d_pattern, &fnode, &attr); 1058 if (rc != 0) 1059 return (rc); 1060 1061 /* 1062 * If case sensitive, do a case insensitive smb_vop_lookup to 1063 * check for case conflict 1064 */ 1065 if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) { 1066 lookup_flags = SMB_IGNORE_CASE; 1067 if (od->d_flags & SMB_ODIR_FLAG_CATIA) 1068 lookup_flags |= SMB_CATIA; 1069 1070 rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp, 1071 NULL, lookup_flags, &flags, od->d_tree->t_snode->vp, 1072 od->d_cred); 1073 if (rc != 0) 1074 return (rc); 1075 VN_RELE(vp); 1076 1077 if (flags & ED_CASE_CONFLICT) 1078 case_conflict = B_TRUE; 1079 } 1080 1081 ino = attr.sa_vattr.va_nodeid; 1082 (void) smb_mangle_name(ino, fnode->od_name, 1083 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1084 name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name; 1085 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1086 1087 /* follow link to get target node & attr */ 1088 if ((fnode->vp->v_type == VLNK) && 1089 (smb_odir_lookup_link(sr, od, fnode->od_name, 1090 &tgt_node, &tgt_attr))) { 1091 smb_node_release(fnode); 1092 fnode = tgt_node; 1093 fattr = &tgt_attr; 1094 } else { 1095 fattr = &attr; 1096 } 1097 1098 /* check search attributes */ 1099 dosattr = smb_node_get_dosattr(fnode); 1100 if (!smb_sattr_check(dosattr, od->d_sattr)) { 1101 smb_node_release(fnode); 1102 return (ENOENT); 1103 } 1104 1105 fileinfo->fi_dosattr = dosattr; 1106 fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid; 1107 fileinfo->fi_size = smb_node_get_size(fnode, fattr); 1108 fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE; 1109 fileinfo->fi_atime = fattr->sa_vattr.va_atime; 1110 fileinfo->fi_mtime = fattr->sa_vattr.va_mtime; 1111 fileinfo->fi_ctime = fattr->sa_vattr.va_ctime; 1112 if (fattr->sa_crtime.tv_sec) 1113 fileinfo->fi_crtime = fattr->sa_crtime; 1114 else 1115 fileinfo->fi_crtime = fattr->sa_vattr.va_mtime; 1116 1117 smb_node_release(fnode); 1118 return (0); 1119 } 1120 1121 /* 1122 * smb_odir_wildcard_fileinfo 1123 * 1124 * odirent contains a directory entry, obtained from a vop_readdir. 1125 * If a case conflict is identified the filename is mangled and the 1126 * shortname is used as 'name', in place of odirent->od_name. This 1127 * name will be used in the smb_fsop_lookup because smb_fsop_lookup 1128 * performs a case insensitive lookup if the tree is case insesitive, 1129 * so the mangled name is required in the case conflict scenario to 1130 * ensure the correct match. 1131 * 1132 * If the looked up file is a link, we attempt to lookup the link target 1133 * to use its attributes in place of those of the files's. 1134 * If we fail to lookup the target of the link we use the original 1135 * file's attributes. 1136 * Check if the attributes match the search attributes. 1137 * 1138 * Although some file systems can have directories larger than 1139 * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger 1140 * than SMB_MAXDIRSIZE is returned. It is therefore safe to use the 1141 * offset as the cookie (uint32_t). 1142 * 1143 * Returns: 0 - success 1144 * ENOENT - no match, proceed to next entry 1145 * errno - error 1146 */ 1147 static int 1148 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od, 1149 smb_odirent_t *odirent, smb_fileinfo_t *fileinfo) 1150 { 1151 int rc; 1152 smb_node_t *fnode, *tgt_node; 1153 smb_attr_t attr, tgt_attr, *fattr; 1154 char *name; 1155 uint32_t dosattr; 1156 boolean_t case_conflict; 1157 1158 ASSERT(sr); 1159 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1160 ASSERT(od); 1161 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1162 1163 ASSERT(MUTEX_HELD(&od->d_mutex)); 1164 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1165 1166 case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) && 1167 (odirent->od_eflags & ED_CASE_CONFLICT)); 1168 (void) smb_mangle_name(odirent->od_ino, odirent->od_name, 1169 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1170 name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name; 1171 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1172 1173 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1174 od->d_dnode, name, &fnode, &attr); 1175 if (rc != 0) 1176 return (rc); 1177 1178 /* follow link to get target node & attr */ 1179 if ((fnode->vp->v_type == VLNK) && 1180 (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) { 1181 smb_node_release(fnode); 1182 fnode = tgt_node; 1183 fattr = &tgt_attr; 1184 } else { 1185 fattr = &attr; 1186 } 1187 1188 /* check search attributes */ 1189 dosattr = smb_node_get_dosattr(fnode); 1190 if (!smb_sattr_check(dosattr, od->d_sattr)) { 1191 smb_node_release(fnode); 1192 return (ENOENT); 1193 } 1194 1195 fileinfo->fi_cookie = (uint32_t)od->d_offset; 1196 fileinfo->fi_dosattr = dosattr; 1197 fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid; 1198 fileinfo->fi_size = smb_node_get_size(fnode, fattr); 1199 fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE; 1200 fileinfo->fi_atime = fattr->sa_vattr.va_atime; 1201 fileinfo->fi_mtime = fattr->sa_vattr.va_mtime; 1202 fileinfo->fi_ctime = fattr->sa_vattr.va_ctime; 1203 if (fattr->sa_crtime.tv_sec) 1204 fileinfo->fi_crtime = fattr->sa_crtime; 1205 else 1206 fileinfo->fi_crtime = fattr->sa_vattr.va_mtime; 1207 1208 smb_node_release(fnode); 1209 return (0); 1210 } 1211 1212 /* 1213 * smb_odir_lookup_link 1214 * 1215 * If the file is a symlink we lookup the object to which the 1216 * symlink refers so that we can return its attributes. 1217 * This can cause a problem if a symlink in a sub-directory 1218 * points to a parent directory (some UNIX GUI's create a symlink 1219 * in $HOME/.desktop that points to the user's home directory). 1220 * Some Windows applications (e.g. virus scanning) loop/hang 1221 * trying to follow this recursive path and there is little 1222 * we can do because the path is constructed on the client. 1223 * smb_dirsymlink_enable allows an end-user to disable 1224 * symlinks to directories. Symlinks to other object types 1225 * should be unaffected. 1226 * 1227 * Returns: B_TRUE - followed link. tgt_node and tgt_attr set 1228 * B_FALSE - link not followed 1229 */ 1230 static boolean_t 1231 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od, 1232 char *fname, smb_node_t **tgt_node, smb_attr_t *tgt_attr) 1233 { 1234 int rc; 1235 1236 rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS, 1237 od->d_tree->t_snode, od->d_dnode, fname, tgt_node, tgt_attr); 1238 if (rc != 0) { 1239 *tgt_node = NULL; 1240 return (B_FALSE); 1241 } 1242 1243 if ((tgt_attr->sa_vattr.va_type == VDIR) && (!smb_dirsymlink_enable)) { 1244 smb_node_release(*tgt_node); 1245 *tgt_node = NULL; 1246 return (B_FALSE); 1247 } 1248 1249 return (B_TRUE); 1250 } 1251