-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateFS.c
More file actions
144 lines (125 loc) · 3.27 KB
/
createFS.c
File metadata and controls
144 lines (125 loc) · 3.27 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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<libgen.h>
#include<sys/types.h>
#include<sys/time.h>
#include<limits.h>
#include<math.h>
#include<time.h>
#define BLOCK_SIZE 512
#define NUM_INODES 16
#define NUM_INODE_BLOCKS 4
#define INODES_BLOCK 4
#define NUM_DATA_BLOCKS 32
#define MAX_NAME_LEN 28
#define NUM_DIRECT 10
#define DISK_INODE_SIZE sizeof(struct diskInode)
#define SB_SIZE sizeof(struct superblock)
#define INODE_BM_SIZE sizeof(struct inodeBit)
#define DATA_BM_SIZE sizeof(struct dataBit)
#define MAX_DIR_DEPTH 20
#define DIR_ENTRIES_BLOCK (BLOCK_SIZE/sizeof(dirRecord))
#define NUM_BLOCKS 39
const int inodeStartBlk = 4;
const int dataStartBlk = 8;
int fsp;
struct superblock{ // Block number 1
unsigned long int fsSize;
unsigned int numFreeData, numFreeInodes, inodeBMap, dataBMap, rootInode;
char filler[BLOCK_SIZE - 28];
};
struct dirRecord{
unsigned int inodeNum;
char name[MAX_NAME_LEN];
};
struct diskInode{
unsigned int uid;
unsigned int gid;
unsigned int n_link;
unsigned int mode;
unsigned int blockNums[NUM_DIRECT];
unsigned int numRecords;
int blockSize;
long size;
long numBlocks;
int aTime;
unsigned int aTimeNsec;
int mTime;
unsigned int mTimeNsec;
int cTime;
unsigned int cTimeNsec;
unsigned int type;
int filler[5];
};
// For the flags 0 - not occupied, 1 - occupied
struct inodeBit{
unsigned char flag[NUM_INODES];
char filler[BLOCK_SIZE - NUM_INODES];
};
struct dataBit{
unsigned char flag[NUM_DATA_BLOCKS];
char filler[BLOCK_SIZE - NUM_DATA_BLOCKS];
};
struct superblock spBlk;
struct inodeBit ibmap;
struct dataBit dbmap;
struct dirRecord rootRec[2];
struct diskInode root;
int main(int argc, char *argv){
unsigned char *buffer = malloc(BLOCK_SIZE);
struct timespec currTime;
spBlk.fsSize = NUM_BLOCKS*BLOCK_SIZE;
spBlk.numFreeData = NUM_DATA_BLOCKS;
spBlk.numFreeInodes = NUM_INODES;
spBlk.inodeBMap = 2;
spBlk.dataBMap = 3;
spBlk.rootInode = 1;
rootRec[0].inodeNum = 1;
strcpy(rootRec[0].name, ".");
rootRec[1].inodeNum = 1;
strcpy(rootRec[1].name, "..");
clock_gettime(CLOCK_REALTIME, &currTime);
root.uid = getuid();
root.gid = getgid();
root.n_link = 1;
root.mode = S_IFDIR | 0777;
root.blockNums[0] = 8;
root.numRecords = 2;
root.blockSize = BLOCK_SIZE;
root.size = 2*sizeof(struct dirRecord);
root.numBlocks = 1;
root.aTime = currTime.tv_sec;
root.aTimeNsec = currTime.tv_nsec;
root.mTime = currTime.tv_sec;
root.mTimeNsec = currTime.tv_nsec;
root.cTime = currTime.tv_sec;
root.cTimeNsec = currTime.tv_nsec;
root.type = 2; // 2 for directory
fsp = open("M", O_CREAT | O_TRUNC | O_RDWR, 0777);
if(fsp<0){
printf("Error in open");
}
write(fsp, &spBlk, sizeof(struct superblock));
for(int i=0; i< NUM_INODES; i++){
ibmap.flag[i] = 0;
}
for(int i=0; i< NUM_DATA_BLOCKS; i++){
dbmap.flag[i] = 0;
}
ibmap.flag[0] = 1;
dbmap.flag[0] = 1;
write(fsp, &ibmap, sizeof(struct inodeBit));
write(fsp, &dbmap, sizeof(struct dataBit));
memset(buffer, 0, BLOCK_SIZE);
memcpy(buffer, &root, sizeof(struct diskInode));
write(fsp, buffer, BLOCK_SIZE);
lseek(fsp, BLOCK_SIZE*7, SEEK_SET);
memset(buffer, 0, BLOCK_SIZE);
memcpy(buffer, &rootRec, sizeof(struct dirRecord)*2);
write(fsp, buffer, BLOCK_SIZE);
close(fsp);
return 0;
}