Remove struct uinode.

Remove type arg to mknod (assume T_DEV).
This commit is contained in:
rsc 2007-08-24 20:54:23 +00:00
parent fa1b34106a
commit 07090dd705
10 changed files with 129 additions and 140 deletions

18
defs.h
View file

@ -6,7 +6,6 @@ struct pipe;
struct proc; struct proc;
struct spinlock; struct spinlock;
struct stat; struct stat;
struct uinode;
// 8253pit.c // 8253pit.c
void pit8253_timerinit(void); void pit8253_timerinit(void);
@ -37,17 +36,18 @@ int filewrite(struct file*, char*, int n);
// fs.c // fs.c
int dirlink(struct inode*, char*, uint); int dirlink(struct inode*, char*, uint);
struct uinode* dirlookup(struct inode*, char*, uint*); struct inode* dirlookup(struct inode*, char*, uint*);
struct uinode* ialloc(uint, short); struct inode* ialloc(uint, short);
struct uinode* idup(struct uinode*); struct inode* idup(struct inode*);
void iinit(void); void iinit(void);
struct inode* ilock(struct uinode*); void ilock(struct inode*);
struct uinode* iunlock(struct inode*); void iput(struct inode*);
void iput(struct uinode*); void iunlock(struct inode*);
void iunlockput(struct inode*);
void iupdate(struct inode*); void iupdate(struct inode*);
int namecmp(const char*, const char*); int namecmp(const char*, const char*);
struct uinode* namei(char*); struct inode* namei(char*);
struct uinode* nameiparent(char*, char*); struct inode* nameiparent(char*, char*);
int readi(struct inode*, char*, uint, uint); int readi(struct inode*, char*, uint, uint);
void stati(struct inode*, struct stat*); void stati(struct inode*, struct stat*);
int writei(struct inode*, char*, uint, uint); int writei(struct inode*, char*, uint, uint);

10
exec.c
View file

@ -29,8 +29,9 @@ exec(char *path, char **argv)
sz = 0; sz = 0;
mem = 0; mem = 0;
if((ip = ilock(namei(path))) == 0) if((ip = namei(path)) == 0)
return -1; return -1;
ilock(ip);
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf)) if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
goto bad; goto bad;
@ -112,8 +113,7 @@ exec(char *path, char **argv)
goto bad2; goto bad2;
memset(cp->mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz); memset(cp->mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz);
} }
iunlockput(ip);
iput(iunlock(ip));
cp->tf->eip = elf.entry; cp->tf->eip = elf.entry;
cp->tf->esp = sp; cp->tf->esp = sp;
@ -124,11 +124,11 @@ exec(char *path, char **argv)
bad: bad:
if(mem) if(mem)
kfree(mem, sz); kfree(mem, sz);
iput(iunlock(ip)); iunlockput(ip);
return -1; return -1;
bad2: bad2:
iput(iunlock(ip)); iunlockput(ip);
proc_exit(); proc_exit();
return 0; return 0;
} }

22
file.c
View file

@ -56,17 +56,16 @@ int
fileread(struct file *f, char *addr, int n) fileread(struct file *f, char *addr, int n)
{ {
int r; int r;
struct inode *ip;
if(f->readable == 0) if(f->readable == 0)
return -1; return -1;
if(f->type == FD_PIPE) if(f->type == FD_PIPE)
return pipe_read(f->pipe, addr, n); return pipe_read(f->pipe, addr, n);
if(f->type == FD_INODE){ if(f->type == FD_INODE){
ip = ilock(f->ip); ilock(f->ip);
if((r = readi(ip, addr, f->off, n)) > 0) if((r = readi(f->ip, addr, f->off, n)) > 0)
f->off += r; f->off += r;
iunlock(ip); iunlock(f->ip);
return r; return r;
} }
panic("fileread"); panic("fileread");
@ -77,17 +76,16 @@ int
filewrite(struct file *f, char *addr, int n) filewrite(struct file *f, char *addr, int n)
{ {
int r; int r;
struct inode *ip;
if(f->writable == 0) if(f->writable == 0)
return -1; return -1;
if(f->type == FD_PIPE) if(f->type == FD_PIPE)
return pipe_write(f->pipe, addr, n); return pipe_write(f->pipe, addr, n);
if(f->type == FD_INODE){ if(f->type == FD_INODE){
ip = ilock(f->ip); ilock(f->ip);
if((r = writei(ip, addr, f->off, n)) > 0) if((r = writei(f->ip, addr, f->off, n)) > 0)
f->off += r; f->off += r;
iunlock(ip); iunlock(f->ip);
return r; return r;
} }
panic("filewrite"); panic("filewrite");
@ -97,12 +95,10 @@ filewrite(struct file *f, char *addr, int n)
int int
filestat(struct file *f, struct stat *st) filestat(struct file *f, struct stat *st)
{ {
struct inode *ip;
if(f->type == FD_INODE){ if(f->type == FD_INODE){
ip = ilock(f->ip); ilock(f->ip);
stati(ip, st); stati(f->ip, st);
iunlock(ip); iunlock(f->ip);
return 0; return 0;
} }
return -1; return -1;

2
file.h
View file

@ -4,6 +4,6 @@ struct file {
char readable; char readable;
char writable; char writable;
struct pipe *pipe; struct pipe *pipe;
struct uinode *ip; struct inode *ip;
uint off; uint off;
}; };

94
fs.c
View file

@ -129,7 +129,7 @@ iinit(void)
// Find the inode with number inum on device dev // Find the inode with number inum on device dev
// and return the in-memory copy. h // and return the in-memory copy. h
static struct uinode* static struct inode*
iget(uint dev, uint inum) iget(uint dev, uint inum)
{ {
struct inode *ip, *empty; struct inode *ip, *empty;
@ -142,7 +142,7 @@ iget(uint dev, uint inum)
if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){ if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){
ip->ref++; ip->ref++;
release(&icache.lock); release(&icache.lock);
return (struct uinode*)ip; return ip;
} }
if(empty == 0 && ip->ref == 0) // Remember empty slot. if(empty == 0 && ip->ref == 0) // Remember empty slot.
empty = ip; empty = ip;
@ -159,37 +159,29 @@ iget(uint dev, uint inum)
ip->flags = 0; ip->flags = 0;
release(&icache.lock); release(&icache.lock);
return (struct uinode*)ip; return ip;
} }
// Increment reference count for ip. // Increment reference count for ip.
// Returns ip to enable ip = idup(ip1) idiom. // Returns ip to enable ip = idup(ip1) idiom.
struct uinode* struct inode*
idup(struct uinode *uip) idup(struct inode *ip)
{ {
struct inode *ip;
ip = (struct inode*)uip;
acquire(&icache.lock); acquire(&icache.lock);
ip->ref++; ip->ref++;
release(&icache.lock); release(&icache.lock);
return uip; return ip;
} }
// Lock the given inode. // Lock the given inode.
struct inode* void
ilock(struct uinode *uip) ilock(struct inode *ip)
{ {
struct buf *bp; struct buf *bp;
struct dinode *dip; struct dinode *dip;
struct inode *ip;
ip = (struct inode*)uip; if(ip == 0 || ip->ref < 1)
if(ip == 0) panic("ilock");
return 0;
if(ip->ref < 1)
panic("ilock: no refs");
acquire(&icache.lock); acquire(&icache.lock);
while(ip->flags & I_BUSY) while(ip->flags & I_BUSY)
@ -211,33 +203,25 @@ ilock(struct uinode *uip)
if(ip->type == 0) if(ip->type == 0)
panic("ilock: no type"); panic("ilock: no type");
} }
return ip;
} }
// Unlock the given inode. // Unlock the given inode.
struct uinode* void
iunlock(struct inode *ip) iunlock(struct inode *ip)
{ {
if(ip == 0) if(ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1)
return 0;
if(!(ip->flags & I_BUSY) || ip->ref < 1)
panic("iunlock"); panic("iunlock");
acquire(&icache.lock); acquire(&icache.lock);
ip->flags &= ~I_BUSY; ip->flags &= ~I_BUSY;
wakeup(ip); wakeup(ip);
release(&icache.lock); release(&icache.lock);
return (struct uinode*)ip;
} }
// Caller holds reference to unlocked ip. Drop reference. // Caller holds reference to unlocked ip. Drop reference.
void void
iput(struct uinode *uip) iput(struct inode *ip)
{ {
struct inode *ip;
ip = (struct inode*)uip;
acquire(&icache.lock); acquire(&icache.lock);
if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0) { if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0) {
// inode is no longer used: truncate and free inode. // inode is no longer used: truncate and free inode.
@ -256,8 +240,15 @@ iput(struct uinode *uip)
release(&icache.lock); release(&icache.lock);
} }
void
iunlockput(struct inode *ip)
{
iunlock(ip);
iput(ip);
}
// Allocate a new inode with the given type on device dev. // Allocate a new inode with the given type on device dev.
struct uinode* struct inode*
ialloc(uint dev, short type) ialloc(uint dev, short type)
{ {
int inum, ninodes; int inum, ninodes;
@ -478,7 +469,7 @@ namecmp(const char *s, const char *t)
// Look for a directory entry in a directory. // Look for a directory entry in a directory.
// If found, set *poff to byte offset of entry. // If found, set *poff to byte offset of entry.
// Caller must have already locked dp. // Caller must have already locked dp.
struct uinode* struct inode*
dirlookup(struct inode *dp, char *name, uint *poff) dirlookup(struct inode *dp, char *name, uint *poff)
{ {
uint off, inum; uint off, inum;
@ -527,11 +518,11 @@ dirlink(struct inode *dp, char *name, uint ino)
{ {
int off; int off;
struct dirent de; struct dirent de;
struct uinode *ipu; struct inode *ip;
// Check that name is not present. // Check that name is not present.
if((ipu = dirlookup(dp, name, 0)) != 0){ if((ip = dirlookup(dp, name, 0)) != 0){
iput(ipu); iput(ip);
return -1; return -1;
} }
@ -593,53 +584,52 @@ skipelem(char *path, char *name)
// If parent is set, return the inode for the parent // If parent is set, return the inode for the parent
// and write the final path element to name, which // and write the final path element to name, which
// should have room for DIRSIZ bytes. // should have room for DIRSIZ bytes.
static struct uinode* static struct inode*
_namei(char *path, int parent, char *name) _namei(char *path, int parent, char *name)
{ {
struct uinode *dpu, *ipu; struct inode *ip, *next;
struct inode *dp;
uint off; uint off;
if(*path == '/') if(*path == '/')
dpu = iget(ROOTDEV, 1); ip = iget(ROOTDEV, 1);
else else
dpu = idup(cp->cwd); ip = idup(cp->cwd);
while((path = skipelem(path, name)) != 0){ while((path = skipelem(path, name)) != 0){
dp = ilock(dpu); ilock(ip);
if(dp->type != T_DIR){ if(ip->type != T_DIR){
iput(iunlock(dp)); iunlockput(ip);
return 0; return 0;
} }
if(parent && *path == '\0'){ if(parent && *path == '\0'){
// Stop one level early. // Stop one level early.
iunlock(dp); iunlock(ip);
return dpu; return ip;
} }
if((ipu = dirlookup(dp, name, &off)) == 0){ if((next = dirlookup(ip, name, &off)) == 0){
iput(iunlock(dp)); iunlockput(ip);
return 0; return 0;
} }
iput(iunlock(dp)); iunlockput(ip);
dpu = ipu; ip = next;
} }
if(parent){ if(parent){
iput(dpu); iput(ip);
return 0; return 0;
} }
return dpu; return ip;
} }
struct uinode* struct inode*
namei(char *path) namei(char *path)
{ {
char name[DIRSIZ]; char name[DIRSIZ];
return _namei(path, 0, name); return _namei(path, 0, name);
} }
struct uinode* struct inode*
nameiparent(char *path, char *name) nameiparent(char *path, char *name)
{ {
return _namei(path, 1, name); return _namei(path, 1, name);

View file

@ -14,11 +14,5 @@ struct inode {
uint addrs[NADDRS]; uint addrs[NADDRS];
}; };
// unlocked inode - only dev and inum are available
struct uinode {
uint dev;
uint inum;
};
#define I_BUSY 0x1 #define I_BUSY 0x1
#define I_VALID 0x2 #define I_VALID 0x2

2
init.c
View file

@ -14,7 +14,7 @@ main(void)
int pid, wpid; int pid, wpid;
if(open("console", O_RDWR) < 0){ if(open("console", O_RDWR) < 0){
mknod("console", T_DEV, 1, 1); mknod("console", 1, 1);
open("console", O_RDWR); open("console", O_RDWR);
} }
dup(0); // stdout dup(0); // stdout

2
proc.h
View file

@ -37,7 +37,7 @@ struct proc {
void *chan; // If non-zero, sleeping on chan void *chan; // If non-zero, sleeping on chan
int killed; // If non-zero, have been killed int killed; // If non-zero, have been killed
struct file *ofile[NOFILE]; // Open files struct file *ofile[NOFILE]; // Open files
struct uinode *cwd; // Current directory struct inode *cwd; // Current directory
struct jmpbuf jmpbuf; // Jump here to run process struct jmpbuf jmpbuf; // Jump here to run process
struct trapframe *tf; // Trap frame for current interrupt struct trapframe *tf; // Trap frame for current interrupt
char name[16]; // Process name (debugging) char name[16]; // Process name (debugging)

109
sysfile.c
View file

@ -103,33 +103,37 @@ sys_link(void)
{ {
char name[DIRSIZ], *new, *old; char name[DIRSIZ], *new, *old;
struct inode *dp, *ip; struct inode *dp, *ip;
struct uinode *ipu;
if(argstr(0, &old) < 0 || argstr(1, &new) < 0) if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
return -1; return -1;
if((ip = ilock(namei(old))) == 0) if((ip = namei(old)) == 0)
return -1; return -1;
ilock(ip);
if(ip->type == T_DIR){ if(ip->type == T_DIR){
iput(iunlock(ip)); iunlockput(ip);
return -1; return -1;
} }
ip->nlink++; ip->nlink++;
iupdate(ip); iupdate(ip);
ipu = iunlock(ip); ip = 0; iunlock(ip);
if((dp = ilock(nameiparent(new, name))) == 0 || if((dp = nameiparent(new, name)) == 0)
dp->dev != ipu->dev || dirlink(dp, name, ipu->inum) < 0){ goto bad;
if(dp) ilock(dp);
iput(iunlock(dp)); if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0)
ip = ilock(ipu); goto bad;
ip->nlink--; iunlockput(dp);
iupdate(ip); iput(ip);
iput(iunlock(ip));
return -1;
}
iput(iunlock(dp));
iput(ipu);
return 0; return 0;
bad:
if(dp)
iunlockput(dp);
ilock(ip);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
return -1;
} }
// Is the directory dp empty except for "." and ".." ? // Is the directory dp empty except for "." and ".." ?
@ -158,65 +162,67 @@ sys_unlink(void)
if(argstr(0, &path) < 0) if(argstr(0, &path) < 0)
return -1; return -1;
if((dp = ilock(nameiparent(path, name))) == 0) if((dp = nameiparent(path, name)) == 0)
return -1; return -1;
ilock(dp);
// Cannot unlink "." or "..". // Cannot unlink "." or "..".
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0){ if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0){
iput(iunlock(dp)); iunlockput(dp);
return -1; return -1;
} }
if((ip = ilock(dirlookup(dp, name, &off))) == 0){ if((ip = dirlookup(dp, name, &off)) == 0){
iput(iunlock(dp)); iunlockput(dp);
return -1; return -1;
} }
ilock(ip);
if(ip->nlink < 1) if(ip->nlink < 1)
panic("unlink: nlink < 1"); panic("unlink: nlink < 1");
if(ip->type == T_DIR && !isdirempty(ip)){ if(ip->type == T_DIR && !isdirempty(ip)){
iput(iunlock(ip)); iunlockput(ip);
iput(iunlock(dp)); iunlockput(dp);
return -1; return -1;
} }
memset(&de, 0, sizeof(de)); memset(&de, 0, sizeof(de));
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("unlink: writei"); panic("unlink: writei");
iput(iunlock(dp)); iunlockput(dp);
ip->nlink--; ip->nlink--;
iupdate(ip); iupdate(ip);
iput(iunlock(ip)); iunlockput(ip);
return 0; return 0;
} }
// Create the path and return its unlocked inode structure.
static struct inode* static struct inode*
mkpath(char *path, int canexist, short type, short major, short minor) mkpath(char *path, int canexist, short type, short major, short minor)
{ {
uint off; uint off;
struct inode *ip, *dp; struct inode *ip, *dp;
struct uinode *ipu;
char name[DIRSIZ]; char name[DIRSIZ];
if((dp = ilock(nameiparent(path, name))) == 0) if((dp = nameiparent(path, name)) == 0)
return 0; return 0;
ilock(dp);
if(canexist && (ipu = dirlookup(dp, name, &off)) != 0){ if(canexist && (ip = dirlookup(dp, name, &off)) != 0){
iput(iunlock(dp)); iunlockput(dp);
ip = ilock(ipu); ilock(ip);
if(ip->type != type || ip->major != major || ip->minor != minor){ if(ip->type != type || ip->major != major || ip->minor != minor){
iput(iunlock(ip)); iunlockput(ip);
return 0; return 0;
} }
return ip; return ip;
} }
if((ip = ilock(ialloc(dp->dev, type))) == 0){ if((ip = ialloc(dp->dev, type)) == 0){
iput(iunlock(dp)); iunlockput(dp);
return 0; return 0;
} }
ilock(ip);
ip->major = major; ip->major = major;
ip->minor = minor; ip->minor = minor;
ip->size = 0; ip->size = 0;
@ -225,8 +231,8 @@ mkpath(char *path, int canexist, short type, short major, short minor)
if(dirlink(dp, name, ip->inum) < 0){ if(dirlink(dp, name, ip->inum) < 0){
ip->nlink = 0; ip->nlink = 0;
iput(iunlock(ip)); iunlockput(ip);
iput(iunlock(dp)); iunlockput(dp);
return 0; return 0;
} }
@ -237,7 +243,7 @@ mkpath(char *path, int canexist, short type, short major, short minor)
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0) if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
panic("mkpath dots"); panic("mkpath dots");
} }
iput(iunlock(dp)); iunlockput(dp);
return ip; return ip;
} }
@ -256,10 +262,11 @@ sys_open(void)
if((ip = mkpath(path, 1, T_FILE, 0, 0)) == 0) if((ip = mkpath(path, 1, T_FILE, 0, 0)) == 0)
return -1; return -1;
}else{ }else{
if((ip = ilock(namei(path))) == 0) if((ip = namei(path)) == 0)
return -1; return -1;
ilock(ip);
if(ip->type == T_DIR && (omode & (O_RDWR|O_WRONLY))){ if(ip->type == T_DIR && (omode & (O_RDWR|O_WRONLY))){
iput(iunlock(ip)); iunlockput(ip);
return -1; return -1;
} }
} }
@ -267,12 +274,13 @@ sys_open(void)
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){ if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f) if(f)
fileclose(f); fileclose(f);
iput(iunlock(ip)); iunlockput(ip);
return -1; return -1;
} }
iunlock(ip);
f->type = FD_INODE; f->type = FD_INODE;
f->ip = iunlock(ip); f->ip = ip;
f->off = 0; f->off = 0;
if(omode & O_RDWR) { if(omode & O_RDWR) {
f->readable = 1; f->readable = 1;
@ -296,13 +304,12 @@ sys_mknod(void)
int len; int len;
int type, major, minor; int type, major, minor;
if((len=argstr(0, &path)) < 0 || argint(1, &type) < 0 || if((len=argstr(0, &path)) < 0 ||
argint(2, &major) < 0 || argint(3, &minor) < 0) argint(1, &major) < 0 ||
argint(2, &minor) < 0 ||
(ip = mkpath(path, 0, T_DEV, major, minor)) == 0)
return -1; return -1;
// XXX check that type == T_DEV or eliminate type arg? iunlockput(ip);
if((ip = mkpath(path, 0, type, major, minor)) == 0)
return -1;
iput(iunlock(ip));
return 0; return 0;
} }
@ -314,7 +321,7 @@ sys_mkdir(void)
if(argstr(0, &path) < 0 || (ip = mkpath(path, 0, T_DIR, 0, 0)) == 0) if(argstr(0, &path) < 0 || (ip = mkpath(path, 0, T_DIR, 0, 0)) == 0)
return -1; return -1;
iput(iunlock(ip)); iunlockput(ip);
return 0; return 0;
} }
@ -324,14 +331,16 @@ sys_chdir(void)
char *path; char *path;
struct inode *ip; struct inode *ip;
if(argstr(0, &path) < 0 || (ip = ilock(namei(path))) == 0) if(argstr(0, &path) < 0 || (ip = namei(path)) == 0)
return -1; return -1;
ilock(ip);
if(ip->type != T_DIR) { if(ip->type != T_DIR) {
iput(iunlock(ip)); iunlockput(ip);
return -1; return -1;
} }
iunlock(ip);
iput(cp->cwd); iput(cp->cwd);
cp->cwd = iunlock(ip); cp->cwd = ip;
return 0; return 0;
} }

2
user.h
View file

@ -9,7 +9,7 @@ int close(int);
int kill(int); int kill(int);
int exec(char*, char**); int exec(char*, char**);
int open(char*, int); int open(char*, int);
int mknod(char*, short, short, short); int mknod(char*, short, short);
int unlink(char*); int unlink(char*);
int fstat(int fd, struct stat*); int fstat(int fd, struct stat*);
int link(char*, char*); int link(char*, char*);