smbd/ioctl: add FSCTL_SET_ZERO_DATA support
[Samba.git] / source3 / smbd / files.c
blob5b3741bb8658a487da72ea76649a363b028a9e60
1 /*
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/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "libcli/security/security.h"
24 #include "util_tdb.h"
25 #include <ccan/hash/hash.h>
26 #include "lib/util/bitmap.h"
28 #define FILE_HANDLE_OFFSET 0x1000
30 /**
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);
41 if (fsp == NULL) {
42 goto fail;
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) {
52 goto fail;
55 fsp->fh->ref_count = 1;
56 fsp->fh->fd = -1;
58 fsp->fnum = FNUM_FIELD_INVALID;
59 fsp->conn = conn;
61 DLIST_ADD(sconn->files, fsp);
62 sconn->num_files += 1;
64 conn->num_files_open++;
66 *result = fsp;
67 return NT_STATUS_OK;
69 fail:
70 if (fsp != NULL) {
71 TALLOC_FREE(fsp->fh);
73 TALLOC_FREE(fsp);
75 return status;
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;
86 files_struct *fsp;
87 NTSTATUS status;
89 status = fsp_new(conn, conn, &fsp);
90 if (!NT_STATUS_IS_OK(status)) {
91 return status;
94 GetTimeOfDay(&fsp->open_time);
96 if (req) {
97 struct smbXsrv_connection *xconn = req->xconn;
98 struct smbXsrv_open *op = NULL;
99 NTTIME now = timeval_to_nttime(&fsp->open_time);
101 status = smbXsrv_open_create(xconn,
102 conn->session_info,
103 now, &op);
104 if (!NT_STATUS_IS_OK(status)) {
105 file_free(NULL, fsp);
106 return status;
108 fsp->op = op;
109 op->compat = fsp;
110 fsp->fnum = op->local_id;
111 fsp->fh->gen_id = smbXsrv_open_hash(op);
112 } else {
113 DEBUG(10, ("%s: req==NULL, INTERNAL_OPEN_ONLY, smbXsrv_open "
114 "allocated\n", __func__));
118 * Create an smb_filename with "" for the base_name. There are very
119 * few NULL checks, so make sure it's initialized with something. to
120 * be safe until an audit can be done.
122 fsp->fsp_name = synthetic_smb_fname(fsp, "", NULL, NULL);
123 if (fsp->fsp_name == NULL) {
124 file_free(NULL, fsp);
125 return NT_STATUS_NO_MEMORY;
128 DEBUG(5,("allocated file structure %s (%u used)\n",
129 fsp_fnum_dbg(fsp), (unsigned int)sconn->num_files));
131 if (req != NULL) {
132 fsp->mid = req->mid;
133 req->chain_fsp = fsp;
136 /* A new fsp invalidates the positive and
137 negative fsp_fi_cache as the new fsp is pushed
138 at the start of the list and we search from
139 a cache hit to the *end* of the list. */
141 ZERO_STRUCT(sconn->fsp_fi_cache);
143 *result = fsp;
144 return NT_STATUS_OK;
147 /****************************************************************************
148 Close all open files for a connection.
149 ****************************************************************************/
151 void file_close_conn(connection_struct *conn)
153 files_struct *fsp, *next;
155 for (fsp=conn->sconn->files; fsp; fsp=next) {
156 next = fsp->next;
157 if (fsp->conn != conn) {
158 continue;
160 if (fsp->op != NULL && fsp->op->global->durable) {
162 * A tree disconnect closes a durable handle
164 fsp->op->global->durable = false;
166 close_file(NULL, fsp, SHUTDOWN_CLOSE);
170 /****************************************************************************
171 Close all open files for a pid and a vuid.
172 ****************************************************************************/
174 void file_close_pid(struct smbd_server_connection *sconn, uint16 smbpid,
175 uint64_t vuid)
177 files_struct *fsp, *next;
179 for (fsp=sconn->files;fsp;fsp=next) {
180 next = fsp->next;
181 if ((fsp->file_pid == smbpid) && (fsp->vuid == vuid)) {
182 close_file(NULL, fsp, SHUTDOWN_CLOSE);
187 /****************************************************************************
188 Initialise file structures.
189 ****************************************************************************/
191 static int files_max_open_fds;
193 bool file_init_global(void)
195 int request_max = lp_max_open_files();
196 int real_lim;
197 int real_max;
199 if (files_max_open_fds != 0) {
200 return true;
204 * Set the max_open files to be the requested
205 * max plus a fudgefactor to allow for the extra
206 * fd's we need such as log files etc...
208 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
210 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
212 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
213 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
216 if (real_max != request_max) {
217 DEBUG(1, ("file_init_global: Information only: requested %d "
218 "open files, %d are available.\n",
219 request_max, real_max));
222 SMB_ASSERT(real_max > 100);
224 files_max_open_fds = real_max;
225 return true;
228 bool file_init(struct smbd_server_connection *sconn)
230 bool ok;
232 ok = file_init_global();
233 if (!ok) {
234 return false;
237 sconn->real_max_open_files = files_max_open_fds;
239 return true;
242 /****************************************************************************
243 Close files open by a specified vuid.
244 ****************************************************************************/
246 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
248 files_struct *fsp, *next;
250 for (fsp=sconn->files; fsp; fsp=next) {
251 next=fsp->next;
252 if (fsp->vuid == vuid) {
253 close_file(NULL, fsp, SHUTDOWN_CLOSE);
259 * Walk the files table until "fn" returns non-NULL
262 struct files_struct *files_forall(
263 struct smbd_server_connection *sconn,
264 struct files_struct *(*fn)(struct files_struct *fsp,
265 void *private_data),
266 void *private_data)
268 struct files_struct *fsp, *next;
270 for (fsp = sconn->files; fsp; fsp = next) {
271 struct files_struct *ret;
272 next = fsp->next;
273 ret = fn(fsp, private_data);
274 if (ret != NULL) {
275 return ret;
278 return NULL;
281 /****************************************************************************
282 Find a fsp given a file descriptor.
283 ****************************************************************************/
285 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
287 int count=0;
288 files_struct *fsp;
290 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
291 if (fsp->fh->fd == fd) {
292 if (count > 10) {
293 DLIST_PROMOTE(sconn->files, fsp);
295 return fsp;
299 return NULL;
302 /****************************************************************************
303 Find a fsp given a device, inode and file_id.
304 ****************************************************************************/
306 files_struct *file_find_dif(struct smbd_server_connection *sconn,
307 struct file_id id, unsigned long gen_id)
309 int count=0;
310 files_struct *fsp;
312 if (gen_id == 0) {
313 return NULL;
316 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
317 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
318 if (file_id_equal(&fsp->file_id, &id) &&
319 fsp->fh->gen_id == gen_id ) {
320 if (count > 10) {
321 DLIST_PROMOTE(sconn->files, fsp);
323 /* Paranoia check. */
324 if ((fsp->fh->fd == -1) &&
325 (fsp->oplock_type != NO_OPLOCK &&
326 fsp->oplock_type != LEASE_OPLOCK)) {
327 DEBUG(0,("file_find_dif: file %s file_id = "
328 "%s, gen = %u oplock_type = %u is a "
329 "stat open with oplock type !\n",
330 fsp_str_dbg(fsp),
331 file_id_string_tos(&fsp->file_id),
332 (unsigned int)fsp->fh->gen_id,
333 (unsigned int)fsp->oplock_type ));
334 smb_panic("file_find_dif");
336 return fsp;
340 return NULL;
343 /****************************************************************************
344 Find the first fsp given a device and inode.
345 We use a singleton cache here to speed up searching from getfilepathinfo
346 calls.
347 ****************************************************************************/
349 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
350 struct file_id id)
352 files_struct *fsp;
354 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
355 /* Positive or negative cache hit. */
356 return sconn->fsp_fi_cache.fsp;
359 sconn->fsp_fi_cache.id = id;
361 for (fsp=sconn->files;fsp;fsp=fsp->next) {
362 if (file_id_equal(&fsp->file_id, &id)) {
363 /* Setup positive cache. */
364 sconn->fsp_fi_cache.fsp = fsp;
365 return fsp;
369 /* Setup negative cache. */
370 sconn->fsp_fi_cache.fsp = NULL;
371 return NULL;
374 /****************************************************************************
375 Find the next fsp having the same device and inode.
376 ****************************************************************************/
378 files_struct *file_find_di_next(files_struct *start_fsp)
380 files_struct *fsp;
382 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
383 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
384 return fsp;
388 return NULL;
391 struct files_struct *file_find_one_fsp_from_lease_key(
392 struct smbd_server_connection *sconn,
393 const struct smb2_lease_key *lease_key)
395 struct files_struct *fsp;
397 for (fsp = sconn->files; fsp; fsp=fsp->next) {
398 if ((fsp->lease != NULL) &&
399 (fsp->lease->lease.lease_key.data[0] ==
400 lease_key->data[0]) &&
401 (fsp->lease->lease.lease_key.data[1] ==
402 lease_key->data[1])) {
403 return fsp;
406 return NULL;
409 /****************************************************************************
410 Find any fsp open with a pathname below that of an already open path.
411 ****************************************************************************/
413 bool file_find_subpath(files_struct *dir_fsp)
415 files_struct *fsp;
416 size_t dlen;
417 char *d_fullname = NULL;
419 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
420 dir_fsp->conn->connectpath,
421 dir_fsp->fsp_name->base_name);
423 if (!d_fullname) {
424 return false;
427 dlen = strlen(d_fullname);
429 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
430 char *d1_fullname;
432 if (fsp == dir_fsp) {
433 continue;
436 d1_fullname = talloc_asprintf(talloc_tos(),
437 "%s/%s",
438 fsp->conn->connectpath,
439 fsp->fsp_name->base_name);
442 * If the open file has a path that is a longer
443 * component, then it's a subpath.
445 if (strnequal(d_fullname, d1_fullname, dlen) &&
446 (d1_fullname[dlen] == '/')) {
447 TALLOC_FREE(d1_fullname);
448 TALLOC_FREE(d_fullname);
449 return true;
451 TALLOC_FREE(d1_fullname);
454 TALLOC_FREE(d_fullname);
455 return false;
458 /****************************************************************************
459 Sync open files on a connection.
460 ****************************************************************************/
462 void file_sync_all(connection_struct *conn)
464 files_struct *fsp, *next;
466 for (fsp=conn->sconn->files; fsp; fsp=next) {
467 next=fsp->next;
468 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
469 sync_file(conn, fsp, True /* write through */);
474 /****************************************************************************
475 Free up a fsp.
476 ****************************************************************************/
478 void fsp_free(files_struct *fsp)
480 struct smbd_server_connection *sconn = fsp->conn->sconn;
482 DLIST_REMOVE(sconn->files, fsp);
483 SMB_ASSERT(sconn->num_files > 0);
484 sconn->num_files--;
486 TALLOC_FREE(fsp->fake_file_handle);
488 if (fsp->fh->ref_count == 1) {
489 TALLOC_FREE(fsp->fh);
490 } else {
491 fsp->fh->ref_count--;
494 if (fsp->lease != NULL) {
495 if (fsp->lease->ref_count == 1) {
496 TALLOC_FREE(fsp->lease);
497 } else {
498 fsp->lease->ref_count--;
502 fsp->conn->num_files_open--;
504 /* this is paranoia, just in case someone tries to reuse the
505 information */
506 ZERO_STRUCTP(fsp);
508 /* fsp->fsp_name is a talloc child and is free'd automatically. */
509 TALLOC_FREE(fsp);
512 void file_free(struct smb_request *req, files_struct *fsp)
514 struct smbd_server_connection *sconn = fsp->conn->sconn;
515 uint64_t fnum = fsp->fnum;
517 if (fsp->notify) {
518 struct notify_context *notify_ctx =
519 fsp->conn->sconn->notify_ctx;
520 notify_remove(notify_ctx, fsp);
521 TALLOC_FREE(fsp->notify);
524 /* Ensure this event will never fire. */
525 TALLOC_FREE(fsp->update_write_time_event);
527 if (fsp->op != NULL) {
528 fsp->op->compat = NULL;
530 TALLOC_FREE(fsp->op);
532 if ((req != NULL) && (fsp == req->chain_fsp)) {
533 req->chain_fsp = NULL;
537 * Clear all possible chained fsp
538 * pointers in the SMB2 request queue.
540 if (req != NULL && req->smb2req) {
541 remove_smb2_chained_fsp(fsp);
544 /* Closing a file can invalidate the positive cache. */
545 if (fsp == sconn->fsp_fi_cache.fsp) {
546 ZERO_STRUCT(sconn->fsp_fi_cache);
549 /* Drop all remaining extensions. */
550 vfs_remove_all_fsp_extensions(fsp);
552 fsp_free(fsp);
554 DEBUG(5,("freed files structure %llu (%u used)\n",
555 (unsigned long long)fnum, (unsigned int)sconn->num_files));
558 /****************************************************************************
559 Get an fsp from a packet given a 16 bit fnum.
560 ****************************************************************************/
562 files_struct *file_fsp(struct smb_request *req, uint16 fid)
564 struct smbXsrv_open *op;
565 NTSTATUS status;
566 NTTIME now = 0;
567 files_struct *fsp;
569 if (req == NULL) {
571 * We should never get here. req==NULL could in theory
572 * only happen from internal opens with a non-zero
573 * root_dir_fid. Internal opens just don't do that, at
574 * least they are not supposed to do so. And if they
575 * start to do so, they better fake up a smb_request
576 * from which we get the right smbd_server_conn. While
577 * this should never happen, let's return NULL here.
579 return NULL;
582 if (req->chain_fsp != NULL) {
583 if (req->chain_fsp->deferred_close) {
584 return NULL;
586 return req->chain_fsp;
589 if (req->xconn == NULL) {
590 return NULL;
593 now = timeval_to_nttime(&req->request_time);
595 status = smb1srv_open_lookup(req->xconn,
596 fid, now, &op);
597 if (!NT_STATUS_IS_OK(status)) {
598 return NULL;
601 fsp = op->compat;
602 if (fsp == NULL) {
603 return NULL;
606 if (fsp->deferred_close) {
607 return NULL;
610 req->chain_fsp = fsp;
611 return fsp;
614 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
615 uint64_t persistent_id,
616 uint64_t volatile_id)
618 struct smbXsrv_open *op;
619 NTSTATUS status;
620 NTTIME now = 0;
621 struct files_struct *fsp;
623 now = timeval_to_nttime(&smb2req->request_time);
625 status = smb2srv_open_lookup(smb2req->xconn,
626 persistent_id, volatile_id,
627 now, &op);
628 if (!NT_STATUS_IS_OK(status)) {
629 return NULL;
632 fsp = op->compat;
633 if (fsp == NULL) {
634 return NULL;
637 if (smb2req->tcon == NULL) {
638 return NULL;
641 if (smb2req->tcon->compat != fsp->conn) {
642 return NULL;
645 if (smb2req->session == NULL) {
646 return NULL;
649 if (smb2req->session->compat == NULL) {
650 return NULL;
653 if (smb2req->session->compat->vuid != fsp->vuid) {
654 return NULL;
657 if (fsp->deferred_close) {
658 return NULL;
661 return fsp;
664 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
665 uint64_t persistent_id,
666 uint64_t volatile_id)
668 struct files_struct *fsp;
670 if (smb2req->compat_chain_fsp != NULL) {
671 if (smb2req->compat_chain_fsp->deferred_close) {
672 return NULL;
674 return smb2req->compat_chain_fsp;
677 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
678 if (fsp == NULL) {
679 return NULL;
682 smb2req->compat_chain_fsp = fsp;
683 return fsp;
686 /****************************************************************************
687 Duplicate the file handle part for a DOS or FCB open.
688 ****************************************************************************/
690 NTSTATUS dup_file_fsp(struct smb_request *req, files_struct *from,
691 uint32 access_mask, uint32 share_access,
692 uint32 create_options, files_struct *to)
694 /* this can never happen for print files */
695 SMB_ASSERT(from->print_file == NULL);
697 TALLOC_FREE(to->fh);
699 to->fh = from->fh;
700 to->fh->ref_count++;
702 to->file_id = from->file_id;
703 to->initial_allocation_size = from->initial_allocation_size;
704 to->file_pid = from->file_pid;
705 to->vuid = from->vuid;
706 to->open_time = from->open_time;
707 to->access_mask = access_mask;
708 to->share_access = share_access;
709 to->oplock_type = from->oplock_type;
710 to->can_lock = from->can_lock;
711 to->can_read = ((access_mask & FILE_READ_DATA) != 0);
712 to->can_write =
713 CAN_WRITE(from->conn) &&
714 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
715 to->modified = from->modified;
716 to->is_directory = from->is_directory;
717 to->aio_write_behind = from->aio_write_behind;
719 return fsp_set_smb_fname(to, from->fsp_name);
723 * Return a jenkins hash of a pathname on a connection.
726 NTSTATUS file_name_hash(connection_struct *conn,
727 const char *name, uint32_t *p_name_hash)
729 char tmpbuf[PATH_MAX];
730 char *fullpath, *to_free;
731 ssize_t len;
733 /* Set the hash of the full pathname. */
735 len = full_path_tos(conn->connectpath, name, tmpbuf, sizeof(tmpbuf),
736 &fullpath, &to_free);
737 if (len == -1) {
738 return NT_STATUS_NO_MEMORY;
740 *p_name_hash = hash(fullpath, len+1, 0);
742 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
743 fullpath,
744 (unsigned int)*p_name_hash ));
746 TALLOC_FREE(to_free);
747 return NT_STATUS_OK;
751 * The only way that the fsp->fsp_name field should ever be set.
753 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
754 const struct smb_filename *smb_fname_in)
756 struct smb_filename *smb_fname_new;
758 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
759 if (smb_fname_new == NULL) {
760 return NT_STATUS_NO_MEMORY;
763 TALLOC_FREE(fsp->fsp_name);
764 fsp->fsp_name = smb_fname_new;
766 return file_name_hash(fsp->conn,
767 smb_fname_str_dbg(fsp->fsp_name),
768 &fsp->name_hash);
771 const struct GUID *fsp_client_guid(const files_struct *fsp)
773 return &fsp->conn->sconn->client->connections->smb2.client.guid;
776 uint32_t fsp_lease_type(struct files_struct *fsp)
778 if (fsp->oplock_type == LEASE_OPLOCK) {
779 return fsp->lease->lease.lease_state;
781 return map_oplock_to_lease_type(fsp->oplock_type);