add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / smbd / files.c
blob3935a12442b397cec6800067a7b84527289d7c29
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #include "includes.h"
24 static int real_max_open_files;
26 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
28 #define FILE_HANDLE_OFFSET 0x1000
30 static struct bitmap *file_bmap;
32 static files_struct *Files;
34 /* a fsp to use when chaining */
35 static files_struct *chain_fsp = NULL;
36 /* a fsp to use to save when breaking an oplock. */
37 static files_struct *oplock_save_chain_fsp = NULL;
39 static int files_used;
41 /****************************************************************************
42 Return a unique number identifying this fsp over the life of this pid.
43 ****************************************************************************/
45 static unsigned long get_gen_count(void)
47 static unsigned long file_gen_counter;
49 if ((++file_gen_counter) == 0)
50 return ++file_gen_counter;
51 return file_gen_counter;
54 /****************************************************************************
55 Find first available file slot.
56 ****************************************************************************/
58 files_struct *file_new(connection_struct *conn)
60 int i;
61 static int first_file;
62 files_struct *fsp, *next;
64 /* we want to give out file handles differently on each new
65 connection because of a common bug in MS clients where they try to
66 reuse a file descriptor from an earlier smb connection. This code
67 increases the chance that the errant client will get an error rather
68 than causing corruption */
69 if (first_file == 0) {
70 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
73 i = bitmap_find(file_bmap, first_file);
74 if (i == -1) {
75 /*
76 * Before we give up, go through the open files
77 * and see if there are any files opened with a
78 * batch oplock. If so break the oplock and then
79 * re-use that entry (if it becomes closed).
80 * This may help as NT/95 clients tend to keep
81 * files batch oplocked for quite a long time
82 * after they have finished with them.
84 for (fsp=Files;fsp;fsp=next) {
85 next=fsp->next;
86 if (attempt_close_oplocked_file(fsp)) {
87 return file_new(conn);
91 DEBUG(0,("ERROR! Out of file structures\n"));
92 unix_ERR_class = ERRSRV;
93 unix_ERR_code = ERRnofids;
94 return NULL;
97 fsp = (files_struct *)malloc(sizeof(*fsp));
98 if (!fsp) {
99 unix_ERR_class = ERRSRV;
100 unix_ERR_code = ERRnofids;
101 return NULL;
104 ZERO_STRUCTP(fsp);
105 fsp->fd = -1;
106 fsp->conn = conn;
107 fsp->file_id = get_gen_count();
108 GetTimeOfDay(&fsp->open_time);
110 first_file = (i+1) % real_max_open_files;
112 bitmap_set(file_bmap, i);
113 files_used++;
115 fsp->fnum = i + FILE_HANDLE_OFFSET;
116 string_set(&fsp->fsp_name,"");
118 DLIST_ADD(Files, fsp);
120 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
121 i, fsp->fnum, files_used));
123 chain_fsp = fsp;
125 return fsp;
128 /****************************************************************************
129 Close all open files for a connection.
130 ****************************************************************************/
132 void file_close_conn(connection_struct *conn)
134 files_struct *fsp, *next;
136 for (fsp=Files;fsp;fsp=next) {
137 next = fsp->next;
138 if (fsp->conn == conn) {
139 close_file(fsp,False);
144 /****************************************************************************
145 Initialise file structures.
146 ****************************************************************************/
148 #define MAX_OPEN_FUDGEFACTOR 10
150 void file_init(void)
152 int request_max_open_files = lp_max_open_files();
153 int real_lim;
156 * Set the max_open files to be the requested
157 * max plus a fudgefactor to allow for the extra
158 * fd's we need such as log files etc...
160 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
162 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
164 if(real_max_open_files != request_max_open_files) {
165 DEBUG(1,("file_init: Information only: requested %d \
166 open files, %d are available.\n", request_max_open_files, real_max_open_files));
169 file_bmap = bitmap_allocate(real_max_open_files);
171 if (!file_bmap) {
172 exit_server("out of memory in file_init");
176 * Ensure that pipe_handle_oppset is set correctly.
178 set_pipe_handle_offset(real_max_open_files);
181 /****************************************************************************
182 Close files open by a specified vuid.
183 ****************************************************************************/
185 void file_close_user(int vuid)
187 files_struct *fsp, *next;
189 for (fsp=Files;fsp;fsp=next) {
190 next=fsp->next;
191 if (fsp->vuid == vuid) {
192 close_file(fsp,False);
197 /****************************************************************************
198 Find a fsp given a file descriptor.
199 ****************************************************************************/
201 files_struct *file_find_fd(int fd)
203 int count=0;
204 files_struct *fsp;
206 for (fsp=Files;fsp;fsp=fsp->next,count++) {
207 if (fsp->fd == fd) {
208 if (count > 10) {
209 DLIST_PROMOTE(Files, fsp);
211 return fsp;
215 return NULL;
218 /****************************************************************************
219 Find a fsp given a device, inode and file_id.
220 ****************************************************************************/
222 files_struct *file_find_dif(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
224 int count=0;
225 files_struct *fsp;
227 for (fsp=Files;fsp;fsp=fsp->next,count++) {
228 if (fsp->fd != -1 &&
229 fsp->dev == dev &&
230 fsp->inode == inode &&
231 fsp->file_id == file_id ) {
232 if (count > 10) {
233 DLIST_PROMOTE(Files, fsp);
235 return fsp;
239 return NULL;
242 /****************************************************************************
243 Check if an fsp still exists.
244 ****************************************************************************/
246 files_struct *file_find_fsp(files_struct *orig_fsp)
248 files_struct *fsp;
250 for (fsp=Files;fsp;fsp=fsp->next) {
251 if (fsp == orig_fsp)
252 return fsp;
255 return NULL;
258 /****************************************************************************
259 Find the first fsp given a device and inode.
260 ****************************************************************************/
262 files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
264 files_struct *fsp;
266 for (fsp=Files;fsp;fsp=fsp->next) {
267 if ( fsp->fd != -1 &&
268 fsp->dev == dev &&
269 fsp->inode == inode )
270 return fsp;
273 return NULL;
276 /****************************************************************************
277 Find the next fsp having the same device and inode.
278 ****************************************************************************/
280 files_struct *file_find_di_next(files_struct *start_fsp)
282 files_struct *fsp;
284 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
285 if ( fsp->fd != -1 &&
286 fsp->dev == start_fsp->dev &&
287 fsp->inode == start_fsp->inode )
288 return fsp;
291 return NULL;
294 /****************************************************************************
295 Find a fsp that is open for printing.
296 ****************************************************************************/
298 files_struct *file_find_print(void)
300 files_struct *fsp;
302 for (fsp=Files;fsp;fsp=fsp->next) {
303 if (fsp->print_file) return fsp;
306 return NULL;
309 /****************************************************************************
310 Sync open files on a connection.
311 ****************************************************************************/
313 void file_sync_all(connection_struct *conn)
315 files_struct *fsp, *next;
317 for (fsp=Files;fsp;fsp=next) {
318 next=fsp->next;
319 if ((conn == fsp->conn) && (fsp->fd != -1)) {
320 sync_file(conn,fsp);
325 /****************************************************************************
326 Free up a fsp.
327 ****************************************************************************/
329 void file_free(files_struct *fsp)
331 DLIST_REMOVE(Files, fsp);
333 string_free(&fsp->fsp_name);
335 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
336 files_used--;
338 DEBUG(5,("freed files structure %d (%d used)\n",
339 fsp->fnum, files_used));
341 /* this is paranoia, just in case someone tries to reuse the
342 information */
343 ZERO_STRUCTP(fsp);
345 if (fsp == chain_fsp) chain_fsp = NULL;
347 SAFE_FREE(fsp);
350 /****************************************************************************
351 Get a fsp from a packet given the offset of a 16 bit fnum.
352 ****************************************************************************/
354 files_struct *file_fsp(char *buf, int where)
356 int fnum, count=0;
357 files_struct *fsp;
359 if (chain_fsp)
360 return chain_fsp;
362 fnum = SVAL(buf, where);
364 for (fsp=Files;fsp;fsp=fsp->next, count++) {
365 if (fsp->fnum == fnum) {
366 chain_fsp = fsp;
367 if (count > 10) {
368 DLIST_PROMOTE(Files, fsp);
370 return fsp;
373 return NULL;
376 /****************************************************************************
377 Reset the chained fsp - done at the start of a packet reply.
378 ****************************************************************************/
380 void file_chain_reset(void)
382 chain_fsp = NULL;
385 /****************************************************************************
386 Save the chained fsp - done when about to do an oplock break.
387 ****************************************************************************/
389 void file_chain_save(void)
391 oplock_save_chain_fsp = chain_fsp;
394 /****************************************************************************
395 Restore the chained fsp - done after an oplock break.
396 ****************************************************************************/
398 void file_chain_restore(void)
400 chain_fsp = oplock_save_chain_fsp;