#include <sys/param.h>
#include <sys/endian.h>
#include <sys/time.h>
#ifdef _STANDALONE
#include <lib/libkern/libkern.h>
#else
#include <stddef.h>
#include <string.h>
#endif
#include "stand.h"
#include "minixfs3.h"
#if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
#define LIBSA_NO_FS_SYMLINK
#endif
#if defined(LIBSA_NO_TWIDDLE)
#define twiddle()
#endif
typedef uint32_t ino32_t;
#ifndef FSBTODB
#define FSBTODB(fs, indp) MFS_FSBTODB(fs, indp)
#endif
#define LN2_IND_CACHE_SZ 6
#define IND_CACHE_SZ (1 << LN2_IND_CACHE_SZ)
#define IND_CACHE_MASK (IND_CACHE_SZ - 1)
struct file {
off_t f_seekp;
struct mfs_sblock *f_fs;
struct mfs_dinode f_di;
uint f_nishift;
block_t f_ind_cache_block;
block_t f_ind_cache[IND_CACHE_SZ];
char *f_buf;
size_t f_buf_size;
daddr_t f_buf_blkno;
};
static int read_inode(ino32_t, struct open_file *);
static int block_map(struct open_file *, block_t, block_t *);
static int buf_read_file(struct open_file *, void *, size_t *);
static int search_directory(const char *, int, struct open_file *, ino32_t *);
static int read_sblock(struct open_file *, struct mfs_sblock *);
static int
read_inode(ino32_t inumber, struct open_file *f)
{
struct file *fp = (struct file *)f->f_fsdata;
struct mfs_sblock *fs = fp->f_fs;
char *buf;
size_t rsize;
int rc;
daddr_t inode_sector;
struct mfs_dinode *dip;
inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
buf = fp->f_buf;
twiddle();
rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
inode_sector, fs->mfs_block_size, buf, &rsize);
if (rc)
return rc;
if (rsize != fs->mfs_block_size)
return EIO;
dip = (struct mfs_dinode *)(buf +
INODE_SIZE * ino_to_fsbo(fs, inumber));
mfs_iload(dip, &fp->f_di);
fp->f_ind_cache_block = ~0;
fp->f_buf_blkno = -1;
return rc;
}
static int
block_map(struct open_file *f, block_t file_block, block_t *disk_block_p)
{
struct file *fp = (struct file *)f->f_fsdata;
struct mfs_sblock *fs = fp->f_fs;
uint level;
block_t ind_cache;
block_t ind_block_num;
zone_t zone;
size_t rsize;
int rc;
int boff;
int scale = fs->mfs_log_zone_size;
block_t *buf = (void *)fp->f_buf;
zone = file_block >> scale;
boff = (int) (file_block - (zone << scale) );
if (zone < NR_DZONES) {
zone_t z = fs2h32(fp->f_di.mdi_zone[zone]);
if (z == NO_ZONE) {
*disk_block_p = NO_BLOCK;
return 0;
}
*disk_block_p = (block_t) ((z << scale) + boff);
return 0;
}
zone -= NR_DZONES;
ind_cache = zone >> LN2_IND_CACHE_SZ;
if (ind_cache == fp->f_ind_cache_block) {
*disk_block_p =
fs2h32(fp->f_ind_cache[zone & IND_CACHE_MASK]);
return 0;
}
for (level = 0;;) {
level += fp->f_nishift;
if (zone < (block_t)1 << level)
break;
if (level > NIADDR * fp->f_nishift)
return EFBIG;
zone -= (block_t)1 << level;
}
ind_block_num =
fs2h32(fp->f_di.mdi_zone[NR_DZONES + (level / fp->f_nishift - 1)]);
for (;;) {
level -= fp->f_nishift;
if (ind_block_num == 0) {
*disk_block_p = NO_BLOCK;
return 0;
}
twiddle();
rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
FSBTODB(fs, ind_block_num), fs->mfs_block_size,
buf, &rsize);
if (rc)
return rc;
if (rsize != fs->mfs_block_size)
return EIO;
ind_block_num = fs2h32(buf[zone >> level]);
if (level == 0)
break;
zone &= (1 << level) - 1;
}
memcpy(fp->f_ind_cache, &buf[zone & ~IND_CACHE_MASK],
IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
fp->f_ind_cache_block = ind_cache;
zone = (zone_t)ind_block_num;
*disk_block_p = (block_t)((zone << scale) + boff);
return 0;
}
static int
buf_read_file(struct open_file *f, void *v, size_t *size_p)
{
char **buf_p = v;
struct file *fp = (struct file *)f->f_fsdata;
struct mfs_sblock *fs = fp->f_fs;
long off;
block_t file_block;
block_t disk_block = 0;
size_t block_size, nsz;
int rc;
off = mfs_blkoff(fs, fp->f_seekp);
file_block = mfs_lblkno(fs, fp->f_seekp);
block_size = fs->mfs_block_size;
if (file_block != fp->f_buf_blkno) {
rc = block_map(f, file_block, &disk_block);
if (rc)
return rc;
if (disk_block == 0) {
memset(fp->f_buf, 0, block_size);
fp->f_buf_size = block_size;
} else {
twiddle();
rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
FSBTODB(fs, disk_block),
block_size, fp->f_buf, &fp->f_buf_size);
if (rc)
return rc;
}
fp->f_buf_blkno = file_block;
}
*buf_p = fp->f_buf + off;
*size_p = block_size - off;
nsz = (size_t)(fp->f_di.mdi_size - fp->f_seekp);
if (*size_p > nsz)
*size_p = nsz;
return 0;
}
static int
search_directory(const char *name, int length, struct open_file *f,
ino32_t *inumber_p)
{
struct file *fp = (struct file *)f->f_fsdata;
struct mfs_sblock *fs = fp->f_fs;
struct mfs_direct *dp;
struct mfs_direct *dbuf;
size_t buf_size;
int namlen;
int rc;
fp->f_seekp = 0;
while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
rc = buf_read_file(f, (void *)&dbuf, &buf_size);
if (rc)
return rc;
if (buf_size == 0)
return EIO;
if (buf_size < fs->mfs_block_size)
buf_size = fs->mfs_block_size;
for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
char *cp;
if (fs2h32(dp->mfsd_ino) == (ino32_t) 0)
continue;
cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
if (cp == NULL)
namlen = sizeof(dp->mfsd_name);
else
namlen = cp - (dp->mfsd_name);
if (namlen == length &&
!memcmp(name, dp->mfsd_name, length)) {
*inumber_p = fs2h32(dp->mfsd_ino);
return 0;
}
}
fp->f_seekp += buf_size;
}
return ENOENT;
}
int
read_sblock(struct open_file *f, struct mfs_sblock *fs)
{
static uint8_t sbbuf[MINBSIZE];
size_t buf_size;
int rc;
if (SBSIZE > MINBSIZE)
return EINVAL;
rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
SUPER_BLOCK_OFF / GETSECSIZE(f), MINBSIZE, sbbuf, &buf_size);
if (rc)
return rc;
if (buf_size != MINBSIZE)
return EIO;
mfs_sbload((void *)sbbuf, fs);
if (fs->mfs_magic != SUPER_MAGIC)
return EINVAL;
if (fs->mfs_block_size < MINBSIZE)
return EINVAL;
if ((fs->mfs_block_size % 512) != 0)
return EINVAL;
if (SBSIZE > fs->mfs_block_size)
return EINVAL;
if ((fs->mfs_block_size % INODE_SIZE) != 0)
return EINVAL;
if (fs->mfs_firstdatazone_old == 0) {
block_t offset;
offset = START_BLOCK + fs->mfs_imap_blocks + fs->mfs_zmap_blocks;
offset += (fs->mfs_ninodes + fs->mfs_inodes_per_block - 1) /
fs->mfs_inodes_per_block;
fs->mfs_firstdatazone =
(offset + (1 << fs->mfs_log_zone_size) - 1) >>
fs->mfs_log_zone_size;
} else {
fs->mfs_firstdatazone = (zone_t) fs->mfs_firstdatazone_old;
}
if (fs->mfs_imap_blocks < 1 || fs->mfs_zmap_blocks < 1
|| fs->mfs_ninodes < 1 || fs->mfs_zones < 1
|| fs->mfs_firstdatazone <= 4
|| fs->mfs_firstdatazone >= fs->mfs_zones
|| (unsigned) fs->mfs_log_zone_size > 4)
return EINVAL;
fs->mfs_inodes_per_block = fs->mfs_block_size / INODE_SIZE;
{
int32_t mult = fs->mfs_block_size >> LOG_MINBSIZE;
int ln2 = LOG_MINBSIZE;
for (; mult != 1; ln2++)
mult >>= 1;
fs->mfs_bshift = ln2;
fs->mfs_fsbtodb = ln2 - LOG_MINBSIZE + 1;
}
fs->mfs_qbmask = fs->mfs_block_size - 1;
fs->mfs_bmask = ~fs->mfs_qbmask;
return 0;
}
__compactcall int
minixfs3_open(const char *path, struct open_file *f)
{
#ifndef LIBSA_FS_SINGLECOMPONENT
const char *cp, *ncp;
int c;
#endif
ino32_t inumber;
struct file *fp;
struct mfs_sblock *fs;
int rc;
#ifndef LIBSA_NO_FS_SYMLINK
ino32_t parent_inumber;
int nlinks = 0;
char namebuf[MAXPATHLEN+1];
char *buf;
#endif
fp = alloc(sizeof(struct file));
memset(fp, 0, sizeof(struct file));
f->f_fsdata = (void *)fp;
fs = alloc(sizeof(*fs));
memset(fs, 0, sizeof(*fs));
fp->f_fs = fs;
twiddle();
rc = read_sblock(f, fs);
if (rc)
goto out;
fp->f_buf = alloc(fs->mfs_block_size);
{
int32_t mult;
int ln2;
mult = MFS_NINDIR(fs);
#ifdef DEBUG
if (!powerof2(mult)) {
rc = EINVAL;
goto out;
}
#endif
for (ln2 = 0; mult != 1; ln2++)
mult >>= 1;
fp->f_nishift = ln2;
}
inumber = ROOT_INODE;
if ((rc = read_inode(inumber, f)) != 0)
goto out;
#ifndef LIBSA_FS_SINGLECOMPONENT
cp = path;
while (*cp) {
while (*cp == '/')
cp++;
if (*cp == '\0')
break;
if ((fp->f_di.mdi_mode & I_TYPE) != I_DIRECTORY) {
rc = ENOTDIR;
goto out;
}
ncp = cp;
while ((c = *cp) != '\0' && c != '/')
cp++;
#ifndef LIBSA_NO_FS_SYMLINK
parent_inumber = inumber;
#endif
rc = search_directory(ncp, cp - ncp, f, &inumber);
if (rc)
goto out;
if ((rc = read_inode(inumber, f)) != 0)
goto out;
#ifndef LIBSA_NO_FS_SYMLINK
if ((fp->f_di.mdi_mode & I_TYPE) == I_SYMBOLIC_LINK) {
int link_len = fp->f_di.mdi_size;
int len;
size_t buf_size;
block_t disk_block;
len = strlen(cp);
if (link_len + len > MAXPATHLEN ||
++nlinks > MAXSYMLINKS) {
rc = ENOENT;
goto out;
}
memmove(&namebuf[link_len], cp, len + 1);
buf = fp->f_buf;
rc = block_map(f, (block_t)0, &disk_block);
if (rc)
goto out;
twiddle();
rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
F_READ, FSBTODB(fs, disk_block),
fs->mfs_block_size, buf, &buf_size);
if (rc)
goto out;
memcpy(namebuf, buf, link_len);
cp = namebuf;
if (*cp != '/')
inumber = parent_inumber;
else
inumber = (ino32_t) ROOT_INODE;
if ((rc = read_inode(inumber, f)) != 0)
goto out;
}
#endif
}
rc = 0;
#else
rc = search_directory(path, strlen(path), f, &inumber);
if (rc)
goto out;
rc = read_inode(inumber, f);
#endif
fp->f_seekp = 0;
out:
if (rc)
minixfs3_close(f);
return rc;
}
__compactcall int
minixfs3_close(struct open_file *f)
{
struct file *fp = (struct file *)f->f_fsdata;
f->f_fsdata = NULL;
if (fp == NULL)
return 0;
if (fp->f_buf)
dealloc(fp->f_buf, fp->f_fs->mfs_block_size);
dealloc(fp->f_fs, sizeof(*fp->f_fs));
dealloc(fp, sizeof(struct file));
return 0;
}
__compactcall int
minixfs3_read(struct open_file *f, void *start, size_t size, size_t *resid)
{
struct file *fp = (struct file *)f->f_fsdata;
size_t csize;
char *buf;
size_t buf_size;
int rc = 0;
char *addr = start;
while (size != 0) {
if (fp->f_seekp >= (off_t)fp->f_di.mdi_size)
break;
rc = buf_read_file(f, &buf, &buf_size);
if (rc)
break;
csize = size;
if (csize > buf_size)
csize = buf_size;
memcpy(addr, buf, csize);
fp->f_seekp += csize;
addr += csize;
size -= csize;
}
if (resid)
*resid = size;
return rc;
}
#ifndef LIBSA_NO_FS_WRITE
__compactcall int
minixfs3_write(struct open_file *f, void *start, size_t size, size_t *resid)
{
return EROFS;
}
#endif
#ifndef LIBSA_NO_FS_SEEK
__compactcall off_t
minixfs3_seek(struct open_file *f, off_t offset, int where)
{
struct file *fp = (struct file *)f->f_fsdata;
switch (where) {
case SEEK_SET:
fp->f_seekp = offset;
break;
case SEEK_CUR:
fp->f_seekp += offset;
break;
case SEEK_END:
fp->f_seekp = fp->f_di.mdi_size - offset;
break;
default:
return -1;
}
return fp->f_seekp;
}
#endif
__compactcall int
minixfs3_stat(struct open_file *f, struct stat *sb)
{
struct file *fp = (struct file *)f->f_fsdata;
memset(sb, 0, sizeof *sb);
sb->st_mode = fp->f_di.mdi_mode;
sb->st_uid = fp->f_di.mdi_uid;
sb->st_gid = fp->f_di.mdi_gid;
sb->st_size = fp->f_di.mdi_size;
return 0;
}
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
__compactcall void
minixfs3_ls(struct open_file *f, const char *pattern)
{
struct file *fp = (struct file *)f->f_fsdata;
struct mfs_sblock *fs = fp->f_fs;
struct mfs_direct *dp;
struct mfs_direct *dbuf;
size_t buf_size;
lsentry_t *names = 0;
fp->f_seekp = 0;
while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
int rc = buf_read_file(f, &dbuf, &buf_size);
if (rc)
goto out;
if (buf_size < fs->mfs_block_size)
buf_size = fs->mfs_block_size;
for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
char *cp;
int namlen;
if (fs2h32(dp->mfsd_ino) == 0)
continue;
cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
if (cp == NULL)
namlen = sizeof(dp->mfsd_name);
else
namlen = cp - (dp->mfsd_name);
lsadd(&names, pattern, dp->mfsd_name, namlen,
fs2h32(dp->mfsd_ino), "?");
}
fp->f_seekp += buf_size;
}
lsprint(names);
out: lsfree(names);
}
#endif
#if BYTE_ORDER == BIG_ENDIAN
void
minixfs3_sb_bswap(struct mfs_sblock *old, struct mfs_sblock *new)
{
new->mfs_ninodes = bswap32(old->mfs_ninodes);
new->mfs_nzones = bswap16(old->mfs_nzones);
new->mfs_imap_blocks = bswap16(old->mfs_imap_blocks);
new->mfs_zmap_blocks = bswap16(old->mfs_zmap_blocks);
new->mfs_firstdatazone_old = bswap16(old->mfs_firstdatazone_old);
new->mfs_log_zone_size = bswap16(old->mfs_log_zone_size);
new->mfs_max_size = bswap32(old->mfs_max_size);
new->mfs_zones = bswap32(old->mfs_zones);
new->mfs_magic = bswap16(old->mfs_magic);
new->mfs_block_size = bswap16(old->mfs_block_size);
new->mfs_disk_version = old->mfs_disk_version;
}
void minixfs3_i_bswap(struct mfs_dinode *old, struct mfs_dinode *new)
{
int i;
new->mdi_mode = bswap16(old->mdi_mode);
new->mdi_nlinks = bswap16(old->mdi_nlinks);
new->mdi_uid = bswap16(old->mdi_uid);
new->mdi_gid = bswap16(old->mdi_gid);
new->mdi_size = bswap32(old->mdi_size);
new->mdi_atime = bswap32(old->mdi_atime);
new->mdi_mtime = bswap32(old->mdi_mtime);
new->mdi_ctime = bswap32(old->mdi_ctime);
for (i = 0; i < NR_TZONES; i++)
new->mdi_zone[i] = old->mdi_zone[i];
}
#endif