xref: /freebsd/stand/libsa/ufs.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
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 /*
71  *	Stand-alone file reading package.
72  */
73 
74 #include <sys/param.h>
75 #include <sys/disklabel.h>
76 #include <sys/time.h>
77 #include <ufs/ufs/dinode.h>
78 #include <ufs/ufs/dir.h>
79 #include <ufs/ffs/fs.h>
80 #include "stand.h"
81 #include "string.h"
82 
83 static int	ufs_open(const char *path, struct open_file *f);
84 static int	ufs_write(struct open_file *f, const void *buf, size_t size,
85 		size_t *resid);
86 static int	ufs_close(struct open_file *f);
87 static int	ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
88 static off_t	ufs_seek(struct open_file *f, off_t offset, int where);
89 static int	ufs_stat(struct open_file *f, struct stat *sb);
90 static int	ufs_readdir(struct open_file *f, struct dirent *d);
91 static int	ufs_mount(const char *dev, const char *path, void **data);
92 static int	ufs_unmount(const char *dev, void *data);
93 
94 struct fs_ops ufs_fsops = {
95 	.fs_name = "ufs",
96 	.fo_open = ufs_open,
97 	.fo_close = ufs_close,
98 	.fo_read = ufs_read,
99 	.fo_write = ufs_write,
100 	.fo_seek = ufs_seek,
101 	.fo_stat = ufs_stat,
102 	.fo_readdir = ufs_readdir,
103 	.fo_mount = ufs_mount,
104 	.fo_unmount = ufs_unmount
105 };
106 
107 /*
108  * In-core open file.
109  */
110 struct file {
111 	off_t		f_seekp;	/* seek pointer */
112 	struct fs	*f_fs;		/* pointer to super-block */
113 	union dinode {
114 		struct ufs1_dinode di1;
115 		struct ufs2_dinode di2;
116 	}		f_di;		/* copy of on-disk inode */
117 	int		f_nindir[UFS_NIADDR];
118 					/* number of blocks mapped by
119 					   indirect block at level i */
120 	char		*f_blk[UFS_NIADDR];	/* buffer for indirect block at
121 					   level i */
122 	size_t		f_blksize[UFS_NIADDR];
123 					/* size of buffer */
124 	ufs2_daddr_t	f_blkno[UFS_NIADDR];/* disk address of block in buffer */
125 	ufs2_daddr_t	f_buf_blkno;	/* block number of data block */
126 	char		*f_buf;		/* buffer for data block */
127 	size_t		f_buf_size;	/* size of data block */
128 	int		f_inumber;	/* inumber */
129 };
130 #define DIP(fp, field) \
131 	((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
132 	(fp)->f_di.di1.field : (fp)->f_di.di2.field)
133 
134 typedef struct ufs_mnt {
135 	char			*um_dev;
136 	int			um_fd;
137 	STAILQ_ENTRY(ufs_mnt)	um_link;
138 } ufs_mnt_t;
139 
140 typedef STAILQ_HEAD(ufs_mnt_list, ufs_mnt) ufs_mnt_list_t;
141 static ufs_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
142 
143 static int	read_inode(ino_t, struct open_file *);
144 static int	block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
145 static int	buf_read_file(struct open_file *, char **, size_t *);
146 static int	buf_write_file(struct open_file *, const char *, size_t *);
147 static int	search_directory(char *, struct open_file *, ino_t *);
148 static int	ufs_use_sa_read(void *, off_t, void **, int);
149 
150 /* from ffs_subr.c */
151 int	ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
152 	    char *filltype,
153 	    int (*readfunc)(void *devfd, off_t loc, void **bufp, int size));
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 == NULL)
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_sbget(f, &fs, UFS_STDSB, UFS_NOHASHFAIL, "stand",
533 		    ufs_use_sa_read)) != 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 
889 	do {
890 		if (fp->f_seekp >= DIP(fp, di_size))
891 			return (ENOENT);
892 		error = buf_read_file(f, &buf, &buf_size);
893 		if (error)
894 			return (error);
895 		dp = (struct direct *)buf;
896 		fp->f_seekp += dp->d_reclen;
897 	} while (dp->d_ino == (ino_t)0);
898 
899 	d->d_type = dp->d_type;
900 	strcpy(d->d_name, dp->d_name);
901 	return (0);
902 }
903 
904 static int
905 ufs_mount(const char *dev, const char *path, void **data)
906 {
907 	char *fs;
908 	ufs_mnt_t *mnt;
909 	struct open_file *f;
910 
911 	errno = 0;
912 	mnt = calloc(1, sizeof(*mnt));
913 	if (mnt == NULL)
914 		return (errno);
915 	mnt->um_fd = -1;
916 	mnt->um_dev = strdup(dev);
917 	if (mnt->um_dev == NULL)
918 		goto done;
919 
920 	if (asprintf(&fs, "%s%s", dev, path) < 0)
921 		goto done;
922 
923 	mnt->um_fd = open(fs, O_RDONLY);
924 	free(fs);
925 	if (mnt->um_fd == -1)
926 		goto done;
927 
928 	/* Is it ufs file system? */
929 	f = fd2open_file(mnt->um_fd);
930 	if (strcmp(f->f_ops->fs_name, "ufs") == 0)
931 		STAILQ_INSERT_TAIL(&mnt_list, mnt, um_link);
932 	else
933 		errno = ENXIO;
934 
935 done:
936 	if (errno != 0) {
937 		free(mnt->um_dev);
938 		if (mnt->um_fd >= 0)
939 			close(mnt->um_fd);
940 		free(mnt);
941 	} else {
942 		*data = mnt;
943 	}
944 
945 	return (errno);
946 }
947 
948 static int
949 ufs_unmount(const char *dev __unused, void *data)
950 {
951 	ufs_mnt_t *mnt = data;
952 
953 	STAILQ_REMOVE(&mnt_list, mnt, ufs_mnt, um_link);
954 	free(mnt->um_dev);
955 	close(mnt->um_fd);
956 	free(mnt);
957 	return (0);
958 }
959