-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_list.c
278 lines (218 loc) · 6.69 KB
/
root_list.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
#include <assert.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <libgen.h>
#include "bitmap.h"
#include "pages.h"
#include "util.h"
#include "slist.h"
#include "inode.h"
#include "directory.h"
#include "root_list.h"
#define NUM_VERSIONS 7
static void* root_list = NULL;
/*
typedef struct cow_version {
int rnum;
int vnum;
char op[26];
} cow_version;
*/
//makes sure data did not get corrupted in root_list
void
root_list_sanity_check() {
for(int ii = NUM_VERSIONS - 1; ii >= 0; --ii) {
cow_version* curr = get_root_list_idx(ii);
if (curr != 0) {
assert(curr->vnum < 10000);
assert(curr->rnum < 10000);
}
}
}
//after calling this, root_list should be set up
void
root_init()
{
// set the page bit for the root_list
// the root_list should be the first things after the bitmaps
if (!bitmap_get(get_pages_bitmap(), 1)) {
// allocate a page for the root list
int page = alloc_page();
assert(page == 1);
assert(root_list = pages_get_page(page));
memset(root_list, 0, NUM_VERSIONS * sizeof(cow_version*));
}
else
{
// set the root
root_list = pages_get_page(1);
root_list_sanity_check();
}
// printf("+ root_init() -> %d\n", 1);
// add_root(0);
}
//gets the inum of the current root
int
get_current_root(){
return ((cow_version*)root_list)->rnum;
}
cow_version*
get_root_list_idx(int idx){
return (cow_version*)root_list + ((idx) * sizeof(cow_version*));
}
void
swap_root(int vnum)
{
cow_version new_root;
for (size_t ii=0; ii< NUM_VERSIONS; ++ii) {
cow_version* tmp = get_root_list_idx(ii);
if (tmp->vnum == vnum){
new_root = *get_root_list_idx(ii);
}
}
cow_version* curr = (cow_version*)root_list;
cow_version* prev = 0;
for (int ii = NUM_VERSIONS - 1; ii > 0; --ii) {
curr = get_root_list_idx(ii); //(cow_version*)root_list + ((ii) * sizeof(cow_version));
prev = get_root_list_idx(ii-1); //(cow_version*)root_list + ((ii - 1) * sizeof(cow_version));
memcpy(curr, prev, sizeof(cow_version));
}
cow_version* add = (cow_version*)root_list;
add->rnum = new_root.rnum;
add->vnum = ((cow_version*)root_list)->vnum + 1;
strcpy(add->op, new_root.op);
root_list_sanity_check();
}
//push_back onto root stack
void
add_root(int rnum, char* op)
{
printf("+ add_root(%d, %s) -> ...\n", rnum, op);
cow_version* curr = (cow_version*)root_list;
cow_version* prev = 0;
int curr_vnum = curr->vnum;
// garbage collection
if(curr_vnum > NUM_VERSIONS - 1){
cow_version* to_free = get_root_list_idx(NUM_VERSIONS - 1);
cow_version* replacable = get_root_list_idx(NUM_VERSIONS - 2);
int rv = traverse_and_free(to_free, replacable);
if(rv < 0) {
printf("\t[DEBUG] traverse and free failed! freeing: {%d, %d} with {%d, %d): %d",
to_free->vnum, to_free->rnum, replacable->vnum, replacable->rnum, rv);
}
}
for (int ii = NUM_VERSIONS - 1; ii > 0; --ii) {
curr = get_root_list_idx(ii); //(cow_version*)root_list + ((ii) * sizeof(cow_version));
prev = get_root_list_idx(ii-1); //(cow_version*)root_list + ((ii - 1) * sizeof(cow_version));
memcpy(curr, prev, sizeof(cow_version));
}
cow_version* add = (cow_version*)root_list;
add->rnum = rnum;
add->vnum = curr_vnum + 1;
strcpy(add->op, op);
root_list_sanity_check();
printf("... new version: -> %d \n", add->vnum);
}
//CHANGE THIS TO ACCOUNT FOR MKNOD AND OTHER OPERATIONS
int
traverse_and_free(cow_version* to_free, cow_version* next_ver)
{
int rnum6 = to_free->rnum;
char* label_from_6 = to_free->op;
char* label_from_5 = next_ver->op;
slist* sl6 = s_split(label_from_6, ' '); // syscall from 6->op
char* syscall6 = strdup(sl6->data);
char* path6 = strdup(sl6->next->data);
slist* sl5 = s_split(label_from_5, ' '); // path from 5->op
char* syscall5 = strdup(sl5->data);
char* path5 = strdup(sl5->next->data);
if(streq(syscall6, "init") || streq(syscall5, "init")){
s_free(sl6);
s_free(sl5);
return 0;
}
int rv = 0;
if (streq(syscall5, "mknod"))
{
rv = traverse_and_free_hlp(rnum6, dirname(path5));
}
if (streq(syscall5, "write")) {
rv = traverse_and_free_hlp(rnum6, path5);
}
if (streq(syscall5, "truncate")) {
rv = traverse_and_free_hlp(rnum6, path5);
}
if(streq(syscall5, "link"))
{
rv = traverse_and_free_hlp(rnum6, dirname(path5));
}
if(streq(syscall5, "set")){ // set time
path5 = sl5->next->next->data;
rv = traverse_and_free_hlp(rnum6, path5);
}
if(streq(syscall5, "chmod")){ // set time
rv = traverse_and_free_hlp(rnum6, path5);
}
s_free(sl6);
s_free(sl5);
/*
// See line 499 in storage.c
if(streq(syscall5, "rename(1)")){
rv = traverse_and_free_hlp(rnum6, path5);
}
if(streq(syscall5, "rename")){
rv = traverse_and_free_hlp(rnum6, dirname(path5));
}
*/
printf("+ traverse_and_free ({%s %s r=%d}, {%s %s}) -> %d\n", syscall6, path6, rnum6, syscall5, path5, rv);
free(syscall5);
free(syscall6);
free(path6);
free(path5);
return rv;
}
int
traverse_and_free_hlp(int rnum6, char* path_from_5)
{
//remove me from path
//get inode for directory,
int inum = tree_lookup_hlp(path_from_5, rnum6);
// don't free the inodes_base
if (inum == 0) {
return inum;
}
if(inum < 0){
// assert(0);
return inum;
}
inode* me = get_inode(inum);
// bitmap_print(get_inode_bitmap(), 512);
free_inode(me);
// bitmap_print(get_inode_bitmap(), 512);
if(streq(path_from_5, "/")){
return 0;
}
char* pathdup = strdup(path_from_5);
traverse_and_free_hlp(rnum6, dirname(pathdup));
free(pathdup);
return 0;
}
slist*
rootlist_version_table(){
//printf("+ rootlist_version_table() -> 7\n");
slist* build = 0;
// int size = min(7, get_root_list_idx(0)->vnum);
// build this list backwards so that it's in the right order for printing
for(int ii = NUM_VERSIONS - 1; ii >= 0; --ii){
cow_version* curr = get_root_list_idx(ii); //(cow_version*)root_list + ii * sizeof(cow_version);
if(curr->vnum > 0) {
char first[32];
snprintf(first, sizeof(first), "%d %s", curr->vnum, curr->op);
// snprintf(first, sizeof(first), "%d %s {r=%d}", curr->vnum, curr->op, curr->rnum);
build = s_cons(first, build);
}
}
root_list_sanity_check();
return build;
}