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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Directory operations for High Sierra filesystem 30 */ 31 32 #include <sys/types.h> 33 #include <sys/t_lock.h> 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/cred.h> 37 #include <sys/user.h> 38 #include <sys/vfs.h> 39 #include <sys/stat.h> 40 #include <sys/vnode.h> 41 #include <sys/mode.h> 42 #include <sys/dnlc.h> 43 #include <sys/cmn_err.h> 44 #include <sys/fbuf.h> 45 #include <sys/kmem.h> 46 #include <sys/policy.h> 47 #include <sys/sunddi.h> 48 #include <vm/hat.h> 49 #include <vm/as.h> 50 #include <vm/pvn.h> 51 #include <vm/seg.h> 52 #include <vm/seg_map.h> 53 #include <vm/seg_kmem.h> 54 #include <vm/page.h> 55 56 #include <sys/fs/hsfs_spec.h> 57 #include <sys/fs/hsfs_isospec.h> 58 #include <sys/fs/hsfs_node.h> 59 #include <sys/fs/hsfs_impl.h> 60 #include <sys/fs/hsfs_susp.h> 61 #include <sys/fs/hsfs_rrip.h> 62 63 #include <sys/sysinfo.h> 64 #include <sys/sysmacros.h> 65 #include <sys/errno.h> 66 #include <sys/debug.h> 67 #include <fs/fs_subr.h> 68 69 /* 70 * This macro expects a name that ends in '.' and returns TRUE if the 71 * name is not "." or ".." 72 */ 73 #define CAN_TRUNCATE_DOT(name, namelen) \ 74 (namelen > 1 && (namelen > 2 || name[0] != '.')) 75 76 enum dirblock_result { FOUND_ENTRY, WENT_PAST, HIT_END }; 77 78 /* 79 * These values determine whether we will try to read a file or dir; 80 * they may be patched via /etc/system to allow users to read 81 * record-oriented files. 82 */ 83 int ide_prohibited = IDE_PROHIBITED; 84 int hde_prohibited = HDE_PROHIBITED; 85 86 /* 87 * This variable determines if the HSFS code will use the 88 * directory name lookup cache. The default is for the cache to be used. 89 */ 90 static int hsfs_use_dnlc = 1; 91 92 /* 93 * This variable determines whether strict ISO-9660 directory ordering 94 * is to be assumed. If false (which it is by default), then when 95 * searching a directory of an ISO-9660 disk, we do not expect the 96 * entries to be sorted (as the spec requires), and so cannot terminate 97 * the search early. Unfortunately, some vendors are producing 98 * non-compliant disks. This variable exists to revert to the old 99 * behavior in case someone relies on this. This option is expected to be 100 * removed at some point in the future. 101 * 102 * Use "set hsfs:strict_iso9660_ordering = 1" in /etc/system to override. 103 */ 104 static int strict_iso9660_ordering = 0; 105 106 /* 107 * This tunable allows us to ignore inode numbers from rrip-1.12. 108 * In this case, we fall back to our default inode algorithm. 109 */ 110 int use_rrip_inodes = 1; 111 112 static void hs_hsnode_cache_reclaim(void *unused); 113 static void hs_addfreeb(struct hsfs *fsp, struct hsnode *hp); 114 static enum dirblock_result process_dirblock(struct fbuf *fbp, uint_t *offset, 115 uint_t last_offset, char *nm, int nmlen, struct hsfs *fsp, 116 struct hsnode *dhp, struct vnode *dvp, struct vnode **vpp, 117 int *error); 118 static int strip_trailing(struct hsfs *fsp, char *nm, int len); 119 static int hs_namelen(struct hsfs *fsp, char *nm, int len); 120 static int uppercase_cp(char *from, char *to, int size); 121 static void hs_log_bogus_joliet_warning(void); 122 static int hs_iso_copy(char *from, char *to, int size); 123 static int32_t hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8); 124 static int hs_utf8_trunc(uint8_t *str, int len); 125 126 /* 127 * hs_access 128 * Return 0 if the desired access may be granted. 129 * Otherwise return error code. 130 */ 131 int 132 hs_access(struct vnode *vp, mode_t m, struct cred *cred) 133 { 134 struct hsnode *hp; 135 int shift = 0; 136 137 /* 138 * Write access cannot be granted for a read-only medium 139 */ 140 if ((m & VWRITE) && !IS_DEVVP(vp)) 141 return (EROFS); 142 143 hp = VTOH(vp); 144 145 /* 146 * XXX - For now, use volume protections. 147 * Also, always grant EXEC access for directories 148 * if READ access is granted. 149 */ 150 if ((vp->v_type == VDIR) && (m & VEXEC)) { 151 m &= ~VEXEC; 152 m |= VREAD; 153 } 154 155 if (crgetuid(cred) != hp->hs_dirent.uid) { 156 shift += 3; 157 if (!groupmember((uid_t)hp->hs_dirent.gid, cred)) 158 shift += 3; 159 } 160 m &= ~(hp->hs_dirent.mode << shift); 161 if (m != 0) 162 return (secpolicy_vnode_access(cred, vp, hp->hs_dirent.uid, m)); 163 return (0); 164 } 165 166 #if ((HS_HASHSIZE & (HS_HASHSIZE - 1)) == 0) 167 #define HS_HASH(l) ((uint_t)(l) & (HS_HASHSIZE - 1)) 168 #else 169 #define HS_HASH(l) ((uint_t)(l) % HS_HASHSIZE) 170 #endif 171 #define HS_HPASH(hp) HS_HASH((hp)->hs_nodeid) 172 173 /* 174 * The tunable nhsnode is now a threshold for a dynamically allocated 175 * pool of hsnodes, not the size of a statically allocated table. 176 * When the number of hsnodes for a particular file system exceeds 177 * nhsnode, the allocate and free logic will try to reduce the number 178 * of allocated nodes by returning unreferenced nodes to the kmem_cache 179 * instead of putting them on the file system's private free list. 180 */ 181 int nhsnode = HS_HSNODESPACE / sizeof (struct hsnode); 182 183 struct kmem_cache *hsnode_cache; /* free hsnode cache */ 184 185 /* 186 * Initialize the cache of free hsnodes. 187 */ 188 void 189 hs_init_hsnode_cache(void) 190 { 191 /* 192 * A kmem_cache is used for the hsnodes 193 * No constructor because hsnodes are initialised by bzeroing. 194 */ 195 hsnode_cache = kmem_cache_create("hsfs_hsnode_cache", 196 sizeof (struct hsnode), 0, NULL, 197 NULL, hs_hsnode_cache_reclaim, NULL, NULL, 0); 198 } 199 200 /* 201 * Destroy the cache of free hsnodes. 202 */ 203 void 204 hs_fini_hsnode_cache(void) 205 { 206 kmem_cache_destroy(hsnode_cache); 207 } 208 209 /* 210 * System is short on memory, free up as much as possible 211 */ 212 /*ARGSUSED*/ 213 static void 214 hs_hsnode_cache_reclaim(void *unused) 215 { 216 struct hsfs *fsp; 217 struct hsnode *hp; 218 219 /* 220 * For each vfs in the hs_mounttab list 221 */ 222 mutex_enter(&hs_mounttab_lock); 223 for (fsp = hs_mounttab; fsp != NULL; fsp = fsp->hsfs_next) { 224 /* 225 * Purge the dnlc of all hsfs entries 226 */ 227 (void) dnlc_purge_vfsp(fsp->hsfs_vfs, 0); 228 229 /* 230 * For each entry in the free chain 231 */ 232 rw_enter(&fsp->hsfs_hash_lock, RW_WRITER); 233 mutex_enter(&fsp->hsfs_free_lock); 234 for (hp = fsp->hsfs_free_f; hp != NULL; hp = fsp->hsfs_free_f) { 235 /* 236 * Remove from chain 237 */ 238 fsp->hsfs_free_f = hp->hs_freef; 239 if (fsp->hsfs_free_f != NULL) { 240 fsp->hsfs_free_f->hs_freeb = NULL; 241 } else { 242 fsp->hsfs_free_b = NULL; 243 } 244 /* 245 * Free the node. Force it to be fully freed 246 * by setting the 3rd arg (nopage) to 1. 247 */ 248 hs_freenode(HTOV(hp), fsp, 1); 249 } 250 mutex_exit(&fsp->hsfs_free_lock); 251 rw_exit(&fsp->hsfs_hash_lock); 252 } 253 mutex_exit(&hs_mounttab_lock); 254 } 255 256 /* 257 * Add an hsnode to the end of the free list. 258 */ 259 static void 260 hs_addfreeb(struct hsfs *fsp, struct hsnode *hp) 261 { 262 struct hsnode *ep; 263 264 vn_invalid(HTOV(hp)); 265 mutex_enter(&fsp->hsfs_free_lock); 266 ep = fsp->hsfs_free_b; 267 fsp->hsfs_free_b = hp; /* hp is the last entry in free list */ 268 hp->hs_freef = NULL; 269 hp->hs_freeb = ep; /* point at previous last entry */ 270 if (ep == NULL) 271 fsp->hsfs_free_f = hp; /* hp is only entry in free list */ 272 else 273 ep->hs_freef = hp; /* point previous last entry at hp */ 274 275 mutex_exit(&fsp->hsfs_free_lock); 276 } 277 278 /* 279 * Get an hsnode from the front of the free list. 280 * Must be called with write hsfs_hash_lock held. 281 */ 282 static struct hsnode * 283 hs_getfree(struct hsfs *fsp) 284 { 285 struct hsnode *hp, **tp; 286 287 ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock)); 288 289 /* 290 * If the number of currently-allocated hsnodes is less than 291 * the hsnode count threshold (nhsnode), or if there are no 292 * nodes on the file system's local free list (which acts as a 293 * cache), call kmem_cache_alloc to get a new hsnode from 294 * kernel memory. 295 */ 296 mutex_enter(&fsp->hsfs_free_lock); 297 if ((fsp->hsfs_nohsnode < nhsnode) || (fsp->hsfs_free_f == NULL)) { 298 mutex_exit(&fsp->hsfs_free_lock); 299 hp = kmem_cache_alloc(hsnode_cache, KM_SLEEP); 300 fsp->hsfs_nohsnode++; 301 bzero((caddr_t)hp, sizeof (*hp)); 302 hp->hs_vnode = vn_alloc(KM_SLEEP); 303 return (hp); 304 } 305 hp = fsp->hsfs_free_f; 306 /* hp cannot be NULL, since we already checked this above */ 307 fsp->hsfs_free_f = hp->hs_freef; 308 if (fsp->hsfs_free_f != NULL) 309 fsp->hsfs_free_f->hs_freeb = NULL; 310 else 311 fsp->hsfs_free_b = NULL; 312 mutex_exit(&fsp->hsfs_free_lock); 313 314 for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL; 315 tp = &(*tp)->hs_hash) { 316 if (*tp == hp) { 317 struct vnode *vp; 318 319 vp = HTOV(hp); 320 321 /* 322 * file is no longer referenced, destroy all old pages 323 */ 324 if (vn_has_cached_data(vp)) 325 /* 326 * pvn_vplist_dirty will abort all old pages 327 */ 328 (void) pvn_vplist_dirty(vp, (u_offset_t)0, 329 hsfs_putapage, B_INVAL, 330 (struct cred *)NULL); 331 *tp = hp->hs_hash; 332 break; 333 } 334 } 335 if (hp->hs_dirent.sym_link != (char *)NULL) { 336 kmem_free(hp->hs_dirent.sym_link, 337 (size_t)(hp->hs_dirent.ext_size + 1)); 338 } 339 340 mutex_destroy(&hp->hs_contents_lock); 341 { 342 vnode_t *vp; 343 344 vp = hp->hs_vnode; 345 bzero((caddr_t)hp, sizeof (*hp)); 346 hp->hs_vnode = vp; 347 vn_reinit(vp); 348 } 349 return (hp); 350 } 351 352 /* 353 * Remove an hsnode from the free list. 354 */ 355 static void 356 hs_remfree(struct hsfs *fsp, struct hsnode *hp) 357 { 358 mutex_enter(&fsp->hsfs_free_lock); 359 if (hp->hs_freef != NULL) 360 hp->hs_freef->hs_freeb = hp->hs_freeb; 361 else 362 fsp->hsfs_free_b = hp->hs_freeb; 363 if (hp->hs_freeb != NULL) 364 hp->hs_freeb->hs_freef = hp->hs_freef; 365 else 366 fsp->hsfs_free_f = hp->hs_freef; 367 mutex_exit(&fsp->hsfs_free_lock); 368 } 369 370 /* 371 * Look for hsnode in hash list. 372 * If the inode number is != HS_DUMMY_INO (16), then only the inode 373 * number is used for the check. 374 * If the inode number is == HS_DUMMY_INO, we additionally always 375 * check the directory offset for the file to avoid caching the 376 * meta data for all zero sized to the first zero sized file that 377 * was touched. 378 * 379 * If found, reactivate it if inactive. 380 * 381 * Must be entered with hsfs_hash_lock held. 382 */ 383 struct vnode * 384 hs_findhash(ino64_t nodeid, uint_t lbn, uint_t off, struct vfs *vfsp) 385 { 386 struct hsnode *tp; 387 struct hsfs *fsp; 388 389 fsp = VFS_TO_HSFS(vfsp); 390 391 ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock)); 392 393 for (tp = fsp->hsfs_hash[HS_HASH(nodeid)]; tp != NULL; 394 tp = tp->hs_hash) { 395 if (tp->hs_nodeid == nodeid) { 396 struct vnode *vp; 397 398 if (nodeid == HS_DUMMY_INO) { 399 /* 400 * If this is the dummy inode number, look for 401 * matching dir_lbn and dir_off. 402 */ 403 for (; tp != NULL; tp = tp->hs_hash) { 404 if (tp->hs_nodeid == nodeid && 405 tp->hs_dir_lbn == lbn && 406 tp->hs_dir_off == off) 407 break; 408 } 409 if (tp == NULL) 410 return (NULL); 411 } 412 413 mutex_enter(&tp->hs_contents_lock); 414 vp = HTOV(tp); 415 VN_HOLD(vp); 416 if ((tp->hs_flags & HREF) == 0) { 417 tp->hs_flags |= HREF; 418 /* 419 * reactivating a free hsnode: 420 * remove from free list 421 */ 422 hs_remfree(fsp, tp); 423 } 424 mutex_exit(&tp->hs_contents_lock); 425 return (vp); 426 } 427 } 428 return (NULL); 429 } 430 431 static void 432 hs_addhash(struct hsfs *fsp, struct hsnode *hp) 433 { 434 ulong_t hashno; 435 436 ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock)); 437 438 hashno = HS_HPASH(hp); 439 hp->hs_hash = fsp->hsfs_hash[hashno]; 440 fsp->hsfs_hash[hashno] = hp; 441 } 442 443 /* 444 * Destroy all old pages and free the hsnodes 445 * Return 1 if busy (a hsnode is still referenced). 446 */ 447 int 448 hs_synchash(struct vfs *vfsp) 449 { 450 struct hsfs *fsp; 451 int i; 452 struct hsnode *hp, *nhp; 453 int busy = 0; 454 struct vnode *vp, *rvp; 455 456 fsp = VFS_TO_HSFS(vfsp); 457 rvp = fsp->hsfs_rootvp; 458 /* make sure no one can come in */ 459 rw_enter(&fsp->hsfs_hash_lock, RW_WRITER); 460 for (i = 0; i < HS_HASHSIZE; i++) { 461 for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = hp->hs_hash) { 462 vp = HTOV(hp); 463 if ((hp->hs_flags & HREF) && (vp != rvp || 464 (vp == rvp && vp->v_count > 1))) { 465 busy = 1; 466 continue; 467 } 468 if (vn_has_cached_data(vp)) 469 (void) pvn_vplist_dirty(vp, (u_offset_t)0, 470 hsfs_putapage, B_INVAL, 471 (struct cred *)NULL); 472 } 473 } 474 if (busy) { 475 rw_exit(&fsp->hsfs_hash_lock); 476 return (1); 477 } 478 479 /* now free the hsnodes */ 480 for (i = 0; i < HS_HASHSIZE; i++) { 481 for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = nhp) { 482 nhp = hp->hs_hash; 483 /* 484 * We know there are no pages associated with 485 * all the hsnodes (they've all been released 486 * above). So remove from free list and 487 * free the entry with nopage set. 488 */ 489 vp = HTOV(hp); 490 if (vp != rvp) { 491 hs_remfree(fsp, hp); 492 hs_freenode(vp, fsp, 1); 493 } 494 } 495 } 496 497 ASSERT(fsp->hsfs_nohsnode == 1); 498 rw_exit(&fsp->hsfs_hash_lock); 499 /* release the root hsnode, this should free the final hsnode */ 500 VN_RELE(rvp); 501 502 return (0); 503 } 504 505 /* 506 * hs_makenode 507 * 508 * Construct an hsnode. 509 * Caller specifies the directory entry, the block number and offset 510 * of the directory entry, and the vfs pointer. 511 * note: off is the sector offset, not lbn offset 512 * if NULL is returned implies file system hsnode table full 513 */ 514 struct vnode * 515 hs_makenode( 516 struct hs_direntry *dp, 517 uint_t lbn, 518 uint_t off, 519 struct vfs *vfsp) 520 { 521 struct hsnode *hp; 522 struct vnode *vp; 523 struct hs_volume *hvp; 524 struct vnode *newvp; 525 struct hsfs *fsp; 526 ino64_t nodeid; 527 528 fsp = VFS_TO_HSFS(vfsp); 529 530 /* 531 * Construct the data that allows us to re-read the meta data without 532 * knowing the name of the file: in the case of a directory 533 * entry, this should point to the canonical dirent, the "." 534 * directory entry for the directory. This dirent is pointed 535 * to by all directory entries for that dir (including the ".") 536 * entry itself. 537 * In the case of a file, simply point to the dirent for that 538 * file (there are hard links in Rock Ridge, so we need to use 539 * different data to contruct the node id). 540 */ 541 if (dp->type == VDIR) { 542 lbn = dp->ext_lbn; 543 off = 0; 544 } 545 546 /* 547 * Normalize lbn and off before creating a nodeid 548 * and before storing them in a hs_node structure 549 */ 550 hvp = &fsp->hsfs_vol; 551 lbn += off >> hvp->lbn_shift; 552 off &= hvp->lbn_maxoffset; 553 /* 554 * If the media carries rrip-v1.12 or newer, and we trust the inodes 555 * from the rrip data (use_rrip_inodes != 0), use that data. If the 556 * media has been created by a recent mkisofs version, we may trust 557 * all numbers in the starting extent number; otherwise, we cannot 558 * do this for zero sized files and symlinks, because if we did we'd 559 * end up mapping all of them to the same node. 560 * We use HS_DUMMY_INO in this case and make sure that we will not 561 * map all files to the same meta data. 562 */ 563 if (dp->inode != 0 && use_rrip_inodes) { 564 nodeid = dp->inode; 565 } else if ((dp->ext_size == 0 || dp->sym_link != (char *)NULL) && 566 (fsp->hsfs_flags & HSFSMNT_INODE) == 0) { 567 nodeid = HS_DUMMY_INO; 568 } else { 569 nodeid = dp->ext_lbn; 570 } 571 572 /* look for hsnode in cache first */ 573 574 rw_enter(&fsp->hsfs_hash_lock, RW_READER); 575 576 if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) { 577 578 /* 579 * Not in cache. However, someone else may have come 580 * to the same conclusion and just put one in. Upgrade 581 * our lock to a write lock and look again. 582 */ 583 rw_exit(&fsp->hsfs_hash_lock); 584 rw_enter(&fsp->hsfs_hash_lock, RW_WRITER); 585 586 if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) { 587 /* 588 * Now we are really sure that the hsnode is not 589 * in the cache. Get one off freelist or else 590 * allocate one. Either way get a bzeroed hsnode. 591 */ 592 hp = hs_getfree(fsp); 593 594 bcopy((caddr_t)dp, (caddr_t)&hp->hs_dirent, 595 sizeof (*dp)); 596 /* 597 * We've just copied this pointer into hs_dirent, 598 * and don't want 2 references to same symlink. 599 */ 600 dp->sym_link = (char *)NULL; 601 602 /* 603 * No need to hold any lock because hsnode is not 604 * yet in the hash chain. 605 */ 606 mutex_init(&hp->hs_contents_lock, NULL, MUTEX_DEFAULT, 607 NULL); 608 hp->hs_dir_lbn = lbn; 609 hp->hs_dir_off = off; 610 hp->hs_nodeid = nodeid; 611 hp->hs_seq = 0; 612 hp->hs_flags = HREF; 613 if (off > HS_SECTOR_SIZE) 614 cmn_err(CE_WARN, "hs_makenode: bad offset"); 615 616 vp = HTOV(hp); 617 vp->v_vfsp = vfsp; 618 vp->v_type = dp->type; 619 vp->v_rdev = dp->r_dev; 620 vn_setops(vp, hsfs_vnodeops); 621 vp->v_data = (caddr_t)hp; 622 vn_exists(vp); 623 /* 624 * if it's a device, call specvp 625 */ 626 if (IS_DEVVP(vp)) { 627 rw_exit(&fsp->hsfs_hash_lock); 628 newvp = specvp(vp, vp->v_rdev, vp->v_type, 629 CRED()); 630 if (newvp == NULL) 631 cmn_err(CE_NOTE, 632 "hs_makenode: specvp failed"); 633 VN_RELE(vp); 634 return (newvp); 635 } 636 637 hs_addhash(fsp, hp); 638 639 } 640 } 641 642 if (dp->sym_link != (char *)NULL) { 643 kmem_free(dp->sym_link, (size_t)(dp->ext_size + 1)); 644 dp->sym_link = (char *)NULL; 645 } 646 647 rw_exit(&fsp->hsfs_hash_lock); 648 return (vp); 649 } 650 651 /* 652 * hs_freenode 653 * 654 * Deactivate an hsnode. 655 * Leave it on the hash list but put it on the free list. 656 * If the vnode does not have any pages, release the hsnode to the 657 * kmem_cache using kmem_cache_free, else put in back of the free list. 658 * 659 * This function can be called with the hsfs_free_lock held, but only 660 * when the code is guaranteed to go through the path where the 661 * node is freed entirely, and not the path where the node could go back 662 * on the free list (and where the free lock would need to be acquired). 663 */ 664 void 665 hs_freenode(vnode_t *vp, struct hsfs *fsp, int nopage) 666 { 667 struct hsnode **tp; 668 struct hsnode *hp = VTOH(vp); 669 670 ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock)); 671 672 if (nopage || (fsp->hsfs_nohsnode >= nhsnode)) { 673 /* remove this node from the hash list, if it's there */ 674 for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL; 675 tp = &(*tp)->hs_hash) { 676 677 if (*tp == hp) { 678 *tp = hp->hs_hash; 679 break; 680 } 681 } 682 683 if (hp->hs_dirent.sym_link != (char *)NULL) { 684 kmem_free(hp->hs_dirent.sym_link, 685 (size_t)(hp->hs_dirent.ext_size + 1)); 686 hp->hs_dirent.sym_link = NULL; 687 } 688 if (vn_has_cached_data(vp)) { 689 /* clean all old pages */ 690 (void) pvn_vplist_dirty(vp, (u_offset_t)0, 691 hsfs_putapage, B_INVAL, (struct cred *)NULL); 692 /* XXX - can we remove pages by fiat like this??? */ 693 vp->v_pages = NULL; 694 } 695 mutex_destroy(&hp->hs_contents_lock); 696 vn_invalid(vp); 697 vn_free(vp); 698 kmem_cache_free(hsnode_cache, hp); 699 fsp->hsfs_nohsnode--; 700 return; 701 } 702 hs_addfreeb(fsp, hp); /* add to back of free list */ 703 } 704 705 /* 706 * hs_remakenode 707 * 708 * Reconstruct a vnode given the location of its directory entry. 709 * Caller specifies the the block number and offset 710 * of the directory entry, and the vfs pointer. 711 * Returns an error code or 0. 712 */ 713 int 714 hs_remakenode(uint_t lbn, uint_t off, struct vfs *vfsp, 715 struct vnode **vpp) 716 { 717 struct buf *secbp; 718 struct hsfs *fsp; 719 uint_t secno; 720 uchar_t *dirp; 721 struct hs_direntry hd; 722 int error; 723 724 /* Convert to sector and offset */ 725 fsp = VFS_TO_HSFS(vfsp); 726 if (off > HS_SECTOR_SIZE) { 727 cmn_err(CE_WARN, "hs_remakenode: bad offset"); 728 error = EINVAL; 729 goto end; 730 } 731 secno = LBN_TO_SEC(lbn, vfsp); 732 secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE); 733 734 error = geterror(secbp); 735 if (error != 0) { 736 cmn_err(CE_NOTE, "hs_remakenode: bread: error=(%d)", error); 737 goto end; 738 } 739 740 dirp = (uchar_t *)secbp->b_un.b_addr; 741 error = hs_parsedir(fsp, &dirp[off], &hd, (char *)NULL, (int *)NULL, 742 HS_SECTOR_SIZE - off); 743 if (!error) { 744 *vpp = hs_makenode(&hd, lbn, off, vfsp); 745 if (*vpp == NULL) 746 error = ENFILE; 747 } 748 749 end: 750 brelse(secbp); 751 return (error); 752 } 753 754 755 /* 756 * hs_dirlook 757 * 758 * Look for a given name in a given directory. 759 * If found, construct an hsnode for it. 760 */ 761 int 762 hs_dirlook( 763 struct vnode *dvp, 764 char *name, 765 int namlen, /* length of 'name' */ 766 struct vnode **vpp, 767 struct cred *cred) 768 { 769 struct hsnode *dhp; 770 struct hsfs *fsp; 771 int error = 0; 772 uint_t offset; /* real offset in directory */ 773 uint_t last_offset; /* last index in directory */ 774 char *cmpname; /* case-folded name */ 775 int cmpname_size; /* how much memory we allocate for it */ 776 int cmpnamelen; 777 int adhoc_search; /* did we start at begin of dir? */ 778 int end; 779 uint_t hsoffset; 780 struct fbuf *fbp; 781 int bytes_wanted; 782 int dirsiz; 783 int is_rrip; 784 785 if (dvp->v_type != VDIR) 786 return (ENOTDIR); 787 788 if (error = hs_access(dvp, (mode_t)VEXEC, cred)) 789 return (error); 790 791 if (hsfs_use_dnlc && (*vpp = dnlc_lookup(dvp, name))) 792 return (0); 793 794 dhp = VTOH(dvp); 795 fsp = VFS_TO_HSFS(dvp->v_vfsp); 796 is_rrip = IS_RRIP_IMPLEMENTED(fsp); 797 798 /* 799 * name == "^A" is illegal for ISO-9660 and Joliet as '..' is '\1' on 800 * disk. It is no problem for Rock Ridge as RR uses '.' and '..'. 801 * XXX It could be OK for Joliet also (because namelen == 1 is 802 * XXX impossible for UCS-2) but then we need a better compare algorith. 803 */ 804 if (!is_rrip && *name == '\1' && namlen == 1) 805 return (EINVAL); 806 807 cmpname_size = (int)(fsp->hsfs_namemax + 1); 808 cmpname = kmem_alloc((size_t)cmpname_size, KM_SLEEP); 809 810 if (namlen >= cmpname_size) 811 namlen = cmpname_size - 1; 812 /* 813 * For the purposes of comparing the name against dir entries, 814 * fold it to upper case. 815 */ 816 if (is_rrip) { 817 (void) strlcpy(cmpname, name, cmpname_size); 818 cmpnamelen = namlen; 819 } else { 820 /* 821 * If we don't consider a trailing dot as part of the filename, 822 * remove it from the specified name 823 */ 824 if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) && 825 name[namlen-1] == '.' && 826 CAN_TRUNCATE_DOT(name, namlen)) 827 name[--namlen] = '\0'; 828 if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2 || 829 fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) { 830 cmpnamelen = hs_iso_copy(name, cmpname, namlen); 831 } else { 832 cmpnamelen = hs_uppercase_copy(name, cmpname, namlen); 833 } 834 } 835 836 /* make sure dirent is filled up with all info */ 837 if (dhp->hs_dirent.ext_size == 0) 838 hs_filldirent(dvp, &dhp->hs_dirent); 839 840 /* 841 * No lock is needed - hs_offset is used as starting 842 * point for searching the directory. 843 */ 844 offset = dhp->hs_offset; 845 hsoffset = offset; 846 adhoc_search = (offset != 0); 847 848 end = dhp->hs_dirent.ext_size; 849 dirsiz = end; 850 851 tryagain: 852 853 while (offset < end) { 854 bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK)); 855 856 error = fbread(dvp, (offset_t)(offset & MAXBMASK), 857 (unsigned int)bytes_wanted, S_READ, &fbp); 858 if (error) 859 goto done; 860 861 last_offset = (offset & MAXBMASK) + fbp->fb_count; 862 863 switch (process_dirblock(fbp, &offset, last_offset, 864 cmpname, cmpnamelen, fsp, dhp, dvp, vpp, &error)) { 865 case FOUND_ENTRY: 866 /* found an entry, either correct or not */ 867 goto done; 868 869 case WENT_PAST: 870 /* 871 * If we get here we know we didn't find it on the 872 * first pass. If adhoc_search, then we started a 873 * bit into the dir, and need to wrap around and 874 * search the first entries. If not, then we started 875 * at the beginning and didn't find it. 876 */ 877 if (adhoc_search) { 878 offset = 0; 879 end = hsoffset; 880 adhoc_search = 0; 881 goto tryagain; 882 } 883 error = ENOENT; 884 goto done; 885 886 case HIT_END: 887 goto tryagain; 888 } 889 } 890 /* 891 * End of all dir blocks, didn't find entry. 892 */ 893 if (adhoc_search) { 894 offset = 0; 895 end = hsoffset; 896 adhoc_search = 0; 897 goto tryagain; 898 } 899 error = ENOENT; 900 done: 901 /* 902 * If we found the entry, add it to the DNLC 903 * If the entry is a device file (assuming we support Rock Ridge), 904 * we enter the device vnode to the cache since that is what 905 * is in *vpp. 906 * That is ok since the CD-ROM is read-only, so (dvp,name) will 907 * always point to the same device. 908 */ 909 if (hsfs_use_dnlc && !error) 910 dnlc_enter(dvp, name, *vpp); 911 912 kmem_free(cmpname, (size_t)cmpname_size); 913 914 return (error); 915 } 916 917 /* 918 * hs_parsedir 919 * 920 * Parse a Directory Record into an hs_direntry structure. 921 * High Sierra and ISO directory are almost the same 922 * except the flag and date 923 */ 924 int 925 hs_parsedir( 926 struct hsfs *fsp, 927 uchar_t *dirp, 928 struct hs_direntry *hdp, 929 char *dnp, 930 int *dnlen, 931 int last_offset) /* last offset in dirp */ 932 { 933 char *on_disk_name; 934 int on_disk_namelen; 935 int on_disk_dirlen; 936 uchar_t flags; 937 int namelen; 938 int error; 939 int name_change_flag = 0; /* set if name was gotten in SUA */ 940 941 hdp->ext_lbn = HDE_EXT_LBN(dirp); 942 hdp->ext_size = HDE_EXT_SIZE(dirp); 943 hdp->xar_len = HDE_XAR_LEN(dirp); 944 hdp->intlf_sz = HDE_INTRLV_SIZE(dirp); 945 hdp->intlf_sk = HDE_INTRLV_SKIP(dirp); 946 hdp->sym_link = (char *)NULL; 947 948 if (fsp->hsfs_vol_type == HS_VOL_TYPE_HS) { 949 flags = HDE_FLAGS(dirp); 950 hs_parse_dirdate(HDE_cdate(dirp), &hdp->cdate); 951 hs_parse_dirdate(HDE_cdate(dirp), &hdp->adate); 952 hs_parse_dirdate(HDE_cdate(dirp), &hdp->mdate); 953 if ((flags & hde_prohibited) == 0) { 954 /* 955 * Skip files with the associated bit set. 956 */ 957 if (flags & HDE_ASSOCIATED) 958 return (EAGAIN); 959 hdp->type = VREG; 960 hdp->mode = HFREG; 961 hdp->nlink = 1; 962 } else if ((flags & hde_prohibited) == HDE_DIRECTORY) { 963 hdp->type = VDIR; 964 hdp->mode = HFDIR; 965 hdp->nlink = 2; 966 } else { 967 hs_log_bogus_disk_warning(fsp, 968 HSFS_ERR_UNSUP_TYPE, flags); 969 return (EINVAL); 970 } 971 hdp->uid = fsp -> hsfs_vol.vol_uid; 972 hdp->gid = fsp -> hsfs_vol.vol_gid; 973 hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777); 974 } else if ((fsp->hsfs_vol_type == HS_VOL_TYPE_ISO) || 975 (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) || 976 (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET)) { 977 978 flags = IDE_FLAGS(dirp); 979 hs_parse_dirdate(IDE_cdate(dirp), &hdp->cdate); 980 hs_parse_dirdate(IDE_cdate(dirp), &hdp->adate); 981 hs_parse_dirdate(IDE_cdate(dirp), &hdp->mdate); 982 983 if ((flags & ide_prohibited) == 0) { 984 /* 985 * Skip files with the associated bit set. 986 */ 987 if (flags & IDE_ASSOCIATED) 988 return (EAGAIN); 989 hdp->type = VREG; 990 hdp->mode = HFREG; 991 hdp->nlink = 1; 992 } else if ((flags & ide_prohibited) == IDE_DIRECTORY) { 993 hdp->type = VDIR; 994 hdp->mode = HFDIR; 995 hdp->nlink = 2; 996 } else { 997 hs_log_bogus_disk_warning(fsp, 998 HSFS_ERR_UNSUP_TYPE, flags); 999 return (EINVAL); 1000 } 1001 hdp->uid = fsp -> hsfs_vol.vol_uid; 1002 hdp->gid = fsp -> hsfs_vol.vol_gid; 1003 hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777); 1004 hdp->inode = 0; /* initialize with 0, then check rrip */ 1005 1006 /* 1007 * Having this all filled in, let's see if we have any 1008 * SUA susp to look at. 1009 */ 1010 if (IS_SUSP_IMPLEMENTED(fsp)) { 1011 error = parse_sua((uchar_t *)dnp, dnlen, 1012 &name_change_flag, dirp, last_offset, 1013 hdp, fsp, 1014 (uchar_t *)NULL, NULL); 1015 if (error) { 1016 if (hdp->sym_link) { 1017 kmem_free(hdp->sym_link, 1018 (size_t)(hdp->ext_size + 1)); 1019 hdp->sym_link = (char *)NULL; 1020 } 1021 return (error); 1022 } 1023 } 1024 } 1025 hdp->xar_prot = (HDE_PROTECTION & flags) != 0; 1026 1027 #if dontskip 1028 if (hdp->xar_len > 0) { 1029 cmn_err(CE_NOTE, "hsfs: extended attributes not supported"); 1030 return (EINVAL); 1031 } 1032 #endif 1033 1034 /* check interleaf size and skip factor */ 1035 /* must both be zero or non-zero */ 1036 if (hdp->intlf_sz + hdp->intlf_sk) { 1037 if ((hdp->intlf_sz == 0) || (hdp->intlf_sk == 0)) { 1038 cmn_err(CE_NOTE, 1039 "hsfs: interleaf size or skip factor error"); 1040 return (EINVAL); 1041 } 1042 if (hdp->ext_size == 0) { 1043 cmn_err(CE_NOTE, 1044 "hsfs: interleaving specified on zero length file"); 1045 return (EINVAL); 1046 } 1047 } 1048 1049 if (HDE_VOL_SET(dirp) != 1) { 1050 if (fsp->hsfs_vol.vol_set_size != 1 && 1051 fsp->hsfs_vol.vol_set_size != HDE_VOL_SET(dirp)) { 1052 cmn_err(CE_NOTE, "hsfs: multivolume file?"); 1053 return (EINVAL); 1054 } 1055 } 1056 1057 /* 1058 * If the name changed, then the NM field for RRIP was hit and 1059 * we should not copy the name again, just return. 1060 */ 1061 if (NAME_HAS_CHANGED(name_change_flag)) 1062 return (0); 1063 1064 /* 1065 * Fall back to the ISO name. Note that as in process_dirblock, 1066 * the on-disk filename length must be validated against ISO 1067 * limits - which, in case of RR present but no RR name found, 1068 * are NOT identical to fsp->hsfs_namemax on this filesystem. 1069 */ 1070 on_disk_name = (char *)HDE_name(dirp); 1071 on_disk_namelen = (int)HDE_NAME_LEN(dirp); 1072 on_disk_dirlen = (int)HDE_DIR_LEN(dirp); 1073 1074 if (on_disk_dirlen < HDE_ROOT_DIR_REC_SIZE || 1075 ((on_disk_dirlen > last_offset) || 1076 ((HDE_FDESIZE + on_disk_namelen) > on_disk_dirlen))) { 1077 hs_log_bogus_disk_warning(fsp, 1078 HSFS_ERR_BAD_DIR_ENTRY, 0); 1079 return (EINVAL); 1080 } 1081 1082 if (on_disk_namelen > fsp->hsfs_namelen && 1083 hs_namelen(fsp, on_disk_name, on_disk_namelen) > 1084 fsp->hsfs_namelen) { 1085 hs_log_bogus_disk_warning(fsp, 1086 fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ? 1087 HSFS_ERR_BAD_JOLIET_FILE_LEN : 1088 HSFS_ERR_BAD_FILE_LEN, 0); 1089 } 1090 if (on_disk_namelen > ISO_NAMELEN_V2_MAX) 1091 on_disk_namelen = fsp->hsfs_namemax; /* Paranoia */ 1092 1093 if (dnp != NULL) { 1094 if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) { 1095 namelen = hs_jnamecopy(on_disk_name, dnp, 1096 on_disk_namelen, fsp->hsfs_namemax, 1097 fsp->hsfs_flags); 1098 /* 1099 * A negative return value means that the file name 1100 * has been truncated to fsp->hsfs_namemax. 1101 */ 1102 if (namelen < 0) { 1103 namelen = -namelen; 1104 hs_log_bogus_disk_warning(fsp, 1105 HSFS_ERR_TRUNC_JOLIET_FILE_LEN, 0); 1106 } 1107 } else { 1108 /* 1109 * HS_VOL_TYPE_ISO && HS_VOL_TYPE_ISO_V2 1110 */ 1111 namelen = hs_namecopy(on_disk_name, dnp, 1112 on_disk_namelen, fsp->hsfs_flags); 1113 } 1114 if (namelen == 0) 1115 return (EINVAL); 1116 if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) && 1117 dnp[ namelen-1 ] == '.' && CAN_TRUNCATE_DOT(dnp, namelen)) 1118 dnp[ --namelen ] = '\0'; 1119 } else 1120 namelen = on_disk_namelen; 1121 if (dnlen != NULL) 1122 *dnlen = namelen; 1123 1124 return (0); 1125 } 1126 1127 /* 1128 * hs_namecopy 1129 * 1130 * Parse a file/directory name into UNIX form. 1131 * Delete trailing blanks, upper-to-lower case, add NULL terminator. 1132 * Returns the (possibly new) length. 1133 * 1134 * Called from hsfs_readdir() via hs_parsedir() 1135 */ 1136 int 1137 hs_namecopy(char *from, char *to, int size, ulong_t flags) 1138 { 1139 uint_t i; 1140 uchar_t c; 1141 int lastspace; 1142 int maplc; 1143 int trailspace; 1144 int version; 1145 1146 /* special handling for '.' and '..' */ 1147 if (size == 1) { 1148 if (*from == '\0') { 1149 *to++ = '.'; 1150 *to = '\0'; 1151 return (1); 1152 } else if (*from == '\1') { 1153 *to++ = '.'; 1154 *to++ = '.'; 1155 *to = '\0'; 1156 return (2); 1157 } 1158 } 1159 1160 maplc = (flags & HSFSMNT_NOMAPLCASE) == 0; 1161 trailspace = (flags & HSFSMNT_NOTRAILSPACE) == 0; 1162 version = (flags & HSFSMNT_NOVERSION) == 0; 1163 for (i = 0, lastspace = -1; i < size; i++) { 1164 c = from[i]; 1165 if (c == ';' && version) 1166 break; 1167 if (c <= ' ' && !trailspace) { 1168 if (lastspace == -1) 1169 lastspace = i; 1170 } else 1171 lastspace = -1; 1172 if (maplc && (c >= 'A') && (c <= 'Z')) 1173 c += 'a' - 'A'; 1174 to[i] = c; 1175 } 1176 if (lastspace != -1) 1177 i = lastspace; 1178 to[i] = '\0'; 1179 return (i); 1180 } 1181 1182 /* 1183 * hs_jnamecopy 1184 * 1185 * This is the Joliet variant of hs_namecopy() 1186 * 1187 * Parse a UCS-2 Joliet file/directory name into UNIX form. 1188 * Add NULL terminator. 1189 * Returns the new length. 1190 * 1191 * Called from hsfs_readdir() via hs_parsedir() 1192 */ 1193 int 1194 hs_jnamecopy(char *from, char *to, int size, int maxsize, ulong_t flags) 1195 { 1196 uint_t i; 1197 uint_t len; 1198 uint16_t c; 1199 int amt; 1200 int version; 1201 1202 /* special handling for '.' and '..' */ 1203 if (size == 1) { 1204 if (*from == '\0') { 1205 *to++ = '.'; 1206 *to = '\0'; 1207 return (1); 1208 } else if (*from == '\1') { 1209 *to++ = '.'; 1210 *to++ = '.'; 1211 *to = '\0'; 1212 return (2); 1213 } 1214 } 1215 1216 version = (flags & HSFSMNT_NOVERSION) == 0; 1217 for (i = 0, len = 0; i < size; i++) { 1218 c = (from[i++] & 0xFF) << 8; 1219 c |= from[i] & 0xFF; 1220 if (c == ';' && version) 1221 break; 1222 1223 if (len > (maxsize-3)) { 1224 if (c < 0x80) 1225 amt = 1; 1226 else if (c < 0x800) 1227 amt = 2; 1228 else 1229 amt = 3; 1230 if ((len+amt) > maxsize) { 1231 to[len] = '\0'; 1232 return (-len); 1233 } 1234 } 1235 amt = hs_ucs2_2_utf8(c, (uint8_t *)&to[len]); 1236 if (amt == 0) { 1237 hs_log_bogus_joliet_warning(); /* should never happen */ 1238 return (0); 1239 } 1240 len += amt; 1241 } 1242 to[len] = '\0'; 1243 return (len); 1244 } 1245 1246 /* 1247 * map a filename to upper case; 1248 * return 1 if found lowercase character 1249 * 1250 * Called from process_dirblock() 1251 * via hsfs_lookup() -> hs_dirlook() -> process_dirblock() 1252 * to create an intermedia name from on disk file names for 1253 * comparing names. 1254 */ 1255 static int 1256 uppercase_cp(char *from, char *to, int size) 1257 { 1258 uint_t i; 1259 uchar_t c; 1260 uchar_t had_lc = 0; 1261 1262 for (i = 0; i < size; i++) { 1263 c = *from++; 1264 if ((c >= 'a') && (c <= 'z')) { 1265 c -= ('a' - 'A'); 1266 had_lc = 1; 1267 } 1268 *to++ = c; 1269 } 1270 return (had_lc); 1271 } 1272 1273 /* 1274 * This is the Joliet variant of uppercase_cp() 1275 * 1276 * map a UCS-2 filename to UTF-8; 1277 * return new length 1278 * 1279 * Called from process_dirblock() 1280 * via hsfs_lookup() -> hs_dirlook() -> process_dirblock() 1281 * to create an intermedia name from on disk file names for 1282 * comparing names. 1283 */ 1284 int 1285 hs_joliet_cp(char *from, char *to, int size) 1286 { 1287 uint_t i; 1288 uint16_t c; 1289 int len = 0; 1290 int amt; 1291 1292 /* special handling for '\0' and '\1' */ 1293 if (size == 1) { 1294 *to = *from; 1295 return (1); 1296 } 1297 for (i = 0; i < size; i += 2) { 1298 c = (*from++ & 0xFF) << 8; 1299 c |= *from++ & 0xFF; 1300 1301 amt = hs_ucs2_2_utf8(c, (uint8_t *)to); 1302 if (amt == 0) { 1303 hs_log_bogus_joliet_warning(); /* should never happen */ 1304 return (0); 1305 } 1306 1307 to += amt; 1308 len += amt; 1309 } 1310 return (len); 1311 } 1312 1313 static void 1314 hs_log_bogus_joliet_warning(void) 1315 { 1316 static int warned = 0; 1317 1318 if (warned) 1319 return; 1320 warned = 1; 1321 cmn_err(CE_CONT, "hsfs: Warning: " 1322 "file name contains bad UCS-2 chacarter\n"); 1323 } 1324 1325 1326 /* 1327 * hs_uppercase_copy 1328 * 1329 * Convert a UNIX-style name into its HSFS equivalent 1330 * replacing '.' and '..' with '\0' and '\1'. 1331 * Map to upper case. 1332 * Returns the (possibly new) length. 1333 * 1334 * Called from hs_dirlook() and rrip_namecopy() 1335 * to create an intermediate name from the callers name from hsfs_lookup() 1336 * XXX Is the call from rrip_namecopy() OK? 1337 */ 1338 int 1339 hs_uppercase_copy(char *from, char *to, int size) 1340 { 1341 uint_t i; 1342 uchar_t c; 1343 1344 /* special handling for '.' and '..' */ 1345 1346 if (size == 1 && *from == '.') { 1347 *to = '\0'; 1348 return (1); 1349 } else if (size == 2 && *from == '.' && *(from+1) == '.') { 1350 *to = '\1'; 1351 return (1); 1352 } 1353 1354 for (i = 0; i < size; i++) { 1355 c = *from++; 1356 if ((c >= 'a') && (c <= 'z')) 1357 c = c - 'a' + 'A'; 1358 *to++ = c; 1359 } 1360 return (size); 1361 } 1362 1363 /* 1364 * hs_iso_copy 1365 * 1366 * This is the Joliet/ISO-9660:1999 variant of hs_uppercase_copy() 1367 * 1368 * Convert a UTF-8 UNIX-style name into its UTF-8 Joliet/ISO equivalent 1369 * replacing '.' and '..' with '\0' and '\1'. 1370 * Returns the (possibly new) length. 1371 * 1372 * Called from hs_dirlook() 1373 * to create an intermediate name from the callers name from hsfs_lookup() 1374 */ 1375 static int 1376 hs_iso_copy(char *from, char *to, int size) 1377 { 1378 uint_t i; 1379 uchar_t c; 1380 1381 /* special handling for '.' and '..' */ 1382 1383 if (size == 1 && *from == '.') { 1384 *to = '\0'; 1385 return (1); 1386 } else if (size == 2 && *from == '.' && *(from+1) == '.') { 1387 *to = '\1'; 1388 return (1); 1389 } 1390 1391 for (i = 0; i < size; i++) { 1392 c = *from++; 1393 *to++ = c; 1394 } 1395 return (size); 1396 } 1397 1398 void 1399 hs_filldirent(struct vnode *vp, struct hs_direntry *hdp) 1400 { 1401 struct buf *secbp; 1402 uint_t secno; 1403 offset_t secoff; 1404 struct hsfs *fsp; 1405 uchar_t *secp; 1406 int error; 1407 1408 if (vp->v_type != VDIR) { 1409 cmn_err(CE_WARN, "hsfs_filldirent: vp (0x%p) not a directory", 1410 (void *)vp); 1411 return; 1412 } 1413 1414 fsp = VFS_TO_HSFS(vp ->v_vfsp); 1415 secno = LBN_TO_SEC(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp); 1416 secoff = LBN_TO_BYTE(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp) & 1417 MAXHSOFFSET; 1418 secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE); 1419 error = geterror(secbp); 1420 if (error != 0) { 1421 cmn_err(CE_NOTE, "hs_filldirent: bread: error=(%d)", error); 1422 goto end; 1423 } 1424 1425 secp = (uchar_t *)secbp->b_un.b_addr; 1426 1427 /* quick check */ 1428 if (hdp->ext_lbn != HDE_EXT_LBN(&secp[secoff])) { 1429 cmn_err(CE_NOTE, "hsfs_filldirent: dirent not match"); 1430 /* keep on going */ 1431 } 1432 (void) hs_parsedir(fsp, &secp[secoff], hdp, (char *)NULL, 1433 (int *)NULL, HS_SECTOR_SIZE - secoff); 1434 1435 end: 1436 brelse(secbp); 1437 } 1438 1439 /* 1440 * Look through a directory block for a matching entry. 1441 * Note: this routine does an fbrelse() on the buffer passed in. 1442 */ 1443 static enum dirblock_result 1444 process_dirblock( 1445 struct fbuf *fbp, /* buffer containing dirblk */ 1446 uint_t *offset, /* lower index */ 1447 uint_t last_offset, /* upper index */ 1448 char *nm, /* upcase nm to compare against */ 1449 int nmlen, /* length of name */ 1450 struct hsfs *fsp, 1451 struct hsnode *dhp, 1452 struct vnode *dvp, 1453 struct vnode **vpp, 1454 int *error) /* return value: errno */ 1455 { 1456 uchar_t *blkp = (uchar_t *)fbp->fb_addr; /* dir block */ 1457 char *dname; /* name in directory entry */ 1458 int dnamelen; /* length of name */ 1459 struct hs_direntry hd; 1460 int hdlen; 1461 uchar_t *dirp; /* the directory entry */ 1462 int res; 1463 int parsedir_res; 1464 int is_rrip; 1465 size_t rrip_name_size; 1466 int rr_namelen = 0; 1467 char *rrip_name_str = NULL; 1468 char *rrip_tmp_name = NULL; 1469 enum dirblock_result err = 0; 1470 int did_fbrelse = 0; 1471 char uppercase_name[JOLIET_NAMELEN_MAX*3 + 1]; /* 331 */ 1472 1473 #define PD_return(retval) \ 1474 { err = retval; goto do_ret; } /* return after cleanup */ 1475 #define rel_offset(offset) \ 1476 ((offset) & MAXBOFFSET) /* index into cur blk */ 1477 #define RESTORE_NM(tmp, orig) \ 1478 if (is_rrip && *(tmp) != '\0') \ 1479 (void) strcpy((orig), (tmp)) 1480 1481 is_rrip = IS_RRIP_IMPLEMENTED(fsp); 1482 if (is_rrip) { 1483 rrip_name_size = RRIP_FILE_NAMELEN + 1; 1484 rrip_name_str = kmem_alloc(rrip_name_size, KM_SLEEP); 1485 rrip_tmp_name = kmem_alloc(rrip_name_size, KM_SLEEP); 1486 rrip_name_str[0] = '\0'; 1487 rrip_tmp_name[0] = '\0'; 1488 } 1489 1490 while (*offset < last_offset) { 1491 1492 /* 1493 * Directory Entries cannot span sectors. 1494 * 1495 * Unused bytes at the end of each sector are zeroed 1496 * according to ISO9660, but we cannot rely on this 1497 * since both media failures and maliciously corrupted 1498 * media may return arbitrary values. 1499 * We therefore have to check for consistency: 1500 * The size of a directory entry must be at least 1501 * 34 bytes (the size of the directory entry metadata), 1502 * or zero (indicating the end-of-sector condition). 1503 * For a non-zero directory entry size of less than 1504 * 34 Bytes, log a warning. 1505 * In any case, skip the rest of this sector and 1506 * continue with the next. 1507 */ 1508 hdlen = (int)((uchar_t) 1509 HDE_DIR_LEN(&blkp[rel_offset(*offset)])); 1510 1511 if (hdlen < HDE_ROOT_DIR_REC_SIZE || 1512 *offset + hdlen > last_offset) { 1513 /* 1514 * Advance to the next sector boundary 1515 */ 1516 *offset = roundup(*offset + 1, HS_SECTOR_SIZE); 1517 if (hdlen) 1518 hs_log_bogus_disk_warning(fsp, 1519 HSFS_ERR_TRAILING_JUNK, 0); 1520 continue; 1521 } 1522 1523 bzero(&hd, sizeof (hd)); 1524 1525 /* 1526 * Check the filename length in the ISO record for 1527 * plausibility and reset it to a safe value, in case 1528 * the name length byte is out of range. Since the ISO 1529 * name will be used as fallback if the rockridge name 1530 * is invalid/nonexistant, we must make sure not to 1531 * blow the bounds and initialize dnamelen to a sensible 1532 * value within the limits of ISO9660. 1533 * In addition to that, the ISO filename is part of the 1534 * directory entry. If the filename length is too large 1535 * to fit, the record is invalid and we'll advance to 1536 * the next. 1537 */ 1538 dirp = &blkp[rel_offset(*offset)]; 1539 dname = (char *)HDE_name(dirp); 1540 dnamelen = (int)((uchar_t)HDE_NAME_LEN(dirp)); 1541 /* 1542 * If the directory entry extends beyond the end of the 1543 * block, it must be invalid. Skip it. 1544 */ 1545 if (dnamelen > hdlen - HDE_FDESIZE) { 1546 hs_log_bogus_disk_warning(fsp, 1547 HSFS_ERR_BAD_DIR_ENTRY, 0); 1548 goto skip_rec; 1549 } else if (dnamelen > fsp->hsfs_namelen && 1550 hs_namelen(fsp, dname, dnamelen) > fsp->hsfs_namelen) { 1551 hs_log_bogus_disk_warning(fsp, 1552 fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ? 1553 HSFS_ERR_BAD_JOLIET_FILE_LEN : 1554 HSFS_ERR_BAD_FILE_LEN, 0); 1555 } 1556 if (dnamelen > ISO_NAMELEN_V2_MAX) 1557 dnamelen = fsp->hsfs_namemax; /* Paranoia */ 1558 1559 /* 1560 * If the rock ridge is implemented, then we copy the name 1561 * from the SUA area to rrip_name_str. If no Alternate 1562 * name is found, then use the uppercase NM in the 1563 * rrip_name_str char array. 1564 */ 1565 if (is_rrip) { 1566 1567 rrip_name_str[0] = '\0'; 1568 rr_namelen = rrip_namecopy(nm, &rrip_name_str[0], 1569 &rrip_tmp_name[0], dirp, last_offset - *offset, 1570 fsp, &hd); 1571 if (hd.sym_link) { 1572 kmem_free(hd.sym_link, 1573 (size_t)(hd.ext_size+1)); 1574 hd.sym_link = (char *)NULL; 1575 } 1576 1577 if (rr_namelen != -1) { 1578 dname = (char *)&rrip_name_str[0]; 1579 dnamelen = rr_namelen; 1580 } 1581 } 1582 1583 if (!is_rrip || rr_namelen == -1) { 1584 /* use iso name instead */ 1585 1586 int i = -1; 1587 /* 1588 * make sure that we get rid of ';' in the dname of 1589 * an iso direntry, as we should have no knowledge 1590 * of file versions. 1591 * 1592 * XXX This is done the wrong way: it does not take 1593 * XXX care of the fact that the version string is 1594 * XXX a decimal number in the range 1 to 32767. 1595 */ 1596 if ((fsp->hsfs_flags & HSFSMNT_NOVERSION) == 0) { 1597 if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) { 1598 for (i = dnamelen - 1; i > 0; i -= 2) { 1599 if (dname[i] == ';' && 1600 dname[i-1] == '\0') { 1601 --i; 1602 break; 1603 } 1604 } 1605 } else { 1606 for (i = dnamelen - 1; i > 0; i--) { 1607 if (dname[i] == ';') 1608 break; 1609 } 1610 } 1611 } 1612 if (i > 0) { 1613 dnamelen = i; 1614 } else if (fsp->hsfs_vol_type != HS_VOL_TYPE_ISO_V2 && 1615 fsp->hsfs_vol_type != HS_VOL_TYPE_JOLIET) { 1616 dnamelen = strip_trailing(fsp, dname, dnamelen); 1617 } 1618 1619 ASSERT(dnamelen < sizeof (uppercase_name)); 1620 1621 if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) { 1622 (void) strncpy(uppercase_name, dname, dnamelen); 1623 } else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) { 1624 dnamelen = hs_joliet_cp(dname, uppercase_name, 1625 dnamelen); 1626 } else if (uppercase_cp(dname, uppercase_name, 1627 dnamelen)) { 1628 hs_log_bogus_disk_warning(fsp, 1629 HSFS_ERR_LOWER_CASE_NM, 0); 1630 } 1631 dname = uppercase_name; 1632 if (!is_rrip && 1633 (fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) && 1634 dname[dnamelen - 1] == '.' && 1635 CAN_TRUNCATE_DOT(dname, dnamelen)) 1636 dname[--dnamelen] = '\0'; 1637 } 1638 1639 /* 1640 * Quickly screen for a non-matching entry, but not for RRIP. 1641 * This test doesn't work for lowercase vs. uppercase names. 1642 */ 1643 1644 /* if we saw a lower case name we can't do this test either */ 1645 if (strict_iso9660_ordering && !is_rrip && 1646 !HSFS_HAVE_LOWER_CASE(fsp) && *nm < *dname) { 1647 RESTORE_NM(rrip_tmp_name, nm); 1648 PD_return(WENT_PAST) 1649 } 1650 1651 if (*nm != *dname || nmlen != dnamelen) 1652 goto skip_rec; 1653 1654 if ((res = bcmp(dname, nm, nmlen)) == 0) { 1655 /* name matches */ 1656 parsedir_res = hs_parsedir(fsp, dirp, &hd, 1657 (char *)NULL, (int *)NULL, 1658 last_offset - *offset); 1659 if (!parsedir_res) { 1660 uint_t lbn; /* logical block number */ 1661 1662 lbn = dhp->hs_dirent.ext_lbn + 1663 dhp->hs_dirent.xar_len; 1664 /* 1665 * Need to do an fbrelse() on the buffer, 1666 * as hs_makenode() may try to acquire 1667 * hs_hashlock, which may not be required 1668 * while a page is locked. 1669 */ 1670 fbrelse(fbp, S_READ); 1671 did_fbrelse = 1; 1672 *vpp = hs_makenode(&hd, lbn, *offset, 1673 dvp->v_vfsp); 1674 if (*vpp == NULL) { 1675 *error = ENFILE; 1676 RESTORE_NM(rrip_tmp_name, nm); 1677 PD_return(FOUND_ENTRY) 1678 } 1679 1680 dhp->hs_offset = *offset; 1681 RESTORE_NM(rrip_tmp_name, nm); 1682 PD_return(FOUND_ENTRY) 1683 } else if (parsedir_res != EAGAIN) { 1684 /* improper dir entry */ 1685 *error = parsedir_res; 1686 RESTORE_NM(rrip_tmp_name, nm); 1687 PD_return(FOUND_ENTRY) 1688 } 1689 } else if (strict_iso9660_ordering && !is_rrip && 1690 !HSFS_HAVE_LOWER_CASE(fsp) && res < 0) { 1691 /* name < dir entry */ 1692 RESTORE_NM(rrip_tmp_name, nm); 1693 PD_return(WENT_PAST) 1694 } 1695 /* 1696 * name > dir entry, 1697 * look at next one. 1698 */ 1699 skip_rec: 1700 *offset += hdlen; 1701 RESTORE_NM(rrip_tmp_name, nm); 1702 } 1703 PD_return(HIT_END) 1704 1705 do_ret: 1706 if (rrip_name_str) 1707 kmem_free(rrip_name_str, rrip_name_size); 1708 if (rrip_tmp_name) 1709 kmem_free(rrip_tmp_name, rrip_name_size); 1710 if (!did_fbrelse) 1711 fbrelse(fbp, S_READ); 1712 return (err); 1713 #undef PD_return 1714 #undef RESTORE_NM 1715 } 1716 1717 /* 1718 * Strip trailing nulls or spaces from the name; 1719 * return adjusted length. If we find such junk, 1720 * log a non-conformant disk message. 1721 */ 1722 static int 1723 strip_trailing(struct hsfs *fsp, char *nm, int len) 1724 { 1725 char *c; 1726 int trailing_junk = 0; 1727 1728 for (c = nm + len - 1; c > nm; c--) { 1729 if (*c == ' ' || *c == '\0') 1730 trailing_junk = 1; 1731 else 1732 break; 1733 } 1734 1735 if (trailing_junk) 1736 hs_log_bogus_disk_warning(fsp, HSFS_ERR_TRAILING_JUNK, 0); 1737 1738 return ((int)(c - nm + 1)); 1739 } 1740 1741 static int 1742 hs_namelen(struct hsfs *fsp, char *nm, int len) 1743 { 1744 char *p = nm + len; 1745 1746 if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) { 1747 return (len); 1748 } else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) { 1749 uint16_t c; 1750 1751 while (--p > &nm[1]) { 1752 c = *p; 1753 c |= *--p * 256; 1754 if (c == ';') 1755 return (p - nm); 1756 if (c < '0' || c > '9') { 1757 p++; 1758 return (p - nm); 1759 } 1760 } 1761 } else { 1762 char c; 1763 1764 while (--p > nm) { 1765 c = *p; 1766 if (c == ';') 1767 return (p - nm); 1768 if (c < '0' || c > '9') { 1769 p++; 1770 return (p - nm); 1771 } 1772 } 1773 } 1774 return (len); 1775 } 1776 1777 /* 1778 * Take a UCS-2 character and convert 1779 * it into a utf8 character. 1780 * A 0 will be returned if the conversion fails 1781 * 1782 * See http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 1783 * 1784 * The code has been taken from udfs/udf_subr.c 1785 */ 1786 static uint8_t hs_first_byte_mark[7] = 1787 { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; 1788 static int32_t 1789 hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8) 1790 { 1791 int32_t nc; 1792 uint32_t c_32; 1793 uint32_t byte_mask = 0xBF; 1794 uint32_t byte_mark = 0x80; 1795 1796 /* 1797 * Convert the 16-bit character to a 32-bit character 1798 */ 1799 c_32 = c_16; 1800 1801 /* 1802 * By here the 16-bit character is converted 1803 * to a 32-bit wide character 1804 */ 1805 if (c_32 < 0x80) { 1806 nc = 1; 1807 } else if (c_32 < 0x800) { 1808 nc = 2; 1809 } else if (c_32 < 0x10000) { 1810 nc = 3; 1811 } else if (c_32 < 0x200000) { 1812 nc = 4; 1813 } else if (c_32 < 0x4000000) { 1814 nc = 5; 1815 } else if (c_32 <= 0x7FFFFFFF) { /* avoid signed overflow */ 1816 nc = 6; 1817 } else { 1818 nc = 0; 1819 } 1820 s_8 += nc; 1821 switch (nc) { 1822 case 6 : 1823 *(--s_8) = (c_32 | byte_mark) & byte_mask; 1824 c_32 >>= 6; 1825 /* FALLTHROUGH */ 1826 case 5 : 1827 *(--s_8) = (c_32 | byte_mark) & byte_mask; 1828 c_32 >>= 6; 1829 /* FALLTHROUGH */ 1830 case 4 : 1831 *(--s_8) = (c_32 | byte_mark) & byte_mask; 1832 c_32 >>= 6; 1833 /* FALLTHROUGH */ 1834 case 3 : 1835 *(--s_8) = (c_32 | byte_mark) & byte_mask; 1836 c_32 >>= 6; 1837 /* FALLTHROUGH */ 1838 case 2 : 1839 *(--s_8) = (c_32 | byte_mark) & byte_mask; 1840 c_32 >>= 6; 1841 /* FALLTHROUGH */ 1842 case 1 : 1843 *(--s_8) = c_32 | hs_first_byte_mark[nc]; 1844 } 1845 return (nc); 1846 } 1847