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"
28 #include "tdb_wrap/tdb_wrap.h"
31 #define DBGC_CLASS DBGC_TDB
33 #define CACHE_DATA_FMT "%12u/"
35 static struct tdb_wrap
*cache
;
36 static struct tdb_wrap
*cache_notrans
;
40 * @brief Generic, persistent and shared between processes cache mechanism
41 * for use by various parts of the Samba code
47 * Cache initialisation function. Opens cache tdb file or creates
48 * it if does not exist.
50 * @return true on successful initialisation of the cache or
54 static bool gencache_init(void)
56 char* cache_fname
= NULL
;
57 int open_flags
= O_RDWR
|O_CREAT
;
60 /* skip file open if it's already opened */
65 hash_size
= lp_parm_int(-1, "gencache", "hash_size", 10000);
67 cache_fname
= cache_path("gencache.tdb");
68 if (cache_fname
== NULL
) {
72 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
74 cache
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
75 TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
,
79 ret
= tdb_check(cache
->tdb
, NULL
, NULL
);
84 * Retry with CLEAR_IF_FIRST.
86 * Warning: Converting this to dbwrap won't work
87 * directly. gencache.c does transactions on this tdb,
88 * and dbwrap forbids this for CLEAR_IF_FIRST
89 * databases. tdb does allow transactions on
90 * CLEAR_IF_FIRST databases, so lets use it here to
91 * clean up a broken database.
93 cache
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
95 TDB_INCOMPATIBLE_HASH
|
101 if (!cache
&& (errno
== EACCES
)) {
102 open_flags
= O_RDONLY
;
103 cache
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
104 TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
,
107 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
110 TALLOC_FREE(cache_fname
);
113 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
117 cache_fname
= lock_path("gencache_notrans.tdb");
118 if (cache_fname
== NULL
) {
123 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
125 cache_notrans
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
127 TDB_INCOMPATIBLE_HASH
|
131 if (cache_notrans
== NULL
) {
132 DEBUG(5, ("Opening %s failed: %s\n", cache_fname
,
134 TALLOC_FREE(cache_fname
);
138 TALLOC_FREE(cache_fname
);
143 static TDB_DATA
last_stabilize_key(void)
146 result
.dptr
= discard_const_p(uint8_t, "@LAST_STABILIZED");
151 struct gencache_have_val_state
{
153 const DATA_BLOB
*data
;
157 static void gencache_have_val_parser(time_t old_timeout
, DATA_BLOB data
,
160 struct gencache_have_val_state
*state
=
161 (struct gencache_have_val_state
*)private_data
;
162 time_t now
= time(NULL
);
163 int cache_time_left
, new_time_left
, additional_time
;
166 * Excuse the many variables, but these time calculations are
167 * confusing to me. We do not want to write to gencache with a
168 * possibly expensive transaction if we are about to write the same
169 * value, just extending the remaining timeout by less than 10%.
172 cache_time_left
= old_timeout
- now
;
173 if (cache_time_left
<= 0) {
175 * timed out, write new value
180 new_time_left
= state
->new_timeout
- now
;
181 if (new_time_left
<= 0) {
183 * Huh -- no new timeout?? Write it.
188 if (new_time_left
< cache_time_left
) {
190 * Someone wants to shorten the timeout. Let it happen.
196 * By how much does the new timeout extend the remaining cache time?
198 additional_time
= new_time_left
- cache_time_left
;
200 if (additional_time
* 10 < 0) {
202 * Integer overflow. We extend by so much that we have to write it.
208 * The comparison below is essentially equivalent to
210 * new_time_left > cache_time_left * 1.10
212 * but without floating point calculations.
215 if (additional_time
* 10 > cache_time_left
) {
217 * We extend the cache timeout by more than 10%. Do it.
223 * Now the more expensive data compare.
225 if (data_blob_cmp(state
->data
, &data
) != 0) {
227 * Write a new value. Certainly do it.
233 * Extending the timeout by less than 10% for the same cache value is
234 * not worth the trouble writing a value into gencache under a
235 * possibly expensive transaction.
240 static bool gencache_have_val(const char *keystr
, const DATA_BLOB
*data
,
243 struct gencache_have_val_state state
;
245 state
.new_timeout
= timeout
;
249 if (!gencache_parse(keystr
, gencache_have_val_parser
, &state
)) {
255 static int last_stabilize_parser(TDB_DATA key
, TDB_DATA data
,
258 time_t *last_stabilize
= private_data
;
260 if ((data
.dsize
!= 0) && (data
.dptr
[data
.dsize
-1] == '\0')) {
261 *last_stabilize
= atoi((char *)data
.dptr
);
267 * Set an entry in the cache file. If there's no such
270 * @param keystr string that represents a key of this entry
271 * @param blob DATA_BLOB value being cached
272 * @param timeout time when the value is expired
274 * @retval true when entry is successfully stored
275 * @retval false on failure
278 bool gencache_set_data_blob(const char *keystr
, const DATA_BLOB
*blob
,
284 time_t last_stabilize
;
285 static int writecount
;
288 if (tdb_data_cmp(string_term_tdb_data(keystr
),
289 last_stabilize_key()) == 0) {
290 DEBUG(10, ("Can't store %s as a key\n", keystr
));
294 if ((keystr
== NULL
) || (blob
== NULL
)) {
298 if (!gencache_init()) {
302 if ((timeout
!= 0) && gencache_have_val(keystr
, blob
, timeout
)) {
303 DEBUG(10, ("Did not store value for %s, we already got it\n",
308 hdr_len
= fstr_sprintf(hdr
, CACHE_DATA_FMT
, (int)timeout
);
313 if ((blob
->length
+ (size_t)hdr_len
) < blob
->length
) {
317 dbufs
[0] = (TDB_DATA
) { .dptr
= (uint8_t *)hdr
, .dsize
= hdr_len
};
318 dbufs
[1] = (TDB_DATA
) { .dptr
= blob
->data
, .dsize
= blob
->length
};
320 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
321 "[%s] (%d seconds %s)\n", keystr
,
322 timestring(talloc_tos(), timeout
),
323 (int)(timeout
- time(NULL
)),
324 timeout
> time(NULL
) ? "ahead" : "in the past"));
326 ret
= tdb_storev(cache_notrans
->tdb
, string_term_tdb_data(keystr
),
333 * Every 100 writes within a single process, stabilize the cache with
334 * a transaction. This is done to prevent a single transaction to
335 * become huge and chew lots of memory.
338 if (writecount
> lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
339 gencache_stabilize();
345 * Every 5 minutes, call gencache_stabilize() to not let grow
346 * gencache_notrans.tdb too large.
351 tdb_parse_record(cache_notrans
->tdb
, last_stabilize_key(),
352 last_stabilize_parser
, &last_stabilize
);
355 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
357 gencache_stabilize();
364 static void gencache_del_parser(time_t timeout
, DATA_BLOB blob
,
368 bool *exists
= private_data
;
374 * Delete one entry from the cache file.
376 * @param keystr string that represents a key of this entry
378 * @retval true upon successful deletion
379 * @retval false in case of failure
382 bool gencache_del(const char *keystr
)
384 TDB_DATA key
= string_term_tdb_data(keystr
);
389 if (keystr
== NULL
) {
393 if (!gencache_init()) {
397 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr
));
399 ret
= tdb_chainlock(cache_notrans
->tdb
, key
);
404 gencache_parse(keystr
, gencache_del_parser
, &exists
);
408 * We delete an element by setting its timeout to
409 * 0. This way we don't have to do a transaction on
410 * gencache.tdb every time we delete an element.
412 result
= gencache_set(keystr
, "", 0);
415 tdb_chainunlock(cache_notrans
->tdb
, key
);
420 static bool gencache_pull_timeout(uint8_t *val
, time_t *pres
, char **payload
)
429 res
= strtol((char *)val
, &endptr
, 10);
431 if ((endptr
== NULL
) || (*endptr
!= '/')) {
432 DEBUG(2, ("Invalid gencache data format: %s\n", (char *)val
));
438 if (payload
!= NULL
) {
444 struct gencache_parse_state
{
445 void (*parser
)(time_t timeout
, DATA_BLOB blob
, void *private_data
);
447 bool copy_to_notrans
;
450 static int gencache_parse_fn(TDB_DATA key
, TDB_DATA data
, void *private_data
)
452 struct gencache_parse_state
*state
;
458 if (data
.dptr
== NULL
) {
461 ret
= gencache_pull_timeout(data
.dptr
, &t
, &payload
);
465 state
= (struct gencache_parse_state
*)private_data
;
466 blob
= data_blob_const(
467 payload
, data
.dsize
- PTR_DIFF(payload
, data
.dptr
));
468 state
->parser(t
, blob
, state
->private_data
);
470 if (state
->copy_to_notrans
) {
471 tdb_store(cache_notrans
->tdb
, key
, data
, 0);
477 bool gencache_parse(const char *keystr
,
478 void (*parser
)(time_t timeout
, DATA_BLOB blob
,
482 struct gencache_parse_state state
;
483 TDB_DATA key
= string_term_tdb_data(keystr
);
486 if (keystr
== NULL
) {
489 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
492 if (!gencache_init()) {
496 state
.parser
= parser
;
497 state
.private_data
= private_data
;
498 state
.copy_to_notrans
= false;
500 ret
= tdb_chainlock(cache_notrans
->tdb
, key
);
505 ret
= tdb_parse_record(cache_notrans
->tdb
, key
,
506 gencache_parse_fn
, &state
);
508 tdb_chainunlock(cache_notrans
->tdb
, key
);
512 state
.copy_to_notrans
= true;
514 ret
= tdb_parse_record(cache
->tdb
, key
, gencache_parse_fn
, &state
);
516 if ((ret
== -1) && (tdb_error(cache
->tdb
) == TDB_ERR_NOEXIST
)) {
518 * The record does not exist. Set a delete-marker in
519 * gencache_notrans, so that we don't have to look at
520 * the fcntl-based cache again.
522 gencache_set(keystr
, "", 0);
525 tdb_chainunlock(cache_notrans
->tdb
, key
);
530 struct gencache_get_data_blob_state
{
537 static void gencache_get_data_blob_parser(time_t timeout
, DATA_BLOB blob
,
540 struct gencache_get_data_blob_state
*state
=
541 (struct gencache_get_data_blob_state
*)private_data
;
544 state
->result
= false;
547 state
->timeout
= timeout
;
549 if (state
->blob
== NULL
) {
550 state
->result
= true;
554 *state
->blob
= data_blob_talloc(state
->mem_ctx
, blob
.data
,
556 if (state
->blob
->data
== NULL
) {
557 state
->result
= false;
560 state
->result
= true;
564 * Get existing entry from the cache file.
566 * @param keystr string that represents a key of this entry
567 * @param blob DATA_BLOB that is filled with entry's blob
568 * @param timeout pointer to a time_t that is filled with entry's
571 * @retval true when entry is successfully fetched
572 * @retval false for failure
575 bool gencache_get_data_blob(const char *keystr
, TALLOC_CTX
*mem_ctx
,
577 time_t *timeout
, bool *was_expired
)
579 struct gencache_get_data_blob_state state
;
580 bool expired
= false;
582 state
.result
= false;
583 state
.mem_ctx
= mem_ctx
;
586 if (!gencache_parse(keystr
, gencache_get_data_blob_parser
, &state
)) {
592 if (state
.timeout
<= time(NULL
)) {
594 * We're expired, delete the entry. We can't use gencache_del
595 * here, because that uses gencache_get_data_blob for checking
596 * the existence of a record. We know the thing exists and
597 * directly store an empty value with 0 timeout.
599 gencache_set(keystr
, "", 0);
604 *timeout
= state
.timeout
;
610 if (was_expired
!= NULL
) {
611 *was_expired
= expired
;
613 if (state
.result
&& state
.blob
) {
614 data_blob_free(state
.blob
);
619 struct stabilize_state
{
622 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
625 static int wipe_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
631 * Migrate the clear-if-first gencache data to the stable,
632 * transaction-based gencache.tdb
635 bool gencache_stabilize(void)
637 struct stabilize_state state
;
641 if (!gencache_init()) {
645 res
= tdb_transaction_start_nonblock(cache
->tdb
);
647 if (tdb_error(cache
->tdb
) == TDB_ERR_NOLOCK
)
650 * Someone else already does the stabilize,
651 * this does not have to be done twice
656 DEBUG(10, ("Could not start transaction on gencache.tdb: "
657 "%s\n", tdb_errorstr(cache
->tdb
)));
661 res
= tdb_lockall_nonblock(cache_notrans
->tdb
);
663 tdb_transaction_cancel(cache
->tdb
);
664 DEBUG(10, ("Could not get allrecord lock on "
665 "gencache_notrans.tdb: %s\n",
666 tdb_errorstr(cache_notrans
->tdb
)));
670 state
.written
= false;
672 res
= tdb_traverse(cache_notrans
->tdb
, stabilize_fn
, &state
);
674 tdb_unlockall(cache_notrans
->tdb
);
675 tdb_transaction_cancel(cache
->tdb
);
679 if (!state
.written
) {
680 tdb_unlockall(cache_notrans
->tdb
);
681 tdb_transaction_cancel(cache
->tdb
);
685 res
= tdb_transaction_commit(cache
->tdb
);
687 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
688 "%s\n", tdb_errorstr(cache
->tdb
)));
689 tdb_unlockall(cache_notrans
->tdb
);
693 res
= tdb_traverse(cache_notrans
->tdb
, wipe_fn
, NULL
);
695 DEBUG(10, ("tdb_traverse with wipe_fn on gencache_notrans.tdb "
697 tdb_errorstr(cache_notrans
->tdb
)));
698 tdb_unlockall(cache_notrans
->tdb
);
702 res
= tdb_unlockall(cache_notrans
->tdb
);
704 DEBUG(10, ("tdb_unlockall on gencache.tdb failed: "
705 "%s\n", tdb_errorstr(cache
->tdb
)));
709 now
= talloc_asprintf(talloc_tos(), "%d", (int)time(NULL
));
711 tdb_store(cache_notrans
->tdb
, last_stabilize_key(),
712 string_term_tdb_data(now
), 0);
719 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
722 struct stabilize_state
*state
= (struct stabilize_state
*)priv
;
726 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
730 if (!gencache_pull_timeout(val
.dptr
, &timeout
, NULL
)) {
731 DEBUG(10, ("Ignoring invalid entry\n"));
734 if ((timeout
< time(NULL
)) || (val
.dsize
== 0)) {
735 res
= tdb_delete(cache
->tdb
, key
);
737 state
->written
= true;
738 } else if (tdb_error(cache
->tdb
) == TDB_ERR_NOEXIST
) {
742 res
= tdb_store(cache
->tdb
, key
, val
, 0);
744 state
->written
= true;
749 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
750 tdb_errorstr(cache
->tdb
)));
757 static int wipe_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
764 res
= tdb_data_cmp(key
, last_stabilize_key());
769 ok
= gencache_pull_timeout(val
.dptr
, &timeout
, NULL
);
771 DEBUG(10, ("Ignoring invalid entry\n"));
775 res
= tdb_delete(tdb
, key
);
777 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
778 "%s\n", tdb_errorstr(cache_notrans
->tdb
)));
787 * Get existing entry from the cache file.
789 * @param keystr string that represents a key of this entry
790 * @param valstr buffer that is allocated and filled with the entry value
791 * buffer's disposing must be done outside
792 * @param timeout pointer to a time_t that is filled with entry's
795 * @retval true when entry is successfully fetched
796 * @retval false for failure
799 bool gencache_get(const char *keystr
, TALLOC_CTX
*mem_ctx
, char **value
,
805 ret
= gencache_get_data_blob(keystr
, mem_ctx
, &blob
, ptimeout
, NULL
);
809 if ((blob
.data
== NULL
) || (blob
.length
== 0)) {
810 data_blob_free(&blob
);
813 if (blob
.data
[blob
.length
-1] != '\0') {
814 /* Not NULL terminated, can't be a string */
815 data_blob_free(&blob
);
820 * talloc_move generates a type-punned warning here. As we
821 * leave the function immediately, do a simple talloc_steal.
823 *value
= (char *)talloc_steal(mem_ctx
, blob
.data
);
826 data_blob_free(&blob
);
831 * Set an entry in the cache file. If there's no such
834 * @param keystr string that represents a key of this entry
835 * @param value text representation value being cached
836 * @param timeout time when the value is expired
838 * @retval true when entry is successfully stored
839 * @retval false on failure
842 bool gencache_set(const char *keystr
, const char *value
, time_t timeout
)
844 DATA_BLOB blob
= data_blob_const(value
, strlen(value
)+1);
845 return gencache_set_data_blob(keystr
, &blob
, timeout
);
848 struct gencache_iterate_blobs_state
{
849 void (*fn
)(const char *key
, DATA_BLOB value
,
850 time_t timeout
, void *private_data
);
856 static int gencache_iterate_blobs_fn(struct tdb_context
*tdb
, TDB_DATA key
,
857 TDB_DATA data
, void *priv
)
859 struct gencache_iterate_blobs_state
*state
=
860 (struct gencache_iterate_blobs_state
*)priv
;
862 char *free_key
= NULL
;
866 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
869 if (state
->in_persistent
&& tdb_exists(cache_notrans
->tdb
, key
)) {
873 if (key
.dptr
[key
.dsize
-1] == '\0') {
874 keystr
= (char *)key
.dptr
;
876 /* ensure 0-termination */
877 keystr
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
879 if (keystr
== NULL
) {
884 if (!gencache_pull_timeout(data
.dptr
, &timeout
, &payload
)) {
893 if (fnmatch(state
->pattern
, keystr
, 0) != 0) {
897 DEBUG(10, ("Calling function with arguments "
898 "(key=[%s], timeout=[%s])\n",
899 keystr
, timestring(talloc_tos(), timeout
)));
902 data_blob_const(payload
,
903 data
.dsize
- PTR_DIFF(payload
, data
.dptr
)),
904 timeout
, state
->private_data
);
907 TALLOC_FREE(free_key
);
911 void gencache_iterate_blobs(void (*fn
)(const char *key
, DATA_BLOB value
,
912 time_t timeout
, void *private_data
),
913 void *private_data
, const char *pattern
)
915 struct gencache_iterate_blobs_state state
;
917 if ((fn
== NULL
) || (pattern
== NULL
) || !gencache_init()) {
921 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern
));
924 state
.pattern
= pattern
;
925 state
.private_data
= private_data
;
927 state
.in_persistent
= false;
928 tdb_traverse(cache_notrans
->tdb
, gencache_iterate_blobs_fn
, &state
);
930 state
.in_persistent
= true;
931 tdb_traverse(cache
->tdb
, gencache_iterate_blobs_fn
, &state
);
935 * Iterate through all entries which key matches to specified pattern
937 * @param fn pointer to the function that will be supplied with each single
938 * matching cache entry (key, value and timeout) as an arguments
939 * @param data void pointer to an arbitrary data that is passed directly to the fn
940 * function on each call
941 * @param keystr_pattern pattern the existing entries' keys are matched to
945 struct gencache_iterate_state
{
946 void (*fn
)(const char *key
, const char *value
, time_t timeout
,
951 static void gencache_iterate_fn(const char *key
, DATA_BLOB value
,
952 time_t timeout
, void *private_data
)
954 struct gencache_iterate_state
*state
=
955 (struct gencache_iterate_state
*)private_data
;
957 char *free_val
= NULL
;
959 if (value
.data
[value
.length
-1] == '\0') {
960 valstr
= (char *)value
.data
;
962 /* ensure 0-termination */
963 valstr
= talloc_strndup(talloc_tos(), (char *)value
.data
, value
.length
);
965 if (valstr
== NULL
) {
970 DEBUG(10, ("Calling function with arguments "
971 "(key=[%s], value=[%s], timeout=[%s])\n",
972 key
, valstr
, timestring(talloc_tos(), timeout
)));
974 state
->fn(key
, valstr
, timeout
, state
->private_data
);
978 TALLOC_FREE(free_val
);
981 void gencache_iterate(void (*fn
)(const char *key
, const char *value
,
982 time_t timeout
, void *dptr
),
983 void *private_data
, const char *pattern
)
985 struct gencache_iterate_state state
;
991 state
.private_data
= private_data
;
992 gencache_iterate_blobs(gencache_iterate_fn
, &state
, pattern
);