40174c803486951b4dbc1445079bf78f392535c6
[nivanova/samba-autobuild/.git] / source3 / smbd / statcache.c
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
8
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.
13
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.
18
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/>.
21 */
22
23 #include "includes.h"
24 #include "memcache.h"
25 #include "smbd/smbd.h"
26 #include "messages.h"
27
28 /****************************************************************************
29  Stat cache code used in unix_convert.
30 *****************************************************************************/
31
32 /**
33  * Add an entry into the stat cache.
34  *
35  * @param full_orig_name       The original name as specified by the client
36  * @param orig_translated_path The name on our filesystem.
37  *
38  * @note Only the first strlen(orig_translated_path) characters are stored
39  *       into the cache.  This means that full_orig_name will be internally
40  *       truncated.
41  *
42  */
43
44 void stat_cache_add( const char *full_orig_name,
45                 char *translated_path,
46                 bool case_sensitive)
47 {
48         size_t translated_path_length;
49         char *original_path;
50         size_t original_path_length;
51         char saved_char;
52         TALLOC_CTX *ctx = talloc_tos();
53
54         if (!lp_stat_cache()) {
55                 return;
56         }
57
58         /*
59          * Don't cache trivial valid directory entries such as . and ..
60          */
61
62         if ((*full_orig_name == '\0')
63             || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
64                 return;
65         }
66
67         /*
68          * If we are in case insentive mode, we don't need to
69          * store names that need no translation - else, it
70          * would be a waste.
71          */
72
73         if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
74                 return;
75         }
76
77         /*
78          * Remove any trailing '/' characters from the
79          * translated path.
80          */
81
82         translated_path_length = strlen(translated_path);
83
84         if(translated_path[translated_path_length-1] == '/') {
85                 translated_path_length--;
86         }
87
88         if(case_sensitive) {
89                 original_path = talloc_strdup(ctx,full_orig_name);
90         } else {
91                 original_path = talloc_strdup_upper(ctx,full_orig_name);
92         }
93
94         if (!original_path) {
95                 return;
96         }
97
98         original_path_length = strlen(original_path);
99
100         if(original_path[original_path_length-1] == '/') {
101                 original_path[original_path_length-1] = '\0';
102                 original_path_length--;
103         }
104
105         if (original_path_length != translated_path_length) {
106                 if (original_path_length < translated_path_length) {
107                         DEBUG(0, ("OOPS - tried to store stat cache entry "
108                         "for weird length paths [%s] %lu and [%s] %lu)!\n",
109                                   original_path,
110                                   (unsigned long)original_path_length,
111                                   translated_path,
112                                   (unsigned long)translated_path_length));
113                         TALLOC_FREE(original_path);
114                         return;
115                 }
116
117                 /* we only want to index by the first part of original_path,
118                         up to the length of translated_path */
119
120                 original_path[translated_path_length] = '\0';
121                 original_path_length = translated_path_length;
122         }
123
124         /* Ensure we're null terminated. */
125         saved_char = translated_path[translated_path_length];
126         translated_path[translated_path_length] = '\0';
127
128         /*
129          * New entry or replace old entry.
130          */
131
132         memcache_add(
133                 smbd_memcache(), STAT_CACHE,
134                 data_blob_const(original_path, original_path_length),
135                 data_blob_const(translated_path, translated_path_length + 1));
136
137         DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
138                  (unsigned long)translated_path,
139                  (unsigned int)translated_path_length,
140                  original_path,
141                  translated_path));
142
143         translated_path[translated_path_length] = saved_char;
144         TALLOC_FREE(original_path);
145 }
146
147 /**
148  * Look through the stat cache for an entry
149  *
150  * @param conn    A connection struct to do the stat() with.
151  * @param name    The path we are attempting to cache, modified by this routine
152  *                to be correct as far as the cache can tell us. We assume that
153  *                it is a talloc'ed string from top of stack, we free it if
154  *                necessary.
155  * @param dirpath The path as far as the stat cache told us. Also talloced
156  *                from top of stack.
157  * @param start   A pointer into name, for where to 'start' in fixing the rest
158  *                of the name up.
159  * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
160  *
161  * @return True if we translated (and did a scuccessful stat on) the entire
162  *                name.
163  *
164  */
165
166 bool stat_cache_lookup(connection_struct *conn,
167                         char **pp_name,
168                         char **pp_dirpath,
169                         char **pp_start,
170                         SMB_STRUCT_STAT *pst)
171 {
172         char *chk_name;
173         size_t namelen;
174         bool sizechanged = False;
175         unsigned int num_components = 0;
176         char *translated_path;
177         size_t translated_path_length;
178         DATA_BLOB data_val;
179         char *name;
180         TALLOC_CTX *ctx = talloc_tos();
181         struct smb_filename smb_fname;
182
183         *pp_dirpath = NULL;
184         *pp_start = *pp_name;
185
186         if (!lp_stat_cache()) {
187                 return False;
188         }
189
190         name = *pp_name;
191         namelen = strlen(name);
192
193         DO_PROFILE_INC(statcache_lookups);
194
195         /*
196          * Don't lookup trivial valid directory entries.
197          */
198         if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
199                 return False;
200         }
201
202         if (conn->case_sensitive) {
203                 chk_name = talloc_strdup(ctx,name);
204                 if (!chk_name) {
205                         DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
206                         return False;
207                 }
208
209         } else {
210                 chk_name = talloc_strdup_upper(ctx,name);
211                 if (!chk_name) {
212                         DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
213                         return False;
214                 }
215
216                 /*
217                  * In some language encodings the length changes
218                  * if we uppercase. We need to treat this differently
219                  * below.
220                  */
221                 if (strlen(chk_name) != namelen) {
222                         sizechanged = True;
223                 }
224         }
225
226         while (1) {
227                 char *sp;
228
229                 data_val = data_blob_null;
230
231                 if (memcache_lookup(
232                             smbd_memcache(), STAT_CACHE,
233                             data_blob_const(chk_name, strlen(chk_name)),
234                             &data_val)) {
235                         break;
236                 }
237
238                 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
239                                 chk_name ));
240                 /*
241                  * Didn't find it - remove last component for next try.
242                  */
243                 if (!(sp = strrchr_m(chk_name, '/'))) {
244                         /*
245                          * We reached the end of the name - no match.
246                          */
247                         DO_PROFILE_INC(statcache_misses);
248                         TALLOC_FREE(chk_name);
249                         return False;
250                 }
251
252                 *sp = '\0';
253
254                 /*
255                  * Count the number of times we have done this, we'll
256                  * need it when reconstructing the string.
257                  */
258
259                 if (sizechanged) {
260                         num_components++;
261                 }
262
263                 if ((*chk_name == '\0')
264                     || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
265                         DO_PROFILE_INC(statcache_misses);
266                         TALLOC_FREE(chk_name);
267                         return False;
268                 }
269         }
270
271         translated_path = talloc_strdup(ctx,(char *)data_val.data);
272         if (!translated_path) {
273                 smb_panic("talloc failed");
274         }
275         translated_path_length = data_val.length - 1;
276
277         DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
278                   "-> [%s]\n", chk_name, translated_path ));
279         DO_PROFILE_INC(statcache_hits);
280
281         ZERO_STRUCT(smb_fname);
282         smb_fname.base_name = translated_path;
283
284         if (SMB_VFS_STAT(conn, &smb_fname) != 0) {
285                 /* Discard this entry - it doesn't exist in the filesystem. */
286                 memcache_delete(smbd_memcache(), STAT_CACHE,
287                                 data_blob_const(chk_name, strlen(chk_name)));
288                 TALLOC_FREE(chk_name);
289                 TALLOC_FREE(translated_path);
290                 return False;
291         }
292         *pst = smb_fname.st;
293
294         if (!sizechanged) {
295                 memcpy(*pp_name, translated_path,
296                        MIN(namelen, translated_path_length));
297         } else {
298                 if (num_components == 0) {
299                         name = talloc_strndup(ctx, translated_path,
300                                            translated_path_length);
301                 } else {
302                         char *sp;
303
304                         sp = strnrchr_m(name, '/', num_components);
305                         if (sp) {
306                                 name = talloc_asprintf(ctx,"%.*s%s",
307                                          (int)translated_path_length,
308                                          translated_path, sp);
309                         } else {
310                                 name = talloc_strndup(ctx,
311                                                 translated_path,
312                                                 translated_path_length);
313                         }
314                 }
315                 if (name == NULL) {
316                         /*
317                          * TODO: Get us out of here with a real error message
318                          */
319                         smb_panic("talloc failed");
320                 }
321                 TALLOC_FREE(*pp_name);
322                 *pp_name = name;
323         }
324
325
326         /* set pointer for 'where to start' on fixing the rest of the name */
327         *pp_start = &name[translated_path_length];
328         if (**pp_start == '/') {
329                 ++*pp_start;
330         }
331
332         *pp_dirpath = translated_path;
333         TALLOC_FREE(chk_name);
334         return (namelen == translated_path_length);
335 }
336
337 /***************************************************************************
338  Tell all smbd's to delete an entry.
339 **************************************************************************/
340
341 void send_stat_cache_delete_message(struct messaging_context *msg_ctx,
342                                     const char *name)
343 {
344 #ifdef DEVELOPER
345         message_send_all(msg_ctx,
346                         MSG_SMB_STAT_CACHE_DELETE,
347                         name,
348                         strlen(name)+1,
349                         NULL);
350 #endif
351 }
352
353 /***************************************************************************
354  Delete an entry.
355 **************************************************************************/
356
357 void stat_cache_delete(const char *name)
358 {
359         char *lname = talloc_strdup_upper(talloc_tos(), name);
360
361         if (!lname) {
362                 return;
363         }
364         DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
365                         lname, name ));
366
367         memcache_delete(smbd_memcache(), STAT_CACHE,
368                         data_blob_const(lname, talloc_get_size(lname)-1));
369         TALLOC_FREE(lname);
370 }
371
372 /***************************************************************
373  Compute a hash value based on a string key value.
374  The function returns the bucket index number for the hashed key.
375  JRA. Use a djb-algorithm hash for speed.
376 ***************************************************************/
377
378 unsigned int fast_string_hash(TDB_DATA *key)
379 {
380         unsigned int n = 0;
381         const char *p;
382         for (p = (const char *)key->dptr; *p != '\0'; p++) {
383                 n = ((n << 5) + n) ^ (unsigned int)(*p);
384         }
385         return n;
386 }
387
388 /***************************************************************************
389  Initializes or clears the stat cache.
390 **************************************************************************/
391
392 bool reset_stat_cache( void )
393 {
394         if (!lp_stat_cache())
395                 return True;
396
397         memcache_flush(smbd_memcache(), STAT_CACHE);
398
399         return True;
400 }