Home | History | Annotate | Line # | Download | only in libsa
ufs.c revision 1.69
      1  1.69  dholland /*	$NetBSD: ufs.c,v 1.69 2015/07/28 05:13:14 dholland Exp $	*/
      2   1.8       cgd 
      3   1.1    brezak /*-
      4   1.1    brezak  * Copyright (c) 1993
      5   1.1    brezak  *	The Regents of the University of California.  All rights reserved.
      6   1.1    brezak  *
      7   1.1    brezak  * This code is derived from software contributed to Berkeley by
      8   1.1    brezak  * The Mach Operating System project at Carnegie-Mellon University.
      9   1.1    brezak  *
     10   1.1    brezak  * Redistribution and use in source and binary forms, with or without
     11   1.1    brezak  * modification, are permitted provided that the following conditions
     12   1.1    brezak  * are met:
     13   1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     14   1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     15   1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    brezak  *    documentation and/or other materials provided with the distribution.
     18  1.36       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1    brezak  *    may be used to endorse or promote products derived from this software
     20   1.1    brezak  *    without specific prior written permission.
     21   1.1    brezak  *
     22   1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1    brezak  * SUCH DAMAGE.
     33  1.22    simonb  *
     34   1.1    brezak  *
     35   1.1    brezak  * Copyright (c) 1990, 1991 Carnegie Mellon University
     36   1.1    brezak  * All Rights Reserved.
     37   1.1    brezak  *
     38   1.1    brezak  * Author: David Golub
     39  1.22    simonb  *
     40   1.1    brezak  * Permission to use, copy, modify and distribute this software and its
     41   1.1    brezak  * documentation is hereby granted, provided that both the copyright
     42   1.1    brezak  * notice and this permission notice appear in all copies of the
     43   1.1    brezak  * software, derivative works or modified versions, and any portions
     44   1.1    brezak  * thereof, and that both notices appear in supporting documentation.
     45  1.22    simonb  *
     46   1.1    brezak  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     47   1.1    brezak  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     48   1.1    brezak  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     49  1.22    simonb  *
     50   1.1    brezak  * Carnegie Mellon requests users of this software to return to
     51  1.22    simonb  *
     52   1.1    brezak  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     53   1.1    brezak  *  School of Computer Science
     54   1.1    brezak  *  Carnegie Mellon University
     55   1.1    brezak  *  Pittsburgh PA 15213-3890
     56  1.22    simonb  *
     57   1.1    brezak  * any improvements or extensions that they make and grant Carnegie the
     58   1.1    brezak  * rights to redistribute these changes.
     59  1.27       cgd  */
     60  1.27       cgd 
     61  1.27       cgd /*
     62  1.37       dsl  *	Stand-alone file reading package for UFS and LFS filesystems.
     63   1.1    brezak  */
     64   1.1    brezak 
     65   1.1    brezak #include <sys/param.h>
     66   1.1    brezak #include <sys/time.h>
     67   1.1    brezak #include <ufs/ufs/dinode.h>
     68   1.1    brezak #include <ufs/ufs/dir.h>
     69  1.35       dsl #ifdef LIBSA_LFS
     70  1.35       dsl #include <sys/queue.h>
     71  1.53        ad #include <sys/condvar.h>
     72  1.35       dsl #include <sys/mount.h>			/* XXX for MNAMELEN */
     73  1.68  dholland #include <ufs/lfs/lfs.h>
     74  1.35       dsl #else
     75  1.20      ross #include <ufs/ffs/fs.h>
     76  1.35       dsl #endif
     77  1.29   thorpej #ifdef _STANDALONE
     78  1.29   thorpej #include <lib/libkern/libkern.h>
     79  1.29   thorpej #else
     80  1.29   thorpej #include <string.h>
     81  1.29   thorpej #endif
     82   1.2     glass 
     83   1.1    brezak #include "stand.h"
     84  1.35       dsl #ifdef LIBSA_LFS
     85  1.35       dsl #include "lfs.h"
     86  1.35       dsl #else
     87  1.21        pk #include "ufs.h"
     88  1.35       dsl #endif
     89  1.35       dsl 
     90  1.35       dsl /* If this file is compiled by itself, build ufs (aka ffsv1) support */
     91  1.35       dsl #if !defined(LIBSA_FFSv2) && !defined(LIBSA_LFS)
     92  1.35       dsl #define LIBSA_FFSv1
     93  1.35       dsl #endif
     94  1.23       cgd 
     95  1.23       cgd #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
     96  1.23       cgd #define LIBSA_NO_FS_SYMLINK
     97  1.25    simonb #endif
     98  1.25    simonb #if defined(COMPAT_UFS) && defined(LIBSA_NO_COMPAT_UFS)
     99  1.25    simonb #undef COMPAT_UFS
    100  1.23       cgd #endif
    101  1.23       cgd 
    102  1.35       dsl #ifdef LIBSA_LFS
    103  1.35       dsl /*
    104  1.66  dholland  * In-core LFS superblock - just the on-disk one.
    105  1.35       dsl  */
    106  1.69  dholland struct salfs {
    107  1.69  dholland 	struct dlfs lfs_dlfs;
    108  1.69  dholland };
    109  1.69  dholland /* Get lfs accessors that use struct salfs. */
    110  1.69  dholland #define STRUCT_LFS struct salfs
    111  1.69  dholland #include <ufs/lfs/lfs_accessors.h>
    112  1.69  dholland 
    113  1.67  dholland typedef struct salfs FS;
    114  1.67  dholland #define fs_magic	lfs_dlfs.dlfs_magic
    115  1.67  dholland #define fs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
    116  1.33      fvdl 
    117  1.35       dsl #define FS_MAGIC	LFS_MAGIC
    118  1.35       dsl #define SBLOCKSIZE	LFS_SBPAD
    119  1.35       dsl #define SBLOCKOFFSET	LFS_LABELPAD
    120  1.33      fvdl #else
    121  1.66  dholland /* NB ufs2 doesn't use the common superblock code... */
    122  1.66  dholland typedef struct fs FS;
    123  1.35       dsl #define FS_MAGIC	FS_UFS1_MAGIC
    124  1.35       dsl #define SBLOCKOFFSET	SBLOCK_UFS1
    125  1.35       dsl #endif
    126  1.35       dsl 
    127  1.35       dsl #if defined(LIBSA_NO_TWIDDLE)
    128  1.35       dsl #define twiddle()
    129  1.35       dsl #endif
    130  1.35       dsl 
    131  1.40       dsl #undef cgstart
    132  1.40       dsl #if defined(LIBSA_FFSv2)
    133  1.40       dsl #define cgstart(fc, c) cgstart_ufs2((fs), (c))
    134  1.40       dsl #else
    135  1.40       dsl #define cgstart(fc, c) cgstart_ufs1((fs), (c))
    136  1.40       dsl #endif
    137  1.40       dsl 
    138  1.35       dsl #ifndef ufs_dinode
    139  1.35       dsl #define ufs_dinode	ufs1_dinode
    140  1.35       dsl #endif
    141  1.35       dsl #ifndef indp_t
    142  1.47       uwe #define indp_t		int32_t
    143  1.35       dsl #endif
    144  1.45  christos typedef uint32_t	ino32_t;
    145  1.61  dholland 
    146  1.35       dsl #ifndef FSBTODB
    147  1.62  dholland #define FSBTODB(fs, indp) FFS_FSBTODB(fs, indp)
    148  1.33      fvdl #endif
    149  1.61  dholland #ifndef UFS_NINDIR
    150  1.61  dholland #define UFS_NINDIR FFS_NINDIR
    151  1.61  dholland #endif
    152  1.61  dholland #ifndef ufs_blkoff
    153  1.61  dholland #define ufs_blkoff ffs_blkoff
    154  1.61  dholland #endif
    155  1.63  dholland #ifndef ufs_lblkno
    156  1.63  dholland #define ufs_lblkno ffs_lblkno
    157  1.63  dholland #endif
    158  1.23       cgd 
    159   1.1    brezak /*
    160  1.40       dsl  * To avoid having a lot of filesystem-block sized buffers lurking (which
    161  1.40       dsl  * could be 32k) we only keep a few entries of the indirect block map.
    162  1.40       dsl  * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block
    163  1.40       dsl  * ~13 times pulling in a 6M kernel.
    164  1.40       dsl  * The cache size must be smaller than the smallest filesystem block,
    165  1.40       dsl  * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks).
    166  1.40       dsl  */
    167  1.40       dsl #define LN2_IND_CACHE_SZ	6
    168  1.40       dsl #define IND_CACHE_SZ		(1 << LN2_IND_CACHE_SZ)
    169  1.40       dsl #define IND_CACHE_MASK		(IND_CACHE_SZ - 1)
    170  1.40       dsl 
    171  1.40       dsl /*
    172   1.1    brezak  * In-core open file.
    173   1.1    brezak  */
    174   1.1    brezak struct file {
    175   1.1    brezak 	off_t		f_seekp;	/* seek pointer */
    176  1.66  dholland 	FS		*f_fs;		/* pointer to super-block */
    177  1.35       dsl 	struct ufs_dinode	f_di;		/* copy of on-disk inode */
    178  1.40       dsl 	uint		f_nishift;	/* for blocks in indirect block */
    179  1.40       dsl 	indp_t		f_ind_cache_block;
    180  1.40       dsl 	indp_t		f_ind_cache[IND_CACHE_SZ];
    181  1.40       dsl 
    182   1.1    brezak 	char		*f_buf;		/* buffer for data block */
    183  1.13       cgd 	size_t		f_buf_size;	/* size of data block */
    184   1.1    brezak 	daddr_t		f_buf_blkno;	/* block number of data block */
    185   1.1    brezak };
    186   1.1    brezak 
    187  1.45  christos static int read_inode(ino32_t, struct open_file *);
    188  1.40       dsl static int block_map(struct open_file *, indp_t, indp_t *);
    189  1.38       dsl static int buf_read_file(struct open_file *, char **, size_t *);
    190  1.45  christos static int search_directory(const char *, int, struct open_file *, ino32_t *);
    191  1.35       dsl #ifdef LIBSA_FFSv1
    192  1.66  dholland static void ffs_oldfscompat(FS *);
    193  1.35       dsl #endif
    194  1.35       dsl #ifdef LIBSA_FFSv2
    195  1.66  dholland static int ffs_find_superblock(struct open_file *, FS *);
    196  1.35       dsl #endif
    197  1.35       dsl 
    198  1.56   tsutsui 
    199  1.35       dsl #ifdef LIBSA_LFS
    200  1.35       dsl /*
    201  1.35       dsl  * Find an inode's block.  Look it up in the ifile.  Whee!
    202  1.35       dsl  */
    203  1.35       dsl static int
    204  1.45  christos find_inode_sector(ino32_t inumber, struct open_file *f, daddr_t *isp)
    205  1.35       dsl {
    206  1.35       dsl 	struct file *fp = (struct file *)f->f_fsdata;
    207  1.66  dholland 	FS *fs = fp->f_fs;
    208  1.35       dsl 	daddr_t ifileent_blkno;
    209  1.35       dsl 	char *ent_in_buf;
    210  1.35       dsl 	size_t buf_after_ent;
    211  1.35       dsl 	int rc;
    212  1.33      fvdl 
    213  1.67  dholland 	rc = read_inode(lfs_sb_getifile(fs), f);
    214  1.35       dsl 	if (rc)
    215  1.51     isaki 		return rc;
    216  1.35       dsl 
    217  1.35       dsl 	ifileent_blkno =
    218  1.67  dholland 	    (inumber / lfs_sb_getifpb(fs)) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs);
    219  1.67  dholland 	fp->f_seekp = (off_t)ifileent_blkno * lfs_sb_getbsize(fs) +
    220  1.67  dholland 	    (inumber % lfs_sb_getifpb(fs)) * sizeof (IFILE_Vx);
    221  1.35       dsl 	rc = buf_read_file(f, &ent_in_buf, &buf_after_ent);
    222  1.35       dsl 	if (rc)
    223  1.51     isaki 		return rc;
    224  1.35       dsl 	/* make sure something's not badly wrong, but don't panic. */
    225  1.35       dsl 	if (buf_after_ent < sizeof (IFILE_Vx))
    226  1.51     isaki 		return EINVAL;
    227  1.35       dsl 
    228  1.35       dsl 	*isp = FSBTODB(fs, ((IFILE_Vx *)ent_in_buf)->if_daddr);
    229  1.35       dsl 	if (*isp == LFS_UNUSED_DADDR)	/* again, something badly wrong */
    230  1.51     isaki 		return EINVAL;
    231  1.51     isaki 	return 0;
    232  1.35       dsl }
    233   1.7    brezak #endif
    234   1.7    brezak 
    235   1.1    brezak /*
    236   1.1    brezak  * Read a new inode into a file structure.
    237   1.1    brezak  */
    238   1.1    brezak static int
    239  1.45  christos read_inode(ino32_t inumber, struct open_file *f)
    240   1.1    brezak {
    241  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    242  1.66  dholland 	FS *fs = fp->f_fs;
    243   1.1    brezak 	char *buf;
    244  1.49       mrg 	size_t rsize;
    245   1.1    brezak 	int rc;
    246  1.64  christos 	daddr_t inode_sector = 0; /* XXX: gcc */
    247  1.35       dsl #ifdef LIBSA_LFS
    248  1.35       dsl 	struct ufs_dinode *dip;
    249  1.35       dsl 	int cnt;
    250  1.35       dsl #endif
    251  1.35       dsl 
    252  1.35       dsl #ifdef LIBSA_LFS
    253  1.67  dholland 	if (inumber == lfs_sb_getifile(fs))
    254  1.67  dholland 		inode_sector = FSBTODB(fs, lfs_sb_getidaddr(fs));
    255  1.35       dsl 	else if ((rc = find_inode_sector(inumber, f, &inode_sector)) != 0)
    256  1.51     isaki 		return rc;
    257  1.35       dsl #else
    258  1.35       dsl 	inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
    259  1.35       dsl #endif
    260   1.1    brezak 
    261   1.1    brezak 	/*
    262   1.1    brezak 	 * Read inode and save it.
    263   1.1    brezak 	 */
    264  1.40       dsl 	buf = fp->f_buf;
    265  1.11   mycroft 	twiddle();
    266  1.23       cgd 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    267  1.40       dsl 	    inode_sector, fs->fs_bsize, buf, &rsize);
    268   1.1    brezak 	if (rc)
    269  1.40       dsl 		return rc;
    270  1.49       mrg 	if (rsize != fs->fs_bsize)
    271  1.40       dsl 		return EIO;
    272   1.1    brezak 
    273  1.35       dsl #ifdef LIBSA_LFS
    274  1.35       dsl 	cnt = INOPBx(fs);
    275  1.40       dsl 	dip = (struct ufs_dinode *)buf + (cnt - 1);
    276  1.40       dsl 	for (; dip->di_inumber != inumber; --dip) {
    277  1.40       dsl 		/* kernel code panics, but boot blocks which panic are Bad. */
    278  1.40       dsl 		if (--cnt == 0)
    279  1.40       dsl 			return EINVAL;
    280   1.1    brezak 	}
    281  1.35       dsl 	fp->f_di = *dip;
    282  1.35       dsl #else
    283  1.35       dsl 	fp->f_di = ((struct ufs_dinode *)buf)[ino_to_fsbo(fs, inumber)];
    284  1.35       dsl #endif
    285   1.1    brezak 
    286   1.1    brezak 	/*
    287   1.1    brezak 	 * Clear out the old buffers
    288   1.1    brezak 	 */
    289  1.40       dsl 	fp->f_ind_cache_block = ~0;
    290  1.40       dsl 	fp->f_buf_blkno = -1;
    291  1.51     isaki 	return rc;
    292   1.1    brezak }
    293   1.1    brezak 
    294   1.1    brezak /*
    295   1.1    brezak  * Given an offset in a file, find the disk block number that
    296   1.1    brezak  * contains that block.
    297   1.1    brezak  */
    298   1.1    brezak static int
    299  1.40       dsl block_map(struct open_file *f, indp_t file_block, indp_t *disk_block_p)
    300   1.1    brezak {
    301  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    302  1.66  dholland 	FS *fs = fp->f_fs;
    303  1.52   tsutsui 	uint level;
    304  1.40       dsl 	indp_t ind_cache;
    305  1.40       dsl 	indp_t ind_block_num;
    306  1.49       mrg 	size_t rsize;
    307   1.1    brezak 	int rc;
    308  1.40       dsl 	indp_t *buf = (void *)fp->f_buf;
    309   1.1    brezak 
    310   1.1    brezak 	/*
    311   1.1    brezak 	 * Index structure of an inode:
    312   1.1    brezak 	 *
    313  1.59  dholland 	 * di_db[0..UFS_NDADDR-1]	hold block numbers for blocks
    314  1.59  dholland 	 *			0..UFS_NDADDR-1
    315   1.1    brezak 	 *
    316   1.1    brezak 	 * di_ib[0]		index block 0 is the single indirect block
    317   1.1    brezak 	 *			holds block numbers for blocks
    318  1.61  dholland 	 *			UFS_NDADDR .. UFS_NDADDR + UFS_NINDIR(fs)-1
    319   1.1    brezak 	 *
    320   1.1    brezak 	 * di_ib[1]		index block 1 is the double indirect block
    321   1.1    brezak 	 *			holds block numbers for INDEX blocks for blocks
    322  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) ..
    323  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2 - 1
    324   1.1    brezak 	 *
    325   1.1    brezak 	 * di_ib[2]		index block 2 is the triple indirect block
    326   1.1    brezak 	 *			holds block numbers for double-indirect
    327   1.1    brezak 	 *			blocks for blocks
    328  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2 ..
    329  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2
    330  1.61  dholland 	 *				+ UFS_NINDIR(fs)**3 - 1
    331   1.1    brezak 	 */
    332   1.1    brezak 
    333  1.59  dholland 	if (file_block < UFS_NDADDR) {
    334   1.1    brezak 		/* Direct block. */
    335  1.35       dsl 		*disk_block_p = fp->f_di.di_db[file_block];
    336  1.51     isaki 		return 0;
    337   1.1    brezak 	}
    338   1.1    brezak 
    339  1.59  dholland 	file_block -= UFS_NDADDR;
    340   1.1    brezak 
    341  1.40       dsl 	ind_cache = file_block >> LN2_IND_CACHE_SZ;
    342  1.40       dsl 	if (ind_cache == fp->f_ind_cache_block) {
    343  1.40       dsl 		*disk_block_p = fp->f_ind_cache[file_block & IND_CACHE_MASK];
    344  1.40       dsl 		return 0;
    345  1.40       dsl 	}
    346  1.40       dsl 
    347  1.40       dsl 	for (level = 0;;) {
    348  1.40       dsl 		level += fp->f_nishift;
    349  1.40       dsl 		if (file_block < (indp_t)1 << level)
    350  1.40       dsl 			break;
    351  1.59  dholland 		if (level > UFS_NIADDR * fp->f_nishift)
    352  1.38       dsl 			/* Block number too high */
    353  1.51     isaki 			return EFBIG;
    354  1.40       dsl 		file_block -= (indp_t)1 << level;
    355   1.1    brezak 	}
    356   1.1    brezak 
    357  1.40       dsl 	ind_block_num = fp->f_di.di_ib[level / fp->f_nishift - 1];
    358   1.1    brezak 
    359  1.40       dsl 	for (;;) {
    360  1.40       dsl 		level -= fp->f_nishift;
    361   1.1    brezak 		if (ind_block_num == 0) {
    362   1.1    brezak 			*disk_block_p = 0;	/* missing */
    363  1.51     isaki 			return 0;
    364   1.1    brezak 		}
    365   1.1    brezak 
    366  1.40       dsl 		twiddle();
    367  1.40       dsl 		/*
    368  1.40       dsl 		 * If we were feeling brave, we could work out the number
    369  1.40       dsl 		 * of the disk sector and read a single disk sector instead
    370  1.40       dsl 		 * of a filesystem block.
    371  1.40       dsl 		 * However we don't do this very often anyway...
    372  1.40       dsl 		 */
    373  1.40       dsl 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    374  1.40       dsl 			FSBTODB(fp->f_fs, ind_block_num), fs->fs_bsize,
    375  1.40       dsl 			buf, &rsize);
    376  1.40       dsl 		if (rc)
    377  1.51     isaki 			return rc;
    378  1.49       mrg 		if (rsize != fs->fs_bsize)
    379  1.40       dsl 			return EIO;
    380  1.40       dsl 		ind_block_num = buf[file_block >> level];
    381  1.40       dsl 		if (level == 0)
    382  1.40       dsl 			break;
    383  1.40       dsl 		file_block &= (1 << level) - 1;
    384  1.40       dsl 	}
    385   1.1    brezak 
    386  1.40       dsl 	/* Save the part of the block that contains this sector */
    387  1.40       dsl 	memcpy(fp->f_ind_cache, &buf[file_block & ~IND_CACHE_MASK],
    388  1.40       dsl 	    IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
    389  1.40       dsl 	fp->f_ind_cache_block = ind_cache;
    390   1.1    brezak 
    391   1.1    brezak 	*disk_block_p = ind_block_num;
    392   1.1    brezak 
    393  1.51     isaki 	return 0;
    394   1.1    brezak }
    395   1.1    brezak 
    396   1.1    brezak /*
    397  1.40       dsl  * Read a portion of a file into an internal buffer.
    398  1.40       dsl  * Return the location in the buffer and the amount in the buffer.
    399   1.1    brezak  */
    400   1.1    brezak static int
    401  1.37       dsl buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
    402   1.1    brezak {
    403  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    404  1.66  dholland 	FS *fs = fp->f_fs;
    405   1.1    brezak 	long off;
    406  1.40       dsl 	indp_t file_block;
    407  1.13       cgd 	size_t block_size;
    408   1.1    brezak 	int rc;
    409   1.1    brezak 
    410  1.61  dholland 	off = ufs_blkoff(fs, fp->f_seekp);
    411  1.63  dholland 	file_block = ufs_lblkno(fs, fp->f_seekp);
    412  1.35       dsl #ifdef LIBSA_LFS
    413  1.35       dsl 	block_size = dblksize(fs, &fp->f_di, file_block);
    414  1.35       dsl #else
    415  1.61  dholland 	block_size = ffs_sblksize(fs, (int64_t)fp->f_di.di_size, file_block);
    416  1.35       dsl #endif
    417   1.1    brezak 
    418   1.1    brezak 	if (file_block != fp->f_buf_blkno) {
    419  1.64  christos 		indp_t disk_block = 0; /* XXX: gcc */
    420   1.1    brezak 		rc = block_map(f, file_block, &disk_block);
    421   1.1    brezak 		if (rc)
    422  1.51     isaki 			return rc;
    423   1.1    brezak 
    424   1.1    brezak 		if (disk_block == 0) {
    425  1.41       dsl 			memset(fp->f_buf, 0, block_size);
    426   1.1    brezak 			fp->f_buf_size = block_size;
    427   1.1    brezak 		} else {
    428  1.11   mycroft 			twiddle();
    429  1.23       cgd 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    430  1.35       dsl 				FSBTODB(fs, disk_block),
    431   1.1    brezak 				block_size, fp->f_buf, &fp->f_buf_size);
    432   1.1    brezak 			if (rc)
    433  1.51     isaki 				return rc;
    434   1.1    brezak 		}
    435   1.1    brezak 
    436   1.1    brezak 		fp->f_buf_blkno = file_block;
    437   1.1    brezak 	}
    438   1.1    brezak 
    439   1.1    brezak 	/*
    440   1.1    brezak 	 * Return address of byte in buffer corresponding to
    441   1.1    brezak 	 * offset, and size of remainder of buffer after that
    442   1.1    brezak 	 * byte.
    443   1.1    brezak 	 */
    444   1.1    brezak 	*buf_p = fp->f_buf + off;
    445   1.1    brezak 	*size_p = block_size - off;
    446   1.1    brezak 
    447   1.1    brezak 	/*
    448   1.1    brezak 	 * But truncate buffer at end of file.
    449   1.1    brezak 	 */
    450  1.35       dsl 	if (*size_p > fp->f_di.di_size - fp->f_seekp)
    451  1.35       dsl 		*size_p = fp->f_di.di_size - fp->f_seekp;
    452   1.1    brezak 
    453  1.51     isaki 	return 0;
    454   1.1    brezak }
    455   1.1    brezak 
    456   1.1    brezak /*
    457   1.1    brezak  * Search a directory for a name and return its
    458  1.38       dsl  * inode number.
    459   1.1    brezak  */
    460   1.1    brezak static int
    461  1.38       dsl search_directory(const char *name, int length, struct open_file *f,
    462  1.51     isaki 	ino32_t *inumber_p)
    463   1.1    brezak {
    464  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    465  1.30  augustss 	struct direct *dp;
    466   1.1    brezak 	struct direct *edp;
    467   1.1    brezak 	char *buf;
    468  1.13       cgd 	size_t buf_size;
    469  1.38       dsl 	int namlen;
    470   1.1    brezak 	int rc;
    471   1.1    brezak 
    472   1.1    brezak 	fp->f_seekp = 0;
    473  1.42      fvdl 	while (fp->f_seekp < (off_t)fp->f_di.di_size) {
    474   1.1    brezak 		rc = buf_read_file(f, &buf, &buf_size);
    475   1.1    brezak 		if (rc)
    476  1.51     isaki 			return rc;
    477   1.1    brezak 
    478   1.1    brezak 		dp = (struct direct *)buf;
    479   1.1    brezak 		edp = (struct direct *)(buf + buf_size);
    480  1.38       dsl 		for (;dp < edp; dp = (void *)((char *)dp + dp->d_reclen)) {
    481  1.40       dsl 			if (dp->d_reclen <= 0)
    482  1.40       dsl 				break;
    483  1.45  christos 			if (dp->d_ino == (ino32_t)0)
    484  1.38       dsl 				continue;
    485   1.1    brezak #if BYTE_ORDER == LITTLE_ENDIAN
    486   1.1    brezak 			if (fp->f_fs->fs_maxsymlinklen <= 0)
    487   1.1    brezak 				namlen = dp->d_type;
    488   1.1    brezak 			else
    489   1.1    brezak #endif
    490   1.1    brezak 				namlen = dp->d_namlen;
    491   1.1    brezak 			if (namlen == length &&
    492  1.38       dsl 			    !memcmp(name, dp->d_name, length)) {
    493   1.1    brezak 				/* found entry */
    494   1.1    brezak 				*inumber_p = dp->d_ino;
    495  1.51     isaki 				return 0;
    496   1.1    brezak 			}
    497   1.1    brezak 		}
    498   1.1    brezak 		fp->f_seekp += buf_size;
    499   1.1    brezak 	}
    500  1.51     isaki 	return ENOENT;
    501   1.1    brezak }
    502   1.1    brezak 
    503  1.35       dsl #ifdef LIBSA_FFSv2
    504  1.33      fvdl 
    505  1.33      fvdl daddr_t sblock_try[] = SBLOCKSEARCH;
    506  1.33      fvdl 
    507  1.33      fvdl static int
    508  1.66  dholland ffs_find_superblock(struct open_file *f, FS *fs)
    509  1.33      fvdl {
    510  1.33      fvdl 	int i, rc;
    511  1.33      fvdl 	size_t buf_size;
    512  1.33      fvdl 
    513  1.33      fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
    514  1.33      fvdl 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    515  1.40       dsl 		    sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, fs, &buf_size);
    516  1.33      fvdl 		if (rc != 0 || buf_size != SBLOCKSIZE)
    517  1.33      fvdl 			return rc;
    518  1.43       dsl 		if (fs->fs_sblockloc != sblock_try[i])
    519  1.43       dsl 			/* an alternate superblock - try again */
    520  1.43       dsl 			continue;
    521  1.33      fvdl 		if (fs->fs_magic == FS_UFS2_MAGIC) {
    522  1.33      fvdl 			return 0;
    523  1.33      fvdl 		}
    524  1.33      fvdl 	}
    525  1.33      fvdl 	return EINVAL;
    526  1.33      fvdl }
    527  1.33      fvdl 
    528  1.33      fvdl #endif
    529  1.33      fvdl 
    530   1.1    brezak /*
    531   1.1    brezak  * Open a file.
    532   1.1    brezak  */
    533  1.55     joerg __compactcall int
    534  1.38       dsl ufs_open(const char *path, struct open_file *f)
    535   1.1    brezak {
    536  1.23       cgd #ifndef LIBSA_FS_SINGLECOMPONENT
    537  1.38       dsl 	const char *cp, *ncp;
    538  1.30  augustss 	int c;
    539  1.23       cgd #endif
    540  1.45  christos 	ino32_t inumber;
    541   1.1    brezak 	struct file *fp;
    542  1.66  dholland 	FS *fs;
    543  1.10        ws 	int rc;
    544  1.23       cgd #ifndef LIBSA_NO_FS_SYMLINK
    545  1.45  christos 	ino32_t parent_inumber;
    546  1.10        ws 	int nlinks = 0;
    547   1.1    brezak 	char namebuf[MAXPATHLEN+1];
    548  1.40       dsl 	char *buf;
    549  1.22    simonb #endif
    550   1.1    brezak 
    551   1.1    brezak 	/* allocate file system specific data structure */
    552   1.1    brezak 	fp = alloc(sizeof(struct file));
    553  1.41       dsl 	memset(fp, 0, sizeof(struct file));
    554   1.1    brezak 	f->f_fsdata = (void *)fp;
    555   1.1    brezak 
    556   1.1    brezak 	/* allocate space and read super block */
    557  1.33      fvdl 	fs = alloc(SBLOCKSIZE);
    558   1.1    brezak 	fp->f_fs = fs;
    559  1.11   mycroft 	twiddle();
    560  1.33      fvdl 
    561  1.35       dsl #ifdef LIBSA_FFSv2
    562  1.33      fvdl 	rc = ffs_find_superblock(f, fs);
    563  1.33      fvdl 	if (rc)
    564  1.33      fvdl 		goto out;
    565  1.33      fvdl #else
    566  1.35       dsl 	{
    567  1.35       dsl 		size_t buf_size;
    568  1.35       dsl 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    569  1.40       dsl 			SBLOCKOFFSET / DEV_BSIZE, SBLOCKSIZE, fs, &buf_size);
    570  1.35       dsl 		if (rc)
    571  1.35       dsl 			goto out;
    572  1.35       dsl 		if (buf_size != SBLOCKSIZE ||
    573  1.35       dsl #ifdef LIBSA_FFS
    574  1.35       dsl 		    fs->lfs_version != REQUIRED_LFS_VERSION ||
    575  1.35       dsl #endif
    576  1.35       dsl 		    fs->fs_magic != FS_MAGIC) {
    577  1.35       dsl 			rc = EINVAL;
    578  1.35       dsl 			goto out;
    579  1.35       dsl 		}
    580  1.33      fvdl 	}
    581  1.35       dsl #if defined(LIBSA_LFS) && REQUIRED_LFS_VERSION == 2
    582  1.35       dsl 	/*
    583  1.35       dsl 	 * XXX	We should check the second superblock and use the eldest
    584  1.35       dsl 	 *	of the two.  See comments near the top of lfs_mountfs()
    585  1.35       dsl 	 *	in sys/ufs/lfs/lfs_vfsops.c.
    586  1.35       dsl 	 *      This may need a LIBSA_LFS_SMALL check as well.
    587  1.35       dsl 	 */
    588  1.35       dsl #endif
    589  1.33      fvdl #endif
    590   1.1    brezak 
    591  1.35       dsl #ifdef LIBSA_FFSv1
    592  1.33      fvdl 	ffs_oldfscompat(fs);
    593  1.35       dsl #endif
    594  1.33      fvdl 
    595  1.42      fvdl 	if (fs->fs_bsize > MAXBSIZE ||
    596  1.66  dholland 	    (size_t)fs->fs_bsize < sizeof(FS)) {
    597   1.1    brezak 		rc = EINVAL;
    598   1.1    brezak 		goto out;
    599   1.1    brezak 	}
    600   1.1    brezak 
    601   1.1    brezak 	/*
    602   1.1    brezak 	 * Calculate indirect block levels.
    603   1.1    brezak 	 */
    604   1.1    brezak 	{
    605  1.40       dsl 		indp_t mult;
    606  1.35       dsl 		int ln2;
    607  1.35       dsl 
    608  1.35       dsl 		/*
    609  1.35       dsl 		 * We note that the number of indirect blocks is always
    610  1.35       dsl 		 * a power of 2.  This lets us use shifts and masks instead
    611  1.35       dsl 		 * of divide and remainder and avoinds pulling in the
    612  1.35       dsl 		 * 64bit division routine into the boot code.
    613  1.35       dsl 		 */
    614  1.61  dholland 		mult = UFS_NINDIR(fs);
    615  1.35       dsl #ifdef DEBUG
    616  1.35       dsl 		if (mult & (mult - 1)) {
    617  1.35       dsl 			/* Hummm was't a power of 2 */
    618  1.35       dsl 			rc = EINVAL;
    619  1.35       dsl 			goto out;
    620  1.35       dsl 		}
    621  1.35       dsl #endif
    622  1.35       dsl 		for (ln2 = 0; mult != 1; ln2++)
    623  1.35       dsl 			mult >>= 1;
    624   1.1    brezak 
    625  1.40       dsl 		fp->f_nishift = ln2;
    626   1.1    brezak 	}
    627   1.1    brezak 
    628  1.40       dsl 	/* alloc a block sized buffer used for all fs transfers */
    629  1.40       dsl 	fp->f_buf = alloc(fs->fs_bsize);
    630  1.59  dholland 	inumber = UFS_ROOTINO;
    631   1.1    brezak 	if ((rc = read_inode(inumber, f)) != 0)
    632   1.1    brezak 		goto out;
    633   1.1    brezak 
    634  1.23       cgd #ifndef LIBSA_FS_SINGLECOMPONENT
    635   1.1    brezak 	cp = path;
    636   1.1    brezak 	while (*cp) {
    637   1.1    brezak 
    638   1.1    brezak 		/*
    639   1.1    brezak 		 * Remove extra separators
    640   1.1    brezak 		 */
    641   1.1    brezak 		while (*cp == '/')
    642   1.1    brezak 			cp++;
    643   1.1    brezak 		if (*cp == '\0')
    644   1.1    brezak 			break;
    645   1.1    brezak 
    646   1.1    brezak 		/*
    647   1.1    brezak 		 * Check that current node is a directory.
    648   1.1    brezak 		 */
    649  1.35       dsl 		if ((fp->f_di.di_mode & IFMT) != IFDIR) {
    650   1.1    brezak 			rc = ENOTDIR;
    651   1.1    brezak 			goto out;
    652   1.1    brezak 		}
    653   1.1    brezak 
    654   1.1    brezak 		/*
    655   1.1    brezak 		 * Get next component of path name.
    656   1.1    brezak 		 */
    657  1.38       dsl 		ncp = cp;
    658  1.38       dsl 		while ((c = *cp) != '\0' && c != '/')
    659  1.38       dsl 			cp++;
    660   1.1    brezak 
    661   1.1    brezak 		/*
    662   1.1    brezak 		 * Look up component in current directory.
    663   1.1    brezak 		 * Save directory inumber in case we find a
    664   1.1    brezak 		 * symbolic link.
    665   1.1    brezak 		 */
    666  1.23       cgd #ifndef LIBSA_NO_FS_SYMLINK
    667   1.1    brezak 		parent_inumber = inumber;
    668  1.23       cgd #endif
    669  1.38       dsl 		rc = search_directory(ncp, cp - ncp, f, &inumber);
    670   1.1    brezak 		if (rc)
    671   1.1    brezak 			goto out;
    672   1.1    brezak 
    673   1.1    brezak 		/*
    674   1.1    brezak 		 * Open next component.
    675   1.1    brezak 		 */
    676   1.1    brezak 		if ((rc = read_inode(inumber, f)) != 0)
    677   1.1    brezak 			goto out;
    678   1.1    brezak 
    679  1.23       cgd #ifndef LIBSA_NO_FS_SYMLINK
    680   1.1    brezak 		/*
    681   1.1    brezak 		 * Check for symbolic link.
    682   1.1    brezak 		 */
    683  1.35       dsl 		if ((fp->f_di.di_mode & IFMT) == IFLNK) {
    684  1.35       dsl 			int link_len = fp->f_di.di_size;
    685  1.10        ws 			int len;
    686  1.10        ws 
    687  1.10        ws 			len = strlen(cp);
    688   1.1    brezak 
    689  1.10        ws 			if (link_len + len > MAXPATHLEN ||
    690  1.10        ws 			    ++nlinks > MAXSYMLINKS) {
    691   1.1    brezak 				rc = ENOENT;
    692   1.1    brezak 				goto out;
    693   1.1    brezak 			}
    694   1.1    brezak 
    695  1.38       dsl 			memmove(&namebuf[link_len], cp, len + 1);
    696   1.1    brezak 
    697   1.9        pk 			if (link_len < fs->fs_maxsymlinklen) {
    698  1.38       dsl 				memcpy(namebuf, fp->f_di.di_db, link_len);
    699   1.1    brezak 			} else {
    700   1.1    brezak 				/*
    701   1.1    brezak 				 * Read file for symbolic link
    702   1.1    brezak 				 */
    703  1.13       cgd 				size_t buf_size;
    704  1.40       dsl 				indp_t	disk_block;
    705   1.9        pk 
    706  1.40       dsl 				buf = fp->f_buf;
    707  1.40       dsl 				rc = block_map(f, (indp_t)0, &disk_block);
    708   1.9        pk 				if (rc)
    709   1.9        pk 					goto out;
    710  1.22    simonb 
    711  1.11   mycroft 				twiddle();
    712  1.23       cgd 				rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
    713  1.35       dsl 					F_READ, FSBTODB(fs, disk_block),
    714   1.9        pk 					fs->fs_bsize, buf, &buf_size);
    715   1.1    brezak 				if (rc)
    716   1.1    brezak 					goto out;
    717   1.1    brezak 
    718  1.38       dsl 				memcpy(namebuf, buf, link_len);
    719   1.1    brezak 			}
    720   1.1    brezak 
    721   1.1    brezak 			/*
    722   1.1    brezak 			 * If relative pathname, restart at parent directory.
    723   1.1    brezak 			 * If absolute pathname, restart at root.
    724   1.1    brezak 			 */
    725   1.1    brezak 			cp = namebuf;
    726   1.1    brezak 			if (*cp != '/')
    727   1.1    brezak 				inumber = parent_inumber;
    728   1.1    brezak 			else
    729  1.59  dholland 				inumber = (ino32_t)UFS_ROOTINO;
    730   1.1    brezak 
    731   1.9        pk 			if ((rc = read_inode(inumber, f)) != 0)
    732   1.1    brezak 				goto out;
    733   1.1    brezak 		}
    734  1.23       cgd #endif	/* !LIBSA_NO_FS_SYMLINK */
    735   1.1    brezak 	}
    736   1.1    brezak 
    737   1.1    brezak 	/*
    738   1.1    brezak 	 * Found terminal component.
    739   1.1    brezak 	 */
    740   1.1    brezak 	rc = 0;
    741  1.23       cgd 
    742  1.23       cgd #else /* !LIBSA_FS_SINGLECOMPONENT */
    743  1.23       cgd 
    744  1.23       cgd 	/* look up component in the current (root) directory */
    745  1.38       dsl 	rc = search_directory(path, strlen(path), f, &inumber);
    746  1.23       cgd 	if (rc)
    747  1.23       cgd 		goto out;
    748  1.23       cgd 
    749  1.23       cgd 	/* open it */
    750  1.23       cgd 	rc = read_inode(inumber, f);
    751  1.23       cgd 
    752  1.23       cgd #endif /* !LIBSA_FS_SINGLECOMPONENT */
    753  1.26       cgd 
    754  1.51     isaki 	fp->f_seekp = 0;		/* reset seek pointer */
    755  1.23       cgd 
    756   1.1    brezak out:
    757  1.40       dsl 	if (rc)
    758  1.38       dsl 		ufs_close(f);
    759  1.58       dsl #ifdef FSMOD		/* Only defined for lfs */
    760  1.58       dsl 	else
    761  1.54        ad 		fsmod = FSMOD;
    762  1.54        ad #endif
    763  1.51     isaki 	return rc;
    764   1.1    brezak }
    765   1.1    brezak 
    766  1.55     joerg __compactcall int
    767  1.37       dsl ufs_close(struct open_file *f)
    768   1.1    brezak {
    769  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    770   1.1    brezak 
    771  1.38       dsl 	f->f_fsdata = NULL;
    772  1.38       dsl 	if (fp == NULL)
    773  1.51     isaki 		return 0;
    774   1.1    brezak 
    775   1.1    brezak 	if (fp->f_buf)
    776  1.48  christos 		dealloc(fp->f_buf, fp->f_fs->fs_bsize);
    777  1.48  christos 	dealloc(fp->f_fs, SBLOCKSIZE);
    778  1.48  christos 	dealloc(fp, sizeof(struct file));
    779  1.51     isaki 	return 0;
    780   1.1    brezak }
    781   1.1    brezak 
    782   1.1    brezak /*
    783   1.1    brezak  * Copy a portion of a file into kernel memory.
    784   1.1    brezak  * Cross block boundaries when necessary.
    785   1.1    brezak  */
    786  1.55     joerg __compactcall int
    787  1.37       dsl ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
    788   1.1    brezak {
    789  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    790  1.30  augustss 	size_t csize;
    791   1.1    brezak 	char *buf;
    792  1.13       cgd 	size_t buf_size;
    793   1.1    brezak 	int rc = 0;
    794  1.30  augustss 	char *addr = start;
    795   1.1    brezak 
    796   1.1    brezak 	while (size != 0) {
    797  1.42      fvdl 		if (fp->f_seekp >= (off_t)fp->f_di.di_size)
    798   1.1    brezak 			break;
    799   1.1    brezak 
    800   1.1    brezak 		rc = buf_read_file(f, &buf, &buf_size);
    801   1.1    brezak 		if (rc)
    802   1.1    brezak 			break;
    803   1.1    brezak 
    804   1.1    brezak 		csize = size;
    805   1.1    brezak 		if (csize > buf_size)
    806   1.1    brezak 			csize = buf_size;
    807   1.1    brezak 
    808  1.38       dsl 		memcpy(addr, buf, csize);
    809   1.1    brezak 
    810   1.1    brezak 		fp->f_seekp += csize;
    811  1.12        pk 		addr += csize;
    812   1.1    brezak 		size -= csize;
    813   1.1    brezak 	}
    814   1.1    brezak 	if (resid)
    815   1.1    brezak 		*resid = size;
    816  1.51     isaki 	return rc;
    817   1.1    brezak }
    818   1.1    brezak 
    819   1.1    brezak /*
    820   1.1    brezak  * Not implemented.
    821   1.1    brezak  */
    822  1.23       cgd #ifndef LIBSA_NO_FS_WRITE
    823  1.55     joerg __compactcall int
    824  1.37       dsl ufs_write(struct open_file *f, void *start, size_t size, size_t *resid)
    825   1.1    brezak {
    826   1.1    brezak 
    827  1.51     isaki 	return EROFS;
    828   1.1    brezak }
    829  1.23       cgd #endif /* !LIBSA_NO_FS_WRITE */
    830   1.1    brezak 
    831  1.23       cgd #ifndef LIBSA_NO_FS_SEEK
    832  1.55     joerg __compactcall off_t
    833  1.37       dsl ufs_seek(struct open_file *f, off_t offset, int where)
    834   1.1    brezak {
    835  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    836   1.1    brezak 
    837   1.1    brezak 	switch (where) {
    838   1.1    brezak 	case SEEK_SET:
    839   1.1    brezak 		fp->f_seekp = offset;
    840   1.1    brezak 		break;
    841   1.1    brezak 	case SEEK_CUR:
    842   1.1    brezak 		fp->f_seekp += offset;
    843   1.1    brezak 		break;
    844   1.1    brezak 	case SEEK_END:
    845  1.35       dsl 		fp->f_seekp = fp->f_di.di_size - offset;
    846   1.1    brezak 		break;
    847   1.1    brezak 	default:
    848  1.51     isaki 		return -1;
    849   1.1    brezak 	}
    850  1.51     isaki 	return fp->f_seekp;
    851   1.1    brezak }
    852  1.23       cgd #endif /* !LIBSA_NO_FS_SEEK */
    853   1.1    brezak 
    854  1.55     joerg __compactcall int
    855  1.37       dsl ufs_stat(struct open_file *f, struct stat *sb)
    856   1.1    brezak {
    857  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    858   1.1    brezak 
    859   1.1    brezak 	/* only important stuff */
    860  1.40       dsl 	memset(sb, 0, sizeof *sb);
    861  1.35       dsl 	sb->st_mode = fp->f_di.di_mode;
    862  1.35       dsl 	sb->st_uid = fp->f_di.di_uid;
    863  1.35       dsl 	sb->st_gid = fp->f_di.di_gid;
    864  1.35       dsl 	sb->st_size = fp->f_di.di_size;
    865  1.51     isaki 	return 0;
    866   1.4        pk }
    867   1.4        pk 
    868  1.56   tsutsui #if defined(LIBSA_ENABLE_LS_OP)
    869  1.65  christos 
    870  1.65  christos #include "ls.h"
    871  1.65  christos 
    872  1.65  christos static const char    *const typestr[] = {
    873  1.65  christos 	"unknown",
    874  1.65  christos 	"FIFO",
    875  1.65  christos 	"CHR",
    876  1.65  christos 	0,
    877  1.65  christos 	"DIR",
    878  1.65  christos 	0,
    879  1.65  christos 	"BLK",
    880  1.65  christos 	0,
    881  1.65  christos 	"REG",
    882  1.65  christos 	0,
    883  1.65  christos 	"LNK",
    884  1.65  christos 	0,
    885  1.65  christos 	"SOCK",
    886  1.65  christos 	0,
    887  1.65  christos 	"WHT"
    888  1.65  christos };
    889  1.65  christos 
    890  1.56   tsutsui __compactcall void
    891  1.56   tsutsui ufs_ls(struct open_file *f, const char *pattern)
    892  1.56   tsutsui {
    893  1.56   tsutsui 	struct file *fp = (struct file *)f->f_fsdata;
    894  1.56   tsutsui 	char *buf;
    895  1.56   tsutsui 	size_t buf_size;
    896  1.65  christos 	lsentry_t *names = NULL;
    897  1.56   tsutsui 
    898  1.56   tsutsui 	fp->f_seekp = 0;
    899  1.56   tsutsui 	while (fp->f_seekp < (off_t)fp->f_di.di_size) {
    900  1.56   tsutsui 		struct direct  *dp, *edp;
    901  1.56   tsutsui 		int rc = buf_read_file(f, &buf, &buf_size);
    902  1.56   tsutsui 		if (rc)
    903  1.56   tsutsui 			goto out;
    904  1.56   tsutsui 		/* some firmware might use block size larger than DEV_BSIZE */
    905  1.60  dholland 		if (buf_size < UFS_DIRBLKSIZ)
    906  1.56   tsutsui 			goto out;
    907  1.56   tsutsui 
    908  1.56   tsutsui 		dp = (struct direct *)buf;
    909  1.56   tsutsui 		edp = (struct direct *)(buf + buf_size);
    910  1.56   tsutsui 
    911  1.56   tsutsui 		for (; dp < edp; dp = (void *)((char *)dp + dp->d_reclen)) {
    912  1.56   tsutsui 			const char *t;
    913  1.56   tsutsui 			if (dp->d_ino ==  0)
    914  1.56   tsutsui 				continue;
    915  1.56   tsutsui 
    916  1.56   tsutsui 			if (dp->d_type >= NELEM(typestr) ||
    917  1.56   tsutsui 			    !(t = typestr[dp->d_type])) {
    918  1.56   tsutsui 				/*
    919  1.56   tsutsui 				 * This does not handle "old"
    920  1.56   tsutsui 				 * filesystems properly. On little
    921  1.56   tsutsui 				 * endian machines, we get a bogus
    922  1.56   tsutsui 				 * type name if the namlen matches a
    923  1.56   tsutsui 				 * valid type identifier. We could
    924  1.56   tsutsui 				 * check if we read namlen "0" and
    925  1.56   tsutsui 				 * handle this case specially, if
    926  1.56   tsutsui 				 * there were a pressing need...
    927  1.56   tsutsui 				 */
    928  1.56   tsutsui 				printf("bad dir entry\n");
    929  1.56   tsutsui 				goto out;
    930  1.56   tsutsui 			}
    931  1.65  christos 			lsadd(&names, pattern, dp->d_name, strlen(dp->d_name),
    932  1.65  christos 			    dp->d_ino, t);
    933  1.56   tsutsui 		}
    934  1.56   tsutsui 		fp->f_seekp += buf_size;
    935  1.56   tsutsui 	}
    936  1.65  christos 	lsprint(names);
    937  1.65  christos out:	lsfree(names);
    938  1.56   tsutsui }
    939  1.56   tsutsui #endif /* LIBSA_ENABLE_LS_OP */
    940  1.56   tsutsui 
    941  1.35       dsl #ifdef LIBSA_FFSv1
    942   1.4        pk /*
    943   1.4        pk  * Sanity checks for old file systems.
    944   1.4        pk  *
    945   1.4        pk  * XXX - goes away some day.
    946  1.40       dsl  * Stripped of stuff libsa doesn't need.....
    947   1.4        pk  */
    948  1.13       cgd static void
    949  1.66  dholland ffs_oldfscompat(FS *fs)
    950   1.4        pk {
    951   1.4        pk 
    952  1.33      fvdl #ifdef COMPAT_UFS
    953  1.50    martin 	/*
    954  1.50    martin 	 * Newer Solaris versions have a slightly incompatible
    955  1.50    martin 	 * superblock - so always calculate this values on the fly, which
    956  1.50    martin 	 * is good enough for libsa purposes
    957  1.50    martin 	 */
    958  1.50    martin 	if (fs->fs_magic == FS_UFS1_MAGIC
    959  1.50    martin #ifndef COMPAT_SOLARIS_UFS
    960  1.50    martin 	    && fs->fs_old_inodefmt < FS_44INODEFMT
    961  1.50    martin #endif
    962  1.50    martin 	    ) {
    963  1.33      fvdl 		fs->fs_qbmask = ~fs->fs_bmask;
    964  1.33      fvdl 		fs->fs_qfmask = ~fs->fs_fmask;
    965  1.33      fvdl 	}
    966  1.33      fvdl #endif
    967   1.1    brezak }
    968  1.35       dsl #endif
    969