freelistcheck.c 2.6 KB

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