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
,
78 if (!cache
&& (errno
== EACCES
)) {
79 open_flags
= O_RDONLY
;
80 cache
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
81 TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
,
84 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
87 TALLOC_FREE(cache_fname
);
90 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
94 cache_fname
= lock_path("gencache_notrans.tdb");
95 if (cache_fname
== NULL
) {
100 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
102 cache_notrans
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
104 TDB_INCOMPATIBLE_HASH
|
108 if (cache_notrans
== NULL
) {
109 DEBUG(5, ("Opening %s failed: %s\n", cache_fname
,
111 TALLOC_FREE(cache_fname
);
115 TALLOC_FREE(cache_fname
);
120 static TDB_DATA
last_stabilize_key(void)
123 result
.dptr
= discard_const_p(uint8_t, "@LAST_STABILIZED");
128 struct gencache_have_val_state
{
130 const DATA_BLOB
*data
;
134 static void gencache_have_val_parser(time_t old_timeout
, DATA_BLOB data
,
137 struct gencache_have_val_state
*state
=
138 (struct gencache_have_val_state
*)private_data
;
139 time_t now
= time(NULL
);
140 int cache_time_left
, new_time_left
, additional_time
;
143 * Excuse the many variables, but these time calculations are
144 * confusing to me. We do not want to write to gencache with a
145 * possibly expensive transaction if we are about to write the same
146 * value, just extending the remaining timeout by less than 10%.
149 cache_time_left
= old_timeout
- now
;
150 if (cache_time_left
<= 0) {
152 * timed out, write new value
157 new_time_left
= state
->new_timeout
- now
;
158 if (new_time_left
<= 0) {
160 * Huh -- no new timeout?? Write it.
165 if (new_time_left
< cache_time_left
) {
167 * Someone wants to shorten the timeout. Let it happen.
173 * By how much does the new timeout extend the remaining cache time?
175 additional_time
= new_time_left
- cache_time_left
;
177 if (additional_time
* 10 < 0) {
179 * Integer overflow. We extend by so much that we have to write it.
185 * The comparison below is essentially equivalent to
187 * new_time_left > cache_time_left * 1.10
189 * but without floating point calculations.
192 if (additional_time
* 10 > cache_time_left
) {
194 * We extend the cache timeout by more than 10%. Do it.
200 * Now the more expensive data compare.
202 if (data_blob_cmp(state
->data
, &data
) != 0) {
204 * Write a new value. Certainly do it.
210 * Extending the timeout by less than 10% for the same cache value is
211 * not worth the trouble writing a value into gencache under a
212 * possibly expensive transaction.
217 static bool gencache_have_val(const char *keystr
, const DATA_BLOB
*data
,
220 struct gencache_have_val_state state
;
222 state
.new_timeout
= timeout
;
226 if (!gencache_parse(keystr
, gencache_have_val_parser
, &state
)) {
232 static int last_stabilize_parser(TDB_DATA key
, TDB_DATA data
,
235 time_t *last_stabilize
= private_data
;
237 if ((data
.dsize
!= 0) && (data
.dptr
[data
.dsize
-1] == '\0')) {
238 *last_stabilize
= atoi((char *)data
.dptr
);
244 * Set an entry in the cache file. If there's no such
247 * @param keystr string that represents a key of this entry
248 * @param blob DATA_BLOB value being cached
249 * @param timeout time when the value is expired
251 * @retval true when entry is successfully stored
252 * @retval false on failure
255 bool gencache_set_data_blob(const char *keystr
, DATA_BLOB blob
,
261 time_t last_stabilize
;
262 static int writecount
;
265 if (tdb_data_cmp(string_term_tdb_data(keystr
),
266 last_stabilize_key()) == 0) {
267 DEBUG(10, ("Can't store %s as a key\n", keystr
));
271 if ((keystr
== NULL
) || (blob
.data
== NULL
)) {
275 if (!gencache_init()) {
279 if ((timeout
!= 0) && gencache_have_val(keystr
, &blob
, timeout
)) {
280 DEBUG(10, ("Did not store value for %s, we already got it\n",
285 hdr_len
= fstr_sprintf(hdr
, CACHE_DATA_FMT
, (int)timeout
);
290 if ((blob
.length
+ (size_t)hdr_len
) < blob
.length
) {
294 dbufs
[0] = (TDB_DATA
) { .dptr
= (uint8_t *)hdr
, .dsize
= hdr_len
};
295 dbufs
[1] = (TDB_DATA
) { .dptr
= blob
.data
, .dsize
= blob
.length
};
297 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
298 "[%s] (%d seconds %s)\n", keystr
,
299 timestring(talloc_tos(), timeout
),
300 (int)(timeout
- time(NULL
)),
301 timeout
> time(NULL
) ? "ahead" : "in the past"));
303 ret
= tdb_storev(cache_notrans
->tdb
, string_term_tdb_data(keystr
),
310 * Every 100 writes within a single process, stabilize the cache with
311 * a transaction. This is done to prevent a single transaction to
312 * become huge and chew lots of memory.
315 if (writecount
> lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
316 gencache_stabilize();
322 * Every 5 minutes, call gencache_stabilize() to not let grow
323 * gencache_notrans.tdb too large.
328 tdb_parse_record(cache_notrans
->tdb
, last_stabilize_key(),
329 last_stabilize_parser
, &last_stabilize
);
332 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
334 gencache_stabilize();
341 static void gencache_del_parser(time_t timeout
, DATA_BLOB blob
,
345 bool *exists
= private_data
;
351 * Delete one entry from the cache file.
353 * @param keystr string that represents a key of this entry
355 * @retval true upon successful deletion
356 * @retval false in case of failure
359 bool gencache_del(const char *keystr
)
361 TDB_DATA key
= string_term_tdb_data(keystr
);
366 if (keystr
== NULL
) {
370 if (!gencache_init()) {
374 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr
));
376 ret
= tdb_chainlock(cache_notrans
->tdb
, key
);
381 gencache_parse(keystr
, gencache_del_parser
, &exists
);
385 * We delete an element by setting its timeout to
386 * 0. This way we don't have to do a transaction on
387 * gencache.tdb every time we delete an element.
389 result
= gencache_set(keystr
, "", 0);
392 tdb_chainunlock(cache_notrans
->tdb
, key
);
397 static bool gencache_pull_timeout(uint8_t *val
, time_t *pres
, char **payload
)
406 res
= strtol((char *)val
, &endptr
, 10);
408 if ((endptr
== NULL
) || (*endptr
!= '/')) {
409 DEBUG(2, ("Invalid gencache data format: %s\n", (char *)val
));
415 if (payload
!= NULL
) {
421 struct gencache_parse_state
{
422 void (*parser
)(time_t timeout
, DATA_BLOB blob
, void *private_data
);
424 bool copy_to_notrans
;
427 static int gencache_parse_fn(TDB_DATA key
, TDB_DATA data
, void *private_data
)
429 struct gencache_parse_state
*state
;
435 if (data
.dptr
== NULL
) {
438 ret
= gencache_pull_timeout(data
.dptr
, &t
, &payload
);
442 state
= (struct gencache_parse_state
*)private_data
;
443 blob
= data_blob_const(
444 payload
, data
.dsize
- PTR_DIFF(payload
, data
.dptr
));
445 state
->parser(t
, blob
, state
->private_data
);
447 if (state
->copy_to_notrans
) {
448 tdb_store(cache_notrans
->tdb
, key
, data
, 0);
454 bool gencache_parse(const char *keystr
,
455 void (*parser
)(time_t timeout
, DATA_BLOB blob
,
459 struct gencache_parse_state state
;
460 TDB_DATA key
= string_term_tdb_data(keystr
);
463 if (keystr
== NULL
) {
466 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
469 if (!gencache_init()) {
473 state
.parser
= parser
;
474 state
.private_data
= private_data
;
475 state
.copy_to_notrans
= false;
477 ret
= tdb_chainlock(cache_notrans
->tdb
, key
);
482 ret
= tdb_parse_record(cache_notrans
->tdb
, key
,
483 gencache_parse_fn
, &state
);
485 tdb_chainunlock(cache_notrans
->tdb
, key
);
489 state
.copy_to_notrans
= true;
491 ret
= tdb_parse_record(cache
->tdb
, key
, gencache_parse_fn
, &state
);
493 if ((ret
== -1) && (tdb_error(cache
->tdb
) == TDB_ERR_NOEXIST
)) {
495 * The record does not exist. Set a delete-marker in
496 * gencache_notrans, so that we don't have to look at
497 * the fcntl-based cache again.
499 gencache_set(keystr
, "", 0);
502 tdb_chainunlock(cache_notrans
->tdb
, key
);
507 struct gencache_get_data_blob_state
{
514 static void gencache_get_data_blob_parser(time_t timeout
, DATA_BLOB blob
,
517 struct gencache_get_data_blob_state
*state
=
518 (struct gencache_get_data_blob_state
*)private_data
;
521 state
->result
= false;
524 state
->timeout
= timeout
;
526 if (state
->blob
== NULL
) {
527 state
->result
= true;
531 *state
->blob
= data_blob_talloc(state
->mem_ctx
, blob
.data
,
533 if (state
->blob
->data
== NULL
) {
534 state
->result
= false;
537 state
->result
= true;
541 * Get existing entry from the cache file.
543 * @param keystr string that represents a key of this entry
544 * @param blob DATA_BLOB that is filled with entry's blob
545 * @param timeout pointer to a time_t that is filled with entry's
548 * @retval true when entry is successfully fetched
549 * @retval false for failure
552 bool gencache_get_data_blob(const char *keystr
, TALLOC_CTX
*mem_ctx
,
554 time_t *timeout
, bool *was_expired
)
556 struct gencache_get_data_blob_state state
;
557 bool expired
= false;
559 state
.result
= false;
560 state
.mem_ctx
= mem_ctx
;
563 if (!gencache_parse(keystr
, gencache_get_data_blob_parser
, &state
)) {
569 if (state
.timeout
<= time(NULL
)) {
571 * We're expired, delete the entry. We can't use gencache_del
572 * here, because that uses gencache_get_data_blob for checking
573 * the existence of a record. We know the thing exists and
574 * directly store an empty value with 0 timeout.
576 gencache_set(keystr
, "", 0);
581 *timeout
= state
.timeout
;
587 if (was_expired
!= NULL
) {
588 *was_expired
= expired
;
590 if (state
.result
&& state
.blob
) {
591 data_blob_free(state
.blob
);
596 struct stabilize_state
{
599 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
605 * Migrate the clear-if-first gencache data to the stable,
606 * transaction-based gencache.tdb
609 bool gencache_stabilize(void)
611 struct stabilize_state state
;
615 if (!gencache_init()) {
619 res
= tdb_transaction_start_nonblock(cache
->tdb
);
621 if (tdb_error(cache
->tdb
) == TDB_ERR_NOLOCK
)
624 * Someone else already does the stabilize,
625 * this does not have to be done twice
630 DEBUG(10, ("Could not start transaction on gencache.tdb: "
631 "%s\n", tdb_errorstr(cache
->tdb
)));
635 res
= tdb_lockall_nonblock(cache_notrans
->tdb
);
637 tdb_transaction_cancel(cache
->tdb
);
638 DEBUG(10, ("Could not get allrecord lock on "
639 "gencache_notrans.tdb: %s\n",
640 tdb_errorstr(cache_notrans
->tdb
)));
644 state
.written
= false;
646 res
= tdb_traverse(cache_notrans
->tdb
, stabilize_fn
, &state
);
648 tdb_unlockall(cache_notrans
->tdb
);
649 tdb_transaction_cancel(cache
->tdb
);
653 if (!state
.written
) {
654 tdb_unlockall(cache_notrans
->tdb
);
655 tdb_transaction_cancel(cache
->tdb
);
659 res
= tdb_transaction_commit(cache
->tdb
);
661 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
662 "%s\n", tdb_errorstr(cache
->tdb
)));
663 tdb_unlockall(cache_notrans
->tdb
);
667 res
= tdb_wipe_all(cache_notrans
->tdb
);
669 DBG_DEBUG("tdb_wipe_all on gencache_notrans.tdb failed: %s\n",
670 tdb_errorstr(cache_notrans
->tdb
));
673 now
= talloc_asprintf(talloc_tos(), "%d", (int)time(NULL
));
675 tdb_store(cache_notrans
->tdb
, last_stabilize_key(),
676 string_term_tdb_data(now
), 0);
680 res
= tdb_unlockall(cache_notrans
->tdb
);
682 DEBUG(10, ("tdb_unlockall on gencache.tdb failed: "
683 "%s\n", tdb_errorstr(cache
->tdb
)));
690 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
693 struct stabilize_state
*state
= (struct stabilize_state
*)priv
;
697 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
701 if (!gencache_pull_timeout(val
.dptr
, &timeout
, NULL
)) {
702 DEBUG(10, ("Ignoring invalid entry\n"));
705 if ((timeout
< time(NULL
)) || (val
.dsize
== 0)) {
706 res
= tdb_delete(cache
->tdb
, key
);
708 state
->written
= true;
709 } else if (tdb_error(cache
->tdb
) == TDB_ERR_NOEXIST
) {
713 res
= tdb_store(cache
->tdb
, key
, val
, 0);
715 state
->written
= true;
720 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
721 tdb_errorstr(cache
->tdb
)));
729 * Get existing entry from the cache file.
731 * @param keystr string that represents a key of this entry
732 * @param valstr buffer that is allocated and filled with the entry value
733 * buffer's disposing must be done outside
734 * @param timeout pointer to a time_t that is filled with entry's
737 * @retval true when entry is successfully fetched
738 * @retval false for failure
741 bool gencache_get(const char *keystr
, TALLOC_CTX
*mem_ctx
, char **value
,
747 ret
= gencache_get_data_blob(keystr
, mem_ctx
, &blob
, ptimeout
, NULL
);
751 if ((blob
.data
== NULL
) || (blob
.length
== 0)) {
752 data_blob_free(&blob
);
755 if (blob
.data
[blob
.length
-1] != '\0') {
756 /* Not NULL terminated, can't be a string */
757 data_blob_free(&blob
);
762 * talloc_move generates a type-punned warning here. As we
763 * leave the function immediately, do a simple talloc_steal.
765 *value
= (char *)talloc_steal(mem_ctx
, blob
.data
);
768 data_blob_free(&blob
);
773 * Set an entry in the cache file. If there's no such
776 * @param keystr string that represents a key of this entry
777 * @param value text representation value being cached
778 * @param timeout time when the value is expired
780 * @retval true when entry is successfully stored
781 * @retval false on failure
784 bool gencache_set(const char *keystr
, const char *value
, time_t timeout
)
786 DATA_BLOB blob
= data_blob_const(value
, strlen(value
)+1);
787 return gencache_set_data_blob(keystr
, blob
, timeout
);
790 struct gencache_iterate_blobs_state
{
791 void (*fn
)(const char *key
, DATA_BLOB value
,
792 time_t timeout
, void *private_data
);
798 static int gencache_iterate_blobs_fn(struct tdb_context
*tdb
, TDB_DATA key
,
799 TDB_DATA data
, void *priv
)
801 struct gencache_iterate_blobs_state
*state
=
802 (struct gencache_iterate_blobs_state
*)priv
;
804 char *free_key
= NULL
;
808 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
811 if (state
->in_persistent
&& tdb_exists(cache_notrans
->tdb
, key
)) {
815 if (key
.dptr
[key
.dsize
-1] == '\0') {
816 keystr
= (char *)key
.dptr
;
818 /* ensure 0-termination */
819 keystr
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
821 if (keystr
== NULL
) {
826 if (!gencache_pull_timeout(data
.dptr
, &timeout
, &payload
)) {
835 if (fnmatch(state
->pattern
, keystr
, 0) != 0) {
839 DEBUG(10, ("Calling function with arguments "
840 "(key=[%s], timeout=[%s])\n",
841 keystr
, timestring(talloc_tos(), timeout
)));
844 data_blob_const(payload
,
845 data
.dsize
- PTR_DIFF(payload
, data
.dptr
)),
846 timeout
, state
->private_data
);
849 TALLOC_FREE(free_key
);
853 void gencache_iterate_blobs(void (*fn
)(const char *key
, DATA_BLOB value
,
854 time_t timeout
, void *private_data
),
855 void *private_data
, const char *pattern
)
857 struct gencache_iterate_blobs_state state
;
859 if ((fn
== NULL
) || (pattern
== NULL
) || !gencache_init()) {
863 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern
));
866 state
.pattern
= pattern
;
867 state
.private_data
= private_data
;
869 state
.in_persistent
= false;
870 tdb_traverse(cache_notrans
->tdb
, gencache_iterate_blobs_fn
, &state
);
872 state
.in_persistent
= true;
873 tdb_traverse(cache
->tdb
, gencache_iterate_blobs_fn
, &state
);
877 * Iterate through all entries which key matches to specified pattern
879 * @param fn pointer to the function that will be supplied with each single
880 * matching cache entry (key, value and timeout) as an arguments
881 * @param data void pointer to an arbitrary data that is passed directly to the fn
882 * function on each call
883 * @param keystr_pattern pattern the existing entries' keys are matched to
887 struct gencache_iterate_state
{
888 void (*fn
)(const char *key
, const char *value
, time_t timeout
,
893 static void gencache_iterate_fn(const char *key
, DATA_BLOB value
,
894 time_t timeout
, void *private_data
)
896 struct gencache_iterate_state
*state
=
897 (struct gencache_iterate_state
*)private_data
;
899 char *free_val
= NULL
;
901 if (value
.data
[value
.length
-1] == '\0') {
902 valstr
= (char *)value
.data
;
904 /* ensure 0-termination */
905 valstr
= talloc_strndup(talloc_tos(), (char *)value
.data
, value
.length
);
907 if (valstr
== NULL
) {
912 DEBUG(10, ("Calling function with arguments "
913 "(key=[%s], value=[%s], timeout=[%s])\n",
914 key
, valstr
, timestring(talloc_tos(), timeout
)));
916 state
->fn(key
, valstr
, timeout
, state
->private_data
);
920 TALLOC_FREE(free_val
);
923 void gencache_iterate(void (*fn
)(const char *key
, const char *value
,
924 time_t timeout
, void *dptr
),
925 void *private_data
, const char *pattern
)
927 struct gencache_iterate_state state
;
933 state
.private_data
= private_data
;
934 gencache_iterate_blobs(gencache_iterate_fn
, &state
, pattern
);