| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | trivial database library
|
|---|
| 5 |
|
|---|
| 6 | Copyright (C) Jeremy Allison 2006
|
|---|
| 7 |
|
|---|
| 8 | ** NOTE! The following LGPL license applies to the tdb
|
|---|
| 9 | ** library. This does NOT imply that all of Samba is released
|
|---|
| 10 | ** under the LGPL
|
|---|
| 11 |
|
|---|
| 12 | This library is free software; you can redistribute it and/or
|
|---|
| 13 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 14 | License as published by the Free Software Foundation; either
|
|---|
| 15 | version 2 of the License, or (at your option) any later version.
|
|---|
| 16 |
|
|---|
| 17 | This library is distributed in the hope that it will be useful,
|
|---|
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 20 | Lesser General Public License for more details.
|
|---|
| 21 |
|
|---|
| 22 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 23 | License along with this library; if not, write to the Free Software
|
|---|
| 24 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include "tdb_private.h"
|
|---|
| 28 |
|
|---|
| 29 | /* Check the freelist is good and contains no loops.
|
|---|
| 30 | Very memory intensive - only do this as a consistency
|
|---|
| 31 | checker. Heh heh - uses an in memory tdb as the storage
|
|---|
| 32 | for the "seen" record list. For some reason this strikes
|
|---|
| 33 | me as extremely clever as I don't have to write another tree
|
|---|
| 34 | data structure implementation :-).
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | static int seen_insert(struct tdb_context *mem_tdb, tdb_off_t rec_ptr)
|
|---|
| 38 | {
|
|---|
| 39 | TDB_DATA key, data;
|
|---|
| 40 |
|
|---|
| 41 | memset(&data, '\0', sizeof(data));
|
|---|
| 42 | key.dptr = (char *)&rec_ptr;
|
|---|
| 43 | key.dsize = sizeof(rec_ptr);
|
|---|
| 44 | return tdb_store(mem_tdb, key, data, TDB_INSERT);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
|
|---|
| 48 | {
|
|---|
| 49 | struct tdb_context *mem_tdb = NULL;
|
|---|
| 50 | struct list_struct rec;
|
|---|
| 51 | tdb_off_t rec_ptr, last_ptr;
|
|---|
| 52 | int ret = -1;
|
|---|
| 53 |
|
|---|
| 54 | *pnum_entries = 0;
|
|---|
| 55 |
|
|---|
| 56 | mem_tdb = tdb_open("flval", tdb->header.hash_size,
|
|---|
| 57 | TDB_INTERNAL, O_RDWR, 0600);
|
|---|
| 58 | if (!mem_tdb) {
|
|---|
| 59 | return -1;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
|
|---|
| 63 | tdb_close(mem_tdb);
|
|---|
| 64 | return 0;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | last_ptr = FREELIST_TOP;
|
|---|
| 68 |
|
|---|
| 69 | /* Store the FREELIST_TOP record. */
|
|---|
| 70 | if (seen_insert(mem_tdb, last_ptr) == -1) {
|
|---|
| 71 | ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
|
|---|
| 72 | goto fail;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /* read in the freelist top */
|
|---|
| 76 | if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1) {
|
|---|
| 77 | goto fail;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | while (rec_ptr) {
|
|---|
| 81 |
|
|---|
| 82 | /* If we can't store this record (we've seen it
|
|---|
| 83 | before) then the free list has a loop and must
|
|---|
| 84 | be corrupt. */
|
|---|
| 85 |
|
|---|
| 86 | if (seen_insert(mem_tdb, rec_ptr)) {
|
|---|
| 87 | ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
|
|---|
| 88 | goto fail;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (rec_free_read(tdb, rec_ptr, &rec) == -1) {
|
|---|
| 92 | goto fail;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /* move to the next record */
|
|---|
| 96 | last_ptr = rec_ptr;
|
|---|
| 97 | rec_ptr = rec.next;
|
|---|
| 98 | *pnum_entries += 1;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | ret = 0;
|
|---|
| 102 |
|
|---|
| 103 | fail:
|
|---|
| 104 |
|
|---|
| 105 | tdb_close(mem_tdb);
|
|---|
| 106 | tdb_unlock(tdb, -1, F_WRLCK);
|
|---|
| 107 | return ret;
|
|---|
| 108 | }
|
|---|