Home | History | Annotate | Line # | Download | only in libsa
ufs.c revision 1.23
      1 /*	$NetBSD: ufs.c,v 1.23 1999/03/31 01:50:26 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * The Mach Operating System project at Carnegie-Mellon University.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *
     39  * Copyright (c) 1990, 1991 Carnegie Mellon University
     40  * All Rights Reserved.
     41  *
     42  * Author: David Golub
     43  *
     44  * Permission to use, copy, modify and distribute this software and its
     45  * documentation is hereby granted, provided that both the copyright
     46  * notice and this permission notice appear in all copies of the
     47  * software, derivative works or modified versions, and any portions
     48  * thereof, and that both notices appear in supporting documentation.
     49  *
     50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     52  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     53  *
     54  * Carnegie Mellon requests users of this software to return to
     55  *
     56  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     57  *  School of Computer Science
     58  *  Carnegie Mellon University
     59  *  Pittsburgh PA 15213-3890
     60  *
     61  * any improvements or extensions that they make and grant Carnegie the
     62  * rights to redistribute these changes.
     63  */
     64 
     65 /*
     66  *	Stand-alone file reading package.
     67  */
     68 
     69 #include <sys/param.h>
     70 #include <sys/time.h>
     71 #include <ufs/ufs/dinode.h>
     72 #include <ufs/ufs/dir.h>
     73 #include <ufs/ffs/fs.h>
     74 #ifdef _STANDALONE
     75 #include <lib/libkern/libkern.h>
     76 #else
     77 #include <string.h>
     78 inline u_int
     79 max(a, b)
     80         u_int a, b;
     81 {
     82         return (a > b ? a : b);
     83 }
     84 #endif
     85 
     86 #include "stand.h"
     87 #include "ufs.h"
     88 
     89 #if defined(UFS_NOSYMLINK) && !defined(LIBSA_NO_FS_SYMLINK)
     90 #define LIBSA_NO_FS_SYMLINK			/* XXX COMPAT */
     91 #endif
     92 #if defined(UFS_NOCLOSE) && !defined(LIBSA_NO_FS_CLOSE)
     93 #define LIBSA_NO_FS_CLOSE			/* XXX COMPAT */
     94 #endif
     95 #if defined(UFS_NOWRITE) && !defined(LIBSA_NO_FS_WRITE)
     96 #define LIBSA_NO_FS_WRITE			/* XXX COMPAT */
     97 #endif
     98 
     99 #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
    100 #define LIBSA_NO_FS_SYMLINK
    101 #endif
    102 
    103 
    104 /*
    105  * In-core open file.
    106  */
    107 struct file {
    108 	off_t		f_seekp;	/* seek pointer */
    109 	struct fs	*f_fs;		/* pointer to super-block */
    110 	struct dinode	f_di;		/* copy of on-disk inode */
    111 	unsigned int	f_nindir[NIADDR];
    112 					/* number of blocks mapped by
    113 					   indirect block at level i */
    114 	char		*f_blk[NIADDR];	/* buffer for indirect block at
    115 					   level i */
    116 	size_t		f_blksize[NIADDR];
    117 					/* size of buffer */
    118 	daddr_t		f_blkno[NIADDR];/* disk address of block in buffer */
    119 	char		*f_buf;		/* buffer for data block */
    120 	size_t		f_buf_size;	/* size of data block */
    121 	daddr_t		f_buf_blkno;	/* block number of data block */
    122 };
    123 
    124 static int	read_inode __P((ino_t, struct open_file *));
    125 static int	block_map __P((struct open_file *, daddr_t, daddr_t *));
    126 static int	buf_read_file __P((struct open_file *, char **, size_t *));
    127 static int	search_directory __P((char *, struct open_file *, ino_t *));
    128 #ifdef COMPAT_UFS
    129 static void	ffs_oldfscompat __P((struct fs *));
    130 #endif
    131 
    132 /*
    133  * Read a new inode into a file structure.
    134  */
    135 static int
    136 read_inode(inumber, f)
    137 	ino_t inumber;
    138 	struct open_file *f;
    139 {
    140 	register struct file *fp = (struct file *)f->f_fsdata;
    141 	register struct fs *fs = fp->f_fs;
    142 	char *buf;
    143 	size_t rsize;
    144 	int rc;
    145 
    146 	/*
    147 	 * Read inode and save it.
    148 	 */
    149 	buf = alloc(fs->fs_bsize);
    150 #if !defined(LIBSA_NO_TWIDDLE)
    151 	twiddle();
    152 #endif
    153 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    154 		fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
    155 		buf, &rsize);
    156 	if (rc)
    157 		goto out;
    158 	if (rsize != fs->fs_bsize) {
    159 		rc = EIO;
    160 		goto out;
    161 	}
    162 
    163 	{
    164 		register struct dinode *dp;
    165 
    166 		dp = (struct dinode *)buf;
    167 		fp->f_di = dp[ino_to_fsbo(fs, inumber)];
    168 	}
    169 
    170 	/*
    171 	 * Clear out the old buffers
    172 	 */
    173 	{
    174 		register int level;
    175 
    176 		for (level = 0; level < NIADDR; level++)
    177 			fp->f_blkno[level] = -1;
    178 		fp->f_buf_blkno = -1;
    179 	}
    180 out:
    181 	free(buf, fs->fs_bsize);
    182 	return (rc);
    183 }
    184 
    185 /*
    186  * Given an offset in a file, find the disk block number that
    187  * contains that block.
    188  */
    189 static int
    190 block_map(f, file_block, disk_block_p)
    191 	struct open_file *f;
    192 	daddr_t file_block;
    193 	daddr_t *disk_block_p;	/* out */
    194 {
    195 	register struct file *fp = (struct file *)f->f_fsdata;
    196 	register struct fs *fs = fp->f_fs;
    197 	int level;
    198 	int idx;
    199 	daddr_t ind_block_num;
    200 	daddr_t *ind_p;
    201 	int rc;
    202 
    203 	/*
    204 	 * Index structure of an inode:
    205 	 *
    206 	 * di_db[0..NDADDR-1]	hold block numbers for blocks
    207 	 *			0..NDADDR-1
    208 	 *
    209 	 * di_ib[0]		index block 0 is the single indirect block
    210 	 *			holds block numbers for blocks
    211 	 *			NDADDR .. NDADDR + NINDIR(fs)-1
    212 	 *
    213 	 * di_ib[1]		index block 1 is the double indirect block
    214 	 *			holds block numbers for INDEX blocks for blocks
    215 	 *			NDADDR + NINDIR(fs) ..
    216 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
    217 	 *
    218 	 * di_ib[2]		index block 2 is the triple indirect block
    219 	 *			holds block numbers for double-indirect
    220 	 *			blocks for blocks
    221 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
    222 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2
    223 	 *				+ NINDIR(fs)**3 - 1
    224 	 */
    225 
    226 	if (file_block < NDADDR) {
    227 		/* Direct block. */
    228 		*disk_block_p = fp->f_di.di_db[file_block];
    229 		return (0);
    230 	}
    231 
    232 	file_block -= NDADDR;
    233 
    234 	/*
    235 	 * nindir[0] = NINDIR
    236 	 * nindir[1] = NINDIR**2
    237 	 * nindir[2] = NINDIR**3
    238 	 *	etc
    239 	 */
    240 	for (level = 0; level < NIADDR; level++) {
    241 		if (file_block < fp->f_nindir[level])
    242 			break;
    243 		file_block -= fp->f_nindir[level];
    244 	}
    245 	if (level == NIADDR) {
    246 		/* Block number too high */
    247 		return (EFBIG);
    248 	}
    249 
    250 	ind_block_num = fp->f_di.di_ib[level];
    251 
    252 	for (; level >= 0; level--) {
    253 		if (ind_block_num == 0) {
    254 			*disk_block_p = 0;	/* missing */
    255 			return (0);
    256 		}
    257 
    258 		if (fp->f_blkno[level] != ind_block_num) {
    259 			if (fp->f_blk[level] == (char *)0)
    260 				fp->f_blk[level] =
    261 					alloc(fs->fs_bsize);
    262 #if !defined(LIBSA_NO_TWIDDLE)
    263 			twiddle();
    264 #endif
    265 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    266 				fsbtodb(fp->f_fs, ind_block_num),
    267 				fs->fs_bsize,
    268 				fp->f_blk[level],
    269 				&fp->f_blksize[level]);
    270 			if (rc)
    271 				return (rc);
    272 			if (fp->f_blksize[level] != fs->fs_bsize)
    273 				return (EIO);
    274 			fp->f_blkno[level] = ind_block_num;
    275 		}
    276 
    277 		ind_p = (daddr_t *)fp->f_blk[level];
    278 
    279 		if (level > 0) {
    280 			idx = file_block / fp->f_nindir[level - 1];
    281 			file_block %= fp->f_nindir[level - 1];
    282 		} else
    283 			idx = file_block;
    284 
    285 		ind_block_num = ind_p[idx];
    286 	}
    287 
    288 	*disk_block_p = ind_block_num;
    289 
    290 	return (0);
    291 }
    292 
    293 /*
    294  * Read a portion of a file into an internal buffer.  Return
    295  * the location in the buffer and the amount in the buffer.
    296  */
    297 static int
    298 buf_read_file(f, buf_p, size_p)
    299 	struct open_file *f;
    300 	char **buf_p;		/* out */
    301 	size_t *size_p;		/* out */
    302 {
    303 	register struct file *fp = (struct file *)f->f_fsdata;
    304 	register struct fs *fs = fp->f_fs;
    305 	long off;
    306 	register daddr_t file_block;
    307 	daddr_t	disk_block;
    308 	size_t block_size;
    309 	int rc;
    310 
    311 	off = blkoff(fs, fp->f_seekp);
    312 	file_block = lblkno(fs, fp->f_seekp);
    313 	block_size = dblksize(fs, &fp->f_di, file_block);
    314 
    315 	if (file_block != fp->f_buf_blkno) {
    316 		rc = block_map(f, file_block, &disk_block);
    317 		if (rc)
    318 			return (rc);
    319 
    320 		if (fp->f_buf == (char *)0)
    321 			fp->f_buf = alloc(fs->fs_bsize);
    322 
    323 		if (disk_block == 0) {
    324 			bzero(fp->f_buf, block_size);
    325 			fp->f_buf_size = block_size;
    326 		} else {
    327 #if !defined(LIBSA_NO_TWIDDLE)
    328 			twiddle();
    329 #endif
    330 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    331 				fsbtodb(fs, disk_block),
    332 				block_size, fp->f_buf, &fp->f_buf_size);
    333 			if (rc)
    334 				return (rc);
    335 		}
    336 
    337 		fp->f_buf_blkno = file_block;
    338 	}
    339 
    340 	/*
    341 	 * Return address of byte in buffer corresponding to
    342 	 * offset, and size of remainder of buffer after that
    343 	 * byte.
    344 	 */
    345 	*buf_p = fp->f_buf + off;
    346 	*size_p = block_size - off;
    347 
    348 	/*
    349 	 * But truncate buffer at end of file.
    350 	 */
    351 	if (*size_p > fp->f_di.di_size - fp->f_seekp)
    352 		*size_p = fp->f_di.di_size - fp->f_seekp;
    353 
    354 	return (0);
    355 }
    356 
    357 /*
    358  * Search a directory for a name and return its
    359  * i_number.
    360  */
    361 static int
    362 search_directory(name, f, inumber_p)
    363 	char *name;
    364 	struct open_file *f;
    365 	ino_t *inumber_p;		/* out */
    366 {
    367 	register struct file *fp = (struct file *)f->f_fsdata;
    368 	register struct direct *dp;
    369 	struct direct *edp;
    370 	char *buf;
    371 	size_t buf_size;
    372 	int namlen, length;
    373 	int rc;
    374 
    375 	length = strlen(name);
    376 
    377 	fp->f_seekp = 0;
    378 	while (fp->f_seekp < fp->f_di.di_size) {
    379 		rc = buf_read_file(f, &buf, &buf_size);
    380 		if (rc)
    381 			return (rc);
    382 
    383 		dp = (struct direct *)buf;
    384 		edp = (struct direct *)(buf + buf_size);
    385 		while (dp < edp) {
    386 			if (dp->d_ino == (ino_t)0)
    387 				goto next;
    388 #if BYTE_ORDER == LITTLE_ENDIAN
    389 			if (fp->f_fs->fs_maxsymlinklen <= 0)
    390 				namlen = dp->d_type;
    391 			else
    392 #endif
    393 				namlen = dp->d_namlen;
    394 			if (namlen == length &&
    395 			    !strcmp(name, dp->d_name)) {
    396 				/* found entry */
    397 				*inumber_p = dp->d_ino;
    398 				return (0);
    399 			}
    400 		next:
    401 			dp = (struct direct *)((char *)dp + dp->d_reclen);
    402 		}
    403 		fp->f_seekp += buf_size;
    404 	}
    405 	return (ENOENT);
    406 }
    407 
    408 /*
    409  * Open a file.
    410  */
    411 int
    412 ufs_open(path, f)
    413 	char *path;
    414 	struct open_file *f;
    415 {
    416 #ifndef LIBSA_FS_SINGLECOMPONENT
    417 	register char *cp, *ncp;
    418 	register int c;
    419 #endif
    420 	ino_t inumber;
    421 	struct file *fp;
    422 	struct fs *fs;
    423 	int rc;
    424 	size_t buf_size;
    425 #ifndef LIBSA_NO_FS_SYMLINK
    426 	ino_t parent_inumber;
    427 	int nlinks = 0;
    428 	char namebuf[MAXPATHLEN+1];
    429 	char *buf = NULL;
    430 #endif
    431 
    432 	/* allocate file system specific data structure */
    433 	fp = alloc(sizeof(struct file));
    434 	bzero(fp, sizeof(struct file));
    435 	f->f_fsdata = (void *)fp;
    436 
    437 	/* allocate space and read super block */
    438 	fs = alloc(SBSIZE);
    439 	fp->f_fs = fs;
    440 #if !defined(LIBSA_NO_TWIDDLE)
    441 	twiddle();
    442 #endif
    443 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    444 		SBLOCK, SBSIZE, (char *)fs, &buf_size);
    445 	if (rc)
    446 		goto out;
    447 
    448 	if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
    449 	    fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    450 		rc = EINVAL;
    451 		goto out;
    452 	}
    453 #ifdef COMPAT_UFS
    454 	ffs_oldfscompat(fs);
    455 #endif
    456 
    457 	/*
    458 	 * Calculate indirect block levels.
    459 	 */
    460 	{
    461 		register int mult;
    462 		register int level;
    463 
    464 		mult = 1;
    465 		for (level = 0; level < NIADDR; level++) {
    466 			mult *= NINDIR(fs);
    467 			fp->f_nindir[level] = mult;
    468 		}
    469 	}
    470 
    471 	inumber = ROOTINO;
    472 	if ((rc = read_inode(inumber, f)) != 0)
    473 		goto out;
    474 
    475 #ifndef LIBSA_FS_SINGLECOMPONENT
    476 	cp = path;
    477 	while (*cp) {
    478 
    479 		/*
    480 		 * Remove extra separators
    481 		 */
    482 		while (*cp == '/')
    483 			cp++;
    484 		if (*cp == '\0')
    485 			break;
    486 
    487 		/*
    488 		 * Check that current node is a directory.
    489 		 */
    490 		if ((fp->f_di.di_mode & IFMT) != IFDIR) {
    491 			rc = ENOTDIR;
    492 			goto out;
    493 		}
    494 
    495 		/*
    496 		 * Get next component of path name.
    497 		 */
    498 		{
    499 			register int len = 0;
    500 
    501 			ncp = cp;
    502 			while ((c = *cp) != '\0' && c != '/') {
    503 				if (++len > MAXNAMLEN) {
    504 					rc = ENOENT;
    505 					goto out;
    506 				}
    507 				cp++;
    508 			}
    509 			*cp = '\0';
    510 		}
    511 
    512 		/*
    513 		 * Look up component in current directory.
    514 		 * Save directory inumber in case we find a
    515 		 * symbolic link.
    516 		 */
    517 #ifndef LIBSA_NO_FS_SYMLINK
    518 		parent_inumber = inumber;
    519 #endif
    520 		rc = search_directory(ncp, f, &inumber);
    521 		*cp = c;
    522 		if (rc)
    523 			goto out;
    524 
    525 		/*
    526 		 * Open next component.
    527 		 */
    528 		if ((rc = read_inode(inumber, f)) != 0)
    529 			goto out;
    530 
    531 #ifndef LIBSA_NO_FS_SYMLINK
    532 		/*
    533 		 * Check for symbolic link.
    534 		 */
    535 		if ((fp->f_di.di_mode & IFMT) == IFLNK) {
    536 			int link_len = fp->f_di.di_size;
    537 			int len;
    538 
    539 			len = strlen(cp);
    540 
    541 			if (link_len + len > MAXPATHLEN ||
    542 			    ++nlinks > MAXSYMLINKS) {
    543 				rc = ENOENT;
    544 				goto out;
    545 			}
    546 
    547 			bcopy(cp, &namebuf[link_len], len + 1);
    548 
    549 			if (link_len < fs->fs_maxsymlinklen) {
    550 				bcopy(fp->f_di.di_shortlink, namebuf,
    551 				      (unsigned) link_len);
    552 			} else {
    553 				/*
    554 				 * Read file for symbolic link
    555 				 */
    556 				size_t buf_size;
    557 				daddr_t	disk_block;
    558 				register struct fs *fs = fp->f_fs;
    559 
    560 				if (!buf)
    561 					buf = alloc(fs->fs_bsize);
    562 				rc = block_map(f, (daddr_t)0, &disk_block);
    563 				if (rc)
    564 					goto out;
    565 
    566 #if !defined(LIBSA_NO_TWIDDLE)
    567 				twiddle();
    568 #endif
    569 				rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
    570 					F_READ, fsbtodb(fs, disk_block),
    571 					fs->fs_bsize, buf, &buf_size);
    572 				if (rc)
    573 					goto out;
    574 
    575 				bcopy((char *)buf, namebuf, (unsigned)link_len);
    576 			}
    577 
    578 			/*
    579 			 * If relative pathname, restart at parent directory.
    580 			 * If absolute pathname, restart at root.
    581 			 */
    582 			cp = namebuf;
    583 			if (*cp != '/')
    584 				inumber = parent_inumber;
    585 			else
    586 				inumber = (ino_t)ROOTINO;
    587 
    588 			if ((rc = read_inode(inumber, f)) != 0)
    589 				goto out;
    590 		}
    591 #endif	/* !LIBSA_NO_FS_SYMLINK */
    592 	}
    593 
    594 	/*
    595 	 * Found terminal component.
    596 	 */
    597 	rc = 0;
    598 
    599 #else /* !LIBSA_FS_SINGLECOMPONENT */
    600 
    601 	/* look up component in the current (root) directory */
    602 	rc = search_directory(path, f, &inumber);
    603 	if (rc)
    604 		goto out;
    605 
    606 	/* open it */
    607 	rc = read_inode(inumber, f);
    608 
    609 #endif /* !LIBSA_FS_SINGLECOMPONENT */
    610 
    611 out:
    612 #ifndef LIBSA_NO_FS_SYMLINK
    613 	if (buf)
    614 		free(buf, fs->fs_bsize);
    615 #endif
    616 	if (rc) {
    617 		if (fp->f_buf)
    618 			free(fp->f_buf, fp->f_fs->fs_bsize);
    619 		free(fp->f_fs, SBSIZE);
    620 		free(fp, sizeof(struct file));
    621 	}
    622 	return (rc);
    623 }
    624 
    625 #ifndef LIBSA_NO_FS_CLOSE
    626 int
    627 ufs_close(f)
    628 	struct open_file *f;
    629 {
    630 	register struct file *fp = (struct file *)f->f_fsdata;
    631 	int level;
    632 
    633 	f->f_fsdata = (void *)0;
    634 	if (fp == (struct file *)0)
    635 		return (0);
    636 
    637 	for (level = 0; level < NIADDR; level++) {
    638 		if (fp->f_blk[level])
    639 			free(fp->f_blk[level], fp->f_fs->fs_bsize);
    640 	}
    641 	if (fp->f_buf)
    642 		free(fp->f_buf, fp->f_fs->fs_bsize);
    643 	free(fp->f_fs, SBSIZE);
    644 	free(fp, sizeof(struct file));
    645 	return (0);
    646 }
    647 #endif /* !LIBSA_NO_FS_CLOSE */
    648 
    649 /*
    650  * Copy a portion of a file into kernel memory.
    651  * Cross block boundaries when necessary.
    652  */
    653 int
    654 ufs_read(f, start, size, resid)
    655 	struct open_file *f;
    656 	void *start;
    657 	size_t size;
    658 	size_t *resid;	/* out */
    659 {
    660 	register struct file *fp = (struct file *)f->f_fsdata;
    661 	register size_t csize;
    662 	char *buf;
    663 	size_t buf_size;
    664 	int rc = 0;
    665 	register char *addr = start;
    666 
    667 	while (size != 0) {
    668 		if (fp->f_seekp >= fp->f_di.di_size)
    669 			break;
    670 
    671 		rc = buf_read_file(f, &buf, &buf_size);
    672 		if (rc)
    673 			break;
    674 
    675 		csize = size;
    676 		if (csize > buf_size)
    677 			csize = buf_size;
    678 
    679 		bcopy(buf, addr, csize);
    680 
    681 		fp->f_seekp += csize;
    682 		addr += csize;
    683 		size -= csize;
    684 	}
    685 	if (resid)
    686 		*resid = size;
    687 	return (rc);
    688 }
    689 
    690 /*
    691  * Not implemented.
    692  */
    693 #ifndef LIBSA_NO_FS_WRITE
    694 int
    695 ufs_write(f, start, size, resid)
    696 	struct open_file *f;
    697 	void *start;
    698 	size_t size;
    699 	size_t *resid;	/* out */
    700 {
    701 
    702 	return (EROFS);
    703 }
    704 #endif /* !LIBSA_NO_FS_WRITE */
    705 
    706 #ifndef LIBSA_NO_FS_SEEK
    707 off_t
    708 ufs_seek(f, offset, where)
    709 	struct open_file *f;
    710 	off_t offset;
    711 	int where;
    712 {
    713 	register struct file *fp = (struct file *)f->f_fsdata;
    714 
    715 	switch (where) {
    716 	case SEEK_SET:
    717 		fp->f_seekp = offset;
    718 		break;
    719 	case SEEK_CUR:
    720 		fp->f_seekp += offset;
    721 		break;
    722 	case SEEK_END:
    723 		fp->f_seekp = fp->f_di.di_size - offset;
    724 		break;
    725 	default:
    726 		return (-1);
    727 	}
    728 	return (fp->f_seekp);
    729 }
    730 #endif /* !LIBSA_NO_FS_SEEK */
    731 
    732 int
    733 ufs_stat(f, sb)
    734 	struct open_file *f;
    735 	struct stat *sb;
    736 {
    737 	register struct file *fp = (struct file *)f->f_fsdata;
    738 
    739 	/* only important stuff */
    740 	sb->st_mode = fp->f_di.di_mode;
    741 	sb->st_uid = fp->f_di.di_uid;
    742 	sb->st_gid = fp->f_di.di_gid;
    743 	sb->st_size = fp->f_di.di_size;
    744 	return (0);
    745 }
    746 
    747 #ifdef COMPAT_UFS
    748 /*
    749  * Sanity checks for old file systems.
    750  *
    751  * XXX - goes away some day.
    752  */
    753 static void
    754 ffs_oldfscompat(fs)
    755 	struct fs *fs;
    756 {
    757 	int i;
    758 
    759 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    760 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    761 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    762 		fs->fs_nrpos = 8;				/* XXX */
    763 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    764 		quad_t sizepb = fs->fs_bsize;			/* XXX */
    765 								/* XXX */
    766 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    767 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    768 			sizepb *= NINDIR(fs);			/* XXX */
    769 			fs->fs_maxfilesize += sizepb;		/* XXX */
    770 		}						/* XXX */
    771 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    772 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    773 	}							/* XXX */
    774 }
    775 #endif
    776