2 Unix SMB/Netbios implementation.
4 Files[] structure handling
5 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
24 extern int DEBUGLEVEL;
26 /* the only restriction is that this must be less than PIPE_HANDLE_OFFSET */
27 #define MAX_FNUMS 4096
29 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < MAX_FNUMS))
31 #define FILE_HANDLE_OFFSET 0x1000
33 static struct bitmap *file_bmap;
34 static struct bitmap *fd_bmap;
36 static files_struct *Files;
39 * Indirection for file fd's. Needed as POSIX locking
40 * is based on file/process, not fd/process.
42 static file_fd_struct *FileFd;
44 static int files_used, fd_ptr_used;
46 /****************************************************************************
47 find first available file slot
48 ****************************************************************************/
49 files_struct *file_new(void )
52 static int first_file;
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;
64 i = bitmap_find(file_bmap, first_file);
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.
75 for (fsp=Files;fsp;fsp=fsp->next) {
76 if (attempt_close_oplocked_file(fsp)) {
81 DEBUG(0,("ERROR! Out of file structures\n"));
85 fsp = (files_struct *)malloc(sizeof(*fsp));
86 if (!fsp) return NULL;
88 memset(fsp, 0, sizeof(*fsp));
90 first_file = (i+1) % MAX_FNUMS;
92 bitmap_set(file_bmap, i);
95 fsp->fnum = i + FILE_HANDLE_OFFSET;
96 string_init(&fsp->fsp_name,"");
98 /* hook into the front of the list */
107 DEBUG(5,("allocated file structure %d (%d used)\n",
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)
121 file_fd_struct *fd_ptr;
123 if(!sbuf) return NULL;
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)) {
130 DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %x, ref_count = %d\n",
131 fd_ptr->dev, fd_ptr->inode,
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)
148 extern struct current_user current_user;
150 file_fd_struct *fd_ptr;
152 i = bitmap_find(fd_bmap, 1);
154 DEBUG(0,("ERROR! Out of file_fd structures\n"));
158 fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
159 if (!fd_ptr) return NULL;
161 memset(fd_ptr, 0, sizeof(*fd_ptr));
164 fd_ptr->dev = (uint32)-1;
165 fd_ptr->inode = (uint32)-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);
173 bitmap_set(fd_bmap, i);
176 /* hook into the front of the list */
180 FileFd->prev = fd_ptr;
181 fd_ptr->next = FileFd;
185 DEBUG(5,("allocated fd_ptr structure %d (%d used)\n",
192 /****************************************************************************
193 close all open files for a connection
194 ****************************************************************************/
195 void file_close_conn(connection_struct *conn)
199 for (fsp=Files;fsp;fsp=fsp->next) {
200 if (fsp->conn == conn && fsp->open) {
201 if (fsp->is_directory)
202 close_directory(fsp);
204 close_file(fsp,False);
209 /****************************************************************************
210 initialise file structures
211 ****************************************************************************/
214 file_bmap = bitmap_allocate(MAX_FNUMS);
215 fd_bmap = bitmap_allocate(MAX_FNUMS);
217 if (!file_bmap || !fd_bmap) {
218 exit_server("out of memory in file_init");
221 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
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
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",
240 /****************************************************************************
241 find a fsp given a fnum
242 ****************************************************************************/
243 files_struct *file_fsp(int fnum)
247 for (fsp=Files;fsp;fsp=fsp->next) {
248 if (fsp->fnum == fnum) return fsp;
255 /****************************************************************************
256 close files open by a specified vuid
257 ****************************************************************************/
258 void file_close_user(int vuid)
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);
267 close_directory(fsp);
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)
280 for (fsp=Files;fsp;fsp=fsp->next) {
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) {
294 /****************************************************************************
295 find a fsp that is open for printing
296 ****************************************************************************/
297 files_struct *file_find_print(void)
301 for (fsp=Files;fsp;fsp=fsp->next) {
302 if (fsp->open && fsp->print_file) return fsp;
308 /****************************************************************************
309 sync open files on a connection
310 ****************************************************************************/
311 void file_sync_all(connection_struct *conn)
315 for (fsp=Files;fsp;fsp=fsp->next) {
316 if (fsp->open && conn == fsp->conn) {
323 /****************************************************************************
325 ****************************************************************************/
326 static void fd_ptr_free(file_fd_struct *fd_ptr)
328 if (fd_ptr == FileFd) {
329 FileFd = fd_ptr->next;
330 if (FileFd) FileFd->prev = NULL;
332 fd_ptr->prev->next = fd_ptr->next;
333 if (fd_ptr->next) fd_ptr->next->prev = fd_ptr->prev;
336 bitmap_clear(fd_bmap, fd_ptr->fdnum);
339 DEBUG(5,("freed fd_ptr structure %d (%d used)\n",
340 fd_ptr->fdnum, fd_ptr_used));
343 memset(fd_ptr, 0, sizeof(*fd_ptr));
349 /****************************************************************************
351 ****************************************************************************/
352 void file_free(files_struct *fsp)
356 if (Files) Files->prev = NULL;
358 fsp->prev->next = fsp->next;
359 if (fsp->next) fsp->next->prev = fsp->prev;
362 string_free(&fsp->fsp_name);
364 if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
365 fd_ptr_free(fsp->fd_ptr);
368 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
371 DEBUG(5,("freed files structure %d (%d used)\n",
372 fsp->fnum, files_used));
374 /* this is paranoia, just in case someone tries to reuse the
376 memset(fsp, 0, sizeof(*fsp));