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