summaryrefslogtreecommitdiff
path: root/lib/tdb/common/rescue.c
blob: 03ae8d6ae51b96e325d0a31b8f29474656376491 (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
 /*
   Unix SMB/CIFS implementation.

   trivial database library, rescue attempt code.

   Copyright (C) Rusty Russell		   2012

     ** NOTE! The following LGPL license applies to the tdb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "tdb_private.h"
#include <assert.h>


struct found {
	tdb_off_t head; /* 0 -> invalid. */
	struct tdb_record rec;
	TDB_DATA key;
	bool in_hash;
	bool in_free;
};

struct found_table {
	/* As an ordered array (by head offset). */
	struct found *arr;
	unsigned int num, max;
};

static bool looks_like_valid_record(struct tdb_context *tdb,
				    tdb_off_t off,
				    const struct tdb_record *rec,
				    TDB_DATA *key)
{
	unsigned int hval;

	if (rec->magic != TDB_MAGIC)
		return false;

	if (rec->key_len + rec->data_len > rec->rec_len)
		return false;

	if (rec->rec_len % TDB_ALIGNMENT)
		return false;

	/* Next pointer must make some sense. */
	if (rec->next > 0 && rec->next < TDB_DATA_START(tdb->header.hash_size))
		return false;

	if (tdb->methods->tdb_oob(tdb, rec->next, sizeof(*rec), 1))
		return false;

	key->dsize = rec->key_len;
	key->dptr = tdb_alloc_read(tdb, off + sizeof(*rec), key->dsize);
	if (!key->dptr)
		return false;

	hval = tdb->hash_fn(key);
	if (hval != rec->full_hash) {
		free(key->dptr);
		return false;
	}

	/* Caller frees up key->dptr */
	return true;
}

static bool add_to_table(struct found_table *found,
			 tdb_off_t off,
			 struct tdb_record *rec,
			 TDB_DATA key)
{
	if (found->num + 1 > found->max) {
		struct found *new;
		found->max = (found->max ? found->max * 2 : 128);
		new = realloc(found->arr, found->max * sizeof(found->arr[0]));
		if (!new)
			return false;
		found->arr = new;
	}

	found->arr[found->num].head = off;
	found->arr[found->num].rec = *rec;
	found->arr[found->num].key = key;
	found->arr[found->num].in_hash = false;
	found->arr[found->num].in_free = false;

	found->num++;
	return true;
}

static bool walk_record(struct tdb_context *tdb,
			const struct found *f,
			void (*walk)(TDB_DATA, TDB_DATA, void *private_data),
			void *private_data)
{
	TDB_DATA data;

	data.dsize = f->rec.data_len;
	data.dptr = tdb_alloc_read(tdb,
				   f->head + sizeof(f->rec) + f->rec.key_len,
				   data.dsize);
	if (!data.dptr) {
		if (tdb->ecode == TDB_ERR_OOM)
			return false;
		/* I/O errors are expected. */
		return true;
	}

	walk(f->key, data, private_data);
	free(data.dptr);
	return true;
}

/* First entry which has offset >= this one. */
static unsigned int find_entry(struct found_table *found, tdb_off_t off)
{
	unsigned int start = 0, end = found->num;

	while (start < end) {
		/* We can't overflow here. */
		unsigned int mid = (start + end) / 2;

		if (off < found->arr[mid].head) {
			end = mid;
		} else if (off > found->arr[mid].head) {
			start = mid + 1;
		} else {
			return mid;
		}
	}

	assert(start == end);
	return end;
}

static void found_in_hashchain(struct found_table *found, tdb_off_t head)
{
	unsigned int match;

	match = find_entry(found, head);
	if (match < found->num && found->arr[match].head == head) {
		found->arr[match].in_hash = true;
	}
}

static void mark_free_area(struct found_table *found, tdb_off_t head,
			   tdb_len_t len)
{
	unsigned int match;

	match = find_entry(found, head);
	/* Mark everything within this free entry. */
	while (match < found->num) {
		if (found->arr[match].head >= head + len) {
			break;
		}
		found->arr[match].in_free = true;
		match++;
	}
}

static int cmp_key(const void *a, const void *b)
{
	const struct found *fa = a, *fb = b;

	if (fa->key.dsize < fb->key.dsize) {
		return -1;
	} else if (fa->key.dsize > fb->key.dsize) {
		return 1;
	}
	return memcmp(fa->key.dptr, fb->key.dptr, fa->key.dsize);
}

static bool key_eq(TDB_DATA a, TDB_DATA b)
{
	return a.dsize == b.dsize
		&& memcmp(a.dptr, b.dptr, a.dsize) == 0;
}

static void free_table(struct found_table *found)
{
	unsigned int i;

	for (i = 0; i < found->num; i++) {
		free(found->arr[i].key.dptr);
	}
	free(found->arr);
}

static void logging_suppressed(struct tdb_context *tdb,
			       enum tdb_debug_level level, const char *fmt, ...)
{
}

_PUBLIC_ int tdb_rescue(struct tdb_context *tdb,
			void (*walk)(TDB_DATA, TDB_DATA, void *private_data),
			void *private_data)
{
	struct found_table found = { NULL, 0, 0 };
	tdb_off_t h, off, i;
	tdb_log_func oldlog = tdb->log.log_fn;
	struct tdb_record rec;
	TDB_DATA key;
	bool locked;

	/* Read-only databases use no locking at all: it's best-effort.
	 * We may have a write lock already, so skip that case too. */
	if (tdb->read_only || tdb->allrecord_lock.count != 0) {
		locked = false;
	} else {
		if (tdb_lockall_read(tdb) == -1)
			return -1;
		locked = true;
	}

	/* Make sure we know true size of the underlying file. */
	tdb->methods->tdb_oob(tdb, tdb->map_size, 1, 1);

	/* Suppress logging, since we anticipate errors. */
	tdb->log.log_fn = logging_suppressed;

	/* Now walk entire db looking for records. */
	for (off = TDB_DATA_START(tdb->header.hash_size);
	     off < tdb->map_size;
	     off += TDB_ALIGNMENT) {
		if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
					   DOCONV()) == -1)
			continue;

		if (looks_like_valid_record(tdb, off, &rec, &key)) {
			if (!add_to_table(&found, off, &rec, key)) {
				goto oom;
			}
		}
	}

	/* Walk hash chains to positive vet. */
	for (h = 0; h < 1+tdb->header.hash_size; h++) {
		bool slow_chase = false;
		tdb_off_t slow_off = FREELIST_TOP + h*sizeof(tdb_off_t);

		if (tdb_ofs_read(tdb, FREELIST_TOP + h*sizeof(tdb_off_t),
				 &off) == -1)
			continue;

		while (off && off != slow_off) {
			if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
						   DOCONV()) != 0) {
				break;
			}

			/* 0 is the free list, rest are hash chains. */
			if (h == 0) {
				/* Don't mark garbage as free. */
				if (rec.magic != TDB_FREE_MAGIC) {
					break;
				}
				mark_free_area(&found, off,
					       sizeof(rec) + rec.rec_len);
			} else {
				found_in_hashchain(&found, off);
			}

			off = rec.next;

			/* Loop detection using second pointer at half-speed */
			if (slow_chase) {
				/* First entry happens to be next ptr */
				tdb_ofs_read(tdb, slow_off, &slow_off);
			}
			slow_chase = !slow_chase;
		}
	}

	/* Recovery area: must be marked as free, since it often has old
	 * records in there! */
	if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off) == 0 && off != 0) {
		if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
					   DOCONV()) == 0) {
			mark_free_area(&found, off, sizeof(rec) + rec.rec_len);
		}
	}

	/* Now sort by key! */
	qsort(found.arr, found.num, sizeof(found.arr[0]), cmp_key);

	for (i = 0; i < found.num; ) {
		unsigned int num, num_in_hash = 0;

		/* How many are identical? */
		for (num = 0; num < found.num - i; num++) {
			if (!key_eq(found.arr[i].key, found.arr[i+num].key)) {
				break;
			}
			if (found.arr[i+num].in_hash) {
				if (!walk_record(tdb, &found.arr[i+num],
						 walk, private_data))
					goto oom;
				num_in_hash++;
			}
		}
		assert(num);

		/* If none were in the hash, we print any not in free list. */
		if (num_in_hash == 0) {
			unsigned int j;

			for (j = i; j < i + num; j++) {
				if (!found.arr[j].in_free) {
					if (!walk_record(tdb, &found.arr[j],
							 walk, private_data))
						goto oom;
				}
			}
		}

		i += num;
	}

	tdb->log.log_fn = oldlog;
	if (locked) {
		tdb_unlockall_read(tdb);
	}
	return 0;

oom:
	tdb->log.log_fn = oldlog;
	tdb->ecode = TDB_ERR_OOM;
	TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_rescue: failed allocating\n"));
	free_table(&found);
	if (locked) {
		tdb_unlockall_read(tdb);
	}
	return -1;
}