diff --git a/fs.c b/fs.c
index 43d872293877fc634169f865cd07703df7897dcc..d3d10e1689db76ea33b33b8930ab4f6eca5b5b7d 100644
--- a/fs.c
+++ b/fs.c
@@ -201,20 +201,16 @@ ialloc(uint dev, short type)
     bp = bread(dev, IBLOCK(inum));
     dip = &((struct dinode*)(bp->data))[inum % IPB];
     if(dip->type == 0) {  // a free inode
-      break;
+      memset(dip, 0, sizeof(*dip));
+      dip->type = type;
+      bwrite(bp, IBLOCK(inum));   // mark it allocated on the disk
+      brelse(bp);
+      ip = iget(dev, inum);
+      return ip;
     }
     brelse(bp);
   }
-
-  if(inum >= ninodes)
-    panic("ialloc: no inodes left");
-
-  memset(dip, 0, sizeof(*dip));
-  dip->type = type;
-  bwrite(bp, IBLOCK(inum));   // mark it allocated on the disk
-  brelse(bp);
-  ip = iget(dev, inum);
-  return ip;
+  panic("ialloc: no inodes");
 }
 
 // Free the given inode from its file system.