s3-param use common struct parmlist_entry to hold parametric options
[samba.git] / source3 / smbd / statcache.c
index d2f09930a0b833b389bd196c43c23c49e4ba0758..963b7c4bc1321bdcebb16ba7294e708cb4d8475d 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/CIFS implementation.
    stat cache code
    Copyright (C) Andrew Tridgell 1992-2000
-   Copyright (C) Jeremy Allison 1999-2004
+   Copyright (C) Jeremy Allison 1999-2007
    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
    Copyright (C) Volker Lendecke 2007
 
 */
 
 #include "includes.h"
+#include "memcache.h"
+#include "smbd/smbd.h"
+#include "messages.h"
+#include "smbprofile.h"
+#include "tdb_compat.h"
 
 /****************************************************************************
  Stat cache code used in unix_convert.
 *****************************************************************************/
 
-static TDB_CONTEXT *tdb_stat_cache;
-
 /**
  * Add an entry into the stat cache.
  *
@@ -40,24 +43,20 @@ static TDB_CONTEXT *tdb_stat_cache;
  *
  */
 
-void stat_cache_add( const char *full_orig_name, const char *translated_path,
-               BOOL case_sensitive)
+void stat_cache_add( const char *full_orig_name,
+               char *translated_path,
+               bool case_sensitive)
 {
        size_t translated_path_length;
-       TDB_DATA data_val;
        char *original_path;
        size_t original_path_length;
-       size_t sc_size = lp_max_stat_cache_size();
+       char saved_char;
+       TALLOC_CTX *ctx = talloc_tos();
 
-       if (!lp_stat_cache())
+       if (!lp_stat_cache()) {
                return;
-
-       if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
-               reset_stat_cache();
        }
 
-       ZERO_STRUCT(data_val);
-
        /*
         * Don't cache trivial valid directory entries such as . and ..
         */
@@ -73,20 +72,15 @@ void stat_cache_add( const char *full_orig_name, const char *translated_path,
         * would be a waste.
         */
 
-       if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0))
+       if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
                return;
+       }
 
        /*
         * Remove any trailing '/' characters from the
         * translated path.
         */
 
-       /*
-        * To save a strdup we don't necessarily 0-terminate the translated
-        * path in the tdb. Instead, we do it directly after the tdb_fetch in
-        * stat_cache_lookup.
-        */
-
        translated_path_length = strlen(translated_path);
 
        if(translated_path[translated_path_length-1] == '/') {
@@ -94,9 +88,9 @@ void stat_cache_add( const char *full_orig_name, const char *translated_path,
        }
 
        if(case_sensitive) {
-               original_path = SMB_STRDUP(full_orig_name);
+               original_path = talloc_strdup(ctx,full_orig_name);
        } else {
-               original_path = strdup_upper(full_orig_name);
+               original_path = talloc_strdup_upper(ctx,full_orig_name);
        }
 
        if (!original_path) {
@@ -118,7 +112,7 @@ void stat_cache_add( const char *full_orig_name, const char *translated_path,
                                  (unsigned long)original_path_length,
                                  translated_path,
                                  (unsigned long)translated_path_length));
-                       SAFE_FREE(original_path);
+                       TALLOC_FREE(original_path);
                        return;
                }
 
@@ -129,26 +123,27 @@ void stat_cache_add( const char *full_orig_name, const char *translated_path,
                original_path_length = translated_path_length;
        }
 
+       /* Ensure we're null terminated. */
+       saved_char = translated_path[translated_path_length];
+       translated_path[translated_path_length] = '\0';
+
        /*
         * New entry or replace old entry.
         */
 
-       data_val.dsize = translated_path_length + 1;
-       data_val.dptr = (uint8 *)translated_path;
+       memcache_add(
+               smbd_memcache(), STAT_CACHE,
+               data_blob_const(original_path, original_path_length),
+               data_blob_const(translated_path, translated_path_length + 1));
 
-       if (tdb_store_bystring(tdb_stat_cache, original_path, data_val,
-                               TDB_REPLACE) != 0) {
-               DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n",
-                                       original_path, translated_path));
-       } else {
-               DEBUG(5,("stat_cache_add: Added entry (%lx:size%x) %s -> %s\n",
-                       (unsigned long)data_val.dptr,
-                       (unsigned int)data_val.dsize,
-                       original_path,
-                       translated_path));
-       }
+       DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
+                (unsigned long)translated_path,
+                (unsigned int)translated_path_length,
+                original_path,
+                translated_path));
 
-       SAFE_FREE(original_path);
+       translated_path[translated_path_length] = saved_char;
+       TALLOC_FREE(original_path);
 }
 
 /**
@@ -157,8 +152,10 @@ void stat_cache_add( const char *full_orig_name, const char *translated_path,
  * @param conn    A connection struct to do the stat() with.
  * @param name    The path we are attempting to cache, modified by this routine
  *                to be correct as far as the cache can tell us. We assume that
- *               it is a malloc'ed string, we free it if necessary.
- * @param dirpath The path as far as the stat cache told us.
+ *               it is a talloc'ed string from top of stack, we free it if
+ *               necessary.
+ * @param dirpath The path as far as the stat cache told us. Also talloced
+ *               from top of stack.
  * @param start   A pointer into name, for where to 'start' in fixing the rest
  *               of the name up.
  * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
@@ -168,26 +165,33 @@ void stat_cache_add( const char *full_orig_name, const char *translated_path,
  *
  */
 
-BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
-                      char **start, SMB_STRUCT_STAT *pst)
+bool stat_cache_lookup(connection_struct *conn,
+                       char **pp_name,
+                       char **pp_dirpath,
+                       char **pp_start,
+                       SMB_STRUCT_STAT *pst)
 {
        char *chk_name;
        size_t namelen;
-       BOOL sizechanged = False;
+       bool sizechanged = False;
        unsigned int num_components = 0;
        char *translated_path;
        size_t translated_path_length;
-       TDB_DATA data_val;
+       DATA_BLOB data_val;
        char *name;
+       TALLOC_CTX *ctx = talloc_tos();
+       struct smb_filename smb_fname;
 
-       if (!lp_stat_cache())
+       *pp_dirpath = NULL;
+       *pp_start = *pp_name;
+
+       if (!lp_stat_cache()) {
                return False;
+       }
 
-       name = *pname;
+       name = *pp_name;
        namelen = strlen(name);
 
-       *start = name;
-
        DO_PROFILE_INC(statcache_lookups);
 
        /*
@@ -198,16 +202,16 @@ BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
        }
 
        if (conn->case_sensitive) {
-               chk_name = SMB_STRDUP(name);
+               chk_name = talloc_strdup(ctx,name);
                if (!chk_name) {
                        DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
                        return False;
                }
 
        } else {
-               chk_name = strdup_upper(name);
+               chk_name = talloc_strdup_upper(ctx,name);
                if (!chk_name) {
-                       DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
+                       DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
                        return False;
                }
 
@@ -216,16 +220,20 @@ BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
                 * if we uppercase. We need to treat this differently
                 * below.
                 */
-               if (strlen(chk_name) != namelen)
+               if (strlen(chk_name) != namelen) {
                        sizechanged = True;
+               }
        }
 
        while (1) {
                char *sp;
 
-               data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
+               data_val = data_blob_null;
 
-               if (data_val.dptr != NULL && data_val.dsize != 0) {
+               if (memcache_lookup(
+                           smbd_memcache(), STAT_CACHE,
+                           data_blob_const(chk_name, strlen(chk_name)),
+                           &data_val)) {
                        break;
                }
 
@@ -239,7 +247,7 @@ BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
                         * We reached the end of the name - no match.
                         */
                        DO_PROFILE_INC(statcache_misses);
-                       SAFE_FREE(chk_name);
+                       TALLOC_FREE(chk_name);
                        return False;
                }
 
@@ -249,77 +257,82 @@ BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
                 * Count the number of times we have done this, we'll
                 * need it when reconstructing the string.
                 */
-               if (sizechanged)
+
+               if (sizechanged) {
                        num_components++;
+               }
 
                if ((*chk_name == '\0')
                    || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
                        DO_PROFILE_INC(statcache_misses);
-                       SAFE_FREE(chk_name);
+                       TALLOC_FREE(chk_name);
                        return False;
                }
        }
 
-       translated_path = (char *)data_val.dptr;
-       translated_path_length = data_val.dsize - 1;
-
-       /*
-        * In stat_cache_add we did not necessarily 0-terminate the translated
-        * path. Do it here, where we do have a freshly malloc'ed blob.
-        */
-       translated_path[translated_path_length] = '\0';
+       translated_path = talloc_strdup(ctx,(char *)data_val.data);
+       if (!translated_path) {
+               smb_panic("talloc failed");
+       }
+       translated_path_length = data_val.length - 1;
 
        DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
                  "-> [%s]\n", chk_name, translated_path ));
        DO_PROFILE_INC(statcache_hits);
 
-       if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
+       ZERO_STRUCT(smb_fname);
+       smb_fname.base_name = translated_path;
+
+       if (SMB_VFS_STAT(conn, &smb_fname) != 0) {
                /* Discard this entry - it doesn't exist in the filesystem. */
-               tdb_delete_bystring(tdb_stat_cache, chk_name);
-               SAFE_FREE(chk_name);
-               SAFE_FREE(data_val.dptr);
+               memcache_delete(smbd_memcache(), STAT_CACHE,
+                               data_blob_const(chk_name, strlen(chk_name)));
+               TALLOC_FREE(chk_name);
+               TALLOC_FREE(translated_path);
                return False;
        }
+       *pst = smb_fname.st;
 
        if (!sizechanged) {
-               memcpy(name, translated_path,
+               memcpy(*pp_name, translated_path,
                       MIN(namelen, translated_path_length));
-       }
-       else {
+       } else {
                if (num_components == 0) {
-                       name = SMB_STRNDUP(translated_path,
+                       name = talloc_strndup(ctx, translated_path,
                                           translated_path_length);
                } else {
                        char *sp;
 
                        sp = strnrchr_m(name, '/', num_components);
                        if (sp) {
-                               asprintf(&name, "%.*s%s",
+                               name = talloc_asprintf(ctx,"%.*s%s",
                                         (int)translated_path_length,
                                         translated_path, sp);
                        } else {
-                               name = SMB_STRNDUP(translated_path,
-                                                  translated_path_length);
+                               name = talloc_strndup(ctx,
+                                               translated_path,
+                                               translated_path_length);
                        }
                }
                if (name == NULL) {
                        /*
                         * TODO: Get us out of here with a real error message
                         */
-                       smb_panic("malloc failed");
+                       smb_panic("talloc failed");
                }
-               SAFE_FREE(*pname);
-               *pname = name;
+               TALLOC_FREE(*pp_name);
+               *pp_name = name;
        }
 
 
        /* set pointer for 'where to start' on fixing the rest of the name */
-       *start = &name[translated_path_length];
-       if (**start == '/')
-               ++*start;
+       *pp_start = &name[translated_path_length];
+       if (**pp_start == '/') {
+               ++*pp_start;
+       }
 
-       *dirpath = translated_path;
-       SAFE_FREE(chk_name);
+       *pp_dirpath = translated_path;
+       TALLOC_FREE(chk_name);
        return (namelen == translated_path_length);
 }
 
@@ -327,10 +340,11 @@ BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
  Tell all smbd's to delete an entry.
 **************************************************************************/
 
-void send_stat_cache_delete_message(const char *name)
+void smbd_send_stat_cache_delete_message(struct messaging_context *msg_ctx,
+                                        const char *name)
 {
 #ifdef DEVELOPER
-       message_send_all(smbd_messaging_context(),
+       message_send_all(msg_ctx,
                        MSG_SMB_STAT_CACHE_DELETE,
                        name,
                        strlen(name)+1,
@@ -344,7 +358,7 @@ void send_stat_cache_delete_message(const char *name)
 
 void stat_cache_delete(const char *name)
 {
-       char *lname = strdup_upper(name);
+       char *lname = talloc_strdup_upper(talloc_tos(), name);
 
        if (!lname) {
                return;
@@ -352,8 +366,9 @@ void stat_cache_delete(const char *name)
        DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
                        lname, name ));
 
-       tdb_delete_bystring(tdb_stat_cache, lname);
-       SAFE_FREE(lname);
+       memcache_delete(smbd_memcache(), STAT_CACHE,
+                       data_blob_const(lname, talloc_get_size(lname)-1));
+       TALLOC_FREE(lname);
 }
 
 /***************************************************************
@@ -376,20 +391,12 @@ unsigned int fast_string_hash(TDB_DATA *key)
  Initializes or clears the stat cache.
 **************************************************************************/
 
-BOOL reset_stat_cache( void )
+bool reset_stat_cache( void )
 {
        if (!lp_stat_cache())
                return True;
 
-       if (tdb_stat_cache) {
-               tdb_close(tdb_stat_cache);
-       }
-
-       /* Create the in-memory tdb using our custom hash function. */
-       tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
-                               (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
+       memcache_flush(smbd_memcache(), STAT_CACHE);
 
-       if (!tdb_stat_cache)
-               return False;
        return True;
 }