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