1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-27 03:21:53 +03:00
samba-mirror/source3/smbd/statcache.c
Jeremy Allison f01698027d r23844: Add patch series from Volker (after review and consultation).
0001-Save-a-strdup-in-stat_cache_add.patch
0002-Use-ISDOT-and-ISDOTDOT.patch
0003-Move-fname_equal-around.patch
0004-unix_convert-pstring-dirpath-char.patch
0005-Ignore-.o-files.patch
0006-Get-rid-of-pstrings-inside-unix_convert.patch
0007-revert-pstring-unix_convert.patch
0008-Make-name-an-allocated-pstring-inside-unix_convert.patch
0009-Pass-explicit-pstring-to-mangle_check_cache.patch
0010-Don-t-overwrite-orig_path-unnecessarily.patch
0011-Defer-allocating-name.patch
0012-Make-sure-dirpath-is-always-correctly-allocated.patch
0013-Remove-one-pstring-dependency-in-unix_convert.patch
0014-Remove-more-name-pstring-dependencies.patch
0015-Hide-the-nasty-API-of-mangle_check_cache-in-mangle_c.patch
0016-name-does-not-need-to-be-pstring-size-anymore.patch
0017-Make-use-of-ISDOT-and-ISDOTDOT.patch
0018-Remove-pstring-from-stat_cache_lookup.patch
0019-Add-my-copyright.patch

To remove pstrings from statcache and unix_convert.

Jeremy.
(This used to be commit ea6ef36889)
2007-10-10 12:28:34 -05:00

383 lines
10 KiB
C

/*
Unix SMB/CIFS implementation.
stat cache code
Copyright (C) Andrew Tridgell 1992-2000
Copyright (C) Jeremy Allison 1999-2004
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
Copyright (C) Volker Lendecke 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
/****************************************************************************
Stat cache code used in unix_convert.
*****************************************************************************/
static TDB_CONTEXT *tdb_stat_cache;
/**
* Add an entry into the stat cache.
*
* @param full_orig_name The original name as specified by the client
* @param orig_translated_path The name on our filesystem.
*
* @note Only the first strlen(orig_translated_path) characters are stored
* into the cache. This means that full_orig_name will be internally
* truncated.
*
*/
void stat_cache_add( const char *full_orig_name, const 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();
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 ..
*/
if ((*full_orig_name == '\0')
|| ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
return;
}
/*
* If we are in case insentive mode, we don't need to
* store names that need no translation - else, it
* would be a waste.
*/
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] == '/') {
translated_path_length--;
}
if(case_sensitive) {
original_path = SMB_STRDUP(full_orig_name);
} else {
original_path = strdup_upper(full_orig_name);
}
if (!original_path) {
return;
}
original_path_length = strlen(original_path);
if(original_path[original_path_length-1] == '/') {
original_path[original_path_length-1] = '\0';
original_path_length--;
}
if (original_path_length != translated_path_length) {
if (original_path_length < translated_path_length) {
DEBUG(0, ("OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)!\n",
original_path, (unsigned long)original_path_length, translated_path, (unsigned long)translated_path_length));
SAFE_FREE(original_path);
return;
}
/* we only want to index by the first part of original_path,
up to the length of translated_path */
original_path[translated_path_length] = '\0';
original_path_length = translated_path_length;
}
/*
* New entry or replace old entry.
*/
data_val.dsize = translated_path_length + 1;
data_val.dptr = (uint8 *)translated_path;
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));
}
SAFE_FREE(original_path);
}
/**
* Look through the stat cache for an entry
*
* @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.
* @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.
*
* @return True if we translated (and did a scuccessful stat on) the entire name.
*
*/
BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
char **start, SMB_STRUCT_STAT *pst)
{
char *chk_name;
size_t namelen;
BOOL sizechanged = False;
unsigned int num_components = 0;
char *translated_path;
size_t translated_path_length;
TDB_DATA data_val;
char *name;
if (!lp_stat_cache())
return False;
name = *pname;
namelen = strlen(name);
*start = name;
DO_PROFILE_INC(statcache_lookups);
/*
* Don't lookup trivial valid directory entries.
*/
if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
return False;
}
if (conn->case_sensitive) {
chk_name = SMB_STRDUP(name);
if (!chk_name) {
DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
return False;
}
} else {
chk_name = strdup_upper(name);
if (!chk_name) {
DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
return False;
}
/*
* In some language encodings the length changes
* if we uppercase. We need to treat this differently
* below.
*/
if (strlen(chk_name) != namelen)
sizechanged = True;
}
while (1) {
char *sp;
data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
if (data_val.dptr != NULL && data_val.dsize != 0) {
break;
}
DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
/*
* Didn't find it - remove last component for next try.
*/
if (!(sp = strrchr_m(chk_name, '/'))) {
/*
* We reached the end of the name - no match.
*/
DO_PROFILE_INC(statcache_misses);
SAFE_FREE(chk_name);
return False;
}
*sp = '\0';
/*
* Count the number of times we have done this, we'll
* need it when reconstructing the string.
*/
if (sizechanged)
num_components++;
if ((*chk_name == '\0')
|| ISDOT(chk_name) || ISDOTDOT(chk_name)) {
DO_PROFILE_INC(statcache_misses);
SAFE_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';
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) {
/* 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);
return False;
}
if (!sizechanged) {
memcpy(name, translated_path,
MIN(namelen, translated_path_length));
}
else {
if (num_components == 0) {
name = SMB_STRNDUP(translated_path,
translated_path_length);
} else {
char *sp;
sp = strnrchr_m(name, '/', num_components);
if (sp) {
asprintf(&name, "%.*s%s",
(int)translated_path_length,
translated_path, sp);
} else {
name = SMB_STRNDUP(translated_path,
translated_path_length);
}
}
if (name == NULL) {
/*
* TODO: Get us out of here with a real error message
*/
smb_panic("malloc failed");
}
SAFE_FREE(*pname);
*pname = name;
}
/* set pointer for 'where to start' on fixing the rest of the name */
*start = &name[translated_path_length];
if (**start == '/')
++*start;
*dirpath = translated_path;
SAFE_FREE(chk_name);
return (namelen == translated_path_length);
}
/***************************************************************************
Tell all smbd's to delete an entry.
**************************************************************************/
void send_stat_cache_delete_message(const char *name)
{
#ifdef DEVELOPER
message_send_all(smbd_messaging_context(),
MSG_SMB_STAT_CACHE_DELETE,
name,
strlen(name)+1,
NULL);
#endif
}
/***************************************************************************
Delete an entry.
**************************************************************************/
void stat_cache_delete(const char *name)
{
char *lname = strdup_upper(name);
if (!lname) {
return;
}
DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
lname, name ));
tdb_delete_bystring(tdb_stat_cache, lname);
SAFE_FREE(lname);
}
/***************************************************************
Compute a hash value based on a string key value.
The function returns the bucket index number for the hashed key.
JRA. Use a djb-algorithm hash for speed.
***************************************************************/
unsigned int fast_string_hash(TDB_DATA *key)
{
unsigned int n = 0;
const char *p;
for (p = (const char *)key->dptr; *p != '\0'; p++) {
n = ((n << 5) + n) ^ (unsigned int)(*p);
}
return n;
}
/***************************************************************************
Initializes or clears the stat cache.
**************************************************************************/
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);
if (!tdb_stat_cache)
return False;
return True;
}