clean namei search loop

This commit is contained in:
rsc 2006-09-07 15:15:32 +00:00
parent 7a054598fa
commit 0d6bbd3172

41
fs.c
View file

@ -122,7 +122,7 @@ iget(uint dev, uint inum)
loop: loop:
nip = 0; nip = 0;
for(ip = &inode[0]; ip < &inode[NINODE]; ip++){ for(ip = &inode[0]; ip < &inode[NINODE]; ip++){
if(ip->count > 0 && ip->dev == dev && ip->inum == inum){ if(ip->ref > 0 && ip->dev == dev && ip->inum == inum){
if(ip->busy){ if(ip->busy){
sleep(ip, &inode_table_lock); sleep(ip, &inode_table_lock);
// Since we droped inode_table_lock, ip might have been reused // Since we droped inode_table_lock, ip might have been reused
@ -131,12 +131,12 @@ iget(uint dev, uint inum)
// and it will not be busy. // and it will not be busy.
goto loop; goto loop;
} }
ip->count++; ip->ref++;
ip->busy = 1; ip->busy = 1;
release(&inode_table_lock); release(&inode_table_lock);
return ip; return ip;
} }
if(nip == 0 && ip->count == 0) if(nip == 0 && ip->ref == 0)
nip = ip; nip = ip;
} }
@ -145,7 +145,7 @@ iget(uint dev, uint inum)
nip->dev = dev; nip->dev = dev;
nip->inum = inum; nip->inum = inum;
nip->count = 1; nip->ref = 1;
nip->busy = 1; nip->busy = 1;
release(&inode_table_lock); release(&inode_table_lock);
@ -236,7 +236,7 @@ ifree(struct inode *ip)
void void
ilock(struct inode *ip) ilock(struct inode *ip)
{ {
if(ip->count < 1) if(ip->ref < 1)
panic("ilock"); panic("ilock");
acquire(&inode_table_lock); acquire(&inode_table_lock);
@ -254,7 +254,7 @@ ilock(struct inode *ip)
void void
iunlock(struct inode *ip) iunlock(struct inode *ip)
{ {
if(ip->busy != 1 || ip->count < 1) if(ip->busy != 1 || ip->ref < 1)
panic("iunlock"); panic("iunlock");
acquire(&inode_table_lock); acquire(&inode_table_lock);
@ -326,17 +326,17 @@ itrunc(struct inode *ip)
void void
iput(struct inode *ip) iput(struct inode *ip)
{ {
if(ip->count < 1 || ip->busy != 1) if(ip->ref < 1 || ip->busy != 1)
panic("iput"); panic("iput");
if((ip->count == 1) && (ip->nlink == 0)) { if((ip->ref == 1) && (ip->nlink == 0)) {
itrunc(ip); itrunc(ip);
ifree(ip); ifree(ip);
} }
acquire(&inode_table_lock); acquire(&inode_table_lock);
ip->count -= 1; ip->ref -= 1;
ip->busy = 0; ip->busy = 0;
wakeup(ip); wakeup(ip);
@ -357,7 +357,7 @@ void
iincref(struct inode *ip) iincref(struct inode *ip)
{ {
ilock(ip); ilock(ip);
ip->count++; ip->ref++;
iunlock(ip); iunlock(ip);
} }
@ -522,10 +522,10 @@ namei(char *path, int mode, uint *ret_off,
ilock(dp); ilock(dp);
} }
for(;;){
while(*cp == '/') while(*cp == '/')
cp++; cp++;
for(;;){
if(*cp == '\0'){ if(*cp == '\0'){
if(mode == NAMEI_LOOKUP) if(mode == NAMEI_LOOKUP)
return dp; return dp;
@ -542,6 +542,13 @@ namei(char *path, int mode, uint *ret_off,
return 0; return 0;
} }
for(i = 0; cp[i] != 0 && cp[i] != '/'; i++)
;
if(i > DIRSIZ){
iput(dp);
return 0;
}
for(off = 0; off < dp->size; off += BSIZE){ for(off = 0; off < dp->size; off += BSIZE){
bp = bread(dp->dev, bmap(dp, off / BSIZE)); bp = bread(dp->dev, bmap(dp, off / BSIZE));
for(ep = (struct dirent*) bp->data; for(ep = (struct dirent*) bp->data;
@ -549,13 +556,9 @@ namei(char *path, int mode, uint *ret_off,
ep++){ ep++){
if(ep->inum == 0) if(ep->inum == 0)
continue; continue;
for(i = 0; i < DIRSIZ && cp[i] != '/' && cp[i]; i++) if(memcmp(cp, ep->name, i) == 0 &&
if(cp[i] != ep->name[i]) (i == DIRSIZ || ep->name[i]== 0)){
break; // entry matches path element
if((cp[i] == '\0' || cp[i] == '/' || i >= DIRSIZ) &&
(i >= DIRSIZ || ep->name[i] == '\0')){
while(cp[i] != '\0' && cp[i] != '/')
i++;
off += (uchar*)ep - bp->data; off += (uchar*)ep - bp->data;
ninum = ep->inum; ninum = ep->inum;
brelse(bp); brelse(bp);
@ -591,8 +594,6 @@ namei(char *path, int mode, uint *ret_off,
dp = iget(dev, ninum); dp = iget(dev, ninum);
if(dp->type == 0 || dp->nlink < 1) if(dp->type == 0 || dp->nlink < 1)
panic("namei"); panic("namei");
while(*cp == '/')
cp++;
} }
} }