summaryrefslogtreecommitdiff
path: root/src/cmd/tmpfs.c
blob: 5e4b4e81d8212defe2326ed78e07be9b3d20112b (plain)
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
#include <assert.h>
#include <camellia/flags.h>
#include <camellia/fs/dir.h>
#include <camellia/fs/misc.h>
#include <camellia/fsutil.h>
#include <camellia/syscalls.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct Node Node;
struct Node {
	char *name;
	size_t namelen;
	bool directory;
	char *buf;
	size_t size, capacity;

	size_t open; /* amount of open handles */

	Node *sibling, *child;
	/* Pointer to the sibling/child field referencing the node. NULL for removed
	 * files. */
	Node **ref;

	/* allocated by tmpfs_open
	 * freed by node_close when (open == 0 && ref == NULL && self != node_root). */
};

static Node node_root = {
	.directory = true,
};

/** Responds to open(), finding an existing node, or creating one when applicable. */
static Node *tmpfs_open(const char *path, struct ufs_request *req);
/** Finds a direct child with the given name. */
static Node *node_getchild(Node *parent, const char *name, size_t len);
/** Corresponds to close(); drops a reference. */
static void node_close(Node *node);
static long node_move(Node *from, Node *to);
/** Removes a file. It's kept in memory until all the open handles are closed. */
static long node_remove(Node *node);
static void node_sanity(Node *node, Node **from);


static Node *
node_getchild(Node *parent, const char *name, size_t len) {
	for (Node *iter = parent->child; iter; iter = iter->sibling) {
		if (iter->namelen == len && memcmp(name, iter->name, len) == 0) {
			return iter;
		}
	}
	return NULL;
}

static Node *
tmpfs_open(const char *path, struct ufs_request *req)
{
	/* *path is not null terminated! */
	Node *node = &node_root;
	if (req->len == 0) return NULL;
	if (req->len == 1) return node; /* "/" */
	path++;
	req->len--;

	bool more = true;
	size_t segpos = 0, seglen; /* segments end with a slash, inclusive */
	while (more) {
		Node *parent = node;
		char *slash = memchr(path + segpos, '/', req->len - segpos);
		seglen = (slash ? (size_t)(slash - path + 1) : req->len) - segpos;
		more = segpos + seglen < req->len;

		node = node_getchild(parent, path + segpos, seglen);
		if (!node) {
			if (!more && (req->flags & OPEN_CREATE)) {
				node = calloc(1, sizeof *node);

				node->name = malloc(seglen + 1);
				if (node->name == NULL) {
					free(node);
					return NULL;
				}
				memcpy(node->name, path + segpos, seglen);
				node->name[seglen] = '\0';

				node->directory = slash;
				node->namelen = seglen;
				if (parent->child) {
					parent->child->ref = &node->sibling;
					*parent->child->ref = parent->child;
				}
				node->ref = &parent->child;
				*node->ref = node;
			} else {
				return NULL;
			}
		}
		segpos += seglen;
	}
	node->open++;
	return node;
}

static void
node_close(Node *node) {
	node->open--;
	if (node->ref == NULL && node != &node_root && node->open == 0) {
		if (node->name) {
			free(node->name);
		}
		free(node->buf);
		free(node);
	}
}

static long
node_move(Node *from, Node *to) {
	if (from == &node_root || to == &node_root) {
		return -1;
	}

	assert(from && to);
	/* 1. unlink from */
	if (from->ref) {
		assert(*from->ref == from);
		if (from->sibling) {
			from->sibling->ref = from->ref;
			*from->sibling->ref = from->sibling;
		} else {
			*from->ref = NULL;
		}
		from->sibling = NULL;
		from->ref = NULL;
	} else {
		assert(from->sibling == NULL);
	}

	/* 2. link *from in place of *to
	 *    note that there's still a reference to *to in the handle */
	if (to->ref) {
		assert(*to->ref == to);
		from->sibling = to->sibling;
		if (from->sibling) {
			from->sibling->ref = &from->sibling;
		}
		from->ref = to->ref;
		*from->ref = from;

		to->ref = NULL;
		to->sibling = NULL;
	}
	from->name = to->name;
	from->namelen = to->namelen;
	to->name = NULL;
	to->namelen = 0;
	return 0;
}

static long
node_remove(Node *node) {
	if (node == &node_root) return -1;
	if (!node->ref) return -1;
	if (node->child) return -ENOTEMPTY;
	*node->ref = node->sibling;
	node->ref = NULL;
	node->sibling = NULL;
	node_close(node);
	return 0;
}

static void
node_sanity(Node *node, Node **from) {
	if (node == NULL) return;
	if (node != &node_root && node->ref != from) {
		assert(false);
	}
	node_sanity(node->sibling, &node->sibling);
	node_sanity(node->child, &node->child);
}

int
main(void) {
	const size_t buflen = 4096;
	char *buf = malloc(buflen);
	if (!buf) return -1;

	node_sanity(&node_root, NULL);

	for (;;) {
		struct ufs_request req;
		hid_t reqh = ufs_wait(buf, buflen, &req);
		Node *ptr = req.id;
		if (reqh < 0) break;

		switch (req.op) {
			case VFSOP_OPEN:
				ptr = tmpfs_open(buf, &req);
				_sys_fs_respond(reqh, ptr, ptr ? 0 : -ENOENT, 0);
				break;

			case VFSOP_READ:
				if (ptr->directory) {
					struct dirbuild db;
					dir_start(&db, req.offset, buf, buflen);
					for (Node *iter = ptr->child; iter; iter = iter->sibling) {
						dir_appendl(&db, iter->name, iter->namelen);
					}
					_sys_fs_respond(reqh, buf, dir_finish(&db), 0);
				} else {
					fs_normslice(&req.offset, &req.len, ptr->size, false);
					_sys_fs_respond(reqh, ptr->buf + req.offset, req.len, 0);
				}
				break;

			case VFSOP_WRITE:
				if (ptr->directory) {
					_sys_fs_respond(reqh, NULL, -ENOSYS, 0);
					break;
				}

				fs_normslice(&req.offset, &req.len, ptr->size, true);
				if (ptr->capacity <= req.offset + req.len) {
					ptr->capacity = (req.offset + req.len + 511) & ~511;
					ptr->buf = realloc(ptr->buf, ptr->capacity);
				}

				memcpy(ptr->buf + req.offset, buf, req.len);
				if ((req.flags & WRITE_TRUNCATE) || ptr->size < req.offset + req.len) {
					ptr->size = req.offset + req.len;
				}
				_sys_fs_respond(reqh, NULL, req.len, 0);
				break;

			case VFSOP_GETSIZE:
				if (ptr->directory) {
					// TODO could be cached in ptr->size
					struct dirbuild db;
					dir_start(&db, req.offset, NULL, buflen);
					for (Node *iter = ptr->child; iter; iter = iter->sibling) {
						dir_append(&db, iter->name);
					}
					_sys_fs_respond(reqh, NULL, dir_finish(&db), 0);
				} else {
					_sys_fs_respond(reqh, NULL, ptr->size, 0);
				}
				break;

			case VFSOP_REMOVE:
				_sys_fs_respond(reqh, NULL, node_remove(ptr), 0);
				break;

			case VFSOP_CLOSE:
				node_close(ptr);
				_sys_fs_respond(reqh, NULL, -1, 0);
				break;

			case VFSOP_DUPLEX:
				if (req.flags == DUPLEX_REMOVE) {
					_sys_fs_respond(reqh, NULL, node_move(req.id, req.id2), 0);
				} else {
					_sys_fs_respond(reqh, NULL, -ENOSYS, 0);
				}
				break;

			default:
				_sys_fs_respond(reqh, NULL, -1, 0);
				break;
		}
		node_sanity(&node_root, NULL);
	}
	return 1;
}