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