2 Unix SMB/CIFS implementation.
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
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.
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.
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/>.
24 #include "../lib/util/memcache.h"
25 #include "smbd/smbd.h"
28 #include "smbprofile.h"
31 #define STAT_CACHE_TWRP_TOKEN "%016" PRIx64 "@%s"
32 #define STAT_CACHE_TWRP_TOKEN_LEN 17
34 /****************************************************************************
35 Stat cache code used in unix_convert.
36 *****************************************************************************/
39 * Add an entry into the stat cache.
41 * @param full_orig_name The original name as specified by the client
42 * @param orig_translated_path The name on our filesystem.
44 * @note Only the first strlen(orig_translated_path) characters are stored
45 * into the cache. This means that full_orig_name will be internally
50 void stat_cache_add( const char *full_orig_name
,
51 const char *translated_path_in
,
55 size_t translated_path_length
;
56 char *translated_path
= NULL
;
58 size_t original_path_length
;
59 TALLOC_CTX
*ctx
= talloc_tos();
61 if (!lp_stat_cache()) {
66 * Don't cache trivial valid directory entries such as . and ..
69 if ((*full_orig_name
== '\0')
70 || ISDOT(full_orig_name
) || ISDOTDOT(full_orig_name
)) {
74 translated_path
= talloc_asprintf(ctx
,
75 STAT_CACHE_TWRP_TOKEN
,
78 if (translated_path
== NULL
) {
83 * If we are in case insentive mode, we don't need to
84 * store names that need no translation - else, it
88 if (!case_sensitive
&& (strcmp(full_orig_name
, translated_path
) == 0)) {
89 TALLOC_FREE(translated_path
);
94 * Remove any trailing '/' characters from the
98 translated_path_length
= strlen(translated_path
);
100 if(translated_path
[translated_path_length
-1] == '/') {
101 translated_path_length
--;
105 original_path
= talloc_asprintf(ctx
,
106 STAT_CACHE_TWRP_TOKEN
,
112 upper
= talloc_strdup_upper(ctx
, full_orig_name
);
114 TALLOC_FREE(translated_path
);
117 original_path
= talloc_asprintf(ctx
,
118 STAT_CACHE_TWRP_TOKEN
,
124 if (!original_path
) {
125 TALLOC_FREE(translated_path
);
129 original_path_length
= strlen(original_path
);
131 if(original_path
[original_path_length
-1] == '/') {
132 original_path
[original_path_length
-1] = '\0';
133 original_path_length
--;
136 if (original_path_length
!= translated_path_length
) {
137 if (original_path_length
< translated_path_length
) {
138 DEBUG(0, ("OOPS - tried to store stat cache entry "
139 "for weird length paths [%s] %lu and [%s] %lu)!\n",
141 (unsigned long)original_path_length
,
143 (unsigned long)translated_path_length
));
144 TALLOC_FREE(original_path
);
145 TALLOC_FREE(translated_path
);
149 /* we only want to index by the first part of original_path,
150 up to the length of translated_path */
152 original_path
[translated_path_length
] = '\0';
153 original_path_length
= translated_path_length
;
156 /* Ensure we're null terminated. */
157 translated_path
[translated_path_length
] = '\0';
160 * New entry or replace old entry.
164 smbd_memcache(), STAT_CACHE
,
165 data_blob_const(original_path
, original_path_length
),
166 data_blob_const(translated_path
, translated_path_length
+ 1));
168 DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
169 (unsigned long)translated_path
,
170 (unsigned int)translated_path_length
,
174 TALLOC_FREE(original_path
);
175 TALLOC_FREE(translated_path
);
179 * Look through the stat cache for an entry
181 * @param conn A connection struct to do the stat() with.
182 * @param posix_paths Whether to lookup using stat() or lstat()
183 * @param name The path we are attempting to cache, modified by this routine
184 * to be correct as far as the cache can tell us. We assume that
185 * it is a talloc'ed string from top of stack, we free it if
187 * @param dirpath The path as far as the stat cache told us. Also talloced
189 * @param start A pointer into name, for where to 'start' in fixing the rest
191 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
193 * @return True if we translated (and did a successful stat on) the entire
198 bool stat_cache_lookup(connection_struct
*conn
,
203 SMB_STRUCT_STAT
*pst
)
207 bool sizechanged
= False
;
208 unsigned int num_components
= 0;
209 char *translated_path
;
210 size_t translated_path_length
;
213 TALLOC_CTX
*ctx
= talloc_tos();
214 struct smb_filename smb_fname
;
218 *pp_start
= *pp_name
;
220 if (!lp_stat_cache()) {
225 namelen
= strlen(name
);
227 DO_PROFILE_INC(statcache_lookups
);
230 * Don't lookup trivial valid directory entries.
232 if ((*name
== '\0') || ISDOT(name
) || ISDOTDOT(name
)) {
236 if (conn
->case_sensitive
) {
237 chk_name
= talloc_asprintf(ctx
,
238 STAT_CACHE_TWRP_TOKEN
,
242 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
249 upper
= talloc_strdup_upper(ctx
,name
);
251 DBG_ERR("talloc_strdup_upper failed!\n");
254 chk_name
= talloc_asprintf(ctx
,
255 STAT_CACHE_TWRP_TOKEN
,
259 DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
264 * In some language encodings the length changes
265 * if we uppercase. We need to treat this differently
268 if (strlen(chk_name
) != namelen
) {
276 data_val
= data_blob_null
;
279 smbd_memcache(), STAT_CACHE
,
280 data_blob_const(chk_name
, strlen(chk_name
)),
285 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
288 * Didn't find it - remove last component for next try.
290 if (!(sp
= strrchr_m(chk_name
, '/'))) {
292 * We reached the end of the name - no match.
294 DO_PROFILE_INC(statcache_misses
);
295 TALLOC_FREE(chk_name
);
302 * Count the number of times we have done this, we'll
303 * need it when reconstructing the string.
307 if ((*chk_name
== '\0')
308 || ISDOT(chk_name
) || ISDOTDOT(chk_name
)) {
309 DO_PROFILE_INC(statcache_misses
);
310 TALLOC_FREE(chk_name
);
315 SMB_ASSERT(data_val
.length
>= STAT_CACHE_TWRP_TOKEN_LEN
);
317 translated_path
= talloc_strdup(
318 ctx
,(char *)data_val
.data
+ STAT_CACHE_TWRP_TOKEN_LEN
);
319 if (!translated_path
) {
320 smb_panic("talloc failed");
322 translated_path_length
= data_val
.length
- 1 - STAT_CACHE_TWRP_TOKEN_LEN
;
324 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
325 "-> [%s]\n", chk_name
, translated_path
));
326 DO_PROFILE_INC(statcache_hits
);
328 smb_fname
= (struct smb_filename
) {
329 .base_name
= translated_path
,
333 ret
= vfs_stat(conn
, &smb_fname
);
335 /* Discard this entry - it doesn't exist in the filesystem. */
336 memcache_delete(smbd_memcache(), STAT_CACHE
,
337 data_blob_const(chk_name
, strlen(chk_name
)));
338 TALLOC_FREE(chk_name
);
339 TALLOC_FREE(translated_path
);
343 * Only copy the stat struct back if we actually hit the full path
345 if (num_components
== 0) {
350 memcpy(*pp_name
, translated_path
,
351 MIN(namelen
, translated_path_length
));
353 if (num_components
== 0) {
354 name
= talloc_strndup(ctx
, translated_path
,
355 translated_path_length
);
359 sp
= strnrchr_m(name
, '/', num_components
);
361 name
= talloc_asprintf(ctx
,"%.*s%s",
362 (int)translated_path_length
,
363 translated_path
, sp
);
365 name
= talloc_strndup(ctx
,
367 translated_path_length
);
372 * TODO: Get us out of here with a real error message
374 smb_panic("talloc failed");
376 TALLOC_FREE(*pp_name
);
381 /* set pointer for 'where to start' on fixing the rest of the name */
382 *pp_start
= &name
[translated_path_length
];
383 if (**pp_start
== '/') {
387 *pp_dirpath
= translated_path
;
388 TALLOC_FREE(chk_name
);
389 return (namelen
== translated_path_length
);
392 /***************************************************************************
393 Tell all smbd's to delete an entry.
394 **************************************************************************/
396 void smbd_send_stat_cache_delete_message(struct messaging_context
*msg_ctx
,
400 messaging_send_all(msg_ctx
,
401 MSG_SMB_STAT_CACHE_DELETE
,
407 /***************************************************************************
409 **************************************************************************/
411 void stat_cache_delete(const char *name
)
413 char *upper
= talloc_strdup_upper(talloc_tos(), name
);
420 lname
= talloc_asprintf(talloc_tos(),
421 STAT_CACHE_TWRP_TOKEN
,
428 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
431 memcache_delete(smbd_memcache(), STAT_CACHE
,
432 data_blob_const(lname
, talloc_get_size(lname
)-1));
436 /***************************************************************************
437 Initializes or clears the stat cache.
438 **************************************************************************/
440 bool reset_stat_cache( void )
442 if (!lp_stat_cache())
445 memcache_flush(smbd_memcache(), STAT_CACHE
);