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;
38 /* a fsp to use when chaining */
39 static files_struct *chain_fsp = NULL;
40 /* a fsp to use to save when breaking an oplock. */
41 static files_struct *oplock_save_chain_fsp = NULL;
44 * Indirection for file fd's. Needed as POSIX locking
45 * is based on file/process, not fd/process.
47 static file_fd_struct *FileFd;
49 static int files_used, fd_ptr_used;
51 /****************************************************************************
52 find first available file slot
53 ****************************************************************************/
54 files_struct *file_new(void )
57 static int first_file;
58 files_struct *fsp, *next;
60 /* we want to give out file handles differently on each new
61 connection because of a common bug in MS clients where they try to
62 reuse a file descriptor from an earlier smb connection. This code
63 increases the chance that the errant client will get an error rather
64 than causing corruption */
65 if (first_file == 0) {
66 first_file = (getpid() ^ (int)time(NULL)) % MAX_FNUMS;
69 i = bitmap_find(file_bmap, first_file);
72 * Before we give up, go through the open files
73 * and see if there are any files opened with a
74 * batch oplock. If so break the oplock and then
75 * re-use that entry (if it becomes closed).
76 * This may help as NT/95 clients tend to keep
77 * files batch oplocked for quite a long time
78 * after they have finished with them.
80 for (fsp=Files;fsp;fsp=next) {
82 if (attempt_close_oplocked_file(fsp)) {
87 DEBUG(0,("ERROR! Out of file structures\n"));
91 fsp = (files_struct *)malloc(sizeof(*fsp));
92 if (!fsp) return NULL;
94 memset(fsp, 0, sizeof(*fsp));
96 first_file = (i+1) % MAX_FNUMS;
98 bitmap_set(file_bmap, i);
101 fsp->fnum = i + FILE_HANDLE_OFFSET;
102 string_init(&fsp->fsp_name,"");
104 DLIST_ADD(Files, fsp);
106 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(SMB_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 (sbuf->st_dev == fd_ptr->dev) &&
129 (sbuf->st_ino == fd_ptr->inode)) {
131 #ifdef LARGE_SMB_INO_T
132 DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %.0f, ref_count = %d\n",
133 (unsigned int)fd_ptr->dev, (double)fd_ptr->inode,
135 #else /* LARGE_SMB_INO_T */
136 DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %x, ref_count = %d\n",
137 (unsigned int)fd_ptr->dev,
138 (unsigned int)fd_ptr->inode,
140 #endif /* LARGE_SMB_INO_T */
150 /****************************************************************************
151 fd support routines - attempt to find a empty slot in the FileFd array.
152 Increments the ref_count of the returned entry.
153 ****************************************************************************/
154 file_fd_struct *fd_get_new(void)
156 extern struct current_user current_user;
158 file_fd_struct *fd_ptr;
160 i = bitmap_find(fd_bmap, 1);
162 DEBUG(0,("ERROR! Out of file_fd structures\n"));
166 fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
167 if (!fd_ptr) return NULL;
169 memset(fd_ptr, 0, sizeof(*fd_ptr));
172 fd_ptr->dev = (SMB_DEV_T)-1;
173 fd_ptr->inode = (SMB_INO_T)-1;
175 fd_ptr->fd_readonly = -1;
176 fd_ptr->fd_writeonly = -1;
177 fd_ptr->real_open_flags = -1;
178 fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
181 bitmap_set(fd_bmap, i);
184 DLIST_ADD(FileFd, fd_ptr);
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)
198 files_struct *fsp, *next;
200 for (fsp=Files;fsp;fsp=next) {
202 if (fsp->conn == conn && fsp->open) {
203 if (fsp->is_directory)
204 close_directory(fsp);
206 close_file(fsp,False);
211 /****************************************************************************
212 initialise file structures
213 ****************************************************************************/
216 file_bmap = bitmap_allocate(MAX_FNUMS);
217 fd_bmap = bitmap_allocate(MAX_FNUMS);
219 if (!file_bmap || !fd_bmap) {
220 exit_server("out of memory in file_init");
223 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
226 getrlimit(RLIMIT_NOFILE, &rlp);
227 /* Set the fd limit to be MAX_FNUMS + 10 to
228 * account for the extra fd we need
229 * as well as the log files and standard
231 rlp.rlim_cur = (MAX_FNUMS+10>rlp.rlim_max)?
232 rlp.rlim_max:MAX_FNUMS+10;
233 setrlimit(RLIMIT_NOFILE, &rlp);
234 getrlimit(RLIMIT_NOFILE, &rlp);
235 DEBUG(3,("Maximum number of open files per session is %d\n",
242 /****************************************************************************
243 close files open by a specified vuid
244 ****************************************************************************/
245 void file_close_user(int vuid)
247 files_struct *fsp, *next;
249 for (fsp=Files;fsp;fsp=next) {
251 if ((fsp->vuid == vuid) && fsp->open) {
252 if(!fsp->is_directory)
253 close_file(fsp,False);
255 close_directory(fsp);
261 /****************************************************************************
262 find a fsp given a device, inode and timevalue
263 ****************************************************************************/
264 files_struct *file_find_dit(SMB_DEV_T dev, SMB_INO_T inode, struct timeval *tval)
269 for (fsp=Files;fsp;fsp=fsp->next,count++) {
271 fsp->fd_ptr->dev == dev &&
272 fsp->fd_ptr->inode == inode &&
273 fsp->open_time.tv_sec == tval->tv_sec &&
274 fsp->open_time.tv_usec == tval->tv_usec) {
276 DLIST_PROMOTE(Files, fsp);
286 /****************************************************************************
287 find a fsp that is open for printing
288 ****************************************************************************/
289 files_struct *file_find_print(void)
293 for (fsp=Files;fsp;fsp=fsp->next) {
294 if (fsp->open && fsp->print_file) return fsp;
300 /****************************************************************************
301 sync open files on a connection
302 ****************************************************************************/
303 void file_sync_all(connection_struct *conn)
305 files_struct *fsp, *next;
307 for (fsp=Files;fsp;fsp=next) {
309 if (fsp->open && conn == fsp->conn) {
316 /****************************************************************************
318 ****************************************************************************/
319 void fd_ptr_free(file_fd_struct *fd_ptr)
321 DLIST_REMOVE(FileFd, fd_ptr);
323 bitmap_clear(fd_bmap, fd_ptr->fdnum);
326 DEBUG(5,("freed fd_ptr structure %d (%d used)\n",
327 fd_ptr->fdnum, fd_ptr_used));
330 memset(fd_ptr, 0, sizeof(*fd_ptr));
336 /****************************************************************************
338 ****************************************************************************/
339 void file_free(files_struct *fsp)
341 DLIST_REMOVE(Files, fsp);
343 string_free(&fsp->fsp_name);
345 if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
346 fd_ptr_free(fsp->fd_ptr);
349 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
352 DEBUG(5,("freed files structure %d (%d used)\n",
353 fsp->fnum, files_used));
355 /* this is paranoia, just in case someone tries to reuse the
357 memset(fsp, 0, sizeof(*fsp));
359 if (fsp == chain_fsp) chain_fsp = NULL;
365 /****************************************************************************
366 get a fsp from a packet given the offset of a 16 bit fnum
367 ****************************************************************************/
368 files_struct *file_fsp(char *buf, int where)
373 if (chain_fsp) return chain_fsp;
375 fnum = SVAL(buf, where);
377 for (fsp=Files;fsp;fsp=fsp->next, count++) {
378 if (fsp->fnum == fnum) {
381 DLIST_PROMOTE(Files, fsp);
390 /****************************************************************************
391 Reset the chained fsp - done at the start of a packet reply
392 ****************************************************************************/
394 void file_chain_reset(void)
399 /****************************************************************************
400 Save the chained fsp - done when about to do an oplock break.
401 ****************************************************************************/
403 void file_chain_save(void)
405 oplock_save_chain_fsp = chain_fsp;
408 /****************************************************************************
409 Restore the chained fsp - done after an oplock break.
410 ****************************************************************************/
411 void file_chain_restore(void)
413 chain_fsp = oplock_save_chain_fsp;