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
30 /****************************************************************************
31 Return a unique number identifying this fsp over the life of this pid.
32 ****************************************************************************/
34 static unsigned long get_gen_count(struct smbd_server_connection
*sconn
)
36 sconn
->file_gen_counter
+= 1;
37 if (sconn
->file_gen_counter
== 0) {
38 sconn
->file_gen_counter
+= 1;
40 return sconn
->file_gen_counter
;
43 /****************************************************************************
44 Find first available file slot.
45 ****************************************************************************/
47 NTSTATUS
file_new(struct smb_request
*req
, connection_struct
*conn
,
48 files_struct
**result
)
50 struct smbd_server_connection
*sconn
= conn
->sconn
;
55 if (sconn
->file_bmap
!= NULL
) {
58 * we want to give out file handles differently on each new
59 * connection because of a common bug in MS clients where they
60 * try to reuse a file descriptor from an earlier smb
61 * connection. This code increases the chance that the errant
62 * client will get an error rather than causing corruption
64 if (sconn
->first_file
== 0) {
65 sconn
->first_file
= (getpid() ^ (int)time(NULL
));
66 sconn
->first_file
%= sconn
->real_max_open_files
;
69 /* TODO: Port the id-tree implementation from Samba4 */
71 i
= bitmap_find(sconn
->file_bmap
, sconn
->first_file
);
73 DEBUG(0,("ERROR! Out of file structures\n"));
75 * TODO: We have to unconditionally return a DOS error
76 * here, W2k3 even returns ERRDOS/ERRnofids for
77 * ntcreate&x with NTSTATUS negotiated
79 return NT_STATUS_TOO_MANY_OPENED_FILES
;
84 * Make a child of the connection_struct as an fsp can't exist
85 * independent of a connection.
87 fsp
= talloc_zero(conn
, struct files_struct
);
89 return NT_STATUS_NO_MEMORY
;
93 * This can't be a child of fsp because the file_handle can be ref'd
94 * when doing a dos/fcb open, which will then share the file_handle
95 * across multiple fsps.
97 fsp
->fh
= talloc_zero(conn
, struct fd_handle
);
100 return NT_STATUS_NO_MEMORY
;
103 fsp
->fh
->ref_count
= 1;
108 fsp
->fh
->gen_id
= get_gen_count(sconn
);
109 GetTimeOfDay(&fsp
->open_time
);
111 if (sconn
->file_bmap
!= NULL
) {
112 sconn
->first_file
= (i
+1) % (sconn
->real_max_open_files
);
114 bitmap_set(sconn
->file_bmap
, i
);
116 fsp
->fnum
= i
+ FILE_HANDLE_OFFSET
;
117 SMB_ASSERT(fsp
->fnum
< 65536);
120 DLIST_ADD(sconn
->files
, fsp
);
121 sconn
->num_files
+= 1;
123 conn
->num_files_open
++;
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 status
= create_synthetic_smb_fname(fsp
, "", NULL
, NULL
,
132 if (!NT_STATUS_IS_OK(status
)) {
133 file_free(NULL
, fsp
);
137 DEBUG(5,("allocated file structure %d, fnum = %d (%u used)\n",
138 i
, fsp
->fnum
, (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
) {
166 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
171 /****************************************************************************
172 Close all open files for a pid and a vuid.
173 ****************************************************************************/
175 void file_close_pid(struct smbd_server_connection
*sconn
, uint16 smbpid
,
178 files_struct
*fsp
, *next
;
180 for (fsp
=sconn
->files
;fsp
;fsp
=next
) {
182 if ((fsp
->file_pid
== smbpid
) && (fsp
->vuid
== vuid
)) {
183 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
188 /****************************************************************************
189 Initialise file structures.
190 ****************************************************************************/
192 static int files_max_open_fds
;
194 bool file_init_global(void)
196 int request_max
= lp_max_open_files();
200 if (files_max_open_fds
!= 0) {
205 * Set the max_open files to be the requested
206 * max plus a fudgefactor to allow for the extra
207 * fd's we need such as log files etc...
209 real_lim
= set_maxfiles(request_max
+ MAX_OPEN_FUDGEFACTOR
);
211 real_max
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
213 if (real_max
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
> 65536) {
214 real_max
= 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
217 if (real_max
!= request_max
) {
218 DEBUG(1, ("file_init_global: Information only: requested %d "
219 "open files, %d are available.\n",
220 request_max
, real_max
));
223 SMB_ASSERT(real_max
> 100);
225 files_max_open_fds
= real_max
;
229 bool file_init(struct smbd_server_connection
*sconn
)
233 ok
= file_init_global();
238 sconn
->real_max_open_files
= files_max_open_fds
;
240 sconn
->file_bmap
= bitmap_talloc(sconn
, sconn
->real_max_open_files
);
241 if (!sconn
->file_bmap
) {
248 /****************************************************************************
249 Close files open by a specified vuid.
250 ****************************************************************************/
252 void file_close_user(struct smbd_server_connection
*sconn
, uint64_t vuid
)
254 files_struct
*fsp
, *next
;
256 for (fsp
=sconn
->files
; fsp
; fsp
=next
) {
258 if (fsp
->vuid
== vuid
) {
259 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
265 * Walk the files table until "fn" returns non-NULL
268 struct files_struct
*files_forall(
269 struct smbd_server_connection
*sconn
,
270 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
274 struct files_struct
*fsp
, *next
;
276 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
277 struct files_struct
*ret
;
279 ret
= fn(fsp
, private_data
);
287 /****************************************************************************
288 Find a fsp given a file descriptor.
289 ****************************************************************************/
291 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
296 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
297 if (fsp
->fh
->fd
== fd
) {
299 DLIST_PROMOTE(sconn
->files
, fsp
);
308 /****************************************************************************
309 Find a fsp given a device, inode and file_id.
310 ****************************************************************************/
312 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
313 struct file_id id
, unsigned long gen_id
)
318 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
319 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
320 if (file_id_equal(&fsp
->file_id
, &id
) &&
321 fsp
->fh
->gen_id
== gen_id
) {
323 DLIST_PROMOTE(sconn
->files
, fsp
);
325 /* Paranoia check. */
326 if ((fsp
->fh
->fd
== -1) &&
327 (fsp
->oplock_type
!= NO_OPLOCK
) &&
328 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
)) {
329 DEBUG(0,("file_find_dif: file %s file_id = "
330 "%s, gen = %u oplock_type = %u is a "
331 "stat open with oplock type !\n",
333 file_id_string_tos(&fsp
->file_id
),
334 (unsigned int)fsp
->fh
->gen_id
,
335 (unsigned int)fsp
->oplock_type
));
336 smb_panic("file_find_dif");
345 /****************************************************************************
346 Find the first fsp given a device and inode.
347 We use a singleton cache here to speed up searching from getfilepathinfo
349 ****************************************************************************/
351 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
356 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
357 /* Positive or negative cache hit. */
358 return sconn
->fsp_fi_cache
.fsp
;
361 sconn
->fsp_fi_cache
.id
= id
;
363 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
364 if (file_id_equal(&fsp
->file_id
, &id
)) {
365 /* Setup positive cache. */
366 sconn
->fsp_fi_cache
.fsp
= fsp
;
371 /* Setup negative cache. */
372 sconn
->fsp_fi_cache
.fsp
= NULL
;
376 /****************************************************************************
377 Find the next fsp having the same device and inode.
378 ****************************************************************************/
380 files_struct
*file_find_di_next(files_struct
*start_fsp
)
384 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
385 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
393 /****************************************************************************
394 Find any fsp open with a pathname below that of an already open path.
395 ****************************************************************************/
397 bool file_find_subpath(files_struct
*dir_fsp
)
401 char *d_fullname
= NULL
;
403 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
404 dir_fsp
->conn
->connectpath
,
405 dir_fsp
->fsp_name
->base_name
);
411 dlen
= strlen(d_fullname
);
413 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
416 if (fsp
== dir_fsp
) {
420 d1_fullname
= talloc_asprintf(talloc_tos(),
422 fsp
->conn
->connectpath
,
423 fsp
->fsp_name
->base_name
);
426 * If the open file has a path that is a longer
427 * component, then it's a subpath.
429 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
430 (d1_fullname
[dlen
] == '/')) {
431 TALLOC_FREE(d1_fullname
);
432 TALLOC_FREE(d_fullname
);
435 TALLOC_FREE(d1_fullname
);
438 TALLOC_FREE(d_fullname
);
442 /****************************************************************************
443 Sync open files on a connection.
444 ****************************************************************************/
446 void file_sync_all(connection_struct
*conn
)
448 files_struct
*fsp
, *next
;
450 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
452 if ((conn
== fsp
->conn
) && (fsp
->fh
->fd
!= -1)) {
453 sync_file(conn
, fsp
, True
/* write through */);
458 /****************************************************************************
460 ****************************************************************************/
462 void file_free(struct smb_request
*req
, files_struct
*fsp
)
464 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
466 DLIST_REMOVE(sconn
->files
, fsp
);
467 SMB_ASSERT(sconn
->num_files
> 0);
470 TALLOC_FREE(fsp
->fake_file_handle
);
472 if (fsp
->fh
->ref_count
== 1) {
473 TALLOC_FREE(fsp
->fh
);
475 fsp
->fh
->ref_count
--;
479 struct notify_context
*notify_ctx
=
480 fsp
->conn
->sconn
->notify_ctx
;
481 notify_remove(notify_ctx
, fsp
);
482 TALLOC_FREE(fsp
->notify
);
485 /* Ensure this event will never fire. */
486 TALLOC_FREE(fsp
->update_write_time_event
);
488 if (sconn
->file_bmap
!= NULL
) {
489 bitmap_clear(sconn
->file_bmap
, fsp
->fnum
- FILE_HANDLE_OFFSET
);
491 DEBUG(5,("freed files structure %d (%u used)\n",
492 fsp
->fnum
, (unsigned int)sconn
->num_files
));
494 fsp
->conn
->num_files_open
--;
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
);
516 /* this is paranoia, just in case someone tries to reuse the
520 /* fsp->fsp_name is a talloc child and is free'd automatically. */
524 /****************************************************************************
525 Get an fsp from a 16 bit fnum.
526 ****************************************************************************/
528 static struct files_struct
*file_fnum(struct smbd_server_connection
*sconn
,
534 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
, count
++) {
535 if (fsp
->fnum
== -1) {
539 if (fsp
->fnum
== fnum
) {
541 DLIST_PROMOTE(sconn
->files
, fsp
);
549 /****************************************************************************
550 Get an fsp from a packet given a 16 bit fnum.
551 ****************************************************************************/
553 files_struct
*file_fsp(struct smb_request
*req
, uint16 fid
)
559 * We should never get here. req==NULL could in theory
560 * only happen from internal opens with a non-zero
561 * root_dir_fid. Internal opens just don't do that, at
562 * least they are not supposed to do so. And if they
563 * start to do so, they better fake up a smb_request
564 * from which we get the right smbd_server_conn. While
565 * this should never happen, let's return NULL here.
570 if (req
->chain_fsp
!= NULL
) {
571 return req
->chain_fsp
;
574 fsp
= file_fnum(req
->sconn
, fid
);
576 req
->chain_fsp
= fsp
;
577 if (req
->smb2req
!= NULL
) {
578 req
->smb2req
->compat_chain_fsp
= fsp
;
584 /****************************************************************************
585 Duplicate the file handle part for a DOS or FCB open.
586 ****************************************************************************/
588 NTSTATUS
dup_file_fsp(struct smb_request
*req
, files_struct
*from
,
589 uint32 access_mask
, uint32 share_access
,
590 uint32 create_options
, files_struct
*to
)
592 /* this can never happen for print files */
593 SMB_ASSERT(from
->print_file
== NULL
);
600 to
->file_id
= from
->file_id
;
601 to
->initial_allocation_size
= from
->initial_allocation_size
;
602 to
->file_pid
= from
->file_pid
;
603 to
->vuid
= from
->vuid
;
604 to
->open_time
= from
->open_time
;
605 to
->access_mask
= access_mask
;
606 to
->share_access
= share_access
;
607 to
->oplock_type
= from
->oplock_type
;
608 to
->can_lock
= from
->can_lock
;
609 to
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
611 CAN_WRITE(from
->conn
) &&
612 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
613 to
->modified
= from
->modified
;
614 to
->is_directory
= from
->is_directory
;
615 to
->aio_write_behind
= from
->aio_write_behind
;
617 return fsp_set_smb_fname(to
, from
->fsp_name
);
621 * Return a jenkins hash of a pathname on a connection.
624 NTSTATUS
file_name_hash(connection_struct
*conn
,
625 const char *name
, uint32_t *p_name_hash
)
627 char *fullpath
= NULL
;
629 /* Set the hash of the full pathname. */
630 fullpath
= talloc_asprintf(talloc_tos(),
635 return NT_STATUS_NO_MEMORY
;
637 *p_name_hash
= hash(fullpath
, talloc_get_size(fullpath
), 0);
639 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
641 (unsigned int)*p_name_hash
));
643 TALLOC_FREE(fullpath
);
648 * The only way that the fsp->fsp_name field should ever be set.
650 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
651 const struct smb_filename
*smb_fname_in
)
654 struct smb_filename
*smb_fname_new
;
656 status
= copy_smb_filename(fsp
, smb_fname_in
, &smb_fname_new
);
657 if (!NT_STATUS_IS_OK(status
)) {
661 TALLOC_FREE(fsp
->fsp_name
);
662 fsp
->fsp_name
= smb_fname_new
;
664 return file_name_hash(fsp
->conn
,
665 smb_fname_str_dbg(fsp
->fsp_name
),