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
;
61 bool first_try
= true;
63 /* skip file open if it's already opened */
64 if (cache
) return True
;
66 cache_fname
= lock_path("gencache.tdb");
68 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
71 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
, open_flags
, 0644);
74 ret
= tdb_check(cache
, NULL
, NULL
);
79 DEBUG(0, ("gencache_init: tdb_check(%s) failed\n",
84 DEBUG(0, ("gencache_init: tdb_check(%s) failed - retry after truncate\n",
86 truncate(cache_fname
, 0);
91 if (!cache
&& (errno
== EACCES
)) {
92 open_flags
= O_RDONLY
;
93 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
, open_flags
,
96 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
101 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
105 cache_fname
= lock_path("gencache_notrans.tdb");
107 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
109 cache_notrans
= tdb_open_log(cache_fname
, 0, TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
111 if (cache_notrans
== NULL
) {
112 DEBUG(5, ("Opening %s failed: %s\n", cache_fname
,
122 static TDB_DATA
last_stabilize_key(void)
125 result
.dptr
= discard_const_p(uint8_t, "@LAST_STABILIZED");
131 * Set an entry in the cache file. If there's no such
134 * @param keystr string that represents a key of this entry
135 * @param blob DATA_BLOB value being cached
136 * @param timeout time when the value is expired
138 * @retval true when entry is successfuly stored
139 * @retval false on failure
142 bool gencache_set_data_blob(const char *keystr
, const DATA_BLOB
*blob
,
148 time_t last_stabilize
;
149 static int writecount
;
151 if (tdb_data_cmp(string_term_tdb_data(keystr
),
152 last_stabilize_key()) == 0) {
153 DEBUG(10, ("Can't store %s as a key\n", keystr
));
157 if ((keystr
== NULL
) || (blob
== NULL
)) {
161 if (!gencache_init()) return False
;
163 val
= talloc_asprintf(talloc_tos(), CACHE_DATA_FMT
, (int)timeout
);
167 val
= talloc_realloc(NULL
, val
, char, talloc_array_length(val
)-1);
171 val
= (char *)talloc_append_blob(NULL
, val
, *blob
);
176 DEBUG(10, ("Adding cache entry with key = %s and timeout ="
177 " %s (%d seconds %s)\n", keystr
, ctime(&timeout
),
178 (int)(timeout
- time(NULL
)),
179 timeout
> time(NULL
) ? "ahead" : "in the past"));
181 ret
= tdb_store_bystring(
182 cache_notrans
, keystr
,
183 make_tdb_data((uint8_t *)val
, talloc_array_length(val
)),
192 * Every 100 writes within a single process, stabilize the cache with
193 * a transaction. This is done to prevent a single transaction to
194 * become huge and chew lots of memory.
197 if (writecount
> lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
198 gencache_stabilize();
204 * Every 5 minutes, call gencache_stabilize() to not let grow
205 * gencache_notrans.tdb too large.
209 databuf
= tdb_fetch_compat(cache_notrans
, last_stabilize_key());
210 if ((databuf
.dptr
!= NULL
)
211 && (databuf
.dptr
[databuf
.dsize
-1] == '\0')) {
212 last_stabilize
= atoi((char *)databuf
.dptr
);
213 SAFE_FREE(databuf
.dptr
);
216 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
218 gencache_stabilize();
226 * Delete one entry from the cache file.
228 * @param keystr string that represents a key of this entry
230 * @retval true upon successful deletion
231 * @retval false in case of failure
234 bool gencache_del(const char *keystr
)
236 bool exists
, was_expired
;
240 if (keystr
== NULL
) {
244 if (!gencache_init()) return False
;
246 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr
));
249 * We delete an element by setting its timeout to 0. This way we don't
250 * have to do a transaction on gencache.tdb every time we delete an
254 exists
= gencache_get_data_blob(keystr
, &value
, NULL
, &was_expired
);
256 if (!exists
&& was_expired
) {
258 * gencache_get_data_blob has implicitly deleted this
259 * entry, so we have to return success here.
265 data_blob_free(&value
);
266 ret
= gencache_set(keystr
, "", 0);
271 static bool gencache_pull_timeout(char *val
, time_t *pres
, char **pendptr
)
280 res
= strtol(val
, &endptr
, 10);
282 if ((endptr
== NULL
) || (*endptr
!= '/')) {
283 DEBUG(2, ("Invalid gencache data format: %s\n", val
));
289 if (pendptr
!= NULL
) {
295 struct gencache_parse_state
{
296 void (*parser
)(time_t timeout
, DATA_BLOB blob
, void *private_data
);
300 static int gencache_parse_fn(TDB_DATA key
, TDB_DATA data
, void *private_data
)
302 struct gencache_parse_state
*state
;
308 if (data
.dptr
== NULL
) {
311 ret
= gencache_pull_timeout((char *)data
.dptr
, &t
, &endptr
);
315 state
= (struct gencache_parse_state
*)private_data
;
316 blob
= data_blob_const(
317 endptr
+1, data
.dsize
- PTR_DIFF(endptr
+1, data
.dptr
));
318 state
->parser(t
, blob
, state
->private_data
);
322 bool gencache_parse(const char *keystr
,
323 void (*parser
)(time_t timeout
, DATA_BLOB blob
,
327 struct gencache_parse_state state
;
331 if (keystr
== NULL
) {
334 if (tdb_data_cmp(string_term_tdb_data(keystr
),
335 last_stabilize_key()) == 0) {
338 if (!gencache_init()) {
342 key
= string_term_tdb_data(keystr
);
343 state
.parser
= parser
;
344 state
.private_data
= private_data
;
346 ret
= tdb_parse_record(cache_notrans
, key
, gencache_parse_fn
, &state
);
350 ret
= tdb_parse_record(cache
, key
, gencache_parse_fn
, &state
);
354 struct gencache_get_data_blob_state
{
360 static void gencache_get_data_blob_parser(time_t timeout
, DATA_BLOB blob
,
363 struct gencache_get_data_blob_state
*state
=
364 (struct gencache_get_data_blob_state
*)private_data
;
367 state
->result
= false;
370 state
->timeout
= timeout
;
372 if (state
->blob
== NULL
) {
373 state
->result
= true;
377 *state
->blob
= data_blob(blob
.data
, blob
.length
);
378 if (state
->blob
->data
== NULL
) {
379 state
->result
= false;
382 state
->result
= true;
386 * Get existing entry from the cache file.
388 * @param keystr string that represents a key of this entry
389 * @param blob DATA_BLOB that is filled with entry's blob
390 * @param timeout pointer to a time_t that is filled with entry's
393 * @retval true when entry is successfuly fetched
394 * @retval False for failure
397 bool gencache_get_data_blob(const char *keystr
, DATA_BLOB
*blob
,
398 time_t *timeout
, bool *was_expired
)
400 struct gencache_get_data_blob_state state
;
401 bool expired
= false;
403 state
.result
= false;
406 if (!gencache_parse(keystr
, gencache_get_data_blob_parser
, &state
)) {
412 if (state
.timeout
<= time(NULL
)) {
414 * We're expired, delete the entry. We can't use gencache_del
415 * here, because that uses gencache_get_data_blob for checking
416 * the existence of a record. We know the thing exists and
417 * directly store an empty value with 0 timeout.
419 gencache_set(keystr
, "", 0);
424 *timeout
= state
.timeout
;
430 if (was_expired
!= NULL
) {
431 *was_expired
= expired
;
433 if (state
.result
&& state
.blob
) {
434 data_blob_free(state
.blob
);
439 struct stabilize_state
{
443 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
449 * Migrate the clear-if-first gencache data to the stable,
450 * transaction-based gencache.tdb
453 bool gencache_stabilize(void)
455 struct stabilize_state state
;
459 if (!gencache_init()) {
463 res
= tdb_transaction_start_nonblock(cache
);
465 if (tdb_error(cache
) == TDB_ERR_NOLOCK
)
468 * Someone else already does the stabilize,
469 * this does not have to be done twice
474 DEBUG(10, ("Could not start transaction on gencache.tdb: "
475 "%s\n", tdb_errorstr_compat(cache
)));
478 res
= tdb_transaction_start(cache_notrans
);
480 tdb_transaction_cancel(cache
);
481 DEBUG(10, ("Could not start transaction on "
482 "gencache_notrans.tdb: %s\n",
483 tdb_errorstr_compat(cache_notrans
)));
488 state
.written
= false;
490 res
= tdb_traverse(cache_notrans
, stabilize_fn
, &state
);
491 if ((res
< 0) || state
.error
) {
492 tdb_transaction_cancel(cache_notrans
);
493 tdb_transaction_cancel(cache
);
497 if (!state
.written
) {
498 tdb_transaction_cancel(cache_notrans
);
499 tdb_transaction_cancel(cache
);
503 res
= tdb_transaction_commit(cache
);
505 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
506 "%s\n", tdb_errorstr_compat(cache
)));
507 tdb_transaction_cancel(cache_notrans
);
511 res
= tdb_transaction_commit(cache_notrans
);
513 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
514 "%s\n", tdb_errorstr_compat(cache
)));
518 now
= talloc_asprintf(talloc_tos(), "%d", (int)time(NULL
));
520 tdb_store(cache_notrans
, last_stabilize_key(),
521 string_term_tdb_data(now
), 0);
528 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
531 struct stabilize_state
*state
= (struct stabilize_state
*)priv
;
535 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
539 if (!gencache_pull_timeout((char *)val
.dptr
, &timeout
, NULL
)) {
540 DEBUG(10, ("Ignoring invalid entry\n"));
543 if ((timeout
< time(NULL
)) || (val
.dsize
== 0)) {
544 res
= tdb_delete(cache
, key
);
545 if ((res
!= 0) && (tdb_error(cache
) == TDB_ERR_NOEXIST
)) {
548 state
->written
= true;
551 res
= tdb_store(cache
, key
, val
, 0);
553 state
->written
= true;
558 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
559 tdb_errorstr_compat(cache
)));
564 if (tdb_delete(cache_notrans
, key
) != 0) {
565 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
566 "%s\n", tdb_errorstr_compat(cache_notrans
)));
574 * Get existing entry from the cache file.
576 * @param keystr string that represents a key of this entry
577 * @param valstr buffer that is allocated and filled with the entry value
578 * buffer's disposing must be done outside
579 * @param timeout pointer to a time_t that is filled with entry's
582 * @retval true when entry is successfuly fetched
583 * @retval False for failure
586 bool gencache_get(const char *keystr
, char **value
, time_t *ptimeout
)
591 ret
= gencache_get_data_blob(keystr
, &blob
, ptimeout
, NULL
);
595 if ((blob
.data
== NULL
) || (blob
.length
== 0)) {
596 SAFE_FREE(blob
.data
);
599 if (blob
.data
[blob
.length
-1] != '\0') {
600 /* Not NULL terminated, can't be a string */
601 SAFE_FREE(blob
.data
);
605 *value
= SMB_STRDUP((char *)blob
.data
);
606 data_blob_free(&blob
);
607 if (*value
== NULL
) {
612 data_blob_free(&blob
);
617 * Set an entry in the cache file. If there's no such
620 * @param keystr string that represents a key of this entry
621 * @param value text representation value being cached
622 * @param timeout time when the value is expired
624 * @retval true when entry is successfuly stored
625 * @retval false on failure
628 bool gencache_set(const char *keystr
, const char *value
, time_t timeout
)
630 DATA_BLOB blob
= data_blob_const(value
, strlen(value
)+1);
631 return gencache_set_data_blob(keystr
, &blob
, timeout
);
634 struct gencache_iterate_blobs_state
{
635 void (*fn
)(const char *key
, DATA_BLOB value
,
636 time_t timeout
, void *private_data
);
642 static int gencache_iterate_blobs_fn(struct tdb_context
*tdb
, TDB_DATA key
,
643 TDB_DATA data
, void *priv
)
645 struct gencache_iterate_blobs_state
*state
=
646 (struct gencache_iterate_blobs_state
*)priv
;
648 char *free_key
= NULL
;
652 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
655 if (state
->in_persistent
&& tdb_exists(cache_notrans
, key
)) {
659 if (key
.dptr
[key
.dsize
-1] == '\0') {
660 keystr
= (char *)key
.dptr
;
662 /* ensure 0-termination */
663 keystr
= SMB_STRNDUP((char *)key
.dptr
, key
.dsize
);
667 if (!gencache_pull_timeout((char *)data
.dptr
, &timeout
, &endptr
)) {
672 if (fnmatch(state
->pattern
, keystr
, 0) != 0) {
676 DEBUG(10, ("Calling function with arguments (key=%s, timeout=%s)\n",
677 keystr
, ctime(&timeout
)));
680 data_blob_const(endptr
,
681 data
.dsize
- PTR_DIFF(endptr
, data
.dptr
)),
682 timeout
, state
->private_data
);
689 void gencache_iterate_blobs(void (*fn
)(const char *key
, DATA_BLOB value
,
690 time_t timeout
, void *private_data
),
691 void *private_data
, const char *pattern
)
693 struct gencache_iterate_blobs_state state
;
695 if ((fn
== NULL
) || (pattern
== NULL
) || !gencache_init()) {
699 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern
));
702 state
.pattern
= pattern
;
703 state
.private_data
= private_data
;
705 state
.in_persistent
= false;
706 tdb_traverse(cache_notrans
, gencache_iterate_blobs_fn
, &state
);
708 state
.in_persistent
= true;
709 tdb_traverse(cache
, gencache_iterate_blobs_fn
, &state
);
713 * Iterate through all entries which key matches to specified pattern
715 * @param fn pointer to the function that will be supplied with each single
716 * matching cache entry (key, value and timeout) as an arguments
717 * @param data void pointer to an arbitrary data that is passed directly to the fn
718 * function on each call
719 * @param keystr_pattern pattern the existing entries' keys are matched to
723 struct gencache_iterate_state
{
724 void (*fn
)(const char *key
, const char *value
, time_t timeout
,
729 static void gencache_iterate_fn(const char *key
, DATA_BLOB value
,
730 time_t timeout
, void *private_data
)
732 struct gencache_iterate_state
*state
=
733 (struct gencache_iterate_state
*)private_data
;
735 char *free_val
= NULL
;
737 if (value
.data
[value
.length
-1] == '\0') {
738 valstr
= (char *)value
.data
;
740 /* ensure 0-termination */
741 valstr
= SMB_STRNDUP((char *)value
.data
, value
.length
);
745 DEBUG(10, ("Calling function with arguments "
746 "(key = %s, value = %s, timeout = %s)\n",
747 key
, valstr
, ctime(&timeout
)));
749 state
->fn(key
, valstr
, timeout
, state
->private_data
);
754 void gencache_iterate(void (*fn
)(const char *key
, const char *value
,
755 time_t timeout
, void *dptr
),
756 void *private_data
, const char *pattern
)
758 struct gencache_iterate_state state
;
764 state
.private_data
= private_data
;
765 gencache_iterate_blobs(gencache_iterate_fn
, &state
, pattern
);