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