-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.h
More file actions
337 lines (268 loc) · 7.93 KB
/
fs.h
File metadata and controls
337 lines (268 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include<sys/types.h>
#include<stdbool.h>
#include<stddef.h>
#include<stdint.h>
#include<stdlib.h>
#ifndef _FILESYS_H
#define _FILESYS_H
#define TOTAL_INODES 28
#define FILE_TABLE_SIZE 64
#define TOTAL_BUFFERS tot_buffs
#define BLOCK_SIZE 1024
#define SUPER_BLOCKS 8
#define NR_HASH 4
#define READ 0
#define WRITE 1
#define REGULAR 1
#define DIR 2
#define SPECIAL 3
#define DEVICE_NUM 4
//assuming this
#define I_MAP_SLOTS 8
#define Z_MAP_SLOTS 8
#define TOTAL_BLOCKS 100
extern int total_inodes;
#define INODES_PER_BLOCK ((BLOCK_SIZE)/sizeof(struct disk_inode))
#define DIR_ENTRIES_PER_BLOCK ((BLOCK_SIZE)/sizeof(struct dir_entry))
#define NAME_SIZE 14
typedef char buffer_block[BLOCK_SIZE];
struct status{
bool b_lock; /*0- free ,1 -locked*/
unsigned short b_count; /*user using this block*/
bool b_dirt;
bool b_uptodate;
};
//extern int inode_table[];
struct buffer_head{
char* b_data; //ptr to data //1024 bytes
unsigned short b_dev; //if ==0 means free
unsigned short b_blocknr; //block number
struct status b_status;
struct buffer_head* prev;
struct buffer_head* next;
struct buffer_head* nextfree;
struct buffer_head* prevfree;
};
struct disk_inode{
unsigned short i_uid; //owner identifier
unsigned short i_gid; //group identifier
short filetype;
unsigned short i_mode;
unsigned long i_size;
unsigned long i_mtime;
unsigned short i_nlinks;
unsigned short i_disk[9];
};
struct inode_status{
bool i_mount; //whether inode is a mount point
bool i_lock;
bool i_dirt;
bool update;
};
//this is the memory i.e in-core inode
struct ii_node{
unsigned short i_uid; //owner identifier
unsigned short i_gid; //group identifier
short filetype;
unsigned short i_mode;
unsigned long i_size;
unsigned long i_mtime;
unsigned short i_nlinks;
unsigned short i_disk[9];
//extra for in-core manipulation
unsigned long i_access_time;
unsigned long i_change_time;
unsigned short i_dev;
unsigned short i_num;
unsigned short i_refcount;
struct inode_status i_status;
struct ii_node* next;
struct ii_node* prev;
struct ii_node* nextfree;
struct ii_node* prevfree;
};
/*
File table entry
*/
struct file{
unsigned short f_mode;
unsigned short f_flags;
unsigned short f_count;
struct ii_node* f_inode;
off_t f_pos_read; //read offset
off_t f_pos_write; //write offset
};
/*
@brief: I/O parameters used in uarea
*/
struct u_io{
unsigned short mode; //READ or WRITE
int count; //number of bytes to read or write
int offset; //byte offset in file
char* address; //target address to copy data, in user memory
};
/*
@brief: user area of process
*/
struct u_area {
struct file* fd_arrays[50];
struct ii_node* curr_dir;
struct ii_node* root;
struct u_io io;
char pwd[100];
}uarea;
struct blk_node{
int array[100];
struct blk_node* next;
};
struct super{
/*
Regarding blocks
*/
int s_fs_size; //size_of_filesystem
unsigned short s_nfree_blk; //number of freeblocks
// unsigned short s_free_blk_ls[50]; //list of free disk block numbers
struct blk_node* s_free_blk_ls;
unsigned short s_next_free_blk; //index of next_free block number
/*
Regarding inode
*/
unsigned short s_nfree_inodes; //number of free inodes
unsigned short s_free_inodes_ls[TOTAL_INODES]; //list of free inodes
unsigned short s_next_free_inode; //index of next free inode on free inode list
short remembered_inode;
/*
Others
*/
unsigned short s_dev; //device number
bool s_locked;
bool s_mod; //whether super block is modified
};
/*
A directory entry consists of 16 bytes
2 bytes: inode number
14 bytes= 14 characters= filename
*/
struct dir_entry{
unsigned short inode;
char name[NAME_SIZE];
};
/*
@brief: freelist of buffers
*/
struct freehead{
struct buffer_head* head;
struct buffer_head* tail;
};
/*
@brief: buffer_cache's headers
*/
struct hash_head{
struct buffer_head* head;
struct buffer_head* tail;
};
/*
@brief: buffer cache hashqueue
*/
struct hash_table{
struct hash_head header[NR_HASH];
uint8_t size;
};
/*For buffers*/
extern struct hash_table h_table;
extern struct freehead freelist;
/*
@brief: inode pool headers
*/
struct i_header{
struct ii_node* head;
struct ii_node* tail;
};
/*
@brief: hashqueues for inodes
*/
struct inode_pool{
struct i_header i_heads[NR_HASH];
int size;
};
/*
@brief: Freelist for inodes
*/
struct inode_list{
struct ii_node* head;
struct ii_node* tail;
};
/*
For inodes
*/
extern struct inode_pool i_pool;
extern struct inode_list i_list;
extern struct ii_node* inode_table[TOTAL_INODES]; //in-core inode table
extern struct file file_table[FILE_TABLE_SIZE]; //file table
extern struct super super_block;
/*
The ufs has more than one super block
whereas SV(System V ) has a single super block
If experimenting with may super block fails use one only
*/
extern struct buffer_head* start_buffer;
extern int tot_buffers;
extern int create_block(struct ii_node* inode,int block);
extern struct ii_node* namei(const char* pathname);
extern void iput(struct ii_node* inode);
extern void init_inodes();
extern struct ii_node* iget(int dev,int num);
extern struct ii_node* get_empty_inode(void);
extern struct buffer_head* get_hash_table(int dev,int block);
extern struct buffer_head* getblk(int dev,int block);
extern void ll_rw_block(int rw,struct buffer_head* bh);
extern void brelse(struct buffer_head* buf);
extern struct buffer_head* bread(int dev,int block);
extern void bwrite(struct buffer_head* bh);
extern void init_buffers();
extern void create_entry(const char* filename,unsigned short i_num);
extern struct buffer_head* alloc(int dev);
//extern void free_blk(int num);
extern int new_block(int dev);
extern void free_block(int dev,int block);
extern struct ii_node* new_inode(int dev);
extern void ifree(int num);
extern void sync_inodes(void);
extern void mount_root(int dev);
extern struct ii_node* ialloc(int dev);
extern void display_inodes();
extern void display_buffers();
extern void display_super();
extern int bmap(struct ii_node* inode,int offset);
/*
extern tells the compiler that the function or variable is defined in another source file or translation unit
inline suggests to the compiler to inline the function to reduce call overhead.
*/
extern struct super* get_super(int dev);
extern int allocateitable(struct ii_node* ref);
extern int allocatefile(int i_index);
extern int allocatefd(int f_index);
/*
Syscalls:
*/
extern int create_file(const char* filename,unsigned short permissions);
/*
system call open
int open(filepath,int flags,int perm);
filename/path:- String which has path of file what we want to open.
The path may be relative or absolute.
flags:- This specifies the "way" bh which the file should be opened i.e for reading or writing or both
modes/perm: This specifies the "file permissions " if file is to be created with open system call
*/
extern int openfile(const char* filename,int flags,int perm);
struct filesystem{
char boot_blk[BLOCK_SIZE]; //1024 bytes
//struct super sb; //216 bytes and hence allocating 1024 bytes i.e 1 block to it
char sb[BLOCK_SIZE];
struct disk_inode dilb[50]; //36*512 = 18,432 bytes
char d_blk_area[BLOCK_SIZE*96]; //96 blocks each of 1024 bytes
};
extern struct filesystem deb;
extern char logical_blocks[BLOCK_SIZE*TOTAL_BLOCKS];
extern void copy_to_logical_blocks(char* logical_blocks,struct filesystem* fs);
#endif