tdb: increase readability of read_record_on_left()
[samba.git] / lib / tdb / common / 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 tdb_record *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=%u - fixing\n",
46                          rec->magic, off));
47                 rec->magic = TDB_FREE_MAGIC;
48                 if (tdb_rec_write(tdb, off, 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=%u\n",
56                            rec->magic, off));
57                 return -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->ecode = TDB_ERR_CORRUPT;
82         TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%u\n", off));
83         return -1;
84 }
85 #endif
86
87
88 /* update a record tailer (must hold allocation lock) */
89 static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
90                          const struct tdb_record *rec)
91 {
92         tdb_off_t totalsize;
93
94         /* Offset of tailer from record header */
95         totalsize = sizeof(*rec) + rec->rec_len;
96         return tdb_ofs_write(tdb, offset + totalsize - sizeof(tdb_off_t),
97                          &totalsize);
98 }
99
100 /**
101  * Read the record directly on the left.
102  * Fail if there is no record on the left.
103  */
104 static int read_record_on_left(struct tdb_context *tdb, tdb_off_t rec_ptr,
105                                tdb_off_t *left_p,
106                                struct tdb_record *left_r)
107 {
108         tdb_off_t left_ptr;
109         tdb_off_t left_size;
110         struct tdb_record left_rec;
111         int ret;
112
113         left_ptr = rec_ptr - sizeof(tdb_off_t);
114
115         if (left_ptr <= TDB_DATA_START(tdb->hash_size)) {
116                 /* no record on the left */
117                 return -1;
118         }
119
120         /* Read in tailer and jump back to header */
121         ret = tdb_ofs_read(tdb, left_ptr, &left_size);
122         if (ret == -1) {
123                 TDB_LOG((tdb, TDB_DEBUG_FATAL,
124                         "tdb_free: left offset read failed at %u\n", left_ptr));
125                 return -1;
126         }
127
128         /* it could be uninitialised data */
129         if (left_size == 0 || left_size == TDB_PAD_U32) {
130                 return -1;
131         }
132
133         if (left_size > rec_ptr) {
134                 return -1;
135         }
136
137         left_ptr = rec_ptr - left_size;
138
139         if (left_ptr < TDB_DATA_START(tdb->hash_size)) {
140                 return -1;
141         }
142
143         /* Now read in the left record */
144         ret = tdb->methods->tdb_read(tdb, left_ptr, &left_rec,
145                                      sizeof(left_rec), DOCONV());
146         if (ret == -1) {
147                 TDB_LOG((tdb, TDB_DEBUG_FATAL,
148                          "tdb_free: left read failed at %u (%u)\n",
149                          left_ptr, left_size));
150                 return -1;
151         }
152
153         *left_p = left_ptr;
154         *left_r = left_rec;
155
156         return 0;
157 }
158
159 /* Add an element into the freelist. Merge adjacent records if
160    necessary. */
161 int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
162 {
163         tdb_off_t left;
164         struct tdb_record l;
165
166         /* Allocation and tailer lock */
167         if (tdb_lock(tdb, -1, F_WRLCK) != 0)
168                 return -1;
169
170         /* set an initial tailer, so if we fail we don't leave a bogus record */
171         if (update_tailer(tdb, offset, rec) != 0) {
172                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed!\n"));
173                 goto fail;
174         }
175
176 #if USE_RIGHT_MERGES
177         /* Look right first (I'm an Australian, dammit) */
178         if (offset + sizeof(*rec) + rec->rec_len + sizeof(*rec) <= tdb->map_size) {
179                 tdb_off_t right = offset + sizeof(*rec) + rec->rec_len;
180                 struct tdb_record r;
181
182                 if (tdb->methods->tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
183                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right read failed at %u\n", right));
184                         goto left;
185                 }
186
187                 /* If it's free, expand to include it. */
188                 if (r.magic == TDB_FREE_MAGIC) {
189                         if (remove_from_freelist(tdb, right, r.next) == -1) {
190                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right free failed at %u\n", right));
191                                 goto left;
192                         }
193                         rec->rec_len += sizeof(r) + r.rec_len;
194                         if (update_tailer(tdb, offset, rec) == -1) {
195                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed at %u\n", offset));
196                                 goto fail;
197                         }
198                 }
199         }
200 left:
201 #endif
202
203         if (read_record_on_left(tdb, offset, &left, &l) == 0) {
204                 /* If it's free, expand to include it. */
205                 if (l.magic == TDB_FREE_MAGIC) {
206                         /* we now merge the new record into the left record, rather than the other
207                            way around. This makes the operation O(1) instead of O(n). This change
208                            prevents traverse from being O(n^2) after a lot of deletes */
209                         l.rec_len += sizeof(*rec) + rec->rec_len;
210                         if (tdb_rec_write(tdb, left, &l) == -1) {
211                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_left failed at %u\n", left));
212                                 goto fail;
213                         }
214                         if (update_tailer(tdb, left, &l) == -1) {
215                                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed at %u\n", offset));
216                                 goto fail;
217                         }
218                         tdb_unlock(tdb, -1, F_WRLCK);
219                         return 0;
220                 }
221         }
222
223 update:
224
225         /* Now, prepend to free list */
226         rec->magic = TDB_FREE_MAGIC;
227
228         if (tdb_ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
229             tdb_rec_write(tdb, offset, rec) == -1 ||
230             tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
231                 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free record write failed at offset=%u\n", offset));
232                 goto fail;
233         }
234
235         /* And we're done. */
236         tdb_unlock(tdb, -1, F_WRLCK);
237         return 0;
238
239  fail:
240         tdb_unlock(tdb, -1, F_WRLCK);
241         return -1;
242 }
243
244
245
246 /*
247    the core of tdb_allocate - called when we have decided which
248    free list entry to use
249
250    Note that we try to allocate by grabbing data from the end of an existing record,
251    not the beginning. This is so the left merge in a free is more likely to be
252    able to free up the record without fragmentation
253  */
254 static tdb_off_t tdb_allocate_ofs(struct tdb_context *tdb,
255                                   tdb_len_t length, tdb_off_t rec_ptr,
256                                   struct tdb_record *rec, tdb_off_t last_ptr)
257 {
258 #define MIN_REC_SIZE (sizeof(struct tdb_record) + sizeof(tdb_off_t) + 8)
259
260         if (rec->rec_len < length + MIN_REC_SIZE) {
261                 /* we have to grab the whole record */
262
263                 /* unlink it from the previous record */
264                 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1) {
265                         return 0;
266                 }
267
268                 /* mark it not free */
269                 rec->magic = TDB_MAGIC;
270                 if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
271                         return 0;
272                 }
273                 return rec_ptr;
274         }
275
276         /* we're going to just shorten the existing record */
277         rec->rec_len -= (length + sizeof(*rec));
278         if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
279                 return 0;
280         }
281         if (update_tailer(tdb, rec_ptr, rec) == -1) {
282                 return 0;
283         }
284
285         /* and setup the new record */
286         rec_ptr += sizeof(*rec) + rec->rec_len;
287
288         memset(rec, '\0', sizeof(*rec));
289         rec->rec_len = length;
290         rec->magic = TDB_MAGIC;
291
292         if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
293                 return 0;
294         }
295
296         if (update_tailer(tdb, rec_ptr, rec) == -1) {
297                 return 0;
298         }
299
300         return rec_ptr;
301 }
302
303 /* allocate some space from the free list. The offset returned points
304    to a unconnected tdb_record within the database with room for at
305    least length bytes of total data
306
307    0 is returned if the space could not be allocated
308  */
309 static tdb_off_t tdb_allocate_from_freelist(
310         struct tdb_context *tdb, tdb_len_t length, struct tdb_record *rec)
311 {
312         tdb_off_t rec_ptr, last_ptr, newrec_ptr;
313         struct {
314                 tdb_off_t rec_ptr, last_ptr;
315                 tdb_len_t rec_len;
316         } bestfit;
317         float multiplier = 1.0;
318
319         /* over-allocate to reduce fragmentation */
320         length *= 1.25;
321
322         /* Extra bytes required for tailer */
323         length += sizeof(tdb_off_t);
324         length = TDB_ALIGN(length, TDB_ALIGNMENT);
325
326  again:
327         last_ptr = FREELIST_TOP;
328
329         /* read in the freelist top */
330         if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
331                 return 0;
332
333         bestfit.rec_ptr = 0;
334         bestfit.last_ptr = 0;
335         bestfit.rec_len = 0;
336
337         /*
338            this is a best fit allocation strategy. Originally we used
339            a first fit strategy, but it suffered from massive fragmentation
340            issues when faced with a slowly increasing record size.
341          */
342         while (rec_ptr) {
343                 if (tdb_rec_free_read(tdb, rec_ptr, rec) == -1) {
344                         return 0;
345                 }
346
347                 if (rec->rec_len >= length) {
348                         if (bestfit.rec_ptr == 0 ||
349                             rec->rec_len < bestfit.rec_len) {
350                                 bestfit.rec_len = rec->rec_len;
351                                 bestfit.rec_ptr = rec_ptr;
352                                 bestfit.last_ptr = last_ptr;
353                         }
354                 }
355
356                 /* move to the next record */
357                 last_ptr = rec_ptr;
358                 rec_ptr = rec->next;
359
360                 /* if we've found a record that is big enough, then
361                    stop searching if its also not too big. The
362                    definition of 'too big' changes as we scan
363                    through */
364                 if (bestfit.rec_len > 0 &&
365                     bestfit.rec_len < length * multiplier) {
366                         break;
367                 }
368
369                 /* this multiplier means we only extremely rarely
370                    search more than 50 or so records. At 50 records we
371                    accept records up to 11 times larger than what we
372                    want */
373                 multiplier *= 1.05;
374         }
375
376         if (bestfit.rec_ptr != 0) {
377                 if (tdb_rec_free_read(tdb, bestfit.rec_ptr, rec) == -1) {
378                         return 0;
379                 }
380
381                 newrec_ptr = tdb_allocate_ofs(tdb, length, bestfit.rec_ptr,
382                                               rec, bestfit.last_ptr);
383                 return newrec_ptr;
384         }
385
386         /* we didn't find enough space. See if we can expand the
387            database and if we can then try again */
388         if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
389                 goto again;
390
391         return 0;
392 }
393
394 static bool tdb_alloc_dead(
395         struct tdb_context *tdb, int hash, tdb_len_t length,
396         tdb_off_t *rec_ptr, struct tdb_record *rec)
397 {
398         tdb_off_t last_ptr;
399
400         *rec_ptr = tdb_find_dead(tdb, hash, rec, length, &last_ptr);
401         if (*rec_ptr == 0) {
402                 return false;
403         }
404         /*
405          * Unlink the record from the hash chain, it's about to be moved into
406          * another one.
407          */
408         return (tdb_ofs_write(tdb, last_ptr, &rec->next) == 0);
409 }
410
411 /*
412  * Chain "hash" is assumed to be locked
413  */
414
415 tdb_off_t tdb_allocate(struct tdb_context *tdb, int hash, tdb_len_t length,
416                        struct tdb_record *rec)
417 {
418         tdb_off_t ret;
419         int i;
420
421         if (tdb->max_dead_records == 0) {
422                 /*
423                  * No dead records to expect anywhere. Do the blocking
424                  * freelist lock without trying to steal from others
425                  */
426                 goto blocking_freelist_allocate;
427         }
428
429         /*
430          * The following loop tries to get the freelist lock nonblocking. If
431          * it gets the lock, allocate from there. If the freelist is busy,
432          * instead of waiting we try to steal dead records from other hash
433          * chains.
434          *
435          * Be aware that we do nonblocking locks on the other hash chains as
436          * well and fail gracefully. This way we avoid deadlocks (we block two
437          * hash chains, something which is pretty bad normally)
438          */
439
440         for (i=0; i<tdb->hash_size; i++) {
441
442                 int list;
443
444                 list = BUCKET(hash+i);
445
446                 if (tdb_lock_nonblock(tdb, list, F_WRLCK) == 0) {
447                         bool got_dead;
448
449                         got_dead = tdb_alloc_dead(tdb, list, length, &ret, rec);
450                         tdb_unlock(tdb, list, F_WRLCK);
451
452                         if (got_dead) {
453                                 return ret;
454                         }
455                 }
456
457                 if (tdb_lock_nonblock(tdb, -1, F_WRLCK) == 0) {
458                         /*
459                          * Under the freelist lock take the chance to give
460                          * back our dead records.
461                          */
462                         tdb_purge_dead(tdb, hash);
463
464                         ret = tdb_allocate_from_freelist(tdb, length, rec);
465                         tdb_unlock(tdb, -1, F_WRLCK);
466                         return ret;
467                 }
468         }
469
470 blocking_freelist_allocate:
471
472         if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
473                 return 0;
474         }
475         ret = tdb_allocate_from_freelist(tdb, length, rec);
476         tdb_unlock(tdb, -1, F_WRLCK);
477         return ret;
478 }
479
480 /*
481    return the size of the freelist - used to decide if we should repack
482 */
483 _PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb)
484 {
485         tdb_off_t ptr;
486         int count=0;
487
488         if (tdb_lock(tdb, -1, F_RDLCK) == -1) {
489                 return -1;
490         }
491
492         ptr = FREELIST_TOP;
493         while (tdb_ofs_read(tdb, ptr, &ptr) == 0 && ptr != 0) {
494                 count++;
495         }
496
497         tdb_unlock(tdb, -1, F_RDLCK);
498         return count;
499 }