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