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/>.
22 static int real_max_open_files;
24 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
26 #define FILE_HANDLE_OFFSET 0x1000
28 static struct bitmap *file_bmap;
30 static files_struct *Files;
32 /* a fsp to use when chaining */
33 static files_struct *chain_fsp = NULL;
35 static int files_used;
37 /* A singleton cache to speed up searching by dev/inode. */
38 static struct fsp_singleton_cache {
43 /****************************************************************************
44 Return a unique number identifying this fsp over the life of this pid.
45 ****************************************************************************/
47 static unsigned long get_gen_count(void)
49 static unsigned long file_gen_counter;
51 if ((++file_gen_counter) == 0)
52 return ++file_gen_counter;
53 return file_gen_counter;
56 /****************************************************************************
57 Find first available file slot.
58 ****************************************************************************/
60 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
61 files_struct **result)
64 static int first_file;
67 /* we want to give out file handles differently on each new
68 connection because of a common bug in MS clients where they try to
69 reuse a file descriptor from an earlier smb connection. This code
70 increases the chance that the errant client will get an error rather
71 than causing corruption */
72 if (first_file == 0) {
73 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
76 /* TODO: Port the id-tree implementation from Samba4 */
78 i = bitmap_find(file_bmap, first_file);
80 DEBUG(0,("ERROR! Out of file structures\n"));
81 /* TODO: We have to unconditionally return a DOS error here,
82 * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
83 * NTSTATUS negotiated */
84 return NT_STATUS_TOO_MANY_OPENED_FILES;
87 fsp = SMB_MALLOC_P(files_struct);
89 return NT_STATUS_NO_MEMORY;
94 fsp->fh = SMB_MALLOC_P(struct fd_handle);
97 return NT_STATUS_NO_MEMORY;
100 ZERO_STRUCTP(fsp->fh);
102 fsp->fh->ref_count = 1;
106 fsp->fh->gen_id = get_gen_count();
107 GetTimeOfDay(&fsp->open_time);
109 first_file = (i+1) % real_max_open_files;
111 bitmap_set(file_bmap, i);
114 fsp->fnum = i + FILE_HANDLE_OFFSET;
115 SMB_ASSERT(fsp->fnum < 65536);
117 string_set(&fsp->fsp_name,"");
119 DLIST_ADD(Files, fsp);
121 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
122 i, fsp->fnum, files_used));
126 /* A new fsp invalidates the positive and
127 negative fsp_fi_cache as the new fsp is pushed
128 at the start of the list and we search from
129 a cache hit to the *end* of the list. */
131 ZERO_STRUCT(fsp_fi_cache);
137 /****************************************************************************
138 Close all open files for a connection.
139 ****************************************************************************/
141 void file_close_conn(connection_struct *conn)
143 files_struct *fsp, *next;
145 for (fsp=Files;fsp;fsp=next) {
147 if (fsp->conn == conn) {
148 close_file(NULL, fsp, SHUTDOWN_CLOSE);
153 /****************************************************************************
154 Close all open files for a pid and a vuid.
155 ****************************************************************************/
157 void file_close_pid(uint16 smbpid, int vuid)
159 files_struct *fsp, *next;
161 for (fsp=Files;fsp;fsp=next) {
163 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
164 close_file(NULL, fsp, SHUTDOWN_CLOSE);
169 /****************************************************************************
170 Initialise file structures.
171 ****************************************************************************/
173 #define MAX_OPEN_FUDGEFACTOR 20
177 int request_max_open_files = lp_max_open_files();
181 * Set the max_open files to be the requested
182 * max plus a fudgefactor to allow for the extra
183 * fd's we need such as log files etc...
185 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
187 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
189 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
190 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
192 if(real_max_open_files != request_max_open_files) {
193 DEBUG(1,("file_init: Information only: requested %d \
194 open files, %d are available.\n", request_max_open_files, real_max_open_files));
197 SMB_ASSERT(real_max_open_files > 100);
199 file_bmap = bitmap_allocate(real_max_open_files);
202 exit_server("out of memory in file_init");
206 * Ensure that pipe_handle_oppset is set correctly.
208 set_pipe_handle_offset(real_max_open_files);
211 /****************************************************************************
212 Close files open by a specified vuid.
213 ****************************************************************************/
215 void file_close_user(int vuid)
217 files_struct *fsp, *next;
219 for (fsp=Files;fsp;fsp=next) {
221 if (fsp->vuid == vuid) {
222 close_file(NULL, fsp, SHUTDOWN_CLOSE);
227 /****************************************************************************
228 Debug to enumerate all open files in the smbd.
229 ****************************************************************************/
231 void file_dump_open_table(void)
236 for (fsp=Files;fsp;fsp=fsp->next,count++) {
237 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, gen = %lu, fileid=%s\n",
238 count, fsp->fnum, fsp->fsp_name, fsp->fh->fd, (unsigned long)fsp->fh->gen_id,
239 file_id_string_tos(&fsp->file_id)));
243 /****************************************************************************
244 Find a fsp given a file descriptor.
245 ****************************************************************************/
247 files_struct *file_find_fd(int fd)
252 for (fsp=Files;fsp;fsp=fsp->next,count++) {
253 if (fsp->fh->fd == fd) {
255 DLIST_PROMOTE(Files, fsp);
264 /****************************************************************************
265 Find a fsp given a device, inode and file_id.
266 ****************************************************************************/
268 files_struct *file_find_dif(struct file_id id, unsigned long gen_id)
273 for (fsp=Files;fsp;fsp=fsp->next,count++) {
274 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
275 if (file_id_equal(&fsp->file_id, &id) &&
276 fsp->fh->gen_id == gen_id ) {
278 DLIST_PROMOTE(Files, fsp);
280 /* Paranoia check. */
281 if ((fsp->fh->fd == -1) &&
282 (fsp->oplock_type != NO_OPLOCK) &&
283 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
284 DEBUG(0,("file_find_dif: file %s file_id = %s, gen = %u \
285 oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name,
286 file_id_string_tos(&fsp->file_id),
287 (unsigned int)fsp->fh->gen_id,
288 (unsigned int)fsp->oplock_type ));
289 smb_panic("file_find_dif");
298 /****************************************************************************
299 Check if an fsp still exists.
300 ****************************************************************************/
302 files_struct *file_find_fsp(files_struct *orig_fsp)
306 for (fsp=Files;fsp;fsp=fsp->next) {
314 /****************************************************************************
315 Find the first fsp given a device and inode.
316 We use a singleton cache here to speed up searching from getfilepathinfo
318 ****************************************************************************/
320 files_struct *file_find_di_first(struct file_id id)
324 if (file_id_equal(&fsp_fi_cache.id, &id)) {
325 /* Positive or negative cache hit. */
326 return fsp_fi_cache.fsp;
329 fsp_fi_cache.id = id;
331 for (fsp=Files;fsp;fsp=fsp->next) {
332 if (file_id_equal(&fsp->file_id, &id)) {
333 /* Setup positive cache. */
334 fsp_fi_cache.fsp = fsp;
339 /* Setup negative cache. */
340 fsp_fi_cache.fsp = NULL;
344 /****************************************************************************
345 Find the next fsp having the same device and inode.
346 ****************************************************************************/
348 files_struct *file_find_di_next(files_struct *start_fsp)
352 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
353 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
361 /****************************************************************************
362 Find a fsp that is open for printing.
363 ****************************************************************************/
365 files_struct *file_find_print(void)
369 for (fsp=Files;fsp;fsp=fsp->next) {
370 if (fsp->print_file) {
378 /****************************************************************************
379 Sync open files on a connection.
380 ****************************************************************************/
382 void file_sync_all(connection_struct *conn)
384 files_struct *fsp, *next;
386 for (fsp=Files;fsp;fsp=next) {
388 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
389 sync_file(conn, fsp, True /* write through */);
394 /****************************************************************************
396 ****************************************************************************/
398 void file_free(struct smb_request *req, files_struct *fsp)
400 DLIST_REMOVE(Files, fsp);
402 string_free(&fsp->fsp_name);
404 TALLOC_FREE(fsp->fake_file_handle);
406 if (fsp->fh->ref_count == 1) {
409 fsp->fh->ref_count--;
413 notify_remove(fsp->conn->notify_ctx, fsp);
414 TALLOC_FREE(fsp->notify);
417 /* Ensure this event will never fire. */
418 TALLOC_FREE(fsp->oplock_timeout);
420 /* Ensure this event will never fire. */
421 TALLOC_FREE(fsp->update_write_time_event);
423 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
426 DEBUG(5,("freed files structure %d (%d used)\n",
427 fsp->fnum, files_used));
429 /* this is paranoia, just in case someone tries to reuse the
433 if (fsp == chain_fsp) {
437 /* Closing a file can invalidate the positive cache. */
438 if (fsp == fsp_fi_cache.fsp) {
439 ZERO_STRUCT(fsp_fi_cache);
442 /* Drop all remaining extensions. */
443 while (fsp->vfs_extension) {
444 vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp);
450 /****************************************************************************
451 Get an fsp from a 16 bit fnum.
452 ****************************************************************************/
454 files_struct *file_fnum(uint16 fnum)
459 for (fsp=Files;fsp;fsp=fsp->next, count++) {
460 if (fsp->fnum == fnum) {
462 DLIST_PROMOTE(Files, fsp);
470 /****************************************************************************
471 Get an fsp from a packet given the offset of a 16 bit fnum.
472 ****************************************************************************/
474 files_struct *file_fsp(struct smb_request *req, uint16 fid)
482 fsp = file_fnum(fid);
489 /****************************************************************************
490 Reset the chained fsp - done at the start of a packet reply.
491 ****************************************************************************/
493 void file_chain_reset(void)
498 /****************************************************************************
499 Duplicate the file handle part for a DOS or FCB open.
500 ****************************************************************************/
502 NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *fsp,
503 uint32 access_mask, uint32 share_access,
504 uint32 create_options, files_struct **result)
507 files_struct *dup_fsp;
509 status = file_new(NULL, fsp->conn, &dup_fsp);
511 if (!NT_STATUS_IS_OK(status)) {
515 SAFE_FREE(dup_fsp->fh);
517 dup_fsp->fh = fsp->fh;
518 dup_fsp->fh->ref_count++;
520 dup_fsp->file_id = fsp->file_id;
521 dup_fsp->initial_allocation_size = fsp->initial_allocation_size;
522 dup_fsp->mode = fsp->mode;
523 dup_fsp->file_pid = fsp->file_pid;
524 dup_fsp->vuid = fsp->vuid;
525 dup_fsp->open_time = fsp->open_time;
526 dup_fsp->access_mask = access_mask;
527 dup_fsp->share_access = share_access;
528 dup_fsp->oplock_type = fsp->oplock_type;
529 dup_fsp->can_lock = fsp->can_lock;
530 dup_fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
531 if (!CAN_WRITE(fsp->conn)) {
532 dup_fsp->can_write = False;
534 dup_fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
536 dup_fsp->print_file = fsp->print_file;
537 dup_fsp->modified = fsp->modified;
538 dup_fsp->is_directory = fsp->is_directory;
539 dup_fsp->aio_write_behind = fsp->aio_write_behind;
540 string_set(&dup_fsp->fsp_name,fsp->fsp_name);