Skip to content

Commit

Permalink
Increment the max file size
Browse files Browse the repository at this point in the history
  • Loading branch information
cw00h committed Jan 20, 2023
1 parent 46bcbaf commit 9f1aa19
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion kernel/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct inode {
short minor;
short nlink;
uint size;
uint addrs[NDIRECT+1];
uint addrs[NDIRECT+2];
};

// map major device number to device functions.
Expand Down
52 changes: 48 additions & 4 deletions kernel/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ iunlockput(struct inode *ip)
static uint
bmap(struct inode *ip, uint bn)
{
uint addr, *a;
struct buf *bp;
uint addr, *a, idx;
struct buf *bp, *bp2;

if(bn < NDIRECT){
if((addr = ip->addrs[bn]) == 0)
Expand All @@ -400,6 +400,29 @@ bmap(struct inode *ip, uint bn)
brelse(bp);
return addr;
}
bn -= NINDIRECT;

if(bn < NDBLINDIRECT) {
idx = bn / NINDIRECT; // Index in the doubly-indirect block
if((addr = ip->addrs[NDIRECT + 1]) == 0) { // The doubly-indirect block is not allocated
ip->addrs[NDIRECT + 1] = addr = balloc(ip->dev);
}
bp = bread(ip->dev, addr);
a = (uint*)bp->data;
if((addr = a[idx]) == 0) { // The singly-indirect block pointed by the doubly-indirect block is not allocated
a[idx] = addr = balloc(ip->dev);
log_write(bp);
}
bp2 = bread(ip->dev, addr);
a = (uint*)bp2->data;
if((addr = a[bn % NINDIRECT]) == 0) { // The data block is not allocated
a[bn % NINDIRECT] = addr = balloc(ip->dev);
log_write(bp2);
}
brelse(bp);
brelse(bp2);
return addr;
}

panic("bmap: out of range");
}
Expand All @@ -410,8 +433,8 @@ void
itrunc(struct inode *ip)
{
int i, j;
struct buf *bp;
uint *a;
struct buf *bp, *bp2;
uint *a, *b;

for(i = 0; i < NDIRECT; i++){
if(ip->addrs[i]){
Expand All @@ -432,6 +455,27 @@ itrunc(struct inode *ip)
ip->addrs[NDIRECT] = 0;
}

if(ip->addrs[NDIRECT + 1]) {
bp = bread(ip->dev, ip->addrs[NDIRECT + 1]);
a = (uint*)bp->data;
for(i = 0; i < NINDIRECT; i++) {
if(a[i]) {
bp2 = bread(ip->dev, a[i]);
b = (uint*)bp2->data;
for(j = 0; j < NINDIRECT; j++) {
if(b[j]) {
bfree(ip->dev, b[j]);
}
}
brelse(bp2);
bfree(ip->dev, a[i]);
}
}
brelse(bp);
bfree(ip->dev, ip->addrs[NDIRECT + 1]);
ip->addrs[NDIRECT + 1] = 0;
}

ip->size = 0;
iupdate(ip);
}
Expand Down
7 changes: 4 additions & 3 deletions kernel/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ struct superblock {

#define FSMAGIC 0x10203040

#define NDIRECT 12
#define NDIRECT 11
#define NINDIRECT (BSIZE / sizeof(uint))
#define MAXFILE (NDIRECT + NINDIRECT)
#define NDBLINDIRECT (((BSIZE * BSIZE) / sizeof(uint)) / sizeof(uint))
#define MAXFILE (NDIRECT + NINDIRECT + NDBLINDIRECT)

// On-disk inode structure
struct dinode {
Expand All @@ -35,7 +36,7 @@ struct dinode {
short minor; // Minor device number (T_DEVICE only)
short nlink; // Number of links to inode in file system
uint size; // Size of file (bytes)
uint addrs[NDIRECT+1]; // Data block addresses
uint addrs[NDIRECT+2]; // Data block addresses
};

// Inodes per block.
Expand Down

0 comments on commit 9f1aa19

Please sign in to comment.