tidy fs.c; bmap callers always expected allocation

This commit is contained in:
rsc 2009-05-31 01:44:20 +00:00
parent f12551b599
commit 13ae8808c4

39
fs.c
View file

@ -316,38 +316,28 @@ iupdate(struct inode *ip)
// listed in the block ip->addrs[INDIRECT]. // listed in the block ip->addrs[INDIRECT].
// 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, alloc controls whether one is allocated. // If there is no such block, bmap allocates one.
static uint static uint
bmap(struct inode *ip, uint bn, int alloc) bmap(struct inode *ip, uint bn)
{ {
uint addr, *a; uint addr, *a;
struct buf *bp; struct buf *bp;
if(bn < NDIRECT){ if(bn < NDIRECT){
if((addr = ip->addrs[bn]) == 0){ if((addr = ip->addrs[bn]) == 0)
if(!alloc)
return -1;
ip->addrs[bn] = addr = balloc(ip->dev); ip->addrs[bn] = addr = balloc(ip->dev);
}
return addr; return addr;
} }
bn -= NDIRECT; bn -= NDIRECT;
if(bn < NINDIRECT){ if(bn < NINDIRECT){
// Load indirect block, allocating if necessary. // Load indirect block, allocating if necessary.
if((addr = ip->addrs[NDIRECT]) == 0){ if((addr = ip->addrs[NDIRECT]) == 0)
if(!alloc)
return -1;
ip->addrs[NDIRECT] = addr = balloc(ip->dev); ip->addrs[NDIRECT] = addr = balloc(ip->dev);
}
bp = bread(ip->dev, addr); bp = bread(ip->dev, addr);
a = (uint*)bp->data; a = (uint*)bp->data;
if((addr = a[bn]) == 0){ if((addr = a[bn]) == 0){
if(!alloc){
brelse(bp);
return -1;
}
a[bn] = addr = balloc(ip->dev); a[bn] = addr = balloc(ip->dev);
bwrite(bp); bwrite(bp);
} }
@ -422,7 +412,7 @@ readi(struct inode *ip, char *dst, uint off, uint n)
n = ip->size - off; n = ip->size - off;
for(tot=0; tot<n; tot+=m, off+=m, dst+=m){ for(tot=0; tot<n; tot+=m, off+=m, dst+=m){
bp = bread(ip->dev, bmap(ip, off/BSIZE, 0)); bp = bread(ip->dev, bmap(ip, off/BSIZE));
m = min(n - tot, BSIZE - off%BSIZE); m = min(n - tot, BSIZE - off%BSIZE);
memmove(dst, bp->data + off%BSIZE, m); memmove(dst, bp->data + off%BSIZE, m);
brelse(bp); brelse(bp);
@ -444,13 +434,13 @@ writei(struct inode *ip, char *src, uint off, uint n)
return devsw[ip->major].write(ip, src, n); return devsw[ip->major].write(ip, src, n);
} }
if(off + n < off) if(off > ip->size || off + n < off)
return -1; return -1;
if(off + n > MAXFILE*BSIZE) if(off + n > MAXFILE*BSIZE)
n = MAXFILE*BSIZE - off; n = MAXFILE*BSIZE - off;
for(tot=0; tot<n; tot+=m, off+=m, src+=m){ for(tot=0; tot<n; tot+=m, off+=m, src+=m){
bp = bread(ip->dev, bmap(ip, off/BSIZE, 1)); bp = bread(ip->dev, bmap(ip, off/BSIZE));
m = min(n - tot, BSIZE - off%BSIZE); m = min(n - tot, BSIZE - off%BSIZE);
memmove(bp->data + off%BSIZE, src, m); memmove(bp->data + off%BSIZE, src, m);
bwrite(bp); bwrite(bp);
@ -487,7 +477,7 @@ dirlookup(struct inode *dp, char *name, uint *poff)
panic("dirlookup not DIR"); panic("dirlookup not DIR");
for(off = 0; off < dp->size; off += BSIZE){ for(off = 0; off < dp->size; off += BSIZE){
bp = bread(dp->dev, bmap(dp, off / BSIZE, 0)); bp = bread(dp->dev, bmap(dp, off / BSIZE));
for(de = (struct dirent*)bp->data; for(de = (struct dirent*)bp->data;
de < (struct dirent*)(bp->data + BSIZE); de < (struct dirent*)(bp->data + BSIZE);
de++){ de++){
@ -507,9 +497,9 @@ dirlookup(struct inode *dp, char *name, uint *poff)
return 0; return 0;
} }
// Write a new directory entry (name, ino) into the directory dp. // Write a new directory entry (name, inum) into the directory dp.
int int
dirlink(struct inode *dp, char *name, uint ino) dirlink(struct inode *dp, char *name, uint inum)
{ {
int off; int off;
struct dirent de; struct dirent de;
@ -530,7 +520,7 @@ dirlink(struct inode *dp, char *name, uint ino)
} }
strncpy(de.name, name, DIRSIZ); strncpy(de.name, name, DIRSIZ);
de.inum = ino; de.inum = inum;
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("dirlink"); panic("dirlink");
@ -549,6 +539,7 @@ dirlink(struct inode *dp, char *name, uint ino)
// Examples: // Examples:
// skipelem("a/bb/c", name) = "bb/c", setting name = "a" // skipelem("a/bb/c", name) = "bb/c", setting name = "a"
// skipelem("///a//bb", name) = "bb", setting name = "a" // skipelem("///a//bb", name) = "bb", setting name = "a"
// skipelem("a", name) = "", setting name = "a"
// skipelem("", name) = skipelem("////", name) = 0 // skipelem("", name) = skipelem("////", name) = 0
// //
static char* static char*
@ -580,7 +571,7 @@ skipelem(char *path, char *name)
// If parent != 0, return the inode for the parent and copy the final // If parent != 0, return the inode for the parent and copy the final
// path element into name, which must have room for DIRSIZ bytes. // path element into name, which must have room for DIRSIZ bytes.
static struct inode* static struct inode*
_namei(char *path, int parent, char *name) namex(char *path, int parent, char *name)
{ {
struct inode *ip, *next; struct inode *ip, *next;
@ -618,11 +609,11 @@ struct inode*
namei(char *path) namei(char *path)
{ {
char name[DIRSIZ]; char name[DIRSIZ];
return _namei(path, 0, name); return namex(path, 0, name);
} }
struct inode* struct inode*
nameiparent(char *path, char *name) nameiparent(char *path, char *name)
{ {
return _namei(path, 1, name); return namex(path, 1, name);
} }