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