2 Unix SMB/CIFS implementation.
4 Wrap GlusterFS GFAPI calls in vfs functions.
6 Copyright (c) 2013 Anand Avati <avati@redhat.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @file vfs_glusterfs.c
24 * @author Anand Avati <avati@redhat.com>
26 * @brief Samba VFS module for glusterfs
29 * - sendfile/recvfile support
31 * A Samba VFS module for GlusterFS, based on Gluster's libgfapi.
32 * This is a "bottom" vfs module (not something to be stacked on top of
33 * another module), and translates (most) calls to the closest actions
34 * available in libgfapi.
39 #include "smbd/smbd.h"
41 #include <glusterfs/api/glfs.h>
42 #include "lib/util/dlinklist.h"
43 #include "lib/util/tevent_unix.h"
44 #include "smbd/globals.h"
45 #include "lib/util/sys_rw.h"
46 #include "smbprofile.h"
47 #include "modules/posixacl_xattr.h"
48 #include "lib/pthreadpool/pthreadpool_tevent.h"
50 #define DEFAULT_VOLFILE_SERVER "localhost"
51 #define GLUSTER_NAME_MAX 255
54 * Helper to convert struct stat to struct stat_ex.
56 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
60 dst->st_ex_dev = src->st_dev;
61 dst->st_ex_ino = src->st_ino;
62 dst->st_ex_mode = src->st_mode;
63 dst->st_ex_nlink = src->st_nlink;
64 dst->st_ex_uid = src->st_uid;
65 dst->st_ex_gid = src->st_gid;
66 dst->st_ex_rdev = src->st_rdev;
67 dst->st_ex_size = src->st_size;
68 dst->st_ex_atime.tv_sec = src->st_atime;
69 dst->st_ex_mtime.tv_sec = src->st_mtime;
70 dst->st_ex_ctime.tv_sec = src->st_ctime;
71 dst->st_ex_btime.tv_sec = src->st_mtime;
72 dst->st_ex_blksize = src->st_blksize;
73 dst->st_ex_blocks = src->st_blocks;
74 dst->st_ex_file_id = dst->st_ex_ino;
75 dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_FILE_ID;
77 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
78 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
79 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
80 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
82 dst->st_ex_itime = dst->st_ex_btime;
83 dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_ITIME;
86 /* pre-opened glfs_t */
88 static struct glfs_preopened {
93 struct glfs_preopened *next, *prev;
97 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
99 struct glfs_preopened *entry = NULL;
101 entry = talloc_zero(NULL, struct glfs_preopened);
107 entry->volume = talloc_strdup(entry, volume);
108 if (!entry->volume) {
114 entry->connectpath = talloc_strdup(entry, connectpath);
115 if (entry->connectpath == NULL) {
124 DLIST_ADD(glfs_preopened, entry);
129 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
131 struct glfs_preopened *entry = NULL;
133 for (entry = glfs_preopened; entry; entry = entry->next) {
134 if (strcmp(entry->volume, volume) == 0 &&
135 strcmp(entry->connectpath, connectpath) == 0)
145 static void glfs_clear_preopened(glfs_t *fs)
147 struct glfs_preopened *entry = NULL;
149 for (entry = glfs_preopened; entry; entry = entry->next) {
150 if (entry->fs == fs) {
154 DLIST_REMOVE(glfs_preopened, entry);
156 glfs_fini(entry->fs);
162 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
163 const char *volfile_servers)
166 size_t server_count = 0;
167 size_t server_success = 0;
169 TALLOC_CTX *frame = talloc_stackframe();
171 DBG_INFO("servers list %s\n", volfile_servers);
173 while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
174 char *transport = NULL;
179 DBG_INFO("server %zu %s\n", server_count, server);
181 /* Determine the transport type */
182 if (strncmp(server, "unix+", 5) == 0) {
184 transport = talloc_strdup(frame, "unix");
189 host = talloc_strdup(frame, server + 5);
196 char *port_index = NULL;
198 if (strncmp(server, "tcp+", 4) == 0) {
202 /* IPv6 is enclosed in []
203 * ':' before ']' is part of IPv6
204 * ':' after ']' indicates port
207 if (server[0] == '[') {
209 p = index(server, ']');
218 port_index = index(p, ':');
220 if (port_index == NULL) {
223 port = atoi(port_index + 1);
224 port_index[0] = '\0';
226 transport = talloc_strdup(frame, "tcp");
231 host = talloc_strdup(frame, server);
238 DBG_INFO("Calling set volfile server with params "
239 "transport=%s, host=%s, port=%d\n", transport,
242 ret = glfs_set_volfile_server(fs, transport, host, port);
244 DBG_WARNING("Failed to set volfile_server "
245 "transport=%s, host=%s, port=%d (%s)\n",
246 transport, host, port, strerror(errno));
253 if (server_count == 0) {
255 } else if (server_success < server_count) {
256 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
257 server_count - server_success, server_count);
265 /* Disk Operations */
267 static int check_for_write_behind_translator(TALLOC_CTX *mem_ctx,
276 bool write_behind_present = false;
280 ret = glfs_get_volfile(fs, NULL, 0);
282 DBG_ERR("%s: Failed to get volfile for "
283 "volume (%s): No volfile\n",
289 DBG_ERR("%s: Invalid return %d for glfs_get_volfile for "
290 "volume (%s): No volfile\n",
299 buf = talloc_zero_array(mem_ctx, char, newlen);
304 ret = glfs_get_volfile(fs, buf, newlen);
307 DBG_ERR("%s: Failed to get volfile for volume (%s)\n",
308 volume, strerror(errno));
312 option = talloc_asprintf(mem_ctx, "volume %s-write-behind", volume);
313 if (option == NULL) {
319 * file_lines_parse() plays horrible tricks with
320 * the passed-in talloc pointers and the hierarcy
321 * which makes freeing hard to get right.
323 * As we know mem_ctx is freed by the caller, after
324 * this point don't free on exit and let the caller
325 * handle it. This violates good Samba coding practice
326 * but we know we're not leaking here.
329 lines = file_lines_parse(buf,
333 if (lines == NULL || numlines <= 0) {
336 /* On success, buf is now a talloc child of lines !! */
338 for (i=0; i < numlines; i++) {
339 if (strequal(lines[i], option)) {
340 write_behind_present = true;
345 if (write_behind_present) {
346 DBG_ERR("Write behind translator is enabled for "
347 "volume (%s), refusing to connect! "
348 "Please turn off the write behind translator by calling "
349 "'gluster volume set %s performance.write-behind off' "
350 "on the commandline. "
351 "Check the vfs_glusterfs(8) manpage for "
352 "further details.\n",
360 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
364 const struct loadparm_substitution *lp_sub =
365 loadparm_s3_global_substitution();
366 const char *volfile_servers;
373 bool write_behind_pass_through_set = false;
375 tmp_ctx = talloc_new(NULL);
376 if (tmp_ctx == NULL) {
380 logfile = lp_parm_substituted_string(tmp_ctx,
387 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
389 volfile_servers = lp_parm_substituted_string(tmp_ctx,
395 if (volfile_servers == NULL) {
396 volfile_servers = DEFAULT_VOLFILE_SERVER;
399 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
401 if (volume == NULL) {
405 fs = glfs_find_preopened(volume, handle->conn->connectpath);
410 fs = glfs_new(volume);
416 ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
418 DBG_ERR("Failed to set volfile_servers from list %s\n",
423 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
426 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
430 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-selinux",
433 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
437 ret = glfs_set_xlator_option(fs, "*-snapview-client",
438 "snapdir-entry-path",
439 handle->conn->connectpath);
441 DEBUG(0, ("%s: Failed to set xlator option:"
442 " snapdir-entry-path\n", volume));
446 #ifdef HAVE_GFAPI_VER_7_9
447 ret = glfs_set_xlator_option(fs, "*-write-behind", "pass-through",
450 DBG_ERR("%s: Failed to set xlator option: pass-through\n",
454 write_behind_pass_through_set = true;
457 ret = glfs_set_logging(fs, logfile, loglevel);
459 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
460 volume, logfile, loglevel));
466 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
467 volume, strerror(errno)));
471 if (!write_behind_pass_through_set) {
472 ret = check_for_write_behind_translator(tmp_ctx, fs, volume);
478 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
480 DEBUG(0, ("%s: Failed to register volume (%s)\n",
481 volume, strerror(errno)));
486 * The shadow_copy2 module will fail to export subdirectories
487 * of a gluster volume unless we specify the mount point,
488 * because the detection fails if the file system is not
490 * https://bugzilla.samba.org/show_bug.cgi?id=13091
492 lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
495 * Unless we have an async implementation of getxattrat turn this off.
497 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
504 DBG_ERR("%s: Initialized volume from servers %s\n",
505 volume, volfile_servers);
508 talloc_free(tmp_ctx);
512 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
518 glfs_clear_preopened(fs);
521 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
522 const struct smb_filename *smb_fname,
527 struct statvfs statvfs = { 0, };
530 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
535 if (bsize_p != NULL) {
536 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
538 if (dfree_p != NULL) {
539 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
541 if (dsize_p != NULL) {
542 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
545 return (uint64_t)statvfs.f_bavail;
548 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
549 const struct smb_filename *smb_fname,
550 enum SMB_QUOTA_TYPE qtype,
559 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
560 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
566 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
567 const struct smb_filename *smb_fname,
568 struct vfs_statvfs_struct *vfs_statvfs)
570 struct statvfs statvfs = { 0, };
573 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
575 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
576 smb_fname->base_name, strerror(errno)));
580 ZERO_STRUCTP(vfs_statvfs);
582 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
583 vfs_statvfs->BlockSize = statvfs.f_bsize;
584 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
585 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
586 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
587 vfs_statvfs->TotalFileNodes = statvfs.f_files;
588 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
589 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
590 vfs_statvfs->FsCapabilities =
591 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
596 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
597 enum timestamp_set_resolution *p_ts_res)
599 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
601 #ifdef HAVE_GFAPI_VER_6
602 caps |= FILE_SUPPORTS_SPARSE_FILES;
605 #ifdef STAT_HAVE_NSEC
606 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
612 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
615 glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
617 DBG_INFO("Failed to fetch fsp extension\n");
621 DBG_INFO("Empty glfs_fd_t pointer\n");
628 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
629 files_struct *fsp, const char *mask,
632 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
634 DBG_ERR("Failed to fetch gluster fd\n");
641 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
645 START_PROFILE(syscall_closedir);
646 ret = glfs_closedir((void *)dirp);
647 END_PROFILE(syscall_closedir);
652 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
653 struct files_struct *dirfsp,
655 SMB_STRUCT_STAT *sbuf)
657 static char direntbuf[512];
660 struct dirent *dirent = 0;
662 START_PROFILE(syscall_readdir);
664 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
667 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
670 if ((ret < 0) || (dirent == NULL)) {
671 END_PROFILE(syscall_readdir);
676 SET_STAT_INVALID(*sbuf);
677 if (!S_ISLNK(stat.st_mode)) {
678 smb_stat_ex_from_stat(sbuf, &stat);
682 END_PROFILE(syscall_readdir);
686 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
690 START_PROFILE(syscall_telldir);
691 ret = glfs_telldir((void *)dirp);
692 END_PROFILE(syscall_telldir);
697 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
700 START_PROFILE(syscall_seekdir);
701 glfs_seekdir((void *)dirp, offset);
702 END_PROFILE(syscall_seekdir);
705 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
707 START_PROFILE(syscall_rewinddir);
708 glfs_seekdir((void *)dirp, 0);
709 END_PROFILE(syscall_rewinddir);
712 static int vfs_gluster_mkdirat(struct vfs_handle_struct *handle,
713 struct files_struct *dirfsp,
714 const struct smb_filename *smb_fname,
717 struct smb_filename *full_fname = NULL;
720 START_PROFILE(syscall_mkdirat);
722 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
725 if (full_fname == NULL) {
726 END_PROFILE(syscall_mkdirat);
730 ret = glfs_mkdir(handle->data, full_fname->base_name, mode);
732 TALLOC_FREE(full_fname);
734 END_PROFILE(syscall_mkdirat);
739 static int vfs_gluster_openat(struct vfs_handle_struct *handle,
740 const struct files_struct *dirfsp,
741 const struct smb_filename *smb_fname,
746 struct smb_filename *name = NULL;
747 bool became_root = false;
751 START_PROFILE(syscall_openat);
754 * Looks like glfs API doesn't have openat().
756 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
757 name = full_path_from_dirfsp_atname(talloc_tos(),
766 p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
769 END_PROFILE(syscall_openat);
774 if (fsp->fsp_flags.is_pathref) {
776 * ceph doesn't support O_PATH so we have to fallback to
783 if (flags & O_DIRECTORY) {
784 glfd = glfs_opendir(handle->data, smb_fname->base_name);
785 } else if (flags & O_CREAT) {
786 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
789 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
796 fsp->fsp_flags.have_proc_fds = false;
800 END_PROFILE(syscall_openat);
801 /* no extension destroy_fn, so no need to save errno */
802 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
809 END_PROFILE(syscall_openat);
810 /* An arbitrary value for error reporting, so you know its us. */
814 static int vfs_gluster_close(struct vfs_handle_struct *handle,
818 glfs_fd_t *glfd = NULL;
820 START_PROFILE(syscall_close);
822 glfd = vfs_gluster_fetch_glfd(handle, fsp);
824 END_PROFILE(syscall_close);
825 DBG_ERR("Failed to fetch gluster fd\n");
829 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
831 ret = glfs_close(glfd);
832 END_PROFILE(syscall_close);
837 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
838 files_struct *fsp, void *data, size_t n,
842 glfs_fd_t *glfd = NULL;
844 START_PROFILE_BYTES(syscall_pread, n);
846 glfd = vfs_gluster_fetch_glfd(handle, fsp);
848 END_PROFILE_BYTES(syscall_pread);
849 DBG_ERR("Failed to fetch gluster fd\n");
853 #ifdef HAVE_GFAPI_VER_7_6
854 ret = glfs_pread(glfd, data, n, offset, 0, NULL);
856 ret = glfs_pread(glfd, data, n, offset, 0);
858 END_PROFILE_BYTES(syscall_pread);
863 struct vfs_gluster_pread_state {
870 struct vfs_aio_state vfs_aio_state;
871 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
874 static void vfs_gluster_pread_do(void *private_data);
875 static void vfs_gluster_pread_done(struct tevent_req *subreq);
876 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state);
878 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
879 *handle, TALLOC_CTX *mem_ctx,
880 struct tevent_context *ev,
882 void *data, size_t n,
885 struct vfs_gluster_pread_state *state;
886 struct tevent_req *req, *subreq;
888 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
890 DBG_ERR("Failed to fetch gluster fd\n");
894 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pread_state);
903 state->offset = offset;
905 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
906 state->profile_bytes, n);
907 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
909 subreq = pthreadpool_tevent_job_send(
910 state, ev, handle->conn->sconn->pool,
911 vfs_gluster_pread_do, state);
912 if (tevent_req_nomem(subreq, req)) {
913 return tevent_req_post(req, ev);
915 tevent_req_set_callback(subreq, vfs_gluster_pread_done, req);
917 talloc_set_destructor(state, vfs_gluster_pread_state_destructor);
922 static void vfs_gluster_pread_do(void *private_data)
924 struct vfs_gluster_pread_state *state = talloc_get_type_abort(
925 private_data, struct vfs_gluster_pread_state);
926 struct timespec start_time;
927 struct timespec end_time;
929 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
931 PROFILE_TIMESTAMP(&start_time);
934 #ifdef HAVE_GFAPI_VER_7_6
935 state->ret = glfs_pread(state->fd, state->buf, state->count,
936 state->offset, 0, NULL);
938 state->ret = glfs_pread(state->fd, state->buf, state->count,
941 } while ((state->ret == -1) && (errno == EINTR));
943 if (state->ret == -1) {
944 state->vfs_aio_state.error = errno;
947 PROFILE_TIMESTAMP(&end_time);
949 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
951 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
954 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state)
959 static void vfs_gluster_pread_done(struct tevent_req *subreq)
961 struct tevent_req *req = tevent_req_callback_data(
962 subreq, struct tevent_req);
963 struct vfs_gluster_pread_state *state = tevent_req_data(
964 req, struct vfs_gluster_pread_state);
967 ret = pthreadpool_tevent_job_recv(subreq);
969 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
970 talloc_set_destructor(state, NULL);
973 tevent_req_error(req, ret);
977 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
978 * means the lower level pthreadpool failed to create a new
979 * thread. Fallback to sync processing in that case to allow
980 * some progress for the client.
982 vfs_gluster_pread_do(state);
985 tevent_req_done(req);
988 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req,
989 struct vfs_aio_state *vfs_aio_state)
991 struct vfs_gluster_pread_state *state = tevent_req_data(
992 req, struct vfs_gluster_pread_state);
994 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
998 *vfs_aio_state = state->vfs_aio_state;
1002 struct vfs_gluster_pwrite_state {
1009 struct vfs_aio_state vfs_aio_state;
1010 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1013 static void vfs_gluster_pwrite_do(void *private_data);
1014 static void vfs_gluster_pwrite_done(struct tevent_req *subreq);
1015 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state);
1017 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
1018 *handle, TALLOC_CTX *mem_ctx,
1019 struct tevent_context *ev,
1021 const void *data, size_t n,
1024 struct tevent_req *req, *subreq;
1025 struct vfs_gluster_pwrite_state *state;
1027 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1029 DBG_ERR("Failed to fetch gluster fd\n");
1033 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pwrite_state);
1042 state->offset = offset;
1044 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
1045 state->profile_bytes, n);
1046 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1048 subreq = pthreadpool_tevent_job_send(
1049 state, ev, handle->conn->sconn->pool,
1050 vfs_gluster_pwrite_do, state);
1051 if (tevent_req_nomem(subreq, req)) {
1052 return tevent_req_post(req, ev);
1054 tevent_req_set_callback(subreq, vfs_gluster_pwrite_done, req);
1056 talloc_set_destructor(state, vfs_gluster_pwrite_state_destructor);
1061 static void vfs_gluster_pwrite_do(void *private_data)
1063 struct vfs_gluster_pwrite_state *state = talloc_get_type_abort(
1064 private_data, struct vfs_gluster_pwrite_state);
1065 struct timespec start_time;
1066 struct timespec end_time;
1068 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1070 PROFILE_TIMESTAMP(&start_time);
1073 #ifdef HAVE_GFAPI_VER_7_6
1074 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1075 state->offset, 0, NULL, NULL);
1077 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1080 } while ((state->ret == -1) && (errno == EINTR));
1082 if (state->ret == -1) {
1083 state->vfs_aio_state.error = errno;
1086 PROFILE_TIMESTAMP(&end_time);
1088 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1090 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1093 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state)
1098 static void vfs_gluster_pwrite_done(struct tevent_req *subreq)
1100 struct tevent_req *req = tevent_req_callback_data(
1101 subreq, struct tevent_req);
1102 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1103 req, struct vfs_gluster_pwrite_state);
1106 ret = pthreadpool_tevent_job_recv(subreq);
1107 TALLOC_FREE(subreq);
1108 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1109 talloc_set_destructor(state, NULL);
1111 if (ret != EAGAIN) {
1112 tevent_req_error(req, ret);
1116 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1117 * means the lower level pthreadpool failed to create a new
1118 * thread. Fallback to sync processing in that case to allow
1119 * some progress for the client.
1121 vfs_gluster_pwrite_do(state);
1124 tevent_req_done(req);
1127 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req,
1128 struct vfs_aio_state *vfs_aio_state)
1130 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1131 req, struct vfs_gluster_pwrite_state);
1133 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1137 *vfs_aio_state = state->vfs_aio_state;
1142 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
1143 files_struct *fsp, const void *data,
1144 size_t n, off_t offset)
1147 glfs_fd_t *glfd = NULL;
1149 START_PROFILE_BYTES(syscall_pwrite, n);
1151 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1153 END_PROFILE_BYTES(syscall_pwrite);
1154 DBG_ERR("Failed to fetch gluster fd\n");
1158 #ifdef HAVE_GFAPI_VER_7_6
1159 ret = glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
1161 ret = glfs_pwrite(glfd, data, n, offset, 0);
1163 END_PROFILE_BYTES(syscall_pwrite);
1168 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
1169 files_struct *fsp, off_t offset, int whence)
1172 glfs_fd_t *glfd = NULL;
1174 START_PROFILE(syscall_lseek);
1176 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1178 END_PROFILE(syscall_lseek);
1179 DBG_ERR("Failed to fetch gluster fd\n");
1183 ret = glfs_lseek(glfd, offset, whence);
1184 END_PROFILE(syscall_lseek);
1189 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
1190 files_struct *fromfsp,
1191 const DATA_BLOB *hdr,
1192 off_t offset, size_t n)
1198 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1199 int fromfd, files_struct *tofsp,
1200 off_t offset, size_t n)
1206 static int vfs_gluster_renameat(struct vfs_handle_struct *handle,
1207 files_struct *srcfsp,
1208 const struct smb_filename *smb_fname_src,
1209 files_struct *dstfsp,
1210 const struct smb_filename *smb_fname_dst)
1214 START_PROFILE(syscall_renameat);
1215 ret = glfs_rename(handle->data, smb_fname_src->base_name,
1216 smb_fname_dst->base_name);
1217 END_PROFILE(syscall_renameat);
1222 struct vfs_gluster_fsync_state {
1226 struct vfs_aio_state vfs_aio_state;
1227 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1230 static void vfs_gluster_fsync_do(void *private_data);
1231 static void vfs_gluster_fsync_done(struct tevent_req *subreq);
1232 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state);
1234 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1235 *handle, TALLOC_CTX *mem_ctx,
1236 struct tevent_context *ev,
1239 struct tevent_req *req, *subreq;
1240 struct vfs_gluster_fsync_state *state;
1242 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1244 DBG_ERR("Failed to fetch gluster fd\n");
1248 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_fsync_state);
1256 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
1257 state->profile_bytes, 0);
1258 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1260 subreq = pthreadpool_tevent_job_send(
1261 state, ev, handle->conn->sconn->pool, vfs_gluster_fsync_do, state);
1262 if (tevent_req_nomem(subreq, req)) {
1263 return tevent_req_post(req, ev);
1265 tevent_req_set_callback(subreq, vfs_gluster_fsync_done, req);
1267 talloc_set_destructor(state, vfs_gluster_fsync_state_destructor);
1272 static void vfs_gluster_fsync_do(void *private_data)
1274 struct vfs_gluster_fsync_state *state = talloc_get_type_abort(
1275 private_data, struct vfs_gluster_fsync_state);
1276 struct timespec start_time;
1277 struct timespec end_time;
1279 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1281 PROFILE_TIMESTAMP(&start_time);
1284 #ifdef HAVE_GFAPI_VER_7_6
1285 state->ret = glfs_fsync(state->fd, NULL, NULL);
1287 state->ret = glfs_fsync(state->fd);
1289 } while ((state->ret == -1) && (errno == EINTR));
1291 if (state->ret == -1) {
1292 state->vfs_aio_state.error = errno;
1295 PROFILE_TIMESTAMP(&end_time);
1297 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1299 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1302 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state)
1307 static void vfs_gluster_fsync_done(struct tevent_req *subreq)
1309 struct tevent_req *req = tevent_req_callback_data(
1310 subreq, struct tevent_req);
1311 struct vfs_gluster_fsync_state *state = tevent_req_data(
1312 req, struct vfs_gluster_fsync_state);
1315 ret = pthreadpool_tevent_job_recv(subreq);
1316 TALLOC_FREE(subreq);
1317 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1318 talloc_set_destructor(state, NULL);
1320 if (ret != EAGAIN) {
1321 tevent_req_error(req, ret);
1325 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1326 * means the lower level pthreadpool failed to create a new
1327 * thread. Fallback to sync processing in that case to allow
1328 * some progress for the client.
1330 vfs_gluster_fsync_do(state);
1333 tevent_req_done(req);
1336 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1337 struct vfs_aio_state *vfs_aio_state)
1339 struct vfs_gluster_fsync_state *state = tevent_req_data(
1340 req, struct vfs_gluster_fsync_state);
1342 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1346 *vfs_aio_state = state->vfs_aio_state;
1350 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1351 struct smb_filename *smb_fname)
1356 START_PROFILE(syscall_stat);
1357 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1359 smb_stat_ex_from_stat(&smb_fname->st, &st);
1361 if (ret < 0 && errno != ENOENT) {
1362 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1363 smb_fname->base_name, strerror(errno)));
1365 END_PROFILE(syscall_stat);
1370 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1371 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1375 glfs_fd_t *glfd = NULL;
1377 START_PROFILE(syscall_fstat);
1379 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1381 END_PROFILE(syscall_fstat);
1382 DBG_ERR("Failed to fetch gluster fd\n");
1386 ret = glfs_fstat(glfd, &st);
1388 smb_stat_ex_from_stat(sbuf, &st);
1391 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1392 fsp_get_io_fd(fsp), strerror(errno)));
1394 END_PROFILE(syscall_fstat);
1399 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1400 struct smb_filename *smb_fname)
1405 START_PROFILE(syscall_lstat);
1406 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1408 smb_stat_ex_from_stat(&smb_fname->st, &st);
1410 if (ret < 0 && errno != ENOENT) {
1411 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1412 smb_fname->base_name, strerror(errno)));
1414 END_PROFILE(syscall_lstat);
1419 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1421 const SMB_STRUCT_STAT *sbuf)
1425 START_PROFILE(syscall_get_alloc_size);
1426 ret = sbuf->st_ex_blocks * 512;
1427 END_PROFILE(syscall_get_alloc_size);
1432 static int vfs_gluster_unlinkat(struct vfs_handle_struct *handle,
1433 struct files_struct *dirfsp,
1434 const struct smb_filename *smb_fname,
1437 struct smb_filename *full_fname = NULL;
1440 START_PROFILE(syscall_unlinkat);
1442 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1445 if (full_fname == NULL) {
1446 END_PROFILE(syscall_unlinkat);
1450 if (flags & AT_REMOVEDIR) {
1451 ret = glfs_rmdir(handle->data, full_fname->base_name);
1453 ret = glfs_unlink(handle->data, full_fname->base_name);
1455 TALLOC_FREE(full_fname);
1456 END_PROFILE(syscall_unlinkat);
1461 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1462 files_struct *fsp, mode_t mode)
1465 glfs_fd_t *glfd = NULL;
1467 START_PROFILE(syscall_fchmod);
1469 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1471 END_PROFILE(syscall_fchmod);
1472 DBG_ERR("Failed to fetch gluster fd\n");
1476 if (!fsp->fsp_flags.is_pathref) {
1478 * We can use an io_fd to remove xattrs.
1480 ret = glfs_fchmod(glfd, mode);
1483 * This is no longer a handle based call.
1485 ret = glfs_chmod(handle->data, fsp->fsp_name->base_name, mode);
1487 END_PROFILE(syscall_fchmod);
1492 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1493 files_struct *fsp, uid_t uid, gid_t gid)
1496 glfs_fd_t *glfd = NULL;
1498 START_PROFILE(syscall_fchown);
1500 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1502 END_PROFILE(syscall_fchown);
1503 DBG_ERR("Failed to fetch gluster fd\n");
1507 ret = glfs_fchown(glfd, uid, gid);
1508 END_PROFILE(syscall_fchown);
1513 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1514 const struct smb_filename *smb_fname,
1520 START_PROFILE(syscall_lchown);
1521 ret = glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1522 END_PROFILE(syscall_lchown);
1527 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1528 const struct smb_filename *smb_fname)
1532 START_PROFILE(syscall_chdir);
1533 ret = glfs_chdir(handle->data, smb_fname->base_name);
1534 END_PROFILE(syscall_chdir);
1539 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1542 char cwd[PATH_MAX] = { '\0' };
1544 struct smb_filename *smb_fname = NULL;
1546 START_PROFILE(syscall_getwd);
1548 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1549 END_PROFILE(syscall_getwd);
1554 smb_fname = synthetic_smb_fname(ctx,
1563 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
1564 const struct smb_filename *smb_fname,
1565 struct smb_file_time *ft)
1568 struct timespec times[2];
1570 START_PROFILE(syscall_ntimes);
1572 if (is_omit_timespec(&ft->atime)) {
1573 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
1574 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
1576 times[0].tv_sec = ft->atime.tv_sec;
1577 times[0].tv_nsec = ft->atime.tv_nsec;
1580 if (is_omit_timespec(&ft->mtime)) {
1581 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
1582 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
1584 times[1].tv_sec = ft->mtime.tv_sec;
1585 times[1].tv_nsec = ft->mtime.tv_nsec;
1588 if ((timespec_compare(×[0],
1589 &smb_fname->st.st_ex_atime) == 0) &&
1590 (timespec_compare(×[1],
1591 &smb_fname->st.st_ex_mtime) == 0)) {
1592 END_PROFILE(syscall_ntimes);
1596 ret = glfs_utimens(handle->data, smb_fname->base_name, times);
1597 END_PROFILE(syscall_ntimes);
1602 static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
1604 struct smb_file_time *ft)
1607 struct timespec times[2];
1608 glfs_fd_t *glfd = NULL;
1610 START_PROFILE(syscall_fntimes);
1612 if (is_omit_timespec(&ft->atime)) {
1613 times[0].tv_sec = fsp->fsp_name->st.st_ex_atime.tv_sec;
1614 times[0].tv_nsec = fsp->fsp_name->st.st_ex_atime.tv_nsec;
1616 times[0].tv_sec = ft->atime.tv_sec;
1617 times[0].tv_nsec = ft->atime.tv_nsec;
1620 if (is_omit_timespec(&ft->mtime)) {
1621 times[1].tv_sec = fsp->fsp_name->st.st_ex_mtime.tv_sec;
1622 times[1].tv_nsec = fsp->fsp_name->st.st_ex_mtime.tv_nsec;
1624 times[1].tv_sec = ft->mtime.tv_sec;
1625 times[1].tv_nsec = ft->mtime.tv_nsec;
1628 if ((timespec_compare(×[0],
1629 &fsp->fsp_name->st.st_ex_atime) == 0) &&
1630 (timespec_compare(×[1],
1631 &fsp->fsp_name->st.st_ex_mtime) == 0)) {
1632 END_PROFILE(syscall_fntimes);
1636 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1638 END_PROFILE(syscall_fntimes);
1639 DBG_ERR("Failed to fetch gluster fd\n");
1643 ret = glfs_futimens(glfd, times);
1644 END_PROFILE(syscall_fntimes);
1649 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1650 files_struct *fsp, off_t offset)
1653 glfs_fd_t *glfd = NULL;
1655 START_PROFILE(syscall_ftruncate);
1657 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1659 END_PROFILE(syscall_ftruncate);
1660 DBG_ERR("Failed to fetch gluster fd\n");
1664 #ifdef HAVE_GFAPI_VER_7_6
1665 ret = glfs_ftruncate(glfd, offset, NULL, NULL);
1667 ret = glfs_ftruncate(glfd, offset);
1669 END_PROFILE(syscall_ftruncate);
1674 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1675 struct files_struct *fsp,
1677 off_t offset, off_t len)
1680 #ifdef HAVE_GFAPI_VER_6
1681 glfs_fd_t *glfd = NULL;
1682 int keep_size, punch_hole;
1684 START_PROFILE(syscall_fallocate);
1686 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1688 END_PROFILE(syscall_fallocate);
1689 DBG_ERR("Failed to fetch gluster fd\n");
1693 keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1694 punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1696 mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1698 END_PROFILE(syscall_fallocate);
1704 ret = glfs_discard(glfd, offset, len);
1706 DBG_DEBUG("glfs_discard failed: %s\n",
1711 ret = glfs_fallocate(glfd, keep_size, offset, len);
1712 END_PROFILE(syscall_fallocate);
1720 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1722 const struct smb_filename *smb_fname)
1724 char *result = NULL;
1725 struct smb_filename *result_fname = NULL;
1726 char *resolved_path = NULL;
1728 START_PROFILE(syscall_realpath);
1730 resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1731 if (resolved_path == NULL) {
1732 END_PROFILE(syscall_realpath);
1737 result = glfs_realpath(handle->data,
1738 smb_fname->base_name,
1740 if (result != NULL) {
1741 result_fname = synthetic_smb_fname(ctx,
1749 SAFE_FREE(resolved_path);
1750 END_PROFILE(syscall_realpath);
1752 return result_fname;
1755 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1756 files_struct *fsp, int op, off_t offset,
1757 off_t count, int type)
1759 struct flock flock = { 0, };
1761 glfs_fd_t *glfd = NULL;
1764 START_PROFILE(syscall_fcntl_lock);
1766 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1768 DBG_ERR("Failed to fetch gluster fd\n");
1773 flock.l_type = type;
1774 flock.l_whence = SEEK_SET;
1775 flock.l_start = offset;
1776 flock.l_len = count;
1779 ret = glfs_posix_lock(glfd, op, &flock);
1781 if (op == F_GETLK) {
1782 /* lock query, true if someone else has locked */
1784 (flock.l_type != F_UNLCK) &&
1785 (flock.l_pid != 0) && (flock.l_pid != getpid())) {
1801 END_PROFILE(syscall_fcntl_lock);
1806 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1807 files_struct *fsp, uint32_t share_access,
1808 uint32_t access_mask)
1814 static int vfs_gluster_fcntl(vfs_handle_struct *handle,
1815 files_struct *fsp, int cmd, va_list cmd_arg)
1818 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1819 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1821 if (cmd == F_GETFL) {
1823 } else if (cmd == F_SETFL) {
1824 va_list dup_cmd_arg;
1827 va_copy(dup_cmd_arg, cmd_arg);
1828 opt = va_arg(dup_cmd_arg, int);
1829 va_end(dup_cmd_arg);
1833 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1836 DBG_ERR("unexpected fcntl: %d\n", cmd);
1842 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1843 files_struct *fsp, int leasetype)
1849 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1850 files_struct *fsp, off_t *poffset,
1851 off_t *pcount, int *ptype, pid_t *ppid)
1853 struct flock flock = { 0, };
1855 glfs_fd_t *glfd = NULL;
1857 START_PROFILE(syscall_fcntl_getlock);
1859 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1861 END_PROFILE(syscall_fcntl_getlock);
1862 DBG_ERR("Failed to fetch gluster fd\n");
1866 flock.l_type = *ptype;
1867 flock.l_whence = SEEK_SET;
1868 flock.l_start = *poffset;
1869 flock.l_len = *pcount;
1872 ret = glfs_posix_lock(glfd, F_GETLK, &flock);
1875 END_PROFILE(syscall_fcntl_getlock);
1879 *ptype = flock.l_type;
1880 *poffset = flock.l_start;
1881 *pcount = flock.l_len;
1882 *ppid = flock.l_pid;
1883 END_PROFILE(syscall_fcntl_getlock);
1888 static int vfs_gluster_symlinkat(struct vfs_handle_struct *handle,
1889 const struct smb_filename *link_target,
1890 struct files_struct *dirfsp,
1891 const struct smb_filename *new_smb_fname)
1893 struct smb_filename *full_fname = NULL;
1896 START_PROFILE(syscall_symlinkat);
1898 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1901 if (full_fname == NULL) {
1902 END_PROFILE(syscall_symlinkat);
1906 ret = glfs_symlink(handle->data,
1907 link_target->base_name,
1908 full_fname->base_name);
1910 TALLOC_FREE(full_fname);
1912 END_PROFILE(syscall_symlinkat);
1917 static int vfs_gluster_readlinkat(struct vfs_handle_struct *handle,
1918 const struct files_struct *dirfsp,
1919 const struct smb_filename *smb_fname,
1923 struct smb_filename *full_fname = NULL;
1926 START_PROFILE(syscall_readlinkat);
1928 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1931 if (full_fname == NULL) {
1932 END_PROFILE(syscall_readlinkat);
1936 ret = glfs_readlink(handle->data, full_fname->base_name, buf, bufsiz);
1938 TALLOC_FREE(full_fname);
1940 END_PROFILE(syscall_readlinkat);
1945 static int vfs_gluster_linkat(struct vfs_handle_struct *handle,
1946 files_struct *srcfsp,
1947 const struct smb_filename *old_smb_fname,
1948 files_struct *dstfsp,
1949 const struct smb_filename *new_smb_fname,
1953 struct smb_filename *full_fname_old = NULL;
1954 struct smb_filename *full_fname_new = NULL;
1956 START_PROFILE(syscall_linkat);
1958 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
1961 if (full_fname_old == NULL) {
1962 END_PROFILE(syscall_linkat);
1965 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
1968 if (full_fname_new == NULL) {
1969 TALLOC_FREE(full_fname_old);
1970 END_PROFILE(syscall_linkat);
1974 ret = glfs_link(handle->data,
1975 full_fname_old->base_name,
1976 full_fname_new->base_name);
1978 TALLOC_FREE(full_fname_old);
1979 TALLOC_FREE(full_fname_new);
1980 END_PROFILE(syscall_linkat);
1985 static int vfs_gluster_mknodat(struct vfs_handle_struct *handle,
1986 files_struct *dirfsp,
1987 const struct smb_filename *smb_fname,
1991 struct smb_filename *full_fname = NULL;
1994 START_PROFILE(syscall_mknodat);
1996 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1999 if (full_fname == NULL) {
2000 END_PROFILE(syscall_mknodat);
2004 ret = glfs_mknod(handle->data, full_fname->base_name, mode, dev);
2006 TALLOC_FREE(full_fname);
2008 END_PROFILE(syscall_mknodat);
2013 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
2014 const struct smb_filename *smb_fname,
2021 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
2022 const struct smb_filename *path,
2024 TALLOC_CTX *mem_ctx,
2028 char key_buf[GLUSTER_NAME_MAX + 64];
2029 char val_buf[GLUSTER_NAME_MAX + 1];
2031 if (strlen(name) >= GLUSTER_NAME_MAX) {
2032 errno = ENAMETOOLONG;
2036 snprintf(key_buf, GLUSTER_NAME_MAX + 64,
2037 "glusterfs.get_real_filename:%s", name);
2039 ret = glfs_getxattr(handle->data, path->base_name, key_buf, val_buf,
2040 GLUSTER_NAME_MAX + 1);
2042 if (errno == ENOATTR) {
2048 *found_name = talloc_strdup(mem_ctx, val_buf);
2049 if (found_name[0] == NULL) {
2056 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
2057 const struct smb_filename *smb_fname)
2059 return handle->conn->connectpath;
2064 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
2065 const struct smb_filename *smb_fname,
2070 return glfs_getxattr(handle->data, smb_fname->base_name,
2074 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
2075 files_struct *fsp, const char *name,
2076 void *value, size_t size)
2078 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2080 DBG_ERR("Failed to fetch gluster fd\n");
2084 return glfs_fgetxattr(glfd, name, value, size);
2087 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
2088 files_struct *fsp, char *list,
2091 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2093 DBG_ERR("Failed to fetch gluster fd\n");
2096 if (!fsp->fsp_flags.is_pathref) {
2098 * We can use an io_fd to list xattrs.
2100 return glfs_flistxattr(glfd, list, size);
2103 * This is no longer a handle based call.
2105 return glfs_listxattr(handle->data,
2106 fsp->fsp_name->base_name,
2112 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
2113 files_struct *fsp, const char *name)
2115 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2117 DBG_ERR("Failed to fetch gluster fd\n");
2120 if (!fsp->fsp_flags.is_pathref) {
2122 * We can use an io_fd to remove xattrs.
2124 return glfs_fremovexattr(glfd, name);
2127 * This is no longer a handle based call.
2129 return glfs_removexattr(handle->data,
2130 fsp->fsp_name->base_name,
2135 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
2136 files_struct *fsp, const char *name,
2137 const void *value, size_t size, int flags)
2139 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2141 DBG_ERR("Failed to fetch gluster fd\n");
2145 if (!fsp->fsp_flags.is_pathref) {
2147 * We can use an io_fd to set xattrs.
2149 return glfs_fsetxattr(glfd, name, value, size, flags);
2152 * This is no longer a handle based call.
2154 return glfs_setxattr(handle->data,
2155 fsp->fsp_name->base_name,
2163 /* AIO Operations */
2165 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
2171 static NTSTATUS vfs_gluster_create_dfs_pathat(struct vfs_handle_struct *handle,
2172 struct files_struct *dirfsp,
2173 const struct smb_filename *smb_fname,
2174 const struct referral *reflist,
2175 size_t referral_count)
2177 TALLOC_CTX *frame = talloc_stackframe();
2178 NTSTATUS status = NT_STATUS_NO_MEMORY;
2180 char *msdfs_link = NULL;
2181 struct smb_filename *full_fname = NULL;
2183 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2186 if (full_fname == NULL) {
2190 /* Form the msdfs_link contents */
2191 msdfs_link = msdfs_link_string(frame,
2194 if (msdfs_link == NULL) {
2198 ret = glfs_symlink(handle->data,
2200 full_fname->base_name);
2202 status = NT_STATUS_OK;
2204 status = map_nt_error_from_unix(errno);
2214 * Read and return the contents of a DFS redirect given a
2215 * pathname. A caller can pass in NULL for ppreflist and
2216 * preferral_count but still determine if this was a
2217 * DFS redirect point by getting NT_STATUS_OK back
2218 * without incurring the overhead of reading and parsing
2219 * the referral contents.
2222 static NTSTATUS vfs_gluster_read_dfs_pathat(struct vfs_handle_struct *handle,
2223 TALLOC_CTX *mem_ctx,
2224 struct files_struct *dirfsp,
2225 struct smb_filename *smb_fname,
2226 struct referral **ppreflist,
2227 size_t *preferral_count)
2229 NTSTATUS status = NT_STATUS_NO_MEMORY;
2231 char *link_target = NULL;
2234 #if defined(HAVE_BROKEN_READLINK)
2235 char link_target_buf[PATH_MAX];
2237 char link_target_buf[7];
2242 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
2244 if (is_named_stream(smb_fname)) {
2245 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2249 if (ppreflist == NULL && preferral_count == NULL) {
2251 * We're only checking if this is a DFS
2252 * redirect. We don't need to return data.
2254 bufsize = sizeof(link_target_buf);
2255 link_target = link_target_buf;
2258 link_target = talloc_array(mem_ctx, char, bufsize);
2264 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
2266 status = map_nt_error_from_unix(errno);
2270 referral_len = glfs_readlink(handle->data,
2271 smb_fname->base_name,
2274 if (referral_len < 0) {
2275 if (errno == EINVAL) {
2276 DBG_INFO("%s is not a link.\n", smb_fname->base_name);
2277 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2279 status = map_nt_error_from_unix(errno);
2280 DBG_ERR("Error reading "
2281 "msdfs link %s: %s\n",
2282 smb_fname->base_name,
2287 link_target[referral_len] = '\0';
2289 DBG_INFO("%s -> %s\n",
2290 smb_fname->base_name,
2293 if (!strnequal(link_target, "msdfs:", 6)) {
2294 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2298 if (ppreflist == NULL && preferral_count == NULL) {
2299 /* Early return for checking if this is a DFS link. */
2300 smb_stat_ex_from_stat(&smb_fname->st, &st);
2301 return NT_STATUS_OK;
2304 ok = parse_msdfs_symlink(mem_ctx,
2305 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
2311 smb_stat_ex_from_stat(&smb_fname->st, &st);
2312 status = NT_STATUS_OK;
2314 status = NT_STATUS_NO_MEMORY;
2319 if (link_target != link_target_buf) {
2320 TALLOC_FREE(link_target);
2325 static struct vfs_fn_pointers glusterfs_fns = {
2327 /* Disk Operations */
2329 .connect_fn = vfs_gluster_connect,
2330 .disconnect_fn = vfs_gluster_disconnect,
2331 .disk_free_fn = vfs_gluster_disk_free,
2332 .get_quota_fn = vfs_gluster_get_quota,
2333 .set_quota_fn = vfs_gluster_set_quota,
2334 .statvfs_fn = vfs_gluster_statvfs,
2335 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
2337 .get_dfs_referrals_fn = NULL,
2339 /* Directory Operations */
2341 .fdopendir_fn = vfs_gluster_fdopendir,
2342 .readdir_fn = vfs_gluster_readdir,
2343 .seekdir_fn = vfs_gluster_seekdir,
2344 .telldir_fn = vfs_gluster_telldir,
2345 .rewind_dir_fn = vfs_gluster_rewinddir,
2346 .mkdirat_fn = vfs_gluster_mkdirat,
2347 .closedir_fn = vfs_gluster_closedir,
2349 /* File Operations */
2351 .openat_fn = vfs_gluster_openat,
2352 .create_file_fn = NULL,
2353 .close_fn = vfs_gluster_close,
2354 .pread_fn = vfs_gluster_pread,
2355 .pread_send_fn = vfs_gluster_pread_send,
2356 .pread_recv_fn = vfs_gluster_pread_recv,
2357 .pwrite_fn = vfs_gluster_pwrite,
2358 .pwrite_send_fn = vfs_gluster_pwrite_send,
2359 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
2360 .lseek_fn = vfs_gluster_lseek,
2361 .sendfile_fn = vfs_gluster_sendfile,
2362 .recvfile_fn = vfs_gluster_recvfile,
2363 .renameat_fn = vfs_gluster_renameat,
2364 .fsync_send_fn = vfs_gluster_fsync_send,
2365 .fsync_recv_fn = vfs_gluster_fsync_recv,
2367 .stat_fn = vfs_gluster_stat,
2368 .fstat_fn = vfs_gluster_fstat,
2369 .lstat_fn = vfs_gluster_lstat,
2370 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
2371 .unlinkat_fn = vfs_gluster_unlinkat,
2373 .fchmod_fn = vfs_gluster_fchmod,
2374 .fchown_fn = vfs_gluster_fchown,
2375 .lchown_fn = vfs_gluster_lchown,
2376 .chdir_fn = vfs_gluster_chdir,
2377 .getwd_fn = vfs_gluster_getwd,
2378 .ntimes_fn = vfs_gluster_ntimes,
2379 .fntimes_fn = vfs_gluster_fntimes,
2380 .ftruncate_fn = vfs_gluster_ftruncate,
2381 .fallocate_fn = vfs_gluster_fallocate,
2382 .lock_fn = vfs_gluster_lock,
2383 .kernel_flock_fn = vfs_gluster_kernel_flock,
2384 .fcntl_fn = vfs_gluster_fcntl,
2385 .linux_setlease_fn = vfs_gluster_linux_setlease,
2386 .getlock_fn = vfs_gluster_getlock,
2387 .symlinkat_fn = vfs_gluster_symlinkat,
2388 .readlinkat_fn = vfs_gluster_readlinkat,
2389 .linkat_fn = vfs_gluster_linkat,
2390 .mknodat_fn = vfs_gluster_mknodat,
2391 .realpath_fn = vfs_gluster_realpath,
2392 .chflags_fn = vfs_gluster_chflags,
2393 .file_id_create_fn = NULL,
2394 .streaminfo_fn = NULL,
2395 .get_real_filename_fn = vfs_gluster_get_real_filename,
2396 .connectpath_fn = vfs_gluster_connectpath,
2397 .create_dfs_pathat_fn = vfs_gluster_create_dfs_pathat,
2398 .read_dfs_pathat_fn = vfs_gluster_read_dfs_pathat,
2400 .brl_lock_windows_fn = NULL,
2401 .brl_unlock_windows_fn = NULL,
2402 .strict_lock_check_fn = NULL,
2403 .translate_name_fn = NULL,
2406 /* NT ACL Operations */
2407 .fget_nt_acl_fn = NULL,
2408 .get_nt_acl_at_fn = NULL,
2409 .fset_nt_acl_fn = NULL,
2410 .audit_file_fn = NULL,
2412 /* Posix ACL Operations */
2413 .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
2414 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
2415 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
2416 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2417 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
2418 .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
2421 .getxattr_fn = vfs_gluster_getxattr,
2422 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2423 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2424 .fgetxattr_fn = vfs_gluster_fgetxattr,
2425 .flistxattr_fn = vfs_gluster_flistxattr,
2426 .fremovexattr_fn = vfs_gluster_fremovexattr,
2427 .fsetxattr_fn = vfs_gluster_fsetxattr,
2429 /* AIO Operations */
2430 .aio_force_fn = vfs_gluster_aio_force,
2432 /* Durable handle Operations */
2433 .durable_cookie_fn = NULL,
2434 .durable_disconnect_fn = NULL,
2435 .durable_reconnect_fn = NULL,
2439 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
2441 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2442 "glusterfs", &glusterfs_fns);