PKUOS - Pintos
Pintos source browser for PKU Operating System course
filesys.c
Go to the documentation of this file.
1#include "filesys/filesys.h"
2#include <debug.h>
3#include <stdio.h>
4#include <string.h>
5#include "filesys/file.h"
6#include "filesys/free-map.h"
7#include "filesys/inode.h"
8#include "filesys/directory.h"
9
10/** Partition that contains the file system. */
12
13static void do_format (void);
14
15/** Initializes the file system module.
16 If FORMAT is true, reformats the file system. */
17void
18filesys_init (bool format)
19{
21 if (fs_device == NULL)
22 PANIC ("No file system device found, can't initialize file system.");
23
24 inode_init ();
26
27 if (format)
28 do_format ();
29
31}
32
33/** Shuts down the file system module, writing any unwritten data
34 to disk. */
35void
37{
39}
40
41/** Creates a file named NAME with the given INITIAL_SIZE.
42 Returns true if successful, false otherwise.
43 Fails if a file named NAME already exists,
44 or if internal memory allocation fails. */
45bool
46filesys_create (const char *name, off_t initial_size)
47{
48 block_sector_t inode_sector = 0;
49 struct dir *dir = dir_open_root ();
50 bool success = (dir != NULL
51 && free_map_allocate (1, &inode_sector)
52 && inode_create (inode_sector, initial_size)
53 && dir_add (dir, name, inode_sector));
54 if (!success && inode_sector != 0)
55 free_map_release (inode_sector, 1);
56 dir_close (dir);
57
58 return success;
59}
60
61/** Opens the file with the given NAME.
62 Returns the new file if successful or a null pointer
63 otherwise.
64 Fails if no file named NAME exists,
65 or if an internal memory allocation fails. */
66struct file *
67filesys_open (const char *name)
68{
69 struct dir *dir = dir_open_root ();
70 struct inode *inode = NULL;
71
72 if (dir != NULL)
74 dir_close (dir);
75
76 return file_open (inode);
77}
78
79/** Deletes the file named NAME.
80 Returns true if successful, false on failure.
81 Fails if no file named NAME exists,
82 or if an internal memory allocation fails. */
83bool
84filesys_remove (const char *name)
85{
86 struct dir *dir = dir_open_root ();
87 bool success = dir != NULL && dir_remove (dir, name);
88 dir_close (dir);
89
90 return success;
91}
92
93/** Formats the file system. */
94static void
96{
97 printf ("Formatting file system...");
99 if (!dir_create (ROOT_DIR_SECTOR, 16))
100 PANIC ("root directory creation failed");
102 printf ("done.\n");
103}
struct block * block_get_role(enum block_type role)
Returns the block device fulfilling the given ROLE, or a null pointer if no block device has been ass...
Definition: block.c:54
uint32_t block_sector_t
Index of a block device sector.
Definition: block.h:15
@ BLOCK_FILESYS
File system.
Definition: block.h:30
#define PANIC(...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
Definition: debug.h:14
bool dir_lookup(const struct dir *dir, const char *name, struct inode **inode)
Searches DIR for a file with the given NAME and returns true if one exists, false otherwise.
Definition: directory.c:119
bool dir_add(struct dir *dir, const char *name, block_sector_t inode_sector)
Adds a file named NAME to DIR, which must not already contain a file by that name.
Definition: directory.c:142
void dir_close(struct dir *dir)
Destroys DIR and frees associated resources.
Definition: directory.c:70
bool dir_create(block_sector_t sector, size_t entry_cnt)
Creates a directory with space for ENTRY_CNT entries in the given SECTOR.
Definition: directory.c:27
bool dir_remove(struct dir *dir, const char *name)
Removes any entry for NAME in DIR.
Definition: directory.c:185
struct dir * dir_open_root(void)
Opens the root directory and returns a directory for it.
Definition: directory.c:55
struct file * file_open(struct inode *inode)
Opens a file for the given INODE, of which it takes ownership, and returns the new file.
Definition: file.c:18
static void do_format(void)
Formats the file system.
Definition: filesys.c:95
struct block * fs_device
Partition that contains the file system.
Definition: filesys.c:11
struct file * filesys_open(const char *name)
Opens the file with the given NAME.
Definition: filesys.c:67
bool filesys_remove(const char *name)
Deletes the file named NAME.
Definition: filesys.c:84
void filesys_done(void)
Shuts down the file system module, writing any unwritten data to disk.
Definition: filesys.c:36
void filesys_init(bool format)
Initializes the file system module.
Definition: filesys.c:18
bool filesys_create(const char *name, off_t initial_size)
Creates a file named NAME with the given INITIAL_SIZE.
Definition: filesys.c:46
#define ROOT_DIR_SECTOR
Root directory file inode sector.
Definition: filesys.h:9
void free_map_init(void)
Initializes the free map.
Definition: free-map.c:13
void free_map_release(block_sector_t sector, size_t cnt)
Makes CNT sectors starting at SECTOR available for use.
Definition: free-map.c:45
void free_map_open(void)
Opens the free map file and reads it from disk.
Definition: free-map.c:54
void free_map_create(void)
Creates a new free map file on disk and writes the free map to it.
Definition: free-map.c:73
bool free_map_allocate(size_t cnt, block_sector_t *sectorp)
Allocates CNT consecutive sectors from the free map and stores the first into *SECTORP.
Definition: free-map.c:28
void free_map_close(void)
Writes the free map to disk and closes the free map file.
Definition: free-map.c:65
bool inode_create(block_sector_t sector, off_t length)
Initializes an inode with LENGTH bytes of data and writes the new inode to sector SECTOR on the file ...
Definition: inode.c:73
void inode_init(void)
Initializes the inode module.
Definition: inode.c:62
char * name[]
Definition: insult.c:47
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
int32_t off_t
An offset within a file.
Definition: off_t.h:9
#define NULL
Definition: stddef.h:4
A block device.
Definition: block.c:10
A directory.
Definition: directory.c:11
An open file.
Definition: file.c:8
In-memory inode.
Definition: inode.c:33