| 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 = 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 |
|
|---|
| 169 | if (!lp_stat_cache())
|
|---|
| 170 | return False;
|
|---|
| 171 |
|
|---|
| 172 | namelen = strlen(name);
|
|---|
| 173 |
|
|---|
| 174 | *start = name;
|
|---|
| 175 |
|
|---|
| 176 | DO_PROFILE_INC(statcache_lookups);
|
|---|
| 177 |
|
|---|
| 178 | /*
|
|---|
| 179 | * Don't lookup trivial valid directory entries.
|
|---|
| 180 | */
|
|---|
| 181 | if((*name == '\0') || (name[0] == '.' &&
|
|---|
| 182 | ((name[1] == '\0') ||
|
|---|
| 183 | (name[1] == '.' && name[1] == '\0'))))
|
|---|
| 184 | return False;
|
|---|
| 185 |
|
|---|
| 186 | if (conn->case_sensitive) {
|
|---|
| 187 | chk_name = SMB_STRDUP(name);
|
|---|
| 188 | if (!chk_name) {
|
|---|
| 189 | DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
|
|---|
| 190 | return False;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | } else {
|
|---|
| 194 | chk_name = strdup_upper(name);
|
|---|
| 195 | if (!chk_name) {
|
|---|
| 196 | DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
|
|---|
| 197 | return False;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /*
|
|---|
| 201 | * In some language encodings the length changes
|
|---|
| 202 | * if we uppercase. We need to treat this differently
|
|---|
| 203 | * below.
|
|---|
| 204 | */
|
|---|
| 205 | if (strlen(chk_name) != namelen)
|
|---|
| 206 | sizechanged = True;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | while (1) {
|
|---|
| 210 | TDB_DATA data_val;
|
|---|
| 211 | char *sp;
|
|---|
| 212 |
|
|---|
| 213 | data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
|
|---|
| 214 | if(data_val.dptr == NULL || data_val.dsize == 0) {
|
|---|
| 215 | DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
|
|---|
| 216 | /*
|
|---|
| 217 | * Didn't find it - remove last component for next try.
|
|---|
| 218 | */
|
|---|
| 219 | sp = strrchr_m(chk_name, '/');
|
|---|
| 220 | if (sp) {
|
|---|
| 221 | *sp = '\0';
|
|---|
| 222 | /*
|
|---|
| 223 | * Count the number of times we have done this,
|
|---|
| 224 | * we'll need it when reconstructing the string.
|
|---|
| 225 | */
|
|---|
| 226 | if (sizechanged)
|
|---|
| 227 | num_components++;
|
|---|
| 228 |
|
|---|
| 229 | } else {
|
|---|
| 230 | /*
|
|---|
| 231 | * We reached the end of the name - no match.
|
|---|
| 232 | */
|
|---|
| 233 | DO_PROFILE_INC(statcache_misses);
|
|---|
| 234 | SAFE_FREE(chk_name);
|
|---|
| 235 | return False;
|
|---|
| 236 | }
|
|---|
| 237 | if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
|
|---|
| 238 | || (strcmp(chk_name, "..") == 0)) {
|
|---|
| 239 | DO_PROFILE_INC(statcache_misses);
|
|---|
| 240 | SAFE_FREE(chk_name);
|
|---|
| 241 | return False;
|
|---|
| 242 | }
|
|---|
| 243 | } else {
|
|---|
| 244 | BOOL retval;
|
|---|
| 245 | char *translated_path = data_val.dptr;
|
|---|
| 246 | size_t translated_path_length = data_val.dsize - 1;
|
|---|
| 247 |
|
|---|
| 248 | DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] -> [%s]\n", chk_name, translated_path ));
|
|---|
| 249 | DO_PROFILE_INC(statcache_hits);
|
|---|
| 250 | if(SMB_VFS_STAT(conn,translated_path, pst) != 0) {
|
|---|
| 251 | /* Discard this entry - it doesn't exist in the filesystem. */
|
|---|
| 252 | tdb_delete_bystring(tdb_stat_cache, chk_name);
|
|---|
| 253 | SAFE_FREE(chk_name);
|
|---|
| 254 | SAFE_FREE(data_val.dptr);
|
|---|
| 255 | return False;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | if (!sizechanged) {
|
|---|
| 259 | memcpy(name, translated_path, MIN(sizeof(pstring)-1, translated_path_length));
|
|---|
| 260 | } else if (num_components == 0) {
|
|---|
| 261 | pstrcpy(name, translated_path);
|
|---|
| 262 | } else {
|
|---|
| 263 | sp = strnrchr_m(name, '/', num_components);
|
|---|
| 264 | if (sp) {
|
|---|
| 265 | pstring last_component;
|
|---|
| 266 | pstrcpy(last_component, sp);
|
|---|
| 267 | pstrcpy(name, translated_path);
|
|---|
| 268 | pstrcat(name, last_component);
|
|---|
| 269 | } else {
|
|---|
| 270 | pstrcpy(name, translated_path);
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /* set pointer for 'where to start' on fixing the rest of the name */
|
|---|
| 275 | *start = &name[translated_path_length];
|
|---|
| 276 | if(**start == '/')
|
|---|
| 277 | ++*start;
|
|---|
| 278 |
|
|---|
| 279 | pstrcpy(dirpath, translated_path);
|
|---|
| 280 | retval = (namelen == translated_path_length) ? True : False;
|
|---|
| 281 | SAFE_FREE(chk_name);
|
|---|
| 282 | SAFE_FREE(data_val.dptr);
|
|---|
| 283 | return retval;
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /***************************************************************************
|
|---|
| 289 | Tell all smbd's to delete an entry.
|
|---|
| 290 | **************************************************************************/
|
|---|
| 291 |
|
|---|
| 292 | void send_stat_cache_delete_message(const char *name)
|
|---|
| 293 | {
|
|---|
| 294 | #ifdef DEVELOPER
|
|---|
| 295 | message_send_all(conn_tdb_ctx(),
|
|---|
| 296 | MSG_SMB_STAT_CACHE_DELETE,
|
|---|
| 297 | name,
|
|---|
| 298 | strlen(name)+1,
|
|---|
| 299 | True,
|
|---|
| 300 | NULL);
|
|---|
| 301 | #endif
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /***************************************************************************
|
|---|
| 305 | Delete an entry.
|
|---|
| 306 | **************************************************************************/
|
|---|
| 307 |
|
|---|
| 308 | void stat_cache_delete(const char *name)
|
|---|
| 309 | {
|
|---|
| 310 | char *lname = strdup_upper(name);
|
|---|
| 311 |
|
|---|
| 312 | if (!lname) {
|
|---|
| 313 | return;
|
|---|
| 314 | }
|
|---|
| 315 | DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
|
|---|
| 316 | lname, name ));
|
|---|
| 317 |
|
|---|
| 318 | tdb_delete_bystring(tdb_stat_cache, lname);
|
|---|
| 319 | SAFE_FREE(lname);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | /***************************************************************
|
|---|
| 323 | Compute a hash value based on a string key value.
|
|---|
| 324 | The function returns the bucket index number for the hashed key.
|
|---|
| 325 | JRA. Use a djb-algorithm hash for speed.
|
|---|
| 326 | ***************************************************************/
|
|---|
| 327 |
|
|---|
| 328 | unsigned int fast_string_hash(TDB_DATA *key)
|
|---|
| 329 | {
|
|---|
| 330 | unsigned int n = 0;
|
|---|
| 331 | const char *p;
|
|---|
| 332 | for (p = key->dptr; *p != '\0'; p++) {
|
|---|
| 333 | n = ((n << 5) + n) ^ (unsigned int)(*p);
|
|---|
| 334 | }
|
|---|
| 335 | return n;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /***************************************************************************
|
|---|
| 339 | Initializes or clears the stat cache.
|
|---|
| 340 | **************************************************************************/
|
|---|
| 341 |
|
|---|
| 342 | BOOL reset_stat_cache( void )
|
|---|
| 343 | {
|
|---|
| 344 | if (!lp_stat_cache())
|
|---|
| 345 | return True;
|
|---|
| 346 |
|
|---|
| 347 | if (tdb_stat_cache) {
|
|---|
| 348 | tdb_close(tdb_stat_cache);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /* Create the in-memory tdb using our custom hash function. */
|
|---|
| 352 | tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
|
|---|
| 353 | (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
|
|---|
| 354 |
|
|---|
| 355 | if (!tdb_stat_cache)
|
|---|
| 356 | return False;
|
|---|
| 357 | return True;
|
|---|
| 358 | }
|
|---|