CS318 - Pintos
Pintos source browser for JHU CS318 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. */
11 struct block *fs_device;
12 
13 static void do_format (void);
14 
15 /** Initializes the file system module.
16  If FORMAT is true, reformats the file system. */
17 void
18 filesys_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 ();
25  free_map_init ();
26 
27  if (format)
28  do_format ();
29 
30  free_map_open ();
31 }
32 
33 /** Shuts down the file system module, writing any unwritten data
34  to disk. */
35 void
36 filesys_done (void)
37 {
38  free_map_close ();
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. */
45 bool
46 filesys_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. */
66 struct file *
67 filesys_open (const char *name)
68 {
69  struct dir *dir = dir_open_root ();
70  struct inode *inode = NULL;
71 
72  if (dir != NULL)
73  dir_lookup (dir, name, &inode);
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. */
83 bool
84 filesys_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. */
94 static void
95 do_format (void)
96 {
97  printf ("Formatting file system...");
98  free_map_create ();
99  if (!dir_create (ROOT_DIR_SECTOR, 16))
100  PANIC ("root directory creation failed");
101  free_map_close ();
102  printf ("done.\n");
103 }
name
char * name[]
Definition: insult.c:47
free_map_create
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
filesys_init
void filesys_init(bool format)
Initializes the file system module.
Definition: filesys.c:18
filesys_open
struct file * filesys_open(const char *name)
Opens the file with the given NAME.
Definition: filesys.c:67
NULL
#define NULL
Definition: stddef.h:4
free_map_allocate
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
filesys.h
string.h
dir_open_root
struct dir * dir_open_root(void)
Opens the root directory and returns a directory for it.
Definition: directory.c:55
file
An open file.
Definition: file.c:7
dir_lookup
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
off_t
int32_t off_t
An offset within a file.
Definition: off_t.h:9
block_get_role
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
block_sector_t
uint32_t block_sector_t
Index of a block device sector.
Definition: block.h:15
filesys_remove
bool filesys_remove(const char *name)
Deletes the file named NAME.
Definition: filesys.c:84
do_format
static void do_format(void)
Formats the file system.
Definition: filesys.c:95
PANIC
#define PANIC(...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
Definition: debug.h:14
free_map_init
void free_map_init(void)
Initializes the free map.
Definition: free-map.c:13
printf
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
directory.h
ROOT_DIR_SECTOR
#define ROOT_DIR_SECTOR
Root directory file inode sector.
Definition: filesys.h:9
inode
In-memory inode.
Definition: inode.c:32
free-map.h
dir_create
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
file_open
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
inode_create
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
filesys_done
void filesys_done(void)
Shuts down the file system module, writing any unwritten data to disk.
Definition: filesys.c:36
filesys_create
bool filesys_create(const char *name, off_t initial_size)
Creates a file named NAME with the given INITIAL_SIZE.
Definition: filesys.c:46
block
A block device.
Definition: block.c:9
dir_add
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
free_map_release
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
free_map_close
void free_map_close(void)
Writes the free map to disk and closes the free map file.
Definition: free-map.c:65
inode_init
void inode_init(void)
Initializes the inode module.
Definition: inode.c:62
fs_device
struct block * fs_device
Partition that contains the file system.
Definition: filesys.c:11
dir_remove
bool dir_remove(struct dir *dir, const char *name)
Removes any entry for NAME in DIR.
Definition: directory.c:185
file.h
BLOCK_FILESYS
File system.
Definition: block.h:30
free_map_open
void free_map_open(void)
Opens the free map file and reads it from disk.
Definition: free-map.c:54
dir
A directory.
Definition: directory.c:10
debug.h
dir_close
void dir_close(struct dir *dir)
Destroys DIR and frees associated resources.
Definition: directory.c:70
inode.h