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;
75 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
76 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
77 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
78 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
82 /* pre-opened glfs_t */
84 static struct glfs_preopened {
89 struct glfs_preopened *next, *prev;
93 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
95 struct glfs_preopened *entry = NULL;
97 entry = talloc_zero(NULL, struct glfs_preopened);
103 entry->volume = talloc_strdup(entry, volume);
104 if (!entry->volume) {
110 entry->connectpath = talloc_strdup(entry, connectpath);
111 if (entry->connectpath == NULL) {
120 DLIST_ADD(glfs_preopened, entry);
125 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
127 struct glfs_preopened *entry = NULL;
129 for (entry = glfs_preopened; entry; entry = entry->next) {
130 if (strcmp(entry->volume, volume) == 0 &&
131 strcmp(entry->connectpath, connectpath) == 0)
141 static void glfs_clear_preopened(glfs_t *fs)
143 struct glfs_preopened *entry = NULL;
145 for (entry = glfs_preopened; entry; entry = entry->next) {
146 if (entry->fs == fs) {
150 DLIST_REMOVE(glfs_preopened, entry);
152 glfs_fini(entry->fs);
158 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
159 const char *volfile_servers)
162 size_t server_count = 0;
163 size_t server_success = 0;
165 TALLOC_CTX *frame = talloc_stackframe();
167 DBG_INFO("servers list %s\n", volfile_servers);
169 while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
170 char *transport = NULL;
175 DBG_INFO("server %zu %s\n", server_count, server);
177 /* Determine the transport type */
178 if (strncmp(server, "unix+", 5) == 0) {
180 transport = talloc_strdup(frame, "unix");
185 host = talloc_strdup(frame, server + 5);
192 char *port_index = NULL;
194 if (strncmp(server, "tcp+", 4) == 0) {
198 /* IPv6 is enclosed in []
199 * ':' before ']' is part of IPv6
200 * ':' after ']' indicates port
203 if (server[0] == '[') {
205 p = index(server, ']');
214 port_index = index(p, ':');
216 if (port_index == NULL) {
219 port = atoi(port_index + 1);
220 port_index[0] = '\0';
222 transport = talloc_strdup(frame, "tcp");
227 host = talloc_strdup(frame, server);
234 DBG_INFO("Calling set volfile server with params "
235 "transport=%s, host=%s, port=%d\n", transport,
238 ret = glfs_set_volfile_server(fs, transport, host, port);
240 DBG_WARNING("Failed to set volfile_server "
241 "transport=%s, host=%s, port=%d (%s)\n",
242 transport, host, port, strerror(errno));
249 if (server_count == 0) {
251 } else if (server_success < server_count) {
252 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
253 server_count - server_success, server_count);
261 /* Disk Operations */
263 static int check_for_write_behind_translator(TALLOC_CTX *mem_ctx,
272 bool write_behind_present = false;
276 ret = glfs_get_volfile(fs, NULL, 0);
278 DBG_ERR("%s: Failed to get volfile for "
279 "volume (%s): No volfile\n",
285 DBG_ERR("%s: Invalid return %d for glfs_get_volfile for "
286 "volume (%s): No volfile\n",
295 buf = talloc_zero_array(mem_ctx, char, newlen);
300 ret = glfs_get_volfile(fs, buf, newlen);
303 DBG_ERR("%s: Failed to get volfile for volume (%s)\n",
304 volume, strerror(errno));
308 option = talloc_asprintf(mem_ctx, "volume %s-write-behind", volume);
309 if (option == NULL) {
315 * file_lines_parse() plays horrible tricks with
316 * the passed-in talloc pointers and the hierarcy
317 * which makes freeing hard to get right.
319 * As we know mem_ctx is freed by the caller, after
320 * this point don't free on exit and let the caller
321 * handle it. This violates good Samba coding practice
322 * but we know we're not leaking here.
325 lines = file_lines_parse(buf,
329 if (lines == NULL || numlines <= 0) {
332 /* On success, buf is now a talloc child of lines !! */
334 for (i=0; i < numlines; i++) {
335 if (strequal(lines[i], option)) {
336 write_behind_present = true;
341 if (write_behind_present) {
342 DBG_ERR("Write behind translator is enabled for "
343 "volume (%s), refusing to connect! "
344 "Please turn off the write behind translator by calling "
345 "'gluster volume set %s performance.write-behind off' "
346 "on the commandline. "
347 "Check the vfs_glusterfs(8) manpage for "
348 "further details.\n",
356 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
360 const struct loadparm_substitution *lp_sub =
361 loadparm_s3_global_substitution();
362 const char *volfile_servers;
369 bool write_behind_pass_through_set = false;
371 tmp_ctx = talloc_new(NULL);
372 if (tmp_ctx == NULL) {
376 logfile = lp_parm_substituted_string(tmp_ctx,
383 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
385 volfile_servers = lp_parm_substituted_string(tmp_ctx,
391 if (volfile_servers == NULL) {
392 volfile_servers = DEFAULT_VOLFILE_SERVER;
395 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
397 if (volume == NULL) {
401 fs = glfs_find_preopened(volume, handle->conn->connectpath);
406 fs = glfs_new(volume);
412 ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
414 DBG_ERR("Failed to set volfile_servers from list %s\n",
419 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
422 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
426 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-selinux",
429 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
433 ret = glfs_set_xlator_option(fs, "*-snapview-client",
434 "snapdir-entry-path",
435 handle->conn->connectpath);
437 DEBUG(0, ("%s: Failed to set xlator option:"
438 " snapdir-entry-path\n", volume));
442 #ifdef HAVE_GFAPI_VER_7_9
443 ret = glfs_set_xlator_option(fs, "*-write-behind", "pass-through",
446 DBG_ERR("%s: Failed to set xlator option: pass-through\n",
450 write_behind_pass_through_set = true;
453 ret = glfs_set_logging(fs, logfile, loglevel);
455 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
456 volume, logfile, loglevel));
462 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
463 volume, strerror(errno)));
467 if (!write_behind_pass_through_set) {
468 ret = check_for_write_behind_translator(tmp_ctx, fs, volume);
474 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
476 DEBUG(0, ("%s: Failed to register volume (%s)\n",
477 volume, strerror(errno)));
482 * The shadow_copy2 module will fail to export subdirectories
483 * of a gluster volume unless we specify the mount point,
484 * because the detection fails if the file system is not
486 * https://bugzilla.samba.org/show_bug.cgi?id=13091
488 lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
491 * Unless we have an async implementation of getxattrat turn this off.
493 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
500 DBG_ERR("%s: Initialized volume from servers %s\n",
501 volume, volfile_servers);
504 talloc_free(tmp_ctx);
508 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
514 glfs_clear_preopened(fs);
517 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
518 const struct smb_filename *smb_fname,
523 struct statvfs statvfs = { 0, };
526 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
531 if (bsize_p != NULL) {
532 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
534 if (dfree_p != NULL) {
535 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
537 if (dsize_p != NULL) {
538 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
541 return (uint64_t)statvfs.f_bavail;
544 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
545 const struct smb_filename *smb_fname,
546 enum SMB_QUOTA_TYPE qtype,
555 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
556 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
562 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
563 const struct smb_filename *smb_fname,
564 struct vfs_statvfs_struct *vfs_statvfs)
566 struct statvfs statvfs = { 0, };
569 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
571 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
572 smb_fname->base_name, strerror(errno)));
576 ZERO_STRUCTP(vfs_statvfs);
578 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
579 vfs_statvfs->BlockSize = statvfs.f_bsize;
580 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
581 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
582 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
583 vfs_statvfs->TotalFileNodes = statvfs.f_files;
584 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
585 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
586 vfs_statvfs->FsCapabilities =
587 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
592 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
593 enum timestamp_set_resolution *p_ts_res)
595 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
597 #ifdef HAVE_GFAPI_VER_6
598 caps |= FILE_SUPPORTS_SPARSE_FILES;
601 #ifdef STAT_HAVE_NSEC
602 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
608 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
611 glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
613 DBG_INFO("Failed to fetch fsp extension\n");
617 DBG_INFO("Empty glfs_fd_t pointer\n");
624 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
625 files_struct *fsp, const char *mask,
628 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
630 DBG_ERR("Failed to fetch gluster fd\n");
637 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
641 START_PROFILE(syscall_closedir);
642 ret = glfs_closedir((void *)dirp);
643 END_PROFILE(syscall_closedir);
648 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
649 struct files_struct *dirfsp,
651 SMB_STRUCT_STAT *sbuf)
653 static char direntbuf[512];
656 struct dirent *dirent = 0;
658 START_PROFILE(syscall_readdir);
660 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
663 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
666 if ((ret < 0) || (dirent == NULL)) {
667 END_PROFILE(syscall_readdir);
672 SET_STAT_INVALID(*sbuf);
673 if (!S_ISLNK(stat.st_mode)) {
674 smb_stat_ex_from_stat(sbuf, &stat);
678 END_PROFILE(syscall_readdir);
682 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
686 START_PROFILE(syscall_telldir);
687 ret = glfs_telldir((void *)dirp);
688 END_PROFILE(syscall_telldir);
693 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
696 START_PROFILE(syscall_seekdir);
697 glfs_seekdir((void *)dirp, offset);
698 END_PROFILE(syscall_seekdir);
701 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
703 START_PROFILE(syscall_rewinddir);
704 glfs_seekdir((void *)dirp, 0);
705 END_PROFILE(syscall_rewinddir);
708 static int vfs_gluster_mkdirat(struct vfs_handle_struct *handle,
709 struct files_struct *dirfsp,
710 const struct smb_filename *smb_fname,
713 struct smb_filename *full_fname = NULL;
716 START_PROFILE(syscall_mkdirat);
718 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
721 if (full_fname == NULL) {
722 END_PROFILE(syscall_mkdirat);
726 ret = glfs_mkdir(handle->data, full_fname->base_name, mode);
728 TALLOC_FREE(full_fname);
730 END_PROFILE(syscall_mkdirat);
735 static int vfs_gluster_openat(struct vfs_handle_struct *handle,
736 const struct files_struct *dirfsp,
737 const struct smb_filename *smb_fname,
742 struct smb_filename *name = NULL;
743 bool became_root = false;
747 START_PROFILE(syscall_openat);
750 * Looks like glfs API doesn't have openat().
752 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
753 name = full_path_from_dirfsp_atname(talloc_tos(),
762 p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
765 END_PROFILE(syscall_openat);
770 if (fsp->fsp_flags.is_pathref) {
772 * ceph doesn't support O_PATH so we have to fallback to
779 if (flags & O_DIRECTORY) {
780 glfd = glfs_opendir(handle->data, smb_fname->base_name);
781 } else if (flags & O_CREAT) {
782 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
785 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
792 fsp->fsp_flags.have_proc_fds = false;
796 END_PROFILE(syscall_openat);
797 /* no extension destroy_fn, so no need to save errno */
798 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
805 END_PROFILE(syscall_openat);
806 /* An arbitrary value for error reporting, so you know its us. */
810 static int vfs_gluster_close(struct vfs_handle_struct *handle,
814 glfs_fd_t *glfd = NULL;
816 START_PROFILE(syscall_close);
818 glfd = vfs_gluster_fetch_glfd(handle, fsp);
820 END_PROFILE(syscall_close);
821 DBG_ERR("Failed to fetch gluster fd\n");
825 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
827 ret = glfs_close(glfd);
828 END_PROFILE(syscall_close);
833 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
834 files_struct *fsp, void *data, size_t n,
838 glfs_fd_t *glfd = NULL;
840 START_PROFILE_BYTES(syscall_pread, n);
842 glfd = vfs_gluster_fetch_glfd(handle, fsp);
844 END_PROFILE_BYTES(syscall_pread);
845 DBG_ERR("Failed to fetch gluster fd\n");
849 #ifdef HAVE_GFAPI_VER_7_6
850 ret = glfs_pread(glfd, data, n, offset, 0, NULL);
852 ret = glfs_pread(glfd, data, n, offset, 0);
854 END_PROFILE_BYTES(syscall_pread);
859 struct vfs_gluster_pread_state {
866 struct vfs_aio_state vfs_aio_state;
867 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
870 static void vfs_gluster_pread_do(void *private_data);
871 static void vfs_gluster_pread_done(struct tevent_req *subreq);
872 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state);
874 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
875 *handle, TALLOC_CTX *mem_ctx,
876 struct tevent_context *ev,
878 void *data, size_t n,
881 struct vfs_gluster_pread_state *state;
882 struct tevent_req *req, *subreq;
884 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
886 DBG_ERR("Failed to fetch gluster fd\n");
890 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pread_state);
899 state->offset = offset;
901 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
902 state->profile_bytes, n);
903 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
905 subreq = pthreadpool_tevent_job_send(
906 state, ev, handle->conn->sconn->pool,
907 vfs_gluster_pread_do, state);
908 if (tevent_req_nomem(subreq, req)) {
909 return tevent_req_post(req, ev);
911 tevent_req_set_callback(subreq, vfs_gluster_pread_done, req);
913 talloc_set_destructor(state, vfs_gluster_pread_state_destructor);
918 static void vfs_gluster_pread_do(void *private_data)
920 struct vfs_gluster_pread_state *state = talloc_get_type_abort(
921 private_data, struct vfs_gluster_pread_state);
922 struct timespec start_time;
923 struct timespec end_time;
925 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
927 PROFILE_TIMESTAMP(&start_time);
930 #ifdef HAVE_GFAPI_VER_7_6
931 state->ret = glfs_pread(state->fd, state->buf, state->count,
932 state->offset, 0, NULL);
934 state->ret = glfs_pread(state->fd, state->buf, state->count,
937 } while ((state->ret == -1) && (errno == EINTR));
939 if (state->ret == -1) {
940 state->vfs_aio_state.error = errno;
943 PROFILE_TIMESTAMP(&end_time);
945 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
947 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
950 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state)
955 static void vfs_gluster_pread_done(struct tevent_req *subreq)
957 struct tevent_req *req = tevent_req_callback_data(
958 subreq, struct tevent_req);
959 struct vfs_gluster_pread_state *state = tevent_req_data(
960 req, struct vfs_gluster_pread_state);
963 ret = pthreadpool_tevent_job_recv(subreq);
965 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
966 talloc_set_destructor(state, NULL);
969 tevent_req_error(req, ret);
973 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
974 * means the lower level pthreadpool failed to create a new
975 * thread. Fallback to sync processing in that case to allow
976 * some progress for the client.
978 vfs_gluster_pread_do(state);
981 tevent_req_done(req);
984 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req,
985 struct vfs_aio_state *vfs_aio_state)
987 struct vfs_gluster_pread_state *state = tevent_req_data(
988 req, struct vfs_gluster_pread_state);
990 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
994 *vfs_aio_state = state->vfs_aio_state;
998 struct vfs_gluster_pwrite_state {
1005 struct vfs_aio_state vfs_aio_state;
1006 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1009 static void vfs_gluster_pwrite_do(void *private_data);
1010 static void vfs_gluster_pwrite_done(struct tevent_req *subreq);
1011 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state);
1013 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
1014 *handle, TALLOC_CTX *mem_ctx,
1015 struct tevent_context *ev,
1017 const void *data, size_t n,
1020 struct tevent_req *req, *subreq;
1021 struct vfs_gluster_pwrite_state *state;
1023 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1025 DBG_ERR("Failed to fetch gluster fd\n");
1029 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pwrite_state);
1038 state->offset = offset;
1040 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
1041 state->profile_bytes, n);
1042 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1044 subreq = pthreadpool_tevent_job_send(
1045 state, ev, handle->conn->sconn->pool,
1046 vfs_gluster_pwrite_do, state);
1047 if (tevent_req_nomem(subreq, req)) {
1048 return tevent_req_post(req, ev);
1050 tevent_req_set_callback(subreq, vfs_gluster_pwrite_done, req);
1052 talloc_set_destructor(state, vfs_gluster_pwrite_state_destructor);
1057 static void vfs_gluster_pwrite_do(void *private_data)
1059 struct vfs_gluster_pwrite_state *state = talloc_get_type_abort(
1060 private_data, struct vfs_gluster_pwrite_state);
1061 struct timespec start_time;
1062 struct timespec end_time;
1064 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1066 PROFILE_TIMESTAMP(&start_time);
1069 #ifdef HAVE_GFAPI_VER_7_6
1070 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1071 state->offset, 0, NULL, NULL);
1073 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1076 } while ((state->ret == -1) && (errno == EINTR));
1078 if (state->ret == -1) {
1079 state->vfs_aio_state.error = errno;
1082 PROFILE_TIMESTAMP(&end_time);
1084 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1086 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1089 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state)
1094 static void vfs_gluster_pwrite_done(struct tevent_req *subreq)
1096 struct tevent_req *req = tevent_req_callback_data(
1097 subreq, struct tevent_req);
1098 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1099 req, struct vfs_gluster_pwrite_state);
1102 ret = pthreadpool_tevent_job_recv(subreq);
1103 TALLOC_FREE(subreq);
1104 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1105 talloc_set_destructor(state, NULL);
1107 if (ret != EAGAIN) {
1108 tevent_req_error(req, ret);
1112 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1113 * means the lower level pthreadpool failed to create a new
1114 * thread. Fallback to sync processing in that case to allow
1115 * some progress for the client.
1117 vfs_gluster_pwrite_do(state);
1120 tevent_req_done(req);
1123 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req,
1124 struct vfs_aio_state *vfs_aio_state)
1126 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1127 req, struct vfs_gluster_pwrite_state);
1129 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1133 *vfs_aio_state = state->vfs_aio_state;
1138 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
1139 files_struct *fsp, const void *data,
1140 size_t n, off_t offset)
1143 glfs_fd_t *glfd = NULL;
1145 START_PROFILE_BYTES(syscall_pwrite, n);
1147 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1149 END_PROFILE_BYTES(syscall_pwrite);
1150 DBG_ERR("Failed to fetch gluster fd\n");
1154 #ifdef HAVE_GFAPI_VER_7_6
1155 ret = glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
1157 ret = glfs_pwrite(glfd, data, n, offset, 0);
1159 END_PROFILE_BYTES(syscall_pwrite);
1164 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
1165 files_struct *fsp, off_t offset, int whence)
1168 glfs_fd_t *glfd = NULL;
1170 START_PROFILE(syscall_lseek);
1172 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1174 END_PROFILE(syscall_lseek);
1175 DBG_ERR("Failed to fetch gluster fd\n");
1179 ret = glfs_lseek(glfd, offset, whence);
1180 END_PROFILE(syscall_lseek);
1185 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
1186 files_struct *fromfsp,
1187 const DATA_BLOB *hdr,
1188 off_t offset, size_t n)
1194 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1195 int fromfd, files_struct *tofsp,
1196 off_t offset, size_t n)
1202 static int vfs_gluster_renameat(struct vfs_handle_struct *handle,
1203 files_struct *srcfsp,
1204 const struct smb_filename *smb_fname_src,
1205 files_struct *dstfsp,
1206 const struct smb_filename *smb_fname_dst)
1208 struct smb_filename *full_fname_src = NULL;
1209 struct smb_filename *full_fname_dst = NULL;
1212 START_PROFILE(syscall_renameat);
1214 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1217 if (full_fname_src == NULL) {
1219 END_PROFILE(syscall_renameat);
1222 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1225 if (full_fname_dst == NULL) {
1226 TALLOC_FREE(full_fname_src);
1228 END_PROFILE(syscall_renameat);
1231 ret = glfs_rename(handle->data,
1232 full_fname_src->base_name,
1233 full_fname_dst->base_name);
1234 TALLOC_FREE(full_fname_src);
1235 TALLOC_FREE(full_fname_dst);
1236 END_PROFILE(syscall_renameat);
1241 struct vfs_gluster_fsync_state {
1245 struct vfs_aio_state vfs_aio_state;
1246 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1249 static void vfs_gluster_fsync_do(void *private_data);
1250 static void vfs_gluster_fsync_done(struct tevent_req *subreq);
1251 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state);
1253 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1254 *handle, TALLOC_CTX *mem_ctx,
1255 struct tevent_context *ev,
1258 struct tevent_req *req, *subreq;
1259 struct vfs_gluster_fsync_state *state;
1261 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1263 DBG_ERR("Failed to fetch gluster fd\n");
1267 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_fsync_state);
1275 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
1276 state->profile_bytes, 0);
1277 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1279 subreq = pthreadpool_tevent_job_send(
1280 state, ev, handle->conn->sconn->pool, vfs_gluster_fsync_do, state);
1281 if (tevent_req_nomem(subreq, req)) {
1282 return tevent_req_post(req, ev);
1284 tevent_req_set_callback(subreq, vfs_gluster_fsync_done, req);
1286 talloc_set_destructor(state, vfs_gluster_fsync_state_destructor);
1291 static void vfs_gluster_fsync_do(void *private_data)
1293 struct vfs_gluster_fsync_state *state = talloc_get_type_abort(
1294 private_data, struct vfs_gluster_fsync_state);
1295 struct timespec start_time;
1296 struct timespec end_time;
1298 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1300 PROFILE_TIMESTAMP(&start_time);
1303 #ifdef HAVE_GFAPI_VER_7_6
1304 state->ret = glfs_fsync(state->fd, NULL, NULL);
1306 state->ret = glfs_fsync(state->fd);
1308 } while ((state->ret == -1) && (errno == EINTR));
1310 if (state->ret == -1) {
1311 state->vfs_aio_state.error = errno;
1314 PROFILE_TIMESTAMP(&end_time);
1316 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1318 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1321 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state)
1326 static void vfs_gluster_fsync_done(struct tevent_req *subreq)
1328 struct tevent_req *req = tevent_req_callback_data(
1329 subreq, struct tevent_req);
1330 struct vfs_gluster_fsync_state *state = tevent_req_data(
1331 req, struct vfs_gluster_fsync_state);
1334 ret = pthreadpool_tevent_job_recv(subreq);
1335 TALLOC_FREE(subreq);
1336 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1337 talloc_set_destructor(state, NULL);
1339 if (ret != EAGAIN) {
1340 tevent_req_error(req, ret);
1344 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1345 * means the lower level pthreadpool failed to create a new
1346 * thread. Fallback to sync processing in that case to allow
1347 * some progress for the client.
1349 vfs_gluster_fsync_do(state);
1352 tevent_req_done(req);
1355 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1356 struct vfs_aio_state *vfs_aio_state)
1358 struct vfs_gluster_fsync_state *state = tevent_req_data(
1359 req, struct vfs_gluster_fsync_state);
1361 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1365 *vfs_aio_state = state->vfs_aio_state;
1369 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1370 struct smb_filename *smb_fname)
1375 START_PROFILE(syscall_stat);
1376 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1378 smb_stat_ex_from_stat(&smb_fname->st, &st);
1380 if (ret < 0 && errno != ENOENT) {
1381 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1382 smb_fname->base_name, strerror(errno)));
1384 END_PROFILE(syscall_stat);
1389 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1390 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1394 glfs_fd_t *glfd = NULL;
1396 START_PROFILE(syscall_fstat);
1398 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1400 END_PROFILE(syscall_fstat);
1401 DBG_ERR("Failed to fetch gluster fd\n");
1405 ret = glfs_fstat(glfd, &st);
1407 smb_stat_ex_from_stat(sbuf, &st);
1410 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1411 fsp_get_io_fd(fsp), strerror(errno)));
1413 END_PROFILE(syscall_fstat);
1418 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1419 struct smb_filename *smb_fname)
1424 START_PROFILE(syscall_lstat);
1425 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1427 smb_stat_ex_from_stat(&smb_fname->st, &st);
1429 if (ret < 0 && errno != ENOENT) {
1430 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1431 smb_fname->base_name, strerror(errno)));
1433 END_PROFILE(syscall_lstat);
1438 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1440 const SMB_STRUCT_STAT *sbuf)
1444 START_PROFILE(syscall_get_alloc_size);
1445 ret = sbuf->st_ex_blocks * 512;
1446 END_PROFILE(syscall_get_alloc_size);
1451 static int vfs_gluster_unlinkat(struct vfs_handle_struct *handle,
1452 struct files_struct *dirfsp,
1453 const struct smb_filename *smb_fname,
1456 struct smb_filename *full_fname = NULL;
1459 START_PROFILE(syscall_unlinkat);
1461 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1464 if (full_fname == NULL) {
1465 END_PROFILE(syscall_unlinkat);
1469 if (flags & AT_REMOVEDIR) {
1470 ret = glfs_rmdir(handle->data, full_fname->base_name);
1472 ret = glfs_unlink(handle->data, full_fname->base_name);
1474 TALLOC_FREE(full_fname);
1475 END_PROFILE(syscall_unlinkat);
1480 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1481 files_struct *fsp, mode_t mode)
1484 glfs_fd_t *glfd = NULL;
1486 START_PROFILE(syscall_fchmod);
1488 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1490 END_PROFILE(syscall_fchmod);
1491 DBG_ERR("Failed to fetch gluster fd\n");
1495 if (!fsp->fsp_flags.is_pathref) {
1497 * We can use an io_fd to remove xattrs.
1499 ret = glfs_fchmod(glfd, mode);
1502 * This is no longer a handle based call.
1504 ret = glfs_chmod(handle->data, fsp->fsp_name->base_name, mode);
1506 END_PROFILE(syscall_fchmod);
1511 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1512 files_struct *fsp, uid_t uid, gid_t gid)
1515 glfs_fd_t *glfd = NULL;
1517 START_PROFILE(syscall_fchown);
1519 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1521 END_PROFILE(syscall_fchown);
1522 DBG_ERR("Failed to fetch gluster fd\n");
1526 ret = glfs_fchown(glfd, uid, gid);
1527 END_PROFILE(syscall_fchown);
1532 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1533 const struct smb_filename *smb_fname,
1539 START_PROFILE(syscall_lchown);
1540 ret = glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1541 END_PROFILE(syscall_lchown);
1546 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1547 const struct smb_filename *smb_fname)
1551 START_PROFILE(syscall_chdir);
1552 ret = glfs_chdir(handle->data, smb_fname->base_name);
1553 END_PROFILE(syscall_chdir);
1558 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1561 char cwd[PATH_MAX] = { '\0' };
1563 struct smb_filename *smb_fname = NULL;
1565 START_PROFILE(syscall_getwd);
1567 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1568 END_PROFILE(syscall_getwd);
1573 smb_fname = synthetic_smb_fname(ctx,
1582 static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
1584 struct smb_file_time *ft)
1587 struct timespec times[2];
1588 glfs_fd_t *glfd = NULL;
1590 START_PROFILE(syscall_fntimes);
1592 if (is_omit_timespec(&ft->atime)) {
1593 times[0].tv_sec = fsp->fsp_name->st.st_ex_atime.tv_sec;
1594 times[0].tv_nsec = fsp->fsp_name->st.st_ex_atime.tv_nsec;
1596 times[0].tv_sec = ft->atime.tv_sec;
1597 times[0].tv_nsec = ft->atime.tv_nsec;
1600 if (is_omit_timespec(&ft->mtime)) {
1601 times[1].tv_sec = fsp->fsp_name->st.st_ex_mtime.tv_sec;
1602 times[1].tv_nsec = fsp->fsp_name->st.st_ex_mtime.tv_nsec;
1604 times[1].tv_sec = ft->mtime.tv_sec;
1605 times[1].tv_nsec = ft->mtime.tv_nsec;
1608 if ((timespec_compare(×[0],
1609 &fsp->fsp_name->st.st_ex_atime) == 0) &&
1610 (timespec_compare(×[1],
1611 &fsp->fsp_name->st.st_ex_mtime) == 0)) {
1612 END_PROFILE(syscall_fntimes);
1616 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1618 END_PROFILE(syscall_fntimes);
1619 DBG_ERR("Failed to fetch gluster fd\n");
1623 ret = glfs_futimens(glfd, times);
1624 END_PROFILE(syscall_fntimes);
1629 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1630 files_struct *fsp, off_t offset)
1633 glfs_fd_t *glfd = NULL;
1635 START_PROFILE(syscall_ftruncate);
1637 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1639 END_PROFILE(syscall_ftruncate);
1640 DBG_ERR("Failed to fetch gluster fd\n");
1644 #ifdef HAVE_GFAPI_VER_7_6
1645 ret = glfs_ftruncate(glfd, offset, NULL, NULL);
1647 ret = glfs_ftruncate(glfd, offset);
1649 END_PROFILE(syscall_ftruncate);
1654 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1655 struct files_struct *fsp,
1657 off_t offset, off_t len)
1660 #ifdef HAVE_GFAPI_VER_6
1661 glfs_fd_t *glfd = NULL;
1662 int keep_size, punch_hole;
1664 START_PROFILE(syscall_fallocate);
1666 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1668 END_PROFILE(syscall_fallocate);
1669 DBG_ERR("Failed to fetch gluster fd\n");
1673 keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1674 punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1676 mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1678 END_PROFILE(syscall_fallocate);
1684 ret = glfs_discard(glfd, offset, len);
1686 DBG_DEBUG("glfs_discard failed: %s\n",
1691 ret = glfs_fallocate(glfd, keep_size, offset, len);
1692 END_PROFILE(syscall_fallocate);
1700 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1702 const struct smb_filename *smb_fname)
1704 char *result = NULL;
1705 struct smb_filename *result_fname = NULL;
1706 char *resolved_path = NULL;
1708 START_PROFILE(syscall_realpath);
1710 resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1711 if (resolved_path == NULL) {
1712 END_PROFILE(syscall_realpath);
1717 result = glfs_realpath(handle->data,
1718 smb_fname->base_name,
1720 if (result != NULL) {
1721 result_fname = synthetic_smb_fname(ctx,
1729 SAFE_FREE(resolved_path);
1730 END_PROFILE(syscall_realpath);
1732 return result_fname;
1735 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1736 files_struct *fsp, int op, off_t offset,
1737 off_t count, int type)
1739 struct flock flock = { 0, };
1741 glfs_fd_t *glfd = NULL;
1744 START_PROFILE(syscall_fcntl_lock);
1746 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1748 DBG_ERR("Failed to fetch gluster fd\n");
1753 flock.l_type = type;
1754 flock.l_whence = SEEK_SET;
1755 flock.l_start = offset;
1756 flock.l_len = count;
1759 ret = glfs_posix_lock(glfd, op, &flock);
1761 if (op == F_GETLK) {
1762 /* lock query, true if someone else has locked */
1764 (flock.l_type != F_UNLCK) &&
1765 (flock.l_pid != 0) && (flock.l_pid != getpid())) {
1781 END_PROFILE(syscall_fcntl_lock);
1786 static int vfs_gluster_filesystem_sharemode(struct vfs_handle_struct *handle,
1788 uint32_t share_access,
1789 uint32_t access_mask)
1795 static int vfs_gluster_fcntl(vfs_handle_struct *handle,
1796 files_struct *fsp, int cmd, va_list cmd_arg)
1799 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1800 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1802 if (cmd == F_GETFL) {
1804 } else if (cmd == F_SETFL) {
1805 va_list dup_cmd_arg;
1808 va_copy(dup_cmd_arg, cmd_arg);
1809 opt = va_arg(dup_cmd_arg, int);
1810 va_end(dup_cmd_arg);
1814 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1817 DBG_ERR("unexpected fcntl: %d\n", cmd);
1823 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1824 files_struct *fsp, int leasetype)
1830 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1831 files_struct *fsp, off_t *poffset,
1832 off_t *pcount, int *ptype, pid_t *ppid)
1834 struct flock flock = { 0, };
1836 glfs_fd_t *glfd = NULL;
1838 START_PROFILE(syscall_fcntl_getlock);
1840 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1842 END_PROFILE(syscall_fcntl_getlock);
1843 DBG_ERR("Failed to fetch gluster fd\n");
1847 flock.l_type = *ptype;
1848 flock.l_whence = SEEK_SET;
1849 flock.l_start = *poffset;
1850 flock.l_len = *pcount;
1853 ret = glfs_posix_lock(glfd, F_GETLK, &flock);
1856 END_PROFILE(syscall_fcntl_getlock);
1860 *ptype = flock.l_type;
1861 *poffset = flock.l_start;
1862 *pcount = flock.l_len;
1863 *ppid = flock.l_pid;
1864 END_PROFILE(syscall_fcntl_getlock);
1869 static int vfs_gluster_symlinkat(struct vfs_handle_struct *handle,
1870 const struct smb_filename *link_target,
1871 struct files_struct *dirfsp,
1872 const struct smb_filename *new_smb_fname)
1874 struct smb_filename *full_fname = NULL;
1877 START_PROFILE(syscall_symlinkat);
1879 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1882 if (full_fname == NULL) {
1883 END_PROFILE(syscall_symlinkat);
1887 ret = glfs_symlink(handle->data,
1888 link_target->base_name,
1889 full_fname->base_name);
1891 TALLOC_FREE(full_fname);
1893 END_PROFILE(syscall_symlinkat);
1898 static int vfs_gluster_readlinkat(struct vfs_handle_struct *handle,
1899 const struct files_struct *dirfsp,
1900 const struct smb_filename *smb_fname,
1904 struct smb_filename *full_fname = NULL;
1907 START_PROFILE(syscall_readlinkat);
1909 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1912 if (full_fname == NULL) {
1913 END_PROFILE(syscall_readlinkat);
1917 ret = glfs_readlink(handle->data, full_fname->base_name, buf, bufsiz);
1919 TALLOC_FREE(full_fname);
1921 END_PROFILE(syscall_readlinkat);
1926 static int vfs_gluster_linkat(struct vfs_handle_struct *handle,
1927 files_struct *srcfsp,
1928 const struct smb_filename *old_smb_fname,
1929 files_struct *dstfsp,
1930 const struct smb_filename *new_smb_fname,
1934 struct smb_filename *full_fname_old = NULL;
1935 struct smb_filename *full_fname_new = NULL;
1937 START_PROFILE(syscall_linkat);
1939 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
1942 if (full_fname_old == NULL) {
1943 END_PROFILE(syscall_linkat);
1946 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
1949 if (full_fname_new == NULL) {
1950 TALLOC_FREE(full_fname_old);
1951 END_PROFILE(syscall_linkat);
1955 ret = glfs_link(handle->data,
1956 full_fname_old->base_name,
1957 full_fname_new->base_name);
1959 TALLOC_FREE(full_fname_old);
1960 TALLOC_FREE(full_fname_new);
1961 END_PROFILE(syscall_linkat);
1966 static int vfs_gluster_mknodat(struct vfs_handle_struct *handle,
1967 files_struct *dirfsp,
1968 const struct smb_filename *smb_fname,
1972 struct smb_filename *full_fname = NULL;
1975 START_PROFILE(syscall_mknodat);
1977 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1980 if (full_fname == NULL) {
1981 END_PROFILE(syscall_mknodat);
1985 ret = glfs_mknod(handle->data, full_fname->base_name, mode, dev);
1987 TALLOC_FREE(full_fname);
1989 END_PROFILE(syscall_mknodat);
1994 static int vfs_gluster_fchflags(struct vfs_handle_struct *handle,
1995 struct files_struct *fsp,
2002 static NTSTATUS vfs_gluster_get_real_filename_at(
2003 struct vfs_handle_struct *handle,
2004 struct files_struct *dirfsp,
2006 TALLOC_CTX *mem_ctx,
2009 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2011 char key_buf[GLUSTER_NAME_MAX + 64];
2012 char val_buf[GLUSTER_NAME_MAX + 1];
2014 if (strlen(name) >= GLUSTER_NAME_MAX) {
2015 return NT_STATUS_OBJECT_NAME_INVALID;
2018 snprintf(key_buf, GLUSTER_NAME_MAX + 64,
2019 "glusterfs.get_real_filename:%s", name);
2021 ret = glfs_fgetxattr(glfd, key_buf, val_buf, GLUSTER_NAME_MAX + 1);
2023 if (errno == ENOATTR) {
2026 return map_nt_error_from_unix(errno);
2029 *found_name = talloc_strdup(mem_ctx, val_buf);
2030 if (found_name[0] == NULL) {
2031 return NT_STATUS_NO_MEMORY;
2033 return NT_STATUS_OK;
2036 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
2037 const struct smb_filename *smb_fname)
2039 return handle->conn->connectpath;
2044 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
2045 files_struct *fsp, const char *name,
2046 void *value, size_t size)
2048 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2050 DBG_ERR("Failed to fetch gluster fd\n");
2054 return glfs_fgetxattr(glfd, name, value, size);
2057 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
2058 files_struct *fsp, char *list,
2061 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2063 DBG_ERR("Failed to fetch gluster fd\n");
2066 if (!fsp->fsp_flags.is_pathref) {
2068 * We can use an io_fd to list xattrs.
2070 return glfs_flistxattr(glfd, list, size);
2073 * This is no longer a handle based call.
2075 return glfs_listxattr(handle->data,
2076 fsp->fsp_name->base_name,
2082 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
2083 files_struct *fsp, const char *name)
2085 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2087 DBG_ERR("Failed to fetch gluster fd\n");
2090 if (!fsp->fsp_flags.is_pathref) {
2092 * We can use an io_fd to remove xattrs.
2094 return glfs_fremovexattr(glfd, name);
2097 * This is no longer a handle based call.
2099 return glfs_removexattr(handle->data,
2100 fsp->fsp_name->base_name,
2105 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
2106 files_struct *fsp, const char *name,
2107 const void *value, size_t size, int flags)
2109 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2111 DBG_ERR("Failed to fetch gluster fd\n");
2115 if (!fsp->fsp_flags.is_pathref) {
2117 * We can use an io_fd to set xattrs.
2119 return glfs_fsetxattr(glfd, name, value, size, flags);
2122 * This is no longer a handle based call.
2124 return glfs_setxattr(handle->data,
2125 fsp->fsp_name->base_name,
2133 /* AIO Operations */
2135 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
2141 static NTSTATUS vfs_gluster_create_dfs_pathat(struct vfs_handle_struct *handle,
2142 struct files_struct *dirfsp,
2143 const struct smb_filename *smb_fname,
2144 const struct referral *reflist,
2145 size_t referral_count)
2147 TALLOC_CTX *frame = talloc_stackframe();
2148 NTSTATUS status = NT_STATUS_NO_MEMORY;
2150 char *msdfs_link = NULL;
2151 struct smb_filename *full_fname = NULL;
2153 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2156 if (full_fname == NULL) {
2160 /* Form the msdfs_link contents */
2161 msdfs_link = msdfs_link_string(frame,
2164 if (msdfs_link == NULL) {
2168 ret = glfs_symlink(handle->data,
2170 full_fname->base_name);
2172 status = NT_STATUS_OK;
2174 status = map_nt_error_from_unix(errno);
2184 * Read and return the contents of a DFS redirect given a
2185 * pathname. A caller can pass in NULL for ppreflist and
2186 * preferral_count but still determine if this was a
2187 * DFS redirect point by getting NT_STATUS_OK back
2188 * without incurring the overhead of reading and parsing
2189 * the referral contents.
2192 static NTSTATUS vfs_gluster_read_dfs_pathat(struct vfs_handle_struct *handle,
2193 TALLOC_CTX *mem_ctx,
2194 struct files_struct *dirfsp,
2195 struct smb_filename *smb_fname,
2196 struct referral **ppreflist,
2197 size_t *preferral_count)
2199 NTSTATUS status = NT_STATUS_NO_MEMORY;
2201 char *link_target = NULL;
2204 #if defined(HAVE_BROKEN_READLINK)
2205 char link_target_buf[PATH_MAX];
2207 char link_target_buf[7];
2210 struct smb_filename *full_fname = NULL;
2213 if (is_named_stream(smb_fname)) {
2214 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2218 if (ppreflist == NULL && preferral_count == NULL) {
2220 * We're only checking if this is a DFS
2221 * redirect. We don't need to return data.
2223 bufsize = sizeof(link_target_buf);
2224 link_target = link_target_buf;
2227 link_target = talloc_array(mem_ctx, char, bufsize);
2233 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2236 if (full_fname == NULL) {
2237 status = NT_STATUS_NO_MEMORY;
2241 ret = glfs_lstat(handle->data, full_fname->base_name, &st);
2243 status = map_nt_error_from_unix(errno);
2247 referral_len = glfs_readlink(handle->data,
2248 full_fname->base_name,
2251 if (referral_len < 0) {
2252 if (errno == EINVAL) {
2253 DBG_INFO("%s is not a link.\n", full_fname->base_name);
2254 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2256 status = map_nt_error_from_unix(errno);
2257 DBG_ERR("Error reading "
2258 "msdfs link %s: %s\n",
2259 full_fname->base_name,
2264 link_target[referral_len] = '\0';
2266 DBG_INFO("%s -> %s\n",
2267 full_fname->base_name,
2270 if (!strnequal(link_target, "msdfs:", 6)) {
2271 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2275 if (ppreflist == NULL && preferral_count == NULL) {
2276 /* Early return for checking if this is a DFS link. */
2277 TALLOC_FREE(full_fname);
2278 smb_stat_ex_from_stat(&smb_fname->st, &st);
2279 return NT_STATUS_OK;
2282 ok = parse_msdfs_symlink(mem_ctx,
2283 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
2289 smb_stat_ex_from_stat(&smb_fname->st, &st);
2290 status = NT_STATUS_OK;
2292 status = NT_STATUS_NO_MEMORY;
2297 if (link_target != link_target_buf) {
2298 TALLOC_FREE(link_target);
2300 TALLOC_FREE(full_fname);
2304 static struct vfs_fn_pointers glusterfs_fns = {
2306 /* Disk Operations */
2308 .connect_fn = vfs_gluster_connect,
2309 .disconnect_fn = vfs_gluster_disconnect,
2310 .disk_free_fn = vfs_gluster_disk_free,
2311 .get_quota_fn = vfs_gluster_get_quota,
2312 .set_quota_fn = vfs_gluster_set_quota,
2313 .statvfs_fn = vfs_gluster_statvfs,
2314 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
2316 .get_dfs_referrals_fn = NULL,
2318 /* Directory Operations */
2320 .fdopendir_fn = vfs_gluster_fdopendir,
2321 .readdir_fn = vfs_gluster_readdir,
2322 .seekdir_fn = vfs_gluster_seekdir,
2323 .telldir_fn = vfs_gluster_telldir,
2324 .rewind_dir_fn = vfs_gluster_rewinddir,
2325 .mkdirat_fn = vfs_gluster_mkdirat,
2326 .closedir_fn = vfs_gluster_closedir,
2328 /* File Operations */
2330 .openat_fn = vfs_gluster_openat,
2331 .create_file_fn = NULL,
2332 .close_fn = vfs_gluster_close,
2333 .pread_fn = vfs_gluster_pread,
2334 .pread_send_fn = vfs_gluster_pread_send,
2335 .pread_recv_fn = vfs_gluster_pread_recv,
2336 .pwrite_fn = vfs_gluster_pwrite,
2337 .pwrite_send_fn = vfs_gluster_pwrite_send,
2338 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
2339 .lseek_fn = vfs_gluster_lseek,
2340 .sendfile_fn = vfs_gluster_sendfile,
2341 .recvfile_fn = vfs_gluster_recvfile,
2342 .renameat_fn = vfs_gluster_renameat,
2343 .fsync_send_fn = vfs_gluster_fsync_send,
2344 .fsync_recv_fn = vfs_gluster_fsync_recv,
2346 .stat_fn = vfs_gluster_stat,
2347 .fstat_fn = vfs_gluster_fstat,
2348 .lstat_fn = vfs_gluster_lstat,
2349 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
2350 .unlinkat_fn = vfs_gluster_unlinkat,
2352 .fchmod_fn = vfs_gluster_fchmod,
2353 .fchown_fn = vfs_gluster_fchown,
2354 .lchown_fn = vfs_gluster_lchown,
2355 .chdir_fn = vfs_gluster_chdir,
2356 .getwd_fn = vfs_gluster_getwd,
2357 .fntimes_fn = vfs_gluster_fntimes,
2358 .ftruncate_fn = vfs_gluster_ftruncate,
2359 .fallocate_fn = vfs_gluster_fallocate,
2360 .lock_fn = vfs_gluster_lock,
2361 .filesystem_sharemode_fn = vfs_gluster_filesystem_sharemode,
2362 .fcntl_fn = vfs_gluster_fcntl,
2363 .linux_setlease_fn = vfs_gluster_linux_setlease,
2364 .getlock_fn = vfs_gluster_getlock,
2365 .symlinkat_fn = vfs_gluster_symlinkat,
2366 .readlinkat_fn = vfs_gluster_readlinkat,
2367 .linkat_fn = vfs_gluster_linkat,
2368 .mknodat_fn = vfs_gluster_mknodat,
2369 .realpath_fn = vfs_gluster_realpath,
2370 .fchflags_fn = vfs_gluster_fchflags,
2371 .file_id_create_fn = NULL,
2372 .fstreaminfo_fn = NULL,
2373 .get_real_filename_at_fn = vfs_gluster_get_real_filename_at,
2374 .connectpath_fn = vfs_gluster_connectpath,
2375 .create_dfs_pathat_fn = vfs_gluster_create_dfs_pathat,
2376 .read_dfs_pathat_fn = vfs_gluster_read_dfs_pathat,
2378 .brl_lock_windows_fn = NULL,
2379 .brl_unlock_windows_fn = NULL,
2380 .strict_lock_check_fn = NULL,
2381 .translate_name_fn = NULL,
2384 /* NT ACL Operations */
2385 .fget_nt_acl_fn = NULL,
2386 .fset_nt_acl_fn = NULL,
2387 .audit_file_fn = NULL,
2389 /* Posix ACL Operations */
2390 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
2391 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2392 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
2393 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
2396 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2397 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2398 .fgetxattr_fn = vfs_gluster_fgetxattr,
2399 .flistxattr_fn = vfs_gluster_flistxattr,
2400 .fremovexattr_fn = vfs_gluster_fremovexattr,
2401 .fsetxattr_fn = vfs_gluster_fsetxattr,
2403 /* AIO Operations */
2404 .aio_force_fn = vfs_gluster_aio_force,
2406 /* Durable handle Operations */
2407 .durable_cookie_fn = NULL,
2408 .durable_disconnect_fn = NULL,
2409 .durable_reconnect_fn = NULL,
2413 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
2415 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2416 "glusterfs", &glusterfs_fns);