r6369: update release notes
[Samba.git] / source / smbd / files.c
blobe893e9fefc1cc7df78a10357d5d85b8c9c063890
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);
109 fsp->fd = -1;
110 fsp->conn = conn;
111 fsp->file_id = get_gen_count();
112 GetTimeOfDay(&fsp->open_time);
114 first_file = (i+1) % real_max_open_files;
116 bitmap_set(file_bmap, i);
117 files_used++;
119 fsp->fnum = i + FILE_HANDLE_OFFSET;
120 SMB_ASSERT(fsp->fnum < 65536);
122 string_set(&fsp->fsp_name,"");
124 DLIST_ADD(Files, fsp);
126 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
127 i, fsp->fnum, files_used));
129 chain_fsp = fsp;
131 /* A new fsp invalidates a negative fsp_fi_cache. */
132 if (fsp_fi_cache.fsp == NULL) {
133 ZERO_STRUCT(fsp_fi_cache);
136 return fsp;
139 /****************************************************************************
140 Close all open files for a connection.
141 ****************************************************************************/
143 void file_close_conn(connection_struct *conn)
145 files_struct *fsp, *next;
147 for (fsp=Files;fsp;fsp=next) {
148 next = fsp->next;
149 if (fsp->conn == conn) {
150 close_file(fsp,False);
155 /****************************************************************************
156 Close all open files for a pid.
157 ****************************************************************************/
159 void file_close_pid(uint16 smbpid)
161 files_struct *fsp, *next;
163 for (fsp=Files;fsp;fsp=next) {
164 next = fsp->next;
165 if (fsp->file_pid == smbpid) {
166 close_file(fsp,False);
171 /****************************************************************************
172 Initialise file structures.
173 ****************************************************************************/
175 #define MAX_OPEN_FUDGEFACTOR 20
177 void file_init(void)
179 int request_max_open_files = lp_max_open_files();
180 int real_lim;
183 * Set the max_open files to be the requested
184 * max plus a fudgefactor to allow for the extra
185 * fd's we need such as log files etc...
187 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
189 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
191 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
192 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
194 if(real_max_open_files != request_max_open_files) {
195 DEBUG(1,("file_init: Information only: requested %d \
196 open files, %d are available.\n", request_max_open_files, real_max_open_files));
199 SMB_ASSERT(real_max_open_files > 100);
201 file_bmap = bitmap_allocate(real_max_open_files);
203 if (!file_bmap) {
204 exit_server("out of memory in file_init");
208 * Ensure that pipe_handle_oppset is set correctly.
210 set_pipe_handle_offset(real_max_open_files);
213 /****************************************************************************
214 Close files open by a specified vuid.
215 ****************************************************************************/
217 void file_close_user(int vuid)
219 files_struct *fsp, *next;
221 for (fsp=Files;fsp;fsp=next) {
222 next=fsp->next;
223 if (fsp->vuid == vuid) {
224 close_file(fsp,False);
229 void file_dump_open_table(void)
231 int count=0;
232 files_struct *fsp;
234 for (fsp=Files;fsp;fsp=fsp->next,count++) {
235 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, fileid = %lu, dev = %x, inode = %.0f\n",
236 count, fsp->fnum, fsp->fsp_name, fsp->fd, (unsigned long)fsp->file_id,
237 (unsigned int)fsp->dev, (double)fsp->inode ));
241 /****************************************************************************
242 Find a fsp given a file descriptor.
243 ****************************************************************************/
245 files_struct *file_find_fd(int fd)
247 int count=0;
248 files_struct *fsp;
250 for (fsp=Files;fsp;fsp=fsp->next,count++) {
251 if (fsp->fd == fd) {
252 if (count > 10) {
253 DLIST_PROMOTE(Files, fsp);
255 return fsp;
259 return NULL;
262 /****************************************************************************
263 Find a fsp given a device, inode and file_id.
264 ****************************************************************************/
266 files_struct *file_find_dif(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
268 int count=0;
269 files_struct *fsp;
271 for (fsp=Files;fsp;fsp=fsp->next,count++) {
272 /* We can have a fsp->fd == -1 here as it could be a stat open. */
273 if (fsp->dev == dev &&
274 fsp->inode == inode &&
275 fsp->file_id == file_id ) {
276 if (count > 10) {
277 DLIST_PROMOTE(Files, fsp);
279 /* Paranoia check. */
280 if (fsp->fd == -1 && fsp->oplock_type != NO_OPLOCK) {
281 DEBUG(0,("file_find_dif: file %s dev = %x, inode = %.0f, file_id = %u \
282 oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name, (unsigned int)fsp->dev,
283 (double)fsp->inode, (unsigned int)fsp->file_id,
284 (unsigned int)fsp->oplock_type ));
285 smb_panic("file_find_dif\n");
287 return fsp;
291 return NULL;
294 /****************************************************************************
295 Check if an fsp still exists.
296 ****************************************************************************/
298 files_struct *file_find_fsp(files_struct *orig_fsp)
300 files_struct *fsp;
302 for (fsp=Files;fsp;fsp=fsp->next) {
303 if (fsp == orig_fsp)
304 return fsp;
307 return NULL;
310 /****************************************************************************
311 Find the first fsp given a device and inode.
312 We use a singleton cache here to speed up searching from getfilepathinfo
313 calls.
314 ****************************************************************************/
316 files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
318 files_struct *fsp;
320 if (fsp_fi_cache.dev == dev && fsp_fi_cache.inode == inode) {
321 /* Positive or negative cache hit. */
322 return fsp_fi_cache.fsp;
325 fsp_fi_cache.dev = dev;
326 fsp_fi_cache.inode = inode;
328 for (fsp=Files;fsp;fsp=fsp->next) {
329 if ( fsp->fd != -1 &&
330 fsp->dev == dev &&
331 fsp->inode == inode ) {
332 /* Setup positive cache. */
333 fsp_fi_cache.fsp = fsp;
334 return fsp;
338 /* Setup negative cache. */
339 fsp_fi_cache.fsp = NULL;
340 return NULL;
343 /****************************************************************************
344 Find the next fsp having the same device and inode.
345 ****************************************************************************/
347 files_struct *file_find_di_next(files_struct *start_fsp)
349 files_struct *fsp;
351 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
352 if ( fsp->fd != -1 &&
353 fsp->dev == start_fsp->dev &&
354 fsp->inode == start_fsp->inode )
355 return fsp;
358 return NULL;
361 /****************************************************************************
362 Find a fsp that is open for printing.
363 ****************************************************************************/
365 files_struct *file_find_print(void)
367 files_struct *fsp;
369 for (fsp=Files;fsp;fsp=fsp->next) {
370 if (fsp->print_file) {
371 return fsp;
375 return NULL;
378 /****************************************************************************
379 Set a pending modtime across all files with a given dev/ino pair.
380 Record the owner of that modtime.
381 ****************************************************************************/
383 void fsp_set_pending_modtime(files_struct *tfsp, time_t pmod)
385 files_struct *fsp;
387 if (null_mtime(pmod)) {
388 return;
391 for (fsp = Files;fsp;fsp=fsp->next) {
392 if ( fsp->fd != -1 &&
393 fsp->dev == tfsp->dev &&
394 fsp->inode == tfsp->inode ) {
395 fsp->pending_modtime = pmod;
396 fsp->pending_modtime_owner = False;
400 tfsp->pending_modtime_owner = True;
403 /****************************************************************************
404 Sync open files on a connection.
405 ****************************************************************************/
407 void file_sync_all(connection_struct *conn)
409 files_struct *fsp, *next;
411 for (fsp=Files;fsp;fsp=next) {
412 next=fsp->next;
413 if ((conn == fsp->conn) && (fsp->fd != -1)) {
414 sync_file(conn,fsp);
419 /****************************************************************************
420 Free up a fsp.
421 ****************************************************************************/
423 void file_free(files_struct *fsp)
425 DLIST_REMOVE(Files, fsp);
427 string_free(&fsp->fsp_name);
429 if (fsp->fake_file_handle) {
430 destroy_fake_file_handle(&fsp->fake_file_handle);
433 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
434 files_used--;
436 DEBUG(5,("freed files structure %d (%d used)\n",
437 fsp->fnum, files_used));
439 /* this is paranoia, just in case someone tries to reuse the
440 information */
441 ZERO_STRUCTP(fsp);
443 if (fsp == chain_fsp) {
444 chain_fsp = NULL;
447 /* Closing a file can invalidate the positive cache. */
448 if (fsp == fsp_fi_cache.fsp) {
449 ZERO_STRUCT(fsp_fi_cache);
452 SAFE_FREE(fsp);
455 /****************************************************************************
456 Get a fsp from a packet given the offset of a 16 bit fnum.
457 ****************************************************************************/
459 files_struct *file_fsp(char *buf, int where)
461 int fnum, count=0;
462 files_struct *fsp;
464 if (chain_fsp)
465 return chain_fsp;
467 if (!buf)
468 return NULL;
469 fnum = SVAL(buf, where);
471 for (fsp=Files;fsp;fsp=fsp->next, count++) {
472 if (fsp->fnum == fnum) {
473 chain_fsp = fsp;
474 if (count > 10) {
475 DLIST_PROMOTE(Files, fsp);
477 return fsp;
480 return NULL;
483 /****************************************************************************
484 Reset the chained fsp - done at the start of a packet reply.
485 ****************************************************************************/
487 void file_chain_reset(void)
489 chain_fsp = NULL;
492 /****************************************************************************
493 Save the chained fsp - done when about to do an oplock break.
494 ****************************************************************************/
496 void file_chain_save(void)
498 oplock_save_chain_fsp = chain_fsp;
501 /****************************************************************************
502 Restore the chained fsp - done after an oplock break.
503 ****************************************************************************/
505 void file_chain_restore(void)
507 chain_fsp = oplock_save_chain_fsp;