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 <ccan/hash/hash.h>
26 #include "lib/util/bitmap.h"
28 #define FILE_HANDLE_OFFSET 0x1000
31 * create new fsp to be used for file_new or a durable handle reconnect
33 NTSTATUS
fsp_new(struct connection_struct
*conn
, TALLOC_CTX
*mem_ctx
,
34 files_struct
**result
)
36 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
37 files_struct
*fsp
= NULL
;
38 struct smbd_server_connection
*sconn
= conn
->sconn
;
40 fsp
= talloc_zero(mem_ctx
, struct files_struct
);
46 * This can't be a child of fsp because the file_handle can be ref'd
47 * when doing a dos/fcb open, which will then share the file_handle
48 * across multiple fsps.
50 fsp
->fh
= talloc_zero(mem_ctx
, struct fd_handle
);
51 if (fsp
->fh
== NULL
) {
55 fsp
->fh
->ref_count
= 1;
58 fsp
->fnum
= FNUM_FIELD_INVALID
;
61 DLIST_ADD(sconn
->files
, fsp
);
62 sconn
->num_files
+= 1;
64 conn
->num_files_open
++;
78 /****************************************************************************
79 Find first available file slot.
80 ****************************************************************************/
82 NTSTATUS
file_new(struct smb_request
*req
, connection_struct
*conn
,
83 files_struct
**result
)
85 struct smbd_server_connection
*sconn
= conn
->sconn
;
89 status
= fsp_new(conn
, conn
, &fsp
);
90 if (!NT_STATUS_IS_OK(status
)) {
94 GetTimeOfDay(&fsp
->open_time
);
97 struct smbXsrv_open
*op
= NULL
;
98 NTTIME now
= timeval_to_nttime(&fsp
->open_time
);
100 status
= smbXsrv_open_create(sconn
->conn
,
103 if (!NT_STATUS_IS_OK(status
)) {
104 file_free(NULL
, fsp
);
109 fsp
->fnum
= op
->local_id
;
110 fsp
->fh
->gen_id
= smbXsrv_open_hash(op
);
114 * Create an smb_filename with "" for the base_name. There are very
115 * few NULL checks, so make sure it's initialized with something. to
116 * be safe until an audit can be done.
118 status
= create_synthetic_smb_fname(fsp
, "", NULL
, NULL
,
120 if (!NT_STATUS_IS_OK(status
)) {
121 file_free(NULL
, fsp
);
125 DEBUG(5,("allocated file structure %s (%u used)\n",
126 fsp_fnum_dbg(fsp
), (unsigned int)sconn
->num_files
));
130 req
->chain_fsp
= fsp
;
133 /* A new fsp invalidates the positive and
134 negative fsp_fi_cache as the new fsp is pushed
135 at the start of the list and we search from
136 a cache hit to the *end* of the list. */
138 ZERO_STRUCT(sconn
->fsp_fi_cache
);
144 /****************************************************************************
145 Close all open files for a connection.
146 ****************************************************************************/
148 void file_close_conn(connection_struct
*conn
)
150 files_struct
*fsp
, *next
;
152 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
154 if (fsp
->conn
== conn
) {
155 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
160 /****************************************************************************
161 Close all open files for a pid and a vuid.
162 ****************************************************************************/
164 void file_close_pid(struct smbd_server_connection
*sconn
, uint16 smbpid
,
167 files_struct
*fsp
, *next
;
169 for (fsp
=sconn
->files
;fsp
;fsp
=next
) {
171 if ((fsp
->file_pid
== smbpid
) && (fsp
->vuid
== vuid
)) {
172 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
177 /****************************************************************************
178 Initialise file structures.
179 ****************************************************************************/
181 static int files_max_open_fds
;
183 bool file_init_global(void)
185 int request_max
= lp_max_open_files();
189 if (files_max_open_fds
!= 0) {
194 * Set the max_open files to be the requested
195 * max plus a fudgefactor to allow for the extra
196 * fd's we need such as log files etc...
198 real_lim
= set_maxfiles(request_max
+ MAX_OPEN_FUDGEFACTOR
);
200 real_max
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
202 if (real_max
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
> 65536) {
203 real_max
= 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
206 if (real_max
!= request_max
) {
207 DEBUG(1, ("file_init_global: Information only: requested %d "
208 "open files, %d are available.\n",
209 request_max
, real_max
));
212 SMB_ASSERT(real_max
> 100);
214 files_max_open_fds
= real_max
;
218 bool file_init(struct smbd_server_connection
*sconn
)
222 ok
= file_init_global();
227 sconn
->real_max_open_files
= files_max_open_fds
;
232 /****************************************************************************
233 Close files open by a specified vuid.
234 ****************************************************************************/
236 void file_close_user(struct smbd_server_connection
*sconn
, uint64_t vuid
)
238 files_struct
*fsp
, *next
;
240 for (fsp
=sconn
->files
; fsp
; fsp
=next
) {
242 if (fsp
->vuid
== vuid
) {
243 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
249 * Walk the files table until "fn" returns non-NULL
252 struct files_struct
*files_forall(
253 struct smbd_server_connection
*sconn
,
254 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
258 struct files_struct
*fsp
, *next
;
260 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
261 struct files_struct
*ret
;
263 ret
= fn(fsp
, private_data
);
271 /****************************************************************************
272 Find a fsp given a file descriptor.
273 ****************************************************************************/
275 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
280 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
281 if (fsp
->fh
->fd
== fd
) {
283 DLIST_PROMOTE(sconn
->files
, fsp
);
292 /****************************************************************************
293 Find a fsp given a device, inode and file_id.
294 ****************************************************************************/
296 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
297 struct file_id id
, unsigned long gen_id
)
306 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
307 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
308 if (file_id_equal(&fsp
->file_id
, &id
) &&
309 fsp
->fh
->gen_id
== gen_id
) {
311 DLIST_PROMOTE(sconn
->files
, fsp
);
313 /* Paranoia check. */
314 if ((fsp
->fh
->fd
== -1) &&
315 (fsp
->oplock_type
!= NO_OPLOCK
) &&
316 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
)) {
317 DEBUG(0,("file_find_dif: file %s file_id = "
318 "%s, gen = %u oplock_type = %u is a "
319 "stat open with oplock type !\n",
321 file_id_string_tos(&fsp
->file_id
),
322 (unsigned int)fsp
->fh
->gen_id
,
323 (unsigned int)fsp
->oplock_type
));
324 smb_panic("file_find_dif");
333 /****************************************************************************
334 Find the first fsp given a device and inode.
335 We use a singleton cache here to speed up searching from getfilepathinfo
337 ****************************************************************************/
339 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
344 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
345 /* Positive or negative cache hit. */
346 return sconn
->fsp_fi_cache
.fsp
;
349 sconn
->fsp_fi_cache
.id
= id
;
351 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
352 if (file_id_equal(&fsp
->file_id
, &id
)) {
353 /* Setup positive cache. */
354 sconn
->fsp_fi_cache
.fsp
= fsp
;
359 /* Setup negative cache. */
360 sconn
->fsp_fi_cache
.fsp
= NULL
;
364 /****************************************************************************
365 Find the next fsp having the same device and inode.
366 ****************************************************************************/
368 files_struct
*file_find_di_next(files_struct
*start_fsp
)
372 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
373 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
381 /****************************************************************************
382 Find any fsp open with a pathname below that of an already open path.
383 ****************************************************************************/
385 bool file_find_subpath(files_struct
*dir_fsp
)
389 char *d_fullname
= NULL
;
391 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
392 dir_fsp
->conn
->connectpath
,
393 dir_fsp
->fsp_name
->base_name
);
399 dlen
= strlen(d_fullname
);
401 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
404 if (fsp
== dir_fsp
) {
408 d1_fullname
= talloc_asprintf(talloc_tos(),
410 fsp
->conn
->connectpath
,
411 fsp
->fsp_name
->base_name
);
414 * If the open file has a path that is a longer
415 * component, then it's a subpath.
417 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
418 (d1_fullname
[dlen
] == '/')) {
419 TALLOC_FREE(d1_fullname
);
420 TALLOC_FREE(d_fullname
);
423 TALLOC_FREE(d1_fullname
);
426 TALLOC_FREE(d_fullname
);
430 /****************************************************************************
431 Sync open files on a connection.
432 ****************************************************************************/
434 void file_sync_all(connection_struct
*conn
)
436 files_struct
*fsp
, *next
;
438 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
440 if ((conn
== fsp
->conn
) && (fsp
->fh
->fd
!= -1)) {
441 sync_file(conn
, fsp
, True
/* write through */);
446 /****************************************************************************
448 ****************************************************************************/
450 void fsp_free(files_struct
*fsp
)
452 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
454 DLIST_REMOVE(sconn
->files
, fsp
);
455 SMB_ASSERT(sconn
->num_files
> 0);
458 TALLOC_FREE(fsp
->fake_file_handle
);
460 if (fsp
->fh
->ref_count
== 1) {
461 TALLOC_FREE(fsp
->fh
);
463 fsp
->fh
->ref_count
--;
466 fsp
->conn
->num_files_open
--;
468 /* this is paranoia, just in case someone tries to reuse the
472 /* fsp->fsp_name is a talloc child and is free'd automatically. */
476 void file_free(struct smb_request
*req
, files_struct
*fsp
)
478 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
479 uint64_t fnum
= fsp
->fnum
;
482 struct notify_context
*notify_ctx
=
483 fsp
->conn
->sconn
->notify_ctx
;
484 notify_remove(notify_ctx
, fsp
);
485 TALLOC_FREE(fsp
->notify
);
488 /* Ensure this event will never fire. */
489 TALLOC_FREE(fsp
->update_write_time_event
);
491 if (fsp
->op
!= NULL
) {
492 fsp
->op
->compat
= NULL
;
494 TALLOC_FREE(fsp
->op
);
496 if ((req
!= NULL
) && (fsp
== req
->chain_fsp
)) {
497 req
->chain_fsp
= NULL
;
501 * Clear all possible chained fsp
502 * pointers in the SMB2 request queue.
504 if (req
!= NULL
&& req
->smb2req
) {
505 remove_smb2_chained_fsp(fsp
);
508 /* Closing a file can invalidate the positive cache. */
509 if (fsp
== sconn
->fsp_fi_cache
.fsp
) {
510 ZERO_STRUCT(sconn
->fsp_fi_cache
);
513 /* Drop all remaining extensions. */
514 vfs_remove_all_fsp_extensions(fsp
);
518 DEBUG(5,("freed files structure %llu (%u used)\n",
519 (unsigned long long)fnum
, (unsigned int)sconn
->num_files
));
522 /****************************************************************************
523 Get an fsp from a packet given a 16 bit fnum.
524 ****************************************************************************/
526 files_struct
*file_fsp(struct smb_request
*req
, uint16 fid
)
528 struct smbXsrv_open
*op
;
535 * We should never get here. req==NULL could in theory
536 * only happen from internal opens with a non-zero
537 * root_dir_fid. Internal opens just don't do that, at
538 * least they are not supposed to do so. And if they
539 * start to do so, they better fake up a smb_request
540 * from which we get the right smbd_server_conn. While
541 * this should never happen, let's return NULL here.
546 if (req
->chain_fsp
!= NULL
) {
547 return req
->chain_fsp
;
550 if (req
->sconn
->conn
== NULL
) {
554 now
= timeval_to_nttime(&req
->request_time
);
556 status
= smb1srv_open_lookup(req
->sconn
->conn
,
558 if (!NT_STATUS_IS_OK(status
)) {
564 req
->chain_fsp
= fsp
;
569 struct files_struct
*file_fsp_smb2(struct smbd_smb2_request
*smb2req
,
570 uint64_t persistent_id
,
571 uint64_t volatile_id
)
573 struct smbXsrv_open
*op
;
576 struct files_struct
*fsp
;
578 if (smb2req
->compat_chain_fsp
!= NULL
) {
579 return smb2req
->compat_chain_fsp
;
582 now
= timeval_to_nttime(&smb2req
->request_time
);
584 status
= smb2srv_open_lookup(smb2req
->sconn
->conn
,
585 persistent_id
, volatile_id
,
587 if (!NT_STATUS_IS_OK(status
)) {
596 if (smb2req
->tcon
== NULL
) {
600 if (smb2req
->tcon
->compat
!= fsp
->conn
) {
604 if (smb2req
->session
== NULL
) {
608 if (smb2req
->session
->compat
== NULL
) {
612 if (smb2req
->session
->compat
->vuid
!= fsp
->vuid
) {
616 smb2req
->compat_chain_fsp
= fsp
;
620 /****************************************************************************
621 Duplicate the file handle part for a DOS or FCB open.
622 ****************************************************************************/
624 NTSTATUS
dup_file_fsp(struct smb_request
*req
, files_struct
*from
,
625 uint32 access_mask
, uint32 share_access
,
626 uint32 create_options
, files_struct
*to
)
628 /* this can never happen for print files */
629 SMB_ASSERT(from
->print_file
== NULL
);
636 to
->file_id
= from
->file_id
;
637 to
->initial_allocation_size
= from
->initial_allocation_size
;
638 to
->file_pid
= from
->file_pid
;
639 to
->vuid
= from
->vuid
;
640 to
->open_time
= from
->open_time
;
641 to
->access_mask
= access_mask
;
642 to
->share_access
= share_access
;
643 to
->oplock_type
= from
->oplock_type
;
644 to
->can_lock
= from
->can_lock
;
645 to
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
647 CAN_WRITE(from
->conn
) &&
648 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
649 to
->modified
= from
->modified
;
650 to
->is_directory
= from
->is_directory
;
651 to
->aio_write_behind
= from
->aio_write_behind
;
653 return fsp_set_smb_fname(to
, from
->fsp_name
);
657 * Return a jenkins hash of a pathname on a connection.
660 NTSTATUS
file_name_hash(connection_struct
*conn
,
661 const char *name
, uint32_t *p_name_hash
)
663 char *fullpath
= NULL
;
665 /* Set the hash of the full pathname. */
666 fullpath
= talloc_asprintf(talloc_tos(),
671 return NT_STATUS_NO_MEMORY
;
673 *p_name_hash
= hash(fullpath
, talloc_get_size(fullpath
), 0);
675 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
677 (unsigned int)*p_name_hash
));
679 TALLOC_FREE(fullpath
);
684 * The only way that the fsp->fsp_name field should ever be set.
686 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
687 const struct smb_filename
*smb_fname_in
)
690 struct smb_filename
*smb_fname_new
;
692 status
= copy_smb_filename(fsp
, smb_fname_in
, &smb_fname_new
);
693 if (!NT_STATUS_IS_OK(status
)) {
697 TALLOC_FREE(fsp
->fsp_name
);
698 fsp
->fsp_name
= smb_fname_new
;
700 return file_name_hash(fsp
->conn
,
701 smb_fname_str_dbg(fsp
->fsp_name
),