bigfile ok
This commit is contained in:
parent
888b75593b
commit
f187a07854
@ -26,7 +26,7 @@ struct inode {
|
|||||||
short minor;
|
short minor;
|
||||||
short nlink;
|
short nlink;
|
||||||
uint size;
|
uint size;
|
||||||
uint addrs[NDIRECT+1];
|
uint addrs[NDIRECT+2];
|
||||||
};
|
};
|
||||||
|
|
||||||
// map major device number to device functions.
|
// map major device number to device functions.
|
||||||
|
|||||||
64
kernel/fs.c
64
kernel/fs.c
@ -416,7 +416,43 @@ bmap(struct inode *ip, uint bn)
|
|||||||
brelse(bp);
|
brelse(bp);
|
||||||
return addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
bn -= NINDIRECT;
|
||||||
|
if (bn < NINDIRECT2) {
|
||||||
|
// Load doubly indirect block L1
|
||||||
|
if ((addr = ip->addrs[NDIRECT+1]) == 0){
|
||||||
|
addr = balloc(ip->dev);
|
||||||
|
if(addr == 0)
|
||||||
|
return 0;
|
||||||
|
ip->addrs[NDIRECT+1] = addr;
|
||||||
|
}
|
||||||
|
// load doubly indirect block L2
|
||||||
|
// we have NINDIRECT blocks for per L1 entry
|
||||||
|
bp = bread(ip->dev, addr);
|
||||||
|
a = (uint*)bp->data;
|
||||||
|
if ((addr = a[bn / NINDIRECT]) == 0) {
|
||||||
|
addr = balloc(ip->dev);
|
||||||
|
if (addr == 0) {
|
||||||
|
brelse(bp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
a[bn / NINDIRECT] = addr;
|
||||||
|
log_write(bp);
|
||||||
|
}
|
||||||
|
brelse(bp);
|
||||||
|
// Load finally the target block
|
||||||
|
bp = bread(ip->dev, addr);
|
||||||
|
a = (uint*)bp->data;
|
||||||
|
if ((addr = a[bn % NINDIRECT]) == 0) {
|
||||||
|
addr = balloc(ip->dev);
|
||||||
|
if (addr) {
|
||||||
|
a[bn % NINDIRECT] = addr;
|
||||||
|
log_write(bp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
brelse(bp);
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
printf("bn=%d\n", bn);
|
||||||
panic("bmap: out of range");
|
panic("bmap: out of range");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,9 +461,9 @@ bmap(struct inode *ip, uint bn)
|
|||||||
void
|
void
|
||||||
itrunc(struct inode *ip)
|
itrunc(struct inode *ip)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j, k;
|
||||||
struct buf *bp;
|
struct buf *bp, *bp1;
|
||||||
uint *a;
|
uint *a, *a1;
|
||||||
|
|
||||||
for(i = 0; i < NDIRECT; i++){
|
for(i = 0; i < NDIRECT; i++){
|
||||||
if(ip->addrs[i]){
|
if(ip->addrs[i]){
|
||||||
@ -448,6 +484,26 @@ itrunc(struct inode *ip)
|
|||||||
ip->addrs[NDIRECT] = 0;
|
ip->addrs[NDIRECT] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ip->addrs[NDIRECT+1]) {
|
||||||
|
bp = bread(ip->dev, ip->addrs[NDIRECT+1]);
|
||||||
|
a = (uint*)bp->data;
|
||||||
|
for(j = 0; j < NINDIRECT; j++){
|
||||||
|
if(a[j]) {
|
||||||
|
bp1 = bread(ip->dev, a[j]);
|
||||||
|
a1 = (uint*)bp1->data;
|
||||||
|
for (k = 0; k < NINDIRECT; ++ k) {
|
||||||
|
if (a1[k]) {
|
||||||
|
bfree(ip->dev, a1[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
brelse(bp1);
|
||||||
|
bfree(ip->dev, a[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
brelse(bp);
|
||||||
|
bfree(ip->dev, ip->addrs[NDIRECT+1]);
|
||||||
|
ip->addrs[NDIRECT+1] = 0;
|
||||||
|
}
|
||||||
ip->size = 0;
|
ip->size = 0;
|
||||||
iupdate(ip);
|
iupdate(ip);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,9 +24,10 @@ struct superblock {
|
|||||||
|
|
||||||
#define FSMAGIC 0x10203040
|
#define FSMAGIC 0x10203040
|
||||||
|
|
||||||
#define NDIRECT 12
|
#define NDIRECT 11
|
||||||
#define NINDIRECT (BSIZE / sizeof(uint))
|
#define NINDIRECT (BSIZE / sizeof(uint))
|
||||||
#define MAXFILE (NDIRECT + NINDIRECT)
|
#define NINDIRECT2 (NINDIRECT * NINDIRECT)
|
||||||
|
#define MAXFILE (NDIRECT + NINDIRECT + NINDIRECT2)
|
||||||
|
|
||||||
// On-disk inode structure
|
// On-disk inode structure
|
||||||
struct dinode {
|
struct dinode {
|
||||||
@ -35,7 +36,7 @@ struct dinode {
|
|||||||
short minor; // Minor device number (T_DEVICE only)
|
short minor; // Minor device number (T_DEVICE only)
|
||||||
short nlink; // Number of links to inode in file system
|
short nlink; // Number of links to inode in file system
|
||||||
uint size; // Size of file (bytes)
|
uint size; // Size of file (bytes)
|
||||||
uint addrs[NDIRECT+1]; // Data block addresses
|
uint addrs[NDIRECT+2]; // Data block addresses
|
||||||
};
|
};
|
||||||
|
|
||||||
// Inodes per block.
|
// Inodes per block.
|
||||||
|
|||||||
@ -32,9 +32,11 @@ main()
|
|||||||
printf("bigfile: file is too small\n");
|
printf("bigfile: file is too small\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
printf("bef close fd\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
printf("aft close fd\n");
|
||||||
fd = open("big.file", O_RDONLY);
|
fd = open("big.file", O_RDONLY);
|
||||||
|
printf("aft open\n");
|
||||||
if(fd < 0){
|
if(fd < 0){
|
||||||
printf("bigfile: cannot re-open big.file for reading\n");
|
printf("bigfile: cannot re-open big.file for reading\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user