summaryrefslogtreecommitdiff
path: root/src/cmd/tmpfs.c
blob: 352b2f1d12543383cd6d5edf3204246f42e2ed8e (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
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
#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;
typedef struct Xattr Xattr;
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;

	Xattr *xattr;

	/* allocated by tmpfs_open
	 * freed by node_close when (open == 0 && ref == NULL && self != node_root). */
};
struct Xattr {
	Xattr *next;
	char *name, *buf; /* owned */
	size_t len; /* 0 means deleted. yeah, this isn't efficient, idc */
};

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 Xattr *node_getxattr(Node *node, const char *name);
static char *node_xattrindex(Node *node, int *plen);


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);
		}
		while (node->xattr) {
			Xattr *x = node->xattr;
			node->xattr = x->next;
			free(x->name);
			free(x->buf);
			free(x);
		}
		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);
}

static Xattr *
node_getxattr(Node *node, const char *name)
{
	for (Xattr *x = node->xattr; x; x = x->next) {
		if (strcmp(name, x->name) == 0) {
			return x;
		}
	}
	return NULL;
}

static char *
node_xattrindex(Node *node, int *plen)
{
	char *buf, *it;
	int len = 0;
	for (Xattr *x = node->xattr; x; x = x->next) {
		if (x->len == 0) continue;
		len += strlen(x->name) + 1;
	}
	buf = it = malloc(len);
	*plen = len;
	for (Xattr *x = node->xattr; x; x = x->next) {
		if (x->len == 0) continue;
		int slen = strlen(x->name) + 1;
		memcpy(it, x->name, slen);
		it += slen;
	}
	assert(buf + len == it);
	return buf;
}

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;

			case VFSOP_GETXATTR:
				if (strcmp(buf, "virt.index") == 0) {
					int len;
					char *res = node_xattrindex(req.id, &len);
					_sys_fs_respond(reqh, res, len, 0);
					free(res);
				} else {
					Xattr *x = node_getxattr(req.id, buf);
					if (x == NULL || x->len == 0) {
						_sys_fs_respond(reqh, NULL, -ENOENT, 0);
					} else {
						_sys_fs_respond(reqh, x->buf, x->len, 0);
					}
				}
				break;

			case VFSOP_SETXATTR:
				/* dirty but safe in this context */
				if (memcmp("user.", buf, 5) != 0) {
					_sys_fs_respond(reqh, NULL, -EACCES, 0);
				} else {
					Xattr *x = node_getxattr(req.id, buf);
					if (x == NULL) {
						x = calloc(1, sizeof(Xattr));
						x->name = strdup(buf);
						x->next = ptr->xattr;
						ptr->xattr = x;
					}
					free(x->buf);
					x->buf = malloc(req.len);
					if (x->buf == NULL) {
						x->len = 0;
						_sys_fs_respond(reqh, NULL, -ENOSPC, 0);
						break;
					}
					x->len = req.len;
					memcpy(x->buf, req.id2, req.len);
					_sys_fs_respond(reqh, 0, req.len, 0);
				}
				break;

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