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"
49 #define DEFAULT_VOLFILE_SERVER "localhost"
51 static int read_fd = -1;
52 static int write_fd = -1;
53 static struct tevent_fd *aio_read_event = NULL;
56 * Helper to convert struct stat to struct stat_ex.
58 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
62 dst->st_ex_dev = src->st_dev;
63 dst->st_ex_ino = src->st_ino;
64 dst->st_ex_mode = src->st_mode;
65 dst->st_ex_nlink = src->st_nlink;
66 dst->st_ex_uid = src->st_uid;
67 dst->st_ex_gid = src->st_gid;
68 dst->st_ex_rdev = src->st_rdev;
69 dst->st_ex_size = src->st_size;
70 dst->st_ex_atime.tv_sec = src->st_atime;
71 dst->st_ex_mtime.tv_sec = src->st_mtime;
72 dst->st_ex_ctime.tv_sec = src->st_ctime;
73 dst->st_ex_btime.tv_sec = src->st_mtime;
74 dst->st_ex_blksize = src->st_blksize;
75 dst->st_ex_blocks = src->st_blocks;
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;
84 /* pre-opened glfs_t */
86 static struct glfs_preopened {
91 struct glfs_preopened *next, *prev;
95 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
97 struct glfs_preopened *entry = NULL;
99 entry = talloc_zero(NULL, struct glfs_preopened);
105 entry->volume = talloc_strdup(entry, volume);
106 if (!entry->volume) {
112 entry->connectpath = talloc_strdup(entry, connectpath);
113 if (entry->connectpath == NULL) {
122 DLIST_ADD(glfs_preopened, entry);
127 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
129 struct glfs_preopened *entry = NULL;
131 for (entry = glfs_preopened; entry; entry = entry->next) {
132 if (strcmp(entry->volume, volume) == 0 &&
133 strcmp(entry->connectpath, connectpath) == 0)
143 static void glfs_clear_preopened(glfs_t *fs)
145 struct glfs_preopened *entry = NULL;
147 for (entry = glfs_preopened; entry; entry = entry->next) {
148 if (entry->fs == fs) {
152 DLIST_REMOVE(glfs_preopened, entry);
154 glfs_fini(entry->fs);
160 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
161 const char *volfile_servers)
164 size_t server_count = 0;
165 size_t server_success = 0;
167 TALLOC_CTX *frame = talloc_stackframe();
169 DBG_INFO("servers list %s\n", volfile_servers);
171 while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
172 char *transport = NULL;
177 DBG_INFO("server %zu %s\n", server_count, server);
179 /* Determine the transport type */
180 if (strncmp(server, "unix+", 5) == 0) {
182 transport = talloc_strdup(frame, "unix");
187 host = talloc_strdup(frame, server + 5);
194 char *port_index = NULL;
196 if (strncmp(server, "tcp+", 4) == 0) {
200 /* IPv6 is enclosed in []
201 * ':' before ']' is part of IPv6
202 * ':' after ']' indicates port
205 if (server[0] == '[') {
207 p = index(server, ']');
216 port_index = index(p, ':');
218 if (port_index == NULL) {
221 port = atoi(port_index + 1);
222 port_index[0] = '\0';
224 transport = talloc_strdup(frame, "tcp");
229 host = talloc_strdup(frame, server);
236 DBG_INFO("Calling set volfile server with params "
237 "transport=%s, host=%s, port=%d\n", transport,
240 ret = glfs_set_volfile_server(fs, transport, host, port);
242 DBG_WARNING("Failed to set volfile_server "
243 "transport=%s, host=%s, port=%d (%s)\n",
244 transport, host, port, strerror(errno));
251 if (server_count == 0) {
253 } else if (server_success < server_count) {
254 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
255 server_count - server_success, server_count);
263 /* Disk Operations */
265 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
269 const char *volfile_servers;
277 tmp_ctx = talloc_new(NULL);
278 if (tmp_ctx == NULL) {
282 logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
285 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
287 volfile_servers = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn),
288 "glusterfs", "volfile_server",
290 if (volfile_servers == NULL) {
291 volfile_servers = DEFAULT_VOLFILE_SERVER;
294 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
296 if (volume == NULL) {
300 fs = glfs_find_preopened(volume, handle->conn->connectpath);
305 fs = glfs_new(volume);
311 ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
313 DBG_ERR("Failed to set volfile_servers from list %s\n",
318 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
321 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
326 ret = glfs_set_xlator_option(fs, "*-snapview-client",
327 "snapdir-entry-path",
328 handle->conn->connectpath);
330 DEBUG(0, ("%s: Failed to set xlator option:"
331 " snapdir-entry-path\n", volume));
335 ret = glfs_set_logging(fs, logfile, loglevel);
337 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
338 volume, logfile, loglevel));
344 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
345 volume, strerror(errno)));
349 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
351 DEBUG(0, ("%s: Failed to register volume (%s)\n",
352 volume, strerror(errno)));
357 * The shadow_copy2 module will fail to export subdirectories
358 * of a gluster volume unless we specify the mount point,
359 * because the detection fails if the file system is not
361 * https://bugzilla.samba.org/show_bug.cgi?id=13091
363 lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
366 * Unless we have an async implementation of getxattrat turn this off.
368 lp_do_parameter(SNUM(handle->conn), "smbd:async dosmode", "false");
375 DBG_ERR("%s: Initialized volume from servers %s\n",
376 volume, volfile_servers);
379 talloc_free(tmp_ctx);
383 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
389 glfs_clear_preopened(fs);
392 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
393 const struct smb_filename *smb_fname,
398 struct statvfs statvfs = { 0, };
401 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
406 if (bsize_p != NULL) {
407 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
409 if (dfree_p != NULL) {
410 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
412 if (dsize_p != NULL) {
413 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
416 return (uint64_t)statvfs.f_bavail;
419 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
420 const struct smb_filename *smb_fname,
421 enum SMB_QUOTA_TYPE qtype,
430 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
431 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
437 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
438 const struct smb_filename *smb_fname,
439 struct vfs_statvfs_struct *vfs_statvfs)
441 struct statvfs statvfs = { 0, };
444 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
446 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
447 smb_fname->base_name, strerror(errno)));
451 ZERO_STRUCTP(vfs_statvfs);
453 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
454 vfs_statvfs->BlockSize = statvfs.f_bsize;
455 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
456 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
457 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
458 vfs_statvfs->TotalFileNodes = statvfs.f_files;
459 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
460 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
461 vfs_statvfs->FsCapabilities =
462 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
467 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
468 enum timestamp_set_resolution *p_ts_res)
470 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
472 #ifdef HAVE_GFAPI_VER_6
473 caps |= FILE_SUPPORTS_SPARSE_FILES;
476 #ifdef STAT_HAVE_NSEC
477 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
483 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
484 const struct smb_filename *smb_fname,
490 fd = glfs_opendir(handle->data, smb_fname->base_name);
492 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
493 smb_fname->base_name, strerror(errno)));
499 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
502 glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
504 DBG_INFO("Failed to fetch fsp extension\n");
508 DBG_INFO("Empty glfs_fd_t pointer\n");
515 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
516 files_struct *fsp, const char *mask,
519 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
521 DBG_ERR("Failed to fetch gluster fd\n");
528 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
530 return glfs_closedir((void *)dirp);
533 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
534 DIR *dirp, SMB_STRUCT_STAT *sbuf)
536 static char direntbuf[512];
539 struct dirent *dirent = 0;
542 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
545 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
548 if ((ret < 0) || (dirent == NULL)) {
553 smb_stat_ex_from_stat(sbuf, &stat);
559 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
561 return glfs_telldir((void *)dirp);
564 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
567 glfs_seekdir((void *)dirp, offset);
570 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
572 glfs_seekdir((void *)dirp, 0);
575 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle,
576 const struct smb_filename *smb_fname,
579 return glfs_mkdir(handle->data, smb_fname->base_name, mode);
582 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle,
583 const struct smb_filename *smb_fname)
585 return glfs_rmdir(handle->data, smb_fname->base_name);
588 static int vfs_gluster_open(struct vfs_handle_struct *handle,
589 struct smb_filename *smb_fname, files_struct *fsp,
590 int flags, mode_t mode)
595 p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
601 if (flags & O_DIRECTORY) {
602 glfd = glfs_opendir(handle->data, smb_fname->base_name);
603 } else if (flags & O_CREAT) {
604 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
607 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
611 /* no extension destroy_fn, so no need to save errno */
612 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
617 /* An arbitrary value for error reporting, so you know its us. */
621 static int vfs_gluster_close(struct vfs_handle_struct *handle,
624 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
626 DBG_ERR("Failed to fetch gluster fd\n");
630 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
631 return glfs_close(glfd);
634 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
635 files_struct *fsp, void *data, size_t n,
638 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
640 DBG_ERR("Failed to fetch gluster fd\n");
644 #ifdef HAVE_GFAPI_VER_7_6
645 return glfs_pread(glfd, data, n, offset, 0, NULL);
647 return glfs_pread(glfd, data, n, offset, 0);
651 struct glusterfs_aio_state;
653 struct glusterfs_aio_wrapper {
654 struct glusterfs_aio_state *state;
657 struct glusterfs_aio_state {
659 struct tevent_req *req;
661 struct vfs_aio_state vfs_aio_state;
662 struct timespec start;
665 static int aio_wrapper_destructor(struct glusterfs_aio_wrapper *wrap)
667 if (wrap->state != NULL) {
668 wrap->state->cancelled = true;
675 * This function is the callback that will be called on glusterfs
676 * threads once the async IO submitted is complete. To notify
677 * Samba of the completion we use a pipe based queue.
679 #ifdef HAVE_GFAPI_VER_7_6
680 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret,
681 struct glfs_stat *prestat,
682 struct glfs_stat *poststat,
685 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
688 struct glusterfs_aio_state *state = NULL;
692 state = (struct glusterfs_aio_state *)data;
694 PROFILE_TIMESTAMP(&end);
698 state->vfs_aio_state.error = errno;
702 state->vfs_aio_state.duration = nsec_time_diff(&end, &state->start);
705 * Write the state pointer to glusterfs_aio_state to the
706 * pipe, so we can call tevent_req_done() from the main thread,
707 * because tevent_req_done() is not designed to be executed in
708 * the multithread environment, so tevent_req_done() must be
709 * executed from the smbd main thread.
711 * write(2) on pipes with sizes under _POSIX_PIPE_BUF
712 * in size is atomic, without this, the use op pipes in this
713 * code would not work.
715 * sys_write is a thin enough wrapper around write(2)
716 * that we can trust it here.
719 sts = sys_write(write_fd, &state, sizeof(struct glusterfs_aio_state *));
721 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
728 * Read each req off the pipe and process it.
730 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
731 struct tevent_fd *fde,
732 uint16_t flags, void *data)
734 struct tevent_req *req = NULL;
735 struct glusterfs_aio_state *state = NULL;
739 * read(2) on pipes is atomic if the needed data is available
740 * in the pipe, per SUS and POSIX. Because we always write
741 * to the pipe in sizeof(struct tevent_req *) chunks, we can
742 * always read in those chunks, atomically.
744 * sys_read is a thin enough wrapper around read(2) that we
748 sts = sys_read(read_fd, &state, sizeof(struct glusterfs_aio_state *));
751 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
754 /* if we've cancelled the op, there is no req, so just clean up. */
755 if (state->cancelled == true) {
763 tevent_req_done(req);
768 static bool init_gluster_aio(struct vfs_handle_struct *handle)
775 * Already initialized.
788 aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
794 if (aio_read_event == NULL) {
800 TALLOC_FREE(aio_read_event);
810 static struct glusterfs_aio_state *aio_state_create(TALLOC_CTX *mem_ctx)
812 struct tevent_req *req = NULL;
813 struct glusterfs_aio_state *state = NULL;
814 struct glusterfs_aio_wrapper *wrapper = NULL;
816 req = tevent_req_create(mem_ctx, &wrapper, struct glusterfs_aio_wrapper);
822 state = talloc_zero(NULL, struct glusterfs_aio_state);
829 talloc_set_destructor(wrapper, aio_wrapper_destructor);
830 state->cancelled = false;
833 wrapper->state = state;
838 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
839 *handle, TALLOC_CTX *mem_ctx,
840 struct tevent_context *ev,
842 void *data, size_t n,
845 struct glusterfs_aio_state *state = NULL;
846 struct tevent_req *req = NULL;
848 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
851 DBG_ERR("Failed to fetch gluster fd\n");
855 state = aio_state_create(mem_ctx);
863 if (!init_gluster_aio(handle)) {
864 tevent_req_error(req, EIO);
865 return tevent_req_post(req, ev);
869 * aio_glusterfs_done and aio_tevent_fd_done()
870 * use the raw tevent context. We need to use
871 * tevent_req_defer_callback() in order to
872 * use the event context we're started with.
874 tevent_req_defer_callback(req, ev);
876 PROFILE_TIMESTAMP(&state->start);
877 ret = glfs_pread_async(glfd, data, n, offset, 0, aio_glusterfs_done,
880 tevent_req_error(req, -ret);
881 return tevent_req_post(req, ev);
887 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
888 *handle, TALLOC_CTX *mem_ctx,
889 struct tevent_context *ev,
891 const void *data, size_t n,
894 struct glusterfs_aio_state *state = NULL;
895 struct tevent_req *req = NULL;
897 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
900 DBG_ERR("Failed to fetch gluster fd\n");
904 state = aio_state_create(mem_ctx);
912 if (!init_gluster_aio(handle)) {
913 tevent_req_error(req, EIO);
914 return tevent_req_post(req, ev);
918 * aio_glusterfs_done and aio_tevent_fd_done()
919 * use the raw tevent context. We need to use
920 * tevent_req_defer_callback() in order to
921 * use the event context we're started with.
923 tevent_req_defer_callback(req, ev);
925 PROFILE_TIMESTAMP(&state->start);
926 ret = glfs_pwrite_async(glfd, data, n, offset, 0, aio_glusterfs_done,
929 tevent_req_error(req, -ret);
930 return tevent_req_post(req, ev);
936 static ssize_t vfs_gluster_recv(struct tevent_req *req,
937 struct vfs_aio_state *vfs_aio_state)
939 struct glusterfs_aio_wrapper *wrapper = NULL;
942 wrapper = tevent_req_data(req, struct glusterfs_aio_wrapper);
944 if (wrapper == NULL) {
948 if (wrapper->state == NULL) {
952 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
956 *vfs_aio_state = wrapper->state->vfs_aio_state;
957 ret = wrapper->state->ret;
959 /* Clean up the state, it is in a NULL context. */
961 TALLOC_FREE(wrapper->state);
966 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
967 files_struct *fsp, const void *data,
968 size_t n, off_t offset)
970 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
972 DBG_ERR("Failed to fetch gluster fd\n");
976 #ifdef HAVE_GFAPI_VER_7_6
977 return glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
979 return glfs_pwrite(glfd, data, n, offset, 0);
983 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
984 files_struct *fsp, off_t offset, int whence)
986 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
988 DBG_ERR("Failed to fetch gluster fd\n");
992 return glfs_lseek(glfd, offset, whence);
995 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
996 files_struct *fromfsp,
997 const DATA_BLOB *hdr,
998 off_t offset, size_t n)
1004 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1005 int fromfd, files_struct *tofsp,
1006 off_t offset, size_t n)
1012 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
1013 const struct smb_filename *smb_fname_src,
1014 const struct smb_filename *smb_fname_dst)
1016 return glfs_rename(handle->data, smb_fname_src->base_name,
1017 smb_fname_dst->base_name);
1020 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1021 *handle, TALLOC_CTX *mem_ctx,
1022 struct tevent_context *ev,
1025 struct tevent_req *req = NULL;
1026 struct glusterfs_aio_state *state = NULL;
1028 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1031 DBG_ERR("Failed to fetch gluster fd\n");
1035 state = aio_state_create(mem_ctx);
1037 if (state == NULL) {
1043 if (!init_gluster_aio(handle)) {
1044 tevent_req_error(req, EIO);
1045 return tevent_req_post(req, ev);
1049 * aio_glusterfs_done and aio_tevent_fd_done()
1050 * use the raw tevent context. We need to use
1051 * tevent_req_defer_callback() in order to
1052 * use the event context we're started with.
1054 tevent_req_defer_callback(req, ev);
1056 PROFILE_TIMESTAMP(&state->start);
1057 ret = glfs_fsync_async(glfd, aio_glusterfs_done, state);
1059 tevent_req_error(req, -ret);
1060 return tevent_req_post(req, ev);
1065 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1066 struct vfs_aio_state *vfs_aio_state)
1069 * Use implicit conversion ssize_t->int
1071 return vfs_gluster_recv(req, vfs_aio_state);
1074 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1075 struct smb_filename *smb_fname)
1080 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1082 smb_stat_ex_from_stat(&smb_fname->st, &st);
1084 if (ret < 0 && errno != ENOENT) {
1085 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1086 smb_fname->base_name, strerror(errno)));
1091 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1092 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1096 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1099 DBG_ERR("Failed to fetch gluster fd\n");
1103 ret = glfs_fstat(glfd, &st);
1105 smb_stat_ex_from_stat(sbuf, &st);
1108 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1109 fsp->fh->fd, strerror(errno)));
1114 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1115 struct smb_filename *smb_fname)
1120 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1122 smb_stat_ex_from_stat(&smb_fname->st, &st);
1124 if (ret < 0 && errno != ENOENT) {
1125 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1126 smb_fname->base_name, strerror(errno)));
1131 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1133 const SMB_STRUCT_STAT *sbuf)
1135 return sbuf->st_ex_blocks * 512;
1138 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
1139 const struct smb_filename *smb_fname)
1141 return glfs_unlink(handle->data, smb_fname->base_name);
1144 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
1145 const struct smb_filename *smb_fname,
1148 return glfs_chmod(handle->data, smb_fname->base_name, mode);
1151 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1152 files_struct *fsp, mode_t mode)
1154 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1157 DBG_ERR("Failed to fetch gluster fd\n");
1161 return glfs_fchmod(glfd, mode);
1164 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
1165 const struct smb_filename *smb_fname,
1169 return glfs_chown(handle->data, smb_fname->base_name, uid, gid);
1172 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1173 files_struct *fsp, uid_t uid, gid_t gid)
1175 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1177 DBG_ERR("Failed to fetch gluster fd\n");
1181 return glfs_fchown(glfd, uid, gid);
1184 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1185 const struct smb_filename *smb_fname,
1189 return glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1192 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1193 const struct smb_filename *smb_fname)
1195 return glfs_chdir(handle->data, smb_fname->base_name);
1198 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1203 struct smb_filename *smb_fname = NULL;
1205 cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
1210 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1215 smb_fname = synthetic_smb_fname(ctx,
1224 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
1225 const struct smb_filename *smb_fname,
1226 struct smb_file_time *ft)
1228 struct timespec times[2];
1230 if (null_timespec(ft->atime)) {
1231 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
1232 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
1234 times[0].tv_sec = ft->atime.tv_sec;
1235 times[0].tv_nsec = ft->atime.tv_nsec;
1238 if (null_timespec(ft->mtime)) {
1239 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
1240 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
1242 times[1].tv_sec = ft->mtime.tv_sec;
1243 times[1].tv_nsec = ft->mtime.tv_nsec;
1246 if ((timespec_compare(×[0],
1247 &smb_fname->st.st_ex_atime) == 0) &&
1248 (timespec_compare(×[1],
1249 &smb_fname->st.st_ex_mtime) == 0)) {
1253 return glfs_utimens(handle->data, smb_fname->base_name, times);
1256 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1257 files_struct *fsp, off_t offset)
1259 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1261 DBG_ERR("Failed to fetch gluster fd\n");
1265 #ifdef HAVE_GFAPI_VER_7_6
1266 return glfs_ftruncate(glfd, offset, NULL, NULL);
1268 return glfs_ftruncate(glfd, offset);
1272 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1273 struct files_struct *fsp,
1275 off_t offset, off_t len)
1277 #ifdef HAVE_GFAPI_VER_6
1278 int keep_size, punch_hole;
1279 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1281 DBG_ERR("Failed to fetch gluster fd\n");
1285 keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1286 punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1288 mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1295 return glfs_discard(glfd, offset, len);
1298 return glfs_fallocate(glfd, keep_size, offset, len);
1305 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1307 const struct smb_filename *smb_fname)
1309 char *result = NULL;
1310 struct smb_filename *result_fname = NULL;
1311 char *resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1313 if (resolved_path == NULL) {
1318 result = glfs_realpath(handle->data,
1319 smb_fname->base_name,
1321 if (result != NULL) {
1322 result_fname = synthetic_smb_fname(ctx, result, NULL, NULL, 0);
1325 SAFE_FREE(resolved_path);
1326 return result_fname;
1329 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1330 files_struct *fsp, int op, off_t offset,
1331 off_t count, int type)
1333 struct flock flock = { 0, };
1335 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1337 DBG_ERR("Failed to fetch gluster fd\n");
1341 flock.l_type = type;
1342 flock.l_whence = SEEK_SET;
1343 flock.l_start = offset;
1344 flock.l_len = count;
1347 ret = glfs_posix_lock(glfd, op, &flock);
1349 if (op == F_GETLK) {
1350 /* lock query, true if someone else has locked */
1352 (flock.l_type != F_UNLCK) &&
1353 (flock.l_pid != 0) && (flock.l_pid != getpid()))
1366 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1367 files_struct *fsp, uint32_t share_mode,
1368 uint32_t access_mask)
1374 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1375 files_struct *fsp, int leasetype)
1381 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1382 files_struct *fsp, off_t *poffset,
1383 off_t *pcount, int *ptype, pid_t *ppid)
1385 struct flock flock = { 0, };
1387 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1389 DBG_ERR("Failed to fetch gluster fd\n");
1393 flock.l_type = *ptype;
1394 flock.l_whence = SEEK_SET;
1395 flock.l_start = *poffset;
1396 flock.l_len = *pcount;
1399 ret = glfs_posix_lock(glfd, F_GETLK, &flock);
1405 *ptype = flock.l_type;
1406 *poffset = flock.l_start;
1407 *pcount = flock.l_len;
1408 *ppid = flock.l_pid;
1413 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1414 const char *link_target,
1415 const struct smb_filename *new_smb_fname)
1417 return glfs_symlink(handle->data,
1419 new_smb_fname->base_name);
1422 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1423 const struct smb_filename *smb_fname,
1427 return glfs_readlink(handle->data, smb_fname->base_name, buf, bufsiz);
1430 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1431 const struct smb_filename *old_smb_fname,
1432 const struct smb_filename *new_smb_fname)
1434 return glfs_link(handle->data,
1435 old_smb_fname->base_name,
1436 new_smb_fname->base_name);
1439 static int vfs_gluster_mknod(struct vfs_handle_struct *handle,
1440 const struct smb_filename *smb_fname,
1444 return glfs_mknod(handle->data, smb_fname->base_name, mode, dev);
1447 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1448 const struct smb_filename *smb_fname,
1455 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1456 const char *path, const char *name,
1457 TALLOC_CTX *mem_ctx, char **_found_name)
1460 char *key_buf = NULL, *val_buf = NULL;
1462 char *found_name = NULL;
1464 name_max = pathconf(path, _PC_NAME_MAX);
1465 if ((name_max + 1) < 1) {
1470 if (strlen(name) >= name_max) {
1471 errno = ENAMETOOLONG;
1475 key_buf = talloc_asprintf(mem_ctx, "glusterfs.get_real_filename:%s",
1477 if (key_buf == NULL) {
1482 val_buf = talloc_zero_array(mem_ctx, char, name_max + 1);
1483 if (val_buf == NULL) {
1487 ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1489 if (errno == ENOATTR) {
1495 found_name = talloc_strdup(mem_ctx, val_buf);
1496 if (found_name == NULL) {
1500 *_found_name = found_name;
1502 TALLOC_FREE(key_buf);
1503 TALLOC_FREE(val_buf);
1508 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1509 const struct smb_filename *smb_fname)
1511 return handle->conn->connectpath;
1516 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1517 const struct smb_filename *smb_fname,
1522 return glfs_getxattr(handle->data, smb_fname->base_name,
1526 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1527 files_struct *fsp, const char *name,
1528 void *value, size_t size)
1530 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1532 DBG_ERR("Failed to fetch gluster fd\n");
1536 return glfs_fgetxattr(glfd, name, value, size);
1539 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1540 const struct smb_filename *smb_fname,
1544 return glfs_listxattr(handle->data, smb_fname->base_name, list, size);
1547 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1548 files_struct *fsp, char *list,
1551 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1553 DBG_ERR("Failed to fetch gluster fd\n");
1557 return glfs_flistxattr(glfd, list, size);
1560 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1561 const struct smb_filename *smb_fname,
1564 return glfs_removexattr(handle->data, smb_fname->base_name, name);
1567 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1568 files_struct *fsp, const char *name)
1570 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1572 DBG_ERR("Failed to fetch gluster fd\n");
1576 return glfs_fremovexattr(glfd, name);
1579 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1580 const struct smb_filename *smb_fname,
1582 const void *value, size_t size, int flags)
1584 return glfs_setxattr(handle->data, smb_fname->base_name, name, value, size, flags);
1587 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1588 files_struct *fsp, const char *name,
1589 const void *value, size_t size, int flags)
1591 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1593 DBG_ERR("Failed to fetch gluster fd\n");
1597 return glfs_fsetxattr(glfd, name, value, size, flags);
1600 /* AIO Operations */
1602 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1608 static struct vfs_fn_pointers glusterfs_fns = {
1610 /* Disk Operations */
1612 .connect_fn = vfs_gluster_connect,
1613 .disconnect_fn = vfs_gluster_disconnect,
1614 .disk_free_fn = vfs_gluster_disk_free,
1615 .get_quota_fn = vfs_gluster_get_quota,
1616 .set_quota_fn = vfs_gluster_set_quota,
1617 .statvfs_fn = vfs_gluster_statvfs,
1618 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1620 .get_dfs_referrals_fn = NULL,
1622 /* Directory Operations */
1624 .opendir_fn = vfs_gluster_opendir,
1625 .fdopendir_fn = vfs_gluster_fdopendir,
1626 .readdir_fn = vfs_gluster_readdir,
1627 .seekdir_fn = vfs_gluster_seekdir,
1628 .telldir_fn = vfs_gluster_telldir,
1629 .rewind_dir_fn = vfs_gluster_rewinddir,
1630 .mkdir_fn = vfs_gluster_mkdir,
1631 .rmdir_fn = vfs_gluster_rmdir,
1632 .closedir_fn = vfs_gluster_closedir,
1634 /* File Operations */
1636 .open_fn = vfs_gluster_open,
1637 .create_file_fn = NULL,
1638 .close_fn = vfs_gluster_close,
1639 .pread_fn = vfs_gluster_pread,
1640 .pread_send_fn = vfs_gluster_pread_send,
1641 .pread_recv_fn = vfs_gluster_recv,
1642 .pwrite_fn = vfs_gluster_pwrite,
1643 .pwrite_send_fn = vfs_gluster_pwrite_send,
1644 .pwrite_recv_fn = vfs_gluster_recv,
1645 .lseek_fn = vfs_gluster_lseek,
1646 .sendfile_fn = vfs_gluster_sendfile,
1647 .recvfile_fn = vfs_gluster_recvfile,
1648 .rename_fn = vfs_gluster_rename,
1649 .fsync_send_fn = vfs_gluster_fsync_send,
1650 .fsync_recv_fn = vfs_gluster_fsync_recv,
1652 .stat_fn = vfs_gluster_stat,
1653 .fstat_fn = vfs_gluster_fstat,
1654 .lstat_fn = vfs_gluster_lstat,
1655 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1656 .unlink_fn = vfs_gluster_unlink,
1658 .chmod_fn = vfs_gluster_chmod,
1659 .fchmod_fn = vfs_gluster_fchmod,
1660 .chown_fn = vfs_gluster_chown,
1661 .fchown_fn = vfs_gluster_fchown,
1662 .lchown_fn = vfs_gluster_lchown,
1663 .chdir_fn = vfs_gluster_chdir,
1664 .getwd_fn = vfs_gluster_getwd,
1665 .ntimes_fn = vfs_gluster_ntimes,
1666 .ftruncate_fn = vfs_gluster_ftruncate,
1667 .fallocate_fn = vfs_gluster_fallocate,
1668 .lock_fn = vfs_gluster_lock,
1669 .kernel_flock_fn = vfs_gluster_kernel_flock,
1670 .linux_setlease_fn = vfs_gluster_linux_setlease,
1671 .getlock_fn = vfs_gluster_getlock,
1672 .symlink_fn = vfs_gluster_symlink,
1673 .readlink_fn = vfs_gluster_readlink,
1674 .link_fn = vfs_gluster_link,
1675 .mknod_fn = vfs_gluster_mknod,
1676 .realpath_fn = vfs_gluster_realpath,
1677 .chflags_fn = vfs_gluster_chflags,
1678 .file_id_create_fn = NULL,
1679 .streaminfo_fn = NULL,
1680 .get_real_filename_fn = vfs_gluster_get_real_filename,
1681 .connectpath_fn = vfs_gluster_connectpath,
1683 .brl_lock_windows_fn = NULL,
1684 .brl_unlock_windows_fn = NULL,
1685 .brl_cancel_windows_fn = NULL,
1686 .strict_lock_check_fn = NULL,
1687 .translate_name_fn = NULL,
1690 /* NT ACL Operations */
1691 .fget_nt_acl_fn = NULL,
1692 .get_nt_acl_fn = NULL,
1693 .fset_nt_acl_fn = NULL,
1694 .audit_file_fn = NULL,
1696 /* Posix ACL Operations */
1697 .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
1698 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1699 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1700 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1701 .sys_acl_set_file_fn = posixacl_xattr_acl_set_file,
1702 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1703 .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
1706 .getxattr_fn = vfs_gluster_getxattr,
1707 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1708 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1709 .fgetxattr_fn = vfs_gluster_fgetxattr,
1710 .listxattr_fn = vfs_gluster_listxattr,
1711 .flistxattr_fn = vfs_gluster_flistxattr,
1712 .removexattr_fn = vfs_gluster_removexattr,
1713 .fremovexattr_fn = vfs_gluster_fremovexattr,
1714 .setxattr_fn = vfs_gluster_setxattr,
1715 .fsetxattr_fn = vfs_gluster_fsetxattr,
1717 /* AIO Operations */
1718 .aio_force_fn = vfs_gluster_aio_force,
1720 /* Durable handle Operations */
1721 .durable_cookie_fn = NULL,
1722 .durable_disconnect_fn = NULL,
1723 .durable_reconnect_fn = NULL,
1727 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
1729 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1730 "glusterfs", &glusterfs_fns);