Home | History | Annotate | Line # | Download | only in libsa
ufs.c revision 1.66
      1  1.66  dholland /*	$NetBSD: ufs.c,v 1.66 2015/07/24 06:53:24 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.35       dsl #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.66  dholland typedef struct dlfs FS;
    107  1.66  dholland #define fs_magic	dlfs_magic
    108  1.66  dholland #define fs_maxsymlinklen dlfs_maxsymlinklen
    109  1.66  dholland 
    110  1.66  dholland /* XXX these will go away in the near future */
    111  1.66  dholland #undef lfs_bmask
    112  1.66  dholland #define lfs_bmask dlfs_bmask
    113  1.66  dholland #undef lfs_ifile
    114  1.66  dholland #define lfs_ifile dlfs_ifile
    115  1.66  dholland #undef lfs_ifpb
    116  1.66  dholland #define lfs_ifpb dlfs_ifpb
    117  1.66  dholland #undef lfs_cleansz
    118  1.66  dholland #define lfs_cleansz dlfs_cleansz
    119  1.66  dholland #undef lfs_segtabsz
    120  1.66  dholland #define lfs_segtabsz dlfs_segtabsz
    121  1.66  dholland #undef lfs_idaddr
    122  1.66  dholland #define lfs_idaddr dlfs_idaddr
    123  1.66  dholland #undef lfs_inopb
    124  1.66  dholland #define lfs_inopb dlfs_inopb
    125  1.66  dholland #undef lfs_bshift
    126  1.66  dholland #define lfs_bshift dlfs_bshift
    127  1.66  dholland #undef lfs_bsize
    128  1.66  dholland #define lfs_bsize dlfs_bsize
    129  1.66  dholland #undef lfs_ffmask
    130  1.66  dholland #define lfs_ffmask dlfs_ffmask
    131  1.66  dholland #undef lfs_fsbtodb
    132  1.66  dholland #define lfs_fsbtodb dlfs_fsbtodb
    133  1.33      fvdl 
    134  1.35       dsl #define FS_MAGIC	LFS_MAGIC
    135  1.35       dsl #define SBLOCKSIZE	LFS_SBPAD
    136  1.35       dsl #define SBLOCKOFFSET	LFS_LABELPAD
    137  1.33      fvdl #else
    138  1.66  dholland /* NB ufs2 doesn't use the common superblock code... */
    139  1.66  dholland typedef struct fs FS;
    140  1.35       dsl #define FS_MAGIC	FS_UFS1_MAGIC
    141  1.35       dsl #define SBLOCKOFFSET	SBLOCK_UFS1
    142  1.35       dsl #endif
    143  1.35       dsl 
    144  1.35       dsl #if defined(LIBSA_NO_TWIDDLE)
    145  1.35       dsl #define twiddle()
    146  1.35       dsl #endif
    147  1.35       dsl 
    148  1.40       dsl #undef cgstart
    149  1.40       dsl #if defined(LIBSA_FFSv2)
    150  1.40       dsl #define cgstart(fc, c) cgstart_ufs2((fs), (c))
    151  1.40       dsl #else
    152  1.40       dsl #define cgstart(fc, c) cgstart_ufs1((fs), (c))
    153  1.40       dsl #endif
    154  1.40       dsl 
    155  1.35       dsl #ifndef ufs_dinode
    156  1.35       dsl #define ufs_dinode	ufs1_dinode
    157  1.35       dsl #endif
    158  1.35       dsl #ifndef indp_t
    159  1.47       uwe #define indp_t		int32_t
    160  1.35       dsl #endif
    161  1.45  christos typedef uint32_t	ino32_t;
    162  1.61  dholland 
    163  1.35       dsl #ifndef FSBTODB
    164  1.62  dholland #define FSBTODB(fs, indp) FFS_FSBTODB(fs, indp)
    165  1.33      fvdl #endif
    166  1.61  dholland #ifndef UFS_NINDIR
    167  1.61  dholland #define UFS_NINDIR FFS_NINDIR
    168  1.61  dholland #endif
    169  1.61  dholland #ifndef ufs_blkoff
    170  1.61  dholland #define ufs_blkoff ffs_blkoff
    171  1.61  dholland #endif
    172  1.63  dholland #ifndef ufs_lblkno
    173  1.63  dholland #define ufs_lblkno ffs_lblkno
    174  1.63  dholland #endif
    175  1.23       cgd 
    176   1.1    brezak /*
    177  1.40       dsl  * To avoid having a lot of filesystem-block sized buffers lurking (which
    178  1.40       dsl  * could be 32k) we only keep a few entries of the indirect block map.
    179  1.40       dsl  * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block
    180  1.40       dsl  * ~13 times pulling in a 6M kernel.
    181  1.40       dsl  * The cache size must be smaller than the smallest filesystem block,
    182  1.40       dsl  * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks).
    183  1.40       dsl  */
    184  1.40       dsl #define LN2_IND_CACHE_SZ	6
    185  1.40       dsl #define IND_CACHE_SZ		(1 << LN2_IND_CACHE_SZ)
    186  1.40       dsl #define IND_CACHE_MASK		(IND_CACHE_SZ - 1)
    187  1.40       dsl 
    188  1.40       dsl /*
    189   1.1    brezak  * In-core open file.
    190   1.1    brezak  */
    191   1.1    brezak struct file {
    192   1.1    brezak 	off_t		f_seekp;	/* seek pointer */
    193  1.66  dholland 	FS		*f_fs;		/* pointer to super-block */
    194  1.35       dsl 	struct ufs_dinode	f_di;		/* copy of on-disk inode */
    195  1.40       dsl 	uint		f_nishift;	/* for blocks in indirect block */
    196  1.40       dsl 	indp_t		f_ind_cache_block;
    197  1.40       dsl 	indp_t		f_ind_cache[IND_CACHE_SZ];
    198  1.40       dsl 
    199   1.1    brezak 	char		*f_buf;		/* buffer for data block */
    200  1.13       cgd 	size_t		f_buf_size;	/* size of data block */
    201   1.1    brezak 	daddr_t		f_buf_blkno;	/* block number of data block */
    202   1.1    brezak };
    203   1.1    brezak 
    204  1.45  christos static int read_inode(ino32_t, struct open_file *);
    205  1.40       dsl static int block_map(struct open_file *, indp_t, indp_t *);
    206  1.38       dsl static int buf_read_file(struct open_file *, char **, size_t *);
    207  1.45  christos static int search_directory(const char *, int, struct open_file *, ino32_t *);
    208  1.35       dsl #ifdef LIBSA_FFSv1
    209  1.66  dholland static void ffs_oldfscompat(FS *);
    210  1.35       dsl #endif
    211  1.35       dsl #ifdef LIBSA_FFSv2
    212  1.66  dholland static int ffs_find_superblock(struct open_file *, FS *);
    213  1.35       dsl #endif
    214  1.35       dsl 
    215  1.56   tsutsui 
    216  1.35       dsl #ifdef LIBSA_LFS
    217  1.35       dsl /*
    218  1.35       dsl  * Find an inode's block.  Look it up in the ifile.  Whee!
    219  1.35       dsl  */
    220  1.35       dsl static int
    221  1.45  christos find_inode_sector(ino32_t inumber, struct open_file *f, daddr_t *isp)
    222  1.35       dsl {
    223  1.35       dsl 	struct file *fp = (struct file *)f->f_fsdata;
    224  1.66  dholland 	FS *fs = fp->f_fs;
    225  1.35       dsl 	daddr_t ifileent_blkno;
    226  1.35       dsl 	char *ent_in_buf;
    227  1.35       dsl 	size_t buf_after_ent;
    228  1.35       dsl 	int rc;
    229  1.33      fvdl 
    230  1.35       dsl 	rc = read_inode(fs->lfs_ifile, f);
    231  1.35       dsl 	if (rc)
    232  1.51     isaki 		return rc;
    233  1.35       dsl 
    234  1.35       dsl 	ifileent_blkno =
    235  1.35       dsl 	    (inumber / fs->lfs_ifpb) + fs->lfs_cleansz + fs->lfs_segtabsz;
    236  1.35       dsl 	fp->f_seekp = (off_t)ifileent_blkno * fs->fs_bsize +
    237  1.35       dsl 	    (inumber % fs->lfs_ifpb) * sizeof (IFILE_Vx);
    238  1.35       dsl 	rc = buf_read_file(f, &ent_in_buf, &buf_after_ent);
    239  1.35       dsl 	if (rc)
    240  1.51     isaki 		return rc;
    241  1.35       dsl 	/* make sure something's not badly wrong, but don't panic. */
    242  1.35       dsl 	if (buf_after_ent < sizeof (IFILE_Vx))
    243  1.51     isaki 		return EINVAL;
    244  1.35       dsl 
    245  1.35       dsl 	*isp = FSBTODB(fs, ((IFILE_Vx *)ent_in_buf)->if_daddr);
    246  1.35       dsl 	if (*isp == LFS_UNUSED_DADDR)	/* again, something badly wrong */
    247  1.51     isaki 		return EINVAL;
    248  1.51     isaki 	return 0;
    249  1.35       dsl }
    250   1.7    brezak #endif
    251   1.7    brezak 
    252   1.1    brezak /*
    253   1.1    brezak  * Read a new inode into a file structure.
    254   1.1    brezak  */
    255   1.1    brezak static int
    256  1.45  christos read_inode(ino32_t inumber, struct open_file *f)
    257   1.1    brezak {
    258  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    259  1.66  dholland 	FS *fs = fp->f_fs;
    260   1.1    brezak 	char *buf;
    261  1.49       mrg 	size_t rsize;
    262   1.1    brezak 	int rc;
    263  1.64  christos 	daddr_t inode_sector = 0; /* XXX: gcc */
    264  1.35       dsl #ifdef LIBSA_LFS
    265  1.35       dsl 	struct ufs_dinode *dip;
    266  1.35       dsl 	int cnt;
    267  1.35       dsl #endif
    268  1.35       dsl 
    269  1.35       dsl #ifdef LIBSA_LFS
    270  1.35       dsl 	if (inumber == fs->lfs_ifile)
    271  1.35       dsl 		inode_sector = FSBTODB(fs, fs->lfs_idaddr);
    272  1.35       dsl 	else if ((rc = find_inode_sector(inumber, f, &inode_sector)) != 0)
    273  1.51     isaki 		return rc;
    274  1.35       dsl #else
    275  1.35       dsl 	inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
    276  1.35       dsl #endif
    277   1.1    brezak 
    278   1.1    brezak 	/*
    279   1.1    brezak 	 * Read inode and save it.
    280   1.1    brezak 	 */
    281  1.40       dsl 	buf = fp->f_buf;
    282  1.11   mycroft 	twiddle();
    283  1.23       cgd 	rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    284  1.40       dsl 	    inode_sector, fs->fs_bsize, buf, &rsize);
    285   1.1    brezak 	if (rc)
    286  1.40       dsl 		return rc;
    287  1.49       mrg 	if (rsize != fs->fs_bsize)
    288  1.40       dsl 		return EIO;
    289   1.1    brezak 
    290  1.35       dsl #ifdef LIBSA_LFS
    291  1.35       dsl 	cnt = INOPBx(fs);
    292  1.40       dsl 	dip = (struct ufs_dinode *)buf + (cnt - 1);
    293  1.40       dsl 	for (; dip->di_inumber != inumber; --dip) {
    294  1.40       dsl 		/* kernel code panics, but boot blocks which panic are Bad. */
    295  1.40       dsl 		if (--cnt == 0)
    296  1.40       dsl 			return EINVAL;
    297   1.1    brezak 	}
    298  1.35       dsl 	fp->f_di = *dip;
    299  1.35       dsl #else
    300  1.35       dsl 	fp->f_di = ((struct ufs_dinode *)buf)[ino_to_fsbo(fs, inumber)];
    301  1.35       dsl #endif
    302   1.1    brezak 
    303   1.1    brezak 	/*
    304   1.1    brezak 	 * Clear out the old buffers
    305   1.1    brezak 	 */
    306  1.40       dsl 	fp->f_ind_cache_block = ~0;
    307  1.40       dsl 	fp->f_buf_blkno = -1;
    308  1.51     isaki 	return rc;
    309   1.1    brezak }
    310   1.1    brezak 
    311   1.1    brezak /*
    312   1.1    brezak  * Given an offset in a file, find the disk block number that
    313   1.1    brezak  * contains that block.
    314   1.1    brezak  */
    315   1.1    brezak static int
    316  1.40       dsl block_map(struct open_file *f, indp_t file_block, indp_t *disk_block_p)
    317   1.1    brezak {
    318  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    319  1.66  dholland 	FS *fs = fp->f_fs;
    320  1.52   tsutsui 	uint level;
    321  1.40       dsl 	indp_t ind_cache;
    322  1.40       dsl 	indp_t ind_block_num;
    323  1.49       mrg 	size_t rsize;
    324   1.1    brezak 	int rc;
    325  1.40       dsl 	indp_t *buf = (void *)fp->f_buf;
    326   1.1    brezak 
    327   1.1    brezak 	/*
    328   1.1    brezak 	 * Index structure of an inode:
    329   1.1    brezak 	 *
    330  1.59  dholland 	 * di_db[0..UFS_NDADDR-1]	hold block numbers for blocks
    331  1.59  dholland 	 *			0..UFS_NDADDR-1
    332   1.1    brezak 	 *
    333   1.1    brezak 	 * di_ib[0]		index block 0 is the single indirect block
    334   1.1    brezak 	 *			holds block numbers for blocks
    335  1.61  dholland 	 *			UFS_NDADDR .. UFS_NDADDR + UFS_NINDIR(fs)-1
    336   1.1    brezak 	 *
    337   1.1    brezak 	 * di_ib[1]		index block 1 is the double indirect block
    338   1.1    brezak 	 *			holds block numbers for INDEX blocks for blocks
    339  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) ..
    340  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2 - 1
    341   1.1    brezak 	 *
    342   1.1    brezak 	 * di_ib[2]		index block 2 is the triple indirect block
    343   1.1    brezak 	 *			holds block numbers for double-indirect
    344   1.1    brezak 	 *			blocks for blocks
    345  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2 ..
    346  1.61  dholland 	 *			UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2
    347  1.61  dholland 	 *				+ UFS_NINDIR(fs)**3 - 1
    348   1.1    brezak 	 */
    349   1.1    brezak 
    350  1.59  dholland 	if (file_block < UFS_NDADDR) {
    351   1.1    brezak 		/* Direct block. */
    352  1.35       dsl 		*disk_block_p = fp->f_di.di_db[file_block];
    353  1.51     isaki 		return 0;
    354   1.1    brezak 	}
    355   1.1    brezak 
    356  1.59  dholland 	file_block -= UFS_NDADDR;
    357   1.1    brezak 
    358  1.40       dsl 	ind_cache = file_block >> LN2_IND_CACHE_SZ;
    359  1.40       dsl 	if (ind_cache == fp->f_ind_cache_block) {
    360  1.40       dsl 		*disk_block_p = fp->f_ind_cache[file_block & IND_CACHE_MASK];
    361  1.40       dsl 		return 0;
    362  1.40       dsl 	}
    363  1.40       dsl 
    364  1.40       dsl 	for (level = 0;;) {
    365  1.40       dsl 		level += fp->f_nishift;
    366  1.40       dsl 		if (file_block < (indp_t)1 << level)
    367  1.40       dsl 			break;
    368  1.59  dholland 		if (level > UFS_NIADDR * fp->f_nishift)
    369  1.38       dsl 			/* Block number too high */
    370  1.51     isaki 			return EFBIG;
    371  1.40       dsl 		file_block -= (indp_t)1 << level;
    372   1.1    brezak 	}
    373   1.1    brezak 
    374  1.40       dsl 	ind_block_num = fp->f_di.di_ib[level / fp->f_nishift - 1];
    375   1.1    brezak 
    376  1.40       dsl 	for (;;) {
    377  1.40       dsl 		level -= fp->f_nishift;
    378   1.1    brezak 		if (ind_block_num == 0) {
    379   1.1    brezak 			*disk_block_p = 0;	/* missing */
    380  1.51     isaki 			return 0;
    381   1.1    brezak 		}
    382   1.1    brezak 
    383  1.40       dsl 		twiddle();
    384  1.40       dsl 		/*
    385  1.40       dsl 		 * If we were feeling brave, we could work out the number
    386  1.40       dsl 		 * of the disk sector and read a single disk sector instead
    387  1.40       dsl 		 * of a filesystem block.
    388  1.40       dsl 		 * However we don't do this very often anyway...
    389  1.40       dsl 		 */
    390  1.40       dsl 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    391  1.40       dsl 			FSBTODB(fp->f_fs, ind_block_num), fs->fs_bsize,
    392  1.40       dsl 			buf, &rsize);
    393  1.40       dsl 		if (rc)
    394  1.51     isaki 			return rc;
    395  1.49       mrg 		if (rsize != fs->fs_bsize)
    396  1.40       dsl 			return EIO;
    397  1.40       dsl 		ind_block_num = buf[file_block >> level];
    398  1.40       dsl 		if (level == 0)
    399  1.40       dsl 			break;
    400  1.40       dsl 		file_block &= (1 << level) - 1;
    401  1.40       dsl 	}
    402   1.1    brezak 
    403  1.40       dsl 	/* Save the part of the block that contains this sector */
    404  1.40       dsl 	memcpy(fp->f_ind_cache, &buf[file_block & ~IND_CACHE_MASK],
    405  1.40       dsl 	    IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
    406  1.40       dsl 	fp->f_ind_cache_block = ind_cache;
    407   1.1    brezak 
    408   1.1    brezak 	*disk_block_p = ind_block_num;
    409   1.1    brezak 
    410  1.51     isaki 	return 0;
    411   1.1    brezak }
    412   1.1    brezak 
    413   1.1    brezak /*
    414  1.40       dsl  * Read a portion of a file into an internal buffer.
    415  1.40       dsl  * Return the location in the buffer and the amount in the buffer.
    416   1.1    brezak  */
    417   1.1    brezak static int
    418  1.37       dsl buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
    419   1.1    brezak {
    420  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    421  1.66  dholland 	FS *fs = fp->f_fs;
    422   1.1    brezak 	long off;
    423  1.40       dsl 	indp_t file_block;
    424  1.13       cgd 	size_t block_size;
    425   1.1    brezak 	int rc;
    426   1.1    brezak 
    427  1.61  dholland 	off = ufs_blkoff(fs, fp->f_seekp);
    428  1.63  dholland 	file_block = ufs_lblkno(fs, fp->f_seekp);
    429  1.35       dsl #ifdef LIBSA_LFS
    430  1.35       dsl 	block_size = dblksize(fs, &fp->f_di, file_block);
    431  1.35       dsl #else
    432  1.61  dholland 	block_size = ffs_sblksize(fs, (int64_t)fp->f_di.di_size, file_block);
    433  1.35       dsl #endif
    434   1.1    brezak 
    435   1.1    brezak 	if (file_block != fp->f_buf_blkno) {
    436  1.64  christos 		indp_t disk_block = 0; /* XXX: gcc */
    437   1.1    brezak 		rc = block_map(f, file_block, &disk_block);
    438   1.1    brezak 		if (rc)
    439  1.51     isaki 			return rc;
    440   1.1    brezak 
    441   1.1    brezak 		if (disk_block == 0) {
    442  1.41       dsl 			memset(fp->f_buf, 0, block_size);
    443   1.1    brezak 			fp->f_buf_size = block_size;
    444   1.1    brezak 		} else {
    445  1.11   mycroft 			twiddle();
    446  1.23       cgd 			rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    447  1.35       dsl 				FSBTODB(fs, disk_block),
    448   1.1    brezak 				block_size, fp->f_buf, &fp->f_buf_size);
    449   1.1    brezak 			if (rc)
    450  1.51     isaki 				return rc;
    451   1.1    brezak 		}
    452   1.1    brezak 
    453   1.1    brezak 		fp->f_buf_blkno = file_block;
    454   1.1    brezak 	}
    455   1.1    brezak 
    456   1.1    brezak 	/*
    457   1.1    brezak 	 * Return address of byte in buffer corresponding to
    458   1.1    brezak 	 * offset, and size of remainder of buffer after that
    459   1.1    brezak 	 * byte.
    460   1.1    brezak 	 */
    461   1.1    brezak 	*buf_p = fp->f_buf + off;
    462   1.1    brezak 	*size_p = block_size - off;
    463   1.1    brezak 
    464   1.1    brezak 	/*
    465   1.1    brezak 	 * But truncate buffer at end of file.
    466   1.1    brezak 	 */
    467  1.35       dsl 	if (*size_p > fp->f_di.di_size - fp->f_seekp)
    468  1.35       dsl 		*size_p = fp->f_di.di_size - fp->f_seekp;
    469   1.1    brezak 
    470  1.51     isaki 	return 0;
    471   1.1    brezak }
    472   1.1    brezak 
    473   1.1    brezak /*
    474   1.1    brezak  * Search a directory for a name and return its
    475  1.38       dsl  * inode number.
    476   1.1    brezak  */
    477   1.1    brezak static int
    478  1.38       dsl search_directory(const char *name, int length, struct open_file *f,
    479  1.51     isaki 	ino32_t *inumber_p)
    480   1.1    brezak {
    481  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    482  1.30  augustss 	struct direct *dp;
    483   1.1    brezak 	struct direct *edp;
    484   1.1    brezak 	char *buf;
    485  1.13       cgd 	size_t buf_size;
    486  1.38       dsl 	int namlen;
    487   1.1    brezak 	int rc;
    488   1.1    brezak 
    489   1.1    brezak 	fp->f_seekp = 0;
    490  1.42      fvdl 	while (fp->f_seekp < (off_t)fp->f_di.di_size) {
    491   1.1    brezak 		rc = buf_read_file(f, &buf, &buf_size);
    492   1.1    brezak 		if (rc)
    493  1.51     isaki 			return rc;
    494   1.1    brezak 
    495   1.1    brezak 		dp = (struct direct *)buf;
    496   1.1    brezak 		edp = (struct direct *)(buf + buf_size);
    497  1.38       dsl 		for (;dp < edp; dp = (void *)((char *)dp + dp->d_reclen)) {
    498  1.40       dsl 			if (dp->d_reclen <= 0)
    499  1.40       dsl 				break;
    500  1.45  christos 			if (dp->d_ino == (ino32_t)0)
    501  1.38       dsl 				continue;
    502   1.1    brezak #if BYTE_ORDER == LITTLE_ENDIAN
    503   1.1    brezak 			if (fp->f_fs->fs_maxsymlinklen <= 0)
    504   1.1    brezak 				namlen = dp->d_type;
    505   1.1    brezak 			else
    506   1.1    brezak #endif
    507   1.1    brezak 				namlen = dp->d_namlen;
    508   1.1    brezak 			if (namlen == length &&
    509  1.38       dsl 			    !memcmp(name, dp->d_name, length)) {
    510   1.1    brezak 				/* found entry */
    511   1.1    brezak 				*inumber_p = dp->d_ino;
    512  1.51     isaki 				return 0;
    513   1.1    brezak 			}
    514   1.1    brezak 		}
    515   1.1    brezak 		fp->f_seekp += buf_size;
    516   1.1    brezak 	}
    517  1.51     isaki 	return ENOENT;
    518   1.1    brezak }
    519   1.1    brezak 
    520  1.35       dsl #ifdef LIBSA_FFSv2
    521  1.33      fvdl 
    522  1.33      fvdl daddr_t sblock_try[] = SBLOCKSEARCH;
    523  1.33      fvdl 
    524  1.33      fvdl static int
    525  1.66  dholland ffs_find_superblock(struct open_file *f, FS *fs)
    526  1.33      fvdl {
    527  1.33      fvdl 	int i, rc;
    528  1.33      fvdl 	size_t buf_size;
    529  1.33      fvdl 
    530  1.33      fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
    531  1.33      fvdl 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    532  1.40       dsl 		    sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, fs, &buf_size);
    533  1.33      fvdl 		if (rc != 0 || buf_size != SBLOCKSIZE)
    534  1.33      fvdl 			return rc;
    535  1.43       dsl 		if (fs->fs_sblockloc != sblock_try[i])
    536  1.43       dsl 			/* an alternate superblock - try again */
    537  1.43       dsl 			continue;
    538  1.33      fvdl 		if (fs->fs_magic == FS_UFS2_MAGIC) {
    539  1.33      fvdl 			return 0;
    540  1.33      fvdl 		}
    541  1.33      fvdl 	}
    542  1.33      fvdl 	return EINVAL;
    543  1.33      fvdl }
    544  1.33      fvdl 
    545  1.33      fvdl #endif
    546  1.33      fvdl 
    547   1.1    brezak /*
    548   1.1    brezak  * Open a file.
    549   1.1    brezak  */
    550  1.55     joerg __compactcall int
    551  1.38       dsl ufs_open(const char *path, struct open_file *f)
    552   1.1    brezak {
    553  1.23       cgd #ifndef LIBSA_FS_SINGLECOMPONENT
    554  1.38       dsl 	const char *cp, *ncp;
    555  1.30  augustss 	int c;
    556  1.23       cgd #endif
    557  1.45  christos 	ino32_t inumber;
    558   1.1    brezak 	struct file *fp;
    559  1.66  dholland 	FS *fs;
    560  1.10        ws 	int rc;
    561  1.23       cgd #ifndef LIBSA_NO_FS_SYMLINK
    562  1.45  christos 	ino32_t parent_inumber;
    563  1.10        ws 	int nlinks = 0;
    564   1.1    brezak 	char namebuf[MAXPATHLEN+1];
    565  1.40       dsl 	char *buf;
    566  1.22    simonb #endif
    567   1.1    brezak 
    568   1.1    brezak 	/* allocate file system specific data structure */
    569   1.1    brezak 	fp = alloc(sizeof(struct file));
    570  1.41       dsl 	memset(fp, 0, sizeof(struct file));
    571   1.1    brezak 	f->f_fsdata = (void *)fp;
    572   1.1    brezak 
    573   1.1    brezak 	/* allocate space and read super block */
    574  1.33      fvdl 	fs = alloc(SBLOCKSIZE);
    575   1.1    brezak 	fp->f_fs = fs;
    576  1.11   mycroft 	twiddle();
    577  1.33      fvdl 
    578  1.35       dsl #ifdef LIBSA_FFSv2
    579  1.33      fvdl 	rc = ffs_find_superblock(f, fs);
    580  1.33      fvdl 	if (rc)
    581  1.33      fvdl 		goto out;
    582  1.33      fvdl #else
    583  1.35       dsl 	{
    584  1.35       dsl 		size_t buf_size;
    585  1.35       dsl 		rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    586  1.40       dsl 			SBLOCKOFFSET / DEV_BSIZE, SBLOCKSIZE, fs, &buf_size);
    587  1.35       dsl 		if (rc)
    588  1.35       dsl 			goto out;
    589  1.35       dsl 		if (buf_size != SBLOCKSIZE ||
    590  1.35       dsl #ifdef LIBSA_FFS
    591  1.35       dsl 		    fs->lfs_version != REQUIRED_LFS_VERSION ||
    592  1.35       dsl #endif
    593  1.35       dsl 		    fs->fs_magic != FS_MAGIC) {
    594  1.35       dsl 			rc = EINVAL;
    595  1.35       dsl 			goto out;
    596  1.35       dsl 		}
    597  1.33      fvdl 	}
    598  1.35       dsl #if defined(LIBSA_LFS) && REQUIRED_LFS_VERSION == 2
    599  1.35       dsl 	/*
    600  1.35       dsl 	 * XXX	We should check the second superblock and use the eldest
    601  1.35       dsl 	 *	of the two.  See comments near the top of lfs_mountfs()
    602  1.35       dsl 	 *	in sys/ufs/lfs/lfs_vfsops.c.
    603  1.35       dsl 	 *      This may need a LIBSA_LFS_SMALL check as well.
    604  1.35       dsl 	 */
    605  1.35       dsl #endif
    606  1.33      fvdl #endif
    607   1.1    brezak 
    608  1.35       dsl #ifdef LIBSA_FFSv1
    609  1.33      fvdl 	ffs_oldfscompat(fs);
    610  1.35       dsl #endif
    611  1.33      fvdl 
    612  1.42      fvdl 	if (fs->fs_bsize > MAXBSIZE ||
    613  1.66  dholland 	    (size_t)fs->fs_bsize < sizeof(FS)) {
    614   1.1    brezak 		rc = EINVAL;
    615   1.1    brezak 		goto out;
    616   1.1    brezak 	}
    617   1.1    brezak 
    618   1.1    brezak 	/*
    619   1.1    brezak 	 * Calculate indirect block levels.
    620   1.1    brezak 	 */
    621   1.1    brezak 	{
    622  1.40       dsl 		indp_t mult;
    623  1.35       dsl 		int ln2;
    624  1.35       dsl 
    625  1.35       dsl 		/*
    626  1.35       dsl 		 * We note that the number of indirect blocks is always
    627  1.35       dsl 		 * a power of 2.  This lets us use shifts and masks instead
    628  1.35       dsl 		 * of divide and remainder and avoinds pulling in the
    629  1.35       dsl 		 * 64bit division routine into the boot code.
    630  1.35       dsl 		 */
    631  1.61  dholland 		mult = UFS_NINDIR(fs);
    632  1.35       dsl #ifdef DEBUG
    633  1.35       dsl 		if (mult & (mult - 1)) {
    634  1.35       dsl 			/* Hummm was't a power of 2 */
    635  1.35       dsl 			rc = EINVAL;
    636  1.35       dsl 			goto out;
    637  1.35       dsl 		}
    638  1.35       dsl #endif
    639  1.35       dsl 		for (ln2 = 0; mult != 1; ln2++)
    640  1.35       dsl 			mult >>= 1;
    641   1.1    brezak 
    642  1.40       dsl 		fp->f_nishift = ln2;
    643   1.1    brezak 	}
    644   1.1    brezak 
    645  1.40       dsl 	/* alloc a block sized buffer used for all fs transfers */
    646  1.40       dsl 	fp->f_buf = alloc(fs->fs_bsize);
    647  1.59  dholland 	inumber = UFS_ROOTINO;
    648   1.1    brezak 	if ((rc = read_inode(inumber, f)) != 0)
    649   1.1    brezak 		goto out;
    650   1.1    brezak 
    651  1.23       cgd #ifndef LIBSA_FS_SINGLECOMPONENT
    652   1.1    brezak 	cp = path;
    653   1.1    brezak 	while (*cp) {
    654   1.1    brezak 
    655   1.1    brezak 		/*
    656   1.1    brezak 		 * Remove extra separators
    657   1.1    brezak 		 */
    658   1.1    brezak 		while (*cp == '/')
    659   1.1    brezak 			cp++;
    660   1.1    brezak 		if (*cp == '\0')
    661   1.1    brezak 			break;
    662   1.1    brezak 
    663   1.1    brezak 		/*
    664   1.1    brezak 		 * Check that current node is a directory.
    665   1.1    brezak 		 */
    666  1.35       dsl 		if ((fp->f_di.di_mode & IFMT) != IFDIR) {
    667   1.1    brezak 			rc = ENOTDIR;
    668   1.1    brezak 			goto out;
    669   1.1    brezak 		}
    670   1.1    brezak 
    671   1.1    brezak 		/*
    672   1.1    brezak 		 * Get next component of path name.
    673   1.1    brezak 		 */
    674  1.38       dsl 		ncp = cp;
    675  1.38       dsl 		while ((c = *cp) != '\0' && c != '/')
    676  1.38       dsl 			cp++;
    677   1.1    brezak 
    678   1.1    brezak 		/*
    679   1.1    brezak 		 * Look up component in current directory.
    680   1.1    brezak 		 * Save directory inumber in case we find a
    681   1.1    brezak 		 * symbolic link.
    682   1.1    brezak 		 */
    683  1.23       cgd #ifndef LIBSA_NO_FS_SYMLINK
    684   1.1    brezak 		parent_inumber = inumber;
    685  1.23       cgd #endif
    686  1.38       dsl 		rc = search_directory(ncp, cp - ncp, f, &inumber);
    687   1.1    brezak 		if (rc)
    688   1.1    brezak 			goto out;
    689   1.1    brezak 
    690   1.1    brezak 		/*
    691   1.1    brezak 		 * Open next component.
    692   1.1    brezak 		 */
    693   1.1    brezak 		if ((rc = read_inode(inumber, f)) != 0)
    694   1.1    brezak 			goto out;
    695   1.1    brezak 
    696  1.23       cgd #ifndef LIBSA_NO_FS_SYMLINK
    697   1.1    brezak 		/*
    698   1.1    brezak 		 * Check for symbolic link.
    699   1.1    brezak 		 */
    700  1.35       dsl 		if ((fp->f_di.di_mode & IFMT) == IFLNK) {
    701  1.35       dsl 			int link_len = fp->f_di.di_size;
    702  1.10        ws 			int len;
    703  1.10        ws 
    704  1.10        ws 			len = strlen(cp);
    705   1.1    brezak 
    706  1.10        ws 			if (link_len + len > MAXPATHLEN ||
    707  1.10        ws 			    ++nlinks > MAXSYMLINKS) {
    708   1.1    brezak 				rc = ENOENT;
    709   1.1    brezak 				goto out;
    710   1.1    brezak 			}
    711   1.1    brezak 
    712  1.38       dsl 			memmove(&namebuf[link_len], cp, len + 1);
    713   1.1    brezak 
    714   1.9        pk 			if (link_len < fs->fs_maxsymlinklen) {
    715  1.38       dsl 				memcpy(namebuf, fp->f_di.di_db, link_len);
    716   1.1    brezak 			} else {
    717   1.1    brezak 				/*
    718   1.1    brezak 				 * Read file for symbolic link
    719   1.1    brezak 				 */
    720  1.13       cgd 				size_t buf_size;
    721  1.40       dsl 				indp_t	disk_block;
    722   1.9        pk 
    723  1.40       dsl 				buf = fp->f_buf;
    724  1.40       dsl 				rc = block_map(f, (indp_t)0, &disk_block);
    725   1.9        pk 				if (rc)
    726   1.9        pk 					goto out;
    727  1.22    simonb 
    728  1.11   mycroft 				twiddle();
    729  1.23       cgd 				rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
    730  1.35       dsl 					F_READ, FSBTODB(fs, disk_block),
    731   1.9        pk 					fs->fs_bsize, buf, &buf_size);
    732   1.1    brezak 				if (rc)
    733   1.1    brezak 					goto out;
    734   1.1    brezak 
    735  1.38       dsl 				memcpy(namebuf, buf, link_len);
    736   1.1    brezak 			}
    737   1.1    brezak 
    738   1.1    brezak 			/*
    739   1.1    brezak 			 * If relative pathname, restart at parent directory.
    740   1.1    brezak 			 * If absolute pathname, restart at root.
    741   1.1    brezak 			 */
    742   1.1    brezak 			cp = namebuf;
    743   1.1    brezak 			if (*cp != '/')
    744   1.1    brezak 				inumber = parent_inumber;
    745   1.1    brezak 			else
    746  1.59  dholland 				inumber = (ino32_t)UFS_ROOTINO;
    747   1.1    brezak 
    748   1.9        pk 			if ((rc = read_inode(inumber, f)) != 0)
    749   1.1    brezak 				goto out;
    750   1.1    brezak 		}
    751  1.23       cgd #endif	/* !LIBSA_NO_FS_SYMLINK */
    752   1.1    brezak 	}
    753   1.1    brezak 
    754   1.1    brezak 	/*
    755   1.1    brezak 	 * Found terminal component.
    756   1.1    brezak 	 */
    757   1.1    brezak 	rc = 0;
    758  1.23       cgd 
    759  1.23       cgd #else /* !LIBSA_FS_SINGLECOMPONENT */
    760  1.23       cgd 
    761  1.23       cgd 	/* look up component in the current (root) directory */
    762  1.38       dsl 	rc = search_directory(path, strlen(path), f, &inumber);
    763  1.23       cgd 	if (rc)
    764  1.23       cgd 		goto out;
    765  1.23       cgd 
    766  1.23       cgd 	/* open it */
    767  1.23       cgd 	rc = read_inode(inumber, f);
    768  1.23       cgd 
    769  1.23       cgd #endif /* !LIBSA_FS_SINGLECOMPONENT */
    770  1.26       cgd 
    771  1.51     isaki 	fp->f_seekp = 0;		/* reset seek pointer */
    772  1.23       cgd 
    773   1.1    brezak out:
    774  1.40       dsl 	if (rc)
    775  1.38       dsl 		ufs_close(f);
    776  1.58       dsl #ifdef FSMOD		/* Only defined for lfs */
    777  1.58       dsl 	else
    778  1.54        ad 		fsmod = FSMOD;
    779  1.54        ad #endif
    780  1.51     isaki 	return rc;
    781   1.1    brezak }
    782   1.1    brezak 
    783  1.55     joerg __compactcall int
    784  1.37       dsl ufs_close(struct open_file *f)
    785   1.1    brezak {
    786  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    787   1.1    brezak 
    788  1.38       dsl 	f->f_fsdata = NULL;
    789  1.38       dsl 	if (fp == NULL)
    790  1.51     isaki 		return 0;
    791   1.1    brezak 
    792   1.1    brezak 	if (fp->f_buf)
    793  1.48  christos 		dealloc(fp->f_buf, fp->f_fs->fs_bsize);
    794  1.48  christos 	dealloc(fp->f_fs, SBLOCKSIZE);
    795  1.48  christos 	dealloc(fp, sizeof(struct file));
    796  1.51     isaki 	return 0;
    797   1.1    brezak }
    798   1.1    brezak 
    799   1.1    brezak /*
    800   1.1    brezak  * Copy a portion of a file into kernel memory.
    801   1.1    brezak  * Cross block boundaries when necessary.
    802   1.1    brezak  */
    803  1.55     joerg __compactcall int
    804  1.37       dsl ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
    805   1.1    brezak {
    806  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    807  1.30  augustss 	size_t csize;
    808   1.1    brezak 	char *buf;
    809  1.13       cgd 	size_t buf_size;
    810   1.1    brezak 	int rc = 0;
    811  1.30  augustss 	char *addr = start;
    812   1.1    brezak 
    813   1.1    brezak 	while (size != 0) {
    814  1.42      fvdl 		if (fp->f_seekp >= (off_t)fp->f_di.di_size)
    815   1.1    brezak 			break;
    816   1.1    brezak 
    817   1.1    brezak 		rc = buf_read_file(f, &buf, &buf_size);
    818   1.1    brezak 		if (rc)
    819   1.1    brezak 			break;
    820   1.1    brezak 
    821   1.1    brezak 		csize = size;
    822   1.1    brezak 		if (csize > buf_size)
    823   1.1    brezak 			csize = buf_size;
    824   1.1    brezak 
    825  1.38       dsl 		memcpy(addr, buf, csize);
    826   1.1    brezak 
    827   1.1    brezak 		fp->f_seekp += csize;
    828  1.12        pk 		addr += csize;
    829   1.1    brezak 		size -= csize;
    830   1.1    brezak 	}
    831   1.1    brezak 	if (resid)
    832   1.1    brezak 		*resid = size;
    833  1.51     isaki 	return rc;
    834   1.1    brezak }
    835   1.1    brezak 
    836   1.1    brezak /*
    837   1.1    brezak  * Not implemented.
    838   1.1    brezak  */
    839  1.23       cgd #ifndef LIBSA_NO_FS_WRITE
    840  1.55     joerg __compactcall int
    841  1.37       dsl ufs_write(struct open_file *f, void *start, size_t size, size_t *resid)
    842   1.1    brezak {
    843   1.1    brezak 
    844  1.51     isaki 	return EROFS;
    845   1.1    brezak }
    846  1.23       cgd #endif /* !LIBSA_NO_FS_WRITE */
    847   1.1    brezak 
    848  1.23       cgd #ifndef LIBSA_NO_FS_SEEK
    849  1.55     joerg __compactcall off_t
    850  1.37       dsl ufs_seek(struct open_file *f, off_t offset, int where)
    851   1.1    brezak {
    852  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    853   1.1    brezak 
    854   1.1    brezak 	switch (where) {
    855   1.1    brezak 	case SEEK_SET:
    856   1.1    brezak 		fp->f_seekp = offset;
    857   1.1    brezak 		break;
    858   1.1    brezak 	case SEEK_CUR:
    859   1.1    brezak 		fp->f_seekp += offset;
    860   1.1    brezak 		break;
    861   1.1    brezak 	case SEEK_END:
    862  1.35       dsl 		fp->f_seekp = fp->f_di.di_size - offset;
    863   1.1    brezak 		break;
    864   1.1    brezak 	default:
    865  1.51     isaki 		return -1;
    866   1.1    brezak 	}
    867  1.51     isaki 	return fp->f_seekp;
    868   1.1    brezak }
    869  1.23       cgd #endif /* !LIBSA_NO_FS_SEEK */
    870   1.1    brezak 
    871  1.55     joerg __compactcall int
    872  1.37       dsl ufs_stat(struct open_file *f, struct stat *sb)
    873   1.1    brezak {
    874  1.30  augustss 	struct file *fp = (struct file *)f->f_fsdata;
    875   1.1    brezak 
    876   1.1    brezak 	/* only important stuff */
    877  1.40       dsl 	memset(sb, 0, sizeof *sb);
    878  1.35       dsl 	sb->st_mode = fp->f_di.di_mode;
    879  1.35       dsl 	sb->st_uid = fp->f_di.di_uid;
    880  1.35       dsl 	sb->st_gid = fp->f_di.di_gid;
    881  1.35       dsl 	sb->st_size = fp->f_di.di_size;
    882  1.51     isaki 	return 0;
    883   1.4        pk }
    884   1.4        pk 
    885  1.56   tsutsui #if defined(LIBSA_ENABLE_LS_OP)
    886  1.65  christos 
    887  1.65  christos #include "ls.h"
    888  1.65  christos 
    889  1.65  christos static const char    *const typestr[] = {
    890  1.65  christos 	"unknown",
    891  1.65  christos 	"FIFO",
    892  1.65  christos 	"CHR",
    893  1.65  christos 	0,
    894  1.65  christos 	"DIR",
    895  1.65  christos 	0,
    896  1.65  christos 	"BLK",
    897  1.65  christos 	0,
    898  1.65  christos 	"REG",
    899  1.65  christos 	0,
    900  1.65  christos 	"LNK",
    901  1.65  christos 	0,
    902  1.65  christos 	"SOCK",
    903  1.65  christos 	0,
    904  1.65  christos 	"WHT"
    905  1.65  christos };
    906  1.65  christos 
    907  1.56   tsutsui __compactcall void
    908  1.56   tsutsui ufs_ls(struct open_file *f, const char *pattern)
    909  1.56   tsutsui {
    910  1.56   tsutsui 	struct file *fp = (struct file *)f->f_fsdata;
    911  1.56   tsutsui 	char *buf;
    912  1.56   tsutsui 	size_t buf_size;
    913  1.65  christos 	lsentry_t *names = NULL;
    914  1.56   tsutsui 
    915  1.56   tsutsui 	fp->f_seekp = 0;
    916  1.56   tsutsui 	while (fp->f_seekp < (off_t)fp->f_di.di_size) {
    917  1.56   tsutsui 		struct direct  *dp, *edp;
    918  1.56   tsutsui 		int rc = buf_read_file(f, &buf, &buf_size);
    919  1.56   tsutsui 		if (rc)
    920  1.56   tsutsui 			goto out;
    921  1.56   tsutsui 		/* some firmware might use block size larger than DEV_BSIZE */
    922  1.60  dholland 		if (buf_size < UFS_DIRBLKSIZ)
    923  1.56   tsutsui 			goto out;
    924  1.56   tsutsui 
    925  1.56   tsutsui 		dp = (struct direct *)buf;
    926  1.56   tsutsui 		edp = (struct direct *)(buf + buf_size);
    927  1.56   tsutsui 
    928  1.56   tsutsui 		for (; dp < edp; dp = (void *)((char *)dp + dp->d_reclen)) {
    929  1.56   tsutsui 			const char *t;
    930  1.56   tsutsui 			if (dp->d_ino ==  0)
    931  1.56   tsutsui 				continue;
    932  1.56   tsutsui 
    933  1.56   tsutsui 			if (dp->d_type >= NELEM(typestr) ||
    934  1.56   tsutsui 			    !(t = typestr[dp->d_type])) {
    935  1.56   tsutsui 				/*
    936  1.56   tsutsui 				 * This does not handle "old"
    937  1.56   tsutsui 				 * filesystems properly. On little
    938  1.56   tsutsui 				 * endian machines, we get a bogus
    939  1.56   tsutsui 				 * type name if the namlen matches a
    940  1.56   tsutsui 				 * valid type identifier. We could
    941  1.56   tsutsui 				 * check if we read namlen "0" and
    942  1.56   tsutsui 				 * handle this case specially, if
    943  1.56   tsutsui 				 * there were a pressing need...
    944  1.56   tsutsui 				 */
    945  1.56   tsutsui 				printf("bad dir entry\n");
    946  1.56   tsutsui 				goto out;
    947  1.56   tsutsui 			}
    948  1.65  christos 			lsadd(&names, pattern, dp->d_name, strlen(dp->d_name),
    949  1.65  christos 			    dp->d_ino, t);
    950  1.56   tsutsui 		}
    951  1.56   tsutsui 		fp->f_seekp += buf_size;
    952  1.56   tsutsui 	}
    953  1.65  christos 	lsprint(names);
    954  1.65  christos out:	lsfree(names);
    955  1.56   tsutsui }
    956  1.56   tsutsui #endif /* LIBSA_ENABLE_LS_OP */
    957  1.56   tsutsui 
    958  1.35       dsl #ifdef LIBSA_FFSv1
    959   1.4        pk /*
    960   1.4        pk  * Sanity checks for old file systems.
    961   1.4        pk  *
    962   1.4        pk  * XXX - goes away some day.
    963  1.40       dsl  * Stripped of stuff libsa doesn't need.....
    964   1.4        pk  */
    965  1.13       cgd static void
    966  1.66  dholland ffs_oldfscompat(FS *fs)
    967   1.4        pk {
    968   1.4        pk 
    969  1.33      fvdl #ifdef COMPAT_UFS
    970  1.50    martin 	/*
    971  1.50    martin 	 * Newer Solaris versions have a slightly incompatible
    972  1.50    martin 	 * superblock - so always calculate this values on the fly, which
    973  1.50    martin 	 * is good enough for libsa purposes
    974  1.50    martin 	 */
    975  1.50    martin 	if (fs->fs_magic == FS_UFS1_MAGIC
    976  1.50    martin #ifndef COMPAT_SOLARIS_UFS
    977  1.50    martin 	    && fs->fs_old_inodefmt < FS_44INODEFMT
    978  1.50    martin #endif
    979  1.50    martin 	    ) {
    980  1.33      fvdl 		fs->fs_qbmask = ~fs->fs_bmask;
    981  1.33      fvdl 		fs->fs_qfmask = ~fs->fs_fmask;
    982  1.33      fvdl 	}
    983  1.33      fvdl #endif
    984   1.1    brezak }
    985  1.35       dsl #endif
    986