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/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "libcli/security/security.h"
25 #include "lib/util/bitmap.h"
27 #define FILE_HANDLE_OFFSET 0x1000
29 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
30 struct smb_filename **_smb_fname);
33 * create new fsp to be used for file_new or a durable handle reconnect
35 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
36 files_struct **result)
38 NTSTATUS status = NT_STATUS_NO_MEMORY;
39 files_struct *fsp = NULL;
40 struct smbd_server_connection *sconn = conn->sconn;
42 fsp = talloc_zero(mem_ctx, struct files_struct);
48 * This can't be a child of fsp because the file_handle can be ref'd
49 * when doing a dos/fcb open, which will then share the file_handle
50 * across multiple fsps.
52 fsp->fh = fd_handle_create(mem_ctx);
53 if (fsp->fh == NULL) {
57 fsp->fsp_flags.use_ofd_locks = !lp_smbd_force_process_locks(SNUM(conn));
58 #ifndef HAVE_OFD_LOCKS
59 fsp->fsp_flags.use_ofd_locks = false;
62 fh_set_refcount(fsp->fh, 1);
65 fsp->fnum = FNUM_FIELD_INVALID;
67 fsp->close_write_time = make_omit_timespec();
69 DLIST_ADD(sconn->files, fsp);
70 sconn->num_files += 1;
72 conn->num_files_open++;
74 DBG_INFO("allocated files structure (%u used)\n",
75 (unsigned int)sconn->num_files);
89 void fsp_set_gen_id(files_struct *fsp)
91 static uint64_t gen_id = 1;
94 * A billion of 64-bit increments per second gives us
95 * more than 500 years of runtime without wrap.
98 fh_set_gen_id(fsp->fh, gen_id);
101 /****************************************************************************
102 Find first available file slot.
103 ****************************************************************************/
105 NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
107 struct smbXsrv_open *op = NULL;
112 DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
116 now = timeval_to_nttime(&fsp->open_time);
118 status = smbXsrv_open_create(req->xconn,
119 fsp->conn->session_info,
122 if (!NT_STATUS_IS_OK(status)) {
127 fsp->fnum = op->local_id;
130 req->chain_fsp = fsp;
132 DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
133 fsp_str_dbg(fsp), fsp->mid);
138 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
139 files_struct **result)
141 struct smbd_server_connection *sconn = conn->sconn;
145 status = fsp_new(conn, conn, &fsp);
146 if (!NT_STATUS_IS_OK(status)) {
150 GetTimeOfDay(&fsp->open_time);
152 status = fsp_bind_smb(fsp, req);
153 if (!NT_STATUS_IS_OK(status)) {
154 file_free(NULL, fsp);
161 * Create an smb_filename with "" for the base_name. There are very
162 * few NULL checks, so make sure it's initialized with something. to
163 * be safe until an audit can be done.
165 fsp->fsp_name = synthetic_smb_fname(fsp,
171 if (fsp->fsp_name == NULL) {
172 file_free(NULL, fsp);
173 return NT_STATUS_NO_MEMORY;
176 DBG_INFO("new file %s\n", fsp_fnum_dbg(fsp));
178 /* A new fsp invalidates the positive and
179 negative fsp_fi_cache as the new fsp is pushed
180 at the start of the list and we search from
181 a cache hit to the *end* of the list. */
183 ZERO_STRUCT(sconn->fsp_fi_cache);
189 NTSTATUS create_internal_fsp(connection_struct *conn,
190 const struct smb_filename *smb_fname,
191 struct files_struct **_fsp)
193 struct files_struct *fsp = NULL;
196 status = file_new(NULL, conn, &fsp);
197 if (!NT_STATUS_IS_OK(status)) {
201 status = fsp_set_smb_fname(fsp, smb_fname);
202 if (!NT_STATUS_IS_OK(status)) {
203 file_free(NULL, fsp);
212 * Create an internal fsp for an *existing* directory.
214 * This should only be used by callers in the VFS that need to control the
215 * opening of the directory. Otherwise use open_internal_dirfsp_at().
217 NTSTATUS create_internal_dirfsp(connection_struct *conn,
218 const struct smb_filename *smb_dname,
219 struct files_struct **_fsp)
221 struct files_struct *fsp = NULL;
224 status = create_internal_fsp(conn, smb_dname, &fsp);
225 if (!NT_STATUS_IS_OK(status)) {
229 fsp->access_mask = FILE_LIST_DIRECTORY;
230 fsp->fsp_flags.is_directory = true;
231 fsp->fsp_flags.is_dirfsp = true;
238 * Open an internal fsp for an *existing* directory.
240 NTSTATUS open_internal_dirfsp(connection_struct *conn,
241 const struct smb_filename *smb_dname,
243 struct files_struct **_fsp)
245 struct files_struct *fsp = NULL;
249 status = create_internal_dirfsp(conn, smb_dname, &fsp);
250 if (!NT_STATUS_IS_OK(status)) {
255 open_flags |= O_DIRECTORY;
257 status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, open_flags, 0);
258 if (!NT_STATUS_IS_OK(status)) {
259 DBG_INFO("Could not open fd for %s (%s)\n",
260 smb_fname_str_dbg(smb_dname),
262 file_free(NULL, fsp);
266 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
268 return map_nt_error_from_unix(errno);
271 if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
272 DBG_ERR("%s is not a directory!\n",
273 smb_fname_str_dbg(smb_dname));
274 file_free(NULL, fsp);
275 return NT_STATUS_NOT_A_DIRECTORY;
278 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
285 * The "link" in the name doesn't imply link in the filesystem
286 * sense. It's a object that "links" together an fsp and an smb_fname
287 * and the link allocated as talloc child of an fsp.
289 * The link is created for fsps that open_smbfname_fsp() returns in
290 * smb_fname->fsp. When this fsp is freed by fsp_free() by some caller
291 * somewhere, the destructor fsp_smb_fname_link_destructor() on the link object
292 * will use the link to reset the reference in smb_fname->fsp that is about to
295 * This prevents smb_fname_internal_fsp_destructor() from seeing dangling fsp
299 struct fsp_smb_fname_link {
300 struct fsp_smb_fname_link **smb_fname_link;
301 struct files_struct **smb_fname_fsp;
304 static int fsp_smb_fname_link_destructor(struct fsp_smb_fname_link *link)
306 if (link->smb_fname_link == NULL) {
310 *link->smb_fname_link = NULL;
311 *link->smb_fname_fsp = NULL;
315 static NTSTATUS fsp_smb_fname_link(struct files_struct *fsp,
316 struct fsp_smb_fname_link **smb_fname_link,
317 struct files_struct **smb_fname_fsp)
319 struct fsp_smb_fname_link *link = NULL;
321 SMB_ASSERT(*smb_fname_link == NULL);
322 SMB_ASSERT(*smb_fname_fsp == NULL);
324 link = talloc_zero(fsp, struct fsp_smb_fname_link);
326 return NT_STATUS_NO_MEMORY;
329 link->smb_fname_link = smb_fname_link;
330 link->smb_fname_fsp = smb_fname_fsp;
331 *smb_fname_link = link;
332 *smb_fname_fsp = fsp;
334 talloc_set_destructor(link, fsp_smb_fname_link_destructor);
339 * Free a link, carefully avoiding to trigger the link destructor
341 static void destroy_fsp_smb_fname_link(struct fsp_smb_fname_link **_link)
343 struct fsp_smb_fname_link *link = *_link;
348 talloc_set_destructor(link, NULL);
354 * Talloc destructor set on an smb_fname set by openat_pathref_fsp() used to
355 * close the embedded smb_fname->fsp.
357 static int smb_fname_fsp_destructor(struct smb_filename *smb_fname)
359 struct files_struct *fsp = smb_fname->fsp;
362 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
368 if (fsp->base_fsp != NULL) {
369 struct files_struct *tmp_base_fsp = fsp->base_fsp;
371 fsp_set_base_fsp(fsp, NULL);
373 status = fd_close(tmp_base_fsp);
374 SMB_ASSERT(NT_STATUS_IS_OK(status));
375 file_free(NULL, tmp_base_fsp);
378 status = fd_close(fsp);
379 SMB_ASSERT(NT_STATUS_IS_OK(status));
380 file_free(NULL, fsp);
381 smb_fname->fsp = NULL;
387 * For proper streams support, we have to open the base_fsp for pathref
390 static NTSTATUS open_pathref_base_fsp(const struct files_struct *dirfsp,
391 struct files_struct *fsp)
393 struct smb_filename *smb_fname_base = NULL;
397 smb_fname_base = synthetic_smb_fname(talloc_tos(),
398 fsp->fsp_name->base_name,
402 fsp->fsp_name->flags);
403 if (smb_fname_base == NULL) {
404 return NT_STATUS_NO_MEMORY;
407 ret = vfs_stat(fsp->conn, smb_fname_base);
409 return map_nt_error_from_unix(errno);
412 status = openat_pathref_fsp(dirfsp, smb_fname_base);
413 if (!NT_STATUS_IS_OK(status)) {
414 TALLOC_FREE(smb_fname_base);
415 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
416 DBG_DEBUG("Opening base file failed: %s\n",
420 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
423 fsp_set_base_fsp(fsp, smb_fname_base->fsp);
424 smb_fname_fsp_unlink(smb_fname_base);
425 TALLOC_FREE(smb_fname_base);
431 * Open an internal O_PATH based fsp for smb_fname. If O_PATH is not
432 * available, open O_RDONLY as root. Both is done in fd_open() ->
433 * non_widelink_open(), triggered by setting fsp->fsp_flags.is_pathref to
436 NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
437 struct smb_filename *smb_fname)
439 connection_struct *conn = dirfsp->conn;
440 struct smb_filename *full_fname = NULL;
441 struct files_struct *fsp = NULL;
442 int open_flags = O_RDONLY;
445 DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
447 if (smb_fname->fsp != NULL) {
448 /* We already have one for this name. */
449 DBG_DEBUG("smb_fname [%s] already has a pathref fsp.\n",
450 smb_fname_str_dbg(smb_fname));
454 if (!VALID_STAT(smb_fname->st)) {
455 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
458 if (S_ISLNK(smb_fname->st.st_ex_mode)) {
459 return NT_STATUS_STOPPED_ON_SYMLINK;
462 status = fsp_new(conn, conn, &fsp);
463 if (!NT_STATUS_IS_OK(status)) {
467 GetTimeOfDay(&fsp->open_time);
469 ZERO_STRUCT(conn->sconn->fsp_fi_cache);
471 fsp->fsp_flags.is_pathref = true;
472 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
473 fsp->fsp_flags.is_directory = true;
474 open_flags |= O_DIRECTORY;
477 full_fname = full_path_from_dirfsp_atname(fsp,
480 if (full_fname == NULL) {
481 status = NT_STATUS_NO_MEMORY;
485 if (is_ntfs_default_stream_smb_fname(full_fname)) {
486 full_fname->stream_name = NULL;
489 status = fsp_attach_smb_fname(fsp, &full_fname);
490 if (!NT_STATUS_IS_OK(status)) {
494 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
495 && is_ntfs_stream_smb_fname(fsp->fsp_name))
497 status = open_pathref_base_fsp(dirfsp, fsp);
498 if (!NT_STATUS_IS_OK(status)) {
503 status = fd_openat(dirfsp, smb_fname, fsp, open_flags, 0);
504 if (!NT_STATUS_IS_OK(status)) {
505 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
506 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND))
509 * streams_xattr return NT_STATUS_NOT_FOUND for
510 * opens of not yet exisiting streams.
512 * ELOOP maps to NT_STATUS_OBJECT_PATH_NOT_FOUND
513 * and this will result from a open request from
514 * a POSIX client on a symlink.
516 * NT_STATUS_OBJECT_NAME_NOT_FOUND is the simple
519 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
524 if (!check_same_dev_ino(&smb_fname->st, &fsp->fsp_name->st)) {
525 DBG_DEBUG("file [%s] - dev/ino mismatch. "
526 "Old (dev=%ju, ino=%ju). "
527 "New (dev=%ju, ino=%ju).\n",
528 smb_fname_str_dbg(smb_fname),
529 (uintmax_t)smb_fname->st.st_ex_dev,
530 (uintmax_t)smb_fname->st.st_ex_ino,
531 (uintmax_t)fsp->fsp_name->st.st_ex_dev,
532 (uintmax_t)fsp->fsp_name->st.st_ex_ino);
533 status = NT_STATUS_ACCESS_DENIED;
537 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
539 status = fsp_smb_fname_link(fsp,
540 &smb_fname->fsp_link,
542 if (!NT_STATUS_IS_OK(status)) {
546 DBG_DEBUG("fsp [%s]: OK\n", fsp_str_dbg(fsp));
548 talloc_set_destructor(smb_fname, smb_fname_fsp_destructor);
552 DBG_DEBUG("Opening pathref for [%s] failed: %s\n",
553 smb_fname_str_dbg(smb_fname),
559 if (fsp->base_fsp != NULL) {
560 struct files_struct *tmp_base_fsp = fsp->base_fsp;
562 fsp_set_base_fsp(fsp, NULL);
564 fd_close(tmp_base_fsp);
565 file_free(NULL, tmp_base_fsp);
568 file_free(NULL, fsp);
572 void smb_fname_fsp_unlink(struct smb_filename *smb_fname)
574 talloc_set_destructor(smb_fname, NULL);
575 smb_fname->fsp = NULL;
576 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
580 * Move any existing embedded fsp refs from the src name to the
581 * destination. It's safe to call this on src smb_fname's that have no embedded
584 NTSTATUS move_smb_fname_fsp_link(struct smb_filename *smb_fname_dst,
585 struct smb_filename *smb_fname_src)
590 * The target should always not be linked yet!
592 SMB_ASSERT(smb_fname_dst->fsp == NULL);
593 SMB_ASSERT(smb_fname_dst->fsp_link == NULL);
595 if (smb_fname_src->fsp == NULL) {
599 status = fsp_smb_fname_link(smb_fname_src->fsp,
600 &smb_fname_dst->fsp_link,
601 &smb_fname_dst->fsp);
602 if (!NT_STATUS_IS_OK(status)) {
606 talloc_set_destructor(smb_fname_dst, smb_fname_fsp_destructor);
608 smb_fname_fsp_unlink(smb_fname_src);
614 * Create an smb_fname and open smb_fname->fsp pathref
616 NTSTATUS synthetic_pathref(TALLOC_CTX *mem_ctx,
617 struct files_struct *dirfsp,
618 const char *base_name,
619 const char *stream_name,
620 const SMB_STRUCT_STAT *psbuf,
623 struct smb_filename **_smb_fname)
625 struct smb_filename *smb_fname = NULL;
629 smb_fname = synthetic_smb_fname(mem_ctx,
635 if (smb_fname == NULL) {
636 return NT_STATUS_NO_MEMORY;
639 if (!VALID_STAT(smb_fname->st)) {
640 ret = vfs_stat(dirfsp->conn, smb_fname);
642 DBG_ERR("stat [%s] failed: %s",
643 smb_fname_str_dbg(smb_fname),
645 TALLOC_FREE(smb_fname);
646 return map_nt_error_from_unix(errno);
650 status = openat_pathref_fsp(dirfsp, smb_fname);
651 if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
652 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
654 if (!NT_STATUS_IS_OK(status)) {
655 DBG_ERR("opening [%s] failed\n",
656 smb_fname_str_dbg(smb_fname));
657 TALLOC_FREE(smb_fname);
661 *_smb_fname = smb_fname;
665 static int atname_destructor(struct smb_filename *smb_fname)
667 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
672 * Turn a path into a parent pathref and atname
674 * This returns the parent pathref in _parent and the name relative to it. If
675 * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
676 * pathref as well, ie _atname->fsp will point at the same fsp as
679 NTSTATUS parent_pathref(TALLOC_CTX *mem_ctx,
680 struct files_struct *dirfsp,
681 const struct smb_filename *smb_fname,
682 struct smb_filename **_parent,
683 struct smb_filename **_atname)
685 struct smb_filename *parent = NULL;
686 struct smb_filename *atname = NULL;
691 ok = parent_smb_fname(mem_ctx,
696 return NT_STATUS_NO_MEMORY;
699 ret = vfs_stat(dirfsp->conn, parent);
702 return map_nt_error_from_unix(errno);
705 status = openat_pathref_fsp(dirfsp, parent);
706 if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
707 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
709 if (!NT_STATUS_IS_OK(status)) {
714 if (smb_fname->fsp != NULL) {
715 status = fsp_smb_fname_link(smb_fname->fsp,
718 if (!NT_STATUS_IS_OK(status)) {
722 talloc_set_destructor(atname, atname_destructor);
729 /****************************************************************************
730 Close all open files for a connection.
731 ****************************************************************************/
733 void file_close_conn(connection_struct *conn)
735 files_struct *fsp, *next;
737 for (fsp=conn->sconn->files; fsp; fsp=next) {
739 if (fsp->conn != conn) {
742 if (fsp->op != NULL && fsp->op->global->durable) {
744 * A tree disconnect closes a durable handle
746 fsp->op->global->durable = false;
748 close_file(NULL, fsp, SHUTDOWN_CLOSE);
752 /****************************************************************************
753 Initialise file structures.
754 ****************************************************************************/
756 static int files_max_open_fds;
758 bool file_init_global(void)
760 int request_max = lp_max_open_files();
764 if (files_max_open_fds != 0) {
769 * Set the max_open files to be the requested
770 * max plus a fudgefactor to allow for the extra
771 * fd's we need such as log files etc...
773 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
775 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
777 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
778 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
781 if (real_max != request_max) {
782 DEBUG(1, ("file_init_global: Information only: requested %d "
783 "open files, %d are available.\n",
784 request_max, real_max));
787 SMB_ASSERT(real_max > 100);
789 files_max_open_fds = real_max;
793 bool file_init(struct smbd_server_connection *sconn)
797 ok = file_init_global();
802 sconn->real_max_open_files = files_max_open_fds;
807 /****************************************************************************
808 Close files open by a specified vuid.
809 ****************************************************************************/
811 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
813 files_struct *fsp, *next;
815 for (fsp=sconn->files; fsp; fsp=next) {
817 if (fsp->vuid == vuid) {
818 close_file(NULL, fsp, SHUTDOWN_CLOSE);
824 * Walk the files table until "fn" returns non-NULL
827 struct files_struct *files_forall(
828 struct smbd_server_connection *sconn,
829 struct files_struct *(*fn)(struct files_struct *fsp,
833 struct files_struct *fsp, *next;
835 for (fsp = sconn->files; fsp; fsp = next) {
836 struct files_struct *ret;
838 ret = fn(fsp, private_data);
846 /****************************************************************************
847 Find a fsp given a file descriptor.
848 ****************************************************************************/
850 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
855 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
856 if (fsp_get_pathref_fd(fsp) == fd) {
858 DLIST_PROMOTE(sconn->files, fsp);
867 /****************************************************************************
868 Find a fsp given a device, inode and file_id.
869 ****************************************************************************/
871 files_struct *file_find_dif(struct smbd_server_connection *sconn,
872 struct file_id id, unsigned long gen_id)
881 for (fsp = sconn->files; fsp; fsp = fsp->next,count++) {
883 * We can have a fsp->fh->fd == -1 here as it could be a stat
886 if (!file_id_equal(&fsp->file_id, &id)) {
889 if (!fsp->fsp_flags.is_fsa) {
892 if (fh_get_gen_id(fsp->fh) != gen_id) {
896 DLIST_PROMOTE(sconn->files, fsp);
898 /* Paranoia check. */
899 if ((fsp_get_pathref_fd(fsp) == -1) &&
900 (fsp->oplock_type != NO_OPLOCK &&
901 fsp->oplock_type != LEASE_OPLOCK))
903 struct file_id_buf idbuf;
905 DBG_ERR("file %s file_id = "
906 "%s, gen = %u oplock_type = %u is a "
907 "stat open with oplock type !\n",
909 file_id_str_buf(fsp->file_id, &idbuf),
910 (unsigned int)fh_get_gen_id(fsp->fh),
911 (unsigned int)fsp->oplock_type);
912 smb_panic("file_find_dif");
920 /****************************************************************************
921 Find the first fsp given a device and inode.
922 We use a singleton cache here to speed up searching from getfilepathinfo
924 ****************************************************************************/
926 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
932 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
933 /* Positive or negative cache hit. */
934 return sconn->fsp_fi_cache.fsp;
937 sconn->fsp_fi_cache.id = id;
939 for (fsp=sconn->files;fsp;fsp=fsp->next) {
940 if (need_fsa && !fsp->fsp_flags.is_fsa) {
943 if (file_id_equal(&fsp->file_id, &id)) {
944 /* Setup positive cache. */
945 sconn->fsp_fi_cache.fsp = fsp;
950 /* Setup negative cache. */
951 sconn->fsp_fi_cache.fsp = NULL;
955 /****************************************************************************
956 Find the next fsp having the same device and inode.
957 ****************************************************************************/
959 files_struct *file_find_di_next(files_struct *start_fsp,
964 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
965 if (need_fsa && !fsp->fsp_flags.is_fsa) {
968 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
976 struct files_struct *file_find_one_fsp_from_lease_key(
977 struct smbd_server_connection *sconn,
978 const struct smb2_lease_key *lease_key)
980 struct files_struct *fsp;
982 for (fsp = sconn->files; fsp; fsp=fsp->next) {
983 if ((fsp->lease != NULL) &&
984 (fsp->lease->lease.lease_key.data[0] ==
985 lease_key->data[0]) &&
986 (fsp->lease->lease.lease_key.data[1] ==
987 lease_key->data[1])) {
994 /****************************************************************************
995 Find any fsp open with a pathname below that of an already open path.
996 ****************************************************************************/
998 bool file_find_subpath(files_struct *dir_fsp)
1002 char *d_fullname = NULL;
1004 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
1005 dir_fsp->conn->connectpath,
1006 dir_fsp->fsp_name->base_name);
1012 dlen = strlen(d_fullname);
1014 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
1017 if (fsp == dir_fsp) {
1021 d1_fullname = talloc_asprintf(talloc_tos(),
1023 fsp->conn->connectpath,
1024 fsp->fsp_name->base_name);
1027 * If the open file has a path that is a longer
1028 * component, then it's a subpath.
1030 if (strnequal(d_fullname, d1_fullname, dlen) &&
1031 (d1_fullname[dlen] == '/')) {
1032 TALLOC_FREE(d1_fullname);
1033 TALLOC_FREE(d_fullname);
1036 TALLOC_FREE(d1_fullname);
1039 TALLOC_FREE(d_fullname);
1043 /****************************************************************************
1045 ****************************************************************************/
1047 static void fsp_free(files_struct *fsp)
1049 struct smbd_server_connection *sconn = fsp->conn->sconn;
1051 if (fsp == sconn->fsp_fi_cache.fsp) {
1052 ZERO_STRUCT(sconn->fsp_fi_cache);
1055 DLIST_REMOVE(sconn->files, fsp);
1056 SMB_ASSERT(sconn->num_files > 0);
1059 TALLOC_FREE(fsp->fake_file_handle);
1061 if (fh_get_refcount(fsp->fh) == 1) {
1062 TALLOC_FREE(fsp->fh);
1064 size_t new_refcount = fh_get_refcount(fsp->fh) - 1;
1065 fh_set_refcount(fsp->fh, new_refcount);
1068 if (fsp->lease != NULL) {
1069 if (fsp->lease->ref_count == 1) {
1070 TALLOC_FREE(fsp->lease);
1072 fsp->lease->ref_count--;
1076 fsp->conn->num_files_open--;
1078 if (fsp->fsp_name != NULL &&
1079 fsp->fsp_name->fsp_link != NULL)
1082 * Free fsp_link of fsp->fsp_name. To do this in the correct
1083 * talloc destructor order we have to do it here. The
1084 * talloc_free() of the link should set the fsp pointer to NULL.
1086 TALLOC_FREE(fsp->fsp_name->fsp_link);
1087 SMB_ASSERT(fsp->fsp_name->fsp == NULL);
1090 /* this is paranoia, just in case someone tries to reuse the
1094 /* fsp->fsp_name is a talloc child and is free'd automatically. */
1098 void file_free(struct smb_request *req, files_struct *fsp)
1100 struct smbd_server_connection *sconn = fsp->conn->sconn;
1101 uint64_t fnum = fsp->fnum;
1103 if (fsp == fsp->conn->cwd_fsp) {
1108 size_t len = fsp_fullbasepath(fsp, NULL, 0);
1109 char fullpath[len+1];
1111 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
1114 * Avoid /. at the end of the path name. notify can't
1117 if (len > 1 && fullpath[len-1] == '.' &&
1118 fullpath[len-2] == '/') {
1119 fullpath[len-2] = '\0';
1122 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
1123 TALLOC_FREE(fsp->notify);
1126 /* Ensure this event will never fire. */
1127 TALLOC_FREE(fsp->update_write_time_event);
1129 if (fsp->op != NULL) {
1130 fsp->op->compat = NULL;
1132 TALLOC_FREE(fsp->op);
1134 if ((req != NULL) && (fsp == req->chain_fsp)) {
1135 req->chain_fsp = NULL;
1139 * Clear all possible chained fsp
1140 * pointers in the SMB2 request queue.
1142 remove_smb2_chained_fsp(fsp);
1144 /* Drop all remaining extensions. */
1145 vfs_remove_all_fsp_extensions(fsp);
1149 DEBUG(5,("freed files structure %llu (%u used)\n",
1150 (unsigned long long)fnum, (unsigned int)sconn->num_files));
1153 /****************************************************************************
1154 Get an fsp from a packet given a 16 bit fnum.
1155 ****************************************************************************/
1157 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
1159 struct smbXsrv_open *op;
1166 * We should never get here. req==NULL could in theory
1167 * only happen from internal opens with a non-zero
1168 * root_dir_fid. Internal opens just don't do that, at
1169 * least they are not supposed to do so. And if they
1170 * start to do so, they better fake up a smb_request
1171 * from which we get the right smbd_server_conn. While
1172 * this should never happen, let's return NULL here.
1177 if (req->chain_fsp != NULL) {
1178 if (req->chain_fsp->fsp_flags.closing) {
1181 return req->chain_fsp;
1184 if (req->xconn == NULL) {
1188 now = timeval_to_nttime(&req->request_time);
1190 status = smb1srv_open_lookup(req->xconn,
1192 if (!NT_STATUS_IS_OK(status)) {
1201 if (fsp->fsp_flags.closing) {
1205 req->chain_fsp = fsp;
1209 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
1210 uint64_t persistent_id,
1211 uint64_t volatile_id)
1213 struct smbXsrv_open *op;
1216 struct files_struct *fsp;
1218 now = timeval_to_nttime(&smb2req->request_time);
1220 status = smb2srv_open_lookup(smb2req->xconn,
1221 persistent_id, volatile_id,
1223 if (!NT_STATUS_IS_OK(status)) {
1232 if (smb2req->tcon == NULL) {
1236 if (smb2req->tcon->compat != fsp->conn) {
1240 if (smb2req->session == NULL) {
1244 if (smb2req->session->global->session_wire_id != fsp->vuid) {
1248 if (fsp->fsp_flags.closing) {
1255 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
1256 uint64_t persistent_id,
1257 uint64_t volatile_id)
1259 struct files_struct *fsp;
1261 if (smb2req->compat_chain_fsp != NULL) {
1262 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
1265 return smb2req->compat_chain_fsp;
1268 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
1273 smb2req->compat_chain_fsp = fsp;
1277 /****************************************************************************
1278 Duplicate the file handle part for a DOS or FCB open.
1279 ****************************************************************************/
1281 NTSTATUS dup_file_fsp(
1282 struct smb_request *req,
1284 uint32_t access_mask,
1285 uint32_t create_options,
1288 size_t new_refcount;
1290 /* this can never happen for print files */
1291 SMB_ASSERT(from->print_file == NULL);
1293 TALLOC_FREE(to->fh);
1296 new_refcount = fh_get_refcount(to->fh) + 1;
1297 fh_set_refcount(to->fh, new_refcount);
1299 to->file_id = from->file_id;
1300 to->initial_allocation_size = from->initial_allocation_size;
1301 to->file_pid = from->file_pid;
1302 to->vuid = from->vuid;
1303 to->open_time = from->open_time;
1304 to->access_mask = access_mask;
1305 to->oplock_type = from->oplock_type;
1306 to->fsp_flags.can_lock = from->fsp_flags.can_lock;
1307 to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
1308 to->fsp_flags.can_write =
1309 CAN_WRITE(from->conn) &&
1310 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1311 to->fsp_flags.modified = from->fsp_flags.modified;
1312 to->fsp_flags.is_directory = from->fsp_flags.is_directory;
1313 to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
1314 to->fsp_flags.is_fsa = from->fsp_flags.is_fsa;
1315 to->fsp_flags.is_pathref = from->fsp_flags.is_pathref;
1316 to->fsp_flags.have_proc_fds = from->fsp_flags.have_proc_fds;
1317 to->fsp_flags.is_dirfsp = from->fsp_flags.is_dirfsp;
1319 return fsp_set_smb_fname(to, from->fsp_name);
1323 * Return a jenkins hash of a pathname on a connection.
1326 NTSTATUS file_name_hash(connection_struct *conn,
1327 const char *name, uint32_t *p_name_hash)
1329 char tmpbuf[PATH_MAX];
1330 char *fullpath, *to_free;
1334 /* Set the hash of the full pathname. */
1336 len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
1337 &fullpath, &to_free);
1339 return NT_STATUS_NO_MEMORY;
1341 key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
1342 *p_name_hash = tdb_jenkins_hash(&key);
1344 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
1346 (unsigned int)*p_name_hash ));
1348 TALLOC_FREE(to_free);
1349 return NT_STATUS_OK;
1352 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
1353 struct smb_filename **_smb_fname)
1355 struct smb_filename *smb_fname_new = *_smb_fname;
1356 const char *name_str = NULL;
1357 uint32_t name_hash = 0;
1360 name_str = smb_fname_str_dbg(smb_fname_new);
1361 if (name_str == NULL) {
1362 return NT_STATUS_NO_MEMORY;
1365 status = file_name_hash(fsp->conn,
1368 if (!NT_STATUS_IS_OK(status)) {
1372 status = fsp_smb_fname_link(fsp,
1373 &smb_fname_new->fsp_link,
1374 &smb_fname_new->fsp);
1375 if (!NT_STATUS_IS_OK(status)) {
1379 fsp->name_hash = name_hash;
1380 fsp->fsp_name = smb_fname_new;
1382 return NT_STATUS_OK;
1386 * The only way that the fsp->fsp_name field should ever be set.
1388 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
1389 const struct smb_filename *smb_fname_in)
1391 struct smb_filename *smb_fname_old = fsp->fsp_name;
1392 struct smb_filename *smb_fname_new = NULL;
1395 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
1396 if (smb_fname_new == NULL) {
1397 return NT_STATUS_NO_MEMORY;
1400 status = fsp_attach_smb_fname(fsp, &smb_fname_new);
1401 if (!NT_STATUS_IS_OK(status)) {
1402 TALLOC_FREE(smb_fname_new);
1406 if (smb_fname_old != NULL) {
1407 smb_fname_fsp_unlink(smb_fname_old);
1408 TALLOC_FREE(smb_fname_old);
1411 return NT_STATUS_OK;
1414 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
1417 char tmp_buf[1] = {'\0'};
1420 * Don't pass NULL buffer to snprintf (to satisfy static checker)
1421 * Some callers will call this function with NULL for buf and
1422 * 0 for buflen in order to get length of fullbasepath (without
1423 * needing to allocate or write to buf)
1429 len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
1430 fsp->fsp_name->base_name);
1436 void fsp_set_base_fsp(struct files_struct *fsp, struct files_struct *base_fsp)
1438 SMB_ASSERT(fsp->stream_fsp == NULL);
1439 if (base_fsp != NULL) {
1440 SMB_ASSERT(base_fsp->base_fsp == NULL);
1441 SMB_ASSERT(base_fsp->stream_fsp == NULL);
1444 if (fsp->base_fsp != NULL) {
1445 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
1446 fsp->base_fsp->stream_fsp = NULL;
1449 fsp->base_fsp = base_fsp;
1450 if (fsp->base_fsp != NULL) {
1451 fsp->base_fsp->stream_fsp = fsp;