xref: /netbsd-src/sys/lib/libsa/ext2fs.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: ext2fs.c,v 1.5 2008/11/19 12:36:41 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Manuel Bouyer.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * The Mach Operating System project at Carnegie-Mellon University.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  *
64  * Copyright (c) 1990, 1991 Carnegie Mellon University
65  * All Rights Reserved.
66  *
67  * Author: David Golub
68  *
69  * Permission to use, copy, modify and distribute this software and its
70  * documentation is hereby granted, provided that both the copyright
71  * notice and this permission notice appear in all copies of the
72  * software, derivative works or modified versions, and any portions
73  * thereof, and that both notices appear in supporting documentation.
74  *
75  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
76  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
77  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
78  *
79  * Carnegie Mellon requests users of this software to return to
80  *
81  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
82  *  School of Computer Science
83  *  Carnegie Mellon University
84  *  Pittsburgh PA 15213-3890
85  *
86  * any improvements or extensions that they make and grant Carnegie the
87  * rights to redistribute these changes.
88  */
89 
90 /*
91  *	Stand-alone file reading package for Ext2 file system.
92  */
93 
94 /* #define EXT2FS_DEBUG */
95 
96 #include <sys/param.h>
97 #include <sys/time.h>
98 #include <ufs/ext2fs/ext2fs_dinode.h>
99 #include <ufs/ext2fs/ext2fs_dir.h>
100 #include <ufs/ext2fs/ext2fs.h>
101 #ifdef _STANDALONE
102 #include <lib/libkern/libkern.h>
103 #else
104 #include <string.h>
105 #endif
106 
107 #include "stand.h"
108 #include "ext2fs.h"
109 
110 #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
111 #define LIBSA_NO_FS_SYMLINK
112 #endif
113 
114 #if defined(LIBSA_NO_TWIDDLE)
115 #define twiddle()
116 #endif
117 
118 #ifndef indp_t
119 #define indp_t		int32_t
120 #endif
121 typedef uint32_t	ino32_t;
122 #ifndef FSBTODB
123 #define FSBTODB(fs, indp) fsbtodb(fs, indp)
124 #endif
125 
126 /*
127  * To avoid having a lot of filesystem-block sized buffers lurking (which
128  * could be 32k) we only keep a few entries of the indirect block map.
129  * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block
130  * ~13 times pulling in a 6M kernel.
131  * The cache size must be smaller than the smallest filesystem block,
132  * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks).
133  */
134 #define LN2_IND_CACHE_SZ	6
135 #define IND_CACHE_SZ		(1 << LN2_IND_CACHE_SZ)
136 #define IND_CACHE_MASK		(IND_CACHE_SZ - 1)
137 
138 /*
139  * In-core open file.
140  */
141 struct file {
142 	off_t		f_seekp;	/* seek pointer */
143 	struct m_ext2fs	*f_fs;		/* pointer to super-block */
144 	struct ext2fs_dinode	f_di;		/* copy of on-disk inode */
145 	uint		f_nishift;	/* for blocks in indirect block */
146 	indp_t		f_ind_cache_block;
147 	indp_t		f_ind_cache[IND_CACHE_SZ];
148 
149 	char		*f_buf;		/* buffer for data block */
150 	size_t		f_buf_size;	/* size of data block */
151 	daddr_t		f_buf_blkno;	/* block number of data block */
152 };
153 
154 static int read_inode(ino32_t, struct open_file *);
155 static int block_map(struct open_file *, indp_t, indp_t *);
156 static int buf_read_file(struct open_file *, char **, size_t *);
157 static int search_directory(const char *, int, struct open_file *, ino32_t *);
158 static int read_sblock(struct open_file *, struct m_ext2fs *);
159 static int read_gdblock(struct open_file *, struct m_ext2fs *);
160 #ifdef EXT2FS_DEBUG
161 static void dump_sblock(struct m_ext2fs *);
162 #endif
163 
164 /*
165  * Read a new inode into a file structure.
166  */
167 static int
168 read_inode(ino32_t inumber, struct open_file *f)
169 {
170 	struct file *fp = (struct file *)f->f_fsdata;
171 	struct m_ext2fs *fs = fp->f_fs;
172 	char *buf;
173 	size_t rsize;
174 	int rc;
175 	daddr_t inode_sector;
176 	struct ext2fs_dinode *dip;
177 
178 	inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
179 
180 	/*
181 	 * Read inode and save it.
182 	 */
183 	buf = fp->f_buf;
184 	twiddle();
185 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
186 	    inode_sector, fs->e2fs_bsize, buf, &rsize);
187 	if (rc)
188 		return rc;
189 	if (rsize != fs->e2fs_bsize)
190 		return EIO;
191 
192 	dip = (struct ext2fs_dinode *)buf;
193 	e2fs_iload(&dip[ino_to_fsbo(fs, inumber)], &fp->f_di);
194 
195 	/*
196 	 * Clear out the old buffers
197 	 */
198 	fp->f_ind_cache_block = ~0;
199 	fp->f_buf_blkno = -1;
200 	return rc;
201 }
202 
203 /*
204  * Given an offset in a file, find the disk block number that
205  * contains that block.
206  */
207 static int
208 block_map(struct open_file *f, indp_t file_block, indp_t *disk_block_p)
209 {
210 	struct file *fp = (struct file *)f->f_fsdata;
211 	struct m_ext2fs *fs = fp->f_fs;
212 	uint level;
213 	indp_t ind_cache;
214 	indp_t ind_block_num;
215 	size_t rsize;
216 	int rc;
217 	indp_t *buf = (void *)fp->f_buf;
218 
219 	/*
220 	 * Index structure of an inode:
221 	 *
222 	 * e2di_blocks[0..NDADDR-1]
223 	 *			hold block numbers for blocks
224 	 *			0..NDADDR-1
225 	 *
226 	 * e2di_blocks[NDADDR+0]
227 	 *			block NDADDR+0 is the single indirect block
228 	 *			holds block numbers for blocks
229 	 *			NDADDR .. NDADDR + NINDIR(fs)-1
230 	 *
231 	 * e2di_blocks[NDADDR+1]
232 	 *			block NDADDR+1 is the double indirect block
233 	 *			holds block numbers for INDEX blocks for blocks
234 	 *			NDADDR + NINDIR(fs) ..
235 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
236 	 *
237 	 * e2di_blocks[NDADDR+2]
238 	 *			block NDADDR+2 is the triple indirect block
239 	 *			holds block numbers for	double-indirect
240 	 *			blocks for blocks
241 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
242 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2
243 	 *				+ NINDIR(fs)**3 - 1
244 	 */
245 
246 	if (file_block < NDADDR) {
247 		/* Direct block. */
248 		*disk_block_p = fs2h32(fp->f_di.e2di_blocks[file_block]);
249 		return 0;
250 	}
251 
252 	file_block -= NDADDR;
253 
254 	ind_cache = file_block >> LN2_IND_CACHE_SZ;
255 	if (ind_cache == fp->f_ind_cache_block) {
256 		*disk_block_p =
257 		    fs2h32(fp->f_ind_cache[file_block & IND_CACHE_MASK]);
258 		return 0;
259 	}
260 
261 	for (level = 0;;) {
262 		level += fp->f_nishift;
263 		if (file_block < (indp_t)1 << level)
264 			break;
265 		if (level > NIADDR * fp->f_nishift)
266 			/* Block number too high */
267 			return EFBIG;
268 		file_block -= (indp_t)1 << level;
269 	}
270 
271 	ind_block_num =
272 	    fs2h32(fp->f_di.e2di_blocks[NDADDR + (level / fp->f_nishift - 1)]);
273 
274 	for (;;) {
275 		level -= fp->f_nishift;
276 		if (ind_block_num == 0) {
277 			*disk_block_p = 0;	/* missing */
278 			return 0;
279 		}
280 
281 		twiddle();
282 		/*
283 		 * If we were feeling brave, we could work out the number
284 		 * of the disk sector and read a single disk sector instead
285 		 * of a filesystem block.
286 		 * However we don't do this very often anyway...
287 		 */
288 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
289 			FSBTODB(fp->f_fs, ind_block_num), fs->e2fs_bsize,
290 			buf, &rsize);
291 		if (rc)
292 			return rc;
293 		if (rsize != fs->e2fs_bsize)
294 			return EIO;
295 		ind_block_num = fs2h32(buf[file_block >> level]);
296 		if (level == 0)
297 			break;
298 		file_block &= (1 << level) - 1;
299 	}
300 
301 	/* Save the part of the block that contains this sector */
302 	memcpy(fp->f_ind_cache, &buf[file_block & ~IND_CACHE_MASK],
303 	    IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
304 	fp->f_ind_cache_block = ind_cache;
305 
306 	*disk_block_p = ind_block_num;
307 
308 	return 0;
309 }
310 
311 /*
312  * Read a portion of a file into an internal buffer.
313  * Return the location in the buffer and the amount in the buffer.
314  */
315 static int
316 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
317 {
318 	struct file *fp = (struct file *)f->f_fsdata;
319 	struct m_ext2fs *fs = fp->f_fs;
320 	long off;
321 	indp_t file_block;
322 	indp_t disk_block;
323 	size_t block_size;
324 	int rc;
325 
326 	off = blkoff(fs, fp->f_seekp);
327 	file_block = lblkno(fs, fp->f_seekp);
328 	block_size = fs->e2fs_bsize;	/* no fragment */
329 
330 	if (file_block != fp->f_buf_blkno) {
331 		rc = block_map(f, file_block, &disk_block);
332 		if (rc)
333 			return rc;
334 
335 		if (disk_block == 0) {
336 			memset(fp->f_buf, 0, block_size);
337 			fp->f_buf_size = block_size;
338 		} else {
339 			twiddle();
340 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
341 				FSBTODB(fs, disk_block),
342 				block_size, fp->f_buf, &fp->f_buf_size);
343 			if (rc)
344 				return rc;
345 		}
346 
347 		fp->f_buf_blkno = file_block;
348 	}
349 
350 	/*
351 	 * Return address of byte in buffer corresponding to
352 	 * offset, and size of remainder of buffer after that
353 	 * byte.
354 	 */
355 	*buf_p = fp->f_buf + off;
356 	*size_p = block_size - off;
357 
358 	/*
359 	 * But truncate buffer at end of file.
360 	 */
361 	/* XXX should handle LARGEFILE */
362 	if (*size_p > fp->f_di.e2di_size - fp->f_seekp)
363 		*size_p = fp->f_di.e2di_size - fp->f_seekp;
364 
365 	return 0;
366 }
367 
368 /*
369  * Search a directory for a name and return its
370  * inode number.
371  */
372 static int
373 search_directory(const char *name, int length, struct open_file *f,
374 	ino32_t *inumber_p)
375 {
376 	struct file *fp = (struct file *)f->f_fsdata;
377 	struct ext2fs_direct *dp;
378 	struct ext2fs_direct *edp;
379 	char *buf;
380 	size_t buf_size;
381 	int namlen;
382 	int rc;
383 
384 	fp->f_seekp = 0;
385 	/* XXX should handle LARGEFILE */
386 	while (fp->f_seekp < (off_t)fp->f_di.e2di_size) {
387 		rc = buf_read_file(f, &buf, &buf_size);
388 		if (rc)
389 			return rc;
390 
391 		dp = (struct ext2fs_direct *)buf;
392 		edp = (struct ext2fs_direct *)(buf + buf_size);
393 		for (; dp < edp;
394 		    dp = (void *)((char *)dp + fs2h16(dp->e2d_reclen))) {
395 			if (fs2h16(dp->e2d_reclen) <= 0)
396 				break;
397 			if (fs2h32(dp->e2d_ino) == (ino32_t)0)
398 				continue;
399 			namlen = dp->e2d_namlen;
400 			if (namlen == length &&
401 			    !memcmp(name, dp->e2d_name, length)) {
402 				/* found entry */
403 				*inumber_p = fs2h32(dp->e2d_ino);
404 				return 0;
405 			}
406 		}
407 		fp->f_seekp += buf_size;
408 	}
409 	return ENOENT;
410 }
411 
412 int
413 read_sblock(struct open_file *f, struct m_ext2fs *fs)
414 {
415 	static uint8_t sbbuf[SBSIZE];
416 	struct ext2fs ext2fs;
417 	size_t buf_size;
418 	int rc;
419 
420 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
421 	    SBOFF / DEV_BSIZE, SBSIZE, sbbuf, &buf_size);
422 	if (rc)
423 		return rc;
424 
425 	if (buf_size != SBSIZE)
426 		return EIO;
427 
428 	e2fs_sbload((void *)sbbuf, &ext2fs);
429 	if (ext2fs.e2fs_magic != E2FS_MAGIC)
430 		return EINVAL;
431 	if (ext2fs.e2fs_rev > E2FS_REV1 ||
432 	    (ext2fs.e2fs_rev == E2FS_REV1 &&
433 	     (ext2fs.e2fs_first_ino != EXT2_FIRSTINO ||
434 	      ext2fs.e2fs_inode_size != EXT2_DINODE_SIZE ||
435 	      ext2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP))) {
436 		return ENODEV;
437 	}
438 
439 	e2fs_sbload((void *)sbbuf, &fs->e2fs);
440 	/* compute in-memory m_ext2fs values */
441 	fs->e2fs_ncg =
442 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
443 	    fs->e2fs.e2fs_bpg);
444 	/* XXX assume hw bsize = 512 */
445 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
446 	fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
447 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
448 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
449 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
450 	fs->e2fs_ngdb =
451 	    howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
452 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
453 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
454 
455 	return 0;
456 }
457 
458 int
459 read_gdblock(struct open_file *f, struct m_ext2fs *fs)
460 {
461 	struct file *fp = (struct file *)f->f_fsdata;
462 	size_t rsize;
463 	uint gdpb;
464 	int i, rc;
465 
466 	gdpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
467 
468 	for (i = 0; i < fs->e2fs_ngdb; i++) {
469 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
470 		    FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
471 		    1 /* superblock */ + i),
472 		    fs->e2fs_bsize, fp->f_buf, &rsize);
473 		if (rc)
474 			return rc;
475 		if (rsize != fs->e2fs_bsize)
476 			return EIO;
477 
478 		e2fs_cgload((struct ext2_gd *)fp->f_buf,
479 		    &fs->e2fs_gd[i * gdpb],
480 		    (i == (fs->e2fs_ngdb - 1)) ?
481 		    (fs->e2fs_ncg - gdpb * i) * sizeof(struct ext2_gd):
482 		    fs->e2fs_bsize);
483 	}
484 
485 	return 0;
486 }
487 
488 
489 /*
490  * Open a file.
491  */
492 int
493 ext2fs_open(const char *path, struct open_file *f)
494 {
495 #ifndef LIBSA_FS_SINGLECOMPONENT
496 	const char *cp, *ncp;
497 	int c;
498 #endif
499 	ino32_t inumber;
500 	struct file *fp;
501 	struct m_ext2fs *fs;
502 	int rc;
503 #ifndef LIBSA_NO_FS_SYMLINK
504 	ino32_t parent_inumber;
505 	int nlinks = 0;
506 	char namebuf[MAXPATHLEN+1];
507 	char *buf;
508 #endif
509 
510 	/* allocate file system specific data structure */
511 	fp = alloc(sizeof(struct file));
512 	memset(fp, 0, sizeof(struct file));
513 	f->f_fsdata = (void *)fp;
514 
515 	/* allocate space and read super block */
516 	fs = alloc(sizeof(*fs));
517 	fp->f_fs = fs;
518 	twiddle();
519 
520 	rc = read_sblock(f, fs);
521 	if (rc)
522 		goto out;
523 
524 #ifdef EXT2FS_DEBUG
525 	dump_sblock(fs);
526 #endif
527 
528 	/* alloc a block sized buffer used for all fs transfers */
529 	fp->f_buf = alloc(fs->e2fs_bsize);
530 
531 	/* read group descriptor blocks */
532 	fs->e2fs_gd = alloc(sizeof(struct ext2_gd) * fs->e2fs_ncg);
533 	rc = read_gdblock(f, fs);
534 	if (rc)
535 		goto out;
536 
537 	/*
538 	 * Calculate indirect block levels.
539 	 */
540 	{
541 		indp_t mult;
542 		int ln2;
543 
544 		/*
545 		 * We note that the number of indirect blocks is always
546 		 * a power of 2.  This lets us use shifts and masks instead
547 		 * of divide and remainder and avoinds pulling in the
548 		 * 64bit division routine into the boot code.
549 		 */
550 		mult = NINDIR(fs);
551 #ifdef DEBUG
552 		if (!powerof2(mult)) {
553 			/* Hummm was't a power of 2 */
554 			rc = EINVAL;
555 			goto out;
556 		}
557 #endif
558 		for (ln2 = 0; mult != 1; ln2++)
559 			mult >>= 1;
560 
561 		fp->f_nishift = ln2;
562 	}
563 
564 	inumber = EXT2_ROOTINO;
565 	if ((rc = read_inode(inumber, f)) != 0)
566 		goto out;
567 
568 #ifndef LIBSA_FS_SINGLECOMPONENT
569 	cp = path;
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 ((fp->f_di.e2di_mode & EXT2_IFMT) != EXT2_IFDIR) {
584 			rc = ENOTDIR;
585 			goto out;
586 		}
587 
588 		/*
589 		 * Get next component of path name.
590 		 */
591 		ncp = cp;
592 		while ((c = *cp) != '\0' && c != '/')
593 			cp++;
594 
595 		/*
596 		 * Look up component in current directory.
597 		 * Save directory inumber in case we find a
598 		 * symbolic link.
599 		 */
600 #ifndef LIBSA_NO_FS_SYMLINK
601 		parent_inumber = inumber;
602 #endif
603 		rc = search_directory(ncp, cp - ncp, f, &inumber);
604 		if (rc)
605 			goto out;
606 
607 		/*
608 		 * Open next component.
609 		 */
610 		if ((rc = read_inode(inumber, f)) != 0)
611 			goto out;
612 
613 #ifndef LIBSA_NO_FS_SYMLINK
614 		/*
615 		 * Check for symbolic link.
616 		 */
617 		if ((fp->f_di.e2di_mode & EXT2_IFMT) == EXT2_IFLNK) {
618 			/* XXX should handle LARGEFILE */
619 			int link_len = fp->f_di.e2di_size;
620 			int len;
621 
622 			len = strlen(cp);
623 
624 			if (link_len + len > MAXPATHLEN ||
625 			    ++nlinks > MAXSYMLINKS) {
626 				rc = ENOENT;
627 				goto out;
628 			}
629 
630 			memmove(&namebuf[link_len], cp, len + 1);
631 
632 			if (link_len < EXT2_MAXSYMLINKLEN) {
633 				memcpy(namebuf, fp->f_di.e2di_blocks, link_len);
634 			} else {
635 				/*
636 				 * Read file for symbolic link
637 				 */
638 				size_t buf_size;
639 				indp_t	disk_block;
640 
641 				buf = fp->f_buf;
642 				rc = block_map(f, (indp_t)0, &disk_block);
643 				if (rc)
644 					goto out;
645 
646 				twiddle();
647 				rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
648 					F_READ, FSBTODB(fs, disk_block),
649 					fs->e2fs_bsize, buf, &buf_size);
650 				if (rc)
651 					goto out;
652 
653 				memcpy(namebuf, buf, link_len);
654 			}
655 
656 			/*
657 			 * If relative pathname, restart at parent directory.
658 			 * If absolute pathname, restart at root.
659 			 */
660 			cp = namebuf;
661 			if (*cp != '/')
662 				inumber = parent_inumber;
663 			else
664 				inumber = (ino32_t)EXT2_ROOTINO;
665 
666 			if ((rc = read_inode(inumber, f)) != 0)
667 				goto out;
668 		}
669 #endif	/* !LIBSA_NO_FS_SYMLINK */
670 	}
671 
672 	/*
673 	 * Found terminal component.
674 	 */
675 	rc = 0;
676 
677 #else /* !LIBSA_FS_SINGLECOMPONENT */
678 
679 	/* look up component in the current (root) directory */
680 	rc = search_directory(path, strlen(path), f, &inumber);
681 	if (rc)
682 		goto out;
683 
684 	/* open it */
685 	rc = read_inode(inumber, f);
686 
687 #endif /* !LIBSA_FS_SINGLECOMPONENT */
688 
689 	fp->f_seekp = 0;		/* reset seek pointer */
690 
691 out:
692 	if (rc)
693 		ext2fs_close(f);
694 	else {
695 		fsmod = "ext2fs";
696 		fsmod2 = "ffs";
697 	}
698 	return rc;
699 }
700 
701 int
702 ext2fs_close(struct open_file *f)
703 {
704 	struct file *fp = (struct file *)f->f_fsdata;
705 
706 	f->f_fsdata = NULL;
707 	if (fp == NULL)
708 		return 0;
709 
710 	if (fp->f_fs->e2fs_gd)
711 		dealloc(fp->f_fs->e2fs_gd,
712 		    sizeof(struct ext2_gd) * fp->f_fs->e2fs_ncg);
713 	if (fp->f_buf)
714 		dealloc(fp->f_buf, fp->f_fs->e2fs_bsize);
715 	dealloc(fp->f_fs, sizeof(*fp->f_fs));
716 	dealloc(fp, sizeof(struct file));
717 	return 0;
718 }
719 
720 /*
721  * Copy a portion of a file into kernel memory.
722  * Cross block boundaries when necessary.
723  */
724 int
725 ext2fs_read(struct open_file *f, void *start, size_t size, size_t *resid)
726 {
727 	struct file *fp = (struct file *)f->f_fsdata;
728 	size_t csize;
729 	char *buf;
730 	size_t buf_size;
731 	int rc = 0;
732 	char *addr = start;
733 
734 	while (size != 0) {
735 		/* XXX should handle LARGEFILE */
736 		if (fp->f_seekp >= (off_t)fp->f_di.e2di_size)
737 			break;
738 
739 		rc = buf_read_file(f, &buf, &buf_size);
740 		if (rc)
741 			break;
742 
743 		csize = size;
744 		if (csize > buf_size)
745 			csize = buf_size;
746 
747 		memcpy(addr, buf, csize);
748 
749 		fp->f_seekp += csize;
750 		addr += csize;
751 		size -= csize;
752 	}
753 	if (resid)
754 		*resid = size;
755 	return rc;
756 }
757 
758 /*
759  * Not implemented.
760  */
761 #ifndef LIBSA_NO_FS_WRITE
762 int
763 ext2fs_write(struct open_file *f, void *start, size_t size, size_t *resid)
764 {
765 
766 	return EROFS;
767 }
768 #endif /* !LIBSA_NO_FS_WRITE */
769 
770 #ifndef LIBSA_NO_FS_SEEK
771 off_t
772 ext2fs_seek(struct open_file *f, off_t offset, int where)
773 {
774 	struct file *fp = (struct file *)f->f_fsdata;
775 
776 	switch (where) {
777 	case SEEK_SET:
778 		fp->f_seekp = offset;
779 		break;
780 	case SEEK_CUR:
781 		fp->f_seekp += offset;
782 		break;
783 	case SEEK_END:
784 		/* XXX should handle LARGEFILE */
785 		fp->f_seekp = fp->f_di.e2di_size - offset;
786 		break;
787 	default:
788 		return -1;
789 	}
790 	return fp->f_seekp;
791 }
792 #endif /* !LIBSA_NO_FS_SEEK */
793 
794 int
795 ext2fs_stat(struct open_file *f, struct stat *sb)
796 {
797 	struct file *fp = (struct file *)f->f_fsdata;
798 
799 	/* only important stuff */
800 	memset(sb, 0, sizeof *sb);
801 	sb->st_mode = fp->f_di.e2di_mode;
802 	sb->st_uid = fp->f_di.e2di_uid;
803 	sb->st_gid = fp->f_di.e2di_gid;
804 	/* XXX should handle LARGEFILE */
805 	sb->st_size = fp->f_di.e2di_size;
806 	return 0;
807 }
808 
809 /*
810  * byte swap functions for big endian machines
811  * (ext2fs is always little endian)
812  *
813  * XXX: We should use src/sys/ufs/ext2fs/ext2fs_bswap.c
814  */
815 
816 /* These functions are only needed if native byte order is not big endian */
817 #if BYTE_ORDER == BIG_ENDIAN
818 void
819 e2fs_sb_bswap(struct ext2fs *old, struct ext2fs *new)
820 {
821 
822 	/* preserve unused fields */
823 	memcpy(new, old, sizeof(struct ext2fs));
824 	new->e2fs_icount	=	bswap32(old->e2fs_icount);
825 	new->e2fs_bcount	=	bswap32(old->e2fs_bcount);
826 	new->e2fs_rbcount	=	bswap32(old->e2fs_rbcount);
827 	new->e2fs_fbcount	=	bswap32(old->e2fs_fbcount);
828 	new->e2fs_ficount	=	bswap32(old->e2fs_ficount);
829 	new->e2fs_first_dblock	=	bswap32(old->e2fs_first_dblock);
830 	new->e2fs_log_bsize	=	bswap32(old->e2fs_log_bsize);
831 	new->e2fs_fsize		=	bswap32(old->e2fs_fsize);
832 	new->e2fs_bpg		=	bswap32(old->e2fs_bpg);
833 	new->e2fs_fpg		=	bswap32(old->e2fs_fpg);
834 	new->e2fs_ipg		=	bswap32(old->e2fs_ipg);
835 	new->e2fs_mtime		=	bswap32(old->e2fs_mtime);
836 	new->e2fs_wtime		=	bswap32(old->e2fs_wtime);
837 	new->e2fs_mnt_count	=	bswap16(old->e2fs_mnt_count);
838 	new->e2fs_max_mnt_count	=	bswap16(old->e2fs_max_mnt_count);
839 	new->e2fs_magic		=	bswap16(old->e2fs_magic);
840 	new->e2fs_state		=	bswap16(old->e2fs_state);
841 	new->e2fs_beh		=	bswap16(old->e2fs_beh);
842 	new->e2fs_minrev	=	bswap16(old->e2fs_minrev);
843 	new->e2fs_lastfsck	=	bswap32(old->e2fs_lastfsck);
844 	new->e2fs_fsckintv	=	bswap32(old->e2fs_fsckintv);
845 	new->e2fs_creator	=	bswap32(old->e2fs_creator);
846 	new->e2fs_rev		=	bswap32(old->e2fs_rev);
847 	new->e2fs_ruid		=	bswap16(old->e2fs_ruid);
848 	new->e2fs_rgid		=	bswap16(old->e2fs_rgid);
849 	new->e2fs_first_ino	=	bswap32(old->e2fs_first_ino);
850 	new->e2fs_inode_size	=	bswap16(old->e2fs_inode_size);
851 	new->e2fs_block_group_nr =	bswap16(old->e2fs_block_group_nr);
852 	new->e2fs_features_compat =	bswap32(old->e2fs_features_compat);
853 	new->e2fs_features_incompat =	bswap32(old->e2fs_features_incompat);
854 	new->e2fs_features_rocompat =	bswap32(old->e2fs_features_rocompat);
855 	new->e2fs_algo		=	bswap32(old->e2fs_algo);
856 	new->e2fs_reserved_ngdb	=	bswap16(old->e2fs_reserved_ngdb);
857 }
858 
859 void e2fs_cg_bswap(struct ext2_gd *old, struct ext2_gd *new, int size)
860 {
861 	int i;
862 
863 	for (i = 0; i < (size / sizeof(struct ext2_gd)); i++) {
864 		new[i].ext2bgd_b_bitmap	= bswap32(old[i].ext2bgd_b_bitmap);
865 		new[i].ext2bgd_i_bitmap	= bswap32(old[i].ext2bgd_i_bitmap);
866 		new[i].ext2bgd_i_tables	= bswap32(old[i].ext2bgd_i_tables);
867 		new[i].ext2bgd_nbfree	= bswap16(old[i].ext2bgd_nbfree);
868 		new[i].ext2bgd_nifree	= bswap16(old[i].ext2bgd_nifree);
869 		new[i].ext2bgd_ndirs	= bswap16(old[i].ext2bgd_ndirs);
870 	}
871 }
872 
873 void e2fs_i_bswap(struct ext2fs_dinode *old, struct ext2fs_dinode *new)
874 {
875 
876 	new->e2di_mode		=	bswap16(old->e2di_mode);
877 	new->e2di_uid		=	bswap16(old->e2di_uid);
878 	new->e2di_gid		=	bswap16(old->e2di_gid);
879 	new->e2di_nlink		=	bswap16(old->e2di_nlink);
880 	new->e2di_size		=	bswap32(old->e2di_size);
881 	new->e2di_atime		=	bswap32(old->e2di_atime);
882 	new->e2di_ctime		=	bswap32(old->e2di_ctime);
883 	new->e2di_mtime		=	bswap32(old->e2di_mtime);
884 	new->e2di_dtime		=	bswap32(old->e2di_dtime);
885 	new->e2di_nblock	=	bswap32(old->e2di_nblock);
886 	new->e2di_flags		=	bswap32(old->e2di_flags);
887 	new->e2di_gen		=	bswap32(old->e2di_gen);
888 	new->e2di_facl		=	bswap32(old->e2di_facl);
889 	new->e2di_dacl		=	bswap32(old->e2di_dacl);
890 	new->e2di_faddr		=	bswap32(old->e2di_faddr);
891 	memcpy(&new->e2di_blocks[0], &old->e2di_blocks[0],
892 	    (NDADDR + NIADDR) * sizeof(uint32_t));
893 }
894 #endif
895 
896 #ifdef EXT2FS_DEBUG
897 void
898 dump_sblock(struct m_ext2fs *fs)
899 {
900 
901 	printf("fs->e2fs.e2fs_bcount = %u\n", fs->e2fs.e2fs_bcount);
902 	printf("fs->e2fs.e2fs_first_dblock = %u\n", fs->e2fs.e2fs_first_dblock);
903 	printf("fs->e2fs.e2fs_log_bsize = %u\n", fs->e2fs.e2fs_log_bsize);
904 	printf("fs->e2fs.e2fs_bpg = %u\n", fs->e2fs.e2fs_bpg);
905 	printf("fs->e2fs.e2fs_ipg = %u\n", fs->e2fs.e2fs_ipg);
906 	printf("fs->e2fs.e2fs_magic = 0x%x\n", fs->e2fs.e2fs_magic);
907 	printf("fs->e2fs.e2fs_rev = %u\n", fs->e2fs.e2fs_rev);
908 
909 	if (fs->e2fs.e2fs_rev == E2FS_REV1) {
910 		printf("fs->e2fs.e2fs_first_ino = %u\n",
911 		    fs->e2fs.e2fs_first_ino);
912 		printf("fs->e2fs.e2fs_inode_size = %u\n",
913 		    fs->e2fs.e2fs_inode_size);
914 		printf("fs->e2fs.e2fs_features_compat = %u\n",
915 		    fs->e2fs.e2fs_features_compat);
916 		printf("fs->e2fs.e2fs_features_incompat = %u\n",
917 		    fs->e2fs.e2fs_features_incompat);
918 		printf("fs->e2fs.e2fs_features_rocompat = %u\n",
919 		    fs->e2fs.e2fs_features_rocompat);
920 		printf("fs->e2fs.e2fs_reserved_ngdb = %u\n",
921 		    fs->e2fs.e2fs_reserved_ngdb);
922 	}
923 
924 	printf("fs->e2fs_bsize = %u\n", fs->e2fs_bsize);
925 	printf("fs->e2fs_fsbtodb = %u\n", fs->e2fs_fsbtodb);
926 	printf("fs->e2fs_ncg = %u\n", fs->e2fs_ncg);
927 	printf("fs->e2fs_ngdb = %u\n", fs->e2fs_ngdb);
928 	printf("fs->e2fs_ipb = %u\n", fs->e2fs_ipb);
929 	printf("fs->e2fs_itpg = %u\n", fs->e2fs_itpg);
930 }
931 #endif
932