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