r23743: Simplify stat_cache_lookup() logic. For the history of this, see
[ira/wip.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-2004
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /****************************************************************************
26  Stat cache code used in unix_convert.
27 *****************************************************************************/
28
29 static TDB_CONTEXT *tdb_stat_cache;
30
31 /**
32  * Add an entry into the stat cache.
33  *
34  * @param full_orig_name       The original name as specified by the client
35  * @param orig_translated_path The name on our filesystem.
36  * 
37  * @note Only the first strlen(orig_translated_path) characters are stored 
38  *       into the cache.  This means that full_orig_name will be internally
39  *       truncated.
40  *
41  */
42
43 void stat_cache_add( const char *full_orig_name, const char *orig_translated_path, BOOL case_sensitive)
44 {
45         char *translated_path;
46         size_t translated_path_length;
47         TDB_DATA data_val;
48         char *original_path;
49         size_t original_path_length;
50         size_t sc_size = lp_max_stat_cache_size();
51
52         if (!lp_stat_cache())
53                 return;
54
55         if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
56                 reset_stat_cache();
57         }
58
59         ZERO_STRUCT(data_val);
60
61         /*
62          * Don't cache trivial valid directory entries such as . and ..
63          */
64
65         if((*full_orig_name == '\0') || (full_orig_name[0] == '.' && 
66                                 ((full_orig_name[1] == '\0') ||
67                                  (full_orig_name[1] == '.' && full_orig_name[2] == '\0'))))
68                 return;
69
70         /*
71          * If we are in case insentive mode, we don't need to
72          * store names that need no translation - else, it
73          * would be a waste.
74          */
75
76         if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
77                 return;
78
79         /*
80          * Remove any trailing '/' characters from the
81          * translated path.
82          */
83
84         translated_path = SMB_STRDUP(orig_translated_path);
85         if (!translated_path)
86                 return;
87
88         translated_path_length = strlen(translated_path);
89
90         if(translated_path[translated_path_length-1] == '/') {
91                 translated_path[translated_path_length-1] = '\0';
92                 translated_path_length--;
93         }
94
95         if(case_sensitive) {
96                 original_path = SMB_STRDUP(full_orig_name);
97         } else {
98                 original_path = strdup_upper(full_orig_name);
99         }
100
101         if (!original_path) {
102                 SAFE_FREE(translated_path);
103                 return;
104         }
105
106         original_path_length = strlen(original_path);
107
108         if(original_path[original_path_length-1] == '/') {
109                 original_path[original_path_length-1] = '\0';
110                 original_path_length--;
111         }
112
113         if (original_path_length != translated_path_length) {
114                 if (original_path_length < translated_path_length) {
115                         DEBUG(0, ("OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)!\n",
116                                   original_path, (unsigned long)original_path_length, translated_path, (unsigned long)translated_path_length));
117                         SAFE_FREE(original_path);
118                         SAFE_FREE(translated_path);
119                         return;
120                 }
121
122                 /* we only want to index by the first part of original_path,
123                         up to the length of translated_path */
124
125                 original_path[translated_path_length] = '\0';
126                 original_path_length = translated_path_length;
127         }
128
129         /*
130          * New entry or replace old entry.
131          */
132   
133         data_val.dsize = translated_path_length + 1;
134         data_val.dptr = (uint8 *)translated_path;
135
136         if (tdb_store_bystring(tdb_stat_cache, original_path, data_val, TDB_REPLACE) != 0) {
137                 DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n", original_path, translated_path));
138         } else {
139                 DEBUG(5,("stat_cache_add: Added entry (%lx:size%x) %s -> %s\n",
140                         (unsigned long)data_val.dptr, (unsigned int)data_val.dsize, original_path, translated_path));
141         }
142
143         SAFE_FREE(original_path);
144         SAFE_FREE(translated_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
153  * @param dirpath The path as far as the stat cache told us.
154  * @param start   A pointer into name, for where to 'start' in fixing the rest of the name up.
155  * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
156  *
157  * @return True if we translated (and did a scuccessful stat on) the entire name.
158  *
159  */
160
161 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath, 
162                        char **start, SMB_STRUCT_STAT *pst)
163 {
164         char *chk_name;
165         size_t namelen;
166         BOOL sizechanged = False;
167         unsigned int num_components = 0;
168         char *translated_path;
169         size_t translated_path_length;
170         TDB_DATA data_val;
171
172         if (!lp_stat_cache())
173                 return False;
174  
175         namelen = strlen(name);
176
177         *start = name;
178
179         DO_PROFILE_INC(statcache_lookups);
180
181         /*
182          * Don't lookup trivial valid directory entries.
183          */
184         if((*name == '\0') || (name[0] == '.' && 
185                                 ((name[1] == '\0') ||
186                                  (name[1] == '.' && name[1] == '\0'))))
187                 return False;
188
189         if (conn->case_sensitive) {
190                 chk_name = SMB_STRDUP(name);
191                 if (!chk_name) {
192                         DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
193                         return False;
194                 }
195
196         } else {
197                 chk_name = strdup_upper(name);
198                 if (!chk_name) {
199                         DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
200                         return False;
201                 }
202
203                 /*
204                  * In some language encodings the length changes
205                  * if we uppercase. We need to treat this differently
206                  * below.
207                  */
208                 if (strlen(chk_name) != namelen)
209                         sizechanged = True;
210         }
211
212         while (1) {
213                 char *sp;
214
215                 data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
216
217                 if (data_val.dptr != NULL && data_val.dsize != 0) {
218                         break;
219                 }
220
221                 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
222                 /*
223                  * Didn't find it - remove last component for next try.
224                  */
225                 if (!(sp = strrchr_m(chk_name, '/'))) {
226                         /*
227                          * We reached the end of the name - no match.
228                          */
229                         DO_PROFILE_INC(statcache_misses);
230                         SAFE_FREE(chk_name);
231                         return False;
232                 }
233
234                 *sp = '\0';
235
236                 /*
237                  * Count the number of times we have done this, we'll
238                  * need it when reconstructing the string.
239                  */
240                 if (sizechanged)
241                         num_components++;
242
243                 if ((*chk_name == '\0')
244                     || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
245                         DO_PROFILE_INC(statcache_misses);
246                         SAFE_FREE(chk_name);
247                         return False;
248                 }
249         }
250
251         translated_path = (char *)data_val.dptr;
252         translated_path_length = data_val.dsize - 1;
253
254         DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
255                   "-> [%s]\n", chk_name, translated_path ));
256         DO_PROFILE_INC(statcache_hits);
257
258         if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
259                 /* Discard this entry - it doesn't exist in the filesystem. */
260                 tdb_delete_bystring(tdb_stat_cache, chk_name);
261                 SAFE_FREE(chk_name);
262                 SAFE_FREE(data_val.dptr);
263                 return False;
264         }
265
266         if (!sizechanged) {
267                 memcpy(name, translated_path,
268                        MIN(sizeof(pstring)-1, translated_path_length));
269         } else if (num_components == 0) {
270                 pstrcpy(name, translated_path);
271         } else {
272                 char *sp;
273
274                 sp = strnrchr_m(name, '/', num_components);
275                 if (sp) {
276                         pstring last_component;
277                         pstrcpy(last_component, sp);
278                         pstrcpy(name, translated_path);
279                         pstrcat(name, last_component);
280                 } else {
281                         pstrcpy(name, translated_path);
282                 }
283         }
284
285         /* set pointer for 'where to start' on fixing the rest of the name */
286         *start = &name[translated_path_length];
287         if (**start == '/')
288                 ++*start;
289
290         pstrcpy(dirpath, translated_path);
291         SAFE_FREE(chk_name);
292         SAFE_FREE(data_val.dptr);
293         return (namelen == translated_path_length);
294 }
295
296 /***************************************************************************
297  Tell all smbd's to delete an entry.
298 **************************************************************************/
299
300 void send_stat_cache_delete_message(const char *name)
301 {
302 #ifdef DEVELOPER
303         message_send_all(smbd_messaging_context(),
304                         MSG_SMB_STAT_CACHE_DELETE,
305                         name,
306                         strlen(name)+1,
307                         NULL);
308 #endif
309 }
310
311 /***************************************************************************
312  Delete an entry.
313 **************************************************************************/
314
315 void stat_cache_delete(const char *name)
316 {
317         char *lname = strdup_upper(name);
318
319         if (!lname) {
320                 return;
321         }
322         DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
323                         lname, name ));
324
325         tdb_delete_bystring(tdb_stat_cache, lname);
326         SAFE_FREE(lname);
327 }
328
329 /***************************************************************
330  Compute a hash value based on a string key value.
331  The function returns the bucket index number for the hashed key.
332  JRA. Use a djb-algorithm hash for speed.
333 ***************************************************************/
334                                                                                                      
335 unsigned int fast_string_hash(TDB_DATA *key)
336 {
337         unsigned int n = 0;
338         const char *p;
339         for (p = (const char *)key->dptr; *p != '\0'; p++) {
340                 n = ((n << 5) + n) ^ (unsigned int)(*p);
341         }
342         return n;
343 }
344
345 /***************************************************************************
346  Initializes or clears the stat cache.
347 **************************************************************************/
348
349 BOOL reset_stat_cache( void )
350 {
351         if (!lp_stat_cache())
352                 return True;
353
354         if (tdb_stat_cache) {
355                 tdb_close(tdb_stat_cache);
356         }
357
358         /* Create the in-memory tdb using our custom hash function. */
359         tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
360                                     (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
361
362         if (!tdb_stat_cache)
363                 return False;
364         return True;
365 }