199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1999,2000 Jonathan Lemon <jlemon@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This code is derived from software contributed to Berkeley by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Mach Operating System project at Carnegie-Mellon University.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. All advertising materials mentioning features or use of this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * must display the following acknowledgement:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This product includes software developed by the University of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * California, Berkeley and its contributors.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1990, 1991 Carnegie Mellon University
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All Rights Reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Author: David Golub
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Permission to use, copy, modify and distribute this software and its
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation is hereby granted, provided that both the copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice and this permission notice appear in all copies of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * software, derivative works or modified versions, and any portions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * thereof, and that both notices appear in supporting documentation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Carnegie Mellon requests users of this software to return to
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
199767f8919635c4928607450d9e0abb932109ceToomas Soome * School of Computer Science
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Carnegie Mellon University
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Pittsburgh PA 15213-3890
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * any improvements or extensions that they make and grant Carnegie the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * rights to redistribute these changes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/time.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "string.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int ext2fs_open(const char *path, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int ext2fs_close(struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int ext2fs_read(struct open_file *f, void *buf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size, size_t *resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t ext2fs_seek(struct open_file *f, off_t offset, int where);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int ext2fs_stat(struct open_file *f, struct stat *sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int ext2fs_readdir(struct open_file *f, struct dirent *d);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int dtmap[] = { DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR,
199767f8919635c4928607450d9e0abb932109ceToomas Soome DT_BLK, DT_FIFO, DT_SOCK, DT_LNK };
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXTFTODT(x) (x) > sizeof(dtmap) / sizeof(dtmap[0]) ? \
199767f8919635c4928607450d9e0abb932109ceToomas Soome DT_UNKNOWN : dtmap[x]
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct fs_ops ext2fs_fsops = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "ext2fs",
199767f8919635c4928607450d9e0abb932109ceToomas Soome ext2fs_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ext2fs_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ext2fs_read,
199767f8919635c4928607450d9e0abb932109ceToomas Soome null_write,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ext2fs_seek,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ext2fs_stat,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ext2fs_readdir
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_SBSIZE 1024
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_SBLOCK (1024 / DEV_BSIZE) /* block offset of superblock */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_MAGIC 0xef53
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_ROOTINO 2
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_REV0 0 /* original revision of ext2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_R0_ISIZE 128 /* inode size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_R0_FIRSTINO 11 /* first inode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_MINBSHIFT 10 /* mininum block shift */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_MINFSHIFT 10 /* mininum frag shift */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NDADDR 12 /* # of direct blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NIADDR 3 /* # of indirect blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * file system block to disk address
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fsb_to_db(fs, blk) ((blk) << (fs)->fs_fsbtodb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * inode to block group offset
199767f8919635c4928607450d9e0abb932109ceToomas Soome * inode to block group
199767f8919635c4928607450d9e0abb932109ceToomas Soome * inode to disk address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * inode to block offset
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ino_to_bgo(fs, ino) (((ino) - 1) % (fs)->fs_ipg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ino_to_bg(fs, ino) (((ino) - 1) / (fs)->fs_ipg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ino_to_db(fs, bg, ino) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome fsb_to_db(fs, ((bg)[ino_to_bg(fs, ino)].bg_inotbl + \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ino_to_bgo(fs, ino) / (fs)->fs_ipb))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ino_to_bo(fs, ino) (ino_to_bgo(fs, ino) % (fs)->fs_ipb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define nindir(fs) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((fs)->fs_bsize / sizeof(u_int32_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define lblkno(fs, loc) /* loc / bsize */ \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((loc) >> (fs)->fs_bshift)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define smalllblktosize(fs, blk) /* blk * bsize */ \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((blk) << (fs)->fs_bshift)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define blkoff(fs, loc) /* loc % bsize */ \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((loc) & (fs)->fs_bmask)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fragroundup(fs, size) /* roundup(size, fsize) */ \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (((size) + (fs)->fs_fmask) & ~(fs)->fs_fmask)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define dblksize(fs, dip, lbn) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (((lbn) >= NDADDR || (dip)->di_size >= smalllblktosize(fs, (lbn) + 1)) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ? (fs)->fs_bsize \
199767f8919635c4928607450d9e0abb932109ceToomas Soome : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * superblock describing ext2fs
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct ext2fs_disk {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_inodes; /* # of inodes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_blocks; /* # of blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_resblk; /* # of reserved blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_freeblk; /* # of free blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_freeino; /* # of free inodes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_firstblk; /* first data block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_bsize; /* block size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_fsize; /* frag size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_bpg; /* blocks per group */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_fpg; /* frags per group */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_ipg; /* inodes per group */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_mtime; /* mount time */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_wtime; /* write time */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_mount; /* # of mounts */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int16_t fd_maxmount; /* max # of mounts */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_magic; /* magic number */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_state; /* state */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_eflag; /* error flags */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_mnrrev; /* minor revision */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_lastchk; /* last check */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_chkintvl; /* maximum check interval */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_os; /* os */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_revision; /* revision */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_uid; /* uid for reserved blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_gid; /* gid for reserved blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_firstino; /* first non-reserved inode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_isize; /* inode size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t fd_nblkgrp; /* block group # of superblock */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_fcompat; /* compatible features */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_fincompat; /* incompatible features */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_frocompat; /* read-only compatibilties */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t fd_uuid[16]; /* volume uuid */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char fd_volname[16]; /* volume name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char fd_fsmnt[64]; /* name last mounted on */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t fd_bitmap; /* compression bitmap */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t fd_nblkpa; /* # of blocks to preallocate */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t fd_ndblkpa; /* # of dir blocks to preallocate */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct ext2fs_core {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_bsize; /* block size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_bshift; /* block shift amount */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_bmask; /* block mask */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_fsize; /* frag size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_fshift; /* frag shift amount */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_fmask; /* frag mask */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_isize; /* inode size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_imask; /* inode mask */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_firstino; /* first non-reserved inode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_ipb; /* inodes per block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fc_fsbtodb; /* fsb to ds shift */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct ext2fs {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs_disk fs_fd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char fs_pad[EXT2_SBSIZE - sizeof(struct ext2fs_disk)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs_core fs_fc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_magic fs_fd.fd_magic
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_revision fs_fd.fd_revision
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_blocks fs_fd.fd_blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_firstblk fs_fd.fd_firstblk
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_bpg fs_fd.fd_bpg
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_ipg fs_fd.fd_ipg
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_bsize fs_fc.fc_bsize
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_bshift fs_fc.fc_bshift
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_bmask fs_fc.fc_bmask
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_fsize fs_fc.fc_fsize
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_fshift fs_fc.fc_fshift
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_fmask fs_fc.fc_fmask
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_isize fs_fc.fc_isize
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_imask fs_fc.fc_imask
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_firstino fs_fc.fc_firstino
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_ipb fs_fc.fc_ipb
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fs_fsbtodb fs_fc.fc_fsbtodb
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct ext2blkgrp {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t bg_blkmap; /* block bitmap */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t bg_inomap; /* inode bitmap */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t bg_inotbl; /* inode table */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t bg_nfblk; /* # of free blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t bg_nfino; /* # of free inodes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t bg_ndirs; /* # of dirs */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char bg_pad[14];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct ext2dinode {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t di_mode; /* mode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t di_uid; /* uid */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_size; /* byte size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_atime; /* access time */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_ctime; /* creation time */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_mtime; /* modification time */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_dtime; /* deletion time */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t di_gid; /* gid */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t di_nlink; /* link count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_nblk; /* block count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_flags; /* file flags */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_osdep1; /* os dependent stuff */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_db[NDADDR]; /* direct blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_ib[NIADDR]; /* indirect blocks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_version; /* version */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_facl; /* file acl */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_dacl; /* dir acl */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t di_faddr; /* fragment addr */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t di_frag; /* fragment number */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t di_fsize; /* fragment size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome char di_pad[10];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define di_shortlink di_db
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXT2_MAXNAMLEN 255
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct ext2dirent {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t d_ino; /* inode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t d_reclen; /* directory entry length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t d_namlen; /* name length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t d_type; /* file type */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char d_name[EXT2_MAXNAMLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct file {
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t f_seekp; /* seek pointer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs *f_fs; /* pointer to super-block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2blkgrp *f_bg; /* pointer to blkgrp map */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2dinode f_di; /* copy of on-disk inode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int f_nindir[NIADDR]; /* number of blocks mapped by
199767f8919635c4928607450d9e0abb932109ceToomas Soome indirect block at level i */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *f_blk[NIADDR]; /* buffer for indirect block
199767f8919635c4928607450d9e0abb932109ceToomas Soome at level i */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t f_blksize[NIADDR]; /* size of buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t f_blkno[NIADDR]; /* disk address of block in
199767f8919635c4928607450d9e0abb932109ceToomas Soome buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *f_buf; /* buffer for data block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t f_buf_size; /* size of data block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t f_buf_blkno; /* block number of data block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* forward decls */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int read_inode(ino_t inumber, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int block_map(struct open_file *f, daddr_t file_block,
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t *disk_block_p);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int buf_read_file(struct open_file *f, char **buf_p,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t *size_p);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int search_directory(char *name, struct open_file *f,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ino_t *inumber_p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open a file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeext2fs_open(const char *upath, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs *fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ino_t inumber, parent_inumber;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, len, groups, bg_per_blk, blkgrps, mult;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nlinks = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp, *ncp, *path = NULL, *buf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char namebuf[MAXPATHLEN+1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* allocate file system specific data structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp = malloc(sizeof(struct file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(fp, sizeof(struct file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *)fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* allocate space and read super block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs = (struct ext2fs *)malloc(sizeof(*fs));
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_fs = fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EXT2_SBLOCK, EXT2_SBSIZE, (char *)fs, &buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf_size != EXT2_SBSIZE || fs->fs_magic != EXT2_MAGIC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = EINVAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * compute in-core values for the superblock
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_bshift = EXT2_MINBSHIFT + fs->fs_fd.fd_bsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_bsize = 1 << fs->fs_bshift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_bmask = fs->fs_bsize - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_fshift = EXT2_MINFSHIFT + fs->fs_fd.fd_fsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_fsize = 1 << fs->fs_fshift;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_fmask = fs->fs_fsize - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fs->fs_revision == EXT2_REV0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_isize = EXT2_R0_ISIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_firstino = EXT2_R0_FIRSTINO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_isize = fs->fs_fd.fd_isize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_firstino = fs->fs_fd.fd_firstino;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_imask = fs->fs_isize - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_ipb = fs->fs_bsize / fs->fs_isize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_fsbtodb = (fs->fs_bsize / DEV_BSIZE) - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we have to load in the "group descriptors" here
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome groups = howmany(fs->fs_blocks - fs->fs_firstblk, fs->fs_bpg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bg_per_blk = fs->fs_bsize / sizeof(struct ext2blkgrp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome blkgrps = howmany(groups, bg_per_blk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = blkgrps * fs->fs_bsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_bg = malloc(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EXT2_SBLOCK + EXT2_SBSIZE / DEV_BSIZE, len,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (char *)fp->f_bg, &buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX
199767f8919635c4928607450d9e0abb932109ceToomas Soome * validation of values? (blocksize, descriptors, etc?)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Calculate indirect block levels.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome mult = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < NIADDR; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome mult *= nindir(fs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_nindir[i] = mult;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome inumber = EXT2_ROOTINO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = read_inode(inumber, f)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = strdup(upath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remove extra separators
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check that current node is a directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (! S_ISDIR(fp->f_di.di_mode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOTDIR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get next component of path name.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ncp = cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = *cp) != '\0' && c != '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (++len > EXT2_MAXNAMLEN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Look up component in current directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Save directory inumber in case we find a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * symbolic link.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome parent_inumber = inumber;
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = search_directory(ncp, f, &inumber);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open next component.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = read_inode(inumber, f)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check for symbolic link.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (S_ISLNK(fp->f_di.di_mode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int link_len = fp->f_di.di_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (link_len + len > MAXPATHLEN ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++nlinks > MAXSYMLINKS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(cp, &namebuf[link_len], len + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_di.di_nblk == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(fp->f_di.di_shortlink,
199767f8919635c4928607450d9e0abb932109ceToomas Soome namebuf, link_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read file for symbolic link
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs *fs = fp->f_fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t disk_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (! buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = malloc(fs->fs_bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = block_map(f, (daddr_t)0, &disk_block);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (f->f_dev->dv_strategy)(f->f_devdata,
199767f8919635c4928607450d9e0abb932109ceToomas Soome F_READ, fsb_to_db(fs, disk_block),
199767f8919635c4928607450d9e0abb932109ceToomas Soome fs->fs_bsize, buf, &buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy((char *)buf, namebuf, link_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If relative pathname, restart at parent directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If absolute pathname, restart at root.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = namebuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp != '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome inumber = parent_inumber;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome inumber = (ino_t)EXT2_ROOTINO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = read_inode(inumber, f)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Found terminal component.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp->f_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp->f_fs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read a new inode into a file structure.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeread_inode(ino_t inumber, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs *fs = fp->f_fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2dinode *dp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t rsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int level, error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read inode and save it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = malloc(fs->fs_bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ino_to_db(fs, fp->f_bg, inumber), fs->fs_bsize, buf, &rsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rsize != fs->fs_bsize) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dp = (struct ext2dinode *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_di = dp[ino_to_bo(fs, inumber)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* clear out old buffers */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (level = 0; level < NIADDR; level++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_blkno[level] = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_buf_blkno = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Given an offset in a file, find the disk block number that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * contains that block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeblock_map(struct open_file *f, daddr_t file_block, daddr_t *disk_block_p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs *fs = fp->f_fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t ind_block_num;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t *ind_p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int idx, level;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Index structure of an inode:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * di_db[0..NDADDR-1] hold block numbers for blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 0..NDADDR-1
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * di_ib[0] index block 0 is the single indirect block
199767f8919635c4928607450d9e0abb932109ceToomas Soome * holds block numbers for blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NDADDR .. NDADDR + NINDIR(fs)-1
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * di_ib[1] index block 1 is the double indirect block
199767f8919635c4928607450d9e0abb932109ceToomas Soome * holds block numbers for INDEX blocks for blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NDADDR + NINDIR(fs) ..
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * di_ib[2] index block 2 is the triple indirect block
199767f8919635c4928607450d9e0abb932109ceToomas Soome * holds block numbers for double-indirect
199767f8919635c4928607450d9e0abb932109ceToomas Soome * blocks for blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NDADDR + NINDIR(fs) + NINDIR(fs)**2
199767f8919635c4928607450d9e0abb932109ceToomas Soome * + NINDIR(fs)**3 - 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (file_block < NDADDR) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Direct block. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *disk_block_p = fp->f_di.di_db[file_block];
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_block -= NDADDR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * nindir[0] = NINDIR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * nindir[1] = NINDIR**2
199767f8919635c4928607450d9e0abb932109ceToomas Soome * nindir[2] = NINDIR**3
199767f8919635c4928607450d9e0abb932109ceToomas Soome * etc
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (level = 0; level < NIADDR; level++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (file_block < fp->f_nindir[level])
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_block -= fp->f_nindir[level];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (level == NIADDR) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Block number too high */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EFBIG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ind_block_num = fp->f_di.di_ib[level];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; level >= 0; level--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ind_block_num == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *disk_block_p = 0; /* missing */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_blkno[level] != ind_block_num) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_blk[level] == (char *)0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_blk[level] =
199767f8919635c4928607450d9e0abb932109ceToomas Soome malloc(fs->fs_bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fsb_to_db(fp->f_fs, ind_block_num), fs->fs_bsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_blk[level], &fp->f_blksize[level]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_blksize[level] != fs->fs_bsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_blkno[level] = ind_block_num;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ind_p = (int32_t *)fp->f_blk[level];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (level > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome idx = file_block / fp->f_nindir[level - 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_block %= fp->f_nindir[level - 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome idx = file_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ind_block_num = ind_p[idx];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *disk_block_p = ind_block_num;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read a portion of a file into an internal buffer. Return
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the location in the buffer and the amount in the buffer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomebuf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2fs *fs = fp->f_fs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome long off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t file_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome daddr_t disk_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t block_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = blkoff(fs, fp->f_seekp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_block = lblkno(fs, fp->f_seekp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome block_size = dblksize(fs, &fp->f_di, file_block);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (file_block != fp->f_buf_blkno) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = block_map(f, file_block, &disk_block);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto done;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_buf == (char *)0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_buf = malloc(fs->fs_bsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (disk_block == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(fp->f_buf, block_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_buf_size = block_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fsb_to_db(fs, disk_block), block_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_buf, &fp->f_buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto done;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_buf_blkno = file_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return address of byte in buffer corresponding to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * offset, and size of remainder of buffer after that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * byte.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf_p = fp->f_buf + off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *size_p = block_size - off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * But truncate buffer at end of file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*size_p > fp->f_di.di_size - fp->f_seekp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *size_p = fp->f_di.di_size - fp->f_seekp;
199767f8919635c4928607450d9e0abb932109ceToomas Soomedone:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Search a directory for a name and return its
199767f8919635c4928607450d9e0abb932109ceToomas Soome * i_number.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomesearch_directory(char *name, struct open_file *f, ino_t *inumber_p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2dirent *dp, *edp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int namlen, length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome length = strlen(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (fp->f_seekp < fp->f_di.di_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = buf_read_file(f, &buf, &buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dp = (struct ext2dirent *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome edp = (struct ext2dirent *)(buf + buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (dp < edp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dp->d_ino == (ino_t)0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome namlen = dp->d_namlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (namlen == length &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncmp(name, dp->d_name, length) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* found entry */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *inumber_p = dp->d_ino;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome next:
199767f8919635c4928607450d9e0abb932109ceToomas Soome dp = (struct ext2dirent *)((char *)dp + dp->d_reclen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp += buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeext2fs_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int level;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *)0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp == (struct file *)0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (level = 0; level < NIADDR; level++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_blk[level])
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp->f_blk[level]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp->f_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_bg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp->f_bg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp->f_fs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeext2fs_read(struct open_file *f, void *addr, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t csize, buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_seekp >= fp->f_di.di_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = buf_read_file(f, &buf, &buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome csize = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (csize > buf_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome csize = buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(buf, addr, csize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp += csize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = (char *)addr + csize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size -= csize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeext2fs_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (where) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_SET:
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp = offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_CUR:
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp += offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_END:
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp = fp->f_di.di_size - offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EINVAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fp->f_seekp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeext2fs_stat(struct open_file *f, struct stat *sb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* only important stuff */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_mode = fp->f_di.di_mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_uid = fp->f_di.di_uid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_gid = fp->f_di.di_gid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = fp->f_di.di_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeext2fs_readdir(struct open_file *f, struct dirent *d)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file *fp = (struct file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ext2dirent *ed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * assume that a directory entry will not be split across blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeagain:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_seekp >= fp->f_di.di_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = buf_read_file(f, &buf, &buf_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ed = (struct ext2dirent *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_seekp += ed->d_reclen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ed->d_ino == (ino_t)0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto again;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_type = EXTFTODT(ed->d_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(d->d_name, ed->d_name, ed->d_namlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_name[ed->d_namlen] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}