include updates
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.0.25b / source / tdb / common / freelistcheck.c
blob3f79a016b8646cfdb128778d9739accf6de19ee7
1 /*
2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Jeremy Allison 2006
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
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.
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.
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
27 #include "tdb_private.h"
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 :-).
37 static int seen_insert(struct tdb_context *mem_tdb, tdb_off_t rec_ptr)
39 TDB_DATA key, data;
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);
47 int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
49 struct tdb_context *mem_tdb = NULL;
50 struct list_struct rec;
51 tdb_off_t rec_ptr, last_ptr;
52 int ret = -1;
54 *pnum_entries = 0;
56 mem_tdb = tdb_open("flval", tdb->header.hash_size,
57 TDB_INTERNAL, O_RDWR, 0600);
58 if (!mem_tdb) {
59 return -1;
62 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
63 tdb_close(mem_tdb);
64 return 0;
67 last_ptr = FREELIST_TOP;
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;
75 /* read in the freelist top */
76 if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1) {
77 goto fail;
80 while (rec_ptr) {
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. */
86 if (seen_insert(mem_tdb, rec_ptr)) {
87 ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
88 goto fail;
91 if (rec_free_read(tdb, rec_ptr, &rec) == -1) {
92 goto fail;
95 /* move to the next record */
96 last_ptr = rec_ptr;
97 rec_ptr = rec.next;
98 *pnum_entries += 1;
101 ret = 0;
103 fail:
105 tdb_close(mem_tdb);
106 tdb_unlock(tdb, -1, F_WRLCK);
107 return ret;