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>
27 #define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
29 #define FILE_HANDLE_OFFSET 0x1000
31 /****************************************************************************
32 Return a unique number identifying this fsp over the life of this pid.
33 ****************************************************************************/
35 static unsigned long get_gen_count(struct smbd_server_connection
*sconn
)
37 sconn
->file_gen_counter
+= 1;
38 if (sconn
->file_gen_counter
== 0) {
39 sconn
->file_gen_counter
+= 1;
41 return sconn
->file_gen_counter
;
44 /****************************************************************************
45 Find first available file slot.
46 ****************************************************************************/
48 NTSTATUS
file_new(struct smb_request
*req
, connection_struct
*conn
,
49 files_struct
**result
)
51 struct smbd_server_connection
*sconn
= conn
->sconn
;
56 /* we want to give out file handles differently on each new
57 connection because of a common bug in MS clients where they try to
58 reuse a file descriptor from an earlier smb connection. This code
59 increases the chance that the errant client will get an error rather
60 than causing corruption */
61 if (sconn
->first_file
== 0) {
62 sconn
->first_file
= (sys_getpid() ^ (int)time(NULL
));
63 sconn
->first_file
%= sconn
->real_max_open_files
;
66 /* TODO: Port the id-tree implementation from Samba4 */
68 i
= bitmap_find(sconn
->file_bmap
, sconn
->first_file
);
70 DEBUG(0,("ERROR! Out of file structures\n"));
71 /* TODO: We have to unconditionally return a DOS error here,
72 * W2k3 even returns ERRDOS/ERRnofids for ntcreate&x with
73 * NTSTATUS negotiated */
74 return NT_STATUS_TOO_MANY_OPENED_FILES
;
78 * Make a child of the connection_struct as an fsp can't exist
79 * independent of a connection.
81 fsp
= talloc_zero(conn
, struct files_struct
);
83 return NT_STATUS_NO_MEMORY
;
87 * This can't be a child of fsp because the file_handle can be ref'd
88 * when doing a dos/fcb open, which will then share the file_handle
89 * across multiple fsps.
91 fsp
->fh
= talloc_zero(conn
, struct fd_handle
);
94 return NT_STATUS_NO_MEMORY
;
97 fsp
->fh
->ref_count
= 1;
101 fsp
->fh
->gen_id
= get_gen_count(sconn
);
102 GetTimeOfDay(&fsp
->open_time
);
104 sconn
->first_file
= (i
+1) % (sconn
->real_max_open_files
);
106 bitmap_set(sconn
->file_bmap
, i
);
107 sconn
->files_used
+= 1;
109 fsp
->fnum
= i
+ FILE_HANDLE_OFFSET
;
110 SMB_ASSERT(fsp
->fnum
< 65536);
113 * Create an smb_filename with "" for the base_name. There are very
114 * few NULL checks, so make sure it's initialized with something. to
115 * be safe until an audit can be done.
117 status
= create_synthetic_smb_fname(fsp
, "", NULL
, NULL
,
119 if (!NT_STATUS_IS_OK(status
)) {
121 TALLOC_FREE(fsp
->fh
);
124 DLIST_ADD(sconn
->files
, fsp
);
126 DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
127 i
, fsp
->fnum
, sconn
->files_used
));
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
);
140 conn
->num_files_open
++;
146 /****************************************************************************
147 Close all open files for a connection.
148 ****************************************************************************/
150 void file_close_conn(connection_struct
*conn
)
152 files_struct
*fsp
, *next
;
154 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
156 if (fsp
->conn
== conn
) {
157 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
162 /****************************************************************************
163 Close all open files for a pid and a vuid.
164 ****************************************************************************/
166 void file_close_pid(struct smbd_server_connection
*sconn
, uint16 smbpid
,
169 files_struct
*fsp
, *next
;
171 for (fsp
=sconn
->files
;fsp
;fsp
=next
) {
173 if ((fsp
->file_pid
== smbpid
) && (fsp
->vuid
== vuid
)) {
174 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
179 /****************************************************************************
180 Initialise file structures.
181 ****************************************************************************/
183 bool file_init(struct smbd_server_connection
*sconn
)
185 int request_max_open_files
= lp_max_open_files();
189 * Set the max_open files to be the requested
190 * max plus a fudgefactor to allow for the extra
191 * fd's we need such as log files etc...
193 real_lim
= set_maxfiles(request_max_open_files
+ MAX_OPEN_FUDGEFACTOR
);
195 sconn
->real_max_open_files
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
197 if (sconn
->real_max_open_files
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
199 sconn
->real_max_open_files
=
200 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
202 if(sconn
->real_max_open_files
!= request_max_open_files
) {
203 DEBUG(1, ("file_init: Information only: requested %d "
204 "open files, %d are available.\n",
205 request_max_open_files
, sconn
->real_max_open_files
));
208 SMB_ASSERT(sconn
->real_max_open_files
> 100);
210 sconn
->file_bmap
= bitmap_talloc(sconn
, sconn
->real_max_open_files
);
212 if (!sconn
->file_bmap
) {
218 /****************************************************************************
219 Close files open by a specified vuid.
220 ****************************************************************************/
222 void file_close_user(struct smbd_server_connection
*sconn
, int vuid
)
224 files_struct
*fsp
, *next
;
226 for (fsp
=sconn
->files
; fsp
; fsp
=next
) {
228 if (fsp
->vuid
== vuid
) {
229 close_file(NULL
, fsp
, SHUTDOWN_CLOSE
);
235 * Walk the files table until "fn" returns non-NULL
238 struct files_struct
*files_forall(
239 struct smbd_server_connection
*sconn
,
240 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
244 struct files_struct
*fsp
, *next
;
246 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
247 struct files_struct
*ret
;
249 ret
= fn(fsp
, private_data
);
257 /****************************************************************************
258 Find a fsp given a file descriptor.
259 ****************************************************************************/
261 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
266 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
267 if (fsp
->fh
->fd
== fd
) {
269 DLIST_PROMOTE(sconn
->files
, fsp
);
278 /****************************************************************************
279 Find a fsp given a device, inode and file_id.
280 ****************************************************************************/
282 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
283 struct file_id id
, unsigned long gen_id
)
288 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
289 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
290 if (file_id_equal(&fsp
->file_id
, &id
) &&
291 fsp
->fh
->gen_id
== gen_id
) {
293 DLIST_PROMOTE(sconn
->files
, fsp
);
295 /* Paranoia check. */
296 if ((fsp
->fh
->fd
== -1) &&
297 (fsp
->oplock_type
!= NO_OPLOCK
) &&
298 (fsp
->oplock_type
!= FAKE_LEVEL_II_OPLOCK
)) {
299 DEBUG(0,("file_find_dif: file %s file_id = "
300 "%s, gen = %u oplock_type = %u is a "
301 "stat open with oplock type !\n",
303 file_id_string_tos(&fsp
->file_id
),
304 (unsigned int)fsp
->fh
->gen_id
,
305 (unsigned int)fsp
->oplock_type
));
306 smb_panic("file_find_dif");
315 /****************************************************************************
316 Find the first fsp given a device and inode.
317 We use a singleton cache here to speed up searching from getfilepathinfo
319 ****************************************************************************/
321 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
326 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
327 /* Positive or negative cache hit. */
328 return sconn
->fsp_fi_cache
.fsp
;
331 sconn
->fsp_fi_cache
.id
= id
;
333 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
334 if (file_id_equal(&fsp
->file_id
, &id
)) {
335 /* Setup positive cache. */
336 sconn
->fsp_fi_cache
.fsp
= fsp
;
341 /* Setup negative cache. */
342 sconn
->fsp_fi_cache
.fsp
= NULL
;
346 /****************************************************************************
347 Find the next fsp having the same device and inode.
348 ****************************************************************************/
350 files_struct
*file_find_di_next(files_struct
*start_fsp
)
354 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
355 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
363 /****************************************************************************
364 Find any fsp open with a pathname below that of an already open path.
365 ****************************************************************************/
367 bool file_find_subpath(files_struct
*dir_fsp
)
371 char *d_fullname
= NULL
;
373 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
374 dir_fsp
->conn
->connectpath
,
375 dir_fsp
->fsp_name
->base_name
);
381 dlen
= strlen(d_fullname
);
383 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
386 if (fsp
== dir_fsp
) {
390 d1_fullname
= talloc_asprintf(talloc_tos(),
392 fsp
->conn
->connectpath
,
393 fsp
->fsp_name
->base_name
);
396 * If the open file has a path that is a longer
397 * component, then it's a subpath.
399 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
400 (d1_fullname
[dlen
] == '/')) {
401 TALLOC_FREE(d1_fullname
);
402 TALLOC_FREE(d_fullname
);
405 TALLOC_FREE(d1_fullname
);
408 TALLOC_FREE(d_fullname
);
412 /****************************************************************************
413 Sync open files on a connection.
414 ****************************************************************************/
416 void file_sync_all(connection_struct
*conn
)
418 files_struct
*fsp
, *next
;
420 for (fsp
=conn
->sconn
->files
; fsp
; fsp
=next
) {
422 if ((conn
== fsp
->conn
) && (fsp
->fh
->fd
!= -1)) {
423 sync_file(conn
, fsp
, True
/* write through */);
428 /****************************************************************************
430 ****************************************************************************/
432 void file_free(struct smb_request
*req
, files_struct
*fsp
)
434 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
436 DLIST_REMOVE(sconn
->files
, fsp
);
438 TALLOC_FREE(fsp
->fake_file_handle
);
440 if (fsp
->fh
->ref_count
== 1) {
441 TALLOC_FREE(fsp
->fh
);
443 fsp
->fh
->ref_count
--;
447 if (fsp
->is_directory
) {
448 notify_remove_onelevel(fsp
->conn
->notify_ctx
,
451 notify_remove(fsp
->conn
->notify_ctx
, fsp
);
452 TALLOC_FREE(fsp
->notify
);
455 /* Ensure this event will never fire. */
456 TALLOC_FREE(fsp
->oplock_timeout
);
458 /* Ensure this event will never fire. */
459 TALLOC_FREE(fsp
->update_write_time_event
);
461 bitmap_clear(sconn
->file_bmap
, fsp
->fnum
- FILE_HANDLE_OFFSET
);
464 DEBUG(5,("freed files structure %d (%d used)\n",
465 fsp
->fnum
, sconn
->files_used
));
467 fsp
->conn
->num_files_open
--;
469 if ((req
!= NULL
) && (fsp
== req
->chain_fsp
)) {
470 req
->chain_fsp
= NULL
;
474 * Clear all possible chained fsp
475 * pointers in the SMB2 request queue.
477 if (req
!= NULL
&& req
->smb2req
) {
478 remove_smb2_chained_fsp(fsp
);
481 /* Closing a file can invalidate the positive cache. */
482 if (fsp
== sconn
->fsp_fi_cache
.fsp
) {
483 ZERO_STRUCT(sconn
->fsp_fi_cache
);
486 /* Drop all remaining extensions. */
487 while (fsp
->vfs_extension
) {
488 vfs_remove_fsp_extension(fsp
->vfs_extension
->owner
, fsp
);
491 /* this is paranoia, just in case someone tries to reuse the
495 /* fsp->fsp_name is a talloc child and is free'd automatically. */
499 /****************************************************************************
500 Get an fsp from a 16 bit fnum.
501 ****************************************************************************/
503 static struct files_struct
*file_fnum(struct smbd_server_connection
*sconn
,
509 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
, count
++) {
510 if (fsp
->fnum
== fnum
) {
512 DLIST_PROMOTE(sconn
->files
, fsp
);
520 /****************************************************************************
521 Get an fsp from a packet given a 16 bit fnum.
522 ****************************************************************************/
524 files_struct
*file_fsp(struct smb_request
*req
, uint16 fid
)
530 * We should never get here. req==NULL could in theory
531 * only happen from internal opens with a non-zero
532 * root_dir_fid. Internal opens just don't do that, at
533 * least they are not supposed to do so. And if they
534 * start to do so, they better fake up a smb_request
535 * from which we get the right smbd_server_conn. While
536 * this should never happen, let's return NULL here.
541 if (req
->chain_fsp
!= NULL
) {
542 return req
->chain_fsp
;
545 fsp
= file_fnum(req
->sconn
, fid
);
547 req
->chain_fsp
= fsp
;
552 /****************************************************************************
553 Duplicate the file handle part for a DOS or FCB open.
554 ****************************************************************************/
556 NTSTATUS
dup_file_fsp(struct smb_request
*req
, files_struct
*from
,
557 uint32 access_mask
, uint32 share_access
,
558 uint32 create_options
, files_struct
*to
)
565 to
->file_id
= from
->file_id
;
566 to
->initial_allocation_size
= from
->initial_allocation_size
;
567 to
->mode
= from
->mode
;
568 to
->file_pid
= from
->file_pid
;
569 to
->vuid
= from
->vuid
;
570 to
->open_time
= from
->open_time
;
571 to
->access_mask
= access_mask
;
572 to
->share_access
= share_access
;
573 to
->oplock_type
= from
->oplock_type
;
574 to
->can_lock
= from
->can_lock
;
575 to
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
576 if (!CAN_WRITE(from
->conn
)) {
577 to
->can_write
= False
;
579 to
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ? True
: False
;
581 to
->modified
= from
->modified
;
582 to
->is_directory
= from
->is_directory
;
583 to
->aio_write_behind
= from
->aio_write_behind
;
585 if (from
->print_file
) {
586 to
->print_file
= talloc(to
, struct print_file_data
);
587 if (!to
->print_file
) return NT_STATUS_NO_MEMORY
;
588 to
->print_file
->rap_jobid
= from
->print_file
->rap_jobid
;
590 to
->print_file
= NULL
;
593 return fsp_set_smb_fname(to
, from
->fsp_name
);
597 * Return a jenkins hash of a pathname on a connection.
600 NTSTATUS
file_name_hash(connection_struct
*conn
,
601 const char *name
, uint32_t *p_name_hash
)
603 char *fullpath
= NULL
;
605 /* Set the hash of the full pathname. */
606 fullpath
= talloc_asprintf(talloc_tos(),
611 return NT_STATUS_NO_MEMORY
;
613 *p_name_hash
= hash(fullpath
, strlen(fullpath
) + 1, 0);
615 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
617 (unsigned int)*p_name_hash
));
619 TALLOC_FREE(fullpath
);
624 * The only way that the fsp->fsp_name field should ever be set.
626 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
627 const struct smb_filename
*smb_fname_in
)
630 struct smb_filename
*smb_fname_new
;
632 status
= copy_smb_filename(fsp
, smb_fname_in
, &smb_fname_new
);
633 if (!NT_STATUS_IS_OK(status
)) {
637 TALLOC_FREE(fsp
->fsp_name
);
638 fsp
->fsp_name
= smb_fname_new
;
640 return file_name_hash(fsp
->conn
,
641 smb_fname_str_dbg(fsp
->fsp_name
),