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
|
#include "medianin.h"
#include <malloc.h>
#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include "defs.h"
#include "dir.h"
#include "file.h"
#include "inode.h"
#include "block_walker.h"
/* Invariante de representación */
#define INVREP(self) ((self)!=NULL && (self)->disk_mem!=NULL && 0<(self)->disk_fd)
/* El FS medianin es el archivo con el disco mapeado en memoria y su fd */
struct PACKED medianin_s
{
char *disk_mem; /* puntero al inicio del disco mapeado en memoria */
int disk_fd; /* fd del openfile con el disco */
time_t mount_time; /* tiempo de montaje: a,m,ctime del rootdir */
};
char *media_block=NULL;
/* Auxiliares */
uint16_t free_blocks_count(const char *disk_mem);
uint16_t free_inodes_count(const char *disk_mem);
uint16_t free_blocks_count(const char *disk_mem)
{
uint16_t i, s=0;
char *buf=(char *)disk_mem+FBB_OFFSET;
/*Conseguir los bloques vacios*/
for(i=0; i<BLOCKPOOL_BLOCKS; i++){
if(GET_BIT(buf, i)==1)
s++;
}
return (BLOCKPOOL_BLOCKS-s);
}
uint16_t free_inodes_count(const char *disk_mem)
{
uint16_t i, s=0;
char *buf=(char *)disk_mem+FIB_OFFSET;
/*Conseguir los bloques vacios*/
for(i=0; i<INODE_ENTRIES; i++){
if(GET_BIT(buf, i)==1)
s++;
}
return (INODE_ENTRIES-s);
}
/*Constructores y Destructores*/
medianin *medianin_create(void)
{
medianin *result = NULL;
void *map;
result = calloc(1, sizeof(*result));
result->disk_mem = NULL;
result->disk_fd = open(MDFS_FILENAME, O_RDWR);
map=mmap(NULL, BLOCKS*BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, result->disk_fd, 0);
/*Nos aseguramos que el mapeo se realize correctamente*/
if(map==MAP_FAILED)
perror("mmap");
result->disk_mem = map;
result->mount_time = time(NULL);
assert(result != NULL);
assert(INVREP(result));
return result;
}
void medianin_destroy(medianin *self)
{
assert(INVREP(self));
close (self->disk_fd);
/*Cerramos el fd, y guardamos los cambios*/
if(msync(self->disk_mem, BLOCKS*BLOCK_SIZE, MS_SYNC) == -1)
perror("msync");
if(munmap(self->disk_mem, BLOCKS*BLOCK_SIZE) == -1)
perror("munmap");
free(self); self = NULL;
}
/*
* Operaciones Básicas
*/
int medianin_getattr(medianin *self, const char *path, struct stat *stbuf)
{
dir *pdir = NULL;
char *name, *path_c;
uint16_t block = 0;
inode *ind = NULL;
assert(INVREP(self));
assert(path != NULL);
assert(stbuf != NULL);
if(strcmp(path,"/") == 0)
{
ind = (inode *) self->disk_mem + INODEPOOL_OFFSET + (1 * INODE_SIZE);
return inode_getattr(ind, 1, stbuf);
}
pdir = dir_get_from_path(self->disk_mem, path);
if (!pdir)
return -ENOENT;
path_c=calloc(NAME_LENGTH, sizeof(char));
strcpy(path_c, path);
name=calloc(NAME_LENGTH, sizeof(char));
strcpy (name, basename(path_c));
block = dir_search(self->disk_mem, pdir, name);
free(path_c);
free(name);
if (!block){
return -ENOENT;
}
ind = (inode *) self->disk_mem + INODEPOOL_OFFSET + (block * INODE_SIZE);
return inode_getattr(ind, block, stbuf);
}
int medianin_readlink(medianin *self, const char *path, char *buf, size_t size)
{
uint16_t inode_block = 0;
dir *parent_dir = NULL;
file *f = NULL;
char name[26];
assert(INVREP(self));
assert(path!=NULL);
assert(buf!=NULL);
parent_dir = dir_get_from_path(self->disk_mem, path);
if(!parent_dir)
return -ENOENT;
strncpy(&(name[0]), basename((char *)path), 26);
inode_block = dir_search(self->disk_mem, parent_dir, name);
if(!inode_block)
return -ENOENT;
f = (file *) self->disk_mem + INODEPOOL_OFFSET + (inode_block*INODE_SIZE);
if(S_ISDIR(inode_get_mode((inode *) f)))
return -EISDIR;
return file_read(self->disk_mem, f, buf, size, 0);
}
int medianin_mknod(medianin *self, const char *path, mode_t mode)
{
dir *idir = NULL;
uint16_t ifile = 0;
char bname[26];
assert(INVREP(self));
assert(path!=NULL);
strncpy(&(bname[0]),basename((char *) path), 26);
if(strlen(bname)>25){
return -ENAMETOOLONG;
}
idir=dir_get_from_path(self->disk_mem, path);
if(dir_search(self->disk_mem ,idir, bname)){
return -EEXIST;
}
if((ifile=file_create(self->disk_mem, mode))==0){
return -ENOSPC;
}
if((dir_add_direntry(self->disk_mem, idir, bname, ifile, 0))==0){
inode_free_block(self->disk_mem, ifile);
return -ENOSPC;
}
return 0;
}
int medianin_unlink(medianin *self, const char *path)
{
int result = 0;
assert(INVREP(self));
assert(path!=NULL);
result = 0;
return result;
}
int medianin_symlink(medianin *self, const char *from, const char *to)
{
int result = 0;
assert(INVREP(self));
assert(from!=NULL && to!=NULL);
result = 0;
return result;
}
int medianin_rename(medianin *self, const char *from, const char *to)
{
int result = 0;
assert(INVREP(self));
assert(from!=NULL && to!=NULL);
result = 0;
return result;
}
int medianin_link(medianin *self, const char *from, const char *to)
{
int result = 0;
char *to_c = NULL;
char *from_c = NULL;
uint16_t inum_from;
dir *from_dir, *to_dir;
file *f;
time_t itime;
assert(INVREP(self));
assert(from!=NULL && to!=NULL);
if(strlen(basename((char *)to))>25){
return -ENAMETOOLONG;
}
to_c = calloc(NAME_LENGTH, sizeof(char));
from_c = calloc(NAME_LENGTH, sizeof(char));
strcpy(to_c, to);
strcpy(from_c, from);
from_dir=dir_get_from_path(self->disk_mem, to);
if((inum_from=dir_search(self->disk_mem, from_dir, basename((char *)from)))==0){
result = -ENOENT;
goto CLEAN;
}
f=(file *)self->disk_mem+INODEPOOL_OFFSET+inum_from*INODE_SIZE;
if(S_ISDIR(f->mode)==1){
result = -EPERM;
goto CLEAN;
}
if((to_dir=dir_get_from_path(self->disk_mem, to))==NULL){
result = -ENOENT;
goto CLEAN;
}
if(!dir_add_direntry(self->disk_mem, to_dir, basename((char *)to), inum_from, 0)){
result = -ENOSPC;
goto CLEAN;
}
itime=NEW_TIME;
f->nlinks+=1;
f->atime=itime;
f->mtime=itime;
CLEAN:
free(to_c);
free(from_c);
return result;
}
int medianin_chmod(medianin *self, const char *path, mode_t mode)
{
dir *pdir = NULL;
char *name;
uint16_t block = 0;
inode *ind = NULL;
assert(INVREP(self));
assert(path != NULL);
if(strcmp(path,"/") == 0)
{
ind = (inode *) self->disk_mem + INODEPOOL_OFFSET + (1 * INODE_SIZE);
return inode_chmod(ind, mode);
}
pdir = dir_get_from_path(self->disk_mem, path);
if (!pdir)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), NAME_LENGTH);
block = dir_search(self->disk_mem, pdir, name);
free(name);
if (!block)
return -ENOENT;
ind = (inode *) self->disk_mem + INODEPOOL_OFFSET + (block * INODE_SIZE);
return inode_chmod(ind, mode);
}
int medianin_chown(medianin *self,const char *path, uid_t uid, gid_t gid)
{
dir *pdir = NULL;
char *name = NULL;
uint16_t block = 0;
inode *ind = NULL;
assert(INVREP(self));
assert(path != NULL);
if(strcmp(path,"/") == 0)
{
ind = (inode *) self->disk_mem + INODEPOOL_OFFSET + (1 * INODE_SIZE);
return inode_chown(ind, uid,gid);
}
pdir = dir_get_from_path(self->disk_mem, path);
if (!pdir)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), 24);
block = dir_search(self->disk_mem, pdir, name);
free(name);
if (!block)
return -ENOENT;
ind = (inode *) self->disk_mem + INODEPOOL_OFFSET + (block * INODE_SIZE);
return inode_chown(ind, uid, gid);
}
int medianin_truncate(medianin *self, const char *path, off_t size)
{
dir *parent_dir = NULL;
file *f = NULL;
char *name = NULL;
uint16_t file_pos = 0;
assert(INVREP(self));
assert(path!=NULL);
if(strcmp(path,"/") == 0)
return -EISDIR;
/* Conseguimos el directorio padre. */
parent_dir = dir_get_from_path(self->disk_mem, path);
if(!parent_dir)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), 26);
file_pos = dir_search(self->disk_mem, parent_dir, name);
free(name);
if(!file_pos)
return -ENOENT;
f = (file *) self->disk_mem + INODEPOOL_OFFSET + (file_pos*INODE_SIZE);
file_truncate(self->disk_mem, f, size);
return 0;
}
int medianin_read(medianin *self, const char *path,
char *buffer, size_t size, off_t offset)
{
uint16_t inode_block = 0;
dir *parent_dir = NULL;
file *f = NULL;
char *name = NULL;
assert(INVREP(self));
assert(path!=NULL);
assert(buffer!=NULL);
parent_dir = dir_get_from_path(self->disk_mem, path);
if(!parent_dir)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy(name, basename((char *)path), NAME_LENGTH);
inode_block = dir_search(self->disk_mem, parent_dir, name);
free(name);
if(!inode_block)
return -ENOENT;
f = (file *) self->disk_mem + INODEPOOL_OFFSET + (inode_block*INODE_SIZE);
if(S_ISDIR(inode_get_mode((inode *) f)))
return -EISDIR;
return file_read(self->disk_mem, f, buffer, size, offset);
}
int medianin_write(medianin *self, const char *path,
const char *buffer, size_t size, off_t offset)
{
uint16_t inode_block = 0, result = 0;
dir *parent_dir = NULL;
file *f = NULL;
char *name = NULL;
assert(INVREP(self));
assert(path!=NULL);
assert(buffer!=NULL);
parent_dir = dir_get_from_path(self->disk_mem, path);
if(parent_dir==NULL)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), NAME_LENGTH);
inode_block = dir_search(self->disk_mem, parent_dir, name);
free(name);
if(inode_block == 0)
return -ENOENT;
f = (file *) self->disk_mem + INODEPOOL_OFFSET + (inode_block*INODE_SIZE);
if(S_ISDIR(inode_get_mode((inode *)f)))
return -EISDIR;
if(file_truncate(self->disk_mem, f, f->size+size)!=0){
return -ENOSPC;
}
result=file_write(self->disk_mem, f, (char *)buffer, size, offset);
return result;
}
int medianin_statfs(medianin *self, const char *path, struct statvfs *stbuf) {
int result = 0;
uint16_t free_blocks=free_blocks_count(self->disk_mem), free_inodes=free_inodes_count(self->disk_mem);
assert(INVREP(self));
assert(path!=NULL);
assert(stbuf!=NULL);
/* man statvfs */
stbuf->f_bsize=BLOCK_SIZE;
stbuf->f_frsize=BLOCK_SIZE;
stbuf->f_blocks=(fsblkcnt_t)BLOCKS;
stbuf->f_bfree=(fsblkcnt_t)free_blocks;
stbuf->f_bavail=(fsblkcnt_t)free_blocks;
stbuf->f_files=(fsblkcnt_t)INODE_ENTRIES;
stbuf->f_ffree=(fsblkcnt_t)free_inodes;
stbuf->f_ffree=(fsblkcnt_t)free_inodes;
stbuf->f_flag=S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH | S_IFDIR;
stbuf->f_namemax=26;
result = 0;
return result;
}
int medianin_utimens(medianin *self, const char *path, const struct timespec tv[2])
{
char *name = NULL;
dir *parent_dir = NULL;
inode *in = NULL;
uint16_t inode_block = 0;
assert(INVREP(self));
assert(path!=NULL);
if(strcmp(path, "/"))
{
in = (inode *) self->disk_mem + INODEPOOL_OFFSET + INODE_SIZE;
return inode_utime(in, tv);
}
parent_dir = dir_get_from_path(self->disk_mem, path);
if(!parent_dir)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), NAME_LENGTH);
inode_block = dir_search(self->disk_mem, parent_dir, name);
free(name);
if (!inode_block)
return -ENOENT;
in = (inode *) self->disk_mem + INODEPOOL_OFFSET +
(inode_block*INODE_SIZE);
return inode_utime(in, tv);
}
/*
* Operaciones sobre Directorios
*/
int medianin_mkdir(medianin *self, const char *path, mode_t mode)
{
int result = 0;
dir *idir = NULL;
uint16_t inode_dir = 0;
char *name = NULL;
assert(INVREP(self));
assert(path!=NULL);
if(S_ISDIR(mode) == 0)
mode = mode | S_IFDIR;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), NAME_LENGTH);
if(strlen(name) > 25){
result = -ENAMETOOLONG;
goto CLEAN;
}
idir=dir_get_from_path(self->disk_mem, path);
if(dir_search(self->disk_mem ,idir, name)){
result = -EEXIST;
goto CLEAN;
}
if((inode_dir=dir_create(self->disk_mem, mode))==0){
result = -ENOSPC;
goto CLEAN;
}
if((dir_add_direntry(self->disk_mem, idir, name, inode_dir, 0))==0){
inode_free_block(self->disk_mem, inode_dir);
result = -ENOSPC;
goto CLEAN;
}
assert(GET_BIT(self->disk_mem + FIB_OFFSET, inode_dir) == 1);
CLEAN:
free(name);
return result;
}
int medianin_rmdir(medianin *self, const char *path)
{
int result = 0;
dir *parent_dir = NULL;
char *name;
uint16_t inum;
uint16_t search_num;
inode *i;
assert(INVREP(self));
assert(path!=NULL);
parent_dir = dir_get_from_path(self->disk_mem, path);
if(!parent_dir)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy (name, basename((char *) path), NAME_LENGTH);
search_num = dir_search(self->disk_mem, parent_dir, name);
if(!search_num)
return -ENOENT;
i = (inode *) self->disk_mem + INODEPOOL_OFFSET + (search_num * INODE_SIZE);
if(!S_ISDIR(inode_get_mode(i)))
return -ENOTDIR;
if((inode_get_mode(i) & S_IWUSR) == 0)
return -EPERM;
/* Chequeos de permisos. */
inum = dir_remove_direntry(self->disk_mem, parent_dir, name);
free(name);
inode_free_block(self->disk_mem, inum);
assert(inum == search_num);
return result;
}
int medianin_readdir(medianin *self, const char *path, void *buf,
fuse_fill_dir_t filler)
{
int result = 0;
dir *parent_dir = NULL;
dir *my_dir = NULL;
uint16_t dir_inumber = 0;
char *name = NULL;
assert(INVREP(self));
assert(path!=NULL && buf!=NULL);
if(strcmp(path, "/") == 0)
{
/* Root dir */
my_dir = (dir *) self->disk_mem + INODEPOOL_OFFSET + (1 * INODE_SIZE);
goto FILL;
}
parent_dir = dir_get_from_path(self->disk_mem, path);
if (parent_dir == NULL)
return -ENOENT;
name = calloc(NAME_LENGTH, sizeof(char));
strncpy(name, basename((char *) path), NAME_LENGTH);
dir_inumber = dir_search(self->disk_mem, parent_dir, name);
free(name);
if(!dir_inumber)
return -ENOENT;
my_dir = (dir *) self->disk_mem + INODEPOOL_OFFSET + (dir_inumber * INODE_SIZE);
FILL:
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
dir_readdir(self->disk_mem, my_dir, buf, filler);
return result;
}
|