2 Unix SMB/CIFS implementation.
4 Generic, persistent and shared between processes cache mechanism for use
5 by various parts of the Samba code
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
25 #include "system/filesys.h"
26 #include "system/glob.h"
30 #define DBGC_CLASS DBGC_TDB
32 #define TIMEOUT_LEN 12
33 #define CACHE_DATA_FMT "%12u/"
34 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
35 #define BLOB_TYPE "DATA_BLOB"
36 #define BLOB_TYPE_LEN 9
38 static struct tdb_context *cache;
39 static struct tdb_context *cache_notrans;
43 * @brief Generic, persistent and shared between processes cache mechanism
44 * for use by various parts of the Samba code
50 * Cache initialisation function. Opens cache tdb file or creates
51 * it if does not exist.
53 * @return true on successful initialisation of the cache or
57 static bool gencache_init(void)
59 char* cache_fname = NULL;
60 int open_flags = O_RDWR|O_CREAT;
62 /* skip file open if it's already opened */
63 if (cache) return True;
65 cache_fname = cache_path("gencache.tdb");
67 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
69 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT|TDB_INCOMPATIBLE_HASH, open_flags, 0644);
72 ret = tdb_check(cache, NULL, NULL);
77 * Retry with CLEAR_IF_FIRST.
79 * Warning: Converting this to dbwrap won't work
80 * directly. gencache.c does transactions on this tdb,
81 * and dbwrap forbids this for CLEAR_IF_FIRST
82 * databases. tdb does allow transactions on
83 * CLEAR_IF_FIRST databases, so lets use it here to
84 * clean up a broken database.
86 cache = tdb_open_log(cache_fname, 0,
88 TDB_INCOMPATIBLE_HASH|
94 if (!cache && (errno == EACCES)) {
95 open_flags = O_RDONLY;
96 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT|TDB_INCOMPATIBLE_HASH, open_flags,
99 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
104 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
108 cache_fname = lock_path("gencache_notrans.tdb");
110 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
112 cache_notrans = tdb_open_log(cache_fname, 0,
114 TDB_INCOMPATIBLE_HASH|
117 if (cache_notrans == NULL) {
118 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
128 static TDB_DATA last_stabilize_key(void)
131 result.dptr = discard_const_p(uint8_t, "@LAST_STABILIZED");
136 struct gencache_have_val_state {
138 const DATA_BLOB *data;
142 static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
145 struct gencache_have_val_state *state =
146 (struct gencache_have_val_state *)private_data;
147 time_t now = time(NULL);
148 int cache_time_left, new_time_left, additional_time;
151 * Excuse the many variables, but these time calculations are
152 * confusing to me. We do not want to write to gencache with a
153 * possibly expensive transaction if we are about to write the same
154 * value, just extending the remaining timeout by less than 10%.
157 cache_time_left = old_timeout - now;
158 if (cache_time_left <= 0) {
160 * timed out, write new value
165 new_time_left = state->new_timeout - now;
166 if (new_time_left <= 0) {
168 * Huh -- no new timeout?? Write it.
173 if (new_time_left < cache_time_left) {
175 * Someone wants to shorten the timeout. Let it happen.
181 * By how much does the new timeout extend the remaining cache time?
183 additional_time = new_time_left - cache_time_left;
185 if (additional_time * 10 < 0) {
187 * Integer overflow. We extend by so much that we have to write it.
193 * The comparison below is essentially equivalent to
195 * new_time_left > cache_time_left * 1.10
197 * but without floating point calculations.
200 if (additional_time * 10 > cache_time_left) {
202 * We extend the cache timeout by more than 10%. Do it.
208 * Now the more expensive data compare.
210 if (data_blob_cmp(state->data, &data) != 0) {
212 * Write a new value. Certainly do it.
218 * Extending the timeout by less than 10% for the same cache value is
219 * not worth the trouble writing a value into gencache under a
220 * possibly expensive transaction.
225 static bool gencache_have_val(const char *keystr, const DATA_BLOB *data,
228 struct gencache_have_val_state state;
230 state.new_timeout = timeout;
234 if (!gencache_parse(keystr, gencache_have_val_parser, &state)) {
241 * Set an entry in the cache file. If there's no such
244 * @param keystr string that represents a key of this entry
245 * @param blob DATA_BLOB value being cached
246 * @param timeout time when the value is expired
248 * @retval true when entry is successfuly stored
249 * @retval false on failure
252 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
258 time_t last_stabilize;
259 static int writecount;
261 if (tdb_data_cmp(string_term_tdb_data(keystr),
262 last_stabilize_key()) == 0) {
263 DEBUG(10, ("Can't store %s as a key\n", keystr));
267 if ((keystr == NULL) || (blob == NULL)) {
271 if (!gencache_init()) return False;
273 if (gencache_have_val(keystr, blob, timeout)) {
274 DEBUG(10, ("Did not store value for %s, we already got it\n",
279 val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
283 val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
287 val = (char *)talloc_append_blob(NULL, val, *blob);
292 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
293 "[%s] (%d seconds %s)\n", keystr,
294 timestring(talloc_tos(), timeout),
295 (int)(timeout - time(NULL)),
296 timeout > time(NULL) ? "ahead" : "in the past"));
298 ret = tdb_store_bystring(
299 cache_notrans, keystr,
300 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
309 * Every 100 writes within a single process, stabilize the cache with
310 * a transaction. This is done to prevent a single transaction to
311 * become huge and chew lots of memory.
314 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
315 gencache_stabilize();
321 * Every 5 minutes, call gencache_stabilize() to not let grow
322 * gencache_notrans.tdb too large.
326 databuf = tdb_fetch_compat(cache_notrans, last_stabilize_key());
327 if ((databuf.dptr != NULL)
328 && (databuf.dptr[databuf.dsize-1] == '\0')) {
329 last_stabilize = atoi((char *)databuf.dptr);
330 SAFE_FREE(databuf.dptr);
333 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
335 gencache_stabilize();
343 * Delete one entry from the cache file.
345 * @param keystr string that represents a key of this entry
347 * @retval true upon successful deletion
348 * @retval false in case of failure
351 bool gencache_del(const char *keystr)
353 bool exists, was_expired;
357 if (keystr == NULL) {
361 if (!gencache_init()) return False;
363 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr));
366 * We delete an element by setting its timeout to 0. This way we don't
367 * have to do a transaction on gencache.tdb every time we delete an
371 exists = gencache_get_data_blob(keystr, NULL, &value, NULL,
374 if (!exists && was_expired) {
376 * gencache_get_data_blob has implicitly deleted this
377 * entry, so we have to return success here.
383 data_blob_free(&value);
384 ret = gencache_set(keystr, "", 0);
389 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
398 res = strtol(val, &endptr, 10);
400 if ((endptr == NULL) || (*endptr != '/')) {
401 DEBUG(2, ("Invalid gencache data format: %s\n", val));
407 if (pendptr != NULL) {
413 struct gencache_parse_state {
414 void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
418 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
420 struct gencache_parse_state *state;
426 if (data.dptr == NULL) {
429 ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
433 state = (struct gencache_parse_state *)private_data;
434 blob = data_blob_const(
435 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
436 state->parser(t, blob, state->private_data);
440 bool gencache_parse(const char *keystr,
441 void (*parser)(time_t timeout, DATA_BLOB blob,
445 struct gencache_parse_state state;
449 if (keystr == NULL) {
452 if (tdb_data_cmp(string_term_tdb_data(keystr),
453 last_stabilize_key()) == 0) {
456 if (!gencache_init()) {
460 key = string_term_tdb_data(keystr);
461 state.parser = parser;
462 state.private_data = private_data;
464 ret = tdb_parse_record(cache_notrans, key, gencache_parse_fn, &state);
468 ret = tdb_parse_record(cache, key, gencache_parse_fn, &state);
472 struct gencache_get_data_blob_state {
479 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
482 struct gencache_get_data_blob_state *state =
483 (struct gencache_get_data_blob_state *)private_data;
486 state->result = false;
489 state->timeout = timeout;
491 if (state->blob == NULL) {
492 state->result = true;
496 *state->blob = data_blob_talloc(state->mem_ctx, blob.data,
498 if (state->blob->data == NULL) {
499 state->result = false;
502 state->result = true;
506 * Get existing entry from the cache file.
508 * @param keystr string that represents a key of this entry
509 * @param blob DATA_BLOB that is filled with entry's blob
510 * @param timeout pointer to a time_t that is filled with entry's
513 * @retval true when entry is successfuly fetched
514 * @retval False for failure
517 bool gencache_get_data_blob(const char *keystr, TALLOC_CTX *mem_ctx,
519 time_t *timeout, bool *was_expired)
521 struct gencache_get_data_blob_state state;
522 bool expired = false;
524 state.result = false;
525 state.mem_ctx = mem_ctx;
528 if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
534 if (state.timeout <= time(NULL)) {
536 * We're expired, delete the entry. We can't use gencache_del
537 * here, because that uses gencache_get_data_blob for checking
538 * the existence of a record. We know the thing exists and
539 * directly store an empty value with 0 timeout.
541 gencache_set(keystr, "", 0);
546 *timeout = state.timeout;
552 if (was_expired != NULL) {
553 *was_expired = expired;
555 if (state.result && state.blob) {
556 data_blob_free(state.blob);
561 struct stabilize_state {
565 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
571 * Migrate the clear-if-first gencache data to the stable,
572 * transaction-based gencache.tdb
575 bool gencache_stabilize(void)
577 struct stabilize_state state;
581 if (!gencache_init()) {
585 res = tdb_transaction_start_nonblock(cache);
587 if (tdb_error(cache) == TDB_ERR_NOLOCK)
590 * Someone else already does the stabilize,
591 * this does not have to be done twice
596 DEBUG(10, ("Could not start transaction on gencache.tdb: "
597 "%s\n", tdb_errorstr_compat(cache)));
600 res = tdb_transaction_start(cache_notrans);
602 tdb_transaction_cancel(cache);
603 DEBUG(10, ("Could not start transaction on "
604 "gencache_notrans.tdb: %s\n",
605 tdb_errorstr_compat(cache_notrans)));
610 state.written = false;
612 res = tdb_traverse(cache_notrans, stabilize_fn, &state);
613 if ((res < 0) || state.error) {
614 tdb_transaction_cancel(cache_notrans);
615 tdb_transaction_cancel(cache);
619 if (!state.written) {
620 tdb_transaction_cancel(cache_notrans);
621 tdb_transaction_cancel(cache);
625 res = tdb_transaction_commit(cache);
627 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
628 "%s\n", tdb_errorstr_compat(cache)));
629 tdb_transaction_cancel(cache_notrans);
633 res = tdb_transaction_commit(cache_notrans);
635 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
636 "%s\n", tdb_errorstr_compat(cache)));
640 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
642 tdb_store(cache_notrans, last_stabilize_key(),
643 string_term_tdb_data(now), 0);
650 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
653 struct stabilize_state *state = (struct stabilize_state *)priv;
657 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
661 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
662 DEBUG(10, ("Ignoring invalid entry\n"));
665 if ((timeout < time(NULL)) || (val.dsize == 0)) {
666 res = tdb_delete(cache, key);
667 if ((res != 0) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
670 state->written = true;
673 res = tdb_store(cache, key, val, 0);
675 state->written = true;
680 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
681 tdb_errorstr_compat(cache)));
686 if (tdb_delete(cache_notrans, key) != 0) {
687 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
688 "%s\n", tdb_errorstr_compat(cache_notrans)));
696 * Get existing entry from the cache file.
698 * @param keystr string that represents a key of this entry
699 * @param valstr buffer that is allocated and filled with the entry value
700 * buffer's disposing must be done outside
701 * @param timeout pointer to a time_t that is filled with entry's
704 * @retval true when entry is successfuly fetched
705 * @retval False for failure
708 bool gencache_get(const char *keystr, TALLOC_CTX *mem_ctx, char **value,
714 ret = gencache_get_data_blob(keystr, mem_ctx, &blob, ptimeout, NULL);
718 if ((blob.data == NULL) || (blob.length == 0)) {
719 data_blob_free(&blob);
722 if (blob.data[blob.length-1] != '\0') {
723 /* Not NULL terminated, can't be a string */
724 data_blob_free(&blob);
728 *value = talloc_move(mem_ctx, (char **)&blob.data);
731 data_blob_free(&blob);
736 * Set an entry in the cache file. If there's no such
739 * @param keystr string that represents a key of this entry
740 * @param value text representation value being cached
741 * @param timeout time when the value is expired
743 * @retval true when entry is successfuly stored
744 * @retval false on failure
747 bool gencache_set(const char *keystr, const char *value, time_t timeout)
749 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
750 return gencache_set_data_blob(keystr, &blob, timeout);
753 struct gencache_iterate_blobs_state {
754 void (*fn)(const char *key, DATA_BLOB value,
755 time_t timeout, void *private_data);
761 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
762 TDB_DATA data, void *priv)
764 struct gencache_iterate_blobs_state *state =
765 (struct gencache_iterate_blobs_state *)priv;
767 char *free_key = NULL;
771 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
774 if (state->in_persistent && tdb_exists(cache_notrans, key)) {
778 if (key.dptr[key.dsize-1] == '\0') {
779 keystr = (char *)key.dptr;
781 /* ensure 0-termination */
782 keystr = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
784 if (keystr == NULL) {
789 if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
794 if (fnmatch(state->pattern, keystr, 0) != 0) {
798 DEBUG(10, ("Calling function with arguments "
799 "(key=[%s], timeout=[%s])\n",
800 keystr, timestring(talloc_tos(), timeout)));
803 data_blob_const(endptr,
804 data.dsize - PTR_DIFF(endptr, data.dptr)),
805 timeout, state->private_data);
808 TALLOC_FREE(free_key);
812 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
813 time_t timeout, void *private_data),
814 void *private_data, const char *pattern)
816 struct gencache_iterate_blobs_state state;
818 if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
822 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
825 state.pattern = pattern;
826 state.private_data = private_data;
828 state.in_persistent = false;
829 tdb_traverse(cache_notrans, gencache_iterate_blobs_fn, &state);
831 state.in_persistent = true;
832 tdb_traverse(cache, gencache_iterate_blobs_fn, &state);
836 * Iterate through all entries which key matches to specified pattern
838 * @param fn pointer to the function that will be supplied with each single
839 * matching cache entry (key, value and timeout) as an arguments
840 * @param data void pointer to an arbitrary data that is passed directly to the fn
841 * function on each call
842 * @param keystr_pattern pattern the existing entries' keys are matched to
846 struct gencache_iterate_state {
847 void (*fn)(const char *key, const char *value, time_t timeout,
852 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
853 time_t timeout, void *private_data)
855 struct gencache_iterate_state *state =
856 (struct gencache_iterate_state *)private_data;
858 char *free_val = NULL;
860 if (value.data[value.length-1] == '\0') {
861 valstr = (char *)value.data;
863 /* ensure 0-termination */
864 valstr = talloc_strndup(talloc_tos(), (char *)value.data, value.length);
866 if (valstr == NULL) {
871 DEBUG(10, ("Calling function with arguments "
872 "(key=[%s], value=[%s], timeout=[%s])\n",
873 key, valstr, timestring(talloc_tos(), timeout)));
875 state->fn(key, valstr, timeout, state->private_data);
879 TALLOC_FREE(free_val);
882 void gencache_iterate(void (*fn)(const char *key, const char *value,
883 time_t timeout, void *dptr),
884 void *private_data, const char *pattern)
886 struct gencache_iterate_state state;
892 state.private_data = private_data;
893 gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);