17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22ae115bcmrj * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * Basic file system reading code for standalone I/O system.
307c478bdstevel@tonic-gate * Simulates a primitive UNIX I/O system (read(), write(), open(), etc).
317c478bdstevel@tonic-gate * Does not support writes.
327c478bdstevel@tonic-gate */
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#include <sys/param.h>
357c478bdstevel@tonic-gate#include <sys/sysmacros.h>
367c478bdstevel@tonic-gate#include <sys/vnode.h>
377c478bdstevel@tonic-gate#include <sys/fs/ufs_fsdir.h>
387c478bdstevel@tonic-gate#include <sys/fs/ufs_fs.h>
397c478bdstevel@tonic-gate#include <sys/fs/ufs_inode.h>
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate#include <sys/fs/hsfs_spec.h>
427c478bdstevel@tonic-gate#include <sys/fs/hsfs_isospec.h>
437c478bdstevel@tonic-gate#include <sys/fs/hsfs_node.h>
447c478bdstevel@tonic-gate#include <sys/fs/hsfs_susp.h>
457c478bdstevel@tonic-gate#include <sys/fs/hsfs_rrip.h>
467c478bdstevel@tonic-gate#include <sys/bootvfs.h>
477c478bdstevel@tonic-gate#include <sys/filep.h>
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#ifdef	_BOOT
507c478bdstevel@tonic-gate#include "../common/util.h"
517c478bdstevel@tonic-gate#else
527c478bdstevel@tonic-gate#include <sys/sunddi.h>
537c478bdstevel@tonic-gate#endif
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate#define	hdbtodb(n)	((ISO_SECTOR_SIZE / DEV_BSIZE) * (n))
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#define	HSFS_NUM_SIG    14
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate#define	SUSP_SP_IX	0
607c478bdstevel@tonic-gate#define	SUSP_CE_IX	1
617c478bdstevel@tonic-gate#define	SUSP_PD_IX	2
627c478bdstevel@tonic-gate#define	SUSP_ST_IX	3
637c478bdstevel@tonic-gate#define	SUSP_ER_IX	4
647c478bdstevel@tonic-gate#define	RRIP_PX_IX	5
657c478bdstevel@tonic-gate#define	RRIP_PN_IX	6
667c478bdstevel@tonic-gate#define	RRIP_SL_IX	7
677c478bdstevel@tonic-gate#define	RRIP_CL_IX	8
687c478bdstevel@tonic-gate#define	RRIP_PL_IX	9
697c478bdstevel@tonic-gate#define	RRIP_RE_IX	10
707c478bdstevel@tonic-gate#define	RRIP_RF_IX	11
717c478bdstevel@tonic-gate#define	RRIP_RR_IX	12
727c478bdstevel@tonic-gate#define	RRIP_NM_IX	13
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate#ifdef	_BOOT
757c478bdstevel@tonic-gate#define	dprintf	if (bootrd_debug) printf
767c478bdstevel@tonic-gate#else
777c478bdstevel@tonic-gate#define	printf	kobj_printf
787c478bdstevel@tonic-gate#define	dprintf	if (bootrd_debug) kobj_printf
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate/* PRINTFLIKE1 */
817c478bdstevel@tonic-gateextern void kobj_printf(char *, ...);
827c478bdstevel@tonic-gate#endif
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gateextern int bootrd_debug;
857c478bdstevel@tonic-gateextern void *bkmem_alloc(size_t);
867c478bdstevel@tonic-gateextern void bkmem_free(void *, size_t);
87ae115bcmrjextern int cf_check_compressed(fileid_t *);
88ae115bcmrjextern void cf_close(fileid_t *);
89ae115bcmrjextern void cf_seek(fileid_t *, off_t, int);
90ae115bcmrjextern int cf_read(fileid_t *, caddr_t, size_t);
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gatestruct dirstuff {
937c478bdstevel@tonic-gate	int loc;
947c478bdstevel@tonic-gate	fileid_t *filep;
957c478bdstevel@tonic-gate};
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gatestruct hs_direct {
987c478bdstevel@tonic-gate    struct	direct  hs_ufs_dir;
997c478bdstevel@tonic-gate    struct	hs_direntry hs_dir;
1007c478bdstevel@tonic-gate};
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gatestatic uint_t root_ino = 0;
1037c478bdstevel@tonic-gatestatic struct hs_volume *hsfsp;
1047c478bdstevel@tonic-gatestatic fileid_t *head;
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gatestatic char *hsfs_sig_tab[] = {
1077c478bdstevel@tonic-gate	SUSP_SP,
1087c478bdstevel@tonic-gate	SUSP_CE,
1097c478bdstevel@tonic-gate	SUSP_PD,
1107c478bdstevel@tonic-gate	SUSP_ST,
1117c478bdstevel@tonic-gate	SUSP_ER,
1127c478bdstevel@tonic-gate	RRIP_PX,
1137c478bdstevel@tonic-gate	RRIP_PN,
1147c478bdstevel@tonic-gate	RRIP_SL,
1157c478bdstevel@tonic-gate	RRIP_CL,
1167c478bdstevel@tonic-gate	RRIP_PL,
1177c478bdstevel@tonic-gate	RRIP_RE,
1187c478bdstevel@tonic-gate	RRIP_TF,
1197c478bdstevel@tonic-gate	RRIP_RR,
1207c478bdstevel@tonic-gate	RRIP_NM
1217c478bdstevel@tonic-gate};
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gatestatic int hsfs_num_sig = sizeof (hsfs_sig_tab) / sizeof (hsfs_sig_tab[0]);
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate/*
1267c478bdstevel@tonic-gate *  Local prototypes
1277c478bdstevel@tonic-gate */
1287c478bdstevel@tonic-gatestatic struct hs_direct *readdir(struct dirstuff *);
1297c478bdstevel@tonic-gatestatic uint_t parse_dir(fileid_t *, int, struct hs_direct *);
1307c478bdstevel@tonic-gatestatic uint_t parse_susp(char *, uint_t *, struct hs_direct *);
1317c478bdstevel@tonic-gatestatic ino_t dlook(char *, fileid_t *);
1327c478bdstevel@tonic-gatestatic int opendir(ino_t, fileid_t *);
1337c478bdstevel@tonic-gatestatic ino_t find(char *, fileid_t *);
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gatestatic int bhsfs_mountroot(char *str);
1367c478bdstevel@tonic-gatestatic int bhsfs_unmountroot(void);
1377c478bdstevel@tonic-gatestatic int bhsfs_open(char *str, int flags);
1387c478bdstevel@tonic-gatestatic int bhsfs_close(int fd);
1397c478bdstevel@tonic-gatestatic void bhsfs_closeall(void);
1407c478bdstevel@tonic-gatestatic ssize_t bhsfs_read(int fdesc, char *buf, size_t count);
1417c478bdstevel@tonic-gatestatic off_t bhsfs_lseek(int fdesc, off_t addr, int whence);
142ea8dc4beschrockstatic int bhsfs_fstat(int fdesc, struct bootstat *stp);
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gatestatic fileid_t *
1457c478bdstevel@tonic-gatefind_fp(int fd)
1467c478bdstevel@tonic-gate{
1477c478bdstevel@tonic-gate	fileid_t *filep = head;
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gate	if (fd >= 0) {
1507c478bdstevel@tonic-gate		while ((filep = filep->fi_forw) != head)
1517c478bdstevel@tonic-gate			if (fd == filep->fi_filedes)
1527c478bdstevel@tonic-gate				return (filep->fi_taken ? filep : 0);
1537c478bdstevel@tonic-gate	}
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gate	return (0);
1567c478bdstevel@tonic-gate}
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gatestatic int
1597c478bdstevel@tonic-gateopendir(ino_t inode, fileid_t *filep)
1607c478bdstevel@tonic-gate{
1617c478bdstevel@tonic-gate	struct hs_direct hsdep;
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate	dprintf("opendir: inode = %ld\n", inode);
1647c478bdstevel@tonic-gate	/* Set up the IO request */
1657c478bdstevel@tonic-gate	filep->fi_offset = 0;
1667c478bdstevel@tonic-gate	filep->fi_blocknum = hdbtodb(inode);
1677c478bdstevel@tonic-gate	filep->fi_count = ISO_SECTOR_SIZE;
1687c478bdstevel@tonic-gate	filep->fi_memp = 0;
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate	if (diskread(filep))
1717c478bdstevel@tonic-gate		return (0);
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	filep->fi_offset = 0;
1747c478bdstevel@tonic-gate	filep->fi_blocknum = hdbtodb(inode);
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate	if (inode != root_ino)
1777c478bdstevel@tonic-gate	    return (0);
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate	if (parse_dir(filep, 0, &hsdep) > 0) {
1807c478bdstevel@tonic-gate		struct inode *ip;
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate		ip = filep->fi_inode;
1837c478bdstevel@tonic-gate		if (ip == NULL)
1847c478bdstevel@tonic-gate			ip = filep->fi_inode = bkmem_alloc(sizeof (*ip));
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate		ip->i_size = hsdep.hs_dir.ext_size;
1877c478bdstevel@tonic-gate		ip->i_smode = hsdep.hs_dir.mode;
1887c478bdstevel@tonic-gate		ip->i_number = inode;
1897c478bdstevel@tonic-gate		return (0);
1907c478bdstevel@tonic-gate	}
1917c478bdstevel@tonic-gate	return (1);
1927c478bdstevel@tonic-gate}
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gatestatic ino_t
1957c478bdstevel@tonic-gatefind(char *path, fileid_t *filep)
1967c478bdstevel@tonic-gate{
1977c478bdstevel@tonic-gate	char *q;
1987c478bdstevel@tonic-gate	char c;
1997c478bdstevel@tonic-gate	ino_t n;
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	dprintf("find: %s\n", path);
2027c478bdstevel@tonic-gate	if (path == NULL || *path == '\0')
2037c478bdstevel@tonic-gate		return (0);
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate	if (opendir(root_ino, filep))
2067c478bdstevel@tonic-gate		return (0);
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	while (*path) {
2097c478bdstevel@tonic-gate		while (*path == '/')
2107c478bdstevel@tonic-gate			path++;
2117c478bdstevel@tonic-gate		q = path;
2127c478bdstevel@tonic-gate		while (*q != '/' && *q != '\0')
2137c478bdstevel@tonic-gate			q++;
2147c478bdstevel@tonic-gate		c = *q;
2157c478bdstevel@tonic-gate		*q = '\0';
216f55ce20szhou		n = dlook(path, filep);
217f55ce20szhou		*q = c;
218f55ce20szhou		path = q;
2197c478bdstevel@tonic-gate
220f55ce20szhou		if (n != 0) {
2217c478bdstevel@tonic-gate			if (c == '\0')
2227c478bdstevel@tonic-gate				break;
2237c478bdstevel@tonic-gate			if (opendir(n, filep))
2247c478bdstevel@tonic-gate				return (0);
2257c478bdstevel@tonic-gate			continue;
2267c478bdstevel@tonic-gate		} else {
2277c478bdstevel@tonic-gate			return (0);
2287c478bdstevel@tonic-gate		}
2297c478bdstevel@tonic-gate	}
2307c478bdstevel@tonic-gate	return ((ino_t)n);
2317c478bdstevel@tonic-gate}
2327c478bdstevel@tonic-gate
2337c478bdstevel@tonic-gatestatic ino_t
2347c478bdstevel@tonic-gatedlook(char *s, fileid_t *filep)
2357c478bdstevel@tonic-gate{
2367c478bdstevel@tonic-gate	struct hs_direct *hsdep;
2377c478bdstevel@tonic-gate	struct direct *udp;
2387c478bdstevel@tonic-gate	struct inode *ip;
2397c478bdstevel@tonic-gate	struct dirstuff dirp;
2407c478bdstevel@tonic-gate	int len;
2417c478bdstevel@tonic-gate
2427c478bdstevel@tonic-gate	dprintf("dlook: %s\n", s);
2437c478bdstevel@tonic-gate	ip = filep->fi_inode;
2447c478bdstevel@tonic-gate	if (s == NULL || *s == '\0')
2457c478bdstevel@tonic-gate		return (0);
2467c478bdstevel@tonic-gate	if ((ip->i_smode & IFMT) != IFDIR) {
2477c478bdstevel@tonic-gate		return (0);
2487c478bdstevel@tonic-gate	}
2497c478bdstevel@tonic-gate	if (ip->i_size == 0) {
2507c478bdstevel@tonic-gate		return (0);
2517c478bdstevel@tonic-gate	}
2527c478bdstevel@tonic-gate	len = strlen(s);
2537c478bdstevel@tonic-gate	dirp.loc = 0;
2547c478bdstevel@tonic-gate	dirp.filep = filep;
2557c478bdstevel@tonic-gate	for (hsdep = readdir(&dirp); hsdep != NULL; hsdep = readdir(&dirp)) {
2567c478bdstevel@tonic-gate		udp = &hsdep->hs_ufs_dir;
2577c478bdstevel@tonic-gate		if (udp->d_namlen == 1 &&
2587c478bdstevel@tonic-gate		    udp->d_name[0] == '.' &&
2597c478bdstevel@tonic-gate		    udp->d_name[1] == '\0')
2607c478bdstevel@tonic-gate			continue;
2617c478bdstevel@tonic-gate		if (udp->d_namlen == 2 &&
2627c478bdstevel@tonic-gate		    udp->d_name[0] == '.' &&
2637c478bdstevel@tonic-gate		    udp->d_name[1] == '.' &&
2647c478bdstevel@tonic-gate		    udp->d_name[2] == '\0')
2657c478bdstevel@tonic-gate			continue;
2667c478bdstevel@tonic-gate		if (udp->d_namlen == len && (strcmp(s, udp->d_name)) == 0) {
2677c478bdstevel@tonic-gate			struct inode *ip = filep->fi_inode;
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate			filep->fi_offset = 0;
2707c478bdstevel@tonic-gate			filep->fi_blocknum = hdbtodb(udp->d_ino);
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gate			bzero(filep->fi_inode, sizeof (struct inode));
2737c478bdstevel@tonic-gate			ip->i_size = hsdep->hs_dir.ext_size;
2747c478bdstevel@tonic-gate			ip->i_smode = hsdep->hs_dir.mode;
2757c478bdstevel@tonic-gate			ip->i_number = udp->d_ino;
2767c478bdstevel@tonic-gate			return (udp->d_ino);
2777c478bdstevel@tonic-gate		}
2787c478bdstevel@tonic-gate	}
2797c478bdstevel@tonic-gate	return (0);
2807c478bdstevel@tonic-gate}
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate/*
2837c478bdstevel@tonic-gate * get next entry in a directory.
2847c478bdstevel@tonic-gate */
2857c478bdstevel@tonic-gatestatic struct hs_direct *
2867c478bdstevel@tonic-gatereaddir(struct dirstuff *dirp)
2877c478bdstevel@tonic-gate{
2887c478bdstevel@tonic-gate	static struct hs_direct hsdep;
2897c478bdstevel@tonic-gate	struct direct *udp = &hsdep.hs_ufs_dir;
2907c478bdstevel@tonic-gate	struct inode *ip;
2917c478bdstevel@tonic-gate	fileid_t *filep;
2927c478bdstevel@tonic-gate	daddr_t lbn;
2937c478bdstevel@tonic-gate	int off;
2947c478bdstevel@tonic-gate
2957c478bdstevel@tonic-gate	dprintf("readdir: start\n");
2967c478bdstevel@tonic-gate	filep = dirp->filep;
2977c478bdstevel@tonic-gate	ip = filep->fi_inode;
2987c478bdstevel@tonic-gate	for (;;) {
2997c478bdstevel@tonic-gate		if (dirp->loc >= ip->i_size) {
3007c478bdstevel@tonic-gate			return (NULL);
3017c478bdstevel@tonic-gate		}
3027c478bdstevel@tonic-gate		off = dirp->loc & ((1 << ISO_SECTOR_SHIFT) - 1);
3037c478bdstevel@tonic-gate		if (off == 0) {
3047c478bdstevel@tonic-gate			lbn = hdbtodb(dirp->loc >> ISO_SECTOR_SHIFT);
3057c478bdstevel@tonic-gate			filep->fi_blocknum = lbn + hdbtodb(ip->i_number);
3067c478bdstevel@tonic-gate			filep->fi_count = ISO_SECTOR_SIZE;
3077c478bdstevel@tonic-gate			filep->fi_memp = 0;
3087c478bdstevel@tonic-gate			if (diskread(filep)) {
3097c478bdstevel@tonic-gate				dprintf("readdir: diskread failed\n");
3107c478bdstevel@tonic-gate				return (NULL);
3117c478bdstevel@tonic-gate			}
3127c478bdstevel@tonic-gate		}
3137c478bdstevel@tonic-gate		dirp->loc += parse_dir(filep, off, &hsdep);
3147c478bdstevel@tonic-gate		if (udp->d_reclen == 0 && dirp->loc <= ip->i_size) {
3157c478bdstevel@tonic-gate			dirp->loc = roundup(dirp->loc, ISO_SECTOR_SIZE);
3167c478bdstevel@tonic-gate			continue;
3177c478bdstevel@tonic-gate		}
3187c478bdstevel@tonic-gate		return (&hsdep);
3197c478bdstevel@tonic-gate	}
3207c478bdstevel@tonic-gate}
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gatestatic int
3237c478bdstevel@tonic-gategetblock(fileid_t *filep)
3247c478bdstevel@tonic-gate{
3257c478bdstevel@tonic-gate	struct inode *ip = filep->fi_inode;
3267c478bdstevel@tonic-gate	int off, size, diff;
3277c478bdstevel@tonic-gate	daddr_t lbn;
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate	dprintf("getblock: start\n");
3307c478bdstevel@tonic-gate	diff = ip->i_size - filep->fi_offset;
3317c478bdstevel@tonic-gate	if (diff <= 0)
3327c478bdstevel@tonic-gate		return (-1);
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate	/* which block (or frag) in the file do we read? */
3357c478bdstevel@tonic-gate	lbn = hdbtodb(filep->fi_offset >> ISO_SECTOR_SHIFT);
3367c478bdstevel@tonic-gate	filep->fi_blocknum = lbn + hdbtodb(ip->i_number);
3377c478bdstevel@tonic-gate
3387c478bdstevel@tonic-gate	off = filep->fi_offset & ((1 << ISO_SECTOR_SHIFT) - 1);
3397c478bdstevel@tonic-gate	size = filep->fi_count = ISO_SECTOR_SIZE;
3407c478bdstevel@tonic-gate	filep->fi_memp = 0;
3417c478bdstevel@tonic-gate	if (diskread(filep))	/* Trap errors */
3427c478bdstevel@tonic-gate		return (-1);
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate	if (filep->fi_offset - off + size >= ip->i_size)
3457c478bdstevel@tonic-gate		filep->fi_count = diff + off;
3467c478bdstevel@tonic-gate	filep->fi_count -= off;
3477c478bdstevel@tonic-gate	filep->fi_memp += off;
3487c478bdstevel@tonic-gate	dprintf("getblock: end\n");
3497c478bdstevel@tonic-gate	return (0);
3507c478bdstevel@tonic-gate}
3517c478bdstevel@tonic-gate
3527c478bdstevel@tonic-gatestatic ssize_t
3537c478bdstevel@tonic-gatebhsfs_read(int fd, caddr_t buf, size_t count)
3547c478bdstevel@tonic-gate{
3557c478bdstevel@tonic-gate	int i, j;
3567c478bdstevel@tonic-gate	fileid_t *filep;
3577c478bdstevel@tonic-gate	struct inode *ip;
3587c478bdstevel@tonic-gate	caddr_t n;
3597c478bdstevel@tonic-gate
360ae115bcmrj	dprintf("bhsfs_read %d, ", fd);
361ae115bcmrj	dprintf("count 0x%lx\n", count);
3627c478bdstevel@tonic-gate	filep = find_fp(fd);
3637c478bdstevel@tonic-gate	if (filep == NULL)
3647c478bdstevel@tonic-gate		return (-1);
3657c478bdstevel@tonic-gate
3667c478bdstevel@tonic-gate	ip = filep->fi_inode;
3677c478bdstevel@tonic-gate	n = buf;
368ae115bcmrj	if ((filep->fi_flags & FI_COMPRESSED) == 0 &&
369ae115bcmrj	    filep->fi_offset + count > ip->i_size)
3707c478bdstevel@tonic-gate		count = ip->i_size - filep->fi_offset;
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	if ((i = count) <= 0)
3737c478bdstevel@tonic-gate		return (0);
3747c478bdstevel@tonic-gate
3757c478bdstevel@tonic-gate	while (i > 0) {
376ae115bcmrj		if (filep->fi_flags & FI_COMPRESSED) {
377ae115bcmrj			if ((j = cf_read(filep, buf, count)) < 0)
378ae115bcmrj				return (0); /* encountered an error */
379ae115bcmrj			if (j < i)
380ae115bcmrj				i = j; /* short read, must have hit EOF */
381