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