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_streaminfo(..., 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_streaminfo 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 <smbsrv/smb_share.h> 250 #include <sys/extdirent.h> 251 252 /* static functions */ 253 static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *, 254 char *, uint16_t, cred_t *); 255 static void smb_odir_delete(smb_odir_t *); 256 static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *, 257 smb_fileinfo_t *); 258 static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *, 259 smb_odirent_t *, smb_fileinfo_t *); 260 static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *); 261 static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *, 262 char *, smb_node_t **); 263 264 265 /* 266 * smb_odir_open 267 * 268 * Create an odir representing the directory specified in pathname. 269 * 270 * Returns: 271 * odid - Unique identifier of newly created odir. 272 * 0 - error, error details set in sr. 273 */ 274 uint16_t 275 smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr, uint32_t flags) 276 { 277 int rc; 278 smb_tree_t *tree; 279 smb_node_t *dnode; 280 char pattern[MAXNAMELEN]; 281 smb_odir_t *od; 282 cred_t *cr; 283 284 ASSERT(sr); 285 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 286 ASSERT(sr->tid_tree); 287 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 288 289 tree = sr->tid_tree; 290 291 rc = smb_pathname_reduce(sr, sr->user_cr, path, 292 tree->t_snode, tree->t_snode, &dnode, pattern); 293 if (rc != 0) { 294 smbsr_errno(sr, rc); 295 return (0); 296 } 297 298 if (dnode->vp->v_type != VDIR) { 299 smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND, 300 ERRDOS, ERROR_PATH_NOT_FOUND); 301 smb_node_release(dnode); 302 return (0); 303 } 304 305 if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) { 306 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 307 ERRDOS, ERROR_ACCESS_DENIED); 308 smb_node_release(dnode); 309 return (0); 310 } 311 312 if (flags & SMB_ODIR_OPENF_BACKUP_INTENT) 313 cr = smb_user_getprivcred(tree->t_user); 314 else 315 cr = tree->t_user->u_cred; 316 317 od = smb_odir_create(sr, dnode, pattern, sattr, cr); 318 smb_node_release(dnode); 319 return (od ? od->d_odid : 0); 320 } 321 322 /* 323 * smb_odir_openat 324 * 325 * Create an odir representing the extended attribute directory 326 * associated with the file (or directory) represented by unode. 327 * 328 * Returns: 329 * odid - Unique identifier of newly created odir. 330 * 0 - error, error details set in sr. 331 */ 332 uint16_t 333 smb_odir_openat(smb_request_t *sr, smb_node_t *unode) 334 { 335 int rc; 336 vnode_t *xattr_dvp; 337 smb_odir_t *od; 338 cred_t *cr; 339 char pattern[SMB_STREAM_PREFIX_LEN + 2]; 340 341 smb_node_t *xattr_dnode; 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); 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 smb_node_t *fnode; 664 smb_attr_t attr; 665 666 ASSERT(sr); 667 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 668 ASSERT(od); 669 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 670 ASSERT(sinfo); 671 672 mutex_enter(&od->d_mutex); 673 ASSERT(od->d_refcnt > 0); 674 675 switch (od->d_state) { 676 case SMB_ODIR_STATE_IN_USE: 677 case SMB_ODIR_STATE_CLOSING: 678 break; 679 case SMB_ODIR_STATE_OPEN: 680 case SMB_ODIR_STATE_CLOSED: 681 default: 682 mutex_exit(&od->d_mutex); 683 return (-1); 684 } 685 686 /* Check that odir represents an xattr directory */ 687 if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) { 688 *eof = B_TRUE; 689 mutex_exit(&od->d_mutex); 690 return (0); 691 } 692 693 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 694 695 for (;;) { 696 bzero(sinfo, sizeof (smb_streaminfo_t)); 697 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 698 break; 699 700 if (strncmp(odirent->od_name, SMB_STREAM_PREFIX, 701 SMB_STREAM_PREFIX_LEN)) { 702 continue; 703 } 704 705 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 706 od->d_dnode, odirent->od_name, &fnode); 707 if (rc == 0) { 708 rc = smb_node_getattr(sr, fnode, &attr); 709 smb_node_release(fnode); 710 } 711 712 if (rc == 0) { 713 (void) strlcpy(sinfo->si_name, 714 odirent->od_name + SMB_STREAM_PREFIX_LEN, 715 sizeof (sinfo->si_name)); 716 sinfo->si_size = attr.sa_vattr.va_size; 717 sinfo->si_alloc_size = attr.sa_allocsz; 718 break; 719 } 720 } 721 mutex_exit(&od->d_mutex); 722 723 kmem_free(odirent, sizeof (smb_odirent_t)); 724 725 switch (rc) { 726 case 0: 727 *eof = B_FALSE; 728 return (0); 729 case ENOENT: 730 *eof = B_TRUE; 731 return (0); 732 default: 733 smbsr_errno(sr, rc); 734 return (-1); 735 } 736 } 737 738 /* 739 * smb_odir_save_cookie 740 * 741 * Callers can save up to SMB_MAX_SEARCH cookies in the odir 742 * to be used as resume points for a 'find next' request. 743 */ 744 void 745 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie) 746 { 747 ASSERT(od); 748 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 749 ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH); 750 751 mutex_enter(&od->d_mutex); 752 od->d_cookies[idx] = cookie; 753 mutex_exit(&od->d_mutex); 754 } 755 756 /* 757 * smb_odir_resume_at 758 * 759 * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single 760 * file and should not be resumed. 761 * 762 * Wildcard searching can be resumed from: 763 * - the cookie saved at a specified index (SMBsearch, SMBfind). 764 * - a specified cookie (SMB_trans2_find) 765 * - a specified filename (SMB_trans2_find) - NOT SUPPORTED. 766 * Defaults to continuing from where the last search ended. 767 * 768 * Continuation from where the last search ended (SMB_trans2_find) 769 * is implemented by saving the last cookie at a specific index (0) 770 * smb_odir_resume_at indicates a new request, so reset od->d_bufptr 771 * and d_eof to force a vop_readdir. 772 */ 773 void 774 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume) 775 { 776 ASSERT(od); 777 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 778 ASSERT(resume); 779 780 mutex_enter(&od->d_mutex); 781 782 if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) { 783 od->d_eof = B_TRUE; 784 mutex_exit(&od->d_mutex); 785 return; 786 } 787 788 switch (resume->or_type) { 789 case SMB_ODIR_RESUME_IDX: 790 ASSERT(resume->or_idx >= 0); 791 ASSERT(resume->or_idx < SMB_MAX_SEARCH); 792 793 if ((resume->or_idx < 0) || 794 (resume->or_idx >= SMB_MAX_SEARCH)) { 795 resume->or_idx = 0; 796 } 797 od->d_offset = od->d_cookies[resume->or_idx]; 798 break; 799 case SMB_ODIR_RESUME_COOKIE: 800 od->d_offset = resume->or_cookie; 801 break; 802 case SMB_ODIR_RESUME_FNAME: 803 default: 804 od->d_offset = od->d_cookies[0]; 805 break; 806 } 807 808 /* Force a vop_readdir to refresh d_buf */ 809 od->d_bufptr = NULL; 810 od->d_eof = B_FALSE; 811 812 mutex_exit(&od->d_mutex); 813 } 814 815 816 /* *** static functions *** */ 817 818 /* 819 * smb_odir_create 820 * Allocate and populate an odir obect and add it to the tree's list. 821 */ 822 static smb_odir_t * 823 smb_odir_create(smb_request_t *sr, smb_node_t *dnode, 824 char *pattern, uint16_t sattr, cred_t *cr) 825 { 826 smb_odir_t *od; 827 smb_tree_t *tree; 828 uint16_t odid; 829 830 ASSERT(sr); 831 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 832 ASSERT(sr->tid_tree); 833 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 834 ASSERT(dnode); 835 ASSERT(dnode->n_magic == SMB_NODE_MAGIC); 836 837 tree = sr->tid_tree; 838 839 if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) { 840 smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES, 841 ERRDOS, ERROR_TOO_MANY_OPEN_FILES); 842 return (NULL); 843 } 844 845 od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP); 846 bzero(od, sizeof (smb_odir_t)); 847 848 mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL); 849 od->d_refcnt = 0; 850 od->d_state = SMB_ODIR_STATE_OPEN; 851 od->d_magic = SMB_ODIR_MAGIC; 852 od->d_opened_by_pid = sr->smb_pid; 853 od->d_session = tree->t_session; 854 od->d_cred = cr; 855 od->d_tree = tree; 856 od->d_dnode = dnode; 857 smb_node_ref(dnode); 858 od->d_odid = odid; 859 od->d_sattr = sattr; 860 (void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern)); 861 od->d_flags = 0; 862 if (smb_convert_wildcards(od->d_pattern) != 0) 863 od->d_flags |= SMB_ODIR_FLAG_WILDCARDS; 864 if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS)) 865 od->d_flags |= SMB_ODIR_FLAG_EDIRENT; 866 if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE)) 867 od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE; 868 if (SMB_TREE_SUPPORTS_CATIA(sr)) 869 od->d_flags |= SMB_ODIR_FLAG_CATIA; 870 if (SMB_TREE_SUPPORTS_ABE(sr)) 871 od->d_flags |= SMB_ODIR_FLAG_ABE; 872 od->d_eof = B_FALSE; 873 874 smb_llist_enter(&tree->t_odir_list, RW_WRITER); 875 smb_llist_insert_tail(&tree->t_odir_list, od); 876 smb_llist_exit(&tree->t_odir_list); 877 878 atomic_inc_32(&tree->t_session->s_dir_cnt); 879 return (od); 880 } 881 882 /* 883 * smb_odir_delete 884 * 885 * Removal of the odir from the tree's list of odirs must be 886 * done before any resources associated with the odir are 887 * released. 888 */ 889 static void 890 smb_odir_delete(smb_odir_t *od) 891 { 892 ASSERT(od); 893 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 894 ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED); 895 896 smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER); 897 smb_llist_remove(&od->d_tree->t_odir_list, od); 898 smb_llist_exit(&od->d_tree->t_odir_list); 899 900 od->d_magic = 0; 901 atomic_dec_32(&od->d_tree->t_session->s_dir_cnt); 902 smb_node_release(od->d_dnode); 903 smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid); 904 mutex_destroy(&od->d_mutex); 905 kmem_cache_free(od->d_tree->t_server->si_cache_odir, od); 906 } 907 908 /* 909 * smb_odir_next_odirent 910 * 911 * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer 912 * is empty or we've reached the end of it), read the next set of 913 * entries from the file system (vop_readdir). 914 * 915 * File systems which support VFSFT_EDIRENT_FLAGS will return the 916 * directory entries as a buffer of edirent_t structure. Others will 917 * return a buffer of dirent64_t structures. For simplicity translate 918 * the data into an smb_odirent_t structure. 919 * The ed_name/d_name in d_buf is NULL terminated by the file system. 920 * 921 * Some file systems can have directories larger than SMB_MAXDIRSIZE. 922 * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof 923 * to true to stop subsequent calls to smb_vop_readdir. 924 * 925 * Returns: 926 * 0 - success. odirent is populated with the next directory entry 927 * ENOENT - no more directory entries 928 * errno - error 929 */ 930 static int 931 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent) 932 { 933 int rc; 934 int reclen; 935 int eof; 936 dirent64_t *dp; 937 edirent_t *edp; 938 char *np; 939 uint32_t abe_flag = 0; 940 941 ASSERT(MUTEX_HELD(&od->d_mutex)); 942 943 bzero(odirent, sizeof (smb_odirent_t)); 944 945 if (od->d_bufptr != NULL) { 946 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 947 reclen = od->d_edp->ed_reclen; 948 else 949 reclen = od->d_dp->d_reclen; 950 951 if (reclen == 0) { 952 od->d_bufptr = NULL; 953 } else { 954 od->d_bufptr += reclen; 955 if (od->d_bufptr >= od->d_buf + od->d_bufsize) 956 od->d_bufptr = NULL; 957 } 958 } 959 960 if (od->d_bufptr == NULL) { 961 if (od->d_eof) 962 return (ENOENT); 963 964 od->d_bufsize = sizeof (od->d_buf); 965 966 if (od->d_flags & SMB_ODIR_FLAG_ABE) 967 abe_flag = SMB_ABE; 968 969 rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset, 970 od->d_buf, &od->d_bufsize, &eof, abe_flag, od->d_cred); 971 972 if ((rc == 0) && (od->d_bufsize == 0)) 973 rc = ENOENT; 974 975 if (rc != 0) { 976 od->d_bufptr = NULL; 977 od->d_bufsize = 0; 978 return (rc); 979 } 980 981 od->d_eof = (eof != 0); 982 od->d_bufptr = od->d_buf; 983 } 984 985 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 986 od->d_offset = od->d_edp->ed_off; 987 else 988 od->d_offset = od->d_dp->d_off; 989 990 if (od->d_offset >= SMB_MAXDIRSIZE) { 991 od->d_bufptr = NULL; 992 od->d_bufsize = 0; 993 od->d_eof = B_TRUE; 994 return (ENOENT); 995 } 996 997 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) { 998 edp = od->d_edp; 999 odirent->od_ino = edp->ed_ino; 1000 odirent->od_eflags = edp->ed_eflags; 1001 np = edp->ed_name; 1002 } else { 1003 dp = od->d_dp; 1004 odirent->od_ino = dp->d_ino; 1005 odirent->od_eflags = 0; 1006 np = dp->d_name; 1007 } 1008 1009 if ((od->d_flags & SMB_ODIR_FLAG_CATIA) && 1010 ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) { 1011 smb_vop_catia_v4tov5(np, odirent->od_name, 1012 sizeof (odirent->od_name)); 1013 } else { 1014 (void) strlcpy(odirent->od_name, np, 1015 sizeof (odirent->od_name)); 1016 } 1017 1018 return (0); 1019 } 1020 1021 /* 1022 * smb_odir_single_fileinfo 1023 * 1024 * Lookup the file identified by od->d_pattern. 1025 * 1026 * If the looked up file is a link, we attempt to lookup the link target 1027 * to use its attributes in place of those of the files's. 1028 * If we fail to lookup the target of the link we use the original 1029 * file's attributes. 1030 * Check if the attributes match the search attributes. 1031 * 1032 * Returns: 0 - success 1033 * ENOENT - no match 1034 * errno - error 1035 */ 1036 static int 1037 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od, 1038 smb_fileinfo_t *fileinfo) 1039 { 1040 int rc; 1041 smb_node_t *fnode, *tgt_node; 1042 smb_attr_t attr; 1043 ino64_t ino; 1044 char *name; 1045 boolean_t case_conflict = B_FALSE; 1046 int lookup_flags, flags = 0; 1047 vnode_t *vp; 1048 1049 ASSERT(sr); 1050 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1051 ASSERT(od); 1052 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1053 1054 ASSERT(MUTEX_HELD(&od->d_mutex)); 1055 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1056 1057 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1058 od->d_dnode, od->d_pattern, &fnode); 1059 if (rc != 0) 1060 return (rc); 1061 1062 /* 1063 * If case sensitive, do a case insensitive smb_vop_lookup to 1064 * check for case conflict 1065 */ 1066 if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) { 1067 lookup_flags = SMB_IGNORE_CASE; 1068 if (od->d_flags & SMB_ODIR_FLAG_CATIA) 1069 lookup_flags |= SMB_CATIA; 1070 1071 rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp, 1072 NULL, lookup_flags, &flags, od->d_tree->t_snode->vp, 1073 od->d_cred); 1074 if (rc != 0) 1075 return (rc); 1076 VN_RELE(vp); 1077 1078 if (flags & ED_CASE_CONFLICT) 1079 case_conflict = B_TRUE; 1080 } 1081 1082 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1083 smb_node_release(fnode); 1084 return (rc); 1085 } 1086 1087 ino = attr.sa_vattr.va_nodeid; 1088 (void) smb_mangle_name(ino, fnode->od_name, 1089 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1090 name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name; 1091 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1092 1093 /* follow link to get target node & attr */ 1094 if ((fnode->vp->v_type == VLNK) && 1095 (smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node))) { 1096 smb_node_release(fnode); 1097 fnode = tgt_node; 1098 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1099 smb_node_release(fnode); 1100 return (rc); 1101 } 1102 } 1103 1104 /* check search attributes */ 1105 if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) { 1106 smb_node_release(fnode); 1107 return (ENOENT); 1108 } 1109 1110 fileinfo->fi_dosattr = attr.sa_dosattr; 1111 fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid; 1112 fileinfo->fi_size = attr.sa_vattr.va_size; 1113 fileinfo->fi_alloc_size = attr.sa_allocsz; 1114 fileinfo->fi_atime = attr.sa_vattr.va_atime; 1115 fileinfo->fi_mtime = attr.sa_vattr.va_mtime; 1116 fileinfo->fi_ctime = attr.sa_vattr.va_ctime; 1117 if (attr.sa_crtime.tv_sec) 1118 fileinfo->fi_crtime = attr.sa_crtime; 1119 else 1120 fileinfo->fi_crtime = attr.sa_vattr.va_mtime; 1121 1122 smb_node_release(fnode); 1123 return (0); 1124 } 1125 1126 /* 1127 * smb_odir_wildcard_fileinfo 1128 * 1129 * odirent contains a directory entry, obtained from a vop_readdir. 1130 * If a case conflict is identified the filename is mangled and the 1131 * shortname is used as 'name', in place of odirent->od_name. This 1132 * name will be used in the smb_fsop_lookup because smb_fsop_lookup 1133 * performs a case insensitive lookup if the tree is case insesitive, 1134 * so the mangled name is required in the case conflict scenario to 1135 * ensure the correct match. 1136 * 1137 * If the looked up file is a link, we attempt to lookup the link target 1138 * to use its attributes in place of those of the files's. 1139 * If we fail to lookup the target of the link we use the original 1140 * file's attributes. 1141 * Check if the attributes match the search attributes. 1142 * 1143 * Although some file systems can have directories larger than 1144 * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger 1145 * than SMB_MAXDIRSIZE is returned. It is therefore safe to use the 1146 * offset as the cookie (uint32_t). 1147 * 1148 * Returns: 0 - success 1149 * ENOENT - no match, proceed to next entry 1150 * errno - error 1151 */ 1152 static int 1153 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od, 1154 smb_odirent_t *odirent, smb_fileinfo_t *fileinfo) 1155 { 1156 int rc; 1157 smb_node_t *fnode, *tgt_node; 1158 smb_attr_t attr; 1159 char *name; 1160 boolean_t case_conflict; 1161 1162 ASSERT(sr); 1163 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1164 ASSERT(od); 1165 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1166 1167 ASSERT(MUTEX_HELD(&od->d_mutex)); 1168 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1169 1170 case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) && 1171 (odirent->od_eflags & ED_CASE_CONFLICT)); 1172 (void) smb_mangle_name(odirent->od_ino, odirent->od_name, 1173 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1174 name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name; 1175 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1176 1177 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1178 od->d_dnode, name, &fnode); 1179 if (rc != 0) 1180 return (rc); 1181 1182 /* follow link to get target node & attr */ 1183 if ((fnode->vp->v_type == VLNK) && 1184 (smb_odir_lookup_link(sr, od, name, &tgt_node))) { 1185 smb_node_release(fnode); 1186 fnode = tgt_node; 1187 } 1188 1189 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1190 smb_node_release(fnode); 1191 return (rc); 1192 } 1193 1194 /* check search attributes */ 1195 if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) { 1196 smb_node_release(fnode); 1197 return (ENOENT); 1198 } 1199 1200 fileinfo->fi_cookie = (uint32_t)od->d_offset; 1201 fileinfo->fi_dosattr = attr.sa_dosattr; 1202 fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid; 1203 fileinfo->fi_size = attr.sa_vattr.va_size; 1204 fileinfo->fi_alloc_size = attr.sa_allocsz; 1205 fileinfo->fi_atime = attr.sa_vattr.va_atime; 1206 fileinfo->fi_mtime = attr.sa_vattr.va_mtime; 1207 fileinfo->fi_ctime = attr.sa_vattr.va_ctime; 1208 if (attr.sa_crtime.tv_sec) 1209 fileinfo->fi_crtime = attr.sa_crtime; 1210 else 1211 fileinfo->fi_crtime = attr.sa_vattr.va_mtime; 1212 1213 smb_node_release(fnode); 1214 return (0); 1215 } 1216 1217 /* 1218 * smb_odir_lookup_link 1219 * 1220 * If the file is a symlink we lookup the object to which the 1221 * symlink refers so that we can return its attributes. 1222 * This can cause a problem if a symlink in a sub-directory 1223 * points to a parent directory (some UNIX GUI's create a symlink 1224 * in $HOME/.desktop that points to the user's home directory). 1225 * Some Windows applications (e.g. virus scanning) loop/hang 1226 * trying to follow this recursive path and there is little 1227 * we can do because the path is constructed on the client. 1228 * smb_dirsymlink_enable allows an end-user to disable 1229 * symlinks to directories. Symlinks to other object types 1230 * should be unaffected. 1231 * 1232 * Returns: B_TRUE - followed link. tgt_node and tgt_attr set 1233 * B_FALSE - link not followed 1234 */ 1235 static boolean_t 1236 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od, 1237 char *fname, smb_node_t **tgt_node) 1238 { 1239 int rc; 1240 1241 rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS, 1242 od->d_tree->t_snode, od->d_dnode, fname, tgt_node); 1243 if (rc != 0) { 1244 *tgt_node = NULL; 1245 return (B_FALSE); 1246 } 1247 1248 if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) { 1249 smb_node_release(*tgt_node); 1250 *tgt_node = NULL; 1251 return (B_FALSE); 1252 } 1253 1254 return (B_TRUE); 1255 } 1256