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