s3: Rename new parameter "ldap ref follow" to "ldap follow referral".
[Samba/gebeck_regimport.git] / source3 / smbd / statcache.c
blobda52cc05d4e0f0dab797afd063f0f33cb85c6a90
1 /*
2 Unix SMB/CIFS implementation.
3 stat cache code
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1999-2007
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/>.
23 #include "includes.h"
25 /****************************************************************************
26 Stat cache code used in unix_convert.
27 *****************************************************************************/
29 /**
30 * Add an entry into the stat cache.
32 * @param full_orig_name The original name as specified by the client
33 * @param orig_translated_path The name on our filesystem.
35 * @note Only the first strlen(orig_translated_path) characters are stored
36 * into the cache. This means that full_orig_name will be internally
37 * truncated.
41 void stat_cache_add( const char *full_orig_name,
42 char *translated_path,
43 bool case_sensitive)
45 size_t translated_path_length;
46 char *original_path;
47 size_t original_path_length;
48 char saved_char;
49 TALLOC_CTX *ctx = talloc_tos();
51 if (!lp_stat_cache()) {
52 return;
56 * Don't cache trivial valid directory entries such as . and ..
59 if ((*full_orig_name == '\0')
60 || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
61 return;
65 * If we are in case insentive mode, we don't need to
66 * store names that need no translation - else, it
67 * would be a waste.
70 if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
71 return;
75 * Remove any trailing '/' characters from the
76 * translated path.
79 translated_path_length = strlen(translated_path);
81 if(translated_path[translated_path_length-1] == '/') {
82 translated_path_length--;
85 if(case_sensitive) {
86 original_path = talloc_strdup(ctx,full_orig_name);
87 } else {
88 original_path = talloc_strdup_upper(ctx,full_orig_name);
91 if (!original_path) {
92 return;
95 original_path_length = strlen(original_path);
97 if(original_path[original_path_length-1] == '/') {
98 original_path[original_path_length-1] = '\0';
99 original_path_length--;
102 if (original_path_length != translated_path_length) {
103 if (original_path_length < translated_path_length) {
104 DEBUG(0, ("OOPS - tried to store stat cache entry "
105 "for weird length paths [%s] %lu and [%s] %lu)!\n",
106 original_path,
107 (unsigned long)original_path_length,
108 translated_path,
109 (unsigned long)translated_path_length));
110 TALLOC_FREE(original_path);
111 return;
114 /* we only want to index by the first part of original_path,
115 up to the length of translated_path */
117 original_path[translated_path_length] = '\0';
118 original_path_length = translated_path_length;
121 /* Ensure we're null terminated. */
122 saved_char = translated_path[translated_path_length];
123 translated_path[translated_path_length] = '\0';
126 * New entry or replace old entry.
129 memcache_add(
130 smbd_memcache(), STAT_CACHE,
131 data_blob_const(original_path, original_path_length),
132 data_blob_const(translated_path, translated_path_length + 1));
134 DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
135 (unsigned long)translated_path,
136 (unsigned int)translated_path_length,
137 original_path,
138 translated_path));
140 translated_path[translated_path_length] = saved_char;
141 TALLOC_FREE(original_path);
145 * Look through the stat cache for an entry
147 * @param conn A connection struct to do the stat() with.
148 * @param name The path we are attempting to cache, modified by this routine
149 * to be correct as far as the cache can tell us. We assume that
150 * it is a talloc'ed string from top of stack, we free it if
151 * necessary.
152 * @param dirpath The path as far as the stat cache told us. Also talloced
153 * from top of stack.
154 * @param start A pointer into name, for where to 'start' in fixing the rest
155 * of the name up.
156 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
158 * @return True if we translated (and did a scuccessful stat on) the entire
159 * name.
163 bool stat_cache_lookup(connection_struct *conn,
164 char **pp_name,
165 char **pp_dirpath,
166 char **pp_start,
167 SMB_STRUCT_STAT *pst)
169 char *chk_name;
170 size_t namelen;
171 bool sizechanged = False;
172 unsigned int num_components = 0;
173 char *translated_path;
174 size_t translated_path_length;
175 DATA_BLOB data_val;
176 char *name;
177 TALLOC_CTX *ctx = talloc_tos();
178 struct smb_filename *smb_fname = NULL;
179 NTSTATUS status;
181 *pp_dirpath = NULL;
182 *pp_start = *pp_name;
184 if (!lp_stat_cache()) {
185 return False;
188 name = *pp_name;
189 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)) {
197 return False;
200 if (conn->case_sensitive) {
201 chk_name = talloc_strdup(ctx,name);
202 if (!chk_name) {
203 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
204 return False;
207 } else {
208 chk_name = talloc_strdup_upper(ctx,name);
209 if (!chk_name) {
210 DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
211 return False;
215 * In some language encodings the length changes
216 * if we uppercase. We need to treat this differently
217 * below.
219 if (strlen(chk_name) != namelen) {
220 sizechanged = True;
224 while (1) {
225 char *sp;
227 data_val = data_blob_null;
229 if (memcache_lookup(
230 smbd_memcache(), STAT_CACHE,
231 data_blob_const(chk_name, strlen(chk_name)),
232 &data_val)) {
233 break;
236 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
237 chk_name ));
239 * Didn't find it - remove last component for next try.
241 if (!(sp = strrchr_m(chk_name, '/'))) {
243 * We reached the end of the name - no match.
245 DO_PROFILE_INC(statcache_misses);
246 TALLOC_FREE(chk_name);
247 return False;
250 *sp = '\0';
253 * Count the number of times we have done this, we'll
254 * need it when reconstructing the string.
257 if (sizechanged) {
258 num_components++;
261 if ((*chk_name == '\0')
262 || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
263 DO_PROFILE_INC(statcache_misses);
264 TALLOC_FREE(chk_name);
265 return False;
269 translated_path = talloc_strdup(ctx,(char *)data_val.data);
270 if (!translated_path) {
271 smb_panic("talloc failed");
273 translated_path_length = data_val.length - 1;
275 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
276 "-> [%s]\n", chk_name, translated_path ));
277 DO_PROFILE_INC(statcache_hits);
279 status = create_synthetic_smb_fname(talloc_tos(), translated_path,
280 NULL, NULL, &smb_fname);
281 if (!NT_STATUS_IS_OK(status)) {
282 TALLOC_FREE(chk_name);
283 TALLOC_FREE(translated_path);
284 return false;
287 if (SMB_VFS_STAT(conn, smb_fname) != 0) {
288 /* Discard this entry - it doesn't exist in the filesystem. */
289 memcache_delete(smbd_memcache(), STAT_CACHE,
290 data_blob_const(chk_name, strlen(chk_name)));
291 TALLOC_FREE(chk_name);
292 TALLOC_FREE(translated_path);
293 TALLOC_FREE(smb_fname);
294 return False;
296 *pst = smb_fname->st;
297 TALLOC_FREE(smb_fname);
299 if (!sizechanged) {
300 memcpy(*pp_name, translated_path,
301 MIN(namelen, translated_path_length));
302 } else {
303 if (num_components == 0) {
304 name = talloc_strndup(ctx, translated_path,
305 translated_path_length);
306 } else {
307 char *sp;
309 sp = strnrchr_m(name, '/', num_components);
310 if (sp) {
311 name = talloc_asprintf(ctx,"%.*s%s",
312 (int)translated_path_length,
313 translated_path, sp);
314 } else {
315 name = talloc_strndup(ctx,
316 translated_path,
317 translated_path_length);
320 if (name == NULL) {
322 * TODO: Get us out of here with a real error message
324 smb_panic("talloc failed");
326 TALLOC_FREE(*pp_name);
327 *pp_name = name;
331 /* set pointer for 'where to start' on fixing the rest of the name */
332 *pp_start = &name[translated_path_length];
333 if (**pp_start == '/') {
334 ++*pp_start;
337 *pp_dirpath = translated_path;
338 TALLOC_FREE(chk_name);
339 return (namelen == translated_path_length);
342 /***************************************************************************
343 Tell all smbd's to delete an entry.
344 **************************************************************************/
346 void send_stat_cache_delete_message(const char *name)
348 #ifdef DEVELOPER
349 message_send_all(smbd_messaging_context(),
350 MSG_SMB_STAT_CACHE_DELETE,
351 name,
352 strlen(name)+1,
353 NULL);
354 #endif
357 /***************************************************************************
358 Delete an entry.
359 **************************************************************************/
361 void stat_cache_delete(const char *name)
363 char *lname = talloc_strdup_upper(talloc_tos(), name);
365 if (!lname) {
366 return;
368 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
369 lname, name ));
371 memcache_delete(smbd_memcache(), STAT_CACHE,
372 data_blob_const(lname, talloc_get_size(lname)-1));
373 TALLOC_FREE(lname);
376 /***************************************************************
377 Compute a hash value based on a string key value.
378 The function returns the bucket index number for the hashed key.
379 JRA. Use a djb-algorithm hash for speed.
380 ***************************************************************/
382 unsigned int fast_string_hash(TDB_DATA *key)
384 unsigned int n = 0;
385 const char *p;
386 for (p = (const char *)key->dptr; *p != '\0'; p++) {
387 n = ((n << 5) + n) ^ (unsigned int)(*p);
389 return n;
392 /***************************************************************************
393 Initializes or clears the stat cache.
394 **************************************************************************/
396 bool reset_stat_cache( void )
398 if (!lp_stat_cache())
399 return True;
401 memcache_flush(smbd_memcache(), STAT_CACHE);
403 return True;