-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.c
699 lines (582 loc) · 19.2 KB
/
fs.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include "fs.h"
#include "utils.h"
#include "StringProc.h"
/* Build a new filesystem image in =fname (the file =fname should be present
* in the OS's filesystem). The new filesystem should use =blocksize as its
* block size; the number of blocks in the filesystem will be automatically
* computed from the file size. The filesystem will be initialized with an
* empty root directory. This function returns NULL on error and sets errno
* to the appropriate error code. If the block size is smaller than
* MIN_BLOCK_SIZE bytes, then the format fails and the function sets errno to
* EINVAL. If there is insufficient space to store MIN_BLOCK_COUNT blocks in
* =fname, then the function fails and sets errno to ENOSPC. */
struct superblock * fs_format(const char *fname, uint64_t blocksize) {
struct superblock *sb;
struct inode *inode;
struct nodeinfo *info;
struct freepage *fp;
int size;
uint64_t i;
if (blocksize < MIN_BLOCK_SIZE) {
errno = EINVAL;
return NULL;
}
sb = (struct superblock*) calloc(1, blocksize);
inode = (struct inode*) calloc(1, blocksize);
info = (struct nodeinfo*) calloc(1, blocksize);
fp = (struct freepage*) calloc(1, blocksize);
size = getFileSize(fname);
//sb setup
sb->fd = open(fname, O_RDWR);
sb->magic = 0xdcc605f5;
sb->root = 1;
sb->blksz = blocksize;
sb->blks = size / blocksize;
sb->freeblks = sb->blks - 4;
sb->freelist = 3;
if (sb->blks < MIN_BLOCK_COUNT) {
errno = ENOSPC;
close(sb->fd);
free(fp);
free(inode);
free(info);
free(sb);
return NULL;
}
//inode setup
inode->parent = 1; //root points to itself
inode->mode = IMDIR;
inode->meta = 2; // metadata pointer
inode->next = 0;
// metadata setup
info->size = 0; //there's no ent in this dir
info->name[0] = '/'; // root name
info->name[1] = '\0'; //string ending escape
// file writeup
lseek(sb->fd, 0, SEEK_SET);
write(sb->fd, sb, blocksize);
assert(sb->magic == 0xdcc605f5);
/////////////////////////////
lseek(sb->fd, sb->root * blocksize, SEEK_SET);
write(sb->fd, inode, blocksize);
/////////////////////////////
lseek(sb->fd, inode->meta * blocksize, SEEK_SET);
write(sb->fd, info, blocksize);
//free page setup
for (i = sb->freelist; i < sb->blks; i++) {
if (i == sb->freelist) {
//first freeBlock
fp->next = i + 1;
fp->count = 0;
lseek(sb->fd, i * blocksize, SEEK_SET);
write(sb->fd, fp, blocksize);
lseek(sb->fd, i * blocksize, SEEK_SET);
read(sb->fd, fp, blocksize);
assert(fp->next == i + 1);
} else if (i + 1 >= sb->blks) {
//last free block
fp->next = 0;
fp->count = 1;
fp->links[0] = i - 1; //link to previous free block(doubly linked list)
lseek(sb->fd, i * blocksize, SEEK_SET);
write(sb->fd, fp, blocksize);
lseek(sb->fd, i * blocksize, SEEK_SET);
read(sb->fd, fp, blocksize);
assert(fp->next == 0);
} else {
fp->next = i + 1;
fp->count = 1;
fp->links[0] = i - 1; //link to previous free block(doubly linked list)
lseek(sb->fd, i * blocksize, SEEK_SET);
write(sb->fd, fp, blocksize);
lseek(sb->fd, i * blocksize, SEEK_SET);
read(sb->fd, fp, blocksize);
assert(fp->next == i + 1);
}
}
free(fp);
free(inode);
free(info);
return sb;
}
struct superblock * fs_open(const char *fname) {
int fd = open(fname, O_RDWR);
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
errno = EBUSY;
return NULL;
}
struct superblock* sb = (struct superblock*) malloc(sizeof (struct superblock));
read(fd, sb, sizeof (struct superblock));
/*
* If =fname does not contain a
* 0xdcc605fs, then errno is set to EBADF.
*/
if (sb->magic != 0xdcc605f5) {
errno = EBADF;
close(fd);
free(sb);
return NULL;
}
int blocksz = sb->blksz;
free(sb);
sb = (struct superblock*) malloc(blocksz);
lseek(fd, 0, SEEK_SET);
read(fd, sb, blocksz);
return sb;
}
int fs_close(struct superblock *sb) {
if (sb == NULL) {
return -1;
}
seek_write(sb, 0, sb);
if (flock(sb->fd, LOCK_UN | LOCK_NB) != 0) {
errno = EBADF;
return -1;
}
close(sb->fd);
free(sb);
return 0;
}
uint64_t fs_get_block(struct superblock *sb) {
if (sb->freeblks == 0) {
//report Error
return 0;
}
uint64_t freeList = sb->freelist;
struct freepage *fp = (struct freepage *) malloc(sb->blksz);
struct freepage *fp_prev = (struct freepage *) malloc(sb->blksz);
struct freepage *fp_next = (struct freepage *) malloc(sb->blksz);
seek_read(sb, freeList, fp);
if (fp->count != 0) {
//update previous pointers
seek_read(sb, fp->links[0], fp_prev);
fp_prev->next = fp->next;
seek_write(sb, fp->links[0], fp_prev);
}
if (fp->next != 0) {
//update next pointers
seek_read(sb, fp->next, fp_next);
fp_next->links[0] = fp->links[0];
seek_write(sb, fp->next, fp_next);
}
sb->freelist = fp->next;
sb->freeblks--;
seek_write(sb, 0, sb);
free(fp_prev);
free(fp);
free(fp_next);
return freeList;
}
int fs_put_block(struct superblock *sb, uint64_t block) {
if (sb->freeblks != 0) {
uint64_t freeList = sb->freelist;
struct freepage *fp = NULL, *fp_next = NULL;
fp = (struct freepage *) malloc(sb->blksz);
fp_next = (struct freepage *) malloc(sb->blksz);
seek_read(sb, freeList, fp_next);
fp->next = freeList;
fp->count = 0;
seek_write(sb, block, fp);
fp_next->count = 1;
fp_next->links[0] = block;
seek_write(sb, freeList, fp_next);
free(fp);
free(fp_next);
} else {
struct freepage *fp = NULL;
fp = (struct freepage *) malloc(sb->blksz);
fp->next = 0;
fp->count = 0;
seek_write(sb, block, fp);
free(fp);
}
sb->freelist = block;
sb->freeblks++;
seek_write(sb, 0, sb);
return 0;
}
int fs_write_file(struct superblock *sb, const char *fname, char *buf, size_t cnt) {
const uint64_t blocksNeeded = MAX(1, cnt / sb->blksz);
if (blocksNeeded > sb->freeblks) {
errno = ENOSPC;
return -1;
}
int blocksUsed = 0;
if (strlen(fname) + 1 > getFileNameMaxLen(sb)) {
errno = ENAMETOOLONG;
return -1;
}
if (existsFile(sb, fname)) {
errno = EEXIST;
return -1;
}
int len = 0, exists = 0;
char** fileParts = getFileParts(fname, &len);
uint64_t dirBlock = findFile(sb, fname, &exists);
char* dirName = NULL;
struct inode* dirNode, *node;
struct nodeinfo* meta = (struct nodeinfo*) calloc(1, sb->blksz);
initNode(&dirNode, sb->blksz);
initNode(&node, sb->blksz);
seek_read(sb, dirBlock, dirNode);
seek_read(sb, dirNode->meta, meta);
dirName = calloc(1, sizeof (char)* (strlen(meta->name) + 1));
strcpy(dirName, meta->name);
if (strcmp(fileParts[MAX(0, len - 2)], dirName) != 0) {
errno = EBADF;
free(dirName);
free(node);
free(meta);
free(dirNode);
return -1;
}
uint64_t fileBlock = fs_get_block(sb);
insertInBlock(sb, dirBlock, fileBlock);
uint64_t blocksList[blocksNeeded];
///properly write the file
while (blocksUsed < blocksNeeded) {
uint64_t block = fs_get_block(sb);
blocksList[blocksUsed] = block;
char* temp = calloc(1, sb->blksz);
char* blockContent = buf + sb->blksz * (blocksUsed);
strcpy(temp, blockContent);
blocksUsed++;
seek_write(sb, block, temp);
free(temp);
}
strcpy(meta->name, fileParts[len - 1]);
meta->size = cnt;
meta->reserved[0] = 0;
node->meta = fs_get_block(sb);
node->mode = IMREG;
node->parent = dirBlock;
seek_write(sb, node->meta, meta);
uint64_t nodeBlock = fileBlock;
blocksUsed = 0;
while (blocksUsed < blocksNeeded) {
int i = 0;
int linksLen = getLinksMaxLen(sb);
int N = MIN(linksLen, blocksNeeded);
while (i < N) {
node->links[i] = blocksList[i];
i++;
blocksUsed++;
}
if (blocksUsed < blocksNeeded) {
node->next = fs_get_block(sb);
seek_write(sb, nodeBlock, node);
nodeBlock = node->next;
node->next = 0;
node->parent = fileBlock;
node->mode = IMCHILD | IMREG;
node->meta = fs_get_block(sb);
strcpy(meta->name, fileParts[len - 1]);
meta->size = cnt;
seek_write(sb, node->meta, meta);
}
}
seek_write(sb, nodeBlock, node);
free(meta);
free(node);
freeFileParts(&fileParts, len);
free(dirNode);
free(dirName);
return 0;
}
ssize_t fs_read_file(struct superblock *sb, const char *fname, char *buf,
size_t bufsz) {
if (existsFile(sb, fname) == FALSE) {
errno = ENOENT;
return -1;
}
int len = 0, exists = 0;
char** fileParts = getFileParts(fname, &len);
uint64_t fileBlock = findFile(sb, fname, &exists);
struct inode* node;
initNode(&node, sb->blksz);
struct nodeinfo* meta = (struct nodeinfo*) calloc(1, sb->blksz);
seek_read(sb, fileBlock, node);
seek_read(sb, node->meta, meta);
assert(strcmp(meta->name, fileParts[len - 1]) == 0);
size_t size = MIN(meta->size, bufsz);
size = MAX(sb->blksz, size);
size_t read_blocks = 0;
char* buf_p = (char*) calloc(1, size);
char* p = buf_p;
do {
int i = 0;
while (node->links[i] != 0) {
seek_read(sb, node->links[i++], p);
p = p + sb->blksz;
read_blocks++;
}
if (node->next == 0)break;
seek_read(sb, node->next, node);
} while (node->next != 0);
strcpy(buf, buf_p);
freeFileParts(&fileParts, len);
free(meta);
free(node);
free(buf_p);
return size;
}
int fs_delete_file(struct superblock *sb, const char *fname) { //o proprio nome ja diz.
int i = 0, found, maxLink, ultimo;
uint64_t fileBlock, fileLinkBlk, lastBlock, folderBlock; //numero do bloco e do bloco do link no diretorio que tem ele
struct inode *file;
struct inode *folder;
struct inode *aux;
struct nodeinfo *folderInfo;
maxLink = getLinksMaxLen(sb);
file = (struct inode *) malloc(sb->blksz);
folder = (struct inode *) malloc(sb->blksz);
aux = (struct inode *) malloc(sb->blksz);
folderInfo = (struct nodeinfo *) malloc(sb->blksz);
fileBlock = findFile(sb, fname, &found);
if (found == 0) { //arquivo nao existe
errno = ENOENT;
return -1;
}
seek_read(sb, fileBlock, file); //pegando inode do arquivo
if (file->mode == IMDIR) { //se for diretorio
errno = EISDIR;
return -1;
}
folderBlock = file->parent;
fileLinkBlk = folderBlock;
seek_read(sb, folderBlock, folder); //diretorio onde esta o arquivo
seek_read(sb, folder->meta, folderInfo);
//removendo o arquivo e blocos associados
fs_put_block(sb, file->meta); //liberando bloco nodeinfo
while ((i < maxLink) && (file->links[i] != 0)) {
//if (i == maxLink)
fs_put_block(sb, file->links[i]);
file->links[i] = 0; //marcando que nao tem mais bloco neste link.
i++;
if (i == maxLink && file->next != 0) {
seek_read(sb, file->next, file);
fs_put_block(sb, file->meta); //apagando bloco anterior depois de ir pro próximo.
i = 0;
}
}
//removendo na pasta tambem;
ultimo = (folder->links[getLinksLen(folder) - 1] == fileBlock);
if (ultimo) {
folder->links[getLinksLen(folder) - 1] = 0;
//folderInfo->size--;
} else {
i = 0;
while ((folder->links[i % maxLink] != fileBlock) && i < folderInfo->size) {
i++;
if ((i % maxLink == 0) && (folder->next != 0)) {
fileLinkBlk = folder->next; //guardando bloco que pode conter link pro arquivo. Saindo do while sera ele.
seek_read(sb, folder->next, folder);
}
}
lastBlock = getNodeLastLinkBlock(sb, folderBlock);
if (lastBlock == fileLinkBlk) {
folder->links[i % maxLink] = folder->links[getLinksLen(folder) - 1];
folder->links[getLinksLen(folder) - 1] = 0;
} else {
seek_read(sb, lastBlock, aux);
folder->links[i % maxLink] = aux->links[getLinksLen(aux) - 1]; //copiando ultimo link pra posicao q indicava bloco do arquivo removido
aux->links[getLinksLen(aux) - 1] = 0;
seek_write(sb, lastBlock, aux); //Devo escrever o bloco alterado de volta no disco, certo?
}
}
folderInfo->size--;
seek_write(sb, fileLinkBlk, folder); //escrevendo o que contem o link pro arquivo (foi alterado ali em cima)
seek_read(sb, folderBlock, folder); //voltando pro inode principal da pasta
seek_write(sb, folder->meta, folderInfo); //pra escrever o folderinfo no bloco onde ele estava.
free(file);
free(folder);
free(folderInfo);
free(aux);
return 0;
}
char * fs_list_dir(struct superblock *sb, const char *dname) { //lista tudo o que tem dentro de dname.
int i = 0, found, size, isDir;
uint64_t dirBlock;
int maxLink;
char *names;
struct inode *dir;
struct inode *aux;
struct nodeinfo *auxinfo;
maxLink = getLinksMaxLen(sb);
dir = (struct inode *) malloc(sb->blksz);
aux = (struct inode *) malloc(sb->blksz);
auxinfo = (struct nodeinfo *) malloc(sb->blksz);
names = (char *) malloc(sizeof (char));
strcpy(names, ""); //comecara 'nulo'
dirBlock = findFile(sb, dname, &found); //vasculhandodo o diretorio
if (found == 0) { //o dretorio nao existe
errno = ENOENT;
return NULL;
}
seek_read(sb, dirBlock, dir); //pegando inode do diretorio
if (dir->mode != IMDIR) { //se nao for diretorio
errno = ENOTDIR;
return NULL;
}
while (i < maxLink && dir->links[i] != 0) { //buscar em todos os links do bloco
seek_read(sb, dir->links[i], aux); //objeto de 'links', sempre sera inode IMREG ou IMDIR
seek_read(sb, aux->meta, auxinfo); //nodeinfo dele
if (aux->mode == IMDIR)
isDir = 1;
else
isDir = 0;
size = strlen(auxinfo->name) + 1 + isDir; //pegando tamanho do nome mais um espaco e mais uma barra (ou nao)
char* temp = (char *) malloc((size + strlen(names) + 1) * sizeof (char));
strcpy(temp, names);
strcat(temp, auxinfo->name);
free(names);
names = temp;
if (isDir)
strcat(names, "/");
strcat(names, " "); //concatenando espaco de separacao de nomes
i++;
if (i == maxLink && (dir->next != 0)) {
i = 0; //tem next? reseta contagem
seek_read(sb, dir->next, dir); //indo pro proximo inode, se houver.
}
}
free(dir);
free(aux);
free(auxinfo);
printf("%s\n", names);
return names;
}
/*
* Leobas:
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include "fs.h"
#include "utils.h"
#include "StringProc.h"
#define SB_SIZE 52
#define invalid -1
#define success 0
void init_folder_struct(struct inode* folder, uint64_t folder_block, uint64_t nodeinfo_block) {
/* inode properties for a folder */
/* =mode indicates that the inode is a directory */
folder->mode = IMDIR;
/* =parent points to the block that contains this inode */
folder->parent = folder_block;
/* start the =next value with 0 */
folder->next = 0;
/* =meta value must point to the nodeinfo block */
folder->meta = nodeinfo_block;
}
int init_nodeinfo_struct(struct superblock* sb, const char* dname, struct nodeinfo* n_info, uint64_t nodeinfo_block) {
/* nodeinfo properties */
/* =size contains the number of files in the directory*/
n_info->size = 0;
/* =name contains the name of the new folder
* i.e. /home/user/dir1/folder
* =name is equals to folder
*/
int size = 0;
char** dname_array = getFileParts(dname, &size);
int max_filename_size = getFileNameMaxLen(sb);
int foldername_size = strlen(dname_array[size - 1]);
if (max_filename_size < foldername_size) {
return invalid;
}
strcpy(n_info->name, dname_array[size - 1]);
freeFileParts(&dname_array, size);
return success;
}
int checkfather_path(struct superblock *sb, const char *dname, struct inode* father) {
struct nodeinfo* info_pai = (struct nodeinfo*) malloc(sb->blksz);
seek_read(sb, father->meta, info_pai);
int size = 0;
char **dname_array = getFileParts(dname, &size);
int valid = 0;
if (size >= 2)
valid = strcmp(dname_array[size - 2], info_pai->name);
freeFileParts(&dname_array, size);
free(info_pai);
info_pai = NULL;
if (valid == 0) {
return success;
} else {
return invalid;
}
}
int fs_mkdir(struct superblock *sb, const char *dname) {
if (!sb->freeblks) {
// disk is full
errno = ENOSPC;
return invalid;
}
int exists = 0;
uint64_t fileBlock = findFile(sb, dname, &exists);
if (exists) {
// dir already exist;
errno = EEXIST;
return invalid;
}
struct inode *father = (struct inode*) malloc(sb->blksz);
struct inode *folder = (struct inode*) malloc(sb->blksz);
struct nodeinfo *n_info = (struct nodeinfo*) malloc(sb->blksz);
uint64_t folder_block = fs_get_block(sb);
uint64_t nodeinfo_block = fs_get_block(sb);
init_folder_struct(folder, folder_block, nodeinfo_block);
int status = init_nodeinfo_struct(sb, dname, n_info, nodeinfo_block);
if (status == invalid) {
free(father);
father = NULL;
free(folder);
folder = NULL;
free(n_info);
n_info = NULL;
errno = ENAMETOOLONG;
return invalid;
}
/* Read father inode stored in file */
seek_read(sb, fileBlock, father);
status = checkfather_path(sb, dname, father);
if (status == invalid) {
free(father);
father = NULL;
free(folder);
folder = NULL;
free(n_info);
n_info = NULL;
errno = ENOENT;
return invalid;
}
/* Ok, proceed */
if (!exists) {
insertInBlock(sb, fileBlock, folder_block);
/* store both inodes related to the folder that
has been just created
*/
seek_write(sb, folder_block, folder);
seek_write(sb, nodeinfo_block, n_info);
}
free(father);
father = NULL;
free(folder);
folder = NULL;
free(n_info);
n_info = NULL;
return success;
}