04aff67a155b1d60547f5be74222689b748c4c03
[kai/samba.git] / source3 / 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 #define MAX_OPEN_FILES 100
27
28 #define MAX_FNUMS (MAX_OPEN_FILES+MAX_OPEN_DIRECTORIES)
29 #define VALID_FNUM(fnum)   (((fnum) >= 0) && ((fnum) < MAX_FNUMS))
30
31 static files_struct Files[MAX_FNUMS];
32
33 /*
34  * Indirection for file fd's. Needed as POSIX locking
35  * is based on file/process, not fd/process.
36  */
37 static file_fd_struct FileFd[MAX_OPEN_FILES];
38 static int max_file_fd_used = 0;
39
40
41 /****************************************************************************
42   find first available file slot
43 ****************************************************************************/
44 files_struct *file_new(void )
45 {
46         int i;
47         static int first_file;
48
49         /* we want to give out file handles differently on each new
50            connection because of a common bug in MS clients where they try to
51            reuse a file descriptor from an earlier smb connection. This code
52            increases the chance that the errant client will get an error rather
53            than causing corruption */
54         if (first_file == 0) {
55                 first_file = (getpid() ^ (int)time(NULL)) % MAX_FNUMS;
56                 if (first_file == 0) first_file = 1;
57         }
58
59         if (first_file >= MAX_FNUMS)
60                 first_file = 1;
61
62         for (i=first_file;i<MAX_FNUMS;i++)
63                 if (!Files[i].open && !Files[i].reserved) {
64                         memset(&Files[i], 0, sizeof(Files[i]));
65                         first_file = i+1;
66                         Files[i].reserved = True;
67                         Files[i].fnum = i;
68                         return &Files[i];
69                 }
70
71         /* returning a file handle of 0 is a bad idea - so we start at 1 */
72         for (i=1;i<first_file;i++)
73                 if (!Files[i].open && !Files[i].reserved) {
74                         memset(&Files[i], 0, sizeof(Files[i]));
75                         first_file = i+1;
76                         Files[i].reserved = True;
77                         Files[i].fnum = i;
78                         return &Files[i];
79                 }
80
81         /* 
82          * Before we give up, go through the open files 
83          * and see if there are any files opened with a
84          * batch oplock. If so break the oplock and then
85          * re-use that entry (if it becomes closed).
86          * This may help as NT/95 clients tend to keep
87          * files batch oplocked for quite a long time
88          * after they have finished with them.
89          */
90         for (i=first_file;i<MAX_FNUMS;i++) {
91           if(attempt_close_oplocked_file( &Files[i])) {
92             memset(&Files[i], 0, sizeof(Files[i]));
93             first_file = i+1;
94             Files[i].reserved = True;
95             Files[i].fnum = i;
96             return &Files[i];
97           }
98         }
99
100         for (i=1;i<MAX_FNUMS;i++) {
101           if(attempt_close_oplocked_file( &Files[i])) {
102             memset(&Files[i], 0, sizeof(Files[i]));
103             first_file = i+1;
104             Files[i].reserved = True;
105             Files[i].fnum = i;
106             return &Files[i];
107           }
108         }
109
110         DEBUG(1,("ERROR! Out of file structures - perhaps increase MAX_OPEN_FILES?\n"));
111         return NULL;
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   int i;
123   file_fd_struct *fd_ptr;
124
125   if(sbuf == 0)
126     return 0;
127
128   for(i = 0; i <= max_file_fd_used; i++) {
129     fd_ptr = &FileFd[i];
130     if((fd_ptr->ref_count > 0) &&
131        (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
132        (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
133       fd_ptr->ref_count++;
134       DEBUG(3,
135        ("Re-used file_fd_struct %d, dev = %x, inode = %x, ref_count = %d\n",
136         i, fd_ptr->dev, fd_ptr->inode, fd_ptr->ref_count));
137       return fd_ptr;
138     }
139   }
140   return 0;
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   for(i = 0; i < MAX_OPEN_FILES; i++) {
154     fd_ptr = &FileFd[i];
155     if(fd_ptr->ref_count == 0) {
156       fd_ptr->dev = (uint32)-1;
157       fd_ptr->inode = (uint32)-1;
158       fd_ptr->fd = -1;
159       fd_ptr->fd_readonly = -1;
160       fd_ptr->fd_writeonly = -1;
161       fd_ptr->real_open_flags = -1;
162       fd_ptr->uid_cache_count = 0;
163       fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
164       fd_ptr->ref_count++;
165       /* Increment max used counter if neccessary, cuts down
166          on search time when re-using */
167       if(i > max_file_fd_used)
168         max_file_fd_used = i;
169       DEBUG(3,("Allocated new file_fd_struct %d, dev = %x, inode = %x\n",
170                i, fd_ptr->dev, fd_ptr->inode));
171       return fd_ptr;
172     }
173   }
174   DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\n"));
175   return 0;
176 }
177
178
179 /****************************************************************************
180 close all open files for a connection
181 ****************************************************************************/
182 void file_close_conn(connection_struct *conn)
183 {
184   int i;
185   for (i=0;i<MAX_FNUMS;i++)
186     if (Files[i].conn == conn && Files[i].open) {
187       if(Files[i].is_directory)
188         close_directory(&Files[i]); 
189       else                  
190         close_file(&Files[i],False); 
191     }
192 }
193
194 /****************************************************************************
195 initialise file structures
196 ****************************************************************************/
197 void file_init(void)
198 {
199         int i;
200
201 #ifdef HAVE_GETRLIMIT
202 #ifdef RLIMIT_NOFILE
203         {
204                 struct rlimit rlp;
205                 getrlimit(RLIMIT_NOFILE, &rlp);
206                 /* Set the fd limit to be MAX_OPEN_FILES + 10 to
207                  * account for the extra fd we need to read
208                  * directories, as well as the log files and standard
209                  * handles etc.  */
210                 rlp.rlim_cur = (MAX_FNUMS+10>rlp.rlim_max)? 
211                         rlp.rlim_max:MAX_FNUMS+10;
212                 setrlimit(RLIMIT_NOFILE, &rlp);
213                 getrlimit(RLIMIT_NOFILE, &rlp);
214                 DEBUG(3,("Maximum number of open files per session is %d\n",
215                          (int)rlp.rlim_cur));
216         }
217 #endif
218 #endif
219
220   
221
222         for (i=0;i<MAX_FNUMS;i++) {
223                 Files[i].open = False;
224                 string_init(&Files[i].fsp_name,"");
225         }
226
227         for (i=0;i<MAX_OPEN_FILES;i++) {
228                 file_fd_struct *fd_ptr = &FileFd[i];
229                 fd_ptr->ref_count = 0;
230                 fd_ptr->dev = (int32)-1;
231                 fd_ptr->inode = (int32)-1;
232                 fd_ptr->fd = -1;
233                 fd_ptr->fd_readonly = -1;
234                 fd_ptr->fd_writeonly = -1;
235                 fd_ptr->real_open_flags = -1;
236         }
237 }
238
239 /****************************************************************************
240 find a fsp given a fnum
241 ****************************************************************************/
242 files_struct *file_fsp(int fnum)
243 {
244         if (!VALID_FNUM(fnum)) return NULL;
245         return &Files[fnum];
246 }
247
248
249 /****************************************************************************
250 close files open by a specified vuid
251 ****************************************************************************/
252 void file_close_user(int vuid)
253 {
254         int i;
255         for (i=0;i<MAX_FNUMS;i++) {
256                 files_struct *fsp = &Files[i];
257                 if ((fsp->vuid == vuid) && fsp->open) {
258                         if(!fsp->is_directory)
259                                 close_file(fsp,False);
260                         else
261                                 close_directory(fsp);
262                 }
263         }
264 }
265
266
267 /****************************************************************************
268 find a fsp given a device, inode and timevalue
269 ****************************************************************************/
270 files_struct *file_find_dit(int dev, int inode, struct timeval *tval)
271 {
272         int i;
273         for (i=0;i<MAX_FNUMS;i++) {
274                 files_struct *fsp = &Files[i];
275                 if (fsp->open && 
276                     fsp->fd_ptr->dev == dev && 
277                     fsp->fd_ptr->inode == inode &&
278                     fsp->open_time.tv_sec == tval->tv_sec &&
279                     fsp->open_time.tv_usec == tval->tv_usec) {
280                         return fsp;
281                 }
282         } 
283         return NULL;
284 }
285
286 /****************************************************************************
287 find a fsp that is open for printing
288 ****************************************************************************/
289 files_struct *file_find_print(void)
290 {
291         int i;
292
293         for (i=0;i<MAX_FNUMS;i++) {
294                 files_struct *fsp = &Files[i];
295                 if (fsp->open && fsp->print_file) {
296                         return fsp;
297                 }
298         } 
299         return NULL;
300 }
301
302
303 /****************************************************************************
304 sync open files on a connection
305 ****************************************************************************/
306 void file_sync_all(connection_struct *conn)
307 {
308         int i;
309         for (i=0;i<MAX_FNUMS;i++) {
310                 files_struct *fsp = &Files[i];
311                 if (fsp->open && conn == fsp->conn) {
312                         sync_file(conn,fsp);
313                 }
314         }
315 }
316
317
318 void file_free(files_struct *fsp)
319 {
320         memset(fsp, 0, sizeof(*fsp));
321 }