提交 c3e73232 创建 作者: Silas Boyd-Wickizer's avatar Silas Boyd-Wickizer

64-bitify file.c and friends -- make block units u32.

上级 4ddb84ac
...@@ -9,11 +9,6 @@ ...@@ -9,11 +9,6 @@
struct devsw __attribute__ ((aligned (CACHELINE))) devsw[NDEV]; struct devsw __attribute__ ((aligned (CACHELINE))) devsw[NDEV];
void
fileinit(void)
{
}
// Allocate a file structure. // Allocate a file structure.
struct file* struct file*
filealloc(void) filealloc(void)
......
...@@ -5,16 +5,16 @@ struct file { ...@@ -5,16 +5,16 @@ struct file {
char writable; char writable;
struct pipe *pipe; struct pipe *pipe;
struct inode *ip; struct inode *ip;
uint off; u32 off;
}; };
// in-core file system types // in-core file system types
struct inode { struct inode {
uint dev; // Device number u32 dev; // Device number
uint inum; // Inode number u32 inum; // Inode number
uint gen; // Generation number u32 gen; // Generation number
int ref; // Reference count int ref; // Reference count
int flags; // I_BUSY, I_VALID int flags; // I_BUSY, I_VALID
int readbusy; int readbusy;
...@@ -27,8 +27,8 @@ struct inode { ...@@ -27,8 +27,8 @@ struct inode {
short major; short major;
short minor; short minor;
short nlink; short nlink;
uint size; u32 size;
uint addrs[NDIRECT+1]; u32 addrs[NDIRECT+1];
}; };
#define I_BUSYR 0x1 #define I_BUSYR 0x1
......
#include "types.h"
#include "kernel.h"
#include "fs.h"
int
namecmp(const char *s, const char *t)
{
return strncmp(s, t, DIRSIZ);
}
struct inode*
namei(char *path)
{
panic("namei");
return NULL;
}
void
iput(struct inode *ip)
{
panic("iput");
}
#if 0
// File system implementation. Four layers: // File system implementation. Four layers:
// + Blocks: allocator for raw disk blocks. // + Blocks: allocator for raw disk blocks.
// + Files: inode allocator, reading, writing, metadata. // + Files: inode allocator, reading, writing, metadata.
...@@ -35,10 +11,10 @@ iput(struct inode *ip) ...@@ -35,10 +11,10 @@ iput(struct inode *ip)
// are in sysfile.c. // are in sysfile.c.
#include "types.h" #include "types.h"
#include "defs.h"
#include "param.h" #include "param.h"
#include "stat.h" #include "stat.h"
#include "mmu.h" #include "mmu.h"
#include "kernel.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h" #include "queue.h"
...@@ -46,6 +22,7 @@ iput(struct inode *ip) ...@@ -46,6 +22,7 @@ iput(struct inode *ip)
#include "buf.h" #include "buf.h"
#include "fs.h" #include "fs.h"
#include "file.h" #include "file.h"
#include "cpu.h"
#define min(a, b) ((a) < (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b))
static void itrunc(struct inode*); static void itrunc(struct inode*);
...@@ -76,8 +53,8 @@ bzero(int dev, int bno) ...@@ -76,8 +53,8 @@ bzero(int dev, int bno)
// Blocks. // Blocks.
// Allocate a disk block. // Allocate a disk block.
static uint static u32
balloc(uint dev) balloc(u32 dev)
{ {
int b, bi, m; int b, bi, m;
struct buf *bp; struct buf *bp;
...@@ -103,11 +80,12 @@ balloc(uint dev) ...@@ -103,11 +80,12 @@ balloc(uint dev)
// Free a disk block. // Free a disk block.
static void static void
bfree(int dev, uint b) bfree(int dev, u64 x)
{ {
struct buf *bp; struct buf *bp;
struct superblock sb; struct superblock sb;
int bi, m; int bi, m;
u32 b = x;
bzero(dev, b); bzero(dev, b);
...@@ -157,10 +135,10 @@ bfree(int dev, uint b) ...@@ -157,10 +135,10 @@ bfree(int dev, uint b)
static struct ns *ins; static struct ns *ins;
static struct { uint x __attribute__((aligned (CACHELINE))); } icache_free[NCPU]; static struct { u32 x __mpalign__; } icache_free[NCPU];
void void
iinit(void) initinode(void)
{ {
ins = nsalloc(0); ins = nsalloc(0);
for (int i = 0; i < NCPU; i++) for (int i = 0; i < NCPU; i++)
...@@ -171,7 +149,7 @@ iinit(void) ...@@ -171,7 +149,7 @@ iinit(void)
// Allocate a new inode with the given type on device dev. // Allocate a new inode with the given type on device dev.
// Returns a locked inode. // Returns a locked inode.
struct inode* struct inode*
ialloc(uint dev, short type) ialloc(u32 dev, short type)
{ {
int inum; int inum;
struct buf *bp; struct buf *bp;
...@@ -261,7 +239,7 @@ ifree(void *arg) ...@@ -261,7 +239,7 @@ ifree(void *arg)
} }
struct inode* struct inode*
iget(uint dev, uint inum) iget(u32 dev, u32 inum)
{ {
struct inode *ip; struct inode *ip;
...@@ -291,7 +269,7 @@ iget(uint dev, uint inum) ...@@ -291,7 +269,7 @@ iget(uint dev, uint inum)
// Allocate fresh inode cache slot. // Allocate fresh inode cache slot.
retry_evict: retry_evict:
(void) 0; (void) 0;
uint cur_free = icache_free[cpu->id].x; u32 cur_free = icache_free[mycpu()->id].x;
if (cur_free == 0) { if (cur_free == 0) {
struct inode *victim = ns_enumerate(ins, evict, 0); struct inode *victim = ns_enumerate(ins, evict, 0);
if (!victim) if (!victim)
...@@ -307,7 +285,7 @@ iget(uint dev, uint inum) ...@@ -307,7 +285,7 @@ iget(uint dev, uint inum)
ns_remove(ins, KII(victim->dev, victim->inum), victim); ns_remove(ins, KII(victim->dev, victim->inum), victim);
rcu_delayed(victim, ifree); rcu_delayed(victim, ifree);
} else { } else {
if (!__sync_bool_compare_and_swap(&icache_free[cpu->id].x, cur_free, cur_free-1)) if (!__sync_bool_compare_and_swap(&icache_free[mycpu()->id].x, cur_free, cur_free-1))
goto retry_evict; goto retry_evict;
} }
...@@ -429,7 +407,7 @@ iput(struct inode *ip) ...@@ -429,7 +407,7 @@ iput(struct inode *ip)
ns_remove(ins, KII(ip->dev, ip->inum), ip); ns_remove(ins, KII(ip->dev, ip->inum), ip);
rcu_delayed(ip, ifree); rcu_delayed(ip, ifree);
__sync_fetch_and_add(&icache_free[cpu->id].x, 1); __sync_fetch_and_add(&icache_free[mycpu()->id].x, 1);
return; return;
} }
release(&ip->lock); release(&ip->lock);
...@@ -454,10 +432,10 @@ iunlockput(struct inode *ip) ...@@ -454,10 +432,10 @@ iunlockput(struct inode *ip)
// Return the disk block address of the nth block in inode ip. // Return the disk block address of the nth block in inode ip.
// If there is no such block, bmap allocates one. // If there is no such block, bmap allocates one.
static uint static u32
bmap(struct inode *ip, uint bn) bmap(struct inode *ip, u32 bn)
{ {
uint addr, *a; u32 addr, *a;
struct buf *bp; struct buf *bp;
if(bn < NDIRECT){ if(bn < NDIRECT){
...@@ -472,7 +450,7 @@ bmap(struct inode *ip, uint bn) ...@@ -472,7 +450,7 @@ bmap(struct inode *ip, uint bn)
if((addr = ip->addrs[NDIRECT]) == 0) if((addr = ip->addrs[NDIRECT]) == 0)
ip->addrs[NDIRECT] = addr = balloc(ip->dev); ip->addrs[NDIRECT] = addr = balloc(ip->dev);
bp = bread(ip->dev, addr, 1); bp = bread(ip->dev, addr, 1);
a = (uint*)bp->data; a = (u32*)bp->data;
if((addr = a[bn]) == 0){ if((addr = a[bn]) == 0){
a[bn] = addr = balloc(ip->dev); a[bn] = addr = balloc(ip->dev);
bwrite(bp); bwrite(bp);
...@@ -492,7 +470,7 @@ itrunc(struct inode *ip) ...@@ -492,7 +470,7 @@ itrunc(struct inode *ip)
{ {
int i, j; int i, j;
struct buf *bp; struct buf *bp;
uint *a; u32 *a;
for(i = 0; i < NDIRECT; i++){ for(i = 0; i < NDIRECT; i++){
if(ip->addrs[i]){ if(ip->addrs[i]){
...@@ -503,7 +481,7 @@ itrunc(struct inode *ip) ...@@ -503,7 +481,7 @@ itrunc(struct inode *ip)
if(ip->addrs[NDIRECT]){ if(ip->addrs[NDIRECT]){
bp = bread(ip->dev, ip->addrs[NDIRECT], 0); bp = bread(ip->dev, ip->addrs[NDIRECT], 0);
a = (uint*)bp->data; a = (u32*)bp->data;
for(j = 0; j < NINDIRECT; j++){ for(j = 0; j < NINDIRECT; j++){
if(a[j]) if(a[j])
rcu_delayed2(ip->dev, a[j], bfree); rcu_delayed2(ip->dev, a[j], bfree);
...@@ -531,15 +509,19 @@ stati(struct inode *ip, struct stat *st) ...@@ -531,15 +509,19 @@ stati(struct inode *ip, struct stat *st)
//PAGEBREAK! //PAGEBREAK!
// Read data from inode. // Read data from inode.
int int
readi(struct inode *ip, char *dst, uint off, uint n) readi(struct inode *ip, char *dst, u32 off, u32 n)
{ {
uint tot, m; u32 tot, m;
struct buf *bp; struct buf *bp;
if(ip->type == T_DEV){ if(ip->type == T_DEV){
// XXX(sbw)
panic("readi T_DEV");
#if 0
if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read) if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].read)
return -1; return -1;
return devsw[ip->major].read(ip, dst, n); return devsw[ip->major].read(ip, dst, n);
#endif
} }
if(off > ip->size || off + n < off) if(off > ip->size || off + n < off)
...@@ -559,15 +541,19 @@ readi(struct inode *ip, char *dst, uint off, uint n) ...@@ -559,15 +541,19 @@ readi(struct inode *ip, char *dst, uint off, uint n)
// PAGEBREAK! // PAGEBREAK!
// Write data to inode. // Write data to inode.
int int
writei(struct inode *ip, char *src, uint off, uint n) writei(struct inode *ip, char *src, u32 off, u32 n)
{ {
uint tot, m; u32 tot, m;
struct buf *bp; struct buf *bp;
if(ip->type == T_DEV){ if(ip->type == T_DEV){
// XXX(sbw)
panic("writei T_DEV");
#if 0
if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write) if(ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].write)
return -1; return -1;
return devsw[ip->major].write(ip, src, n); return devsw[ip->major].write(ip, src, n);
#endif
} }
if(off > ip->size || off + n < off) if(off > ip->size || off + n < off)
...@@ -608,7 +594,7 @@ dir_init(struct inode *dp) ...@@ -608,7 +594,7 @@ dir_init(struct inode *dp)
panic("dir_init not DIR"); panic("dir_init not DIR");
struct ns *dir = nsalloc(0); struct ns *dir = nsalloc(0);
for (uint off = 0; off < dp->size; off += BSIZE) { for (u32 off = 0; off < dp->size; off += BSIZE) {
struct buf *bp = bread(dp->dev, bmap(dp, off / BSIZE), 0); struct buf *bp = bread(dp->dev, bmap(dp, off / BSIZE), 0);
for (struct dirent *de = (struct dirent *) bp->data; for (struct dirent *de = (struct dirent *) bp->data;
de < (struct dirent *) (bp->data + BSIZE); de < (struct dirent *) (bp->data + BSIZE);
...@@ -616,7 +602,7 @@ dir_init(struct inode *dp) ...@@ -616,7 +602,7 @@ dir_init(struct inode *dp)
if (de->inum == 0) if (de->inum == 0)
continue; continue;
ns_insert(dir, KD(de->name), (void*) (uint) de->inum); ns_insert(dir, KD(de->name), (void*) (u64) de->inum);
} }
brelse(bp, 0); brelse(bp, 0);
} }
...@@ -628,7 +614,7 @@ dir_init(struct inode *dp) ...@@ -628,7 +614,7 @@ dir_init(struct inode *dp)
struct flush_state { struct flush_state {
struct inode *dp; struct inode *dp;
uint off; u32 off;
}; };
static void * static void *
...@@ -636,7 +622,7 @@ dir_flush_cb(void *key, void *val, void *arg) ...@@ -636,7 +622,7 @@ dir_flush_cb(void *key, void *val, void *arg)
{ {
struct flush_state *fs = arg; struct flush_state *fs = arg;
char *name = key; char *name = key;
uint inum = (uint) val; u32 inum = (u64) val;
struct dirent de; struct dirent de;
strncpy(de.name, name, DIRSIZ); strncpy(de.name, name, DIRSIZ);
...@@ -668,7 +654,7 @@ dirlookup(struct inode *dp, char *name) ...@@ -668,7 +654,7 @@ dirlookup(struct inode *dp, char *name)
dir_init(dp); dir_init(dp);
void *vinum = ns_lookup(dp->dir, KD(name)); void *vinum = ns_lookup(dp->dir, KD(name));
uint inum = (uint) vinum; u32 inum = (u64) vinum;
//cprintf("dirlookup: %x (%d): %s -> %d\n", dp, dp->inum, name, inum); //cprintf("dirlookup: %x (%d): %s -> %d\n", dp, dp->inum, name, inum);
if (inum == 0) if (inum == 0)
...@@ -679,15 +665,14 @@ dirlookup(struct inode *dp, char *name) ...@@ -679,15 +665,14 @@ dirlookup(struct inode *dp, char *name)
// Write a new directory entry (name, inum) into the directory dp. // Write a new directory entry (name, inum) into the directory dp.
int int
dirlink(struct inode *dp, char *name, uint inum) dirlink(struct inode *dp, char *name, u32 inum)
{ {
dir_init(dp); dir_init(dp);
//cprintf("dirlink: %x (%d): %s -> %d\n", dp, dp->inum, name, inum); //cprintf("dirlink: %x (%d): %s -> %d\n", dp, dp->inum, name, inum);
return ns_insert(dp->dir, KD(name), (void*)inum); return ns_insert(dp->dir, KD(name), (void*)(u64)inum);
} }
//PAGEBREAK!
// Paths // Paths
// Copy the next path element from path into name. // Copy the next path element from path into name.
...@@ -739,7 +724,7 @@ namex(char *path, int nameiparent, char *name) ...@@ -739,7 +724,7 @@ namex(char *path, int nameiparent, char *name)
if(*path == '/') if(*path == '/')
ip = iget(ROOTDEV, ROOTINO); ip = iget(ROOTDEV, ROOTINO);
else else
ip = idup(proc->cwd); ip = idup(myproc()->cwd);
while((path = skipelem(path, name)) != 0){ while((path = skipelem(path, name)) != 0){
next = 0; next = 0;
...@@ -788,4 +773,4 @@ nameiparent(char *path, char *name) ...@@ -788,4 +773,4 @@ nameiparent(char *path, char *name)
{ {
return namex(path, 1, name); return namex(path, 1, name);
} }
#endif
// Directory is a file containing a sequence of dirent structures.
#define DIRSIZ 14
#if 0
// On-disk file system format. // On-disk file system format.
// Both the kernel and user programs use this header file. // Both the kernel and user programs use this header file.
...@@ -14,13 +10,13 @@ ...@@ -14,13 +10,13 @@
// File system super block // File system super block
struct superblock { struct superblock {
uint size; // Size of file system image (blocks) u32 size; // Size of file system image (blocks)
uint nblocks; // Number of data blocks u32 nblocks; // Number of data blocks
uint ninodes; // Number of inodes. u32 ninodes; // Number of inodes.
}; };
#define NDIRECT 11 #define NDIRECT 11
#define NINDIRECT (BSIZE / sizeof(uint)) #define NINDIRECT (BSIZE / sizeof(u32))
#define MAXFILE (NDIRECT + NINDIRECT) #define MAXFILE (NDIRECT + NINDIRECT)
// On-disk inode structure // On-disk inode structure
...@@ -30,9 +26,9 @@ struct dinode { ...@@ -30,9 +26,9 @@ struct dinode {
short major; // Major device number (T_DEV only) short major; // Major device number (T_DEV only)
short minor; // Minor device number (T_DEV only) short minor; // Minor device number (T_DEV only)
short nlink; // Number of links to inode in file system short nlink; // Number of links to inode in file system
uint size; // Size of file (bytes) u32 size; // Size of file (bytes)
uint gen; // Generation # (to check name cache) u32 gen; // Generation # (to check name cache)
uint addrs[NDIRECT+1]; // Data block addresses u32 addrs[NDIRECT+1]; // Data block addresses
}; };
// Inodes per block. // Inodes per block.
...@@ -51,8 +47,6 @@ struct dinode { ...@@ -51,8 +47,6 @@ struct dinode {
#define DIRSIZ 14 #define DIRSIZ 14
struct dirent { struct dirent {
ushort inum; u16 inum;
char name[DIRSIZ]; char name[DIRSIZ];
}; };
#endif
...@@ -44,6 +44,11 @@ void snprintf(char *buf, u32 n, char *fmt, ...); ...@@ -44,6 +44,11 @@ void snprintf(char *buf, u32 n, char *fmt, ...);
int namecmp(const char*, const char*); int namecmp(const char*, const char*);
struct inode* namei(char*); struct inode* namei(char*);
void iput(struct inode*); void iput(struct inode*);
struct inode* iget(u32 dev, u32 inum);
void ilock(struct inode*, int writer);
void iunlockput(struct inode*);
void iupdate(struct inode*);
void iunlock(struct inode*);
// ide.c // ide.c
void ideinit(void); void ideinit(void);
......
...@@ -17,6 +17,8 @@ extern void initkalloc(void); ...@@ -17,6 +17,8 @@ extern void initkalloc(void);
extern void initrcu(void); extern void initrcu(void);
extern void initproc(void); extern void initproc(void);
extern void initbio(void); extern void initbio(void);
extern void initinode(void);
extern void initdisk(void);
extern void inituser(void); extern void inituser(void);
static volatile int bstate; static volatile int bstate;
...@@ -30,9 +32,14 @@ mpboot(void) ...@@ -30,9 +32,14 @@ mpboot(void)
initseg(); initseg();
initlapic(); initlapic();
inittls(); inittls();
// XXX
// XXX
// XXX
scheduler(); // start running processes scheduler(); // start running processes
#if 0
bstate = 1; bstate = 1;
panic("mpboot"); scheduler(); // start running processes
#endif
} }
// Start the non-boot processors. // Start the non-boot processors.
...@@ -87,12 +94,10 @@ cmain(void) ...@@ -87,12 +94,10 @@ cmain(void)
initrcu(); // initialize rcu module initrcu(); // initialize rcu module
initproc(); // process table initproc(); // process table
initbio(); // buffer cache initbio(); // buffer cache
initinode(); // inode cache
initdisk(); // disk
#if 0 cprintf("booting others..\n");
fileinit(); // file table
iinit(); // inode cache
ideinit(); // disk
#endif
inituser(); // first user process inituser(); // first user process
bootothers(); // start other processors bootothers(); // start other processors
......
...@@ -2,25 +2,27 @@ ...@@ -2,25 +2,27 @@
// Useful for running kernel without scratch disk. // Useful for running kernel without scratch disk.
#include "types.h" #include "types.h"
#include "defs.h"
#include "param.h" #include "param.h"
#include "mmu.h" #include "mmu.h"
#include "kernel.h"
#include "spinlock.h"
#include "queue.h"
#include "condvar.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
#include "traps.h" #include "traps.h"
#include "spinlock.h"
#include "buf.h" #include "buf.h"
extern uchar _binary_fs_img_start[], _binary_fs_img_size[]; extern u8 _binary_fs_img_start[], _binary_fs_img_size[];
static int disksize; static u64 disksize;
static uchar *memdisk; static u8 *memdisk;
void void
ideinit(void) initdisk(void)
{ {
memdisk = _binary_fs_img_start; memdisk = _binary_fs_img_start;
disksize = (uint)_binary_fs_img_size/512; disksize = (u64)_binary_fs_img_size/512;
} }
// Interrupt handler. // Interrupt handler.
...@@ -36,7 +38,7 @@ ideintr(void) ...@@ -36,7 +38,7 @@ ideintr(void)
void void
iderw(struct buf *b) iderw(struct buf *b)
{ {
uchar *p; u8 *p;
if(!(b->flags & B_BUSY)) if(!(b->flags & B_BUSY))
panic("iderw: buf not busy"); panic("iderw: buf not busy");
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
struct stat { struct stat {
short type; // Type of file short type; // Type of file
int dev; // Device number int dev; // Device number
uint ino; // Inode number on device u32 ino; // Inode number on device
short nlink; // Number of links to file short nlink; // Number of links to file
uint size; // Size of file in bytes u32 size; // Size of file in bytes
}; };
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论