s3:smbd: use PROTOCOL_SMB2_02 instead PROTOCOL_SMB2
[Samba/gebeck_regimport.git] / source3 / smbd / statcache.c
blob963b7c4bc1321bdcebb16ba7294e708cb4d8475d
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"
24 #include "memcache.h"
25 #include "smbd/smbd.h"
26 #include "messages.h"
27 #include "smbprofile.h"
28 #include "tdb_compat.h"
30 /****************************************************************************
31 Stat cache code used in unix_convert.
32 *****************************************************************************/
34 /**
35 * Add an entry into the stat cache.
37 * @param full_orig_name The original name as specified by the client
38 * @param orig_translated_path The name on our filesystem.
40 * @note Only the first strlen(orig_translated_path) characters are stored
41 * into the cache. This means that full_orig_name will be internally
42 * truncated.
46 void stat_cache_add( const char *full_orig_name,
47 char *translated_path,
48 bool case_sensitive)
50 size_t translated_path_length;
51 char *original_path;
52 size_t original_path_length;
53 char saved_char;
54 TALLOC_CTX *ctx = talloc_tos();
56 if (!lp_stat_cache()) {
57 return;
61 * Don't cache trivial valid directory entries such as . and ..
64 if ((*full_orig_name == '\0')
65 || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
66 return;
70 * If we are in case insentive mode, we don't need to
71 * store names that need no translation - else, it
72 * would be a waste.
75 if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
76 return;
80 * Remove any trailing '/' characters from the
81 * translated path.
84 translated_path_length = strlen(translated_path);
86 if(translated_path[translated_path_length-1] == '/') {
87 translated_path_length--;
90 if(case_sensitive) {
91 original_path = talloc_strdup(ctx,full_orig_name);
92 } else {
93 original_path = talloc_strdup_upper(ctx,full_orig_name);
96 if (!original_path) {
97 return;
100 original_path_length = strlen(original_path);
102 if(original_path[original_path_length-1] == '/') {
103 original_path[original_path_length-1] = '\0';
104 original_path_length--;
107 if (original_path_length != translated_path_length) {
108 if (original_path_length < translated_path_length) {
109 DEBUG(0, ("OOPS - tried to store stat cache entry "
110 "for weird length paths [%s] %lu and [%s] %lu)!\n",
111 original_path,
112 (unsigned long)original_path_length,
113 translated_path,
114 (unsigned long)translated_path_length));
115 TALLOC_FREE(original_path);
116 return;
119 /* we only want to index by the first part of original_path,
120 up to the length of translated_path */
122 original_path[translated_path_length] = '\0';
123 original_path_length = translated_path_length;
126 /* Ensure we're null terminated. */
127 saved_char = translated_path[translated_path_length];
128 translated_path[translated_path_length] = '\0';
131 * New entry or replace old entry.
134 memcache_add(
135 smbd_memcache(), STAT_CACHE,
136 data_blob_const(original_path, original_path_length),
137 data_blob_const(translated_path, translated_path_length + 1));
139 DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
140 (unsigned long)translated_path,
141 (unsigned int)translated_path_length,
142 original_path,
143 translated_path));
145 translated_path[translated_path_length] = saved_char;
146 TALLOC_FREE(original_path);
150 * Look through the stat cache for an entry
152 * @param conn A connection struct to do the stat() with.
153 * @param name The path we are attempting to cache, modified by this routine
154 * to be correct as far as the cache can tell us. We assume that
155 * it is a talloc'ed string from top of stack, we free it if
156 * necessary.
157 * @param dirpath The path as far as the stat cache told us. Also talloced
158 * from top of stack.
159 * @param start A pointer into name, for where to 'start' in fixing the rest
160 * of the name up.
161 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
163 * @return True if we translated (and did a scuccessful stat on) the entire
164 * name.
168 bool stat_cache_lookup(connection_struct *conn,
169 char **pp_name,
170 char **pp_dirpath,
171 char **pp_start,
172 SMB_STRUCT_STAT *pst)
174 char *chk_name;
175 size_t namelen;
176 bool sizechanged = False;
177 unsigned int num_components = 0;
178 char *translated_path;
179 size_t translated_path_length;
180 DATA_BLOB data_val;
181 char *name;
182 TALLOC_CTX *ctx = talloc_tos();
183 struct smb_filename smb_fname;
185 *pp_dirpath = NULL;
186 *pp_start = *pp_name;
188 if (!lp_stat_cache()) {
189 return False;
192 name = *pp_name;
193 namelen = strlen(name);
195 DO_PROFILE_INC(statcache_lookups);
198 * Don't lookup trivial valid directory entries.
200 if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
201 return False;
204 if (conn->case_sensitive) {
205 chk_name = talloc_strdup(ctx,name);
206 if (!chk_name) {
207 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
208 return False;
211 } else {
212 chk_name = talloc_strdup_upper(ctx,name);
213 if (!chk_name) {
214 DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
215 return False;
219 * In some language encodings the length changes
220 * if we uppercase. We need to treat this differently
221 * below.
223 if (strlen(chk_name) != namelen) {
224 sizechanged = True;
228 while (1) {
229 char *sp;
231 data_val = data_blob_null;
233 if (memcache_lookup(
234 smbd_memcache(), STAT_CACHE,
235 data_blob_const(chk_name, strlen(chk_name)),
236 &data_val)) {
237 break;
240 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
241 chk_name ));
243 * Didn't find it - remove last component for next try.
245 if (!(sp = strrchr_m(chk_name, '/'))) {
247 * We reached the end of the name - no match.
249 DO_PROFILE_INC(statcache_misses);
250 TALLOC_FREE(chk_name);
251 return False;
254 *sp = '\0';
257 * Count the number of times we have done this, we'll
258 * need it when reconstructing the string.
261 if (sizechanged) {
262 num_components++;
265 if ((*chk_name == '\0')
266 || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
267 DO_PROFILE_INC(statcache_misses);
268 TALLOC_FREE(chk_name);
269 return False;
273 translated_path = talloc_strdup(ctx,(char *)data_val.data);
274 if (!translated_path) {
275 smb_panic("talloc failed");
277 translated_path_length = data_val.length - 1;
279 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
280 "-> [%s]\n", chk_name, translated_path ));
281 DO_PROFILE_INC(statcache_hits);
283 ZERO_STRUCT(smb_fname);
284 smb_fname.base_name = translated_path;
286 if (SMB_VFS_STAT(conn, &smb_fname) != 0) {
287 /* Discard this entry - it doesn't exist in the filesystem. */
288 memcache_delete(smbd_memcache(), STAT_CACHE,
289 data_blob_const(chk_name, strlen(chk_name)));
290 TALLOC_FREE(chk_name);
291 TALLOC_FREE(translated_path);
292 return False;
294 *pst = smb_fname.st;
296 if (!sizechanged) {
297 memcpy(*pp_name, translated_path,
298 MIN(namelen, translated_path_length));
299 } else {
300 if (num_components == 0) {
301 name = talloc_strndup(ctx, translated_path,
302 translated_path_length);
303 } else {
304 char *sp;
306 sp = strnrchr_m(name, '/', num_components);
307 if (sp) {
308 name = talloc_asprintf(ctx,"%.*s%s",
309 (int)translated_path_length,
310 translated_path, sp);
311 } else {
312 name = talloc_strndup(ctx,
313 translated_path,
314 translated_path_length);
317 if (name == NULL) {
319 * TODO: Get us out of here with a real error message
321 smb_panic("talloc failed");
323 TALLOC_FREE(*pp_name);
324 *pp_name = name;
328 /* set pointer for 'where to start' on fixing the rest of the name */
329 *pp_start = &name[translated_path_length];
330 if (**pp_start == '/') {
331 ++*pp_start;
334 *pp_dirpath = translated_path;
335 TALLOC_FREE(chk_name);
336 return (namelen == translated_path_length);
339 /***************************************************************************
340 Tell all smbd's to delete an entry.
341 **************************************************************************/
343 void smbd_send_stat_cache_delete_message(struct messaging_context *msg_ctx,
344 const char *name)
346 #ifdef DEVELOPER
347 message_send_all(msg_ctx,
348 MSG_SMB_STAT_CACHE_DELETE,
349 name,
350 strlen(name)+1,
351 NULL);
352 #endif
355 /***************************************************************************
356 Delete an entry.
357 **************************************************************************/
359 void stat_cache_delete(const char *name)
361 char *lname = talloc_strdup_upper(talloc_tos(), name);
363 if (!lname) {
364 return;
366 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
367 lname, name ));
369 memcache_delete(smbd_memcache(), STAT_CACHE,
370 data_blob_const(lname, talloc_get_size(lname)-1));
371 TALLOC_FREE(lname);
374 /***************************************************************
375 Compute a hash value based on a string key value.
376 The function returns the bucket index number for the hashed key.
377 JRA. Use a djb-algorithm hash for speed.
378 ***************************************************************/
380 unsigned int fast_string_hash(TDB_DATA *key)
382 unsigned int n = 0;
383 const char *p;
384 for (p = (const char *)key->dptr; *p != '\0'; p++) {
385 n = ((n << 5) + n) ^ (unsigned int)(*p);
387 return n;
390 /***************************************************************************
391 Initializes or clears the stat cache.
392 **************************************************************************/
394 bool reset_stat_cache( void )
396 if (!lp_stat_cache())
397 return True;
399 memcache_flush(smbd_memcache(), STAT_CACHE);
401 return True;