| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | Files[] structure handling | 
|---|
| 4 | Copyright (C) Andrew Tridgell 1998 | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 9 | (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | This program is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #include "includes.h" | 
|---|
| 21 |  | 
|---|
| 22 | static int real_max_open_files; | 
|---|
| 23 |  | 
|---|
| 24 | #define VALID_FNUM(fnum)   (((fnum) >= 0) && ((fnum) < real_max_open_files)) | 
|---|
| 25 |  | 
|---|
| 26 | #define FILE_HANDLE_OFFSET 0x1000 | 
|---|
| 27 |  | 
|---|
| 28 | static struct bitmap *file_bmap; | 
|---|
| 29 |  | 
|---|
| 30 | static files_struct *Files; | 
|---|
| 31 |  | 
|---|
| 32 | /* a fsp to use when chaining */ | 
|---|
| 33 | static files_struct *chain_fsp = NULL; | 
|---|
| 34 |  | 
|---|
| 35 | static int files_used; | 
|---|
| 36 |  | 
|---|
| 37 | /* A singleton cache to speed up searching by dev/inode. */ | 
|---|
| 38 | static struct fsp_singleton_cache { | 
|---|
| 39 | files_struct *fsp; | 
|---|
| 40 | struct file_id id; | 
|---|
| 41 | } fsp_fi_cache; | 
|---|
| 42 |  | 
|---|
| 43 | /**************************************************************************** | 
|---|
| 44 | Return a unique number identifying this fsp over the life of this pid. | 
|---|
| 45 | ****************************************************************************/ | 
|---|
| 46 |  | 
|---|
| 47 | static unsigned long get_gen_count(void) | 
|---|
| 48 | { | 
|---|
| 49 | static unsigned long file_gen_counter; | 
|---|
| 50 |  | 
|---|
| 51 | if ((++file_gen_counter) == 0) | 
|---|
| 52 | return ++file_gen_counter; | 
|---|
| 53 | return file_gen_counter; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | /**************************************************************************** | 
|---|
| 57 | Find first available file slot. | 
|---|
| 58 | ****************************************************************************/ | 
|---|
| 59 |  | 
|---|
| 60 | NTSTATUS file_new(connection_struct *conn, files_struct **result) | 
|---|
| 61 | { | 
|---|
| 62 | int i; | 
|---|
| 63 | static int first_file; | 
|---|
| 64 | files_struct *fsp; | 
|---|
| 65 |  | 
|---|
| 66 | /* we want to give out file handles differently on each new | 
|---|
| 67 | connection because of a common bug in MS clients where they try to | 
|---|
| 68 | reuse a file descriptor from an earlier smb connection. This code | 
|---|
| 69 | increases the chance that the errant client will get an error rather | 
|---|
| 70 | than causing corruption */ | 
|---|
| 71 | if (first_file == 0) { | 
|---|
| 72 | first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /* TODO: Port the id-tree implementation from Samba4 */ | 
|---|
| 76 |  | 
|---|
| 77 | i = bitmap_find(file_bmap, first_file); | 
|---|
| 78 | if (i == -1) { | 
|---|
| 79 | DEBUG(0,("ERROR! Out of file structures\n")); | 
|---|
| 80 | /* TODO: We have to unconditionally return a DOS error here, | 
|---|
| 81 | * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with | 
|---|
| 82 | * NTSTATUS negotiated */ | 
|---|
| 83 | return NT_STATUS_TOO_MANY_OPENED_FILES; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | fsp = SMB_MALLOC_P(files_struct); | 
|---|
| 87 | if (!fsp) { | 
|---|
| 88 | return NT_STATUS_NO_MEMORY; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | ZERO_STRUCTP(fsp); | 
|---|
| 92 |  | 
|---|
| 93 | fsp->fh = SMB_MALLOC_P(struct fd_handle); | 
|---|
| 94 | if (!fsp->fh) { | 
|---|
| 95 | SAFE_FREE(fsp); | 
|---|
| 96 | return NT_STATUS_NO_MEMORY; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | ZERO_STRUCTP(fsp->fh); | 
|---|
| 100 |  | 
|---|
| 101 | fsp->fh->ref_count = 1; | 
|---|
| 102 | fsp->fh->fd = -1; | 
|---|
| 103 |  | 
|---|
| 104 | fsp->conn = conn; | 
|---|
| 105 | fsp->fh->gen_id = get_gen_count(); | 
|---|
| 106 | GetTimeOfDay(&fsp->open_time); | 
|---|
| 107 |  | 
|---|
| 108 | first_file = (i+1) % real_max_open_files; | 
|---|
| 109 |  | 
|---|
| 110 | bitmap_set(file_bmap, i); | 
|---|
| 111 | files_used++; | 
|---|
| 112 |  | 
|---|
| 113 | fsp->fnum = i + FILE_HANDLE_OFFSET; | 
|---|
| 114 | SMB_ASSERT(fsp->fnum < 65536); | 
|---|
| 115 |  | 
|---|
| 116 | string_set(&fsp->fsp_name,""); | 
|---|
| 117 |  | 
|---|
| 118 | DLIST_ADD(Files, fsp); | 
|---|
| 119 |  | 
|---|
| 120 | DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n", | 
|---|
| 121 | i, fsp->fnum, files_used)); | 
|---|
| 122 |  | 
|---|
| 123 | chain_fsp = fsp; | 
|---|
| 124 |  | 
|---|
| 125 | /* A new fsp invalidates the positive and | 
|---|
| 126 | negative fsp_fi_cache as the new fsp is pushed | 
|---|
| 127 | at the start of the list and we search from | 
|---|
| 128 | a cache hit to the *end* of the list. */ | 
|---|
| 129 |  | 
|---|
| 130 | ZERO_STRUCT(fsp_fi_cache); | 
|---|
| 131 |  | 
|---|
| 132 | conn->num_files_open++; | 
|---|
| 133 |  | 
|---|
| 134 | *result = fsp; | 
|---|
| 135 | return NT_STATUS_OK; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | /**************************************************************************** | 
|---|
| 139 | Close all open files for a connection. | 
|---|
| 140 | ****************************************************************************/ | 
|---|
| 141 |  | 
|---|
| 142 | void file_close_conn(connection_struct *conn) | 
|---|
| 143 | { | 
|---|
| 144 | files_struct *fsp, *next; | 
|---|
| 145 |  | 
|---|
| 146 | for (fsp=Files;fsp;fsp=next) { | 
|---|
| 147 | next = fsp->next; | 
|---|
| 148 | if (fsp->conn == conn) { | 
|---|
| 149 | close_file(fsp,SHUTDOWN_CLOSE); | 
|---|
| 150 | } | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /**************************************************************************** | 
|---|
| 155 | Close all open files for a pid and a vuid. | 
|---|
| 156 | ****************************************************************************/ | 
|---|
| 157 |  | 
|---|
| 158 | void file_close_pid(uint16 smbpid, int vuid) | 
|---|
| 159 | { | 
|---|
| 160 | files_struct *fsp, *next; | 
|---|
| 161 |  | 
|---|
| 162 | for (fsp=Files;fsp;fsp=next) { | 
|---|
| 163 | next = fsp->next; | 
|---|
| 164 | if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) { | 
|---|
| 165 | close_file(fsp,SHUTDOWN_CLOSE); | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | /**************************************************************************** | 
|---|
| 171 | Initialise file structures. | 
|---|
| 172 | ****************************************************************************/ | 
|---|
| 173 |  | 
|---|
| 174 | #define MAX_OPEN_FUDGEFACTOR 20 | 
|---|
| 175 |  | 
|---|
| 176 | void file_init(void) | 
|---|
| 177 | { | 
|---|
| 178 | int request_max_open_files = lp_max_open_files(); | 
|---|
| 179 | int real_lim; | 
|---|
| 180 |  | 
|---|
| 181 | /* | 
|---|
| 182 | * Set the max_open files to be the requested | 
|---|
| 183 | * max plus a fudgefactor to allow for the extra | 
|---|
| 184 | * fd's we need such as log files etc... | 
|---|
| 185 | */ | 
|---|
| 186 | real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR); | 
|---|
| 187 |  | 
|---|
| 188 | real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR; | 
|---|
| 189 |  | 
|---|
| 190 | if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) | 
|---|
| 191 | real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES; | 
|---|
| 192 |  | 
|---|
| 193 | if(real_max_open_files != request_max_open_files) { | 
|---|
| 194 | DEBUG(1,("file_init: Information only: requested %d \ | 
|---|
| 195 | open files, %d are available.\n", request_max_open_files, real_max_open_files)); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | SMB_ASSERT(real_max_open_files > 100); | 
|---|
| 199 |  | 
|---|
| 200 | file_bmap = bitmap_allocate(real_max_open_files); | 
|---|
| 201 |  | 
|---|
| 202 | if (!file_bmap) { | 
|---|
| 203 | exit_server("out of memory in file_init"); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | /* | 
|---|
| 207 | * Ensure that pipe_handle_oppset is set correctly. | 
|---|
| 208 | */ | 
|---|
| 209 | set_pipe_handle_offset(real_max_open_files); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /**************************************************************************** | 
|---|
| 213 | Close files open by a specified vuid. | 
|---|
| 214 | ****************************************************************************/ | 
|---|
| 215 |  | 
|---|
| 216 | void file_close_user(int vuid) | 
|---|
| 217 | { | 
|---|
| 218 | files_struct *fsp, *next; | 
|---|
| 219 |  | 
|---|
| 220 | for (fsp=Files;fsp;fsp=next) { | 
|---|
| 221 | next=fsp->next; | 
|---|
| 222 | if (fsp->vuid == vuid) { | 
|---|
| 223 | close_file(fsp,SHUTDOWN_CLOSE); | 
|---|
| 224 | } | 
|---|
| 225 | } | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | /**************************************************************************** | 
|---|
| 229 | Debug to enumerate all open files in the smbd. | 
|---|
| 230 | ****************************************************************************/ | 
|---|
| 231 |  | 
|---|
| 232 | void file_dump_open_table(void) | 
|---|
| 233 | { | 
|---|
| 234 | int count=0; | 
|---|
| 235 | files_struct *fsp; | 
|---|
| 236 |  | 
|---|
| 237 | for (fsp=Files;fsp;fsp=fsp->next,count++) { | 
|---|
| 238 | DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, gen = %lu, fileid=%s\n", | 
|---|
| 239 | count, fsp->fnum, fsp->fsp_name, fsp->fh->fd, (unsigned long)fsp->fh->gen_id, | 
|---|
| 240 | file_id_string_tos(&fsp->file_id))); | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | /**************************************************************************** | 
|---|
| 245 | Find a fsp given a file descriptor. | 
|---|
| 246 | ****************************************************************************/ | 
|---|
| 247 |  | 
|---|
| 248 | files_struct *file_find_fd(int fd) | 
|---|
| 249 | { | 
|---|
| 250 | int count=0; | 
|---|
| 251 | files_struct *fsp; | 
|---|
| 252 |  | 
|---|
| 253 | for (fsp=Files;fsp;fsp=fsp->next,count++) { | 
|---|
| 254 | if (fsp->fh->fd == fd) { | 
|---|
| 255 | if (count > 10) { | 
|---|
| 256 | DLIST_PROMOTE(Files, fsp); | 
|---|
| 257 | } | 
|---|
| 258 | return fsp; | 
|---|
| 259 | } | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | return NULL; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | /**************************************************************************** | 
|---|
| 266 | Find a fsp given a device, inode and file_id. | 
|---|
| 267 | ****************************************************************************/ | 
|---|
| 268 |  | 
|---|
| 269 | files_struct *file_find_dif(struct file_id id, unsigned long gen_id) | 
|---|
| 270 | { | 
|---|
| 271 | int count=0; | 
|---|
| 272 | files_struct *fsp; | 
|---|
| 273 |  | 
|---|
| 274 | for (fsp=Files;fsp;fsp=fsp->next,count++) { | 
|---|
| 275 | /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */ | 
|---|
| 276 | if (file_id_equal(&fsp->file_id, &id) && | 
|---|
| 277 | fsp->fh->gen_id == gen_id ) { | 
|---|
| 278 | if (count > 10) { | 
|---|
| 279 | DLIST_PROMOTE(Files, fsp); | 
|---|
| 280 | } | 
|---|
| 281 | /* Paranoia check. */ | 
|---|
| 282 | if ((fsp->fh->fd == -1) && | 
|---|
| 283 | (fsp->oplock_type != NO_OPLOCK) && | 
|---|
| 284 | (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) { | 
|---|
| 285 | DEBUG(0,("file_find_dif: file %s file_id = %s, gen = %u \ | 
|---|
| 286 | oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name, | 
|---|
| 287 | file_id_string_tos(&fsp->file_id), | 
|---|
| 288 | (unsigned int)fsp->fh->gen_id, | 
|---|
| 289 | (unsigned int)fsp->oplock_type )); | 
|---|
| 290 | smb_panic("file_find_dif"); | 
|---|
| 291 | } | 
|---|
| 292 | return fsp; | 
|---|
| 293 | } | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | return NULL; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | /**************************************************************************** | 
|---|
| 300 | Check if an fsp still exists. | 
|---|
| 301 | ****************************************************************************/ | 
|---|
| 302 |  | 
|---|
| 303 | files_struct *file_find_fsp(files_struct *orig_fsp) | 
|---|
| 304 | { | 
|---|
| 305 | files_struct *fsp; | 
|---|
| 306 |  | 
|---|
| 307 | for (fsp=Files;fsp;fsp=fsp->next) { | 
|---|
| 308 | if (fsp == orig_fsp) | 
|---|
| 309 | return fsp; | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | return NULL; | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | /**************************************************************************** | 
|---|
| 316 | Find the first fsp given a device and inode. | 
|---|
| 317 | We use a singleton cache here to speed up searching from getfilepathinfo | 
|---|
| 318 | calls. | 
|---|
| 319 | ****************************************************************************/ | 
|---|
| 320 |  | 
|---|
| 321 | files_struct *file_find_di_first(struct file_id id) | 
|---|
| 322 | { | 
|---|
| 323 | files_struct *fsp; | 
|---|
| 324 |  | 
|---|
| 325 | if (file_id_equal(&fsp_fi_cache.id, &id)) { | 
|---|
| 326 | /* Positive or negative cache hit. */ | 
|---|
| 327 | return fsp_fi_cache.fsp; | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | fsp_fi_cache.id = id; | 
|---|
| 331 |  | 
|---|
| 332 | for (fsp=Files;fsp;fsp=fsp->next) { | 
|---|
| 333 | if (file_id_equal(&fsp->file_id, &id)) { | 
|---|
| 334 | /* Setup positive cache. */ | 
|---|
| 335 | fsp_fi_cache.fsp = fsp; | 
|---|
| 336 | return fsp; | 
|---|
| 337 | } | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | /* Setup negative cache. */ | 
|---|
| 341 | fsp_fi_cache.fsp = NULL; | 
|---|
| 342 | return NULL; | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | /**************************************************************************** | 
|---|
| 346 | Find the next fsp having the same device and inode. | 
|---|
| 347 | ****************************************************************************/ | 
|---|
| 348 |  | 
|---|
| 349 | files_struct *file_find_di_next(files_struct *start_fsp) | 
|---|
| 350 | { | 
|---|
| 351 | files_struct *fsp; | 
|---|
| 352 |  | 
|---|
| 353 | for (fsp = start_fsp->next;fsp;fsp=fsp->next) { | 
|---|
| 354 | if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) { | 
|---|
| 355 | return fsp; | 
|---|
| 356 | } | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | return NULL; | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | /**************************************************************************** | 
|---|
| 363 | Find a fsp that is open for printing. | 
|---|
| 364 | ****************************************************************************/ | 
|---|
| 365 |  | 
|---|
| 366 | files_struct *file_find_print(void) | 
|---|
| 367 | { | 
|---|
| 368 | files_struct *fsp; | 
|---|
| 369 |  | 
|---|
| 370 | for (fsp=Files;fsp;fsp=fsp->next) { | 
|---|
| 371 | if (fsp->print_file) { | 
|---|
| 372 | return fsp; | 
|---|
| 373 | } | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | return NULL; | 
|---|
| 377 | } | 
|---|
| 378 |  | 
|---|
| 379 | /**************************************************************************** | 
|---|
| 380 | Find any fsp open with a pathname below that of an already open path. | 
|---|
| 381 | ****************************************************************************/ | 
|---|
| 382 |  | 
|---|
| 383 | bool file_find_subpath(files_struct *dir_fsp) | 
|---|
| 384 | { | 
|---|
| 385 | files_struct *fsp; | 
|---|
| 386 | size_t dlen; | 
|---|
| 387 | char *d_fullname = talloc_asprintf(talloc_tos(), | 
|---|
| 388 | "%s/%s", | 
|---|
| 389 | dir_fsp->conn->connectpath, | 
|---|
| 390 | dir_fsp->fsp_name); | 
|---|
| 391 |  | 
|---|
| 392 | if (!d_fullname) { | 
|---|
| 393 | return false; | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 | dlen = strlen(d_fullname); | 
|---|
| 397 |  | 
|---|
| 398 | for (fsp=Files;fsp;fsp=fsp->next) { | 
|---|
| 399 | char *d1_fullname; | 
|---|
| 400 |  | 
|---|
| 401 | if (fsp == dir_fsp) { | 
|---|
| 402 | continue; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | d1_fullname = talloc_asprintf(talloc_tos(), | 
|---|
| 406 | "%s/%s", | 
|---|
| 407 | fsp->conn->connectpath, | 
|---|
| 408 | fsp->fsp_name); | 
|---|
| 409 |  | 
|---|
| 410 | /* | 
|---|
| 411 | * If the open file has a path that is a longer | 
|---|
| 412 | * component, then it's a subpath. | 
|---|
| 413 | */ | 
|---|
| 414 | if (strnequal(d_fullname, d1_fullname, dlen) && | 
|---|
| 415 | (d1_fullname[dlen] == '/')) { | 
|---|
| 416 | TALLOC_FREE(d_fullname); | 
|---|
| 417 | TALLOC_FREE(d1_fullname); | 
|---|
| 418 | return true; | 
|---|
| 419 | } | 
|---|
| 420 | TALLOC_FREE(d1_fullname); | 
|---|
| 421 | } | 
|---|
| 422 |  | 
|---|
| 423 | TALLOC_FREE(d_fullname); | 
|---|
| 424 | return false; | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | /**************************************************************************** | 
|---|
| 428 | Sync open files on a connection. | 
|---|
| 429 | ****************************************************************************/ | 
|---|
| 430 |  | 
|---|
| 431 | void file_sync_all(connection_struct *conn) | 
|---|
| 432 | { | 
|---|
| 433 | files_struct *fsp, *next; | 
|---|
| 434 |  | 
|---|
| 435 | for (fsp=Files;fsp;fsp=next) { | 
|---|
| 436 | next=fsp->next; | 
|---|
| 437 | if ((conn == fsp->conn) && (fsp->fh->fd != -1)) { | 
|---|
| 438 | sync_file(conn, fsp, True /* write through */); | 
|---|
| 439 | } | 
|---|
| 440 | } | 
|---|
| 441 | } | 
|---|
| 442 |  | 
|---|
| 443 | /**************************************************************************** | 
|---|
| 444 | Free up a fsp. | 
|---|
| 445 | ****************************************************************************/ | 
|---|
| 446 |  | 
|---|
| 447 | void file_free(files_struct *fsp) | 
|---|
| 448 | { | 
|---|
| 449 | DLIST_REMOVE(Files, fsp); | 
|---|
| 450 |  | 
|---|
| 451 | string_free(&fsp->fsp_name); | 
|---|
| 452 |  | 
|---|
| 453 | TALLOC_FREE(fsp->fake_file_handle); | 
|---|
| 454 |  | 
|---|
| 455 | if (fsp->fh->ref_count == 1) { | 
|---|
| 456 | SAFE_FREE(fsp->fh); | 
|---|
| 457 | } else { | 
|---|
| 458 | fsp->fh->ref_count--; | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | if (fsp->notify) { | 
|---|
| 462 | notify_remove(fsp->conn->notify_ctx, fsp); | 
|---|
| 463 | TALLOC_FREE(fsp->notify); | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | /* Ensure this event will never fire. */ | 
|---|
| 467 | TALLOC_FREE(fsp->oplock_timeout); | 
|---|
| 468 |  | 
|---|
| 469 | /* Ensure this event will never fire. */ | 
|---|
| 470 | TALLOC_FREE(fsp->update_write_time_event); | 
|---|
| 471 |  | 
|---|
| 472 | bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET); | 
|---|
| 473 | files_used--; | 
|---|
| 474 |  | 
|---|
| 475 | DEBUG(5,("freed files structure %d (%d used)\n", | 
|---|
| 476 | fsp->fnum, files_used)); | 
|---|
| 477 |  | 
|---|
| 478 | fsp->conn->num_files_open--; | 
|---|
| 479 |  | 
|---|
| 480 | if (fsp == chain_fsp) { | 
|---|
| 481 | chain_fsp = NULL; | 
|---|
| 482 | } | 
|---|
| 483 |  | 
|---|
| 484 | /* Closing a file can invalidate the positive cache. */ | 
|---|
| 485 | if (fsp == fsp_fi_cache.fsp) { | 
|---|
| 486 | ZERO_STRUCT(fsp_fi_cache); | 
|---|
| 487 | } | 
|---|
| 488 |  | 
|---|
| 489 | /* Drop all remaining extensions. */ | 
|---|
| 490 | while (fsp->vfs_extension) { | 
|---|
| 491 | vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp); | 
|---|
| 492 | } | 
|---|
| 493 |  | 
|---|
| 494 | /* this is paranoia, just in case someone tries to reuse the | 
|---|
| 495 | information */ | 
|---|
| 496 | ZERO_STRUCTP(fsp); | 
|---|
| 497 |  | 
|---|
| 498 | SAFE_FREE(fsp); | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 501 | /**************************************************************************** | 
|---|
| 502 | Get an fsp from a 16 bit fnum. | 
|---|
| 503 | ****************************************************************************/ | 
|---|
| 504 |  | 
|---|
| 505 | files_struct *file_fnum(uint16 fnum) | 
|---|
| 506 | { | 
|---|
| 507 | files_struct *fsp; | 
|---|
| 508 | int count=0; | 
|---|
| 509 |  | 
|---|
| 510 | for (fsp=Files;fsp;fsp=fsp->next, count++) { | 
|---|
| 511 | if (fsp->fnum == fnum) { | 
|---|
| 512 | if (count > 10) { | 
|---|
| 513 | DLIST_PROMOTE(Files, fsp); | 
|---|
| 514 | } | 
|---|
| 515 | return fsp; | 
|---|
| 516 | } | 
|---|
| 517 | } | 
|---|
| 518 | return NULL; | 
|---|
| 519 | } | 
|---|
| 520 |  | 
|---|
| 521 | /**************************************************************************** | 
|---|
| 522 | Get an fsp from a packet given the offset of a 16 bit fnum. | 
|---|
| 523 | ****************************************************************************/ | 
|---|
| 524 |  | 
|---|
| 525 | files_struct *file_fsp(uint16 fid) | 
|---|
| 526 | { | 
|---|
| 527 | files_struct *fsp; | 
|---|
| 528 |  | 
|---|
| 529 | if (chain_fsp) { | 
|---|
| 530 | return chain_fsp; | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | fsp = file_fnum(fid); | 
|---|
| 534 | if (fsp) { | 
|---|
| 535 | chain_fsp = fsp; | 
|---|
| 536 | } | 
|---|
| 537 | return fsp; | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 | /**************************************************************************** | 
|---|
| 541 | Reset the chained fsp - done at the start of a packet reply. | 
|---|
| 542 | ****************************************************************************/ | 
|---|
| 543 |  | 
|---|
| 544 | void file_chain_reset(void) | 
|---|
| 545 | { | 
|---|
| 546 | chain_fsp = NULL; | 
|---|
| 547 | } | 
|---|
| 548 |  | 
|---|
| 549 | /**************************************************************************** | 
|---|
| 550 | Duplicate the file handle part for a DOS or FCB open. | 
|---|
| 551 | ****************************************************************************/ | 
|---|
| 552 |  | 
|---|
| 553 | void dup_file_fsp(files_struct *from, | 
|---|
| 554 | uint32 access_mask, | 
|---|
| 555 | uint32 share_access, | 
|---|
| 556 | uint32 create_options, | 
|---|
| 557 | files_struct *to) | 
|---|
| 558 | { | 
|---|
| 559 | SAFE_FREE(to->fh); | 
|---|
| 560 |  | 
|---|
| 561 | to->fh = from->fh; | 
|---|
| 562 | to->fh->ref_count++; | 
|---|
| 563 |  | 
|---|
| 564 | to->file_id = from->file_id; | 
|---|
| 565 | to->initial_allocation_size = from->initial_allocation_size; | 
|---|
| 566 | to->mode = from->mode; | 
|---|
| 567 | to->file_pid = from->file_pid; | 
|---|
| 568 | to->vuid = from->vuid; | 
|---|
| 569 | to->open_time = from->open_time; | 
|---|
| 570 | to->access_mask = access_mask; | 
|---|
| 571 | to->share_access = share_access; | 
|---|
| 572 | to->oplock_type = from->oplock_type; | 
|---|
| 573 | to->can_lock = from->can_lock; | 
|---|
| 574 | to->can_read = (access_mask & (FILE_READ_DATA)) ? True : False; | 
|---|
| 575 | if (!CAN_WRITE(from->conn)) { | 
|---|
| 576 | to->can_write = False; | 
|---|
| 577 | } else { | 
|---|
| 578 | to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False; | 
|---|
| 579 | } | 
|---|
| 580 | to->print_file = from->print_file; | 
|---|
| 581 | to->modified = from->modified; | 
|---|
| 582 | to->is_directory = from->is_directory; | 
|---|
| 583 | to->aio_write_behind = from->aio_write_behind; | 
|---|
| 584 | string_set(&to->fsp_name,from->fsp_name); | 
|---|
| 585 | } | 
|---|