2 Unix SMB/CIFS implementation.
3 Wrap disk only vfs functions to sidestep dodgy compilers.
4 Copyright (C) Tim Potter 1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
27 #include "smbprofile.h"
28 #include "../libcli/security/security.h"
29 #include "passdb/lookup_sid.h"
30 #include "source3/include/msdfs.h"
31 #include "librpc/gen_ndr/ndr_dfsblobs.h"
32 #include "lib/util/tevent_unix.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/sys_rw.h"
35 #include "lib/pthreadpool/pthreadpool_tevent.h"
36 #include "librpc/gen_ndr/ndr_ioctl.h"
37 #include "offload_token.h"
40 #define DBGC_CLASS DBGC_VFS
42 /* Check for NULL pointer parameters in vfswrap_* functions */
44 /* We don't want to have NULL function pointers lying around. Someone
45 is sure to try and execute them. These stubs are used to prevent
48 static int vfswrap_connect(vfs_handle_struct *handle, const char *service, const char *user)
50 return 0; /* Return >= 0 for success */
53 static void vfswrap_disconnect(vfs_handle_struct *handle)
59 static uint64_t vfswrap_disk_free(vfs_handle_struct *handle,
60 const struct smb_filename *smb_fname,
65 if (sys_fsusage(smb_fname->base_name, dfree, dsize) != 0) {
73 static int vfswrap_get_quota(struct vfs_handle_struct *handle,
74 const struct smb_filename *smb_fname,
75 enum SMB_QUOTA_TYPE qtype,
79 #ifdef HAVE_SYS_QUOTAS
82 START_PROFILE(syscall_get_quota);
83 result = sys_get_quota(smb_fname->base_name, qtype, id, qt);
84 END_PROFILE(syscall_get_quota);
92 static int vfswrap_set_quota(struct vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
94 #ifdef HAVE_SYS_QUOTAS
97 START_PROFILE(syscall_set_quota);
98 result = sys_set_quota(handle->conn->connectpath, qtype, id, qt);
99 END_PROFILE(syscall_set_quota);
107 static int vfswrap_get_shadow_copy_data(struct vfs_handle_struct *handle,
108 struct files_struct *fsp,
109 struct shadow_copy_data *shadow_copy_data,
113 return -1; /* Not implemented. */
116 static int vfswrap_statvfs(struct vfs_handle_struct *handle,
117 const struct smb_filename *smb_fname,
118 vfs_statvfs_struct *statbuf)
120 return sys_statvfs(smb_fname->base_name, statbuf);
123 static uint32_t vfswrap_fs_capabilities(struct vfs_handle_struct *handle,
124 enum timestamp_set_resolution *p_ts_res)
126 connection_struct *conn = handle->conn;
127 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
128 struct smb_filename *smb_fname_cpath = NULL;
129 struct vfs_statvfs_struct statbuf;
132 smb_fname_cpath = synthetic_smb_fname(talloc_tos(), conn->connectpath,
134 if (smb_fname_cpath == NULL) {
138 ZERO_STRUCT(statbuf);
139 ret = SMB_VFS_STATVFS(conn, smb_fname_cpath, &statbuf);
141 caps = statbuf.FsCapabilities;
144 *p_ts_res = TIMESTAMP_SET_SECONDS;
146 /* Work out what timestamp resolution we can
147 * use when setting a timestamp. */
149 ret = SMB_VFS_STAT(conn, smb_fname_cpath);
151 TALLOC_FREE(smb_fname_cpath);
155 if (smb_fname_cpath->st.st_ex_mtime.tv_nsec ||
156 smb_fname_cpath->st.st_ex_atime.tv_nsec ||
157 smb_fname_cpath->st.st_ex_ctime.tv_nsec) {
158 /* If any of the normal UNIX directory timestamps
159 * have a non-zero tv_nsec component assume
160 * we might be able to set sub-second timestamps.
161 * See what filetime set primitives we have.
163 #if defined(HAVE_UTIMENSAT)
164 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
165 #elif defined(HAVE_UTIMES)
166 /* utimes allows msec timestamps to be set. */
167 *p_ts_res = TIMESTAMP_SET_MSEC;
168 #elif defined(HAVE_UTIME)
169 /* utime only allows sec timestamps to be set. */
170 *p_ts_res = TIMESTAMP_SET_SECONDS;
173 DEBUG(10,("vfswrap_fs_capabilities: timestamp "
175 "available on share %s, directory %s\n",
176 *p_ts_res == TIMESTAMP_SET_MSEC ? "msec" : "sec",
177 lp_servicename(talloc_tos(), conn->params->service),
178 conn->connectpath ));
180 TALLOC_FREE(smb_fname_cpath);
184 static NTSTATUS vfswrap_get_dfs_referrals(struct vfs_handle_struct *handle,
185 struct dfs_GetDFSReferral *r)
187 struct junction_map *junction = NULL;
189 bool self_referral = false;
190 char *pathnamep = NULL;
191 char *local_dfs_path = NULL;
194 uint16_t max_referral_level = r->in.req.max_referral_level;
197 NDR_PRINT_IN_DEBUG(dfs_GetDFSReferral, r);
200 /* get the junction entry */
201 if (r->in.req.servername == NULL) {
202 return NT_STATUS_NOT_FOUND;
206 * Trim pathname sent by client so it begins with only one backslash.
207 * Two backslashes confuse some dfs clients
210 local_dfs_path = talloc_strdup(r, r->in.req.servername);
211 if (local_dfs_path == NULL) {
212 return NT_STATUS_NO_MEMORY;
214 pathnamep = local_dfs_path;
215 while (IS_DIRECTORY_SEP(pathnamep[0]) &&
216 IS_DIRECTORY_SEP(pathnamep[1])) {
220 junction = talloc_zero(r, struct junction_map);
221 if (junction == NULL) {
222 return NT_STATUS_NO_MEMORY;
225 /* The following call can change cwd. */
226 status = get_referred_path(r, pathnamep,
227 handle->conn->sconn->remote_address,
228 handle->conn->sconn->local_address,
229 !handle->conn->sconn->using_smb2,
230 junction, &consumedcnt, &self_referral);
231 if (!NT_STATUS_IS_OK(status)) {
232 struct smb_filename connectpath_fname = {
233 .base_name = handle->conn->connectpath
235 vfs_ChDir(handle->conn, &connectpath_fname);
239 struct smb_filename connectpath_fname = {
240 .base_name = handle->conn->connectpath
242 vfs_ChDir(handle->conn, &connectpath_fname);
245 if (!self_referral) {
246 pathnamep[consumedcnt] = '\0';
249 dbgtext("Path %s to alternate path(s):",
251 for (i=0; i < junction->referral_count; i++) {
253 junction->referral_list[i].alternate_path);
259 if (r->in.req.max_referral_level <= 2) {
260 max_referral_level = 2;
262 if (r->in.req.max_referral_level >= 3) {
263 max_referral_level = 3;
266 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
267 if (r->out.resp == NULL) {
268 return NT_STATUS_NO_MEMORY;
271 r->out.resp->path_consumed = strlen_m(pathnamep) * 2;
272 r->out.resp->nb_referrals = junction->referral_count;
274 r->out.resp->header_flags = DFS_HEADER_FLAG_STORAGE_SVR;
276 r->out.resp->header_flags |= DFS_HEADER_FLAG_REFERAL_SVR;
279 r->out.resp->referral_entries = talloc_zero_array(r,
280 struct dfs_referral_type,
281 r->out.resp->nb_referrals);
282 if (r->out.resp->referral_entries == NULL) {
283 return NT_STATUS_NO_MEMORY;
286 switch (max_referral_level) {
288 for(i=0; i < junction->referral_count; i++) {
289 struct referral *ref = &junction->referral_list[i];
290 TALLOC_CTX *mem_ctx = r->out.resp->referral_entries;
291 struct dfs_referral_type *t =
292 &r->out.resp->referral_entries[i];
293 struct dfs_referral_v2 *v2 = &t->referral.v2;
296 v2->size = VERSION2_REFERRAL_SIZE;
298 v2->server_type = DFS_SERVER_ROOT;
300 v2->server_type = DFS_SERVER_NON_ROOT;
303 v2->proximity = ref->proximity;
305 v2->DFS_path = talloc_strdup(mem_ctx, pathnamep);
306 if (v2->DFS_path == NULL) {
307 return NT_STATUS_NO_MEMORY;
309 v2->DFS_alt_path = talloc_strdup(mem_ctx, pathnamep);
310 if (v2->DFS_alt_path == NULL) {
311 return NT_STATUS_NO_MEMORY;
313 v2->netw_address = talloc_strdup(mem_ctx,
314 ref->alternate_path);
315 if (v2->netw_address == NULL) {
316 return NT_STATUS_NO_MEMORY;
322 for(i=0; i < junction->referral_count; i++) {
323 struct referral *ref = &junction->referral_list[i];
324 TALLOC_CTX *mem_ctx = r->out.resp->referral_entries;
325 struct dfs_referral_type *t =
326 &r->out.resp->referral_entries[i];
327 struct dfs_referral_v3 *v3 = &t->referral.v3;
328 struct dfs_normal_referral *r1 = &v3->referrals.r1;
331 v3->size = VERSION3_REFERRAL_SIZE;
333 v3->server_type = DFS_SERVER_ROOT;
335 v3->server_type = DFS_SERVER_NON_ROOT;
339 r1->DFS_path = talloc_strdup(mem_ctx, pathnamep);
340 if (r1->DFS_path == NULL) {
341 return NT_STATUS_NO_MEMORY;
343 r1->DFS_alt_path = talloc_strdup(mem_ctx, pathnamep);
344 if (r1->DFS_alt_path == NULL) {
345 return NT_STATUS_NO_MEMORY;
347 r1->netw_address = talloc_strdup(mem_ctx,
348 ref->alternate_path);
349 if (r1->netw_address == NULL) {
350 return NT_STATUS_NO_MEMORY;
355 DEBUG(0,("Invalid dfs referral version: %d\n",
356 max_referral_level));
357 return NT_STATUS_INVALID_LEVEL;
361 NDR_PRINT_OUT_DEBUG(dfs_GetDFSReferral, r);
367 static NTSTATUS vfswrap_snap_check_path(struct vfs_handle_struct *handle,
369 const char *service_path,
372 return NT_STATUS_NOT_SUPPORTED;
375 static NTSTATUS vfswrap_snap_create(struct vfs_handle_struct *handle,
377 const char *base_volume,
383 return NT_STATUS_NOT_SUPPORTED;
386 static NTSTATUS vfswrap_snap_delete(struct vfs_handle_struct *handle,
391 return NT_STATUS_NOT_SUPPORTED;
394 /* Directory operations */
396 static DIR *vfswrap_opendir(vfs_handle_struct *handle,
397 const struct smb_filename *smb_fname,
403 START_PROFILE(syscall_opendir);
404 result = opendir(smb_fname->base_name);
405 END_PROFILE(syscall_opendir);
409 static DIR *vfswrap_fdopendir(vfs_handle_struct *handle,
416 START_PROFILE(syscall_fdopendir);
417 result = sys_fdopendir(fsp->fh->fd);
418 END_PROFILE(syscall_fdopendir);
423 static struct dirent *vfswrap_readdir(vfs_handle_struct *handle,
425 SMB_STRUCT_STAT *sbuf)
427 struct dirent *result;
429 START_PROFILE(syscall_readdir);
430 result = readdir(dirp);
431 END_PROFILE(syscall_readdir);
433 /* Default Posix readdir() does not give us stat info.
434 * Set to invalid to indicate we didn't return this info. */
435 SET_STAT_INVALID(*sbuf);
436 #if defined(HAVE_DIRFD) && defined(HAVE_FSTATAT)
437 if (result != NULL) {
438 /* See if we can efficiently return this. */
440 int flags = AT_SYMLINK_NOFOLLOW;
441 int ret = fstatat(dirfd(dirp),
446 * As this is an optimization,
447 * ignore it if we stat'ed a
448 * symlink. Make the caller
449 * do it again as we don't
450 * know if they wanted the link
451 * info, or its target info.
453 if ((ret == 0) && (!S_ISLNK(st.st_mode))) {
454 init_stat_ex_from_stat(sbuf,
456 lp_fake_directory_create_times(
457 SNUM(handle->conn)));
465 static NTSTATUS vfswrap_readdir_attr(struct vfs_handle_struct *handle,
466 const struct smb_filename *fname,
468 struct readdir_attr_data **attr_data)
470 return NT_STATUS_NOT_SUPPORTED;
473 static void vfswrap_seekdir(vfs_handle_struct *handle, DIR *dirp, long offset)
475 START_PROFILE(syscall_seekdir);
476 seekdir(dirp, offset);
477 END_PROFILE(syscall_seekdir);
480 static long vfswrap_telldir(vfs_handle_struct *handle, DIR *dirp)
483 START_PROFILE(syscall_telldir);
484 result = telldir(dirp);
485 END_PROFILE(syscall_telldir);
489 static void vfswrap_rewinddir(vfs_handle_struct *handle, DIR *dirp)
491 START_PROFILE(syscall_rewinddir);
493 END_PROFILE(syscall_rewinddir);
496 static int vfswrap_mkdir(vfs_handle_struct *handle,
497 const struct smb_filename *smb_fname,
501 const char *path = smb_fname->base_name;
504 START_PROFILE(syscall_mkdir);
506 if (lp_inherit_acls(SNUM(handle->conn))
507 && parent_dirname(talloc_tos(), path, &parent, NULL)
508 && directory_has_default_acl(handle->conn, parent)) {
509 mode = (0777 & lp_directory_mask(SNUM(handle->conn)));
514 result = mkdir(path, mode);
516 END_PROFILE(syscall_mkdir);
520 static int vfswrap_rmdir(vfs_handle_struct *handle,
521 const struct smb_filename *smb_fname)
525 START_PROFILE(syscall_rmdir);
526 result = rmdir(smb_fname->base_name);
527 END_PROFILE(syscall_rmdir);
531 static int vfswrap_closedir(vfs_handle_struct *handle, DIR *dirp)
535 START_PROFILE(syscall_closedir);
536 result = closedir(dirp);
537 END_PROFILE(syscall_closedir);
541 /* File operations */
543 static int vfswrap_open(vfs_handle_struct *handle,
544 struct smb_filename *smb_fname,
545 files_struct *fsp, int flags, mode_t mode)
549 START_PROFILE(syscall_open);
551 if (smb_fname->stream_name) {
556 result = open(smb_fname->base_name, flags, mode);
558 END_PROFILE(syscall_open);
562 static NTSTATUS vfswrap_create_file(vfs_handle_struct *handle,
563 struct smb_request *req,
564 uint16_t root_dir_fid,
565 struct smb_filename *smb_fname,
566 uint32_t access_mask,
567 uint32_t share_access,
568 uint32_t create_disposition,
569 uint32_t create_options,
570 uint32_t file_attributes,
571 uint32_t oplock_request,
572 struct smb2_lease *lease,
573 uint64_t allocation_size,
574 uint32_t private_flags,
575 struct security_descriptor *sd,
576 struct ea_list *ea_list,
577 files_struct **result,
579 const struct smb2_create_blobs *in_context_blobs,
580 struct smb2_create_blobs *out_context_blobs)
582 return create_file_default(handle->conn, req, root_dir_fid, smb_fname,
583 access_mask, share_access,
584 create_disposition, create_options,
585 file_attributes, oplock_request, lease,
586 allocation_size, private_flags,
588 pinfo, in_context_blobs, out_context_blobs);
591 static int vfswrap_close(vfs_handle_struct *handle, files_struct *fsp)
595 START_PROFILE(syscall_close);
596 result = fd_close_posix(fsp);
597 END_PROFILE(syscall_close);
601 static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, void *data,
602 size_t n, off_t offset)
606 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
607 START_PROFILE_BYTES(syscall_pread, n);
608 result = sys_pread(fsp->fh->fd, data, n, offset);
609 END_PROFILE_BYTES(syscall_pread);
611 if (result == -1 && errno == ESPIPE) {
612 /* Maintain the fiction that pipes can be seeked (sought?) on. */
613 result = sys_read(fsp->fh->fd, data, n);
617 #else /* HAVE_PREAD */
620 #endif /* HAVE_PREAD */
625 static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, const void *data,
626 size_t n, off_t offset)
630 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
631 START_PROFILE_BYTES(syscall_pwrite, n);
632 result = sys_pwrite(fsp->fh->fd, data, n, offset);
633 END_PROFILE_BYTES(syscall_pwrite);
635 if (result == -1 && errno == ESPIPE) {
636 /* Maintain the fiction that pipes can be sought on. */
637 result = sys_write(fsp->fh->fd, data, n);
640 #else /* HAVE_PWRITE */
643 #endif /* HAVE_PWRITE */
648 struct vfswrap_pread_state {
655 struct vfs_aio_state vfs_aio_state;
656 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
659 static void vfs_pread_do(void *private_data);
660 static void vfs_pread_done(struct tevent_req *subreq);
661 static int vfs_pread_state_destructor(struct vfswrap_pread_state *state);
663 static struct tevent_req *vfswrap_pread_send(struct vfs_handle_struct *handle,
665 struct tevent_context *ev,
666 struct files_struct *fsp,
668 size_t n, off_t offset)
670 struct tevent_req *req, *subreq;
671 struct vfswrap_pread_state *state;
673 req = tevent_req_create(mem_ctx, &state, struct vfswrap_pread_state);
679 state->fd = fsp->fh->fd;
682 state->offset = offset;
684 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
685 state->profile_bytes, n);
686 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
688 subreq = pthreadpool_tevent_job_send(
689 state, ev, handle->conn->sconn->raw_thread_pool,
690 vfs_pread_do, state);
691 if (tevent_req_nomem(subreq, req)) {
692 return tevent_req_post(req, ev);
694 tevent_req_set_callback(subreq, vfs_pread_done, req);
696 talloc_set_destructor(state, vfs_pread_state_destructor);
701 static void vfs_pread_do(void *private_data)
703 struct vfswrap_pread_state *state = talloc_get_type_abort(
704 private_data, struct vfswrap_pread_state);
705 struct timespec start_time;
706 struct timespec end_time;
708 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
710 PROFILE_TIMESTAMP(&start_time);
713 state->ret = pread(state->fd, state->buf, state->count,
715 } while ((state->ret == -1) && (errno == EINTR));
717 if (state->ret == -1) {
718 state->vfs_aio_state.error = errno;
721 PROFILE_TIMESTAMP(&end_time);
723 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
725 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
728 static int vfs_pread_state_destructor(struct vfswrap_pread_state *state)
733 static void vfs_pread_done(struct tevent_req *subreq)
735 struct tevent_req *req = tevent_req_callback_data(
736 subreq, struct tevent_req);
737 struct vfswrap_pread_state *state = tevent_req_data(
738 req, struct vfswrap_pread_state);
741 ret = pthreadpool_tevent_job_recv(subreq);
743 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
744 talloc_set_destructor(state, NULL);
745 if (tevent_req_error(req, ret)) {
749 tevent_req_done(req);
752 static ssize_t vfswrap_pread_recv(struct tevent_req *req,
753 struct vfs_aio_state *vfs_aio_state)
755 struct vfswrap_pread_state *state = tevent_req_data(
756 req, struct vfswrap_pread_state);
758 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
762 *vfs_aio_state = state->vfs_aio_state;
766 struct vfswrap_pwrite_state {
773 struct vfs_aio_state vfs_aio_state;
774 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
777 static void vfs_pwrite_do(void *private_data);
778 static void vfs_pwrite_done(struct tevent_req *subreq);
779 static int vfs_pwrite_state_destructor(struct vfswrap_pwrite_state *state);
781 static struct tevent_req *vfswrap_pwrite_send(struct vfs_handle_struct *handle,
783 struct tevent_context *ev,
784 struct files_struct *fsp,
786 size_t n, off_t offset)
788 struct tevent_req *req, *subreq;
789 struct vfswrap_pwrite_state *state;
791 req = tevent_req_create(mem_ctx, &state, struct vfswrap_pwrite_state);
797 state->fd = fsp->fh->fd;
800 state->offset = offset;
802 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
803 state->profile_bytes, n);
804 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
806 subreq = pthreadpool_tevent_job_send(
807 state, ev, handle->conn->sconn->raw_thread_pool,
808 vfs_pwrite_do, state);
809 if (tevent_req_nomem(subreq, req)) {
810 return tevent_req_post(req, ev);
812 tevent_req_set_callback(subreq, vfs_pwrite_done, req);
814 talloc_set_destructor(state, vfs_pwrite_state_destructor);
819 static void vfs_pwrite_do(void *private_data)
821 struct vfswrap_pwrite_state *state = talloc_get_type_abort(
822 private_data, struct vfswrap_pwrite_state);
823 struct timespec start_time;
824 struct timespec end_time;
826 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
828 PROFILE_TIMESTAMP(&start_time);
831 state->ret = pwrite(state->fd, state->buf, state->count,
833 } while ((state->ret == -1) && (errno == EINTR));
835 if (state->ret == -1) {
836 state->vfs_aio_state.error = errno;
839 PROFILE_TIMESTAMP(&end_time);
841 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
843 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
846 static int vfs_pwrite_state_destructor(struct vfswrap_pwrite_state *state)
851 static void vfs_pwrite_done(struct tevent_req *subreq)
853 struct tevent_req *req = tevent_req_callback_data(
854 subreq, struct tevent_req);
855 struct vfswrap_pwrite_state *state = tevent_req_data(
856 req, struct vfswrap_pwrite_state);
859 ret = pthreadpool_tevent_job_recv(subreq);
861 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
862 talloc_set_destructor(state, NULL);
863 if (tevent_req_error(req, ret)) {
867 tevent_req_done(req);
870 static ssize_t vfswrap_pwrite_recv(struct tevent_req *req,
871 struct vfs_aio_state *vfs_aio_state)
873 struct vfswrap_pwrite_state *state = tevent_req_data(
874 req, struct vfswrap_pwrite_state);
876 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
880 *vfs_aio_state = state->vfs_aio_state;
884 struct vfswrap_fsync_state {
888 struct vfs_aio_state vfs_aio_state;
889 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
892 static void vfs_fsync_do(void *private_data);
893 static void vfs_fsync_done(struct tevent_req *subreq);
894 static int vfs_fsync_state_destructor(struct vfswrap_fsync_state *state);
896 static struct tevent_req *vfswrap_fsync_send(struct vfs_handle_struct *handle,
898 struct tevent_context *ev,
899 struct files_struct *fsp)
901 struct tevent_req *req, *subreq;
902 struct vfswrap_fsync_state *state;
904 req = tevent_req_create(mem_ctx, &state, struct vfswrap_fsync_state);
910 state->fd = fsp->fh->fd;
912 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
913 state->profile_bytes, 0);
914 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
916 subreq = pthreadpool_tevent_job_send(
917 state, ev, handle->conn->sconn->raw_thread_pool,
918 vfs_fsync_do, state);
919 if (tevent_req_nomem(subreq, req)) {
920 return tevent_req_post(req, ev);
922 tevent_req_set_callback(subreq, vfs_fsync_done, req);
924 talloc_set_destructor(state, vfs_fsync_state_destructor);
929 static void vfs_fsync_do(void *private_data)
931 struct vfswrap_fsync_state *state = talloc_get_type_abort(
932 private_data, struct vfswrap_fsync_state);
933 struct timespec start_time;
934 struct timespec end_time;
936 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
938 PROFILE_TIMESTAMP(&start_time);
941 state->ret = fsync(state->fd);
942 } while ((state->ret == -1) && (errno == EINTR));
944 if (state->ret == -1) {
945 state->vfs_aio_state.error = errno;
948 PROFILE_TIMESTAMP(&end_time);
950 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
952 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
955 static int vfs_fsync_state_destructor(struct vfswrap_fsync_state *state)
960 static void vfs_fsync_done(struct tevent_req *subreq)
962 struct tevent_req *req = tevent_req_callback_data(
963 subreq, struct tevent_req);
964 struct vfswrap_fsync_state *state = tevent_req_data(
965 req, struct vfswrap_fsync_state);
968 ret = pthreadpool_tevent_job_recv(subreq);
970 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
971 talloc_set_destructor(state, NULL);
972 if (tevent_req_error(req, ret)) {
976 tevent_req_done(req);
979 static int vfswrap_fsync_recv(struct tevent_req *req,
980 struct vfs_aio_state *vfs_aio_state)
982 struct vfswrap_fsync_state *state = tevent_req_data(
983 req, struct vfswrap_fsync_state);
985 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
989 *vfs_aio_state = state->vfs_aio_state;
993 static off_t vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
997 START_PROFILE(syscall_lseek);
999 /* Cope with 'stat' file opens. */
1000 if (fsp->fh->fd != -1)
1001 result = lseek(fsp->fh->fd, offset, whence);
1004 * We want to maintain the fiction that we can seek
1005 * on a fifo for file system purposes. This allows
1006 * people to set up UNIX fifo's that feed data to Windows
1007 * applications. JRA.
1010 if((result == -1) && (errno == ESPIPE)) {
1015 END_PROFILE(syscall_lseek);
1019 static ssize_t vfswrap_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
1020 off_t offset, size_t n)
1024 START_PROFILE_BYTES(syscall_sendfile, n);
1025 result = sys_sendfile(tofd, fromfsp->fh->fd, hdr, offset, n);
1026 END_PROFILE_BYTES(syscall_sendfile);
1030 static ssize_t vfswrap_recvfile(vfs_handle_struct *handle,
1032 files_struct *tofsp,
1038 START_PROFILE_BYTES(syscall_recvfile, n);
1039 result = sys_recvfile(fromfd, tofsp->fh->fd, offset, n);
1040 END_PROFILE_BYTES(syscall_recvfile);
1044 static int vfswrap_rename(vfs_handle_struct *handle,
1045 const struct smb_filename *smb_fname_src,
1046 const struct smb_filename *smb_fname_dst)
1050 START_PROFILE(syscall_rename);
1052 if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
1057 result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
1060 END_PROFILE(syscall_rename);
1064 static int vfswrap_stat(vfs_handle_struct *handle,
1065 struct smb_filename *smb_fname)
1069 START_PROFILE(syscall_stat);
1071 if (smb_fname->stream_name) {
1076 result = sys_stat(smb_fname->base_name, &smb_fname->st,
1077 lp_fake_directory_create_times(SNUM(handle->conn)));
1079 END_PROFILE(syscall_stat);
1083 static int vfswrap_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1087 START_PROFILE(syscall_fstat);
1088 result = sys_fstat(fsp->fh->fd,
1089 sbuf, lp_fake_directory_create_times(SNUM(handle->conn)));
1090 END_PROFILE(syscall_fstat);
1094 static int vfswrap_lstat(vfs_handle_struct *handle,
1095 struct smb_filename *smb_fname)
1099 START_PROFILE(syscall_lstat);
1101 if (smb_fname->stream_name) {
1106 result = sys_lstat(smb_fname->base_name, &smb_fname->st,
1107 lp_fake_directory_create_times(SNUM(handle->conn)));
1109 END_PROFILE(syscall_lstat);
1113 static NTSTATUS vfswrap_translate_name(struct vfs_handle_struct *handle,
1115 enum vfs_translate_direction direction,
1116 TALLOC_CTX *mem_ctx,
1119 return NT_STATUS_NONE_MAPPED;
1123 * Implement the default fsctl operation.
1125 static bool vfswrap_logged_ioctl_message = false;
1127 static NTSTATUS vfswrap_fsctl(struct vfs_handle_struct *handle,
1128 struct files_struct *fsp,
1131 uint16_t req_flags, /* Needed for UNICODE ... */
1132 const uint8_t *_in_data,
1134 uint8_t **_out_data,
1135 uint32_t max_out_len,
1138 const char *in_data = (const char *)_in_data;
1139 char **out_data = (char **)_out_data;
1143 case FSCTL_SET_SPARSE:
1145 bool set_sparse = true;
1147 if (in_len >= 1 && in_data[0] == 0) {
1151 status = file_set_sparse(handle->conn, fsp, set_sparse);
1153 DEBUG(NT_STATUS_IS_OK(status) ? 10 : 9,
1154 ("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
1155 smb_fname_str_dbg(fsp->fsp_name), set_sparse,
1156 nt_errstr(status)));
1161 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1163 unsigned char objid[16];
1164 char *return_data = NULL;
1166 /* This should return the object-id on this file.
1167 * I think I'll make this be the inode+dev. JRA.
1170 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on %s\n",
1171 fsp_fnum_dbg(fsp)));
1173 *out_len = MIN(max_out_len, 64);
1175 /* Hmmm, will this cause problems if less data asked for? */
1176 return_data = talloc_array(ctx, char, 64);
1177 if (return_data == NULL) {
1178 return NT_STATUS_NO_MEMORY;
1181 /* For backwards compatibility only store the dev/inode. */
1182 push_file_id_16(return_data, &fsp->file_id);
1183 memcpy(return_data+16,create_volume_objectid(fsp->conn,objid),16);
1184 push_file_id_16(return_data+32, &fsp->file_id);
1185 memset(return_data+48, 0, 16);
1186 *out_data = return_data;
1187 return NT_STATUS_OK;
1190 case FSCTL_GET_REPARSE_POINT:
1192 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
1193 DEBUG(10, ("FSCTL_GET_REPARSE_POINT: called on %s. "
1194 "Status: NOT_IMPLEMENTED\n", fsp_fnum_dbg(fsp)));
1195 return NT_STATUS_NOT_A_REPARSE_POINT;
1198 case FSCTL_SET_REPARSE_POINT:
1200 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
1201 DEBUG(10, ("FSCTL_SET_REPARSE_POINT: called on %s. "
1202 "Status: NOT_IMPLEMENTED\n", fsp_fnum_dbg(fsp)));
1203 return NT_STATUS_NOT_A_REPARSE_POINT;
1206 case FSCTL_GET_SHADOW_COPY_DATA:
1209 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1210 * and return their volume names. If max_data_count is 16, then it is just
1211 * asking for the number of volumes and length of the combined names.
1213 * pdata is the data allocated by our caller, but that uses
1214 * total_data_count (which is 0 in our case) rather than max_data_count.
1215 * Allocate the correct amount and return the pointer to let
1216 * it be deallocated when we return.
1218 struct shadow_copy_data *shadow_data = NULL;
1219 bool labels = False;
1220 uint32_t labels_data_count = 0;
1222 char *cur_pdata = NULL;
1224 if (max_out_len < 16) {
1225 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1227 return NT_STATUS_INVALID_PARAMETER;
1230 if (max_out_len > 16) {
1234 shadow_data = talloc_zero(ctx, struct shadow_copy_data);
1235 if (shadow_data == NULL) {
1236 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1237 return NT_STATUS_NO_MEMORY;
1241 * Call the VFS routine to actually do the work.
1243 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1246 /* broken module didn't set errno on error */
1247 status = NT_STATUS_UNSUCCESSFUL;
1249 status = map_nt_error_from_unix(errno);
1250 if (NT_STATUS_EQUAL(status,
1251 NT_STATUS_NOT_SUPPORTED)) {
1255 DEBUG(log_lev, ("FSCTL_GET_SHADOW_COPY_DATA: "
1256 "connectpath %s, failed - %s.\n",
1257 fsp->conn->connectpath,
1258 nt_errstr(status)));
1259 TALLOC_FREE(shadow_data);
1263 labels_data_count = (shadow_data->num_volumes * 2 *
1264 sizeof(SHADOW_COPY_LABEL)) + 2;
1269 *out_len = 12 + labels_data_count;
1272 if (max_out_len < *out_len) {
1273 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1274 max_out_len, *out_len));
1275 TALLOC_FREE(shadow_data);
1276 return NT_STATUS_BUFFER_TOO_SMALL;
1279 cur_pdata = talloc_zero_array(ctx, char, *out_len);
1280 if (cur_pdata == NULL) {
1281 TALLOC_FREE(shadow_data);
1282 return NT_STATUS_NO_MEMORY;
1285 *out_data = cur_pdata;
1287 /* num_volumes 4 bytes */
1288 SIVAL(cur_pdata, 0, shadow_data->num_volumes);
1291 /* num_labels 4 bytes */
1292 SIVAL(cur_pdata, 4, shadow_data->num_volumes);
1295 /* needed_data_count 4 bytes */
1296 SIVAL(cur_pdata, 8, labels_data_count);
1300 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1301 shadow_data->num_volumes, fsp_str_dbg(fsp)));
1302 if (labels && shadow_data->labels) {
1303 for (i=0; i<shadow_data->num_volumes; i++) {
1305 status = srvstr_push(cur_pdata, req_flags,
1306 cur_pdata, shadow_data->labels[i],
1307 2 * sizeof(SHADOW_COPY_LABEL),
1308 STR_UNICODE|STR_TERMINATE, &len);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 TALLOC_FREE(*out_data);
1311 TALLOC_FREE(shadow_data);
1314 cur_pdata += 2 * sizeof(SHADOW_COPY_LABEL);
1315 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1319 TALLOC_FREE(shadow_data);
1321 return NT_STATUS_OK;
1324 case FSCTL_FIND_FILES_BY_SID:
1326 /* pretend this succeeded -
1328 * we have to send back a list with all files owned by this SID
1330 * but I have to check that --metze
1333 struct dom_sid_buf buf;
1337 DEBUG(10, ("FSCTL_FIND_FILES_BY_SID: called on %s\n",
1338 fsp_fnum_dbg(fsp)));
1341 /* NT_STATUS_BUFFER_TOO_SMALL maybe? */
1342 return NT_STATUS_INVALID_PARAMETER;
1345 sid_len = MIN(in_len - 4,SID_MAX_SIZE);
1347 /* unknown 4 bytes: this is not the length of the sid :-( */
1348 /*unknown = IVAL(pdata,0);*/
1350 if (!sid_parse(_in_data + 4, sid_len, &sid)) {
1351 return NT_STATUS_INVALID_PARAMETER;
1353 DEBUGADD(10, ("for SID: %s\n",
1354 dom_sid_str_buf(&sid, &buf)));
1356 if (!sid_to_uid(&sid, &uid)) {
1357 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1358 dom_sid_str_buf(&sid, &buf),
1359 (unsigned long)sid_len));
1363 /* we can take a look at the find source :-)
1365 * find ./ -uid $uid -name '*' is what we need here
1368 * and send 4bytes len and then NULL terminated unicode strings
1371 * but I don't know how to deal with the paged results
1372 * (maybe we can hang the result anywhere in the fsp struct)
1374 * but I don't know how to deal with the paged results
1375 * (maybe we can hang the result anywhere in the fsp struct)
1377 * we don't send all files at once
1378 * and at the next we should *not* start from the beginning,
1379 * so we have to cache the result
1384 /* this works for now... */
1385 return NT_STATUS_OK;
1388 case FSCTL_QUERY_ALLOCATED_RANGES:
1390 /* FIXME: This is just a dummy reply, telling that all of the
1391 * file is allocated. MKS cp needs that.
1392 * Adding the real allocated ranges via FIEMAP on Linux
1393 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
1394 * this FSCTL correct for sparse files.
1396 uint64_t offset, length;
1397 char *out_data_tmp = NULL;
1400 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
1402 return NT_STATUS_INVALID_PARAMETER;
1405 if (max_out_len < 16) {
1406 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_out_len (%u) < 16 is invalid!\n",
1408 return NT_STATUS_INVALID_PARAMETER;
1411 offset = BVAL(in_data,0);
1412 length = BVAL(in_data,8);
1414 if (offset + length < offset) {
1415 /* No 64-bit integer wrap. */
1416 return NT_STATUS_INVALID_PARAMETER;
1419 /* Shouldn't this be SMB_VFS_STAT ... ? */
1420 status = vfs_stat_fsp(fsp);
1421 if (!NT_STATUS_IS_OK(status)) {
1426 out_data_tmp = talloc_array(ctx, char, *out_len);
1427 if (out_data_tmp == NULL) {
1428 DEBUG(10, ("unable to allocate memory for response\n"));
1429 return NT_STATUS_NO_MEMORY;
1432 if (offset > fsp->fsp_name->st.st_ex_size ||
1433 fsp->fsp_name->st.st_ex_size == 0 ||
1435 memset(out_data_tmp, 0, *out_len);
1437 uint64_t end = offset + length;
1438 end = MIN(end, fsp->fsp_name->st.st_ex_size);
1439 SBVAL(out_data_tmp, 0, 0);
1440 SBVAL(out_data_tmp, 8, end);
1443 *out_data = out_data_tmp;
1445 return NT_STATUS_OK;
1448 case FSCTL_IS_VOLUME_DIRTY:
1450 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on %s "
1451 "(but remotely not supported)\n", fsp_fnum_dbg(fsp)));
1453 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
1454 * says we have to respond with NT_STATUS_INVALID_PARAMETER
1456 return NT_STATUS_INVALID_PARAMETER;
1461 * Only print once ... unfortunately there could be lots of
1462 * different FSCTLs that are called.
1464 if (!vfswrap_logged_ioctl_message) {
1465 vfswrap_logged_ioctl_message = true;
1466 DEBUG(2, ("%s (0x%x): Currently not implemented.\n",
1467 __func__, function));
1471 return NT_STATUS_NOT_SUPPORTED;
1474 static bool vfswrap_is_offline(struct vfs_handle_struct *handle,
1475 const struct smb_filename *fname,
1476 SMB_STRUCT_STAT *sbuf);
1478 static NTSTATUS vfswrap_get_dos_attributes(struct vfs_handle_struct *handle,
1479 struct smb_filename *smb_fname,
1484 offline = vfswrap_is_offline(handle, smb_fname, &smb_fname->st);
1486 *dosmode |= FILE_ATTRIBUTE_OFFLINE;
1489 return get_ea_dos_attribute(handle->conn, smb_fname, dosmode);
1492 struct vfswrap_get_dos_attributes_state {
1493 struct vfs_aio_state aio_state;
1494 connection_struct *conn;
1495 TALLOC_CTX *mem_ctx;
1496 const struct smb_vfs_ev_glue *evg;
1497 files_struct *dir_fsp;
1498 struct smb_filename *smb_fname;
1503 static void vfswrap_get_dos_attributes_getxattr_done(struct tevent_req *subreq);
1505 static struct tevent_req *vfswrap_get_dos_attributes_send(
1506 TALLOC_CTX *mem_ctx,
1507 const struct smb_vfs_ev_glue *evg,
1508 struct vfs_handle_struct *handle,
1509 files_struct *dir_fsp,
1510 struct smb_filename *smb_fname)
1512 struct tevent_context *ev = smb_vfs_ev_glue_ev_ctx(evg);
1513 struct tevent_req *req = NULL;
1514 struct tevent_req *subreq = NULL;
1515 struct vfswrap_get_dos_attributes_state *state = NULL;
1517 req = tevent_req_create(mem_ctx, &state,
1518 struct vfswrap_get_dos_attributes_state);
1523 *state = (struct vfswrap_get_dos_attributes_state) {
1524 .conn = dir_fsp->conn,
1528 .smb_fname = smb_fname,
1531 subreq = SMB_VFS_GETXATTRAT_SEND(state,
1535 SAMBA_XATTR_DOS_ATTRIB,
1537 if (tevent_req_nomem(subreq, req)) {
1538 return tevent_req_post(req, ev);
1540 tevent_req_set_callback(subreq,
1541 vfswrap_get_dos_attributes_getxattr_done,
1547 static void vfswrap_get_dos_attributes_getxattr_done(struct tevent_req *subreq)
1549 struct tevent_req *req =
1550 tevent_req_callback_data(subreq,
1552 struct vfswrap_get_dos_attributes_state *state =
1553 tevent_req_data(req,
1554 struct vfswrap_get_dos_attributes_state);
1556 DATA_BLOB blob = {0};
1559 xattr_size = SMB_VFS_GETXATTRAT_RECV(subreq,
1563 TALLOC_FREE(subreq);
1564 if (xattr_size == -1) {
1565 const struct smb_vfs_ev_glue *root_evg = NULL;
1567 status = map_nt_error_from_unix(state->aio_state.error);
1569 if (state->as_root) {
1570 tevent_req_nterror(req, status);
1573 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1574 tevent_req_nterror(req, status);
1578 state->as_root = true;
1579 root_evg = smb_vfs_ev_glue_get_root_glue(state->evg);
1581 subreq = SMB_VFS_GETXATTRAT_SEND(state,
1585 SAMBA_XATTR_DOS_ATTRIB,
1587 if (tevent_req_nomem(subreq, req)) {
1590 tevent_req_set_callback(subreq,
1591 vfswrap_get_dos_attributes_getxattr_done,
1596 blob.length = xattr_size;
1598 status = parse_dos_attribute_blob(state->smb_fname,
1601 if (!NT_STATUS_IS_OK(status)) {
1602 tevent_req_nterror(req, status);
1606 tevent_req_done(req);
1610 static NTSTATUS vfswrap_get_dos_attributes_recv(struct tevent_req *req,
1611 struct vfs_aio_state *aio_state,
1614 struct vfswrap_get_dos_attributes_state *state =
1615 tevent_req_data(req,
1616 struct vfswrap_get_dos_attributes_state);
1619 if (tevent_req_is_nterror(req, &status)) {
1620 tevent_req_received(req);
1624 *aio_state = state->aio_state;
1625 *dosmode = state->dosmode;
1626 tevent_req_received(req);
1627 return NT_STATUS_OK;
1630 static NTSTATUS vfswrap_fget_dos_attributes(struct vfs_handle_struct *handle,
1631 struct files_struct *fsp,
1636 offline = vfswrap_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1638 *dosmode |= FILE_ATTRIBUTE_OFFLINE;
1641 return get_ea_dos_attribute(handle->conn, fsp->fsp_name, dosmode);
1644 static NTSTATUS vfswrap_set_dos_attributes(struct vfs_handle_struct *handle,
1645 const struct smb_filename *smb_fname,
1648 return set_ea_dos_attribute(handle->conn, smb_fname, dosmode);
1651 static NTSTATUS vfswrap_fset_dos_attributes(struct vfs_handle_struct *handle,
1652 struct files_struct *fsp,
1655 return set_ea_dos_attribute(handle->conn, fsp->fsp_name, dosmode);
1658 static struct vfs_offload_ctx *vfswrap_offload_ctx;
1660 struct vfswrap_offload_read_state {
1664 static struct tevent_req *vfswrap_offload_read_send(
1665 TALLOC_CTX *mem_ctx,
1666 struct tevent_context *ev,
1667 struct vfs_handle_struct *handle,
1668 struct files_struct *fsp,
1674 struct tevent_req *req = NULL;
1675 struct vfswrap_offload_read_state *state = NULL;
1678 req = tevent_req_create(mem_ctx, &state,
1679 struct vfswrap_offload_read_state);
1684 status = vfs_offload_token_ctx_init(fsp->conn->sconn->client,
1685 &vfswrap_offload_ctx);
1686 if (tevent_req_nterror(req, status)) {
1687 return tevent_req_post(req, ev);
1690 if (fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
1691 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
1692 return tevent_req_post(req, ev);
1695 status = vfs_offload_token_create_blob(state, fsp, fsctl,
1697 if (tevent_req_nterror(req, status)) {
1698 return tevent_req_post(req, ev);
1701 status = vfs_offload_token_db_store_fsp(vfswrap_offload_ctx, fsp,
1703 if (tevent_req_nterror(req, status)) {
1704 return tevent_req_post(req, ev);
1707 tevent_req_done(req);
1708 return tevent_req_post(req, ev);
1711 static NTSTATUS vfswrap_offload_read_recv(struct tevent_req *req,
1712 struct vfs_handle_struct *handle,
1713 TALLOC_CTX *mem_ctx,
1716 struct vfswrap_offload_read_state *state = tevent_req_data(
1717 req, struct vfswrap_offload_read_state);
1720 if (tevent_req_is_nterror(req, &status)) {
1721 tevent_req_received(req);
1725 token->length = state->token.length;
1726 token->data = talloc_move(mem_ctx, &state->token.data);
1728 tevent_req_received(req);
1729 return NT_STATUS_OK;
1732 struct vfswrap_offload_write_state {
1734 bool read_lck_locked;
1735 bool write_lck_locked;
1737 struct tevent_context *src_ev;
1738 struct files_struct *src_fsp;
1740 struct tevent_context *dst_ev;
1741 struct files_struct *dst_fsp;
1745 size_t next_io_size;
1748 static void vfswrap_offload_write_cleanup(struct tevent_req *req,
1749 enum tevent_req_state req_state)
1751 struct vfswrap_offload_write_state *state = tevent_req_data(
1752 req, struct vfswrap_offload_write_state);
1755 if (state->dst_fsp == NULL) {
1759 ok = change_to_user_by_fsp(state->dst_fsp);
1761 state->dst_fsp = NULL;
1764 static NTSTATUS vfswrap_offload_write_loop(struct tevent_req *req);
1766 static struct tevent_req *vfswrap_offload_write_send(
1767 struct vfs_handle_struct *handle,
1768 TALLOC_CTX *mem_ctx,
1769 struct tevent_context *ev,
1772 off_t transfer_offset,
1773 struct files_struct *dest_fsp,
1777 struct tevent_req *req;
1778 struct vfswrap_offload_write_state *state = NULL;
1779 size_t num = MIN(to_copy, COPYCHUNK_MAX_TOTAL_LEN);
1780 files_struct *src_fsp = NULL;
1784 req = tevent_req_create(mem_ctx, &state,
1785 struct vfswrap_offload_write_state);
1790 *state = (struct vfswrap_offload_write_state) {
1792 .src_off = transfer_offset,
1794 .dst_fsp = dest_fsp,
1795 .dst_off = dest_off,
1797 .remaining = to_copy,
1800 tevent_req_set_cleanup_fn(req, vfswrap_offload_write_cleanup);
1803 case FSCTL_SRV_COPYCHUNK:
1804 case FSCTL_SRV_COPYCHUNK_WRITE:
1807 case FSCTL_OFFLOAD_WRITE:
1808 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1809 return tevent_req_post(req, ev);
1811 case FSCTL_DUP_EXTENTS_TO_FILE:
1812 DBG_DEBUG("COW clones not supported by vfs_default\n");
1813 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1814 return tevent_req_post(req, ev);
1817 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1818 return tevent_req_post(req, ev);
1822 * From here on we assume a copy-chunk fsctl
1826 tevent_req_done(req);
1827 return tevent_req_post(req, ev);
1830 status = vfs_offload_token_db_fetch_fsp(vfswrap_offload_ctx,
1832 if (tevent_req_nterror(req, status)) {
1833 return tevent_req_post(req, ev);
1836 DBG_DEBUG("server side copy chunk of length %" PRIu64 "\n", to_copy);
1838 status = vfs_offload_token_check_handles(fsctl, src_fsp, dest_fsp);
1839 if (!NT_STATUS_IS_OK(status)) {
1840 tevent_req_nterror(req, status);
1841 return tevent_req_post(req, ev);
1844 ok = change_to_user_by_fsp(src_fsp);
1846 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
1847 return tevent_req_post(req, ev);
1850 state->src_ev = src_fsp->conn->user_ev_ctx;
1851 state->src_fsp = src_fsp;
1853 state->buf = talloc_array(state, uint8_t, num);
1854 if (tevent_req_nomem(state->buf, req)) {
1855 return tevent_req_post(req, ev);
1858 status = vfs_stat_fsp(src_fsp);
1859 if (tevent_req_nterror(req, status)) {
1860 return tevent_req_post(req, ev);
1863 if (src_fsp->fsp_name->st.st_ex_size < state->src_off + num) {
1865 * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
1866 * If the SourceOffset or SourceOffset + Length extends beyond
1867 * the end of file, the server SHOULD<240> treat this as a
1868 * STATUS_END_OF_FILE error.
1870 * <240> Section 3.3.5.15.6: Windows servers will return
1871 * STATUS_INVALID_VIEW_SIZE instead of STATUS_END_OF_FILE.
1873 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
1874 return tevent_req_post(req, ev);
1877 status = vfswrap_offload_write_loop(req);
1878 if (!NT_STATUS_IS_OK(status)) {
1879 tevent_req_nterror(req, status);
1880 return tevent_req_post(req, ev);
1886 static void vfswrap_offload_write_read_done(struct tevent_req *subreq);
1888 static NTSTATUS vfswrap_offload_write_loop(struct tevent_req *req)
1890 struct vfswrap_offload_write_state *state = tevent_req_data(
1891 req, struct vfswrap_offload_write_state);
1892 struct tevent_req *subreq = NULL;
1893 struct lock_struct read_lck;
1897 * This is called under the context of state->src_fsp.
1900 state->next_io_size = MIN(state->remaining, talloc_array_length(state->buf));
1902 init_strict_lock_struct(state->src_fsp,
1903 state->src_fsp->op->global->open_persistent_id,
1905 state->next_io_size,
1909 ok = SMB_VFS_STRICT_LOCK_CHECK(state->src_fsp->conn,
1913 return NT_STATUS_FILE_LOCK_CONFLICT;
1916 subreq = SMB_VFS_PREAD_SEND(state,
1920 state->next_io_size,
1922 if (subreq == NULL) {
1923 return NT_STATUS_NO_MEMORY;
1925 tevent_req_set_callback(subreq, vfswrap_offload_write_read_done, req);
1927 return NT_STATUS_OK;
1930 static void vfswrap_offload_write_write_done(struct tevent_req *subreq);
1932 static void vfswrap_offload_write_read_done(struct tevent_req *subreq)
1934 struct tevent_req *req = tevent_req_callback_data(
1935 subreq, struct tevent_req);
1936 struct vfswrap_offload_write_state *state = tevent_req_data(
1937 req, struct vfswrap_offload_write_state);
1938 struct vfs_aio_state aio_state;
1939 struct lock_struct write_lck;
1943 nread = SMB_VFS_PREAD_RECV(subreq, &aio_state);
1944 TALLOC_FREE(subreq);
1946 DBG_ERR("read failed: %s\n", strerror(errno));
1947 tevent_req_nterror(req, map_nt_error_from_unix(aio_state.error));
1950 if (nread != state->next_io_size) {
1951 DBG_ERR("Short read, only %zd of %zu\n",
1952 nread, state->next_io_size);
1953 tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
1957 state->src_off += nread;
1959 ok = change_to_user_by_fsp(state->dst_fsp);
1961 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1965 init_strict_lock_struct(state->dst_fsp,
1966 state->dst_fsp->op->global->open_persistent_id,
1968 state->next_io_size,
1972 ok = SMB_VFS_STRICT_LOCK_CHECK(state->dst_fsp->conn,
1976 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
1980 subreq = SMB_VFS_PWRITE_SEND(state,
1984 state->next_io_size,
1986 if (subreq == NULL) {
1987 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1990 tevent_req_set_callback(subreq, vfswrap_offload_write_write_done, req);
1993 static void vfswrap_offload_write_write_done(struct tevent_req *subreq)
1995 struct tevent_req *req = tevent_req_callback_data(
1996 subreq, struct tevent_req);
1997 struct vfswrap_offload_write_state *state = tevent_req_data(
1998 req, struct vfswrap_offload_write_state);
1999 struct vfs_aio_state aio_state;
2004 nwritten = SMB_VFS_PWRITE_RECV(subreq, &aio_state);
2005 TALLOC_FREE(subreq);
2006 if (nwritten == -1) {
2007 DBG_ERR("write failed: %s\n", strerror(errno));
2008 tevent_req_nterror(req, map_nt_error_from_unix(aio_state.error));
2011 if (nwritten != state->next_io_size) {
2012 DBG_ERR("Short write, only %zd of %zu\n", nwritten, state->next_io_size);
2013 tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
2017 state->dst_off += nwritten;
2019 if (state->remaining < nwritten) {
2020 /* Paranoia check */
2021 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
2024 state->remaining -= nwritten;
2025 if (state->remaining == 0) {
2026 tevent_req_done(req);
2030 ok = change_to_user_by_fsp(state->src_fsp);
2032 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
2036 status = vfswrap_offload_write_loop(req);
2037 if (!NT_STATUS_IS_OK(status)) {
2038 tevent_req_nterror(req, status);
2045 static NTSTATUS vfswrap_offload_write_recv(struct vfs_handle_struct *handle,
2046 struct tevent_req *req,
2049 struct vfswrap_offload_write_state *state = tevent_req_data(
2050 req, struct vfswrap_offload_write_state);
2053 if (tevent_req_is_nterror(req, &status)) {
2054 DBG_DEBUG("copy chunk failed: %s\n", nt_errstr(status));
2056 tevent_req_received(req);
2060 *copied = state->to_copy;
2061 DBG_DEBUG("copy chunk copied %lu\n", (unsigned long)*copied);
2062 tevent_req_received(req);
2064 return NT_STATUS_OK;
2067 static NTSTATUS vfswrap_get_compression(struct vfs_handle_struct *handle,
2068 TALLOC_CTX *mem_ctx,
2069 struct files_struct *fsp,
2070 struct smb_filename *smb_fname,
2071 uint16_t *_compression_fmt)
2073 return NT_STATUS_INVALID_DEVICE_REQUEST;
2076 static NTSTATUS vfswrap_set_compression(struct vfs_handle_struct *handle,
2077 TALLOC_CTX *mem_ctx,
2078 struct files_struct *fsp,
2079 uint16_t compression_fmt)
2081 return NT_STATUS_INVALID_DEVICE_REQUEST;
2084 /********************************************************************
2085 Given a stat buffer return the allocated size on disk, taking into
2086 account sparse files.
2087 ********************************************************************/
2088 static uint64_t vfswrap_get_alloc_size(vfs_handle_struct *handle,
2089 struct files_struct *fsp,
2090 const SMB_STRUCT_STAT *sbuf)
2094 START_PROFILE(syscall_get_alloc_size);
2096 if(S_ISDIR(sbuf->st_ex_mode)) {
2101 #if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
2102 /* The type of st_blocksize is blkcnt_t which *MUST* be
2103 signed (according to POSIX) and can be less than 64-bits.
2104 Ensure when we're converting to 64 bits wide we don't
2106 #if defined(SIZEOF_BLKCNT_T_8)
2107 result = (uint64_t)STAT_ST_BLOCKSIZE * (uint64_t)sbuf->st_ex_blocks;
2108 #elif defined(SIZEOF_BLKCNT_T_4)
2110 uint64_t bs = ((uint64_t)sbuf->st_ex_blocks) & 0xFFFFFFFFLL;
2111 result = (uint64_t)STAT_ST_BLOCKSIZE * bs;
2114 #error SIZEOF_BLKCNT_T_NOT_A_SUPPORTED_VALUE
2118 * Some file systems do not allocate a block for very
2119 * small files. But for non-empty file should report a
2123 uint64_t filesize = get_file_size_stat(sbuf);
2125 result = MIN((uint64_t)STAT_ST_BLOCKSIZE, filesize);
2129 result = get_file_size_stat(sbuf);
2132 if (fsp && fsp->initial_allocation_size)
2133 result = MAX(result,fsp->initial_allocation_size);
2135 result = smb_roundup(handle->conn, result);
2138 END_PROFILE(syscall_get_alloc_size);
2142 static int vfswrap_unlink(vfs_handle_struct *handle,
2143 const struct smb_filename *smb_fname)
2147 START_PROFILE(syscall_unlink);
2149 if (smb_fname->stream_name) {
2153 result = unlink(smb_fname->base_name);
2156 END_PROFILE(syscall_unlink);
2160 static int vfswrap_chmod(vfs_handle_struct *handle,
2161 const struct smb_filename *smb_fname,
2166 START_PROFILE(syscall_chmod);
2167 result = chmod(smb_fname->base_name, mode);
2168 END_PROFILE(syscall_chmod);
2172 static int vfswrap_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
2176 START_PROFILE(syscall_fchmod);
2177 #if defined(HAVE_FCHMOD)
2178 result = fchmod(fsp->fh->fd, mode);
2184 END_PROFILE(syscall_fchmod);
2188 static int vfswrap_chown(vfs_handle_struct *handle,
2189 const struct smb_filename *smb_fname,
2195 START_PROFILE(syscall_chown);
2196 result = chown(smb_fname->base_name, uid, gid);
2197 END_PROFILE(syscall_chown);
2201 static int vfswrap_fchown(vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
2206 START_PROFILE(syscall_fchown);
2207 result = fchown(fsp->fh->fd, uid, gid);
2208 END_PROFILE(syscall_fchown);
2216 static int vfswrap_lchown(vfs_handle_struct *handle,
2217 const struct smb_filename *smb_fname,
2223 START_PROFILE(syscall_lchown);
2224 result = lchown(smb_fname->base_name, uid, gid);
2225 END_PROFILE(syscall_lchown);
2229 static int vfswrap_chdir(vfs_handle_struct *handle,
2230 const struct smb_filename *smb_fname)
2234 START_PROFILE(syscall_chdir);
2235 result = chdir(smb_fname->base_name);
2236 END_PROFILE(syscall_chdir);
2240 static struct smb_filename *vfswrap_getwd(vfs_handle_struct *handle,
2244 struct smb_filename *smb_fname = NULL;
2246 START_PROFILE(syscall_getwd);
2247 result = sys_getwd();
2248 END_PROFILE(syscall_getwd);
2250 if (result == NULL) {
2253 smb_fname = synthetic_smb_fname(ctx,
2259 * sys_getwd() *always* returns malloced memory.
2260 * We must free here to avoid leaks:
2261 * BUG:https://bugzilla.samba.org/show_bug.cgi?id=13372
2267 /*********************************************************************
2268 nsec timestamp resolution call. Convert down to whatever the underlying
2269 system will support.
2270 **********************************************************************/
2272 static int vfswrap_ntimes(vfs_handle_struct *handle,
2273 const struct smb_filename *smb_fname,
2274 struct smb_file_time *ft)
2278 START_PROFILE(syscall_ntimes);
2280 if (smb_fname->stream_name) {
2286 if (null_timespec(ft->atime)) {
2287 ft->atime= smb_fname->st.st_ex_atime;
2290 if (null_timespec(ft->mtime)) {
2291 ft->mtime = smb_fname->st.st_ex_mtime;
2294 if (!null_timespec(ft->create_time)) {
2295 set_create_timespec_ea(handle->conn,
2300 if ((timespec_compare(&ft->atime,
2301 &smb_fname->st.st_ex_atime) == 0) &&
2302 (timespec_compare(&ft->mtime,
2303 &smb_fname->st.st_ex_mtime) == 0)) {
2308 #if defined(HAVE_UTIMENSAT)
2310 struct timespec ts[2];
2313 result = utimensat(AT_FDCWD, smb_fname->base_name, ts, 0);
2315 result = utimensat(AT_FDCWD, smb_fname->base_name, NULL, 0);
2317 if (!((result == -1) && (errno == ENOSYS))) {
2321 #if defined(HAVE_UTIMES)
2323 struct timeval tv[2];
2324 tv[0] = convert_timespec_to_timeval(ft->atime);
2325 tv[1] = convert_timespec_to_timeval(ft->mtime);
2326 result = utimes(smb_fname->base_name, tv);
2328 result = utimes(smb_fname->base_name, NULL);
2330 if (!((result == -1) && (errno == ENOSYS))) {
2334 #if defined(HAVE_UTIME)
2336 struct utimbuf times;
2337 times.actime = convert_timespec_to_time_t(ft->atime);
2338 times.modtime = convert_timespec_to_time_t(ft->mtime);
2339 result = utime(smb_fname->base_name, ×);
2341 result = utime(smb_fname->base_name, NULL);
2343 if (!((result == -1) && (errno == ENOSYS))) {
2351 END_PROFILE(syscall_ntimes);
2355 /*********************************************************************
2356 A version of ftruncate that will write the space on disk if strict
2358 **********************************************************************/
2360 static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fsp, off_t len)
2362 off_t space_to_write;
2363 uint64_t space_avail;
2364 uint64_t bsize,dfree,dsize;
2367 SMB_STRUCT_STAT *pst;
2369 status = vfs_stat_fsp(fsp);
2370 if (!NT_STATUS_IS_OK(status)) {
2373 pst = &fsp->fsp_name->st;
2376 if (S_ISFIFO(pst->st_ex_mode))
2380 if (pst->st_ex_size == len)
2383 /* Shrink - just ftruncate. */
2384 if (pst->st_ex_size > len)
2385 return ftruncate(fsp->fh->fd, len);
2387 space_to_write = len - pst->st_ex_size;
2389 /* for allocation try fallocate first. This can fail on some
2390 platforms e.g. when the filesystem doesn't support it and no
2391 emulation is being done by the libc (like on AIX with JFS1). In that
2392 case we do our own emulation. fallocate implementations can
2393 return ENOTSUP or EINVAL in cases like that. */
2394 ret = SMB_VFS_FALLOCATE(fsp, 0, pst->st_ex_size, space_to_write);
2395 if (ret == -1 && errno == ENOSPC) {
2401 DEBUG(10,("strict_allocate_ftruncate: SMB_VFS_FALLOCATE failed with "
2402 "error %d. Falling back to slow manual allocation\n", errno));
2404 /* available disk space is enough or not? */
2406 get_dfree_info(fsp->conn, fsp->fsp_name, &bsize, &dfree, &dsize);
2407 /* space_avail is 1k blocks */
2408 if (space_avail == (uint64_t)-1 ||
2409 ((uint64_t)space_to_write/1024 > space_avail) ) {
2414 /* Write out the real space on disk. */
2415 ret = vfs_slow_fallocate(fsp, pst->st_ex_size, space_to_write);
2423 static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, off_t len)
2426 SMB_STRUCT_STAT *pst;
2430 START_PROFILE(syscall_ftruncate);
2432 if (lp_strict_allocate(SNUM(fsp->conn)) && !fsp->is_sparse) {
2433 result = strict_allocate_ftruncate(handle, fsp, len);
2434 END_PROFILE(syscall_ftruncate);
2438 /* we used to just check HAVE_FTRUNCATE_EXTEND and only use
2439 ftruncate if the system supports it. Then I discovered that
2440 you can have some filesystems that support ftruncate
2441 expansion and some that don't! On Linux fat can't do
2442 ftruncate extend but ext2 can. */
2444 result = ftruncate(fsp->fh->fd, len);
2446 /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
2447 extend a file with ftruncate. Provide alternate implementation
2450 /* Do an fstat to see if the file is longer than the requested
2451 size in which case the ftruncate above should have
2452 succeeded or shorter, in which case seek to len - 1 and
2453 write 1 byte of zero */
2454 status = vfs_stat_fsp(fsp);
2455 if (!NT_STATUS_IS_OK(status)) {
2459 /* We need to update the files_struct after successful ftruncate */
2464 pst = &fsp->fsp_name->st;
2467 if (S_ISFIFO(pst->st_ex_mode)) {
2473 if (pst->st_ex_size == len) {
2478 if (pst->st_ex_size > len) {
2479 /* the ftruncate should have worked */
2483 if (SMB_VFS_PWRITE(fsp, &c, 1, len-1)!=1) {
2491 END_PROFILE(syscall_ftruncate);
2495 static int vfswrap_fallocate(vfs_handle_struct *handle,
2503 START_PROFILE(syscall_fallocate);
2505 result = sys_posix_fallocate(fsp->fh->fd, offset, len);
2507 * posix_fallocate returns 0 on success, errno on error
2508 * and doesn't set errno. Make it behave like fallocate()
2509 * which returns -1, and sets errno on failure.
2516 /* sys_fallocate handles filtering of unsupported mode flags */
2517 result = sys_fallocate(fsp->fh->fd, mode, offset, len);
2519 END_PROFILE(syscall_fallocate);
2523 static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
2527 START_PROFILE(syscall_fcntl_lock);
2529 if (fsp->use_ofd_locks || !lp_parm_bool(SNUM(fsp->conn),
2531 "force process locks",
2533 op = map_process_lock_to_ofd_lock(op, &fsp->use_ofd_locks);
2536 result = fcntl_lock(fsp->fh->fd, op, offset, count, type);
2537 END_PROFILE(syscall_fcntl_lock);
2541 static int vfswrap_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
2542 uint32_t share_mode, uint32_t access_mask)
2544 START_PROFILE(syscall_kernel_flock);
2545 kernel_flock(fsp->fh->fd, share_mode, access_mask);
2546 END_PROFILE(syscall_kernel_flock);
2550 static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
2555 START_PROFILE(syscall_fcntl_getlock);
2557 if (fsp->use_ofd_locks || !lp_parm_bool(SNUM(fsp->conn),
2559 "force process locks",
2561 op = map_process_lock_to_ofd_lock(op, &fsp->use_ofd_locks);
2564 result = fcntl_getlock(fsp->fh->fd, op, poffset, pcount, ptype, ppid);
2565 END_PROFILE(syscall_fcntl_getlock);
2569 static int vfswrap_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
2574 START_PROFILE(syscall_linux_setlease);
2576 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
2577 result = linux_setlease(fsp->fh->fd, leasetype);
2581 END_PROFILE(syscall_linux_setlease);
2585 static int vfswrap_symlink(vfs_handle_struct *handle,
2586 const char *link_target,
2587 const struct smb_filename *new_smb_fname)
2591 START_PROFILE(syscall_symlink);
2592 result = symlink(link_target, new_smb_fname->base_name);
2593 END_PROFILE(syscall_symlink);
2597 static int vfswrap_readlink(vfs_handle_struct *handle,
2598 const struct smb_filename *smb_fname,
2604 START_PROFILE(syscall_readlink);
2605 result = readlink(smb_fname->base_name, buf, bufsiz);
2606 END_PROFILE(syscall_readlink);
2610 static int vfswrap_link(vfs_handle_struct *handle,
2611 const struct smb_filename *old_smb_fname,
2612 const struct smb_filename *new_smb_fname)
2616 START_PROFILE(syscall_link);
2617 result = link(old_smb_fname->base_name, new_smb_fname->base_name);
2618 END_PROFILE(syscall_link);
2622 static int vfswrap_mknod(vfs_handle_struct *handle,
2623 const struct smb_filename *smb_fname,
2629 START_PROFILE(syscall_mknod);
2630 result = sys_mknod(smb_fname->base_name, mode, dev);
2631 END_PROFILE(syscall_mknod);
2635 static struct smb_filename *vfswrap_realpath(vfs_handle_struct *handle,
2637 const struct smb_filename *smb_fname)
2640 struct smb_filename *result_fname = NULL;
2642 START_PROFILE(syscall_realpath);
2643 result = sys_realpath(smb_fname->base_name);
2644 END_PROFILE(syscall_realpath);
2646 result_fname = synthetic_smb_fname(ctx, result, NULL, NULL, 0);
2649 return result_fname;
2652 static int vfswrap_chflags(vfs_handle_struct *handle,
2653 const struct smb_filename *smb_fname,
2657 return chflags(smb_fname->base_name, flags);
2664 static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle,
2665 const SMB_STRUCT_STAT *sbuf)
2669 /* the ZERO_STRUCT ensures padding doesn't break using the key as a
2673 key.devid = sbuf->st_ex_dev;
2674 key.inode = sbuf->st_ex_ino;
2675 /* key.extid is unused by default. */
2680 static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
2681 struct files_struct *fsp,
2682 const struct smb_filename *smb_fname,
2683 TALLOC_CTX *mem_ctx,
2684 unsigned int *pnum_streams,
2685 struct stream_struct **pstreams)
2687 SMB_STRUCT_STAT sbuf;
2688 struct stream_struct *tmp_streams = NULL;
2691 if ((fsp != NULL) && (fsp->is_directory)) {
2693 * No default streams on directories
2698 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
2699 ret = SMB_VFS_FSTAT(fsp, &sbuf);
2702 struct smb_filename smb_fname_cp;
2704 ZERO_STRUCT(smb_fname_cp);
2705 smb_fname_cp.base_name = discard_const_p(char,
2706 smb_fname->base_name);
2707 smb_fname_cp.flags = smb_fname->flags;
2709 if (smb_fname_cp.flags & SMB_FILENAME_POSIX_PATH) {
2710 ret = SMB_VFS_LSTAT(handle->conn, &smb_fname_cp);
2712 ret = SMB_VFS_STAT(handle->conn, &smb_fname_cp);
2714 sbuf = smb_fname_cp.st;
2718 return map_nt_error_from_unix(errno);
2721 if (S_ISDIR(sbuf.st_ex_mode)) {
2725 tmp_streams = talloc_realloc(mem_ctx, *pstreams, struct stream_struct,
2726 (*pnum_streams) + 1);
2727 if (tmp_streams == NULL) {
2728 return NT_STATUS_NO_MEMORY;
2730 tmp_streams[*pnum_streams].name = talloc_strdup(tmp_streams, "::$DATA");
2731 if (tmp_streams[*pnum_streams].name == NULL) {
2732 return NT_STATUS_NO_MEMORY;
2734 tmp_streams[*pnum_streams].size = sbuf.st_ex_size;
2735 tmp_streams[*pnum_streams].alloc_size = SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp, &sbuf);
2738 *pstreams = tmp_streams;
2740 return NT_STATUS_OK;
2743 static int vfswrap_get_real_filename(struct vfs_handle_struct *handle,
2746 TALLOC_CTX *mem_ctx,
2750 * Don't fall back to get_real_filename so callers can differentiate
2751 * between a full directory scan and an actual case-insensitive stat.
2757 static const char *vfswrap_connectpath(struct vfs_handle_struct *handle,
2758 const struct smb_filename *smb_fname)
2760 return handle->conn->connectpath;
2763 static NTSTATUS vfswrap_brl_lock_windows(struct vfs_handle_struct *handle,
2764 struct byte_range_lock *br_lck,
2765 struct lock_struct *plock,
2768 SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
2770 /* Note: blr is not used in the default implementation. */
2771 return brl_lock_windows_default(br_lck, plock, blocking_lock);
2774 static bool vfswrap_brl_unlock_windows(struct vfs_handle_struct *handle,
2775 struct messaging_context *msg_ctx,
2776 struct byte_range_lock *br_lck,
2777 const struct lock_struct *plock)
2779 SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
2781 return brl_unlock_windows_default(msg_ctx, br_lck, plock);
2784 static bool vfswrap_brl_cancel_windows(struct vfs_handle_struct *handle,
2785 struct byte_range_lock *br_lck,
2786 struct lock_struct *plock)
2788 SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
2790 /* Note: blr is not used in the default implementation. */
2791 return brl_lock_cancel_default(br_lck, plock);
2794 static bool vfswrap_strict_lock_check(struct vfs_handle_struct *handle,
2796 struct lock_struct *plock)
2798 SMB_ASSERT(plock->lock_type == READ_LOCK ||
2799 plock->lock_type == WRITE_LOCK);
2801 return strict_lock_check_default(fsp, plock);
2804 /* NT ACL operations. */
2806 static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
2808 uint32_t security_info,
2809 TALLOC_CTX *mem_ctx,
2810 struct security_descriptor **ppdesc)
2814 START_PROFILE(fget_nt_acl);
2815 result = posix_fget_nt_acl(fsp, security_info,
2817 END_PROFILE(fget_nt_acl);
2821 static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
2822 const struct smb_filename *smb_fname,
2823 uint32_t security_info,
2824 TALLOC_CTX *mem_ctx,
2825 struct security_descriptor **ppdesc)
2829 START_PROFILE(get_nt_acl);
2830 result = posix_get_nt_acl(handle->conn,
2835 END_PROFILE(get_nt_acl);
2839 static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
2843 START_PROFILE(fset_nt_acl);
2844 result = set_nt_acl(fsp, security_info_sent, psd);
2845 END_PROFILE(fset_nt_acl);
2849 static NTSTATUS vfswrap_audit_file(struct vfs_handle_struct *handle,
2850 struct smb_filename *file,
2851 struct security_acl *sacl,
2852 uint32_t access_requested,
2853 uint32_t access_denied)
2855 return NT_STATUS_OK; /* Nothing to do here ... */
2858 static SMB_ACL_T vfswrap_sys_acl_get_file(vfs_handle_struct *handle,
2859 const struct smb_filename *smb_fname,
2860 SMB_ACL_TYPE_T type,
2861 TALLOC_CTX *mem_ctx)
2863 return sys_acl_get_file(handle, smb_fname, type, mem_ctx);
2866 static SMB_ACL_T vfswrap_sys_acl_get_fd(vfs_handle_struct *handle,
2868 TALLOC_CTX *mem_ctx)
2870 return sys_acl_get_fd(handle, fsp, mem_ctx);
2873 static int vfswrap_sys_acl_set_file(vfs_handle_struct *handle,
2874 const struct smb_filename *smb_fname,
2875 SMB_ACL_TYPE_T acltype,
2878 return sys_acl_set_file(handle, smb_fname, acltype, theacl);
2881 static int vfswrap_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp, SMB_ACL_T theacl)
2883 return sys_acl_set_fd(handle, fsp, theacl);
2886 static int vfswrap_sys_acl_delete_def_file(vfs_handle_struct *handle,
2887 const struct smb_filename *smb_fname)
2889 return sys_acl_delete_def_file(handle, smb_fname);
2892 /****************************************************************
2893 Extended attribute operations.
2894 *****************************************************************/
2896 static ssize_t vfswrap_getxattr(struct vfs_handle_struct *handle,
2897 const struct smb_filename *smb_fname,
2902 return getxattr(smb_fname->base_name, name, value, size);
2905 struct vfswrap_getxattrat_state {
2908 size_t xattr_bufsize;
2909 const char *xattr_name;
2911 uint8_t *xattr_value;
2913 struct vfs_aio_state vfs_aio_state;
2914 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
2917 static int vfswrap_getxattrat_state_destructor(
2918 struct vfswrap_getxattrat_state *state)
2923 static void vfswrap_getxattrat_do(void *private_data);
2924 static void vfswrap_getxattrat_done(struct tevent_req *subreq);
2926 static struct tevent_req *vfswrap_getxattrat_send(
2927 TALLOC_CTX *mem_ctx,
2928 const struct smb_vfs_ev_glue *evg,
2929 struct vfs_handle_struct *handle,
2930 files_struct *dir_fsp,
2931 const struct smb_filename *smb_fname,
2932 const char *xattr_name,
2935 struct tevent_context *ev = smb_vfs_ev_glue_ev_ctx(evg);
2936 struct pthreadpool_tevent *tp = smb_vfs_ev_glue_tp_chdir_safe(evg);
2937 struct tevent_req *req = NULL;
2938 struct tevent_req *subreq = NULL;
2939 struct vfswrap_getxattrat_state *state = NULL;
2941 req = tevent_req_create(mem_ctx, &state,
2942 struct vfswrap_getxattrat_state);
2946 *state = (struct vfswrap_getxattrat_state) {
2947 .dirfd = dir_fsp->fh->fd,
2948 .xattr_bufsize = alloc_hint,
2951 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_getxattrat, profile_p,
2952 state->profile_bytes, 0);
2954 if (state->dirfd == -1) {
2955 DBG_ERR("Need a valid directory fd\n");
2956 tevent_req_error(req, EINVAL);
2957 return tevent_req_post(req, ev);
2961 * Now allocate all parameters from a memory context that won't go away
2962 * no matter what. These paremeters will get used in threads and we
2963 * can't reliably cancel threads, so all buffers passed to the threads
2964 * must not be freed before all referencing threads terminate.
2967 state->name = talloc_strdup(state, smb_fname->base_name);
2968 if (tevent_req_nomem(state->name, req)) {
2969 return tevent_req_post(req, ev);
2972 state->xattr_name = talloc_strdup(state, xattr_name);
2973 if (tevent_req_nomem(state->xattr_name, req)) {
2974 return tevent_req_post(req, ev);
2977 if (state->xattr_bufsize > 0) {
2978 state->xattr_value = talloc_zero_array(state,
2980 state->xattr_bufsize);
2981 if (tevent_req_nomem(state->xattr_value, req)) {
2982 return tevent_req_post(req, ev);
2986 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
2988 subreq = pthreadpool_tevent_job_send(state,
2991 vfswrap_getxattrat_do,
2993 if (tevent_req_nomem(subreq, req)) {
2994 return tevent_req_post(req, ev);
2996 tevent_req_set_callback(subreq, vfswrap_getxattrat_done, req);
2998 talloc_set_destructor(state, vfswrap_getxattrat_state_destructor);
3003 static void vfswrap_getxattrat_do(void *private_data)
3005 struct vfswrap_getxattrat_state *state = talloc_get_type_abort(
3006 private_data, struct vfswrap_getxattrat_state);
3007 struct timespec start_time;
3008 struct timespec end_time;
3011 PROFILE_TIMESTAMP(&start_time);
3012 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
3015 * Here we simulate a getxattrat()
3016 * call using fchdir();getxattr()
3018 * We don't need to revert the directory
3019 * change as pthreadpool_tevent wrapper
3022 SMB_ASSERT(pthreadpool_tevent_current_job_per_thread_cwd());
3024 ret = fchdir(state->dirfd);
3026 state->xattr_size = -1;
3027 state->vfs_aio_state.error = errno;
3031 state->xattr_size = getxattr(state->name,
3034 state->xattr_bufsize);
3035 if (state->xattr_size == -1) {
3036 state->vfs_aio_state.error = errno;
3040 PROFILE_TIMESTAMP(&end_time);
3041 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
3042 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
3045 static void vfswrap_getxattrat_done(struct tevent_req *subreq)
3047 struct tevent_req *req = tevent_req_callback_data(
3048 subreq, struct tevent_req);
3049 struct vfswrap_getxattrat_state *state = tevent_req_data(
3050 req, struct vfswrap_getxattrat_state);
3053 ret = pthreadpool_tevent_job_recv(subreq);
3054 TALLOC_FREE(subreq);
3055 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
3056 talloc_set_destructor(state, NULL);
3057 if (tevent_req_error(req, ret)) {
3061 if (state->xattr_size == -1) {
3062 tevent_req_error(req, state->vfs_aio_state.error);
3066 if (state->xattr_value == NULL) {
3068 * The caller only wanted the size.
3070 tevent_req_done(req);
3075 * shrink the buffer to the returned size.
3076 * (can't fail). It means NULL if size is 0.
3078 state->xattr_value = talloc_realloc(state,
3083 tevent_req_done(req);
3086 static ssize_t vfswrap_getxattrat_recv(struct tevent_req *req,
3087 struct vfs_aio_state *aio_state,
3088 TALLOC_CTX *mem_ctx,
3089 uint8_t **xattr_value)
3091 struct vfswrap_getxattrat_state *state = tevent_req_data(
3092 req, struct vfswrap_getxattrat_state);
3095 if (tevent_req_is_unix_error(req, &aio_state->error)) {
3096 tevent_req_received(req);
3100 *aio_state = state->vfs_aio_state;
3101 xattr_size = state->xattr_size;
3102 if (xattr_value != NULL) {
3103 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
3106 tevent_req_received(req);
3110 static ssize_t vfswrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
3112 return fgetxattr(fsp->fh->fd, name, value, size);
3115 static ssize_t vfswrap_listxattr(struct vfs_handle_struct *handle,
3116 const struct smb_filename *smb_fname,
3120 return listxattr(smb_fname->base_name, list, size);
3123 static ssize_t vfswrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
3125 return flistxattr(fsp->fh->fd, list, size);
3128 static int vfswrap_removexattr(struct vfs_handle_struct *handle,
3129 const struct smb_filename *smb_fname,
3132 return removexattr(smb_fname->base_name, name);
3135 static int vfswrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
3137 return fremovexattr(fsp->fh->fd, name);
3140 static int vfswrap_setxattr(struct vfs_handle_struct *handle,
3141 const struct smb_filename *smb_fname,
3147 return setxattr(smb_fname->base_name, name, value, size, flags);
3150 static int vfswrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
3152 return fsetxattr(fsp->fh->fd, name, value, size, flags);
3155 static bool vfswrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
3160 static bool vfswrap_is_offline(struct vfs_handle_struct *handle,
3161 const struct smb_filename *fname,
3162 SMB_STRUCT_STAT *sbuf)
3166 bool offline = false;
3168 if (ISDOT(fname->base_name) || ISDOTDOT(fname->base_name)) {
3172 if (!lp_dmapi_support(SNUM(handle->conn)) || !dmapi_have_session()) {
3173 #if defined(ENOTSUP)
3179 status = get_full_smb_filename(talloc_tos(), fname, &path);
3180 if (!NT_STATUS_IS_OK(status)) {
3181 errno = map_errno_from_nt_status(status);
3185 offline = (dmapi_file_flags(path) & FILE_ATTRIBUTE_OFFLINE) != 0;
3192 static NTSTATUS vfswrap_durable_cookie(struct vfs_handle_struct *handle,
3193 struct files_struct *fsp,
3194 TALLOC_CTX *mem_ctx,
3197 return vfs_default_durable_cookie(fsp, mem_ctx, cookie);
3200 static NTSTATUS vfswrap_durable_disconnect(struct vfs_handle_struct *handle,
3201 struct files_struct *fsp,
3202 const DATA_BLOB old_cookie,
3203 TALLOC_CTX *mem_ctx,
3204 DATA_BLOB *new_cookie)
3206 return vfs_default_durable_disconnect(fsp, old_cookie, mem_ctx,
3210 static NTSTATUS vfswrap_durable_reconnect(struct vfs_handle_struct *handle,
3211 struct smb_request *smb1req,
3212 struct smbXsrv_open *op,
3213 const DATA_BLOB old_cookie,
3214 TALLOC_CTX *mem_ctx,
3215 struct files_struct **fsp,
3216 DATA_BLOB *new_cookie)
3218 return vfs_default_durable_reconnect(handle->conn, smb1req, op,
3219 old_cookie, mem_ctx,
3223 static struct vfs_fn_pointers vfs_default_fns = {
3224 /* Disk operations */
3226 .connect_fn = vfswrap_connect,
3227 .disconnect_fn = vfswrap_disconnect,
3228 .disk_free_fn = vfswrap_disk_free,
3229 .get_quota_fn = vfswrap_get_quota,
3230 .set_quota_fn = vfswrap_set_quota,
3231 .get_shadow_copy_data_fn = vfswrap_get_shadow_copy_data,
3232 .statvfs_fn = vfswrap_statvfs,
3233 .fs_capabilities_fn = vfswrap_fs_capabilities,
3234 .get_dfs_referrals_fn = vfswrap_get_dfs_referrals,
3235 .snap_check_path_fn = vfswrap_snap_check_path,
3236 .snap_create_fn = vfswrap_snap_create,
3237 .snap_delete_fn = vfswrap_snap_delete,
3239 /* Directory operations */
3241 .opendir_fn = vfswrap_opendir,
3242 .fdopendir_fn = vfswrap_fdopendir,
3243 .readdir_fn = vfswrap_readdir,
3244 .readdir_attr_fn = vfswrap_readdir_attr,
3245 .seekdir_fn = vfswrap_seekdir,
3246 .telldir_fn = vfswrap_telldir,
3247 .rewind_dir_fn = vfswrap_rewinddir,
3248 .mkdir_fn = vfswrap_mkdir,
3249 .rmdir_fn = vfswrap_rmdir,
3250 .closedir_fn = vfswrap_closedir,
3252 /* File operations */
3254 .open_fn = vfswrap_open,
3255 .create_file_fn = vfswrap_create_file,
3256 .close_fn = vfswrap_close,
3257 .pread_fn = vfswrap_pread,
3258 .pread_send_fn = vfswrap_pread_send,
3259 .pread_recv_fn = vfswrap_pread_recv,
3260 .pwrite_fn = vfswrap_pwrite,
3261 .pwrite_send_fn = vfswrap_pwrite_send,
3262 .pwrite_recv_fn = vfswrap_pwrite_recv,
3263 .lseek_fn = vfswrap_lseek,
3264 .sendfile_fn = vfswrap_sendfile,
3265 .recvfile_fn = vfswrap_recvfile,
3266 .rename_fn = vfswrap_rename,
3267 .fsync_send_fn = vfswrap_fsync_send,
3268 .fsync_recv_fn = vfswrap_fsync_recv,
3269 .stat_fn = vfswrap_stat,
3270 .fstat_fn = vfswrap_fstat,
3271 .lstat_fn = vfswrap_lstat,
3272 .get_alloc_size_fn = vfswrap_get_alloc_size,
3273 .unlink_fn = vfswrap_unlink,
3274 .chmod_fn = vfswrap_chmod,
3275 .fchmod_fn = vfswrap_fchmod,
3276 .chown_fn = vfswrap_chown,
3277 .fchown_fn = vfswrap_fchown,
3278 .lchown_fn = vfswrap_lchown,
3279 .chdir_fn = vfswrap_chdir,
3280 .getwd_fn = vfswrap_getwd,
3281 .ntimes_fn = vfswrap_ntimes,
3282 .ftruncate_fn = vfswrap_ftruncate,
3283 .fallocate_fn = vfswrap_fallocate,
3284 .lock_fn = vfswrap_lock,
3285 .kernel_flock_fn = vfswrap_kernel_flock,
3286 .linux_setlease_fn = vfswrap_linux_setlease,
3287 .getlock_fn = vfswrap_getlock,
3288 .symlink_fn = vfswrap_symlink,
3289 .readlink_fn = vfswrap_readlink,
3290 .link_fn = vfswrap_link,
3291 .mknod_fn = vfswrap_mknod,
3292 .realpath_fn = vfswrap_realpath,
3293 .chflags_fn = vfswrap_chflags,
3294 .file_id_create_fn = vfswrap_file_id_create,
3295 .streaminfo_fn = vfswrap_streaminfo,
3296 .get_real_filename_fn = vfswrap_get_real_filename,
3297 .connectpath_fn = vfswrap_connectpath,
3298 .brl_lock_windows_fn = vfswrap_brl_lock_windows,
3299 .brl_unlock_windows_fn = vfswrap_brl_unlock_windows,
3300 .brl_cancel_windows_fn = vfswrap_brl_cancel_windows,
3301 .strict_lock_check_fn = vfswrap_strict_lock_check,
3302 .translate_name_fn = vfswrap_translate_name,
3303 .fsctl_fn = vfswrap_fsctl,
3304 .set_dos_attributes_fn = vfswrap_set_dos_attributes,
3305 .fset_dos_attributes_fn = vfswrap_fset_dos_attributes,
3306 .get_dos_attributes_fn = vfswrap_get_dos_attributes,
3307 .get_dos_attributes_send_fn = vfswrap_get_dos_attributes_send,
3308 .get_dos_attributes_recv_fn = vfswrap_get_dos_attributes_recv,
3309 .fget_dos_attributes_fn = vfswrap_fget_dos_attributes,
3310 .offload_read_send_fn = vfswrap_offload_read_send,
3311 .offload_read_recv_fn = vfswrap_offload_read_recv,
3312 .offload_write_send_fn = vfswrap_offload_write_send,
3313 .offload_write_recv_fn = vfswrap_offload_write_recv,
3314 .get_compression_fn = vfswrap_get_compression,
3315 .set_compression_fn = vfswrap_set_compression,
3317 /* NT ACL operations. */
3319 .fget_nt_acl_fn = vfswrap_fget_nt_acl,
3320 .get_nt_acl_fn = vfswrap_get_nt_acl,
3321 .fset_nt_acl_fn = vfswrap_fset_nt_acl,
3322 .audit_file_fn = vfswrap_audit_file,
3324 /* POSIX ACL operations. */
3326 .sys_acl_get_file_fn = vfswrap_sys_acl_get_file,
3327 .sys_acl_get_fd_fn = vfswrap_sys_acl_get_fd,
3328 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
3329 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
3330 .sys_acl_set_file_fn = vfswrap_sys_acl_set_file,
3331 .sys_acl_set_fd_fn = vfswrap_sys_acl_set_fd,
3332 .sys_acl_delete_def_file_fn = vfswrap_sys_acl_delete_def_file,
3334 /* EA operations. */
3335 .getxattr_fn = vfswrap_getxattr,
3336 .getxattrat_send_fn = vfswrap_getxattrat_send,
3337 .getxattrat_recv_fn = vfswrap_getxattrat_recv,
3338 .fgetxattr_fn = vfswrap_fgetxattr,
3339 .listxattr_fn = vfswrap_listxattr,
3340 .flistxattr_fn = vfswrap_flistxattr,
3341 .removexattr_fn = vfswrap_removexattr,
3342 .fremovexattr_fn = vfswrap_fremovexattr,
3343 .setxattr_fn = vfswrap_setxattr,
3344 .fsetxattr_fn = vfswrap_fsetxattr,
3346 /* aio operations */
3347 .aio_force_fn = vfswrap_aio_force,
3349 /* durable handle operations */
3350 .durable_cookie_fn = vfswrap_durable_cookie,
3351 .durable_disconnect_fn = vfswrap_durable_disconnect,
3352 .durable_reconnect_fn = vfswrap_durable_reconnect,
3356 NTSTATUS vfs_default_init(TALLOC_CTX *ctx)
3359 * Here we need to implement every call!
3361 * As this is the end of the vfs module chain.
3363 smb_vfs_assert_all_fns(&vfs_default_fns, DEFAULT_VFS_MODULE_NAME);
3364 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3365 DEFAULT_VFS_MODULE_NAME, &vfs_default_fns);