2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1999-2004
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7 Copyright (C) Volker Lendecke 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /****************************************************************************
26 Stat cache code used in unix_convert.
27 *****************************************************************************/
29 static TDB_CONTEXT *tdb_stat_cache;
32 * Add an entry into the stat cache.
34 * @param full_orig_name The original name as specified by the client
35 * @param orig_translated_path The name on our filesystem.
37 * @note Only the first strlen(orig_translated_path) characters are stored
38 * into the cache. This means that full_orig_name will be internally
43 void stat_cache_add( const char *full_orig_name, const char *translated_path,
46 size_t translated_path_length;
49 size_t original_path_length;
50 size_t sc_size = lp_max_stat_cache_size();
55 if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
59 ZERO_STRUCT(data_val);
62 * Don't cache trivial valid directory entries such as . and ..
65 if ((*full_orig_name == '\0')
66 || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
71 * If we are in case insentive mode, we don't need to
72 * store names that need no translation - else, it
76 if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0))
80 * Remove any trailing '/' characters from the
85 * To save a strdup we don't necessarily 0-terminate the translated
86 * path in the tdb. Instead, we do it directly after the tdb_fetch in
90 translated_path_length = strlen(translated_path);
92 if(translated_path[translated_path_length-1] == '/') {
93 translated_path_length--;
97 original_path = SMB_STRDUP(full_orig_name);
99 original_path = strdup_upper(full_orig_name);
102 if (!original_path) {
106 original_path_length = strlen(original_path);
108 if(original_path[original_path_length-1] == '/') {
109 original_path[original_path_length-1] = '\0';
110 original_path_length--;
113 if (original_path_length != translated_path_length) {
114 if (original_path_length < translated_path_length) {
115 DEBUG(0, ("OOPS - tried to store stat cache entry "
116 "for weird length paths [%s] %lu and [%s] %lu)!\n",
118 (unsigned long)original_path_length,
120 (unsigned long)translated_path_length));
121 SAFE_FREE(original_path);
125 /* we only want to index by the first part of original_path,
126 up to the length of translated_path */
128 original_path[translated_path_length] = '\0';
129 original_path_length = translated_path_length;
133 * New entry or replace old entry.
136 data_val.dsize = translated_path_length + 1;
137 data_val.dptr = (uint8 *)translated_path;
139 if (tdb_store_bystring(tdb_stat_cache, original_path, data_val,
141 DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n",
142 original_path, translated_path));
144 DEBUG(5,("stat_cache_add: Added entry (%lx:size%x) %s -> %s\n",
145 (unsigned long)data_val.dptr,
146 (unsigned int)data_val.dsize,
151 SAFE_FREE(original_path);
155 * Look through the stat cache for an entry
157 * @param conn A connection struct to do the stat() with.
158 * @param name The path we are attempting to cache, modified by this routine
159 * to be correct as far as the cache can tell us. We assume that
160 * it is a malloc'ed string, we free it if necessary.
161 * @param dirpath The path as far as the stat cache told us.
162 * @param start A pointer into name, for where to 'start' in fixing the rest
164 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
166 * @return True if we translated (and did a scuccessful stat on) the entire
171 BOOL stat_cache_lookup(connection_struct *conn, char **pname, char **dirpath,
172 char **start, SMB_STRUCT_STAT *pst)
176 BOOL sizechanged = False;
177 unsigned int num_components = 0;
178 char *translated_path;
179 size_t translated_path_length;
183 if (!lp_stat_cache())
187 namelen = strlen(name);
191 DO_PROFILE_INC(statcache_lookups);
194 * Don't lookup trivial valid directory entries.
196 if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
200 if (conn->case_sensitive) {
201 chk_name = SMB_STRDUP(name);
203 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
208 chk_name = strdup_upper(name);
210 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
215 * In some language encodings the length changes
216 * if we uppercase. We need to treat this differently
219 if (strlen(chk_name) != namelen)
226 data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
228 if (data_val.dptr != NULL && data_val.dsize != 0) {
232 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
235 * Didn't find it - remove last component for next try.
237 if (!(sp = strrchr_m(chk_name, '/'))) {
239 * We reached the end of the name - no match.
241 DO_PROFILE_INC(statcache_misses);
249 * Count the number of times we have done this, we'll
250 * need it when reconstructing the string.
255 if ((*chk_name == '\0')
256 || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
257 DO_PROFILE_INC(statcache_misses);
263 translated_path = (char *)data_val.dptr;
264 translated_path_length = data_val.dsize - 1;
267 * In stat_cache_add we did not necessarily 0-terminate the translated
268 * path. Do it here, where we do have a freshly malloc'ed blob.
270 translated_path[translated_path_length] = '\0';
272 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
273 "-> [%s]\n", chk_name, translated_path ));
274 DO_PROFILE_INC(statcache_hits);
276 if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
277 /* Discard this entry - it doesn't exist in the filesystem. */
278 tdb_delete_bystring(tdb_stat_cache, chk_name);
280 SAFE_FREE(data_val.dptr);
285 memcpy(name, translated_path,
286 MIN(namelen, translated_path_length));
289 if (num_components == 0) {
290 name = SMB_STRNDUP(translated_path,
291 translated_path_length);
295 sp = strnrchr_m(name, '/', num_components);
297 asprintf(&name, "%.*s%s",
298 (int)translated_path_length,
299 translated_path, sp);
301 name = SMB_STRNDUP(translated_path,
302 translated_path_length);
307 * TODO: Get us out of here with a real error message
309 smb_panic("malloc failed");
316 /* set pointer for 'where to start' on fixing the rest of the name */
317 *start = &name[translated_path_length];
321 *dirpath = translated_path;
323 return (namelen == translated_path_length);
326 /***************************************************************************
327 Tell all smbd's to delete an entry.
328 **************************************************************************/
330 void send_stat_cache_delete_message(const char *name)
333 message_send_all(smbd_messaging_context(),
334 MSG_SMB_STAT_CACHE_DELETE,
341 /***************************************************************************
343 **************************************************************************/
345 void stat_cache_delete(const char *name)
347 char *lname = strdup_upper(name);
352 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
355 tdb_delete_bystring(tdb_stat_cache, lname);
359 /***************************************************************
360 Compute a hash value based on a string key value.
361 The function returns the bucket index number for the hashed key.
362 JRA. Use a djb-algorithm hash for speed.
363 ***************************************************************/
365 unsigned int fast_string_hash(TDB_DATA *key)
369 for (p = (const char *)key->dptr; *p != '\0'; p++) {
370 n = ((n << 5) + n) ^ (unsigned int)(*p);
375 /***************************************************************************
376 Initializes or clears the stat cache.
377 **************************************************************************/
379 BOOL reset_stat_cache( void )
381 if (!lp_stat_cache())
384 if (tdb_stat_cache) {
385 tdb_close(tdb_stat_cache);
388 /* Create the in-memory tdb using our custom hash function. */
389 tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
390 (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);