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