smbd: Don't talloc_zero where we assign the struct a line below
[Samba.git] / source3 / smbd / vfs.c
blob13b3ba61ceac49223f0694fee26f3c5d92b612c1
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, NOTIFY_ACTION_MODIFIED,
653 FILE_NOTIFY_CHANGE_SIZE
654 | FILE_NOTIFY_CHANGE_ATTRIBUTES,
655 fsp->fsp_name->base_name);
658 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
660 return ret;
663 /****************************************************************************
664 A slow version of fallocate. Fallback code if SMB_VFS_FALLOCATE
665 fails. Needs to be outside of the default version of SMB_VFS_FALLOCATE
666 as this is also called from the default SMB_VFS_FTRUNCATE code.
667 Always extends the file size.
668 Returns 0 on success, -1 on failure.
669 ****************************************************************************/
671 #define SPARSE_BUF_WRITE_SIZE (32*1024)
673 int vfs_slow_fallocate(files_struct *fsp, off_t offset, off_t len)
675 ssize_t pwrite_ret;
676 size_t total = 0;
677 bool ok;
679 ok = vfs_valid_pwrite_range(offset, len);
680 if (!ok) {
681 errno = EINVAL;
682 return -1;
685 if (!sparse_buf) {
686 sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
687 if (!sparse_buf) {
688 errno = ENOMEM;
689 return -1;
693 while (total < len) {
694 size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (len - total));
696 pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
697 if (pwrite_ret == -1) {
698 int saved_errno = errno;
699 DEBUG(10,("vfs_slow_fallocate: SMB_VFS_PWRITE for file "
700 "%s failed with error %s\n",
701 fsp_str_dbg(fsp), strerror(saved_errno)));
702 errno = saved_errno;
703 return -1;
705 total += pwrite_ret;
708 return 0;
711 /****************************************************************************
712 A vfs fill sparse call.
713 Writes zeros from the end of file to len, if len is greater than EOF.
714 Used only by strict_sync.
715 Returns 0 on success, -1 on failure.
716 ****************************************************************************/
718 int vfs_fill_sparse(files_struct *fsp, off_t len)
720 int ret;
721 NTSTATUS status;
722 off_t offset;
723 size_t num_to_write;
724 bool ok;
726 ok = vfs_valid_pwrite_range(len, 0);
727 if (!ok) {
728 errno = EINVAL;
729 return -1;
732 status = vfs_stat_fsp(fsp);
733 if (!NT_STATUS_IS_OK(status)) {
734 return -1;
737 if (len <= fsp->fsp_name->st.st_ex_size) {
738 return 0;
741 #ifdef S_ISFIFO
742 if (S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
743 return 0;
745 #endif
747 DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to "
748 "len %.0f (%.0f bytes)\n", fsp_str_dbg(fsp),
749 (double)fsp->fsp_name->st.st_ex_size, (double)len,
750 (double)(len - fsp->fsp_name->st.st_ex_size)));
752 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_FILL_SPARSE);
754 offset = fsp->fsp_name->st.st_ex_size;
755 num_to_write = len - fsp->fsp_name->st.st_ex_size;
757 /* Only do this on non-stream file handles. */
758 if (!fsp_is_alternate_stream(fsp)) {
759 /* for allocation try fallocate first. This can fail on some
760 * platforms e.g. when the filesystem doesn't support it and no
761 * emulation is being done by the libc (like on AIX with JFS1). In that
762 * case we do our own emulation. fallocate implementations can
763 * return ENOTSUP or EINVAL in cases like that. */
764 ret = SMB_VFS_FALLOCATE(fsp, 0, offset, num_to_write);
765 if (ret == -1 && errno == ENOSPC) {
766 goto out;
768 if (ret == 0) {
769 goto out;
771 DEBUG(10,("vfs_fill_sparse: SMB_VFS_FALLOCATE failed with "
772 "error %d. Falling back to slow manual allocation\n", ret));
775 ret = vfs_slow_fallocate(fsp, offset, num_to_write);
777 out:
779 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_FILL_SPARSE);
780 return ret;
783 /*******************************************************************************
784 Set a fd into blocking/nonblocking mode through VFS
785 *******************************************************************************/
787 int vfs_set_blocking(files_struct *fsp, bool set)
789 int val;
790 #ifdef O_NONBLOCK
791 #define FLAG_TO_SET O_NONBLOCK
792 #else
793 #ifdef SYSV
794 #define FLAG_TO_SET O_NDELAY
795 #else /* BSD */
796 #define FLAG_TO_SET FNDELAY
797 #endif
798 #endif
800 if (fsp->fsp_flags.is_pathref) {
801 return 0;
804 val = SMB_VFS_FCNTL(fsp, F_GETFL, 0);
805 if (val == -1) {
806 return -1;
809 if (set) {
810 val &= ~FLAG_TO_SET;
811 } else {
812 val |= FLAG_TO_SET;
815 return SMB_VFS_FCNTL(fsp, F_SETFL, val);
816 #undef FLAG_TO_SET
819 /****************************************************************************
820 Transfer some data (n bytes) between two file_struct's.
821 ****************************************************************************/
823 static ssize_t vfs_pread_fn(void *file, void *buf, size_t len, off_t offset)
825 struct files_struct *fsp = (struct files_struct *)file;
827 return SMB_VFS_PREAD(fsp, buf, len, offset);
830 static ssize_t vfs_pwrite_fn(void *file, const void *buf, size_t len, off_t offset)
832 struct files_struct *fsp = (struct files_struct *)file;
834 return SMB_VFS_PWRITE(fsp, buf, len, offset);
837 off_t vfs_transfer_file(files_struct *in, files_struct *out, off_t n)
839 return transfer_file_internal((void *)in, (void *)out, n,
840 vfs_pread_fn, vfs_pwrite_fn);
843 /*******************************************************************
844 A vfs_readdir wrapper which just returns the file name.
845 ********************************************************************/
847 const char *vfs_readdirname(connection_struct *conn,
848 struct files_struct *dirfsp,
849 DIR *d,
850 char **talloced)
852 struct dirent *ptr= NULL;
853 const char *dname;
854 char *translated;
855 NTSTATUS status;
857 if (d == NULL) {
858 return(NULL);
861 ptr = SMB_VFS_READDIR(conn, dirfsp, d);
862 if (!ptr)
863 return(NULL);
865 dname = ptr->d_name;
867 status = SMB_VFS_TRANSLATE_NAME(conn, dname, vfs_translate_to_windows,
868 talloc_tos(), &translated);
869 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
870 *talloced = NULL;
871 return dname;
873 *talloced = translated;
874 if (!NT_STATUS_IS_OK(status)) {
875 return NULL;
877 return translated;
880 /*******************************************************************
881 A wrapper for vfs_chdir().
882 ********************************************************************/
884 int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
886 int ret;
887 struct smb_filename *cwd = NULL;
889 if (!LastDir) {
890 LastDir = SMB_STRDUP("");
893 if (ISDOT(smb_fname->base_name)) {
895 * passing a '.' is a noop,
896 * and we only expect this after
897 * everything is initialized.
899 * So the first vfs_ChDir() on a given
900 * connection_struct must not be '.'.
902 * Note: conn_new() sets
903 * conn->cwd_fsp->fh->fd = -1
904 * and vfs_ChDir() leaves with
905 * conn->cwd_fsp->fh->fd = AT_FDCWD
906 * on success!
908 if (fsp_get_pathref_fd(conn->cwd_fsp) != AT_FDCWD) {
910 * This should never happen and
911 * we might change this to
912 * SMB_ASSERT() in future.
914 DBG_ERR("Called with '.' as first operation!\n");
915 log_stack_trace();
916 errno = EINVAL;
917 return -1;
919 return 0;
922 if (smb_fname->base_name[0] == '/' &&
923 strcsequal(LastDir,smb_fname->base_name))
926 * conn->cwd_fsp->fsp_name and the kernel
927 * are already correct, but conn->cwd_fsp->fh->fd
928 * might still be -1 as initialized in conn_new().
930 * This can happen when a client made a 2nd
931 * tree connect to a share with the same underlying
932 * path (may or may not the same share).
934 fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
935 return 0;
938 DEBUG(4,("vfs_ChDir to %s\n", smb_fname->base_name));
940 ret = SMB_VFS_CHDIR(conn, smb_fname);
941 if (ret != 0) {
942 return -1;
946 * Always replace conn->cwd_fsp. We
947 * don't know if it's been modified by
948 * VFS modules in the stack.
950 fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
952 /* conn cache. */
953 cwd = vfs_GetWd(conn, conn);
954 if (cwd == NULL) {
956 * vfs_GetWd() failed.
957 * We must be able to read cwd.
958 * Return to original directory
959 * and return -1.
961 int saved_errno = errno;
963 if (conn->cwd_fsp->fsp_name == NULL) {
965 * Failed on the very first chdir()+getwd()
966 * for this connection. We can't
967 * continue.
969 smb_panic("conn->cwd getwd failed\n");
970 /* NOTREACHED */
971 return -1;
974 /* Return to the previous $cwd. */
975 ret = SMB_VFS_CHDIR(conn, conn->cwd_fsp->fsp_name);
976 if (ret != 0) {
977 smb_panic("conn->cwd getwd failed\n");
978 /* NOTREACHED */
979 return -1;
981 errno = saved_errno;
982 /* And fail the chdir(). */
983 return -1;
986 /* vfs_GetWd() succeeded. */
987 /* Replace global cache. */
988 SAFE_FREE(LastDir);
989 LastDir = SMB_STRDUP(smb_fname->base_name);
992 * (Indirect) Callers of vfs_ChDir() may still hold references to the
993 * old conn->cwd_fsp->fsp_name. Move it to talloc_tos(), that way
994 * callers can use it for the lifetime of the SMB request.
996 talloc_move(talloc_tos(), &conn->cwd_fsp->fsp_name);
998 conn->cwd_fsp->fsp_name = talloc_move(conn->cwd_fsp, &cwd);
1000 DBG_INFO("vfs_ChDir got %s\n", fsp_str_dbg(conn->cwd_fsp));
1002 return ret;
1005 /*******************************************************************
1006 Return the absolute current directory path - given a UNIX pathname.
1007 Note that this path is returned in DOS format, not UNIX
1008 format. Note this can be called with conn == NULL.
1009 ********************************************************************/
1011 struct smb_filename *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
1013 struct smb_filename *current_dir_fname = NULL;
1014 struct file_id key;
1015 struct smb_filename *smb_fname_dot = NULL;
1016 struct smb_filename *smb_fname_full = NULL;
1017 struct smb_filename *result = NULL;
1019 if (!lp_getwd_cache()) {
1020 goto nocache;
1023 smb_fname_dot = synthetic_smb_fname(ctx,
1024 ".",
1025 NULL,
1026 NULL,
1029 if (smb_fname_dot == NULL) {
1030 errno = ENOMEM;
1031 goto out;
1034 if (SMB_VFS_STAT(conn, smb_fname_dot) == -1) {
1036 * Known to fail for root: the directory may be NFS-mounted
1037 * and exported with root_squash (so has no root access).
1039 DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
1040 "(NFS problem ?)\n", strerror(errno) ));
1041 goto nocache;
1044 key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
1046 smb_fname_full = (struct smb_filename *)memcache_lookup_talloc(
1047 smbd_memcache(),
1048 GETWD_CACHE,
1049 data_blob_const(&key, sizeof(key)));
1051 if (smb_fname_full == NULL) {
1052 goto nocache;
1055 if ((SMB_VFS_STAT(conn, smb_fname_full) == 0) &&
1056 (smb_fname_dot->st.st_ex_dev == smb_fname_full->st.st_ex_dev) &&
1057 (smb_fname_dot->st.st_ex_ino == smb_fname_full->st.st_ex_ino) &&
1058 (S_ISDIR(smb_fname_dot->st.st_ex_mode))) {
1060 * Ok, we're done
1061 * Note: smb_fname_full is owned by smbd_memcache()
1062 * so we must make a copy to return.
1064 result = cp_smb_filename(ctx, smb_fname_full);
1065 if (result == NULL) {
1066 errno = ENOMEM;
1068 goto out;
1071 nocache:
1074 * We don't have the information to hand so rely on traditional
1075 * methods. The very slow getcwd, which spawns a process on some
1076 * systems, or the not quite so bad getwd.
1079 current_dir_fname = SMB_VFS_GETWD(conn, ctx);
1080 if (current_dir_fname == NULL) {
1081 DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
1082 strerror(errno)));
1083 goto out;
1086 if (lp_getwd_cache() && VALID_STAT(smb_fname_dot->st)) {
1087 key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
1090 * smbd_memcache() will own current_dir_fname after the
1091 * memcache_add_talloc call, so we must make
1092 * a copy on ctx to return.
1094 result = cp_smb_filename(ctx, current_dir_fname);
1095 if (result == NULL) {
1096 errno = ENOMEM;
1100 * Ensure the memory going into the cache
1101 * doesn't have a destructor so it can be
1102 * cleanly freed.
1104 talloc_set_destructor(current_dir_fname, NULL);
1106 memcache_add_talloc(smbd_memcache(),
1107 GETWD_CACHE,
1108 data_blob_const(&key, sizeof(key)),
1109 &current_dir_fname);
1110 /* current_dir_fname is now == NULL here. */
1111 } else {
1112 /* current_dir_fname is already allocated on ctx. */
1113 result = current_dir_fname;
1116 out:
1117 TALLOC_FREE(smb_fname_dot);
1119 * Don't free current_dir_fname here. It's either been moved
1120 * to the memcache or is being returned in result.
1122 return result;
1126 * Ensure LSTAT is called for POSIX paths.
1128 int vfs_stat(struct connection_struct *conn,
1129 struct smb_filename *smb_fname)
1131 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1132 return SMB_VFS_LSTAT(conn, smb_fname);
1134 return SMB_VFS_STAT(conn, smb_fname);
1138 * XXX: This is temporary and there should be no callers of this once
1139 * smb_filename is plumbed through all path based operations.
1141 * Called when we know stream name parsing has already been done.
1143 int vfs_stat_smb_basename(struct connection_struct *conn,
1144 const struct smb_filename *smb_fname_in,
1145 SMB_STRUCT_STAT *psbuf)
1147 struct smb_filename smb_fname = {
1148 .base_name = discard_const_p(char, smb_fname_in->base_name),
1149 .flags = smb_fname_in->flags,
1150 .twrp = smb_fname_in->twrp,
1152 int ret;
1154 ret = vfs_stat(conn, &smb_fname);
1155 if (ret != -1) {
1156 *psbuf = smb_fname.st;
1158 return ret;
1162 * Ensure LSTAT is called for POSIX paths.
1165 NTSTATUS vfs_stat_fsp(files_struct *fsp)
1167 int ret;
1168 struct stat_ex saved_stat = fsp->fsp_name->st;
1170 if (fsp->fake_file_handle != NULL) {
1171 return NT_STATUS_OK;
1174 if (fsp_get_pathref_fd(fsp) == -1) {
1175 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1176 ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1177 } else {
1178 ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1180 } else {
1181 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
1183 if (ret == -1) {
1184 return map_nt_error_from_unix(errno);
1186 update_stat_ex_from_saved_stat(&fsp->fsp_name->st, &saved_stat);
1187 fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
1188 return NT_STATUS_OK;
1191 void init_smb_file_time(struct smb_file_time *ft)
1193 struct timespec omit = make_omit_timespec();
1195 *ft = (struct smb_file_time) {
1196 .atime = omit,
1197 .ctime = omit,
1198 .mtime = omit,
1199 .create_time = omit,
1204 * Initialize num_streams and streams, then call VFS op streaminfo
1207 NTSTATUS vfs_fstreaminfo(struct files_struct *fsp,
1208 TALLOC_CTX *mem_ctx,
1209 unsigned int *num_streams,
1210 struct stream_struct **streams)
1212 *num_streams = 0;
1213 *streams = NULL;
1215 if (fsp == NULL) {
1217 * Callers may pass fsp == NULL when passing smb_fname->fsp of a
1218 * symlink. This is ok, handle it here, by just return no
1219 * streams on a symlink.
1221 return NT_STATUS_OK;
1224 if (fsp_get_pathref_fd(fsp) == -1) {
1226 * No streams on non-real files/directories.
1228 return NT_STATUS_OK;
1231 return SMB_VFS_FSTREAMINFO(fsp,
1232 mem_ctx,
1233 num_streams,
1234 streams);
1237 int vfs_fake_fd(void)
1239 int pipe_fds[2];
1240 int ret;
1243 * Return a valid fd, but ensure any attempt to use
1244 * it returns an error (EPIPE).
1246 ret = pipe(pipe_fds);
1247 if (ret != 0) {
1248 return -1;
1251 close(pipe_fds[1]);
1252 return pipe_fds[0];
1256 * This is just a helper to make
1257 * users of vfs_fake_fd() more symmetric
1259 int vfs_fake_fd_close(int fd)
1261 return close(fd);
1265 generate a file_id from a stat structure
1267 struct file_id vfs_file_id_from_sbuf(connection_struct *conn, const SMB_STRUCT_STAT *sbuf)
1269 return SMB_VFS_FILE_ID_CREATE(conn, sbuf);
1272 NTSTATUS vfs_at_fspcwd(TALLOC_CTX *mem_ctx,
1273 struct connection_struct *conn,
1274 struct files_struct **_fsp)
1276 struct files_struct *fsp = NULL;
1278 fsp = talloc_zero(mem_ctx, struct files_struct);
1279 if (fsp == NULL) {
1280 return NT_STATUS_NO_MEMORY;
1283 fsp->fsp_name = synthetic_smb_fname(fsp, ".", NULL, NULL, 0, 0);
1284 if (fsp->fsp_name == NULL) {
1285 TALLOC_FREE(fsp);
1286 return NT_STATUS_NO_MEMORY;
1289 fsp->fh = fd_handle_create(fsp);
1290 if (fsp->fh == NULL) {
1291 TALLOC_FREE(fsp);
1292 return NT_STATUS_NO_MEMORY;
1295 fsp_set_fd(fsp, AT_FDCWD);
1296 fsp->fnum = FNUM_FIELD_INVALID;
1297 fsp->conn = conn;
1299 *_fsp = fsp;
1300 return NT_STATUS_OK;
1303 static struct smb_vfs_deny_state *smb_vfs_deny_global;
1305 void smb_vfs_assert_allowed(void)
1307 if (unlikely(smb_vfs_deny_global != NULL)) {
1308 DBG_ERR("Called with VFS denied by %s\n",
1309 smb_vfs_deny_global->location);
1310 smb_panic("Called with VFS denied!");
1314 void _smb_vfs_deny_push(struct smb_vfs_deny_state *state, const char *location)
1316 SMB_ASSERT(smb_vfs_deny_global != state);
1318 *state = (struct smb_vfs_deny_state) {
1319 .parent = smb_vfs_deny_global,
1320 .location = location,
1323 smb_vfs_deny_global = state;
1326 void _smb_vfs_deny_pop(struct smb_vfs_deny_state *state, const char *location)
1328 SMB_ASSERT(smb_vfs_deny_global == state);
1330 smb_vfs_deny_global = state->parent;
1332 *state = (struct smb_vfs_deny_state) { .parent = NULL, };
1335 #define VFS_FIND(__fn__) do { \
1336 if (unlikely(smb_vfs_deny_global != NULL)) { \
1337 DBG_ERR("Called with VFS denied by %s\n", \
1338 smb_vfs_deny_global->location); \
1339 smb_panic("Called with VFS denied!"); \
1341 while (handle->fns->__fn__##_fn==NULL) { \
1342 handle = handle->next; \
1344 } while(0)
1346 int smb_vfs_call_connect(struct vfs_handle_struct *handle,
1347 const char *service, const char *user)
1349 VFS_FIND(connect);
1350 return handle->fns->connect_fn(handle, service, user);
1353 void smb_vfs_call_disconnect(struct vfs_handle_struct *handle)
1355 VFS_FIND(disconnect);
1356 handle->fns->disconnect_fn(handle);
1359 uint64_t smb_vfs_call_disk_free(struct vfs_handle_struct *handle,
1360 const struct smb_filename *smb_fname,
1361 uint64_t *bsize,
1362 uint64_t *dfree,
1363 uint64_t *dsize)
1365 VFS_FIND(disk_free);
1366 return handle->fns->disk_free_fn(handle, smb_fname,
1367 bsize, dfree, dsize);
1370 int smb_vfs_call_get_quota(struct vfs_handle_struct *handle,
1371 const struct smb_filename *smb_fname,
1372 enum SMB_QUOTA_TYPE qtype,
1373 unid_t id,
1374 SMB_DISK_QUOTA *qt)
1376 VFS_FIND(get_quota);
1377 return handle->fns->get_quota_fn(handle, smb_fname, qtype, id, qt);
1380 int smb_vfs_call_set_quota(struct vfs_handle_struct *handle,
1381 enum SMB_QUOTA_TYPE qtype, unid_t id,
1382 SMB_DISK_QUOTA *qt)
1384 VFS_FIND(set_quota);
1385 return handle->fns->set_quota_fn(handle, qtype, id, qt);
1388 int smb_vfs_call_get_shadow_copy_data(struct vfs_handle_struct *handle,
1389 struct files_struct *fsp,
1390 struct shadow_copy_data *shadow_copy_data,
1391 bool labels)
1393 VFS_FIND(get_shadow_copy_data);
1394 return handle->fns->get_shadow_copy_data_fn(handle, fsp,
1395 shadow_copy_data,
1396 labels);
1398 int smb_vfs_call_statvfs(struct vfs_handle_struct *handle,
1399 const struct smb_filename *smb_fname,
1400 struct vfs_statvfs_struct *statbuf)
1402 VFS_FIND(statvfs);
1403 return handle->fns->statvfs_fn(handle, smb_fname, statbuf);
1406 uint32_t smb_vfs_call_fs_capabilities(struct vfs_handle_struct *handle,
1407 enum timestamp_set_resolution *p_ts_res)
1409 VFS_FIND(fs_capabilities);
1410 return handle->fns->fs_capabilities_fn(handle, p_ts_res);
1413 NTSTATUS smb_vfs_call_get_dfs_referrals(struct vfs_handle_struct *handle,
1414 struct dfs_GetDFSReferral *r)
1416 VFS_FIND(get_dfs_referrals);
1417 return handle->fns->get_dfs_referrals_fn(handle, r);
1420 NTSTATUS smb_vfs_call_create_dfs_pathat(struct vfs_handle_struct *handle,
1421 struct files_struct *dirfsp,
1422 const struct smb_filename *smb_fname,
1423 const struct referral *reflist,
1424 size_t referral_count)
1426 VFS_FIND(create_dfs_pathat);
1427 return handle->fns->create_dfs_pathat_fn(handle,
1428 dirfsp,
1429 smb_fname,
1430 reflist,
1431 referral_count);
1434 NTSTATUS smb_vfs_call_read_dfs_pathat(struct vfs_handle_struct *handle,
1435 TALLOC_CTX *mem_ctx,
1436 struct files_struct *dirfsp,
1437 struct smb_filename *smb_fname,
1438 struct referral **ppreflist,
1439 size_t *preferral_count)
1441 VFS_FIND(read_dfs_pathat);
1442 return handle->fns->read_dfs_pathat_fn(handle,
1443 mem_ctx,
1444 dirfsp,
1445 smb_fname,
1446 ppreflist,
1447 preferral_count);
1450 DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
1451 struct files_struct *fsp,
1452 const char *mask,
1453 uint32_t attributes)
1455 VFS_FIND(fdopendir);
1456 return handle->fns->fdopendir_fn(handle, fsp, mask, attributes);
1459 struct dirent *smb_vfs_call_readdir(struct vfs_handle_struct *handle,
1460 struct files_struct *dirfsp,
1461 DIR *dirp)
1463 VFS_FIND(readdir);
1464 return handle->fns->readdir_fn(handle, dirfsp, dirp);
1467 void smb_vfs_call_rewind_dir(struct vfs_handle_struct *handle,
1468 DIR *dirp)
1470 VFS_FIND(rewind_dir);
1471 handle->fns->rewind_dir_fn(handle, dirp);
1474 int smb_vfs_call_mkdirat(struct vfs_handle_struct *handle,
1475 struct files_struct *dirfsp,
1476 const struct smb_filename *smb_fname,
1477 mode_t mode)
1479 VFS_FIND(mkdirat);
1480 return handle->fns->mkdirat_fn(handle,
1481 dirfsp,
1482 smb_fname,
1483 mode);
1486 int smb_vfs_call_closedir(struct vfs_handle_struct *handle,
1487 DIR *dir)
1489 VFS_FIND(closedir);
1490 return handle->fns->closedir_fn(handle, dir);
1493 int smb_vfs_call_openat(struct vfs_handle_struct *handle,
1494 const struct files_struct *dirfsp,
1495 const struct smb_filename *smb_fname,
1496 struct files_struct *fsp,
1497 const struct vfs_open_how *how)
1499 VFS_FIND(openat);
1500 return handle->fns->openat_fn(handle,
1501 dirfsp,
1502 smb_fname,
1503 fsp,
1504 how);
1507 NTSTATUS smb_vfs_call_create_file(struct vfs_handle_struct *handle,
1508 struct smb_request *req,
1509 struct files_struct *dirfsp,
1510 struct smb_filename *smb_fname,
1511 uint32_t access_mask,
1512 uint32_t share_access,
1513 uint32_t create_disposition,
1514 uint32_t create_options,
1515 uint32_t file_attributes,
1516 uint32_t oplock_request,
1517 const struct smb2_lease *lease,
1518 uint64_t allocation_size,
1519 uint32_t private_flags,
1520 struct security_descriptor *sd,
1521 struct ea_list *ea_list,
1522 files_struct **result,
1523 int *pinfo,
1524 const struct smb2_create_blobs *in_context_blobs,
1525 struct smb2_create_blobs *out_context_blobs)
1527 VFS_FIND(create_file);
1528 return handle->fns->create_file_fn(
1529 handle, req, dirfsp, smb_fname,
1530 access_mask, share_access, create_disposition, create_options,
1531 file_attributes, oplock_request, lease, allocation_size,
1532 private_flags, sd, ea_list,
1533 result, pinfo, in_context_blobs, out_context_blobs);
1536 int smb_vfs_call_close(struct vfs_handle_struct *handle,
1537 struct files_struct *fsp)
1539 VFS_FIND(close);
1540 return handle->fns->close_fn(handle, fsp);
1543 ssize_t smb_vfs_call_pread(struct vfs_handle_struct *handle,
1544 struct files_struct *fsp, void *data, size_t n,
1545 off_t offset)
1547 VFS_FIND(pread);
1548 return handle->fns->pread_fn(handle, fsp, data, n, offset);
1551 struct smb_vfs_call_pread_state {
1552 ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1553 ssize_t retval;
1554 struct vfs_aio_state vfs_aio_state;
1557 static void smb_vfs_call_pread_done(struct tevent_req *subreq);
1559 struct tevent_req *smb_vfs_call_pread_send(struct vfs_handle_struct *handle,
1560 TALLOC_CTX *mem_ctx,
1561 struct tevent_context *ev,
1562 struct files_struct *fsp,
1563 void *data,
1564 size_t n, off_t offset)
1566 struct tevent_req *req, *subreq;
1567 struct smb_vfs_call_pread_state *state;
1569 req = tevent_req_create(mem_ctx, &state,
1570 struct smb_vfs_call_pread_state);
1571 if (req == NULL) {
1572 return NULL;
1574 VFS_FIND(pread_send);
1575 state->recv_fn = handle->fns->pread_recv_fn;
1577 subreq = handle->fns->pread_send_fn(handle, state, ev, fsp, data, n,
1578 offset);
1579 if (tevent_req_nomem(subreq, req)) {
1580 return tevent_req_post(req, ev);
1582 tevent_req_set_callback(subreq, smb_vfs_call_pread_done, req);
1583 return req;
1586 static void smb_vfs_call_pread_done(struct tevent_req *subreq)
1588 struct tevent_req *req = tevent_req_callback_data(
1589 subreq, struct tevent_req);
1590 struct smb_vfs_call_pread_state *state = tevent_req_data(
1591 req, struct smb_vfs_call_pread_state);
1593 state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1594 TALLOC_FREE(subreq);
1595 if (state->retval == -1) {
1596 tevent_req_error(req, state->vfs_aio_state.error);
1597 return;
1599 tevent_req_done(req);
1602 ssize_t SMB_VFS_PREAD_RECV(struct tevent_req *req,
1603 struct vfs_aio_state *vfs_aio_state)
1605 struct smb_vfs_call_pread_state *state = tevent_req_data(
1606 req, struct smb_vfs_call_pread_state);
1607 ssize_t retval;
1609 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1610 tevent_req_received(req);
1611 return -1;
1613 *vfs_aio_state = state->vfs_aio_state;
1614 retval = state->retval;
1615 tevent_req_received(req);
1616 return retval;
1619 ssize_t smb_vfs_call_pwrite(struct vfs_handle_struct *handle,
1620 struct files_struct *fsp, const void *data,
1621 size_t n, off_t offset)
1623 VFS_FIND(pwrite);
1624 return handle->fns->pwrite_fn(handle, fsp, data, n, offset);
1627 struct smb_vfs_call_pwrite_state {
1628 ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1629 ssize_t retval;
1630 struct vfs_aio_state vfs_aio_state;
1633 static void smb_vfs_call_pwrite_done(struct tevent_req *subreq);
1635 struct tevent_req *smb_vfs_call_pwrite_send(struct vfs_handle_struct *handle,
1636 TALLOC_CTX *mem_ctx,
1637 struct tevent_context *ev,
1638 struct files_struct *fsp,
1639 const void *data,
1640 size_t n, off_t offset)
1642 struct tevent_req *req, *subreq;
1643 struct smb_vfs_call_pwrite_state *state;
1645 req = tevent_req_create(mem_ctx, &state,
1646 struct smb_vfs_call_pwrite_state);
1647 if (req == NULL) {
1648 return NULL;
1650 VFS_FIND(pwrite_send);
1651 state->recv_fn = handle->fns->pwrite_recv_fn;
1653 subreq = handle->fns->pwrite_send_fn(handle, state, ev, fsp, data, n,
1654 offset);
1655 if (tevent_req_nomem(subreq, req)) {
1656 return tevent_req_post(req, ev);
1658 tevent_req_set_callback(subreq, smb_vfs_call_pwrite_done, req);
1659 return req;
1662 static void smb_vfs_call_pwrite_done(struct tevent_req *subreq)
1664 struct tevent_req *req = tevent_req_callback_data(
1665 subreq, struct tevent_req);
1666 struct smb_vfs_call_pwrite_state *state = tevent_req_data(
1667 req, struct smb_vfs_call_pwrite_state);
1669 state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1670 TALLOC_FREE(subreq);
1671 if (state->retval == -1) {
1672 tevent_req_error(req, state->vfs_aio_state.error);
1673 return;
1675 tevent_req_done(req);
1678 ssize_t SMB_VFS_PWRITE_RECV(struct tevent_req *req,
1679 struct vfs_aio_state *vfs_aio_state)
1681 struct smb_vfs_call_pwrite_state *state = tevent_req_data(
1682 req, struct smb_vfs_call_pwrite_state);
1683 ssize_t retval;
1685 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1686 tevent_req_received(req);
1687 return -1;
1689 *vfs_aio_state = state->vfs_aio_state;
1690 retval = state->retval;
1691 tevent_req_received(req);
1692 return retval;
1695 off_t smb_vfs_call_lseek(struct vfs_handle_struct *handle,
1696 struct files_struct *fsp, off_t offset,
1697 int whence)
1699 VFS_FIND(lseek);
1700 return handle->fns->lseek_fn(handle, fsp, offset, whence);
1703 ssize_t smb_vfs_call_sendfile(struct vfs_handle_struct *handle, int tofd,
1704 files_struct *fromfsp, const DATA_BLOB *header,
1705 off_t offset, size_t count)
1707 VFS_FIND(sendfile);
1708 return handle->fns->sendfile_fn(handle, tofd, fromfsp, header, offset,
1709 count);
1712 ssize_t smb_vfs_call_recvfile(struct vfs_handle_struct *handle, int fromfd,
1713 files_struct *tofsp, off_t offset,
1714 size_t count)
1716 VFS_FIND(recvfile);
1717 return handle->fns->recvfile_fn(handle, fromfd, tofsp, offset, count);
1720 int smb_vfs_call_renameat(struct vfs_handle_struct *handle,
1721 files_struct *srcfsp,
1722 const struct smb_filename *smb_fname_src,
1723 files_struct *dstfsp,
1724 const struct smb_filename *smb_fname_dst)
1726 VFS_FIND(renameat);
1727 return handle->fns->renameat_fn(handle,
1728 srcfsp,
1729 smb_fname_src,
1730 dstfsp,
1731 smb_fname_dst);
1734 struct smb_vfs_call_fsync_state {
1735 int (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1736 int retval;
1737 struct vfs_aio_state vfs_aio_state;
1740 static void smb_vfs_call_fsync_done(struct tevent_req *subreq);
1742 struct tevent_req *smb_vfs_call_fsync_send(struct vfs_handle_struct *handle,
1743 TALLOC_CTX *mem_ctx,
1744 struct tevent_context *ev,
1745 struct files_struct *fsp)
1747 struct tevent_req *req, *subreq;
1748 struct smb_vfs_call_fsync_state *state;
1750 req = tevent_req_create(mem_ctx, &state,
1751 struct smb_vfs_call_fsync_state);
1752 if (req == NULL) {
1753 return NULL;
1755 VFS_FIND(fsync_send);
1756 state->recv_fn = handle->fns->fsync_recv_fn;
1758 subreq = handle->fns->fsync_send_fn(handle, state, ev, fsp);
1759 if (tevent_req_nomem(subreq, req)) {
1760 return tevent_req_post(req, ev);
1762 tevent_req_set_callback(subreq, smb_vfs_call_fsync_done, req);
1763 return req;
1766 static void smb_vfs_call_fsync_done(struct tevent_req *subreq)
1768 struct tevent_req *req = tevent_req_callback_data(
1769 subreq, struct tevent_req);
1770 struct smb_vfs_call_fsync_state *state = tevent_req_data(
1771 req, struct smb_vfs_call_fsync_state);
1773 state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1774 TALLOC_FREE(subreq);
1775 if (state->retval == -1) {
1776 tevent_req_error(req, state->vfs_aio_state.error);
1777 return;
1779 tevent_req_done(req);
1782 int SMB_VFS_FSYNC_RECV(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state)
1784 struct smb_vfs_call_fsync_state *state = tevent_req_data(
1785 req, struct smb_vfs_call_fsync_state);
1786 ssize_t retval;
1788 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1789 tevent_req_received(req);
1790 return -1;
1792 *vfs_aio_state = state->vfs_aio_state;
1793 retval = state->retval;
1794 tevent_req_received(req);
1795 return retval;
1799 * Synchronous version of fsync, built from backend
1800 * async VFS primitives. Uses a temporary sub-event
1801 * context (NOT NESTED).
1804 int smb_vfs_fsync_sync(files_struct *fsp)
1806 TALLOC_CTX *frame = talloc_stackframe();
1807 struct tevent_req *req = NULL;
1808 struct vfs_aio_state aio_state = { 0 };
1809 int ret = -1;
1810 bool ok;
1811 struct tevent_context *ev = samba_tevent_context_init(frame);
1813 if (ev == NULL) {
1814 goto out;
1817 req = SMB_VFS_FSYNC_SEND(talloc_tos(), ev, fsp);
1818 if (req == NULL) {
1819 goto out;
1822 ok = tevent_req_poll(req, ev);
1823 if (!ok) {
1824 goto out;
1827 ret = SMB_VFS_FSYNC_RECV(req, &aio_state);
1829 out:
1831 TALLOC_FREE(frame);
1832 if (aio_state.error != 0) {
1833 errno = aio_state.error;
1835 return ret;
1838 int smb_vfs_call_stat(struct vfs_handle_struct *handle,
1839 struct smb_filename *smb_fname)
1841 VFS_FIND(stat);
1842 return handle->fns->stat_fn(handle, smb_fname);
1845 int smb_vfs_call_fstat(struct vfs_handle_struct *handle,
1846 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1848 VFS_FIND(fstat);
1849 return handle->fns->fstat_fn(handle, fsp, sbuf);
1852 int smb_vfs_call_lstat(struct vfs_handle_struct *handle,
1853 struct smb_filename *smb_filename)
1855 VFS_FIND(lstat);
1856 return handle->fns->lstat_fn(handle, smb_filename);
1859 int smb_vfs_call_fstatat(
1860 struct vfs_handle_struct *handle,
1861 const struct files_struct *dirfsp,
1862 const struct smb_filename *smb_fname,
1863 SMB_STRUCT_STAT *sbuf,
1864 int flags)
1866 VFS_FIND(fstatat);
1867 return handle->fns->fstatat_fn(handle, dirfsp, smb_fname, sbuf, flags);
1870 uint64_t smb_vfs_call_get_alloc_size(struct vfs_handle_struct *handle,
1871 struct files_struct *fsp,
1872 const SMB_STRUCT_STAT *sbuf)
1874 VFS_FIND(get_alloc_size);
1875 return handle->fns->get_alloc_size_fn(handle, fsp, sbuf);
1878 int smb_vfs_call_unlinkat(struct vfs_handle_struct *handle,
1879 struct files_struct *dirfsp,
1880 const struct smb_filename *smb_fname,
1881 int flags)
1883 VFS_FIND(unlinkat);
1884 return handle->fns->unlinkat_fn(handle,
1885 dirfsp,
1886 smb_fname,
1887 flags);
1890 int smb_vfs_call_fchmod(struct vfs_handle_struct *handle,
1891 struct files_struct *fsp, mode_t mode)
1893 VFS_FIND(fchmod);
1894 return handle->fns->fchmod_fn(handle, fsp, mode);
1897 int smb_vfs_call_fchown(struct vfs_handle_struct *handle,
1898 struct files_struct *fsp, uid_t uid, gid_t gid)
1900 VFS_FIND(fchown);
1901 return handle->fns->fchown_fn(handle, fsp, uid, gid);
1904 int smb_vfs_call_lchown(struct vfs_handle_struct *handle,
1905 const struct smb_filename *smb_fname,
1906 uid_t uid,
1907 gid_t gid)
1909 VFS_FIND(lchown);
1910 return handle->fns->lchown_fn(handle, smb_fname, uid, gid);
1913 int smb_vfs_call_chdir(struct vfs_handle_struct *handle,
1914 const struct smb_filename *smb_fname)
1916 VFS_FIND(chdir);
1917 return handle->fns->chdir_fn(handle, smb_fname);
1920 struct smb_filename *smb_vfs_call_getwd(struct vfs_handle_struct *handle,
1921 TALLOC_CTX *ctx)
1923 VFS_FIND(getwd);
1924 return handle->fns->getwd_fn(handle, ctx);
1927 int smb_vfs_call_fntimes(struct vfs_handle_struct *handle,
1928 struct files_struct *fsp,
1929 struct smb_file_time *ft)
1931 VFS_FIND(fntimes);
1932 return handle->fns->fntimes_fn(handle, fsp, ft);
1935 int smb_vfs_call_ftruncate(struct vfs_handle_struct *handle,
1936 struct files_struct *fsp, off_t offset)
1938 VFS_FIND(ftruncate);
1939 return handle->fns->ftruncate_fn(handle, fsp, offset);
1942 int smb_vfs_call_fallocate(struct vfs_handle_struct *handle,
1943 struct files_struct *fsp,
1944 uint32_t mode,
1945 off_t offset,
1946 off_t len)
1948 VFS_FIND(fallocate);
1949 return handle->fns->fallocate_fn(handle, fsp, mode, offset, len);
1952 int smb_vfs_call_filesystem_sharemode(struct vfs_handle_struct *handle,
1953 struct files_struct *fsp,
1954 uint32_t share_mode,
1955 uint32_t access_mask)
1957 VFS_FIND(filesystem_sharemode);
1958 return handle->fns->filesystem_sharemode_fn(handle,
1959 fsp,
1960 share_mode,
1961 access_mask);
1964 int smb_vfs_call_fcntl(struct vfs_handle_struct *handle,
1965 struct files_struct *fsp, int cmd, ...)
1967 int result;
1968 va_list cmd_arg;
1970 VFS_FIND(fcntl);
1972 va_start(cmd_arg, cmd);
1973 result = handle->fns->fcntl_fn(handle, fsp, cmd, cmd_arg);
1974 va_end(cmd_arg);
1976 return result;
1979 int smb_vfs_call_linux_setlease(struct vfs_handle_struct *handle,
1980 struct files_struct *fsp, int leasetype)
1982 VFS_FIND(linux_setlease);
1983 return handle->fns->linux_setlease_fn(handle, fsp, leasetype);
1986 int smb_vfs_call_symlinkat(struct vfs_handle_struct *handle,
1987 const struct smb_filename *link_target,
1988 struct files_struct *dirfsp,
1989 const struct smb_filename *new_smb_fname)
1991 VFS_FIND(symlinkat);
1992 return handle->fns->symlinkat_fn(handle,
1993 link_target,
1994 dirfsp,
1995 new_smb_fname);
1998 int smb_vfs_call_readlinkat(struct vfs_handle_struct *handle,
1999 const struct files_struct *dirfsp,
2000 const struct smb_filename *smb_fname,
2001 char *buf,
2002 size_t bufsiz)
2004 VFS_FIND(readlinkat);
2005 return handle->fns->readlinkat_fn(handle,
2006 dirfsp,
2007 smb_fname,
2008 buf,
2009 bufsiz);
2012 int smb_vfs_call_linkat(struct vfs_handle_struct *handle,
2013 struct files_struct *srcfsp,
2014 const struct smb_filename *old_smb_fname,
2015 struct files_struct *dstfsp,
2016 const struct smb_filename *new_smb_fname,
2017 int flags)
2019 VFS_FIND(linkat);
2020 return handle->fns->linkat_fn(handle,
2021 srcfsp,
2022 old_smb_fname,
2023 dstfsp,
2024 new_smb_fname,
2025 flags);
2028 int smb_vfs_call_mknodat(struct vfs_handle_struct *handle,
2029 struct files_struct *dirfsp,
2030 const struct smb_filename *smb_fname,
2031 mode_t mode,
2032 SMB_DEV_T dev)
2034 VFS_FIND(mknodat);
2035 return handle->fns->mknodat_fn(handle,
2036 dirfsp,
2037 smb_fname,
2038 mode,
2039 dev);
2042 struct smb_filename *smb_vfs_call_realpath(struct vfs_handle_struct *handle,
2043 TALLOC_CTX *ctx,
2044 const struct smb_filename *smb_fname)
2046 VFS_FIND(realpath);
2047 return handle->fns->realpath_fn(handle, ctx, smb_fname);
2050 int smb_vfs_call_fchflags(struct vfs_handle_struct *handle,
2051 struct files_struct *fsp,
2052 unsigned int flags)
2054 VFS_FIND(fchflags);
2055 return handle->fns->fchflags_fn(handle, fsp, flags);
2058 struct file_id smb_vfs_call_file_id_create(struct vfs_handle_struct *handle,
2059 const SMB_STRUCT_STAT *sbuf)
2061 VFS_FIND(file_id_create);
2062 return handle->fns->file_id_create_fn(handle, sbuf);
2065 uint64_t smb_vfs_call_fs_file_id(struct vfs_handle_struct *handle,
2066 const SMB_STRUCT_STAT *sbuf)
2068 VFS_FIND(fs_file_id);
2069 return handle->fns->fs_file_id_fn(handle, sbuf);
2072 NTSTATUS smb_vfs_call_fstreaminfo(struct vfs_handle_struct *handle,
2073 struct files_struct *fsp,
2074 TALLOC_CTX *mem_ctx,
2075 unsigned int *num_streams,
2076 struct stream_struct **streams)
2078 VFS_FIND(fstreaminfo);
2079 return handle->fns->fstreaminfo_fn(handle, fsp, mem_ctx,
2080 num_streams, streams);
2083 NTSTATUS smb_vfs_call_get_real_filename_at(struct vfs_handle_struct *handle,
2084 struct files_struct *dirfsp,
2085 const char *name,
2086 TALLOC_CTX *mem_ctx,
2087 char **found_name)
2089 VFS_FIND(get_real_filename_at);
2090 return handle->fns->get_real_filename_at_fn(
2091 handle, dirfsp, name, mem_ctx, found_name);
2094 const char *smb_vfs_call_connectpath(struct vfs_handle_struct *handle,
2095 const struct files_struct *dirfsp,
2096 const struct smb_filename *smb_fname)
2098 VFS_FIND(connectpath);
2099 return handle->fns->connectpath_fn(handle, dirfsp, smb_fname);
2102 bool smb_vfs_call_strict_lock_check(struct vfs_handle_struct *handle,
2103 struct files_struct *fsp,
2104 struct lock_struct *plock)
2106 VFS_FIND(strict_lock_check);
2107 return handle->fns->strict_lock_check_fn(handle, fsp, plock);
2110 NTSTATUS smb_vfs_call_translate_name(struct vfs_handle_struct *handle,
2111 const char *name,
2112 enum vfs_translate_direction direction,
2113 TALLOC_CTX *mem_ctx,
2114 char **mapped_name)
2116 VFS_FIND(translate_name);
2117 return handle->fns->translate_name_fn(handle, name, direction, mem_ctx,
2118 mapped_name);
2121 NTSTATUS smb_vfs_call_parent_pathname(struct vfs_handle_struct *handle,
2122 TALLOC_CTX *mem_ctx,
2123 const struct smb_filename *smb_fname_in,
2124 struct smb_filename **parent_dir_out,
2125 struct smb_filename **atname_out)
2127 VFS_FIND(parent_pathname);
2128 return handle->fns->parent_pathname_fn(handle,
2129 mem_ctx,
2130 smb_fname_in,
2131 parent_dir_out,
2132 atname_out);
2135 NTSTATUS smb_vfs_call_fsctl(struct vfs_handle_struct *handle,
2136 struct files_struct *fsp,
2137 TALLOC_CTX *ctx,
2138 uint32_t function,
2139 uint16_t req_flags,
2140 const uint8_t *in_data,
2141 uint32_t in_len,
2142 uint8_t **out_data,
2143 uint32_t max_out_len,
2144 uint32_t *out_len)
2146 VFS_FIND(fsctl);
2147 return handle->fns->fsctl_fn(handle, fsp, ctx, function, req_flags,
2148 in_data, in_len, out_data, max_out_len,
2149 out_len);
2152 NTSTATUS smb_vfs_call_fget_dos_attributes(struct vfs_handle_struct *handle,
2153 struct files_struct *fsp,
2154 uint32_t *dosmode)
2156 VFS_FIND(fget_dos_attributes);
2157 return handle->fns->fget_dos_attributes_fn(handle, fsp, dosmode);
2160 NTSTATUS smb_vfs_call_fset_dos_attributes(struct vfs_handle_struct *handle,
2161 struct files_struct *fsp,
2162 uint32_t dosmode)
2164 VFS_FIND(fset_dos_attributes);
2165 return handle->fns->fset_dos_attributes_fn(handle, fsp, dosmode);
2168 struct tevent_req *smb_vfs_call_offload_read_send(TALLOC_CTX *mem_ctx,
2169 struct tevent_context *ev,
2170 struct vfs_handle_struct *handle,
2171 struct files_struct *fsp,
2172 uint32_t fsctl,
2173 uint32_t ttl,
2174 off_t offset,
2175 size_t to_copy)
2177 VFS_FIND(offload_read_send);
2178 return handle->fns->offload_read_send_fn(mem_ctx, ev, handle,
2179 fsp, fsctl,
2180 ttl, offset, to_copy);
2183 NTSTATUS smb_vfs_call_offload_read_recv(struct tevent_req *req,
2184 struct vfs_handle_struct *handle,
2185 TALLOC_CTX *mem_ctx,
2186 uint32_t *flags,
2187 uint64_t *xferlen,
2188 DATA_BLOB *token_blob)
2190 VFS_FIND(offload_read_recv);
2191 return handle->fns->offload_read_recv_fn(req, handle, mem_ctx, flags, xferlen, token_blob);
2194 struct tevent_req *smb_vfs_call_offload_write_send(struct vfs_handle_struct *handle,
2195 TALLOC_CTX *mem_ctx,
2196 struct tevent_context *ev,
2197 uint32_t fsctl,
2198 DATA_BLOB *token,
2199 off_t transfer_offset,
2200 struct files_struct *dest_fsp,
2201 off_t dest_off,
2202 off_t num)
2204 VFS_FIND(offload_write_send);
2205 return handle->fns->offload_write_send_fn(handle, mem_ctx, ev, fsctl,
2206 token, transfer_offset,
2207 dest_fsp, dest_off, num);
2210 NTSTATUS smb_vfs_call_offload_write_recv(struct vfs_handle_struct *handle,
2211 struct tevent_req *req,
2212 off_t *copied)
2214 VFS_FIND(offload_write_recv);
2215 return handle->fns->offload_write_recv_fn(handle, req, copied);
2218 struct smb_vfs_call_get_dos_attributes_state {
2219 files_struct *dir_fsp;
2220 NTSTATUS (*recv_fn)(struct tevent_req *req,
2221 struct vfs_aio_state *aio_state,
2222 uint32_t *dosmode);
2223 struct vfs_aio_state aio_state;
2224 uint32_t dos_attributes;
2227 static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq);
2229 struct tevent_req *smb_vfs_call_get_dos_attributes_send(
2230 TALLOC_CTX *mem_ctx,
2231 struct tevent_context *ev,
2232 struct vfs_handle_struct *handle,
2233 files_struct *dir_fsp,
2234 struct smb_filename *smb_fname)
2236 struct tevent_req *req = NULL;
2237 struct smb_vfs_call_get_dos_attributes_state *state = NULL;
2238 struct tevent_req *subreq = NULL;
2240 req = tevent_req_create(mem_ctx, &state,
2241 struct smb_vfs_call_get_dos_attributes_state);
2242 if (req == NULL) {
2243 return NULL;
2246 VFS_FIND(get_dos_attributes_send);
2248 *state = (struct smb_vfs_call_get_dos_attributes_state) {
2249 .dir_fsp = dir_fsp,
2250 .recv_fn = handle->fns->get_dos_attributes_recv_fn,
2253 subreq = handle->fns->get_dos_attributes_send_fn(mem_ctx,
2255 handle,
2256 dir_fsp,
2257 smb_fname);
2258 if (tevent_req_nomem(subreq, req)) {
2259 return tevent_req_post(req, ev);
2261 tevent_req_defer_callback(req, ev);
2263 tevent_req_set_callback(subreq,
2264 smb_vfs_call_get_dos_attributes_done,
2265 req);
2267 return req;
2270 static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq)
2272 struct tevent_req *req =
2273 tevent_req_callback_data(subreq,
2274 struct tevent_req);
2275 struct smb_vfs_call_get_dos_attributes_state *state =
2276 tevent_req_data(req,
2277 struct smb_vfs_call_get_dos_attributes_state);
2278 NTSTATUS status;
2279 bool ok;
2282 * Make sure we run as the user again
2284 ok = change_to_user_and_service_by_fsp(state->dir_fsp);
2285 SMB_ASSERT(ok);
2287 status = state->recv_fn(subreq,
2288 &state->aio_state,
2289 &state->dos_attributes);
2290 TALLOC_FREE(subreq);
2291 if (tevent_req_nterror(req, status)) {
2292 return;
2295 tevent_req_done(req);
2298 NTSTATUS smb_vfs_call_get_dos_attributes_recv(
2299 struct tevent_req *req,
2300 struct vfs_aio_state *aio_state,
2301 uint32_t *dos_attributes)
2303 struct smb_vfs_call_get_dos_attributes_state *state =
2304 tevent_req_data(req,
2305 struct smb_vfs_call_get_dos_attributes_state);
2306 NTSTATUS status;
2308 if (tevent_req_is_nterror(req, &status)) {
2309 tevent_req_received(req);
2310 return status;
2313 *aio_state = state->aio_state;
2314 *dos_attributes = state->dos_attributes;
2315 tevent_req_received(req);
2316 return NT_STATUS_OK;
2319 NTSTATUS smb_vfs_call_fget_compression(vfs_handle_struct *handle,
2320 TALLOC_CTX *mem_ctx,
2321 struct files_struct *fsp,
2322 uint16_t *_compression_fmt)
2324 VFS_FIND(fget_compression);
2325 return handle->fns->fget_compression_fn(handle, mem_ctx, fsp,
2326 _compression_fmt);
2329 NTSTATUS smb_vfs_call_set_compression(vfs_handle_struct *handle,
2330 TALLOC_CTX *mem_ctx,
2331 struct files_struct *fsp,
2332 uint16_t compression_fmt)
2334 VFS_FIND(set_compression);
2335 return handle->fns->set_compression_fn(handle, mem_ctx, fsp,
2336 compression_fmt);
2339 NTSTATUS smb_vfs_call_snap_check_path(vfs_handle_struct *handle,
2340 TALLOC_CTX *mem_ctx,
2341 const char *service_path,
2342 char **base_volume)
2344 VFS_FIND(snap_check_path);
2345 return handle->fns->snap_check_path_fn(handle, mem_ctx, service_path,
2346 base_volume);
2349 NTSTATUS smb_vfs_call_snap_create(struct vfs_handle_struct *handle,
2350 TALLOC_CTX *mem_ctx,
2351 const char *base_volume,
2352 time_t *tstamp,
2353 bool rw,
2354 char **base_path,
2355 char **snap_path)
2357 VFS_FIND(snap_create);
2358 return handle->fns->snap_create_fn(handle, mem_ctx, base_volume, tstamp,
2359 rw, base_path, snap_path);
2362 NTSTATUS smb_vfs_call_snap_delete(struct vfs_handle_struct *handle,
2363 TALLOC_CTX *mem_ctx,
2364 char *base_path,
2365 char *snap_path)
2367 VFS_FIND(snap_delete);
2368 return handle->fns->snap_delete_fn(handle, mem_ctx, base_path,
2369 snap_path);
2372 NTSTATUS smb_vfs_call_fget_nt_acl(struct vfs_handle_struct *handle,
2373 struct files_struct *fsp,
2374 uint32_t security_info,
2375 TALLOC_CTX *mem_ctx,
2376 struct security_descriptor **ppdesc)
2378 VFS_FIND(fget_nt_acl);
2379 return handle->fns->fget_nt_acl_fn(handle, fsp, security_info,
2380 mem_ctx, ppdesc);
2383 NTSTATUS smb_vfs_call_fset_nt_acl(struct vfs_handle_struct *handle,
2384 struct files_struct *fsp,
2385 uint32_t security_info_sent,
2386 const struct security_descriptor *psd)
2388 VFS_FIND(fset_nt_acl);
2389 return handle->fns->fset_nt_acl_fn(handle, fsp, security_info_sent,
2390 psd);
2393 NTSTATUS smb_vfs_call_audit_file(struct vfs_handle_struct *handle,
2394 struct smb_filename *file,
2395 struct security_acl *sacl,
2396 uint32_t access_requested,
2397 uint32_t access_denied)
2399 VFS_FIND(audit_file);
2400 return handle->fns->audit_file_fn(handle,
2401 file,
2402 sacl,
2403 access_requested,
2404 access_denied);
2407 SMB_ACL_T smb_vfs_call_sys_acl_get_fd(struct vfs_handle_struct *handle,
2408 struct files_struct *fsp,
2409 SMB_ACL_TYPE_T type,
2410 TALLOC_CTX *mem_ctx)
2412 VFS_FIND(sys_acl_get_fd);
2413 return handle->fns->sys_acl_get_fd_fn(handle, fsp, type, mem_ctx);
2416 int smb_vfs_call_sys_acl_blob_get_fd(struct vfs_handle_struct *handle,
2417 struct files_struct *fsp,
2418 TALLOC_CTX *mem_ctx,
2419 char **blob_description,
2420 DATA_BLOB *blob)
2422 VFS_FIND(sys_acl_blob_get_fd);
2423 return handle->fns->sys_acl_blob_get_fd_fn(handle, fsp, mem_ctx, blob_description, blob);
2426 int smb_vfs_call_sys_acl_set_fd(struct vfs_handle_struct *handle,
2427 struct files_struct *fsp,
2428 SMB_ACL_TYPE_T type,
2429 SMB_ACL_T theacl)
2431 VFS_FIND(sys_acl_set_fd);
2432 return handle->fns->sys_acl_set_fd_fn(handle, fsp, type, theacl);
2435 int smb_vfs_call_sys_acl_delete_def_fd(struct vfs_handle_struct *handle,
2436 struct files_struct *fsp)
2438 VFS_FIND(sys_acl_delete_def_fd);
2439 return handle->fns->sys_acl_delete_def_fd_fn(handle, fsp);
2442 struct smb_vfs_call_getxattrat_state {
2443 files_struct *dir_fsp;
2444 ssize_t (*recv_fn)(struct tevent_req *req,
2445 struct vfs_aio_state *aio_state,
2446 TALLOC_CTX *mem_ctx,
2447 uint8_t **xattr_value);
2448 ssize_t retval;
2449 uint8_t *xattr_value;
2450 struct vfs_aio_state aio_state;
2453 static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq);
2455 struct tevent_req *smb_vfs_call_getxattrat_send(
2456 TALLOC_CTX *mem_ctx,
2457 struct tevent_context *ev,
2458 struct vfs_handle_struct *handle,
2459 files_struct *dir_fsp,
2460 const struct smb_filename *smb_fname,
2461 const char *xattr_name,
2462 size_t alloc_hint)
2464 struct tevent_req *req = NULL;
2465 struct smb_vfs_call_getxattrat_state *state = NULL;
2466 struct tevent_req *subreq = NULL;
2468 req = tevent_req_create(mem_ctx, &state,
2469 struct smb_vfs_call_getxattrat_state);
2470 if (req == NULL) {
2471 return NULL;
2474 VFS_FIND(getxattrat_send);
2476 *state = (struct smb_vfs_call_getxattrat_state) {
2477 .dir_fsp = dir_fsp,
2478 .recv_fn = handle->fns->getxattrat_recv_fn,
2481 subreq = handle->fns->getxattrat_send_fn(mem_ctx,
2483 handle,
2484 dir_fsp,
2485 smb_fname,
2486 xattr_name,
2487 alloc_hint);
2488 if (tevent_req_nomem(subreq, req)) {
2489 return tevent_req_post(req, ev);
2491 tevent_req_defer_callback(req, ev);
2493 tevent_req_set_callback(subreq, smb_vfs_call_getxattrat_done, req);
2494 return req;
2497 static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq)
2499 struct tevent_req *req = tevent_req_callback_data(
2500 subreq, struct tevent_req);
2501 struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
2502 req, struct smb_vfs_call_getxattrat_state);
2503 bool ok;
2506 * Make sure we run as the user again
2508 ok = change_to_user_and_service_by_fsp(state->dir_fsp);
2509 SMB_ASSERT(ok);
2511 state->retval = state->recv_fn(subreq,
2512 &state->aio_state,
2513 state,
2514 &state->xattr_value);
2515 TALLOC_FREE(subreq);
2516 if (state->retval == -1) {
2517 tevent_req_error(req, state->aio_state.error);
2518 return;
2521 tevent_req_done(req);
2524 ssize_t smb_vfs_call_getxattrat_recv(struct tevent_req *req,
2525 struct vfs_aio_state *aio_state,
2526 TALLOC_CTX *mem_ctx,
2527 uint8_t **xattr_value)
2529 struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
2530 req, struct smb_vfs_call_getxattrat_state);
2531 size_t xattr_size;
2533 if (tevent_req_is_unix_error(req, &aio_state->error)) {
2534 tevent_req_received(req);
2535 return -1;
2538 *aio_state = state->aio_state;
2539 xattr_size = state->retval;
2540 if (xattr_value != NULL) {
2541 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2544 tevent_req_received(req);
2545 return xattr_size;
2548 ssize_t smb_vfs_call_fgetxattr(struct vfs_handle_struct *handle,
2549 struct files_struct *fsp, const char *name,
2550 void *value, size_t size)
2552 VFS_FIND(fgetxattr);
2553 return handle->fns->fgetxattr_fn(handle, fsp, name, value, size);
2556 ssize_t smb_vfs_call_flistxattr(struct vfs_handle_struct *handle,
2557 struct files_struct *fsp, char *list,
2558 size_t size)
2560 VFS_FIND(flistxattr);
2561 return handle->fns->flistxattr_fn(handle, fsp, list, size);
2564 int smb_vfs_call_fremovexattr(struct vfs_handle_struct *handle,
2565 struct files_struct *fsp, const char *name)
2567 VFS_FIND(fremovexattr);
2568 return handle->fns->fremovexattr_fn(handle, fsp, name);
2571 int smb_vfs_call_fsetxattr(struct vfs_handle_struct *handle,
2572 struct files_struct *fsp, const char *name,
2573 const void *value, size_t size, int flags)
2575 VFS_FIND(fsetxattr);
2576 return handle->fns->fsetxattr_fn(handle, fsp, name, value, size, flags);
2579 bool smb_vfs_call_aio_force(struct vfs_handle_struct *handle,
2580 struct files_struct *fsp)
2582 VFS_FIND(aio_force);
2583 return handle->fns->aio_force_fn(handle, fsp);
2586 NTSTATUS smb_vfs_call_durable_cookie(struct vfs_handle_struct *handle,
2587 struct files_struct *fsp,
2588 TALLOC_CTX *mem_ctx,
2589 DATA_BLOB *cookie)
2591 VFS_FIND(durable_cookie);
2592 return handle->fns->durable_cookie_fn(handle, fsp, mem_ctx, cookie);
2595 NTSTATUS smb_vfs_call_durable_disconnect(struct vfs_handle_struct *handle,
2596 struct files_struct *fsp,
2597 const DATA_BLOB old_cookie,
2598 TALLOC_CTX *mem_ctx,
2599 DATA_BLOB *new_cookie)
2601 VFS_FIND(durable_disconnect);
2602 return handle->fns->durable_disconnect_fn(handle, fsp, old_cookie,
2603 mem_ctx, new_cookie);
2606 NTSTATUS smb_vfs_call_durable_reconnect(struct vfs_handle_struct *handle,
2607 struct smb_request *smb1req,
2608 struct smbXsrv_open *op,
2609 const DATA_BLOB old_cookie,
2610 TALLOC_CTX *mem_ctx,
2611 struct files_struct **fsp,
2612 DATA_BLOB *new_cookie)
2614 VFS_FIND(durable_reconnect);
2615 return handle->fns->durable_reconnect_fn(handle, smb1req, op,
2616 old_cookie, mem_ctx, fsp,
2617 new_cookie);
2620 NTSTATUS smb_vfs_call_freaddir_attr(struct vfs_handle_struct *handle,
2621 struct files_struct *fsp,
2622 TALLOC_CTX *mem_ctx,
2623 struct readdir_attr_data **attr_data)
2625 VFS_FIND(freaddir_attr);
2626 return handle->fns->freaddir_attr_fn(handle,
2627 fsp,
2628 mem_ctx,
2629 attr_data);
2632 bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
2633 struct files_struct *fsp, int op, off_t offset,
2634 off_t count, int type)
2636 VFS_FIND(lock);
2637 return handle->fns->lock_fn(handle, fsp, op, offset, count, type);
2640 bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
2641 struct files_struct *fsp, off_t *poffset,
2642 off_t *pcount, int *ptype, pid_t *ppid)
2644 VFS_FIND(getlock);
2645 return handle->fns->getlock_fn(handle, fsp, poffset, pcount, ptype,
2646 ppid);
2649 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
2650 struct byte_range_lock *br_lck,
2651 struct lock_struct *plock)
2653 VFS_FIND(brl_lock_windows);
2654 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock);
2657 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
2658 struct byte_range_lock *br_lck,
2659 const struct lock_struct *plock)
2661 VFS_FIND(brl_unlock_windows);
2662 return handle->fns->brl_unlock_windows_fn(handle, br_lck, plock);