J
Joseph
Modify the file system to allow the maximum size of a file to be as
large as the disk (128Kbytes). In the basic file system, each file is
limited to a file size of just under 4Kbytes. Each file has a header
(class FileHeader) that is a table of direct pointers to the disk blocks
for that file. Since the header is stored in one disk sector, the
maximum size of a file is limited by the number of pointers that will
fit in one disk sector. Increasing the limit to 128KBytes will probably
but not necessarily require you to implement double indirect, or
linked-list blocks.
i get a segment fault when the file size is larger thank 8K. I'm pretty
sure its in the Allocation method.
attachment is the code
// modified
// filehdr.cc
// Routines for managing the disk file header (in UNIX, this
// would be called the i-node).
//
// The file header is used to locate where on disk the
// file's data is stored. We implement this as a fixed size
// table of pointers -- each entry in the table points to the
// disk sector containing that portion of the file data
// (in other words, there are no indirect or doubly indirect
// blocks). The table size is chosen so that the file header
// will be just big enough to fit in one disk sector,
//
// Unlike in a real system, we do not keep track of file permissions,
// ownership, last modification date, etc., in the file header.
//
// A file header can be initialized in two ways:
// for a new file, by modifying the in-memory data structure
// to point to the newly allocated data blocks
// for a file already on disk, by reading the file header from disk
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#include "copyright.h"
#include "system.h"
#include "filehdr.h"
//----------------------------------------------------------------------
// FileHeader::Allocate
// Initialize a fresh file header for a newly created file.
// Allocate data blocks for the file out of the map of free disk blocks.
// Return FALSE if there are not enough free blocks to accomodate
// the new file.
//
// "freeMap" is the bit map of free disk sectors
// "fileSize" is the bit map of free disk sectors
//----------------------------------------------------------------------
bool
FileHeader::Allocate(BitMap *freeMap, int fileSize)
{
int i;
numBytes = fileSize;
numSectors = divRoundUp(fileSize, SectorSize);
if (freeMap->NumClear() < (numSectors+1))
return FALSE; // not enough space
int directSectors = numSectors % NumDirect;
for (i = 0; i < directSectors; i++)
dataSectors = freeMap->Find();
if( (numSectors / NumDirect) > 0 ) {
indirectHdrSector = freeMap->Find();
FileHeader *hdr = new FileHeader;
for( i = 0; i < (numSectors - directSectors); i++ )
hdr->dataSectors = freeMap->Find();
hdr->WriteBack(indirectHdrSector);
delete hdr;
}
return TRUE;
}
//----------------------------------------------------------------------
// FileHeader:eallocate
// De-allocate all the space allocated for data blocks for this file.
//
// "freeMap" is the bit map of free disk sectors
//----------------------------------------------------------------------
void
FileHeader:eallocate(BitMap *freeMap)
{
int i;
int directSectors = numSectors % NumDirect;
for (i = 0; i < directSectors; i++) {
ASSERT(freeMap->Test((int) dataSectors)); // ought to be marked!
freeMap->Clear((int) dataSectors);
}
if( indirectHdrSector != 0 ) {
FileHeader *hdr = new FileHeader;
hdr->FetchFrom(indirectHdrSector);
for( i = 0; i < (numSectors - directSectors); i++) {
ASSERT(freeMap->Test((int) hdr->dataSectors));
freeMap->Clear((int) hdr->dataSectors);
}
delete hdr;
}
}
//----------------------------------------------------------------------
// FileHeader::FetchFrom
// Fetch contents of file header from disk.
//
// "sector" is the disk sector containing the file header
//----------------------------------------------------------------------
void
FileHeader::FetchFrom(int sector)
{
DEBUG('z', "Reading the sector from FileHeader:%d:\n", sector);
synchDisk->ReadSector(sector, (char *)this);
}
//----------------------------------------------------------------------
// FileHeader::WriteBack
// Write the modified contents of the file header back to disk.
//
// "sector" is the disk sector to contain the file header
//----------------------------------------------------------------------
void
FileHeader::WriteBack(int sector)
{
synchDisk->WriteSector(sector, (char *)this);
}
//----------------------------------------------------------------------
// FileHeader::ByteToSector
// Return which disk sector is storing a particular byte within the file.
// This is essentially a translation from a virtual address (the
// offset in the file) to a physical address (the sector where the
// data at the offset is stored).
//
// "offset" is the location within the file of the byte in question
//----------------------------------------------------------------------
int
FileHeader::ByteToSector(int offset)
{
int SectorNum = offset / SectorSize;
if( SectorNum < NumDirect )
return dataSectors[SectorNum];
else {
FileHeader *hdr = new FileHeader;
hdr->FetchFrom(indirectHdrSector);
int returnNum = hdr->dataSectors[ SectorNum - NumDirect ];
delete hdr;
return returnNum;
}
//return(dataSectors[offset / SectorSize]);
}
//----------------------------------------------------------------------
// FileHeader::FileLength
// Return the number of bytes in the file.
//----------------------------------------------------------------------
int
FileHeader::FileLength()
{
return numBytes;
}
//----------------------------------------------------------------------
// FileHeader:rint
// Print the contents of the file header, and the contents of all
// the data blocks pointed to by the file header.
//----------------------------------------------------------------------
void
FileHeader:rint()
{
int i, j, k;
char *data = new char[SectorSize];
printf("FileHeader contents. File size: %d. File blocks:\n", numBytes);
for (i = 0; i < numSectors; i++)
printf("%d ", dataSectors);
printf("\nFile contents:\n");
for (i = k = 0; i < numSectors; i++) {
synchDisk->ReadSector(dataSectors, data);
for (j = 0; (j < SectorSize) && (k < numBytes); j++, k++) {
if ('\040' <= data[j] && data[j] <= '\176') // isprint(data[j])
printf("%c", data[j]);
else
printf("\\%x", (unsigned char)data[j]);
}
printf("\n");
}
delete [] data;
}
#ifdef CHANGED
void FileHeader::SetFileAttr(int TotBytes, int TotSectors) {
numBytes = TotBytes;
numSectors = TotSectors;
}
#endif
// modified
// filehdr.h
// Data structures for managing a disk file header.
//
// A file header describes where on disk to find the data in a file,
// along with other information about the file (for instance, its
// length, owner, etc.)
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#include "copyright.h"
#ifndef FILEHDR_H
#define FILEHDR_H
#include "disk.h"
#include "bitmap.h"
#define NumDirect ((SectorSize - 3 * sizeof(int)) / sizeof(int))
#define MaxFileSize (NumDirect * SectorSize)
// The following class defines the Nachos "file header" (in UNIX terms,
// the "i-node"), describing where on disk to find all of the data in the file.
// The file header is organized as a simple table of pointers to
// data blocks.
//
// The file header data structure can be stored in memory or on disk.
// When it is on disk, it is stored in a single sector -- this means
// that we assume the size of this data structure to be the same
// as one disk sector. Without indirect addressing, this
// limits the maximum file length to just under 4K bytes.
//
// There is no constructor; rather the file header can be initialized
// by allocating blocks for the file (if it is a new file), or by
// reading it from disk.
class FileHeader {
public:
bool Allocate(BitMap *bitMap, int fileSize);// Initialize a file header,
// including allocating space
// on disk for the file data
void Deallocate(BitMap *bitMap); // De-allocate this file's
// data blocks
void FetchFrom(int sectorNumber); // Initialize file header from disk
void WriteBack(int sectorNumber); // Write modifications to file header
// back to disk
int ByteToSector(int offset); // Convert a byte offset into the file
// to the disk sector containing
// the byte
int FileLength(); // Return the length of the file
// in bytes
void Print(); // Print the contents of the file.
#ifdef CHANGED
void SetFileAttr(int TotBytes, int TotSectors);
#endif
int dataSectors[NumDirect];
int indirectHdrSector;
private:
int numBytes; // Number of bytes in the file
int numSectors; // Number of data sectors in the file
// int dataSectors[NumDirect]; // Disk sector numbers for each data
// block in the file
};
#endif // FILEHDR_H
large as the disk (128Kbytes). In the basic file system, each file is
limited to a file size of just under 4Kbytes. Each file has a header
(class FileHeader) that is a table of direct pointers to the disk blocks
for that file. Since the header is stored in one disk sector, the
maximum size of a file is limited by the number of pointers that will
fit in one disk sector. Increasing the limit to 128KBytes will probably
but not necessarily require you to implement double indirect, or
linked-list blocks.
i get a segment fault when the file size is larger thank 8K. I'm pretty
sure its in the Allocation method.
attachment is the code
// modified
// filehdr.cc
// Routines for managing the disk file header (in UNIX, this
// would be called the i-node).
//
// The file header is used to locate where on disk the
// file's data is stored. We implement this as a fixed size
// table of pointers -- each entry in the table points to the
// disk sector containing that portion of the file data
// (in other words, there are no indirect or doubly indirect
// blocks). The table size is chosen so that the file header
// will be just big enough to fit in one disk sector,
//
// Unlike in a real system, we do not keep track of file permissions,
// ownership, last modification date, etc., in the file header.
//
// A file header can be initialized in two ways:
// for a new file, by modifying the in-memory data structure
// to point to the newly allocated data blocks
// for a file already on disk, by reading the file header from disk
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#include "copyright.h"
#include "system.h"
#include "filehdr.h"
//----------------------------------------------------------------------
// FileHeader::Allocate
// Initialize a fresh file header for a newly created file.
// Allocate data blocks for the file out of the map of free disk blocks.
// Return FALSE if there are not enough free blocks to accomodate
// the new file.
//
// "freeMap" is the bit map of free disk sectors
// "fileSize" is the bit map of free disk sectors
//----------------------------------------------------------------------
bool
FileHeader::Allocate(BitMap *freeMap, int fileSize)
{
int i;
numBytes = fileSize;
numSectors = divRoundUp(fileSize, SectorSize);
if (freeMap->NumClear() < (numSectors+1))
return FALSE; // not enough space
int directSectors = numSectors % NumDirect;
for (i = 0; i < directSectors; i++)
dataSectors = freeMap->Find();
if( (numSectors / NumDirect) > 0 ) {
indirectHdrSector = freeMap->Find();
FileHeader *hdr = new FileHeader;
for( i = 0; i < (numSectors - directSectors); i++ )
hdr->dataSectors = freeMap->Find();
hdr->WriteBack(indirectHdrSector);
delete hdr;
}
return TRUE;
}
//----------------------------------------------------------------------
// FileHeader:eallocate
// De-allocate all the space allocated for data blocks for this file.
//
// "freeMap" is the bit map of free disk sectors
//----------------------------------------------------------------------
void
FileHeader:eallocate(BitMap *freeMap)
{
int i;
int directSectors = numSectors % NumDirect;
for (i = 0; i < directSectors; i++) {
ASSERT(freeMap->Test((int) dataSectors)); // ought to be marked!
freeMap->Clear((int) dataSectors);
}
if( indirectHdrSector != 0 ) {
FileHeader *hdr = new FileHeader;
hdr->FetchFrom(indirectHdrSector);
for( i = 0; i < (numSectors - directSectors); i++) {
ASSERT(freeMap->Test((int) hdr->dataSectors));
freeMap->Clear((int) hdr->dataSectors);
}
delete hdr;
}
}
//----------------------------------------------------------------------
// FileHeader::FetchFrom
// Fetch contents of file header from disk.
//
// "sector" is the disk sector containing the file header
//----------------------------------------------------------------------
void
FileHeader::FetchFrom(int sector)
{
DEBUG('z', "Reading the sector from FileHeader:%d:\n", sector);
synchDisk->ReadSector(sector, (char *)this);
}
//----------------------------------------------------------------------
// FileHeader::WriteBack
// Write the modified contents of the file header back to disk.
//
// "sector" is the disk sector to contain the file header
//----------------------------------------------------------------------
void
FileHeader::WriteBack(int sector)
{
synchDisk->WriteSector(sector, (char *)this);
}
//----------------------------------------------------------------------
// FileHeader::ByteToSector
// Return which disk sector is storing a particular byte within the file.
// This is essentially a translation from a virtual address (the
// offset in the file) to a physical address (the sector where the
// data at the offset is stored).
//
// "offset" is the location within the file of the byte in question
//----------------------------------------------------------------------
int
FileHeader::ByteToSector(int offset)
{
int SectorNum = offset / SectorSize;
if( SectorNum < NumDirect )
return dataSectors[SectorNum];
else {
FileHeader *hdr = new FileHeader;
hdr->FetchFrom(indirectHdrSector);
int returnNum = hdr->dataSectors[ SectorNum - NumDirect ];
delete hdr;
return returnNum;
}
//return(dataSectors[offset / SectorSize]);
}
//----------------------------------------------------------------------
// FileHeader::FileLength
// Return the number of bytes in the file.
//----------------------------------------------------------------------
int
FileHeader::FileLength()
{
return numBytes;
}
//----------------------------------------------------------------------
// FileHeader:rint
// Print the contents of the file header, and the contents of all
// the data blocks pointed to by the file header.
//----------------------------------------------------------------------
void
FileHeader:rint()
{
int i, j, k;
char *data = new char[SectorSize];
printf("FileHeader contents. File size: %d. File blocks:\n", numBytes);
for (i = 0; i < numSectors; i++)
printf("%d ", dataSectors);
printf("\nFile contents:\n");
for (i = k = 0; i < numSectors; i++) {
synchDisk->ReadSector(dataSectors, data);
for (j = 0; (j < SectorSize) && (k < numBytes); j++, k++) {
if ('\040' <= data[j] && data[j] <= '\176') // isprint(data[j])
printf("%c", data[j]);
else
printf("\\%x", (unsigned char)data[j]);
}
printf("\n");
}
delete [] data;
}
#ifdef CHANGED
void FileHeader::SetFileAttr(int TotBytes, int TotSectors) {
numBytes = TotBytes;
numSectors = TotSectors;
}
#endif
// modified
// filehdr.h
// Data structures for managing a disk file header.
//
// A file header describes where on disk to find the data in a file,
// along with other information about the file (for instance, its
// length, owner, etc.)
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#include "copyright.h"
#ifndef FILEHDR_H
#define FILEHDR_H
#include "disk.h"
#include "bitmap.h"
#define NumDirect ((SectorSize - 3 * sizeof(int)) / sizeof(int))
#define MaxFileSize (NumDirect * SectorSize)
// The following class defines the Nachos "file header" (in UNIX terms,
// the "i-node"), describing where on disk to find all of the data in the file.
// The file header is organized as a simple table of pointers to
// data blocks.
//
// The file header data structure can be stored in memory or on disk.
// When it is on disk, it is stored in a single sector -- this means
// that we assume the size of this data structure to be the same
// as one disk sector. Without indirect addressing, this
// limits the maximum file length to just under 4K bytes.
//
// There is no constructor; rather the file header can be initialized
// by allocating blocks for the file (if it is a new file), or by
// reading it from disk.
class FileHeader {
public:
bool Allocate(BitMap *bitMap, int fileSize);// Initialize a file header,
// including allocating space
// on disk for the file data
void Deallocate(BitMap *bitMap); // De-allocate this file's
// data blocks
void FetchFrom(int sectorNumber); // Initialize file header from disk
void WriteBack(int sectorNumber); // Write modifications to file header
// back to disk
int ByteToSector(int offset); // Convert a byte offset into the file
// to the disk sector containing
// the byte
int FileLength(); // Return the length of the file
// in bytes
void Print(); // Print the contents of the file.
#ifdef CHANGED
void SetFileAttr(int TotBytes, int TotSectors);
#endif
int dataSectors[NumDirect];
int indirectHdrSector;
private:
int numBytes; // Number of bytes in the file
int numSectors; // Number of data sectors in the file
// int dataSectors[NumDirect]; // Disk sector numbers for each data
// block in the file
};
#endif // FILEHDR_H