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