s3: let gencache_init() use tdb_check()
[ira/wip.git] / source3 / lib / gencache.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Generic, persistent and shared between processes cache mechanism for use
5    by various parts of the Samba code
6
7    Copyright (C) Rafal Szczesniak    2002
8    Copyright (C) Volker Lendecke     2009
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25
26 #undef  DBGC_CLASS
27 #define DBGC_CLASS DBGC_TDB
28
29 #define TIMEOUT_LEN 12
30 #define CACHE_DATA_FMT  "%12u/"
31 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
32 #define BLOB_TYPE "DATA_BLOB"
33 #define BLOB_TYPE_LEN 9
34
35 static struct tdb_context *cache;
36 static struct tdb_context *cache_notrans;
37
38 /**
39  * @file gencache.c
40  * @brief Generic, persistent and shared between processes cache mechanism
41  *        for use by various parts of the Samba code
42  *
43  **/
44
45
46 /**
47  * Cache initialisation function. Opens cache tdb file or creates
48  * it if does not exist.
49  *
50  * @return true on successful initialisation of the cache or
51  *         false on failure
52  **/
53
54 static bool gencache_init(void)
55 {
56         char* cache_fname = NULL;
57         int open_flags = O_RDWR|O_CREAT;
58         bool first_try = true;
59
60         /* skip file open if it's already opened */
61         if (cache) return True;
62
63         cache_fname = lock_path("gencache.tdb");
64
65         DEBUG(5, ("Opening cache file at %s\n", cache_fname));
66
67 again:
68         cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, open_flags, 0644);
69         if (cache) {
70                 int ret;
71                 ret = tdb_check(cache, NULL, NULL);
72                 if (ret != 0) {
73                         tdb_close(cache);
74                         cache = NULL;
75                         if (!first_try) {
76                                 DEBUG(0, ("gencache_init: tdb_check(%s) failed\n",
77                                           cache_fname));
78                                 return false;
79                         }
80                         first_try = false;
81                         DEBUG(0, ("gencache_init: tdb_check(%s) failed - retry after CLEAR_IF_FIRST\n",
82                                   cache_fname));
83                         cache = tdb_open_log(cache_fname, 0, TDB_CLEAR_IF_FIRST, open_flags, 0644);
84                         if (cache) {
85                                 tdb_close(cache);
86                                 cache = NULL;
87                                 goto again;
88                         }
89                 }
90         }
91
92         if (!cache && (errno == EACCES)) {
93                 open_flags = O_RDONLY;
94                 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, open_flags,
95                                      0644);
96                 if (cache) {
97                         DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
98                 }
99         }
100
101         if (!cache) {
102                 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
103                 return False;
104         }
105
106         cache_fname = lock_path("gencache_notrans.tdb");
107
108         DEBUG(5, ("Opening cache file at %s\n", cache_fname));
109
110         cache_notrans = tdb_open_log(cache_fname, 0, TDB_CLEAR_IF_FIRST,
111                                      open_flags, 0644);
112         if (cache_notrans == NULL) {
113                 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
114                           strerror(errno)));
115                 tdb_close(cache);
116                 cache = NULL;
117                 return false;
118         }
119
120         return True;
121 }
122
123 static TDB_DATA last_stabilize_key(void)
124 {
125         TDB_DATA result;
126         result.dptr = (uint8_t *)"@LAST_STABILIZED";
127         result.dsize = 17;
128         return result;
129 }
130
131 /**
132  * Set an entry in the cache file. If there's no such
133  * one, then add it.
134  *
135  * @param keystr string that represents a key of this entry
136  * @param blob DATA_BLOB value being cached
137  * @param timeout time when the value is expired
138  *
139  * @retval true when entry is successfuly stored
140  * @retval false on failure
141  **/
142
143 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
144                             time_t timeout)
145 {
146         int ret;
147         TDB_DATA databuf;
148         char* val;
149         time_t last_stabilize;
150         static int writecount;
151
152         if (tdb_data_cmp(string_term_tdb_data(keystr),
153                          last_stabilize_key()) == 0) {
154                 DEBUG(10, ("Can't store %s as a key\n", keystr));
155                 return false;
156         }
157
158         if ((keystr == NULL) || (blob == NULL)) {
159                 return false;
160         }
161
162         if (!gencache_init()) return False;
163
164         val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
165         if (val == NULL) {
166                 return False;
167         }
168         val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
169         if (val == NULL) {
170                 return false;
171         }
172         val = (char *)talloc_append_blob(NULL, val, *blob);
173         if (val == NULL) {
174                 return false;
175         }
176
177         DEBUG(10, ("Adding cache entry with key = %s and timeout ="
178                    " %s (%d seconds %s)\n", keystr, ctime(&timeout),
179                    (int)(timeout - time(NULL)), 
180                    timeout > time(NULL) ? "ahead" : "in the past"));
181
182         ret = tdb_store_bystring(
183                 cache_notrans, keystr,
184                 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
185                 0);
186         TALLOC_FREE(val);
187
188         if (ret != 0) {
189                 return false;
190         }
191
192         /*
193          * Every 100 writes within a single process, stabilize the cache with
194          * a transaction. This is done to prevent a single transaction to
195          * become huge and chew lots of memory.
196          */
197         writecount += 1;
198         if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
199                 gencache_stabilize();
200                 writecount = 0;
201                 goto done;
202         }
203
204         /*
205          * Every 5 minutes, call gencache_stabilize() to not let grow
206          * gencache_notrans.tdb too large.
207          */
208
209         last_stabilize = 0;
210         databuf = tdb_fetch(cache_notrans, last_stabilize_key());
211         if ((databuf.dptr != NULL)
212             && (databuf.dptr[databuf.dsize-1] == '\0')) {
213                 last_stabilize = atoi((char *)databuf.dptr);
214                 SAFE_FREE(databuf.dptr);
215         }
216         if ((last_stabilize
217              + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
218             < time(NULL)) {
219                 gencache_stabilize();
220         }
221
222 done:
223         return ret == 0;
224 }
225
226 /**
227  * Delete one entry from the cache file.
228  *
229  * @param keystr string that represents a key of this entry
230  *
231  * @retval true upon successful deletion
232  * @retval false in case of failure
233  **/
234
235 bool gencache_del(const char *keystr)
236 {
237         bool exists, was_expired;
238         bool ret = false;
239         DATA_BLOB value;
240
241         if (keystr == NULL) {
242                 return false;
243         }
244
245         if (!gencache_init()) return False;     
246
247         DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
248
249         /*
250          * We delete an element by setting its timeout to 0. This way we don't
251          * have to do a transaction on gencache.tdb every time we delete an
252          * element.
253          */
254
255         exists = gencache_get_data_blob(keystr, &value, NULL, &was_expired);
256
257         if (!exists && was_expired) {
258                 /*
259                  * gencache_get_data_blob has implicitly deleted this
260                  * entry, so we have to return success here.
261                  */
262                 return true;
263         }
264
265         if (exists) {
266                 data_blob_free(&value);
267                 ret = gencache_set(keystr, "", 0);
268         }
269         return ret;
270 }
271
272 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
273 {
274         time_t res;
275         char *endptr;
276
277         res = strtol(val, &endptr, 10);
278
279         if ((endptr == NULL) || (*endptr != '/')) {
280                 DEBUG(2, ("Invalid gencache data format: %s\n", val));
281                 return false;
282         }
283         if (pres != NULL) {
284                 *pres = res;
285         }
286         if (pendptr != NULL) {
287                 *pendptr = endptr;
288         }
289         return true;
290 }
291
292 /**
293  * Get existing entry from the cache file.
294  *
295  * @param keystr string that represents a key of this entry
296  * @param blob DATA_BLOB that is filled with entry's blob
297  * @param timeout pointer to a time_t that is filled with entry's
298  *        timeout
299  *
300  * @retval true when entry is successfuly fetched
301  * @retval False for failure
302  **/
303
304 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob,
305                             time_t *timeout, bool *was_expired)
306 {
307         TDB_DATA databuf;
308         time_t t;
309         char *endptr;
310         bool expired = false;
311
312         if (keystr == NULL) {
313                 goto fail;
314         }
315
316         if (tdb_data_cmp(string_term_tdb_data(keystr),
317                          last_stabilize_key()) == 0) {
318                 DEBUG(10, ("Can't get %s as a key\n", keystr));
319                 goto fail;
320         }
321
322         if (!gencache_init()) {
323                 goto fail;
324         }
325
326         databuf = tdb_fetch_bystring(cache_notrans, keystr);
327
328         if (databuf.dptr == NULL) {
329                 databuf = tdb_fetch_bystring(cache, keystr);
330         }
331
332         if (databuf.dptr == NULL) {
333                 DEBUG(10, ("Cache entry with key = %s couldn't be found \n",
334                            keystr));
335                 goto fail;
336         }
337
338         if (!gencache_pull_timeout((char *)databuf.dptr, &t, &endptr)) {
339                 SAFE_FREE(databuf.dptr);
340                 goto fail;
341         }
342
343         DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
344                    "timeout = %s", t > time(NULL) ? "valid" :
345                    "expired", keystr, endptr+1, ctime(&t)));
346
347         if (t == 0) {
348                 /* Deleted */
349                 SAFE_FREE(databuf.dptr);
350                 goto fail;
351         }
352
353         if (t <= time(NULL)) {
354
355                 /*
356                  * We're expired, delete the entry. We can't use gencache_del
357                  * here, because that uses gencache_get_data_blob for checking
358                  * the existence of a record. We know the thing exists and
359                  * directly store an empty value with 0 timeout.
360                  */
361                 gencache_set(keystr, "", 0);
362
363                 SAFE_FREE(databuf.dptr);
364
365                 expired = true;
366                 goto fail;
367         }
368
369         if (blob != NULL) {
370                 *blob = data_blob(
371                         endptr+1,
372                         databuf.dsize - PTR_DIFF(endptr+1, databuf.dptr));
373                 if (blob->data == NULL) {
374                         SAFE_FREE(databuf.dptr);
375                         DEBUG(0, ("memdup failed\n"));
376                         goto fail;
377                 }
378         }
379
380         SAFE_FREE(databuf.dptr);
381
382         if (timeout) {
383                 *timeout = t;
384         }
385
386         return True;
387
388 fail:
389         if (was_expired != NULL) {
390                 *was_expired = expired;
391         }
392         return false;
393
394
395 struct stabilize_state {
396         bool written;
397         bool error;
398 };
399 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
400                         void *priv);
401
402 /**
403  * Stabilize gencache
404  *
405  * Migrate the clear-if-first gencache data to the stable,
406  * transaction-based gencache.tdb
407  */
408
409 bool gencache_stabilize(void)
410 {
411         struct stabilize_state state;
412         int res;
413         char *now;
414
415         if (!gencache_init()) {
416                 return false;
417         }
418
419         res = tdb_transaction_start(cache);
420         if (res == -1) {
421                 DEBUG(10, ("Could not start transaction on gencache.tdb: "
422                            "%s\n", tdb_errorstr(cache)));
423                 return false;
424         }
425         res = tdb_transaction_start(cache_notrans);
426         if (res == -1) {
427                 tdb_transaction_cancel(cache);
428                 DEBUG(10, ("Could not start transaction on "
429                            "gencache_notrans.tdb: %s\n",
430                            tdb_errorstr(cache_notrans)));
431                 return false;
432         }
433
434         state.error = false;
435         state.written = false;
436
437         res = tdb_traverse(cache_notrans, stabilize_fn, &state);
438         if ((res == -1) || state.error) {
439                 if ((tdb_transaction_cancel(cache_notrans) == -1)
440                     || (tdb_transaction_cancel(cache) == -1)) {
441                         smb_panic("tdb_transaction_cancel failed\n");
442                 }
443                 return false;
444         }
445
446         if (!state.written) {
447                 if ((tdb_transaction_cancel(cache_notrans) == -1)
448                     || (tdb_transaction_cancel(cache) == -1)) {
449                         smb_panic("tdb_transaction_cancel failed\n");
450                 }
451                 return true;
452         }
453
454         res = tdb_transaction_commit(cache);
455         if (res == -1) {
456                 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
457                            "%s\n", tdb_errorstr(cache)));
458                 if (tdb_transaction_cancel(cache_notrans) == -1) {
459                         smb_panic("tdb_transaction_cancel failed\n");
460                 }
461                 return false;
462         }
463
464         res = tdb_transaction_commit(cache_notrans);
465         if (res == -1) {
466                 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
467                            "%s\n", tdb_errorstr(cache)));
468                 return false;
469         }
470
471         now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
472         if (now != NULL) {
473                 tdb_store(cache_notrans, last_stabilize_key(),
474                           string_term_tdb_data(now), 0);
475                 TALLOC_FREE(now);
476         }
477
478         return true;
479 }
480
481 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
482                         void *priv)
483 {
484         struct stabilize_state *state = (struct stabilize_state *)priv;
485         int res;
486         time_t timeout;
487
488         if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
489                 return 0;
490         }
491
492         if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
493                 DEBUG(10, ("Ignoring invalid entry\n"));
494                 return 0;
495         }
496         if ((timeout < time(NULL)) || (val.dsize == 0)) {
497                 res = tdb_delete(cache, key);
498                 if ((res == -1) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
499                         res = 0;
500                 } else {
501                         state->written = true;
502                 }
503         } else {
504                 res = tdb_store(cache, key, val, 0);
505                 if (res == 0) {
506                         state->written = true;
507                 }
508         }
509
510         if (res == -1) {
511                 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
512                            tdb_errorstr(cache)));
513                 state->error = true;
514                 return -1;
515         }
516
517         if (tdb_delete(cache_notrans, key) == -1) {
518                 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
519                            "%s\n", tdb_errorstr(cache_notrans)));
520                 state->error = true;
521                 return -1;
522         }
523         return 0;
524 }
525
526 /**
527  * Get existing entry from the cache file.
528  *
529  * @param keystr string that represents a key of this entry
530  * @param valstr buffer that is allocated and filled with the entry value
531  *        buffer's disposing must be done outside
532  * @param timeout pointer to a time_t that is filled with entry's
533  *        timeout
534  *
535  * @retval true when entry is successfuly fetched
536  * @retval False for failure
537  **/
538
539 bool gencache_get(const char *keystr, char **value, time_t *ptimeout)
540 {
541         DATA_BLOB blob;
542         bool ret = False;
543
544         ret = gencache_get_data_blob(keystr, &blob, ptimeout, NULL);
545         if (!ret) {
546                 return false;
547         }
548         if ((blob.data == NULL) || (blob.length == 0)) {
549                 SAFE_FREE(blob.data);
550                 return false;
551         }
552         if (blob.data[blob.length-1] != '\0') {
553                 /* Not NULL terminated, can't be a string */
554                 SAFE_FREE(blob.data);
555                 return false;
556         }
557         if (value) {
558                 *value = SMB_STRDUP((char *)blob.data);
559                 data_blob_free(&blob);
560                 if (*value == NULL) {
561                         return false;
562                 }
563                 return true;
564         }
565         data_blob_free(&blob);
566         return true;
567 }
568
569 /**
570  * Set an entry in the cache file. If there's no such
571  * one, then add it.
572  *
573  * @param keystr string that represents a key of this entry
574  * @param value text representation value being cached
575  * @param timeout time when the value is expired
576  *
577  * @retval true when entry is successfuly stored
578  * @retval false on failure
579  **/
580
581 bool gencache_set(const char *keystr, const char *value, time_t timeout)
582 {
583         DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
584         return gencache_set_data_blob(keystr, &blob, timeout);
585 }
586
587 /**
588  * Iterate through all entries which key matches to specified pattern
589  *
590  * @param fn pointer to the function that will be supplied with each single
591  *        matching cache entry (key, value and timeout) as an arguments
592  * @param data void pointer to an arbitrary data that is passed directly to the fn
593  *        function on each call
594  * @param keystr_pattern pattern the existing entries' keys are matched to
595  *
596  **/
597
598 struct gencache_iterate_state {
599         void (*fn)(const char *key, const char *value, time_t timeout,
600                    void *priv);
601         const char *pattern;
602         void *priv;
603         bool in_persistent;
604 };
605
606 static int gencache_iterate_fn(struct tdb_context *tdb, TDB_DATA key,
607                                TDB_DATA value, void *priv)
608 {
609         struct gencache_iterate_state *state =
610                 (struct gencache_iterate_state *)priv;
611         char *keystr;
612         char *free_key = NULL;
613         char *valstr;
614         char *free_val = NULL;
615         unsigned long u;
616         time_t timeout;
617         char *timeout_endp;
618
619         if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
620                 return 0;
621         }
622
623         if (state->in_persistent && tdb_exists(cache_notrans, key)) {
624                 return 0;
625         }
626
627         if (key.dptr[key.dsize-1] == '\0') {
628                 keystr = (char *)key.dptr;
629         } else {
630                 /* ensure 0-termination */
631                 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
632                 free_key = keystr;
633         }
634
635         if ((value.dptr == NULL) || (value.dsize <= TIMEOUT_LEN)) {
636                 goto done;
637         }
638
639         if (fnmatch(state->pattern, keystr, 0) != 0) {
640                 goto done;
641         }
642
643         if (value.dptr[value.dsize-1] == '\0') {
644                 valstr = (char *)value.dptr;
645         } else {
646                 /* ensure 0-termination */
647                 valstr = SMB_STRNDUP((char *)value.dptr, value.dsize);
648                 free_val = valstr;
649         }
650
651         u = strtoul(valstr, &timeout_endp, 10);
652
653         if ((*timeout_endp != '/') || ((timeout_endp-valstr) != TIMEOUT_LEN)) {
654                 goto done;
655         }
656
657         timeout = u;
658         timeout_endp += 1;
659
660         DEBUG(10, ("Calling function with arguments "
661                    "(key = %s, value = %s, timeout = %s)\n",
662                    keystr, timeout_endp, ctime(&timeout)));
663         state->fn(keystr, timeout_endp, timeout, state->priv);
664
665  done:
666         SAFE_FREE(free_key);
667         SAFE_FREE(free_val);
668         return 0;
669 }
670
671 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
672                       void* data, const char* keystr_pattern)
673 {
674         struct gencache_iterate_state state;
675
676         if ((fn == NULL) || (keystr_pattern == NULL)) {
677                 return;
678         }
679
680         if (!gencache_init()) return;
681
682         DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
683
684         state.fn = fn;
685         state.pattern = keystr_pattern;
686         state.priv = data;
687
688         state.in_persistent = false;
689         tdb_traverse(cache_notrans, gencache_iterate_fn, &state);
690
691         state.in_persistent = true;
692         tdb_traverse(cache, gencache_iterate_fn, &state);
693 }