Changed arguments to fsync() function to break dependency on
[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 static int real_max_open_files;
27
28 #define VALID_FNUM(fnum)   (((fnum) >= 0) && ((fnum) < real_max_open_files))
29
30 #define FILE_HANDLE_OFFSET 0x1000
31
32 static struct bitmap *file_bmap;
33
34 static files_struct *Files;
35  
36 /* a fsp to use when chaining */
37 static files_struct *chain_fsp = NULL;
38 /* a fsp to use to save when breaking an oplock. */
39 static files_struct *oplock_save_chain_fsp = NULL;
40
41 /*
42  * Indirection for file fd's. Needed as POSIX locking
43  * is based on file/process, not fd/process.
44  */
45 static file_fd_struct *FileFd;
46
47 static int files_used, fd_ptr_used;
48
49 /****************************************************************************
50   find first available file slot
51 ****************************************************************************/
52 files_struct *file_new(void )
53 {
54         int i;
55         static int first_file;
56         files_struct *fsp, *next;
57
58         /* we want to give out file handles differently on each new
59            connection because of a common bug in MS clients where they try to
60            reuse a file descriptor from an earlier smb connection. This code
61            increases the chance that the errant client will get an error rather
62            than causing corruption */
63         if (first_file == 0) {
64                 first_file = (getpid() ^ (int)time(NULL)) % real_max_open_files;
65         }
66
67         i = bitmap_find(file_bmap, first_file);
68         if (i == -1) {
69                 /* 
70                  * Before we give up, go through the open files 
71                  * and see if there are any files opened with a
72                  * batch oplock. If so break the oplock and then
73                  * re-use that entry (if it becomes closed).
74                  * This may help as NT/95 clients tend to keep
75                  * files batch oplocked for quite a long time
76                  * after they have finished with them.
77                  */
78                 for (fsp=Files;fsp;fsp=next) {
79                         next=fsp->next;
80                         if (attempt_close_oplocked_file(fsp)) {
81                                 return file_new();
82                         }
83                 }
84
85                 DEBUG(0,("ERROR! Out of file structures\n"));
86                 return NULL;
87         }
88
89         fsp = (files_struct *)malloc(sizeof(*fsp));
90         if (!fsp) return NULL;
91
92         ZERO_STRUCTP(fsp);
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_init(&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 /****************************************************************************
115 fd support routines - attempt to find an already open file by dev
116 and inode - increments the ref_count of the returned file_fd_struct *.
117 ****************************************************************************/
118 file_fd_struct *fd_get_already_open(SMB_STRUCT_STAT *sbuf)
119 {
120         file_fd_struct *fd_ptr;
121
122         if(!sbuf) return NULL;
123
124         for (fd_ptr=FileFd;fd_ptr;fd_ptr=fd_ptr->next) {
125                 if ((fd_ptr->ref_count > 0) &&
126                     (sbuf->st_dev == fd_ptr->dev) &&
127                     (sbuf->st_ino == fd_ptr->inode)) {
128                         fd_ptr->ref_count++;
129
130                         DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %.0f, ref_count = %d\n",
131                                  (unsigned int)fd_ptr->dev, (double)fd_ptr->inode, 
132                                  fd_ptr->ref_count));
133
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         file_fd_struct *fd_ptr;
151
152         fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
153         if (!fd_ptr) {
154           DEBUG(0,("ERROR! malloc fail for file_fd struct.\n"));
155           return NULL;
156         }
157         
158         ZERO_STRUCTP(fd_ptr);
159         
160         fd_ptr->dev = (SMB_DEV_T)-1;
161         fd_ptr->inode = (SMB_INO_T)-1;
162         fd_ptr->fd = -1;
163         fd_ptr->fd_readonly = -1;
164         fd_ptr->fd_writeonly = -1;
165         fd_ptr->real_open_flags = -1;
166         fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
167         fd_ptr->ref_count++;
168
169         fd_ptr_used++;
170
171         DLIST_ADD(FileFd, fd_ptr);
172
173         DEBUG(5,("allocated fd_ptr structure (%d used)\n", fd_ptr_used));
174
175         return fd_ptr;
176 }
177
178
179 /****************************************************************************
180 close all open files for a connection
181 ****************************************************************************/
182 void file_close_conn(connection_struct *conn)
183 {
184         files_struct *fsp, *next;
185         
186         for (fsp=Files;fsp;fsp=next) {
187                 next = fsp->next;
188                 if (fsp->conn == conn && fsp->open) {
189                         if (fsp->is_directory)
190                                 close_directory(fsp); 
191                         else                  
192                                 close_file(fsp,False); 
193                 }
194         }
195 }
196
197 /****************************************************************************
198 initialise file structures
199 ****************************************************************************/
200
201 #define MAX_OPEN_FUDGEFACTOR 10
202
203 void file_init(void)
204 {
205         int request_max_open_files = lp_max_open_files();
206         int real_lim;
207
208         /*
209          * Set the max_open files to be the requested
210          * max plus a fudgefactor to allow for the extra
211          * fd's we need such as log files etc...
212          */
213         real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
214
215         real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
216
217         if(real_max_open_files != request_max_open_files) {
218                 DEBUG(1,("file_init: Information only: requested %d \
219 open files, %d are available.\n", request_max_open_files, real_max_open_files));
220         }
221
222         file_bmap = bitmap_allocate(real_max_open_files);
223         
224         if (!file_bmap) {
225                 exit_server("out of memory in file_init");
226         }
227         
228         /*
229          * Ensure that pipe_handle_oppset is set correctly.
230          */
231         set_pipe_handle_offset(real_max_open_files);
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  If this is from a kernel oplock break request then tval may be NULL.
257 ****************************************************************************/
258
259 files_struct *file_find_dit(SMB_DEV_T dev, SMB_INO_T inode, struct timeval *tval)
260 {
261         int count=0;
262         files_struct *fsp;
263
264         for (fsp=Files;fsp;fsp=fsp->next,count++) {
265                 if (fsp->open && 
266                         fsp->fd_ptr != NULL &&
267                     fsp->fd_ptr->dev == dev && 
268                     fsp->fd_ptr->inode == inode &&
269                     (tval ? (fsp->open_time.tv_sec == tval->tv_sec) : True ) &&
270                     (tval ? (fsp->open_time.tv_usec == tval->tv_usec) : True )) {
271                         if (count > 10) {
272                                 DLIST_PROMOTE(Files, fsp);
273                         }
274                         return fsp;
275                 }
276         }
277
278         return NULL;
279 }
280
281 /****************************************************************************
282  Find the first fsp given a device and inode.
283 ****************************************************************************/
284
285 files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
286 {
287     files_struct *fsp;
288
289     for (fsp=Files;fsp;fsp=fsp->next) {
290         if (fsp->open &&
291                         fsp->fd_ptr != NULL &&
292             fsp->fd_ptr->dev == dev &&
293             fsp->fd_ptr->inode == inode )
294             return fsp;
295     }
296
297     return NULL;
298 }
299
300 /****************************************************************************
301  Find the next fsp having the same device and inode.
302 ****************************************************************************/
303
304 files_struct *file_find_di_next(files_struct *start_fsp)
305 {
306     files_struct *fsp;
307
308     for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
309         if (fsp->open &&
310                         fsp->fd_ptr != NULL &&
311             fsp->fd_ptr->dev == start_fsp->fd_ptr->dev &&
312             fsp->fd_ptr->inode == start_fsp->fd_ptr->inode )
313             return fsp;
314     }
315
316     return NULL;
317 }
318
319 /****************************************************************************
320 find a fsp that is open for printing
321 ****************************************************************************/
322 files_struct *file_find_print(void)
323 {
324         files_struct *fsp;
325
326         for (fsp=Files;fsp;fsp=fsp->next) {
327                 if (fsp->open && fsp->print_file) return fsp;
328         } 
329
330         return NULL;
331 }
332
333
334 /****************************************************************************
335 sync open files on a connection
336 ****************************************************************************/
337 void file_sync_all(connection_struct *conn)
338 {
339         files_struct *fsp, *next;
340
341         for (fsp=Files;fsp;fsp=next) {
342                 next=fsp->next;
343                 if (fsp->open && (conn == fsp->conn) && (fsp->fd_ptr != NULL)
344                     && lp_strict_sync(SNUM(conn))){
345                     conn->vfs_ops.sync(fsp->fd_ptr->fd);
346                 }
347         }
348 }
349
350 /****************************************************************************
351 free up a fd_ptr
352 ****************************************************************************/
353 void fd_ptr_free(file_fd_struct *fd_ptr)
354 {
355         DLIST_REMOVE(FileFd, fd_ptr);
356
357         fd_ptr_used--;
358
359         DEBUG(5,("freed fd_ptr structure (%d used)\n", fd_ptr_used));
360
361         /* paranoia */
362         ZERO_STRUCTP(fd_ptr);
363
364         free(fd_ptr);
365 }
366
367
368 /****************************************************************************
369 free up a fsp
370 ****************************************************************************/
371 void file_free(files_struct *fsp)
372 {
373         DLIST_REMOVE(Files, fsp);
374
375         string_free(&fsp->fsp_name);
376
377         if ((fsp->fd_ptr != NULL) && fsp->fd_ptr->ref_count == 0) {
378                 fd_ptr_free(fsp->fd_ptr);
379         }
380
381         bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
382         files_used--;
383
384         DEBUG(5,("freed files structure %d (%d used)\n",
385                  fsp->fnum, files_used));
386
387         /* this is paranoia, just in case someone tries to reuse the 
388            information */
389         ZERO_STRUCTP(fsp);
390
391         if (fsp == chain_fsp) chain_fsp = NULL;
392
393         free(fsp);
394 }
395
396
397 /****************************************************************************
398 get a fsp from a packet given the offset of a 16 bit fnum
399 ****************************************************************************/
400 files_struct *file_fsp(char *buf, int where)
401 {
402         int fnum, count=0;
403         files_struct *fsp;
404
405         if (chain_fsp) return chain_fsp;
406
407         fnum = SVAL(buf, where);
408
409         for (fsp=Files;fsp;fsp=fsp->next, count++) {
410                 if (fsp->fnum == fnum) {
411                         chain_fsp = fsp;
412                         if (count > 10) {
413                                 DLIST_PROMOTE(Files, fsp);
414                         }
415                         return fsp;
416                 }
417         }
418         return NULL;
419 }
420
421 /****************************************************************************
422  Reset the chained fsp - done at the start of a packet reply
423 ****************************************************************************/
424
425 void file_chain_reset(void)
426 {
427         chain_fsp = NULL;
428 }
429
430 /****************************************************************************
431 Save the chained fsp - done when about to do an oplock break.
432 ****************************************************************************/
433
434 void file_chain_save(void)
435 {
436         oplock_save_chain_fsp = chain_fsp;
437 }
438
439 /****************************************************************************
440 Restore the chained fsp - done after an oplock break.
441 ****************************************************************************/
442 void file_chain_restore(void)
443 {
444         chain_fsp = oplock_save_chain_fsp;
445 }