r9242: Fix my fix for #2953. I'd moved too much code until after we verify the user,
[Samba.git] / source3 / smbd / files.c
blobc90c2b627cae47177bce2f5fd6b10c7f77278a88
1 /*
2 Unix SMB/CIFS implementation.
3 Files[] structure handling
4 Copyright (C) Andrew Tridgell 1998
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 static int real_max_open_files;
25 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
27 #define FILE_HANDLE_OFFSET 0x1000
29 static struct bitmap *file_bmap;
31 static files_struct *Files;
33 /* a fsp to use when chaining */
34 static files_struct *chain_fsp = NULL;
35 /* a fsp to use to save when breaking an oplock. */
36 static files_struct *oplock_save_chain_fsp = NULL;
38 static int files_used;
40 /* A singleton cache to speed up searching by dev/inode. */
41 static struct fsp_singleton_cache {
42 files_struct *fsp;
43 SMB_DEV_T dev;
44 SMB_INO_T inode;
45 } fsp_fi_cache;
47 /****************************************************************************
48 Return a unique number identifying this fsp over the life of this pid.
49 ****************************************************************************/
51 static unsigned long get_gen_count(void)
53 static unsigned long file_gen_counter;
55 if ((++file_gen_counter) == 0)
56 return ++file_gen_counter;
57 return file_gen_counter;
60 /****************************************************************************
61 Find first available file slot.
62 ****************************************************************************/
64 files_struct *file_new(connection_struct *conn)
66 int i;
67 static int first_file;
68 files_struct *fsp, *next;
70 /* we want to give out file handles differently on each new
71 connection because of a common bug in MS clients where they try to
72 reuse a file descriptor from an earlier smb connection. This code
73 increases the chance that the errant client will get an error rather
74 than causing corruption */
75 if (first_file == 0) {
76 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
79 i = bitmap_find(file_bmap, first_file);
80 if (i == -1) {
81 /*
82 * Before we give up, go through the open files
83 * and see if there are any files opened with a
84 * batch oplock. If so break the oplock and then
85 * re-use that entry (if it becomes closed).
86 * This may help as NT/95 clients tend to keep
87 * files batch oplocked for quite a long time
88 * after they have finished with them.
90 for (fsp=Files;fsp;fsp=next) {
91 next=fsp->next;
92 if (attempt_close_oplocked_file(fsp)) {
93 return file_new(conn);
97 DEBUG(0,("ERROR! Out of file structures\n"));
98 set_saved_error_triple(ERRSRV, ERRnofids, NT_STATUS_TOO_MANY_OPENED_FILES);
99 return NULL;
102 fsp = SMB_MALLOC_P(files_struct);
103 if (!fsp) {
104 set_saved_error_triple(ERRDOS, ERRnomem, NT_STATUS_NO_MEMORY);
105 return NULL;
108 ZERO_STRUCTP(fsp);
110 fsp->fh = SMB_MALLOC_P(struct fd_handle);
111 if (!fsp->fh) {
112 SAFE_FREE(fsp);
113 set_saved_error_triple(ERRDOS, ERRnomem, NT_STATUS_NO_MEMORY);
114 return NULL;
117 ZERO_STRUCTP(fsp->fh);
119 fsp->fh->ref_count = 1;
120 fsp->fh->fd = -1;
122 fsp->conn = conn;
123 fsp->file_id = get_gen_count();
124 GetTimeOfDay(&fsp->open_time);
126 first_file = (i+1) % real_max_open_files;
128 bitmap_set(file_bmap, i);
129 files_used++;
131 fsp->fnum = i + FILE_HANDLE_OFFSET;
132 SMB_ASSERT(fsp->fnum < 65536);
134 string_set(&fsp->fsp_name,"");
136 DLIST_ADD(Files, fsp);
138 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
139 i, fsp->fnum, files_used));
141 chain_fsp = fsp;
143 /* A new fsp invalidates a negative fsp_fi_cache. */
144 if (fsp_fi_cache.fsp == NULL) {
145 ZERO_STRUCT(fsp_fi_cache);
148 return fsp;
151 /****************************************************************************
152 Close all open files for a connection.
153 ****************************************************************************/
155 void file_close_conn(connection_struct *conn)
157 files_struct *fsp, *next;
159 for (fsp=Files;fsp;fsp=next) {
160 next = fsp->next;
161 if (fsp->conn == conn) {
162 close_file(fsp,False);
167 /****************************************************************************
168 Close all open files for a pid.
169 ****************************************************************************/
171 void file_close_pid(uint16 smbpid)
173 files_struct *fsp, *next;
175 for (fsp=Files;fsp;fsp=next) {
176 next = fsp->next;
177 if (fsp->file_pid == smbpid) {
178 close_file(fsp,False);
183 /****************************************************************************
184 Initialise file structures.
185 ****************************************************************************/
187 #define MAX_OPEN_FUDGEFACTOR 20
189 void file_init(void)
191 int request_max_open_files = lp_max_open_files();
192 int real_lim;
195 * Set the max_open files to be the requested
196 * max plus a fudgefactor to allow for the extra
197 * fd's we need such as log files etc...
199 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
201 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
203 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
204 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
206 if(real_max_open_files != request_max_open_files) {
207 DEBUG(1,("file_init: Information only: requested %d \
208 open files, %d are available.\n", request_max_open_files, real_max_open_files));
211 SMB_ASSERT(real_max_open_files > 100);
213 file_bmap = bitmap_allocate(real_max_open_files);
215 if (!file_bmap) {
216 exit_server("out of memory in file_init");
220 * Ensure that pipe_handle_oppset is set correctly.
222 set_pipe_handle_offset(real_max_open_files);
225 /****************************************************************************
226 Close files open by a specified vuid.
227 ****************************************************************************/
229 void file_close_user(int vuid)
231 files_struct *fsp, *next;
233 for (fsp=Files;fsp;fsp=next) {
234 next=fsp->next;
235 if (fsp->vuid == vuid) {
236 close_file(fsp,False);
241 /****************************************************************************
242 Debug to enumerate all open files in the smbd.
243 ****************************************************************************/
245 void file_dump_open_table(void)
247 int count=0;
248 files_struct *fsp;
250 for (fsp=Files;fsp;fsp=fsp->next,count++) {
251 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, fileid = %lu, dev = %x, inode = %.0f\n",
252 count, fsp->fnum, fsp->fsp_name, fsp->fh->fd, (unsigned long)fsp->file_id,
253 (unsigned int)fsp->dev, (double)fsp->inode ));
257 /****************************************************************************
258 Find a fsp given a file descriptor.
259 ****************************************************************************/
261 files_struct *file_find_fd(int fd)
263 int count=0;
264 files_struct *fsp;
266 for (fsp=Files;fsp;fsp=fsp->next,count++) {
267 if (fsp->fh->fd == fd) {
268 if (count > 10) {
269 DLIST_PROMOTE(Files, fsp);
271 return fsp;
275 return NULL;
278 /****************************************************************************
279 Find a fsp given a device, inode and file_id.
280 ****************************************************************************/
282 files_struct *file_find_dif(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
284 int count=0;
285 files_struct *fsp;
287 for (fsp=Files;fsp;fsp=fsp->next,count++) {
288 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
289 if (fsp->dev == dev &&
290 fsp->inode == inode &&
291 fsp->file_id == file_id ) {
292 if (count > 10) {
293 DLIST_PROMOTE(Files, fsp);
295 /* Paranoia check. */
296 if (fsp->fh->fd == -1 && fsp->oplock_type != NO_OPLOCK) {
297 DEBUG(0,("file_find_dif: file %s dev = %x, inode = %.0f, file_id = %u \
298 oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name, (unsigned int)fsp->dev,
299 (double)fsp->inode, (unsigned int)fsp->file_id,
300 (unsigned int)fsp->oplock_type ));
301 smb_panic("file_find_dif\n");
303 return fsp;
307 return NULL;
310 /****************************************************************************
311 Check if an fsp still exists.
312 ****************************************************************************/
314 files_struct *file_find_fsp(files_struct *orig_fsp)
316 files_struct *fsp;
318 for (fsp=Files;fsp;fsp=fsp->next) {
319 if (fsp == orig_fsp)
320 return fsp;
323 return NULL;
326 /****************************************************************************
327 Find the first fsp given a device and inode.
328 We use a singleton cache here to speed up searching from getfilepathinfo
329 calls.
330 ****************************************************************************/
332 files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
334 files_struct *fsp;
336 if (fsp_fi_cache.dev == dev && fsp_fi_cache.inode == inode) {
337 /* Positive or negative cache hit. */
338 return fsp_fi_cache.fsp;
341 fsp_fi_cache.dev = dev;
342 fsp_fi_cache.inode = inode;
344 for (fsp=Files;fsp;fsp=fsp->next) {
345 if ( fsp->fh->fd != -1 &&
346 fsp->dev == dev &&
347 fsp->inode == inode ) {
348 /* Setup positive cache. */
349 fsp_fi_cache.fsp = fsp;
350 return fsp;
354 /* Setup negative cache. */
355 fsp_fi_cache.fsp = NULL;
356 return NULL;
359 /****************************************************************************
360 Find the next fsp having the same device and inode.
361 ****************************************************************************/
363 files_struct *file_find_di_next(files_struct *start_fsp)
365 files_struct *fsp;
367 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
368 if ( fsp->fh->fd != -1 &&
369 fsp->dev == start_fsp->dev &&
370 fsp->inode == start_fsp->inode )
371 return fsp;
374 return NULL;
377 /****************************************************************************
378 Find a fsp that is open for printing.
379 ****************************************************************************/
381 files_struct *file_find_print(void)
383 files_struct *fsp;
385 for (fsp=Files;fsp;fsp=fsp->next) {
386 if (fsp->print_file) {
387 return fsp;
391 return NULL;
394 /****************************************************************************
395 Set a pending modtime across all files with a given dev/ino pair.
396 Record the owner of that modtime.
397 ****************************************************************************/
399 void fsp_set_pending_modtime(files_struct *tfsp, time_t pmod)
401 files_struct *fsp;
403 if (null_mtime(pmod)) {
404 return;
407 for (fsp = Files;fsp;fsp=fsp->next) {
408 if ( fsp->fh->fd != -1 &&
409 fsp->dev == tfsp->dev &&
410 fsp->inode == tfsp->inode ) {
411 fsp->pending_modtime = pmod;
412 fsp->pending_modtime_owner = False;
416 tfsp->pending_modtime_owner = True;
419 /****************************************************************************
420 Sync open files on a connection.
421 ****************************************************************************/
423 void file_sync_all(connection_struct *conn)
425 files_struct *fsp, *next;
427 for (fsp=Files;fsp;fsp=next) {
428 next=fsp->next;
429 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
430 sync_file(conn,fsp);
435 /****************************************************************************
436 Free up a fsp.
437 ****************************************************************************/
439 void file_free(files_struct *fsp)
441 DLIST_REMOVE(Files, fsp);
443 string_free(&fsp->fsp_name);
445 if (fsp->fake_file_handle) {
446 destroy_fake_file_handle(&fsp->fake_file_handle);
449 if (fsp->fh->ref_count == 1) {
450 SAFE_FREE(fsp->fh);
451 } else {
452 fsp->fh->ref_count--;
455 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
456 files_used--;
458 DEBUG(5,("freed files structure %d (%d used)\n",
459 fsp->fnum, files_used));
461 /* this is paranoia, just in case someone tries to reuse the
462 information */
463 ZERO_STRUCTP(fsp);
465 if (fsp == chain_fsp) {
466 chain_fsp = NULL;
469 /* Closing a file can invalidate the positive cache. */
470 if (fsp == fsp_fi_cache.fsp) {
471 ZERO_STRUCT(fsp_fi_cache);
474 SAFE_FREE(fsp);
477 /****************************************************************************
478 Get a fsp from a packet given the offset of a 16 bit fnum.
479 ****************************************************************************/
481 files_struct *file_fsp(char *buf, int where)
483 int fnum, count=0;
484 files_struct *fsp;
486 if (chain_fsp)
487 return chain_fsp;
489 if (!buf)
490 return NULL;
491 fnum = SVAL(buf, where);
493 for (fsp=Files;fsp;fsp=fsp->next, count++) {
494 if (fsp->fnum == fnum) {
495 chain_fsp = fsp;
496 if (count > 10) {
497 DLIST_PROMOTE(Files, fsp);
499 return fsp;
502 return NULL;
505 /****************************************************************************
506 Reset the chained fsp - done at the start of a packet reply.
507 ****************************************************************************/
509 void file_chain_reset(void)
511 chain_fsp = NULL;
514 /****************************************************************************
515 Save the chained fsp - done when about to do an oplock break.
516 ****************************************************************************/
518 void file_chain_save(void)
520 oplock_save_chain_fsp = chain_fsp;
523 /****************************************************************************
524 Restore the chained fsp - done after an oplock break.
525 ****************************************************************************/
527 void file_chain_restore(void)
529 chain_fsp = oplock_save_chain_fsp;
532 /****************************************************************************
533 Duplicate the file handle part for a DOS or FCB open.
534 ****************************************************************************/
536 files_struct *dup_file_fsp(files_struct *fsp,
537 uint32 access_mask,
538 uint32 share_access,
539 uint32 create_options)
541 files_struct *dup_fsp = file_new(fsp->conn);
543 if (!dup_fsp) {
544 return NULL;
547 SAFE_FREE(dup_fsp->fh);
549 dup_fsp->fh = fsp->fh;
550 dup_fsp->fh->ref_count++;
552 dup_fsp->dev = fsp->dev;
553 dup_fsp->inode = fsp->inode;
554 dup_fsp->initial_allocation_size = fsp->initial_allocation_size;
555 dup_fsp->mode = fsp->mode;
556 dup_fsp->file_pid = fsp->file_pid;
557 dup_fsp->vuid = fsp->vuid;
558 dup_fsp->open_time = fsp->open_time;
559 dup_fsp->access_mask = access_mask;
560 dup_fsp->share_access = share_access;
561 dup_fsp->pending_modtime_owner = fsp->pending_modtime_owner;
562 dup_fsp->pending_modtime = fsp->pending_modtime;
563 dup_fsp->last_write_time = fsp->last_write_time;
564 dup_fsp->oplock_type = fsp->oplock_type;
565 dup_fsp->can_lock = fsp->can_lock;
566 dup_fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
567 if (!CAN_WRITE(fsp->conn)) {
568 dup_fsp->can_write = False;
569 } else {
570 dup_fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
572 dup_fsp->print_file = fsp->print_file;
573 dup_fsp->modified = fsp->modified;
574 dup_fsp->is_directory = fsp->is_directory;
575 dup_fsp->is_stat = fsp->is_stat;
576 dup_fsp->aio_write_behind = fsp->aio_write_behind;
577 string_set(&dup_fsp->fsp_name,fsp->fsp_name);
579 return dup_fsp;