2 Unix SMB/CIFS implementation.
3 Files[] structure handling
4 Copyright (C) Andrew Tridgell 1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "smbd/globals.h"
23 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
25 #define FILE_HANDLE_OFFSET 0x1000
27 /****************************************************************************
28 Return a unique number identifying this fsp over the life of this pid.
29 ****************************************************************************/
31 static unsigned long get_gen_count(void)
33 if ((++file_gen_counter) == 0)
34 return ++file_gen_counter;
35 return file_gen_counter;
38 /****************************************************************************
39 Find first available file slot.
40 ****************************************************************************/
42 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
43 files_struct **result)
48 /* we want to give out file handles differently on each new
49 connection because of a common bug in MS clients where they try to
50 reuse a file descriptor from an earlier smb connection. This code
51 increases the chance that the errant client will get an error rather
52 than causing corruption */
53 if (first_file == 0) {
54 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
57 /* TODO: Port the id-tree implementation from Samba4 */
59 i = bitmap_find(file_bmap, first_file);
61 DEBUG(0,("ERROR! Out of file structures\n"));
62 /* TODO: We have to unconditionally return a DOS error here,
63 * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
64 * NTSTATUS negotiated */
65 return NT_STATUS_TOO_MANY_OPENED_FILES;
68 fsp = SMB_MALLOC_P(files_struct);
70 return NT_STATUS_NO_MEMORY;
75 fsp->fh = SMB_MALLOC_P(struct fd_handle);
78 return NT_STATUS_NO_MEMORY;
81 ZERO_STRUCTP(fsp->fh);
83 fsp->fh->ref_count = 1;
87 fsp->fh->gen_id = get_gen_count();
88 GetTimeOfDay(&fsp->open_time);
90 first_file = (i+1) % real_max_open_files;
92 bitmap_set(file_bmap, i);
95 fsp->fnum = i + FILE_HANDLE_OFFSET;
96 SMB_ASSERT(fsp->fnum < 65536);
98 string_set(&fsp->fsp_name,"");
100 DLIST_ADD(Files, fsp);
102 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
103 i, fsp->fnum, files_used));
106 req->chain_fsp = fsp;
109 /* A new fsp invalidates the positive and
110 negative fsp_fi_cache as the new fsp is pushed
111 at the start of the list and we search from
112 a cache hit to the *end* of the list. */
114 ZERO_STRUCT(fsp_fi_cache);
116 conn->num_files_open++;
122 /****************************************************************************
123 Close all open files for a connection.
124 ****************************************************************************/
126 void file_close_conn(connection_struct *conn)
128 files_struct *fsp, *next;
130 for (fsp=Files;fsp;fsp=next) {
132 if (fsp->conn == conn) {
133 close_file(NULL, fsp, SHUTDOWN_CLOSE);
138 /****************************************************************************
139 Close all open files for a pid and a vuid.
140 ****************************************************************************/
142 void file_close_pid(uint16 smbpid, int vuid)
144 files_struct *fsp, *next;
146 for (fsp=Files;fsp;fsp=next) {
148 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
149 close_file(NULL, fsp, SHUTDOWN_CLOSE);
154 /****************************************************************************
155 Initialise file structures.
156 ****************************************************************************/
158 #define MAX_OPEN_FUDGEFACTOR 20
162 int request_max_open_files = lp_max_open_files();
166 * Set the max_open files to be the requested
167 * max plus a fudgefactor to allow for the extra
168 * fd's we need such as log files etc...
170 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
172 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
174 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
175 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
177 if(real_max_open_files != request_max_open_files) {
178 DEBUG(1,("file_init: Information only: requested %d \
179 open files, %d are available.\n", request_max_open_files, real_max_open_files));
182 SMB_ASSERT(real_max_open_files > 100);
184 file_bmap = bitmap_allocate(real_max_open_files);
187 exit_server("out of memory in file_init");
191 /****************************************************************************
192 Close files open by a specified vuid.
193 ****************************************************************************/
195 void file_close_user(int vuid)
197 files_struct *fsp, *next;
199 for (fsp=Files;fsp;fsp=next) {
201 if (fsp->vuid == vuid) {
202 close_file(NULL, fsp, SHUTDOWN_CLOSE);
207 /****************************************************************************
208 Debug to enumerate all open files in the smbd.
209 ****************************************************************************/
211 void file_dump_open_table(void)
216 for (fsp=Files;fsp;fsp=fsp->next,count++) {
217 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, gen = %lu, fileid=%s\n",
218 count, fsp->fnum, fsp->fsp_name, fsp->fh->fd, (unsigned long)fsp->fh->gen_id,
219 file_id_string_tos(&fsp->file_id)));
223 /****************************************************************************
224 Find a fsp given a file descriptor.
225 ****************************************************************************/
227 files_struct *file_find_fd(int fd)
232 for (fsp=Files;fsp;fsp=fsp->next,count++) {
233 if (fsp->fh->fd == fd) {
235 DLIST_PROMOTE(Files, fsp);
244 /****************************************************************************
245 Find a fsp given a device, inode and file_id.
246 ****************************************************************************/
248 files_struct *file_find_dif(struct file_id id, unsigned long gen_id)
253 for (fsp=Files;fsp;fsp=fsp->next,count++) {
254 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
255 if (file_id_equal(&fsp->file_id, &id) &&
256 fsp->fh->gen_id == gen_id ) {
258 DLIST_PROMOTE(Files, fsp);
260 /* Paranoia check. */
261 if ((fsp->fh->fd == -1) &&
262 (fsp->oplock_type != NO_OPLOCK) &&
263 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
264 DEBUG(0,("file_find_dif: file %s file_id = %s, gen = %u \
265 oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name,
266 file_id_string_tos(&fsp->file_id),
267 (unsigned int)fsp->fh->gen_id,
268 (unsigned int)fsp->oplock_type ));
269 smb_panic("file_find_dif");
278 /****************************************************************************
279 Check if an fsp still exists.
280 ****************************************************************************/
282 files_struct *file_find_fsp(files_struct *orig_fsp)
286 for (fsp=Files;fsp;fsp=fsp->next) {
294 /****************************************************************************
295 Find the first fsp given a device and inode.
296 We use a singleton cache here to speed up searching from getfilepathinfo
298 ****************************************************************************/
300 files_struct *file_find_di_first(struct file_id id)
304 if (file_id_equal(&fsp_fi_cache.id, &id)) {
305 /* Positive or negative cache hit. */
306 return fsp_fi_cache.fsp;
309 fsp_fi_cache.id = id;
311 for (fsp=Files;fsp;fsp=fsp->next) {
312 if (file_id_equal(&fsp->file_id, &id)) {
313 /* Setup positive cache. */
314 fsp_fi_cache.fsp = fsp;
319 /* Setup negative cache. */
320 fsp_fi_cache.fsp = NULL;
324 /****************************************************************************
325 Find the next fsp having the same device and inode.
326 ****************************************************************************/
328 files_struct *file_find_di_next(files_struct *start_fsp)
332 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
333 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
341 /****************************************************************************
342 Find a fsp that is open for printing.
343 ****************************************************************************/
345 files_struct *file_find_print(void)
349 for (fsp=Files;fsp;fsp=fsp->next) {
350 if (fsp->print_file) {
358 /****************************************************************************
359 Sync open files on a connection.
360 ****************************************************************************/
362 void file_sync_all(connection_struct *conn)
364 files_struct *fsp, *next;
366 for (fsp=Files;fsp;fsp=next) {
368 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
369 sync_file(conn, fsp, True /* write through */);
374 /****************************************************************************
376 ****************************************************************************/
378 void file_free(struct smb_request *req, files_struct *fsp)
380 DLIST_REMOVE(Files, fsp);
382 string_free(&fsp->fsp_name);
384 TALLOC_FREE(fsp->fake_file_handle);
386 if (fsp->fh->ref_count == 1) {
389 fsp->fh->ref_count--;
393 notify_remove(fsp->conn->notify_ctx, fsp);
394 TALLOC_FREE(fsp->notify);
397 /* Ensure this event will never fire. */
398 TALLOC_FREE(fsp->oplock_timeout);
400 /* Ensure this event will never fire. */
401 TALLOC_FREE(fsp->update_write_time_event);
403 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
406 DEBUG(5,("freed files structure %d (%d used)\n",
407 fsp->fnum, files_used));
409 fsp->conn->num_files_open--;
411 if ((req != NULL) && (fsp == req->chain_fsp)) {
412 req->chain_fsp = NULL;
415 /* Closing a file can invalidate the positive cache. */
416 if (fsp == fsp_fi_cache.fsp) {
417 ZERO_STRUCT(fsp_fi_cache);
420 /* Drop all remaining extensions. */
421 while (fsp->vfs_extension) {
422 vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp);
425 /* this is paranoia, just in case someone tries to reuse the
432 /****************************************************************************
433 Get an fsp from a 16 bit fnum.
434 ****************************************************************************/
436 files_struct *file_fnum(uint16 fnum)
441 for (fsp=Files;fsp;fsp=fsp->next, count++) {
442 if (fsp->fnum == fnum) {
444 DLIST_PROMOTE(Files, fsp);
452 /****************************************************************************
453 Get an fsp from a packet given the offset of a 16 bit fnum.
454 ****************************************************************************/
456 files_struct *file_fsp(struct smb_request *req, uint16 fid)
460 if ((req != NULL) && (req->chain_fsp != NULL)) {
461 return req->chain_fsp;
464 fsp = file_fnum(fid);
465 if ((fsp != NULL) && (req != NULL)) {
466 req->chain_fsp = fsp;
471 /****************************************************************************
472 Duplicate the file handle part for a DOS or FCB open.
473 ****************************************************************************/
475 void dup_file_fsp(struct smb_request *req, files_struct *from,
476 uint32 access_mask, uint32 share_access,
477 uint32 create_options, files_struct *to)
484 to->file_id = from->file_id;
485 to->initial_allocation_size = from->initial_allocation_size;
486 to->mode = from->mode;
487 to->file_pid = from->file_pid;
488 to->vuid = from->vuid;
489 to->open_time = from->open_time;
490 to->access_mask = access_mask;
491 to->share_access = share_access;
492 to->oplock_type = from->oplock_type;
493 to->can_lock = from->can_lock;
494 to->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
495 if (!CAN_WRITE(from->conn)) {
496 to->can_write = False;
498 to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
500 to->print_file = from->print_file;
501 to->modified = from->modified;
502 to->is_directory = from->is_directory;
503 to->aio_write_behind = from->aio_write_behind;
504 string_set(&to->fsp_name,from->fsp_name);