#include <sys/param.h>
#include <sys/time.h>
#include "stand.h"
#include <vfs/ufs/dinode.h>
#include <vfs/ufs/dir.h>
#include <vfs/ufs/fs.h>
#include "string.h"
static int ufs_open(const char *path, struct open_file *f);
static int ufs_close(struct open_file *f);
static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
static off_t ufs_seek(struct open_file *f, off_t offset, int where);
static int ufs_stat(struct open_file *f, struct stat *sb);
static int ufs_readdir(struct open_file *f, struct dirent *d);
struct fs_ops ufs_fsops = {
"ufs",
ufs_open,
ufs_close,
ufs_read,
null_write,
ufs_seek,
ufs_stat,
ufs_readdir
};
struct file {
off_t f_seekp;
struct fs *f_fs;
struct ufs1_dinode f_di;
int f_nindir[UFS_NIADDR];
char *f_blk[UFS_NIADDR];
size_t f_blksize[UFS_NIADDR];
daddr_t f_blkno[UFS_NIADDR];
char *f_buf;
size_t f_buf_size;
daddr_t f_buf_blkno;
};
static int read_inode(ino_t, struct open_file *);
static int block_map(struct open_file *, daddr_t, daddr_t *);
static int buf_read_file(struct open_file *, char **, size_t *);
static int search_directory(char *, struct open_file *, ino_t *);
#ifdef COMPAT_UFS
static void ffs_oldfscompat(struct fs *);
#endif
static int
read_inode(ino_t inumber, struct open_file *f)
{
struct file *fp = (struct file *)f->f_fsdata;
struct fs *fs = fp->f_fs;
char *buf;
size_t rsize;
int rc;
if (fs == NULL)
panic("fs == NULL");
buf = malloc(fs->fs_bsize);
twiddle();
rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
buf, &rsize);
if (rc)
goto out;
if (rsize != fs->fs_bsize) {
rc = EIO;
goto out;
}
{
struct ufs1_dinode *dp;
dp = (struct ufs1_dinode *)buf;
fp->f_di = dp[ino_to_fsbo(fs, inumber)];
}
{
int level;
for (level = 0; level < UFS_NIADDR; level++)
fp->f_blkno[level] = -1;
fp->f_buf_blkno = -1;
}
out:
free(buf);
return (rc);
}
static int
block_map(struct open_file *f, daddr_t file_block, daddr_t *disk_block_p)
{
struct file *fp = (struct file *)f->f_fsdata;
struct fs *fs = fp->f_fs;
int level;
int idx;
daddr_t ind_block_num;
daddr_t *ind_p;
int rc;
if (file_block < UFS_NDADDR) {
*disk_block_p = fp->f_di.di_db[file_block];
return (0);
}
file_block -= UFS_NDADDR;
for (level = 0; level < UFS_NIADDR; level++) {
if (file_block < fp->f_nindir[level])
break;
file_block -= fp->f_nindir[level];
}
if (level == UFS_NIADDR) {
return (EFBIG);
}
ind_block_num = fp->f_di.di_ib[level];
for (; level >= 0; level--) {
if (ind_block_num == 0) {
*disk_block_p = 0;
return (0);
}
if (fp->f_blkno[level] != ind_block_num) {
if (fp->f_blk[level] == NULL)
fp->f_blk[level] =
malloc(fs->fs_bsize);
twiddle();
rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
fsbtodb(fp->f_fs, ind_block_num),
fs->fs_bsize,
fp->f_blk[level],
&fp->f_blksize[level]);
if (rc)
return (rc);
if (fp->f_blksize[level] != fs->fs_bsize)
return (EIO);
fp->f_blkno[level] = ind_block_num;
}
ind_p = (daddr_t *)fp->f_blk[level];
if (level > 0) {
idx = file_block / fp->f_nindir[level - 1];
file_block %= fp->f_nindir[level - 1];
} else
idx = file_block;
ind_block_num = ind_p[idx];
}
*disk_block_p = ind_block_num;
return (0);
}
static int
buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
{
struct file *fp = (struct file *)f->f_fsdata;
struct fs *fs = fp->f_fs;
long off;
daddr_t file_block;
daddr_t disk_block;
size_t block_size;
int rc;
off = blkoff(fs, fp->f_seekp);
file_block = lblkno(fs, fp->f_seekp);
block_size = dblksize(fs, &fp->f_di, file_block);
if (file_block != fp->f_buf_blkno) {
rc = block_map(f, file_block, &disk_block);
if (rc)
return (rc);
if (fp->f_buf == NULL)
fp->f_buf = malloc(fs->fs_bsize);
if (disk_block == 0) {
bzero(fp->f_buf, block_size);
fp->f_buf_size = block_size;
} else {
twiddle();
rc = (f->f_dev->dv_strategy)(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;
if (*size_p > fp->f_di.di_size - fp->f_seekp)
*size_p = fp->f_di.di_size - fp->f_seekp;
return (0);
}
static int
search_directory(char *name, struct open_file *f, ino_t *inumber_p)
{
struct file *fp = (struct file *)f->f_fsdata;
struct direct *dp;
struct direct *edp;
char *buf;
size_t buf_size;
int namlen, length;
int rc;
length = strlen(name);
fp->f_seekp = 0;
while (fp->f_seekp < fp->f_di.di_size) {
rc = buf_read_file(f, &buf, &buf_size);
if (rc)
return (rc);
dp = (struct direct *)buf;
edp = (struct direct *)(buf + buf_size);
while (dp < edp) {
if (dp->d_ino == (ino_t)0)
goto next;
if (dp->d_type == DT_WHT)
goto next;
#if BYTE_ORDER == LITTLE_ENDIAN
if (fp->f_fs->fs_maxsymlinklen <= 0)
namlen = dp->d_type;
else
#endif
namlen = dp->d_namlen;
if (namlen == length &&
!strcmp(name, dp->d_name)) {
*inumber_p = dp->d_ino;
return (0);
}
next:
dp = (struct direct *)((char *)dp + dp->d_reclen);
}
fp->f_seekp += buf_size;
}
return (ENOENT);
}
static int
ufs_open(const char *upath, struct open_file *f)
{
char *cp, *ncp;
int c;
ino_t inumber, parent_inumber;
struct file *fp;
struct fs *fs;
int rc;
size_t buf_size;
int nlinks = 0;
char namebuf[MAXPATHLEN+1];
char *buf = NULL;
char *path = NULL;
fp = malloc(sizeof(struct file));
bzero(fp, sizeof(struct file));
f->f_fsdata = (void *)fp;
fs = malloc(SBSIZE);
fp->f_fs = fs;
twiddle();
rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
SBOFF / DEV_BSIZE, SBSIZE, (char *)fs, &buf_size);
if (rc)
goto out;
if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
rc = EINVAL;
goto out;
}
#ifdef COMPAT_UFS
ffs_oldfscompat(fs);
#endif
{
int omult;
int mult;
int level;
omult = 0;
mult = 1;
for (level = 0; level < UFS_NIADDR; level++) {
mult *= NINDIR(fs);
if (mult < omult)
mult = 0x7FFFFFFF;
fp->f_nindir[level] = mult;
omult = mult;
}
}
inumber = UFS_ROOTINO;
if ((rc = read_inode(inumber, f)) != 0)
goto out;
cp = path = strdup(upath);
if (path == NULL) {
rc = ENOMEM;
goto out;
}
while (*cp) {
while (*cp == '/')
cp++;
if (*cp == '\0')
break;
if ((fp->f_di.di_mode & IFMT) != IFDIR) {
rc = ENOTDIR;
goto out;
}
{
int len = 0;
ncp = cp;
while ((c = *cp) != '\0' && c != '/') {
if (++len > MAXNAMLEN) {
rc = ENOENT;
goto out;
}
cp++;
}
*cp = '\0';
}
parent_inumber = inumber;
rc = search_directory(ncp, f, &inumber);
*cp = c;
if (rc)
goto out;
if ((rc = read_inode(inumber, f)) != 0)
goto out;
if ((fp->f_di.di_mode & IFMT) == IFLNK) {
int link_len = fp->f_di.di_size;
int len;
len = strlen(cp);
if (link_len + len > MAXPATHLEN ||
++nlinks > MAXSYMLINKS) {
rc = ENOENT;
goto out;
}
bcopy(cp, &namebuf[link_len], len + 1);
if (link_len < fs->fs_maxsymlinklen) {
bcopy(fp->f_di.di_shortlink, namebuf,
(unsigned) link_len);
} else {
size_t buf_size;
daddr_t disk_block;
struct fs *fs = fp->f_fs;
if (!buf)
buf = malloc(fs->fs_bsize);
rc = block_map(f, (daddr_t)0, &disk_block);
if (rc)
goto out;
twiddle();
rc = (f->f_dev->dv_strategy)(f->f_devdata,
F_READ, fsbtodb(fs, disk_block),
fs->fs_bsize, buf, &buf_size);
if (rc)
goto out;
bcopy((char *)buf, namebuf, (unsigned)link_len);
}
cp = namebuf;
if (*cp != '/')
inumber = parent_inumber;
else
inumber = (ino_t)UFS_ROOTINO;
if ((rc = read_inode(inumber, f)) != 0)
goto out;
}
}
fp->f_seekp = 0;
rc = 0;
out:
if (buf)
free(buf);
if (path)
free(path);
if (rc) {
f->f_fsdata = NULL;
if (fp->f_buf)
free(fp->f_buf);
free(fp->f_fs);
free(fp);
}
return (rc);
}
static int
ufs_close(struct open_file *f)
{
struct file *fp = (struct file *)f->f_fsdata;
int level;
f->f_fsdata = NULL;
if (fp == NULL)
return (0);
for (level = 0; level < UFS_NIADDR; level++) {
if (fp->f_blk[level])
free(fp->f_blk[level]);
}
if (fp->f_buf)
free(fp->f_buf);
free(fp->f_fs);
free(fp);
return (0);
}
static int
ufs_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 >= fp->f_di.di_size)
break;
rc = buf_read_file(f, &buf, &buf_size);
if (rc)
break;
csize = size;
if (csize > buf_size)
csize = buf_size;
bcopy(buf, addr, csize);
fp->f_seekp += csize;
addr += csize;
size -= csize;
}
if (resid)
*resid = size;
return (rc);
}
static off_t
ufs_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.di_size - offset;
break;
default:
return (-1);
}
return (fp->f_seekp);
}
static int
ufs_stat(struct open_file *f, struct stat *sb)
{
struct file *fp = (struct file *)f->f_fsdata;
sb->st_mode = fp->f_di.di_mode;
sb->st_uid = fp->f_di.di_uid;
sb->st_gid = fp->f_di.di_gid;
sb->st_size = fp->f_di.di_size;
return (0);
}
static int
ufs_readdir(struct open_file *f, struct dirent *d)
{
struct file *fp = (struct file *)f->f_fsdata;
struct direct *dp;
char *buf;
size_t buf_size;
int error;
again:
if (fp->f_seekp >= fp->f_di.di_size)
return (ENOENT);
error = buf_read_file(f, &buf, &buf_size);
if (error)
return (error);
dp = (struct direct *)buf;
fp->f_seekp += dp->d_reclen;
if (dp->d_ino == (ino_t)0)
goto again;
d->d_type = dp->d_type;
strcpy(d->d_name, dp->d_name);
return (0);
}
#ifdef COMPAT_UFS
static void
ffs_oldfscompat(struct fs *fs)
{
int i;
fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);
fs->fs_interleave = max(fs->fs_interleave, 1);
if (fs->fs_postblformat == FS_42POSTBLFMT)
fs->fs_nrpos = 8;
if (fs->fs_inodefmt < FS_44INODEFMT) {
quad_t sizepb = fs->fs_bsize;
fs->fs_maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
for (i = 0; i < UFS_NIADDR; i++) {
sizepb *= NINDIR(fs);
fs->fs_maxfilesize += sizepb;
}
fs->fs_qbmask = ~fs->fs_bmask;
fs->fs_qfmask = ~fs->fs_fmask;
}
}
#endif