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