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 "smbd/smbXsrv_open.h"
24 #include "libcli/security/security.h"
26 #include "lib/util/bitmap.h"
27 #include "lib/util/strv.h"
29 #define FILE_HANDLE_OFFSET 0x1000
31 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
32 struct smb_filename **_smb_fname);
35 * create new fsp to be used for file_new or a durable handle reconnect
37 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
38 files_struct **result)
40 NTSTATUS status = NT_STATUS_NO_MEMORY;
41 files_struct *fsp = NULL;
42 struct smbd_server_connection *sconn = conn->sconn;
44 fsp = talloc_zero(mem_ctx, struct files_struct);
50 * This can't be a child of fsp because the file_handle can be ref'd
51 * when doing a dos/fcb open, which will then share the file_handle
52 * across multiple fsps.
54 fsp->fh = fd_handle_create(mem_ctx);
55 if (fsp->fh == NULL) {
59 fsp->fsp_flags.use_ofd_locks = !lp_smbd_force_process_locks(SNUM(conn));
60 #ifndef HAVE_OFD_LOCKS
61 fsp->fsp_flags.use_ofd_locks = false;
64 fh_set_refcount(fsp->fh, 1);
67 fsp->fnum = FNUM_FIELD_INVALID;
69 fsp->close_write_time = make_omit_timespec();
71 DLIST_ADD(sconn->files, fsp);
72 sconn->num_files += 1;
74 conn->num_files_open++;
76 DBG_INFO("allocated files structure (%u used)\n",
77 (unsigned int)sconn->num_files);
91 void fsp_set_gen_id(files_struct *fsp)
93 static uint64_t gen_id = 1;
96 * A billion of 64-bit increments per second gives us
97 * more than 500 years of runtime without wrap.
100 fh_set_gen_id(fsp->fh, gen_id);
103 /****************************************************************************
104 Find first available file slot.
105 ****************************************************************************/
107 NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
109 struct smbXsrv_open *op = NULL;
114 DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
118 now = timeval_to_nttime(&fsp->open_time);
120 status = smbXsrv_open_create(req->xconn,
121 fsp->conn->session_info,
124 if (!NT_STATUS_IS_OK(status)) {
129 fsp->fnum = op->local_id;
132 req->chain_fsp = fsp;
134 DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
135 fsp_str_dbg(fsp), fsp->mid);
140 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
141 files_struct **result)
143 struct smbd_server_connection *sconn = conn->sconn;
147 status = fsp_new(conn, conn, &fsp);
148 if (!NT_STATUS_IS_OK(status)) {
152 GetTimeOfDay(&fsp->open_time);
154 status = fsp_bind_smb(fsp, req);
155 if (!NT_STATUS_IS_OK(status)) {
156 file_free(NULL, fsp);
163 * Create an smb_filename with "" for the base_name. There are very
164 * few NULL checks, so make sure it's initialized with something. to
165 * be safe until an audit can be done.
167 fsp->fsp_name = synthetic_smb_fname(fsp,
173 if (fsp->fsp_name == NULL) {
174 file_free(NULL, fsp);
175 return NT_STATUS_NO_MEMORY;
178 DBG_INFO("new file %s\n", fsp_fnum_dbg(fsp));
180 /* A new fsp invalidates the positive and
181 negative fsp_fi_cache as the new fsp is pushed
182 at the start of the list and we search from
183 a cache hit to the *end* of the list. */
185 ZERO_STRUCT(sconn->fsp_fi_cache);
191 NTSTATUS create_internal_fsp(connection_struct *conn,
192 const struct smb_filename *smb_fname,
193 struct files_struct **_fsp)
195 struct files_struct *fsp = NULL;
198 status = file_new(NULL, conn, &fsp);
199 if (!NT_STATUS_IS_OK(status)) {
203 status = fsp_set_smb_fname(fsp, smb_fname);
204 if (!NT_STATUS_IS_OK(status)) {
205 file_free(NULL, fsp);
214 * Create an internal fsp for an *existing* directory.
216 * This should only be used by callers in the VFS that need to control the
217 * opening of the directory. Otherwise use open_internal_dirfsp_at().
219 NTSTATUS create_internal_dirfsp(connection_struct *conn,
220 const struct smb_filename *smb_dname,
221 struct files_struct **_fsp)
223 struct files_struct *fsp = NULL;
226 status = create_internal_fsp(conn, smb_dname, &fsp);
227 if (!NT_STATUS_IS_OK(status)) {
231 fsp->access_mask = FILE_LIST_DIRECTORY;
232 fsp->fsp_flags.is_directory = true;
233 fsp->fsp_flags.is_dirfsp = true;
240 * Open an internal fsp for an *existing* directory.
242 NTSTATUS open_internal_dirfsp(connection_struct *conn,
243 const struct smb_filename *smb_dname,
245 struct files_struct **_fsp)
247 struct files_struct *fsp = NULL;
250 status = create_internal_dirfsp(conn, smb_dname, &fsp);
251 if (!NT_STATUS_IS_OK(status)) {
256 open_flags |= O_DIRECTORY;
258 status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, open_flags, 0);
259 if (!NT_STATUS_IS_OK(status)) {
260 DBG_INFO("Could not open fd for %s (%s)\n",
261 smb_fname_str_dbg(smb_dname),
263 file_free(NULL, fsp);
267 status = vfs_stat_fsp(fsp);
268 if (!NT_STATUS_IS_OK(status)) {
269 file_free(NULL, fsp);
273 if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
274 DBG_ERR("%s is not a directory!\n",
275 smb_fname_str_dbg(smb_dname));
276 file_free(NULL, fsp);
277 return NT_STATUS_NOT_A_DIRECTORY;
280 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
287 * Convert a pathref dirfsp into a real fsp. No need to do any cwd
288 * tricks, we just open ".".
290 NTSTATUS openat_internal_dir_from_pathref(
291 struct files_struct *dirfsp,
293 struct files_struct **_fsp)
295 struct connection_struct *conn = dirfsp->conn;
296 struct smb_filename *smb_dname = dirfsp->fsp_name;
297 struct files_struct *fsp = NULL;
299 struct smb_filename smb_dot = {
301 .flags = smb_dname->flags,
302 .twrp = smb_dname->twrp,
306 status = create_internal_dirfsp(conn, smb_dname, &fsp);
307 if (!NT_STATUS_IS_OK(status)) {
312 * Pointless for opening ".", but you never know...
314 open_flags |= O_NOFOLLOW;
316 status = fd_openat(dirfsp, &smb_dot, fsp, open_flags, 0);
317 if (!NT_STATUS_IS_OK(status)) {
318 DBG_INFO("fd_openat(\"%s\", \".\") failed: %s\n",
321 file_free(NULL, fsp);
325 fsp->fsp_name->st = smb_dname->st;
326 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
332 * The "link" in the name doesn't imply link in the filesystem
333 * sense. It's a object that "links" together an fsp and an smb_fname
334 * and the link allocated as talloc child of an fsp.
336 * The link is created for fsps that openat_pathref_fsp() returns in
337 * smb_fname->fsp. When this fsp is freed by file_free() by some caller
338 * somewhere, the destructor fsp_smb_fname_link_destructor() on the link object
339 * will use the link to reset the reference in smb_fname->fsp that is about to
342 * This prevents smb_fname_internal_fsp_destructor() from seeing dangling fsp
346 struct fsp_smb_fname_link {
347 struct fsp_smb_fname_link **smb_fname_link;
348 struct files_struct **smb_fname_fsp;
351 static int fsp_smb_fname_link_destructor(struct fsp_smb_fname_link *link)
353 if (link->smb_fname_link == NULL) {
357 *link->smb_fname_link = NULL;
358 *link->smb_fname_fsp = NULL;
362 static NTSTATUS fsp_smb_fname_link(struct files_struct *fsp,
363 struct fsp_smb_fname_link **smb_fname_link,
364 struct files_struct **smb_fname_fsp)
366 struct fsp_smb_fname_link *link = NULL;
368 SMB_ASSERT(*smb_fname_link == NULL);
369 SMB_ASSERT(*smb_fname_fsp == NULL);
371 link = talloc_zero(fsp, struct fsp_smb_fname_link);
373 return NT_STATUS_NO_MEMORY;
376 link->smb_fname_link = smb_fname_link;
377 link->smb_fname_fsp = smb_fname_fsp;
378 *smb_fname_link = link;
379 *smb_fname_fsp = fsp;
381 talloc_set_destructor(link, fsp_smb_fname_link_destructor);
386 * Free a link, carefully avoiding to trigger the link destructor
388 static void destroy_fsp_smb_fname_link(struct fsp_smb_fname_link **_link)
390 struct fsp_smb_fname_link *link = *_link;
395 talloc_set_destructor(link, NULL);
401 * Talloc destructor set on an smb_fname set by openat_pathref_fsp() used to
402 * close the embedded smb_fname->fsp.
404 static int smb_fname_fsp_destructor(struct smb_filename *smb_fname)
406 struct files_struct *fsp = smb_fname->fsp;
408 int saved_errno = errno;
410 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
417 if (fsp_is_alternate_stream(fsp)) {
418 struct files_struct *tmp_base_fsp = fsp->base_fsp;
420 fsp_set_base_fsp(fsp, NULL);
422 status = fd_close(tmp_base_fsp);
423 if (!NT_STATUS_IS_OK(status)) {
424 DBG_ERR("Closing fd for fsp [%s] failed: %s. "
425 "Please check your filesystem!!!\n",
426 fsp_str_dbg(fsp), nt_errstr(status));
428 file_free(NULL, tmp_base_fsp);
431 status = fd_close(fsp);
432 if (!NT_STATUS_IS_OK(status)) {
433 DBG_ERR("Closing fd for fsp [%s] failed: %s. "
434 "Please check your filesystem!!!\n",
435 fsp_str_dbg(fsp), nt_errstr(status));
437 file_free(NULL, fsp);
438 smb_fname->fsp = NULL;
444 static NTSTATUS openat_pathref_fullname(
445 struct connection_struct *conn,
446 const struct files_struct *dirfsp,
447 struct files_struct *basefsp,
448 struct smb_filename **full_fname,
449 struct smb_filename *smb_fname)
451 struct files_struct *fsp = NULL;
452 bool have_dirfsp = (dirfsp != NULL);
453 bool have_basefsp = (basefsp != NULL);
456 DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
458 SMB_ASSERT(smb_fname->fsp == NULL);
459 SMB_ASSERT(have_dirfsp != have_basefsp);
461 status = fsp_new(conn, conn, &fsp);
462 if (!NT_STATUS_IS_OK(status)) {
466 GetTimeOfDay(&fsp->open_time);
468 ZERO_STRUCT(conn->sconn->fsp_fi_cache);
470 fsp->fsp_flags.is_pathref = true;
472 status = fsp_attach_smb_fname(fsp, full_fname);
473 if (!NT_STATUS_IS_OK(status)) {
476 fsp_set_base_fsp(fsp, basefsp);
479 dirfsp, smb_fname, fsp, O_RDONLY|O_NONBLOCK, 0);
480 if (!NT_STATUS_IS_OK(status)) {
482 smb_fname->st = fsp->fsp_name->st;
484 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
485 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
486 NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK))
489 * streams_xattr return NT_STATUS_NOT_FOUND for
490 * opens of not yet existing streams.
492 * ELOOP maps to NT_STATUS_OBJECT_PATH_NOT_FOUND
493 * and this will result from a open request from
494 * a POSIX client on a symlink.
496 * NT_STATUS_OBJECT_NAME_NOT_FOUND is the simple
499 * NT_STATUS_STOPPED_ON_SYMLINK is returned when trying
500 * to open a symlink, our callers are not interested in
503 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
509 * fd_openat() has done an FSTAT on the handle
510 * so update the smb_fname stat info with "truth".
513 smb_fname->st = fsp->fsp_name->st;
515 fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
517 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
519 status = fsp_smb_fname_link(fsp,
520 &smb_fname->fsp_link,
522 if (!NT_STATUS_IS_OK(status)) {
526 DBG_DEBUG("fsp [%s]: OK\n", fsp_str_dbg(fsp));
528 talloc_set_destructor(smb_fname, smb_fname_fsp_destructor);
532 DBG_DEBUG("Opening pathref for [%s] failed: %s\n",
533 smb_fname_str_dbg(smb_fname),
536 fsp_set_base_fsp(fsp, NULL);
538 file_free(NULL, fsp);
543 * Open an internal O_PATH based fsp for smb_fname. If O_PATH is not
544 * available, open O_RDONLY as root. Both is done in fd_open() ->
545 * non_widelink_open(), triggered by setting fsp->fsp_flags.is_pathref to
548 NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
549 struct smb_filename *smb_fname)
551 connection_struct *conn = dirfsp->conn;
552 struct smb_filename *full_fname = NULL;
553 struct smb_filename *base_fname = NULL;
556 DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
558 if (smb_fname->fsp != NULL) {
559 /* We already have one for this name. */
560 DBG_DEBUG("smb_fname [%s] already has a pathref fsp.\n",
561 smb_fname_str_dbg(smb_fname));
565 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS) ||
566 !is_named_stream(smb_fname)) {
568 * openat_pathref_fullname() will make "full_fname" a
569 * talloc child of the smb_fname->fsp. Don't use
570 * talloc_tos() to allocate it to avoid making the
571 * talloc stackframe pool long-lived.
573 full_fname = full_path_from_dirfsp_atname(
577 if (full_fname == NULL) {
578 status = NT_STATUS_NO_MEMORY;
581 status = openat_pathref_fullname(
582 conn, dirfsp, NULL, &full_fname, smb_fname);
583 TALLOC_FREE(full_fname);
590 base_fname = cp_smb_filename_nostream(conn, smb_fname);
591 if (base_fname == NULL) {
592 return NT_STATUS_NO_MEMORY;
595 full_fname = full_path_from_dirfsp_atname(
596 conn, /* no talloc_tos(), see comment above */
599 if (full_fname == NULL) {
600 status = NT_STATUS_NO_MEMORY;
604 status = openat_pathref_fullname(
605 conn, dirfsp, NULL, &full_fname, base_fname);
606 TALLOC_FREE(full_fname);
607 if (!NT_STATUS_IS_OK(status)) {
608 DBG_DEBUG("openat_pathref_nostream failed: %s\n",
613 status = open_stream_pathref_fsp(&base_fname->fsp, smb_fname);
614 if (!NT_STATUS_IS_OK(status)) {
615 DBG_DEBUG("open_stream_pathref_fsp failed: %s\n",
620 smb_fname_fsp_unlink(base_fname);
622 TALLOC_FREE(base_fname);
627 * Open a stream given an already opened base_fsp. Avoid
628 * non_widelink_open: This is only valid for the case where we have a
629 * valid non-cwd_fsp dirfsp that we can pass to SMB_VFS_OPENAT()
631 NTSTATUS open_stream_pathref_fsp(
632 struct files_struct **_base_fsp,
633 struct smb_filename *smb_fname)
635 struct files_struct *base_fsp = *_base_fsp;
636 connection_struct *conn = base_fsp->conn;
637 struct smb_filename *base_fname = base_fsp->fsp_name;
638 struct smb_filename *full_fname = NULL;
641 SMB_ASSERT(smb_fname->fsp == NULL);
642 SMB_ASSERT(is_named_stream(smb_fname));
644 full_fname = synthetic_smb_fname(
645 conn, /* no talloc_tos(), this will be long-lived */
646 base_fname->base_name,
647 smb_fname->stream_name,
651 if (full_fname == NULL) {
652 return NT_STATUS_NO_MEMORY;
655 status = openat_pathref_fullname(
656 conn, NULL, base_fsp, &full_fname, smb_fname);
657 TALLOC_FREE(full_fname);
661 static char *path_to_strv(TALLOC_CTX *mem_ctx, const char *path)
663 char *result = talloc_strdup(mem_ctx, path);
665 if (result == NULL) {
668 string_replace(result, '/', '\0');
672 static NTSTATUS readlink_talloc(
674 struct files_struct *dirfsp,
675 struct smb_filename *smb_relname,
683 if (_substitute == NULL) {
687 if (smb_relname == NULL) {
689 * We have a Linux O_PATH handle in dirfsp and want to
690 * read its value, essentially a freadlink
692 smb_relname = synthetic_smb_fname(
693 talloc_tos(), "", NULL, NULL, 0, 0);
694 if (smb_relname == NULL) {
695 DBG_DEBUG("synthetic_smb_fname() failed\n");
696 return NT_STATUS_NO_MEMORY;
700 ret = SMB_VFS_READLINKAT(
701 dirfsp->conn, dirfsp, smb_relname, buf, sizeof(buf));
703 status = map_nt_error_from_unix(errno);
704 DBG_DEBUG("SMB_VFS_READLINKAT() failed: %s\n",
709 if ((size_t)ret == sizeof(buf)) {
711 * Do we need symlink targets >4k?
713 DBG_DEBUG("Got full %zu bytes from readlink, too long\n",
715 return NT_STATUS_BUFFER_OVERFLOW;
718 substitute = talloc_strndup(mem_ctx, buf, ret);
719 if (substitute == NULL) {
720 DBG_DEBUG("talloc_strndup() failed\n");
721 return NT_STATUS_NO_MEMORY;
724 *_substitute = substitute;
728 NTSTATUS openat_pathref_dirfsp_nosymlink(
730 struct connection_struct *conn,
733 struct smb_filename **_smb_fname,
737 struct files_struct *dirfsp = conn->cwd_fsp;
738 struct smb_filename full_fname = {
742 struct smb_filename rel_fname = {
746 struct smb_filename *result = NULL;
747 struct files_struct *fsp = NULL;
748 char *path = NULL, *next = NULL;
749 int flags = O_NOFOLLOW|O_DIRECTORY;
753 DBG_DEBUG("path_in=%s\n", path_in);
755 status = fsp_new(conn, conn, &fsp);
756 if (!NT_STATUS_IS_OK(status)) {
757 DBG_DEBUG("fsp_new() failed: %s\n", nt_errstr(status));
760 fsp->fsp_name = &full_fname;
764 * Add O_PATH manually, doing this by setting
765 * fsp->fsp_flags.is_pathref will make us become_root() in the
766 * non-O_PATH case, which would cause a security problem.
772 * O_SEARCH just checks for the "x" bit. We are traversing
773 * directories, so we don't need the implicit O_RDONLY ("r"
774 * permissions) but only the "x"-permissions requested by
775 * O_SEARCH. We need either O_PATH or O_SEARCH to correctly
776 * function, without either we will incorrectly require also
777 * the "r" bit when traversing the directory hierarchy.
783 full_fname.base_name = talloc_strdup(talloc_tos(), "");
784 if (full_fname.base_name == NULL) {
785 DBG_DEBUG("talloc_strdup() failed\n");
789 path = path_to_strv(talloc_tos(), path_in);
791 DBG_DEBUG("path_to_strv() failed\n");
794 rel_fname.base_name = path;
797 next = strv_next(path, rel_fname.base_name);
799 if (ISDOT(rel_fname.base_name) || ISDOTDOT(rel_fname.base_name)) {
800 DBG_DEBUG("%s contains a dot\n", path_in);
801 status = NT_STATUS_OBJECT_NAME_INVALID;
813 if ((fd == -1) && (errno == ENOENT)) {
814 status = get_real_filename_at(
818 &rel_fname.base_name);
820 if (!NT_STATUS_IS_OK(status)) {
821 DBG_DEBUG("get_real_filename_at failed: %s\n",
835 if ((fd == -1) && (errno == ENOTDIR)) {
836 status = readlink_talloc(
837 mem_ctx, dirfsp, &rel_fname, substitute);
839 if (NT_STATUS_IS_OK(status)) {
841 * readlink_talloc() found a symlink
843 status = NT_STATUS_STOPPED_ON_SYMLINK;
845 if (unparsed != NULL) {
849 size_t parsed = next - path;
850 size_t len = talloc_get_size(path);
851 *unparsed = len - parsed;
856 DBG_DEBUG("readlink_talloc failed: %s\n",
859 * Restore the error status from SMB_VFS_OPENAT()
861 status = NT_STATUS_NOT_A_DIRECTORY;
867 status = map_nt_error_from_unix(errno);
868 DBG_DEBUG("SMB_VFS_OPENAT() failed: %s\n",
874 fsp->fsp_flags.is_directory = true; /* See O_DIRECTORY above */
876 full_fname.base_name = talloc_asprintf_append_buffer(
877 full_fname.base_name,
879 full_fname.base_name[0] == '\0' ? "" : "/",
880 rel_fname.base_name);
882 if (full_fname.base_name == NULL) {
883 DBG_DEBUG("talloc_asprintf_append_buffer() failed\n");
888 struct files_struct *tmp = NULL;
890 if (dirfsp != conn->cwd_fsp) {
897 if (tmp == conn->cwd_fsp) {
898 status = fsp_new(conn, conn, &fsp);
899 if (!NT_STATUS_IS_OK(status)) {
900 DBG_DEBUG("fsp_new() failed: %s\n",
904 fsp->fsp_name = &full_fname;
909 rel_fname.base_name = next;
914 if (dirfsp != conn->cwd_fsp) {
915 dirfsp->fsp_name = NULL;
916 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) != -1);
918 file_free(NULL, dirfsp);
922 fsp->fsp_flags.is_pathref = true;
923 fsp->fsp_name = NULL;
925 status = fsp_set_smb_fname(fsp, &full_fname);
926 if (!NT_STATUS_IS_OK(status)) {
927 DBG_DEBUG("fsp_set_smb_fname() failed: %s\n",
932 status = vfs_stat_fsp(fsp);
933 if (!NT_STATUS_IS_OK(status)) {
934 DBG_DEBUG("vfs_stat_fsp(%s) failed: %s\n",
940 * We must correctly set fsp->file_id as code inside
941 * open.c will use this to check if delete_on_close
942 * has been set on the dirfsp.
944 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
946 result = cp_smb_filename(mem_ctx, fsp->fsp_name);
947 if (result == NULL) {
948 DBG_DEBUG("cp_smb_filename() failed\n");
952 status = fsp_smb_fname_link(fsp,
955 if (!NT_STATUS_IS_OK(status)) {
958 talloc_set_destructor(result, smb_fname_fsp_destructor);
960 *_smb_fname = result;
962 DBG_DEBUG("returning %s\n", smb_fname_str_dbg(result));
967 status = NT_STATUS_NO_MEMORY;
970 if (fsp_get_pathref_fd(fsp) != -1) {
973 file_free(NULL, fsp);
977 if ((dirfsp != NULL) && (dirfsp != conn->cwd_fsp)) {
978 dirfsp->fsp_name = NULL;
979 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) != -1);
981 file_free(NULL, dirfsp);
989 void smb_fname_fsp_unlink(struct smb_filename *smb_fname)
991 talloc_set_destructor(smb_fname, NULL);
992 smb_fname->fsp = NULL;
993 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
997 * Move any existing embedded fsp refs from the src name to the
998 * destination. It's safe to call this on src smb_fname's that have no embedded
1001 NTSTATUS move_smb_fname_fsp_link(struct smb_filename *smb_fname_dst,
1002 struct smb_filename *smb_fname_src)
1007 * The target should always not be linked yet!
1009 SMB_ASSERT(smb_fname_dst->fsp == NULL);
1010 SMB_ASSERT(smb_fname_dst->fsp_link == NULL);
1012 if (smb_fname_src->fsp == NULL) {
1013 return NT_STATUS_OK;
1016 status = fsp_smb_fname_link(smb_fname_src->fsp,
1017 &smb_fname_dst->fsp_link,
1018 &smb_fname_dst->fsp);
1019 if (!NT_STATUS_IS_OK(status)) {
1023 talloc_set_destructor(smb_fname_dst, smb_fname_fsp_destructor);
1025 smb_fname_fsp_unlink(smb_fname_src);
1027 return NT_STATUS_OK;
1031 * Create an smb_fname and open smb_fname->fsp pathref
1033 NTSTATUS synthetic_pathref(TALLOC_CTX *mem_ctx,
1034 struct files_struct *dirfsp,
1035 const char *base_name,
1036 const char *stream_name,
1037 const SMB_STRUCT_STAT *psbuf,
1040 struct smb_filename **_smb_fname)
1042 struct smb_filename *smb_fname = NULL;
1045 smb_fname = synthetic_smb_fname(mem_ctx,
1051 if (smb_fname == NULL) {
1052 return NT_STATUS_NO_MEMORY;
1055 status = openat_pathref_fsp(dirfsp, smb_fname);
1056 if (!NT_STATUS_IS_OK(status)) {
1057 DBG_ERR("opening [%s] failed\n",
1058 smb_fname_str_dbg(smb_fname));
1059 TALLOC_FREE(smb_fname);
1063 *_smb_fname = smb_fname;
1064 return NT_STATUS_OK;
1067 static int atname_destructor(struct smb_filename *smb_fname)
1069 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
1074 * Turn a path into a parent pathref and atname
1076 * This returns the parent pathref in _parent and the name relative to it. If
1077 * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
1078 * pathref as well, ie _atname->fsp will point at the same fsp as
1081 NTSTATUS parent_pathref(TALLOC_CTX *mem_ctx,
1082 struct files_struct *dirfsp,
1083 const struct smb_filename *smb_fname,
1084 struct smb_filename **_parent,
1085 struct smb_filename **_atname)
1087 struct smb_filename *parent = NULL;
1088 struct smb_filename *atname = NULL;
1091 status = SMB_VFS_PARENT_PATHNAME(dirfsp->conn,
1096 if (!NT_STATUS_IS_OK(status)) {
1101 * We know that the parent name must
1102 * exist, and the name has been canonicalized
1103 * even if this was a POSIX pathname.
1104 * Ensure that we follow symlinks for
1105 * the parent. See the torture test
1106 * POSIX-SYMLINK-PARENT for details.
1108 parent->flags &= ~SMB_FILENAME_POSIX_PATH;
1110 status = openat_pathref_fsp(dirfsp, parent);
1111 if (!NT_STATUS_IS_OK(status)) {
1112 TALLOC_FREE(parent);
1116 if (smb_fname->fsp != NULL) {
1117 status = fsp_smb_fname_link(smb_fname->fsp,
1120 if (!NT_STATUS_IS_OK(status)) {
1121 TALLOC_FREE(parent);
1124 talloc_set_destructor(atname, atname_destructor);
1128 return NT_STATUS_OK;
1131 static bool close_file_in_loop(struct files_struct *fsp)
1133 if (fsp_is_alternate_stream(fsp)) {
1135 * This is a stream, it can't be a base
1137 SMB_ASSERT(fsp->stream_fsp == NULL);
1138 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
1141 * Remove the base<->stream link so that
1142 * close_file_free() does not close fsp->base_fsp as
1143 * well. This would destroy walking the linked list of
1146 fsp->base_fsp->stream_fsp = NULL;
1147 fsp->base_fsp = NULL;
1149 close_file_free(NULL, &fsp, SHUTDOWN_CLOSE);
1153 if (fsp->stream_fsp != NULL) {
1155 * This is the base of a stream.
1157 SMB_ASSERT(fsp->stream_fsp->base_fsp == fsp);
1160 * Remove the base<->stream link. This will make fsp
1161 * look like a normal fsp for the next round.
1163 fsp->stream_fsp->base_fsp = NULL;
1164 fsp->stream_fsp = NULL;
1167 * Have us called back a second time. In the second
1168 * round, "fsp" now looks like a normal fsp.
1173 close_file_free(NULL, &fsp, SHUTDOWN_CLOSE);
1177 /****************************************************************************
1178 Close all open files for a connection.
1179 ****************************************************************************/
1181 struct file_close_conn_state {
1182 struct connection_struct *conn;
1183 bool fsp_left_behind;
1186 static struct files_struct *file_close_conn_fn(
1187 struct files_struct *fsp,
1190 struct file_close_conn_state *state = private_data;
1193 if (fsp->conn != state->conn) {
1197 if (fsp->op != NULL && fsp->op->global->durable) {
1199 * A tree disconnect closes a durable handle
1201 fsp->op->global->durable = false;
1204 did_close = close_file_in_loop(fsp);
1206 state->fsp_left_behind = true;
1212 void file_close_conn(connection_struct *conn)
1214 struct file_close_conn_state state = { .conn = conn };
1216 files_forall(conn->sconn, file_close_conn_fn, &state);
1218 if (state.fsp_left_behind) {
1219 state.fsp_left_behind = false;
1220 files_forall(conn->sconn, file_close_conn_fn, &state);
1221 SMB_ASSERT(!state.fsp_left_behind);
1225 /****************************************************************************
1226 Initialise file structures.
1227 ****************************************************************************/
1229 static int files_max_open_fds;
1231 bool file_init_global(void)
1233 int request_max = lp_max_open_files();
1237 if (files_max_open_fds != 0) {
1242 * Set the max_open files to be the requested
1243 * max plus a fudgefactor to allow for the extra
1244 * fd's we need such as log files etc...
1246 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
1248 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
1250 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
1251 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
1254 if (real_max != request_max) {
1255 DEBUG(1, ("file_init_global: Information only: requested %d "
1256 "open files, %d are available.\n",
1257 request_max, real_max));
1260 SMB_ASSERT(real_max > 100);
1262 files_max_open_fds = real_max;
1266 bool file_init(struct smbd_server_connection *sconn)
1270 ok = file_init_global();
1275 sconn->real_max_open_files = files_max_open_fds;
1280 /****************************************************************************
1281 Close files open by a specified vuid.
1282 ****************************************************************************/
1284 struct file_close_user_state {
1286 bool fsp_left_behind;
1289 static struct files_struct *file_close_user_fn(
1290 struct files_struct *fsp,
1293 struct file_close_user_state *state = private_data;
1296 if (fsp->vuid != state->vuid) {
1300 did_close = close_file_in_loop(fsp);
1302 state->fsp_left_behind = true;
1308 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
1310 struct file_close_user_state state = { .vuid = vuid };
1312 files_forall(sconn, file_close_user_fn, &state);
1314 if (state.fsp_left_behind) {
1315 state.fsp_left_behind = false;
1316 files_forall(sconn, file_close_user_fn, &state);
1317 SMB_ASSERT(!state.fsp_left_behind);
1322 * Walk the files table until "fn" returns non-NULL
1325 struct files_struct *files_forall(
1326 struct smbd_server_connection *sconn,
1327 struct files_struct *(*fn)(struct files_struct *fsp,
1328 void *private_data),
1331 struct files_struct *fsp, *next;
1333 for (fsp = sconn->files; fsp; fsp = next) {
1334 struct files_struct *ret;
1336 ret = fn(fsp, private_data);
1344 /****************************************************************************
1345 Find a fsp given a file descriptor.
1346 ****************************************************************************/
1348 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
1353 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
1354 if (fsp_get_pathref_fd(fsp) == fd) {
1356 DLIST_PROMOTE(sconn->files, fsp);
1365 /****************************************************************************
1366 Find a fsp given a device, inode and file_id.
1367 ****************************************************************************/
1369 files_struct *file_find_dif(struct smbd_server_connection *sconn,
1370 struct file_id id, unsigned long gen_id)
1379 for (fsp = sconn->files; fsp; fsp = fsp->next,count++) {
1381 * We can have a fsp->fh->fd == -1 here as it could be a stat
1384 if (!file_id_equal(&fsp->file_id, &id)) {
1387 if (!fsp->fsp_flags.is_fsa) {
1390 if (fh_get_gen_id(fsp->fh) != gen_id) {
1394 DLIST_PROMOTE(sconn->files, fsp);
1396 /* Paranoia check. */
1397 if ((fsp_get_pathref_fd(fsp) == -1) &&
1398 (fsp->oplock_type != NO_OPLOCK &&
1399 fsp->oplock_type != LEASE_OPLOCK))
1401 struct file_id_buf idbuf;
1403 DBG_ERR("file %s file_id = "
1404 "%s, gen = %u oplock_type = %u is a "
1405 "stat open with oplock type !\n",
1407 file_id_str_buf(fsp->file_id, &idbuf),
1408 (unsigned int)fh_get_gen_id(fsp->fh),
1409 (unsigned int)fsp->oplock_type);
1410 smb_panic("file_find_dif");
1418 /****************************************************************************
1419 Find the first fsp given a device and inode.
1420 We use a singleton cache here to speed up searching from getfilepathinfo
1422 ****************************************************************************/
1424 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
1430 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
1431 /* Positive or negative cache hit. */
1432 return sconn->fsp_fi_cache.fsp;
1435 sconn->fsp_fi_cache.id = id;
1437 for (fsp=sconn->files;fsp;fsp=fsp->next) {
1438 if (need_fsa && !fsp->fsp_flags.is_fsa) {
1441 if (file_id_equal(&fsp->file_id, &id)) {
1442 /* Setup positive cache. */
1443 sconn->fsp_fi_cache.fsp = fsp;
1448 /* Setup negative cache. */
1449 sconn->fsp_fi_cache.fsp = NULL;
1453 /****************************************************************************
1454 Find the next fsp having the same device and inode.
1455 ****************************************************************************/
1457 files_struct *file_find_di_next(files_struct *start_fsp,
1462 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
1463 if (need_fsa && !fsp->fsp_flags.is_fsa) {
1466 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
1474 struct files_struct *file_find_one_fsp_from_lease_key(
1475 struct smbd_server_connection *sconn,
1476 const struct smb2_lease_key *lease_key)
1478 struct files_struct *fsp;
1480 for (fsp = sconn->files; fsp; fsp=fsp->next) {
1481 if ((fsp->lease != NULL) &&
1482 (fsp->lease->lease.lease_key.data[0] ==
1483 lease_key->data[0]) &&
1484 (fsp->lease->lease.lease_key.data[1] ==
1485 lease_key->data[1])) {
1492 /****************************************************************************
1493 Find any fsp open with a pathname below that of an already open path.
1494 ****************************************************************************/
1496 bool file_find_subpath(files_struct *dir_fsp)
1500 char *d_fullname = NULL;
1502 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
1503 dir_fsp->conn->connectpath,
1504 dir_fsp->fsp_name->base_name);
1510 dlen = strlen(d_fullname);
1512 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
1515 if (fsp == dir_fsp) {
1519 d1_fullname = talloc_asprintf(talloc_tos(),
1521 fsp->conn->connectpath,
1522 fsp->fsp_name->base_name);
1525 * If the open file has a path that is a longer
1526 * component, then it's a subpath.
1528 if (strnequal(d_fullname, d1_fullname, dlen) &&
1529 (d1_fullname[dlen] == '/')) {
1530 TALLOC_FREE(d1_fullname);
1531 TALLOC_FREE(d_fullname);
1534 TALLOC_FREE(d1_fullname);
1537 TALLOC_FREE(d_fullname);
1541 /****************************************************************************
1543 ****************************************************************************/
1545 static void fsp_free(files_struct *fsp)
1547 struct smbd_server_connection *sconn = fsp->conn->sconn;
1549 if (fsp == sconn->fsp_fi_cache.fsp) {
1550 ZERO_STRUCT(sconn->fsp_fi_cache);
1553 DLIST_REMOVE(sconn->files, fsp);
1554 SMB_ASSERT(sconn->num_files > 0);
1557 TALLOC_FREE(fsp->fake_file_handle);
1559 if (fh_get_refcount(fsp->fh) == 1) {
1560 TALLOC_FREE(fsp->fh);
1562 size_t new_refcount = fh_get_refcount(fsp->fh) - 1;
1563 fh_set_refcount(fsp->fh, new_refcount);
1566 if (fsp->lease != NULL) {
1567 if (fsp->lease->ref_count == 1) {
1568 TALLOC_FREE(fsp->lease);
1570 fsp->lease->ref_count--;
1574 fsp->conn->num_files_open--;
1576 if (fsp->fsp_name != NULL &&
1577 fsp->fsp_name->fsp_link != NULL)
1580 * Free fsp_link of fsp->fsp_name. To do this in the correct
1581 * talloc destructor order we have to do it here. The
1582 * talloc_free() of the link should set the fsp pointer to NULL.
1584 TALLOC_FREE(fsp->fsp_name->fsp_link);
1585 SMB_ASSERT(fsp->fsp_name->fsp == NULL);
1588 /* this is paranoia, just in case someone tries to reuse the
1592 /* fsp->fsp_name is a talloc child and is free'd automatically. */
1597 * Rundown of all smb-related sub-structures of an fsp
1599 void fsp_unbind_smb(struct smb_request *req, files_struct *fsp)
1601 if (fsp == fsp->conn->cwd_fsp) {
1606 size_t len = fsp_fullbasepath(fsp, NULL, 0);
1607 char fullpath[len+1];
1609 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
1612 * Avoid /. at the end of the path name. notify can't
1615 if (len > 1 && fullpath[len-1] == '.' &&
1616 fullpath[len-2] == '/') {
1617 fullpath[len-2] = '\0';
1620 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
1621 TALLOC_FREE(fsp->notify);
1624 /* Ensure this event will never fire. */
1625 TALLOC_FREE(fsp->update_write_time_event);
1627 if (fsp->op != NULL) {
1628 fsp->op->compat = NULL;
1630 TALLOC_FREE(fsp->op);
1632 if ((req != NULL) && (fsp == req->chain_fsp)) {
1633 req->chain_fsp = NULL;
1637 * Clear all possible chained fsp
1638 * pointers in the SMB2 request queue.
1640 remove_smb2_chained_fsp(fsp);
1643 void file_free(struct smb_request *req, files_struct *fsp)
1645 struct smbd_server_connection *sconn = fsp->conn->sconn;
1646 uint64_t fnum = fsp->fnum;
1648 fsp_unbind_smb(req, fsp);
1650 /* Drop all remaining extensions. */
1651 vfs_remove_all_fsp_extensions(fsp);
1655 DBG_INFO("freed files structure %"PRIu64" (%zu used)\n",
1660 /****************************************************************************
1661 Get an fsp from a packet given a 16 bit fnum.
1662 ****************************************************************************/
1664 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
1666 struct smbXsrv_open *op;
1673 * We should never get here. req==NULL could in theory
1674 * only happen from internal opens with a non-zero
1675 * root_dir_fid. Internal opens just don't do that, at
1676 * least they are not supposed to do so. And if they
1677 * start to do so, they better fake up a smb_request
1678 * from which we get the right smbd_server_conn. While
1679 * this should never happen, let's return NULL here.
1684 if (req->chain_fsp != NULL) {
1685 if (req->chain_fsp->fsp_flags.closing) {
1688 return req->chain_fsp;
1691 if (req->xconn == NULL) {
1695 now = timeval_to_nttime(&req->request_time);
1697 status = smb1srv_open_lookup(req->xconn,
1699 if (!NT_STATUS_IS_OK(status)) {
1708 if (fsp->fsp_flags.closing) {
1712 req->chain_fsp = fsp;
1716 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
1717 uint64_t persistent_id,
1718 uint64_t volatile_id)
1720 struct smbXsrv_open *op;
1723 struct files_struct *fsp;
1725 now = timeval_to_nttime(&smb2req->request_time);
1727 status = smb2srv_open_lookup(smb2req->xconn,
1728 persistent_id, volatile_id,
1730 if (!NT_STATUS_IS_OK(status)) {
1739 if (smb2req->tcon == NULL) {
1743 if (smb2req->tcon->compat != fsp->conn) {
1747 if (smb2req->session == NULL) {
1751 if (smb2req->session->global->session_wire_id != fsp->vuid) {
1755 if (fsp->fsp_flags.closing) {
1762 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
1763 uint64_t persistent_id,
1764 uint64_t volatile_id)
1766 struct files_struct *fsp;
1768 if (smb2req->compat_chain_fsp != NULL) {
1769 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
1772 return smb2req->compat_chain_fsp;
1775 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
1780 smb2req->compat_chain_fsp = fsp;
1784 /****************************************************************************
1785 Duplicate the file handle part for a DOS or FCB open.
1786 ****************************************************************************/
1788 NTSTATUS dup_file_fsp(
1790 uint32_t access_mask,
1793 size_t new_refcount;
1795 /* this can never happen for print files */
1796 SMB_ASSERT(from->print_file == NULL);
1798 TALLOC_FREE(to->fh);
1801 new_refcount = fh_get_refcount(to->fh) + 1;
1802 fh_set_refcount(to->fh, new_refcount);
1804 to->file_id = from->file_id;
1805 to->initial_allocation_size = from->initial_allocation_size;
1806 to->file_pid = from->file_pid;
1807 to->vuid = from->vuid;
1808 to->open_time = from->open_time;
1809 to->access_mask = access_mask;
1810 to->oplock_type = from->oplock_type;
1811 to->fsp_flags.can_lock = from->fsp_flags.can_lock;
1812 to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
1813 to->fsp_flags.can_write =
1814 CAN_WRITE(from->conn) &&
1815 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1816 to->fsp_flags.modified = from->fsp_flags.modified;
1817 to->fsp_flags.is_directory = from->fsp_flags.is_directory;
1818 to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
1819 to->fsp_flags.is_fsa = from->fsp_flags.is_fsa;
1820 to->fsp_flags.is_pathref = from->fsp_flags.is_pathref;
1821 to->fsp_flags.have_proc_fds = from->fsp_flags.have_proc_fds;
1822 to->fsp_flags.is_dirfsp = from->fsp_flags.is_dirfsp;
1824 return fsp_set_smb_fname(to, from->fsp_name);
1828 * Return a jenkins hash of a pathname on a connection.
1831 NTSTATUS file_name_hash(connection_struct *conn,
1832 const char *name, uint32_t *p_name_hash)
1834 char tmpbuf[PATH_MAX];
1835 char *fullpath, *to_free;
1839 /* Set the hash of the full pathname. */
1841 if (name[0] == '/') {
1842 strlcpy(tmpbuf, name, sizeof(tmpbuf));
1844 len = strlen(fullpath);
1847 len = full_path_tos(conn->connectpath,
1855 return NT_STATUS_NO_MEMORY;
1857 key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
1858 *p_name_hash = tdb_jenkins_hash(&key);
1860 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
1862 (unsigned int)*p_name_hash ));
1864 TALLOC_FREE(to_free);
1865 return NT_STATUS_OK;
1868 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
1869 struct smb_filename **_smb_fname)
1871 struct smb_filename *smb_fname_new = talloc_move(fsp, _smb_fname);
1872 const char *name_str = NULL;
1873 uint32_t name_hash = 0;
1876 name_str = smb_fname_str_dbg(smb_fname_new);
1877 if (name_str == NULL) {
1878 return NT_STATUS_NO_MEMORY;
1881 status = file_name_hash(fsp->conn,
1884 if (!NT_STATUS_IS_OK(status)) {
1888 status = fsp_smb_fname_link(fsp,
1889 &smb_fname_new->fsp_link,
1890 &smb_fname_new->fsp);
1891 if (!NT_STATUS_IS_OK(status)) {
1895 fsp->name_hash = name_hash;
1896 fsp->fsp_name = smb_fname_new;
1898 return NT_STATUS_OK;
1902 * The only way that the fsp->fsp_name field should ever be set.
1904 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
1905 const struct smb_filename *smb_fname_in)
1907 struct smb_filename *smb_fname_old = fsp->fsp_name;
1908 struct smb_filename *smb_fname_new = NULL;
1911 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
1912 if (smb_fname_new == NULL) {
1913 return NT_STATUS_NO_MEMORY;
1916 status = fsp_attach_smb_fname(fsp, &smb_fname_new);
1917 if (!NT_STATUS_IS_OK(status)) {
1918 TALLOC_FREE(smb_fname_new);
1922 if (smb_fname_old != NULL) {
1923 smb_fname_fsp_unlink(smb_fname_old);
1924 TALLOC_FREE(smb_fname_old);
1927 return NT_STATUS_OK;
1930 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
1933 char tmp_buf[1] = {'\0'};
1936 * Don't pass NULL buffer to snprintf (to satisfy static checker)
1937 * Some callers will call this function with NULL for buf and
1938 * 0 for buflen in order to get length of fullbasepath (without
1939 * needing to allocate or write to buf)
1943 SMB_ASSERT(buflen==0);
1946 len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
1947 fsp->fsp_name->base_name);
1953 void fsp_set_base_fsp(struct files_struct *fsp, struct files_struct *base_fsp)
1955 SMB_ASSERT(fsp->stream_fsp == NULL);
1956 if (base_fsp != NULL) {
1957 SMB_ASSERT(base_fsp->base_fsp == NULL);
1958 SMB_ASSERT(base_fsp->stream_fsp == NULL);
1961 if (fsp->base_fsp != NULL) {
1962 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
1963 fsp->base_fsp->stream_fsp = NULL;
1966 fsp->base_fsp = base_fsp;
1967 if (fsp->base_fsp != NULL) {
1968 fsp->base_fsp->stream_fsp = fsp;
1972 bool fsp_is_alternate_stream(const struct files_struct *fsp)
1974 return (fsp->base_fsp != NULL);
1977 struct files_struct *metadata_fsp(struct files_struct *fsp)
1979 if (fsp_is_alternate_stream(fsp)) {
1980 return fsp->base_fsp;