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