Home | History | Annotate | Line # | Download | only in libsa
minixfs3.c revision 1.5.2.1
      1  1.5.2.1     rmind /*	$NetBSD: minixfs3.c,v 1.5.2.1 2014/05/18 17:46:08 rmind 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.4  dholland #define FSBTODB(fs, indp) MFS_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 static int read_inode(ino32_t, struct open_file *);
    173      1.1  christos static int block_map(struct open_file *, block_t, block_t *);
    174      1.1  christos static int buf_read_file(struct open_file *, void *, size_t *);
    175      1.1  christos static int search_directory(const char *, int, struct open_file *, ino32_t *);
    176      1.1  christos static int read_sblock(struct open_file *, struct mfs_sblock *);
    177      1.1  christos 
    178      1.1  christos /*
    179      1.1  christos  * Read a new inode into a file structure.
    180      1.1  christos  */
    181      1.1  christos static int
    182      1.1  christos read_inode(ino32_t inumber, struct open_file *f)
    183      1.1  christos {
    184      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    185      1.1  christos 	struct mfs_sblock *fs = fp->f_fs;
    186      1.1  christos 	char *buf;
    187      1.1  christos 	size_t rsize;
    188      1.1  christos 	int rc;
    189      1.1  christos 	daddr_t inode_sector;
    190      1.1  christos 	struct mfs_dinode *dip;
    191      1.1  christos 
    192      1.1  christos 	inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
    193      1.1  christos 
    194      1.1  christos 	/*
    195      1.1  christos 	 * Read inode and save it.
    196      1.1  christos 	 */
    197      1.1  christos 	buf = fp->f_buf;
    198      1.1  christos 	twiddle();
    199      1.1  christos 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    200      1.1  christos 	    inode_sector, fs->mfs_block_size, buf, &rsize);
    201      1.1  christos 	if (rc)
    202      1.1  christos 		return rc;
    203      1.1  christos 	if (rsize != fs->mfs_block_size)
    204      1.1  christos 		return EIO;
    205      1.1  christos 
    206      1.1  christos 	dip = (struct mfs_dinode *)(buf +
    207      1.1  christos 	    INODE_SIZE * ino_to_fsbo(fs, inumber));
    208      1.1  christos 	mfs_iload(dip, &fp->f_di);
    209      1.1  christos 
    210      1.1  christos 	/*
    211      1.1  christos 	 * Clear out the old buffers
    212      1.1  christos 	 */
    213      1.1  christos 	fp->f_ind_cache_block = ~0;
    214      1.1  christos 	fp->f_buf_blkno = -1;
    215      1.1  christos 	return rc;
    216      1.1  christos }
    217      1.1  christos 
    218      1.1  christos /*
    219      1.1  christos  * Given an offset in a file, find the disk block number (not zone!)
    220      1.1  christos  * that contains that block.
    221      1.1  christos  */
    222      1.1  christos static int
    223      1.1  christos block_map(struct open_file *f, block_t file_block, block_t *disk_block_p)
    224      1.1  christos {
    225      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    226      1.1  christos 	struct mfs_sblock *fs = fp->f_fs;
    227      1.1  christos 	uint level;
    228      1.1  christos 	block_t ind_cache;
    229      1.1  christos 	block_t ind_block_num;
    230      1.1  christos 	zone_t zone;
    231      1.1  christos 	size_t rsize;
    232      1.1  christos 	int rc;
    233      1.1  christos 	int boff;
    234      1.1  christos 	int scale = fs->mfs_log_zone_size; /* for block-zone conversion */
    235      1.1  christos 	block_t *buf = (void *)fp->f_buf;
    236      1.1  christos 
    237      1.1  christos 	/*
    238      1.1  christos 	 * Index structure of an inode:
    239      1.1  christos 	 *
    240      1.1  christos 	 * mdi_blocks[0..NR_DZONES-1]
    241      1.1  christos 	 *			hold zone numbers for zones
    242      1.1  christos 	 *			0..NR_DZONES-1
    243      1.1  christos 	 *
    244      1.1  christos 	 * mdi_blocks[NR_DZONES+0]
    245      1.1  christos 	 *			block NDADDR+0 is the single indirect block
    246      1.1  christos 	 *			holds zone numbers for zones
    247      1.2  dholland 	 *			NR_DZONES .. NR_DZONES + MFS_NINDIR(fs)-1
    248      1.1  christos 	 *
    249      1.1  christos 	 * mdi_blocks[NR_DZONES+1]
    250      1.1  christos 	 *			block NDADDR+1 is the double indirect block
    251      1.1  christos 	 *			holds zone numbers for INDEX blocks for zones
    252      1.2  dholland 	 *			NR_DZONES + MFS_NINDIR(fs) ..
    253      1.2  dholland 	 *			NR_TZONES + MFS_NINDIR(fs) + MFS_NINDIR(fs)**2 - 1
    254      1.1  christos 	 */
    255      1.1  christos 
    256      1.1  christos 	zone = file_block >> scale;
    257      1.1  christos 	boff = (int) (file_block - (zone << scale) ); /* relative blk in zone */
    258      1.1  christos 
    259      1.1  christos 	if (zone < NR_DZONES) {
    260      1.1  christos 		/* Direct zone */
    261      1.1  christos 		zone_t z = fs2h32(fp->f_di.mdi_zone[zone]);
    262      1.1  christos 		if (z == NO_ZONE) {
    263      1.1  christos 			*disk_block_p = NO_BLOCK;
    264      1.1  christos 			return 0;
    265      1.1  christos 		}
    266      1.1  christos 		*disk_block_p = (block_t) ((z << scale) + boff);
    267      1.1  christos 		return 0;
    268      1.1  christos 	}
    269      1.1  christos 
    270      1.1  christos 	zone -= NR_DZONES;
    271      1.1  christos 
    272      1.1  christos 	ind_cache = zone >> LN2_IND_CACHE_SZ;
    273      1.1  christos 	if (ind_cache == fp->f_ind_cache_block) {
    274      1.1  christos 		*disk_block_p =
    275      1.1  christos 		    fs2h32(fp->f_ind_cache[zone & IND_CACHE_MASK]);
    276      1.1  christos 		return 0;
    277      1.1  christos 	}
    278      1.1  christos 
    279      1.1  christos 	for (level = 0;;) {
    280      1.1  christos 		level += fp->f_nishift;
    281      1.1  christos 
    282      1.1  christos 		if (zone < (block_t)1 << level)
    283      1.1  christos 			break;
    284      1.1  christos 		if (level > NIADDR * fp->f_nishift)
    285      1.1  christos 			/* Zone number too high */
    286      1.1  christos 			return EFBIG;
    287      1.1  christos 		zone -= (block_t)1 << level;
    288      1.1  christos 	}
    289      1.1  christos 
    290      1.1  christos 	ind_block_num =
    291      1.1  christos 	    fs2h32(fp->f_di.mdi_zone[NR_DZONES + (level / fp->f_nishift - 1)]);
    292      1.1  christos 
    293      1.1  christos 	for (;;) {
    294      1.1  christos 		level -= fp->f_nishift;
    295      1.1  christos 		if (ind_block_num == 0) {
    296      1.1  christos 			*disk_block_p = NO_BLOCK;	/* missing */
    297      1.1  christos 			return 0;
    298      1.1  christos 		}
    299      1.1  christos 
    300      1.1  christos 		twiddle();
    301      1.1  christos 		/*
    302      1.1  christos 		 * If we were feeling brave, we could work out the number
    303      1.1  christos 		 * of the disk sector and read a single disk sector instead
    304      1.1  christos 		 * of a filesystem block.
    305      1.1  christos 		 * However we don't do this very often anyway...
    306      1.1  christos 		 */
    307      1.1  christos 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    308      1.1  christos 			FSBTODB(fs, ind_block_num), fs->mfs_block_size,
    309      1.1  christos 			buf, &rsize);
    310      1.1  christos 		if (rc)
    311      1.1  christos 			return rc;
    312      1.1  christos 		if (rsize != fs->mfs_block_size)
    313      1.1  christos 			return EIO;
    314      1.1  christos 
    315      1.1  christos 		ind_block_num = fs2h32(buf[zone >> level]);
    316      1.1  christos 		if (level == 0)
    317      1.1  christos 			break;
    318      1.1  christos 		zone &= (1 << level) - 1;
    319      1.1  christos 	}
    320      1.1  christos 
    321      1.1  christos 	/* Save the part of the block that contains this sector */
    322      1.1  christos 	memcpy(fp->f_ind_cache, &buf[zone & ~IND_CACHE_MASK],
    323      1.1  christos 	    IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
    324      1.1  christos 	fp->f_ind_cache_block = ind_cache;
    325      1.1  christos 
    326      1.1  christos 	zone = (zone_t)ind_block_num;
    327      1.1  christos 	*disk_block_p = (block_t)((zone << scale) + boff);
    328      1.1  christos 	return 0;
    329      1.1  christos }
    330      1.1  christos 
    331      1.1  christos /*
    332      1.1  christos  * Read a portion of a file into an internal buffer.
    333      1.1  christos  * Return the location in the buffer and the amount in the buffer.
    334      1.1  christos  */
    335      1.1  christos static int
    336      1.1  christos buf_read_file(struct open_file *f, void *v, size_t *size_p)
    337      1.1  christos {
    338      1.1  christos 	char **buf_p = v;
    339      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    340      1.1  christos 	struct mfs_sblock *fs = fp->f_fs;
    341      1.1  christos 	long off;
    342      1.1  christos 	block_t file_block;
    343  1.5.2.1     rmind 	block_t disk_block = 0;	/* XXX: gcc */
    344      1.1  christos 	size_t block_size;
    345      1.1  christos 	int rc;
    346      1.1  christos 
    347      1.3  dholland 	off = mfs_blkoff(fs, fp->f_seekp);
    348      1.5  dholland 	file_block = mfs_lblkno(fs, fp->f_seekp);
    349      1.1  christos 	block_size = fs->mfs_block_size;
    350      1.1  christos 
    351      1.1  christos 	if (file_block != fp->f_buf_blkno) {
    352      1.1  christos 		rc = block_map(f, file_block, &disk_block);
    353      1.1  christos 		if (rc)
    354      1.1  christos 			return rc;
    355      1.1  christos 
    356      1.1  christos 		if (disk_block == 0) {
    357      1.1  christos 			memset(fp->f_buf, 0, block_size);
    358      1.1  christos 			fp->f_buf_size = block_size;
    359      1.1  christos 		} else {
    360      1.1  christos 			twiddle();
    361      1.1  christos 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    362      1.1  christos 				FSBTODB(fs, disk_block),
    363      1.1  christos 				block_size, fp->f_buf, &fp->f_buf_size);
    364      1.1  christos 			if (rc)
    365      1.1  christos 				return rc;
    366      1.1  christos 		}
    367      1.1  christos 
    368      1.1  christos 		fp->f_buf_blkno = file_block;
    369      1.1  christos 	}
    370      1.1  christos 
    371      1.1  christos 	/*
    372      1.1  christos 	 * Return address of byte in buffer corresponding to
    373      1.1  christos 	 * offset, and size of remainder of buffer after that
    374      1.1  christos 	 * byte.
    375      1.1  christos 	 */
    376      1.1  christos 	*buf_p = fp->f_buf + off;
    377      1.1  christos 	*size_p = block_size - off;
    378      1.1  christos 
    379      1.1  christos 	/*
    380      1.1  christos 	 * But truncate buffer at end of file.
    381      1.1  christos 	 */
    382      1.1  christos 	if (*size_p > fp->f_di.mdi_size - fp->f_seekp)
    383      1.1  christos 		*size_p = fp->f_di.mdi_size - fp->f_seekp;
    384      1.1  christos 
    385      1.1  christos 	return 0;
    386      1.1  christos }
    387      1.1  christos 
    388      1.1  christos /*
    389      1.1  christos  * Search a directory for a name and return its
    390      1.1  christos  * inode number.
    391      1.1  christos  */
    392      1.1  christos static int
    393      1.1  christos search_directory(const char *name, int length, struct open_file *f,
    394      1.1  christos 	ino32_t *inumber_p)
    395      1.1  christos {
    396      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    397      1.1  christos 	struct mfs_sblock *fs = fp->f_fs;
    398      1.1  christos 	struct mfs_direct *dp;
    399      1.1  christos 	struct mfs_direct *dbuf;
    400      1.1  christos 	size_t buf_size;
    401      1.1  christos 	int namlen;
    402      1.1  christos 	int rc;
    403      1.1  christos 
    404      1.1  christos 	fp->f_seekp = 0;
    405      1.1  christos 
    406      1.1  christos 	while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
    407      1.1  christos 		rc = buf_read_file(f, (void *)&dbuf, &buf_size);
    408      1.1  christos 		if (rc)
    409      1.1  christos 			return rc;
    410      1.1  christos 		if (buf_size == 0)
    411      1.1  christos 			return EIO;
    412      1.1  christos 
    413      1.1  christos 		/* XXX we assume, that buf_read_file reads an fs block and
    414      1.1  christos 		 * doesn't truncate buffer. Currently i_size in MFS doesn't
    415      1.1  christos 		 * the same as size of allocated blocks, it makes buf_read_file
    416      1.1  christos 		 * to truncate buf_size.
    417      1.1  christos 		 */
    418      1.1  christos 		if (buf_size < fs->mfs_block_size)
    419      1.1  christos 			buf_size = fs->mfs_block_size;
    420      1.1  christos 
    421      1.1  christos 		for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
    422      1.1  christos 			char *cp;
    423      1.1  christos 			if (fs2h32(dp->mfsd_ino) == (ino32_t) 0)
    424      1.1  christos 				continue;
    425      1.1  christos 			/* Compute the length of the name */
    426      1.1  christos 			cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
    427      1.1  christos 			if (cp == NULL)
    428      1.1  christos 				namlen = sizeof(dp->mfsd_name);
    429      1.1  christos 			else
    430      1.1  christos 				namlen = cp - (dp->mfsd_name);
    431      1.1  christos 
    432      1.1  christos 			if (namlen == length &&
    433      1.1  christos 			    !memcmp(name, dp->mfsd_name, length)) {
    434      1.1  christos 				/* found entry */
    435      1.1  christos 				*inumber_p = fs2h32(dp->mfsd_ino);
    436      1.1  christos 				return 0;
    437      1.1  christos 			}
    438      1.1  christos 		}
    439      1.1  christos 		fp->f_seekp += buf_size;
    440      1.1  christos 	}
    441      1.1  christos 	return ENOENT;
    442      1.1  christos }
    443      1.1  christos 
    444      1.1  christos int
    445      1.1  christos read_sblock(struct open_file *f, struct mfs_sblock *fs)
    446      1.1  christos {
    447      1.1  christos 	static uint8_t sbbuf[MINBSIZE];
    448      1.1  christos 	size_t buf_size;
    449      1.1  christos 	int rc;
    450      1.1  christos 
    451      1.1  christos 	/* We must read amount multiple of sector size, hence we can't
    452      1.1  christos 	 * read SBSIZE and read MINBSIZE.
    453      1.1  christos 	 */
    454      1.1  christos 	if (SBSIZE > MINBSIZE)
    455      1.1  christos 		return EINVAL;
    456      1.1  christos 
    457      1.1  christos 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    458      1.1  christos 	    SUPER_BLOCK_OFF / DEV_BSIZE, MINBSIZE, sbbuf, &buf_size);
    459      1.1  christos 	if (rc)
    460      1.1  christos 		return rc;
    461      1.1  christos 
    462      1.1  christos 	if (buf_size != MINBSIZE)
    463      1.1  christos 		return EIO;
    464      1.1  christos 
    465      1.1  christos 	mfs_sbload((void *)sbbuf, fs);
    466      1.1  christos 
    467      1.1  christos 	if (fs->mfs_magic != SUPER_MAGIC)
    468      1.1  christos 		return EINVAL;
    469      1.1  christos 	if (fs->mfs_block_size < MINBSIZE)
    470      1.1  christos 		return EINVAL;
    471      1.1  christos 	if ((fs->mfs_block_size % 512) != 0)
    472      1.1  christos 		return EINVAL;
    473      1.1  christos 	if (SBSIZE > fs->mfs_block_size)
    474      1.1  christos 		return EINVAL;
    475      1.1  christos 	if ((fs->mfs_block_size % INODE_SIZE) != 0)
    476      1.1  christos 		return EINVAL;
    477      1.1  christos 
    478      1.1  christos 	/* For even larger disks, a similar problem occurs with s_firstdatazone.
    479      1.1  christos 	 * If the on-disk field contains zero, we assume that the value was too
    480      1.1  christos 	 * large to fit, and compute it on the fly.
    481      1.1  christos 	 */
    482      1.1  christos 	if (fs->mfs_firstdatazone_old == 0) {
    483      1.1  christos 		block_t offset;
    484      1.1  christos 		offset = START_BLOCK + fs->mfs_imap_blocks + fs->mfs_zmap_blocks;
    485      1.1  christos 		offset += (fs->mfs_ninodes + fs->mfs_inodes_per_block - 1) /
    486      1.1  christos 				fs->mfs_inodes_per_block;
    487      1.1  christos 
    488      1.1  christos 		fs->mfs_firstdatazone =
    489      1.1  christos 			(offset + (1 << fs->mfs_log_zone_size) - 1) >>
    490      1.1  christos 				fs->mfs_log_zone_size;
    491      1.1  christos 	} else {
    492      1.1  christos 		fs->mfs_firstdatazone = (zone_t) fs->mfs_firstdatazone_old;
    493      1.1  christos 	}
    494      1.1  christos 
    495      1.1  christos 	if (fs->mfs_imap_blocks < 1 || fs->mfs_zmap_blocks < 1
    496      1.1  christos 			|| fs->mfs_ninodes < 1 || fs->mfs_zones < 1
    497      1.1  christos 			|| fs->mfs_firstdatazone <= 4
    498      1.1  christos 			|| fs->mfs_firstdatazone >= fs->mfs_zones
    499      1.1  christos 			|| (unsigned) fs->mfs_log_zone_size > 4)
    500      1.1  christos 		return EINVAL;
    501      1.1  christos 
    502      1.1  christos 	/* compute in-memory mfs_sblock values */
    503      1.1  christos 	fs->mfs_inodes_per_block = fs->mfs_block_size / INODE_SIZE;
    504      1.1  christos 
    505      1.1  christos 
    506      1.1  christos 	{
    507      1.1  christos 		int32_t mult = fs->mfs_block_size >> LOG_MINBSIZE;
    508      1.1  christos 		int ln2 = LOG_MINBSIZE;
    509      1.1  christos 
    510      1.1  christos 		for (; mult != 1; ln2++)
    511      1.1  christos 			mult >>= 1;
    512      1.1  christos 
    513      1.1  christos 		fs->mfs_bshift = ln2;
    514      1.1  christos 		/* XXX assume hw bsize = 512 */
    515      1.1  christos 		fs->mfs_fsbtodb = ln2 - LOG_MINBSIZE + 1;
    516      1.1  christos 	}
    517      1.1  christos 
    518      1.1  christos 	fs->mfs_qbmask = fs->mfs_block_size - 1;
    519      1.1  christos 	fs->mfs_bmask = ~fs->mfs_qbmask;
    520      1.1  christos 
    521      1.1  christos 	return 0;
    522      1.1  christos }
    523      1.1  christos 
    524      1.1  christos /*
    525      1.1  christos  * Open a file.
    526      1.1  christos  */
    527      1.1  christos __compactcall int
    528      1.1  christos minixfs3_open(const char *path, struct open_file *f)
    529      1.1  christos {
    530      1.1  christos #ifndef LIBSA_FS_SINGLECOMPONENT
    531      1.1  christos 	const char *cp, *ncp;
    532      1.1  christos 	int c;
    533      1.1  christos #endif
    534      1.1  christos 	ino32_t inumber;
    535      1.1  christos 	struct file *fp;
    536      1.1  christos 	struct mfs_sblock *fs;
    537      1.1  christos 	int rc;
    538      1.1  christos #ifndef LIBSA_NO_FS_SYMLINK
    539      1.1  christos 	ino32_t parent_inumber;
    540      1.1  christos 	int nlinks = 0;
    541      1.1  christos 	char namebuf[MAXPATHLEN+1];
    542      1.1  christos 	char *buf;
    543      1.1  christos #endif
    544      1.1  christos 
    545      1.1  christos 	/* allocate file system specific data structure */
    546      1.1  christos 	fp = alloc(sizeof(struct file));
    547      1.1  christos 	memset(fp, 0, sizeof(struct file));
    548      1.1  christos 	f->f_fsdata = (void *)fp;
    549      1.1  christos 
    550      1.1  christos 	/* allocate space and read super block */
    551      1.1  christos 	fs = alloc(sizeof(*fs));
    552      1.1  christos 	memset(fs, 0, sizeof(*fs));
    553      1.1  christos 	fp->f_fs = fs;
    554      1.1  christos 	twiddle();
    555      1.1  christos 
    556      1.1  christos 	rc = read_sblock(f, fs);
    557      1.1  christos 	if (rc)
    558      1.1  christos 		goto out;
    559      1.1  christos 
    560      1.1  christos 	/* alloc a block sized buffer used for all fs transfers */
    561      1.1  christos 	fp->f_buf = alloc(fs->mfs_block_size);
    562      1.1  christos 
    563      1.1  christos 	/*
    564      1.1  christos 	 * Calculate indirect block levels.
    565      1.1  christos 	 */
    566      1.1  christos 	{
    567      1.1  christos 		int32_t mult;
    568      1.1  christos 		int ln2;
    569      1.1  christos 
    570      1.1  christos 		/*
    571      1.1  christos 		 * We note that the number of indirect blocks is always
    572      1.1  christos 		 * a power of 2.  This lets us use shifts and masks instead
    573      1.1  christos 		 * of divide and remainder and avoinds pulling in the
    574      1.1  christos 		 * 64bit division routine into the boot code.
    575      1.1  christos 		 */
    576      1.2  dholland 		mult = MFS_NINDIR(fs);
    577      1.1  christos #ifdef DEBUG
    578      1.1  christos 		if (!powerof2(mult)) {
    579      1.1  christos 			/* Hummm was't a power of 2 */
    580      1.1  christos 			rc = EINVAL;
    581      1.1  christos 			goto out;
    582      1.1  christos 		}
    583      1.1  christos #endif
    584      1.1  christos 		for (ln2 = 0; mult != 1; ln2++)
    585      1.1  christos 			mult >>= 1;
    586      1.1  christos 
    587      1.1  christos 		fp->f_nishift = ln2;
    588      1.1  christos 	}
    589      1.1  christos 
    590      1.1  christos 	inumber = ROOT_INODE;
    591      1.1  christos 	if ((rc = read_inode(inumber, f)) != 0)
    592      1.1  christos 		goto out;
    593      1.1  christos 
    594      1.1  christos #ifndef LIBSA_FS_SINGLECOMPONENT
    595      1.1  christos 	cp = path;
    596      1.1  christos 	while (*cp) {
    597      1.1  christos 
    598      1.1  christos 		/*
    599      1.1  christos 		 * Remove extra separators
    600      1.1  christos 		 */
    601      1.1  christos 		while (*cp == '/')
    602      1.1  christos 			cp++;
    603      1.1  christos 		if (*cp == '\0')
    604      1.1  christos 			break;
    605      1.1  christos 
    606      1.1  christos 		/*
    607      1.1  christos 		 * Check that current node is a directory.
    608      1.1  christos 		 */
    609      1.1  christos 		if ((fp->f_di.mdi_mode & I_TYPE) != I_DIRECTORY) {
    610      1.1  christos 			rc = ENOTDIR;
    611      1.1  christos 			goto out;
    612      1.1  christos 		}
    613      1.1  christos 
    614      1.1  christos 		/*
    615      1.1  christos 		 * Get next component of path name.
    616      1.1  christos 		 */
    617      1.1  christos 		ncp = cp;
    618      1.1  christos 		while ((c = *cp) != '\0' && c != '/')
    619      1.1  christos 			cp++;
    620      1.1  christos 
    621      1.1  christos 		/*
    622      1.1  christos 		 * Look up component in current directory.
    623      1.1  christos 		 * Save directory inumber in case we find a
    624      1.1  christos 		 * symbolic link.
    625      1.1  christos 		 */
    626      1.1  christos #ifndef LIBSA_NO_FS_SYMLINK
    627      1.1  christos 		parent_inumber = inumber;
    628      1.1  christos #endif
    629      1.1  christos 		rc = search_directory(ncp, cp - ncp, f, &inumber);
    630      1.1  christos 		if (rc)
    631      1.1  christos 			goto out;
    632      1.1  christos 
    633      1.1  christos 		/*
    634      1.1  christos 		 * Open next component.
    635      1.1  christos 		 */
    636      1.1  christos 		if ((rc = read_inode(inumber, f)) != 0)
    637      1.1  christos 			goto out;
    638      1.1  christos 
    639      1.1  christos #ifndef LIBSA_NO_FS_SYMLINK
    640      1.1  christos 		/*
    641      1.1  christos 		 * Check for symbolic link.
    642      1.1  christos 		 */
    643      1.1  christos 		if ((fp->f_di.mdi_mode & I_TYPE) == I_SYMBOLIC_LINK) {
    644      1.1  christos 			int link_len = fp->f_di.mdi_size;
    645      1.1  christos 			int len;
    646      1.1  christos 			size_t buf_size;
    647      1.1  christos 			block_t	disk_block;
    648      1.1  christos 
    649      1.1  christos 			len = strlen(cp);
    650      1.1  christos 
    651      1.1  christos 			if (link_len + len > MAXPATHLEN ||
    652      1.1  christos 			    ++nlinks > MAXSYMLINKS) {
    653      1.1  christos 				rc = ENOENT;
    654      1.1  christos 				goto out;
    655      1.1  christos 			}
    656      1.1  christos 
    657      1.1  christos 			memmove(&namebuf[link_len], cp, len + 1);
    658      1.1  christos 
    659      1.1  christos 			/*
    660      1.1  christos 			 * Read file for symbolic link
    661      1.1  christos 			 */
    662      1.1  christos 			buf = fp->f_buf;
    663      1.1  christos 			rc = block_map(f, (block_t)0, &disk_block);
    664      1.1  christos 			if (rc)
    665      1.1  christos 				goto out;
    666      1.1  christos 
    667      1.1  christos 			twiddle();
    668      1.1  christos 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
    669      1.1  christos 					F_READ, FSBTODB(fs, disk_block),
    670      1.1  christos 					fs->mfs_block_size, buf, &buf_size);
    671      1.1  christos 			if (rc)
    672      1.1  christos 				goto out;
    673      1.1  christos 
    674      1.1  christos 			memcpy(namebuf, buf, link_len);
    675      1.1  christos 
    676      1.1  christos 			/*
    677      1.1  christos 			 * If relative pathname, restart at parent directory.
    678      1.1  christos 			 * If absolute pathname, restart at root.
    679      1.1  christos 			 */
    680      1.1  christos 			cp = namebuf;
    681      1.1  christos 			if (*cp != '/')
    682      1.1  christos 				inumber = parent_inumber;
    683      1.1  christos 			else
    684      1.1  christos 				inumber = (ino32_t) ROOT_INODE;
    685      1.1  christos 
    686      1.1  christos 			if ((rc = read_inode(inumber, f)) != 0)
    687      1.1  christos 				goto out;
    688      1.1  christos 		}
    689      1.1  christos #endif	/* !LIBSA_NO_FS_SYMLINK */
    690      1.1  christos 	}
    691      1.1  christos 
    692      1.1  christos 	/*
    693      1.1  christos 	 * Found terminal component.
    694      1.1  christos 	 */
    695      1.1  christos 	rc = 0;
    696      1.1  christos 
    697      1.1  christos #else /* !LIBSA_FS_SINGLECOMPONENT */
    698      1.1  christos 
    699      1.1  christos 	/* look up component in the current (root) directory */
    700      1.1  christos 	rc = search_directory(path, strlen(path), f, &inumber);
    701      1.1  christos 	if (rc)
    702      1.1  christos 		goto out;
    703      1.1  christos 
    704      1.1  christos 	/* open it */
    705      1.1  christos 	rc = read_inode(inumber, f);
    706      1.1  christos 
    707      1.1  christos #endif /* !LIBSA_FS_SINGLECOMPONENT */
    708      1.1  christos 
    709      1.1  christos 	fp->f_seekp = 0;		/* reset seek pointer */
    710      1.1  christos 
    711      1.1  christos out:
    712      1.1  christos 	if (rc)
    713      1.1  christos 		minixfs3_close(f);
    714      1.1  christos 
    715      1.1  christos 	return rc;
    716      1.1  christos }
    717      1.1  christos 
    718      1.1  christos __compactcall int
    719      1.1  christos minixfs3_close(struct open_file *f)
    720      1.1  christos {
    721      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    722      1.1  christos 
    723      1.1  christos 	f->f_fsdata = NULL;
    724      1.1  christos 	if (fp == NULL)
    725      1.1  christos 		return 0;
    726      1.1  christos 
    727      1.1  christos 	if (fp->f_buf)
    728      1.1  christos 		dealloc(fp->f_buf, fp->f_fs->mfs_block_size);
    729      1.1  christos 	dealloc(fp->f_fs, sizeof(*fp->f_fs));
    730      1.1  christos 	dealloc(fp, sizeof(struct file));
    731      1.1  christos 	return 0;
    732      1.1  christos }
    733      1.1  christos 
    734      1.1  christos /*
    735      1.1  christos  * Copy a portion of a file into kernel memory.
    736      1.1  christos  * Cross block boundaries when necessary.
    737      1.1  christos  */
    738      1.1  christos __compactcall int
    739      1.1  christos minixfs3_read(struct open_file *f, void *start, size_t size, size_t *resid)
    740      1.1  christos {
    741      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    742      1.1  christos 	size_t csize;
    743      1.1  christos 	char *buf;
    744      1.1  christos 	size_t buf_size;
    745      1.1  christos 	int rc = 0;
    746      1.1  christos 	char *addr = start;
    747      1.1  christos 
    748      1.1  christos 	while (size != 0) {
    749      1.1  christos 		if (fp->f_seekp >= (off_t)fp->f_di.mdi_size)
    750      1.1  christos 			break;
    751      1.1  christos 
    752      1.1  christos 		rc = buf_read_file(f, &buf, &buf_size);
    753      1.1  christos 		if (rc)
    754      1.1  christos 			break;
    755      1.1  christos 
    756      1.1  christos 		csize = size;
    757      1.1  christos 		if (csize > buf_size)
    758      1.1  christos 			csize = buf_size;
    759      1.1  christos 
    760      1.1  christos 		memcpy(addr, buf, csize);
    761      1.1  christos 
    762      1.1  christos 		fp->f_seekp += csize;
    763      1.1  christos 		addr += csize;
    764      1.1  christos 		size -= csize;
    765      1.1  christos 	}
    766      1.1  christos 
    767      1.1  christos 	if (resid)
    768      1.1  christos 		*resid = size;
    769      1.1  christos 	return rc;
    770      1.1  christos }
    771      1.1  christos 
    772      1.1  christos /*
    773      1.1  christos  * Not implemented.
    774      1.1  christos  */
    775      1.1  christos #ifndef LIBSA_NO_FS_WRITE
    776      1.1  christos __compactcall int
    777      1.1  christos minixfs3_write(struct open_file *f, void *start, size_t size, size_t *resid)
    778      1.1  christos {
    779      1.1  christos 
    780      1.1  christos 	return EROFS;
    781      1.1  christos }
    782      1.1  christos #endif /* !LIBSA_NO_FS_WRITE */
    783      1.1  christos 
    784      1.1  christos #ifndef LIBSA_NO_FS_SEEK
    785      1.1  christos __compactcall off_t
    786      1.1  christos minixfs3_seek(struct open_file *f, off_t offset, int where)
    787      1.1  christos {
    788      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    789      1.1  christos 
    790      1.1  christos 	switch (where) {
    791      1.1  christos 	case SEEK_SET:
    792      1.1  christos 		fp->f_seekp = offset;
    793      1.1  christos 		break;
    794      1.1  christos 	case SEEK_CUR:
    795      1.1  christos 		fp->f_seekp += offset;
    796      1.1  christos 		break;
    797      1.1  christos 	case SEEK_END:
    798      1.1  christos 		fp->f_seekp = fp->f_di.mdi_size - offset;
    799      1.1  christos 		break;
    800      1.1  christos 	default:
    801      1.1  christos 		return -1;
    802      1.1  christos 	}
    803      1.1  christos 	return fp->f_seekp;
    804      1.1  christos }
    805      1.1  christos #endif /* !LIBSA_NO_FS_SEEK */
    806      1.1  christos 
    807      1.1  christos __compactcall int
    808      1.1  christos minixfs3_stat(struct open_file *f, struct stat *sb)
    809      1.1  christos {
    810      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    811      1.1  christos 
    812      1.1  christos 	/* only important stuff */
    813      1.1  christos 	memset(sb, 0, sizeof *sb);
    814      1.1  christos 	sb->st_mode = fp->f_di.mdi_mode;
    815      1.1  christos 	sb->st_uid = fp->f_di.mdi_uid;
    816      1.1  christos 	sb->st_gid = fp->f_di.mdi_gid;
    817      1.1  christos 	sb->st_size = fp->f_di.mdi_size;
    818      1.1  christos 	return 0;
    819      1.1  christos }
    820      1.1  christos 
    821      1.1  christos #if defined(LIBSA_ENABLE_LS_OP)
    822  1.5.2.1     rmind #include "ls.h"
    823      1.1  christos __compactcall void
    824      1.1  christos minixfs3_ls(struct open_file *f, const char *pattern)
    825      1.1  christos {
    826      1.1  christos 	struct file *fp = (struct file *)f->f_fsdata;
    827      1.1  christos 	struct mfs_sblock *fs = fp->f_fs;
    828      1.1  christos 	struct mfs_direct *dp;
    829      1.1  christos 	struct mfs_direct *dbuf;
    830      1.1  christos 	size_t buf_size;
    831  1.5.2.1     rmind 	lsentry_t *names = 0;
    832      1.1  christos 
    833      1.1  christos 	fp->f_seekp = 0;
    834      1.1  christos 	while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
    835      1.1  christos 		int rc = buf_read_file(f, &dbuf, &buf_size);
    836      1.1  christos 		if (rc)
    837      1.1  christos 			goto out;
    838      1.1  christos 
    839      1.1  christos 		/* XXX we assume, that buf_read_file reads an fs block and
    840      1.1  christos 		 * doesn't truncate buffer. Currently i_size in MFS doesn't
    841      1.1  christos 		 * the same as size of allocated blocks, it makes buf_read_file
    842      1.1  christos 		 * to truncate buf_size.
    843      1.1  christos 		 */
    844      1.1  christos 		if (buf_size < fs->mfs_block_size)
    845      1.1  christos 			buf_size = fs->mfs_block_size;
    846      1.1  christos 
    847      1.1  christos 		for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
    848      1.1  christos 			char *cp;
    849      1.1  christos 			int namlen;
    850      1.1  christos 
    851      1.1  christos 			if (fs2h32(dp->mfsd_ino) == 0)
    852      1.1  christos 				continue;
    853      1.1  christos 
    854      1.1  christos 			/* Compute the length of the name,
    855      1.1  christos 			 * We don't use strlen and strcpy, because original MFS
    856      1.1  christos 			 * code doesn't.
    857      1.1  christos 			 */
    858      1.1  christos 			cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
    859      1.1  christos 			if (cp == NULL)
    860      1.1  christos 				namlen = sizeof(dp->mfsd_name);
    861      1.1  christos 			else
    862      1.1  christos 				namlen = cp - (dp->mfsd_name);
    863      1.1  christos 
    864  1.5.2.1     rmind 			lsadd(&names, pattern, dp->mfsd_name, namlen,
    865  1.5.2.1     rmind 			    fs2h32(dp->mfsd_ino), "?");
    866      1.1  christos 		}
    867      1.1  christos 		fp->f_seekp += buf_size;
    868      1.1  christos 	}
    869  1.5.2.1     rmind 	lsprint(names);
    870  1.5.2.1     rmind out:	lsfree(names);
    871      1.1  christos }
    872      1.1  christos #endif
    873      1.1  christos 
    874      1.1  christos /*
    875      1.1  christos  * byte swap functions for big endian machines
    876      1.1  christos  * (mfs is always little endian)
    877      1.1  christos  */
    878      1.1  christos 
    879      1.1  christos /* These functions are only needed if native byte order is not big endian */
    880      1.1  christos #if BYTE_ORDER == BIG_ENDIAN
    881      1.1  christos void
    882      1.1  christos minixfs3_sb_bswap(struct mfs_sblock *old, struct mfs_sblock *new)
    883      1.1  christos {
    884      1.1  christos 	new->mfs_ninodes	=	bswap32(old->mfs_ninodes);
    885      1.1  christos 	new->mfs_nzones		=	bswap16(old->mfs_nzones);
    886      1.1  christos 	new->mfs_imap_blocks	=	bswap16(old->mfs_imap_blocks);
    887      1.1  christos 	new->mfs_zmap_blocks	=	bswap16(old->mfs_zmap_blocks);
    888      1.1  christos 	new->mfs_firstdatazone_old =	bswap16(old->mfs_firstdatazone_old);
    889      1.1  christos 	new->mfs_log_zone_size	=	bswap16(old->mfs_log_zone_size);
    890      1.1  christos 	new->mfs_max_size	=	bswap32(old->mfs_max_size);
    891      1.1  christos 	new->mfs_zones		=	bswap32(old->mfs_zones);
    892      1.1  christos 	new->mfs_magic		=	bswap16(old->mfs_magic);
    893      1.1  christos 	new->mfs_block_size	=	bswap16(old->mfs_block_size);
    894      1.1  christos 	new->mfs_disk_version	=	old->mfs_disk_version;
    895      1.1  christos }
    896      1.1  christos 
    897      1.1  christos void minixfs3_i_bswap(struct mfs_dinode *old, struct mfs_dinode *new)
    898      1.1  christos {
    899      1.1  christos 	int i;
    900      1.1  christos 
    901      1.1  christos 	new->mdi_mode		=	bswap16(old->mdi_mode);
    902      1.1  christos 	new->mdi_nlinks		=	bswap16(old->mdi_nlinks);
    903      1.1  christos 	new->mdi_uid		=	bswap16(old->mdi_uid);
    904      1.1  christos 	new->mdi_gid		=	bswap16(old->mdi_gid);
    905      1.1  christos 	new->mdi_size		=	bswap32(old->mdi_size);
    906      1.1  christos 	new->mdi_atime		=	bswap32(old->mdi_atime);
    907      1.1  christos 	new->mdi_mtime		=	bswap32(old->mdi_mtime);
    908      1.1  christos 	new->mdi_ctime		=	bswap32(old->mdi_ctime);
    909      1.1  christos 
    910      1.1  christos 	/* We don't swap here, because indirects must be swapped later
    911      1.1  christos 	 * anyway, hence everything is done by block_map().
    912      1.1  christos 	 */
    913      1.1  christos 	for (i = 0; i < NR_TZONES; i++)
    914      1.1  christos 		new->mdi_zone[i] = old->mdi_zone[i];
    915      1.1  christos }
    916      1.1  christos #endif
    917