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 static struct bitmap *file_bmap;
32 static struct bitmap *fd_bmap;
34 static files_struct *Files;
37 * Indirection for file fd's. Needed as POSIX locking
38 * is based on file/process, not fd/process.
40 static file_fd_struct *FileFd;
42 static int files_used, fd_ptr_used;
44 /****************************************************************************
45 find first available file slot
46 ****************************************************************************/
47 files_struct *file_new(void )
50 static int first_file;
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;
63 if (first_file >= MAX_FNUMS) {
67 i = bitmap_find(file_bmap, first_file);
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.
78 for (fsp=Files;fsp;fsp=fsp->next) {
79 if (attempt_close_oplocked_file(fsp)) {
84 DEBUG(0,("ERROR! Out of file structures\n"));
88 fsp = (files_struct *)malloc(sizeof(*fsp));
89 if (!fsp) return NULL;
91 memset(fsp, 0, sizeof(*fsp));
94 string_init(&fsp->fsp_name,"");
96 bitmap_set(file_bmap, i);
99 /* hook into the front of the list */
108 DEBUG(5,("allocated file structure %d (%d used)\n",
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)
122 file_fd_struct *fd_ptr;
124 if(!sbuf) return NULL;
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)) {
131 DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %x, ref_count = %d\n",
132 fd_ptr->dev, fd_ptr->inode,
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)
149 extern struct current_user current_user;
151 file_fd_struct *fd_ptr;
153 i = bitmap_find(fd_bmap, 1);
155 DEBUG(0,("ERROR! Out of file_fd structures\n"));
159 fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
160 if (!fd_ptr) return NULL;
162 memset(fd_ptr, 0, sizeof(*fd_ptr));
165 fd_ptr->dev = (uint32)-1;
166 fd_ptr->inode = (uint32)-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);
174 bitmap_set(fd_bmap, i);
177 /* hook into the front of the list */
181 FileFd->prev = fd_ptr;
182 fd_ptr->next = FileFd;
186 DEBUG(5,("allocated fd_ptr structure %d (%d used)\n",
193 /****************************************************************************
194 close all open files for a connection
195 ****************************************************************************/
196 void file_close_conn(connection_struct *conn)
200 for (fsp=Files;fsp;fsp=fsp->next) {
201 if (fsp->conn == conn && fsp->open) {
202 if (fsp->is_directory)
203 close_directory(fsp);
205 close_file(fsp,False);
210 /****************************************************************************
211 initialise file structures
212 ****************************************************************************/
215 file_bmap = bitmap_allocate(MAX_FNUMS);
216 fd_bmap = bitmap_allocate(MAX_FNUMS);
218 if (!file_bmap || !fd_bmap) {
219 exit_server("out of memory in file_init");
222 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
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
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",
241 /****************************************************************************
242 find a fsp given a fnum
243 ****************************************************************************/
244 files_struct *file_fsp(int fnum)
248 if (!VALID_FNUM(fnum)) return NULL;
250 for (fsp=Files;fsp;fsp=fsp->next) {
251 if (fsp->fnum == fnum) return fsp;
258 /****************************************************************************
259 close files open by a specified vuid
260 ****************************************************************************/
261 void file_close_user(int vuid)
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);
270 close_directory(fsp);
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)
283 for (fsp=Files;fsp;fsp=fsp->next) {
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) {
297 /****************************************************************************
298 find a fsp that is open for printing
299 ****************************************************************************/
300 files_struct *file_find_print(void)
304 for (fsp=Files;fsp;fsp=fsp->next) {
305 if (fsp->open && fsp->print_file) return fsp;
311 /****************************************************************************
312 sync open files on a connection
313 ****************************************************************************/
314 void file_sync_all(connection_struct *conn)
318 for (fsp=Files;fsp;fsp=fsp->next) {
319 if (fsp->open && conn == fsp->conn) {
326 /****************************************************************************
328 ****************************************************************************/
329 static void fd_ptr_free(file_fd_struct *fd_ptr)
331 if (fd_ptr == FileFd) {
332 FileFd = fd_ptr->next;
333 if (FileFd) FileFd->prev = NULL;
335 fd_ptr->prev->next = fd_ptr->next;
336 if (fd_ptr->next) fd_ptr->next->prev = fd_ptr->prev;
339 bitmap_clear(fd_bmap, fd_ptr->fdnum);
342 DEBUG(5,("freed fd_ptr structure %d (%d used)\n",
343 fd_ptr->fdnum, fd_ptr_used));
346 memset(fd_ptr, 0, sizeof(*fd_ptr));
352 /****************************************************************************
354 ****************************************************************************/
355 void file_free(files_struct *fsp)
359 if (Files) Files->prev = NULL;
361 fsp->prev->next = fsp->next;
362 if (fsp->next) fsp->next->prev = fsp->prev;
365 string_free(&fsp->fsp_name);
367 if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
368 fd_ptr_free(fsp->fd_ptr);
371 bitmap_clear(file_bmap, fsp->fnum);
374 DEBUG(5,("freed files structure %d (%d used)\n",
375 fsp->fnum, files_used));
377 /* this is paranoia, just in case someone tries to reuse the
379 memset(fsp, 0, sizeof(*fsp));