s3-rpc_client: move protos to cli_lsarpc.h
[Samba/ekacnet.git] / source3 / smbd / files.c
blob43956e3903eac4525892c8cc5545a78a07907f40
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"
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)
45 int i;
46 files_struct *fsp;
47 NTSTATUS status;
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 = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
58 /* TODO: Port the id-tree implementation from Samba4 */
60 i = bitmap_find(file_bmap, first_file);
61 if (i == -1) {
62 DEBUG(0,("ERROR! Out of file structures\n"));
63 /* TODO: We have to unconditionally return a DOS error here,
64 * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
65 * NTSTATUS negotiated */
66 return NT_STATUS_TOO_MANY_OPENED_FILES;
70 * Make a child of the connection_struct as an fsp can't exist
71 * indepenedent of a connection.
73 fsp = talloc_zero(conn, struct files_struct);
74 if (!fsp) {
75 return NT_STATUS_NO_MEMORY;
79 * This can't be a child of fsp because the file_handle can be ref'd
80 * when doing a dos/fcb open, which will then share the file_handle
81 * across multiple fsps.
83 fsp->fh = talloc_zero(conn, struct fd_handle);
84 if (!fsp->fh) {
85 TALLOC_FREE(fsp);
86 return NT_STATUS_NO_MEMORY;
89 fsp->fh->ref_count = 1;
90 fsp->fh->fd = -1;
92 fsp->conn = conn;
93 fsp->fh->gen_id = get_gen_count();
94 GetTimeOfDay(&fsp->open_time);
96 first_file = (i+1) % real_max_open_files;
98 bitmap_set(file_bmap, i);
99 files_used++;
101 fsp->fnum = i + FILE_HANDLE_OFFSET;
102 SMB_ASSERT(fsp->fnum < 65536);
105 * Create an smb_filename with "" for the base_name. There are very
106 * few NULL checks, so make sure it's initialized with something. to
107 * be safe until an audit can be done.
109 status = create_synthetic_smb_fname(fsp, "", NULL, NULL,
110 &fsp->fsp_name);
111 if (!NT_STATUS_IS_OK(status)) {
112 TALLOC_FREE(fsp);
113 TALLOC_FREE(fsp->fh);
116 DLIST_ADD(Files, fsp);
118 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
119 i, fsp->fnum, files_used));
121 if (req != NULL) {
122 req->chain_fsp = fsp;
125 /* A new fsp invalidates the positive and
126 negative fsp_fi_cache as the new fsp is pushed
127 at the start of the list and we search from
128 a cache hit to the *end* of the list. */
130 ZERO_STRUCT(fsp_fi_cache);
132 conn->num_files_open++;
134 *result = fsp;
135 return NT_STATUS_OK;
138 /****************************************************************************
139 Close all open files for a connection.
140 ****************************************************************************/
142 void file_close_conn(connection_struct *conn)
144 files_struct *fsp, *next;
146 for (fsp=Files;fsp;fsp=next) {
147 next = fsp->next;
148 if (fsp->conn == conn) {
149 close_file(NULL, fsp, SHUTDOWN_CLOSE);
154 /****************************************************************************
155 Close all open files for a pid and a vuid.
156 ****************************************************************************/
158 void file_close_pid(uint16 smbpid, int vuid)
160 files_struct *fsp, *next;
162 for (fsp=Files;fsp;fsp=next) {
163 next = fsp->next;
164 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
165 close_file(NULL, fsp, SHUTDOWN_CLOSE);
170 /****************************************************************************
171 Initialise file structures.
172 ****************************************************************************/
174 void file_init(void)
176 int request_max_open_files = lp_max_open_files();
177 int real_lim;
180 * Set the max_open files to be the requested
181 * max plus a fudgefactor to allow for the extra
182 * fd's we need such as log files etc...
184 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
186 real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
188 if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
189 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
191 if(real_max_open_files != request_max_open_files) {
192 DEBUG(1,("file_init: Information only: requested %d \
193 open files, %d are available.\n", request_max_open_files, real_max_open_files));
196 SMB_ASSERT(real_max_open_files > 100);
198 file_bmap = bitmap_talloc(talloc_autofree_context(),
199 real_max_open_files);
201 if (!file_bmap) {
202 exit_server("out of memory in file_init");
206 /****************************************************************************
207 Close files open by a specified vuid.
208 ****************************************************************************/
210 void file_close_user(int vuid)
212 files_struct *fsp, *next;
214 for (fsp=Files;fsp;fsp=next) {
215 next=fsp->next;
216 if (fsp->vuid == vuid) {
217 close_file(NULL, fsp, SHUTDOWN_CLOSE);
223 * Walk the files table until "fn" returns non-NULL
226 struct files_struct *files_forall(
227 struct files_struct *(*fn)(struct files_struct *fsp,
228 void *private_data),
229 void *private_data)
231 struct files_struct *fsp, *next;
233 for (fsp = Files; fsp; fsp = next) {
234 struct files_struct *ret;
235 next = fsp->next;
236 ret = fn(fsp, private_data);
237 if (ret != NULL) {
238 return ret;
241 return NULL;
244 /****************************************************************************
245 Debug to enumerate all open files in the smbd.
246 ****************************************************************************/
248 void file_dump_open_table(void)
250 int count=0;
251 files_struct *fsp;
253 for (fsp=Files;fsp;fsp=fsp->next,count++) {
254 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, gen = %lu, "
255 "fileid=%s\n", count, fsp->fnum, fsp_str_dbg(fsp),
256 fsp->fh->fd, (unsigned long)fsp->fh->gen_id,
257 file_id_string_tos(&fsp->file_id)));
261 /****************************************************************************
262 Find a fsp given a file descriptor.
263 ****************************************************************************/
265 files_struct *file_find_fd(int fd)
267 int count=0;
268 files_struct *fsp;
270 for (fsp=Files;fsp;fsp=fsp->next,count++) {
271 if (fsp->fh->fd == fd) {
272 if (count > 10) {
273 DLIST_PROMOTE(Files, fsp);
275 return fsp;
279 return NULL;
282 /****************************************************************************
283 Find a fsp given a device, inode and file_id.
284 ****************************************************************************/
286 files_struct *file_find_dif(struct file_id id, unsigned long gen_id)
288 int count=0;
289 files_struct *fsp;
291 for (fsp=Files;fsp;fsp=fsp->next,count++) {
292 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
293 if (file_id_equal(&fsp->file_id, &id) &&
294 fsp->fh->gen_id == gen_id ) {
295 if (count > 10) {
296 DLIST_PROMOTE(Files, fsp);
298 /* Paranoia check. */
299 if ((fsp->fh->fd == -1) &&
300 (fsp->oplock_type != NO_OPLOCK) &&
301 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
302 DEBUG(0,("file_find_dif: file %s file_id = "
303 "%s, gen = %u oplock_type = %u is a "
304 "stat open with oplock type !\n",
305 fsp_str_dbg(fsp),
306 file_id_string_tos(&fsp->file_id),
307 (unsigned int)fsp->fh->gen_id,
308 (unsigned int)fsp->oplock_type ));
309 smb_panic("file_find_dif");
311 return fsp;
315 return NULL;
318 /****************************************************************************
319 Check if an fsp still exists.
320 ****************************************************************************/
322 files_struct *file_find_fsp(files_struct *orig_fsp)
324 files_struct *fsp;
326 for (fsp=Files;fsp;fsp=fsp->next) {
327 if (fsp == orig_fsp)
328 return fsp;
331 return NULL;
334 /****************************************************************************
335 Find the first fsp given a device and inode.
336 We use a singleton cache here to speed up searching from getfilepathinfo
337 calls.
338 ****************************************************************************/
340 files_struct *file_find_di_first(struct file_id id)
342 files_struct *fsp;
344 if (file_id_equal(&fsp_fi_cache.id, &id)) {
345 /* Positive or negative cache hit. */
346 return fsp_fi_cache.fsp;
349 fsp_fi_cache.id = id;
351 for (fsp=Files;fsp;fsp=fsp->next) {
352 if (file_id_equal(&fsp->file_id, &id)) {
353 /* Setup positive cache. */
354 fsp_fi_cache.fsp = fsp;
355 return fsp;
359 /* Setup negative cache. */
360 fsp_fi_cache.fsp = NULL;
361 return NULL;
364 /****************************************************************************
365 Find the next fsp having the same device and inode.
366 ****************************************************************************/
368 files_struct *file_find_di_next(files_struct *start_fsp)
370 files_struct *fsp;
372 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
373 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
374 return fsp;
378 return NULL;
381 /****************************************************************************
382 Find a fsp that is open for printing.
383 ****************************************************************************/
385 files_struct *file_find_print(void)
387 files_struct *fsp;
389 for (fsp=Files;fsp;fsp=fsp->next) {
390 if (fsp->print_file) {
391 return fsp;
395 return NULL;
398 /****************************************************************************
399 Find any fsp open with a pathname below that of an already open path.
400 ****************************************************************************/
402 bool file_find_subpath(files_struct *dir_fsp)
404 files_struct *fsp;
405 size_t dlen;
406 char *d_fullname = NULL;
408 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
409 dir_fsp->conn->connectpath,
410 dir_fsp->fsp_name->base_name);
412 if (!d_fullname) {
413 return false;
416 dlen = strlen(d_fullname);
418 for (fsp=Files;fsp;fsp=fsp->next) {
419 char *d1_fullname;
421 if (fsp == dir_fsp) {
422 continue;
425 d1_fullname = talloc_asprintf(talloc_tos(),
426 "%s/%s",
427 fsp->conn->connectpath,
428 fsp->fsp_name->base_name);
431 * If the open file has a path that is a longer
432 * component, then it's a subpath.
434 if (strnequal(d_fullname, d1_fullname, dlen) &&
435 (d1_fullname[dlen] == '/')) {
436 TALLOC_FREE(d1_fullname);
437 TALLOC_FREE(d_fullname);
438 return true;
440 TALLOC_FREE(d1_fullname);
443 TALLOC_FREE(d_fullname);
444 return false;
447 /****************************************************************************
448 Sync open files on a connection.
449 ****************************************************************************/
451 void file_sync_all(connection_struct *conn)
453 files_struct *fsp, *next;
455 for (fsp=Files;fsp;fsp=next) {
456 next=fsp->next;
457 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
458 sync_file(conn, fsp, True /* write through */);
463 /****************************************************************************
464 Free up a fsp.
465 ****************************************************************************/
467 void file_free(struct smb_request *req, files_struct *fsp)
469 DLIST_REMOVE(Files, fsp);
471 TALLOC_FREE(fsp->fake_file_handle);
473 if (fsp->fh->ref_count == 1) {
474 TALLOC_FREE(fsp->fh);
475 } else {
476 fsp->fh->ref_count--;
479 if (fsp->notify) {
480 if (fsp->is_directory) {
481 notify_remove_onelevel(fsp->conn->notify_ctx,
482 &fsp->file_id, fsp);
484 notify_remove(fsp->conn->notify_ctx, fsp);
485 TALLOC_FREE(fsp->notify);
488 /* Ensure this event will never fire. */
489 TALLOC_FREE(fsp->oplock_timeout);
491 /* Ensure this event will never fire. */
492 TALLOC_FREE(fsp->update_write_time_event);
494 bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
495 files_used--;
497 DEBUG(5,("freed files structure %d (%d used)\n",
498 fsp->fnum, files_used));
500 fsp->conn->num_files_open--;
502 if ((req != NULL) && (fsp == req->chain_fsp)) {
503 req->chain_fsp = NULL;
506 /* Closing a file can invalidate the positive cache. */
507 if (fsp == fsp_fi_cache.fsp) {
508 ZERO_STRUCT(fsp_fi_cache);
511 /* Drop all remaining extensions. */
512 while (fsp->vfs_extension) {
513 vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp);
516 /* this is paranoia, just in case someone tries to reuse the
517 information */
518 ZERO_STRUCTP(fsp);
520 /* fsp->fsp_name is a talloc child and is free'd automatically. */
521 TALLOC_FREE(fsp);
524 /****************************************************************************
525 Get an fsp from a 16 bit fnum.
526 ****************************************************************************/
528 files_struct *file_fnum(uint16 fnum)
530 files_struct *fsp;
531 int count=0;
533 for (fsp=Files;fsp;fsp=fsp->next, count++) {
534 if (fsp->fnum == fnum) {
535 if (count > 10) {
536 DLIST_PROMOTE(Files, fsp);
538 return fsp;
541 return NULL;
544 /****************************************************************************
545 Get an fsp from a packet given the offset of a 16 bit fnum.
546 ****************************************************************************/
548 files_struct *file_fsp(struct smb_request *req, uint16 fid)
550 files_struct *fsp;
552 if ((req != NULL) && (req->chain_fsp != NULL)) {
553 return req->chain_fsp;
556 fsp = file_fnum(fid);
557 if ((fsp != NULL) && (req != NULL)) {
558 req->chain_fsp = fsp;
560 return fsp;
563 /****************************************************************************
564 Duplicate the file handle part for a DOS or FCB open.
565 ****************************************************************************/
567 NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
568 uint32 access_mask, uint32 share_access,
569 uint32 create_options, files_struct *to)
571 TALLOC_FREE(to->fh);
573 to->fh = from->fh;
574 to->fh->ref_count++;
576 to->file_id = from->file_id;
577 to->initial_allocation_size = from->initial_allocation_size;
578 to->mode = from->mode;
579 to->file_pid = from->file_pid;
580 to->vuid = from->vuid;
581 to->open_time = from->open_time;
582 to->access_mask = access_mask;
583 to->share_access = share_access;
584 to->oplock_type = from->oplock_type;
585 to->can_lock = from->can_lock;
586 to->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
587 if (!CAN_WRITE(from->conn)) {
588 to->can_write = False;
589 } else {
590 to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
592 to->modified = from->modified;
593 to->is_directory = from->is_directory;
594 to->aio_write_behind = from->aio_write_behind;
596 if (from->print_file) {
597 to->print_file = talloc(to, struct print_file_data);
598 if (!to->print_file) return NT_STATUS_NO_MEMORY;
599 to->print_file->rap_jobid = from->print_file->rap_jobid;
600 } else {
601 to->print_file = NULL;
604 return fsp_set_smb_fname(to, from->fsp_name);
608 * The only way that the fsp->fsp_name field should ever be set.
610 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
611 const struct smb_filename *smb_fname_in)
613 NTSTATUS status;
614 struct smb_filename *smb_fname_new;
616 status = copy_smb_filename(fsp, smb_fname_in, &smb_fname_new);
617 if (!NT_STATUS_IS_OK(status)) {
618 return status;
621 TALLOC_FREE(fsp->fsp_name);
622 fsp->fsp_name = smb_fname_new;
624 return NT_STATUS_OK;