smbd: Factor out get_share_mode_write_time of get_file_infos
[Samba.git] / source3 / smbd / files.c
blobc64c84173cfcc10119418f6091801c13d578e980
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 * create new fsp to be used for file_new or a durable handle reconnect
33 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
34 files_struct **result)
36 NTSTATUS status = NT_STATUS_NO_MEMORY;
37 files_struct *fsp = NULL;
38 struct smbd_server_connection *sconn = conn->sconn;
40 fsp = talloc_zero(mem_ctx, struct files_struct);
41 if (fsp == NULL) {
42 goto fail;
46 * This can't be a child of fsp because the file_handle can be ref'd
47 * when doing a dos/fcb open, which will then share the file_handle
48 * across multiple fsps.
50 fsp->fh = talloc_zero(mem_ctx, struct fd_handle);
51 if (fsp->fh == NULL) {
52 goto fail;
55 fsp->fh->ref_count = 1;
56 fsp->fh->fd = -1;
58 fsp->fnum = FNUM_FIELD_INVALID;
59 fsp->conn = conn;
61 DLIST_ADD(sconn->files, fsp);
62 sconn->num_files += 1;
64 conn->num_files_open++;
66 *result = fsp;
67 return NT_STATUS_OK;
69 fail:
70 if (fsp != NULL) {
71 TALLOC_FREE(fsp->fh);
73 TALLOC_FREE(fsp);
75 return status;
78 /****************************************************************************
79 Find first available file slot.
80 ****************************************************************************/
82 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
83 files_struct **result)
85 struct smbd_server_connection *sconn = conn->sconn;
86 files_struct *fsp;
87 NTSTATUS status;
89 status = fsp_new(conn, conn, &fsp);
90 if (!NT_STATUS_IS_OK(status)) {
91 return status;
94 GetTimeOfDay(&fsp->open_time);
96 if (sconn->conn) {
97 struct smbXsrv_open *op = NULL;
98 NTTIME now = timeval_to_nttime(&fsp->open_time);
100 status = smbXsrv_open_create(sconn->conn,
101 conn->session_info,
102 now, &op);
103 if (!NT_STATUS_IS_OK(status)) {
104 file_free(NULL, fsp);
105 return status;
107 fsp->op = op;
108 op->compat = fsp;
109 fsp->fnum = op->local_id;
110 fsp->fh->gen_id = smbXsrv_open_hash(op);
114 * Create an smb_filename with "" for the base_name. There are very
115 * few NULL checks, so make sure it's initialized with something. to
116 * be safe until an audit can be done.
118 fsp->fsp_name = synthetic_smb_fname(fsp, "", NULL, NULL);
119 if (fsp->fsp_name == NULL) {
120 file_free(NULL, fsp);
121 return NT_STATUS_NO_MEMORY;
124 DEBUG(5,("allocated file structure %s (%u used)\n",
125 fsp_fnum_dbg(fsp), (unsigned int)sconn->num_files));
127 if (req != NULL) {
128 fsp->mid = req->mid;
129 req->chain_fsp = fsp;
132 /* A new fsp invalidates the positive and
133 negative fsp_fi_cache as the new fsp is pushed
134 at the start of the list and we search from
135 a cache hit to the *end* of the list. */
137 ZERO_STRUCT(sconn->fsp_fi_cache);
139 *result = fsp;
140 return NT_STATUS_OK;
143 /****************************************************************************
144 Close all open files for a connection.
145 ****************************************************************************/
147 void file_close_conn(connection_struct *conn)
149 files_struct *fsp, *next;
151 for (fsp=conn->sconn->files; fsp; fsp=next) {
152 next = fsp->next;
153 if (fsp->conn != conn) {
154 continue;
156 if (fsp->op != NULL && fsp->op->global->durable) {
158 * A tree disconnect closes a durable handle
160 fsp->op->global->durable = false;
162 close_file(NULL, fsp, SHUTDOWN_CLOSE);
166 /****************************************************************************
167 Close all open files for a pid and a vuid.
168 ****************************************************************************/
170 void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
171 uint64_t vuid)
173 files_struct *fsp, *next;
175 for (fsp=sconn->files;fsp;fsp=next) {
176 next = fsp->next;
177 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
178 close_file(NULL, fsp, SHUTDOWN_CLOSE);
183 /****************************************************************************
184 Initialise file structures.
185 ****************************************************************************/
187 static int files_max_open_fds;
189 bool file_init_global(void)
191 int request_max = lp_max_open_files();
192 int real_lim;
193 int real_max;
195 if (files_max_open_fds != 0) {
196 return true;
200 * Set the max_open files to be the requested
201 * max plus a fudgefactor to allow for the extra
202 * fd's we need such as log files etc...
204 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
206 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
208 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
209 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
212 if (real_max != request_max) {
213 DEBUG(1, ("file_init_global: Information only: requested %d "
214 "open files, %d are available.\n",
215 request_max, real_max));
218 SMB_ASSERT(real_max > 100);
220 files_max_open_fds = real_max;
221 return true;
224 bool file_init(struct smbd_server_connection *sconn)
226 bool ok;
228 ok = file_init_global();
229 if (!ok) {
230 return false;
233 sconn->real_max_open_files = files_max_open_fds;
235 return true;
238 /****************************************************************************
239 Close files open by a specified vuid.
240 ****************************************************************************/
242 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
244 files_struct *fsp, *next;
246 for (fsp=sconn->files; fsp; fsp=next) {
247 next=fsp->next;
248 if (fsp->vuid == vuid) {
249 close_file(NULL, fsp, SHUTDOWN_CLOSE);
255 * Walk the files table until "fn" returns non-NULL
258 struct files_struct *files_forall(
259 struct smbd_server_connection *sconn,
260 struct files_struct *(*fn)(struct files_struct *fsp,
261 void *private_data),
262 void *private_data)
264 struct files_struct *fsp, *next;
266 for (fsp = sconn->files; fsp; fsp = next) {
267 struct files_struct *ret;
268 next = fsp->next;
269 ret = fn(fsp, private_data);
270 if (ret != NULL) {
271 return ret;
274 return NULL;
277 /****************************************************************************
278 Find a fsp given a file descriptor.
279 ****************************************************************************/
281 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
283 int count=0;
284 files_struct *fsp;
286 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
287 if (fsp->fh->fd == fd) {
288 if (count > 10) {
289 DLIST_PROMOTE(sconn->files, fsp);
291 return fsp;
295 return NULL;
298 /****************************************************************************
299 Find a fsp given a device, inode and file_id.
300 ****************************************************************************/
302 files_struct *file_find_dif(struct smbd_server_connection *sconn,
303 struct file_id id, unsigned long gen_id)
305 int count=0;
306 files_struct *fsp;
308 if (gen_id == 0) {
309 return NULL;
312 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
313 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
314 if (file_id_equal(&fsp->file_id, &id) &&
315 fsp->fh->gen_id == gen_id ) {
316 if (count > 10) {
317 DLIST_PROMOTE(sconn->files, fsp);
319 /* Paranoia check. */
320 if ((fsp->fh->fd == -1) &&
321 (fsp->oplock_type != NO_OPLOCK)) {
322 DEBUG(0,("file_find_dif: file %s file_id = "
323 "%s, gen = %u oplock_type = %u is a "
324 "stat open with oplock type !\n",
325 fsp_str_dbg(fsp),
326 file_id_string_tos(&fsp->file_id),
327 (unsigned int)fsp->fh->gen_id,
328 (unsigned int)fsp->oplock_type ));
329 smb_panic("file_find_dif");
331 return fsp;
335 return NULL;
338 /****************************************************************************
339 Find the first fsp given a device and inode.
340 We use a singleton cache here to speed up searching from getfilepathinfo
341 calls.
342 ****************************************************************************/
344 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
345 struct file_id id)
347 files_struct *fsp;
349 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
350 /* Positive or negative cache hit. */
351 return sconn->fsp_fi_cache.fsp;
354 sconn->fsp_fi_cache.id = id;
356 for (fsp=sconn->files;fsp;fsp=fsp->next) {
357 if (file_id_equal(&fsp->file_id, &id)) {
358 /* Setup positive cache. */
359 sconn->fsp_fi_cache.fsp = fsp;
360 return fsp;
364 /* Setup negative cache. */
365 sconn->fsp_fi_cache.fsp = NULL;
366 return NULL;
369 /****************************************************************************
370 Find the next fsp having the same device and inode.
371 ****************************************************************************/
373 files_struct *file_find_di_next(files_struct *start_fsp)
375 files_struct *fsp;
377 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
378 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
379 return fsp;
383 return NULL;
386 /****************************************************************************
387 Find any fsp open with a pathname below that of an already open path.
388 ****************************************************************************/
390 bool file_find_subpath(files_struct *dir_fsp)
392 files_struct *fsp;
393 size_t dlen;
394 char *d_fullname = NULL;
396 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
397 dir_fsp->conn->connectpath,
398 dir_fsp->fsp_name->base_name);
400 if (!d_fullname) {
401 return false;
404 dlen = strlen(d_fullname);
406 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
407 char *d1_fullname;
409 if (fsp == dir_fsp) {
410 continue;
413 d1_fullname = talloc_asprintf(talloc_tos(),
414 "%s/%s",
415 fsp->conn->connectpath,
416 fsp->fsp_name->base_name);
419 * If the open file has a path that is a longer
420 * component, then it's a subpath.
422 if (strnequal(d_fullname, d1_fullname, dlen) &&
423 (d1_fullname[dlen] == '/')) {
424 TALLOC_FREE(d1_fullname);
425 TALLOC_FREE(d_fullname);
426 return true;
428 TALLOC_FREE(d1_fullname);
431 TALLOC_FREE(d_fullname);
432 return false;
435 /****************************************************************************
436 Sync open files on a connection.
437 ****************************************************************************/
439 void file_sync_all(connection_struct *conn)
441 files_struct *fsp, *next;
443 for (fsp=conn->sconn->files; fsp; fsp=next) {
444 next=fsp->next;
445 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
446 sync_file(conn, fsp, True /* write through */);
451 /****************************************************************************
452 Free up a fsp.
453 ****************************************************************************/
455 void fsp_free(files_struct *fsp)
457 struct smbd_server_connection *sconn = fsp->conn->sconn;
459 DLIST_REMOVE(sconn->files, fsp);
460 SMB_ASSERT(sconn->num_files > 0);
461 sconn->num_files--;
463 TALLOC_FREE(fsp->fake_file_handle);
465 if (fsp->fh->ref_count == 1) {
466 TALLOC_FREE(fsp->fh);
467 } else {
468 fsp->fh->ref_count--;
471 fsp->conn->num_files_open--;
473 /* this is paranoia, just in case someone tries to reuse the
474 information */
475 ZERO_STRUCTP(fsp);
477 /* fsp->fsp_name is a talloc child and is free'd automatically. */
478 TALLOC_FREE(fsp);
481 void file_free(struct smb_request *req, files_struct *fsp)
483 struct smbd_server_connection *sconn = fsp->conn->sconn;
484 uint64_t fnum = fsp->fnum;
486 if (fsp->notify) {
487 struct notify_context *notify_ctx =
488 fsp->conn->sconn->notify_ctx;
489 notify_remove(notify_ctx, fsp);
490 TALLOC_FREE(fsp->notify);
493 /* Ensure this event will never fire. */
494 TALLOC_FREE(fsp->update_write_time_event);
496 if (fsp->op != NULL) {
497 fsp->op->compat = NULL;
499 TALLOC_FREE(fsp->op);
501 if ((req != NULL) && (fsp == req->chain_fsp)) {
502 req->chain_fsp = NULL;
506 * Clear all possible chained fsp
507 * pointers in the SMB2 request queue.
509 if (req != NULL && req->smb2req) {
510 remove_smb2_chained_fsp(fsp);
513 /* Closing a file can invalidate the positive cache. */
514 if (fsp == sconn->fsp_fi_cache.fsp) {
515 ZERO_STRUCT(sconn->fsp_fi_cache);
518 /* Drop all remaining extensions. */
519 vfs_remove_all_fsp_extensions(fsp);
521 fsp_free(fsp);
523 DEBUG(5,("freed files structure %llu (%u used)\n",
524 (unsigned long long)fnum, (unsigned int)sconn->num_files));
527 /****************************************************************************
528 Get an fsp from a packet given a 16 bit fnum.
529 ****************************************************************************/
531 files_struct *file_fsp(struct smb_request *req, uint16 fid)
533 struct smbXsrv_open *op;
534 NTSTATUS status;
535 NTTIME now = 0;
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 if (req->chain_fsp->deferred_close) {
553 return NULL;
555 return req->chain_fsp;
558 if (req->sconn->conn == NULL) {
559 return NULL;
562 now = timeval_to_nttime(&req->request_time);
564 status = smb1srv_open_lookup(req->sconn->conn,
565 fid, now, &op);
566 if (!NT_STATUS_IS_OK(status)) {
567 return NULL;
570 fsp = op->compat;
571 if (fsp == NULL) {
572 return NULL;
575 if (fsp->deferred_close) {
576 return NULL;
579 req->chain_fsp = fsp;
580 return fsp;
583 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
584 uint64_t persistent_id,
585 uint64_t volatile_id)
587 struct smbXsrv_open *op;
588 NTSTATUS status;
589 NTTIME now = 0;
590 struct files_struct *fsp;
592 now = timeval_to_nttime(&smb2req->request_time);
594 status = smb2srv_open_lookup(smb2req->sconn->conn,
595 persistent_id, volatile_id,
596 now, &op);
597 if (!NT_STATUS_IS_OK(status)) {
598 return NULL;
601 fsp = op->compat;
602 if (fsp == NULL) {
603 return NULL;
606 if (smb2req->tcon == NULL) {
607 return NULL;
610 if (smb2req->tcon->compat != fsp->conn) {
611 return NULL;
614 if (smb2req->session == NULL) {
615 return NULL;
618 if (smb2req->session->compat == NULL) {
619 return NULL;
622 if (smb2req->session->compat->vuid != fsp->vuid) {
623 return NULL;
626 if (fsp->deferred_close) {
627 return NULL;
630 return fsp;
633 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
634 uint64_t persistent_id,
635 uint64_t volatile_id)
637 struct files_struct *fsp;
639 if (smb2req->compat_chain_fsp != NULL) {
640 if (smb2req->compat_chain_fsp->deferred_close) {
641 return NULL;
643 return smb2req->compat_chain_fsp;
646 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
647 if (fsp == NULL) {
648 return NULL;
651 smb2req->compat_chain_fsp = fsp;
652 return fsp;
655 /****************************************************************************
656 Duplicate the file handle part for a DOS or FCB open.
657 ****************************************************************************/
659 NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
660 uint32 access_mask, uint32 share_access,
661 uint32 create_options, files_struct *to)
663 /* this can never happen for print files */
664 SMB_ASSERT(from->print_file == NULL);
666 TALLOC_FREE(to->fh);
668 to->fh = from->fh;
669 to->fh->ref_count++;
671 to->file_id = from->file_id;
672 to->initial_allocation_size = from->initial_allocation_size;
673 to->file_pid = from->file_pid;
674 to->vuid = from->vuid;
675 to->open_time = from->open_time;
676 to->access_mask = access_mask;
677 to->share_access = share_access;
678 to->oplock_type = from->oplock_type;
679 to->can_lock = from->can_lock;
680 to->can_read = ((access_mask & FILE_READ_DATA) != 0);
681 to->can_write =
682 CAN_WRITE(from->conn) &&
683 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
684 to->modified = from->modified;
685 to->is_directory = from->is_directory;
686 to->aio_write_behind = from->aio_write_behind;
688 return fsp_set_smb_fname(to, from->fsp_name);
692 * Return a jenkins hash of a pathname on a connection.
695 NTSTATUS file_name_hash(connection_struct *conn,
696 const char *name, uint32_t *p_name_hash)
698 char *fullpath = NULL;
700 /* Set the hash of the full pathname. */
701 fullpath = talloc_asprintf(talloc_tos(),
702 "%s/%s",
703 conn->connectpath,
704 name);
705 if (!fullpath) {
706 return NT_STATUS_NO_MEMORY;
708 *p_name_hash = hash(fullpath, talloc_get_size(fullpath), 0);
710 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
711 fullpath,
712 (unsigned int)*p_name_hash ));
714 TALLOC_FREE(fullpath);
715 return NT_STATUS_OK;
719 * The only way that the fsp->fsp_name field should ever be set.
721 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
722 const struct smb_filename *smb_fname_in)
724 struct smb_filename *smb_fname_new;
726 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
727 if (smb_fname_new == NULL) {
728 return NT_STATUS_NO_MEMORY;
731 TALLOC_FREE(fsp->fsp_name);
732 fsp->fsp_name = smb_fname_new;
734 return file_name_hash(fsp->conn,
735 smb_fname_str_dbg(fsp->fsp_name),
736 &fsp->name_hash);