Changeset 745 for trunk/server/source3/smbd/files.c
- Timestamp:
- Nov 27, 2012, 4:43:17 PM (13 years ago)
- Location:
- trunk/server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/server
- Property svn:mergeinfo changed
/vendor/current merged: 581,587,591,594,597,600,615,618,740
- Property svn:mergeinfo changed
-
trunk/server/source3/smbd/files.c
r414 r745 3 3 Files[] structure handling 4 4 Copyright (C) Andrew Tridgell 1998 5 5 6 6 This program is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU General Public License as published by 8 8 the Free Software Foundation; either version 3 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This program is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU General Public License for more details. 15 15 16 16 You should have received a copy of the GNU General Public License 17 17 along with this program. If not, see <http://www.gnu.org/licenses/>. … … 19 19 20 20 #include "includes.h" 21 #include "smbd/smbd.h" 21 22 #include "smbd/globals.h" 23 #include "libcli/security/security.h" 24 #include "util_tdb.h" 22 25 23 26 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files)) … … 29 32 ****************************************************************************/ 30 33 31 static unsigned long get_gen_count(void) 32 { 33 if ((++file_gen_counter) == 0) 34 return ++file_gen_counter; 35 return file_gen_counter; 34 static unsigned long get_gen_count(struct smbd_server_connection *sconn) 35 { 36 sconn->file_gen_counter += 1; 37 if (sconn->file_gen_counter == 0) { 38 sconn->file_gen_counter += 1; 39 } 40 return sconn->file_gen_counter; 36 41 } 37 42 … … 43 48 files_struct **result) 44 49 { 50 struct smbd_server_connection *sconn = conn->sconn; 45 51 int i; 46 52 files_struct *fsp; … … 52 58 increases the chance that the errant client will get an error rather 53 59 than causing corruption */ 54 if (first_file == 0) { 55 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files; 60 if (sconn->first_file == 0) { 61 sconn->first_file = (sys_getpid() ^ (int)time(NULL)); 62 sconn->first_file %= sconn->real_max_open_files; 56 63 } 57 64 58 65 /* TODO: Port the id-tree implementation from Samba4 */ 59 66 60 i = bitmap_find( file_bmap,first_file);67 i = bitmap_find(sconn->file_bmap, sconn->first_file); 61 68 if (i == -1) { 62 69 DEBUG(0,("ERROR! Out of file structures\n")); … … 69 76 /* 70 77 * Make a child of the connection_struct as an fsp can't exist 71 * indepen edent of a connection.78 * independent of a connection. 72 79 */ 73 80 fsp = talloc_zero(conn, struct files_struct); … … 91 98 92 99 fsp->conn = conn; 93 fsp->fh->gen_id = get_gen_count( );100 fsp->fh->gen_id = get_gen_count(sconn); 94 101 GetTimeOfDay(&fsp->open_time); 95 102 96 first_file = (i+1) % real_max_open_files;97 98 bitmap_set( file_bmap, i);99 files_used++;103 sconn->first_file = (i+1) % (sconn->real_max_open_files); 104 105 bitmap_set(sconn->file_bmap, i); 106 sconn->files_used += 1; 100 107 101 108 fsp->fnum = i + FILE_HANDLE_OFFSET; … … 114 121 } 115 122 116 DLIST_ADD( Files, fsp);123 DLIST_ADD(sconn->files, fsp); 117 124 118 125 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n", 119 i, fsp->fnum, files_used));126 i, fsp->fnum, sconn->files_used)); 120 127 121 128 if (req != NULL) { … … 128 135 a cache hit to the *end* of the list. */ 129 136 130 ZERO_STRUCT( fsp_fi_cache);137 ZERO_STRUCT(sconn->fsp_fi_cache); 131 138 132 139 conn->num_files_open++; … … 143 150 { 144 151 files_struct *fsp, *next; 145 146 for (fsp= Files;fsp;fsp=next) {152 153 for (fsp=conn->sconn->files; fsp; fsp=next) { 147 154 next = fsp->next; 148 155 if (fsp->conn == conn) { … … 156 163 ****************************************************************************/ 157 164 158 void file_close_pid(uint16 smbpid, int vuid) 165 void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid, 166 int vuid) 159 167 { 160 168 files_struct *fsp, *next; 161 162 for (fsp= Files;fsp;fsp=next) {169 170 for (fsp=sconn->files;fsp;fsp=next) { 163 171 next = fsp->next; 164 172 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) { … … 172 180 ****************************************************************************/ 173 181 174 void file_init(void)182 bool file_init(struct smbd_server_connection *sconn) 175 183 { 176 184 int request_max_open_files = lp_max_open_files(); … … 184 192 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR); 185 193 186 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR; 187 188 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) 189 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES; 190 191 if(real_max_open_files != request_max_open_files) { 192 DEBUG(1,("file_init: Information only: requested %d \ 193 open files, %d are available.\n", request_max_open_files, real_max_open_files)); 194 } 195 196 SMB_ASSERT(real_max_open_files > 100); 197 198 file_bmap = bitmap_allocate(real_max_open_files); 199 200 if (!file_bmap) { 201 exit_server("out of memory in file_init"); 202 } 194 sconn->real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR; 195 196 if (sconn->real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES 197 > 65536) 198 sconn->real_max_open_files = 199 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES; 200 201 if(sconn->real_max_open_files != request_max_open_files) { 202 DEBUG(1, ("file_init: Information only: requested %d " 203 "open files, %d are available.\n", 204 request_max_open_files, sconn->real_max_open_files)); 205 } 206 207 SMB_ASSERT(sconn->real_max_open_files > 100); 208 209 sconn->file_bmap = bitmap_talloc(sconn, sconn->real_max_open_files); 210 211 if (!sconn->file_bmap) { 212 return false; 213 } 214 return true; 203 215 } 204 216 … … 207 219 ****************************************************************************/ 208 220 209 void file_close_user( int vuid)221 void file_close_user(struct smbd_server_connection *sconn, int vuid) 210 222 { 211 223 files_struct *fsp, *next; 212 224 213 for (fsp= Files;fsp;fsp=next) {225 for (fsp=sconn->files; fsp; fsp=next) { 214 226 next=fsp->next; 215 227 if (fsp->vuid == vuid) { … … 223 235 */ 224 236 225 struct files_struct *file_walk_table( 237 struct files_struct *files_forall( 238 struct smbd_server_connection *sconn, 226 239 struct files_struct *(*fn)(struct files_struct *fsp, 227 240 void *private_data), … … 230 243 struct files_struct *fsp, *next; 231 244 232 for (fsp = Files; fsp; fsp = next) {245 for (fsp = sconn->files; fsp; fsp = next) { 233 246 struct files_struct *ret; 234 247 next = fsp->next; … … 242 255 243 256 /**************************************************************************** 244 Debug to enumerate all open files in the smbd.245 ****************************************************************************/ 246 247 void file_dump_open_table(void)257 Find a fsp given a file descriptor. 258 ****************************************************************************/ 259 260 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd) 248 261 { 249 262 int count=0; 250 263 files_struct *fsp; 251 264 252 for (fsp=Files;fsp;fsp=fsp->next,count++) { 253 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, gen = %lu, " 254 "fileid=%s\n", count, fsp->fnum, fsp_str_dbg(fsp), 255 fsp->fh->fd, (unsigned long)fsp->fh->gen_id, 256 file_id_string_tos(&fsp->file_id))); 257 } 258 } 259 260 /**************************************************************************** 261 Find a fsp given a file descriptor. 262 ****************************************************************************/ 263 264 files_struct *file_find_fd(int fd) 265 { 266 int count=0; 267 files_struct *fsp; 268 269 for (fsp=Files;fsp;fsp=fsp->next,count++) { 265 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) { 270 266 if (fsp->fh->fd == fd) { 271 267 if (count > 10) { 272 DLIST_PROMOTE( Files, fsp);268 DLIST_PROMOTE(sconn->files, fsp); 273 269 } 274 270 return fsp; … … 283 279 ****************************************************************************/ 284 280 285 files_struct *file_find_dif(struct file_id id, unsigned long gen_id) 281 files_struct *file_find_dif(struct smbd_server_connection *sconn, 282 struct file_id id, unsigned long gen_id) 286 283 { 287 284 int count=0; 288 285 files_struct *fsp; 289 286 290 for (fsp= Files;fsp;fsp=fsp->next,count++) {287 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) { 291 288 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */ 292 289 if (file_id_equal(&fsp->file_id, &id) && 293 290 fsp->fh->gen_id == gen_id ) { 294 291 if (count > 10) { 295 DLIST_PROMOTE( Files, fsp);292 DLIST_PROMOTE(sconn->files, fsp); 296 293 } 297 294 /* Paranoia check. */ … … 316 313 317 314 /**************************************************************************** 318 Check if an fsp still exists.319 ****************************************************************************/320 321 files_struct *file_find_fsp(files_struct *orig_fsp)322 {323 files_struct *fsp;324 325 for (fsp=Files;fsp;fsp=fsp->next) {326 if (fsp == orig_fsp)327 return fsp;328 }329 330 return NULL;331 }332 333 /****************************************************************************334 315 Find the first fsp given a device and inode. 335 316 We use a singleton cache here to speed up searching from getfilepathinfo … … 337 318 ****************************************************************************/ 338 319 339 files_struct *file_find_di_first(struct file_id id) 340 { 341 files_struct *fsp; 342 343 if (file_id_equal(&fsp_fi_cache.id, &id)) { 320 files_struct *file_find_di_first(struct smbd_server_connection *sconn, 321 struct file_id id) 322 { 323 files_struct *fsp; 324 325 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) { 344 326 /* Positive or negative cache hit. */ 345 return fsp_fi_cache.fsp;346 } 347 348 fsp_fi_cache.id = id;349 350 for (fsp= Files;fsp;fsp=fsp->next) {327 return sconn->fsp_fi_cache.fsp; 328 } 329 330 sconn->fsp_fi_cache.id = id; 331 332 for (fsp=sconn->files;fsp;fsp=fsp->next) { 351 333 if (file_id_equal(&fsp->file_id, &id)) { 352 334 /* Setup positive cache. */ 353 fsp_fi_cache.fsp = fsp;335 sconn->fsp_fi_cache.fsp = fsp; 354 336 return fsp; 355 337 } … … 357 339 358 340 /* Setup negative cache. */ 359 fsp_fi_cache.fsp = NULL;341 sconn->fsp_fi_cache.fsp = NULL; 360 342 return NULL; 361 343 } … … 379 361 380 362 /**************************************************************************** 381 Find a fsp that is open for printing.382 ****************************************************************************/383 384 files_struct *file_find_print(void)385 {386 files_struct *fsp;387 388 for (fsp=Files;fsp;fsp=fsp->next) {389 if (fsp->print_file) {390 return fsp;391 }392 }393 394 return NULL;395 }396 397 /****************************************************************************398 363 Find any fsp open with a pathname below that of an already open path. 399 364 ****************************************************************************/ … … 415 380 dlen = strlen(d_fullname); 416 381 417 for (fsp= Files;fsp;fsp=fsp->next) {382 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) { 418 383 char *d1_fullname; 419 384 … … 452 417 files_struct *fsp, *next; 453 418 454 for (fsp= Files;fsp;fsp=next) {419 for (fsp=conn->sconn->files; fsp; fsp=next) { 455 420 next=fsp->next; 456 421 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) { … … 466 431 void file_free(struct smb_request *req, files_struct *fsp) 467 432 { 468 DLIST_REMOVE(Files, fsp); 433 struct smbd_server_connection *sconn = fsp->conn->sconn; 434 435 DLIST_REMOVE(sconn->files, fsp); 469 436 470 437 TALLOC_FREE(fsp->fake_file_handle); … … 491 458 TALLOC_FREE(fsp->update_write_time_event); 492 459 493 bitmap_clear( file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);494 files_used--;460 bitmap_clear(sconn->file_bmap, fsp->fnum - FILE_HANDLE_OFFSET); 461 sconn->files_used--; 495 462 496 463 DEBUG(5,("freed files structure %d (%d used)\n", 497 fsp->fnum, files_used));464 fsp->fnum, sconn->files_used)); 498 465 499 466 fsp->conn->num_files_open--; … … 503 470 } 504 471 472 /* 473 * Clear all possible chained fsp 474 * pointers in the SMB2 request queue. 475 */ 476 if (req != NULL && req->smb2req) { 477 remove_smb2_chained_fsp(fsp); 478 } 479 505 480 /* Closing a file can invalidate the positive cache. */ 506 if (fsp == fsp_fi_cache.fsp) {507 ZERO_STRUCT( fsp_fi_cache);481 if (fsp == sconn->fsp_fi_cache.fsp) { 482 ZERO_STRUCT(sconn->fsp_fi_cache); 508 483 } 509 484 … … 525 500 ****************************************************************************/ 526 501 527 files_struct *file_fnum(uint16 fnum) 502 static struct files_struct *file_fnum(struct smbd_server_connection *sconn, 503 uint16 fnum) 528 504 { 529 505 files_struct *fsp; 530 506 int count=0; 531 507 532 for (fsp= Files;fsp;fsp=fsp->next, count++) {508 for (fsp=sconn->files; fsp; fsp=fsp->next, count++) { 533 509 if (fsp->fnum == fnum) { 534 510 if (count > 10) { 535 DLIST_PROMOTE( Files, fsp);511 DLIST_PROMOTE(sconn->files, fsp); 536 512 } 537 513 return fsp; … … 542 518 543 519 /**************************************************************************** 544 Get an fsp from a packet given the offset ofa 16 bit fnum.520 Get an fsp from a packet given a 16 bit fnum. 545 521 ****************************************************************************/ 546 522 … … 549 525 files_struct *fsp; 550 526 551 if ((req != NULL) && (req->chain_fsp != NULL)) { 527 if (req == NULL) { 528 /* 529 * We should never get here. req==NULL could in theory 530 * only happen from internal opens with a non-zero 531 * root_dir_fid. Internal opens just don't do that, at 532 * least they are not supposed to do so. And if they 533 * start to do so, they better fake up a smb_request 534 * from which we get the right smbd_server_conn. While 535 * this should never happen, let's return NULL here. 536 */ 537 return NULL; 538 } 539 540 if (req->chain_fsp != NULL) { 552 541 return req->chain_fsp; 553 542 } 554 543 555 fsp = file_fnum( fid);556 if ( (fsp != NULL) && (req != NULL)) {544 fsp = file_fnum(req->sconn, fid); 545 if (fsp != NULL) { 557 546 req->chain_fsp = fsp; 558 547 } … … 589 578 to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False; 590 579 } 591 to->print_file = from->print_file;592 580 to->modified = from->modified; 593 581 to->is_directory = from->is_directory; 594 582 to->aio_write_behind = from->aio_write_behind; 583 584 if (from->print_file) { 585 to->print_file = talloc(to, struct print_file_data); 586 if (!to->print_file) return NT_STATUS_NO_MEMORY; 587 to->print_file->rap_jobid = from->print_file->rap_jobid; 588 } else { 589 to->print_file = NULL; 590 } 591 595 592 return fsp_set_smb_fname(to, from->fsp_name); 593 } 594 595 /** 596 * Return a jenkins hash of a pathname on a connection. 597 */ 598 599 NTSTATUS file_name_hash(connection_struct *conn, 600 const char *name, uint32_t *p_name_hash) 601 { 602 TDB_DATA key; 603 char *fullpath = NULL; 604 605 /* Set the hash of the full pathname. */ 606 fullpath = talloc_asprintf(talloc_tos(), 607 "%s/%s", 608 conn->connectpath, 609 name); 610 if (!fullpath) { 611 return NT_STATUS_NO_MEMORY; 612 } 613 key = string_term_tdb_data(fullpath); 614 *p_name_hash = tdb_jenkins_hash(&key); 615 616 DEBUG(10,("file_name_hash: %s hash 0x%x\n", 617 fullpath, 618 (unsigned int)*p_name_hash )); 619 620 TALLOC_FREE(fullpath); 621 return NT_STATUS_OK; 596 622 } 597 623 … … 613 639 fsp->fsp_name = smb_fname_new; 614 640 615 return NT_STATUS_OK; 616 } 641 return file_name_hash(fsp->conn, 642 smb_fname_str_dbg(fsp->fsp_name), 643 &fsp->name_hash); 644 }
Note:
See TracChangeset
for help on using the changeset viewer.