summaryrefslogtreecommitdiffstats
path: root/lib/util/util_ntdb.c
blob: be720e55a6c14847ee900e1540e22b12eabb541c (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
/*
   Unix SMB/CIFS implementation.

   ntdb utility functions

   Copyright (C) Rusty Russell 2012

   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; either version 3 of the License, or
   (at your option) any later version.

   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.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "util_ntdb.h"
#include "lib/param/param.h"
#include "replace.h"
#include "system/filesys.h"

/*
 * This handles NTDB_CLEAR_IF_FIRST.
 *
 * It's a bad idea for new code, but S3 uses it quite a bit.
 */
static enum NTDB_ERROR clear_if_first(int fd, void *unused)
{
	/* We hold a lock offset 4 always, so we can tell if anyone else is. */
	struct flock fl;

	fl.l_type = F_WRLCK;
	fl.l_whence = SEEK_SET;
	fl.l_start = 4; /* ACTIVE_LOCK */
	fl.l_len = 1;

	if (fcntl(fd, F_SETLK, &fl) == 0) {
		/* We must be first ones to open it w/ NTDB_CLEAR_IF_FIRST! */
		if (ftruncate(fd, 0) != 0) {
			return NTDB_ERR_IO;
		}
	}
	fl.l_type = F_RDLCK;
	if (fcntl(fd, F_SETLKW, &fl) != 0) {
		return NTDB_ERR_IO;
	}
	return NTDB_SUCCESS;
}

/* We only need these for the CLEAR_IF_FIRST lock. */
static int reacquire_cif_lock(struct ntdb_context *ntdb, bool *fail)
{
	struct flock fl;
	union ntdb_attribute cif;

	cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
	cif.openhook.base.next = NULL;

	if (ntdb_get_attribute(ntdb, &cif) != NTDB_SUCCESS
	    || cif.openhook.fn != clear_if_first) {
		return 0;
	}

	/* We hold a lock offset 4 always, so we can tell if anyone else is. */
	fl.l_type = F_RDLCK;
	fl.l_whence = SEEK_SET;
	fl.l_start = 4; /* ACTIVE_LOCK */
	fl.l_len = 1;
	if (fcntl(ntdb_fd(ntdb), F_SETLKW, &fl) != 0) {
		*fail = true;
		return -1;
	}
	return 0;
}

/* You only need this on databases with NTDB_CLEAR_IF_FIRST */
int ntdb_reopen(struct ntdb_context *ntdb)
{
	bool unused;
	return reacquire_cif_lock(ntdb, &unused);
}

/* You only need to do this if you have NTDB_CLEAR_IF_FIRST databases, and
 * the parent will go away before this child. */
int ntdb_reopen_all(void)
{
	bool fail = false;

	ntdb_foreach(reacquire_cif_lock, &fail);
	if (fail)
		return -1;
	return 0;
}

static void *ntdb_talloc(const void *owner, size_t len, void *priv_data)
{
	return talloc_size(owner, len);
}

static void *ntdb_expand(void *old, size_t newlen, void *priv_data)
{
	return talloc_realloc_size(NULL, old, newlen);
}

static void ntdb_free(void *old, void *priv_data)
{
	talloc_free(old);
}

static int ntdb_destroy(struct ntdb_context *ntdb)
{
	ntdb_close(ntdb);
	return 0;
}

static void ntdb_log(struct ntdb_context *ntdb,
		     enum ntdb_log_level level,
		     enum NTDB_ERROR ecode,
		     const char *message,
		     void *unused)
{
	int dl;
	const char *name = ntdb_name(ntdb);

	switch (level) {
	case NTDB_LOG_USE_ERROR:
	case NTDB_LOG_ERROR:
		dl = 0;
		break;
	case NTDB_LOG_WARNING:
		dl = 2;
		break;
	default:
		dl = 0;
	}

	DEBUG(dl, ("ntdb(%s):%s: %s\n", name ? name : "unnamed",
		   ntdb_errorstr(ecode), message));
}

struct ntdb_context *ntdb_new(TALLOC_CTX *ctx,
			      const char *name, int ntdb_flags,
			      int open_flags, mode_t mode,
			      union ntdb_attribute *attr,
			      struct loadparm_context *lp_ctx)
{
	union ntdb_attribute log_attr, alloc_attr, open_attr;
	struct ntdb_context *ntdb;

	if (lp_ctx && !lpcfg_use_mmap(lp_ctx)) {
		ntdb_flags |= NTDB_NOMMAP;
	}

	/* Great hack for speeding testing! */
	if (getenv("TDB_NO_FSYNC")) {
		ntdb_flags |= NTDB_NOSYNC;
	}

	log_attr.base.next = attr;
	log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
	log_attr.log.fn = ntdb_log;

	alloc_attr.base.next = &log_attr;
	alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
	alloc_attr.alloc.alloc = ntdb_talloc;
	alloc_attr.alloc.expand = ntdb_expand;
	alloc_attr.alloc.free = ntdb_free;

	if (ntdb_flags & NTDB_CLEAR_IF_FIRST) {
		log_attr.base.next = &open_attr;
		open_attr.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
		open_attr.openhook.base.next = attr;
		open_attr.openhook.fn = clear_if_first;
		ntdb_flags &= ~NTDB_CLEAR_IF_FIRST;
	}

	ntdb = ntdb_open(name, ntdb_flags, open_flags, mode, &alloc_attr);
	if (!ntdb) {
		return NULL;
	}

	/* We can re-use the tdb's path name for the talloc name */
	name = ntdb_name(ntdb);
	if (name) {
		talloc_set_name_const(ntdb, name);
	} else {
		talloc_set_name_const(ntdb, "unnamed ntdb");
	}
	talloc_set_destructor(ntdb, ntdb_destroy);

	return talloc_steal(ctx, ntdb);
}

enum NTDB_ERROR ntdb_lock_bystring(struct ntdb_context *ntdb,
				   const char *keyval)
{
	NTDB_DATA key = string_term_ntdb_data(keyval);

	return ntdb_chainlock(ntdb, key);
}

void ntdb_unlock_bystring(struct ntdb_context *ntdb, const char *keyval)
{
	NTDB_DATA key = string_term_ntdb_data(keyval);

	ntdb_chainunlock(ntdb, key);
}

enum NTDB_ERROR ntdb_delete_bystring(struct ntdb_context *ntdb,
				     const char *keystr)
{
	NTDB_DATA key = string_term_ntdb_data(keystr);

	return ntdb_delete(ntdb, key);
}

enum NTDB_ERROR ntdb_store_bystring(struct ntdb_context *ntdb,
				    const char *keystr,
				    NTDB_DATA data, int nflags)
{
	NTDB_DATA key = string_term_ntdb_data(keystr);

	return ntdb_store(ntdb, key, data, nflags);
}

enum NTDB_ERROR ntdb_fetch_bystring(struct ntdb_context *ntdb,
				    const char *keystr,
				    NTDB_DATA *data)
{
	NTDB_DATA key = string_term_ntdb_data(keystr);

	return ntdb_fetch(ntdb, key, data);
}

enum NTDB_ERROR ntdb_fetch_int32(struct ntdb_context *ntdb,
				 const char *keystr, int32_t *val)
{
	NTDB_DATA data;
	enum NTDB_ERROR err;

	err = ntdb_fetch(ntdb, string_term_ntdb_data(keystr), &data);
	if (err == NTDB_SUCCESS) {
		if (data.dsize != sizeof(*val)) {
			err = NTDB_ERR_CORRUPT;
		} else {
			*val = IVAL(data.dptr,0);
		}
		talloc_free(data.dptr);
	}
	return NTDB_SUCCESS;
}

enum NTDB_ERROR ntdb_store_int32(struct ntdb_context *ntdb,
				 const char *keystr, int32_t val)
{
	NTDB_DATA data, key;
	int32_t v_store;

	SIVAL(&v_store, 0, val);
	data = ntdb_mkdata(&v_store, sizeof(v_store));
	key = string_term_ntdb_data(keystr);

	return ntdb_store(ntdb, key, data, NTDB_REPLACE);
}

enum NTDB_ERROR ntdb_add_int32_atomic(struct ntdb_context *ntdb,
				      const char *keystr,
				      int32_t *oldval, int32_t addval)
{
	int32_t val;
	enum NTDB_ERROR err;

	err = ntdb_lock_bystring(ntdb, keystr);
	if (err) {
		return err;
	}

	err = ntdb_fetch_int32(ntdb, keystr, &val);
	if (err) {
		if (err == NTDB_ERR_NOEXIST) {
			/* Start with 'old' value */
			val = *oldval;
		} else {
			goto err_out;
		}
	} else {
		/* It worked, set return value (oldval) to tdb data */
		*oldval = val;
	}

	/* Increase value and store for next time. */
	val += addval;
	err = ntdb_store_int32(ntdb, keystr, val);

  err_out:
	ntdb_unlock_bystring(ntdb, keystr);
	return err;
}

NTSTATUS map_nt_error_from_ntdb(enum NTDB_ERROR err)
{
	NTSTATUS result = NT_STATUS_INTERNAL_ERROR;

	switch (err) {
	case NTDB_SUCCESS:
		result = NT_STATUS_OK;
		break;
	case NTDB_ERR_CORRUPT:
		result = NT_STATUS_INTERNAL_DB_CORRUPTION;
		break;
	case NTDB_ERR_IO:
		result = NT_STATUS_UNEXPECTED_IO_ERROR;
		break;
	case NTDB_ERR_OOM:
		result = NT_STATUS_NO_MEMORY;
		break;
	case NTDB_ERR_EXISTS:
		result = NT_STATUS_OBJECT_NAME_COLLISION;
		break;

	case NTDB_ERR_LOCK:
		/*
		 * NTDB_ERR_LOCK is very broad, we could for example
		 * distinguish between fcntl locks and invalid lock
		 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
		 * compromise.
		 */
		result = NT_STATUS_FILE_LOCK_CONFLICT;
		break;
	case NTDB_ERR_NOEXIST:
		result = NT_STATUS_NOT_FOUND;
		break;
	case NTDB_ERR_EINVAL:
		result = NT_STATUS_INVALID_PARAMETER;
		break;
	case NTDB_ERR_RDONLY:
		result = NT_STATUS_ACCESS_DENIED;
		break;
	};
	return result;
}