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 #define MAX_OPEN_FILES 100
28 #define MAX_FNUMS (MAX_OPEN_FILES+MAX_OPEN_DIRECTORIES)
29 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < MAX_FNUMS))
31 static files_struct Files[MAX_FNUMS];
34 * Indirection for file fd's. Needed as POSIX locking
35 * is based on file/process, not fd/process.
37 static file_fd_struct FileFd[MAX_OPEN_FILES];
38 static int max_file_fd_used = 0;
41 /****************************************************************************
42 find first available file slot
43 ****************************************************************************/
44 files_struct *file_new(void )
47 static int first_file;
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;
59 if (first_file >= MAX_FNUMS)
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]));
66 Files[i].reserved = True;
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]));
76 Files[i].reserved = True;
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.
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]));
94 Files[i].reserved = True;
100 for (i=1;i<MAX_FNUMS;i++) {
101 if(attempt_close_oplocked_file( &Files[i])) {
102 memset(&Files[i], 0, sizeof(Files[i]));
104 Files[i].reserved = True;
110 DEBUG(1,("ERROR! Out of file structures - perhaps increase MAX_OPEN_FILES?\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)
123 file_fd_struct *fd_ptr;
128 for(i = 0; i <= max_file_fd_used; i++) {
130 if((fd_ptr->ref_count > 0) &&
131 (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
132 (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
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));
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 for(i = 0; i < MAX_OPEN_FILES; i++) {
155 if(fd_ptr->ref_count == 0) {
156 fd_ptr->dev = (uint32)-1;
157 fd_ptr->inode = (uint32)-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);
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));
174 DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\n"));
179 /****************************************************************************
180 close all open files for a connection
181 ****************************************************************************/
182 void file_close_conn(connection_struct *conn)
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]);
190 close_file(&Files[i],False);
194 /****************************************************************************
195 initialise file structures
196 ****************************************************************************/
201 #ifdef HAVE_GETRLIMIT
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
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",
222 for (i=0;i<MAX_FNUMS;i++) {
223 Files[i].open = False;
224 string_init(&Files[i].fsp_name,"");
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;
233 fd_ptr->fd_readonly = -1;
234 fd_ptr->fd_writeonly = -1;
235 fd_ptr->real_open_flags = -1;
239 /****************************************************************************
240 find a fsp given a fnum
241 ****************************************************************************/
242 files_struct *file_fsp(int fnum)
244 if (!VALID_FNUM(fnum)) return NULL;
249 /****************************************************************************
250 close files open by a specified vuid
251 ****************************************************************************/
252 void file_close_user(int vuid)
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);
261 close_directory(fsp);
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)
273 for (i=0;i<MAX_FNUMS;i++) {
274 files_struct *fsp = &Files[i];
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) {
286 /****************************************************************************
287 find a fsp that is open for printing
288 ****************************************************************************/
289 files_struct *file_find_print(void)
293 for (i=0;i<MAX_FNUMS;i++) {
294 files_struct *fsp = &Files[i];
295 if (fsp->open && fsp->print_file) {
303 /****************************************************************************
304 sync open files on a connection
305 ****************************************************************************/
306 void file_sync_all(connection_struct *conn)
309 for (i=0;i<MAX_FNUMS;i++) {
310 files_struct *fsp = &Files[i];
311 if (fsp->open && conn == fsp->conn) {
318 void file_free(files_struct *fsp)
320 memset(fsp, 0, sizeof(*fsp));