various cleanups
This commit is contained in:
parent
b6095304b7
commit
7d4aef6cfd
1 changed files with 43 additions and 56 deletions
53
fs.c
53
fs.c
|
@ -41,13 +41,9 @@ iinit(void)
|
||||||
static uint
|
static uint
|
||||||
balloc(uint dev)
|
balloc(uint dev)
|
||||||
{
|
{
|
||||||
int b;
|
int b, bi, m, ninodes, size;
|
||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
struct superblock *sb;
|
struct superblock *sb;
|
||||||
int bi = 0;
|
|
||||||
int size;
|
|
||||||
int ninodes;
|
|
||||||
uchar m;
|
|
||||||
|
|
||||||
bp = bread(dev, 1);
|
bp = bread(dev, 1);
|
||||||
sb = (struct superblock*) bp->data;
|
sb = (struct superblock*) bp->data;
|
||||||
|
@ -62,17 +58,14 @@ balloc(uint dev)
|
||||||
bi = b % BPB;
|
bi = b % BPB;
|
||||||
m = 0x1 << (bi % 8);
|
m = 0x1 << (bi % 8);
|
||||||
if((bp->data[bi/8] & m) == 0) { // is block free?
|
if((bp->data[bi/8] & m) == 0) { // is block free?
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(b >= size)
|
|
||||||
panic("balloc: out of blocks");
|
|
||||||
|
|
||||||
bp->data[bi/8] |= 0x1 << (bi % 8);
|
bp->data[bi/8] |= 0x1 << (bi % 8);
|
||||||
bwrite(bp, BBLOCK(b, ninodes)); // mark it allocated on disk
|
bwrite(bp, BBLOCK(b, ninodes)); // mark it allocated on disk
|
||||||
brelse(bp);
|
brelse(bp);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
panic("balloc: out of blocks");
|
||||||
|
}
|
||||||
|
|
||||||
// Free a disk block.
|
// Free a disk block.
|
||||||
static void
|
static void
|
||||||
|
@ -80,9 +73,7 @@ bfree(int dev, uint b)
|
||||||
{
|
{
|
||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
struct superblock *sb;
|
struct superblock *sb;
|
||||||
int bi;
|
int bi, m, ninodes;
|
||||||
int ninodes;
|
|
||||||
uchar m;
|
|
||||||
|
|
||||||
bp = bread(dev, 1);
|
bp = bread(dev, 1);
|
||||||
sb = (struct superblock*) bp->data;
|
sb = (struct superblock*) bp->data;
|
||||||
|
@ -96,8 +87,8 @@ bfree(int dev, uint b)
|
||||||
|
|
||||||
bp = bread(dev, BBLOCK(b, ninodes));
|
bp = bread(dev, BBLOCK(b, ninodes));
|
||||||
bi = b % BPB;
|
bi = b % BPB;
|
||||||
m = ~(0x1 << (bi %8));
|
m = 0x1 << (bi % 8);
|
||||||
bp->data[bi/8] &= m;
|
bp->data[bi/8] &= ~m;
|
||||||
bwrite(bp, BBLOCK(b, ninodes)); // mark it free on disk
|
bwrite(bp, BBLOCK(b, ninodes)); // mark it free on disk
|
||||||
brelse(bp);
|
brelse(bp);
|
||||||
}
|
}
|
||||||
|
@ -176,7 +167,7 @@ iupdate(struct inode *ip)
|
||||||
dip->nlink = ip->nlink;
|
dip->nlink = ip->nlink;
|
||||||
dip->size = ip->size;
|
dip->size = ip->size;
|
||||||
memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
|
memmove(dip->addrs, ip->addrs, sizeof(ip->addrs));
|
||||||
bwrite(bp, IBLOCK(ip->inum)); // mark it allocated on the disk
|
bwrite(bp, IBLOCK(ip->inum));
|
||||||
brelse(bp);
|
brelse(bp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,7 +177,7 @@ struct inode*
|
||||||
ialloc(uint dev, short type)
|
ialloc(uint dev, short type)
|
||||||
{
|
{
|
||||||
struct inode *ip;
|
struct inode *ip;
|
||||||
struct dinode *dip = 0;
|
struct dinode *dip;
|
||||||
struct superblock *sb;
|
struct superblock *sb;
|
||||||
int ninodes;
|
int ninodes;
|
||||||
int inum;
|
int inum;
|
||||||
|
@ -290,12 +281,13 @@ itrunc(struct inode *ip)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
struct buf *inbp;
|
struct buf *inbp;
|
||||||
|
uint *a;
|
||||||
|
|
||||||
for(i = 0; i < NADDRS; i++) {
|
for(i = 0; i < NADDRS; i++) {
|
||||||
if(ip->addrs[i] != 0) {
|
if(ip->addrs[i] != 0) {
|
||||||
if(i == INDIRECT) {
|
if(i == INDIRECT) {
|
||||||
inbp = bread(ip->dev, ip->addrs[INDIRECT]);
|
inbp = bread(ip->dev, ip->addrs[INDIRECT]);
|
||||||
uint *a = (uint*) inbp->data;
|
a = (uint*)inbp->data;
|
||||||
for(j = 0; j < NINDIRECT; j++) {
|
for(j = 0; j < NINDIRECT; j++) {
|
||||||
if(a[j] != 0) {
|
if(a[j] != 0) {
|
||||||
bfree(ip->dev, a[j]);
|
bfree(ip->dev, a[j]);
|
||||||
|
@ -372,7 +364,7 @@ stati(struct inode *ip, struct stat *st)
|
||||||
int
|
int
|
||||||
readi(struct inode *ip, char *dst, uint off, uint n)
|
readi(struct inode *ip, char *dst, uint off, uint n)
|
||||||
{
|
{
|
||||||
uint target = n, n1;
|
uint target, n1;
|
||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
|
|
||||||
if(ip->type == T_DEV) {
|
if(ip->type == T_DEV) {
|
||||||
|
@ -381,6 +373,7 @@ readi(struct inode *ip, char *dst, uint off, uint n)
|
||||||
return devsw[ip->major].read(ip->minor, dst, n);
|
return devsw[ip->major].read(ip->minor, dst, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target = n;
|
||||||
while(n > 0 && off < ip->size){
|
while(n > 0 && off < ip->size){
|
||||||
bp = bread(ip->dev, bmap(ip, off / BSIZE));
|
bp = bread(ip->dev, bmap(ip, off / BSIZE));
|
||||||
n1 = min(n, ip->size - off);
|
n1 = min(n, ip->size - off);
|
||||||
|
@ -435,16 +428,16 @@ newblock(struct inode *ip, uint lbn)
|
||||||
int
|
int
|
||||||
writei(struct inode *ip, char *addr, uint off, uint n)
|
writei(struct inode *ip, char *addr, uint off, uint n)
|
||||||
{
|
{
|
||||||
|
struct buf *bp;
|
||||||
|
int r, m, lbn;
|
||||||
|
|
||||||
if(ip->type == T_DEV) {
|
if(ip->type == T_DEV) {
|
||||||
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->minor, addr, n);
|
return devsw[ip->major].write(ip->minor, addr, n);
|
||||||
} else if(ip->type == T_FILE || ip->type == T_DIR) {
|
}
|
||||||
struct buf *bp;
|
|
||||||
int r = 0;
|
for(r=0; r<n; ) {
|
||||||
int m;
|
|
||||||
int lbn;
|
|
||||||
while(r < n) {
|
|
||||||
lbn = off / BSIZE;
|
lbn = off / BSIZE;
|
||||||
if(lbn >= MAXFILE)
|
if(lbn >= MAXFILE)
|
||||||
return r;
|
return r;
|
||||||
|
@ -460,20 +453,14 @@ writei(struct inode *ip, char *addr, uint off, uint n)
|
||||||
r += m;
|
r += m;
|
||||||
off += m;
|
off += m;
|
||||||
}
|
}
|
||||||
if(r > 0) {
|
if(r > 0 && off > ip->size) {
|
||||||
if(off > ip->size) {
|
|
||||||
if(ip->type == T_DIR)
|
if(ip->type == T_DIR)
|
||||||
ip->size = ((off / BSIZE) + 1) * BSIZE;
|
ip->size = ((off / BSIZE) + 1) * BSIZE;
|
||||||
else
|
else
|
||||||
ip->size = off;
|
ip->size = off;
|
||||||
}
|
|
||||||
iupdate(ip);
|
iupdate(ip);
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
} else {
|
|
||||||
panic("writei: unknown type");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip over the next path element in path,
|
// Skip over the next path element in path,
|
||||||
|
|
Loading…
Reference in a new issue