-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
356 lines (283 loc) · 7.8 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
#define FUSE_USE_VERSION 31
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <libgen.h>
#include <libpmemobj.h>
POBJ_LAYOUT_BEGIN(inode);
POBJ_LAYOUT_ROOT(r_inode, struct r_inode);
POBJ_LAYOUT_TOID(inode, struct pc_inode);
POBJ_LAYOUT_END(inode);
struct r_inode {
TOID(struct pc_inode) head;
TOID(struct pc_inode) tail;
};
struct pc_inode {
char name[128];
char data[4096];
struct stat st;
TOID(struct pc_inode) next;
};
static char *pmem_pool;
static PMEMobjpool *pop;
static TOID(struct r_inode) root;
static TOID(struct pc_inode) pre_search_inode(const char *path)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
TOID(struct pc_inode) p_inode = TOID_NULL(struct pc_inode);
char *s_path;
inode = D_RO(root)->head;
if (!strcmp(path, "/"))
return inode;
s_path = (char *)calloc(strlen(path), sizeof(char));
strcpy(s_path, path);
s_path = s_path + 1;
while (!TOID_IS_NULL(inode)) {
if (!strcmp(D_RO(inode)->name, s_path))
return p_inode;
p_inode = inode;
inode = D_RO(inode)->next;
}
return TOID_NULL(struct pc_inode);
}
static TOID(struct pc_inode) search_inode(const char *path)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
char *s_path;
inode = D_RO(root)->head;
if (!strcmp(path, "/"))
return inode;
s_path = (char *)calloc(strlen(path), sizeof(char));
strcpy(s_path, path);
s_path = s_path + 1;
while (!TOID_IS_NULL(inode)) {
if (!strcmp(D_RO(inode)->name, s_path))
return inode;
inode = D_RO(inode)->next;
}
return TOID_NULL(struct pc_inode);
}
static int pc_getattr(const char *path, struct stat *stbuf)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
inode = search_inode(path);
if (TOID_IS_NULL(inode)) {
return -ENOENT;
} else if (!strcmp(path, "/")) {
memset(stbuf, 0, sizeof(struct stat));
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (!strcmp(path + 1, D_RO(inode)->name)){
memset(stbuf, 0, sizeof(struct stat));
stbuf->st_mode = D_RO(inode)->st.st_mode;
stbuf->st_nlink = D_RO(inode)->st.st_nlink;
stbuf->st_size = D_RO(inode)->st.st_size;
} else
return -ENOENT;
return 0;
}
static int pc_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
(void) path;
(void) fi;
(void) offset;
inode = D_RO(root)->head;
inode = D_RO(inode)->next;
if (TOID_IS_NULL(inode))
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
do {
filler(buf, D_RO(inode)->name, NULL, 0);
inode = D_RO(inode)->next;
} while (!TOID_IS_NULL((inode)));
return 0;
}
static int pc_open(const char *path, struct fuse_file_info *fi)
{
(void) fi;
(void) path;
return 0;
}
static int pc_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
size_t len;
(void) fi;
inode = search_inode(path);
if (TOID_IS_NULL(inode))
return -ENOENT;
len = D_RO(inode)->st.st_size;
if ((size_t)offset < len)
{
if (offset + size > len)
size = len - offset;
memcpy(buf, D_RO(inode)->data + offset, size);
}
else
size = 0;
return size;
}
static int pc_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
char *tmp;
char buffer[BUFSIZ];
(void) fi;
inode = search_inode(path);
if (TOID_IS_NULL(inode))
return -ENOENT;
if ((size_t)(size + offset) > (size_t)D_RO(inode)->st.st_size) {
if (D_RO(inode)->st.st_size != 0) {
tmp = (char *)malloc(size + D_RO(inode)->st.st_size + 1);
memcpy(tmp, D_RO(inode)->data, D_RO(inode)->st.st_size);
sprintf(buffer, "%s%s", tmp, buf);
free(tmp);
} else {
sprintf(buffer, "%s", buf);
}
} else {
sprintf(buffer, "%s", buf);
}
TX_BEGIN (pop) {
D_RW(inode)->st.st_size += size;
TX_MEMCPY(D_RW(inode)->data, buffer, strlen(buffer));
} TX_END
return size;
}
static int pc_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
TOID(struct pc_inode) p_inode = TOID_NULL(struct pc_inode);
p_inode = D_RW(root)->tail;
uint64_t len = strlen(path);
char *fname = (char *)malloc(sizeof(char *) * len);
strcpy(fname, path);
fname = fname + 1;
(void) fi;
TX_BEGIN(pop) {
TOID(struct pc_inode) inode = TX_NEW(struct pc_inode);
TX_MEMCPY(D_RW(inode)->name, fname, strlen(fname));
D_RW(inode)->next = TOID_NULL(struct pc_inode);
D_RW(inode)->st.st_mode = mode | 0777;
D_RW(inode)->st.st_nlink = 1;
D_RW(inode)->st.st_size = 0;
D_RW(inode)->next = TOID_NULL(struct pc_inode);
D_RW(p_inode)->next = inode;
D_RW(root)->tail = inode;
} TX_END
return 0;
}
static int pc_unlink(const char *path)
{
TOID(struct pc_inode) inode = TOID_NULL(struct pc_inode);
TOID(struct pc_inode) old = TOID_NULL(struct pc_inode);
inode = pre_search_inode(path);
TX_BEGIN(pop) {
old = D_RO(inode)->next;
D_RW(inode)->next = D_RO(old)->next;
TX_FREE(old);
} TX_END
return 0;
}
static void *pc_init(struct fuse_conn_info *conn)
{
(void) conn;
pop = pmemobj_open(pmem_pool, POBJ_LAYOUT_NAME(inode));
if (pop == NULL) {
perror("pmemobj_open");
}
root = POBJ_ROOT(pop, struct r_inode);
TX_BEGIN(pop) {
TX_ADD(root);
} TX_END
return NULL;
}
static struct fuse_operations fs_oper = {
.init = pc_init,
.getattr = pc_getattr,
.readdir = pc_readdir,
.open = pc_open,
.read = pc_read,
.write = pc_write,
.create = pc_create,
.unlink = pc_unlink,
};
#define MKFS "mkfs.fs"
static int mkfs(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage: ./"MKFS" <file>\n");
return -1;
}
pmem_pool = argv[1];
printf("mkfs - create -start\n");
pop = pmemobj_create(pmem_pool, POBJ_LAYOUT_NAME(inode), PMEMOBJ_MIN_POOL, 0666);
if (pop == NULL) {
perror("pmemobj_create");
return -1;
}
root = POBJ_ROOT(pop, struct r_inode);
TX_BEGIN(pop) {
TX_ADD(root);
TOID(struct pc_inode) inode = TX_NEW(struct pc_inode);
TX_MEMCPY(D_RW(inode)->name, "", strlen(""));
D_RW(inode)->next = TOID_NULL(struct pc_inode);
D_RW(inode)->st.st_mode = (S_IFDIR | 0755);
D_RW(inode)->st.st_nlink = 2;
D_RW(root)->head = inode;
printf("hey - %s\n", D_RO(inode)->name);
#ifdef __DEBUG__
TOID(struct pc_inode) iinode = TX_NEW(struct pc_inode);
TX_MEMCPY(D_RW(iinode)->name, "test", strlen("test"));
D_RW(iinode)->next = TOID_NULL(struct pc_inode);
D_RW(iinode)->st.st_mode = (S_IFREG | 0755);
D_RW(iinode)->st.st_nlink = 1;
TX_MEMCPY(D_RW(iinode)->data, "Hello, World!\n", strlen("Hello, World!\n"));
D_RW(iinode)->st.st_size = strlen(D_RO(iinode)->data);
D_RW(inode)->next = iinode;
printf("hey - %s\n", D_RO(iinode)->name);
TOID(struct pc_inode) iiinode = TX_NEW(struct pc_inode);
TX_MEMCPY(D_RW(iiinode)->name, "file", strlen("file"));
D_RW(iiinode)->next = TOID_NULL(struct pc_inode);
D_RW(iiinode)->st.st_mode = (S_IFREG | 0755);
D_RW(iiinode)->st.st_nlink = 1;
char *con = "#include<stdio.h>\nint main(void) {\nprintf(\"hello\");\nreturn 0;}\n";
TX_MEMCPY(D_RW(iiinode)->data, con, strlen(con));
D_RW(iiinode)->st.st_size = strlen(D_RO(iiinode)->data);
D_RW(iinode)->next = iiinode;
printf("hey - %s\n", D_RO(iiinode)->name);
D_RW(root)->tail = iiinode;
#endif
D_RW(root)->tail = inode;
} TX_END;
printf("mkfs - create - end\n");
return 1;
}
int main(int argc, char *argv[])
{
int ret = 0;
char *bname = basename(argv[0]);
if (!strcmp(MKFS, bname)) {
mkfs(argc, argv);
} else {
if (argc == 6) {
pmem_pool = argv[4];
argv[4] = argv[5];
ret = fuse_main(--argc, argv, &fs_oper, NULL);
pmemobj_close(pop);
}
if (argc == 5) {
pmem_pool = argv[3];
argv[3] = argv[4];
ret = fuse_main(--argc, argv, &fs_oper, NULL);
pmemobj_close(pop);
}
}
return ret;
}