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"
29 #define DBGC_CLASS DBGC_TDB
31 #define TIMEOUT_LEN 12
32 #define CACHE_DATA_FMT "%12u/"
33 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
34 #define BLOB_TYPE "DATA_BLOB"
35 #define BLOB_TYPE_LEN 9
37 static struct tdb_context *cache;
38 static struct tdb_context *cache_notrans;
42 * @brief Generic, persistent and shared between processes cache mechanism
43 * for use by various parts of the Samba code
49 * Cache initialisation function. Opens cache tdb file or creates
50 * it if does not exist.
52 * @return true on successful initialisation of the cache or
56 static bool gencache_init(void)
58 char* cache_fname = NULL;
59 int open_flags = O_RDWR|O_CREAT;
60 bool first_try = true;
62 /* skip file open if it's already opened */
63 if (cache) return True;
65 cache_fname = lock_path("gencache.tdb");
67 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
70 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT|TDB_INCOMPATIBLE_HASH, open_flags, 0644);
73 ret = tdb_check(cache, NULL, NULL);
78 DEBUG(0, ("gencache_init: tdb_check(%s) failed\n",
83 DEBUG(0, ("gencache_init: tdb_check(%s) failed - retry after CLEAR_IF_FIRST\n",
85 cache = tdb_open_log(cache_fname, 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, open_flags, 0644);
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, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
114 if (cache_notrans == NULL) {
115 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
125 static TDB_DATA last_stabilize_key(void)
128 result.dptr = (uint8_t *)"@LAST_STABILIZED";
134 * Set an entry in the cache file. If there's no such
137 * @param keystr string that represents a key of this entry
138 * @param blob DATA_BLOB value being cached
139 * @param timeout time when the value is expired
141 * @retval true when entry is successfuly stored
142 * @retval false on failure
145 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
151 time_t last_stabilize;
152 static int writecount;
154 if (tdb_data_cmp(string_term_tdb_data(keystr),
155 last_stabilize_key()) == 0) {
156 DEBUG(10, ("Can't store %s as a key\n", keystr));
160 if ((keystr == NULL) || (blob == NULL)) {
164 if (!gencache_init()) return False;
166 val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
170 val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
174 val = (char *)talloc_append_blob(NULL, val, *blob);
179 DEBUG(10, ("Adding cache entry with key = %s and timeout ="
180 " %s (%d seconds %s)\n", keystr, ctime(&timeout),
181 (int)(timeout - time(NULL)),
182 timeout > time(NULL) ? "ahead" : "in the past"));
184 ret = tdb_store_bystring(
185 cache_notrans, keystr,
186 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
195 * Every 100 writes within a single process, stabilize the cache with
196 * a transaction. This is done to prevent a single transaction to
197 * become huge and chew lots of memory.
200 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
201 gencache_stabilize();
207 * Every 5 minutes, call gencache_stabilize() to not let grow
208 * gencache_notrans.tdb too large.
212 databuf = tdb_fetch(cache_notrans, last_stabilize_key());
213 if ((databuf.dptr != NULL)
214 && (databuf.dptr[databuf.dsize-1] == '\0')) {
215 last_stabilize = atoi((char *)databuf.dptr);
216 SAFE_FREE(databuf.dptr);
219 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
221 gencache_stabilize();
229 * Delete one entry from the cache file.
231 * @param keystr string that represents a key of this entry
233 * @retval true upon successful deletion
234 * @retval false in case of failure
237 bool gencache_del(const char *keystr)
239 bool exists, was_expired;
243 if (keystr == NULL) {
247 if (!gencache_init()) return False;
249 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
252 * We delete an element by setting its timeout to 0. This way we don't
253 * have to do a transaction on gencache.tdb every time we delete an
257 exists = gencache_get_data_blob(keystr, &value, NULL, &was_expired);
259 if (!exists && was_expired) {
261 * gencache_get_data_blob has implicitly deleted this
262 * entry, so we have to return success here.
268 data_blob_free(&value);
269 ret = gencache_set(keystr, "", 0);
274 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
283 res = strtol(val, &endptr, 10);
285 if ((endptr == NULL) || (*endptr != '/')) {
286 DEBUG(2, ("Invalid gencache data format: %s\n", val));
292 if (pendptr != NULL) {
298 struct gencache_parse_state {
299 void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
303 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
305 struct gencache_parse_state *state;
311 if (data.dptr == NULL) {
314 ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
318 state = (struct gencache_parse_state *)private_data;
319 blob = data_blob_const(
320 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
321 state->parser(t, blob, state->private_data);
325 bool gencache_parse(const char *keystr,
326 void (*parser)(time_t timeout, DATA_BLOB blob,
330 struct gencache_parse_state state;
334 if (keystr == NULL) {
337 if (tdb_data_cmp(string_term_tdb_data(keystr),
338 last_stabilize_key()) == 0) {
341 if (!gencache_init()) {
345 key = string_term_tdb_data(keystr);
346 state.parser = parser;
347 state.private_data = private_data;
349 ret = tdb_parse_record(cache_notrans, key, gencache_parse_fn, &state);
353 ret = tdb_parse_record(cache, key, gencache_parse_fn, &state);
357 struct gencache_get_data_blob_state {
363 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
366 struct gencache_get_data_blob_state *state =
367 (struct gencache_get_data_blob_state *)private_data;
370 state->result = false;
373 state->timeout = timeout;
375 if (state->blob == NULL) {
376 state->result = true;
380 *state->blob = data_blob(blob.data, blob.length);
381 if (state->blob->data == NULL) {
382 state->result = false;
385 state->result = true;
389 * Get existing entry from the cache file.
391 * @param keystr string that represents a key of this entry
392 * @param blob DATA_BLOB that is filled with entry's blob
393 * @param timeout pointer to a time_t that is filled with entry's
396 * @retval true when entry is successfuly fetched
397 * @retval False for failure
400 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob,
401 time_t *timeout, bool *was_expired)
403 struct gencache_get_data_blob_state state;
404 bool expired = false;
406 state.result = false;
409 if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
415 if (state.timeout <= time(NULL)) {
417 * We're expired, delete the entry. We can't use gencache_del
418 * here, because that uses gencache_get_data_blob for checking
419 * the existence of a record. We know the thing exists and
420 * directly store an empty value with 0 timeout.
422 gencache_set(keystr, "", 0);
427 *timeout = state.timeout;
433 if (was_expired != NULL) {
434 *was_expired = expired;
436 if (state.result && state.blob) {
437 data_blob_free(state.blob);
442 struct stabilize_state {
446 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
452 * Migrate the clear-if-first gencache data to the stable,
453 * transaction-based gencache.tdb
456 bool gencache_stabilize(void)
458 struct stabilize_state state;
462 if (!gencache_init()) {
466 res = tdb_transaction_start_nonblock(cache);
469 if (tdb_error(cache) == TDB_ERR_NOLOCK) {
471 * Someone else already does the stabilize,
472 * this does not have to be done twice
477 DEBUG(10, ("Could not start transaction on gencache.tdb: "
478 "%s\n", tdb_errorstr(cache)));
481 res = tdb_transaction_start(cache_notrans);
483 tdb_transaction_cancel(cache);
484 DEBUG(10, ("Could not start transaction on "
485 "gencache_notrans.tdb: %s\n",
486 tdb_errorstr(cache_notrans)));
491 state.written = false;
493 res = tdb_traverse(cache_notrans, stabilize_fn, &state);
494 if ((res == -1) || state.error) {
495 if ((tdb_transaction_cancel(cache_notrans) == -1)
496 || (tdb_transaction_cancel(cache) == -1)) {
497 smb_panic("tdb_transaction_cancel failed\n");
502 if (!state.written) {
503 if ((tdb_transaction_cancel(cache_notrans) == -1)
504 || (tdb_transaction_cancel(cache) == -1)) {
505 smb_panic("tdb_transaction_cancel failed\n");
510 res = tdb_transaction_commit(cache);
512 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
513 "%s\n", tdb_errorstr(cache)));
514 if (tdb_transaction_cancel(cache_notrans) == -1) {
515 smb_panic("tdb_transaction_cancel failed\n");
520 res = tdb_transaction_commit(cache_notrans);
522 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
523 "%s\n", tdb_errorstr(cache)));
527 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
529 tdb_store(cache_notrans, last_stabilize_key(),
530 string_term_tdb_data(now), 0);
537 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
540 struct stabilize_state *state = (struct stabilize_state *)priv;
544 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
548 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
549 DEBUG(10, ("Ignoring invalid entry\n"));
552 if ((timeout < time(NULL)) || (val.dsize == 0)) {
553 res = tdb_delete(cache, key);
554 if ((res == -1) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
557 state->written = true;
560 res = tdb_store(cache, key, val, 0);
562 state->written = true;
567 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
568 tdb_errorstr(cache)));
573 if (tdb_delete(cache_notrans, key) == -1) {
574 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
575 "%s\n", tdb_errorstr(cache_notrans)));
583 * Get existing entry from the cache file.
585 * @param keystr string that represents a key of this entry
586 * @param valstr buffer that is allocated and filled with the entry value
587 * buffer's disposing must be done outside
588 * @param timeout pointer to a time_t that is filled with entry's
591 * @retval true when entry is successfuly fetched
592 * @retval False for failure
595 bool gencache_get(const char *keystr, char **value, time_t *ptimeout)
600 ret = gencache_get_data_blob(keystr, &blob, ptimeout, NULL);
604 if ((blob.data == NULL) || (blob.length == 0)) {
605 SAFE_FREE(blob.data);
608 if (blob.data[blob.length-1] != '\0') {
609 /* Not NULL terminated, can't be a string */
610 SAFE_FREE(blob.data);
614 *value = SMB_STRDUP((char *)blob.data);
615 data_blob_free(&blob);
616 if (*value == NULL) {
621 data_blob_free(&blob);
626 * Set an entry in the cache file. If there's no such
629 * @param keystr string that represents a key of this entry
630 * @param value text representation value being cached
631 * @param timeout time when the value is expired
633 * @retval true when entry is successfuly stored
634 * @retval false on failure
637 bool gencache_set(const char *keystr, const char *value, time_t timeout)
639 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
640 return gencache_set_data_blob(keystr, &blob, timeout);
643 struct gencache_iterate_blobs_state {
644 void (*fn)(const char *key, DATA_BLOB value,
645 time_t timeout, void *private_data);
651 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
652 TDB_DATA data, void *priv)
654 struct gencache_iterate_blobs_state *state =
655 (struct gencache_iterate_blobs_state *)priv;
657 char *free_key = NULL;
661 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
664 if (state->in_persistent && tdb_exists(cache_notrans, key)) {
668 if (key.dptr[key.dsize-1] == '\0') {
669 keystr = (char *)key.dptr;
671 /* ensure 0-termination */
672 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
676 if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
681 if (fnmatch(state->pattern, keystr, 0) != 0) {
685 DEBUG(10, ("Calling function with arguments (key=%s, timeout=%s)\n",
686 keystr, ctime(&timeout)));
689 data_blob_const(endptr,
690 data.dsize - PTR_DIFF(endptr, data.dptr)),
691 timeout, state->private_data);
698 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
699 time_t timeout, void *private_data),
700 void *private_data, const char *pattern)
702 struct gencache_iterate_blobs_state state;
704 if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
708 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
711 state.pattern = pattern;
712 state.private_data = private_data;
714 state.in_persistent = false;
715 tdb_traverse(cache_notrans, gencache_iterate_blobs_fn, &state);
717 state.in_persistent = true;
718 tdb_traverse(cache, gencache_iterate_blobs_fn, &state);
722 * Iterate through all entries which key matches to specified pattern
724 * @param fn pointer to the function that will be supplied with each single
725 * matching cache entry (key, value and timeout) as an arguments
726 * @param data void pointer to an arbitrary data that is passed directly to the fn
727 * function on each call
728 * @param keystr_pattern pattern the existing entries' keys are matched to
732 struct gencache_iterate_state {
733 void (*fn)(const char *key, const char *value, time_t timeout,
738 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
739 time_t timeout, void *private_data)
741 struct gencache_iterate_state *state =
742 (struct gencache_iterate_state *)private_data;
744 char *free_val = NULL;
746 if (value.data[value.length-1] == '\0') {
747 valstr = (char *)value.data;
749 /* ensure 0-termination */
750 valstr = SMB_STRNDUP((char *)value.data, value.length);
754 DEBUG(10, ("Calling function with arguments "
755 "(key = %s, value = %s, timeout = %s)\n",
756 key, valstr, ctime(&timeout)));
758 state->fn(key, valstr, timeout, state->private_data);
763 void gencache_iterate(void (*fn)(const char *key, const char *value,
764 time_t timeout, void *dptr),
765 void *private_data, const char *pattern)
767 struct gencache_iterate_state state;
773 state.private_data = private_data;
774 gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);