Fixed bug introduced by the recent changes where the chain_fnum
[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 extern int DEBUGLEVEL;
25
26 /* the only restriction is that this must be less than PIPE_HANDLE_OFFSET */
27 #define MAX_FNUMS 4096
28
29 #define VALID_FNUM(fnum)   (((fnum) >= 0) && ((fnum) < MAX_FNUMS))
30
31 #define FILE_HANDLE_OFFSET 0x1000
32
33 static struct bitmap *file_bmap;
34 static struct bitmap *fd_bmap;
35
36 static files_struct *Files;
37
38 /* a fsp to use when chaining */
39 static files_struct *chain_fsp = NULL;
40 /* a fsp to use to save when breaking an oplock. */
41 static files_struct *oplock_save_chain_fsp = NULL;
42
43 /*
44  * Indirection for file fd's. Needed as POSIX locking
45  * is based on file/process, not fd/process.
46  */
47 static file_fd_struct *FileFd;
48
49 static int files_used, fd_ptr_used;
50
51 /****************************************************************************
52   find first available file slot
53 ****************************************************************************/
54 files_struct *file_new(void )
55 {
56         int i;
57         static int first_file;
58         files_struct *fsp, *next;
59
60         /* we want to give out file handles differently on each new
61            connection because of a common bug in MS clients where they try to
62            reuse a file descriptor from an earlier smb connection. This code
63            increases the chance that the errant client will get an error rather
64            than causing corruption */
65         if (first_file == 0) {
66                 first_file = (getpid() ^ (int)time(NULL)) % MAX_FNUMS;
67         }
68
69         i = bitmap_find(file_bmap, first_file);
70         if (i == -1) {
71                 /* 
72                  * Before we give up, go through the open files 
73                  * and see if there are any files opened with a
74                  * batch oplock. If so break the oplock and then
75                  * re-use that entry (if it becomes closed).
76                  * This may help as NT/95 clients tend to keep
77                  * files batch oplocked for quite a long time
78                  * after they have finished with them.
79                  */
80                 for (fsp=Files;fsp;fsp=next) {
81                         next=fsp->next;
82                         if (attempt_close_oplocked_file(fsp)) {
83                                 return file_new();
84                         }
85                 }
86
87                 DEBUG(0,("ERROR! Out of file structures\n"));
88                 return NULL;
89         }
90
91         fsp = (files_struct *)malloc(sizeof(*fsp));
92         if (!fsp) return NULL;
93
94         memset(fsp, 0, sizeof(*fsp));
95
96         first_file = (i+1) % MAX_FNUMS;
97
98         bitmap_set(file_bmap, i);
99         files_used++;
100
101         fsp->fnum = i + FILE_HANDLE_OFFSET;
102         string_init(&fsp->fsp_name,"");
103         
104         DLIST_ADD(Files, fsp);
105
106         DEBUG(5,("allocated file structure %d (%d used)\n",
107                  i, files_used));
108
109         chain_fsp = fsp;
110         
111         return fsp;
112 }
113
114
115
116 /****************************************************************************
117 fd support routines - attempt to find an already open file by dev
118 and inode - increments the ref_count of the returned file_fd_struct *.
119 ****************************************************************************/
120 file_fd_struct *fd_get_already_open(struct stat *sbuf)
121 {
122         file_fd_struct *fd_ptr;
123
124         if(!sbuf) return NULL;
125
126         for (fd_ptr=FileFd;fd_ptr;fd_ptr=fd_ptr->next) {
127                 if ((fd_ptr->ref_count > 0) &&
128                     (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
129                     (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
130                         fd_ptr->ref_count++;
131                         DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %x, ref_count = %d\n",
132                                  fd_ptr->dev, fd_ptr->inode, 
133                                  fd_ptr->ref_count));
134                         return fd_ptr;
135                 }
136         }
137
138         return NULL;
139 }
140
141
142
143 /****************************************************************************
144 fd support routines - attempt to find a empty slot in the FileFd array.
145 Increments the ref_count of the returned entry.
146 ****************************************************************************/
147 file_fd_struct *fd_get_new(void)
148 {
149         extern struct current_user current_user;
150         int i;
151         file_fd_struct *fd_ptr;
152
153         i = bitmap_find(fd_bmap, 1);
154         if (i == -1) {
155                 DEBUG(0,("ERROR! Out of file_fd structures\n"));
156                 return NULL;
157         }
158
159         fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
160         if (!fd_ptr) return NULL;
161         
162         memset(fd_ptr, 0, sizeof(*fd_ptr));
163         
164         fd_ptr->fdnum = i;
165         fd_ptr->dev = (uint32)-1;
166         fd_ptr->inode = (uint32)-1;
167         fd_ptr->fd = -1;
168         fd_ptr->fd_readonly = -1;
169         fd_ptr->fd_writeonly = -1;
170         fd_ptr->real_open_flags = -1;
171         fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
172         fd_ptr->ref_count++;
173
174         bitmap_set(fd_bmap, i);
175         fd_ptr_used++;
176
177         DLIST_ADD(FileFd, fd_ptr);
178
179         DEBUG(5,("allocated fd_ptr structure %d (%d used)\n",
180                  i, fd_ptr_used));
181
182         return fd_ptr;
183 }
184
185
186 /****************************************************************************
187 close all open files for a connection
188 ****************************************************************************/
189 void file_close_conn(connection_struct *conn)
190 {
191         files_struct *fsp, *next;
192         
193         for (fsp=Files;fsp;fsp=next) {
194                 next = fsp->next;
195                 if (fsp->conn == conn && fsp->open) {
196                         if (fsp->is_directory)
197                                 close_directory(fsp); 
198                         else                  
199                                 close_file(fsp,False); 
200                 }
201         }
202 }
203
204 /****************************************************************************
205 initialise file structures
206 ****************************************************************************/
207 void file_init(void)
208 {
209         file_bmap = bitmap_allocate(MAX_FNUMS);
210         fd_bmap = bitmap_allocate(MAX_FNUMS);
211
212         if (!file_bmap || !fd_bmap) {
213                 exit_server("out of memory in file_init");
214         }
215
216 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
217         {
218                 struct rlimit rlp;
219                 getrlimit(RLIMIT_NOFILE, &rlp);
220                 /* Set the fd limit to be MAX_FNUMS + 10 to
221                  * account for the extra fd we need 
222                  * as well as the log files and standard
223                  * handles etc.  */
224                 rlp.rlim_cur = (MAX_FNUMS+10>rlp.rlim_max)? 
225                         rlp.rlim_max:MAX_FNUMS+10;
226                 setrlimit(RLIMIT_NOFILE, &rlp);
227                 getrlimit(RLIMIT_NOFILE, &rlp);
228                 DEBUG(3,("Maximum number of open files per session is %d\n",
229                          (int)rlp.rlim_cur));
230         }
231 #endif
232 }
233
234
235 /****************************************************************************
236 close files open by a specified vuid
237 ****************************************************************************/
238 void file_close_user(int vuid)
239 {
240         files_struct *fsp, *next;
241
242         for (fsp=Files;fsp;fsp=next) {
243                 next=fsp->next;
244                 if ((fsp->vuid == vuid) && fsp->open) {
245                         if(!fsp->is_directory)
246                                 close_file(fsp,False);
247                         else
248                                 close_directory(fsp);
249                 }
250         }
251 }
252
253
254 /****************************************************************************
255 find a fsp given a device, inode and timevalue
256 ****************************************************************************/
257 files_struct *file_find_dit(int dev, int inode, struct timeval *tval)
258 {
259         int count=0;
260         files_struct *fsp;
261
262         for (fsp=Files;fsp;fsp=fsp->next,count++) {
263                 if (fsp->open && 
264                     fsp->fd_ptr->dev == dev && 
265                     fsp->fd_ptr->inode == inode &&
266                     fsp->open_time.tv_sec == tval->tv_sec &&
267                     fsp->open_time.tv_usec == tval->tv_usec) {
268                         if (count > 10) {
269                                 DLIST_PROMOTE(Files, fsp);
270                         }
271                         return fsp;
272                 }
273         }
274
275         return NULL;
276 }
277
278
279 /****************************************************************************
280 find a fsp that is open for printing
281 ****************************************************************************/
282 files_struct *file_find_print(void)
283 {
284         files_struct *fsp;
285
286         for (fsp=Files;fsp;fsp=fsp->next) {
287                 if (fsp->open && fsp->print_file) return fsp;
288         } 
289         return NULL;
290 }
291
292
293 /****************************************************************************
294 sync open files on a connection
295 ****************************************************************************/
296 void file_sync_all(connection_struct *conn)
297 {
298         files_struct *fsp, *next;
299
300         for (fsp=Files;fsp;fsp=next) {
301                 next=fsp->next;
302                 if (fsp->open && conn == fsp->conn) {
303                         sync_file(conn,fsp);
304                 }
305         }
306 }
307
308
309 /****************************************************************************
310 free up a fd_ptr
311 ****************************************************************************/
312 static void fd_ptr_free(file_fd_struct *fd_ptr)
313 {
314         DLIST_REMOVE(FileFd, fd_ptr);
315
316         bitmap_clear(fd_bmap, fd_ptr->fdnum);
317         fd_ptr_used--;
318
319         DEBUG(5,("freed fd_ptr structure %d (%d used)\n",
320                  fd_ptr->fdnum, fd_ptr_used));
321
322         /* paranoia */
323         memset(fd_ptr, 0, sizeof(*fd_ptr));
324
325         free(fd_ptr);
326 }
327
328
329 /****************************************************************************
330 free up a fsp
331 ****************************************************************************/
332 void file_free(files_struct *fsp)
333 {
334         DLIST_REMOVE(Files, fsp);
335
336         string_free(&fsp->fsp_name);
337
338         if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
339                 fd_ptr_free(fsp->fd_ptr);
340         }
341
342         bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
343         files_used--;
344
345         DEBUG(5,("freed files structure %d (%d used)\n",
346                  fsp->fnum, files_used));
347
348         /* this is paranoia, just in case someone tries to reuse the 
349            information */
350         memset(fsp, 0, sizeof(*fsp));
351
352         if (fsp == chain_fsp) chain_fsp = NULL;
353
354         free(fsp);
355 }
356
357
358 /****************************************************************************
359 get a fsp from a packet given the offset of a 16 bit fnum
360 ****************************************************************************/
361 files_struct *file_fsp(char *buf, int where)
362 {
363         int fnum, count=0;
364         files_struct *fsp;
365
366         if (chain_fsp) return chain_fsp;
367
368         fnum = SVAL(buf, where);
369
370         for (fsp=Files;fsp;fsp=fsp->next, count++) {
371                 if (fsp->fnum == fnum) {
372                         chain_fsp = fsp;
373                         if (count > 10) {
374                                 DLIST_PROMOTE(Files, fsp);
375                         }
376                         return fsp;
377                 }
378         }
379
380         return NULL;
381 }
382
383
384 /****************************************************************************
385 reset the chained fsp - done at the start of a packet reply
386 ****************************************************************************/
387 void file_chain_reset(void)
388 {
389         chain_fsp = NULL;
390 }
391
392 /****************************************************************************
393 Save the chained fsp - done when about to do an oplock break.
394 ****************************************************************************/
395
396 void file_chain_save(void)
397 {
398         oplock_save_chain_fsp = chain_fsp;
399 }
400
401 /****************************************************************************
402 Restore the chained fsp - done after an oplock break.
403 ****************************************************************************/
404 void file_chain_restore(void)
405 {
406         chain_fsp = oplock_save_chain_fsp;
407 }