aboutsummaryrefslogtreecommitdiffstats
path: root/libdbfs.c
blob: f0f5ef796c8d170c45dc24cf32feaad568bac2cf (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
/*
 * Maintained by Jeff Garzik <jgarzik@pobox.com>
 *
 * Copyright 2006 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <glib.h>
#include <db.h>
#include "dbfs.h"

struct dbfs *gfs;

static void dbfs_db_syslog(const DB_ENV *dbenv, const char *errpfx,
			const char *msg)
{
	syslog(LOG_WARNING, "%s: %s", errpfx, msg);
}

static int open_db(DB_ENV *env, DB **db_out, const char *name,
		   unsigned int page_size, unsigned int flags)
{
	int rc;
	DB *db;

	rc = db_create(db_out, env, 0);
	if (rc) {
		env->err(env, rc, "db_create");
		return -EIO;
	}

	db = *db_out;

	rc = db->set_pagesize(db, page_size);
	if (rc) {
		db->err(db, rc, "db->set_pagesize");
		return -EIO;
	}

	/* fix everything as little endian */
	rc = db->set_lorder(db, 1234);
	if (rc) {
		db->err(db, rc, "db->set_lorder");
		return -EIO;
	}

	rc = db->open(db, NULL, name, NULL, DB_HASH,
		      DB_AUTO_COMMIT | flags, 0666);
	if (rc) {
		db->err(db, rc, "db->open");
		return -EIO;
	}

	return 0;
}

int dbfs_open(struct dbfs *fs, unsigned int env_flags, unsigned int flags,
	      const char *errpfx, gboolean syslog)
{
	const char *db_home, *db_password;
	int rc;

	/*
	 * open DB environment
	 */

	db_home = fs->home;
	g_assert(db_home != NULL);

	/* this isn't a very secure way to handle passwords */
	db_password = fs->passwd;

	rc = db_env_create(&fs->env, 0);
	if (rc) {
		fprintf(stderr, "fs->env_create failed: %d\n", rc);
		return rc;
	}

	if (syslog)
		fs->env->set_errcall(fs->env, dbfs_db_syslog);
	else
		fs->env->set_errfile(fs->env, stderr);
	fs->env->set_errpfx(fs->env, errpfx);

	if (db_password) {
		flags |= DB_ENCRYPT;
		rc = fs->env->set_encrypt(fs->env, db_password, DB_ENCRYPT_AES);
		if (rc) {
			fs->env->err(fs->env, rc, "fs->env->set_encrypt");
			goto err_out;
		}

		memset(fs->passwd, 0, strlen(fs->passwd));
		free(fs->passwd);
		fs->passwd = NULL;
	}

	/* init DB transactional environment, stored in directory db_home */
	rc = fs->env->open(fs->env, db_home,
			  DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
			  DB_INIT_TXN | env_flags, 0666);
	if (rc) {
		fs->env->err(fs->env, rc, "fs->env->open");
		goto err_out;
	}

	/*
	 * Open metadata database
	 */

	rc = open_db(fs->env, &fs->meta, "metadata", DBFS_PGSZ_METADATA, flags);
	if (rc)
		goto err_out;

	rc = open_db(fs->env, &fs->hashref, "hash", DBFS_PGSZ_METADATA, flags);
	if (rc)
		goto err_out;

	rc = open_db(fs->env, &fs->data, "data", DBFS_PGSZ_DATA, flags);
	if (rc)
		goto err_out_meta;

	return 0;

err_out_meta:
	fs->meta->close(fs->meta, 0);
err_out:
	fs->env->close(fs->env, 0);
	return rc;
}

void dbfs_close(struct dbfs *fs)
{
	fs->data->close(fs->data, 0);
	fs->hashref->close(fs->hashref, 0);
	fs->meta->close(fs->meta, 0);
	fs->env->close(fs->env, 0);

	fs->env = NULL;
	fs->meta = NULL;
}

struct dbfs *dbfs_new(void)
{
	struct dbfs *fs;
	char *passwd;

	fs = g_new0(struct dbfs, 1);
	if (!fs)
		return NULL;

	fs->next_inode = 2ULL;

	fs->home = getenv("DB_HOME");
	if (!fs->home) {
		fprintf(stderr, "DB_HOME not set, aborting\n");
		goto err_out;
	}

	passwd = getenv("DB_PASSWORD");
	if (passwd) {
		fs->passwd = strdup(passwd);

		/* FIXME: this isn't a very good way to shroud the password */
		if (putenv("DB_PASSWORD=X"))
			perror("putenv DB_PASSWORD (SECURITY WARNING)");
	}

	return fs;

err_out:
	g_free(fs);
	return NULL;
}

void dbfs_free(struct dbfs *fs)
{
	g_free(fs);
}

void dbfs_inode_free(struct dbfs_inode *ino)
{
	if (!ino)		/* permit dbfs_inode_free(NULL) */
		return;

	free(ino->raw_inode);
	g_free(ino);
}

int dbfs_inode_write(DB_TXN *txn, struct dbfs_inode *ino)
{
	struct dbfs_raw_inode *raw_ino = ino->raw_inode;
	guint64 ino_n = GUINT64_FROM_LE(ino->raw_inode->ino);
	DBT key, val;
	char key_str[32];

	memset(&key, 0, sizeof(key));
	memset(&val, 0, sizeof(val));

	sprintf(key_str, "/inode/%Lu", (unsigned long long) ino_n);

	key.data = key_str;
	key.size = strlen(key_str);

	val.data = raw_ino;
	val.size = ino->raw_ino_size;

	raw_ino->version = GUINT64_TO_LE(
		GUINT64_FROM_LE(raw_ino->version) + 1);

	return gfs->meta->put(gfs->meta, txn, &key, &val, 0) ? -EIO : 0;
}

int dbfs_dir_new(DB_TXN *txn, guint64 parent, guint64 ino_n,
		 const struct dbfs_inode *ino)
{
	void *mem, *p, *q;
	struct dbfs_dirent *de;
	size_t namelen;
	DBT val;
	int rc;

	p = mem = malloc(128);
	memset(mem, 0, 128);

	/*
	 * add entry for "."
	 */
	de = p;
	de->magic = GUINT32_TO_LE(DBFS_DE_MAGIC);
	de->namelen = GUINT16_TO_LE(1);
	de->ino = GUINT64_TO_LE(ino_n);

	q = p + sizeof(struct dbfs_dirent);
	memcpy(q, ".", 1);

	namelen = GUINT16_FROM_LE(de->namelen);
	p += dbfs_dirent_next(namelen);

	/*
	 * add entry for ".."
	 */
	de = p;
	de->magic = GUINT32_TO_LE(DBFS_DE_MAGIC);
	de->namelen = GUINT16_TO_LE(2);
	de->ino = GUINT64_TO_LE(parent);

	q = p + sizeof(struct dbfs_dirent);
	memcpy(q, "..", 2);

	namelen = GUINT16_FROM_LE(de->namelen);
	p += dbfs_dirent_next(namelen);

	/*
	 * add terminating entry
	 */
	de = p;
	de->magic = GUINT32_TO_LE(DBFS_DE_MAGIC);

	namelen = GUINT16_FROM_LE(de->namelen);
	p += dbfs_dirent_next(namelen);

	/*
	 * store dir in database
	 */
	memset(&val, 0, sizeof(val));
	val.data = mem;
	val.size = p - mem;

	rc = dbfs_dir_write(txn, ino_n, &val);

	free(mem);

	return rc;
}

int dbfs_dir_write(DB_TXN *txn, guint64 ino, DBT *val)
{
	DBT key;
	char key_str[32];

	memset(&key, 0, sizeof(key));

	sprintf(key_str, "/dir/%Lu", (unsigned long long) ino);

	key.data = key_str;
	key.size = strlen(key_str);

	return gfs->meta->put(gfs->meta, txn, &key, val, 0) ? -EIO : 0;
}