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
->use_ofd_locks
= true;
56 if (lp_parm_bool(SNUM(conn
),
58 "force process locks",
60 fsp
->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
, "", NULL
, NULL
, 0);
144 if (fsp
->fsp_name
== NULL
) {
145 file_free(NULL
, fsp
);
146 return NT_STATUS_NO_MEMORY
;
149 DEBUG(5,("allocated file structure %s (%u used)\n",
150 fsp_fnum_dbg(fsp
), (unsigned int)sconn
->num_files
));
154 req
->chain_fsp
= fsp
;
157 /* A new fsp invalidates the positive and
158 negative fsp_fi_cache as the new fsp is pushed
159 at the start of the list and we search from
160 a cache hit to the *end* of the list. */
162 ZERO_STRUCT(sconn
->fsp_fi_cache
);
168 /****************************************************************************
169 Close all open files for a connection.
170 ****************************************************************************/
172 void file_close_conn(connection_struct
*conn
)
174 files_struct
*fsp
, *next
;
176 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
178 if (fsp
->conn
!= conn
) {
181 if (fsp
->op
!= NULL
&& fsp
->op
->global
->durable
) {
183 * A tree disconnect closes a durable handle
185 fsp
->op
->global
->durable
= false;
187 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
191 /****************************************************************************
192 Close all open files for a pid and a vuid.
193 ****************************************************************************/
195 void file_close_pid(struct smbd_server_connection
*sconn
, uint16_t smbpid
,
198 files_struct
*fsp
, *next
;
200 for (fsp
=sconn
->files
;fsp
;fsp
=next
) {
202 if ((fsp
->file_pid
== smbpid
) && (fsp
->vuid
== vuid
)) {
203 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
208 /****************************************************************************
209 Initialise file structures.
210 ****************************************************************************/
212 static int files_max_open_fds
;
214 bool file_init_global(void)
216 int request_max
= lp_max_open_files();
220 if (files_max_open_fds
!= 0) {
225 * Set the max_open files to be the requested
226 * max plus a fudgefactor to allow for the extra
227 * fd's we need such as log files etc...
229 real_lim
= set_maxfiles(request_max
+ MAX_OPEN_FUDGEFACTOR
);
231 real_max
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
233 if (real_max
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
> 65536) {
234 real_max
= 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
237 if (real_max
!= request_max
) {
238 DEBUG(1, ("file_init_global: Information only: requested %d "
239 "open files, %d are available.\n",
240 request_max
, real_max
));
243 SMB_ASSERT(real_max
> 100);
245 files_max_open_fds
= real_max
;
249 bool file_init(struct smbd_server_connection
*sconn
)
253 ok
= file_init_global();
258 sconn
->real_max_open_files
= files_max_open_fds
;
263 /****************************************************************************
264 Close files open by a specified vuid.
265 ****************************************************************************/
267 void file_close_user(struct smbd_server_connection
*sconn
, uint64_t vuid
)
269 files_struct
*fsp
, *next
;
271 for (fsp
=sconn
->files
; fsp
; fsp
=next
) {
273 if (fsp
->vuid
== vuid
) {
274 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
280 * Walk the files table until "fn" returns non-NULL
283 struct files_struct
*files_forall(
284 struct smbd_server_connection
*sconn
,
285 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
289 struct files_struct
*fsp
, *next
;
291 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
292 struct files_struct
*ret
;
294 ret
= fn(fsp
, private_data
);
302 /****************************************************************************
303 Find a fsp given a file descriptor.
304 ****************************************************************************/
306 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
311 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
312 if (fsp
->fh
->fd
== fd
) {
314 DLIST_PROMOTE(sconn
->files
, fsp
);
323 /****************************************************************************
324 Find a fsp given a device, inode and file_id.
325 ****************************************************************************/
327 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
328 struct file_id id
, unsigned long gen_id
)
337 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
338 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
339 if (file_id_equal(&fsp
->file_id
, &id
) &&
340 fsp
->fh
->gen_id
== gen_id
) {
342 DLIST_PROMOTE(sconn
->files
, fsp
);
344 /* Paranoia check. */
345 if ((fsp
->fh
->fd
== -1) &&
346 (fsp
->oplock_type
!= NO_OPLOCK
&&
347 fsp
->oplock_type
!= LEASE_OPLOCK
)) {
348 struct file_id_buf idbuf
;
349 DEBUG(0,("file_find_dif: file %s file_id = "
350 "%s, gen = %u oplock_type = %u is a "
351 "stat open with oplock type !\n",
353 file_id_str_buf(fsp
->file_id
, &idbuf
),
354 (unsigned int)fsp
->fh
->gen_id
,
355 (unsigned int)fsp
->oplock_type
));
356 smb_panic("file_find_dif");
365 /****************************************************************************
366 Find the first fsp given a device and inode.
367 We use a singleton cache here to speed up searching from getfilepathinfo
369 ****************************************************************************/
371 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
376 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
377 /* Positive or negative cache hit. */
378 return sconn
->fsp_fi_cache
.fsp
;
381 sconn
->fsp_fi_cache
.id
= id
;
383 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
384 if (file_id_equal(&fsp
->file_id
, &id
)) {
385 /* Setup positive cache. */
386 sconn
->fsp_fi_cache
.fsp
= fsp
;
391 /* Setup negative cache. */
392 sconn
->fsp_fi_cache
.fsp
= NULL
;
396 /****************************************************************************
397 Find the next fsp having the same device and inode.
398 ****************************************************************************/
400 files_struct
*file_find_di_next(files_struct
*start_fsp
)
404 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
405 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
413 struct files_struct
*file_find_one_fsp_from_lease_key(
414 struct smbd_server_connection
*sconn
,
415 const struct smb2_lease_key
*lease_key
)
417 struct files_struct
*fsp
;
419 for (fsp
= sconn
->files
; fsp
; fsp
=fsp
->next
) {
420 if ((fsp
->lease
!= NULL
) &&
421 (fsp
->lease
->lease
.lease_key
.data
[0] ==
422 lease_key
->data
[0]) &&
423 (fsp
->lease
->lease
.lease_key
.data
[1] ==
424 lease_key
->data
[1])) {
431 /****************************************************************************
432 Find any fsp open with a pathname below that of an already open path.
433 ****************************************************************************/
435 bool file_find_subpath(files_struct
*dir_fsp
)
439 char *d_fullname
= NULL
;
441 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
442 dir_fsp
->conn
->connectpath
,
443 dir_fsp
->fsp_name
->base_name
);
449 dlen
= strlen(d_fullname
);
451 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
454 if (fsp
== dir_fsp
) {
458 d1_fullname
= talloc_asprintf(talloc_tos(),
460 fsp
->conn
->connectpath
,
461 fsp
->fsp_name
->base_name
);
464 * If the open file has a path that is a longer
465 * component, then it's a subpath.
467 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
468 (d1_fullname
[dlen
] == '/')) {
469 TALLOC_FREE(d1_fullname
);
470 TALLOC_FREE(d_fullname
);
473 TALLOC_FREE(d1_fullname
);
476 TALLOC_FREE(d_fullname
);
480 /****************************************************************************
482 ****************************************************************************/
484 void fsp_free(files_struct
*fsp
)
486 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
488 if (fsp
== sconn
->fsp_fi_cache
.fsp
) {
489 ZERO_STRUCT(sconn
->fsp_fi_cache
);
492 DLIST_REMOVE(sconn
->files
, fsp
);
493 SMB_ASSERT(sconn
->num_files
> 0);
496 TALLOC_FREE(fsp
->fake_file_handle
);
498 if (fsp
->fh
->ref_count
== 1) {
499 TALLOC_FREE(fsp
->fh
);
501 fsp
->fh
->ref_count
--;
504 if (fsp
->lease
!= NULL
) {
505 if (fsp
->lease
->ref_count
== 1) {
506 TALLOC_FREE(fsp
->lease
);
508 fsp
->lease
->ref_count
--;
512 fsp
->conn
->num_files_open
--;
514 /* this is paranoia, just in case someone tries to reuse the
518 /* fsp->fsp_name is a talloc child and is free'd automatically. */
522 void file_free(struct smb_request
*req
, files_struct
*fsp
)
524 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
525 uint64_t fnum
= fsp
->fnum
;
528 size_t len
= fsp_fullbasepath(fsp
, NULL
, 0);
529 char fullpath
[len
+1];
531 fsp_fullbasepath(fsp
, fullpath
, sizeof(fullpath
));
534 * Avoid /. at the end of the path name. notify can't
537 if (len
> 1 && fullpath
[len
-1] == '.' &&
538 fullpath
[len
-2] == '/') {
539 fullpath
[len
-2] = '\0';
542 notify_remove(fsp
->conn
->sconn
->notify_ctx
, fsp
, fullpath
);
543 TALLOC_FREE(fsp
->notify
);
546 /* Ensure this event will never fire. */
547 TALLOC_FREE(fsp
->update_write_time_event
);
549 if (fsp
->op
!= NULL
) {
550 fsp
->op
->compat
= NULL
;
552 TALLOC_FREE(fsp
->op
);
554 if ((req
!= NULL
) && (fsp
== req
->chain_fsp
)) {
555 req
->chain_fsp
= NULL
;
559 * Clear all possible chained fsp
560 * pointers in the SMB2 request queue.
562 remove_smb2_chained_fsp(fsp
);
564 /* Drop all remaining extensions. */
565 vfs_remove_all_fsp_extensions(fsp
);
569 DEBUG(5,("freed files structure %llu (%u used)\n",
570 (unsigned long long)fnum
, (unsigned int)sconn
->num_files
));
573 /****************************************************************************
574 Get an fsp from a packet given a 16 bit fnum.
575 ****************************************************************************/
577 files_struct
*file_fsp(struct smb_request
*req
, uint16_t fid
)
579 struct smbXsrv_open
*op
;
586 * We should never get here. req==NULL could in theory
587 * only happen from internal opens with a non-zero
588 * root_dir_fid. Internal opens just don't do that, at
589 * least they are not supposed to do so. And if they
590 * start to do so, they better fake up a smb_request
591 * from which we get the right smbd_server_conn. While
592 * this should never happen, let's return NULL here.
597 if (req
->chain_fsp
!= NULL
) {
598 if (req
->chain_fsp
->deferred_close
) {
601 return req
->chain_fsp
;
604 if (req
->xconn
== NULL
) {
608 now
= timeval_to_nttime(&req
->request_time
);
610 status
= smb1srv_open_lookup(req
->xconn
,
612 if (!NT_STATUS_IS_OK(status
)) {
621 if (fsp
->deferred_close
) {
625 req
->chain_fsp
= fsp
;
629 struct files_struct
*file_fsp_get(struct smbd_smb2_request
*smb2req
,
630 uint64_t persistent_id
,
631 uint64_t volatile_id
)
633 struct smbXsrv_open
*op
;
636 struct files_struct
*fsp
;
638 now
= timeval_to_nttime(&smb2req
->request_time
);
640 status
= smb2srv_open_lookup(smb2req
->xconn
,
641 persistent_id
, volatile_id
,
643 if (!NT_STATUS_IS_OK(status
)) {
652 if (smb2req
->tcon
== NULL
) {
656 if (smb2req
->tcon
->compat
!= fsp
->conn
) {
660 if (smb2req
->session
== NULL
) {
664 if (smb2req
->session
->global
->session_wire_id
!= fsp
->vuid
) {
668 if (fsp
->deferred_close
) {
675 struct files_struct
*file_fsp_smb2(struct smbd_smb2_request
*smb2req
,
676 uint64_t persistent_id
,
677 uint64_t volatile_id
)
679 struct files_struct
*fsp
;
681 if (smb2req
->compat_chain_fsp
!= NULL
) {
682 if (smb2req
->compat_chain_fsp
->deferred_close
) {
685 return smb2req
->compat_chain_fsp
;
688 fsp
= file_fsp_get(smb2req
, persistent_id
, volatile_id
);
693 smb2req
->compat_chain_fsp
= fsp
;
697 /****************************************************************************
698 Duplicate the file handle part for a DOS or FCB open.
699 ****************************************************************************/
701 NTSTATUS
dup_file_fsp(
702 struct smb_request
*req
,
704 uint32_t access_mask
,
705 uint32_t create_options
,
708 /* this can never happen for print files */
709 SMB_ASSERT(from
->print_file
== NULL
);
716 to
->file_id
= from
->file_id
;
717 to
->initial_allocation_size
= from
->initial_allocation_size
;
718 to
->file_pid
= from
->file_pid
;
719 to
->vuid
= from
->vuid
;
720 to
->open_time
= from
->open_time
;
721 to
->access_mask
= access_mask
;
722 to
->oplock_type
= from
->oplock_type
;
723 to
->can_lock
= from
->can_lock
;
724 to
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
726 CAN_WRITE(from
->conn
) &&
727 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
728 to
->modified
= from
->modified
;
729 to
->is_directory
= from
->is_directory
;
730 to
->aio_write_behind
= from
->aio_write_behind
;
732 return fsp_set_smb_fname(to
, from
->fsp_name
);
736 * Return a jenkins hash of a pathname on a connection.
739 NTSTATUS
file_name_hash(connection_struct
*conn
,
740 const char *name
, uint32_t *p_name_hash
)
742 char tmpbuf
[PATH_MAX
];
743 char *fullpath
, *to_free
;
747 /* Set the hash of the full pathname. */
749 len
= full_path_tos(conn
->connectpath
, name
, tmpbuf
, sizeof(tmpbuf
),
750 &fullpath
, &to_free
);
752 return NT_STATUS_NO_MEMORY
;
754 key
= (TDB_DATA
) { .dptr
= (uint8_t *)fullpath
, .dsize
= len
+1 };
755 *p_name_hash
= tdb_jenkins_hash(&key
);
757 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
759 (unsigned int)*p_name_hash
));
761 TALLOC_FREE(to_free
);
766 * The only way that the fsp->fsp_name field should ever be set.
768 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
769 const struct smb_filename
*smb_fname_in
)
771 struct smb_filename
*smb_fname_new
;
773 smb_fname_new
= cp_smb_filename(fsp
, smb_fname_in
);
774 if (smb_fname_new
== NULL
) {
775 return NT_STATUS_NO_MEMORY
;
778 TALLOC_FREE(fsp
->fsp_name
);
779 fsp
->fsp_name
= smb_fname_new
;
781 return file_name_hash(fsp
->conn
,
782 smb_fname_str_dbg(fsp
->fsp_name
),
786 size_t fsp_fullbasepath(struct files_struct
*fsp
, char *buf
, size_t buflen
)
789 char tmp_buf
[1] = {'\0'};
792 * Don't pass NULL buffer to snprintf (to satisfy static checker)
793 * Some callers will call this function with NULL for buf and
794 * 0 for buflen in order to get length of fullbasepatch (without
795 * needing to allocate or write to buf)
801 len
= snprintf(buf
, buflen
, "%s/%s", fsp
->conn
->connectpath
,
802 fsp
->fsp_name
->base_name
);