]> git.ozlabs.org Git - ccan/blob - ccan/tdb/freelist.c
Fix sequence logic bug, and satisfies() logic fix/cleanup.
[ccan] / ccan / tdb / freelist.c
1  /* 
2    Unix SMB/CIFS implementation.
3
4    trivial database library
5
6    Copyright (C) Andrew Tridgell              1999-2005
7    Copyright (C) Paul `Rusty' Russell              2000
8    Copyright (C) Jeremy Allison                    2000-2003
9    
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13    
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include "tdb_private.h"
29
30 /* 'right' merges can involve O(n^2) cost when combined with a
31    traverse, so they are disabled until we find a way to do them in 
32    O(1) time
33 */
34 #define USE_RIGHT_MERGES 0
35
36 /* read a freelist record and check for simple errors */
37 int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec)
38 {
39         if (tdb->methods->tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
40                 return -1;
41
42         if (rec->magic == TDB_MAGIC) {
43                 /* this happens when a app is showdown while deleting a record - we should
44                    not completely fail when this happens */
45                 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read non-free magic 0x%x at offset=%d - fixing\n", 
46                          rec->magic, off));
47                 rec->magic = TDB_FREE_MAGIC;
48                 if (tdb->methods->tdb_write(tdb, off, rec, sizeof(*rec)) == -1)
49                         return -1;
50         }
51
52         if (rec->magic != TDB_FREE_MAGIC) {
53                 /* Ensure ecode is set for log fn. */
54                 tdb->ecode = TDB_ERR_CORRUPT;
55                 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read bad magic 0x%x at offset=%d\n", 
56                            rec->magic, off));
57                 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
58         }
59         if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
60                 return -1;
61         return 0;
62 }
63
64
65 #if USE_RIGHT_MERGES
66 /* Remove an element from the freelist.  Must have alloc lock. */
67 static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_t next)
68 {
69         tdb_off_t last_ptr, i;
70
71         /* read in the freelist top */
72         last_ptr = FREELIST_TOP;
73         while (tdb_ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
74                 if (i == off) {
75                         /* We've found it! */
76                         return tdb_ofs_write(tdb, last_ptr, &next);
77                 }
78                 /* Follow chain (next offset is at start of record) */
79                 last_ptr = i;
80         }
81         TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off));
82         return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
83 }
84 #endif
85
86
87 /* update a record tailer (must hold allocation lock) */
88 static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
89                          const struct list_struct *rec)
90 {
91         tdb_off_t totalsize;
92
93         /* Offset of tailer from record header */
94         totalsize = sizeof(*rec) + rec->rec_len;
95         return tdb_ofs_write(tdb, offset + totalsize - sizeof(tdb_off_t),
96                          &totalsize);
97 }
98
99 /* Add an element into the freelist. Merge adjacent records if
100    neccessary. */
101 int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
102 {
103         /* Allocation and tailer lock */
104         if (tdb_lock(tdb, -1, F_WRLCK) != 0)
105                 return -1;
106
107         /* set an initial tailer, so if we fail we don't leave a bogus record */
108         if (update_tailer(tdb, offset, rec) != 0) {
109                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed!\n"));
110                 goto fail;
111         }
112
113 #if USE_RIGHT_MERGES
114         /* Look right first (I'm an Australian, dammit) */
115         if (offset + sizeof(*rec) + rec->rec_len + sizeof(*rec) <= tdb->map_size) {
116                 tdb_off_t right = offset + sizeof(*rec) + rec->rec_len;
117                 struct list_struct r;
118
119                 if (tdb->methods->tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
120                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right read failed at %u\n", right));
121                         goto left;
122                 }
123
124                 /* If it's free, expand to include it. */
125                 if (r.magic == TDB_FREE_MAGIC) {
126                         if (remove_from_freelist(tdb, right, r.next) == -1) {
127                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right free failed at %u\n", right));
128                                 goto left;
129                         }
130                         rec->rec_len += sizeof(r) + r.rec_len;
131                         if (update_tailer(tdb, offset, rec) == -1) {
132                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed at %u\n", offset));
133                                 goto fail;
134                         }
135                 }
136         }
137 left:
138 #endif
139
140         /* Look left */
141         if (offset - sizeof(tdb_off_t) > TDB_DATA_START(tdb->header.hash_size)) {
142                 tdb_off_t left = offset - sizeof(tdb_off_t);
143                 struct list_struct l;
144                 tdb_off_t leftsize;
145                 
146                 /* Read in tailer and jump back to header */
147                 if (tdb_ofs_read(tdb, left, &leftsize) == -1) {
148                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left offset read failed at %u\n", left));
149                         goto update;
150                 }
151
152                 /* it could be uninitialised data */
153                 if (leftsize == 0 || leftsize == TDB_PAD_U32) {
154                         goto update;
155                 }
156
157                 left = offset - leftsize;
158
159                 if (leftsize > offset ||
160                     left < TDB_DATA_START(tdb->header.hash_size)) {
161                         goto update;
162                 }
163
164                 /* Now read in the left record */
165                 if (tdb->methods->tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
166                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
167                         goto update;
168                 }
169
170                 /* If it's free, expand to include it. */
171                 if (l.magic == TDB_FREE_MAGIC) {
172                         /* we now merge the new record into the left record, rather than the other 
173                            way around. This makes the operation O(1) instead of O(n). This change
174                            prevents traverse from being O(n^2) after a lot of deletes */
175                         l.rec_len += sizeof(*rec) + rec->rec_len;
176                         if (tdb_rec_write(tdb, left, &l) == -1) {
177                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_left failed at %u\n", left));
178                                 goto fail;
179                         }
180                         if (update_tailer(tdb, left, &l) == -1) {
181                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed at %u\n", offset));
182                                 goto fail;
183                         }
184                         tdb_unlock(tdb, -1, F_WRLCK);
185                         return 0;
186                 }
187         }
188
189 update:
190
191         /* Now, prepend to free list */
192         rec->magic = TDB_FREE_MAGIC;
193
194         if (tdb_ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
195             tdb_rec_write(tdb, offset, rec) == -1 ||
196             tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
197                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free record write failed at offset=%d\n", offset));
198                 goto fail;
199         }
200
201         /* And we're done. */
202         tdb_unlock(tdb, -1, F_WRLCK);
203         return 0;
204
205  fail:
206         tdb_unlock(tdb, -1, F_WRLCK);
207         return -1;
208 }
209
210
211
212 /* 
213    the core of tdb_allocate - called when we have decided which
214    free list entry to use
215
216    Note that we try to allocate by grabbing data from the end of an existing record,
217    not the beginning. This is so the left merge in a free is more likely to be
218    able to free up the record without fragmentation
219  */
220 static tdb_off_t tdb_allocate_ofs(struct tdb_context *tdb, 
221                                   tdb_len_t length, tdb_off_t rec_ptr,
222                                   struct list_struct *rec, tdb_off_t last_ptr)
223 {
224 #define MIN_REC_SIZE (sizeof(struct list_struct) + sizeof(tdb_off_t) + 8)
225
226         if (rec->rec_len < length + MIN_REC_SIZE) {
227                 /* we have to grab the whole record */
228
229                 /* unlink it from the previous record */
230                 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1) {
231                         return 0;
232                 }
233
234                 /* mark it not free */
235                 rec->magic = TDB_MAGIC;
236                 if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
237                         return 0;
238                 }
239                 return rec_ptr;
240         }
241
242         /* we're going to just shorten the existing record */
243         rec->rec_len -= (length + sizeof(*rec));
244         if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
245                 return 0;
246         }
247         if (update_tailer(tdb, rec_ptr, rec) == -1) {
248                 return 0;
249         }
250
251         /* and setup the new record */
252         rec_ptr += sizeof(*rec) + rec->rec_len; 
253
254         memset(rec, '\0', sizeof(*rec));
255         rec->rec_len = length;
256         rec->magic = TDB_MAGIC;
257
258         if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
259                 return 0;
260         }
261
262         if (update_tailer(tdb, rec_ptr, rec) == -1) {
263                 return 0;
264         }
265
266         return rec_ptr;
267 }
268
269 /* allocate some space from the free list. The offset returned points
270    to a unconnected list_struct within the database with room for at
271    least length bytes of total data
272
273    0 is returned if the space could not be allocated
274  */
275 tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec)
276 {
277         tdb_off_t rec_ptr, last_ptr, newrec_ptr;
278         struct {
279                 tdb_off_t rec_ptr, last_ptr;
280                 tdb_len_t rec_len;
281         } bestfit;
282         float multiplier = 1.0;
283
284         if (tdb_lock(tdb, -1, F_WRLCK) == -1)
285                 return 0;
286
287         /* Extra bytes required for tailer */
288         length += sizeof(tdb_off_t);
289         length = TDB_ALIGN(length, TDB_ALIGNMENT);
290
291  again:
292         last_ptr = FREELIST_TOP;
293
294         /* read in the freelist top */
295         if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
296                 goto fail;
297
298         bestfit.rec_ptr = 0;
299         bestfit.last_ptr = 0;
300         bestfit.rec_len = 0;
301
302         /* 
303            this is a best fit allocation strategy. Originally we used
304            a first fit strategy, but it suffered from massive fragmentation
305            issues when faced with a slowly increasing record size.
306          */
307         while (rec_ptr) {
308                 if (tdb_rec_free_read(tdb, rec_ptr, rec) == -1) {
309                         goto fail;
310                 }
311
312                 if (rec->rec_len >= length) {
313                         if (bestfit.rec_ptr == 0 ||
314                             rec->rec_len < bestfit.rec_len) {
315                                 bestfit.rec_len = rec->rec_len;
316                                 bestfit.rec_ptr = rec_ptr;
317                                 bestfit.last_ptr = last_ptr;
318                         }
319                 }
320
321                 /* move to the next record */
322                 last_ptr = rec_ptr;
323                 rec_ptr = rec->next;
324
325                 /* if we've found a record that is big enough, then
326                    stop searching if its also not too big. The
327                    definition of 'too big' changes as we scan
328                    through */
329                 if (bestfit.rec_len > 0 &&
330                     bestfit.rec_len < length * multiplier) {
331                         break;
332                 }
333                 
334                 /* this multiplier means we only extremely rarely
335                    search more than 50 or so records. At 50 records we
336                    accept records up to 11 times larger than what we
337                    want */
338                 multiplier *= 1.05;
339         }
340
341         if (bestfit.rec_ptr != 0) {
342                 if (tdb_rec_free_read(tdb, bestfit.rec_ptr, rec) == -1) {
343                         goto fail;
344                 }
345
346                 newrec_ptr = tdb_allocate_ofs(tdb, length, bestfit.rec_ptr, 
347                                               rec, bestfit.last_ptr);
348                 tdb_unlock(tdb, -1, F_WRLCK);
349                 return newrec_ptr;
350         }
351
352         /* we didn't find enough space. See if we can expand the
353            database and if we can then try again */
354         if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
355                 goto again;
356  fail:
357         tdb_unlock(tdb, -1, F_WRLCK);
358         return 0;
359 }
360
361
362
363 /* 
364    return the size of the freelist - used to decide if we should repack 
365 */
366 int tdb_freelist_size(struct tdb_context *tdb)
367 {
368         tdb_off_t ptr;
369         int count=0;
370
371         if (tdb_lock(tdb, -1, F_RDLCK) == -1) {
372                 return -1;
373         }
374
375         ptr = FREELIST_TOP;
376         while (tdb_ofs_read(tdb, ptr, &ptr) == 0 && ptr != 0) {
377                 count++;
378         }
379
380         tdb_unlock(tdb, -1, F_RDLCK);
381         return count;
382 }