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
;
69 DLIST_ADD(sconn
->files
, fsp
);
70 sconn
->num_files
+= 1;
72 conn
->num_files_open
++;
86 /****************************************************************************
87 Find first available file slot.
88 ****************************************************************************/
90 NTSTATUS
file_new(struct smb_request
*req
, connection_struct
*conn
,
91 files_struct
**result
)
93 struct smbd_server_connection
*sconn
= conn
->sconn
;
97 status
= fsp_new(conn
, conn
, &fsp
);
98 if (!NT_STATUS_IS_OK(status
)) {
102 GetTimeOfDay(&fsp
->open_time
);
105 struct smbXsrv_connection
*xconn
= req
->xconn
;
106 struct smbXsrv_open
*op
= NULL
;
107 NTTIME now
= timeval_to_nttime(&fsp
->open_time
);
109 status
= smbXsrv_open_create(xconn
,
112 if (!NT_STATUS_IS_OK(status
)) {
113 file_free(NULL
, fsp
);
118 fsp
->fnum
= op
->local_id
;
119 fsp
->fh
->gen_id
= smbXsrv_open_hash(op
);
121 DEBUG(10, ("%s: req==NULL, INTERNAL_OPEN_ONLY, smbXsrv_open "
122 "allocated\n", __func__
));
126 * Create an smb_filename with "" for the base_name. There are very
127 * few NULL checks, so make sure it's initialized with something. to
128 * be safe until an audit can be done.
130 fsp
->fsp_name
= synthetic_smb_fname(fsp
, "", NULL
, NULL
, 0);
131 if (fsp
->fsp_name
== NULL
) {
132 file_free(NULL
, fsp
);
133 return NT_STATUS_NO_MEMORY
;
136 DEBUG(5,("allocated file structure %s (%u used)\n",
137 fsp_fnum_dbg(fsp
), (unsigned int)sconn
->num_files
));
141 req
->chain_fsp
= fsp
;
144 /* A new fsp invalidates the positive and
145 negative fsp_fi_cache as the new fsp is pushed
146 at the start of the list and we search from
147 a cache hit to the *end* of the list. */
149 ZERO_STRUCT(sconn
->fsp_fi_cache
);
155 /****************************************************************************
156 Close all open files for a connection.
157 ****************************************************************************/
159 void file_close_conn(connection_struct
*conn
)
161 files_struct
*fsp
, *next
;
163 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
165 if (fsp
->conn
!= conn
) {
168 if (fsp
->op
!= NULL
&& fsp
->op
->global
->durable
) {
170 * A tree disconnect closes a durable handle
172 fsp
->op
->global
->durable
= false;
174 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
178 /****************************************************************************
179 Close all open files for a pid and a vuid.
180 ****************************************************************************/
182 void file_close_pid(struct smbd_server_connection
*sconn
, uint16_t smbpid
,
185 files_struct
*fsp
, *next
;
187 for (fsp
=sconn
->files
;fsp
;fsp
=next
) {
189 if ((fsp
->file_pid
== smbpid
) && (fsp
->vuid
== vuid
)) {
190 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
195 /****************************************************************************
196 Initialise file structures.
197 ****************************************************************************/
199 static int files_max_open_fds
;
201 bool file_init_global(void)
203 int request_max
= lp_max_open_files();
207 if (files_max_open_fds
!= 0) {
212 * Set the max_open files to be the requested
213 * max plus a fudgefactor to allow for the extra
214 * fd's we need such as log files etc...
216 real_lim
= set_maxfiles(request_max
+ MAX_OPEN_FUDGEFACTOR
);
218 real_max
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
220 if (real_max
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
> 65536) {
221 real_max
= 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
224 if (real_max
!= request_max
) {
225 DEBUG(1, ("file_init_global: Information only: requested %d "
226 "open files, %d are available.\n",
227 request_max
, real_max
));
230 SMB_ASSERT(real_max
> 100);
232 files_max_open_fds
= real_max
;
236 bool file_init(struct smbd_server_connection
*sconn
)
240 ok
= file_init_global();
245 sconn
->real_max_open_files
= files_max_open_fds
;
250 /****************************************************************************
251 Close files open by a specified vuid.
252 ****************************************************************************/
254 void file_close_user(struct smbd_server_connection
*sconn
, uint64_t vuid
)
256 files_struct
*fsp
, *next
;
258 for (fsp
=sconn
->files
; fsp
; fsp
=next
) {
260 if (fsp
->vuid
== vuid
) {
261 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
267 * Walk the files table until "fn" returns non-NULL
270 struct files_struct
*files_forall(
271 struct smbd_server_connection
*sconn
,
272 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
276 struct files_struct
*fsp
, *next
;
278 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
279 struct files_struct
*ret
;
281 ret
= fn(fsp
, private_data
);
289 /****************************************************************************
290 Find a fsp given a file descriptor.
291 ****************************************************************************/
293 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
298 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
299 if (fsp
->fh
->fd
== fd
) {
301 DLIST_PROMOTE(sconn
->files
, fsp
);
310 /****************************************************************************
311 Find a fsp given a device, inode and file_id.
312 ****************************************************************************/
314 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
315 struct file_id id
, unsigned long gen_id
)
324 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
325 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
326 if (file_id_equal(&fsp
->file_id
, &id
) &&
327 fsp
->fh
->gen_id
== gen_id
) {
329 DLIST_PROMOTE(sconn
->files
, fsp
);
331 /* Paranoia check. */
332 if ((fsp
->fh
->fd
== -1) &&
333 (fsp
->oplock_type
!= NO_OPLOCK
&&
334 fsp
->oplock_type
!= LEASE_OPLOCK
)) {
335 DEBUG(0,("file_find_dif: file %s file_id = "
336 "%s, gen = %u oplock_type = %u is a "
337 "stat open with oplock type !\n",
339 file_id_string_tos(&fsp
->file_id
),
340 (unsigned int)fsp
->fh
->gen_id
,
341 (unsigned int)fsp
->oplock_type
));
342 smb_panic("file_find_dif");
351 /****************************************************************************
352 Find the first fsp given a device and inode.
353 We use a singleton cache here to speed up searching from getfilepathinfo
355 ****************************************************************************/
357 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
362 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
363 /* Positive or negative cache hit. */
364 return sconn
->fsp_fi_cache
.fsp
;
367 sconn
->fsp_fi_cache
.id
= id
;
369 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
370 if (file_id_equal(&fsp
->file_id
, &id
)) {
371 /* Setup positive cache. */
372 sconn
->fsp_fi_cache
.fsp
= fsp
;
377 /* Setup negative cache. */
378 sconn
->fsp_fi_cache
.fsp
= NULL
;
382 /****************************************************************************
383 Find the next fsp having the same device and inode.
384 ****************************************************************************/
386 files_struct
*file_find_di_next(files_struct
*start_fsp
)
390 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
391 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
399 struct files_struct
*file_find_one_fsp_from_lease_key(
400 struct smbd_server_connection
*sconn
,
401 const struct smb2_lease_key
*lease_key
)
403 struct files_struct
*fsp
;
405 for (fsp
= sconn
->files
; fsp
; fsp
=fsp
->next
) {
406 if ((fsp
->lease
!= NULL
) &&
407 (fsp
->lease
->lease
.lease_key
.data
[0] ==
408 lease_key
->data
[0]) &&
409 (fsp
->lease
->lease
.lease_key
.data
[1] ==
410 lease_key
->data
[1])) {
417 /****************************************************************************
418 Find any fsp open with a pathname below that of an already open path.
419 ****************************************************************************/
421 bool file_find_subpath(files_struct
*dir_fsp
)
425 char *d_fullname
= NULL
;
427 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
428 dir_fsp
->conn
->connectpath
,
429 dir_fsp
->fsp_name
->base_name
);
435 dlen
= strlen(d_fullname
);
437 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
440 if (fsp
== dir_fsp
) {
444 d1_fullname
= talloc_asprintf(talloc_tos(),
446 fsp
->conn
->connectpath
,
447 fsp
->fsp_name
->base_name
);
450 * If the open file has a path that is a longer
451 * component, then it's a subpath.
453 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
454 (d1_fullname
[dlen
] == '/')) {
455 TALLOC_FREE(d1_fullname
);
456 TALLOC_FREE(d_fullname
);
459 TALLOC_FREE(d1_fullname
);
462 TALLOC_FREE(d_fullname
);
466 /****************************************************************************
468 ****************************************************************************/
470 void fsp_free(files_struct
*fsp
)
472 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
474 if (fsp
== sconn
->fsp_fi_cache
.fsp
) {
475 ZERO_STRUCT(sconn
->fsp_fi_cache
);
478 DLIST_REMOVE(sconn
->files
, fsp
);
479 SMB_ASSERT(sconn
->num_files
> 0);
482 TALLOC_FREE(fsp
->fake_file_handle
);
484 if (fsp
->fh
->ref_count
== 1) {
485 TALLOC_FREE(fsp
->fh
);
487 fsp
->fh
->ref_count
--;
490 if (fsp
->lease
!= NULL
) {
491 if (fsp
->lease
->ref_count
== 1) {
492 TALLOC_FREE(fsp
->lease
);
494 fsp
->lease
->ref_count
--;
498 fsp
->conn
->num_files_open
--;
500 /* this is paranoia, just in case someone tries to reuse the
504 /* fsp->fsp_name is a talloc child and is free'd automatically. */
508 void file_free(struct smb_request
*req
, files_struct
*fsp
)
510 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
511 uint64_t fnum
= fsp
->fnum
;
514 size_t len
= fsp_fullbasepath(fsp
, NULL
, 0);
515 char fullpath
[len
+1];
517 fsp_fullbasepath(fsp
, fullpath
, sizeof(fullpath
));
520 * Avoid /. at the end of the path name. notify can't
523 if (len
> 1 && fullpath
[len
-1] == '.' &&
524 fullpath
[len
-2] == '/') {
525 fullpath
[len
-2] = '\0';
528 notify_remove(fsp
->conn
->sconn
->notify_ctx
, fsp
, fullpath
);
529 TALLOC_FREE(fsp
->notify
);
532 /* Ensure this event will never fire. */
533 TALLOC_FREE(fsp
->update_write_time_event
);
535 if (fsp
->op
!= NULL
) {
536 fsp
->op
->compat
= NULL
;
538 TALLOC_FREE(fsp
->op
);
540 if ((req
!= NULL
) && (fsp
== req
->chain_fsp
)) {
541 req
->chain_fsp
= NULL
;
545 * Clear all possible chained fsp
546 * pointers in the SMB2 request queue.
548 remove_smb2_chained_fsp(fsp
);
550 /* Drop all remaining extensions. */
551 vfs_remove_all_fsp_extensions(fsp
);
555 DEBUG(5,("freed files structure %llu (%u used)\n",
556 (unsigned long long)fnum
, (unsigned int)sconn
->num_files
));
559 /****************************************************************************
560 Get an fsp from a packet given a 16 bit fnum.
561 ****************************************************************************/
563 files_struct
*file_fsp(struct smb_request
*req
, uint16_t fid
)
565 struct smbXsrv_open
*op
;
572 * We should never get here. req==NULL could in theory
573 * only happen from internal opens with a non-zero
574 * root_dir_fid. Internal opens just don't do that, at
575 * least they are not supposed to do so. And if they
576 * start to do so, they better fake up a smb_request
577 * from which we get the right smbd_server_conn. While
578 * this should never happen, let's return NULL here.
583 if (req
->chain_fsp
!= NULL
) {
584 if (req
->chain_fsp
->deferred_close
) {
587 return req
->chain_fsp
;
590 if (req
->xconn
== NULL
) {
594 now
= timeval_to_nttime(&req
->request_time
);
596 status
= smb1srv_open_lookup(req
->xconn
,
598 if (!NT_STATUS_IS_OK(status
)) {
607 if (fsp
->deferred_close
) {
611 req
->chain_fsp
= fsp
;
615 struct files_struct
*file_fsp_get(struct smbd_smb2_request
*smb2req
,
616 uint64_t persistent_id
,
617 uint64_t volatile_id
)
619 struct smbXsrv_open
*op
;
622 struct files_struct
*fsp
;
624 now
= timeval_to_nttime(&smb2req
->request_time
);
626 status
= smb2srv_open_lookup(smb2req
->xconn
,
627 persistent_id
, volatile_id
,
629 if (!NT_STATUS_IS_OK(status
)) {
638 if (smb2req
->tcon
== NULL
) {
642 if (smb2req
->tcon
->compat
!= fsp
->conn
) {
646 if (smb2req
->session
== NULL
) {
650 if (smb2req
->session
->compat
== NULL
) {
654 if (smb2req
->session
->compat
->vuid
!= fsp
->vuid
) {
658 if (fsp
->deferred_close
) {
665 struct files_struct
*file_fsp_smb2(struct smbd_smb2_request
*smb2req
,
666 uint64_t persistent_id
,
667 uint64_t volatile_id
)
669 struct files_struct
*fsp
;
671 if (smb2req
->compat_chain_fsp
!= NULL
) {
672 if (smb2req
->compat_chain_fsp
->deferred_close
) {
675 return smb2req
->compat_chain_fsp
;
678 fsp
= file_fsp_get(smb2req
, persistent_id
, volatile_id
);
683 smb2req
->compat_chain_fsp
= fsp
;
687 /****************************************************************************
688 Duplicate the file handle part for a DOS or FCB open.
689 ****************************************************************************/
691 NTSTATUS
dup_file_fsp(struct smb_request
*req
, files_struct
*from
,
692 uint32_t access_mask
, uint32_t share_access
,
693 uint32_t create_options
, files_struct
*to
)
695 /* this can never happen for print files */
696 SMB_ASSERT(from
->print_file
== NULL
);
703 to
->file_id
= from
->file_id
;
704 to
->initial_allocation_size
= from
->initial_allocation_size
;
705 to
->file_pid
= from
->file_pid
;
706 to
->vuid
= from
->vuid
;
707 to
->open_time
= from
->open_time
;
708 to
->access_mask
= access_mask
;
709 to
->share_access
= share_access
;
710 to
->oplock_type
= from
->oplock_type
;
711 to
->can_lock
= from
->can_lock
;
712 to
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
714 CAN_WRITE(from
->conn
) &&
715 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
716 to
->modified
= from
->modified
;
717 to
->is_directory
= from
->is_directory
;
718 to
->aio_write_behind
= from
->aio_write_behind
;
720 return fsp_set_smb_fname(to
, from
->fsp_name
);
724 * Return a jenkins hash of a pathname on a connection.
727 NTSTATUS
file_name_hash(connection_struct
*conn
,
728 const char *name
, uint32_t *p_name_hash
)
730 char tmpbuf
[PATH_MAX
];
731 char *fullpath
, *to_free
;
735 /* Set the hash of the full pathname. */
737 len
= full_path_tos(conn
->connectpath
, name
, tmpbuf
, sizeof(tmpbuf
),
738 &fullpath
, &to_free
);
740 return NT_STATUS_NO_MEMORY
;
742 key
= (TDB_DATA
) { .dptr
= (uint8_t *)fullpath
, .dsize
= len
+1 };
743 *p_name_hash
= tdb_jenkins_hash(&key
);
745 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
747 (unsigned int)*p_name_hash
));
749 TALLOC_FREE(to_free
);
754 * The only way that the fsp->fsp_name field should ever be set.
756 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
757 const struct smb_filename
*smb_fname_in
)
759 struct smb_filename
*smb_fname_new
;
761 smb_fname_new
= cp_smb_filename(fsp
, smb_fname_in
);
762 if (smb_fname_new
== NULL
) {
763 return NT_STATUS_NO_MEMORY
;
766 TALLOC_FREE(fsp
->fsp_name
);
767 fsp
->fsp_name
= smb_fname_new
;
769 return file_name_hash(fsp
->conn
,
770 smb_fname_str_dbg(fsp
->fsp_name
),
774 const struct GUID
*fsp_client_guid(const files_struct
*fsp
)
776 return &fsp
->conn
->sconn
->client
->connections
->smb2
.client
.guid
;
779 size_t fsp_fullbasepath(struct files_struct
*fsp
, char *buf
, size_t buflen
)
782 char tmp_buf
[1] = {'\0'};
785 * Don't pass NULL buffer to snprintf (to satisfy static checker)
786 * Some callers will call this function with NULL for buf and
787 * 0 for buflen in order to get length of fullbasepatch (without
788 * needing to allocate or write to buf)
794 len
= snprintf(buf
, buflen
, "%s/%s", fsp
->conn
->connectpath
,
795 fsp
->fsp_name
->base_name
);