more merging voodoo
[tprouty/samba.git] / source / smbd / files.c
1 #define OLD_NTDOMAIN 1
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    Files[] structure handling
6    Copyright (C) Andrew Tridgell 1998
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 extern int DEBUGLEVEL;
26
27 static int real_max_open_files;
28
29 #define VALID_FNUM(fnum)   (((fnum) >= 0) && ((fnum) < real_max_open_files))
30
31 #define FILE_HANDLE_OFFSET 0x1000
32
33 static struct bitmap *file_bmap;
34
35 static files_struct *Files;
36  
37 /* a fsp to use when chaining */
38 static files_struct *chain_fsp = NULL;
39 /* a fsp to use to save when breaking an oplock. */
40 static files_struct *oplock_save_chain_fsp = NULL;
41
42 static int files_used;
43
44 /****************************************************************************
45   find first available file slot
46 ****************************************************************************/
47 files_struct *file_new(void )
48 {
49         int i;
50         static int first_file;
51         files_struct *fsp, *next;
52
53         /* we want to give out file handles differently on each new
54            connection because of a common bug in MS clients where they try to
55            reuse a file descriptor from an earlier smb connection. This code
56            increases the chance that the errant client will get an error rather
57            than causing corruption */
58         if (first_file == 0) {
59                 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
60         }
61
62         i = bitmap_find(file_bmap, first_file);
63         if (i == -1) {
64                 /* 
65                  * Before we give up, go through the open files 
66                  * and see if there are any files opened with a
67                  * batch oplock. If so break the oplock and then
68                  * re-use that entry (if it becomes closed).
69                  * This may help as NT/95 clients tend to keep
70                  * files batch oplocked for quite a long time
71                  * after they have finished with them.
72                  */
73                 for (fsp=Files;fsp;fsp=next) {
74                         next=fsp->next;
75                         if (attempt_close_oplocked_file(fsp)) {
76                                 return file_new();
77                         }
78                 }
79
80                 DEBUG(0,("ERROR! Out of file structures\n"));
81                 unix_ERR_class = ERRSRV;
82                 unix_ERR_code = ERRnofids;
83                 return NULL;
84         }
85
86         fsp = (files_struct *)malloc(sizeof(*fsp));
87         if (!fsp) {
88                 unix_ERR_class = ERRSRV;
89                 unix_ERR_code = ERRnofids;
90                 return NULL;
91         }
92
93         ZERO_STRUCTP(fsp);
94         fsp->fd = -1;
95
96         first_file = (i+1) % real_max_open_files;
97
98         bitmap_set(file_bmap, i);
99         files_used++;
100
101         fsp->fnum = i + FILE_HANDLE_OFFSET;
102         string_set(&fsp->fsp_name,"");
103         
104         DLIST_ADD(Files, fsp);
105
106         DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
107                  i, fsp->fnum, files_used));
108
109         chain_fsp = fsp;
110         
111         return fsp;
112 }
113
114
115 /****************************************************************************
116 close all open files for a connection
117 ****************************************************************************/
118 void file_close_conn(connection_struct *conn)
119 {
120         files_struct *fsp, *next;
121         
122         for (fsp=Files;fsp;fsp=next) {
123                 next = fsp->next;
124                 if (fsp->conn == conn) {
125                         close_file(fsp,False); 
126                 }
127         }
128 }
129
130 /****************************************************************************
131 initialise file structures
132 ****************************************************************************/
133
134 #define MAX_OPEN_FUDGEFACTOR 10
135
136 void file_init(void)
137 {
138         int request_max_open_files = lp_max_open_files();
139         int real_lim;
140
141         /*
142          * Set the max_open files to be the requested
143          * max plus a fudgefactor to allow for the extra
144          * fd's we need such as log files etc...
145          */
146         real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
147
148         real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
149
150         if(real_max_open_files != request_max_open_files) {
151                 DEBUG(1,("file_init: Information only: requested %d \
152 open files, %d are available.\n", request_max_open_files, real_max_open_files));
153         }
154
155         file_bmap = bitmap_allocate(real_max_open_files);
156         
157         if (!file_bmap) {
158                 exit_server("out of memory in file_init");
159         }
160         
161         /*
162          * Ensure that pipe_handle_oppset is set correctly.
163          */
164         set_pipe_handle_offset(real_max_open_files);
165 }
166
167
168 /****************************************************************************
169 close files open by a specified vuid
170 ****************************************************************************/
171 void file_close_user(int vuid)
172 {
173         files_struct *fsp, *next;
174
175         for (fsp=Files;fsp;fsp=next) {
176                 next=fsp->next;
177                 if (fsp->vuid == vuid) {
178                         close_file(fsp,False);
179                 }
180         }
181 }
182
183
184 /****************************************************************************
185  Find a fsp given a device, inode and timevalue
186  If this is from a kernel oplock break request then tval may be NULL.
187 ****************************************************************************/
188
189 files_struct *file_find_dit(SMB_DEV_T dev, SMB_INO_T inode, struct timeval *tval)
190 {
191         int count=0;
192         files_struct *fsp;
193
194         for (fsp=Files;fsp;fsp=fsp->next,count++) {
195                 if (fsp->fd != -1 &&
196                     fsp->dev == dev && 
197                     fsp->inode == inode &&
198                     (tval ? (fsp->open_time.tv_sec == tval->tv_sec) : True ) &&
199                     (tval ? (fsp->open_time.tv_usec == tval->tv_usec) : True )) {
200                         if (count > 10) {
201                                 DLIST_PROMOTE(Files, fsp);
202                         }
203                         return fsp;
204                 }
205         }
206
207         return NULL;
208 }
209
210 /****************************************************************************
211  Check if an fsp still exists.
212 ****************************************************************************/
213
214 files_struct *file_find_fsp(files_struct *orig_fsp)
215 {
216         files_struct *fsp;
217
218     for (fsp=Files;fsp;fsp=fsp->next) {
219         if (fsp == orig_fsp)
220             return fsp;
221     }
222
223     return NULL;
224 }
225
226 /****************************************************************************
227  Find the first fsp given a device and inode.
228 ****************************************************************************/
229
230 files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
231 {
232     files_struct *fsp;
233
234     for (fsp=Files;fsp;fsp=fsp->next) {
235         if ( fsp->fd != -1 &&
236             fsp->dev == dev &&
237             fsp->inode == inode )
238             return fsp;
239     }
240
241     return NULL;
242 }
243
244 /****************************************************************************
245  Find the next fsp having the same device and inode.
246 ****************************************************************************/
247
248 files_struct *file_find_di_next(files_struct *start_fsp)
249 {
250     files_struct *fsp;
251
252     for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
253         if ( fsp->fd != -1 &&
254             fsp->dev == start_fsp->dev &&
255             fsp->inode == start_fsp->inode )
256             return fsp;
257     }
258
259     return NULL;
260 }
261
262 /****************************************************************************
263 find a fsp that is open for printing
264 ****************************************************************************/
265 files_struct *file_find_print(void)
266 {
267         files_struct *fsp;
268
269         for (fsp=Files;fsp;fsp=fsp->next) {
270                 if (fsp->print_file) return fsp;
271         } 
272
273         return NULL;
274 }
275
276
277 /****************************************************************************
278 sync open files on a connection
279 ****************************************************************************/
280 void file_sync_all(connection_struct *conn)
281 {
282         files_struct *fsp, *next;
283
284         for (fsp=Files;fsp;fsp=next) {
285                 next=fsp->next;
286                 if ((conn == fsp->conn) && (fsp->fd != -1)) {
287                         sync_file(conn,fsp);
288                 }
289         }
290 }
291
292
293 /****************************************************************************
294 free up a fsp
295 ****************************************************************************/
296 void file_free(files_struct *fsp)
297 {
298         DLIST_REMOVE(Files, fsp);
299
300         string_free(&fsp->fsp_name);
301
302         bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
303         files_used--;
304
305         DEBUG(5,("freed files structure %d (%d used)\n",
306                  fsp->fnum, files_used));
307
308         /* this is paranoia, just in case someone tries to reuse the 
309            information */
310         ZERO_STRUCTP(fsp);
311
312         if (fsp == chain_fsp) chain_fsp = NULL;
313
314         free(fsp);
315 }
316
317
318 /****************************************************************************
319 get a fsp from a packet given the offset of a 16 bit fnum
320 ****************************************************************************/
321 files_struct *file_fsp(char *buf, int where)
322 {
323         int fnum, count=0;
324         files_struct *fsp;
325
326         if (chain_fsp) return chain_fsp;
327
328         fnum = SVAL(buf, where);
329
330         for (fsp=Files;fsp;fsp=fsp->next, count++) {
331                 if (fsp->fnum == fnum) {
332                         chain_fsp = fsp;
333                         if (count > 10) {
334                                 DLIST_PROMOTE(Files, fsp);
335                         }
336                         return fsp;
337                 }
338         }
339         return NULL;
340 }
341
342 /****************************************************************************
343  Reset the chained fsp - done at the start of a packet reply
344 ****************************************************************************/
345
346 void file_chain_reset(void)
347 {
348         chain_fsp = NULL;
349 }
350
351 /****************************************************************************
352 Save the chained fsp - done when about to do an oplock break.
353 ****************************************************************************/
354
355 void file_chain_save(void)
356 {
357         oplock_save_chain_fsp = chain_fsp;
358 }
359
360 /****************************************************************************
361 Restore the chained fsp - done after an oplock break.
362 ****************************************************************************/
363 void file_chain_restore(void)
364 {
365         chain_fsp = oplock_save_chain_fsp;
366 }
367 #undef OLD_NTDOMAIN