r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / smbd / files.c
blob02e4cd96634d54112536cbbecb2a253a5926aaca
1 /*
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/>.
20 #include "includes.h"
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 {
39 files_struct *fsp;
40 struct file_id id;
41 } fsp_fi_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(connection_struct *conn, files_struct **result)
62 int i;
63 static int first_file;
64 files_struct *fsp;
66 /* we want to give out file handles differently on each new
67 connection because of a common bug in MS clients where they try to
68 reuse a file descriptor from an earlier smb connection. This code
69 increases the chance that the errant client will get an error rather
70 than causing corruption */
71 if (first_file == 0) {
72 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
75 /* TODO: Port the id-tree implementation from Samba4 */
77 i = bitmap_find(file_bmap, first_file);
78 if (i == -1) {
79 DEBUG(0,("ERROR! Out of file structures\n"));
80 /* TODO: We have to unconditionally return a DOS error here,
81 * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
82 * NTSTATUS negotiated */
83 return NT_STATUS_TOO_MANY_OPENED_FILES;
86 fsp = SMB_MALLOC_P(files_struct);
87 if (!fsp) {
88 return NT_STATUS_NO_MEMORY;
91 ZERO_STRUCTP(fsp);
93 fsp->fh = SMB_MALLOC_P(struct fd_handle);
94 if (!fsp->fh) {
95 SAFE_FREE(fsp);
96 return NT_STATUS_NO_MEMORY;
99 ZERO_STRUCTP(fsp->fh);
101 fsp->fh->ref_count = 1;
102 fsp->fh->fd = -1;
104 fsp->conn = conn;
105 fsp->fh->gen_id = get_gen_count();
106 GetTimeOfDay(&fsp->open_time);
108 first_file = (i+1) % real_max_open_files;
110 bitmap_set(file_bmap, i);
111 files_used++;
113 fsp->fnum = i + FILE_HANDLE_OFFSET;
114 SMB_ASSERT(fsp->fnum < 65536);
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 /* A new fsp invalidates a negative fsp_fi_cache. */
126 if (fsp_fi_cache.fsp == NULL) {
127 ZERO_STRUCT(fsp_fi_cache);
130 *result = fsp;
131 return NT_STATUS_OK;
134 /****************************************************************************
135 Close all open files for a connection.
136 ****************************************************************************/
138 void file_close_conn(connection_struct *conn)
140 files_struct *fsp, *next;
142 for (fsp=Files;fsp;fsp=next) {
143 next = fsp->next;
144 if (fsp->conn == conn) {
145 close_file(fsp,SHUTDOWN_CLOSE);
150 /****************************************************************************
151 Close all open files for a pid and a vuid.
152 ****************************************************************************/
154 void file_close_pid(uint16 smbpid, int vuid)
156 files_struct *fsp, *next;
158 for (fsp=Files;fsp;fsp=next) {
159 next = fsp->next;
160 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
161 close_file(fsp,SHUTDOWN_CLOSE);
166 /****************************************************************************
167 Initialise file structures.
168 ****************************************************************************/
170 #define MAX_OPEN_FUDGEFACTOR 20
172 void file_init(void)
174 int request_max_open_files = lp_max_open_files();
175 int real_lim;
178 * Set the max_open files to be the requested
179 * max plus a fudgefactor to allow for the extra
180 * fd's we need such as log files etc...
182 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
184 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
186 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
187 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
189 if(real_max_open_files != request_max_open_files) {
190 DEBUG(1,("file_init: Information only: requested %d \
191 open files, %d are available.\n", request_max_open_files, real_max_open_files));
194 SMB_ASSERT(real_max_open_files > 100);
196 file_bmap = bitmap_allocate(real_max_open_files);
198 if (!file_bmap) {
199 exit_server("out of memory in file_init");
203 * Ensure that pipe_handle_oppset is set correctly.
205 set_pipe_handle_offset(real_max_open_files);
208 /****************************************************************************
209 Close files open by a specified vuid.
210 ****************************************************************************/
212 void file_close_user(int vuid)
214 files_struct *fsp, *next;
216 for (fsp=Files;fsp;fsp=next) {
217 next=fsp->next;
218 if (fsp->vuid == vuid) {
219 close_file(fsp,SHUTDOWN_CLOSE);
224 /****************************************************************************
225 Debug to enumerate all open files in the smbd.
226 ****************************************************************************/
228 void file_dump_open_table(void)
230 int count=0;
231 files_struct *fsp;
233 for (fsp=Files;fsp;fsp=fsp->next,count++) {
234 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, gen = %lu, fileid=%s\n",
235 count, fsp->fnum, fsp->fsp_name, fsp->fh->fd, (unsigned long)fsp->fh->gen_id,
236 file_id_static_string(&fsp->file_id)));
240 /****************************************************************************
241 Find a fsp given a file descriptor.
242 ****************************************************************************/
244 files_struct *file_find_fd(int fd)
246 int count=0;
247 files_struct *fsp;
249 for (fsp=Files;fsp;fsp=fsp->next,count++) {
250 if (fsp->fh->fd == fd) {
251 if (count > 10) {
252 DLIST_PROMOTE(Files, fsp);
254 return fsp;
258 return NULL;
261 /****************************************************************************
262 Find a fsp given a device, inode and file_id.
263 ****************************************************************************/
265 files_struct *file_find_dif(struct file_id id, unsigned long gen_id)
267 int count=0;
268 files_struct *fsp;
270 for (fsp=Files;fsp;fsp=fsp->next,count++) {
271 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
272 if (file_id_equal(&fsp->file_id, &id) &&
273 fsp->fh->gen_id == gen_id ) {
274 if (count > 10) {
275 DLIST_PROMOTE(Files, fsp);
277 /* Paranoia check. */
278 if ((fsp->fh->fd == -1) &&
279 (fsp->oplock_type != NO_OPLOCK) &&
280 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
281 DEBUG(0,("file_find_dif: file %s file_id = %s, gen = %u \
282 oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name,
283 file_id_static_string(&fsp->file_id),
284 (unsigned int)fsp->fh->gen_id,
285 (unsigned int)fsp->oplock_type ));
286 smb_panic("file_find_dif");
288 return fsp;
292 return NULL;
295 /****************************************************************************
296 Check if an fsp still exists.
297 ****************************************************************************/
299 files_struct *file_find_fsp(files_struct *orig_fsp)
301 files_struct *fsp;
303 for (fsp=Files;fsp;fsp=fsp->next) {
304 if (fsp == orig_fsp)
305 return fsp;
308 return NULL;
311 /****************************************************************************
312 Find the first fsp given a device and inode.
313 We use a singleton cache here to speed up searching from getfilepathinfo
314 calls.
315 ****************************************************************************/
317 files_struct *file_find_di_first(struct file_id id)
319 files_struct *fsp;
321 if (file_id_equal(&fsp_fi_cache.id, &id)) {
322 /* Positive or negative cache hit. */
323 return fsp_fi_cache.fsp;
326 fsp_fi_cache.id = id;
328 for (fsp=Files;fsp;fsp=fsp->next) {
329 if ( fsp->fh->fd != -1 &&
330 file_id_equal(&fsp->file_id, &id)) {
331 /* Setup positive cache. */
332 fsp_fi_cache.fsp = fsp;
333 return fsp;
337 /* Setup negative cache. */
338 fsp_fi_cache.fsp = NULL;
339 return NULL;
342 /****************************************************************************
343 Find the next fsp having the same device and inode.
344 ****************************************************************************/
346 files_struct *file_find_di_next(files_struct *start_fsp)
348 files_struct *fsp;
350 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
351 if ( fsp->fh->fd != -1 &&
352 file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
353 return fsp;
357 return NULL;
360 /****************************************************************************
361 Find a fsp that is open for printing.
362 ****************************************************************************/
364 files_struct *file_find_print(void)
366 files_struct *fsp;
368 for (fsp=Files;fsp;fsp=fsp->next) {
369 if (fsp->print_file) {
370 return fsp;
374 return NULL;
377 /****************************************************************************
378 Set a pending modtime across all files with a given dev/ino pair.
379 Record the owner of that modtime.
380 ****************************************************************************/
382 void fsp_set_pending_modtime(files_struct *tfsp, const struct timespec mod)
384 files_struct *fsp;
386 if (null_timespec(mod)) {
387 return;
390 for (fsp = Files;fsp;fsp=fsp->next) {
391 if ( fsp->fh->fd != -1 && file_id_equal(&fsp->file_id, &tfsp->file_id)) {
392 fsp->pending_modtime = mod;
393 fsp->pending_modtime_owner = False;
397 tfsp->pending_modtime_owner = True;
400 /****************************************************************************
401 Sync open files on a connection.
402 ****************************************************************************/
404 void file_sync_all(connection_struct *conn)
406 files_struct *fsp, *next;
408 for (fsp=Files;fsp;fsp=next) {
409 next=fsp->next;
410 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
411 sync_file(conn, fsp, True /* write through */);
416 /****************************************************************************
417 Free up a fsp.
418 ****************************************************************************/
420 void file_free(files_struct *fsp)
422 DLIST_REMOVE(Files, fsp);
424 string_free(&fsp->fsp_name);
426 if (fsp->fake_file_handle) {
427 destroy_fake_file_handle(&fsp->fake_file_handle);
430 if (fsp->fh->ref_count == 1) {
431 SAFE_FREE(fsp->fh);
432 } else {
433 fsp->fh->ref_count--;
436 if (fsp->notify) {
437 notify_remove(fsp->conn->notify_ctx, fsp);
438 TALLOC_FREE(fsp->notify);
441 /* Ensure this event will never fire. */
442 TALLOC_FREE(fsp->oplock_timeout);
444 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
445 files_used--;
447 DEBUG(5,("freed files structure %d (%d used)\n",
448 fsp->fnum, files_used));
450 /* this is paranoia, just in case someone tries to reuse the
451 information */
452 ZERO_STRUCTP(fsp);
454 if (fsp == chain_fsp) {
455 chain_fsp = NULL;
458 /* Closing a file can invalidate the positive cache. */
459 if (fsp == fsp_fi_cache.fsp) {
460 ZERO_STRUCT(fsp_fi_cache);
463 SAFE_FREE(fsp);
466 /****************************************************************************
467 Get an fsp from a 16 bit fnum.
468 ****************************************************************************/
470 files_struct *file_fnum(uint16 fnum)
472 files_struct *fsp;
473 int count=0;
475 for (fsp=Files;fsp;fsp=fsp->next, count++) {
476 if (fsp->fnum == fnum) {
477 if (count > 10) {
478 DLIST_PROMOTE(Files, fsp);
480 return fsp;
483 return NULL;
486 /****************************************************************************
487 Get an fsp from a packet given the offset of a 16 bit fnum.
488 ****************************************************************************/
490 files_struct *file_fsp(char *buf, int where)
492 files_struct *fsp;
494 if (chain_fsp) {
495 return chain_fsp;
498 if (!buf) {
499 return NULL;
502 fsp = file_fnum(SVAL(buf, where));
503 if (fsp) {
504 chain_fsp = fsp;
506 return fsp;
509 /****************************************************************************
510 Reset the chained fsp - done at the start of a packet reply.
511 ****************************************************************************/
513 void file_chain_reset(void)
515 chain_fsp = NULL;
518 /****************************************************************************
519 Duplicate the file handle part for a DOS or FCB open.
520 ****************************************************************************/
522 NTSTATUS dup_file_fsp(files_struct *fsp,
523 uint32 access_mask,
524 uint32 share_access,
525 uint32 create_options,
526 files_struct **result)
528 NTSTATUS status;
529 files_struct *dup_fsp;
531 status = file_new(fsp->conn, &dup_fsp);
533 if (!NT_STATUS_IS_OK(status)) {
534 return status;
537 SAFE_FREE(dup_fsp->fh);
539 dup_fsp->fh = fsp->fh;
540 dup_fsp->fh->ref_count++;
542 dup_fsp->file_id = fsp->file_id;
543 dup_fsp->initial_allocation_size = fsp->initial_allocation_size;
544 dup_fsp->mode = fsp->mode;
545 dup_fsp->file_pid = fsp->file_pid;
546 dup_fsp->vuid = fsp->vuid;
547 dup_fsp->open_time = fsp->open_time;
548 dup_fsp->access_mask = access_mask;
549 dup_fsp->share_access = share_access;
550 dup_fsp->pending_modtime_owner = fsp->pending_modtime_owner;
551 dup_fsp->pending_modtime = fsp->pending_modtime;
552 dup_fsp->last_write_time = fsp->last_write_time;
553 dup_fsp->oplock_type = fsp->oplock_type;
554 dup_fsp->can_lock = fsp->can_lock;
555 dup_fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
556 if (!CAN_WRITE(fsp->conn)) {
557 dup_fsp->can_write = False;
558 } else {
559 dup_fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
561 dup_fsp->print_file = fsp->print_file;
562 dup_fsp->modified = fsp->modified;
563 dup_fsp->is_directory = fsp->is_directory;
564 dup_fsp->is_stat = fsp->is_stat;
565 string_set(&dup_fsp->fsp_name,fsp->fsp_name);
567 *result = dup_fsp;
568 return NT_STATUS_OK;