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
30 * create new fsp to be used for file_new or a durable handle reconnect
32 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
33 files_struct **result)
35 NTSTATUS status = NT_STATUS_NO_MEMORY;
36 files_struct *fsp = NULL;
37 struct smbd_server_connection *sconn = conn->sconn;
39 fsp = talloc_zero(mem_ctx, struct files_struct);
45 * This can't be a child of fsp because the file_handle can be ref'd
46 * when doing a dos/fcb open, which will then share the file_handle
47 * across multiple fsps.
49 fsp->fh = talloc_zero(mem_ctx, struct fd_handle);
50 if (fsp->fh == NULL) {
54 #if defined(HAVE_OFD_LOCKS)
55 fsp->fsp_flags.use_ofd_locks = true;
56 if (lp_parm_bool(SNUM(conn),
58 "force process locks",
60 fsp->fsp_flags.use_ofd_locks = false;
63 fsp->fh->ref_count = 1;
66 fsp->fnum = FNUM_FIELD_INVALID;
68 fsp->close_write_time = make_omit_timespec();
70 DLIST_ADD(sconn->files, fsp);
71 sconn->num_files += 1;
73 conn->num_files_open++;
87 void fsp_set_gen_id(files_struct *fsp)
89 static uint64_t gen_id = 1;
92 * A billion of 64-bit increments per second gives us
93 * more than 500 years of runtime without wrap.
95 fsp->fh->gen_id = gen_id++;
98 /****************************************************************************
99 Find first available file slot.
100 ****************************************************************************/
102 NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
104 struct smbXsrv_open *op = NULL;
109 DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
113 now = timeval_to_nttime(&fsp->open_time);
115 status = smbXsrv_open_create(req->xconn,
116 fsp->conn->session_info,
119 if (!NT_STATUS_IS_OK(status)) {
124 fsp->fnum = op->local_id;
127 req->chain_fsp = fsp;
129 DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
130 fsp_str_dbg(fsp), fsp->mid);
135 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
136 files_struct **result)
138 struct smbd_server_connection *sconn = conn->sconn;
142 status = fsp_new(conn, conn, &fsp);
143 if (!NT_STATUS_IS_OK(status)) {
147 GetTimeOfDay(&fsp->open_time);
149 status = fsp_bind_smb(fsp, req);
150 if (!NT_STATUS_IS_OK(status)) {
151 file_free(NULL, fsp);
158 * Create an smb_filename with "" for the base_name. There are very
159 * few NULL checks, so make sure it's initialized with something. to
160 * be safe until an audit can be done.
162 fsp->fsp_name = synthetic_smb_fname(fsp,
168 if (fsp->fsp_name == NULL) {
169 file_free(NULL, fsp);
170 return NT_STATUS_NO_MEMORY;
173 DEBUG(5,("allocated file structure %s (%u used)\n",
174 fsp_fnum_dbg(fsp), (unsigned int)sconn->num_files));
176 /* A new fsp invalidates the positive and
177 negative fsp_fi_cache as the new fsp is pushed
178 at the start of the list and we search from
179 a cache hit to the *end* of the list. */
181 ZERO_STRUCT(sconn->fsp_fi_cache);
188 * Create an internal fsp for an *existing* directory.
190 * This should only be used by callers in the VFS that need to control the
191 * opening of the directory. Otherwise use open_internal_dirfsp_at().
193 NTSTATUS create_internal_dirfsp(connection_struct *conn,
194 const struct smb_filename *smb_dname,
195 struct files_struct **_fsp)
197 struct files_struct *fsp = NULL;
200 status = file_new(NULL, conn, &fsp);
201 if (!NT_STATUS_IS_OK(status)) {
205 status = fsp_set_smb_fname(fsp, smb_dname);
206 if (!NT_STATUS_IS_OK(status)) {
207 file_free(NULL, fsp);
211 fsp->access_mask = FILE_LIST_DIRECTORY;
212 fsp->fsp_flags.is_directory = true;
213 fsp->fsp_flags.is_dirfsp = true;
220 * Open an internal fsp for an *existing* directory.
222 NTSTATUS open_internal_dirfsp(connection_struct *conn,
223 const struct smb_filename *smb_dname,
225 struct files_struct **_fsp)
227 struct files_struct *fsp = NULL;
231 status = create_internal_dirfsp(conn, smb_dname, &fsp);
232 if (!NT_STATUS_IS_OK(status)) {
237 open_flags |= O_DIRECTORY;
239 status = fd_open(fsp, open_flags, 0);
240 if (!NT_STATUS_IS_OK(status)) {
241 DBG_INFO("Could not open fd for %s (%s)\n",
242 smb_fname_str_dbg(smb_dname),
244 file_free(NULL, fsp);
248 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
250 return map_nt_error_from_unix(errno);
253 if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
254 DBG_ERR("%s is not a directory!\n",
255 smb_fname_str_dbg(smb_dname));
256 file_free(NULL, fsp);
257 return NT_STATUS_NOT_A_DIRECTORY;
260 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
266 /****************************************************************************
267 Close all open files for a connection.
268 ****************************************************************************/
270 void file_close_conn(connection_struct *conn)
272 files_struct *fsp, *next;
274 for (fsp=conn->sconn->files; fsp; fsp=next) {
276 if (fsp->conn != conn) {
279 if (fsp->op != NULL && fsp->op->global->durable) {
281 * A tree disconnect closes a durable handle
283 fsp->op->global->durable = false;
285 close_file(NULL, fsp, SHUTDOWN_CLOSE);
289 /****************************************************************************
290 Initialise file structures.
291 ****************************************************************************/
293 static int files_max_open_fds;
295 bool file_init_global(void)
297 int request_max = lp_max_open_files();
301 if (files_max_open_fds != 0) {
306 * Set the max_open files to be the requested
307 * max plus a fudgefactor to allow for the extra
308 * fd's we need such as log files etc...
310 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
312 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
314 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
315 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
318 if (real_max != request_max) {
319 DEBUG(1, ("file_init_global: Information only: requested %d "
320 "open files, %d are available.\n",
321 request_max, real_max));
324 SMB_ASSERT(real_max > 100);
326 files_max_open_fds = real_max;
330 bool file_init(struct smbd_server_connection *sconn)
334 ok = file_init_global();
339 sconn->real_max_open_files = files_max_open_fds;
344 /****************************************************************************
345 Close files open by a specified vuid.
346 ****************************************************************************/
348 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
350 files_struct *fsp, *next;
352 for (fsp=sconn->files; fsp; fsp=next) {
354 if (fsp->vuid == vuid) {
355 close_file(NULL, fsp, SHUTDOWN_CLOSE);
361 * Walk the files table until "fn" returns non-NULL
364 struct files_struct *files_forall(
365 struct smbd_server_connection *sconn,
366 struct files_struct *(*fn)(struct files_struct *fsp,
370 struct files_struct *fsp, *next;
372 for (fsp = sconn->files; fsp; fsp = next) {
373 struct files_struct *ret;
375 ret = fn(fsp, private_data);
383 /****************************************************************************
384 Find a fsp given a file descriptor.
385 ****************************************************************************/
387 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
392 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
393 if (fsp->fh->fd == fd) {
395 DLIST_PROMOTE(sconn->files, fsp);
404 /****************************************************************************
405 Find a fsp given a device, inode and file_id.
406 ****************************************************************************/
408 files_struct *file_find_dif(struct smbd_server_connection *sconn,
409 struct file_id id, unsigned long gen_id)
418 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
419 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
420 if (file_id_equal(&fsp->file_id, &id) &&
421 fsp->fh->gen_id == gen_id ) {
423 DLIST_PROMOTE(sconn->files, fsp);
425 /* Paranoia check. */
426 if ((fsp->fh->fd == -1) &&
427 (fsp->oplock_type != NO_OPLOCK &&
428 fsp->oplock_type != LEASE_OPLOCK)) {
429 struct file_id_buf idbuf;
430 DEBUG(0,("file_find_dif: file %s file_id = "
431 "%s, gen = %u oplock_type = %u is a "
432 "stat open with oplock type !\n",
434 file_id_str_buf(fsp->file_id, &idbuf),
435 (unsigned int)fsp->fh->gen_id,
436 (unsigned int)fsp->oplock_type ));
437 smb_panic("file_find_dif");
446 /****************************************************************************
447 Find the first fsp given a device and inode.
448 We use a singleton cache here to speed up searching from getfilepathinfo
450 ****************************************************************************/
452 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
457 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
458 /* Positive or negative cache hit. */
459 return sconn->fsp_fi_cache.fsp;
462 sconn->fsp_fi_cache.id = id;
464 for (fsp=sconn->files;fsp;fsp=fsp->next) {
465 if (file_id_equal(&fsp->file_id, &id)) {
466 /* Setup positive cache. */
467 sconn->fsp_fi_cache.fsp = fsp;
472 /* Setup negative cache. */
473 sconn->fsp_fi_cache.fsp = NULL;
477 /****************************************************************************
478 Find the next fsp having the same device and inode.
479 ****************************************************************************/
481 files_struct *file_find_di_next(files_struct *start_fsp)
485 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
486 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
494 struct files_struct *file_find_one_fsp_from_lease_key(
495 struct smbd_server_connection *sconn,
496 const struct smb2_lease_key *lease_key)
498 struct files_struct *fsp;
500 for (fsp = sconn->files; fsp; fsp=fsp->next) {
501 if ((fsp->lease != NULL) &&
502 (fsp->lease->lease.lease_key.data[0] ==
503 lease_key->data[0]) &&
504 (fsp->lease->lease.lease_key.data[1] ==
505 lease_key->data[1])) {
512 /****************************************************************************
513 Find any fsp open with a pathname below that of an already open path.
514 ****************************************************************************/
516 bool file_find_subpath(files_struct *dir_fsp)
520 char *d_fullname = NULL;
522 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
523 dir_fsp->conn->connectpath,
524 dir_fsp->fsp_name->base_name);
530 dlen = strlen(d_fullname);
532 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
535 if (fsp == dir_fsp) {
539 d1_fullname = talloc_asprintf(talloc_tos(),
541 fsp->conn->connectpath,
542 fsp->fsp_name->base_name);
545 * If the open file has a path that is a longer
546 * component, then it's a subpath.
548 if (strnequal(d_fullname, d1_fullname, dlen) &&
549 (d1_fullname[dlen] == '/')) {
550 TALLOC_FREE(d1_fullname);
551 TALLOC_FREE(d_fullname);
554 TALLOC_FREE(d1_fullname);
557 TALLOC_FREE(d_fullname);
561 /****************************************************************************
563 ****************************************************************************/
565 void fsp_free(files_struct *fsp)
567 struct smbd_server_connection *sconn = fsp->conn->sconn;
569 if (fsp == sconn->fsp_fi_cache.fsp) {
570 ZERO_STRUCT(sconn->fsp_fi_cache);
573 DLIST_REMOVE(sconn->files, fsp);
574 SMB_ASSERT(sconn->num_files > 0);
577 TALLOC_FREE(fsp->fake_file_handle);
579 if (fsp->fh->ref_count == 1) {
580 TALLOC_FREE(fsp->fh);
582 fsp->fh->ref_count--;
585 if (fsp->lease != NULL) {
586 if (fsp->lease->ref_count == 1) {
587 TALLOC_FREE(fsp->lease);
589 fsp->lease->ref_count--;
593 fsp->conn->num_files_open--;
595 /* this is paranoia, just in case someone tries to reuse the
599 /* fsp->fsp_name is a talloc child and is free'd automatically. */
603 void file_free(struct smb_request *req, files_struct *fsp)
605 struct smbd_server_connection *sconn = fsp->conn->sconn;
606 uint64_t fnum = fsp->fnum;
608 if (fsp == fsp->conn->cwd_fsp) {
613 size_t len = fsp_fullbasepath(fsp, NULL, 0);
614 char fullpath[len+1];
616 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
619 * Avoid /. at the end of the path name. notify can't
622 if (len > 1 && fullpath[len-1] == '.' &&
623 fullpath[len-2] == '/') {
624 fullpath[len-2] = '\0';
627 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
628 TALLOC_FREE(fsp->notify);
631 /* Ensure this event will never fire. */
632 TALLOC_FREE(fsp->update_write_time_event);
634 if (fsp->op != NULL) {
635 fsp->op->compat = NULL;
637 TALLOC_FREE(fsp->op);
639 if ((req != NULL) && (fsp == req->chain_fsp)) {
640 req->chain_fsp = NULL;
644 * Clear all possible chained fsp
645 * pointers in the SMB2 request queue.
647 remove_smb2_chained_fsp(fsp);
649 /* Drop all remaining extensions. */
650 vfs_remove_all_fsp_extensions(fsp);
654 DEBUG(5,("freed files structure %llu (%u used)\n",
655 (unsigned long long)fnum, (unsigned int)sconn->num_files));
658 /****************************************************************************
659 Get an fsp from a packet given a 16 bit fnum.
660 ****************************************************************************/
662 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
664 struct smbXsrv_open *op;
671 * We should never get here. req==NULL could in theory
672 * only happen from internal opens with a non-zero
673 * root_dir_fid. Internal opens just don't do that, at
674 * least they are not supposed to do so. And if they
675 * start to do so, they better fake up a smb_request
676 * from which we get the right smbd_server_conn. While
677 * this should never happen, let's return NULL here.
682 if (req->chain_fsp != NULL) {
683 if (req->chain_fsp->fsp_flags.closing) {
686 return req->chain_fsp;
689 if (req->xconn == NULL) {
693 now = timeval_to_nttime(&req->request_time);
695 status = smb1srv_open_lookup(req->xconn,
697 if (!NT_STATUS_IS_OK(status)) {
706 if (fsp->fsp_flags.closing) {
710 req->chain_fsp = fsp;
714 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
715 uint64_t persistent_id,
716 uint64_t volatile_id)
718 struct smbXsrv_open *op;
721 struct files_struct *fsp;
723 now = timeval_to_nttime(&smb2req->request_time);
725 status = smb2srv_open_lookup(smb2req->xconn,
726 persistent_id, volatile_id,
728 if (!NT_STATUS_IS_OK(status)) {
737 if (smb2req->tcon == NULL) {
741 if (smb2req->tcon->compat != fsp->conn) {
745 if (smb2req->session == NULL) {
749 if (smb2req->session->global->session_wire_id != fsp->vuid) {
753 if (fsp->fsp_flags.closing) {
760 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
761 uint64_t persistent_id,
762 uint64_t volatile_id)
764 struct files_struct *fsp;
766 if (smb2req->compat_chain_fsp != NULL) {
767 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
770 return smb2req->compat_chain_fsp;
773 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
778 smb2req->compat_chain_fsp = fsp;
782 /****************************************************************************
783 Duplicate the file handle part for a DOS or FCB open.
784 ****************************************************************************/
786 NTSTATUS dup_file_fsp(
787 struct smb_request *req,
789 uint32_t access_mask,
790 uint32_t create_options,
793 /* this can never happen for print files */
794 SMB_ASSERT(from->print_file == NULL);
801 to->file_id = from->file_id;
802 to->initial_allocation_size = from->initial_allocation_size;
803 to->file_pid = from->file_pid;
804 to->vuid = from->vuid;
805 to->open_time = from->open_time;
806 to->access_mask = access_mask;
807 to->oplock_type = from->oplock_type;
808 to->fsp_flags.can_lock = from->fsp_flags.can_lock;
809 to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
810 to->fsp_flags.can_write =
811 CAN_WRITE(from->conn) &&
812 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
813 to->fsp_flags.modified = from->fsp_flags.modified;
814 to->fsp_flags.is_directory = from->fsp_flags.is_directory;
815 to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
817 return fsp_set_smb_fname(to, from->fsp_name);
821 * Return a jenkins hash of a pathname on a connection.
824 NTSTATUS file_name_hash(connection_struct *conn,
825 const char *name, uint32_t *p_name_hash)
827 char tmpbuf[PATH_MAX];
828 char *fullpath, *to_free;
832 /* Set the hash of the full pathname. */
834 len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
835 &fullpath, &to_free);
837 return NT_STATUS_NO_MEMORY;
839 key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
840 *p_name_hash = tdb_jenkins_hash(&key);
842 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
844 (unsigned int)*p_name_hash ));
846 TALLOC_FREE(to_free);
851 * The only way that the fsp->fsp_name field should ever be set.
853 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
854 const struct smb_filename *smb_fname_in)
856 struct smb_filename *smb_fname_new;
858 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
859 if (smb_fname_new == NULL) {
860 return NT_STATUS_NO_MEMORY;
863 TALLOC_FREE(fsp->fsp_name);
864 fsp->fsp_name = smb_fname_new;
866 return file_name_hash(fsp->conn,
867 smb_fname_str_dbg(fsp->fsp_name),
871 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
874 char tmp_buf[1] = {'\0'};
877 * Don't pass NULL buffer to snprintf (to satisfy static checker)
878 * Some callers will call this function with NULL for buf and
879 * 0 for buflen in order to get length of fullbasepatch (without
880 * needing to allocate or write to buf)
886 len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
887 fsp->fsp_name->base_name);