tdb: Make robust against shrinking tdbs
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 8 Oct 2012 18:56:47 +0000 (11:56 -0700)
committerKarolin Seeger <kseeger@samba.org>
Mon, 29 Oct 2012 09:05:43 +0000 (10:05 +0100)
When probing for a size change (eg. just before tdb_expand, tdb_check,
tdb_rescue) we call tdb_oob(tdb, tdb->map_size, 1, 1).  Unfortunately
this does nothing if the tdb has actually shrunk, which as Volker
demonstrated, can actually happen if a "longlived" parent crashes.

So move the map/update size/remap before the limit check.
(cherry picked from commit e7e86fcb929e7b8e7d879349d5f7f9422126a3a2)

lib/tdb/common/io.c

index 78bbf2ec777a0517c404ee7c1ad66b0c7314df17..649b70f639fb80a838ad961050b65855fc900872 100644 (file)
@@ -53,6 +53,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
                return -1;
        }
 
+       /* Unmap, update size, remap */
+       if (tdb_munmap(tdb) == -1) {
+               tdb->ecode = TDB_ERR_IO;
+               return -1;
+       }
+       tdb->map_size = st.st_size;
+       tdb_mmap(tdb);
+
        if (st.st_size < (size_t)len) {
                if (!probe) {
                        /* Ensure ecode is set for log fn. */
@@ -63,13 +71,6 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
                return -1;
        }
 
-       /* Unmap, update size, remap */
-       if (tdb_munmap(tdb) == -1) {
-               tdb->ecode = TDB_ERR_IO;
-               return -1;
-       }
-       tdb->map_size = st.st_size;
-       tdb_mmap(tdb);
        return 0;
 }