Home | History | Annotate | Line # | Download | only in libsa
ext2fs.c revision 1.1
      1 /*	$NetBSD: ext2fs.c,v 1.1 2007/12/01 18:06:22 tsutsui 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 (at) 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 	 * di_db[0..NDADDR-1]	hold block numbers for blocks
    223 	 *			0..NDADDR-1
    224 	 *
    225 	 * di_ib[0]		index block 0 is the single indirect block
    226 	 *			holds block numbers for blocks
    227 	 *			NDADDR .. NDADDR + NINDIR(fs)-1
    228 	 *
    229 	 * di_ib[1]		index block 1 is the double indirect block
    230 	 *			holds block numbers for INDEX blocks for blocks
    231 	 *			NDADDR + NINDIR(fs) ..
    232 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
    233 	 *
    234 	 * di_ib[2]		index block 2 is the triple indirect block
    235 	 *			holds block numbers for double-indirect
    236 	 *			blocks for blocks
    237 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
    238 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2
    239 	 *				+ NINDIR(fs)**3 - 1
    240 	 */
    241 
    242 	if (file_block < NDADDR) {
    243 		/* Direct block. */
    244 		*disk_block_p = fs2h32(fp->f_di.e2di_blocks[file_block]);
    245 		return 0;
    246 	}
    247 
    248 	file_block -= NDADDR;
    249 
    250 	ind_cache = file_block >> LN2_IND_CACHE_SZ;
    251 	if (ind_cache == fp->f_ind_cache_block) {
    252 		*disk_block_p = fp->f_ind_cache[file_block & IND_CACHE_MASK];
    253 		return 0;
    254 	}
    255 
    256 	for (level = 0;;) {
    257 		level += fp->f_nishift;
    258 		if (file_block < (indp_t)1 << level)
    259 			break;
    260 		if (level > NIADDR * fp->f_nishift)
    261 			/* Block number too high */
    262 			return EFBIG;
    263 		file_block -= (indp_t)1 << level;
    264 	}
    265 
    266 	ind_block_num =
    267 	    fs2h32(fp->f_di.e2di_blocks[NDADDR + (level / fp->f_nishift - 1)]);
    268 
    269 	for (;;) {
    270 		level -= fp->f_nishift;
    271 		if (ind_block_num == 0) {
    272 			*disk_block_p = 0;	/* missing */
    273 			return 0;
    274 		}
    275 
    276 		twiddle();
    277 		/*
    278 		 * If we were feeling brave, we could work out the number
    279 		 * of the disk sector and read a single disk sector instead
    280 		 * of a filesystem block.
    281 		 * However we don't do this very often anyway...
    282 		 */
    283 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    284 			FSBTODB(fp->f_fs, ind_block_num), fs->e2fs_bsize,
    285 			buf, &rsize);
    286 		if (rc)
    287 			return rc;
    288 		if (rsize != fs->e2fs_bsize)
    289 			return EIO;
    290 		ind_block_num = buf[file_block >> level];
    291 		if (level == 0)
    292 			break;
    293 		file_block &= (1 << level) - 1;
    294 	}
    295 
    296 	/* Save the part of the block that contains this sector */
    297 	memcpy(fp->f_ind_cache, &buf[file_block & ~IND_CACHE_MASK],
    298 	    IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
    299 	fp->f_ind_cache_block = ind_cache;
    300 
    301 	*disk_block_p = ind_block_num;
    302 
    303 	return 0;
    304 }
    305 
    306 /*
    307  * Read a portion of a file into an internal buffer.
    308  * Return the location in the buffer and the amount in the buffer.
    309  */
    310 static int
    311 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
    312 {
    313 	struct file *fp = (struct file *)f->f_fsdata;
    314 	struct m_ext2fs *fs = fp->f_fs;
    315 	long off;
    316 	indp_t file_block;
    317 	indp_t disk_block;
    318 	size_t block_size;
    319 	int rc;
    320 
    321 	off = blkoff(fs, fp->f_seekp);
    322 	file_block = lblkno(fs, fp->f_seekp);
    323 	block_size = fs->e2fs_bsize;	/* no fragment */
    324 
    325 	if (file_block != fp->f_buf_blkno) {
    326 		rc = block_map(f, file_block, &disk_block);
    327 		if (rc)
    328 			return rc;
    329 
    330 		if (disk_block == 0) {
    331 			memset(fp->f_buf, 0, block_size);
    332 			fp->f_buf_size = block_size;
    333 		} else {
    334 			twiddle();
    335 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    336 				FSBTODB(fs, disk_block),
    337 				block_size, fp->f_buf, &fp->f_buf_size);
    338 			if (rc)
    339 				return rc;
    340 		}
    341 
    342 		fp->f_buf_blkno = file_block;
    343 	}
    344 
    345 	/*
    346 	 * Return address of byte in buffer corresponding to
    347 	 * offset, and size of remainder of buffer after that
    348 	 * byte.
    349 	 */
    350 	*buf_p = fp->f_buf + off;
    351 	*size_p = block_size - off;
    352 
    353 	/*
    354 	 * But truncate buffer at end of file.
    355 	 */
    356 	/* XXX should handle LARGEFILE */
    357 	if (*size_p > fp->f_di.e2di_size - fp->f_seekp)
    358 		*size_p = fp->f_di.e2di_size - fp->f_seekp;
    359 
    360 	return 0;
    361 }
    362 
    363 /*
    364  * Search a directory for a name and return its
    365  * inode number.
    366  */
    367 static int
    368 search_directory(const char *name, int length, struct open_file *f,
    369 	ino32_t *inumber_p)
    370 {
    371 	struct file *fp = (struct file *)f->f_fsdata;
    372 	struct ext2fs_direct *dp;
    373 	struct ext2fs_direct *edp;
    374 	char *buf;
    375 	size_t buf_size;
    376 	int namlen;
    377 	int rc;
    378 
    379 	fp->f_seekp = 0;
    380 	/* XXX should handle LARGEFILE */
    381 	while (fp->f_seekp < (off_t)fp->f_di.e2di_size) {
    382 		rc = buf_read_file(f, &buf, &buf_size);
    383 		if (rc)
    384 			return rc;
    385 
    386 		dp = (struct ext2fs_direct *)buf;
    387 		edp = (struct ext2fs_direct *)(buf + buf_size);
    388 		for (; dp < edp;
    389 		    dp = (void *)((char *)dp + fs2h16(dp->e2d_reclen))) {
    390 			if (fs2h16(dp->e2d_reclen) <= 0)
    391 				break;
    392 			if (fs2h32(dp->e2d_ino) == (ino32_t)0)
    393 				continue;
    394 			namlen = dp->e2d_namlen;
    395 			if (namlen == length &&
    396 			    !memcmp(name, dp->e2d_name, length)) {
    397 				/* found entry */
    398 				*inumber_p = fs2h32(dp->e2d_ino);
    399 				return 0;
    400 			}
    401 		}
    402 		fp->f_seekp += buf_size;
    403 	}
    404 	return ENOENT;
    405 }
    406 
    407 int
    408 read_sblock(struct open_file *f, struct m_ext2fs *fs)
    409 {
    410 	static uint8_t sbbuf[SBSIZE];
    411 	struct ext2fs ext2fs;
    412 	size_t buf_size;
    413 	int rc;
    414 
    415 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    416 	    SBOFF / DEV_BSIZE, SBSIZE, sbbuf, &buf_size);
    417 	if (rc)
    418 		return rc;
    419 
    420 	if (buf_size != SBSIZE)
    421 		return EIO;
    422 
    423 	e2fs_sbload((void *)sbbuf, &ext2fs);
    424 	if (ext2fs.e2fs_magic != E2FS_MAGIC)
    425 		return EINVAL;
    426 	if (ext2fs.e2fs_rev > E2FS_REV1 ||
    427 	    (ext2fs.e2fs_rev == E2FS_REV1 &&
    428 	     (ext2fs.e2fs_first_ino != EXT2_FIRSTINO ||
    429 	      ext2fs.e2fs_inode_size != EXT2_DINODE_SIZE ||
    430 	      ext2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP))) {
    431 		return ENODEV;
    432 	}
    433 
    434 	e2fs_sbload((void *)sbbuf, &fs->e2fs);
    435 	/* compute in-memory m_ext2fs values */
    436 	fs->e2fs_ncg =
    437 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    438 	    fs->e2fs.e2fs_bpg);
    439 	/* XXX assume hw bsize = 512 */
    440 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
    441 	fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
    442 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    443 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    444 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    445 	fs->e2fs_ngdb =
    446 	    howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
    447 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
    448 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
    449 
    450 	return 0;
    451 }
    452 
    453 int
    454 read_gdblock(struct open_file *f, struct m_ext2fs *fs)
    455 {
    456 	struct file *fp = (struct file *)f->f_fsdata;
    457 	size_t rsize;
    458 	uint gdpb;
    459 	int i, rc;
    460 
    461 	gdpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
    462 
    463 	for (i = 0; i < fs->e2fs_ngdb; i++) {
    464 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    465 		    FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
    466 		    1 /* superblock */ + i),
    467 		    fs->e2fs_bsize, fp->f_buf, &rsize);
    468 		if (rc)
    469 			return rc;
    470 		if (rsize != fs->e2fs_bsize)
    471 			return EIO;
    472 
    473 		e2fs_cgload((struct ext2_gd *)fp->f_buf,
    474 		    &fs->e2fs_gd[i * gdpb],
    475 		    (i == (fs->e2fs_ngdb - 1)) ?
    476 		    (fs->e2fs_ncg - gdpb * i) * sizeof(struct ext2_gd):
    477 		    fs->e2fs_bsize);
    478 	}
    479 
    480 	return 0;
    481 }
    482 
    483 
    484 /*
    485  * Open a file.
    486  */
    487 int
    488 ext2fs_open(const char *path, struct open_file *f)
    489 {
    490 #ifndef LIBSA_FS_SINGLECOMPONENT
    491 	const char *cp, *ncp;
    492 	int c;
    493 #endif
    494 	ino32_t inumber;
    495 	struct file *fp;
    496 	struct m_ext2fs *fs;
    497 	int rc;
    498 #ifndef LIBSA_NO_FS_SYMLINK
    499 	ino32_t parent_inumber;
    500 	int nlinks = 0;
    501 	char namebuf[MAXPATHLEN+1];
    502 	char *buf;
    503 #endif
    504 
    505 	/* allocate file system specific data structure */
    506 	fp = alloc(sizeof(struct file));
    507 	memset(fp, 0, sizeof(struct file));
    508 	f->f_fsdata = (void *)fp;
    509 
    510 	/* allocate space and read super block */
    511 	fs = alloc(SBSIZE);
    512 	fp->f_fs = fs;
    513 	twiddle();
    514 
    515 	rc = read_sblock(f, fs);
    516 	if (rc)
    517 		goto out;
    518 
    519 #ifdef EXT2FS_DEBUG
    520 	dump_sblock(fs);
    521 #endif
    522 
    523 	/* alloc a block sized buffer used for all fs transfers */
    524 	fp->f_buf = alloc(fs->e2fs_bsize);
    525 
    526 	/* read group descriptor blocks */
    527 	fs->e2fs_gd = alloc(sizeof(struct ext2_gd) * fs->e2fs_ncg);
    528 	rc = read_gdblock(f, fs);
    529 	if (rc)
    530 		goto out;
    531 
    532 	/*
    533 	 * Calculate indirect block levels.
    534 	 */
    535 	{
    536 		indp_t mult;
    537 		int ln2;
    538 
    539 		/*
    540 		 * We note that the number of indirect blocks is always
    541 		 * a power of 2.  This lets us use shifts and masks instead
    542 		 * of divide and remainder and avoinds pulling in the
    543 		 * 64bit division routine into the boot code.
    544 		 */
    545 		mult = NINDIR(fs);
    546 #ifdef DEBUG
    547 		if (!powerof2(mult)) {
    548 			/* Hummm was't a power of 2 */
    549 			rc = EINVAL;
    550 			goto out;
    551 		}
    552 #endif
    553 		for (ln2 = 0; mult != 1; ln2++)
    554 			mult >>= 1;
    555 
    556 		fp->f_nishift = ln2;
    557 	}
    558 
    559 	inumber = EXT2_ROOTINO;
    560 	if ((rc = read_inode(inumber, f)) != 0)
    561 		goto out;
    562 
    563 #ifndef LIBSA_FS_SINGLECOMPONENT
    564 	cp = path;
    565 	while (*cp) {
    566 
    567 		/*
    568 		 * Remove extra separators
    569 		 */
    570 		while (*cp == '/')
    571 			cp++;
    572 		if (*cp == '\0')
    573 			break;
    574 
    575 		/*
    576 		 * Check that current node is a directory.
    577 		 */
    578 		if ((fp->f_di.e2di_mode & EXT2_IFMT) != EXT2_IFDIR) {
    579 			rc = ENOTDIR;
    580 			goto out;
    581 		}
    582 
    583 		/*
    584 		 * Get next component of path name.
    585 		 */
    586 		ncp = cp;
    587 		while ((c = *cp) != '\0' && c != '/')
    588 			cp++;
    589 
    590 		/*
    591 		 * Look up component in current directory.
    592 		 * Save directory inumber in case we find a
    593 		 * symbolic link.
    594 		 */
    595 #ifndef LIBSA_NO_FS_SYMLINK
    596 		parent_inumber = inumber;
    597 #endif
    598 		rc = search_directory(ncp, cp - ncp, f, &inumber);
    599 		if (rc)
    600 			goto out;
    601 
    602 		/*
    603 		 * Open next component.
    604 		 */
    605 		if ((rc = read_inode(inumber, f)) != 0)
    606 			goto out;
    607 
    608 #ifndef LIBSA_NO_FS_SYMLINK
    609 		/*
    610 		 * Check for symbolic link.
    611 		 */
    612 		if ((fp->f_di.e2di_mode & EXT2_IFMT) == EXT2_IFLNK) {
    613 			/* XXX should handle LARGEFILE */
    614 			int link_len = fp->f_di.e2di_size;
    615 			int len;
    616 
    617 			len = strlen(cp);
    618 
    619 			if (link_len + len > MAXPATHLEN ||
    620 			    ++nlinks > MAXSYMLINKS) {
    621 				rc = ENOENT;
    622 				goto out;
    623 			}
    624 
    625 			memmove(&namebuf[link_len], cp, len + 1);
    626 
    627 			if (link_len < EXT2_MAXSYMLINKLEN) {
    628 				memcpy(namebuf, fp->f_di.e2di_blocks, link_len);
    629 			} else {
    630 				/*
    631 				 * Read file for symbolic link
    632 				 */
    633 				size_t buf_size;
    634 				indp_t	disk_block;
    635 
    636 				buf = fp->f_buf;
    637 				rc = block_map(f, (indp_t)0, &disk_block);
    638 				if (rc)
    639 					goto out;
    640 
    641 				twiddle();
    642 				rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
    643 					F_READ, FSBTODB(fs, disk_block),
    644 					fs->e2fs_bsize, buf, &buf_size);
    645 				if (rc)
    646 					goto out;
    647 
    648 				memcpy(namebuf, buf, link_len);
    649 			}
    650 
    651 			/*
    652 			 * If relative pathname, restart at parent directory.
    653 			 * If absolute pathname, restart at root.
    654 			 */
    655 			cp = namebuf;
    656 			if (*cp != '/')
    657 				inumber = parent_inumber;
    658 			else
    659 				inumber = (ino32_t)EXT2_ROOTINO;
    660 
    661 			if ((rc = read_inode(inumber, f)) != 0)
    662 				goto out;
    663 		}
    664 #endif	/* !LIBSA_NO_FS_SYMLINK */
    665 	}
    666 
    667 	/*
    668 	 * Found terminal component.
    669 	 */
    670 	rc = 0;
    671 
    672 #else /* !LIBSA_FS_SINGLECOMPONENT */
    673 
    674 	/* look up component in the current (root) directory */
    675 	rc = search_directory(path, strlen(path), f, &inumber);
    676 	if (rc)
    677 		goto out;
    678 
    679 	/* open it */
    680 	rc = read_inode(inumber, f);
    681 
    682 #endif /* !LIBSA_FS_SINGLECOMPONENT */
    683 
    684 	fp->f_seekp = 0;		/* reset seek pointer */
    685 
    686 out:
    687 	if (rc)
    688 		ext2fs_close(f);
    689 	return rc;
    690 }
    691 
    692 int
    693 ext2fs_close(struct open_file *f)
    694 {
    695 	struct file *fp = (struct file *)f->f_fsdata;
    696 
    697 	f->f_fsdata = NULL;
    698 	if (fp == NULL)
    699 		return 0;
    700 
    701 	if (fp->f_buf)
    702 		dealloc(fp->f_buf, fp->f_fs->e2fs_bsize);
    703 	dealloc(fp->f_fs, SBSIZE);
    704 	dealloc(fp, sizeof(struct file));
    705 	return 0;
    706 }
    707 
    708 /*
    709  * Copy a portion of a file into kernel memory.
    710  * Cross block boundaries when necessary.
    711  */
    712 int
    713 ext2fs_read(struct open_file *f, void *start, size_t size, size_t *resid)
    714 {
    715 	struct file *fp = (struct file *)f->f_fsdata;
    716 	size_t csize;
    717 	char *buf;
    718 	size_t buf_size;
    719 	int rc = 0;
    720 	char *addr = start;
    721 
    722 	while (size != 0) {
    723 		/* XXX should handle LARGEFILE */
    724 		if (fp->f_seekp >= (off_t)fp->f_di.e2di_size)
    725 			break;
    726 
    727 		rc = buf_read_file(f, &buf, &buf_size);
    728 		if (rc)
    729 			break;
    730 
    731 		csize = size;
    732 		if (csize > buf_size)
    733 			csize = buf_size;
    734 
    735 		memcpy(addr, buf, csize);
    736 
    737 		fp->f_seekp += csize;
    738 		addr += csize;
    739 		size -= csize;
    740 	}
    741 	if (resid)
    742 		*resid = size;
    743 	return rc;
    744 }
    745 
    746 /*
    747  * Not implemented.
    748  */
    749 #ifndef LIBSA_NO_FS_WRITE
    750 int
    751 ext2fs_write(struct open_file *f, void *start, size_t size, size_t *resid)
    752 {
    753 
    754 	return EROFS;
    755 }
    756 #endif /* !LIBSA_NO_FS_WRITE */
    757 
    758 #ifndef LIBSA_NO_FS_SEEK
    759 off_t
    760 ext2fs_seek(struct open_file *f, off_t offset, int where)
    761 {
    762 	struct file *fp = (struct file *)f->f_fsdata;
    763 
    764 	switch (where) {
    765 	case SEEK_SET:
    766 		fp->f_seekp = offset;
    767 		break;
    768 	case SEEK_CUR:
    769 		fp->f_seekp += offset;
    770 		break;
    771 	case SEEK_END:
    772 		/* XXX should handle LARGEFILE */
    773 		fp->f_seekp = fp->f_di.e2di_size - offset;
    774 		break;
    775 	default:
    776 		return -1;
    777 	}
    778 	return fp->f_seekp;
    779 }
    780 #endif /* !LIBSA_NO_FS_SEEK */
    781 
    782 int
    783 ext2fs_stat(struct open_file *f, struct stat *sb)
    784 {
    785 	struct file *fp = (struct file *)f->f_fsdata;
    786 
    787 	/* only important stuff */
    788 	memset(sb, 0, sizeof *sb);
    789 	sb->st_mode = fp->f_di.e2di_mode;
    790 	sb->st_uid = fp->f_di.e2di_uid;
    791 	sb->st_gid = fp->f_di.e2di_gid;
    792 	/* XXX should handle LARGEFILE */
    793 	sb->st_size = fp->f_di.e2di_size;
    794 	return 0;
    795 }
    796 
    797 /*
    798  * byte swap functions for big endian machines
    799  * (ext2fs is always little endian)
    800  *
    801  * XXX: We should use src/sys/ufs/ext2fs/ext2fs_bswap.c
    802  */
    803 
    804 /* These functions are only needed if native byte order is not big endian */
    805 #if BYTE_ORDER == BIG_ENDIAN
    806 void
    807 e2fs_sb_bswap(struct ext2fs *old, struct ext2fs *new)
    808 {
    809 
    810 	/* preserve unused fields */
    811 	memcpy(new, old, sizeof(struct ext2fs));
    812 	new->e2fs_icount	=	bswap32(old->e2fs_icount);
    813 	new->e2fs_bcount	=	bswap32(old->e2fs_bcount);
    814 	new->e2fs_rbcount	=	bswap32(old->e2fs_rbcount);
    815 	new->e2fs_fbcount	=	bswap32(old->e2fs_fbcount);
    816 	new->e2fs_ficount	=	bswap32(old->e2fs_ficount);
    817 	new->e2fs_first_dblock	=	bswap32(old->e2fs_first_dblock);
    818 	new->e2fs_log_bsize	=	bswap32(old->e2fs_log_bsize);
    819 	new->e2fs_fsize		=	bswap32(old->e2fs_fsize);
    820 	new->e2fs_bpg		=	bswap32(old->e2fs_bpg);
    821 	new->e2fs_fpg		=	bswap32(old->e2fs_fpg);
    822 	new->e2fs_ipg		=	bswap32(old->e2fs_ipg);
    823 	new->e2fs_mtime		=	bswap32(old->e2fs_mtime);
    824 	new->e2fs_wtime		=	bswap32(old->e2fs_wtime);
    825 	new->e2fs_mnt_count	=	bswap16(old->e2fs_mnt_count);
    826 	new->e2fs_max_mnt_count	=	bswap16(old->e2fs_max_mnt_count);
    827 	new->e2fs_magic		=	bswap16(old->e2fs_magic);
    828 	new->e2fs_state		=	bswap16(old->e2fs_state);
    829 	new->e2fs_beh		=	bswap16(old->e2fs_beh);
    830 	new->e2fs_minrev	=	bswap16(old->e2fs_minrev);
    831 	new->e2fs_lastfsck	=	bswap32(old->e2fs_lastfsck);
    832 	new->e2fs_fsckintv	=	bswap32(old->e2fs_fsckintv);
    833 	new->e2fs_creator	=	bswap32(old->e2fs_creator);
    834 	new->e2fs_rev		=	bswap32(old->e2fs_rev);
    835 	new->e2fs_ruid		=	bswap16(old->e2fs_ruid);
    836 	new->e2fs_rgid		=	bswap16(old->e2fs_rgid);
    837 	new->e2fs_first_ino	=	bswap32(old->e2fs_first_ino);
    838 	new->e2fs_inode_size	=	bswap16(old->e2fs_inode_size);
    839 	new->e2fs_block_group_nr =	bswap16(old->e2fs_block_group_nr);
    840 	new->e2fs_features_compat =	bswap32(old->e2fs_features_compat);
    841 	new->e2fs_features_incompat =	bswap32(old->e2fs_features_incompat);
    842 	new->e2fs_features_rocompat =	bswap32(old->e2fs_features_rocompat);
    843 	new->e2fs_algo		=	bswap32(old->e2fs_algo);
    844 	new->e2fs_reserved_ngdb	=	bswap16(old->e2fs_reserved_ngdb);
    845 }
    846 
    847 void e2fs_cg_bswap(struct ext2_gd *old, struct ext2_gd *new, int size)
    848 {
    849 	int i;
    850 
    851 	for (i = 0; i < (size / sizeof(struct ext2_gd)); i++) {
    852 		new[i].ext2bgd_b_bitmap	= bswap32(old[i].ext2bgd_b_bitmap);
    853 		new[i].ext2bgd_i_bitmap	= bswap32(old[i].ext2bgd_i_bitmap);
    854 		new[i].ext2bgd_i_tables	= bswap32(old[i].ext2bgd_i_tables);
    855 		new[i].ext2bgd_nbfree	= bswap16(old[i].ext2bgd_nbfree);
    856 		new[i].ext2bgd_nifree	= bswap16(old[i].ext2bgd_nifree);
    857 		new[i].ext2bgd_ndirs	= bswap16(old[i].ext2bgd_ndirs);
    858 	}
    859 }
    860 
    861 void e2fs_i_bswap(struct ext2fs_dinode *old, struct ext2fs_dinode *new)
    862 {
    863 
    864 	new->e2di_mode		=	bswap16(old->e2di_mode);
    865 	new->e2di_uid		=	bswap16(old->e2di_uid);
    866 	new->e2di_gid		=	bswap16(old->e2di_gid);
    867 	new->e2di_nlink		=	bswap16(old->e2di_nlink);
    868 	new->e2di_size		=	bswap32(old->e2di_size);
    869 	new->e2di_atime		=	bswap32(old->e2di_atime);
    870 	new->e2di_ctime		=	bswap32(old->e2di_ctime);
    871 	new->e2di_mtime		=	bswap32(old->e2di_mtime);
    872 	new->e2di_dtime		=	bswap32(old->e2di_dtime);
    873 	new->e2di_nblock	=	bswap32(old->e2di_nblock);
    874 	new->e2di_flags		=	bswap32(old->e2di_flags);
    875 	new->e2di_gen		=	bswap32(old->e2di_gen);
    876 	new->e2di_facl		=	bswap32(old->e2di_facl);
    877 	new->e2di_dacl		=	bswap32(old->e2di_dacl);
    878 	new->e2di_faddr		=	bswap32(old->e2di_faddr);
    879 	memcpy(&new->e2di_blocks[0], &old->e2di_blocks[0],
    880 	    (NDADDR + NIADDR) * sizeof(uint32_t));
    881 }
    882 #endif
    883 
    884 #ifdef EXT2FS_DEBUG
    885 void
    886 dump_sblock(struct m_ext2fs *fs)
    887 {
    888 
    889 	printf("fs->e2fs.e2fs_bcount = %u\n", fs->e2fs.e2fs_bcount);
    890 	printf("fs->e2fs.e2fs_first_dblock = %u\n", fs->e2fs.e2fs_first_dblock);
    891 	printf("fs->e2fs.e2fs_log_bsize = %u\n", fs->e2fs.e2fs_log_bsize);
    892 	printf("fs->e2fs.e2fs_bpg = %u\n", fs->e2fs.e2fs_bpg);
    893 	printf("fs->e2fs.e2fs_ipg = %u\n", fs->e2fs.e2fs_ipg);
    894 	printf("fs->e2fs.e2fs_magic = 0x%x\n", fs->e2fs.e2fs_magic);
    895 	printf("fs->e2fs.e2fs_rev = %u\n", fs->e2fs.e2fs_rev);
    896 
    897 	if (fs->e2fs.e2fs_rev == E2FS_REV1) {
    898 		printf("fs->e2fs.e2fs_first_ino = %u\n",
    899 		    fs->e2fs.e2fs_first_ino);
    900 		printf("fs->e2fs.e2fs_inode_size = %u\n",
    901 		    fs->e2fs.e2fs_inode_size);
    902 		printf("fs->e2fs.e2fs_features_compat = %u\n",
    903 		    fs->e2fs.e2fs_features_compat);
    904 		printf("fs->e2fs.e2fs_features_incompat = %u\n",
    905 		    fs->e2fs.e2fs_features_incompat);
    906 		printf("fs->e2fs.e2fs_features_rocompat = %u\n",
    907 		    fs->e2fs.e2fs_features_rocompat);
    908 		printf("fs->e2fs.e2fs_reserved_ngdb = %u\n",
    909 		    fs->e2fs.e2fs_reserved_ngdb);
    910 	}
    911 
    912 	printf("fs->e2fs_bsize = %u\n", fs->e2fs_bsize);
    913 	printf("fs->e2fs_fsbtodb = %u\n", fs->e2fs_fsbtodb);
    914 	printf("fs->e2fs_ncg = %u\n", fs->e2fs_ncg);
    915 	printf("fs->e2fs_ngdb = %u\n", fs->e2fs_ngdb);
    916 	printf("fs->e2fs_ipb = %u\n", fs->e2fs_ipb);
    917 	printf("fs->e2fs_itpg = %u\n", fs->e2fs_itpg);
    918 }
    919 #endif
    920