Xv6 has been modified to support best-effort file recovery. This feature only works for files; directory recovery is not supported. A file can only be recovered if its integrity remains intact, meaning that none of its data blocks have been reused by another file or directory.
To make this possible, I introduced two significant changes to XV6:
- Track File Blocks:
Each file now has an array of its memory block addresses, stored in its file structure. - Deleted Flag in Directory Entries:
Each directory entry has adel
flag indicating whether the file is deleted.
By default, when a file is deleted in XV6, all its content is removed, and the busy flag is cleared (set to 0). Consequently, the file structure can be reused for storing data. I changed this behavior so that when a file is deleted, all of its data remains on disk, but the busy flag is set to 0, and the del
flag is set to 1. This way, the file’s data is preserved while the file structure remains available for reuse.
-
Two System Calls
-
int lsdel(char *path, char *result)
Lists all deleted files in a given directory.path
: Path to the directory to search.result
: A buffer where names of all deleted files are appended.- Return Value: The number of deleted files in the directory, or -1 if the path is invalid.
-
int rec(char *path)
Attempts a "best-effort" recovery of a deleted file.path
: Path to the file to be recovered.- Return Value:
- 0 = Recovery successful
- 1 = Invalid path
- 2 = File not found in the directory
- 3 = File structure is already in use by something else
- 4 = Some of the file’s blocks are used by another file or directory
-
-
Three User Programs
-
lsdel [path]
Prints deleted files from the specified directory. If no path is provided, it defaults to the current directory. -
rec <path>
Attempts to recover a file at the specified path, or prints an error message if recovery is not possible. -
writer <filename> <numberOfBytes>
Creates a file named<filename>
of size<numberOfBytes>
.
-
- Open a terminal in the project’s directory.
- Run
make clean
. - Run
make qemu
.
At this point, the XV6 operating system should start, and a QEMU window will be displayed.
- Type
writer a 500
- Type
rm a
- Type
rec a
- Type
cd home
- Type
writer a 500
- Type
rm a
- Type
cd ..
- Type
writer b 500
- Type
cd home
- Type
rec a
The error occurs because, after deleting file a
, file b
was created in another directory, which reused the file structure previously occupied by a
.
- Type
cd home
- Type
writer a 10
- Type
writer b 10
- Type
rm a b
- Type
cd ..
- Type
writer c 1500
- Type
cd home
- Type
rec b
The error occurs because, after deleting files a
and b
, we created c
(1500 bytes), which overwrote the memory blocks used by both a
and b
. Note that c
also reused the file structure that a
occupied.