-
Notifications
You must be signed in to change notification settings - Fork 1
/
inode.c
434 lines (334 loc) · 10.5 KB
/
inode.c
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Operating System Lab
* Lab4 (File System in Userspace)
* Copyright (C) 2017 Juhyung Son <[email protected]>
* First Writing: 30/12/2016
*
* inode.c :
* - inode related operatios.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/
#include <fuse.h>
#include <ulockmgr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/file.h>
#include "include/lab4_fs_types.h"
int do_path_check(const char *path)
{
char *basename_path;
basename_path = strrchr(path, '/');
if (basename_path == NULL) {
return LAB4_ERROR;
}
// basename_path++;
return LAB4_SUCCESS;
}
static inline u16 old_encode_dev(dev_t dev)
{
return (major(dev) << 8) | minor(dev);
}
static inline u32 new_encode_dev(dev_t dev)
{
unsigned major = major(dev);
unsigned minor = minor(dev);
return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
}
static inline int old_valid_dev(dev_t dev)
{
return major(dev) < 256 && minor(dev) < 256;
}
static inline dev_t old_decode_dev(u16 val)
{
return makedev((val >> 8) & 255, val & 255);
}
static inline dev_t new_decode_dev(u32 dev)
{
unsigned major = (dev & 0xfff00) >> 8;
unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
return makedev(major, minor);
}
int do_fill_inode(IOM *iom, INODE *inode, struct stat *stbuf){
stbuf->st_mode = inode->i_mode;
stbuf->st_nlink = inode->i_links_count;
stbuf->st_size = inode->i_size;
stbuf->st_atime = inode->i_atime;
stbuf->st_mtime = inode->i_mtime;
stbuf->st_ctime = inode->i_ctime;
stbuf->st_gid = inode->i_gid;
stbuf->st_uid = inode->i_uid;
if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
stbuf->st_rdev = old_decode_dev(inode->i_blocks[0]);
} else {
stbuf->st_rdev = new_decode_dev(inode->i_blocks[1]);
}
return LAB4_SUCCESS;
}
INODE* do_read_inode(IOM *iom, inode_t ino){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
INODE *inode = NULL, *tmp_inode = NULL;
int blocknr;
int offset;
char *buf_itble;
if (ino < LAB4_ROOT_INO)
return NULL;
inode = (struct lab4_inode*)malloc(LAB4_INODE_ENTRY_SIZE);
if(!inode)
return NULL;
memset(inode, 0x0, LAB4_INODE_ENTRY_SIZE);
buf_itble = iom->iom_buf_itble;
tmp_inode = (INODE*)buf_itble;
memcpy(inode, &tmp_inode[ino], LAB4_INODE_ENTRY_SIZE);
return inode;
}
inode_t get_root_ino(struct lab4_sb_info *sbi){
return sbi->root_ino;
}
int lab4_read_inode(IOM *iom, const char *path, INODE **inode){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_dir_entry dir_entry;
INODE *tmp_inode=NULL;
inode_t cur_ino;
char tmp_path[256];
char* filename;
int res,isdelete=0;
cur_ino = get_root_ino(sbi);
memset(tmp_path, 0x0, 256);
if(strcmp(path,"/")){
strcpy(tmp_path,path);
filename = strtok(tmp_path,"/");
while(filename != NULL)
{
res = lab4_lookup(iom, cur_ino, &dir_entry,filename,&isdelete);
if(res == LAB4_ERROR)
return LAB4_ERROR;
if(res == -ENOENT)
return -ENOENT;
cur_ino = dir_entry.d_ino;
filename = strtok(NULL,"/");
if(filename == NULL)
break;
cur_ino = dir_entry.d_ino;
memset(&dir_entry, 0x0, LAB4_DIR_ENTRY_SIZE);
}
}
*inode = do_read_inode(iom,cur_ino);
if(!inode)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int update_sb(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pwrite(iom->dev_fd, iom->sb ,sbi->cluster_size,sbi->sb_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int update_ibmap(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pwrite(iom->dev_fd, iom->iom_buf_ibmap ,sbi->cluster_size, sbi->ibitmap_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int update_dbmap(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pwrite(iom->dev_fd, iom->iom_buf_dbmap ,sbi->cluster_size, sbi->dbitmap_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int update_itble(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pwrite(iom->dev_fd, iom->iom_buf_itble ,sbi->cluster_size, sbi->itable_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int restore_sb(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pread(iom->dev_fd, iom->sb,sbi->cluster_size,sbi->sb_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
free(sbi);
lab4_fill_super(iom);
return LAB4_SUCCESS;
}
int restore_ibmap(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pread(iom->dev_fd, iom->iom_buf_ibmap ,sbi->cluster_size, sbi->ibitmap_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int restore_dbmap(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pread(iom->dev_fd, iom->iom_buf_dbmap ,sbi->cluster_size, sbi->dbitmap_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
int restore_itble(IOM *iom){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res;
res = pread(iom->dev_fd, iom->iom_buf_itble ,sbi->cluster_size, sbi->itable_start);
if(res == LAB4_ERROR)
return LAB4_ERROR;
return LAB4_SUCCESS;
}
void inc_free_inodes(IOM *iom){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
sbi->sb_free_inodes++;
sb->sb_free_inodes++;
}
void dec_free_inodes(IOM *iom){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
sbi->sb_free_inodes--;
sb->sb_free_inodes--;
}
void inc_free_blocks(IOM *iom){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
sbi->sb_free_blocks++;
sb->sb_free_blocks++;
}
void dec_free_blocks(IOM *iom){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
sbi->sb_free_blocks--;
sb->sb_free_blocks--;
}
INODE* alloc_new_inode(IOM *iom){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_inode *new_inode;
inode_t last_allocated_ino = sbi->last_allocated_ino;
int res;
inode_t new_ino;
res = scan_allocate_ibmap(iom,last_allocated_ino,&new_ino);
if(res == LAB4_ERROR)
return NULL;
new_inode = (INODE*)malloc(LAB4_INODE_ENTRY_SIZE);
memset(new_inode, 0x0, LAB4_INODE_ENTRY_SIZE);
new_inode->i_ino = new_ino;
new_inode->i_deleted = 0;
new_inode->i_version++;
sb->sb_last_allocated_ino = new_ino+1;
sbi->last_allocated_ino = new_ino+1;
dec_free_inodes(iom);
return new_inode;
}
int write_inode_to_itble(IOM *iom, INODE *new_inode){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_inode *tmp_inode;
int res, new_ino_pos;
char *buf_itble;
buf_itble = iom->iom_buf_itble;
tmp_inode = (struct lab4_inode*)buf_itble;
new_ino_pos = new_inode->i_ino * LAB4_INODE_ENTRY_SIZE;
if(new_ino_pos > sbi->cluster_size)
return LAB4_ERROR;
memcpy(&tmp_inode[new_inode->i_ino], new_inode, LAB4_INODE_ENTRY_SIZE);
return LAB4_SUCCESS;
}
int delete_inode_from_itble(IOM *iom, INODE *del_inode){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_inode *tmp_inode;
inode_t del_ino_pos;
int res;
char *buf_itble, *buf_zero;
buf_itble = iom->iom_buf_itble;
tmp_inode = (struct lab4_inode*)buf_itble;
del_ino_pos = del_inode->i_ino * LAB4_INODE_ENTRY_SIZE;
if(del_ino_pos > sbi->cluster_size)
return LAB4_ERROR;
memset(&tmp_inode[del_inode->i_ino], 0x0, LAB4_INODE_ENTRY_SIZE);
return LAB4_SUCCESS;
}
void delete_block_inbmap(IOM *iom, INODE *inode){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int blocknr_inbmap;
long del_blocknr = inode->i_blocks[0];
blocknr_inbmap = (del_blocknr - sbi->darea_start)/ sbi->cluster_size;
scan_delete_dbmap(iom, blocknr_inbmap);
inc_free_blocks(iom);
}
int restore_data(IOM *iom,char *buf, int size, long blocknr){
pwrite(iom->dev_fd, buf, size,blocknr);
}
int delete_inode(IOM *iom, INODE *del_inode){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int res = LAB4_ERROR, blocknr_inbmap;
char *buf_del_block;
long del_blocknr;
scan_delete_ibmap(iom, del_inode->i_ino);
res = delete_inode_from_itble(iom, del_inode);
if(res == LAB4_ERROR){
return LAB4_ERROR;
}
inc_free_inodes(iom);
return LAB4_SUCCESS;
}
void inc_link(IOM *iom, inode_t par_ino){
char *buf_itble = iom->iom_buf_itble;
INODE *tmp_inode = (INODE*)buf_itble;
tmp_inode[par_ino].i_links_count++;
}
void dec_link(IOM *iom, inode_t par_ino){
char *buf_itble = iom->iom_buf_itble;
INODE *tmp_inode = (INODE*)buf_itble;
tmp_inode[par_ino].i_links_count--;
}
int do_utimens(IOM *iom, const char *path, const struct timespec ts[2])
{
struct lab4_dir_entry file_entry;
struct lab4_inode *inode;
char f_name[FNAME_SIZE] = {0,};
char d_name[FNAME_SIZE] = {0,};
int res, isdelete=0;
if (strcmp(path, "/") == 0) {
res = -1;
} else {
res = do_path_check(path);
if(res == LAB4_ERROR)
return LAB4_ERROR;
get_name_from_path(path, f_name, d_name);
res = lab4_read_inode(iom, path, &inode);
if(res == LAB4_ERROR)
return LAB4_ERROR;
else if(res == -ENOENT)
return -ENOENT;
/* set new access time */
inode->i_atime = ts[0].tv_sec;
/* sec new modification time */
inode->i_mtime = ts[1].tv_sec;
}
return LAB4_SUCCESS;
}