ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source3 / smbd / vfs.c
blob9940dee0e82ff7193265fb79ff5bd556dff98769
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 VFS initialisation and support functions
5 Copyright (C) Tim Potter 1999
6 Copyright (C) Alexander Bokovoy 2002
7 Copyright (C) James Peach 2006
8 Copyright (C) Volker Lendecke 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 This work was sponsored by Optifacio Software Services, Inc.
26 #include "includes.h"
27 #include "system/filesys.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "../lib/util/memcache.h"
31 #include "transfer_file.h"
32 #include "ntioctl.h"
33 #include "lib/util/tevent_unix.h"
34 #include "lib/util/tevent_ntstatus.h"
35 #include "lib/util/sys_rw.h"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_VFS
40 static_decl_vfs;
42 struct vfs_fsp_data {
43 struct vfs_fsp_data *next;
44 struct vfs_handle_struct *owner;
45 void (*destroy)(void *p_data);
46 void *_dummy_;
47 /* NOTE: This structure contains four pointers so that we can guarantee
48 * that the end of the structure is always both 4-byte and 8-byte aligned.
52 struct vfs_init_function_entry {
53 char *name;
54 struct vfs_init_function_entry *prev, *next;
55 const struct vfs_fn_pointers *fns;
58 /****************************************************************************
59 maintain the list of available backends
60 ****************************************************************************/
62 static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
64 struct vfs_init_function_entry *entry = backends;
66 DEBUG(10, ("vfs_find_backend_entry called for %s\n", name));
68 while(entry) {
69 if (strcmp(entry->name, name)==0) return entry;
70 entry = entry->next;
73 return NULL;
76 NTSTATUS smb_register_vfs(int version, const char *name,
77 const struct vfs_fn_pointers *fns)
79 struct vfs_init_function_entry *entry = backends;
81 if ((version != SMB_VFS_INTERFACE_VERSION)) {
82 DEBUG(0, ("Failed to register vfs module.\n"
83 "The module was compiled against SMB_VFS_INTERFACE_VERSION %d,\n"
84 "current SMB_VFS_INTERFACE_VERSION is %d.\n"
85 "Please recompile against the current Samba Version!\n",
86 version, SMB_VFS_INTERFACE_VERSION));
87 return NT_STATUS_OBJECT_TYPE_MISMATCH;
90 if (!name || !name[0]) {
91 DEBUG(0,("smb_register_vfs() called with NULL pointer or empty name!\n"));
92 return NT_STATUS_INVALID_PARAMETER;
95 if (vfs_find_backend_entry(name)) {
96 DEBUG(0,("VFS module %s already loaded!\n", name));
97 return NT_STATUS_OBJECT_NAME_COLLISION;
100 entry = SMB_XMALLOC_P(struct vfs_init_function_entry);
101 entry->name = smb_xstrdup(name);
102 entry->fns = fns;
104 DLIST_ADD(backends, entry);
105 DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
106 return NT_STATUS_OK;
109 /****************************************************************************
110 initialise default vfs hooks
111 ****************************************************************************/
113 static void vfs_init_default(connection_struct *conn)
115 DEBUG(3, ("Initialising default vfs hooks\n"));
116 vfs_init_custom(conn, DEFAULT_VFS_MODULE_NAME);
119 /****************************************************************************
120 initialise custom vfs hooks
121 ****************************************************************************/
123 bool vfs_init_custom(connection_struct *conn, const char *vfs_object)
125 char *module_path = NULL;
126 char *module_name = NULL;
127 char *module_param = NULL, *p;
128 vfs_handle_struct *handle;
129 const struct vfs_init_function_entry *entry;
131 if (!conn||!vfs_object||!vfs_object[0]) {
132 DEBUG(0, ("vfs_init_custom() called with NULL pointer or "
133 "empty vfs_object!\n"));
134 return False;
137 if(!backends) {
138 static_init_vfs(NULL);
141 DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
143 module_path = smb_xstrdup(vfs_object);
145 p = strchr_m(module_path, ':');
147 if (p) {
148 *p = 0;
149 module_param = p+1;
150 trim_char(module_param, ' ', ' ');
153 trim_char(module_path, ' ', ' ');
155 module_name = smb_xstrdup(module_path);
157 if ((module_name[0] == '/') &&
158 (strcmp(module_path, DEFAULT_VFS_MODULE_NAME) != 0)) {
161 * Extract the module name from the path. Just use the base
162 * name of the last path component.
165 SAFE_FREE(module_name);
166 module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
168 p = strchr_m(module_name, '.');
170 if (p != NULL) {
171 *p = '\0';
175 /* First, try to load the module with the new module system */
176 entry = vfs_find_backend_entry(module_name);
177 if (!entry) {
178 NTSTATUS status;
180 DEBUG(5, ("vfs module [%s] not loaded - trying to load...\n",
181 vfs_object));
183 status = smb_load_module("vfs", module_path);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(0, ("error probing vfs module '%s': %s\n",
186 module_path, nt_errstr(status)));
187 goto fail;
190 entry = vfs_find_backend_entry(module_name);
191 if (!entry) {
192 DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
193 goto fail;
197 DEBUGADD(5,("Successfully loaded vfs module [%s] with the new modules system\n", vfs_object));
199 handle = talloc_zero(conn, vfs_handle_struct);
200 if (!handle) {
201 DEBUG(0,("TALLOC_ZERO() failed!\n"));
202 goto fail;
204 handle->conn = conn;
205 handle->fns = entry->fns;
206 if (module_param) {
207 handle->param = talloc_strdup(conn, module_param);
209 DLIST_ADD(conn->vfs_handles, handle);
211 SAFE_FREE(module_path);
212 SAFE_FREE(module_name);
213 return True;
215 fail:
216 SAFE_FREE(module_path);
217 SAFE_FREE(module_name);
218 return False;
221 /*****************************************************************
222 Allow VFS modules to extend files_struct with VFS-specific state.
223 This will be ok for small numbers of extensions, but might need to
224 be refactored if it becomes more widely used.
225 ******************************************************************/
227 #define EXT_DATA_AREA(e) ((uint8_t *)(e) + sizeof(struct vfs_fsp_data))
229 void *vfs_add_fsp_extension_notype(vfs_handle_struct *handle,
230 files_struct *fsp, size_t ext_size,
231 void (*destroy_fn)(void *p_data))
233 struct vfs_fsp_data *ext;
234 void * ext_data;
236 /* Prevent VFS modules adding multiple extensions. */
237 if ((ext_data = vfs_fetch_fsp_extension(handle, fsp))) {
238 return ext_data;
241 ext = talloc_zero_size(
242 handle->conn, sizeof(struct vfs_fsp_data) + ext_size);
243 if (ext == NULL) {
244 return NULL;
247 ext->owner = handle;
248 ext->next = fsp->vfs_extension;
249 ext->destroy = destroy_fn;
250 fsp->vfs_extension = ext;
251 return EXT_DATA_AREA(ext);
254 void vfs_remove_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
256 struct vfs_fsp_data *curr;
257 struct vfs_fsp_data *prev;
259 for (curr = fsp->vfs_extension, prev = NULL;
260 curr;
261 prev = curr, curr = curr->next) {
262 if (curr->owner == handle) {
263 if (prev) {
264 prev->next = curr->next;
265 } else {
266 fsp->vfs_extension = curr->next;
268 if (curr->destroy) {
269 curr->destroy(EXT_DATA_AREA(curr));
271 TALLOC_FREE(curr);
272 return;
277 void vfs_remove_all_fsp_extensions(files_struct *fsp)
279 struct vfs_fsp_data *curr;
280 struct vfs_fsp_data *next;
282 for (curr = fsp->vfs_extension; curr; curr = next) {
284 next = curr->next;
285 fsp->vfs_extension = next;
287 if (curr->destroy) {
288 curr->destroy(EXT_DATA_AREA(curr));
290 TALLOC_FREE(curr);
294 void *vfs_memctx_fsp_extension(vfs_handle_struct *handle,
295 const struct files_struct *fsp)
297 struct vfs_fsp_data *head;
299 for (head = fsp->vfs_extension; head; head = head->next) {
300 if (head->owner == handle) {
301 return head;
305 return NULL;
308 void *vfs_fetch_fsp_extension(vfs_handle_struct *handle,
309 const struct files_struct *fsp)
311 struct vfs_fsp_data *head;
313 head = (struct vfs_fsp_data *)vfs_memctx_fsp_extension(handle, fsp);
314 if (head != NULL) {
315 return EXT_DATA_AREA(head);
318 return NULL;
321 #undef EXT_DATA_AREA
324 * Ensure this module catches all VFS functions.
326 #ifdef DEVELOPER
327 void smb_vfs_assert_all_fns(const struct vfs_fn_pointers* fns,
328 const char *module)
330 bool missing_fn = false;
331 unsigned int idx;
332 const uintptr_t *end = (const uintptr_t *)(fns + 1);
334 for (idx = 0; ((const uintptr_t *)fns + idx) < end; idx++) {
335 if (*((const uintptr_t *)fns + idx) == 0) {
336 DBG_ERR("VFS function at index %d not implemented "
337 "in module %s\n", idx, module);
338 missing_fn = true;
342 if (missing_fn) {
343 smb_panic("Required VFS function not implemented in module.\n");
346 #else
347 void smb_vfs_assert_all_fns(const struct vfs_fn_pointers* fns,
348 const char *module)
351 #endif
353 /*****************************************************************
354 Generic VFS init.
355 ******************************************************************/
357 bool smbd_vfs_init(connection_struct *conn)
359 const char **vfs_objects;
360 unsigned int i = 0;
361 int j = 0;
363 /* Normal share - initialise with disk access functions */
364 vfs_init_default(conn);
366 /* No need to load vfs modules for printer connections */
367 if (conn->printer) {
368 return True;
371 if (lp_widelinks(SNUM(conn))) {
373 * As the widelinks logic is now moving into a
374 * vfs_widelinks module, we need to custom load
375 * it after the default module is initialized.
376 * That way no changes to smb.conf files are
377 * needed.
379 bool ok = vfs_init_custom(conn, "widelinks");
380 if (!ok) {
381 DBG_ERR("widelinks enabled and vfs_init_custom "
382 "failed for vfs_widelinks module\n");
383 return false;
387 vfs_objects = lp_vfs_objects(SNUM(conn));
389 /* Override VFS functions if 'vfs object' was not specified*/
390 if (!vfs_objects || !vfs_objects[0])
391 return True;
393 for (i=0; vfs_objects[i] ;) {
394 i++;
397 for (j=i-1; j >= 0; j--) {
398 if (!vfs_init_custom(conn, vfs_objects[j])) {
399 DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_objects[j]));
400 return False;
403 return True;
406 /*******************************************************************
407 Check if a file exists in the vfs.
408 ********************************************************************/
410 NTSTATUS vfs_file_exist(connection_struct *conn, struct smb_filename *smb_fname)
412 /* Only return OK if stat was successful and S_ISREG */
413 if ((SMB_VFS_STAT(conn, smb_fname) != -1) &&
414 S_ISREG(smb_fname->st.st_ex_mode)) {
415 return NT_STATUS_OK;
418 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
421 bool vfs_valid_pread_range(off_t offset, size_t length)
423 return sys_valid_io_range(offset, length);
426 bool vfs_valid_pwrite_range(off_t offset, size_t length)
429 * See MAXFILESIZE in [MS-FSA] 2.1.5.3 Server Requests a Write
431 static const uint64_t maxfilesize = 0xfffffff0000;
432 uint64_t last_byte_ofs;
433 bool ok;
435 ok = sys_valid_io_range(offset, length);
436 if (!ok) {
437 return false;
440 if (length == 0) {
441 return true;
444 last_byte_ofs = offset + length;
445 if (last_byte_ofs > maxfilesize) {
446 return false;
449 return true;
452 ssize_t vfs_pwrite_data(struct smb_request *req,
453 files_struct *fsp,
454 const char *buffer,
455 size_t N,
456 off_t offset)
458 size_t total=0;
459 ssize_t ret;
460 bool ok;
462 ok = vfs_valid_pwrite_range(offset, N);
463 if (!ok) {
464 errno = EINVAL;
465 return -1;
468 if (req && req->unread_bytes) {
469 int sockfd = req->xconn->transport.sock;
470 SMB_ASSERT(req->unread_bytes == N);
471 /* VFS_RECVFILE must drain the socket
472 * before returning. */
473 req->unread_bytes = 0;
475 * Leave the socket non-blocking and
476 * use SMB_VFS_RECVFILE. If it returns
477 * EAGAIN || EWOULDBLOCK temporarily set
478 * the socket blocking and retry
479 * the RECVFILE.
481 while (total < N) {
482 ret = SMB_VFS_RECVFILE(sockfd,
483 fsp,
484 offset + total,
485 N - total);
486 if (ret == 0 || (ret == -1 &&
487 (errno == EAGAIN ||
488 errno == EWOULDBLOCK))) {
489 int old_flags;
490 /* Ensure the socket is blocking. */
491 old_flags = fcntl(sockfd, F_GETFL, 0);
492 if (set_blocking(sockfd, true) == -1) {
493 return (ssize_t)-1;
495 ret = SMB_VFS_RECVFILE(sockfd,
496 fsp,
497 offset + total,
498 N - total);
499 if (fcntl(sockfd, F_SETFL, old_flags) == -1) {
500 return (ssize_t)-1;
502 if (ret == -1) {
503 return (ssize_t)-1;
505 total += ret;
506 return (ssize_t)total;
508 /* Any other error case. */
509 if (ret == -1) {
510 return ret;
512 total += ret;
514 return (ssize_t)total;
517 while (total < N) {
518 ret = SMB_VFS_PWRITE(fsp, buffer + total, N - total,
519 offset + total);
521 if (ret == -1)
522 return -1;
523 if (ret == 0)
524 return total;
526 total += ret;
528 return (ssize_t)total;
530 /****************************************************************************
531 An allocate file space call using the vfs interface.
532 Allocates space for a file from a filedescriptor.
533 Returns 0 on success, -1 on failure.
534 ****************************************************************************/
536 int vfs_allocate_file_space(files_struct *fsp, uint64_t len)
538 int ret;
539 connection_struct *conn = fsp->conn;
540 uint64_t space_avail;
541 uint64_t bsize,dfree,dsize;
542 NTSTATUS status;
543 bool ok;
546 * Actually try and commit the space on disk....
549 DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n",
550 fsp_str_dbg(fsp), (double)len));
552 ok = vfs_valid_pwrite_range((off_t)len, 0);
553 if (!ok) {
554 DEBUG(0,("vfs_allocate_file_space: %s negative/invalid len "
555 "requested.\n", fsp_str_dbg(fsp)));
556 errno = EINVAL;
557 return -1;
560 status = vfs_stat_fsp(fsp);
561 if (!NT_STATUS_IS_OK(status)) {
562 return -1;
565 if (len == (uint64_t)fsp->fsp_name->st.st_ex_size)
566 return 0;
568 if (len < (uint64_t)fsp->fsp_name->st.st_ex_size) {
569 /* Shrink - use ftruncate. */
571 DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current "
572 "size %.0f\n", fsp_str_dbg(fsp),
573 (double)fsp->fsp_name->st.st_ex_size));
575 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
577 ret = SMB_VFS_FTRUNCATE(fsp, (off_t)len);
579 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
581 return ret;
584 /* Grow - we need to test if we have enough space. */
586 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_GROW);
588 if (lp_strict_allocate(SNUM(fsp->conn))) {
589 /* See if we have a syscall that will allocate beyond
590 end-of-file without changing EOF. */
591 ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_FL_KEEP_SIZE,
592 0, len);
593 } else {
594 ret = 0;
597 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_GROW);
599 if (ret == 0) {
600 /* We changed the allocation size on disk, but not
601 EOF - exactly as required. We're done ! */
602 return 0;
605 if (ret == -1 && errno == ENOSPC) {
606 return -1;
609 len -= fsp->fsp_name->st.st_ex_size;
610 len /= 1024; /* Len is now number of 1k blocks needed. */
611 space_avail =
612 get_dfree_info(conn, fsp->fsp_name, &bsize, &dfree, &dsize);
613 if (space_avail == (uint64_t)-1) {
614 return -1;
617 DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, "
618 "needed blocks = %.0f, space avail = %.0f\n",
619 fsp_str_dbg(fsp), (double)fsp->fsp_name->st.st_ex_size, (double)len,
620 (double)space_avail));
622 if (len > space_avail) {
623 errno = ENOSPC;
624 return -1;
627 return 0;
630 /****************************************************************************
631 A vfs set_filelen call.
632 set the length of a file from a filedescriptor.
633 Returns 0 on success, -1 on failure.
634 ****************************************************************************/
636 int vfs_set_filelen(files_struct *fsp, off_t len)
638 int ret;
639 bool ok;
641 ok = vfs_valid_pwrite_range(len, 0);
642 if (!ok) {
643 errno = EINVAL;
644 return -1;
647 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
649 DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n",
650 fsp_str_dbg(fsp), (double)len));
651 if ((ret = SMB_VFS_FTRUNCATE(fsp, len)) != -1) {
652 notify_fname(fsp->conn,
653 NOTIFY_ACTION_MODIFIED |
654 NOTIFY_ACTION_DIRLEASE_BREAK,
655 FILE_NOTIFY_CHANGE_SIZE |
656 FILE_NOTIFY_CHANGE_ATTRIBUTES,
657 fsp->fsp_name,
658 fsp_get_smb2_lease(fsp));
661 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
663 return ret;
666 /****************************************************************************
667 A slow version of fallocate. Fallback code if SMB_VFS_FALLOCATE
668 fails. Needs to be outside of the default version of SMB_VFS_FALLOCATE
669 as this is also called from the default SMB_VFS_FTRUNCATE code.
670 Always extends the file size.
671 Returns 0 on success, -1 on failure.
672 ****************************************************************************/
674 #define SPARSE_BUF_WRITE_SIZE (32*1024)
676 int vfs_slow_fallocate(files_struct *fsp, off_t offset, off_t len)
678 ssize_t pwrite_ret;
679 size_t total = 0;
680 bool ok;
682 ok = vfs_valid_pwrite_range(offset, len);
683 if (!ok) {
684 errno = EINVAL;
685 return -1;
688 if (!sparse_buf) {
689 sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
690 if (!sparse_buf) {
691 errno = ENOMEM;
692 return -1;
696 while (total < len) {
697 size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (len - total));
699 pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
700 if (pwrite_ret == -1) {
701 int saved_errno = errno;
702 DEBUG(10,("vfs_slow_fallocate: SMB_VFS_PWRITE for file "
703 "%s failed with error %s\n",
704 fsp_str_dbg(fsp), strerror(saved_errno)));
705 errno = saved_errno;
706 return -1;
708 total += pwrite_ret;
711 return 0;
714 /****************************************************************************
715 A vfs fill sparse call.
716 Writes zeros from the end of file to len, if len is greater than EOF.
717 Used only by strict_sync.
718 Returns 0 on success, -1 on failure.
719 ****************************************************************************/
721 int vfs_fill_sparse(files_struct *fsp, off_t len)
723 int ret;
724 NTSTATUS status;
725 off_t offset;
726 size_t num_to_write;
727 bool ok;
729 ok = vfs_valid_pwrite_range(len, 0);
730 if (!ok) {
731 errno = EINVAL;
732 return -1;
735 status = vfs_stat_fsp(fsp);
736 if (!NT_STATUS_IS_OK(status)) {
737 return -1;
740 if (len <= fsp->fsp_name->st.st_ex_size) {
741 return 0;
744 #ifdef S_ISFIFO
745 if (S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
746 return 0;
748 #endif
750 DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to "
751 "len %.0f (%.0f bytes)\n", fsp_str_dbg(fsp),
752 (double)fsp->fsp_name->st.st_ex_size, (double)len,
753 (double)(len - fsp->fsp_name->st.st_ex_size)));
755 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_FILL_SPARSE);
757 offset = fsp->fsp_name->st.st_ex_size;
758 num_to_write = len - fsp->fsp_name->st.st_ex_size;
760 /* Only do this on non-stream file handles. */
761 if (!fsp_is_alternate_stream(fsp)) {
762 /* for allocation try fallocate first. This can fail on some
763 * platforms e.g. when the filesystem doesn't support it and no
764 * emulation is being done by the libc (like on AIX with JFS1). In that
765 * case we do our own emulation. fallocate implementations can
766 * return ENOTSUP or EINVAL in cases like that. */
767 ret = SMB_VFS_FALLOCATE(fsp, 0, offset, num_to_write);
768 if (ret == -1 && errno == ENOSPC) {
769 goto out;
771 if (ret == 0) {
772 goto out;
774 DEBUG(10,("vfs_fill_sparse: SMB_VFS_FALLOCATE failed with "
775 "error %d. Falling back to slow manual allocation\n", ret));
778 ret = vfs_slow_fallocate(fsp, offset, num_to_write);
780 out:
782 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_FILL_SPARSE);
783 return ret;
786 /*******************************************************************************
787 Set a fd into blocking/nonblocking mode through VFS
788 *******************************************************************************/
790 int vfs_set_blocking(files_struct *fsp, bool set)
792 int val;
793 #ifdef O_NONBLOCK
794 #define FLAG_TO_SET O_NONBLOCK
795 #else
796 #ifdef SYSV
797 #define FLAG_TO_SET O_NDELAY
798 #else /* BSD */
799 #define FLAG_TO_SET FNDELAY
800 #endif
801 #endif
803 if (fsp->fsp_flags.is_pathref) {
804 return 0;
807 val = SMB_VFS_FCNTL(fsp, F_GETFL, 0);
808 if (val == -1) {
809 return -1;
812 if (set) {
813 val &= ~FLAG_TO_SET;
814 } else {
815 val |= FLAG_TO_SET;
818 return SMB_VFS_FCNTL(fsp, F_SETFL, val);
819 #undef FLAG_TO_SET
822 /****************************************************************************
823 Transfer some data (n bytes) between two file_struct's.
824 ****************************************************************************/
826 static ssize_t vfs_pread_fn(void *file, void *buf, size_t len, off_t offset)
828 struct files_struct *fsp = (struct files_struct *)file;
830 return SMB_VFS_PREAD(fsp, buf, len, offset);
833 static ssize_t vfs_pwrite_fn(void *file, const void *buf, size_t len, off_t offset)
835 struct files_struct *fsp = (struct files_struct *)file;
837 return SMB_VFS_PWRITE(fsp, buf, len, offset);
840 off_t vfs_transfer_file(files_struct *in, files_struct *out, off_t n)
842 return transfer_file_internal((void *)in, (void *)out, n,
843 vfs_pread_fn, vfs_pwrite_fn);
846 /*******************************************************************
847 A vfs_readdir wrapper which just returns the file name.
848 ********************************************************************/
850 const char *vfs_readdirname(connection_struct *conn,
851 struct files_struct *dirfsp,
852 DIR *d,
853 char **talloced)
855 struct dirent *ptr= NULL;
856 const char *dname;
857 char *translated;
858 NTSTATUS status;
860 if (d == NULL) {
861 return(NULL);
864 ptr = SMB_VFS_READDIR(conn, dirfsp, d);
865 if (!ptr)
866 return(NULL);
868 dname = ptr->d_name;
870 status = SMB_VFS_TRANSLATE_NAME(conn, dname, vfs_translate_to_windows,
871 talloc_tos(), &translated);
872 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
873 *talloced = NULL;
874 return dname;
876 *talloced = translated;
877 if (!NT_STATUS_IS_OK(status)) {
878 return NULL;
880 return translated;
883 /*******************************************************************
884 A wrapper for vfs_chdir().
885 ********************************************************************/
887 int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
889 int ret;
890 struct smb_filename *cwd = NULL;
892 if (!LastDir) {
893 LastDir = SMB_STRDUP("");
896 if (ISDOT(smb_fname->base_name)) {
898 * passing a '.' is a noop,
899 * and we only expect this after
900 * everything is initialized.
902 * So the first vfs_ChDir() on a given
903 * connection_struct must not be '.'.
905 * Note: conn_new() sets
906 * conn->cwd_fsp->fh->fd = -1
907 * and vfs_ChDir() leaves with
908 * conn->cwd_fsp->fh->fd = AT_FDCWD
909 * on success!
911 if (fsp_get_pathref_fd(conn->cwd_fsp) != AT_FDCWD) {
913 * This should never happen and
914 * we might change this to
915 * SMB_ASSERT() in future.
917 DBG_ERR("Called with '.' as first operation!\n");
918 log_stack_trace();
919 errno = EINVAL;
920 return -1;
922 return 0;
925 if (smb_fname->base_name[0] == '/' &&
926 strcsequal(LastDir,smb_fname->base_name))
929 * conn->cwd_fsp->fsp_name and the kernel
930 * are already correct, but conn->cwd_fsp->fh->fd
931 * might still be -1 as initialized in conn_new().
933 * This can happen when a client made a 2nd
934 * tree connect to a share with the same underlying
935 * path (may or may not the same share).
937 fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
938 return 0;
941 DEBUG(4,("vfs_ChDir to %s\n", smb_fname->base_name));
943 ret = SMB_VFS_CHDIR(conn, smb_fname);
944 if (ret != 0) {
945 return -1;
949 * Always replace conn->cwd_fsp. We
950 * don't know if it's been modified by
951 * VFS modules in the stack.
953 fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
955 /* conn cache. */
956 cwd = vfs_GetWd(conn, conn);
957 if (cwd == NULL) {
959 * vfs_GetWd() failed.
960 * We must be able to read cwd.
961 * Return to original directory
962 * and return -1.
964 int saved_errno = errno;
966 if (conn->cwd_fsp->fsp_name == NULL) {
968 * Failed on the very first chdir()+getwd()
969 * for this connection. We can't
970 * continue.
972 smb_panic("conn->cwd getwd failed\n");
973 /* NOTREACHED */
974 return -1;
977 /* Return to the previous $cwd. */
978 ret = SMB_VFS_CHDIR(conn, conn->cwd_fsp->fsp_name);
979 if (ret != 0) {
980 smb_panic("conn->cwd getwd failed\n");
981 /* NOTREACHED */
982 return -1;
984 errno = saved_errno;
985 /* And fail the chdir(). */
986 return -1;
989 /* vfs_GetWd() succeeded. */
990 /* Replace global cache. */
991 SAFE_FREE(LastDir);
992 LastDir = SMB_STRDUP(smb_fname->base_name);
995 * (Indirect) Callers of vfs_ChDir() may still hold references to the
996 * old conn->cwd_fsp->fsp_name. Move it to talloc_tos(), that way
997 * callers can use it for the lifetime of the SMB request.
999 talloc_move(talloc_tos(), &conn->cwd_fsp->fsp_name);
1001 conn->cwd_fsp->fsp_name = talloc_move(conn->cwd_fsp, &cwd);
1003 DBG_INFO("vfs_ChDir got %s\n", fsp_str_dbg(conn->cwd_fsp));
1005 return ret;
1008 /*******************************************************************
1009 Return the absolute current directory path - given a UNIX pathname.
1010 Note that this path is returned in DOS format, not UNIX
1011 format. Note this can be called with conn == NULL.
1012 ********************************************************************/
1014 struct smb_filename *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
1016 struct smb_filename *current_dir_fname = NULL;
1017 struct file_id key;
1018 struct smb_filename *smb_fname_dot = NULL;
1019 struct smb_filename *smb_fname_full = NULL;
1020 struct smb_filename *result = NULL;
1022 if (!lp_getwd_cache()) {
1023 goto nocache;
1026 smb_fname_dot = synthetic_smb_fname(ctx,
1027 ".",
1028 NULL,
1029 NULL,
1032 if (smb_fname_dot == NULL) {
1033 errno = ENOMEM;
1034 goto out;
1037 if (SMB_VFS_STAT(conn, smb_fname_dot) == -1) {
1039 * Known to fail for root: the directory may be NFS-mounted
1040 * and exported with root_squash (so has no root access).
1042 DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
1043 "(NFS problem ?)\n", strerror(errno) ));
1044 goto nocache;
1047 key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
1049 smb_fname_full = (struct smb_filename *)memcache_lookup_talloc(
1050 smbd_memcache(),
1051 GETWD_CACHE,
1052 data_blob_const(&key, sizeof(key)));
1054 if (smb_fname_full == NULL) {
1055 goto nocache;
1058 if ((SMB_VFS_STAT(conn, smb_fname_full) == 0) &&
1059 (smb_fname_dot->st.st_ex_dev == smb_fname_full->st.st_ex_dev) &&
1060 (smb_fname_dot->st.st_ex_ino == smb_fname_full->st.st_ex_ino) &&
1061 (S_ISDIR(smb_fname_dot->st.st_ex_mode))) {
1063 * Ok, we're done
1064 * Note: smb_fname_full is owned by smbd_memcache()
1065 * so we must make a copy to return.
1067 result = cp_smb_filename(ctx, smb_fname_full);
1068 if (result == NULL) {
1069 errno = ENOMEM;
1071 goto out;
1074 nocache:
1077 * We don't have the information to hand so rely on traditional
1078 * methods. The very slow getcwd, which spawns a process on some
1079 * systems, or the not quite so bad getwd.
1082 current_dir_fname = SMB_VFS_GETWD(conn, ctx);
1083 if (current_dir_fname == NULL) {
1084 DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
1085 strerror(errno)));
1086 goto out;
1089 if (lp_getwd_cache() && VALID_STAT(smb_fname_dot->st)) {
1090 key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
1093 * smbd_memcache() will own current_dir_fname after the
1094 * memcache_add_talloc call, so we must make
1095 * a copy on ctx to return.
1097 result = cp_smb_filename(ctx, current_dir_fname);
1098 if (result == NULL) {
1099 errno = ENOMEM;
1103 * Ensure the memory going into the cache
1104 * doesn't have a destructor so it can be
1105 * cleanly freed.
1107 talloc_set_destructor(current_dir_fname, NULL);
1109 memcache_add_talloc(smbd_memcache(),
1110 GETWD_CACHE,
1111 data_blob_const(&key, sizeof(key)),
1112 &current_dir_fname);
1113 /* current_dir_fname is now == NULL here. */
1114 } else {
1115 /* current_dir_fname is already allocated on ctx. */
1116 result = current_dir_fname;
1119 out:
1120 TALLOC_FREE(smb_fname_dot);
1122 * Don't free current_dir_fname here. It's either been moved
1123 * to the memcache or is being returned in result.
1125 return result;
1129 * Ensure LSTAT is called for POSIX paths.
1131 int vfs_stat(struct connection_struct *conn,
1132 struct smb_filename *smb_fname)
1134 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1135 return SMB_VFS_LSTAT(conn, smb_fname);
1137 return SMB_VFS_STAT(conn, smb_fname);
1141 * XXX: This is temporary and there should be no callers of this once
1142 * smb_filename is plumbed through all path based operations.
1144 * Called when we know stream name parsing has already been done.
1146 int vfs_stat_smb_basename(struct connection_struct *conn,
1147 const struct smb_filename *smb_fname_in,
1148 SMB_STRUCT_STAT *psbuf)
1150 struct smb_filename smb_fname = {
1151 .base_name = discard_const_p(char, smb_fname_in->base_name),
1152 .flags = smb_fname_in->flags,
1153 .twrp = smb_fname_in->twrp,
1155 int ret;
1157 ret = vfs_stat(conn, &smb_fname);
1158 if (ret != -1) {
1159 *psbuf = smb_fname.st;
1161 return ret;
1165 * Ensure LSTAT is called for POSIX paths.
1168 NTSTATUS vfs_stat_fsp(files_struct *fsp)
1170 int ret;
1171 struct stat_ex saved_stat = fsp->fsp_name->st;
1173 if (fsp->fake_file_handle != NULL) {
1174 return NT_STATUS_OK;
1177 if (fsp_get_pathref_fd(fsp) == -1) {
1178 if (fsp->fsp_flags.posix_open) {
1179 ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1180 } else {
1181 ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1183 } else {
1184 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
1186 if (ret == -1) {
1187 return map_nt_error_from_unix(errno);
1189 update_stat_ex_from_saved_stat(&fsp->fsp_name->st, &saved_stat);
1190 fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
1191 return NT_STATUS_OK;
1194 void init_smb_file_time(struct smb_file_time *ft)
1196 struct timespec omit = make_omit_timespec();
1198 *ft = (struct smb_file_time) {
1199 .atime = omit,
1200 .ctime = omit,
1201 .mtime = omit,
1202 .create_time = omit,
1207 * Initialize num_streams and streams, then call VFS op streaminfo
1210 NTSTATUS vfs_fstreaminfo(struct files_struct *fsp,
1211 TALLOC_CTX *mem_ctx,
1212 unsigned int *num_streams,
1213 struct stream_struct **streams)
1215 *num_streams = 0;
1216 *streams = NULL;
1218 if (fsp == NULL) {
1220 * Callers may pass fsp == NULL when passing smb_fname->fsp of a
1221 * symlink. This is ok, handle it here, by just return no
1222 * streams on a symlink.
1224 return NT_STATUS_OK;
1227 if (fsp_get_pathref_fd(fsp) == -1) {
1229 * No streams on non-real files/directories.
1231 return NT_STATUS_OK;
1234 return SMB_VFS_FSTREAMINFO(fsp,
1235 mem_ctx,
1236 num_streams,
1237 streams);
1240 int vfs_fake_fd(void)
1242 int pipe_fds[2];
1243 int ret;
1246 * Return a valid fd, but ensure any attempt to use
1247 * it returns an error (EPIPE).
1249 ret = pipe(pipe_fds);
1250 if (ret != 0) {
1251 return -1;
1254 close(pipe_fds[1]);
1255 return pipe_fds[0];
1259 * This is just a helper to make
1260 * users of vfs_fake_fd() more symmetric
1262 int vfs_fake_fd_close(int fd)
1264 return close(fd);
1268 generate a file_id from a stat structure
1270 struct file_id vfs_file_id_from_sbuf(connection_struct *conn, const SMB_STRUCT_STAT *sbuf)
1272 return SMB_VFS_FILE_ID_CREATE(conn, sbuf);
1275 NTSTATUS vfs_at_fspcwd(TALLOC_CTX *mem_ctx,
1276 struct connection_struct *conn,
1277 struct files_struct **_fsp)
1279 struct files_struct *fsp = NULL;
1281 fsp = talloc_zero(mem_ctx, struct files_struct);
1282 if (fsp == NULL) {
1283 return NT_STATUS_NO_MEMORY;
1286 fsp->fsp_name = synthetic_smb_fname(fsp, ".", NULL, NULL, 0, 0);
1287 if (fsp->fsp_name == NULL) {
1288 TALLOC_FREE(fsp);
1289 return NT_STATUS_NO_MEMORY;
1292 fsp->fh = fd_handle_create(fsp);
1293 if (fsp->fh == NULL) {
1294 TALLOC_FREE(fsp);
1295 return NT_STATUS_NO_MEMORY;
1298 fsp_set_fd(fsp, AT_FDCWD);
1299 fsp->fnum = FNUM_FIELD_INVALID;
1300 fsp->conn = conn;
1302 *_fsp = fsp;
1303 return NT_STATUS_OK;
1306 uint32_t vfs_get_fs_capabilities(struct connection_struct *conn,
1307 enum timestamp_set_resolution *ts_res)
1309 const struct loadparm_substitution *lp_sub =
1310 loadparm_s3_global_substitution();
1311 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
1312 struct smb_filename *smb_fname_cpath = NULL;
1313 struct vfs_statvfs_struct statbuf;
1314 int ret;
1316 smb_fname_cpath = synthetic_smb_fname(talloc_tos(),
1317 conn->connectpath,
1318 NULL,
1319 NULL,
1322 if (smb_fname_cpath == NULL) {
1323 return caps;
1326 ZERO_STRUCT(statbuf);
1327 ret = SMB_VFS_STATVFS(conn, smb_fname_cpath, &statbuf);
1328 if (ret == 0) {
1329 caps = statbuf.FsCapabilities;
1332 if (lp_nt_acl_support(SNUM(conn))) {
1333 caps |= FILE_PERSISTENT_ACLS;
1336 caps |= lp_parm_int(SNUM(conn), "share", "fake_fscaps", 0);
1338 *ts_res = TIMESTAMP_SET_SECONDS;
1340 /* Work out what timestamp resolution we can
1341 * use when setting a timestamp. */
1343 ret = SMB_VFS_STAT(conn, smb_fname_cpath);
1344 if (ret == -1) {
1345 TALLOC_FREE(smb_fname_cpath);
1346 return caps;
1349 if (smb_fname_cpath->st.st_ex_mtime.tv_nsec ||
1350 smb_fname_cpath->st.st_ex_atime.tv_nsec ||
1351 smb_fname_cpath->st.st_ex_ctime.tv_nsec) {
1352 /* If any of the normal UNIX directory timestamps
1353 * have a non-zero tv_nsec component assume
1354 * we might be able to set sub-second timestamps.
1355 * See what filetime set primitives we have.
1357 #if defined(HAVE_UTIMENSAT)
1358 *ts_res = TIMESTAMP_SET_NT_OR_BETTER;
1359 #elif defined(HAVE_UTIMES)
1360 /* utimes allows msec timestamps to be set. */
1361 *ts_res = TIMESTAMP_SET_MSEC;
1362 #elif defined(HAVE_UTIME)
1363 /* utime only allows sec timestamps to be set. */
1364 *ts_res = TIMESTAMP_SET_SECONDS;
1365 #endif
1367 DBG_DEBUG("vfs_get_fs_capabilities: timestamp "
1368 "resolution of %s "
1369 "available on share %s, directory %s\n",
1370 *ts_res == TIMESTAMP_SET_MSEC ? "msec" : "sec",
1371 lp_servicename(talloc_tos(), lp_sub, conn->params->service),
1372 conn->connectpath );
1374 TALLOC_FREE(smb_fname_cpath);
1375 return caps;
1378 static struct smb_vfs_deny_state *smb_vfs_deny_global;
1380 void smb_vfs_assert_allowed(void)
1382 if (unlikely(smb_vfs_deny_global != NULL)) {
1383 DBG_ERR("Called with VFS denied by %s\n",
1384 smb_vfs_deny_global->location);
1385 smb_panic("Called with VFS denied!");
1389 void _smb_vfs_deny_push(struct smb_vfs_deny_state *state, const char *location)
1391 SMB_ASSERT(smb_vfs_deny_global != state);
1393 *state = (struct smb_vfs_deny_state) {
1394 .parent = smb_vfs_deny_global,
1395 .location = location,
1398 smb_vfs_deny_global = state;
1401 void _smb_vfs_deny_pop(struct smb_vfs_deny_state *state, const char *location)
1403 SMB_ASSERT(smb_vfs_deny_global == state);
1405 smb_vfs_deny_global = state->parent;
1407 *state = (struct smb_vfs_deny_state) { .parent = NULL, };
1410 #define VFS_FIND(__fn__) do { \
1411 if (unlikely(smb_vfs_deny_global != NULL)) { \
1412 DBG_ERR("Called with VFS denied by %s\n", \
1413 smb_vfs_deny_global->location); \
1414 smb_panic("Called with VFS denied!"); \
1416 while (handle->fns->__fn__##_fn==NULL) { \
1417 handle = handle->next; \
1419 } while(0)
1421 int smb_vfs_call_connect(struct vfs_handle_struct *handle,
1422 const char *service, const char *user)
1424 VFS_FIND(connect);
1425 return handle->fns->connect_fn(handle, service, user);
1428 void smb_vfs_call_disconnect(struct vfs_handle_struct *handle)
1430 VFS_FIND(disconnect);
1431 handle->fns->disconnect_fn(handle);
1434 uint64_t smb_vfs_call_disk_free(struct vfs_handle_struct *handle,
1435 const struct smb_filename *smb_fname,
1436 uint64_t *bsize,
1437 uint64_t *dfree,
1438 uint64_t *dsize)
1440 VFS_FIND(disk_free);
1441 return handle->fns->disk_free_fn(handle, smb_fname,
1442 bsize, dfree, dsize);
1445 int smb_vfs_call_get_quota(struct vfs_handle_struct *handle,
1446 const struct smb_filename *smb_fname,
1447 enum SMB_QUOTA_TYPE qtype,
1448 unid_t id,
1449 SMB_DISK_QUOTA *qt)
1451 VFS_FIND(get_quota);
1452 return handle->fns->get_quota_fn(handle, smb_fname, qtype, id, qt);
1455 int smb_vfs_call_set_quota(struct vfs_handle_struct *handle,
1456 enum SMB_QUOTA_TYPE qtype, unid_t id,
1457 SMB_DISK_QUOTA *qt)
1459 VFS_FIND(set_quota);
1460 return handle->fns->set_quota_fn(handle, qtype, id, qt);
1463 int smb_vfs_call_get_shadow_copy_data(struct vfs_handle_struct *handle,
1464 struct files_struct *fsp,
1465 struct shadow_copy_data *shadow_copy_data,
1466 bool labels)
1468 VFS_FIND(get_shadow_copy_data);
1469 return handle->fns->get_shadow_copy_data_fn(handle, fsp,
1470 shadow_copy_data,
1471 labels);
1473 int smb_vfs_call_statvfs(struct vfs_handle_struct *handle,
1474 const struct smb_filename *smb_fname,
1475 struct vfs_statvfs_struct *statbuf)
1477 VFS_FIND(statvfs);
1478 return handle->fns->statvfs_fn(handle, smb_fname, statbuf);
1481 uint32_t smb_vfs_call_fs_capabilities(struct vfs_handle_struct *handle,
1482 enum timestamp_set_resolution *p_ts_res)
1484 VFS_FIND(fs_capabilities);
1485 return handle->fns->fs_capabilities_fn(handle, p_ts_res);
1488 NTSTATUS smb_vfs_call_get_dfs_referrals(struct vfs_handle_struct *handle,
1489 struct dfs_GetDFSReferral *r)
1491 VFS_FIND(get_dfs_referrals);
1492 return handle->fns->get_dfs_referrals_fn(handle, r);
1495 NTSTATUS smb_vfs_call_create_dfs_pathat(struct vfs_handle_struct *handle,
1496 struct files_struct *dirfsp,
1497 const struct smb_filename *smb_fname,
1498 const struct referral *reflist,
1499 size_t referral_count)
1501 VFS_FIND(create_dfs_pathat);
1502 return handle->fns->create_dfs_pathat_fn(handle,
1503 dirfsp,
1504 smb_fname,
1505 reflist,
1506 referral_count);
1509 NTSTATUS smb_vfs_call_read_dfs_pathat(struct vfs_handle_struct *handle,
1510 TALLOC_CTX *mem_ctx,
1511 struct files_struct *dirfsp,
1512 struct smb_filename *smb_fname,
1513 struct referral **ppreflist,
1514 size_t *preferral_count)
1516 VFS_FIND(read_dfs_pathat);
1517 return handle->fns->read_dfs_pathat_fn(handle,
1518 mem_ctx,
1519 dirfsp,
1520 smb_fname,
1521 ppreflist,
1522 preferral_count);
1525 DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
1526 struct files_struct *fsp,
1527 const char *mask,
1528 uint32_t attributes)
1530 VFS_FIND(fdopendir);
1531 return handle->fns->fdopendir_fn(handle, fsp, mask, attributes);
1534 struct dirent *smb_vfs_call_readdir(struct vfs_handle_struct *handle,
1535 struct files_struct *dirfsp,
1536 DIR *dirp)
1538 VFS_FIND(readdir);
1539 return handle->fns->readdir_fn(handle, dirfsp, dirp);
1542 void smb_vfs_call_rewind_dir(struct vfs_handle_struct *handle,
1543 DIR *dirp)
1545 VFS_FIND(rewind_dir);
1546 handle->fns->rewind_dir_fn(handle, dirp);
1549 int smb_vfs_call_mkdirat(struct vfs_handle_struct *handle,
1550 struct files_struct *dirfsp,
1551 const struct smb_filename *smb_fname,
1552 mode_t mode)
1554 VFS_FIND(mkdirat);
1555 return handle->fns->mkdirat_fn(handle,
1556 dirfsp,
1557 smb_fname,
1558 mode);
1561 int smb_vfs_call_closedir(struct vfs_handle_struct *handle,
1562 DIR *dir)
1564 VFS_FIND(closedir);
1565 return handle->fns->closedir_fn(handle, dir);
1568 int smb_vfs_call_openat(struct vfs_handle_struct *handle,
1569 const struct files_struct *dirfsp,
1570 const struct smb_filename *smb_fname,
1571 struct files_struct *fsp,
1572 const struct vfs_open_how *how)
1574 VFS_FIND(openat);
1575 return handle->fns->openat_fn(handle,
1576 dirfsp,
1577 smb_fname,
1578 fsp,
1579 how);
1582 NTSTATUS smb_vfs_call_create_file(struct vfs_handle_struct *handle,
1583 struct smb_request *req,
1584 struct files_struct *dirfsp,
1585 struct smb_filename *smb_fname,
1586 uint32_t access_mask,
1587 uint32_t share_access,
1588 uint32_t create_disposition,
1589 uint32_t create_options,
1590 uint32_t file_attributes,
1591 uint32_t oplock_request,
1592 const struct smb2_lease *lease,
1593 uint64_t allocation_size,
1594 uint32_t private_flags,
1595 struct security_descriptor *sd,
1596 struct ea_list *ea_list,
1597 files_struct **result,
1598 int *pinfo,
1599 const struct smb2_create_blobs *in_context_blobs,
1600 struct smb2_create_blobs *out_context_blobs)
1602 VFS_FIND(create_file);
1603 return handle->fns->create_file_fn(
1604 handle, req, dirfsp, smb_fname,
1605 access_mask, share_access, create_disposition, create_options,
1606 file_attributes, oplock_request, lease, allocation_size,
1607 private_flags, sd, ea_list,
1608 result, pinfo, in_context_blobs, out_context_blobs);
1611 int smb_vfs_call_close(struct vfs_handle_struct *handle,
1612 struct files_struct *fsp)
1614 VFS_FIND(close);
1615 return handle->fns->close_fn(handle, fsp);
1618 ssize_t smb_vfs_call_pread(struct vfs_handle_struct *handle,
1619 struct files_struct *fsp, void *data, size_t n,
1620 off_t offset)
1622 VFS_FIND(pread);
1623 return handle->fns->pread_fn(handle, fsp, data, n, offset);
1626 struct smb_vfs_call_pread_state {
1627 ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1628 ssize_t retval;
1629 struct vfs_aio_state vfs_aio_state;
1632 static void smb_vfs_call_pread_done(struct tevent_req *subreq);
1634 struct tevent_req *smb_vfs_call_pread_send(struct vfs_handle_struct *handle,
1635 TALLOC_CTX *mem_ctx,
1636 struct tevent_context *ev,
1637 struct files_struct *fsp,
1638 void *data,
1639 size_t n, off_t offset)
1641 struct tevent_req *req, *subreq;
1642 struct smb_vfs_call_pread_state *state;
1644 req = tevent_req_create(mem_ctx, &state,
1645 struct smb_vfs_call_pread_state);
1646 if (req == NULL) {
1647 return NULL;
1649 VFS_FIND(pread_send);
1650 state->recv_fn = handle->fns->pread_recv_fn;
1652 subreq = handle->fns->pread_send_fn(handle, state, ev, fsp, data, n,
1653 offset);
1654 if (tevent_req_nomem(subreq, req)) {
1655 return tevent_req_post(req, ev);
1657 tevent_req_set_callback(subreq, smb_vfs_call_pread_done, req);
1658 return req;
1661 static void smb_vfs_call_pread_done(struct tevent_req *subreq)
1663 struct tevent_req *req = tevent_req_callback_data(
1664 subreq, struct tevent_req);
1665 struct smb_vfs_call_pread_state *state = tevent_req_data(
1666 req, struct smb_vfs_call_pread_state);
1668 state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1669 TALLOC_FREE(subreq);
1670 if (state->retval == -1) {
1671 tevent_req_error(req, state->vfs_aio_state.error);
1672 return;
1674 tevent_req_done(req);
1677 ssize_t SMB_VFS_PREAD_RECV(struct tevent_req *req,
1678 struct vfs_aio_state *vfs_aio_state)
1680 struct smb_vfs_call_pread_state *state = tevent_req_data(
1681 req, struct smb_vfs_call_pread_state);
1682 ssize_t retval;
1684 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1685 tevent_req_received(req);
1686 return -1;
1688 *vfs_aio_state = state->vfs_aio_state;
1689 retval = state->retval;
1690 tevent_req_received(req);
1691 return retval;
1694 ssize_t smb_vfs_call_pwrite(struct vfs_handle_struct *handle,
1695 struct files_struct *fsp, const void *data,
1696 size_t n, off_t offset)
1698 VFS_FIND(pwrite);
1699 return handle->fns->pwrite_fn(handle, fsp, data, n, offset);
1702 struct smb_vfs_call_pwrite_state {
1703 ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1704 ssize_t retval;
1705 struct vfs_aio_state vfs_aio_state;
1708 static void smb_vfs_call_pwrite_done(struct tevent_req *subreq);
1710 struct tevent_req *smb_vfs_call_pwrite_send(struct vfs_handle_struct *handle,
1711 TALLOC_CTX *mem_ctx,
1712 struct tevent_context *ev,
1713 struct files_struct *fsp,
1714 const void *data,
1715 size_t n, off_t offset)
1717 struct tevent_req *req, *subreq;
1718 struct smb_vfs_call_pwrite_state *state;
1720 req = tevent_req_create(mem_ctx, &state,
1721 struct smb_vfs_call_pwrite_state);
1722 if (req == NULL) {
1723 return NULL;
1725 VFS_FIND(pwrite_send);
1726 state->recv_fn = handle->fns->pwrite_recv_fn;
1728 subreq = handle->fns->pwrite_send_fn(handle, state, ev, fsp, data, n,
1729 offset);
1730 if (tevent_req_nomem(subreq, req)) {
1731 return tevent_req_post(req, ev);
1733 tevent_req_set_callback(subreq, smb_vfs_call_pwrite_done, req);
1734 return req;
1737 static void smb_vfs_call_pwrite_done(struct tevent_req *subreq)
1739 struct tevent_req *req = tevent_req_callback_data(
1740 subreq, struct tevent_req);
1741 struct smb_vfs_call_pwrite_state *state = tevent_req_data(
1742 req, struct smb_vfs_call_pwrite_state);
1744 state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1745 TALLOC_FREE(subreq);
1746 if (state->retval == -1) {
1747 tevent_req_error(req, state->vfs_aio_state.error);
1748 return;
1750 tevent_req_done(req);
1753 ssize_t SMB_VFS_PWRITE_RECV(struct tevent_req *req,
1754 struct vfs_aio_state *vfs_aio_state)
1756 struct smb_vfs_call_pwrite_state *state = tevent_req_data(
1757 req, struct smb_vfs_call_pwrite_state);
1758 ssize_t retval;
1760 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1761 tevent_req_received(req);
1762 return -1;
1764 *vfs_aio_state = state->vfs_aio_state;
1765 retval = state->retval;
1766 tevent_req_received(req);
1767 return retval;
1770 off_t smb_vfs_call_lseek(struct vfs_handle_struct *handle,
1771 struct files_struct *fsp, off_t offset,
1772 int whence)
1774 VFS_FIND(lseek);
1775 return handle->fns->lseek_fn(handle, fsp, offset, whence);
1778 ssize_t smb_vfs_call_sendfile(struct vfs_handle_struct *handle, int tofd,
1779 files_struct *fromfsp, const DATA_BLOB *header,
1780 off_t offset, size_t count)
1782 VFS_FIND(sendfile);
1783 return handle->fns->sendfile_fn(handle, tofd, fromfsp, header, offset,
1784 count);
1787 ssize_t smb_vfs_call_recvfile(struct vfs_handle_struct *handle, int fromfd,
1788 files_struct *tofsp, off_t offset,
1789 size_t count)
1791 VFS_FIND(recvfile);
1792 return handle->fns->recvfile_fn(handle, fromfd, tofsp, offset, count);
1795 int smb_vfs_call_renameat(struct vfs_handle_struct *handle,
1796 files_struct *srcfsp,
1797 const struct smb_filename *smb_fname_src,
1798 files_struct *dstfsp,
1799 const struct smb_filename *smb_fname_dst,
1800 const struct vfs_rename_how *how)
1802 VFS_FIND(renameat);
1803 return handle->fns->renameat_fn(handle,
1804 srcfsp,
1805 smb_fname_src,
1806 dstfsp,
1807 smb_fname_dst,
1808 how);
1811 struct smb_vfs_call_fsync_state {
1812 int (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1813 int retval;
1814 struct vfs_aio_state vfs_aio_state;
1817 static void smb_vfs_call_fsync_done(struct tevent_req *subreq);
1819 struct tevent_req *smb_vfs_call_fsync_send(struct vfs_handle_struct *handle,
1820 TALLOC_CTX *mem_ctx,
1821 struct tevent_context *ev,
1822 struct files_struct *fsp)
1824 struct tevent_req *req, *subreq;
1825 struct smb_vfs_call_fsync_state *state;
1827 req = tevent_req_create(mem_ctx, &state,
1828 struct smb_vfs_call_fsync_state);
1829 if (req == NULL) {
1830 return NULL;
1832 VFS_FIND(fsync_send);
1833 state->recv_fn = handle->fns->fsync_recv_fn;
1835 subreq = handle->fns->fsync_send_fn(handle, state, ev, fsp);
1836 if (tevent_req_nomem(subreq, req)) {
1837 return tevent_req_post(req, ev);
1839 tevent_req_set_callback(subreq, smb_vfs_call_fsync_done, req);
1840 return req;
1843 static void smb_vfs_call_fsync_done(struct tevent_req *subreq)
1845 struct tevent_req *req = tevent_req_callback_data(
1846 subreq, struct tevent_req);
1847 struct smb_vfs_call_fsync_state *state = tevent_req_data(
1848 req, struct smb_vfs_call_fsync_state);
1850 state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1851 TALLOC_FREE(subreq);
1852 if (state->retval == -1) {
1853 tevent_req_error(req, state->vfs_aio_state.error);
1854 return;
1856 tevent_req_done(req);
1859 int SMB_VFS_FSYNC_RECV(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state)
1861 struct smb_vfs_call_fsync_state *state = tevent_req_data(
1862 req, struct smb_vfs_call_fsync_state);
1863 ssize_t retval;
1865 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1866 tevent_req_received(req);
1867 return -1;
1869 *vfs_aio_state = state->vfs_aio_state;
1870 retval = state->retval;
1871 tevent_req_received(req);
1872 return retval;
1876 * Synchronous version of fsync, built from backend
1877 * async VFS primitives. Uses a temporary sub-event
1878 * context (NOT NESTED).
1881 int smb_vfs_fsync_sync(files_struct *fsp)
1883 TALLOC_CTX *frame = talloc_stackframe();
1884 struct tevent_req *req = NULL;
1885 struct vfs_aio_state aio_state = { 0 };
1886 int ret = -1;
1887 bool ok;
1888 struct tevent_context *ev = samba_tevent_context_init(frame);
1890 if (ev == NULL) {
1891 goto out;
1894 req = SMB_VFS_FSYNC_SEND(talloc_tos(), ev, fsp);
1895 if (req == NULL) {
1896 goto out;
1899 ok = tevent_req_poll(req, ev);
1900 if (!ok) {
1901 goto out;
1904 ret = SMB_VFS_FSYNC_RECV(req, &aio_state);
1906 out:
1908 TALLOC_FREE(frame);
1909 if (aio_state.error != 0) {
1910 errno = aio_state.error;
1912 return ret;
1915 int smb_vfs_call_stat(struct vfs_handle_struct *handle,
1916 struct smb_filename *smb_fname)
1918 VFS_FIND(stat);
1919 return handle->fns->stat_fn(handle, smb_fname);
1922 int smb_vfs_call_fstat(struct vfs_handle_struct *handle,
1923 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1925 VFS_FIND(fstat);
1926 return handle->fns->fstat_fn(handle, fsp, sbuf);
1929 int smb_vfs_call_lstat(struct vfs_handle_struct *handle,
1930 struct smb_filename *smb_filename)
1932 VFS_FIND(lstat);
1933 return handle->fns->lstat_fn(handle, smb_filename);
1936 int smb_vfs_call_fstatat(
1937 struct vfs_handle_struct *handle,
1938 const struct files_struct *dirfsp,
1939 const struct smb_filename *smb_fname,
1940 SMB_STRUCT_STAT *sbuf,
1941 int flags)
1943 VFS_FIND(fstatat);
1944 return handle->fns->fstatat_fn(handle, dirfsp, smb_fname, sbuf, flags);
1947 uint64_t smb_vfs_call_get_alloc_size(struct vfs_handle_struct *handle,
1948 struct files_struct *fsp,
1949 const SMB_STRUCT_STAT *sbuf)
1951 VFS_FIND(get_alloc_size);
1952 return handle->fns->get_alloc_size_fn(handle, fsp, sbuf);
1955 int smb_vfs_call_unlinkat(struct vfs_handle_struct *handle,
1956 struct files_struct *dirfsp,
1957 const struct smb_filename *smb_fname,
1958 int flags)
1960 VFS_FIND(unlinkat);
1961 return handle->fns->unlinkat_fn(handle,
1962 dirfsp,
1963 smb_fname,
1964 flags);
1967 int smb_vfs_call_fchmod(struct vfs_handle_struct *handle,
1968 struct files_struct *fsp, mode_t mode)
1970 VFS_FIND(fchmod);
1971 return handle->fns->fchmod_fn(handle, fsp, mode);
1974 int smb_vfs_call_fchown(struct vfs_handle_struct *handle,
1975 struct files_struct *fsp, uid_t uid, gid_t gid)
1977 VFS_FIND(fchown);
1978 return handle->fns->fchown_fn(handle, fsp, uid, gid);
1981 int smb_vfs_call_lchown(struct vfs_handle_struct *handle,
1982 const struct smb_filename *smb_fname,
1983 uid_t uid,
1984 gid_t gid)
1986 VFS_FIND(lchown);
1987 return handle->fns->lchown_fn(handle, smb_fname, uid, gid);
1990 int smb_vfs_call_chdir(struct vfs_handle_struct *handle,
1991 const struct smb_filename *smb_fname)
1993 VFS_FIND(chdir);
1994 return handle->fns->chdir_fn(handle, smb_fname);
1997 struct smb_filename *smb_vfs_call_getwd(struct vfs_handle_struct *handle,
1998 TALLOC_CTX *ctx)
2000 VFS_FIND(getwd);
2001 return handle->fns->getwd_fn(handle, ctx);
2004 int smb_vfs_call_fntimes(struct vfs_handle_struct *handle,
2005 struct files_struct *fsp,
2006 struct smb_file_time *ft)
2008 VFS_FIND(fntimes);
2009 return handle->fns->fntimes_fn(handle, fsp, ft);
2012 int smb_vfs_call_ftruncate(struct vfs_handle_struct *handle,
2013 struct files_struct *fsp, off_t offset)
2015 VFS_FIND(ftruncate);
2016 return handle->fns->ftruncate_fn(handle, fsp, offset);
2019 int smb_vfs_call_fallocate(struct vfs_handle_struct *handle,
2020 struct files_struct *fsp,
2021 uint32_t mode,
2022 off_t offset,
2023 off_t len)
2025 VFS_FIND(fallocate);
2026 return handle->fns->fallocate_fn(handle, fsp, mode, offset, len);
2029 int smb_vfs_call_filesystem_sharemode(struct vfs_handle_struct *handle,
2030 struct files_struct *fsp,
2031 uint32_t share_mode,
2032 uint32_t access_mask)
2034 VFS_FIND(filesystem_sharemode);
2035 return handle->fns->filesystem_sharemode_fn(handle,
2036 fsp,
2037 share_mode,
2038 access_mask);
2041 int smb_vfs_call_fcntl(struct vfs_handle_struct *handle,
2042 struct files_struct *fsp, int cmd, ...)
2044 int result;
2045 va_list cmd_arg;
2047 VFS_FIND(fcntl);
2049 va_start(cmd_arg, cmd);
2050 result = handle->fns->fcntl_fn(handle, fsp, cmd, cmd_arg);
2051 va_end(cmd_arg);
2053 return result;
2056 int smb_vfs_call_linux_setlease(struct vfs_handle_struct *handle,
2057 struct files_struct *fsp, int leasetype)
2059 VFS_FIND(linux_setlease);
2060 return handle->fns->linux_setlease_fn(handle, fsp, leasetype);
2063 int smb_vfs_call_symlinkat(struct vfs_handle_struct *handle,
2064 const struct smb_filename *link_target,
2065 struct files_struct *dirfsp,
2066 const struct smb_filename *new_smb_fname)
2068 VFS_FIND(symlinkat);
2069 return handle->fns->symlinkat_fn(handle,
2070 link_target,
2071 dirfsp,
2072 new_smb_fname);
2075 int smb_vfs_call_readlinkat(struct vfs_handle_struct *handle,
2076 const struct files_struct *dirfsp,
2077 const struct smb_filename *smb_fname,
2078 char *buf,
2079 size_t bufsiz)
2081 VFS_FIND(readlinkat);
2082 return handle->fns->readlinkat_fn(handle,
2083 dirfsp,
2084 smb_fname,
2085 buf,
2086 bufsiz);
2089 int smb_vfs_call_linkat(struct vfs_handle_struct *handle,
2090 struct files_struct *srcfsp,
2091 const struct smb_filename *old_smb_fname,
2092 struct files_struct *dstfsp,
2093 const struct smb_filename *new_smb_fname,
2094 int flags)
2096 VFS_FIND(linkat);
2097 return handle->fns->linkat_fn(handle,
2098 srcfsp,
2099 old_smb_fname,
2100 dstfsp,
2101 new_smb_fname,
2102 flags);
2105 int smb_vfs_call_mknodat(struct vfs_handle_struct *handle,
2106 struct files_struct *dirfsp,
2107 const struct smb_filename *smb_fname,
2108 mode_t mode,
2109 SMB_DEV_T dev)
2111 VFS_FIND(mknodat);
2112 return handle->fns->mknodat_fn(handle,
2113 dirfsp,
2114 smb_fname,
2115 mode,
2116 dev);
2119 struct smb_filename *smb_vfs_call_realpath(struct vfs_handle_struct *handle,
2120 TALLOC_CTX *ctx,
2121 const struct smb_filename *smb_fname)
2123 VFS_FIND(realpath);
2124 return handle->fns->realpath_fn(handle, ctx, smb_fname);
2127 int smb_vfs_call_fchflags(struct vfs_handle_struct *handle,
2128 struct files_struct *fsp,
2129 unsigned int flags)
2131 VFS_FIND(fchflags);
2132 return handle->fns->fchflags_fn(handle, fsp, flags);
2135 struct file_id smb_vfs_call_file_id_create(struct vfs_handle_struct *handle,
2136 const SMB_STRUCT_STAT *sbuf)
2138 VFS_FIND(file_id_create);
2139 return handle->fns->file_id_create_fn(handle, sbuf);
2142 uint64_t smb_vfs_call_fs_file_id(struct vfs_handle_struct *handle,
2143 const SMB_STRUCT_STAT *sbuf)
2145 VFS_FIND(fs_file_id);
2146 return handle->fns->fs_file_id_fn(handle, sbuf);
2149 NTSTATUS smb_vfs_call_fstreaminfo(struct vfs_handle_struct *handle,
2150 struct files_struct *fsp,
2151 TALLOC_CTX *mem_ctx,
2152 unsigned int *num_streams,
2153 struct stream_struct **streams)
2155 VFS_FIND(fstreaminfo);
2156 return handle->fns->fstreaminfo_fn(handle, fsp, mem_ctx,
2157 num_streams, streams);
2160 NTSTATUS smb_vfs_call_get_real_filename_at(struct vfs_handle_struct *handle,
2161 struct files_struct *dirfsp,
2162 const char *name,
2163 TALLOC_CTX *mem_ctx,
2164 char **found_name)
2166 VFS_FIND(get_real_filename_at);
2167 return handle->fns->get_real_filename_at_fn(
2168 handle, dirfsp, name, mem_ctx, found_name);
2171 const char *smb_vfs_call_connectpath(struct vfs_handle_struct *handle,
2172 const struct files_struct *dirfsp,
2173 const struct smb_filename *smb_fname)
2175 VFS_FIND(connectpath);
2176 return handle->fns->connectpath_fn(handle, dirfsp, smb_fname);
2179 bool smb_vfs_call_strict_lock_check(struct vfs_handle_struct *handle,
2180 struct files_struct *fsp,
2181 struct lock_struct *plock)
2183 VFS_FIND(strict_lock_check);
2184 return handle->fns->strict_lock_check_fn(handle, fsp, plock);
2187 NTSTATUS smb_vfs_call_translate_name(struct vfs_handle_struct *handle,
2188 const char *name,
2189 enum vfs_translate_direction direction,
2190 TALLOC_CTX *mem_ctx,
2191 char **mapped_name)
2193 VFS_FIND(translate_name);
2194 return handle->fns->translate_name_fn(handle, name, direction, mem_ctx,
2195 mapped_name);
2198 NTSTATUS smb_vfs_call_parent_pathname(struct vfs_handle_struct *handle,
2199 TALLOC_CTX *mem_ctx,
2200 const struct smb_filename *smb_fname_in,
2201 struct smb_filename **parent_dir_out,
2202 struct smb_filename **atname_out)
2204 VFS_FIND(parent_pathname);
2205 return handle->fns->parent_pathname_fn(handle,
2206 mem_ctx,
2207 smb_fname_in,
2208 parent_dir_out,
2209 atname_out);
2212 NTSTATUS smb_vfs_call_fsctl(struct vfs_handle_struct *handle,
2213 struct files_struct *fsp,
2214 TALLOC_CTX *ctx,
2215 uint32_t function,
2216 uint16_t req_flags,
2217 const uint8_t *in_data,
2218 uint32_t in_len,
2219 uint8_t **out_data,
2220 uint32_t max_out_len,
2221 uint32_t *out_len)
2223 VFS_FIND(fsctl);
2224 return handle->fns->fsctl_fn(handle, fsp, ctx, function, req_flags,
2225 in_data, in_len, out_data, max_out_len,
2226 out_len);
2229 NTSTATUS smb_vfs_call_fget_dos_attributes(struct vfs_handle_struct *handle,
2230 struct files_struct *fsp,
2231 uint32_t *dosmode)
2233 VFS_FIND(fget_dos_attributes);
2234 return handle->fns->fget_dos_attributes_fn(handle, fsp, dosmode);
2237 NTSTATUS smb_vfs_call_fset_dos_attributes(struct vfs_handle_struct *handle,
2238 struct files_struct *fsp,
2239 uint32_t dosmode)
2241 VFS_FIND(fset_dos_attributes);
2242 return handle->fns->fset_dos_attributes_fn(handle, fsp, dosmode);
2245 struct tevent_req *smb_vfs_call_offload_read_send(TALLOC_CTX *mem_ctx,
2246 struct tevent_context *ev,
2247 struct vfs_handle_struct *handle,
2248 struct files_struct *fsp,
2249 uint32_t fsctl,
2250 uint32_t ttl,
2251 off_t offset,
2252 size_t to_copy)
2254 VFS_FIND(offload_read_send);
2255 return handle->fns->offload_read_send_fn(mem_ctx, ev, handle,
2256 fsp, fsctl,
2257 ttl, offset, to_copy);
2260 NTSTATUS smb_vfs_call_offload_read_recv(struct tevent_req *req,
2261 struct vfs_handle_struct *handle,
2262 TALLOC_CTX *mem_ctx,
2263 uint32_t *flags,
2264 uint64_t *xferlen,
2265 DATA_BLOB *token_blob)
2267 VFS_FIND(offload_read_recv);
2268 return handle->fns->offload_read_recv_fn(req, handle, mem_ctx, flags, xferlen, token_blob);
2271 struct tevent_req *smb_vfs_call_offload_write_send(struct vfs_handle_struct *handle,
2272 TALLOC_CTX *mem_ctx,
2273 struct tevent_context *ev,
2274 uint32_t fsctl,
2275 DATA_BLOB *token,
2276 off_t transfer_offset,
2277 struct files_struct *dest_fsp,
2278 off_t dest_off,
2279 off_t num)
2281 VFS_FIND(offload_write_send);
2282 return handle->fns->offload_write_send_fn(handle, mem_ctx, ev, fsctl,
2283 token, transfer_offset,
2284 dest_fsp, dest_off, num);
2287 NTSTATUS smb_vfs_call_offload_write_recv(struct vfs_handle_struct *handle,
2288 struct tevent_req *req,
2289 off_t *copied)
2291 VFS_FIND(offload_write_recv);
2292 return handle->fns->offload_write_recv_fn(handle, req, copied);
2295 struct smb_vfs_call_get_dos_attributes_state {
2296 files_struct *dir_fsp;
2297 NTSTATUS (*recv_fn)(struct tevent_req *req,
2298 struct vfs_aio_state *aio_state,
2299 uint32_t *dosmode);
2300 struct vfs_aio_state aio_state;
2301 uint32_t dos_attributes;
2304 static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq);
2306 struct tevent_req *smb_vfs_call_get_dos_attributes_send(
2307 TALLOC_CTX *mem_ctx,
2308 struct tevent_context *ev,
2309 struct vfs_handle_struct *handle,
2310 files_struct *dir_fsp,
2311 struct smb_filename *smb_fname)
2313 struct tevent_req *req = NULL;
2314 struct smb_vfs_call_get_dos_attributes_state *state = NULL;
2315 struct tevent_req *subreq = NULL;
2317 req = tevent_req_create(mem_ctx, &state,
2318 struct smb_vfs_call_get_dos_attributes_state);
2319 if (req == NULL) {
2320 return NULL;
2323 VFS_FIND(get_dos_attributes_send);
2325 *state = (struct smb_vfs_call_get_dos_attributes_state) {
2326 .dir_fsp = dir_fsp,
2327 .recv_fn = handle->fns->get_dos_attributes_recv_fn,
2330 subreq = handle->fns->get_dos_attributes_send_fn(mem_ctx,
2332 handle,
2333 dir_fsp,
2334 smb_fname);
2335 if (tevent_req_nomem(subreq, req)) {
2336 return tevent_req_post(req, ev);
2338 tevent_req_defer_callback(req, ev);
2340 tevent_req_set_callback(subreq,
2341 smb_vfs_call_get_dos_attributes_done,
2342 req);
2344 return req;
2347 static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq)
2349 struct tevent_req *req =
2350 tevent_req_callback_data(subreq,
2351 struct tevent_req);
2352 struct smb_vfs_call_get_dos_attributes_state *state =
2353 tevent_req_data(req,
2354 struct smb_vfs_call_get_dos_attributes_state);
2355 NTSTATUS status;
2356 bool ok;
2359 * Make sure we run as the user again
2361 ok = change_to_user_and_service_by_fsp(state->dir_fsp);
2362 SMB_ASSERT(ok);
2364 status = state->recv_fn(subreq,
2365 &state->aio_state,
2366 &state->dos_attributes);
2367 TALLOC_FREE(subreq);
2368 if (tevent_req_nterror(req, status)) {
2369 return;
2372 tevent_req_done(req);
2375 NTSTATUS smb_vfs_call_get_dos_attributes_recv(
2376 struct tevent_req *req,
2377 struct vfs_aio_state *aio_state,
2378 uint32_t *dos_attributes)
2380 struct smb_vfs_call_get_dos_attributes_state *state =
2381 tevent_req_data(req,
2382 struct smb_vfs_call_get_dos_attributes_state);
2383 NTSTATUS status;
2385 if (tevent_req_is_nterror(req, &status)) {
2386 tevent_req_received(req);
2387 return status;
2390 *aio_state = state->aio_state;
2391 *dos_attributes = state->dos_attributes;
2392 tevent_req_received(req);
2393 return NT_STATUS_OK;
2396 NTSTATUS smb_vfs_call_fget_compression(vfs_handle_struct *handle,
2397 TALLOC_CTX *mem_ctx,
2398 struct files_struct *fsp,
2399 uint16_t *_compression_fmt)
2401 VFS_FIND(fget_compression);
2402 return handle->fns->fget_compression_fn(handle, mem_ctx, fsp,
2403 _compression_fmt);
2406 NTSTATUS smb_vfs_call_set_compression(vfs_handle_struct *handle,
2407 TALLOC_CTX *mem_ctx,
2408 struct files_struct *fsp,
2409 uint16_t compression_fmt)
2411 VFS_FIND(set_compression);
2412 return handle->fns->set_compression_fn(handle, mem_ctx, fsp,
2413 compression_fmt);
2416 NTSTATUS smb_vfs_call_snap_check_path(vfs_handle_struct *handle,
2417 TALLOC_CTX *mem_ctx,
2418 const char *service_path,
2419 char **base_volume)
2421 VFS_FIND(snap_check_path);
2422 return handle->fns->snap_check_path_fn(handle, mem_ctx, service_path,
2423 base_volume);
2426 NTSTATUS smb_vfs_call_snap_create(struct vfs_handle_struct *handle,
2427 TALLOC_CTX *mem_ctx,
2428 const char *base_volume,
2429 time_t *tstamp,
2430 bool rw,
2431 char **base_path,
2432 char **snap_path)
2434 VFS_FIND(snap_create);
2435 return handle->fns->snap_create_fn(handle, mem_ctx, base_volume, tstamp,
2436 rw, base_path, snap_path);
2439 NTSTATUS smb_vfs_call_snap_delete(struct vfs_handle_struct *handle,
2440 TALLOC_CTX *mem_ctx,
2441 char *base_path,
2442 char *snap_path)
2444 VFS_FIND(snap_delete);
2445 return handle->fns->snap_delete_fn(handle, mem_ctx, base_path,
2446 snap_path);
2449 NTSTATUS smb_vfs_call_fget_nt_acl(struct vfs_handle_struct *handle,
2450 struct files_struct *fsp,
2451 uint32_t security_info,
2452 TALLOC_CTX *mem_ctx,
2453 struct security_descriptor **ppdesc)
2455 VFS_FIND(fget_nt_acl);
2456 return handle->fns->fget_nt_acl_fn(handle, fsp, security_info,
2457 mem_ctx, ppdesc);
2460 NTSTATUS smb_vfs_call_fset_nt_acl(struct vfs_handle_struct *handle,
2461 struct files_struct *fsp,
2462 uint32_t security_info_sent,
2463 const struct security_descriptor *psd)
2465 VFS_FIND(fset_nt_acl);
2466 return handle->fns->fset_nt_acl_fn(handle, fsp, security_info_sent,
2467 psd);
2470 NTSTATUS smb_vfs_call_audit_file(struct vfs_handle_struct *handle,
2471 struct smb_filename *file,
2472 struct security_acl *sacl,
2473 uint32_t access_requested,
2474 uint32_t access_denied)
2476 VFS_FIND(audit_file);
2477 return handle->fns->audit_file_fn(handle,
2478 file,
2479 sacl,
2480 access_requested,
2481 access_denied);
2484 SMB_ACL_T smb_vfs_call_sys_acl_get_fd(struct vfs_handle_struct *handle,
2485 struct files_struct *fsp,
2486 SMB_ACL_TYPE_T type,
2487 TALLOC_CTX *mem_ctx)
2489 VFS_FIND(sys_acl_get_fd);
2490 return handle->fns->sys_acl_get_fd_fn(handle, fsp, type, mem_ctx);
2493 int smb_vfs_call_sys_acl_blob_get_fd(struct vfs_handle_struct *handle,
2494 struct files_struct *fsp,
2495 TALLOC_CTX *mem_ctx,
2496 char **blob_description,
2497 DATA_BLOB *blob)
2499 VFS_FIND(sys_acl_blob_get_fd);
2500 return handle->fns->sys_acl_blob_get_fd_fn(handle, fsp, mem_ctx, blob_description, blob);
2503 int smb_vfs_call_sys_acl_set_fd(struct vfs_handle_struct *handle,
2504 struct files_struct *fsp,
2505 SMB_ACL_TYPE_T type,
2506 SMB_ACL_T theacl)
2508 VFS_FIND(sys_acl_set_fd);
2509 return handle->fns->sys_acl_set_fd_fn(handle, fsp, type, theacl);
2512 int smb_vfs_call_sys_acl_delete_def_fd(struct vfs_handle_struct *handle,
2513 struct files_struct *fsp)
2515 VFS_FIND(sys_acl_delete_def_fd);
2516 return handle->fns->sys_acl_delete_def_fd_fn(handle, fsp);
2519 struct smb_vfs_call_getxattrat_state {
2520 files_struct *dir_fsp;
2521 ssize_t (*recv_fn)(struct tevent_req *req,
2522 struct vfs_aio_state *aio_state,
2523 TALLOC_CTX *mem_ctx,
2524 uint8_t **xattr_value);
2525 ssize_t retval;
2526 uint8_t *xattr_value;
2527 struct vfs_aio_state aio_state;
2530 static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq);
2532 struct tevent_req *smb_vfs_call_getxattrat_send(
2533 TALLOC_CTX *mem_ctx,
2534 struct tevent_context *ev,
2535 struct vfs_handle_struct *handle,
2536 files_struct *dir_fsp,
2537 const struct smb_filename *smb_fname,
2538 const char *xattr_name,
2539 size_t alloc_hint)
2541 struct tevent_req *req = NULL;
2542 struct smb_vfs_call_getxattrat_state *state = NULL;
2543 struct tevent_req *subreq = NULL;
2545 req = tevent_req_create(mem_ctx, &state,
2546 struct smb_vfs_call_getxattrat_state);
2547 if (req == NULL) {
2548 return NULL;
2551 VFS_FIND(getxattrat_send);
2553 *state = (struct smb_vfs_call_getxattrat_state) {
2554 .dir_fsp = dir_fsp,
2555 .recv_fn = handle->fns->getxattrat_recv_fn,
2558 subreq = handle->fns->getxattrat_send_fn(mem_ctx,
2560 handle,
2561 dir_fsp,
2562 smb_fname,
2563 xattr_name,
2564 alloc_hint);
2565 if (tevent_req_nomem(subreq, req)) {
2566 return tevent_req_post(req, ev);
2568 tevent_req_defer_callback(req, ev);
2570 tevent_req_set_callback(subreq, smb_vfs_call_getxattrat_done, req);
2571 return req;
2574 static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq)
2576 struct tevent_req *req = tevent_req_callback_data(
2577 subreq, struct tevent_req);
2578 struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
2579 req, struct smb_vfs_call_getxattrat_state);
2580 bool ok;
2583 * Make sure we run as the user again
2585 ok = change_to_user_and_service_by_fsp(state->dir_fsp);
2586 SMB_ASSERT(ok);
2588 state->retval = state->recv_fn(subreq,
2589 &state->aio_state,
2590 state,
2591 &state->xattr_value);
2592 TALLOC_FREE(subreq);
2593 if (state->retval == -1) {
2594 tevent_req_error(req, state->aio_state.error);
2595 return;
2598 tevent_req_done(req);
2601 ssize_t smb_vfs_call_getxattrat_recv(struct tevent_req *req,
2602 struct vfs_aio_state *aio_state,
2603 TALLOC_CTX *mem_ctx,
2604 uint8_t **xattr_value)
2606 struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
2607 req, struct smb_vfs_call_getxattrat_state);
2608 size_t xattr_size;
2610 if (tevent_req_is_unix_error(req, &aio_state->error)) {
2611 tevent_req_received(req);
2612 return -1;
2615 *aio_state = state->aio_state;
2616 xattr_size = state->retval;
2617 if (xattr_value != NULL) {
2618 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2621 tevent_req_received(req);
2622 return xattr_size;
2625 ssize_t smb_vfs_call_fgetxattr(struct vfs_handle_struct *handle,
2626 struct files_struct *fsp, const char *name,
2627 void *value, size_t size)
2629 VFS_FIND(fgetxattr);
2630 return handle->fns->fgetxattr_fn(handle, fsp, name, value, size);
2633 ssize_t smb_vfs_call_flistxattr(struct vfs_handle_struct *handle,
2634 struct files_struct *fsp, char *list,
2635 size_t size)
2637 VFS_FIND(flistxattr);
2638 return handle->fns->flistxattr_fn(handle, fsp, list, size);
2641 int smb_vfs_call_fremovexattr(struct vfs_handle_struct *handle,
2642 struct files_struct *fsp, const char *name)
2644 VFS_FIND(fremovexattr);
2645 return handle->fns->fremovexattr_fn(handle, fsp, name);
2648 int smb_vfs_call_fsetxattr(struct vfs_handle_struct *handle,
2649 struct files_struct *fsp, const char *name,
2650 const void *value, size_t size, int flags)
2652 VFS_FIND(fsetxattr);
2653 return handle->fns->fsetxattr_fn(handle, fsp, name, value, size, flags);
2656 bool smb_vfs_call_aio_force(struct vfs_handle_struct *handle,
2657 struct files_struct *fsp)
2659 VFS_FIND(aio_force);
2660 return handle->fns->aio_force_fn(handle, fsp);
2663 NTSTATUS smb_vfs_call_durable_cookie(struct vfs_handle_struct *handle,
2664 struct files_struct *fsp,
2665 TALLOC_CTX *mem_ctx,
2666 DATA_BLOB *cookie)
2668 VFS_FIND(durable_cookie);
2669 return handle->fns->durable_cookie_fn(handle, fsp, mem_ctx, cookie);
2672 NTSTATUS smb_vfs_call_durable_disconnect(struct vfs_handle_struct *handle,
2673 struct files_struct *fsp,
2674 const DATA_BLOB old_cookie,
2675 TALLOC_CTX *mem_ctx,
2676 DATA_BLOB *new_cookie)
2678 VFS_FIND(durable_disconnect);
2679 return handle->fns->durable_disconnect_fn(handle, fsp, old_cookie,
2680 mem_ctx, new_cookie);
2683 NTSTATUS smb_vfs_call_durable_reconnect(struct vfs_handle_struct *handle,
2684 struct smb_request *smb1req,
2685 struct smbXsrv_open *op,
2686 const DATA_BLOB old_cookie,
2687 TALLOC_CTX *mem_ctx,
2688 struct files_struct **fsp,
2689 DATA_BLOB *new_cookie)
2691 VFS_FIND(durable_reconnect);
2692 return handle->fns->durable_reconnect_fn(handle, smb1req, op,
2693 old_cookie, mem_ctx, fsp,
2694 new_cookie);
2697 NTSTATUS smb_vfs_call_freaddir_attr(struct vfs_handle_struct *handle,
2698 struct files_struct *fsp,
2699 TALLOC_CTX *mem_ctx,
2700 struct readdir_attr_data **attr_data)
2702 VFS_FIND(freaddir_attr);
2703 return handle->fns->freaddir_attr_fn(handle,
2704 fsp,
2705 mem_ctx,
2706 attr_data);
2709 bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
2710 struct files_struct *fsp, int op, off_t offset,
2711 off_t count, int type)
2713 VFS_FIND(lock);
2714 return handle->fns->lock_fn(handle, fsp, op, offset, count, type);
2717 bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
2718 struct files_struct *fsp, off_t *poffset,
2719 off_t *pcount, int *ptype, pid_t *ppid)
2721 VFS_FIND(getlock);
2722 return handle->fns->getlock_fn(handle, fsp, poffset, pcount, ptype,
2723 ppid);
2726 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
2727 struct byte_range_lock *br_lck,
2728 struct lock_struct *plock)
2730 VFS_FIND(brl_lock_windows);
2731 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock);
2734 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
2735 struct byte_range_lock *br_lck,
2736 const struct lock_struct *plock)
2738 VFS_FIND(brl_unlock_windows);
2739 return handle->fns->brl_unlock_windows_fn(handle, br_lck, plock);