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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Vnode operations for the High Sierra filesystem
31  */
32 
33 #include <sys/types.h>
34 #include <sys/t_lock.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/sysmacros.h>
39 #include <sys/resource.h>
40 #include <sys/signal.h>
41 #include <sys/cred.h>
42 #include <sys/user.h>
43 #include <sys/buf.h>
44 #include <sys/vfs.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 /* ARGSUSED */
87 static int
88 hsfs_fsync(vnode_t *cp, int syncflag, cred_t *cred)
89 {
90 	return (0);
91 }
92 
93 
94 /*ARGSUSED*/
95 static int
96 hsfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
97 	struct caller_context *ct)
98 {
99 	caddr_t base;
100 	offset_t diff;
101 	int error;
102 	struct hsnode *hp;
103 	uint_t filesize;
104 
105 	hp = VTOH(vp);
106 	/*
107 	 * if vp is of type VDIR, make sure dirent
108 	 * is filled up with all info (because of ptbl)
109 	 */
110 	if (vp->v_type == VDIR) {
111 		if (hp->hs_dirent.ext_size == 0)
112 			hs_filldirent(vp, &hp->hs_dirent);
113 	}
114 	filesize = hp->hs_dirent.ext_size;
115 
116 	/* Sanity checks. */
117 	if (uiop->uio_resid == 0 ||		/* No data wanted. */
118 	    uiop->uio_loffset >= MAXOFF_T ||	/* Offset too big. */
119 	    uiop->uio_loffset >= filesize)	/* Past EOF. */
120 		return (0);
121 
122 	do {
123 		/*
124 		 * We want to ask for only the "right" amount of data.
125 		 * In this case that means:-
126 		 *
127 		 * We can't get data from beyond our EOF. If asked,
128 		 * we will give a short read.
129 		 *
130 		 * segmap_getmapflt returns buffers of MAXBSIZE bytes.
131 		 * These buffers are always MAXBSIZE aligned.
132 		 * If our starting offset is not MAXBSIZE aligned,
133 		 * we can only ask for less than MAXBSIZE bytes.
134 		 *
135 		 * If our requested offset and length are such that
136 		 * they belong in different MAXBSIZE aligned slots
137 		 * then we'll be making more than one call on
138 		 * segmap_getmapflt.
139 		 *
140 		 * This diagram shows the variables we use and their
141 		 * relationships.
142 		 *
143 		 * |<-----MAXBSIZE----->|
144 		 * +--------------------------...+
145 		 * |.....mapon->|<--n-->|....*...|EOF
146 		 * +--------------------------...+
147 		 * uio_loffset->|
148 		 * uio_resid....|<---------->|
149 		 * diff.........|<-------------->|
150 		 *
151 		 * So, in this case our offset is not aligned
152 		 * and our request takes us outside of the
153 		 * MAXBSIZE window. We will break this up into
154 		 * two segmap_getmapflt calls.
155 		 */
156 		size_t nbytes;
157 		offset_t mapon;
158 		size_t n;
159 		uint_t flags;
160 
161 		mapon = uiop->uio_loffset & MAXBOFFSET;
162 		diff = filesize - uiop->uio_loffset;
163 		nbytes = (size_t)MIN(MAXBSIZE - mapon, uiop->uio_resid);
164 		n = MIN(diff, nbytes);
165 		if (n <= 0) {
166 			/* EOF or request satisfied. */
167 			return (0);
168 		}
169 
170 		base = segmap_getmapflt(segkmap, vp,
171 		    (u_offset_t)uiop->uio_loffset, n, 1, S_READ);
172 
173 		error = uiomove(base + mapon, n, UIO_READ, uiop);
174 
175 		if (error == 0) {
176 			/*
177 			 * if read a whole block, or read to eof,
178 			 *  won't need this buffer again soon.
179 			 */
180 			if (n + mapon == MAXBSIZE ||
181 			    uiop->uio_loffset == filesize)
182 				flags = SM_DONTNEED;
183 			else
184 				flags = 0;
185 			error = segmap_release(segkmap, base, flags);
186 		} else
187 			(void) segmap_release(segkmap, base, 0);
188 	} while (error == 0 && uiop->uio_resid > 0);
189 
190 	return (error);
191 }
192 
193 /*ARGSUSED2*/
194 static int
195 hsfs_getattr(
196 	struct vnode *vp,
197 	struct vattr *vap,
198 	int flags,
199 	struct cred *cred)
200 {
201 	struct hsnode *hp;
202 	struct vfs *vfsp;
203 	struct hsfs *fsp;
204 
205 	hp = VTOH(vp);
206 	fsp = VFS_TO_HSFS(vp->v_vfsp);
207 	vfsp = vp->v_vfsp;
208 
209 	if ((hp->hs_dirent.ext_size == 0) && (vp->v_type == VDIR)) {
210 		hs_filldirent(vp, &hp->hs_dirent);
211 	}
212 	vap->va_type = IFTOVT(hp->hs_dirent.mode);
213 	vap->va_mode = hp->hs_dirent.mode;
214 	vap->va_uid = hp->hs_dirent.uid;
215 	vap->va_gid = hp->hs_dirent.gid;
216 
217 	vap->va_fsid = vfsp->vfs_dev;
218 	vap->va_nodeid = (ino64_t)hp->hs_nodeid;
219 	vap->va_nlink = hp->hs_dirent.nlink;
220 	vap->va_size =	(offset_t)hp->hs_dirent.ext_size;
221 
222 	vap->va_atime.tv_sec = hp->hs_dirent.adate.tv_sec;
223 	vap->va_atime.tv_nsec = hp->hs_dirent.adate.tv_usec*1000;
224 	vap->va_mtime.tv_sec = hp->hs_dirent.mdate.tv_sec;
225 	vap->va_mtime.tv_nsec = hp->hs_dirent.mdate.tv_usec*1000;
226 	vap->va_ctime.tv_sec = hp->hs_dirent.cdate.tv_sec;
227 	vap->va_ctime.tv_nsec = hp->hs_dirent.cdate.tv_usec*1000;
228 	if (vp->v_type == VCHR || vp->v_type == VBLK)
229 		vap->va_rdev = hp->hs_dirent.r_dev;
230 	else
231 		vap->va_rdev = 0;
232 	vap->va_blksize = vfsp->vfs_bsize;
233 	/* no. of blocks = no. of data blocks + no. of xar blocks */
234 	vap->va_nblocks = (fsblkcnt64_t)howmany(vap->va_size + (u_longlong_t)
235 	    (hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift), DEV_BSIZE);
236 	vap->va_seq = hp->hs_seq;
237 	return (0);
238 }
239 
240 /*ARGSUSED*/
241 static int
242 hsfs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred)
243 {
244 	struct hsnode *hp;
245 
246 	if (vp->v_type != VLNK)
247 		return (EINVAL);
248 
249 	hp = VTOH(vp);
250 
251 	if (hp->hs_dirent.sym_link == (char *)NULL)
252 		return (ENOENT);
253 
254 	return (uiomove(hp->hs_dirent.sym_link,
255 	    (size_t)MIN(hp->hs_dirent.ext_size,
256 	    uiop->uio_resid), UIO_READ, uiop));
257 }
258 
259 /*ARGSUSED*/
260 static void
261 hsfs_inactive(struct vnode *vp, struct cred *cred)
262 {
263 	struct hsnode *hp;
264 	struct hsfs *fsp;
265 
266 	int nopage;
267 
268 	hp = VTOH(vp);
269 	fsp = VFS_TO_HSFS(vp->v_vfsp);
270 	/*
271 	 * Note: acquiring and holding v_lock for quite a while
272 	 * here serializes on the vnode; this is unfortunate, but
273 	 * likely not to overly impact performance, as the underlying
274 	 * device (CDROM drive) is quite slow.
275 	 */
276 	rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
277 	mutex_enter(&hp->hs_contents_lock);
278 	mutex_enter(&vp->v_lock);
279 
280 	if (vp->v_count < 1) {
281 		panic("hsfs_inactive: v_count < 1");
282 		/*NOTREACHED*/
283 	}
284 
285 	if (vp->v_count > 1 || (hp->hs_flags & HREF) == 0) {
286 		vp->v_count--;	/* release hold from vn_rele */
287 		mutex_exit(&vp->v_lock);
288 		mutex_exit(&hp->hs_contents_lock);
289 		rw_exit(&fsp->hsfs_hash_lock);
290 		return;
291 	}
292 	vp->v_count--;	/* release hold from vn_rele */
293 	if (vp->v_count == 0) {
294 		/*
295 		 * Free the hsnode.
296 		 * If there are no pages associated with the
297 		 * hsnode, give it back to the kmem_cache,
298 		 * else put at the end of this file system's
299 		 * internal free list.
300 		 */
301 		nopage = !vn_has_cached_data(vp);
302 		hp->hs_flags = 0;
303 		/*
304 		 * exit these locks now, since hs_freenode may
305 		 * kmem_free the hsnode and embedded vnode
306 		 */
307 		mutex_exit(&vp->v_lock);
308 		mutex_exit(&hp->hs_contents_lock);
309 		hs_freenode(vp, fsp, nopage);
310 	} else {
311 		mutex_exit(&vp->v_lock);
312 		mutex_exit(&hp->hs_contents_lock);
313 	}
314 	rw_exit(&fsp->hsfs_hash_lock);
315 }
316 
317 
318 /*ARGSUSED*/
319 static int
320 hsfs_lookup(
321 	struct vnode *dvp,
322 	char *nm,
323 	struct vnode **vpp,
324 	struct pathname *pnp,
325 	int flags,
326 	struct vnode *rdir,
327 	struct cred *cred)
328 {
329 	int error;
330 	int namelen = (int)strlen(nm);
331 
332 	if (*nm == '\0') {
333 		VN_HOLD(dvp);
334 		*vpp = dvp;
335 		return (0);
336 	}
337 
338 	/*
339 	 * If we're looking for ourself, life is simple.
340 	 */
341 	if (namelen == 1 && *nm == '.') {
342 		if (error = hs_access(dvp, (mode_t)VEXEC, cred))
343 			return (error);
344 		VN_HOLD(dvp);
345 		*vpp = dvp;
346 		return (0);
347 	}
348 
349 	return (hs_dirlook(dvp, nm, namelen, vpp, cred));
350 }
351 
352 
353 /*ARGSUSED*/
354 static int
355 hsfs_readdir(
356 	struct vnode	*vp,
357 	struct uio	*uiop,
358 	struct cred	*cred,
359 	int		*eofp)
360 {
361 	struct hsnode	*dhp;
362 	struct hsfs	*fsp;
363 	struct hs_direntry hd;
364 	struct dirent64	*nd;
365 	int		error;
366 	uint_t		offset;		/* real offset in directory */
367 	uint_t		dirsiz;		/* real size of directory */
368 	uchar_t		*blkp;
369 	int		hdlen;		/* length of hs directory entry */
370 	long		ndlen;		/* length of dirent entry */
371 	int		bytes_wanted;
372 	size_t		bufsize;	/* size of dirent buffer */
373 	char		*outbuf;	/* ptr to dirent buffer */
374 	char		*dname;
375 	int		dnamelen;
376 	size_t		dname_size;
377 	struct fbuf	*fbp;
378 	uint_t		last_offset;	/* last index into current dir block */
379 	ulong_t		dir_lbn;	/* lbn of directory */
380 	ino64_t		dirino;	/* temporary storage before storing in dirent */
381 	off_t		diroff;
382 
383 	dhp = VTOH(vp);
384 	fsp = VFS_TO_HSFS(vp->v_vfsp);
385 	if (dhp->hs_dirent.ext_size == 0)
386 		hs_filldirent(vp, &dhp->hs_dirent);
387 	dirsiz = dhp->hs_dirent.ext_size;
388 	dir_lbn = dhp->hs_dirent.ext_lbn;
389 	if (uiop->uio_loffset >= dirsiz) {	/* at or beyond EOF */
390 		if (eofp)
391 			*eofp = 1;
392 		return (0);
393 	}
394 	ASSERT(uiop->uio_loffset <= MAXOFF_T);
395 	offset = (uint_t)uiop->uio_offset;
396 
397 	dname_size = fsp->hsfs_namemax + 1;	/* 1 for the ending NUL */
398 	dname = kmem_alloc(dname_size, KM_SLEEP);
399 	bufsize = uiop->uio_resid + sizeof (struct dirent64);
400 
401 	outbuf = kmem_alloc(bufsize, KM_SLEEP);
402 	nd = (struct dirent64 *)outbuf;
403 
404 	while (offset < dirsiz) {
405 		if ((offset & MAXBMASK) + MAXBSIZE > dirsiz)
406 			bytes_wanted = dirsiz - (offset & MAXBMASK);
407 		else
408 			bytes_wanted = MAXBSIZE;
409 
410 		error = fbread(vp, (offset_t)(offset & MAXBMASK),
411 			(unsigned int)bytes_wanted, S_READ, &fbp);
412 		if (error)
413 			goto done;
414 
415 		blkp = (uchar_t *)fbp->fb_addr;
416 		last_offset = (offset & MAXBMASK) + fbp->fb_count - 1;
417 
418 #define	rel_offset(offset) ((offset) & MAXBOFFSET)	/* index into blkp */
419 
420 		while (offset < last_offset) {
421 			/*
422 			 * Directory Entries cannot span sectors.
423 			 * Unused bytes at the end of each sector are zeroed.
424 			 * Therefore, detect this condition when the size
425 			 * field of the directory entry is zero.
426 			 */
427 			hdlen = (int)((uchar_t)
428 				HDE_DIR_LEN(&blkp[rel_offset(offset)]));
429 			if (hdlen == 0) {
430 				/* advance to next sector boundary */
431 				offset = (offset & MAXHSMASK) + HS_SECTOR_SIZE;
432 
433 				/*
434 				 * Have we reached the end of current block?
435 				 */
436 				if (offset > last_offset)
437 					break;
438 				else
439 					continue;
440 			}
441 
442 			/* make sure this is nullified before  reading it */
443 			bzero(&hd, sizeof (hd));
444 
445 			/*
446 			 * Just ignore invalid directory entries.
447 			 * XXX - maybe hs_parsedir() will detect EXISTENCE bit
448 			 */
449 			if (!hs_parsedir(fsp, &blkp[rel_offset(offset)],
450 				&hd, dname, &dnamelen)) {
451 				/*
452 				 * Determine if there is enough room
453 				 */
454 				ndlen = (long)DIRENT64_RECLEN((dnamelen));
455 
456 				if ((ndlen + ((char *)nd - outbuf)) >
457 				    uiop->uio_resid) {
458 					fbrelse(fbp, S_READ);
459 					goto done; /* output buffer full */
460 				}
461 
462 				diroff = offset + hdlen;
463 				/*
464 				 * Generate nodeid.
465 				 * If a directory, nodeid points to the
466 				 * canonical dirent describing the directory:
467 				 * the dirent of the "." entry for the
468 				 * directory, which is pointed to by all
469 				 * dirents for that directory.
470 				 * Otherwise, nodeid points to dirent of file.
471 				 */
472 				if (hd.type == VDIR) {
473 					dirino = (ino64_t)
474 					    MAKE_NODEID(hd.ext_lbn, 0,
475 					    vp->v_vfsp);
476 				} else {
477 					struct hs_volume *hvp;
478 					offset_t lbn, off;
479 
480 					/*
481 					 * Normalize lbn and off
482 					 */
483 					hvp = &fsp->hsfs_vol;
484 					lbn = dir_lbn +
485 					    (offset >> hvp->lbn_shift);
486 					off = offset & hvp->lbn_maxoffset;
487 					dirino = (ino64_t)MAKE_NODEID(lbn,
488 					    off, vp->v_vfsp);
489 				}
490 
491 
492 				/* strncpy(9f) will zero uninitialized bytes */
493 
494 				ASSERT(strlen(dname) + 1 <=
495 				    DIRENT64_NAMELEN(ndlen));
496 				(void) strncpy(nd->d_name, dname,
497 				    DIRENT64_NAMELEN(ndlen));
498 				nd->d_reclen = (ushort_t)ndlen;
499 				nd->d_off = (offset_t)diroff;
500 				nd->d_ino = dirino;
501 				nd = (struct dirent64 *)((char *)nd + ndlen);
502 
503 				/*
504 				 * free up space allocated for symlink
505 				 */
506 				if (hd.sym_link != (char *)NULL) {
507 					kmem_free(hd.sym_link,
508 					    (size_t)(hd.ext_size+1));
509 					hd.sym_link = (char *)NULL;
510 				}
511 			}
512 
513 			offset += hdlen;
514 		}
515 		fbrelse(fbp, S_READ);
516 	}
517 
518 	/*
519 	 * Got here for one of the following reasons:
520 	 *	1) outbuf is full (error == 0)
521 	 *	2) end of directory reached (error == 0)
522 	 *	3) error reading directory sector (error != 0)
523 	 *	4) directory entry crosses sector boundary (error == 0)
524 	 *
525 	 * If any directory entries have been copied, don't report
526 	 * case 4.  Instead, return the valid directory entries.
527 	 *
528 	 * If no entries have been copied, report the error.
529 	 * If case 4, this will be indistiguishable from EOF.
530 	 */
531 done:
532 	ndlen = ((char *)nd - outbuf);
533 	if (ndlen != 0) {
534 		error = uiomove(outbuf, (size_t)ndlen, UIO_READ, uiop);
535 		uiop->uio_offset = offset;
536 	}
537 	kmem_free(dname, dname_size);
538 	kmem_free(outbuf, bufsize);
539 	if (eofp && error == 0)
540 		*eofp = (uiop->uio_offset >= dirsiz);
541 	return (error);
542 }
543 
544 static int
545 hsfs_fid(struct vnode *vp, struct fid *fidp)
546 {
547 	struct hsnode *hp;
548 	struct hsfid *fid;
549 
550 	if (fidp->fid_len < (sizeof (*fid) - sizeof (fid->hf_len))) {
551 		fidp->fid_len = sizeof (*fid) - sizeof (fid->hf_len);
552 		return (ENOSPC);
553 	}
554 
555 	fid = (struct hsfid *)fidp;
556 	fid->hf_len = sizeof (*fid) - sizeof (fid->hf_len);
557 	hp = VTOH(vp);
558 	mutex_enter(&hp->hs_contents_lock);
559 	fid->hf_dir_lbn = hp->hs_dir_lbn;
560 	fid->hf_dir_off = (ushort_t)hp->hs_dir_off;
561 	mutex_exit(&hp->hs_contents_lock);
562 	return (0);
563 }
564 
565 /*ARGSUSED*/
566 static int
567 hsfs_open(struct vnode **vpp, int flag, struct cred *cred)
568 {
569 	return (0);
570 }
571 
572 /*ARGSUSED*/
573 static int
574 hsfs_close(
575 	struct vnode *vp,
576 	int flag,
577 	int count,
578 	offset_t offset,
579 	struct cred *cred)
580 {
581 	(void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
582 	cleanshares(vp, ttoproc(curthread)->p_pid);
583 	return (0);
584 }
585 
586 /*ARGSUSED2*/
587 static int
588 hsfs_access(struct vnode *vp, int mode, int flags, cred_t *cred)
589 {
590 	return (hs_access(vp, (mode_t)mode, cred));
591 }
592 
593 /*
594  * the seek time of a CD-ROM is very slow, and data transfer
595  * rate is even worse (max. 150K per sec).  The design
596  * decision is to reduce access to cd-rom as much as possible,
597  * and to transfer a sizable block (read-ahead) of data at a time.
598  * UFS style of read ahead one block at a time is not appropriate,
599  * and is not supported
600  */
601 
602 /*
603  * KLUSTSIZE should be a multiple of PAGESIZE and <= MAXPHYS.
604  */
605 #define	KLUSTSIZE	(56 * 1024)
606 /* we don't support read ahead */
607 int hsfs_lostpage;	/* no. of times we lost original page */
608 
609 /*
610  * Used to prevent biodone() from releasing buf resources that
611  * we didn't allocate in quite the usual way.
612  */
613 /*ARGSUSED*/
614 int
615 hsfs_iodone(struct buf *bp)
616 {
617 	sema_v(&bp->b_io);
618 	return (0);
619 }
620 
621 /*
622  * Each file may have a different interleaving on disk.  This makes
623  * things somewhat interesting.  The gist is that there are some
624  * number of contiguous data sectors, followed by some other number
625  * of contiguous skip sectors.  The sum of those two sets of sectors
626  * defines the interleave size.  Unfortunately, it means that we generally
627  * can't simply read N sectors starting at a given offset to satisfy
628  * any given request.
629  *
630  * What we do is get the relevant memory pages via pvn_read_kluster(),
631  * then stride through the interleaves, setting up a buf for each
632  * sector that needs to be brought in.  Instead of kmem_alloc'ing
633  * space for the sectors, though, we just point at the appropriate
634  * spot in the relevant page for each of them.  This saves us a bunch
635  * of copying.
636  */
637 /*ARGSUSED*/
638 static int
639 hsfs_getapage(
640 	struct vnode *vp,
641 	u_offset_t off,
642 	size_t len,
643 	uint_t *protp,
644 	struct page *pl[],
645 	size_t plsz,
646 	struct seg *seg,
647 	caddr_t addr,
648 	enum seg_rw rw,
649 	struct cred *cred)
650 {
651 	struct hsnode *hp;
652 	struct hsfs *fsp;
653 	int	err;
654 	struct buf *bufs;
655 	caddr_t *vas;
656 	caddr_t va;
657 	struct page *pp, *searchp, *lastp;
658 	page_t	*pagefound;
659 	offset_t	bof;
660 	struct vnode *devvp;
661 	ulong_t	byte_offset;
662 	size_t	io_len_tmp;
663 	uint_t	io_off, io_len;
664 	uint_t	xlen;
665 	uint_t	filsiz;
666 	uint_t	secsize;
667 	uint_t	bufcnt;
668 	uint_t	bufsused;
669 	uint_t	count;
670 	uint_t	io_end;
671 	uint_t	which_chunk_lbn;
672 	uint_t	offset_lbn;
673 	uint_t	offset_extra;
674 	offset_t	offset_bytes;
675 	uint_t	remaining_bytes;
676 	uint_t	extension;
677 	int	remainder;	/* must be signed */
678 	int	chunk_lbn_count;
679 	int	chunk_data_bytes;
680 	int	xarsiz;
681 	diskaddr_t driver_block;
682 	u_offset_t io_off_tmp;
683 
684 	/*
685 	 * We don't support asynchronous operation at the moment, so
686 	 * just pretend we did it.  If the pages are ever actually
687 	 * needed, they'll get brought in then.
688 	 */
689 	if (pl == NULL)
690 		return (0);
691 
692 	hp = VTOH(vp);
693 	fsp = VFS_TO_HSFS(vp->v_vfsp);
694 	devvp = fsp->hsfs_devvp;
695 	secsize = fsp->hsfs_vol.lbn_size;  /* bytes per logical block */
696 
697 	/* file data size */
698 	filsiz = hp->hs_dirent.ext_size;
699 
700 	/* disk addr for start of file */
701 	bof = LBN_TO_BYTE((offset_t)hp->hs_dirent.ext_lbn, vp->v_vfsp);
702 
703 	/* xarsiz byte must be skipped for data */
704 	xarsiz = hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift;
705 
706 	/* how many logical blocks in an interleave (data+skip) */
707 	chunk_lbn_count = hp->hs_dirent.intlf_sz + hp->hs_dirent.intlf_sk;
708 
709 	if (chunk_lbn_count == 0) {
710 		chunk_lbn_count = 1;
711 	}
712 
713 	/*
714 	 * Convert interleaving size into bytes.  The zero case
715 	 * (no interleaving) optimization is handled as a side-
716 	 * effect of the read-ahead logic.
717 	 */
718 	if (hp->hs_dirent.intlf_sz == 0) {
719 		chunk_data_bytes = LBN_TO_BYTE(1, vp->v_vfsp);
720 	} else {
721 		chunk_data_bytes = LBN_TO_BYTE(hp->hs_dirent.intlf_sz,
722 			vp->v_vfsp);
723 	}
724 
725 reread:
726 	err = 0;
727 	pagefound = 0;
728 
729 	/*
730 	 * Do some read-ahead.  This mostly saves us a bit of
731 	 * system cpu time more than anything else when doing
732 	 * sequential reads.  At some point, could do the
733 	 * read-ahead asynchronously which might gain us something
734 	 * on wall time, but it seems unlikely....
735 	 *
736 	 * We do the easy case here, which is to read through
737 	 * the end of the chunk, minus whatever's at the end that
738 	 * won't exactly fill a page.
739 	 */
740 	which_chunk_lbn = (off + len) / chunk_data_bytes;
741 	extension = ((which_chunk_lbn + 1) * chunk_data_bytes) - off;
742 	extension -= (extension % PAGESIZE);
743 	if (extension != 0 && extension < filsiz - off) {
744 		len = extension;
745 	} else {
746 		len = PAGESIZE;
747 	}
748 	/*
749 	 * Some cd writers don't write sectors that aren't used.  Also,
750 	 * there's no point in reading sectors we'll never look at.  So,
751 	 * if we're asked to go beyond the end of a file, truncate to the
752 	 * length of that file.
753 	 *
754 	 * Additionally, this behaviour is required by section 6.4.5 of
755 	 * ISO 9660:1988(E).
756 	 */
757 	if (len > (filsiz - off)) {
758 		len = filsiz - off;
759 	}
760 
761 	/* A little paranoia. */
762 	ASSERT(len > 0);
763 
764 	/*
765 	 * After all that, make sure we're asking for things in units
766 	 * that bdev_strategy() will understand (see bug 4202551).
767 	 */
768 	len = roundup(len, DEV_BSIZE);
769 
770 	pp = NULL;
771 again:
772 	/* search for page in buffer */
773 	if ((pagefound = page_exists(vp, off)) == 0) {
774 		/*
775 		 * Need to really do disk IO to get the page.
776 		 */
777 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp,
778 		    &io_len_tmp, off, len, 0);
779 
780 		if (pp == NULL)
781 			goto again;
782 
783 		io_off = (uint_t)io_off_tmp;
784 		io_len = (uint_t)io_len_tmp;
785 
786 		/* check for truncation */
787 		/*
788 		 * xxx Clean up and return EIO instead?
789 		 * xxx Ought to go to u_offset_t for everything, but we
790 		 * xxx call lots of things that want uint_t arguments.
791 		 */
792 		ASSERT(io_off == io_off_tmp);
793 
794 		/*
795 		 * get enough buffers for worst-case scenario
796 		 * (i.e., no coalescing possible).
797 		 */
798 		bufcnt = (len + secsize - 1) / secsize;
799 		bufs = kmem_zalloc(bufcnt * sizeof (struct buf), KM_SLEEP);
800 		vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP);
801 		for (count = 0; count < bufcnt; count++) {
802 			bufs[count].b_edev = devvp->v_rdev;
803 			bufs[count].b_dev = cmpdev(devvp->v_rdev);
804 			bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ;
805 			bufs[count].b_iodone = hsfs_iodone;
806 			bufs[count].b_vp = vp;
807 			bufs[count].b_file = vp;
808 			sema_init(&bufs[count].b_io, 0, NULL,
809 			    SEMA_DEFAULT, NULL);
810 			sema_init(&bufs[count].b_sem, 0, NULL,
811 			    SEMA_DEFAULT, NULL);
812 		}
813 
814 		/*
815 		 * If our filesize is not an integer multiple of PAGESIZE,
816 		 * we zero that part of the last page that's between EOF and
817 		 * the PAGESIZE boundary.
818 		 */
819 		xlen = io_len & PAGEOFFSET;
820 		if (xlen != 0)
821 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
822 
823 		va = NULL;
824 		lastp = NULL;
825 		searchp = pp;
826 		io_end = io_off + io_len;
827 		for (count = 0, byte_offset = io_off;
828 			byte_offset < io_end;
829 			count++) {
830 			ASSERT(count < bufcnt);
831 
832 			/* Compute disk address for interleaving. */
833 
834 			/* considered without skips */
835 			which_chunk_lbn = byte_offset / chunk_data_bytes;
836 
837 			/* factor in skips */
838 			offset_lbn = which_chunk_lbn * chunk_lbn_count;
839 
840 			/* convert to physical byte offset for lbn */
841 			offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp);
842 
843 			/* don't forget offset into lbn */
844 			offset_extra = byte_offset % chunk_data_bytes;
845 
846 			/* get virtual block number for driver */
847 			driver_block = lbtodb(bof + xarsiz
848 				+ offset_bytes + offset_extra);
849 
850 			if (lastp != searchp) {
851 				/* this branch taken first time through loop */
852 				va = vas[count]
853 					= ppmapin(searchp, PROT_WRITE,
854 						(caddr_t)-1);
855 				/* ppmapin() guarantees not to return NULL */
856 			} else {
857 				vas[count] = NULL;
858 			}
859 
860 			bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE;
861 			bufs[count].b_offset =
862 			    (offset_t)(byte_offset - io_off + off);
863 
864 			/*
865 			 * We specifically use the b_lblkno member here
866 			 * as even in the 32 bit world driver_block can
867 			 * get very large in line with the ISO9660 spec.
868 			 */
869 
870 			bufs[count].b_lblkno = driver_block;
871 
872 			remaining_bytes = ((which_chunk_lbn + 1)
873 				* chunk_data_bytes)
874 				- byte_offset;
875 
876 			/*
877 			 * remaining_bytes can't be zero, as we derived
878 			 * which_chunk_lbn directly from byte_offset.
879 			 */
880 			if ((remaining_bytes+byte_offset) < (off+len)) {
881 				/* coalesce-read the rest of the chunk */
882 				bufs[count].b_bcount = remaining_bytes;
883 			} else {
884 				/* get the final bits */
885 				bufs[count].b_bcount = off + len - byte_offset;
886 			}
887 
888 			/*
889 			 * It would be nice to do multiple pages'
890 			 * worth at once here when the opportunity
891 			 * arises, as that has been shown to improve
892 			 * our wall time.  However, to do that
893 			 * requires that we use the pageio subsystem,
894 			 * which doesn't mix well with what we're
895 			 * already using here.  We can't use pageio
896 			 * all the time, because that subsystem
897 			 * assumes that a page is stored in N
898 			 * contiguous blocks on the device.
899 			 * Interleaving violates that assumption.
900 			 */
901 
902 			remainder = PAGESIZE - (byte_offset % PAGESIZE);
903 			if (bufs[count].b_bcount > remainder) {
904 				bufs[count].b_bcount = remainder;
905 			}
906 
907 			bufs[count].b_bufsize = bufs[count].b_bcount;
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 <= MAXOFF_T);
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 <= MAXOFF_T);
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,
1102 					hsfs_putapage, flags, cr);
1103 	else {
1104 		offset_t end_off = off + len;
1105 		offset_t file_size = VTOH(vp)->hs_dirent.ext_size;
1106 		offset_t io_off;
1107 
1108 		file_size = (file_size + PAGESIZE - 1) & PAGEMASK;
1109 		if (end_off > file_size)
1110 			end_off = file_size;
1111 
1112 		for (io_off = off; io_off < end_off; io_off += PAGESIZE) {
1113 			page_t *pp;
1114 
1115 			/*
1116 			 * We insist on getting the page only if we are
1117 			 * about to invalidate, free or write it and
1118 			 * the B_ASYNC flag is not set.
1119 			 */
1120 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1121 				pp = page_lookup(vp, io_off,
1122 					(flags & (B_INVAL | B_FREE)) ?
1123 					    SE_EXCL : SE_SHARED);
1124 			} else {
1125 				pp = page_lookup_nowait(vp, io_off,
1126 					(flags & B_FREE) ? SE_EXCL : SE_SHARED);
1127 			}
1128 
1129 			if (pp == NULL)
1130 				continue;
1131 			/*
1132 			 * Normally pvn_getdirty() should return 0, which
1133 			 * impies that it has done the job for us.
1134 			 * The shouldn't-happen scenario is when it returns 1.
1135 			 * This means that the page has been modified and
1136 			 * needs to be put back.
1137 			 * Since we can't write on a CD, we fake a failed
1138 			 * I/O and force pvn_write_done() to destroy the page.
1139 			 */
1140 			if (pvn_getdirty(pp, flags) == 1) {
1141 				cmn_err(CE_NOTE,
1142 					"hsfs_putpage: dirty HSFS page");
1143 				pvn_write_done(pp, flags |
1144 				    B_ERROR | B_WRITE | B_INVAL | B_FORCE);
1145 			}
1146 		}
1147 	}
1148 	return (error);
1149 }
1150 
1151 
1152 /*ARGSUSED*/
1153 static int
1154 hsfs_map(
1155 	struct vnode *vp,
1156 	offset_t off,
1157 	struct as *as,
1158 	caddr_t *addrp,
1159 	size_t len,
1160 	uchar_t prot,
1161 	uchar_t maxprot,
1162 	uint_t flags,
1163 	struct cred *cred)
1164 {
1165 	struct segvn_crargs vn_a;
1166 	int error;
1167 
1168 	/* VFS_RECORD(vp->v_vfsp, VS_MAP, VS_CALL); */
1169 
1170 	if (vp->v_flag & VNOMAP)
1171 		return (ENOSYS);
1172 
1173 	if (off > MAXOFF_T || off < 0 ||
1174 	    (off + len) < 0 || (off + len) > MAXOFF_T)
1175 		return (ENXIO);
1176 
1177 	if (vp->v_type != VREG) {
1178 		return (ENODEV);
1179 	}
1180 
1181 	/*
1182 	 * If file is being locked, disallow mapping.
1183 	 */
1184 	if (vn_has_mandatory_locks(vp, VTOH(vp)->hs_dirent.mode))
1185 		return (EAGAIN);
1186 
1187 	as_rangelock(as);
1188 
1189 	if ((flags & MAP_FIXED) == 0) {
1190 		map_addr(addrp, len, off, 1, flags);
1191 		if (*addrp == NULL) {
1192 			as_rangeunlock(as);
1193 			return (ENOMEM);
1194 		}
1195 	} else {
1196 		/*
1197 		 * User specified address - blow away any previous mappings
1198 		 */
1199 		(void) as_unmap(as, *addrp, len);
1200 	}
1201 
1202 	vn_a.vp = vp;
1203 	vn_a.offset = off;
1204 	vn_a.type = flags & MAP_TYPE;
1205 	vn_a.prot = prot;
1206 	vn_a.maxprot = maxprot;
1207 	vn_a.flags = flags & ~MAP_TYPE;
1208 	vn_a.cred = cred;
1209 	vn_a.amp = NULL;
1210 	vn_a.szc = 0;
1211 	vn_a.lgrp_mem_policy_flags = 0;
1212 
1213 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
1214 	as_rangeunlock(as);
1215 	return (error);
1216 }
1217 
1218 /* ARGSUSED */
1219 static int
1220 hsfs_addmap(
1221 	struct vnode *vp,
1222 	offset_t off,
1223 	struct as *as,
1224 	caddr_t addr,
1225 	size_t len,
1226 	uchar_t prot,
1227 	uchar_t maxprot,
1228 	uint_t flags,
1229 	struct cred *cr)
1230 {
1231 	struct hsnode *hp;
1232 
1233 	if (vp->v_flag & VNOMAP)
1234 		return (ENOSYS);
1235 
1236 	hp = VTOH(vp);
1237 	mutex_enter(&hp->hs_contents_lock);
1238 	hp->hs_mapcnt += btopr(len);
1239 	mutex_exit(&hp->hs_contents_lock);
1240 	return (0);
1241 }
1242 
1243 /*ARGSUSED*/
1244 static int
1245 hsfs_delmap(
1246 	struct vnode *vp,
1247 	offset_t off,
1248 	struct as *as,
1249 	caddr_t addr,
1250 	size_t len,
1251 	uint_t prot,
1252 	uint_t maxprot,
1253 	uint_t flags,
1254 	struct cred *cr)
1255 {
1256 	struct hsnode *hp;
1257 
1258 	if (vp->v_flag & VNOMAP)
1259 		return (ENOSYS);
1260 
1261 	hp = VTOH(vp);
1262 	mutex_enter(&hp->hs_contents_lock);
1263 	hp->hs_mapcnt -= btopr(len);	/* Count released mappings */
1264 	ASSERT(hp->hs_mapcnt >= 0);
1265 	mutex_exit(&hp->hs_contents_lock);
1266 	return (0);
1267 }
1268 
1269 /* ARGSUSED */
1270 static int
1271 hsfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
1272 {
1273 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
1274 }
1275 
1276 /* ARGSUSED */
1277 static int
1278 hsfs_frlock(
1279 	struct vnode *vp,
1280 	int cmd,
1281 	struct flock64 *bfp,
1282 	int flag,
1283 	offset_t offset,
1284 	struct flk_callback *flk_cbp,
1285 	cred_t *cr)
1286 {
1287 	struct hsnode *hp = VTOH(vp);
1288 
1289 	/*
1290 	 * If the file is being mapped, disallow fs_frlock.
1291 	 * We are not holding the hs_contents_lock while checking
1292 	 * hs_mapcnt because the current locking strategy drops all
1293 	 * locks before calling fs_frlock.
1294 	 * So, hs_mapcnt could change before we enter fs_frlock making
1295 	 * it meaningless to have held hs_contents_lock in the first place.
1296 	 */
1297 	if (hp->hs_mapcnt > 0 && MANDLOCK(vp, hp->hs_dirent.mode))
1298 		return (EAGAIN);
1299 
1300 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
1301 }
1302 
1303 const fs_operation_def_t hsfs_vnodeops_template[] = {
1304 	VOPNAME_OPEN, hsfs_open,
1305 	VOPNAME_CLOSE, hsfs_close,
1306 	VOPNAME_READ, hsfs_read,
1307 	VOPNAME_GETATTR, hsfs_getattr,
1308 	VOPNAME_ACCESS, hsfs_access,
1309 	VOPNAME_LOOKUP, hsfs_lookup,
1310 	VOPNAME_READDIR, hsfs_readdir,
1311 	VOPNAME_READLINK, hsfs_readlink,
1312 	VOPNAME_FSYNC, hsfs_fsync,
1313 	VOPNAME_INACTIVE, (fs_generic_func_p) hsfs_inactive,
1314 	VOPNAME_FID, hsfs_fid,
1315 	VOPNAME_SEEK, hsfs_seek,
1316 	VOPNAME_FRLOCK, hsfs_frlock,
1317 	VOPNAME_GETPAGE, hsfs_getpage,
1318 	VOPNAME_PUTPAGE, hsfs_putpage,
1319 	VOPNAME_MAP, (fs_generic_func_p) hsfs_map,
1320 	VOPNAME_ADDMAP, (fs_generic_func_p) hsfs_addmap,
1321 	VOPNAME_DELMAP, hsfs_delmap,
1322 	NULL, NULL
1323 };
1324 
1325 struct vnodeops *hsfs_vnodeops;
1326