got rid of the Files[] array completely (previously I'd just made it
[ira/wip.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 static struct bitmap *file_bmap;
32 static struct bitmap *fd_bmap;
33
34 static files_struct *Files;
35
36 /*
37  * Indirection for file fd's. Needed as POSIX locking
38  * is based on file/process, not fd/process.
39  */
40 static file_fd_struct *FileFd;
41
42 static int files_used, fd_ptr_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;
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 = (getpid() ^ (int)time(NULL)) % MAX_FNUMS;
60                 if (first_file == 0) first_file = 1;
61         }
62
63         if (first_file >= MAX_FNUMS) {
64                 first_file = 1;
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=fsp->next) {
79                         if (attempt_close_oplocked_file(fsp)) {
80                                 return file_new();
81                         }
82                 }
83
84                 DEBUG(0,("ERROR! Out of file structures\n"));
85                 return NULL;
86         }
87
88         fsp = (files_struct *)malloc(sizeof(*fsp));
89         if (!fsp) return NULL;
90
91         memset(fsp, 0, sizeof(*fsp));
92         first_file = i+1;
93         fsp->fnum = i;
94         string_init(&fsp->fsp_name,"");
95
96         bitmap_set(file_bmap, i);
97         files_used++;
98         
99         /* hook into the front of the list */
100         if (!Files) {
101                 Files = fsp;
102         } else {
103                 Files->prev = fsp;
104                 fsp->next = Files;
105                 Files = fsp;
106         }
107
108         DEBUG(5,("allocated file structure %d (%d used)\n",
109                  i, files_used));
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         /* hook into the front of the list */
178         if (!FileFd) {
179                 FileFd = fd_ptr;
180         } else {
181                 FileFd->prev = fd_ptr;
182                 fd_ptr->next = FileFd;
183                 FileFd = fd_ptr;
184         }
185
186         DEBUG(5,("allocated fd_ptr structure %d (%d used)\n",
187                  i, fd_ptr_used));
188
189         return fd_ptr;
190 }
191
192
193 /****************************************************************************
194 close all open files for a connection
195 ****************************************************************************/
196 void file_close_conn(connection_struct *conn)
197 {
198         files_struct *fsp;
199         
200         for (fsp=Files;fsp;fsp=fsp->next) {
201                 if (fsp->conn == conn && fsp->open) {
202                         if (fsp->is_directory)
203                                 close_directory(fsp); 
204                         else                  
205                                 close_file(fsp,False); 
206                 }
207         }
208 }
209
210 /****************************************************************************
211 initialise file structures
212 ****************************************************************************/
213 void file_init(void)
214 {
215         file_bmap = bitmap_allocate(MAX_FNUMS);
216         fd_bmap = bitmap_allocate(MAX_FNUMS);
217
218         if (!file_bmap || !fd_bmap) {
219                 exit_server("out of memory in file_init");
220         }
221
222 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
223         {
224                 struct rlimit rlp;
225                 getrlimit(RLIMIT_NOFILE, &rlp);
226                 /* Set the fd limit to be MAX_OPEN_FILES + 10 to
227                  * account for the extra fd we need to read
228                  * directories, as well as the log files and standard
229                  * handles etc.  */
230                 rlp.rlim_cur = (MAX_FNUMS+10>rlp.rlim_max)? 
231                         rlp.rlim_max:MAX_FNUMS+10;
232                 setrlimit(RLIMIT_NOFILE, &rlp);
233                 getrlimit(RLIMIT_NOFILE, &rlp);
234                 DEBUG(3,("Maximum number of open files per session is %d\n",
235                          (int)rlp.rlim_cur));
236         }
237 #endif
238 }
239
240
241 /****************************************************************************
242 find a fsp given a fnum
243 ****************************************************************************/
244 files_struct *file_fsp(int fnum)
245 {
246         files_struct *fsp;
247
248         if (!VALID_FNUM(fnum)) return NULL;
249         
250         for (fsp=Files;fsp;fsp=fsp->next) {
251                 if (fsp->fnum == fnum) return fsp;
252         }
253
254         return NULL;
255 }
256
257
258 /****************************************************************************
259 close files open by a specified vuid
260 ****************************************************************************/
261 void file_close_user(int vuid)
262 {
263         files_struct *fsp;
264
265         for (fsp=Files;fsp;fsp=fsp->next) {
266                 if ((fsp->vuid == vuid) && fsp->open) {
267                         if(!fsp->is_directory)
268                                 close_file(fsp,False);
269                         else
270                                 close_directory(fsp);
271                 }
272         }
273 }
274
275
276 /****************************************************************************
277 find a fsp given a device, inode and timevalue
278 ****************************************************************************/
279 files_struct *file_find_dit(int dev, int inode, struct timeval *tval)
280 {
281         files_struct *fsp;
282
283         for (fsp=Files;fsp;fsp=fsp->next) {
284                 if (fsp->open && 
285                     fsp->fd_ptr->dev == dev && 
286                     fsp->fd_ptr->inode == inode &&
287                     fsp->open_time.tv_sec == tval->tv_sec &&
288                     fsp->open_time.tv_usec == tval->tv_usec) {
289                         return fsp;
290                 }
291         }
292
293         return NULL;
294 }
295
296
297 /****************************************************************************
298 find a fsp that is open for printing
299 ****************************************************************************/
300 files_struct *file_find_print(void)
301 {
302         files_struct *fsp;
303
304         for (fsp=Files;fsp;fsp=fsp->next) {
305                 if (fsp->open && fsp->print_file) return fsp;
306         } 
307         return NULL;
308 }
309
310
311 /****************************************************************************
312 sync open files on a connection
313 ****************************************************************************/
314 void file_sync_all(connection_struct *conn)
315 {
316         files_struct *fsp;
317
318         for (fsp=Files;fsp;fsp=fsp->next) {
319                 if (fsp->open && conn == fsp->conn) {
320                         sync_file(conn,fsp);
321                 }
322         }
323 }
324
325
326 /****************************************************************************
327 free up a fd_ptr
328 ****************************************************************************/
329 static void fd_ptr_free(file_fd_struct *fd_ptr)
330 {
331         if (fd_ptr == FileFd) {
332                 FileFd = fd_ptr->next;
333                 if (FileFd) FileFd->prev = NULL;
334         } else {
335                 fd_ptr->prev->next = fd_ptr->next;
336                 if (fd_ptr->next) fd_ptr->next->prev = fd_ptr->prev;
337         }
338
339         bitmap_clear(fd_bmap, fd_ptr->fdnum);
340         fd_ptr_used--;
341
342         DEBUG(5,("freed fd_ptr structure %d (%d used)\n",
343                  fd_ptr->fdnum, fd_ptr_used));
344
345         /* paranoia */
346         memset(fd_ptr, 0, sizeof(*fd_ptr));
347
348         free(fd_ptr);
349 }
350
351
352 /****************************************************************************
353 free up a fsp
354 ****************************************************************************/
355 void file_free(files_struct *fsp)
356 {
357         if (fsp == Files) {
358                 Files = fsp->next;
359                 if (Files) Files->prev = NULL;
360         } else {
361                 fsp->prev->next = fsp->next;
362                 if (fsp->next) fsp->next->prev = fsp->prev;
363         }
364
365         string_free(&fsp->fsp_name);
366
367         if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
368                 fd_ptr_free(fsp->fd_ptr);
369         }
370
371         bitmap_clear(file_bmap, fsp->fnum);
372         files_used--;
373
374         DEBUG(5,("freed files structure %d (%d used)\n",
375                  fsp->fnum, files_used));
376
377         /* this is paranoia, just in case someone tries to reuse the 
378            information */
379         memset(fsp, 0, sizeof(*fsp));
380
381         free(fsp);
382 }