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 * Vnode operations for the High Sierra filesystem 30 */ 31 32 #include <sys/types.h> 33 #include <sys/t_lock.h> 34 #include <sys/param.h> 35 #include <sys/time.h> 36 #include <sys/systm.h> 37 #include <sys/sysmacros.h> 38 #include <sys/resource.h> 39 #include <sys/signal.h> 40 #include <sys/cred.h> 41 #include <sys/user.h> 42 #include <sys/buf.h> 43 #include <sys/vfs.h> 44 #include <sys/vfs_opreg.h> 45 #include <sys/stat.h> 46 #include <sys/vnode.h> 47 #include <sys/mode.h> 48 #include <sys/proc.h> 49 #include <sys/disp.h> 50 #include <sys/file.h> 51 #include <sys/fcntl.h> 52 #include <sys/flock.h> 53 #include <sys/kmem.h> 54 #include <sys/uio.h> 55 #include <sys/conf.h> 56 #include <sys/errno.h> 57 #include <sys/mman.h> 58 #include <sys/pathname.h> 59 #include <sys/debug.h> 60 #include <sys/vmsystm.h> 61 #include <sys/cmn_err.h> 62 #include <sys/fbuf.h> 63 #include <sys/dirent.h> 64 #include <sys/errno.h> 65 66 #include <vm/hat.h> 67 #include <vm/page.h> 68 #include <vm/pvn.h> 69 #include <vm/as.h> 70 #include <vm/seg.h> 71 #include <vm/seg_map.h> 72 #include <vm/seg_kmem.h> 73 #include <vm/seg_vn.h> 74 #include <vm/rm.h> 75 #include <vm/page.h> 76 #include <sys/swap.h> 77 78 #include <sys/fs/hsfs_spec.h> 79 #include <sys/fs/hsfs_node.h> 80 #include <sys/fs/hsfs_impl.h> 81 #include <sys/fs/hsfs_susp.h> 82 #include <sys/fs/hsfs_rrip.h> 83 84 #include <fs/fs_subr.h> 85 86 /* 87 * This tunable allows us to ignore inode numbers from rrip-1.12. 88 * In this case, we fall back to our default inode algorithm. 89 */ 90 extern int use_rrip_inodes; 91 92 93 /* ARGSUSED */ 94 static int 95 hsfs_fsync(vnode_t *cp, int syncflag, cred_t *cred) 96 { 97 return (0); 98 } 99 100 101 /*ARGSUSED*/ 102 static int 103 hsfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred, 104 struct caller_context *ct) 105 { 106 caddr_t base; 107 offset_t diff; 108 int error; 109 struct hsnode *hp; 110 uint_t filesize; 111 112 hp = VTOH(vp); 113 /* 114 * if vp is of type VDIR, make sure dirent 115 * is filled up with all info (because of ptbl) 116 */ 117 if (vp->v_type == VDIR) { 118 if (hp->hs_dirent.ext_size == 0) 119 hs_filldirent(vp, &hp->hs_dirent); 120 } 121 filesize = hp->hs_dirent.ext_size; 122 123 /* Sanity checks. */ 124 if (uiop->uio_resid == 0 || /* No data wanted. */ 125 uiop->uio_loffset > HS_MAXFILEOFF || /* Offset too big. */ 126 uiop->uio_loffset >= filesize) /* Past EOF. */ 127 return (0); 128 129 do { 130 /* 131 * We want to ask for only the "right" amount of data. 132 * In this case that means:- 133 * 134 * We can't get data from beyond our EOF. If asked, 135 * we will give a short read. 136 * 137 * segmap_getmapflt returns buffers of MAXBSIZE bytes. 138 * These buffers are always MAXBSIZE aligned. 139 * If our starting offset is not MAXBSIZE aligned, 140 * we can only ask for less than MAXBSIZE bytes. 141 * 142 * If our requested offset and length are such that 143 * they belong in different MAXBSIZE aligned slots 144 * then we'll be making more than one call on 145 * segmap_getmapflt. 146 * 147 * This diagram shows the variables we use and their 148 * relationships. 149 * 150 * |<-----MAXBSIZE----->| 151 * +--------------------------...+ 152 * |.....mapon->|<--n-->|....*...|EOF 153 * +--------------------------...+ 154 * uio_loffset->| 155 * uio_resid....|<---------->| 156 * diff.........|<-------------->| 157 * 158 * So, in this case our offset is not aligned 159 * and our request takes us outside of the 160 * MAXBSIZE window. We will break this up into 161 * two segmap_getmapflt calls. 162 */ 163 size_t nbytes; 164 offset_t mapon; 165 size_t n; 166 uint_t flags; 167 168 mapon = uiop->uio_loffset & MAXBOFFSET; 169 diff = filesize - uiop->uio_loffset; 170 nbytes = (size_t)MIN(MAXBSIZE - mapon, uiop->uio_resid); 171 n = MIN(diff, nbytes); 172 if (n <= 0) { 173 /* EOF or request satisfied. */ 174 return (0); 175 } 176 177 base = segmap_getmapflt(segkmap, vp, 178 (u_offset_t)uiop->uio_loffset, n, 1, S_READ); 179 180 error = uiomove(base + mapon, n, UIO_READ, uiop); 181 182 if (error == 0) { 183 /* 184 * if read a whole block, or read to eof, 185 * won't need this buffer again soon. 186 */ 187 if (n + mapon == MAXBSIZE || 188 uiop->uio_loffset == filesize) 189 flags = SM_DONTNEED; 190 else 191 flags = 0; 192 error = segmap_release(segkmap, base, flags); 193 } else 194 (void) segmap_release(segkmap, base, 0); 195 } while (error == 0 && uiop->uio_resid > 0); 196 197 return (error); 198 } 199 200 /*ARGSUSED2*/ 201 static int 202 hsfs_getattr( 203 struct vnode *vp, 204 struct vattr *vap, 205 int flags, 206 struct cred *cred) 207 { 208 struct hsnode *hp; 209 struct vfs *vfsp; 210 struct hsfs *fsp; 211 212 hp = VTOH(vp); 213 fsp = VFS_TO_HSFS(vp->v_vfsp); 214 vfsp = vp->v_vfsp; 215 216 if ((hp->hs_dirent.ext_size == 0) && (vp->v_type == VDIR)) { 217 hs_filldirent(vp, &hp->hs_dirent); 218 } 219 vap->va_type = IFTOVT(hp->hs_dirent.mode); 220 vap->va_mode = hp->hs_dirent.mode; 221 vap->va_uid = hp->hs_dirent.uid; 222 vap->va_gid = hp->hs_dirent.gid; 223 224 vap->va_fsid = vfsp->vfs_dev; 225 vap->va_nodeid = (ino64_t)hp->hs_nodeid; 226 vap->va_nlink = hp->hs_dirent.nlink; 227 vap->va_size = (offset_t)hp->hs_dirent.ext_size; 228 229 vap->va_atime.tv_sec = hp->hs_dirent.adate.tv_sec; 230 vap->va_atime.tv_nsec = hp->hs_dirent.adate.tv_usec*1000; 231 vap->va_mtime.tv_sec = hp->hs_dirent.mdate.tv_sec; 232 vap->va_mtime.tv_nsec = hp->hs_dirent.mdate.tv_usec*1000; 233 vap->va_ctime.tv_sec = hp->hs_dirent.cdate.tv_sec; 234 vap->va_ctime.tv_nsec = hp->hs_dirent.cdate.tv_usec*1000; 235 if (vp->v_type == VCHR || vp->v_type == VBLK) 236 vap->va_rdev = hp->hs_dirent.r_dev; 237 else 238 vap->va_rdev = 0; 239 vap->va_blksize = vfsp->vfs_bsize; 240 /* no. of blocks = no. of data blocks + no. of xar blocks */ 241 vap->va_nblocks = (fsblkcnt64_t)howmany(vap->va_size + (u_longlong_t) 242 (hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift), DEV_BSIZE); 243 vap->va_seq = hp->hs_seq; 244 return (0); 245 } 246 247 /*ARGSUSED*/ 248 static int 249 hsfs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred) 250 { 251 struct hsnode *hp; 252 253 if (vp->v_type != VLNK) 254 return (EINVAL); 255 256 hp = VTOH(vp); 257 258 if (hp->hs_dirent.sym_link == (char *)NULL) 259 return (ENOENT); 260 261 return (uiomove(hp->hs_dirent.sym_link, 262 (size_t)MIN(hp->hs_dirent.ext_size, 263 uiop->uio_resid), UIO_READ, uiop)); 264 } 265 266 /*ARGSUSED*/ 267 static void 268 hsfs_inactive(struct vnode *vp, struct cred *cred) 269 { 270 struct hsnode *hp; 271 struct hsfs *fsp; 272 273 int nopage; 274 275 hp = VTOH(vp); 276 fsp = VFS_TO_HSFS(vp->v_vfsp); 277 /* 278 * Note: acquiring and holding v_lock for quite a while 279 * here serializes on the vnode; this is unfortunate, but 280 * likely not to overly impact performance, as the underlying 281 * device (CDROM drive) is quite slow. 282 */ 283 rw_enter(&fsp->hsfs_hash_lock, RW_WRITER); 284 mutex_enter(&hp->hs_contents_lock); 285 mutex_enter(&vp->v_lock); 286 287 if (vp->v_count < 1) { 288 panic("hsfs_inactive: v_count < 1"); 289 /*NOTREACHED*/ 290 } 291 292 if (vp->v_count > 1 || (hp->hs_flags & HREF) == 0) { 293 vp->v_count--; /* release hold from vn_rele */ 294 mutex_exit(&vp->v_lock); 295 mutex_exit(&hp->hs_contents_lock); 296 rw_exit(&fsp->hsfs_hash_lock); 297 return; 298 } 299 vp->v_count--; /* release hold from vn_rele */ 300 if (vp->v_count == 0) { 301 /* 302 * Free the hsnode. 303 * If there are no pages associated with the 304 * hsnode, give it back to the kmem_cache, 305 * else put at the end of this file system's 306 * internal free list. 307 */ 308 nopage = !vn_has_cached_data(vp); 309 hp->hs_flags = 0; 310 /* 311 * exit these locks now, since hs_freenode may 312 * kmem_free the hsnode and embedded vnode 313 */ 314 mutex_exit(&vp->v_lock); 315 mutex_exit(&hp->hs_contents_lock); 316 hs_freenode(vp, fsp, nopage); 317 } else { 318 mutex_exit(&vp->v_lock); 319 mutex_exit(&hp->hs_contents_lock); 320 } 321 rw_exit(&fsp->hsfs_hash_lock); 322 } 323 324 325 /*ARGSUSED*/ 326 static int 327 hsfs_lookup( 328 struct vnode *dvp, 329 char *nm, 330 struct vnode **vpp, 331 struct pathname *pnp, 332 int flags, 333 struct vnode *rdir, 334 struct cred *cred) 335 { 336 int error; 337 int namelen = (int)strlen(nm); 338 339 if (*nm == '\0') { 340 VN_HOLD(dvp); 341 *vpp = dvp; 342 return (0); 343 } 344 345 /* 346 * If we're looking for ourself, life is simple. 347 */ 348 if (namelen == 1 && *nm == '.') { 349 if (error = hs_access(dvp, (mode_t)VEXEC, cred)) 350 return (error); 351 VN_HOLD(dvp); 352 *vpp = dvp; 353 return (0); 354 } 355 356 return (hs_dirlook(dvp, nm, namelen, vpp, cred)); 357 } 358 359 360 /*ARGSUSED*/ 361 static int 362 hsfs_readdir( 363 struct vnode *vp, 364 struct uio *uiop, 365 struct cred *cred, 366 int *eofp) 367 { 368 struct hsnode *dhp; 369 struct hsfs *fsp; 370 struct hs_direntry hd; 371 struct dirent64 *nd; 372 int error; 373 uint_t offset; /* real offset in directory */ 374 uint_t dirsiz; /* real size of directory */ 375 uchar_t *blkp; 376 int hdlen; /* length of hs directory entry */ 377 long ndlen; /* length of dirent entry */ 378 int bytes_wanted; 379 size_t bufsize; /* size of dirent buffer */ 380 char *outbuf; /* ptr to dirent buffer */ 381 char *dname; 382 int dnamelen; 383 size_t dname_size; 384 struct fbuf *fbp; 385 uint_t last_offset; /* last index into current dir block */ 386 ino64_t dirino; /* temporary storage before storing in dirent */ 387 off_t diroff; 388 389 dhp = VTOH(vp); 390 fsp = VFS_TO_HSFS(vp->v_vfsp); 391 if (dhp->hs_dirent.ext_size == 0) 392 hs_filldirent(vp, &dhp->hs_dirent); 393 dirsiz = dhp->hs_dirent.ext_size; 394 if (uiop->uio_loffset >= dirsiz) { /* at or beyond EOF */ 395 if (eofp) 396 *eofp = 1; 397 return (0); 398 } 399 ASSERT(uiop->uio_loffset <= HS_MAXFILEOFF); 400 offset = uiop->uio_loffset; 401 402 dname_size = fsp->hsfs_namemax + 1; /* 1 for the ending NUL */ 403 dname = kmem_alloc(dname_size, KM_SLEEP); 404 bufsize = uiop->uio_resid + sizeof (struct dirent64); 405 406 outbuf = kmem_alloc(bufsize, KM_SLEEP); 407 nd = (struct dirent64 *)outbuf; 408 409 while (offset < dirsiz) { 410 bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK)); 411 412 error = fbread(vp, (offset_t)(offset & MAXBMASK), 413 (unsigned int)bytes_wanted, S_READ, &fbp); 414 if (error) 415 goto done; 416 417 blkp = (uchar_t *)fbp->fb_addr; 418 last_offset = (offset & MAXBMASK) + fbp->fb_count; 419 420 #define rel_offset(offset) ((offset) & MAXBOFFSET) /* index into blkp */ 421 422 while (offset < last_offset) { 423 /* 424 * Very similar validation code is found in 425 * process_dirblock(), hsfs_node.c. 426 * For an explanation, see there. 427 * It may make sense for the future to 428 * "consolidate" the code in hs_parsedir(), 429 * process_dirblock() and hsfs_readdir() into 430 * a single utility function. 431 */ 432 hdlen = (int)((uchar_t) 433 HDE_DIR_LEN(&blkp[rel_offset(offset)])); 434 if (hdlen < HDE_ROOT_DIR_REC_SIZE || 435 offset + hdlen > last_offset) { 436 /* 437 * advance to next sector boundary 438 */ 439 offset = roundup(offset + 1, HS_SECTOR_SIZE); 440 if (hdlen) 441 hs_log_bogus_disk_warning(fsp, 442 HSFS_ERR_TRAILING_JUNK, 0); 443 444 continue; 445 } 446 447 bzero(&hd, sizeof (hd)); 448 449 /* 450 * Just ignore invalid directory entries. 451 * XXX - maybe hs_parsedir() will detect EXISTENCE bit 452 */ 453 if (!hs_parsedir(fsp, &blkp[rel_offset(offset)], 454 &hd, dname, &dnamelen, last_offset - offset)) { 455 /* 456 * Determine if there is enough room 457 */ 458 ndlen = (long)DIRENT64_RECLEN((dnamelen)); 459 460 if ((ndlen + ((char *)nd - outbuf)) > 461 uiop->uio_resid) { 462 fbrelse(fbp, S_READ); 463 goto done; /* output buffer full */ 464 } 465 466 diroff = offset + hdlen; 467 /* 468 * If the media carries rrip-v1.12 or newer, 469 * and we trust the inodes from the rrip data 470 * (use_rrip_inodes != 0), use that data. If the 471 * media has been created by a recent mkisofs 472 * version, we may trust all numbers in the 473 * starting extent number; otherwise, we cannot 474 * do this for zero sized files and symlinks, 475 * because if we did we'd end up mapping all of 476 * them to the same node. We use HS_DUMMY_INO 477 * in this case and make sure that we will not 478 * map all files to the same meta data. 479 */ 480 if (hd.inode != 0 && use_rrip_inodes) { 481 dirino = hd.inode; 482 } else if ((hd.ext_size == 0 || 483 hd.sym_link != (char *)NULL) && 484 (fsp->hsfs_flags & HSFSMNT_INODE) == 0) { 485 dirino = HS_DUMMY_INO; 486 } else { 487 dirino = hd.ext_lbn; 488 } 489 490 /* strncpy(9f) will zero uninitialized bytes */ 491 492 ASSERT(strlen(dname) + 1 <= 493 DIRENT64_NAMELEN(ndlen)); 494 (void) strncpy(nd->d_name, dname, 495 DIRENT64_NAMELEN(ndlen)); 496 nd->d_reclen = (ushort_t)ndlen; 497 nd->d_off = (offset_t)diroff; 498 nd->d_ino = dirino; 499 nd = (struct dirent64 *)((char *)nd + ndlen); 500 501 /* 502 * free up space allocated for symlink 503 */ 504 if (hd.sym_link != (char *)NULL) { 505 kmem_free(hd.sym_link, 506 (size_t)(hd.ext_size+1)); 507 hd.sym_link = (char *)NULL; 508 } 509 } 510 offset += hdlen; 511 } 512 fbrelse(fbp, S_READ); 513 } 514 515 /* 516 * Got here for one of the following reasons: 517 * 1) outbuf is full (error == 0) 518 * 2) end of directory reached (error == 0) 519 * 3) error reading directory sector (error != 0) 520 * 4) directory entry crosses sector boundary (error == 0) 521 * 522 * If any directory entries have been copied, don't report 523 * case 4. Instead, return the valid directory entries. 524 * 525 * If no entries have been copied, report the error. 526 * If case 4, this will be indistiguishable from EOF. 527 */ 528 done: 529 ndlen = ((char *)nd - outbuf); 530 if (ndlen != 0) { 531 error = uiomove(outbuf, (size_t)ndlen, UIO_READ, uiop); 532 uiop->uio_loffset = offset; 533 } 534 kmem_free(dname, dname_size); 535 kmem_free(outbuf, bufsize); 536 if (eofp && error == 0) 537 *eofp = (uiop->uio_loffset >= dirsiz); 538 return (error); 539 } 540 541 static int 542 hsfs_fid(struct vnode *vp, struct fid *fidp) 543 { 544 struct hsnode *hp; 545 struct hsfid *fid; 546 547 if (fidp->fid_len < (sizeof (*fid) - sizeof (fid->hf_len))) { 548 fidp->fid_len = sizeof (*fid) - sizeof (fid->hf_len); 549 return (ENOSPC); 550 } 551 552 fid = (struct hsfid *)fidp; 553 fid->hf_len = sizeof (*fid) - sizeof (fid->hf_len); 554 hp = VTOH(vp); 555 mutex_enter(&hp->hs_contents_lock); 556 fid->hf_dir_lbn = hp->hs_dir_lbn; 557 fid->hf_dir_off = (ushort_t)hp->hs_dir_off; 558 fid->hf_ino = hp->hs_nodeid; 559 mutex_exit(&hp->hs_contents_lock); 560 return (0); 561 } 562 563 /*ARGSUSED*/ 564 static int 565 hsfs_open(struct vnode **vpp, int flag, struct cred *cred) 566 { 567 return (0); 568 } 569 570 /*ARGSUSED*/ 571 static int 572 hsfs_close( 573 struct vnode *vp, 574 int flag, 575 int count, 576 offset_t offset, 577 struct cred *cred) 578 { 579 (void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 580 cleanshares(vp, ttoproc(curthread)->p_pid); 581 return (0); 582 } 583 584 /*ARGSUSED2*/ 585 static int 586 hsfs_access(struct vnode *vp, int mode, int flags, cred_t *cred) 587 { 588 return (hs_access(vp, (mode_t)mode, cred)); 589 } 590 591 /* 592 * the seek time of a CD-ROM is very slow, and data transfer 593 * rate is even worse (max. 150K per sec). The design 594 * decision is to reduce access to cd-rom as much as possible, 595 * and to transfer a sizable block (read-ahead) of data at a time. 596 * UFS style of read ahead one block at a time is not appropriate, 597 * and is not supported 598 */ 599 600 /* 601 * KLUSTSIZE should be a multiple of PAGESIZE and <= MAXPHYS. 602 */ 603 #define KLUSTSIZE (56 * 1024) 604 /* we don't support read ahead */ 605 int hsfs_lostpage; /* no. of times we lost original page */ 606 607 /* 608 * Used to prevent biodone() from releasing buf resources that 609 * we didn't allocate in quite the usual way. 610 */ 611 /*ARGSUSED*/ 612 int 613 hsfs_iodone(struct buf *bp) 614 { 615 sema_v(&bp->b_io); 616 return (0); 617 } 618 619 /* 620 * Each file may have a different interleaving on disk. This makes 621 * things somewhat interesting. The gist is that there are some 622 * number of contiguous data sectors, followed by some other number 623 * of contiguous skip sectors. The sum of those two sets of sectors 624 * defines the interleave size. Unfortunately, it means that we generally 625 * can't simply read N sectors starting at a given offset to satisfy 626 * any given request. 627 * 628 * What we do is get the relevant memory pages via pvn_read_kluster(), 629 * then stride through the interleaves, setting up a buf for each 630 * sector that needs to be brought in. Instead of kmem_alloc'ing 631 * space for the sectors, though, we just point at the appropriate 632 * spot in the relevant page for each of them. This saves us a bunch 633 * of copying. 634 */ 635 /*ARGSUSED*/ 636 static int 637 hsfs_getapage( 638 struct vnode *vp, 639 u_offset_t off, 640 size_t len, 641 uint_t *protp, 642 struct page *pl[], 643 size_t plsz, 644 struct seg *seg, 645 caddr_t addr, 646 enum seg_rw rw, 647 struct cred *cred) 648 { 649 struct hsnode *hp; 650 struct hsfs *fsp; 651 int err; 652 struct buf *bufs; 653 caddr_t *vas; 654 caddr_t va; 655 struct page *pp, *searchp, *lastp; 656 page_t *pagefound; 657 offset_t bof; 658 struct vnode *devvp; 659 ulong_t byte_offset; 660 size_t io_len_tmp; 661 uint_t io_off, io_len; 662 uint_t xlen; 663 uint_t filsiz; 664 uint_t secsize; 665 uint_t bufcnt; 666 uint_t bufsused; 667 uint_t count; 668 uint_t io_end; 669 uint_t which_chunk_lbn; 670 uint_t offset_lbn; 671 uint_t offset_extra; 672 offset_t offset_bytes; 673 uint_t remaining_bytes; 674 uint_t extension; 675 int remainder; /* must be signed */ 676 int chunk_lbn_count; 677 int chunk_data_bytes; 678 int xarsiz; 679 diskaddr_t driver_block; 680 u_offset_t io_off_tmp; 681 682 /* 683 * We don't support asynchronous operation at the moment, so 684 * just pretend we did it. If the pages are ever actually 685 * needed, they'll get brought in then. 686 */ 687 if (pl == NULL) 688 return (0); 689 690 hp = VTOH(vp); 691 fsp = VFS_TO_HSFS(vp->v_vfsp); 692 devvp = fsp->hsfs_devvp; 693 secsize = fsp->hsfs_vol.lbn_size; /* bytes per logical block */ 694 695 /* file data size */ 696 filsiz = hp->hs_dirent.ext_size; 697 698 /* disk addr for start of file */ 699 bof = LBN_TO_BYTE((offset_t)hp->hs_dirent.ext_lbn, vp->v_vfsp); 700 701 /* xarsiz byte must be skipped for data */ 702 xarsiz = hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift; 703 704 /* how many logical blocks in an interleave (data+skip) */ 705 chunk_lbn_count = hp->hs_dirent.intlf_sz + hp->hs_dirent.intlf_sk; 706 707 if (chunk_lbn_count == 0) { 708 chunk_lbn_count = 1; 709 } 710 711 /* 712 * Convert interleaving size into bytes. The zero case 713 * (no interleaving) optimization is handled as a side- 714 * effect of the read-ahead logic. 715 */ 716 if (hp->hs_dirent.intlf_sz == 0) { 717 chunk_data_bytes = LBN_TO_BYTE(1, vp->v_vfsp); 718 } else { 719 chunk_data_bytes = 720 LBN_TO_BYTE(hp->hs_dirent.intlf_sz, vp->v_vfsp); 721 } 722 723 reread: 724 err = 0; 725 pagefound = 0; 726 727 /* 728 * Do some read-ahead. This mostly saves us a bit of 729 * system cpu time more than anything else when doing 730 * sequential reads. At some point, could do the 731 * read-ahead asynchronously which might gain us something 732 * on wall time, but it seems unlikely.... 733 * 734 * We do the easy case here, which is to read through 735 * the end of the chunk, minus whatever's at the end that 736 * won't exactly fill a page. 737 */ 738 which_chunk_lbn = (off + len) / chunk_data_bytes; 739 extension = ((which_chunk_lbn + 1) * chunk_data_bytes) - off; 740 extension -= (extension % PAGESIZE); 741 if (extension != 0 && extension < filsiz - off) { 742 len = extension; 743 } else { 744 len = PAGESIZE; 745 } 746 /* 747 * Some cd writers don't write sectors that aren't used. Also, 748 * there's no point in reading sectors we'll never look at. So, 749 * if we're asked to go beyond the end of a file, truncate to the 750 * length of that file. 751 * 752 * Additionally, this behaviour is required by section 6.4.5 of 753 * ISO 9660:1988(E). 754 */ 755 if (len > (filsiz - off)) { 756 len = filsiz - off; 757 } 758 759 /* A little paranoia. */ 760 ASSERT(len > 0); 761 762 /* 763 * After all that, make sure we're asking for things in units 764 * that bdev_strategy() will understand (see bug 4202551). 765 */ 766 len = roundup(len, DEV_BSIZE); 767 768 pp = NULL; 769 again: 770 /* search for page in buffer */ 771 if ((pagefound = page_exists(vp, off)) == 0) { 772 /* 773 * Need to really do disk IO to get the page. 774 */ 775 pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp, 776 &io_len_tmp, off, len, 0); 777 778 if (pp == NULL) 779 goto again; 780 781 io_off = (uint_t)io_off_tmp; 782 io_len = (uint_t)io_len_tmp; 783 784 /* check for truncation */ 785 /* 786 * xxx Clean up and return EIO instead? 787 * xxx Ought to go to u_offset_t for everything, but we 788 * xxx call lots of things that want uint_t arguments. 789 */ 790 ASSERT(io_off == io_off_tmp); 791 792 /* 793 * get enough buffers for worst-case scenario 794 * (i.e., no coalescing possible). 795 */ 796 bufcnt = (len + secsize - 1) / secsize; 797 bufs = kmem_zalloc(bufcnt * sizeof (struct buf), KM_SLEEP); 798 vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP); 799 for (count = 0; count < bufcnt; count++) { 800 bufs[count].b_edev = devvp->v_rdev; 801 bufs[count].b_dev = cmpdev(devvp->v_rdev); 802 bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ; 803 bufs[count].b_iodone = hsfs_iodone; 804 bufs[count].b_vp = vp; 805 bufs[count].b_file = vp; 806 sema_init(&bufs[count].b_io, 0, NULL, 807 SEMA_DEFAULT, NULL); 808 sema_init(&bufs[count].b_sem, 0, NULL, 809 SEMA_DEFAULT, NULL); 810 } 811 812 /* 813 * If our filesize is not an integer multiple of PAGESIZE, 814 * we zero that part of the last page that's between EOF and 815 * the PAGESIZE boundary. 816 */ 817 xlen = io_len & PAGEOFFSET; 818 if (xlen != 0) 819 pagezero(pp->p_prev, xlen, PAGESIZE - xlen); 820 821 va = NULL; 822 lastp = NULL; 823 searchp = pp; 824 io_end = io_off + io_len; 825 for (count = 0, byte_offset = io_off; 826 byte_offset < io_end; count++) { 827 ASSERT(count < bufcnt); 828 829 /* Compute disk address for interleaving. */ 830 831 /* considered without skips */ 832 which_chunk_lbn = byte_offset / chunk_data_bytes; 833 834 /* factor in skips */ 835 offset_lbn = which_chunk_lbn * chunk_lbn_count; 836 837 /* convert to physical byte offset for lbn */ 838 offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp); 839 840 /* don't forget offset into lbn */ 841 offset_extra = byte_offset % chunk_data_bytes; 842 843 /* get virtual block number for driver */ 844 driver_block = 845 lbtodb(bof + xarsiz + offset_bytes + offset_extra); 846 847 if (lastp != searchp) { 848 /* this branch taken first time through loop */ 849 va = vas[count] = 850 ppmapin(searchp, PROT_WRITE, (caddr_t)-1); 851 /* ppmapin() guarantees not to return NULL */ 852 } else { 853 vas[count] = NULL; 854 } 855 856 bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE; 857 bufs[count].b_offset = 858 (offset_t)(byte_offset - io_off + off); 859 860 /* 861 * We specifically use the b_lblkno member here 862 * as even in the 32 bit world driver_block can 863 * get very large in line with the ISO9660 spec. 864 */ 865 866 bufs[count].b_lblkno = driver_block; 867 868 remaining_bytes = 869 ((which_chunk_lbn + 1) * chunk_data_bytes) 870 - byte_offset; 871 872 /* 873 * remaining_bytes can't be zero, as we derived 874 * which_chunk_lbn directly from byte_offset. 875 */ 876 if ((remaining_bytes + byte_offset) < (off + len)) { 877 /* coalesce-read the rest of the chunk */ 878 bufs[count].b_bcount = remaining_bytes; 879 } else { 880 /* get the final bits */ 881 bufs[count].b_bcount = off + len - byte_offset; 882 } 883 884 /* 885 * It would be nice to do multiple pages' 886 * worth at once here when the opportunity 887 * arises, as that has been shown to improve 888 * our wall time. However, to do that 889 * requires that we use the pageio subsystem, 890 * which doesn't mix well with what we're 891 * already using here. We can't use pageio 892 * all the time, because that subsystem 893 * assumes that a page is stored in N 894 * contiguous blocks on the device. 895 * Interleaving violates that assumption. 896 */ 897 898 remainder = PAGESIZE - (byte_offset % PAGESIZE); 899 if (bufs[count].b_bcount > remainder) { 900 bufs[count].b_bcount = remainder; 901 } 902 903 bufs[count].b_bufsize = bufs[count].b_bcount; 904 if (((offset_t)byte_offset + bufs[count].b_bcount) > 905 HS_MAXFILEOFF) { 906 break; 907 } 908 byte_offset += bufs[count].b_bcount; 909 910 (void) bdev_strategy(&bufs[count]); 911 912 lwp_stat_update(LWP_STAT_INBLK, 1); 913 lastp = searchp; 914 if ((remainder - bufs[count].b_bcount) < 1) { 915 searchp = searchp->p_next; 916 } 917 } 918 919 bufsused = count; 920 /* Now wait for everything to come in */ 921 for (count = 0; count < bufsused; count++) { 922 if (err == 0) { 923 err = biowait(&bufs[count]); 924 } else 925 (void) biowait(&bufs[count]); 926 } 927 928 /* Don't leak resources */ 929 for (count = 0; count < bufcnt; count++) { 930 sema_destroy(&bufs[count].b_io); 931 sema_destroy(&bufs[count].b_sem); 932 if (count < bufsused && vas[count] != NULL) { 933 ppmapout(vas[count]); 934 } 935 } 936 937 kmem_free(vas, bufcnt * sizeof (caddr_t)); 938 kmem_free(bufs, bufcnt * sizeof (struct buf)); 939 } 940 941 if (err) { 942 pvn_read_done(pp, B_ERROR); 943 return (err); 944 } 945 946 /* 947 * Lock the requested page, and the one after it if possible. 948 * Don't bother if our caller hasn't given us a place to stash 949 * the page pointers, since otherwise we'd lock pages that would 950 * never get unlocked. 951 */ 952 if (pagefound) { 953 int index; 954 ulong_t soff; 955 956 /* 957 * Make sure it's in memory before we say it's here. 958 */ 959 if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) { 960 hsfs_lostpage++; 961 goto reread; 962 } 963 964 pl[0] = pp; 965 index = 1; 966 967 /* 968 * Try to lock the next page, if it exists, without 969 * blocking. 970 */ 971 plsz -= PAGESIZE; 972 /* LINTED (plsz is unsigned) */ 973 for (soff = off + PAGESIZE; plsz > 0; 974 soff += PAGESIZE, plsz -= PAGESIZE) { 975 pp = page_lookup_nowait(vp, (u_offset_t)soff, 976 SE_SHARED); 977 if (pp == NULL) 978 break; 979 pl[index++] = pp; 980 } 981 pl[index] = NULL; 982 return (0); 983 } 984 985 if (pp != NULL) { 986 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 987 } 988 989 return (err); 990 } 991 992 static int 993 hsfs_getpage( 994 struct vnode *vp, 995 offset_t off, 996 size_t len, 997 uint_t *protp, 998 struct page *pl[], 999 size_t plsz, 1000 struct seg *seg, 1001 caddr_t addr, 1002 enum seg_rw rw, 1003 struct cred *cred) 1004 { 1005 int err; 1006 uint_t filsiz; 1007 struct hsnode *hp = VTOH(vp); 1008 1009 /* does not support write */ 1010 if (rw == S_WRITE) { 1011 panic("write attempt on READ ONLY HSFS"); 1012 /*NOTREACHED*/ 1013 } 1014 1015 if (vp->v_flag & VNOMAP) { 1016 return (ENOSYS); 1017 } 1018 1019 ASSERT(off <= HS_MAXFILEOFF); 1020 1021 /* 1022 * Determine file data size for EOF check. 1023 */ 1024 filsiz = hp->hs_dirent.ext_size; 1025 if ((off + len) > (offset_t)(filsiz + PAGEOFFSET) && seg != segkmap) 1026 return (EFAULT); /* beyond EOF */ 1027 1028 if (protp != NULL) 1029 *protp = PROT_ALL; 1030 1031 if (len <= PAGESIZE) 1032 err = hsfs_getapage(vp, (u_offset_t)off, len, protp, pl, plsz, 1033 seg, addr, rw, cred); 1034 else 1035 err = pvn_getpages(hsfs_getapage, vp, off, len, protp, 1036 pl, plsz, seg, addr, rw, cred); 1037 1038 return (err); 1039 } 1040 1041 1042 1043 /* 1044 * This function should never be called. We need to have it to pass 1045 * it as an argument to other functions. 1046 */ 1047 /*ARGSUSED*/ 1048 int 1049 hsfs_putapage( 1050 vnode_t *vp, 1051 page_t *pp, 1052 u_offset_t *offp, 1053 size_t *lenp, 1054 int flags, 1055 cred_t *cr) 1056 { 1057 /* should never happen - just destroy it */ 1058 cmn_err(CE_NOTE, "hsfs_putapage: dirty HSFS page"); 1059 pvn_write_done(pp, B_ERROR | B_WRITE | B_INVAL | B_FORCE | flags); 1060 return (0); 1061 } 1062 1063 1064 /* 1065 * The only flags we support are B_INVAL, B_FREE and B_DONTNEED. 1066 * B_INVAL is set by: 1067 * 1068 * 1) the MC_SYNC command of memcntl(2) to support the MS_INVALIDATE flag. 1069 * 2) the MC_ADVISE command of memcntl(2) with the MADV_DONTNEED advice 1070 * which translates to an MC_SYNC with the MS_INVALIDATE flag. 1071 * 1072 * The B_FREE (as well as the B_DONTNEED) flag is set when the 1073 * MADV_SEQUENTIAL advice has been used. VOP_PUTPAGE is invoked 1074 * from SEGVN to release pages behind a pagefault. 1075 */ 1076 /*ARGSUSED*/ 1077 static int 1078 hsfs_putpage( 1079 struct vnode *vp, 1080 offset_t off, 1081 size_t len, 1082 int flags, 1083 struct cred *cr) 1084 { 1085 int error = 0; 1086 1087 if (vp->v_count == 0) { 1088 panic("hsfs_putpage: bad v_count"); 1089 /*NOTREACHED*/ 1090 } 1091 1092 if (vp->v_flag & VNOMAP) 1093 return (ENOSYS); 1094 1095 ASSERT(off <= HS_MAXFILEOFF); 1096 1097 if (!vn_has_cached_data(vp)) /* no pages mapped */ 1098 return (0); 1099 1100 if (len == 0) { /* from 'off' to EOF */ 1101 error = pvn_vplist_dirty(vp, off, hsfs_putapage, flags, cr); 1102 } else { 1103 offset_t end_off = off + len; 1104 offset_t file_size = VTOH(vp)->hs_dirent.ext_size; 1105 offset_t io_off; 1106 1107 file_size = (file_size + PAGESIZE - 1) & PAGEMASK; 1108 if (end_off > file_size) 1109 end_off = file_size; 1110 1111 for (io_off = off; io_off < end_off; io_off += PAGESIZE) { 1112 page_t *pp; 1113 1114 /* 1115 * We insist on getting the page only if we are 1116 * about to invalidate, free or write it and 1117 * the B_ASYNC flag is not set. 1118 */ 1119 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 1120 pp = page_lookup(vp, io_off, 1121 (flags & (B_INVAL | B_FREE)) ? 1122 SE_EXCL : SE_SHARED); 1123 } else { 1124 pp = page_lookup_nowait(vp, io_off, 1125 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 1126 } 1127 1128 if (pp == NULL) 1129 continue; 1130 /* 1131 * Normally pvn_getdirty() should return 0, which 1132 * impies that it has done the job for us. 1133 * The shouldn't-happen scenario is when it returns 1. 1134 * This means that the page has been modified and 1135 * needs to be put back. 1136 * Since we can't write on a CD, we fake a failed 1137 * I/O and force pvn_write_done() to destroy the page. 1138 */ 1139 if (pvn_getdirty(pp, flags) == 1) { 1140 cmn_err(CE_NOTE, 1141 "hsfs_putpage: dirty HSFS page"); 1142 pvn_write_done(pp, flags | 1143 B_ERROR | B_WRITE | B_INVAL | B_FORCE); 1144 } 1145 } 1146 } 1147 return (error); 1148 } 1149 1150 1151 /*ARGSUSED*/ 1152 static int 1153 hsfs_map( 1154 struct vnode *vp, 1155 offset_t off, 1156 struct as *as, 1157 caddr_t *addrp, 1158 size_t len, 1159 uchar_t prot, 1160 uchar_t maxprot, 1161 uint_t flags, 1162 struct cred *cred) 1163 { 1164 struct segvn_crargs vn_a; 1165 int error; 1166 1167 /* VFS_RECORD(vp->v_vfsp, VS_MAP, VS_CALL); */ 1168 1169 if (vp->v_flag & VNOMAP) 1170 return (ENOSYS); 1171 1172 if (off > HS_MAXFILEOFF || off < 0 || 1173 (off + len) < 0 || (off + len) > HS_MAXFILEOFF) 1174 return (ENXIO); 1175 1176 if (vp->v_type != VREG) { 1177 return (ENODEV); 1178 } 1179 1180 /* 1181 * If file is being locked, disallow mapping. 1182 */ 1183 if (vn_has_mandatory_locks(vp, VTOH(vp)->hs_dirent.mode)) 1184 return (EAGAIN); 1185 1186 as_rangelock(as); 1187 1188 if ((flags & MAP_FIXED) == 0) { 1189 map_addr(addrp, len, off, 1, flags); 1190 if (*addrp == NULL) { 1191 as_rangeunlock(as); 1192 return (ENOMEM); 1193 } 1194 } else { 1195 /* 1196 * User specified address - blow away any previous mappings 1197 */ 1198 (void) as_unmap(as, *addrp, len); 1199 } 1200 1201 vn_a.vp = vp; 1202 vn_a.offset = off; 1203 vn_a.type = flags & MAP_TYPE; 1204 vn_a.prot = prot; 1205 vn_a.maxprot = maxprot; 1206 vn_a.flags = flags & ~MAP_TYPE; 1207 vn_a.cred = cred; 1208 vn_a.amp = NULL; 1209 vn_a.szc = 0; 1210 vn_a.lgrp_mem_policy_flags = 0; 1211 1212 error = as_map(as, *addrp, len, segvn_create, &vn_a); 1213 as_rangeunlock(as); 1214 return (error); 1215 } 1216 1217 /* ARGSUSED */ 1218 static int 1219 hsfs_addmap( 1220 struct vnode *vp, 1221 offset_t off, 1222 struct as *as, 1223 caddr_t addr, 1224 size_t len, 1225 uchar_t prot, 1226 uchar_t maxprot, 1227 uint_t flags, 1228 struct cred *cr) 1229 { 1230 struct hsnode *hp; 1231 1232 if (vp->v_flag & VNOMAP) 1233 return (ENOSYS); 1234 1235 hp = VTOH(vp); 1236 mutex_enter(&hp->hs_contents_lock); 1237 hp->hs_mapcnt += btopr(len); 1238 mutex_exit(&hp->hs_contents_lock); 1239 return (0); 1240 } 1241 1242 /*ARGSUSED*/ 1243 static int 1244 hsfs_delmap( 1245 struct vnode *vp, 1246 offset_t off, 1247 struct as *as, 1248 caddr_t addr, 1249 size_t len, 1250 uint_t prot, 1251 uint_t maxprot, 1252 uint_t flags, 1253 struct cred *cr) 1254 { 1255 struct hsnode *hp; 1256 1257 if (vp->v_flag & VNOMAP) 1258 return (ENOSYS); 1259 1260 hp = VTOH(vp); 1261 mutex_enter(&hp->hs_contents_lock); 1262 hp->hs_mapcnt -= btopr(len); /* Count released mappings */ 1263 ASSERT(hp->hs_mapcnt >= 0); 1264 mutex_exit(&hp->hs_contents_lock); 1265 return (0); 1266 } 1267 1268 /* ARGSUSED */ 1269 static int 1270 hsfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp) 1271 { 1272 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 1273 } 1274 1275 /* ARGSUSED */ 1276 static int 1277 hsfs_frlock( 1278 struct vnode *vp, 1279 int cmd, 1280 struct flock64 *bfp, 1281 int flag, 1282 offset_t offset, 1283 struct flk_callback *flk_cbp, 1284 cred_t *cr) 1285 { 1286 struct hsnode *hp = VTOH(vp); 1287 1288 /* 1289 * If the file is being mapped, disallow fs_frlock. 1290 * We are not holding the hs_contents_lock while checking 1291 * hs_mapcnt because the current locking strategy drops all 1292 * locks before calling fs_frlock. 1293 * So, hs_mapcnt could change before we enter fs_frlock making 1294 * it meaningless to have held hs_contents_lock in the first place. 1295 */ 1296 if (hp->hs_mapcnt > 0 && MANDLOCK(vp, hp->hs_dirent.mode)) 1297 return (EAGAIN); 1298 1299 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 1300 } 1301 1302 /* ARGSUSED */ 1303 static int 1304 hsfs_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr) 1305 { 1306 struct hsfs *fsp; 1307 1308 int error = 0; 1309 1310 switch (cmd) { 1311 1312 case _PC_NAME_MAX: 1313 fsp = VFS_TO_HSFS(vp->v_vfsp); 1314 *valp = fsp->hsfs_namemax; 1315 break; 1316 1317 case _PC_FILESIZEBITS: 1318 *valp = 33; /* Without multi extent support: 4 GB - 2k */ 1319 break; 1320 1321 default: 1322 error = fs_pathconf(vp, cmd, valp, cr); 1323 } 1324 1325 return (error); 1326 } 1327 1328 1329 1330 const fs_operation_def_t hsfs_vnodeops_template[] = { 1331 VOPNAME_OPEN, { .vop_open = hsfs_open }, 1332 VOPNAME_CLOSE, { .vop_close = hsfs_close }, 1333 VOPNAME_READ, { .vop_read = hsfs_read }, 1334 VOPNAME_GETATTR, { .vop_getattr = hsfs_getattr }, 1335 VOPNAME_ACCESS, { .vop_access = hsfs_access }, 1336 VOPNAME_LOOKUP, { .vop_lookup = hsfs_lookup }, 1337 VOPNAME_READDIR, { .vop_readdir = hsfs_readdir }, 1338 VOPNAME_READLINK, { .vop_readlink = hsfs_readlink }, 1339 VOPNAME_FSYNC, { .vop_fsync = hsfs_fsync }, 1340 VOPNAME_INACTIVE, { .vop_inactive = hsfs_inactive }, 1341 VOPNAME_FID, { .vop_fid = hsfs_fid }, 1342 VOPNAME_SEEK, { .vop_seek = hsfs_seek }, 1343 VOPNAME_FRLOCK, { .vop_frlock = hsfs_frlock }, 1344 VOPNAME_GETPAGE, { .vop_getpage = hsfs_getpage }, 1345 VOPNAME_PUTPAGE, { .vop_putpage = hsfs_putpage }, 1346 VOPNAME_MAP, { .vop_map = hsfs_map }, 1347 VOPNAME_ADDMAP, { .vop_addmap = hsfs_addmap }, 1348 VOPNAME_DELMAP, { .vop_delmap = hsfs_delmap }, 1349 VOPNAME_PATHCONF, { .vop_pathconf = hsfs_pathconf }, 1350 NULL, NULL 1351 }; 1352 1353 struct vnodeops *hsfs_vnodeops; 1354