xref: /titanic_53/usr/src/common/fs/ufsops.c (revision de6a15ee5bac749223cdd3f3d02367ab582243ff)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
327c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
337c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
347c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
377c478bd9Sstevel@tonic-gate #include <sys/filep.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifdef	_BOOT
407c478bd9Sstevel@tonic-gate #include "../common/util.h"
417c478bd9Sstevel@tonic-gate #else
427c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern void *bkmem_alloc(size_t);
467c478bd9Sstevel@tonic-gate extern void bkmem_free(void *, size_t);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int bootrd_debug;
497c478bd9Sstevel@tonic-gate #ifdef _BOOT
507c478bd9Sstevel@tonic-gate #define	dprintf	if (bootrd_debug) printf
517c478bd9Sstevel@tonic-gate #else
527c478bd9Sstevel@tonic-gate #define	printf	kobj_printf
537c478bd9Sstevel@tonic-gate #define	dprintf	if (bootrd_debug) kobj_printf
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* PRINTLIKE */
567c478bd9Sstevel@tonic-gate extern void kobj_printf(char *, ...);
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * This fd is used when talking to the device file itself.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate static fileid_t *head;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* Only got one of these...ergo, only 1 fs open at once */
657c478bd9Sstevel@tonic-gate /* static */
667c478bd9Sstevel@tonic-gate devid_t		*ufs_devp;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate struct dirinfo {
697c478bd9Sstevel@tonic-gate 	int 	loc;
707c478bd9Sstevel@tonic-gate 	fileid_t *fi;
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static	int	bufs_close(int);
747c478bd9Sstevel@tonic-gate static	void	bufs_closeall(int);
757c478bd9Sstevel@tonic-gate static 	ino_t	find(fileid_t *filep, char *path);
767c478bd9Sstevel@tonic-gate static	ino_t	dlook(fileid_t *filep, char *path);
777c478bd9Sstevel@tonic-gate static 	daddr32_t	sbmap(fileid_t *filep, daddr32_t bn);
787c478bd9Sstevel@tonic-gate static  struct direct *readdir(struct dirinfo *dstuff);
797c478bd9Sstevel@tonic-gate static	void set_cache(int, void *, uint_t);
807c478bd9Sstevel@tonic-gate static	void *get_cache(int);
817c478bd9Sstevel@tonic-gate static	void free_cache();
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  *	There is only 1 open (mounted) device at any given time.
867c478bd9Sstevel@tonic-gate  *	So we can keep a single, global devp file descriptor to
877c478bd9Sstevel@tonic-gate  *	use to index into the di[] array.  This is not true for the
887c478bd9Sstevel@tonic-gate  *	fi[] array.  We can have more than one file open at once,
897c478bd9Sstevel@tonic-gate  *	so there is no global fd for the fi[].
907c478bd9Sstevel@tonic-gate  *	The user program must save the fd passed back from open()
917c478bd9Sstevel@tonic-gate  *	and use it to do subsequent read()'s.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int
957c478bd9Sstevel@tonic-gate openi(fileid_t *filep, ino_t inode)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	struct dinode *dp;
987c478bd9Sstevel@tonic-gate 	devid_t *devp = filep->fi_devp;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	filep->fi_inode = get_cache((int)inode);
1017c478bd9Sstevel@tonic-gate 	if (filep->fi_inode != 0)
1027c478bd9Sstevel@tonic-gate 		return (0);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	filep->fi_offset = 0;
1057c478bd9Sstevel@tonic-gate 	filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs,
1067c478bd9Sstevel@tonic-gate 				itod(&devp->un_fs.di_fs, inode));
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/* never more than 1 disk block */
1097c478bd9Sstevel@tonic-gate 	filep->fi_count = devp->un_fs.di_fs.fs_bsize;
1107c478bd9Sstevel@tonic-gate 	filep->fi_memp = 0;		/* cached read */
1117c478bd9Sstevel@tonic-gate 	if (diskread(filep) != 0) {
1127c478bd9Sstevel@tonic-gate 		return (0);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	dp = (struct dinode *)filep->fi_memp;
1167c478bd9Sstevel@tonic-gate 	filep->fi_inode = (struct inode *)
1177c478bd9Sstevel@tonic-gate 	    bkmem_alloc(sizeof (struct inode));
1187c478bd9Sstevel@tonic-gate 	bzero((char *)filep->fi_inode, sizeof (struct inode));
1197c478bd9Sstevel@tonic-gate 	filep->fi_inode->i_ic =
1207c478bd9Sstevel@tonic-gate 	    dp[itoo(&devp->un_fs.di_fs, inode)].di_un.di_icom;
1217c478bd9Sstevel@tonic-gate 	filep->fi_inode->i_number = inode;
1227c478bd9Sstevel@tonic-gate 	set_cache((int)inode, (void *)filep->fi_inode, sizeof (struct inode));
1237c478bd9Sstevel@tonic-gate 	return (0);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static fileid_t *
1277c478bd9Sstevel@tonic-gate find_fp(int fd)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	fileid_t *filep = head;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1327c478bd9Sstevel@tonic-gate 		while ((filep = filep->fi_forw) != head)
1337c478bd9Sstevel@tonic-gate 			if (fd == filep->fi_filedes)
1347c478bd9Sstevel@tonic-gate 				return (filep->fi_taken ? filep : 0);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static ino_t
1417c478bd9Sstevel@tonic-gate find(fileid_t *filep, char *path)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	char *q;
1447c478bd9Sstevel@tonic-gate 	char c;
1457c478bd9Sstevel@tonic-gate 	ino_t inode;
1467c478bd9Sstevel@tonic-gate 	char lpath[MAXPATHLEN];
1477c478bd9Sstevel@tonic-gate 	char *lpathp = lpath;
1487c478bd9Sstevel@tonic-gate 	int len, r;
1497c478bd9Sstevel@tonic-gate 	devid_t	*devp;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (path == NULL || *path == '\0') {
1527c478bd9Sstevel@tonic-gate 		printf("null path\n");
1537c478bd9Sstevel@tonic-gate 		return ((ino_t)0);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	dprintf("openi: %s\n", path);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	bzero(lpath, sizeof (lpath));
1597c478bd9Sstevel@tonic-gate 	bcopy(path, lpath, strlen(path));
1607c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
1617c478bd9Sstevel@tonic-gate 	while (*lpathp) {
1627c478bd9Sstevel@tonic-gate 		/* if at the beginning of pathname get root inode */
1637c478bd9Sstevel@tonic-gate 		r = (lpathp == lpath);
1647c478bd9Sstevel@tonic-gate 		if (r && openi(filep, (ino_t)UFSROOTINO))
1657c478bd9Sstevel@tonic-gate 			return ((ino_t)0);
1667c478bd9Sstevel@tonic-gate 		while (*lpathp == '/')
1677c478bd9Sstevel@tonic-gate 			lpathp++;	/* skip leading slashes */
1687c478bd9Sstevel@tonic-gate 		q = lpathp;
1697c478bd9Sstevel@tonic-gate 		while (*q != '/' && *q != '\0')
1707c478bd9Sstevel@tonic-gate 			q++;		/* find end of component */
1717c478bd9Sstevel@tonic-gate 		c = *q;
1727c478bd9Sstevel@tonic-gate 		*q = '\0';		/* terminate component */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		/* Bail out early if opening root */
1757c478bd9Sstevel@tonic-gate 		if (r && (*lpathp == '\0'))
1767c478bd9Sstevel@tonic-gate 			return ((ino_t)UFSROOTINO);
1777c478bd9Sstevel@tonic-gate 		if ((inode = dlook(filep, lpathp)) != 0) {
1787c478bd9Sstevel@tonic-gate 			if (openi(filep, inode))
1797c478bd9Sstevel@tonic-gate 				return ((ino_t)0);
1807c478bd9Sstevel@tonic-gate 			if ((filep->fi_inode->i_smode & IFMT) == IFLNK) {
1817c478bd9Sstevel@tonic-gate 				filep->fi_blocknum =
1827c478bd9Sstevel@tonic-gate 				    fsbtodb(&devp->un_fs.di_fs,
1837c478bd9Sstevel@tonic-gate 				    filep->fi_inode->i_db[0]);
1847c478bd9Sstevel@tonic-gate 				filep->fi_count = DEV_BSIZE;
1857c478bd9Sstevel@tonic-gate 				filep->fi_memp = 0;
1867c478bd9Sstevel@tonic-gate 				if (diskread(filep) != 0)
1877c478bd9Sstevel@tonic-gate 					return ((ino_t)0);
1887c478bd9Sstevel@tonic-gate 				len = strlen(filep->fi_memp);
1897c478bd9Sstevel@tonic-gate 				if (filep->fi_memp[0] == '/')
1907c478bd9Sstevel@tonic-gate 					/* absolute link */
1917c478bd9Sstevel@tonic-gate 					lpathp = lpath;
1927c478bd9Sstevel@tonic-gate 				/* copy rest of unprocessed path up */
1937c478bd9Sstevel@tonic-gate 				bcopy(q, lpathp + len, strlen(q + 1) + 2);
1947c478bd9Sstevel@tonic-gate 				/* point to unprocessed path */
1957c478bd9Sstevel@tonic-gate 				*(lpathp + len) = c;
1967c478bd9Sstevel@tonic-gate 				/* prepend link in before unprocessed path */
1977c478bd9Sstevel@tonic-gate 				bcopy(filep->fi_memp, lpathp, len);
1987c478bd9Sstevel@tonic-gate 				lpathp = lpath;
1997c478bd9Sstevel@tonic-gate 				continue;
2007c478bd9Sstevel@tonic-gate 			} else
2017c478bd9Sstevel@tonic-gate 				*q = c;
2027c478bd9Sstevel@tonic-gate 			if (c == '\0')
2037c478bd9Sstevel@tonic-gate 				break;
2047c478bd9Sstevel@tonic-gate 			lpathp = q;
2057c478bd9Sstevel@tonic-gate 			continue;
2067c478bd9Sstevel@tonic-gate 		} else {
2077c478bd9Sstevel@tonic-gate 			return ((ino_t)0);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	return (inode);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static daddr32_t
2147c478bd9Sstevel@tonic-gate sbmap(fileid_t *filep, daddr32_t bn)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	struct inode *inodep;
2177c478bd9Sstevel@tonic-gate 	int i, j, sh;
2187c478bd9Sstevel@tonic-gate 	daddr32_t nb, *bap;
2197c478bd9Sstevel@tonic-gate 	daddr32_t *db;
2207c478bd9Sstevel@tonic-gate 	devid_t	*devp;
2217c478bd9Sstevel@tonic-gate 
222*de6a15eeSsetje 	/* These are the pools of buffers, etc. */
223*de6a15eeSsetje 	/* Compilers like to play with alignment, so force the issue here */
224*de6a15eeSsetje 	static union {
225*de6a15eeSsetje 		char		*blk[NIADDR + 1];
226*de6a15eeSsetje 		daddr32_t	*dummy;
227*de6a15eeSsetje 	} b;
228*de6a15eeSsetje 	daddr32_t blknos[NIADDR + 1];
229*de6a15eeSsetje 
2307c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
2317c478bd9Sstevel@tonic-gate 	inodep = filep->fi_inode;
2327c478bd9Sstevel@tonic-gate 	db = inodep->i_db;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * blocks 0..NDADDR are direct blocks
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	if (bn < NDADDR) {
2387c478bd9Sstevel@tonic-gate 		nb = db[bn];
2397c478bd9Sstevel@tonic-gate 		return (nb);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * addresses NIADDR have single and double indirect blocks.
2447c478bd9Sstevel@tonic-gate 	 * the first step is to determine how many levels of indirection.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 	sh = 1;
2477c478bd9Sstevel@tonic-gate 	bn -= NDADDR;
2487c478bd9Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
2497c478bd9Sstevel@tonic-gate 		sh *= NINDIR(&devp->un_fs.di_fs);
2507c478bd9Sstevel@tonic-gate 		if (bn < sh)
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 		bn -= sh;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	if (j == 0) {
2557c478bd9Sstevel@tonic-gate 		return ((daddr32_t)0);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * fetch the first indirect block address from the inode
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	nb = inodep->i_ib[NIADDR - j];
2627c478bd9Sstevel@tonic-gate 	if (nb == 0) {
2637c478bd9Sstevel@tonic-gate 		return ((daddr32_t)0);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * fetch through the indirect blocks
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	for (; j <= NIADDR; j++) {
2707c478bd9Sstevel@tonic-gate 		if (blknos[j] != nb) {
2717c478bd9Sstevel@tonic-gate 			filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs, nb);
2727c478bd9Sstevel@tonic-gate 			filep->fi_count = devp->un_fs.di_fs.fs_bsize;
2737c478bd9Sstevel@tonic-gate 			filep->fi_memp = 0;
2747c478bd9Sstevel@tonic-gate 			if (diskread(filep) != 0)
2757c478bd9Sstevel@tonic-gate 				return (0);
2767c478bd9Sstevel@tonic-gate 			b.blk[j] = filep->fi_memp;
2777c478bd9Sstevel@tonic-gate 			blknos[j] = nb;
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 		bap = (daddr32_t *)b.blk[j];
2807c478bd9Sstevel@tonic-gate 		sh /= NINDIR(&devp->un_fs.di_fs);
2817c478bd9Sstevel@tonic-gate 		i = (bn / sh) % NINDIR(&devp->un_fs.di_fs);
2827c478bd9Sstevel@tonic-gate 		nb = bap[i];
2837c478bd9Sstevel@tonic-gate 		if (nb == 0) {
2847c478bd9Sstevel@tonic-gate 			return ((daddr32_t)0);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	return (nb);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static ino_t
2917c478bd9Sstevel@tonic-gate dlook(fileid_t *filep, char *path)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	struct direct *dp;
2947c478bd9Sstevel@tonic-gate 	struct inode *ip;
2957c478bd9Sstevel@tonic-gate 	struct dirinfo dirp;
2967c478bd9Sstevel@tonic-gate 	int len;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	ip = filep->fi_inode;
2997c478bd9Sstevel@tonic-gate 	if (path == NULL || *path == '\0')
3007c478bd9Sstevel@tonic-gate 		return (0);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	dprintf("dlook: %s\n", path);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if ((ip->i_smode & IFMT) != IFDIR) {
3057c478bd9Sstevel@tonic-gate 		return (0);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	if (ip->i_size == 0) {
3087c478bd9Sstevel@tonic-gate 		return (0);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	len = strlen(path);
3117c478bd9Sstevel@tonic-gate 	dirp.loc = 0;
3127c478bd9Sstevel@tonic-gate 	dirp.fi = filep;
3137c478bd9Sstevel@tonic-gate 	for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
3147c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
3157c478bd9Sstevel@tonic-gate 			continue;
3167c478bd9Sstevel@tonic-gate 		if (dp->d_namlen == len && strcmp(path, dp->d_name) == 0) {
3177c478bd9Sstevel@tonic-gate 			return (dp->d_ino);
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		/* Allow "*" to print all names at that level, w/out match */
3207c478bd9Sstevel@tonic-gate 		if (strcmp(path, "*") == 0)
3217c478bd9Sstevel@tonic-gate 			dprintf("%s\n", dp->d_name);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	return (0);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * get next entry in a directory.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate struct direct *
3307c478bd9Sstevel@tonic-gate readdir(struct dirinfo *dstuff)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	struct direct *dp;
3337c478bd9Sstevel@tonic-gate 	fileid_t *filep;
3347c478bd9Sstevel@tonic-gate 	daddr32_t lbn, d;
3357c478bd9Sstevel@tonic-gate 	int off;
3367c478bd9Sstevel@tonic-gate 	devid_t	*devp;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	filep = dstuff->fi;
3397c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
3407c478bd9Sstevel@tonic-gate 	for (;;) {
3417c478bd9Sstevel@tonic-gate 		if (dstuff->loc >= filep->fi_inode->i_size) {
3427c478bd9Sstevel@tonic-gate 			return (NULL);
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 		off = blkoff(&devp->un_fs.di_fs, dstuff->loc);
3457c478bd9Sstevel@tonic-gate 		dprintf("readdir: off = 0x%x\n", off);
3467c478bd9Sstevel@tonic-gate 		if (off == 0) {
3477c478bd9Sstevel@tonic-gate 			lbn = lblkno(&devp->un_fs.di_fs, dstuff->loc);
3487c478bd9Sstevel@tonic-gate 			d = sbmap(filep, lbn);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 			if (d == 0)
3517c478bd9Sstevel@tonic-gate 				return (NULL);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 			filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs, d);
3547c478bd9Sstevel@tonic-gate 			filep->fi_count =
3557c478bd9Sstevel@tonic-gate 			    blksize(&devp->un_fs.di_fs, filep->fi_inode, lbn);
3567c478bd9Sstevel@tonic-gate 			filep->fi_memp = 0;
3577c478bd9Sstevel@tonic-gate 			if (diskread(filep) != 0) {
3587c478bd9Sstevel@tonic-gate 				return (NULL);
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 		dp = (struct direct *)(filep->fi_memp + off);
3627c478bd9Sstevel@tonic-gate 		dstuff->loc += dp->d_reclen;
3637c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
3647c478bd9Sstevel@tonic-gate 			continue;
3657c478bd9Sstevel@tonic-gate 		dprintf("readdir: name = %s\n", dp->d_name);
3667c478bd9Sstevel@tonic-gate 		return (dp);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * Get the next block of data from the file.  If possible, dma right into
3727c478bd9Sstevel@tonic-gate  * user's buffer
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate static int
3757c478bd9Sstevel@tonic-gate getblock(fileid_t *filep, caddr_t buf, int count, int *rcount)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	struct fs *fs;
3787c478bd9Sstevel@tonic-gate 	caddr_t p;
3797c478bd9Sstevel@tonic-gate 	int off, size, diff;
3807c478bd9Sstevel@tonic-gate 	daddr32_t lbn;
3817c478bd9Sstevel@tonic-gate 	devid_t	*devp;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	dprintf("getblock: buf 0x%p, count 0x%x\n", (void *)buf, count);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	devp = filep->fi_devp;
3867c478bd9Sstevel@tonic-gate 	p = filep->fi_memp;
3877c478bd9Sstevel@tonic-gate 	if ((signed)filep->fi_count <= 0) {
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		/* find the amt left to be read in the file */
3907c478bd9Sstevel@tonic-gate 		diff = filep->fi_inode->i_size - filep->fi_offset;
3917c478bd9Sstevel@tonic-gate 		if (diff <= 0) {
3927c478bd9Sstevel@tonic-gate 			printf("Short read\n");
3937c478bd9Sstevel@tonic-gate 			return (-1);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		fs = &devp->un_fs.di_fs;
3977c478bd9Sstevel@tonic-gate 		/* which block (or frag) in the file do we read? */
3987c478bd9Sstevel@tonic-gate 		lbn = lblkno(fs, filep->fi_offset);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/* which physical block on the device do we read? */
4017c478bd9Sstevel@tonic-gate 		filep->fi_blocknum = fsbtodb(fs, sbmap(filep, lbn));
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		off = blkoff(fs, filep->fi_offset);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		/* either blksize or fragsize */
4067c478bd9Sstevel@tonic-gate 		size = blksize(fs, filep->fi_inode, lbn);
4077c478bd9Sstevel@tonic-gate 		filep->fi_count = size;
4087c478bd9Sstevel@tonic-gate 		filep->fi_memp = filep->fi_buf;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		/*
4117c478bd9Sstevel@tonic-gate 		 * optimization if we are reading large blocks of data then
4127c478bd9Sstevel@tonic-gate 		 * we can go directly to user's buffer
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		*rcount = 0;
4157c478bd9Sstevel@tonic-gate 		if (off == 0 && count >= size) {
4167c478bd9Sstevel@tonic-gate 			filep->fi_memp = buf;
4177c478bd9Sstevel@tonic-gate 			if (diskread(filep)) {
4187c478bd9Sstevel@tonic-gate 				return (-1);
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			*rcount = size;
4217c478bd9Sstevel@tonic-gate 			filep->fi_count = 0;
4227c478bd9Sstevel@tonic-gate 			return (0);
4237c478bd9Sstevel@tonic-gate 		} else if (diskread(filep))
4247c478bd9Sstevel@tonic-gate 			return (-1);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		if (filep->fi_offset - off + size >= filep->fi_inode->i_size)
4277c478bd9Sstevel@tonic-gate 			filep->fi_count = diff + off;
4287c478bd9Sstevel@tonic-gate 		filep->fi_count -= off;
4297c478bd9Sstevel@tonic-gate 		p = &filep->fi_memp[off];
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	filep->fi_memp = p;
4327c478bd9Sstevel@tonic-gate 	return (0);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  *  This is the high-level read function.  It works like this.
4387c478bd9Sstevel@tonic-gate  *  We assume that our IO device buffers up some amount of
4397c478bd9Sstevel@tonic-gate  *  data and that we can get a ptr to it.  Thus we need
4407c478bd9Sstevel@tonic-gate  *  to actually call the device func about filesize/blocksize times
4417c478bd9Sstevel@tonic-gate  *  and this greatly increases our IO speed.  When we already
4427c478bd9Sstevel@tonic-gate  *  have data in the buffer, we just return that data (with bcopy() ).
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate static ssize_t
4467c478bd9Sstevel@tonic-gate bufs_read(int fd, caddr_t buf, size_t count)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	size_t i, j;
4497c478bd9Sstevel@tonic-gate 	caddr_t	n;
4507c478bd9Sstevel@tonic-gate 	int rcount;
4517c478bd9Sstevel@tonic-gate 	fileid_t *filep;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (!(filep = find_fp(fd))) {
4547c478bd9Sstevel@tonic-gate 		return (-1);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (filep->fi_offset + count > filep->fi_inode->i_size)
4587c478bd9Sstevel@tonic-gate 		count = filep->fi_inode->i_size - filep->fi_offset;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/* that was easy */
4617c478bd9Sstevel@tonic-gate 	if ((i = count) == 0)
4627c478bd9Sstevel@tonic-gate 		return (0);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	n = buf;
4657c478bd9Sstevel@tonic-gate 	while (i > 0) {
4667c478bd9Sstevel@tonic-gate 		/* If we need to reload the buffer, do so */
4677c478bd9Sstevel@tonic-gate 		if ((j = filep->fi_count) == 0) {
4687c478bd9Sstevel@tonic-gate 			(void) getblock(filep, buf, i, &rcount);
4697c478bd9Sstevel@tonic-gate 			i -= rcount;
4707c478bd9Sstevel@tonic-gate 			buf += rcount;
4717c478bd9Sstevel@tonic-gate 			filep->fi_offset += rcount;
4727c478bd9Sstevel@tonic-gate 		} else {
4737c478bd9Sstevel@tonic-gate 			/* else just bcopy from our buffer */
4747c478bd9Sstevel@tonic-gate 			j = MIN(i, j);
4757c478bd9Sstevel@tonic-gate 			bcopy(filep->fi_memp, buf, (unsigned)j);
4767c478bd9Sstevel@tonic-gate 			buf += j;
4777c478bd9Sstevel@tonic-gate 			filep->fi_memp += j;
4787c478bd9Sstevel@tonic-gate 			filep->fi_offset += j;
4797c478bd9Sstevel@tonic-gate 			filep->fi_count -= j;
4807c478bd9Sstevel@tonic-gate 			i -= j;
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	return (buf - n);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  *	This routine will open a device as it is known by the V2 OBP.
4887c478bd9Sstevel@tonic-gate  *	Interface Defn:
4897c478bd9Sstevel@tonic-gate  *	err = mountroot(string);
4907c478bd9Sstevel@tonic-gate  *		err = 0 on success
4917c478bd9Sstevel@tonic-gate  *		err = -1 on failure
4927c478bd9Sstevel@tonic-gate  *	string:	char string describing the properties of the device.
4937c478bd9Sstevel@tonic-gate  *	We must not dork with any fi[]'s here.  Save that for later.
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate static int
4977c478bd9Sstevel@tonic-gate bufs_mountroot(char *str)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	if (ufs_devp)		/* already mounted */
5007c478bd9Sstevel@tonic-gate 		return (0);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	ufs_devp = (devid_t *)bkmem_alloc(sizeof (devid_t));
5037c478bd9Sstevel@tonic-gate 	ufs_devp->di_taken = 1;
5047c478bd9Sstevel@tonic-gate 	ufs_devp->di_dcookie = 0;
5057c478bd9Sstevel@tonic-gate 	ufs_devp->di_desc = (char *)bkmem_alloc(strlen(str) + 1);
5067c478bd9Sstevel@tonic-gate 	(void) strcpy(ufs_devp->di_desc, str);
5077c478bd9Sstevel@tonic-gate 	bzero(ufs_devp->un_fs.dummy, SBSIZE);
5087c478bd9Sstevel@tonic-gate 	head = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
5097c478bd9Sstevel@tonic-gate 	head->fi_back = head->fi_forw = head;
5107c478bd9Sstevel@tonic-gate 	head->fi_filedes = 0;
5117c478bd9Sstevel@tonic-gate 	head->fi_taken = 0;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/* Setup read of the superblock */
5147c478bd9Sstevel@tonic-gate 	head->fi_devp = ufs_devp;
5157c478bd9Sstevel@tonic-gate 	head->fi_blocknum = SBLOCK;
5167c478bd9Sstevel@tonic-gate 	head->fi_count = (uint_t)SBSIZE;
5177c478bd9Sstevel@tonic-gate 	head->fi_memp = (caddr_t)&(ufs_devp->un_fs.di_fs);
5187c478bd9Sstevel@tonic-gate 	head->fi_offset = 0;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (diskread(head)) {
5217c478bd9Sstevel@tonic-gate 		printf("failed to read superblock\n");
5227c478bd9Sstevel@tonic-gate 		(void) bufs_closeall(1);
5237c478bd9Sstevel@tonic-gate 		return (-1);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (ufs_devp->un_fs.di_fs.fs_magic != FS_MAGIC) {
5277c478bd9Sstevel@tonic-gate 		dprintf("fs magic = 0x%x\n", ufs_devp->un_fs.di_fs.fs_magic);
5287c478bd9Sstevel@tonic-gate 		(void) bufs_closeall(1);
5297c478bd9Sstevel@tonic-gate 		return (-1);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	dprintf("mountroot succeeded\n");
5327c478bd9Sstevel@tonic-gate 	return (0);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * Unmount the currently mounted root fs.  In practice, this means
5377c478bd9Sstevel@tonic-gate  * closing all open files and releasing resources.  All of this
5387c478bd9Sstevel@tonic-gate  * is done by closeall().
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate static int
5427c478bd9Sstevel@tonic-gate bufs_unmountroot(void)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	if (ufs_devp == NULL)
5457c478bd9Sstevel@tonic-gate 		return (-1);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	(void) bufs_closeall(1);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	return (0);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate  *	We allocate an fd here for use when talking
5547c478bd9Sstevel@tonic-gate  *	to the file itself.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5587c478bd9Sstevel@tonic-gate static int
5597c478bd9Sstevel@tonic-gate bufs_open(char *filename, int flags)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	fileid_t	*filep;
5627c478bd9Sstevel@tonic-gate 	ino_t	inode;
5637c478bd9Sstevel@tonic-gate 	static int	filedes = 1;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	dprintf("open: %s\n", filename);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* build and link a new file descriptor */
5687c478bd9Sstevel@tonic-gate 	filep = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
5697c478bd9Sstevel@tonic-gate 	filep->fi_back = head->fi_back;
5707c478bd9Sstevel@tonic-gate 	filep->fi_forw = head;
5717c478bd9Sstevel@tonic-gate 	head->fi_back->fi_forw = filep;
5727c478bd9Sstevel@tonic-gate 	head->fi_back = filep;
5737c478bd9Sstevel@tonic-gate 	filep->fi_filedes = filedes++;
5747c478bd9Sstevel@tonic-gate 	filep->fi_taken = 1;
5757c478bd9Sstevel@tonic-gate 	filep->fi_path = (char *)bkmem_alloc(strlen(filename) + 1);
5767c478bd9Sstevel@tonic-gate 	(void) strcpy(filep->fi_path, filename);
5777c478bd9Sstevel@tonic-gate 	filep->fi_devp = ufs_devp; /* dev is already "mounted" */
5787c478bd9Sstevel@tonic-gate 	filep->fi_inode = NULL;
5797c478bd9Sstevel@tonic-gate 	bzero(filep->fi_buf, MAXBSIZE);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	inode = find(filep, (char *)filename);
5827c478bd9Sstevel@tonic-gate 	if (inode == (ino_t)0) {
5837c478bd9Sstevel@tonic-gate 		dprintf("open: cannot find %s\n", filename);
5847c478bd9Sstevel@tonic-gate 		(void) bufs_close(filep->fi_filedes);
5857c478bd9Sstevel@tonic-gate 		return (-1);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 	if (openi(filep, inode)) {
5887c478bd9Sstevel@tonic-gate 		printf("open: cannot open %s\n", filename);
5897c478bd9Sstevel@tonic-gate 		(void) bufs_close(filep->fi_filedes);
5907c478bd9Sstevel@tonic-gate 		return (-1);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	filep->fi_offset = filep->fi_count = 0;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	return (filep->fi_filedes);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  *  We don't do any IO here.
6007c478bd9Sstevel@tonic-gate  *  We just play games with the device pointers.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate static off_t
6047c478bd9Sstevel@tonic-gate bufs_lseek(int fd, off_t addr, int whence)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	fileid_t *filep;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* Make sure user knows what file he is talking to */
6097c478bd9Sstevel@tonic-gate 	if (!(filep = find_fp(fd)))
6107c478bd9Sstevel@tonic-gate 		return (-1);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	switch (whence) {
6137c478bd9Sstevel@tonic-gate 	case SEEK_CUR:
6147c478bd9Sstevel@tonic-gate 		filep->fi_offset += addr;
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 	case SEEK_SET:
6177c478bd9Sstevel@tonic-gate 		filep->fi_offset = addr;
6187c478bd9Sstevel@tonic-gate 		break;
6197c478bd9Sstevel@tonic-gate 	default:
6207c478bd9Sstevel@tonic-gate 	case SEEK_END:
6217c478bd9Sstevel@tonic-gate 		printf("lseek(): invalid whence value %d\n", whence);
6227c478bd9Sstevel@tonic-gate 		break;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	filep->fi_blocknum = addr / DEV_BSIZE;
6267c478bd9Sstevel@tonic-gate 	filep->fi_count = 0;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	return (0);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate static int
6327c478bd9Sstevel@tonic-gate bufs_close(int fd)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	fileid_t *filep;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* Make sure user knows what file he is talking to */
6377c478bd9Sstevel@tonic-gate 	if (!(filep = find_fp(fd)))
6387c478bd9Sstevel@tonic-gate 		return (-1);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (filep->fi_taken && (filep != head)) {
6417c478bd9Sstevel@tonic-gate 		/* Clear the ranks */
6427c478bd9Sstevel@tonic-gate 		bkmem_free(filep->fi_path, strlen(filep->fi_path)+1);
6437c478bd9Sstevel@tonic-gate 		filep->fi_blocknum = filep->fi_count = filep->fi_offset = 0;
6447c478bd9Sstevel@tonic-gate 		filep->fi_memp = (caddr_t)0;
6457c478bd9Sstevel@tonic-gate 		filep->fi_devp = 0;
6467c478bd9Sstevel@tonic-gate 		filep->fi_taken = 0;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		/* unlink and deallocate node */
6497c478bd9Sstevel@tonic-gate 		filep->fi_forw->fi_back = filep->fi_back;
6507c478bd9Sstevel@tonic-gate 		filep->fi_back->fi_forw = filep->fi_forw;
6517c478bd9Sstevel@tonic-gate 		bkmem_free((char *)filep, sizeof (fileid_t));
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		return (0);
6547c478bd9Sstevel@tonic-gate 	} else {
6557c478bd9Sstevel@tonic-gate 		/* Big problem */
6567c478bd9Sstevel@tonic-gate 		printf("\nFile descrip %d not allocated!", fd);
6577c478bd9Sstevel@tonic-gate 		return (-1);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6627c478bd9Sstevel@tonic-gate static void
6637c478bd9Sstevel@tonic-gate bufs_closeall(int flag)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	fileid_t *filep = head;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	while ((filep = filep->fi_forw) != head)
6687c478bd9Sstevel@tonic-gate 		if (filep->fi_taken)
6697c478bd9Sstevel@tonic-gate 			if (bufs_close(filep->fi_filedes))
6707c478bd9Sstevel@tonic-gate 				printf("Filesystem may be inconsistent.\n");
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	ufs_devp->di_taken = 0;
6737c478bd9Sstevel@tonic-gate 	bkmem_free((char *)ufs_devp, sizeof (devid_t));
6747c478bd9Sstevel@tonic-gate 	bkmem_free((char *)head, sizeof (fileid_t));
6757c478bd9Sstevel@tonic-gate 	ufs_devp = (devid_t *)NULL;
6767c478bd9Sstevel@tonic-gate 	head = (fileid_t *)NULL;
6777c478bd9Sstevel@tonic-gate 	free_cache();
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate static struct cache {
6817c478bd9Sstevel@tonic-gate 	struct cache *next;
6827c478bd9Sstevel@tonic-gate 	void *data;
6837c478bd9Sstevel@tonic-gate 	int key;
6847c478bd9Sstevel@tonic-gate 	uint_t size;
6857c478bd9Sstevel@tonic-gate } *icache;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate void
6887c478bd9Sstevel@tonic-gate set_cache(int key, void *data, uint_t size)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	struct cache *entry = bkmem_alloc(sizeof (*entry));
6917c478bd9Sstevel@tonic-gate 	entry->key = key;
6927c478bd9Sstevel@tonic-gate 	entry->data = data;
6937c478bd9Sstevel@tonic-gate 	entry->size = size;
6947c478bd9Sstevel@tonic-gate 	if (icache) {
6957c478bd9Sstevel@tonic-gate 		entry->next = icache;
6967c478bd9Sstevel@tonic-gate 		icache = entry;
6977c478bd9Sstevel@tonic-gate 	} else {
6987c478bd9Sstevel@tonic-gate 		icache = entry;
6997c478bd9Sstevel@tonic-gate 		entry->next = 0;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate void *
7047c478bd9Sstevel@tonic-gate get_cache(int key)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	struct cache *entry = icache;
7077c478bd9Sstevel@tonic-gate 	while (entry) {
7087c478bd9Sstevel@tonic-gate 		if (entry->key == key)
7097c478bd9Sstevel@tonic-gate 			return (entry->data);
7107c478bd9Sstevel@tonic-gate 		entry = entry->next;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	return (NULL);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate void
7167c478bd9Sstevel@tonic-gate free_cache()
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	struct cache *next, *entry = icache;
7197c478bd9Sstevel@tonic-gate 	while (entry) {
7207c478bd9Sstevel@tonic-gate 		next = entry->next;
7217c478bd9Sstevel@tonic-gate 		bkmem_free(entry->data, entry->size);
7227c478bd9Sstevel@tonic-gate 		bkmem_free(entry, sizeof (*entry));
7237c478bd9Sstevel@tonic-gate 		entry = next;
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	icache = 0;
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate struct boot_fs_ops bufs_ops = {
7297c478bd9Sstevel@tonic-gate 	"boot_ufs",
7307c478bd9Sstevel@tonic-gate 	bufs_mountroot,
7317c478bd9Sstevel@tonic-gate 	bufs_unmountroot,
7327c478bd9Sstevel@tonic-gate 	bufs_open,
7337c478bd9Sstevel@tonic-gate 	bufs_close,
7347c478bd9Sstevel@tonic-gate 	bufs_read,
7357c478bd9Sstevel@tonic-gate 	bufs_lseek,
7367c478bd9Sstevel@tonic-gate 	NULL
7377c478bd9Sstevel@tonic-gate };
738