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 file_new(struct smb_request *req, connection_struct *conn,
103 files_struct **result)
105 struct smbd_server_connection *sconn = conn->sconn;
109 status = fsp_new(conn, conn, &fsp);
110 if (!NT_STATUS_IS_OK(status)) {
114 GetTimeOfDay(&fsp->open_time);
117 struct smbXsrv_connection *xconn = req->xconn;
118 struct smbXsrv_open *op = NULL;
119 NTTIME now = timeval_to_nttime(&fsp->open_time);
121 status = smbXsrv_open_create(xconn,
124 if (!NT_STATUS_IS_OK(status)) {
125 file_free(NULL, fsp);
130 fsp->fnum = op->local_id;
132 DEBUG(10, ("%s: req==NULL, INTERNAL_OPEN_ONLY, smbXsrv_open "
133 "allocated\n", __func__));
139 * Create an smb_filename with "" for the base_name. There are very
140 * few NULL checks, so make sure it's initialized with something. to
141 * be safe until an audit can be done.
143 fsp->fsp_name = synthetic_smb_fname(fsp,
149 if (fsp->fsp_name == NULL) {
150 file_free(NULL, fsp);
151 return NT_STATUS_NO_MEMORY;
154 DEBUG(5,("allocated file structure %s (%u used)\n",
155 fsp_fnum_dbg(fsp), (unsigned int)sconn->num_files));
159 req->chain_fsp = fsp;
162 /* A new fsp invalidates the positive and
163 negative fsp_fi_cache as the new fsp is pushed
164 at the start of the list and we search from
165 a cache hit to the *end* of the list. */
167 ZERO_STRUCT(sconn->fsp_fi_cache);
174 * Create an internal fsp for an *existing* directory.
176 * This should only be used by callers in the VFS that need to control the
177 * opening of the directory. Otherwise use open_internal_dirfsp_at().
179 NTSTATUS create_internal_dirfsp(connection_struct *conn,
180 const struct smb_filename *smb_dname,
181 struct files_struct **_fsp)
183 struct files_struct *fsp = NULL;
186 status = file_new(NULL, conn, &fsp);
187 if (!NT_STATUS_IS_OK(status)) {
191 status = fsp_set_smb_fname(fsp, smb_dname);
192 if (!NT_STATUS_IS_OK(status)) {
193 file_free(NULL, fsp);
197 fsp->access_mask = FILE_LIST_DIRECTORY;
198 fsp->fsp_flags.is_directory = true;
199 fsp->fsp_flags.is_dirfsp = true;
206 * Open an internal fsp for an *existing* directory.
208 NTSTATUS open_internal_dirfsp(connection_struct *conn,
209 const struct smb_filename *smb_dname,
211 struct files_struct **_fsp)
213 struct files_struct *fsp = NULL;
217 status = create_internal_dirfsp(conn, smb_dname, &fsp);
218 if (!NT_STATUS_IS_OK(status)) {
223 open_flags |= O_DIRECTORY;
225 status = fd_open(fsp, open_flags, 0);
226 if (!NT_STATUS_IS_OK(status)) {
227 DBG_INFO("Could not open fd for %s (%s)\n",
228 smb_fname_str_dbg(smb_dname),
230 file_free(NULL, fsp);
234 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
236 return map_nt_error_from_unix(errno);
239 if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
240 DBG_ERR("%s is not a directory!\n",
241 smb_fname_str_dbg(smb_dname));
242 file_free(NULL, fsp);
243 return NT_STATUS_NOT_A_DIRECTORY;
246 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
252 /****************************************************************************
253 Close all open files for a connection.
254 ****************************************************************************/
256 void file_close_conn(connection_struct *conn)
258 files_struct *fsp, *next;
260 for (fsp=conn->sconn->files; fsp; fsp=next) {
262 if (fsp->conn != conn) {
265 if (fsp->op != NULL && fsp->op->global->durable) {
267 * A tree disconnect closes a durable handle
269 fsp->op->global->durable = false;
271 close_file(NULL, fsp, SHUTDOWN_CLOSE);
275 /****************************************************************************
276 Initialise file structures.
277 ****************************************************************************/
279 static int files_max_open_fds;
281 bool file_init_global(void)
283 int request_max = lp_max_open_files();
287 if (files_max_open_fds != 0) {
292 * Set the max_open files to be the requested
293 * max plus a fudgefactor to allow for the extra
294 * fd's we need such as log files etc...
296 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
298 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
300 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
301 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
304 if (real_max != request_max) {
305 DEBUG(1, ("file_init_global: Information only: requested %d "
306 "open files, %d are available.\n",
307 request_max, real_max));
310 SMB_ASSERT(real_max > 100);
312 files_max_open_fds = real_max;
316 bool file_init(struct smbd_server_connection *sconn)
320 ok = file_init_global();
325 sconn->real_max_open_files = files_max_open_fds;
330 /****************************************************************************
331 Close files open by a specified vuid.
332 ****************************************************************************/
334 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
336 files_struct *fsp, *next;
338 for (fsp=sconn->files; fsp; fsp=next) {
340 if (fsp->vuid == vuid) {
341 close_file(NULL, fsp, SHUTDOWN_CLOSE);
347 * Walk the files table until "fn" returns non-NULL
350 struct files_struct *files_forall(
351 struct smbd_server_connection *sconn,
352 struct files_struct *(*fn)(struct files_struct *fsp,
356 struct files_struct *fsp, *next;
358 for (fsp = sconn->files; fsp; fsp = next) {
359 struct files_struct *ret;
361 ret = fn(fsp, private_data);
369 /****************************************************************************
370 Find a fsp given a file descriptor.
371 ****************************************************************************/
373 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
378 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
379 if (fsp->fh->fd == fd) {
381 DLIST_PROMOTE(sconn->files, fsp);
390 /****************************************************************************
391 Find a fsp given a device, inode and file_id.
392 ****************************************************************************/
394 files_struct *file_find_dif(struct smbd_server_connection *sconn,
395 struct file_id id, unsigned long gen_id)
404 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
405 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
406 if (file_id_equal(&fsp->file_id, &id) &&
407 fsp->fh->gen_id == gen_id ) {
409 DLIST_PROMOTE(sconn->files, fsp);
411 /* Paranoia check. */
412 if ((fsp->fh->fd == -1) &&
413 (fsp->oplock_type != NO_OPLOCK &&
414 fsp->oplock_type != LEASE_OPLOCK)) {
415 struct file_id_buf idbuf;
416 DEBUG(0,("file_find_dif: file %s file_id = "
417 "%s, gen = %u oplock_type = %u is a "
418 "stat open with oplock type !\n",
420 file_id_str_buf(fsp->file_id, &idbuf),
421 (unsigned int)fsp->fh->gen_id,
422 (unsigned int)fsp->oplock_type ));
423 smb_panic("file_find_dif");
432 /****************************************************************************
433 Find the first fsp given a device and inode.
434 We use a singleton cache here to speed up searching from getfilepathinfo
436 ****************************************************************************/
438 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
443 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
444 /* Positive or negative cache hit. */
445 return sconn->fsp_fi_cache.fsp;
448 sconn->fsp_fi_cache.id = id;
450 for (fsp=sconn->files;fsp;fsp=fsp->next) {
451 if (file_id_equal(&fsp->file_id, &id)) {
452 /* Setup positive cache. */
453 sconn->fsp_fi_cache.fsp = fsp;
458 /* Setup negative cache. */
459 sconn->fsp_fi_cache.fsp = NULL;
463 /****************************************************************************
464 Find the next fsp having the same device and inode.
465 ****************************************************************************/
467 files_struct *file_find_di_next(files_struct *start_fsp)
471 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
472 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
480 struct files_struct *file_find_one_fsp_from_lease_key(
481 struct smbd_server_connection *sconn,
482 const struct smb2_lease_key *lease_key)
484 struct files_struct *fsp;
486 for (fsp = sconn->files; fsp; fsp=fsp->next) {
487 if ((fsp->lease != NULL) &&
488 (fsp->lease->lease.lease_key.data[0] ==
489 lease_key->data[0]) &&
490 (fsp->lease->lease.lease_key.data[1] ==
491 lease_key->data[1])) {
498 /****************************************************************************
499 Find any fsp open with a pathname below that of an already open path.
500 ****************************************************************************/
502 bool file_find_subpath(files_struct *dir_fsp)
506 char *d_fullname = NULL;
508 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
509 dir_fsp->conn->connectpath,
510 dir_fsp->fsp_name->base_name);
516 dlen = strlen(d_fullname);
518 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
521 if (fsp == dir_fsp) {
525 d1_fullname = talloc_asprintf(talloc_tos(),
527 fsp->conn->connectpath,
528 fsp->fsp_name->base_name);
531 * If the open file has a path that is a longer
532 * component, then it's a subpath.
534 if (strnequal(d_fullname, d1_fullname, dlen) &&
535 (d1_fullname[dlen] == '/')) {
536 TALLOC_FREE(d1_fullname);
537 TALLOC_FREE(d_fullname);
540 TALLOC_FREE(d1_fullname);
543 TALLOC_FREE(d_fullname);
547 /****************************************************************************
549 ****************************************************************************/
551 void fsp_free(files_struct *fsp)
553 struct smbd_server_connection *sconn = fsp->conn->sconn;
555 if (fsp == sconn->fsp_fi_cache.fsp) {
556 ZERO_STRUCT(sconn->fsp_fi_cache);
559 DLIST_REMOVE(sconn->files, fsp);
560 SMB_ASSERT(sconn->num_files > 0);
563 TALLOC_FREE(fsp->fake_file_handle);
565 if (fsp->fh->ref_count == 1) {
566 TALLOC_FREE(fsp->fh);
568 fsp->fh->ref_count--;
571 if (fsp->lease != NULL) {
572 if (fsp->lease->ref_count == 1) {
573 TALLOC_FREE(fsp->lease);
575 fsp->lease->ref_count--;
579 fsp->conn->num_files_open--;
581 /* this is paranoia, just in case someone tries to reuse the
585 /* fsp->fsp_name is a talloc child and is free'd automatically. */
589 void file_free(struct smb_request *req, files_struct *fsp)
591 struct smbd_server_connection *sconn = fsp->conn->sconn;
592 uint64_t fnum = fsp->fnum;
595 size_t len = fsp_fullbasepath(fsp, NULL, 0);
596 char fullpath[len+1];
598 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
601 * Avoid /. at the end of the path name. notify can't
604 if (len > 1 && fullpath[len-1] == '.' &&
605 fullpath[len-2] == '/') {
606 fullpath[len-2] = '\0';
609 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
610 TALLOC_FREE(fsp->notify);
613 /* Ensure this event will never fire. */
614 TALLOC_FREE(fsp->update_write_time_event);
616 if (fsp->op != NULL) {
617 fsp->op->compat = NULL;
619 TALLOC_FREE(fsp->op);
621 if ((req != NULL) && (fsp == req->chain_fsp)) {
622 req->chain_fsp = NULL;
626 * Clear all possible chained fsp
627 * pointers in the SMB2 request queue.
629 remove_smb2_chained_fsp(fsp);
631 /* Drop all remaining extensions. */
632 vfs_remove_all_fsp_extensions(fsp);
636 DEBUG(5,("freed files structure %llu (%u used)\n",
637 (unsigned long long)fnum, (unsigned int)sconn->num_files));
640 /****************************************************************************
641 Get an fsp from a packet given a 16 bit fnum.
642 ****************************************************************************/
644 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
646 struct smbXsrv_open *op;
653 * We should never get here. req==NULL could in theory
654 * only happen from internal opens with a non-zero
655 * root_dir_fid. Internal opens just don't do that, at
656 * least they are not supposed to do so. And if they
657 * start to do so, they better fake up a smb_request
658 * from which we get the right smbd_server_conn. While
659 * this should never happen, let's return NULL here.
664 if (req->chain_fsp != NULL) {
665 if (req->chain_fsp->fsp_flags.closing) {
668 return req->chain_fsp;
671 if (req->xconn == NULL) {
675 now = timeval_to_nttime(&req->request_time);
677 status = smb1srv_open_lookup(req->xconn,
679 if (!NT_STATUS_IS_OK(status)) {
688 if (fsp->fsp_flags.closing) {
692 req->chain_fsp = fsp;
696 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
697 uint64_t persistent_id,
698 uint64_t volatile_id)
700 struct smbXsrv_open *op;
703 struct files_struct *fsp;
705 now = timeval_to_nttime(&smb2req->request_time);
707 status = smb2srv_open_lookup(smb2req->xconn,
708 persistent_id, volatile_id,
710 if (!NT_STATUS_IS_OK(status)) {
719 if (smb2req->tcon == NULL) {
723 if (smb2req->tcon->compat != fsp->conn) {
727 if (smb2req->session == NULL) {
731 if (smb2req->session->global->session_wire_id != fsp->vuid) {
735 if (fsp->fsp_flags.closing) {
742 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
743 uint64_t persistent_id,
744 uint64_t volatile_id)
746 struct files_struct *fsp;
748 if (smb2req->compat_chain_fsp != NULL) {
749 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
752 return smb2req->compat_chain_fsp;
755 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
760 smb2req->compat_chain_fsp = fsp;
764 /****************************************************************************
765 Duplicate the file handle part for a DOS or FCB open.
766 ****************************************************************************/
768 NTSTATUS dup_file_fsp(
769 struct smb_request *req,
771 uint32_t access_mask,
772 uint32_t create_options,
775 /* this can never happen for print files */
776 SMB_ASSERT(from->print_file == NULL);
783 to->file_id = from->file_id;
784 to->initial_allocation_size = from->initial_allocation_size;
785 to->file_pid = from->file_pid;
786 to->vuid = from->vuid;
787 to->open_time = from->open_time;
788 to->access_mask = access_mask;
789 to->oplock_type = from->oplock_type;
790 to->fsp_flags.can_lock = from->fsp_flags.can_lock;
791 to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
792 to->fsp_flags.can_write =
793 CAN_WRITE(from->conn) &&
794 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
795 to->fsp_flags.modified = from->fsp_flags.modified;
796 to->fsp_flags.is_directory = from->fsp_flags.is_directory;
797 to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
799 return fsp_set_smb_fname(to, from->fsp_name);
803 * Return a jenkins hash of a pathname on a connection.
806 NTSTATUS file_name_hash(connection_struct *conn,
807 const char *name, uint32_t *p_name_hash)
809 char tmpbuf[PATH_MAX];
810 char *fullpath, *to_free;
814 /* Set the hash of the full pathname. */
816 len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
817 &fullpath, &to_free);
819 return NT_STATUS_NO_MEMORY;
821 key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
822 *p_name_hash = tdb_jenkins_hash(&key);
824 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
826 (unsigned int)*p_name_hash ));
828 TALLOC_FREE(to_free);
833 * The only way that the fsp->fsp_name field should ever be set.
835 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
836 const struct smb_filename *smb_fname_in)
838 struct smb_filename *smb_fname_new;
840 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
841 if (smb_fname_new == NULL) {
842 return NT_STATUS_NO_MEMORY;
845 TALLOC_FREE(fsp->fsp_name);
846 fsp->fsp_name = smb_fname_new;
848 return file_name_hash(fsp->conn,
849 smb_fname_str_dbg(fsp->fsp_name),
853 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
856 char tmp_buf[1] = {'\0'};
859 * Don't pass NULL buffer to snprintf (to satisfy static checker)
860 * Some callers will call this function with NULL for buf and
861 * 0 for buflen in order to get length of fullbasepatch (without
862 * needing to allocate or write to buf)
868 len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
869 fsp->fsp_name->base_name);