xref: /illumos-gate/usr/src/common/fs/ufsops.c (revision 342440ec)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*342440ecSPrasad Singamsetty 
227c478bd9Sstevel@tonic-gate /*
23*342440ecSPrasad Singamsetty  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
307c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
317c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
327c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/filep.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	_BOOT
387c478bd9Sstevel@tonic-gate #include "../common/util.h"
397c478bd9Sstevel@tonic-gate #else
407c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern void *bkmem_alloc(size_t);
447c478bd9Sstevel@tonic-gate extern void bkmem_free(void *, size_t);
45ae115bc7Smrj extern int cf_check_compressed(fileid_t *);
46ae115bc7Smrj extern void cf_close(fileid_t *);
47ae115bc7Smrj extern void cf_seek(fileid_t *, off_t, int);
48ae115bc7Smrj extern int cf_read(fileid_t *, caddr_t, size_t);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate int bootrd_debug;
517c478bd9Sstevel@tonic-gate #ifdef _BOOT
527c478bd9Sstevel@tonic-gate #define	dprintf	if (bootrd_debug) printf
537c478bd9Sstevel@tonic-gate #else
547c478bd9Sstevel@tonic-gate #define	printf	kobj_printf
557c478bd9Sstevel@tonic-gate #define	dprintf	if (bootrd_debug) kobj_printf
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* PRINTLIKE */
587c478bd9Sstevel@tonic-gate extern void kobj_printf(char *, ...);
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * This fd is used when talking to the device file itself.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static fileid_t *head;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Only got one of these...ergo, only 1 fs open at once */
677c478bd9Sstevel@tonic-gate /* static */
687c478bd9Sstevel@tonic-gate devid_t		*ufs_devp;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate struct dirinfo {
717c478bd9Sstevel@tonic-gate 	int 	loc;
727c478bd9Sstevel@tonic-gate 	fileid_t *fi;
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static	int	bufs_close(int);
767c478bd9Sstevel@tonic-gate static	void	bufs_closeall(int);
777c478bd9Sstevel@tonic-gate static 	ino_t	find(fileid_t *filep, char *path);
787c478bd9Sstevel@tonic-gate static	ino_t	dlook(fileid_t *filep, char *path);
797c478bd9Sstevel@tonic-gate static 	daddr32_t	sbmap(fileid_t *filep, daddr32_t bn);
807c478bd9Sstevel@tonic-gate static  struct direct *readdir(struct dirinfo *dstuff);
817c478bd9Sstevel@tonic-gate static	void set_cache(int, void *, uint_t);
827c478bd9Sstevel@tonic-gate static	void *get_cache(int);
837c478bd9Sstevel@tonic-gate static	void free_cache();
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  *	There is only 1 open (mounted) device at any given time.
887c478bd9Sstevel@tonic-gate  *	So we can keep a single, global devp file descriptor to
897c478bd9Sstevel@tonic-gate  *	use to index into the di[] array.  This is not true for the
907c478bd9Sstevel@tonic-gate  *	fi[] array.  We can have more than one file open at once,
917c478bd9Sstevel@tonic-gate  *	so there is no global fd for the fi[].
927c478bd9Sstevel@tonic-gate  *	The user program must save the fd passed back from open()
937c478bd9Sstevel@tonic-gate  *	and use it to do subsequent read()'s.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static int
977c478bd9Sstevel@tonic-gate openi(fileid_t *filep, ino_t inode)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	struct dinode *dp;
1007c478bd9Sstevel@tonic-gate 	devid_t *devp = filep->fi_devp;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	filep->fi_inode = get_cache((int)inode);
1037c478bd9Sstevel@tonic-gate 	if (filep->fi_inode != 0)
1047c478bd9Sstevel@tonic-gate 		return (0);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	filep->fi_offset = 0;
1077c478bd9Sstevel@tonic-gate 	filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs,
108*342440ecSPrasad Singamsetty 	    itod(&devp->un_fs.di_fs, inode));
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* never more than 1 disk block */
1117c478bd9Sstevel@tonic-gate 	filep->fi_count = devp->un_fs.di_fs.fs_bsize;
1127c478bd9Sstevel@tonic-gate 	filep->fi_memp = 0;		/* cached read */
1137c478bd9Sstevel@tonic-gate 	if (diskread(filep) != 0) {
1147c478bd9Sstevel@tonic-gate 		return (0);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	dp = (struct dinode *)filep->fi_memp;
1187c478bd9Sstevel@tonic-gate 	filep->fi_inode = (struct inode *)
1197c478bd9Sstevel@tonic-gate 	    bkmem_alloc(sizeof (struct inode));
1207c478bd9Sstevel@tonic-gate 	bzero((char *)filep->fi_inode, sizeof (struct inode));
1217c478bd9Sstevel@tonic-gate 	filep->fi_inode->i_ic =
1227c478bd9Sstevel@tonic-gate 	    dp[itoo(&devp->un_fs.di_fs, inode)].di_un.di_icom;
1237c478bd9Sstevel@tonic-gate 	filep->fi_inode->i_number = inode;
1247c478bd9Sstevel@tonic-gate 	set_cache((int)inode, (void *)filep->fi_inode, sizeof (struct inode));
1257c478bd9Sstevel@tonic-gate 	return (0);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static fileid_t *
1297c478bd9Sstevel@tonic-gate find_fp(int fd)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	fileid_t *filep = head;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1347c478bd9Sstevel@tonic-gate 		while ((filep = filep->fi_forw) != head)
1357c478bd9Sstevel@tonic-gate 			if (fd == filep->fi_filedes)
1367c478bd9Sstevel@tonic-gate 				return (filep->fi_taken ? filep : 0);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (0);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static ino_t
1437c478bd9Sstevel@tonic-gate find(fileid_t *filep, char *path)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	char *q;
1467c478bd9Sstevel@tonic-gate 	char c;
1477c478bd9Sstevel@tonic-gate 	ino_t inode;
1487c478bd9Sstevel@tonic-gate 	char lpath[MAXPATHLEN];
1497c478bd9Sstevel@tonic-gate 	char *lpathp = lpath;
1507c478bd9Sstevel@tonic-gate 	int len, r;
1517c478bd9Sstevel@tonic-gate 	devid_t	*devp;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (path == NULL || *path == '\0') {
1547c478bd9Sstevel@tonic-gate 		printf("null path\n");
1557c478bd9Sstevel@tonic-gate 		return ((ino_t)0);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	dprintf("openi: %s\n", path);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	bzero(lpath, sizeof (lpath));
1617c478bd9Sstevel@tonic-gate 	bcopy(path, lpath, strlen(path));
1627c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
1637c478bd9Sstevel@tonic-gate 	while (*lpathp) {
1647c478bd9Sstevel@tonic-gate 		/* if at the beginning of pathname get root inode */
1657c478bd9Sstevel@tonic-gate 		r = (lpathp == lpath);
1667c478bd9Sstevel@tonic-gate 		if (r && openi(filep, (ino_t)UFSROOTINO))
1677c478bd9Sstevel@tonic-gate 			return ((ino_t)0);
1687c478bd9Sstevel@tonic-gate 		while (*lpathp == '/')
1697c478bd9Sstevel@tonic-gate 			lpathp++;	/* skip leading slashes */
1707c478bd9Sstevel@tonic-gate 		q = lpathp;
1717c478bd9Sstevel@tonic-gate 		while (*q != '/' && *q != '\0')
1727c478bd9Sstevel@tonic-gate 			q++;		/* find end of component */
1737c478bd9Sstevel@tonic-gate 		c = *q;
1747c478bd9Sstevel@tonic-gate 		*q = '\0';		/* terminate component */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		/* Bail out early if opening root */
1777c478bd9Sstevel@tonic-gate 		if (r && (*lpathp == '\0'))
1787c478bd9Sstevel@tonic-gate 			return ((ino_t)UFSROOTINO);
1797c478bd9Sstevel@tonic-gate 		if ((inode = dlook(filep, lpathp)) != 0) {
1807c478bd9Sstevel@tonic-gate 			if (openi(filep, inode))
1817c478bd9Sstevel@tonic-gate 				return ((ino_t)0);
1827c478bd9Sstevel@tonic-gate 			if ((filep->fi_inode->i_smode & IFMT) == IFLNK) {
1837c478bd9Sstevel@tonic-gate 				filep->fi_blocknum =
1847c478bd9Sstevel@tonic-gate 				    fsbtodb(&devp->un_fs.di_fs,
1857c478bd9Sstevel@tonic-gate 				    filep->fi_inode->i_db[0]);
1867c478bd9Sstevel@tonic-gate 				filep->fi_count = DEV_BSIZE;
1877c478bd9Sstevel@tonic-gate 				filep->fi_memp = 0;
1887c478bd9Sstevel@tonic-gate 				if (diskread(filep) != 0)
1897c478bd9Sstevel@tonic-gate 					return ((ino_t)0);
1907c478bd9Sstevel@tonic-gate 				len = strlen(filep->fi_memp);
1917c478bd9Sstevel@tonic-gate 				if (filep->fi_memp[0] == '/')
1927c478bd9Sstevel@tonic-gate 					/* absolute link */
1937c478bd9Sstevel@tonic-gate 					lpathp = lpath;
1947c478bd9Sstevel@tonic-gate 				/* copy rest of unprocessed path up */
1957c478bd9Sstevel@tonic-gate 				bcopy(q, lpathp + len, strlen(q + 1) + 2);
1967c478bd9Sstevel@tonic-gate 				/* point to unprocessed path */
1977c478bd9Sstevel@tonic-gate 				*(lpathp + len) = c;
1987c478bd9Sstevel@tonic-gate 				/* prepend link in before unprocessed path */
1997c478bd9Sstevel@tonic-gate 				bcopy(filep->fi_memp, lpathp, len);
2007c478bd9Sstevel@tonic-gate 				lpathp = lpath;
2017c478bd9Sstevel@tonic-gate 				continue;
2027c478bd9Sstevel@tonic-gate 			} else
2037c478bd9Sstevel@tonic-gate 				*q = c;
2047c478bd9Sstevel@tonic-gate 			if (c == '\0')
2057c478bd9Sstevel@tonic-gate 				break;
2067c478bd9Sstevel@tonic-gate 			lpathp = q;
2077c478bd9Sstevel@tonic-gate 			continue;
2087c478bd9Sstevel@tonic-gate 		} else {
2097c478bd9Sstevel@tonic-gate 			return ((ino_t)0);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	return (inode);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static daddr32_t
2167c478bd9Sstevel@tonic-gate sbmap(fileid_t *filep, daddr32_t bn)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	struct inode *inodep;
2197c478bd9Sstevel@tonic-gate 	int i, j, sh;
2207c478bd9Sstevel@tonic-gate 	daddr32_t nb, *bap;
2217c478bd9Sstevel@tonic-gate 	daddr32_t *db;
2227c478bd9Sstevel@tonic-gate 	devid_t	*devp;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
2257c478bd9Sstevel@tonic-gate 	inodep = filep->fi_inode;
2267c478bd9Sstevel@tonic-gate 	db = inodep->i_db;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * blocks 0..NDADDR are direct blocks
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (bn < NDADDR) {
2327c478bd9Sstevel@tonic-gate 		nb = db[bn];
2337c478bd9Sstevel@tonic-gate 		return (nb);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * addresses NIADDR have single and double indirect blocks.
2387c478bd9Sstevel@tonic-gate 	 * the first step is to determine how many levels of indirection.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	sh = 1;
2417c478bd9Sstevel@tonic-gate 	bn -= NDADDR;
2427c478bd9Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
2437c478bd9Sstevel@tonic-gate 		sh *= NINDIR(&devp->un_fs.di_fs);
2447c478bd9Sstevel@tonic-gate 		if (bn < sh)
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 		bn -= sh;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	if (j == 0) {
2497c478bd9Sstevel@tonic-gate 		return ((daddr32_t)0);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * fetch the first indirect block address from the inode
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	nb = inodep->i_ib[NIADDR - j];
2567c478bd9Sstevel@tonic-gate 	if (nb == 0) {
2577c478bd9Sstevel@tonic-gate 		return ((daddr32_t)0);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * fetch through the indirect blocks
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	for (; j <= NIADDR; j++) {
264941880d6Saf 		filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs, nb);
265941880d6Saf 		filep->fi_count = devp->un_fs.di_fs.fs_bsize;
266941880d6Saf 		filep->fi_memp = 0;
267941880d6Saf 		if (diskread(filep) != 0)
268941880d6Saf 			return (0);
269941880d6Saf 		bap = (daddr32_t *)filep->fi_memp;
2707c478bd9Sstevel@tonic-gate 		sh /= NINDIR(&devp->un_fs.di_fs);
2717c478bd9Sstevel@tonic-gate 		i = (bn / sh) % NINDIR(&devp->un_fs.di_fs);
2727c478bd9Sstevel@tonic-gate 		nb = bap[i];
2737c478bd9Sstevel@tonic-gate 		if (nb == 0) {
2747c478bd9Sstevel@tonic-gate 			return ((daddr32_t)0);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	return (nb);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static ino_t
2817c478bd9Sstevel@tonic-gate dlook(fileid_t *filep, char *path)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	struct direct *dp;
2847c478bd9Sstevel@tonic-gate 	struct inode *ip;
2857c478bd9Sstevel@tonic-gate 	struct dirinfo dirp;
2867c478bd9Sstevel@tonic-gate 	int len;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	ip = filep->fi_inode;
2897c478bd9Sstevel@tonic-gate 	if (path == NULL || *path == '\0')
2907c478bd9Sstevel@tonic-gate 		return (0);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	dprintf("dlook: %s\n", path);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if ((ip->i_smode & IFMT) != IFDIR) {
2957c478bd9Sstevel@tonic-gate 		return (0);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	if (ip->i_size == 0) {
2987c478bd9Sstevel@tonic-gate 		return (0);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	len = strlen(path);
3017c478bd9Sstevel@tonic-gate 	dirp.loc = 0;
3027c478bd9Sstevel@tonic-gate 	dirp.fi = filep;
3037c478bd9Sstevel@tonic-gate 	for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
3047c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
3057c478bd9Sstevel@tonic-gate 			continue;
3067c478bd9Sstevel@tonic-gate 		if (dp->d_namlen == len && strcmp(path, dp->d_name) == 0) {
3077c478bd9Sstevel@tonic-gate 			return (dp->d_ino);
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 		/* Allow "*" to print all names at that level, w/out match */
3107c478bd9Sstevel@tonic-gate 		if (strcmp(path, "*") == 0)
3117c478bd9Sstevel@tonic-gate 			dprintf("%s\n", dp->d_name);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	return (0);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * get next entry in a directory.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate struct direct *
3207c478bd9Sstevel@tonic-gate readdir(struct dirinfo *dstuff)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	struct direct *dp;
3237c478bd9Sstevel@tonic-gate 	fileid_t *filep;
3247c478bd9Sstevel@tonic-gate 	daddr32_t lbn, d;
3257c478bd9Sstevel@tonic-gate 	int off;
3267c478bd9Sstevel@tonic-gate 	devid_t	*devp;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	filep = dstuff->fi;
3297c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
3307c478bd9Sstevel@tonic-gate 	for (;;) {
3317c478bd9Sstevel@tonic-gate 		if (dstuff->loc >= filep->fi_inode->i_size) {
3327c478bd9Sstevel@tonic-gate 			return (NULL);
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		off = blkoff(&devp->un_fs.di_fs, dstuff->loc);
3357c478bd9Sstevel@tonic-gate 		dprintf("readdir: off = 0x%x\n", off);
3367c478bd9Sstevel@tonic-gate 		if (off == 0) {
3377c478bd9Sstevel@tonic-gate 			lbn = lblkno(&devp->un_fs.di_fs, dstuff->loc);
3387c478bd9Sstevel@tonic-gate 			d = sbmap(filep, lbn);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			if (d == 0)
3417c478bd9Sstevel@tonic-gate 				return (NULL);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs, d);
3447c478bd9Sstevel@tonic-gate 			filep->fi_count =
3457c478bd9Sstevel@tonic-gate 			    blksize(&devp->un_fs.di_fs, filep->fi_inode, lbn);
3467c478bd9Sstevel@tonic-gate 			filep->fi_memp = 0;
3477c478bd9Sstevel@tonic-gate 			if (diskread(filep) != 0) {
3487c478bd9Sstevel@tonic-gate 				return (NULL);
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		dp = (struct direct *)(filep->fi_memp + off);
3527c478bd9Sstevel@tonic-gate 		dstuff->loc += dp->d_reclen;
3537c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
3547c478bd9Sstevel@tonic-gate 			continue;
3557c478bd9Sstevel@tonic-gate 		dprintf("readdir: name = %s\n", dp->d_name);
3567c478bd9Sstevel@tonic-gate 		return (dp);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * Get the next block of data from the file.  If possible, dma right into
3627c478bd9Sstevel@tonic-gate  * user's buffer
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate static int
3657c478bd9Sstevel@tonic-gate getblock(fileid_t *filep, caddr_t buf, int count, int *rcount)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	struct fs *fs;
3687c478bd9Sstevel@tonic-gate 	caddr_t p;
3697c478bd9Sstevel@tonic-gate 	int off, size, diff;
3707c478bd9Sstevel@tonic-gate 	daddr32_t lbn;
3717c478bd9Sstevel@tonic-gate 	devid_t	*devp;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	dprintf("getblock: buf 0x%p, count 0x%x\n", (void *)buf, count);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
3767c478bd9Sstevel@tonic-gate 	p = filep->fi_memp;
3777c478bd9Sstevel@tonic-gate 	if ((signed)filep->fi_count <= 0) {
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		/* find the amt left to be read in the file */
3807c478bd9Sstevel@tonic-gate 		diff = filep->fi_inode->i_size - filep->fi_offset;
3817c478bd9Sstevel@tonic-gate 		if (diff <= 0) {
3827c478bd9Sstevel@tonic-gate 			printf("Short read\n");
3837c478bd9Sstevel@tonic-gate 			return (-1);
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		fs = &devp->un_fs.di_fs;
3877c478bd9Sstevel@tonic-gate 		/* which block (or frag) in the file do we read? */
3887c478bd9Sstevel@tonic-gate 		lbn = lblkno(fs, filep->fi_offset);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		/* which physical block on the device do we read? */
3917c478bd9Sstevel@tonic-gate 		filep->fi_blocknum = fsbtodb(fs, sbmap(filep, lbn));
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		off = blkoff(fs, filep->fi_offset);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		/* either blksize or fragsize */
3967c478bd9Sstevel@tonic-gate 		size = blksize(fs, filep->fi_inode, lbn);
3977c478bd9Sstevel@tonic-gate 		filep->fi_count = size;
3987c478bd9Sstevel@tonic-gate 		filep->fi_memp = filep->fi_buf;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/*
4017c478bd9Sstevel@tonic-gate 		 * optimization if we are reading large blocks of data then
4027c478bd9Sstevel@tonic-gate 		 * we can go directly to user's buffer
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		*rcount = 0;
4057c478bd9Sstevel@tonic-gate 		if (off == 0 && count >= size) {
4067c478bd9Sstevel@tonic-gate 			filep->fi_memp = buf;
4077c478bd9Sstevel@tonic-gate 			if (diskread(filep)) {
4087c478bd9Sstevel@tonic-gate 				return (-1);
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			*rcount = size;
4117c478bd9Sstevel@tonic-gate 			filep->fi_count = 0;
4127c478bd9Sstevel@tonic-gate 			return (0);
4137c478bd9Sstevel@tonic-gate 		} else if (diskread(filep))
4147c478bd9Sstevel@tonic-gate 			return (-1);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		if (filep->fi_offset - off + size >= filep->fi_inode->i_size)
4177c478bd9Sstevel@tonic-gate 			filep->fi_count = diff + off;
4187c478bd9Sstevel@tonic-gate 		filep->fi_count -= off;
4197c478bd9Sstevel@tonic-gate 		p = &filep->fi_memp[off];
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	filep->fi_memp = p;
4227c478bd9Sstevel@tonic-gate 	return (0);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
425ae115bc7Smrj /*
426ae115bc7Smrj  * Get the next block of data from the file.  Don't attempt to go directly
427ae115bc7Smrj  * to user's buffer.
428ae115bc7Smrj  */
429ae115bc7Smrj static int
430ae115bc7Smrj getblock_noopt(fileid_t *filep)
431ae115bc7Smrj {
432ae115bc7Smrj 	struct fs *fs;
433ae115bc7Smrj 	caddr_t p;
434ae115bc7Smrj 	int off, size, diff;
435ae115bc7Smrj 	daddr32_t lbn;
436ae115bc7Smrj 	devid_t	*devp;
437ae115bc7Smrj 
438ae115bc7Smrj 	dprintf("getblock_noopt: start\n");
439ae115bc7Smrj 
440ae115bc7Smrj 	devp = filep->fi_devp;
441ae115bc7Smrj 	p = filep->fi_memp;
442ae115bc7Smrj 	if ((signed)filep->fi_count <= 0) {
443ae115bc7Smrj 
444ae115bc7Smrj 		/* find the amt left to be read in the file */
445ae115bc7Smrj 		diff = filep->fi_inode->i_size - filep->fi_offset;
446ae115bc7Smrj 		if (diff <= 0) {
447ae115bc7Smrj 			printf("Short read\n");
448ae115bc7Smrj 			return (-1);
449ae115bc7Smrj 		}
450ae115bc7Smrj 
451ae115bc7Smrj 		fs = &devp->un_fs.di_fs;
452ae115bc7Smrj 		/* which block (or frag) in the file do we read? */
453ae115bc7Smrj 		lbn = lblkno(fs, filep->fi_offset);
454ae115bc7Smrj 
455ae115bc7Smrj 		/* which physical block on the device do we read? */
456ae115bc7Smrj 		filep->fi_blocknum = fsbtodb(fs, sbmap(filep, lbn));
457ae115bc7Smrj 
458ae115bc7Smrj 		off = blkoff(fs, filep->fi_offset);
459ae115bc7Smrj 
460ae115bc7Smrj 		/* either blksize or fragsize */
461ae115bc7Smrj 		size = blksize(fs, filep->fi_inode, lbn);
462ae115bc7Smrj 		filep->fi_count = size;
463ae115bc7Smrj 		/* reading on a ramdisk, just get a pointer to the data */
464ae115bc7Smrj 		filep->fi_memp = NULL;
465ae115bc7Smrj 
466ae115bc7Smrj 		if (diskread(filep))
467ae115bc7Smrj 			return (-1);
468ae115bc7Smrj 
469ae115bc7Smrj 		if (filep->fi_offset - off + size >= filep->fi_inode->i_size)
470ae115bc7Smrj 			filep->fi_count = diff + off;
471ae115bc7Smrj 		filep->fi_count -= off;
472ae115bc7Smrj 		p = &filep->fi_memp[off];
473ae115bc7Smrj 	}
474ae115bc7Smrj 	filep->fi_memp = p;
475ae115bc7Smrj 	return (0);
476ae115bc7Smrj }
477ae115bc7Smrj 
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  *  This is the high-level read function.  It works like this.
4817c478bd9Sstevel@tonic-gate  *  We assume that our IO device buffers up some amount of
4827c478bd9Sstevel@tonic-gate  *  data and that we can get a ptr to it.  Thus we need
4837c478bd9Sstevel@tonic-gate  *  to actually call the device func about filesize/blocksize times
4847c478bd9Sstevel@tonic-gate  *  and this greatly increases our IO speed.  When we already
4857c478bd9Sstevel@tonic-gate  *  have data in the buffer, we just return that data (with bcopy() ).
4867c478bd9Sstevel@tonic-gate  */
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate static ssize_t
4897c478bd9Sstevel@tonic-gate bufs_read(int fd, caddr_t buf, size_t count)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	size_t i, j;
4927c478bd9Sstevel@tonic-gate 	caddr_t	n;
4937c478bd9Sstevel@tonic-gate 	int rcount;
4947c478bd9Sstevel@tonic-gate 	fileid_t *filep;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if (!(filep = find_fp(fd))) {
4977c478bd9Sstevel@tonic-gate 		return (-1);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
500ae115bc7Smrj 	if ((filep->fi_flags & FI_COMPRESSED) == 0 &&
501ae115bc7Smrj 	    filep->fi_offset + count > filep->fi_inode->i_size)
5027c478bd9Sstevel@tonic-gate 		count = filep->fi_inode->i_size - filep->fi_offset;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/* that was easy */
5057c478bd9Sstevel@tonic-gate 	if ((i = count) == 0)
5067c478bd9Sstevel@tonic-gate 		return (0);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	n = buf;
5097c478bd9Sstevel@tonic-gate 	while (i > 0) {
510ae115bc7Smrj 		if (filep->fi_flags & FI_COMPRESSED) {
511ae115bc7Smrj 			if ((j = cf_read(filep, buf, count)) < 0)
512ae115bc7Smrj 				return (0); /* encountered an error */
513ae115bc7Smrj 			if (j < i)
514ae115bc7Smrj 				i = j; /* short read, must have hit EOF */
5157c478bd9Sstevel@tonic-gate 		} else {
516ae115bc7Smrj 			/* If we need to reload the buffer, do so */
517ae115bc7Smrj 			if ((j = filep->fi_count) == 0) {
518ae115bc7Smrj 				(void) getblock(filep, buf, i, &rcount);
519ae115bc7Smrj 				i -= rcount;
520ae115bc7Smrj 				buf += rcount;
521ae115bc7Smrj 				filep->fi_offset += rcount;
522ae115bc7Smrj 				continue;
523ae115bc7Smrj 			} else {
524ae115bc7Smrj 				/* else just bcopy from our buffer */
525ae115bc7Smrj 				j = MIN(i, j);
526ae115bc7Smrj 				bcopy(filep->fi_memp, buf, (unsigned)j);
527ae115bc7Smrj 			}
5287c478bd9Sstevel@tonic-gate 		}
529ae115bc7Smrj 		buf += j;
530ae115bc7Smrj 		filep->fi_memp += j;
531ae115bc7Smrj 		filep->fi_offset += j;
532ae115bc7Smrj 		filep->fi_count -= j;
533ae115bc7Smrj 		i -= j;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	return (buf - n);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  *	This routine will open a device as it is known by the V2 OBP.
5407c478bd9Sstevel@tonic-gate  *	Interface Defn:
5417c478bd9Sstevel@tonic-gate  *	err = mountroot(string);
5427c478bd9Sstevel@tonic-gate  *		err = 0 on success
5437c478bd9Sstevel@tonic-gate  *		err = -1 on failure
5447c478bd9Sstevel@tonic-gate  *	string:	char string describing the properties of the device.
5457c478bd9Sstevel@tonic-gate  *	We must not dork with any fi[]'s here.  Save that for later.
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate static int
5497c478bd9Sstevel@tonic-gate bufs_mountroot(char *str)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	if (ufs_devp)		/* already mounted */
5527c478bd9Sstevel@tonic-gate 		return (0);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	ufs_devp = (devid_t *)bkmem_alloc(sizeof (devid_t));
5557c478bd9Sstevel@tonic-gate 	ufs_devp->di_taken = 1;
5567c478bd9Sstevel@tonic-gate 	ufs_devp->di_dcookie = 0;
5577c478bd9Sstevel@tonic-gate 	ufs_devp->di_desc = (char *)bkmem_alloc(strlen(str) + 1);
5587c478bd9Sstevel@tonic-gate 	(void) strcpy(ufs_devp->di_desc, str);
5597c478bd9Sstevel@tonic-gate 	bzero(ufs_devp->un_fs.dummy, SBSIZE);
5607c478bd9Sstevel@tonic-gate 	head = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
5617c478bd9Sstevel@tonic-gate 	head->fi_back = head->fi_forw = head;
5627c478bd9Sstevel@tonic-gate 	head->fi_filedes = 0;
5637c478bd9Sstevel@tonic-gate 	head->fi_taken = 0;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/* Setup read of the superblock */
5667c478bd9Sstevel@tonic-gate 	head->fi_devp = ufs_devp;
5677c478bd9Sstevel@tonic-gate 	head->fi_blocknum = SBLOCK;
5687c478bd9Sstevel@tonic-gate 	head->fi_count = (uint_t)SBSIZE;
5697c478bd9Sstevel@tonic-gate 	head->fi_memp = (caddr_t)&(ufs_devp->un_fs.di_fs);
5707c478bd9Sstevel@tonic-gate 	head->fi_offset = 0;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (diskread(head)) {
5737c478bd9Sstevel@tonic-gate 		printf("failed to read superblock\n");
5747c478bd9Sstevel@tonic-gate 		(void) bufs_closeall(1);
5757c478bd9Sstevel@tonic-gate 		return (-1);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (ufs_devp->un_fs.di_fs.fs_magic != FS_MAGIC) {
5797c478bd9Sstevel@tonic-gate 		dprintf("fs magic = 0x%x\n", ufs_devp->un_fs.di_fs.fs_magic);
5807c478bd9Sstevel@tonic-gate 		(void) bufs_closeall(1);
5817c478bd9Sstevel@tonic-gate 		return (-1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	dprintf("mountroot succeeded\n");
5847c478bd9Sstevel@tonic-gate 	return (0);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * Unmount the currently mounted root fs.  In practice, this means
5897c478bd9Sstevel@tonic-gate  * closing all open files and releasing resources.  All of this
5907c478bd9Sstevel@tonic-gate  * is done by closeall().
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate static int
5947c478bd9Sstevel@tonic-gate bufs_unmountroot(void)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	if (ufs_devp == NULL)
5977c478bd9Sstevel@tonic-gate 		return (-1);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	(void) bufs_closeall(1);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	return (0);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  *	We allocate an fd here for use when talking
6067c478bd9Sstevel@tonic-gate  *	to the file itself.
6077c478bd9Sstevel@tonic-gate  */
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6107c478bd9Sstevel@tonic-gate static int
6117c478bd9Sstevel@tonic-gate bufs_open(char *filename, int flags)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	fileid_t	*filep;
6147c478bd9Sstevel@tonic-gate 	ino_t	inode;
6157c478bd9Sstevel@tonic-gate 	static int	filedes = 1;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	dprintf("open: %s\n", filename);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/* build and link a new file descriptor */
6207c478bd9Sstevel@tonic-gate 	filep = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
6217c478bd9Sstevel@tonic-gate 	filep->fi_back = head->fi_back;
6227c478bd9Sstevel@tonic-gate 	filep->fi_forw = head;
6237c478bd9Sstevel@tonic-gate 	head->fi_back->fi_forw = filep;
6247c478bd9Sstevel@tonic-gate 	head->fi_back = filep;
6257c478bd9Sstevel@tonic-gate 	filep->fi_filedes = filedes++;
6267c478bd9Sstevel@tonic-gate 	filep->fi_taken = 1;
6277c478bd9Sstevel@tonic-gate 	filep->fi_path = (char *)bkmem_alloc(strlen(filename) + 1);
6287c478bd9Sstevel@tonic-gate 	(void) strcpy(filep->fi_path, filename);
6297c478bd9Sstevel@tonic-gate 	filep->fi_devp = ufs_devp; /* dev is already "mounted" */
6307c478bd9Sstevel@tonic-gate 	filep->fi_inode = NULL;
6317c478bd9Sstevel@tonic-gate 	bzero(filep->fi_buf, MAXBSIZE);
632ae115bc7Smrj 	filep->fi_getblock = getblock_noopt;
633ae115bc7Smrj 	filep->fi_flags = 0;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	inode = find(filep, (char *)filename);
6367c478bd9Sstevel@tonic-gate 	if (inode == (ino_t)0) {
6377c478bd9Sstevel@tonic-gate 		dprintf("open: cannot find %s\n", filename);
6387c478bd9Sstevel@tonic-gate 		(void) bufs_close(filep->fi_filedes);
6397c478bd9Sstevel@tonic-gate 		return (-1);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	if (openi(filep, inode)) {
6427c478bd9Sstevel@tonic-gate 		printf("open: cannot open %s\n", filename);
6437c478bd9Sstevel@tonic-gate 		(void) bufs_close(filep->fi_filedes);
6447c478bd9Sstevel@tonic-gate 		return (-1);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	filep->fi_offset = filep->fi_count = 0;
6487c478bd9Sstevel@tonic-gate 
649ae115bc7Smrj 	if (cf_check_compressed(filep) != 0)
650ae115bc7Smrj 		return (-1);
6517c478bd9Sstevel@tonic-gate 	return (filep->fi_filedes);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  *  We don't do any IO here.
6567c478bd9Sstevel@tonic-gate  *  We just play games with the device pointers.
6577c478bd9Sstevel@tonic-gate  */
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate static off_t
6607c478bd9Sstevel@tonic-gate bufs_lseek(int fd, off_t addr, int whence)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	fileid_t *filep;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* Make sure user knows what file he is talking to */
6657c478bd9Sstevel@tonic-gate 	if (!(filep = find_fp(fd)))
6667c478bd9Sstevel@tonic-gate 		return (-1);
6677c478bd9Sstevel@tonic-gate 
668ae115bc7Smrj 	if (filep->fi_flags & FI_COMPRESSED) {
669ae115bc7Smrj 		cf_seek(filep, addr, whence);
670ae115bc7Smrj 	} else {
671ae115bc7Smrj 		switch (whence) {
672ae115bc7Smrj 		case SEEK_CUR:
673ae115bc7Smrj 			filep->fi_offset += addr;
674ae115bc7Smrj 			break;
675ae115bc7Smrj 		case SEEK_SET:
676ae115bc7Smrj 			filep->fi_offset = addr;
677ae115bc7Smrj 			break;
678ae115bc7Smrj 		default:
679ae115bc7Smrj 		case SEEK_END:
680ae115bc7Smrj 			printf("lseek(): invalid whence value %d\n", whence);
681ae115bc7Smrj 			break;
682ae115bc7Smrj 		}
683ae115bc7Smrj 		filep->fi_blocknum = addr / DEV_BSIZE;
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	filep->fi_count = 0;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	return (0);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
691ea8dc4b6Seschrock 
692ea8dc4b6Seschrock int
693ea8dc4b6Seschrock bufs_fstat(int fd, struct bootstat *stp)
694ea8dc4b6Seschrock {
695ea8dc4b6Seschrock 	fileid_t	*filep;
696ea8dc4b6Seschrock 	struct inode	*ip;
697ea8dc4b6Seschrock 
698ea8dc4b6Seschrock 	if (!(filep = find_fp(fd)))
699ea8dc4b6Seschrock 		return (-1);
700ea8dc4b6Seschrock 
701ea8dc4b6Seschrock 	ip = filep->fi_inode;
702ea8dc4b6Seschrock 
703ea8dc4b6Seschrock 	stp->st_mode = 0;
704ea8dc4b6Seschrock 	stp->st_size = 0;
705ea8dc4b6Seschrock 
706ea8dc4b6Seschrock 	if (ip == NULL)
707ea8dc4b6Seschrock 		return (0);
708ea8dc4b6Seschrock 
709ea8dc4b6Seschrock 	switch (ip->i_smode & IFMT) {
710ea8dc4b6Seschrock 	case IFLNK:
711ea8dc4b6Seschrock 		stp->st_mode = S_IFLNK;
712ea8dc4b6Seschrock 		break;
713ea8dc4b6Seschrock 	case IFREG:
714ea8dc4b6Seschrock 		stp->st_mode = S_IFREG;
715ea8dc4b6Seschrock 		break;
716ea8dc4b6Seschrock 	default:
717ea8dc4b6Seschrock 		break;
718ea8dc4b6Seschrock 	}
719ae115bc7Smrj 	/*
720ae115bc7Smrj 	 * NOTE: this size will be the compressed size for a compressed file
721ae115bc7Smrj 	 * This could confuse the caller since we decompress the file behind
722ae115bc7Smrj 	 * the scenes when the file is read.
723ae115bc7Smrj 	 */
724ea8dc4b6Seschrock 	stp->st_size = ip->i_size;
725ea8dc4b6Seschrock 	stp->st_atim.tv_sec = ip->i_atime.tv_sec;
726ea8dc4b6Seschrock 	stp->st_atim.tv_nsec = ip->i_atime.tv_usec * 1000;
727ea8dc4b6Seschrock 	stp->st_mtim.tv_sec = ip->i_mtime.tv_sec;
728ea8dc4b6Seschrock 	stp->st_mtim.tv_nsec = ip->i_mtime.tv_usec * 1000;
729ea8dc4b6Seschrock 	stp->st_ctim.tv_sec = ip->i_ctime.tv_sec;
730ea8dc4b6Seschrock 	stp->st_ctim.tv_nsec = ip->i_ctime.tv_usec * 1000;
731ea8dc4b6Seschrock 
732ea8dc4b6Seschrock 	return (0);
733ea8dc4b6Seschrock }
734ea8dc4b6Seschrock 
735ea8dc4b6Seschrock 
7367c478bd9Sstevel@tonic-gate static int
7377c478bd9Sstevel@tonic-gate bufs_close(int fd)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	fileid_t *filep;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/* Make sure user knows what file he is talking to */
7427c478bd9Sstevel@tonic-gate 	if (!(filep = find_fp(fd)))
7437c478bd9Sstevel@tonic-gate 		return (-1);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	if (filep->fi_taken && (filep != head)) {
7467c478bd9Sstevel@tonic-gate 		/* Clear the ranks */
7477c478bd9Sstevel@tonic-gate 		bkmem_free(filep->fi_path, strlen(filep->fi_path)+1);
7487c478bd9Sstevel@tonic-gate 		filep->fi_blocknum = filep->fi_count = filep->fi_offset = 0;
7497c478bd9Sstevel@tonic-gate 		filep->fi_memp = (caddr_t)0;
7507c478bd9Sstevel@tonic-gate 		filep->fi_devp = 0;
7517c478bd9Sstevel@tonic-gate 		filep->fi_taken = 0;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		/* unlink and deallocate node */
7547c478bd9Sstevel@tonic-gate 		filep->fi_forw->fi_back = filep->fi_back;
7557c478bd9Sstevel@tonic-gate 		filep->fi_back->fi_forw = filep->fi_forw;
756ae115bc7Smrj 		cf_close(filep);
7577c478bd9Sstevel@tonic-gate 		bkmem_free((char *)filep, sizeof (fileid_t));
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		return (0);
7607c478bd9Sstevel@tonic-gate 	} else {
7617c478bd9Sstevel@tonic-gate 		/* Big problem */
7627c478bd9Sstevel@tonic-gate 		printf("\nFile descrip %d not allocated!", fd);
7637c478bd9Sstevel@tonic-gate 		return (-1);
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7687c478bd9Sstevel@tonic-gate static void
7697c478bd9Sstevel@tonic-gate bufs_closeall(int flag)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	fileid_t *filep = head;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	while ((filep = filep->fi_forw) != head)
7747c478bd9Sstevel@tonic-gate 		if (filep->fi_taken)
7757c478bd9Sstevel@tonic-gate 			if (bufs_close(filep->fi_filedes))
7767c478bd9Sstevel@tonic-gate 				printf("Filesystem may be inconsistent.\n");
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	ufs_devp->di_taken = 0;
7797c478bd9Sstevel@tonic-gate 	bkmem_free((char *)ufs_devp, sizeof (devid_t));
7807c478bd9Sstevel@tonic-gate 	bkmem_free((char *)head, sizeof (fileid_t));
7817c478bd9Sstevel@tonic-gate 	ufs_devp = (devid_t *)NULL;
7827c478bd9Sstevel@tonic-gate 	head = (fileid_t *)NULL;
7837c478bd9Sstevel@tonic-gate 	free_cache();
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate static struct cache {
7877c478bd9Sstevel@tonic-gate 	struct cache *next;
7887c478bd9Sstevel@tonic-gate 	void *data;
7897c478bd9Sstevel@tonic-gate 	int key;
7907c478bd9Sstevel@tonic-gate 	uint_t size;
7917c478bd9Sstevel@tonic-gate } *icache;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate void
7947c478bd9Sstevel@tonic-gate set_cache(int key, void *data, uint_t size)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	struct cache *entry = bkmem_alloc(sizeof (*entry));
7977c478bd9Sstevel@tonic-gate 	entry->key = key;
7987c478bd9Sstevel@tonic-gate 	entry->data = data;
7997c478bd9Sstevel@tonic-gate 	entry->size = size;
8007c478bd9Sstevel@tonic-gate 	if (icache) {
8017c478bd9Sstevel@tonic-gate 		entry->next = icache;
8027c478bd9Sstevel@tonic-gate 		icache = entry;
8037c478bd9Sstevel@tonic-gate 	} else {
8047c478bd9Sstevel@tonic-gate 		icache = entry;
8057c478bd9Sstevel@tonic-gate 		entry->next = 0;
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate void *
8107c478bd9Sstevel@tonic-gate get_cache(int key)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	struct cache *entry = icache;
8137c478bd9Sstevel@tonic-gate 	while (entry) {
8147c478bd9Sstevel@tonic-gate 		if (entry->key == key)
8157c478bd9Sstevel@tonic-gate 			return (entry->data);
8167c478bd9Sstevel@tonic-gate 		entry = entry->next;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	return (NULL);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate void
8227c478bd9Sstevel@tonic-gate free_cache()
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	struct cache *next, *entry = icache;
8257c478bd9Sstevel@tonic-gate 	while (entry) {
8267c478bd9Sstevel@tonic-gate 		next = entry->next;
8277c478bd9Sstevel@tonic-gate 		bkmem_free(entry->data, entry->size);
8287c478bd9Sstevel@tonic-gate 		bkmem_free(entry, sizeof (*entry));
8297c478bd9Sstevel@tonic-gate 		entry = next;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 	icache = 0;
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate struct boot_fs_ops bufs_ops = {
8357c478bd9Sstevel@tonic-gate 	"boot_ufs",
8367c478bd9Sstevel@tonic-gate 	bufs_mountroot,
8377c478bd9Sstevel@tonic-gate 	bufs_unmountroot,
8387c478bd9Sstevel@tonic-gate 	bufs_open,
8397c478bd9Sstevel@tonic-gate 	bufs_close,
8407c478bd9Sstevel@tonic-gate 	bufs_read,
8417c478bd9Sstevel@tonic-gate 	bufs_lseek,
842ea8dc4b6Seschrock 	bufs_fstat,
8437c478bd9Sstevel@tonic-gate 	NULL
8447c478bd9Sstevel@tonic-gate };
845