s3:smbd/files: work without sconn->file_bmap and assign fsp->fnum = -1
[Samba/gebeck_regimport.git] / source3 / smbd / files.c
blobae340060398bf1e13ad95146522b00f3673fdce0
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/smbd.h"
22 #include "smbd/globals.h"
23 #include "libcli/security/security.h"
24 #include "util_tdb.h"
25 #include <ccan/hash/hash.h>
26 #include "lib/util/bitmap.h"
28 #define FILE_HANDLE_OFFSET 0x1000
30 /****************************************************************************
31 Return a unique number identifying this fsp over the life of this pid.
32 ****************************************************************************/
34 static unsigned long get_gen_count(struct smbd_server_connection *sconn)
36 sconn->file_gen_counter += 1;
37 if (sconn->file_gen_counter == 0) {
38 sconn->file_gen_counter += 1;
40 return sconn->file_gen_counter;
43 /****************************************************************************
44 Find first available file slot.
45 ****************************************************************************/
47 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
48 files_struct **result)
50 struct smbd_server_connection *sconn = conn->sconn;
51 int i = -1;
52 files_struct *fsp;
53 NTSTATUS status;
55 if (sconn->file_bmap != NULL) {
58 * we want to give out file handles differently on each new
59 * connection because of a common bug in MS clients where they
60 * try to reuse a file descriptor from an earlier smb
61 * connection. This code increases the chance that the errant
62 * client will get an error rather than causing corruption
64 if (sconn->first_file == 0) {
65 sconn->first_file = (getpid() ^ (int)time(NULL));
66 sconn->first_file %= sconn->real_max_open_files;
69 /* TODO: Port the id-tree implementation from Samba4 */
71 i = bitmap_find(sconn->file_bmap, sconn->first_file);
72 if (i == -1) {
73 DEBUG(0,("ERROR! Out of file structures\n"));
75 * TODO: We have to unconditionally return a DOS error
76 * here, W2k3 even returns ERRDOS/ERRnofids for
77 * ntcreate&x with NTSTATUS negotiated
79 return NT_STATUS_TOO_MANY_OPENED_FILES;
84 * Make a child of the connection_struct as an fsp can't exist
85 * independent of a connection.
87 fsp = talloc_zero(conn, struct files_struct);
88 if (!fsp) {
89 return NT_STATUS_NO_MEMORY;
93 * This can't be a child of fsp because the file_handle can be ref'd
94 * when doing a dos/fcb open, which will then share the file_handle
95 * across multiple fsps.
97 fsp->fh = talloc_zero(conn, struct fd_handle);
98 if (!fsp->fh) {
99 TALLOC_FREE(fsp);
100 return NT_STATUS_NO_MEMORY;
103 fsp->fh->ref_count = 1;
104 fsp->fh->fd = -1;
106 fsp->fnum = -1;
107 fsp->conn = conn;
108 fsp->fh->gen_id = get_gen_count(sconn);
109 GetTimeOfDay(&fsp->open_time);
111 if (sconn->file_bmap != NULL) {
112 sconn->first_file = (i+1) % (sconn->real_max_open_files);
114 bitmap_set(sconn->file_bmap, i);
116 fsp->fnum = i + FILE_HANDLE_OFFSET;
117 SMB_ASSERT(fsp->fnum < 65536);
120 DLIST_ADD(sconn->files, fsp);
121 sconn->num_files += 1;
123 conn->num_files_open++;
126 * Create an smb_filename with "" for the base_name. There are very
127 * few NULL checks, so make sure it's initialized with something. to
128 * be safe until an audit can be done.
130 status = create_synthetic_smb_fname(fsp, "", NULL, NULL,
131 &fsp->fsp_name);
132 if (!NT_STATUS_IS_OK(status)) {
133 file_free(NULL, fsp);
134 return status;
137 DEBUG(5,("allocated file structure %d, fnum = %d (%u used)\n",
138 i, fsp->fnum, (unsigned int)sconn->num_files));
140 if (req != NULL) {
141 req->chain_fsp = fsp;
144 /* A new fsp invalidates the positive and
145 negative fsp_fi_cache as the new fsp is pushed
146 at the start of the list and we search from
147 a cache hit to the *end* of the list. */
149 ZERO_STRUCT(sconn->fsp_fi_cache);
151 *result = fsp;
152 return NT_STATUS_OK;
155 /****************************************************************************
156 Close all open files for a connection.
157 ****************************************************************************/
159 void file_close_conn(connection_struct *conn)
161 files_struct *fsp, *next;
163 for (fsp=conn->sconn->files; fsp; fsp=next) {
164 next = fsp->next;
165 if (fsp->conn == conn) {
166 close_file(NULL, fsp, SHUTDOWN_CLOSE);
171 /****************************************************************************
172 Close all open files for a pid and a vuid.
173 ****************************************************************************/
175 void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
176 int vuid)
178 files_struct *fsp, *next;
180 for (fsp=sconn->files;fsp;fsp=next) {
181 next = fsp->next;
182 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
183 close_file(NULL, fsp, SHUTDOWN_CLOSE);
188 /****************************************************************************
189 Initialise file structures.
190 ****************************************************************************/
192 bool file_init(struct smbd_server_connection *sconn)
194 int request_max_open_files = lp_max_open_files();
195 int real_lim;
198 * Set the max_open files to be the requested
199 * max plus a fudgefactor to allow for the extra
200 * fd's we need such as log files etc...
202 real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
204 sconn->real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
206 if (sconn->real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES
207 > 65536)
208 sconn->real_max_open_files =
209 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
211 if(sconn->real_max_open_files != request_max_open_files) {
212 DEBUG(1, ("file_init: Information only: requested %d "
213 "open files, %d are available.\n",
214 request_max_open_files, sconn->real_max_open_files));
217 SMB_ASSERT(sconn->real_max_open_files > 100);
219 sconn->file_bmap = bitmap_talloc(sconn, sconn->real_max_open_files);
221 if (!sconn->file_bmap) {
222 return false;
224 return true;
227 /****************************************************************************
228 Close files open by a specified vuid.
229 ****************************************************************************/
231 void file_close_user(struct smbd_server_connection *sconn, int vuid)
233 files_struct *fsp, *next;
235 for (fsp=sconn->files; fsp; fsp=next) {
236 next=fsp->next;
237 if (fsp->vuid == vuid) {
238 close_file(NULL, fsp, SHUTDOWN_CLOSE);
244 * Walk the files table until "fn" returns non-NULL
247 struct files_struct *files_forall(
248 struct smbd_server_connection *sconn,
249 struct files_struct *(*fn)(struct files_struct *fsp,
250 void *private_data),
251 void *private_data)
253 struct files_struct *fsp, *next;
255 for (fsp = sconn->files; fsp; fsp = next) {
256 struct files_struct *ret;
257 next = fsp->next;
258 ret = fn(fsp, private_data);
259 if (ret != NULL) {
260 return ret;
263 return NULL;
266 /****************************************************************************
267 Find a fsp given a file descriptor.
268 ****************************************************************************/
270 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
272 int count=0;
273 files_struct *fsp;
275 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
276 if (fsp->fh->fd == fd) {
277 if (count > 10) {
278 DLIST_PROMOTE(sconn->files, fsp);
280 return fsp;
284 return NULL;
287 /****************************************************************************
288 Find a fsp given a device, inode and file_id.
289 ****************************************************************************/
291 files_struct *file_find_dif(struct smbd_server_connection *sconn,
292 struct file_id id, unsigned long gen_id)
294 int count=0;
295 files_struct *fsp;
297 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
298 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
299 if (file_id_equal(&fsp->file_id, &id) &&
300 fsp->fh->gen_id == gen_id ) {
301 if (count > 10) {
302 DLIST_PROMOTE(sconn->files, fsp);
304 /* Paranoia check. */
305 if ((fsp->fh->fd == -1) &&
306 (fsp->oplock_type != NO_OPLOCK) &&
307 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
308 DEBUG(0,("file_find_dif: file %s file_id = "
309 "%s, gen = %u oplock_type = %u is a "
310 "stat open with oplock type !\n",
311 fsp_str_dbg(fsp),
312 file_id_string_tos(&fsp->file_id),
313 (unsigned int)fsp->fh->gen_id,
314 (unsigned int)fsp->oplock_type ));
315 smb_panic("file_find_dif");
317 return fsp;
321 return NULL;
324 /****************************************************************************
325 Find the first fsp given a device and inode.
326 We use a singleton cache here to speed up searching from getfilepathinfo
327 calls.
328 ****************************************************************************/
330 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
331 struct file_id id)
333 files_struct *fsp;
335 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
336 /* Positive or negative cache hit. */
337 return sconn->fsp_fi_cache.fsp;
340 sconn->fsp_fi_cache.id = id;
342 for (fsp=sconn->files;fsp;fsp=fsp->next) {
343 if (file_id_equal(&fsp->file_id, &id)) {
344 /* Setup positive cache. */
345 sconn->fsp_fi_cache.fsp = fsp;
346 return fsp;
350 /* Setup negative cache. */
351 sconn->fsp_fi_cache.fsp = NULL;
352 return NULL;
355 /****************************************************************************
356 Find the next fsp having the same device and inode.
357 ****************************************************************************/
359 files_struct *file_find_di_next(files_struct *start_fsp)
361 files_struct *fsp;
363 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
364 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
365 return fsp;
369 return NULL;
372 /****************************************************************************
373 Find any fsp open with a pathname below that of an already open path.
374 ****************************************************************************/
376 bool file_find_subpath(files_struct *dir_fsp)
378 files_struct *fsp;
379 size_t dlen;
380 char *d_fullname = NULL;
382 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
383 dir_fsp->conn->connectpath,
384 dir_fsp->fsp_name->base_name);
386 if (!d_fullname) {
387 return false;
390 dlen = strlen(d_fullname);
392 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
393 char *d1_fullname;
395 if (fsp == dir_fsp) {
396 continue;
399 d1_fullname = talloc_asprintf(talloc_tos(),
400 "%s/%s",
401 fsp->conn->connectpath,
402 fsp->fsp_name->base_name);
405 * If the open file has a path that is a longer
406 * component, then it's a subpath.
408 if (strnequal(d_fullname, d1_fullname, dlen) &&
409 (d1_fullname[dlen] == '/')) {
410 TALLOC_FREE(d1_fullname);
411 TALLOC_FREE(d_fullname);
412 return true;
414 TALLOC_FREE(d1_fullname);
417 TALLOC_FREE(d_fullname);
418 return false;
421 /****************************************************************************
422 Sync open files on a connection.
423 ****************************************************************************/
425 void file_sync_all(connection_struct *conn)
427 files_struct *fsp, *next;
429 for (fsp=conn->sconn->files; fsp; fsp=next) {
430 next=fsp->next;
431 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
432 sync_file(conn, fsp, True /* write through */);
437 /****************************************************************************
438 Free up a fsp.
439 ****************************************************************************/
441 void file_free(struct smb_request *req, files_struct *fsp)
443 struct smbd_server_connection *sconn = fsp->conn->sconn;
445 DLIST_REMOVE(sconn->files, fsp);
446 SMB_ASSERT(sconn->num_files > 0);
447 sconn->num_files--;
449 TALLOC_FREE(fsp->fake_file_handle);
451 if (fsp->fh->ref_count == 1) {
452 TALLOC_FREE(fsp->fh);
453 } else {
454 fsp->fh->ref_count--;
457 if (fsp->notify) {
458 struct notify_context *notify_ctx =
459 fsp->conn->sconn->notify_ctx;
460 notify_remove(notify_ctx, fsp);
461 TALLOC_FREE(fsp->notify);
464 /* Ensure this event will never fire. */
465 TALLOC_FREE(fsp->update_write_time_event);
467 if (sconn->file_bmap != NULL) {
468 bitmap_clear(sconn->file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
470 DEBUG(5,("freed files structure %d (%u used)\n",
471 fsp->fnum, (unsigned int)sconn->num_files));
473 fsp->conn->num_files_open--;
475 if ((req != NULL) && (fsp == req->chain_fsp)) {
476 req->chain_fsp = NULL;
480 * Clear all possible chained fsp
481 * pointers in the SMB2 request queue.
483 if (req != NULL && req->smb2req) {
484 remove_smb2_chained_fsp(fsp);
487 /* Closing a file can invalidate the positive cache. */
488 if (fsp == sconn->fsp_fi_cache.fsp) {
489 ZERO_STRUCT(sconn->fsp_fi_cache);
492 /* Drop all remaining extensions. */
493 while (fsp->vfs_extension) {
494 vfs_remove_fsp_extension(fsp->vfs_extension->owner, fsp);
497 /* this is paranoia, just in case someone tries to reuse the
498 information */
499 ZERO_STRUCTP(fsp);
501 /* fsp->fsp_name is a talloc child and is free'd automatically. */
502 TALLOC_FREE(fsp);
505 /****************************************************************************
506 Get an fsp from a 16 bit fnum.
507 ****************************************************************************/
509 static struct files_struct *file_fnum(struct smbd_server_connection *sconn,
510 uint16 fnum)
512 files_struct *fsp;
513 int count=0;
515 for (fsp=sconn->files; fsp; fsp=fsp->next, count++) {
516 if (fsp->fnum == -1) {
517 continue;
520 if (fsp->fnum == fnum) {
521 if (count > 10) {
522 DLIST_PROMOTE(sconn->files, fsp);
524 return fsp;
527 return NULL;
530 /****************************************************************************
531 Get an fsp from a packet given a 16 bit fnum.
532 ****************************************************************************/
534 files_struct *file_fsp(struct smb_request *req, uint16 fid)
536 files_struct *fsp;
538 if (req == NULL) {
540 * We should never get here. req==NULL could in theory
541 * only happen from internal opens with a non-zero
542 * root_dir_fid. Internal opens just don't do that, at
543 * least they are not supposed to do so. And if they
544 * start to do so, they better fake up a smb_request
545 * from which we get the right smbd_server_conn. While
546 * this should never happen, let's return NULL here.
548 return NULL;
551 if (req->chain_fsp != NULL) {
552 return req->chain_fsp;
555 fsp = file_fnum(req->sconn, fid);
556 if (fsp != NULL) {
557 req->chain_fsp = fsp;
559 return fsp;
562 /****************************************************************************
563 Duplicate the file handle part for a DOS or FCB open.
564 ****************************************************************************/
566 NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
567 uint32 access_mask, uint32 share_access,
568 uint32 create_options, files_struct *to)
570 TALLOC_FREE(to->fh);
572 to->fh = from->fh;
573 to->fh->ref_count++;
575 to->file_id = from->file_id;
576 to->initial_allocation_size = from->initial_allocation_size;
577 to->file_pid = from->file_pid;
578 to->vuid = from->vuid;
579 to->open_time = from->open_time;
580 to->access_mask = access_mask;
581 to->share_access = share_access;
582 to->oplock_type = from->oplock_type;
583 to->can_lock = from->can_lock;
584 to->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
585 if (!CAN_WRITE(from->conn)) {
586 to->can_write = False;
587 } else {
588 to->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
590 to->modified = from->modified;
591 to->is_directory = from->is_directory;
592 to->aio_write_behind = from->aio_write_behind;
594 if (from->print_file) {
595 to->print_file = talloc(to, struct print_file_data);
596 if (!to->print_file) return NT_STATUS_NO_MEMORY;
597 to->print_file->rap_jobid = from->print_file->rap_jobid;
598 } else {
599 to->print_file = NULL;
602 return fsp_set_smb_fname(to, from->fsp_name);
606 * Return a jenkins hash of a pathname on a connection.
609 NTSTATUS file_name_hash(connection_struct *conn,
610 const char *name, uint32_t *p_name_hash)
612 char *fullpath = NULL;
614 /* Set the hash of the full pathname. */
615 fullpath = talloc_asprintf(talloc_tos(),
616 "%s/%s",
617 conn->connectpath,
618 name);
619 if (!fullpath) {
620 return NT_STATUS_NO_MEMORY;
622 *p_name_hash = hash(fullpath, talloc_get_size(fullpath), 0);
624 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
625 fullpath,
626 (unsigned int)*p_name_hash ));
628 TALLOC_FREE(fullpath);
629 return NT_STATUS_OK;
633 * The only way that the fsp->fsp_name field should ever be set.
635 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
636 const struct smb_filename *smb_fname_in)
638 NTSTATUS status;
639 struct smb_filename *smb_fname_new;
641 status = copy_smb_filename(fsp, smb_fname_in, &smb_fname_new);
642 if (!NT_STATUS_IS_OK(status)) {
643 return status;
646 TALLOC_FREE(fsp->fsp_name);
647 fsp->fsp_name = smb_fname_new;
649 return file_name_hash(fsp->conn,
650 smb_fname_str_dbg(fsp->fsp_name),
651 &fsp->name_hash);