Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Various changes made while offline.

 + bwrite sector argument is redundant; use b->sector.
 + reformatting of files for nicer PDF page breaks
 + distinguish between locked, unlocked inodes in type signatures
 + change FD_FILE to FD_INODE
 + move userinit (nee proc0init) to proc.c
 + move ROOTDEV to param.h
 + always parenthesize sizeof argument
  • Loading branch information
rsc committed Aug 22, 2007
1 parent 3dcf889 commit eaea18c
Show file tree
Hide file tree
Showing 25 changed files with 633 additions and 665 deletions.
46 changes: 5 additions & 41 deletions BUGS
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,11 @@ proc.c:
and be able to break out with an error return.
it is better if you check *before* sleep.

can swap procdump up after proc_kill
and then have proc_exit and proc_wait on same sheet

sched -> switch2scheduler? or just switch?

factor out switching and scheduling code from process code

shuffle for formatting

syscall.c:
cannot convince runoff1 to split the extern lists to fill previous page completely.

fs.c: split all name operations off in name.c? (starting with namei but
wdir keep in fs.c)
locking?
shuffle for formatting

pipe.c:
more comments?
comment how functions get called?

sysfile.c:
is the sys_exec picture upside down?
can sys_open and sys_exec be simplified any?

general:
sizeof parens?

bio.c:
decide odd or even
bwrite doesn't need a second argument

file.c:
move fileincref onto page 1?

L=$HOME/mit/l
(for i in *.c; do xoc -x xgnu -x ./nodecleq.zeta --typesonly $i; done) 2>&1 | grep warning

saw random sharedfd failure.

why does fdalloc consume reference?
cannot convince runoff1 to split the extern lists
to fill previous page completely.

why mkdir and create?
formatting:
file.c filewrite leaks onto next page
need to fix PAGEBREAK mechanism

5 changes: 2 additions & 3 deletions bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,11 @@ bread(uint dev, uint sector)
// Write buf's contents to disk.
// Must be locked.
void
bwrite(struct buf *b, uint sector)
bwrite(struct buf *b)
{
if((b->flags & B_BUSY) == 0)
panic("bwrite");

ide_rw(b->dev & 0xff, sector, b->data, 1, 0);
ide_rw(b->dev & 0xff, b->sector, b->data, 1, 0);
b->flags |= B_VALID;
}

Expand Down
55 changes: 28 additions & 27 deletions bootmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
// * cmain() in this file takes over,
// reads in the kernel and jumps to it.

//PAGEBREAK!
#include "types.h"
#include "elf.h"
#include "x86.h"

#define SECTSIZE 512
#define ELFHDR ((struct elfhdr*) 0x10000) // scratch space

void readsect(void*, uint);
void readseg(uint, uint, uint);

void
Expand Down Expand Up @@ -64,32 +64,6 @@ cmain(void)
;
}

// Read 'count' bytes at 'offset' from kernel into virtual address 'va'.
// Might copy more than asked
void
readseg(uint va, uint count, uint offset)
{
uint end_va;

va &= 0xFFFFFF;
end_va = va + count;

// round down to sector boundary
va &= ~(SECTSIZE - 1);

// translate from bytes to sectors, and kernel starts at sector 1
offset = (offset / SECTSIZE) + 1;

// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
while(va < end_va) {
readsect((uchar*) va, offset);
va += SECTSIZE;
offset++;
}
}

void
waitdisk(void)
{
Expand All @@ -98,6 +72,7 @@ waitdisk(void)
;
}

// Read a single sector at offset into dst.
void
readsect(void *dst, uint offset)
{
Expand All @@ -118,3 +93,29 @@ readsect(void *dst, uint offset)
insl(0x1F0, dst, SECTSIZE/4);
}

// Read 'count' bytes at 'offset' from kernel into virtual address 'va'.
// Might copy more than asked.
void
readseg(uint va, uint count, uint offset)
{
uint end_va;

va &= 0xFFFFFF;
end_va = va + count;

// round down to sector boundary
va &= ~(SECTSIZE - 1);

// translate from bytes to sectors, and kernel starts at sector 1
offset = (offset / SECTSIZE) + 1;

// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
while(va < end_va) {
readsect((uchar*) va, offset);
va += SECTSIZE;
offset++;
}
}

28 changes: 15 additions & 13 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ int proc_kill(int);
int proc_wait(void);
void yield(void);
void procdump(void);
void userinit(void);

// setjmp.S
struct jmpbuf;
Expand Down Expand Up @@ -117,30 +118,31 @@ void ide_rw(int, uint, void*, uint, int);
void binit(void);
struct buf;
struct buf* bread(uint, uint);
void bwrite(struct buf*, uint);
void bwrite(struct buf*);
void brelse(struct buf*);

// fs.c
struct inode;
struct uinode;
void iinit(void);
void ilock(struct inode*);
void iunlock(struct inode*);
void idecref(struct inode*);
struct inode* iincref(struct inode*);
void iput(struct inode*);
struct inode* namei(char*);
struct inode* ilock(struct uinode*);
struct uinode* iunlock(struct inode*);
void iput(struct uinode*);
struct uinode* idup(struct uinode*);
struct uinode* namei(char*);
void stati(struct inode*, struct stat*);
int readi(struct inode*, char*, uint, uint);
int writei(struct inode*, char*, uint, uint);
struct inode* mknod(char*, short, short, short);
int unlink(char*);
int link(char*, char*);
struct inode* igetroot(void);
int mkdir(char *path);
struct inode* create(char *path);
int dirlink(struct inode *dp, char *name, uint ino);
struct uinode* dirlookup(struct inode *dp, char *name, uint *poff);
void iupdate(struct inode *ip);
int namecmp(const char *s, const char *t);
struct uinode* ialloc(uint, short);
struct uinode* nameiparent(char *path, char *name);

// exec.c
int exec(char*, char**);

// number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))

22 changes: 10 additions & 12 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int
exec(char *path, char **argv)
{
uint sz, sp, p1, p2;
int i, nargs, argbytes, len;
int i, nargs, argbytes, len, off;
struct inode *ip;
struct elfhdr elf;
struct proghdr ph;
Expand All @@ -29,7 +29,7 @@ exec(char *path, char **argv)
sz = 0;
mem = 0;

if((ip = namei(path)) == 0)
if((ip = ilock(namei(path))) == 0)
return -1;

if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
Expand All @@ -38,9 +38,8 @@ exec(char *path, char **argv)
if(elf.magic != ELF_MAGIC)
goto bad;

for(i = 0; i < elf.phnum; i++){
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
sizeof(ph)) != sizeof(ph))
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
goto bad;
if(ph.type != ELF_PROG_LOAD)
continue;
Expand Down Expand Up @@ -94,17 +93,16 @@ exec(char *path, char **argv)
for(last=s=path; *s; s++)
if(*s == '/')
last = s+1;
safestrcpy(cp->name, last, sizeof cp->name);
safestrcpy(cp->name, last, sizeof(cp->name));

// commit to the new image.
kfree(cp->mem, cp->sz);
cp->sz = sz;
cp->mem = mem;
mem = 0;

for(i = 0; i < elf.phnum; i++){
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
sizeof(ph)) != sizeof(ph))
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
goto bad2;
if(ph.type != ELF_PROG_LOAD)
continue;
Expand All @@ -115,7 +113,7 @@ exec(char *path, char **argv)
memset(cp->mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz);
}

iput(ip);
iput(iunlock(ip));

cp->tf->eip = elf.entry;
cp->tf->esp = sp;
Expand All @@ -126,11 +124,11 @@ exec(char *path, char **argv)
bad:
if(mem)
kfree(mem, sz);
iput(ip);
iput(iunlock(ip));
return -1;

bad2:
iput(ip);
iput(iunlock(ip));
proc_exit();
return 0;
}
38 changes: 21 additions & 17 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#include "fs.h"
#include "fsvar.h"

struct spinlock file_table_lock;
struct devsw devsw[NDEV];

struct spinlock file_table_lock;
struct file file[NFILE];

void
Expand All @@ -22,7 +21,7 @@ fileinit(void)
initlock(&file_table_lock, "file_table");
}

// Allocate a file structure
// Allocate a file structure.
struct file*
filealloc(void)
{
Expand Down Expand Up @@ -57,16 +56,17 @@ int
fileread(struct file *f, char *addr, int n)
{
int r;
struct inode *ip;

if(f->readable == 0)
return -1;
if(f->type == FD_PIPE)
return pipe_read(f->pipe, addr, n);
if(f->type == FD_FILE){
ilock(f->ip);
if((r = readi(f->ip, addr, f->off, n)) > 0)
if(f->type == FD_INODE){
ip = ilock(f->ip);
if((r = readi(ip, addr, f->off, n)) > 0)
f->off += r;
iunlock(f->ip);
iunlock(ip);
return r;
}
panic("fileread");
Expand All @@ -77,16 +77,17 @@ int
filewrite(struct file *f, char *addr, int n)
{
int r;
struct inode *ip;

if(f->writable == 0)
return -1;
if(f->type == FD_PIPE)
return pipe_write(f->pipe, addr, n);
if(f->type == FD_FILE){
ilock(f->ip);
if((r = writei(f->ip, addr, f->off, n)) > 0)
if(f->type == FD_INODE){
ip = ilock(f->ip);
if((r = writei(ip, addr, f->off, n)) > 0)
f->off += r;
iunlock(f->ip);
iunlock(ip);
return r;
}
panic("filewrite");
Expand All @@ -96,10 +97,12 @@ filewrite(struct file *f, char *addr, int n)
int
filestat(struct file *f, struct stat *st)
{
if(f->type == FD_FILE){
ilock(f->ip);
stati(f->ip, st);
iunlock(f->ip);
struct inode *ip;

if(f->type == FD_INODE){
ip = ilock(f->ip);
stati(ip, st);
iunlock(ip);
return 0;
}
return -1;
Expand All @@ -110,6 +113,7 @@ void
fileclose(struct file *f)
{
struct file ff;

acquire(&file_table_lock);

if(f->ref < 1 || f->type == FD_CLOSED)
Expand All @@ -127,8 +131,8 @@ fileclose(struct file *f)

if(ff.type == FD_PIPE)
pipe_close(ff.pipe, ff.writable);
else if(ff.type == FD_FILE)
idecref(ff.ip);
else if(ff.type == FD_INODE)
iput(ff.ip);
else
panic("fileclose");
}
Expand Down
4 changes: 2 additions & 2 deletions file.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
struct file {
enum { FD_CLOSED, FD_NONE, FD_PIPE, FD_FILE } type;
enum { FD_CLOSED, FD_NONE, FD_PIPE, FD_INODE } type;
int ref; // reference count
char readable;
char writable;
struct pipe *pipe;
struct inode *ip;
struct uinode *ip;
uint off;
};
Loading

0 comments on commit eaea18c

Please sign in to comment.