#include <sys/param.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_fsdir.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_inode.h>
#include <sys/sysmacros.h>
#include <sys/promif.h>
#include <sys/stat.h>
#include <sys/bootvfs.h>
#include <sys/bootdebug.h>
#include <sys/salib.h>
#include <sys/sacache.h>
int print_cache_stats = 0;
static fileid_t *head;
extern void lufs_boot_init(fileid_t *);
extern void lufs_closeall(void);
extern void lufs_merge_deltas(fileid_t *);
devid_t *ufs_devp;
struct dirinfo {
int loc;
fileid_t *fi;
};
static int boot_ufs_mountroot(char *str);
static int boot_ufs_unmountroot(void);
static int boot_ufs_open(char *filename, int flags);
static int boot_ufs_close(int fd);
static ssize_t boot_ufs_read(int fd, caddr_t buf, size_t size);
static off_t boot_ufs_lseek(int, off_t, int);
static int boot_ufs_fstat(int fd, struct bootstat *stp);
static void boot_ufs_closeall(int flag);
static int boot_ufs_getdents(int fd, struct dirent *dep, unsigned size);
struct boot_fs_ops boot_ufs_ops = {
"ufs",
boot_ufs_mountroot,
boot_ufs_unmountroot,
boot_ufs_open,
boot_ufs_close,
boot_ufs_read,
boot_ufs_lseek,
boot_ufs_fstat,
boot_ufs_closeall,
boot_ufs_getdents
};
static ino_t find(fileid_t *filep, char *path);
static ino_t dlook(fileid_t *filep, char *path);
static daddr32_t sbmap(fileid_t *filep, daddr32_t bn);
static struct direct *readdir(struct dirinfo *dstuff);
#define NBUFS (NIADDR+1)
static union {
char *blk[NBUFS];
daddr32_t *dummy;
} b;
daddr32_t blknos[NBUFS];
static int
openi(fileid_t *filep, ino_t inode)
{
int retval;
struct dinode *dp;
devid_t *devp = filep->fi_devp;
if ((filep->fi_inode = get_icache(devp->di_dcookie, inode)) != NULL)
return (0);
filep->fi_offset = 0;
filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs,
itod(&devp->un_fs.di_fs, inode));
filep->fi_count = devp->un_fs.di_fs.fs_bsize;
filep->fi_memp = filep->fi_buf;
if ((filep->fi_memp = get_bcache(filep)) == NULL) {
if (retval = set_bcache(filep))
return (retval);
lufs_merge_deltas(filep);
}
dp = (struct dinode *)filep->fi_memp;
filep->fi_inode = (struct inode *)
bkmem_alloc(sizeof (struct inode));
bzero((char *)filep->fi_inode, sizeof (struct inode));
filep->fi_inode->i_ic =
dp[itoo(&devp->un_fs.di_fs, inode)].di_un.di_icom;
filep->fi_inode->i_number = inode;
if (set_ricache(devp->di_dcookie, inode, (void *)filep->fi_inode,
sizeof (struct inode)))
filep->fi_inode->i_flag = FI_NOCACHE;
return (0);
}
static fileid_t *
find_fp(int fd)
{
fileid_t *filep = head;
if (fd >= 0) {
while ((filep = filep->fi_forw) != head)
if (fd == filep->fi_filedes)
return (filep->fi_taken ? filep : 0);
}
return (0);
}
static ino_t
find(fileid_t *filep, char *path)
{
char *q;
char c;
ino_t inode;
char lpath[MAXPATHLEN];
char *lpathp = lpath;
int len, r;
devid_t *devp;
if (path == NULL || *path == '\0') {
printf("null path\n");
return ((ino_t)0);
}
bzero(lpath, sizeof (lpath));
bcopy(path, lpath, strlen(path));
devp = filep->fi_devp;
while (*lpathp) {
r = (lpathp == lpath);
if (r && openi(filep, (ino_t)UFSROOTINO))
return ((ino_t)0);
while (*lpathp == '/')
lpathp++;
q = lpathp;
while (*q != '/' && *q != '\0')
q++;
c = *q;
*q = '\0';
if (r && (*lpathp == '\0'))
return ((ino_t)UFSROOTINO);
if ((inode = dlook(filep, lpathp)) != 0) {
if (openi(filep, inode))
return ((ino_t)0);
if ((filep->fi_inode->i_smode & IFMT) == IFLNK) {
filep->fi_blocknum =
fsbtodb(&devp->un_fs.di_fs,
filep->fi_inode->i_db[0]);
filep->fi_count = DEV_BSIZE;
if ((filep->fi_memp =
get_bcache(filep)) == NULL) {
if (set_bcache(filep))
return ((ino_t)0);
lufs_merge_deltas(filep);
}
len = strlen(filep->fi_memp);
if (filep->fi_memp[0] == '/')
lpathp = lpath;
bcopy(q, lpathp + len, strlen(q + 1) + 2);
*(lpathp + len) = c;
bcopy(filep->fi_memp, lpathp, len);
lpathp = lpath;
continue;
} else
*q = c;
if (c == '\0')
break;
lpathp = q;
continue;
} else {
return ((ino_t)0);
}
}
return (inode);
}
static daddr32_t
sbmap(fileid_t *filep, daddr32_t bn)
{
struct inode *inodep;
int i, j, sh;
daddr32_t nb, *bap;
daddr32_t *db;
devid_t *devp;
devp = filep->fi_devp;
inodep = filep->fi_inode;
db = inodep->i_db;
if (bn < NDADDR) {
nb = db[bn];
return (nb);
}
sh = 1;
bn -= NDADDR;
for (j = NIADDR; j > 0; j--) {
sh *= NINDIR(&devp->un_fs.di_fs);
if (bn < sh)
break;
bn -= sh;
}
if (j == 0) {
return ((daddr32_t)0);
}
nb = inodep->i_ib[NIADDR - j];
if (nb == 0) {
return ((daddr32_t)0);
}
for (; j <= NIADDR; j++) {
if (blknos[j] != nb) {
filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs, nb);
filep->fi_count = devp->un_fs.di_fs.fs_bsize;
if ((filep->fi_memp = get_bcache(filep)) == NULL) {
if (set_bcache(filep))
return (0);
lufs_merge_deltas(filep);
}
b.blk[j] = filep->fi_memp;
blknos[j] = nb;
}
bap = (daddr32_t *)b.blk[j];
sh /= NINDIR(&devp->un_fs.di_fs);
i = (bn / sh) % NINDIR(&devp->un_fs.di_fs);
nb = bap[i];
if (nb == 0) {
return ((daddr32_t)0);
}
}
return (nb);
}
static ino_t
dlook(fileid_t *filep, char *path)
{
devid_t *devp = filep->fi_devp;
struct direct *dp;
struct inode *ip;
struct dirinfo dirp;
int len;
ino_t in;
#ifdef DEBUG
static int warned = 0;
#endif
ip = filep->fi_inode;
if (path == NULL || *path == '\0')
return (0);
if ((ip->i_smode & IFMT) != IFDIR)
return (0);
if (ip->i_size == 0)
return (0);
len = strlen(path);
if ((in = get_dcache(devp->di_dcookie, path, ip->i_number)) != 0)
return (in);
if (ip->i_flag & FI_CACHED)
return (0);
in = 0;
dirp.loc = 0;
dirp.fi = filep;
if (!(ip->i_flag & FI_NOCACHE))
ip->i_flag |= FI_CACHED;
for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
if (dp->d_ino == 0)
continue;
if (dp->d_namlen == len && strcmp(path, dp->d_name) == 0)
in = dp->d_ino;
if (strcmp(path, "*") == 0)
printf("%s\n", dp->d_name);
if (ip->i_flag & FI_NOCACHE)
continue;
if ((ip->i_flag & FI_PARTIAL_CACHE) &&
(get_dcache(devp->di_dcookie, dp->d_name, dp->d_ino) != 0))
continue;
if (set_rdcache(devp->di_dcookie, dp->d_name, ip->i_number,
dp->d_ino)) {
ip->i_flag &= ~FI_CACHED;
ip->i_flag |= FI_PARTIAL_CACHE;
#ifdef DEBUG
if (!warned) {
printf("ufsboot: directory cache too small\n");
warned++;
}
#endif
}
}
return (in);
}
struct direct *
readdir(struct dirinfo *dstuff)
{
struct direct *dp;
fileid_t *filep;
daddr32_t lbn, d;
int off;
devid_t *devp;
filep = dstuff->fi;
devp = filep->fi_devp;
for (;;) {
if (dstuff->loc >= filep->fi_inode->i_size) {
return (NULL);
}
off = blkoff(&devp->un_fs.di_fs, dstuff->loc);
if (off == 0) {
lbn = lblkno(&devp->un_fs.di_fs, dstuff->loc);
d = sbmap(filep, lbn);
if (d <= 0)
return (NULL);
filep->fi_blocknum = fsbtodb(&devp->un_fs.di_fs, d);
filep->fi_count =
blksize(&devp->un_fs.di_fs, filep->fi_inode, lbn);
if ((filep->fi_memp = get_bcache(filep)) == NULL) {
if (set_bcache(filep))
return (NULL);
lufs_merge_deltas(filep);
}
}
dp = (struct direct *)(filep->fi_memp + off);
dstuff->loc += dp->d_reclen;
if (dp->d_ino == 0)
continue;
return (dp);
}
}
static int
getblock(fileid_t *filep, caddr_t buf, int count, int *rcount)
{
struct fs *fs;
caddr_t p;
int off, size, diff, zeroize;
daddr32_t lbn, fsbn;
devid_t *devp;
static int pos;
static char ind[] = "|/-\\";
static int blks_read;
devp = filep->fi_devp;
p = filep->fi_memp;
if ((signed)filep->fi_count <= 0) {
diff = filep->fi_inode->i_size - filep->fi_offset;
if (diff <= 0) {
printf("Short read\n");
return (-1);
}
fs = &devp->un_fs.di_fs;
lbn = lblkno(fs, filep->fi_offset);
fsbn = sbmap(filep, lbn);
zeroize = (fsbn <= 0);
filep->fi_blocknum = fsbtodb(fs, fsbn);
off = blkoff(fs, filep->fi_offset);
size = blksize(fs, filep->fi_inode, lbn);
filep->fi_count = size;
filep->fi_memp = filep->fi_buf;
*rcount = 0;
if (off == 0 && count >= size) {
filep->fi_memp = buf;
if (zeroize) {
bzero(buf, size);
} else if (diskread(filep)) {
return (-1);
}
*rcount = size;
filep->fi_count = 0;
read_opt++;
if ((blks_read++ & 0x3) == 0)
printf("%c\b", ind[pos++ & 3]);
return (0);
} else {
if (zeroize) {
bzero(filep->fi_memp, size);
} else if (diskread(filep))
return (-1);
}
if ((blks_read++ & 0x3) == 0)
printf("%c\b", ind[pos++ & 3]);
if (filep->fi_offset - off + size >= filep->fi_inode->i_size)
filep->fi_count = diff + off;
filep->fi_count -= off;
p = &filep->fi_memp[off];
}
filep->fi_memp = p;
return (0);
}
static ssize_t
boot_ufs_read(int fd, caddr_t buf, size_t count)
{
size_t i, j;
caddr_t n;
int rcount;
fileid_t *filep;
if (!(filep = find_fp(fd))) {
return (-1);
}
if (filep->fi_offset + count > filep->fi_inode->i_size)
count = filep->fi_inode->i_size - filep->fi_offset;
if ((i = count) == 0)
return (0);
n = buf;
while (i > 0) {
if ((j = filep->fi_count) == 0) {
(void) getblock(filep, buf, i, &rcount);
i -= rcount;
buf += rcount;
filep->fi_offset += rcount;
} else {
j = MIN(i, j);
bcopy(filep->fi_memp, buf, (unsigned)j);
buf += j;
filep->fi_memp += j;
filep->fi_offset += j;
filep->fi_count -= j;
i -= j;
}
}
return (buf - n);
}
static int
boot_ufs_mountroot(char *str)
{
int h;
if (ufs_devp == NULL) {
if (strchr(str, ':') == NULL) {
(void) strcat(str, ":a");
}
h = prom_open(str);
if (h == 0) {
printf("Cannot open %s\n", str);
return (-1);
}
ufs_devp = (devid_t *)bkmem_alloc(sizeof (devid_t));
ufs_devp->di_taken = 1;
ufs_devp->di_dcookie = h;
ufs_devp->di_desc = (char *)bkmem_alloc(strlen(str) + 1);
(void) strcpy(ufs_devp->di_desc, str);
bzero(ufs_devp->un_fs.dummy, SBSIZE);
head = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
head->fi_back = head->fi_forw = head;
head->fi_filedes = 0;
head->fi_taken = 0;
head->fi_devp = ufs_devp;
head->fi_blocknum = SBLOCK;
head->fi_count = (uint_t)SBSIZE;
head->fi_memp = (caddr_t)&(ufs_devp->un_fs.di_fs);
head->fi_offset = 0;
if (diskread(head) ||
ufs_devp->un_fs.di_fs.fs_magic != FS_MAGIC) {
boot_ufs_closeall(1);
return (-1);
}
lufs_boot_init(head);
}
return (0);
}
int
boot_ufs_unmountroot(void)
{
if (ufs_devp == NULL)
return (-1);
boot_ufs_closeall(1);
return (0);
}
static int
boot_ufs_open(char *filename, int flags)
{
fileid_t *filep;
ino_t inode;
static int filedes = 1;
filep = (fileid_t *)bkmem_alloc(sizeof (fileid_t));
filep->fi_back = head->fi_back;
filep->fi_forw = head;
head->fi_back->fi_forw = filep;
head->fi_back = filep;
filep->fi_filedes = filedes++;
filep->fi_taken = 1;
filep->fi_path = (char *)bkmem_alloc(strlen(filename) + 1);
(void) strcpy(filep->fi_path, filename);
filep->fi_devp = ufs_devp;
filep->fi_inode = NULL;
bzero(filep->fi_buf, MAXBSIZE);
inode = find(filep, filename);
if (inode == (ino_t)0) {
(void) boot_ufs_close(filep->fi_filedes);
return (-1);
}
if (openi(filep, inode)) {
(void) boot_ufs_close(filep->fi_filedes);
return (-1);
}
filep->fi_offset = filep->fi_count = 0;
return (filep->fi_filedes);
}
static off_t
boot_ufs_lseek(int fd, off_t addr, int whence)
{
fileid_t *filep;
if (!(filep = find_fp(fd)))
return (-1);
switch (whence) {
case SEEK_CUR:
filep->fi_offset += addr;
break;
case SEEK_SET:
filep->fi_offset = addr;
break;
default:
case SEEK_END:
printf("ufs_lseek(): invalid whence value %d\n", whence);
break;
}
filep->fi_blocknum = addr / DEV_BSIZE;
filep->fi_count = 0;
return (0);
}
static int
boot_ufs_fstat(int fd, struct bootstat *stp)
{
fileid_t *filep;
struct inode *ip;
if (!(filep = find_fp(fd)))
return (-1);
ip = filep->fi_inode;
stp->st_mode = 0;
stp->st_size = 0;
if (ip == NULL)
return (0);
switch (ip->i_smode & IFMT) {
case IFDIR:
stp->st_mode = S_IFDIR;
break;
case IFLNK:
stp->st_mode = S_IFLNK;
break;
case IFREG:
stp->st_mode = S_IFREG;
break;
default:
break;
}
stp->st_size = ip->i_size;
stp->st_atim.tv_sec = ip->i_atime.tv_sec;
stp->st_atim.tv_nsec = ip->i_atime.tv_usec * 1000;
stp->st_mtim.tv_sec = ip->i_mtime.tv_sec;
stp->st_mtim.tv_nsec = ip->i_mtime.tv_usec * 1000;
stp->st_ctim.tv_sec = ip->i_ctime.tv_sec;
stp->st_ctim.tv_nsec = ip->i_ctime.tv_usec * 1000;
return (0);
}
static int
boot_ufs_close(int fd)
{
fileid_t *filep;
if (!(filep = find_fp(fd)))
return (-1);
if (filep->fi_taken && (filep != head)) {
bkmem_free(filep->fi_path, strlen(filep->fi_path)+1);
filep->fi_blocknum = filep->fi_count = filep->fi_offset = 0;
filep->fi_memp = (caddr_t)0;
filep->fi_devp = 0;
filep->fi_taken = 0;
filep->fi_forw->fi_back = filep->fi_back;
filep->fi_back->fi_forw = filep->fi_forw;
bkmem_free((char *)filep, sizeof (fileid_t));
return (0);
} else {
printf("\nFile descrip %d not allocated!", fd);
return (-1);
}
}
static void
boot_ufs_closeall(int flag)
{
fileid_t *filep = head;
if (ufs_devp == NULL) {
if (head)
prom_panic("boot_ufs_closeall: head != NULL.\n");
return;
}
while ((filep = filep->fi_forw) != head)
if (filep->fi_taken)
if (boot_ufs_close(filep->fi_filedes))
prom_panic("Filesystem may be inconsistent.\n");
release_cache(ufs_devp->di_dcookie);
(void) prom_close(ufs_devp->di_dcookie);
ufs_devp->di_taken = 0;
if (verbosemode & print_cache_stats)
print_cache_data();
lufs_closeall();
bkmem_free((char *)ufs_devp, sizeof (devid_t));
bkmem_free((char *)head, sizeof (fileid_t));
ufs_devp = (devid_t *)NULL;
head = (fileid_t *)NULL;
}
static int
boot_ufs_getdents(int fd, struct dirent *dep, unsigned size)
{
int n;
fileid_t *fp;
unsigned long oldoff, oldblok;
#define SLOP (sizeof (struct dirent) - offsetof(struct dirent, d_name[1]))
if (fp = find_fp(fd)) {
while ((fp->fi_inode->i_smode & IFMT) == IFLNK) {
fileid_t fx;
char pn[MAXPATHLEN];
fp->fi_count = DEV_BSIZE;
fp->fi_blocknum = fsbtodb(&fp->fi_devp->un_fs.di_fs,
fp->fi_inode->i_db[0]);
if ((fp->fi_memp = get_bcache(fp)) == NULL) {
if (set_bcache(fp))
return (-1);
lufs_merge_deltas(fp);
}
if (!(n = find(&fx, strcpy(pn, fp->fi_memp))) ||
openi(fp = &fx, n)) {
return (-1);
}
}
if ((fp->fi_inode->i_smode & IFMT) == IFDIR) {
int cnt = 0;
struct direct *dp;
struct dirinfo dir;
dir.fi = fp;
oldblok = fp->fi_blocknum;
dir.loc = oldoff = fp->fi_offset;
for (dp = readdir(&dir); dp; dp = readdir(&dir)) {
if (dp->d_ino) {
dep->d_ino = dp->d_ino;
n = strlen(dp->d_name);
n = roundup((sizeof (struct dirent) +
((n > SLOP) ? n : 0)),
sizeof (off_t));
if (n > size)
break;
oldblok = fp->fi_blocknum;
oldoff = dir.loc;
size -= n;
cnt += 1;
(void) strlcpy(dep->d_name, dp->d_name,
strlen(dp->d_name) + 1);
dep->d_off = dir.loc;
dep->d_reclen = (ushort_t)n;
dep = (struct dirent *)
((char *)dep + n);
}
}
fp->fi_blocknum = oldblok;
fp->fi_offset = oldoff;
return (cnt);
}
}
#undef SLOP
return (-1);
}