xref: /freebsd/stand/libsa/ufs.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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, const void *buf, size_t size,
88 		size_t *resid);
89 static int	ufs_close(struct open_file *f);
90 static int	ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
91 static off_t	ufs_seek(struct open_file *f, off_t offset, int where);
92 static int	ufs_stat(struct open_file *f, struct stat *sb);
93 static int	ufs_readdir(struct open_file *f, struct dirent *d);
94 
95 struct fs_ops ufs_fsops = {
96 	"ufs",
97 	ufs_open,
98 	ufs_close,
99 	ufs_read,
100 	ufs_write,
101 	ufs_seek,
102 	ufs_stat,
103 	ufs_readdir
104 };
105 
106 /*
107  * In-core open file.
108  */
109 struct file {
110 	off_t		f_seekp;	/* seek pointer */
111 	struct fs	*f_fs;		/* pointer to super-block */
112 	union dinode {
113 		struct ufs1_dinode di1;
114 		struct ufs2_dinode di2;
115 	}		f_di;		/* copy of on-disk inode */
116 	int		f_nindir[UFS_NIADDR];
117 					/* number of blocks mapped by
118 					   indirect block at level i */
119 	char		*f_blk[UFS_NIADDR];	/* buffer for indirect block at
120 					   level i */
121 	size_t		f_blksize[UFS_NIADDR];
122 					/* size of buffer */
123 	ufs2_daddr_t	f_blkno[UFS_NIADDR];/* disk address of block in buffer */
124 	ufs2_daddr_t	f_buf_blkno;	/* block number of data block */
125 	char		*f_buf;		/* buffer for data block */
126 	size_t		f_buf_size;	/* size of data block */
127 	int		f_inumber;	/* inumber */
128 };
129 #define DIP(fp, field) \
130 	((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
131 	(fp)->f_di.di1.field : (fp)->f_di.di2.field)
132 
133 static int	read_inode(ino_t, struct open_file *);
134 static int	block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
135 static int	buf_read_file(struct open_file *, char **, size_t *);
136 static int	buf_write_file(struct open_file *, const char *, size_t *);
137 static int	search_directory(char *, struct open_file *, ino_t *);
138 static int	ufs_use_sa_read(void *, off_t, void **, int);
139 
140 /* from ffs_subr.c */
141 int	ffs_sbget(void *, struct fs **, off_t, char *,
142 	    int (*)(void *, off_t, void **, int));
143 /*
144  * Request standard superblock location in ffs_sbget
145  */
146 #define	STDSB			-1	/* Fail if check-hash is bad */
147 #define	STDSB_NOHASHFAIL	-2	/* Ignore check-hash failure */
148 
149 /*
150  * Read a new inode into a file structure.
151  */
152 static int
153 read_inode(inumber, f)
154 	ino_t inumber;
155 	struct open_file *f;
156 {
157 	struct file *fp = (struct file *)f->f_fsdata;
158 	struct fs *fs = fp->f_fs;
159 	char *buf;
160 	size_t rsize;
161 	int rc;
162 
163 	if (fs == NULL)
164 	    panic("fs == NULL");
165 
166 	/*
167 	 * Read inode and save it.
168 	 */
169 	buf = malloc(fs->fs_bsize);
170 	twiddle(1);
171 	rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
172 		fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
173 		buf, &rsize);
174 	if (rc)
175 		goto out;
176 	if (rsize != fs->fs_bsize) {
177 		rc = EIO;
178 		goto out;
179 	}
180 
181 	if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
182 		fp->f_di.di1 = ((struct ufs1_dinode *)buf)
183 		    [ino_to_fsbo(fs, inumber)];
184 	else
185 		fp->f_di.di2 = ((struct ufs2_dinode *)buf)
186 		    [ino_to_fsbo(fs, inumber)];
187 
188 	/*
189 	 * Clear out the old buffers
190 	 */
191 	{
192 		int level;
193 
194 		for (level = 0; level < UFS_NIADDR; level++)
195 			fp->f_blkno[level] = -1;
196 		fp->f_buf_blkno = -1;
197 	}
198 	fp->f_seekp = 0;
199 	fp->f_inumber = inumber;
200 out:
201 	free(buf);
202 	return (rc);
203 }
204 
205 /*
206  * Given an offset in a file, find the disk block number that
207  * contains that block.
208  */
209 static int
210 block_map(f, file_block, disk_block_p)
211 	struct open_file *f;
212 	ufs2_daddr_t file_block;
213 	ufs2_daddr_t *disk_block_p;	/* out */
214 {
215 	struct file *fp = (struct file *)f->f_fsdata;
216 	struct fs *fs = fp->f_fs;
217 	int level;
218 	int idx;
219 	ufs2_daddr_t ind_block_num;
220 	int rc;
221 
222 	/*
223 	 * Index structure of an inode:
224 	 *
225 	 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
226 	 *			0..UFS_NDADDR-1
227 	 *
228 	 * di_ib[0]		index block 0 is the single indirect block
229 	 *			holds block numbers for blocks
230 	 *			UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1
231 	 *
232 	 * di_ib[1]		index block 1 is the double indirect block
233 	 *			holds block numbers for INDEX blocks for blocks
234 	 *			UFS_NDADDR + NINDIR(fs) ..
235 	 *			UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
236 	 *
237 	 * di_ib[2]		index block 2 is the triple indirect block
238 	 *			holds block numbers for double-indirect
239 	 *			blocks for blocks
240 	 *			UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
241 	 *			UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
242 	 *				+ NINDIR(fs)**3 - 1
243 	 */
244 
245 	if (file_block < UFS_NDADDR) {
246 		/* Direct block. */
247 		*disk_block_p = DIP(fp, di_db[file_block]);
248 		return (0);
249 	}
250 
251 	file_block -= UFS_NDADDR;
252 
253 	/*
254 	 * nindir[0] = NINDIR
255 	 * nindir[1] = NINDIR**2
256 	 * nindir[2] = NINDIR**3
257 	 *	etc
258 	 */
259 	for (level = 0; level < UFS_NIADDR; level++) {
260 		if (file_block < fp->f_nindir[level])
261 			break;
262 		file_block -= fp->f_nindir[level];
263 	}
264 	if (level == UFS_NIADDR) {
265 		/* Block number too high */
266 		return (EFBIG);
267 	}
268 
269 	ind_block_num = DIP(fp, di_ib[level]);
270 
271 	for (; level >= 0; level--) {
272 		if (ind_block_num == 0) {
273 			*disk_block_p = 0;	/* missing */
274 			return (0);
275 		}
276 
277 		if (fp->f_blkno[level] != ind_block_num) {
278 			if (fp->f_blk[level] == (char *)0)
279 				fp->f_blk[level] =
280 					malloc(fs->fs_bsize);
281 			twiddle(1);
282 			rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
283 				fsbtodb(fp->f_fs, ind_block_num),
284 				fs->fs_bsize,
285 				fp->f_blk[level],
286 				&fp->f_blksize[level]);
287 			if (rc)
288 				return (rc);
289 			if (fp->f_blksize[level] != fs->fs_bsize)
290 				return (EIO);
291 			fp->f_blkno[level] = ind_block_num;
292 		}
293 
294 		if (level > 0) {
295 			idx = file_block / fp->f_nindir[level - 1];
296 			file_block %= fp->f_nindir[level - 1];
297 		} else
298 			idx = file_block;
299 
300 		if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
301 			ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
302 		else
303 			ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
304 	}
305 
306 	*disk_block_p = ind_block_num;
307 
308 	return (0);
309 }
310 
311 /*
312  * Write a portion of a file from an internal buffer.
313  */
314 static int
315 buf_write_file(f, buf_p, size_p)
316 	struct open_file *f;
317 	const char *buf_p;
318 	size_t *size_p;		/* out */
319 {
320 	struct file *fp = (struct file *)f->f_fsdata;
321 	struct fs *fs = fp->f_fs;
322 	long off;
323 	ufs_lbn_t file_block;
324 	ufs2_daddr_t disk_block;
325 	size_t block_size;
326 	int rc;
327 
328 	/*
329 	 * Calculate the starting block address and offset.
330 	 */
331 	off = blkoff(fs, fp->f_seekp);
332 	file_block = lblkno(fs, fp->f_seekp);
333 	block_size = sblksize(fs, DIP(fp, di_size), file_block);
334 
335 	rc = block_map(f, file_block, &disk_block);
336 	if (rc)
337 		return (rc);
338 
339  	if (disk_block == 0)
340 		/* Because we can't allocate space on the drive */
341 		return (EFBIG);
342 
343 	/*
344 	 * Truncate buffer at end of file, and at the end of
345 	 * this block.
346 	 */
347 	if (*size_p > DIP(fp, di_size) - fp->f_seekp)
348 		*size_p = DIP(fp, di_size) - fp->f_seekp;
349 	if (*size_p > block_size - off)
350 		*size_p = block_size - off;
351 
352 	/*
353 	 * If we don't entirely occlude the block and it's not
354 	 * in memory already, read it in first.
355 	 */
356 	if (((off > 0) || (*size_p + off < block_size)) &&
357 	    (file_block != fp->f_buf_blkno)) {
358 
359 		if (fp->f_buf == (char *)0)
360 			fp->f_buf = malloc(fs->fs_bsize);
361 
362 		twiddle(4);
363 		rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
364 			fsbtodb(fs, disk_block),
365 			block_size, fp->f_buf, &fp->f_buf_size);
366 		if (rc)
367 			return (rc);
368 
369 		fp->f_buf_blkno = file_block;
370 	}
371 
372 	/*
373 	 *	Copy the user data into the cached block.
374 	 */
375 	bcopy(buf_p, fp->f_buf + off, *size_p);
376 
377 	/*
378 	 *	Write the block out to storage.
379 	 */
380 
381 	twiddle(4);
382 	rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
383 		fsbtodb(fs, disk_block),
384 		block_size, fp->f_buf, &fp->f_buf_size);
385 	return (rc);
386 }
387 
388 /*
389  * Read a portion of a file into an internal buffer.  Return
390  * the location in the buffer and the amount in the buffer.
391  */
392 static int
393 buf_read_file(f, buf_p, size_p)
394 	struct open_file *f;
395 	char **buf_p;		/* out */
396 	size_t *size_p;		/* out */
397 {
398 	struct file *fp = (struct file *)f->f_fsdata;
399 	struct fs *fs = fp->f_fs;
400 	long off;
401 	ufs_lbn_t file_block;
402 	ufs2_daddr_t disk_block;
403 	size_t block_size;
404 	int rc;
405 
406 	off = blkoff(fs, fp->f_seekp);
407 	file_block = lblkno(fs, fp->f_seekp);
408 	block_size = sblksize(fs, DIP(fp, di_size), file_block);
409 
410 	if (file_block != fp->f_buf_blkno) {
411 		if (fp->f_buf == (char *)0)
412 			fp->f_buf = malloc(fs->fs_bsize);
413 
414 		rc = block_map(f, file_block, &disk_block);
415 		if (rc)
416 			return (rc);
417 
418 		if (disk_block == 0) {
419 			bzero(fp->f_buf, block_size);
420 			fp->f_buf_size = block_size;
421 		} else {
422 			twiddle(4);
423 			rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
424 				fsbtodb(fs, disk_block),
425 				block_size, fp->f_buf, &fp->f_buf_size);
426 			if (rc)
427 				return (rc);
428 		}
429 
430 		fp->f_buf_blkno = file_block;
431 	}
432 
433 	/*
434 	 * Return address of byte in buffer corresponding to
435 	 * offset, and size of remainder of buffer after that
436 	 * byte.
437 	 */
438 	*buf_p = fp->f_buf + off;
439 	*size_p = block_size - off;
440 
441 	/*
442 	 * But truncate buffer at end of file.
443 	 */
444 	if (*size_p > DIP(fp, di_size) - fp->f_seekp)
445 		*size_p = DIP(fp, di_size) - fp->f_seekp;
446 
447 	return (0);
448 }
449 
450 /*
451  * Search a directory for a name and return its
452  * i_number.
453  */
454 static int
455 search_directory(name, f, inumber_p)
456 	char *name;
457 	struct open_file *f;
458 	ino_t *inumber_p;		/* out */
459 {
460 	struct file *fp = (struct file *)f->f_fsdata;
461 	struct direct *dp;
462 	struct direct *edp;
463 	char *buf;
464 	size_t buf_size;
465 	int namlen, length;
466 	int rc;
467 
468 	length = strlen(name);
469 
470 	fp->f_seekp = 0;
471 	while (fp->f_seekp < DIP(fp, di_size)) {
472 		rc = buf_read_file(f, &buf, &buf_size);
473 		if (rc)
474 			return (rc);
475 
476 		dp = (struct direct *)buf;
477 		edp = (struct direct *)(buf + buf_size);
478 		while (dp < edp) {
479 			if (dp->d_ino == (ino_t)0)
480 				goto next;
481 #if BYTE_ORDER == LITTLE_ENDIAN
482 			if (fp->f_fs->fs_maxsymlinklen <= 0)
483 				namlen = dp->d_type;
484 			else
485 #endif
486 				namlen = dp->d_namlen;
487 			if (namlen == length &&
488 			    !strcmp(name, dp->d_name)) {
489 				/* found entry */
490 				*inumber_p = dp->d_ino;
491 				return (0);
492 			}
493 		next:
494 			dp = (struct direct *)((char *)dp + dp->d_reclen);
495 		}
496 		fp->f_seekp += buf_size;
497 	}
498 	return (ENOENT);
499 }
500 
501 /*
502  * Open a file.
503  */
504 static int
505 ufs_open(upath, f)
506 	const char *upath;
507 	struct open_file *f;
508 {
509 	char *cp, *ncp;
510 	int c;
511 	ino_t inumber, parent_inumber;
512 	struct file *fp;
513 	struct fs *fs;
514 	int rc;
515 	int nlinks = 0;
516 	char namebuf[MAXPATHLEN+1];
517 	char *buf = NULL;
518 	char *path = NULL;
519 
520 	/* allocate file system specific data structure */
521 	fp = malloc(sizeof(struct file));
522 	bzero(fp, sizeof(struct file));
523 	f->f_fsdata = (void *)fp;
524 
525 	/* read super block */
526 	twiddle(1);
527 	if ((rc = ffs_sbget(f, &fs, STDSB_NOHASHFAIL, "stand",
528 	     ufs_use_sa_read)) != 0)
529 		goto out;
530 	fp->f_fs = fs;
531 	/*
532 	 * Calculate indirect block levels.
533 	 */
534 	{
535 		ufs2_daddr_t mult;
536 		int level;
537 
538 		mult = 1;
539 		for (level = 0; level < UFS_NIADDR; level++) {
540 			mult *= NINDIR(fs);
541 			fp->f_nindir[level] = mult;
542 		}
543 	}
544 
545 	inumber = UFS_ROOTINO;
546 	if ((rc = read_inode(inumber, f)) != 0)
547 		goto out;
548 
549 	cp = path = strdup(upath);
550 	if (path == NULL) {
551 	    rc = ENOMEM;
552 	    goto out;
553 	}
554 	while (*cp) {
555 
556 		/*
557 		 * Remove extra separators
558 		 */
559 		while (*cp == '/')
560 			cp++;
561 		if (*cp == '\0')
562 			break;
563 
564 		/*
565 		 * Check that current node is a directory.
566 		 */
567 		if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
568 			rc = ENOTDIR;
569 			goto out;
570 		}
571 
572 		/*
573 		 * Get next component of path name.
574 		 */
575 		{
576 			int len = 0;
577 
578 			ncp = cp;
579 			while ((c = *cp) != '\0' && c != '/') {
580 				if (++len > UFS_MAXNAMLEN) {
581 					rc = ENOENT;
582 					goto out;
583 				}
584 				cp++;
585 			}
586 			*cp = '\0';
587 		}
588 
589 		/*
590 		 * Look up component in current directory.
591 		 * Save directory inumber in case we find a
592 		 * symbolic link.
593 		 */
594 		parent_inumber = inumber;
595 		rc = search_directory(ncp, f, &inumber);
596 		*cp = c;
597 		if (rc)
598 			goto out;
599 
600 		/*
601 		 * Open next component.
602 		 */
603 		if ((rc = read_inode(inumber, f)) != 0)
604 			goto out;
605 
606 		/*
607 		 * Check for symbolic link.
608 		 */
609 		if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
610 			int link_len = DIP(fp, di_size);
611 			int len;
612 
613 			len = strlen(cp);
614 
615 			if (link_len + len > MAXPATHLEN ||
616 			    ++nlinks > MAXSYMLINKS) {
617 				rc = ENOENT;
618 				goto out;
619 			}
620 
621 			bcopy(cp, &namebuf[link_len], len + 1);
622 
623 			if (link_len < fs->fs_maxsymlinklen) {
624 				if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
625 					cp = (caddr_t)(fp->f_di.di1.di_db);
626 				else
627 					cp = (caddr_t)(fp->f_di.di2.di_db);
628 				bcopy(cp, namebuf, (unsigned) link_len);
629 			} else {
630 				/*
631 				 * Read file for symbolic link
632 				 */
633 				size_t buf_size;
634 				ufs2_daddr_t disk_block;
635 				struct fs *fs = fp->f_fs;
636 
637 				if (!buf)
638 					buf = malloc(fs->fs_bsize);
639 				rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
640 				if (rc)
641 					goto out;
642 
643 				twiddle(1);
644 				rc = (f->f_dev->dv_strategy)(f->f_devdata,
645 					F_READ, fsbtodb(fs, disk_block),
646 					fs->fs_bsize, buf, &buf_size);
647 				if (rc)
648 					goto out;
649 
650 				bcopy((char *)buf, namebuf, (unsigned)link_len);
651 			}
652 
653 			/*
654 			 * If relative pathname, restart at parent directory.
655 			 * If absolute pathname, restart at root.
656 			 */
657 			cp = namebuf;
658 			if (*cp != '/')
659 				inumber = parent_inumber;
660 			else
661 				inumber = (ino_t)UFS_ROOTINO;
662 
663 			if ((rc = read_inode(inumber, f)) != 0)
664 				goto out;
665 		}
666 	}
667 
668 	/*
669 	 * Found terminal component.
670 	 */
671 	rc = 0;
672 	fp->f_seekp = 0;
673 out:
674 	if (buf)
675 		free(buf);
676 	if (path)
677 		free(path);
678 	if (rc) {
679 		if (fp->f_buf)
680 			free(fp->f_buf);
681 		free(fp->f_fs);
682 		free(fp);
683 	}
684 	return (rc);
685 }
686 
687 /*
688  * A read function for use by standalone-layer routines.
689  */
690 static int
691 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size)
692 {
693 	struct open_file *f;
694 	size_t buf_size;
695 	int error;
696 
697 	f = (struct open_file *)devfd;
698 	if ((*bufp = malloc(size)) == NULL)
699 		return (ENOSPC);
700 	error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE,
701 	    size, *bufp, &buf_size);
702 	if (error != 0)
703 		return (error);
704 	if (buf_size != size)
705 		return (EIO);
706 	return (0);
707 }
708 
709 static int
710 ufs_close(f)
711 	struct open_file *f;
712 {
713 	struct file *fp = (struct file *)f->f_fsdata;
714 	int level;
715 
716 	f->f_fsdata = (void *)0;
717 	if (fp == (struct file *)0)
718 		return (0);
719 
720 	for (level = 0; level < UFS_NIADDR; level++) {
721 		if (fp->f_blk[level])
722 			free(fp->f_blk[level]);
723 	}
724 	if (fp->f_buf)
725 		free(fp->f_buf);
726 	free(fp->f_fs);
727 	free(fp);
728 	return (0);
729 }
730 
731 /*
732  * Copy a portion of a file into kernel memory.
733  * Cross block boundaries when necessary.
734  */
735 static int
736 ufs_read(f, start, size, resid)
737 	struct open_file *f;
738 	void *start;
739 	size_t size;
740 	size_t *resid;	/* out */
741 {
742 	struct file *fp = (struct file *)f->f_fsdata;
743 	size_t csize;
744 	char *buf;
745 	size_t buf_size;
746 	int rc = 0;
747 	char *addr = start;
748 
749 	while (size != 0) {
750 		if (fp->f_seekp >= DIP(fp, di_size))
751 			break;
752 
753 		rc = buf_read_file(f, &buf, &buf_size);
754 		if (rc)
755 			break;
756 
757 		csize = size;
758 		if (csize > buf_size)
759 			csize = buf_size;
760 
761 		bcopy(buf, addr, csize);
762 
763 		fp->f_seekp += csize;
764 		addr += csize;
765 		size -= csize;
766 	}
767 	if (resid)
768 		*resid = size;
769 	return (rc);
770 }
771 
772 /*
773  * Write to a portion of an already allocated file.
774  * Cross block boundaries when necessary. Can not
775  * extend the file.
776  */
777 static int
778 ufs_write(f, start, size, resid)
779 	struct open_file *f;
780 	const void *start;
781 	size_t size;
782 	size_t *resid;	/* out */
783 {
784 	struct file *fp = (struct file *)f->f_fsdata;
785 	size_t csize;
786 	int rc = 0;
787 	const char *addr = start;
788 
789 	csize = size;
790 	while ((size != 0) && (csize != 0)) {
791 		if (fp->f_seekp >= DIP(fp, di_size))
792 			break;
793 
794 		if (csize >= 512) csize = 512; /* XXX */
795 
796 		rc = buf_write_file(f, addr, &csize);
797 		if (rc)
798 			break;
799 
800 		fp->f_seekp += csize;
801 		addr += csize;
802 		size -= csize;
803 	}
804 	if (resid)
805 		*resid = size;
806 	return (rc);
807 }
808 
809 static off_t
810 ufs_seek(f, offset, where)
811 	struct open_file *f;
812 	off_t offset;
813 	int where;
814 {
815 	struct file *fp = (struct file *)f->f_fsdata;
816 
817 	switch (where) {
818 	case SEEK_SET:
819 		fp->f_seekp = offset;
820 		break;
821 	case SEEK_CUR:
822 		fp->f_seekp += offset;
823 		break;
824 	case SEEK_END:
825 		fp->f_seekp = DIP(fp, di_size) - offset;
826 		break;
827 	default:
828 		errno = EINVAL;
829 		return (-1);
830 	}
831 	return (fp->f_seekp);
832 }
833 
834 static int
835 ufs_stat(f, sb)
836 	struct open_file *f;
837 	struct stat *sb;
838 {
839 	struct file *fp = (struct file *)f->f_fsdata;
840 
841 	/* only important stuff */
842 	sb->st_mode = DIP(fp, di_mode);
843 	sb->st_uid = DIP(fp, di_uid);
844 	sb->st_gid = DIP(fp, di_gid);
845 	sb->st_size = DIP(fp, di_size);
846 	sb->st_mtime = DIP(fp, di_mtime);
847 	/*
848 	 * The items below are ufs specific!
849 	 * Other fs types will need their own solution
850 	 * if these fields are needed.
851 	 */
852 	sb->st_ino = fp->f_inumber;
853 	/*
854 	 * We need something to differentiate devs.
855 	 * fs_id is unique but 64bit, we xor the two
856 	 * halves to squeeze it into 32bits.
857 	 */
858 	sb->st_dev = (dev_t)(fp->f_fs->fs_id[0] ^ fp->f_fs->fs_id[1]);
859 
860 	return (0);
861 }
862 
863 static int
864 ufs_readdir(struct open_file *f, struct dirent *d)
865 {
866 	struct file *fp = (struct file *)f->f_fsdata;
867 	struct direct *dp;
868 	char *buf;
869 	size_t buf_size;
870 	int error;
871 
872 	/*
873 	 * assume that a directory entry will not be split across blocks
874 	 */
875 again:
876 	if (fp->f_seekp >= DIP(fp, di_size))
877 		return (ENOENT);
878 	error = buf_read_file(f, &buf, &buf_size);
879 	if (error)
880 		return (error);
881 	dp = (struct direct *)buf;
882 	fp->f_seekp += dp->d_reclen;
883 	if (dp->d_ino == (ino_t)0)
884 		goto again;
885 	d->d_type = dp->d_type;
886 	strcpy(d->d_name, dp->d_name);
887 	return (0);
888 }
889