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 static int real_max_open_files;
28 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
30 #define FILE_HANDLE_OFFSET 0x1000
32 static struct bitmap *file_bmap;
34 static files_struct *Files;
36 /* a fsp to use when chaining */
37 static files_struct *chain_fsp = NULL;
38 /* a fsp to use to save when breaking an oplock. */
39 static files_struct *oplock_save_chain_fsp = NULL;
42 * Indirection for file fd's. Needed as POSIX locking
43 * is based on file/process, not fd/process.
45 static file_fd_struct *FileFd;
47 static int files_used, fd_ptr_used;
49 /****************************************************************************
50 find first available file slot
51 ****************************************************************************/
52 files_struct *file_new(void )
55 static int first_file;
56 files_struct *fsp, *next;
58 /* we want to give out file handles differently on each new
59 connection because of a common bug in MS clients where they try to
60 reuse a file descriptor from an earlier smb connection. This code
61 increases the chance that the errant client will get an error rather
62 than causing corruption */
63 if (first_file == 0) {
64 first_file = (getpid() ^ (int)time(NULL)) % real_max_open_files;
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=next) {
80 if (attempt_close_oplocked_file(fsp)) {
85 DEBUG(0,("ERROR! Out of file structures\n"));
89 fsp = (files_struct *)malloc(sizeof(*fsp));
90 if (!fsp) return NULL;
94 first_file = (i+1) % real_max_open_files;
96 bitmap_set(file_bmap, i);
99 fsp->fnum = i + FILE_HANDLE_OFFSET;
100 string_init(&fsp->fsp_name,"");
102 DLIST_ADD(Files, fsp);
104 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
105 i, fsp->fnum, files_used));
114 /****************************************************************************
115 fd support routines - attempt to find an already open file by dev
116 and inode - increments the ref_count of the returned file_fd_struct *.
117 ****************************************************************************/
118 file_fd_struct *fd_get_already_open(SMB_STRUCT_STAT *sbuf)
120 file_fd_struct *fd_ptr;
122 if(!sbuf) return NULL;
124 for (fd_ptr=FileFd;fd_ptr;fd_ptr=fd_ptr->next) {
125 if ((fd_ptr->ref_count > 0) &&
126 (sbuf->st_dev == fd_ptr->dev) &&
127 (sbuf->st_ino == fd_ptr->inode)) {
130 DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %.0f, ref_count = %d\n",
131 (unsigned int)fd_ptr->dev, (double)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;
150 file_fd_struct *fd_ptr;
152 fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
154 DEBUG(0,("ERROR! malloc fail for file_fd struct.\n"));
158 ZERO_STRUCTP(fd_ptr);
160 fd_ptr->dev = (SMB_DEV_T)-1;
161 fd_ptr->inode = (SMB_INO_T)-1;
163 fd_ptr->fd_readonly = -1;
164 fd_ptr->fd_writeonly = -1;
165 fd_ptr->real_open_flags = -1;
166 fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
171 DLIST_ADD(FileFd, fd_ptr);
173 DEBUG(5,("allocated fd_ptr structure (%d used)\n", fd_ptr_used));
179 /****************************************************************************
180 close all open files for a connection
181 ****************************************************************************/
182 void file_close_conn(connection_struct *conn)
184 files_struct *fsp, *next;
186 for (fsp=Files;fsp;fsp=next) {
188 if (fsp->conn == conn && fsp->open) {
189 if (fsp->is_directory)
190 close_directory(fsp);
192 close_file(fsp,False);
197 /****************************************************************************
198 initialise file structures
199 ****************************************************************************/
201 #define MAX_OPEN_FUDGEFACTOR 10
205 int real_max_open_files, lim;
207 lim = set_maxfiles();
208 lim = MIN(lim, lp_max_open_files());
210 real_max_open_files = lim - MAX_OPEN_FUDGEFACTOR;
212 file_bmap = bitmap_allocate(real_max_open_files);
215 exit_server("out of memory in file_init");
219 * Ensure that pipe_handle_oppset is set correctly.
221 set_pipe_handle_offset(real_max_open_files);
225 /****************************************************************************
226 close files open by a specified vuid
227 ****************************************************************************/
228 void file_close_user(int vuid)
230 files_struct *fsp, *next;
232 for (fsp=Files;fsp;fsp=next) {
234 if ((fsp->vuid == vuid) && fsp->open) {
235 if(!fsp->is_directory)
236 close_file(fsp,False);
238 close_directory(fsp);
244 /****************************************************************************
245 Find a fsp given a device, inode and timevalue
246 If this is from a kernel oplock break request then tval may be NULL.
247 ****************************************************************************/
249 files_struct *file_find_dit(SMB_DEV_T dev, SMB_INO_T inode, struct timeval *tval)
254 for (fsp=Files;fsp;fsp=fsp->next,count++) {
256 fsp->fd_ptr->dev == dev &&
257 fsp->fd_ptr->inode == inode &&
258 (tval ? (fsp->open_time.tv_sec == tval->tv_sec) : True ) &&
259 (tval ? (fsp->open_time.tv_usec == tval->tv_usec) : True )) {
261 DLIST_PROMOTE(Files, fsp);
271 /****************************************************************************
272 find a fsp that is open for printing
273 ****************************************************************************/
274 files_struct *file_find_print(void)
278 for (fsp=Files;fsp;fsp=fsp->next) {
279 if (fsp->open && fsp->print_file) return fsp;
286 /****************************************************************************
287 sync open files on a connection
288 ****************************************************************************/
289 void file_sync_all(connection_struct *conn)
291 files_struct *fsp, *next;
293 for (fsp=Files;fsp;fsp=next) {
295 if (fsp->open && conn == fsp->conn) {
302 /****************************************************************************
304 ****************************************************************************/
305 void fd_ptr_free(file_fd_struct *fd_ptr)
307 DLIST_REMOVE(FileFd, fd_ptr);
311 DEBUG(5,("freed fd_ptr structure (%d used)\n", fd_ptr_used));
314 ZERO_STRUCTP(fd_ptr);
320 /****************************************************************************
322 ****************************************************************************/
323 void file_free(files_struct *fsp)
325 DLIST_REMOVE(Files, fsp);
327 string_free(&fsp->fsp_name);
329 if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
330 fd_ptr_free(fsp->fd_ptr);
333 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
336 DEBUG(5,("freed files structure %d (%d used)\n",
337 fsp->fnum, files_used));
339 /* this is paranoia, just in case someone tries to reuse the
343 if (fsp == chain_fsp) chain_fsp = NULL;
349 /****************************************************************************
350 get a fsp from a packet given the offset of a 16 bit fnum
351 ****************************************************************************/
352 files_struct *file_fsp(char *buf, int where)
357 if (chain_fsp) return chain_fsp;
359 fnum = SVAL(buf, where);
361 for (fsp=Files;fsp;fsp=fsp->next, count++) {
362 if (fsp->fnum == fnum) {
365 DLIST_PROMOTE(Files, fsp);
373 /****************************************************************************
374 Reset the chained fsp - done at the start of a packet reply
375 ****************************************************************************/
377 void file_chain_reset(void)
382 /****************************************************************************
383 Save the chained fsp - done when about to do an oplock break.
384 ****************************************************************************/
386 void file_chain_save(void)
388 oplock_save_chain_fsp = chain_fsp;
391 /****************************************************************************
392 Restore the chained fsp - done after an oplock break.
393 ****************************************************************************/
394 void file_chain_restore(void)
396 chain_fsp = oplock_save_chain_fsp;