aboutsummaryrefslogtreecommitdiffstats
path: root/dbfs.c
blob: 6ad725a4c7a287962f1c171e74fbc79c52c13cf2 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#define FUSE_USE_VERSION 25

#include <fuse_lowlevel.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <glib.h>
#include <db.h>
#include "dbfs.h"

static void dbfs_fill_ent(const struct dbfs_inode *ino,
			  struct fuse_entry_param *ent)
{
	memset(ent, 0, sizeof(*ent));

	ent->ino = GUINT64_FROM_LE(ino->raw_inode->ino);
	ent->generation = GUINT64_FROM_LE(ino->raw_inode->version);

	/* these timeouts are just a guess */
	ent->attr_timeout = 2.0;
	ent->entry_timeout = 2.0;
}

static void dbfs_reply_ino(fuse_req_t req, struct dbfs_inode *ino)
{
	struct fuse_entry_param ent;

	dbfs_fill_ent(ino, &ent);

	fuse_reply_entry(req, &ent);

	dbfs_inode_free(ino);
}

static void dbfs_op_init(void *userdata)
{
	struct dbfs **fs_io = userdata;
	struct dbfs *fs;
	int rc;

	fs = dbfs_new();

	rc = dbfs_open(fs);
	if (rc)
		abort();			/* TODO: improve */

	*fs_io = fs;
}

static void dbfs_op_destroy(void *userdata)
{
	struct dbfs **fs_io = userdata;
	struct dbfs *fs = *fs_io;

	dbfs_close(fs);
	dbfs_free(fs);

	*fs_io = NULL;
}

static void dbfs_op_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	guint64 ino_n;
	struct dbfs_inode *ino;
	int rc;

	/* lookup inode in parent directory */
	rc = dbfs_dir_lookup(parent, name, &ino_n);
	if (rc) {
		fuse_reply_err(req, -rc);
		return;
	}

	rc = dbfs_inode_read(ino_n, &ino);
	if (rc) {
		fuse_reply_err(req, -rc);
		return;
	}

	/* send reply */
	dbfs_reply_ino(req, ino);
}

static void dbfs_op_getattr(fuse_req_t req, fuse_ino_t ino_n,
			     struct fuse_file_info *fi)
{
	struct dbfs_inode *ino = NULL;
	struct stat st;
	int rc;

	/* read inode from database */
	rc = dbfs_inode_read(ino_n, &ino);
	if (rc) {
		fuse_reply_err(req, ENOENT);
		return;
	}

	/* fill in stat buf, taking care to convert from
	 * little endian to native endian
	 */
	memset(&st, 0, sizeof(st));
	st.st_dev	= 1;
	st.st_ino	= ino_n;
	st.st_mode	= GUINT32_FROM_LE(ino->raw_inode->mode);
	st.st_nlink	= GUINT32_FROM_LE(ino->raw_inode->nlink);
	st.st_uid	= GUINT32_FROM_LE(ino->raw_inode->uid);
	st.st_gid	= GUINT32_FROM_LE(ino->raw_inode->gid);
	st.st_rdev	= GUINT64_FROM_LE(ino->raw_inode->rdev);
	st.st_size	= GUINT64_FROM_LE(ino->raw_inode->size);
	st.st_blksize	= 512;
	st.st_blocks	= GUINT64_FROM_LE(ino->raw_inode->size) / 512ULL;
	st.st_atime	= GUINT64_FROM_LE(ino->raw_inode->atime);
	st.st_mtime	= GUINT64_FROM_LE(ino->raw_inode->mtime);
	st.st_ctime	= GUINT64_FROM_LE(ino->raw_inode->ctime);

	/* send result back to FUSE */
	fuse_reply_attr(req, &st, 2.0);

	dbfs_inode_free(ino);
}

static void dbfs_op_readlink(fuse_req_t req, fuse_ino_t ino)
{
	int rc;
	DBT val;
	char *s;

	/* read link from database */
	rc = dbfs_symlink_read(ino, &val);
	if (rc) {
		fuse_reply_err(req, -rc);
		return;
	}

	/* send reply; use g_strndup to append a trailing null */
	s = g_strndup(val.data, val.size);
	fuse_reply_readlink(req, s);
	g_free(s);

	free(val.data);
}

static int dbfs_mode_validate(mode_t mode)
{
	unsigned int ifmt = mode & S_IFMT;
	int rc = 0;

	if (S_ISREG(mode)) {
		if (ifmt & ~S_IFREG)
			rc = -EINVAL;
	}
	else if (S_ISDIR(mode)) {
		if (ifmt & ~S_IFDIR)
			rc = -EINVAL;
	}
	else if (S_ISCHR(mode)) {
		if (ifmt & ~S_IFCHR)
			rc = -EINVAL;
	}
	else if (S_ISBLK(mode)) {
		if (ifmt & ~S_IFBLK)
			rc = -EINVAL;
	}
	else if (S_ISFIFO(mode)) {
		if (ifmt & ~S_IFIFO)
			rc = -EINVAL;
	}
	else if (S_ISLNK(mode))
		rc = -EINVAL;
	else if (S_ISSOCK(mode)) {
		if (ifmt & ~S_IFSOCK)
			rc = -EINVAL;
	}
	else
		rc = -EINVAL;

	return rc;
}

static void dbfs_op_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
			  mode_t mode, dev_t rdev)
{
	struct dbfs_inode *ino;
	int rc;

	rc = dbfs_mode_validate(mode);
	if (rc) {
		fuse_reply_err(req, -rc);
		return;
	}

	rc = dbfs_mknod(parent, name, mode, rdev, &ino);
	if (rc) {
		fuse_reply_err(req, -rc);
		return;
	}

	dbfs_reply_ino(req, ino);
}

static void dbfs_op_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
			  mode_t mode)
{
	mode &= (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
	mode |= S_IFDIR;

	return dbfs_op_mknod(req, parent, name, mode, 0);
}

static void dbfs_op_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	int rc = dbfs_unlink(parent, name, 0);
	fuse_reply_err(req, rc ? -rc : 0);
}

static int dbfs_chk_empty(struct dbfs_dirent *de, void *userdata)
{
	if ((GUINT16_FROM_LE(de->namelen) == 1) && (!memcmp(de->name, ".", 1)))
		return 0;
	if ((GUINT16_FROM_LE(de->namelen) == 2) && (!memcmp(de->name, "..", 2)))
		return 0;
	return ENOTEMPTY;
}

static void dbfs_op_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	guint64 ino_n;
	int rc;
	DBT val;

	/* get inode number associated with name */
	rc = dbfs_dir_lookup(parent, name, &ino_n);
	if (rc)
		goto out;

	/* read dir associated with name */
	rc = dbfs_dir_read(ino_n, &val);
	if (rc)
		goto out;

	/* make sure dir only contains "." and ".." */
	rc = dbfs_dir_foreach(val.data, dbfs_chk_empty, NULL);
	free(val.data);

	/* if dbfs_chk_empty() returns non-zero, dir is not empty */
	if (rc)
		goto out;

	/* dir is empty, go ahead and unlink */
	rc = dbfs_unlink(parent, name, DBFS_UNLINK_DIR);

out:
	fuse_reply_err(req, rc ? -rc : 0);
}

static void dbfs_op_symlink(fuse_req_t req, const char *link,
			    fuse_ino_t parent, const char *name)
{
	struct dbfs_inode *ino;
	int rc;

	if (!g_utf8_validate(link, -1, NULL)) {
		rc = -EINVAL;
		goto err_out;
	}

	rc = dbfs_mknod(parent, name,
			S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO, 0, &ino);
	if (rc)
		goto err_out;

	rc = dbfs_symlink_write(GUINT64_FROM_LE(ino->raw_inode->ino), link);
	if (rc)
		goto err_out_mknod;

	dbfs_reply_ino(req, ino);
	return;

err_out_mknod:
	dbfs_inode_del(ino);
	dbfs_inode_free(ino);
err_out:
	fuse_reply_err(req, -rc);
}

static void dbfs_op_opendir(fuse_req_t req, fuse_ino_t ino,
			    struct fuse_file_info *fi)
{
	DBT val;
	int rc;

	/* read directory from database */
	rc = dbfs_dir_read(ino, &val);
	if (rc) {
		fuse_reply_err(req, -rc);
		return;
	}

	/* save for later use */
	fi->fh = (uint64_t) (unsigned long) val.data;

	/* send reply */
	fuse_reply_open(req, fi);
}

static void dbfs_op_releasedir(fuse_req_t req, fuse_ino_t ino,
			       struct fuse_file_info *fi)
{
	void *p = (void *) (unsigned long) fi->fh;

	/* release directory contents */
	free(p);
}

struct dirbuf {
	char *p;
	size_t size;
};

/* stock function copied from FUSE template */
static void dirbuf_add(struct dirbuf *b, const char *name, fuse_ino_t ino)
{
	struct stat stbuf;
	size_t oldsize = b->size;
	b->size += fuse_dirent_size(strlen(name));
	b->p = (char *)realloc(b->p, b->size);
	memset(&stbuf, 0, sizeof(stbuf));
	stbuf.st_ino = ino;
	fuse_add_dirent(b->p + oldsize, name, &stbuf, b->size);
}

#ifndef min
#define min(x, y) ((x) < (y) ? (x) : (y))
#endif

/* stock function copied from FUSE template */
static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
			     off_t off, size_t maxsize)
{
	if (off < bufsize)
		return fuse_reply_buf(req, buf + off,
				      min(bufsize - off, maxsize));
	else
		return fuse_reply_buf(req, NULL, 0);
}

static int dbfs_fill_dirbuf(struct dbfs_dirent *de, void *userdata)
{
	struct dirbuf *b = userdata;
	char *s;

	/* add dirent to buffer; use g_strndup solely to append nul */
	s = g_strndup(de->name, GUINT16_FROM_LE(de->namelen));
	dirbuf_add(b, s, GUINT64_FROM_LE(de->ino));
	free(s);
	return 0;
}

static void dbfs_op_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
			     off_t off, struct fuse_file_info *fi)
{
	struct dirbuf b;
	void *p;

	/* grab directory contents stored by opendir */
	p = (void *) (unsigned long) fi->fh;

	/* iterate through each dirent, filling dirbuf */
	memset(&b, 0, sizeof(b));
	dbfs_dir_foreach(p, dbfs_fill_dirbuf, &b);

	/* send reply */
	reply_buf_limited(req, b.p, b.size, off, size);
	free(b.p);
}

static struct fuse_lowlevel_ops dbfs_ops = {
	.init		= dbfs_op_init,
	.destroy	= dbfs_op_destroy,
	.lookup		= dbfs_op_lookup,
	.forget		= NULL,
	.getattr	= dbfs_op_getattr,
	.setattr	= NULL,
	.readlink	= dbfs_op_readlink,
	.mknod		= dbfs_op_mknod,
	.mkdir		= dbfs_op_mkdir,
	.unlink		= dbfs_op_unlink,
	.rmdir		= dbfs_op_rmdir,
	.symlink	= dbfs_op_symlink,
	.rename		= NULL,
	.link		= NULL,
	.open		= NULL,
	.read		= NULL,
	.write		= NULL,
	.flush		= NULL,
	.release	= NULL,
	.fsync		= NULL,
	.opendir	= dbfs_op_opendir,
	.readdir	= dbfs_op_readdir,
	.releasedir	= dbfs_op_releasedir,
	.fsyncdir	= NULL,
	.statfs		= NULL,
	.setxattr	= NULL,
	.getxattr	= NULL,
	.listxattr	= NULL,
	.removexattr	= NULL,
	.access		= NULL,
	.create		= NULL,
};

/* stock main() from FUSE example */
int main(int argc, char *argv[])
{
	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
	char *mountpoint;
	int err = -1;
	int fd;

	if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
	    (fd = fuse_mount(mountpoint, &args)) != -1) {
		struct fuse_session *se;

		se = fuse_lowlevel_new(&args, &dbfs_ops,
				       sizeof(dbfs_ops), NULL);
		if (se != NULL) {
			if (fuse_set_signal_handlers(se) != -1) {
				struct fuse_chan *ch = fuse_kern_chan_new(fd);
				if (ch != NULL) {
					fuse_session_add_chan(se, ch);
					err = fuse_session_loop(se);
				}
				fuse_remove_signal_handlers(se);
			}
			fuse_session_destroy(se);
		}
		close(fd);
	}
	fuse_unmount(mountpoint);
	fuse_opt_free_args(&args);

	return err ? 1 : 0;
}