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 int server_count
= 0;
165 int 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 %d %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 %d out of %d 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 if (flags
& O_DIRECTORY
) {
596 glfd
= glfs_opendir(handle
->data
, smb_fname
->base_name
);
597 } else if (flags
& O_CREAT
) {
598 glfd
= glfs_creat(handle
->data
, smb_fname
->base_name
, flags
,
601 glfd
= glfs_open(handle
->data
, smb_fname
->base_name
, flags
);
607 p_tmp
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, glfs_fd_t
*, NULL
);
609 /* An arbitrary value for error reporting, so you know its us. */
613 static int vfs_gluster_close(struct vfs_handle_struct
*handle
,
616 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
618 DBG_ERR("Failed to fetch gluster fd\n");
622 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
623 return glfs_close(glfd
);
626 static ssize_t
vfs_gluster_pread(struct vfs_handle_struct
*handle
,
627 files_struct
*fsp
, void *data
, size_t n
,
630 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
632 DBG_ERR("Failed to fetch gluster fd\n");
636 #ifdef HAVE_GFAPI_VER_7_6
637 return glfs_pread(glfd
, data
, n
, offset
, 0, NULL
);
639 return glfs_pread(glfd
, data
, n
, offset
, 0);
643 struct glusterfs_aio_state
;
645 struct glusterfs_aio_wrapper
{
646 struct glusterfs_aio_state
*state
;
649 struct glusterfs_aio_state
{
651 struct tevent_req
*req
;
653 struct vfs_aio_state vfs_aio_state
;
654 struct timespec start
;
657 static int aio_wrapper_destructor(struct glusterfs_aio_wrapper
*wrap
)
659 if (wrap
->state
!= NULL
) {
660 wrap
->state
->cancelled
= true;
667 * This function is the callback that will be called on glusterfs
668 * threads once the async IO submitted is complete. To notify
669 * Samba of the completion we use a pipe based queue.
671 #ifdef HAVE_GFAPI_VER_7_6
672 static void aio_glusterfs_done(glfs_fd_t
*fd
, ssize_t ret
,
673 struct glfs_stat
*prestat
,
674 struct glfs_stat
*poststat
,
677 static void aio_glusterfs_done(glfs_fd_t
*fd
, ssize_t ret
, void *data
)
680 struct glusterfs_aio_state
*state
= NULL
;
684 state
= (struct glusterfs_aio_state
*)data
;
686 PROFILE_TIMESTAMP(&end
);
690 state
->vfs_aio_state
.error
= errno
;
694 state
->vfs_aio_state
.duration
= nsec_time_diff(&end
, &state
->start
);
697 * Write the state pointer to glusterfs_aio_state to the
698 * pipe, so we can call tevent_req_done() from the main thread,
699 * because tevent_req_done() is not designed to be executed in
700 * the multithread environment, so tevent_req_done() must be
701 * executed from the smbd main thread.
703 * write(2) on pipes with sizes under _POSIX_PIPE_BUF
704 * in size is atomic, without this, the use op pipes in this
705 * code would not work.
707 * sys_write is a thin enough wrapper around write(2)
708 * that we can trust it here.
711 sts
= sys_write(write_fd
, &state
, sizeof(struct glusterfs_aio_state
*));
713 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno
)));
720 * Read each req off the pipe and process it.
722 static void aio_tevent_fd_done(struct tevent_context
*event_ctx
,
723 struct tevent_fd
*fde
,
724 uint16_t flags
, void *data
)
726 struct tevent_req
*req
= NULL
;
727 struct glusterfs_aio_state
*state
= NULL
;
731 * read(2) on pipes is atomic if the needed data is available
732 * in the pipe, per SUS and POSIX. Because we always write
733 * to the pipe in sizeof(struct tevent_req *) chunks, we can
734 * always read in those chunks, atomically.
736 * sys_read is a thin enough wrapper around read(2) that we
740 sts
= sys_read(read_fd
, &state
, sizeof(struct glusterfs_aio_state
*));
743 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno
)));
746 /* if we've cancelled the op, there is no req, so just clean up. */
747 if (state
->cancelled
== true) {
755 tevent_req_done(req
);
760 static bool init_gluster_aio(struct vfs_handle_struct
*handle
)
767 * Already initialized.
780 aio_read_event
= tevent_add_fd(handle
->conn
->sconn
->ev_ctx
,
786 if (aio_read_event
== NULL
) {
792 TALLOC_FREE(aio_read_event
);
802 static struct glusterfs_aio_state
*aio_state_create(TALLOC_CTX
*mem_ctx
)
804 struct tevent_req
*req
= NULL
;
805 struct glusterfs_aio_state
*state
= NULL
;
806 struct glusterfs_aio_wrapper
*wrapper
= NULL
;
808 req
= tevent_req_create(mem_ctx
, &wrapper
, struct glusterfs_aio_wrapper
);
814 state
= talloc_zero(NULL
, struct glusterfs_aio_state
);
821 talloc_set_destructor(wrapper
, aio_wrapper_destructor
);
822 state
->cancelled
= false;
825 wrapper
->state
= state
;
830 static struct tevent_req
*vfs_gluster_pread_send(struct vfs_handle_struct
831 *handle
, TALLOC_CTX
*mem_ctx
,
832 struct tevent_context
*ev
,
834 void *data
, size_t n
,
837 struct glusterfs_aio_state
*state
= NULL
;
838 struct tevent_req
*req
= NULL
;
840 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
843 DBG_ERR("Failed to fetch gluster fd\n");
847 state
= aio_state_create(mem_ctx
);
855 if (!init_gluster_aio(handle
)) {
856 tevent_req_error(req
, EIO
);
857 return tevent_req_post(req
, ev
);
861 * aio_glusterfs_done and aio_tevent_fd_done()
862 * use the raw tevent context. We need to use
863 * tevent_req_defer_callback() in order to
864 * use the event context we're started with.
866 tevent_req_defer_callback(req
, ev
);
868 PROFILE_TIMESTAMP(&state
->start
);
869 ret
= glfs_pread_async(glfd
, data
, n
, offset
, 0, aio_glusterfs_done
,
872 tevent_req_error(req
, -ret
);
873 return tevent_req_post(req
, ev
);
879 static struct tevent_req
*vfs_gluster_pwrite_send(struct vfs_handle_struct
880 *handle
, TALLOC_CTX
*mem_ctx
,
881 struct tevent_context
*ev
,
883 const void *data
, size_t n
,
886 struct glusterfs_aio_state
*state
= NULL
;
887 struct tevent_req
*req
= NULL
;
889 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
892 DBG_ERR("Failed to fetch gluster fd\n");
896 state
= aio_state_create(mem_ctx
);
904 if (!init_gluster_aio(handle
)) {
905 tevent_req_error(req
, EIO
);
906 return tevent_req_post(req
, ev
);
910 * aio_glusterfs_done and aio_tevent_fd_done()
911 * use the raw tevent context. We need to use
912 * tevent_req_defer_callback() in order to
913 * use the event context we're started with.
915 tevent_req_defer_callback(req
, ev
);
917 PROFILE_TIMESTAMP(&state
->start
);
918 ret
= glfs_pwrite_async(glfd
, data
, n
, offset
, 0, aio_glusterfs_done
,
921 tevent_req_error(req
, -ret
);
922 return tevent_req_post(req
, ev
);
928 static ssize_t
vfs_gluster_recv(struct tevent_req
*req
,
929 struct vfs_aio_state
*vfs_aio_state
)
931 struct glusterfs_aio_wrapper
*wrapper
= NULL
;
934 wrapper
= tevent_req_data(req
, struct glusterfs_aio_wrapper
);
936 if (wrapper
== NULL
) {
940 if (wrapper
->state
== NULL
) {
944 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
948 *vfs_aio_state
= wrapper
->state
->vfs_aio_state
;
949 ret
= wrapper
->state
->ret
;
951 /* Clean up the state, it is in a NULL context. */
953 TALLOC_FREE(wrapper
->state
);
958 static ssize_t
vfs_gluster_pwrite(struct vfs_handle_struct
*handle
,
959 files_struct
*fsp
, const void *data
,
960 size_t n
, off_t offset
)
962 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
964 DBG_ERR("Failed to fetch gluster fd\n");
968 #ifdef HAVE_GFAPI_VER_7_6
969 return glfs_pwrite(glfd
, data
, n
, offset
, 0, NULL
, NULL
);
971 return glfs_pwrite(glfd
, data
, n
, offset
, 0);
975 static off_t
vfs_gluster_lseek(struct vfs_handle_struct
*handle
,
976 files_struct
*fsp
, off_t offset
, int whence
)
978 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
980 DBG_ERR("Failed to fetch gluster fd\n");
984 return glfs_lseek(glfd
, offset
, whence
);
987 static ssize_t
vfs_gluster_sendfile(struct vfs_handle_struct
*handle
, int tofd
,
988 files_struct
*fromfsp
,
989 const DATA_BLOB
*hdr
,
990 off_t offset
, size_t n
)
996 static ssize_t
vfs_gluster_recvfile(struct vfs_handle_struct
*handle
,
997 int fromfd
, files_struct
*tofsp
,
998 off_t offset
, size_t n
)
1004 static int vfs_gluster_rename(struct vfs_handle_struct
*handle
,
1005 const struct smb_filename
*smb_fname_src
,
1006 const struct smb_filename
*smb_fname_dst
)
1008 return glfs_rename(handle
->data
, smb_fname_src
->base_name
,
1009 smb_fname_dst
->base_name
);
1012 static struct tevent_req
*vfs_gluster_fsync_send(struct vfs_handle_struct
1013 *handle
, TALLOC_CTX
*mem_ctx
,
1014 struct tevent_context
*ev
,
1017 struct tevent_req
*req
= NULL
;
1018 struct glusterfs_aio_state
*state
= NULL
;
1020 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1023 DBG_ERR("Failed to fetch gluster fd\n");
1027 state
= aio_state_create(mem_ctx
);
1029 if (state
== NULL
) {
1035 if (!init_gluster_aio(handle
)) {
1036 tevent_req_error(req
, EIO
);
1037 return tevent_req_post(req
, ev
);
1041 * aio_glusterfs_done and aio_tevent_fd_done()
1042 * use the raw tevent context. We need to use
1043 * tevent_req_defer_callback() in order to
1044 * use the event context we're started with.
1046 tevent_req_defer_callback(req
, ev
);
1048 PROFILE_TIMESTAMP(&state
->start
);
1049 ret
= glfs_fsync_async(glfd
, aio_glusterfs_done
, state
);
1051 tevent_req_error(req
, -ret
);
1052 return tevent_req_post(req
, ev
);
1057 static int vfs_gluster_fsync_recv(struct tevent_req
*req
,
1058 struct vfs_aio_state
*vfs_aio_state
)
1061 * Use implicit conversion ssize_t->int
1063 return vfs_gluster_recv(req
, vfs_aio_state
);
1066 static int vfs_gluster_stat(struct vfs_handle_struct
*handle
,
1067 struct smb_filename
*smb_fname
)
1072 ret
= glfs_stat(handle
->data
, smb_fname
->base_name
, &st
);
1074 smb_stat_ex_from_stat(&smb_fname
->st
, &st
);
1076 if (ret
< 0 && errno
!= ENOENT
) {
1077 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1078 smb_fname
->base_name
, strerror(errno
)));
1083 static int vfs_gluster_fstat(struct vfs_handle_struct
*handle
,
1084 files_struct
*fsp
, SMB_STRUCT_STAT
*sbuf
)
1088 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1091 DBG_ERR("Failed to fetch gluster fd\n");
1095 ret
= glfs_fstat(glfd
, &st
);
1097 smb_stat_ex_from_stat(sbuf
, &st
);
1100 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1101 fsp
->fh
->fd
, strerror(errno
)));
1106 static int vfs_gluster_lstat(struct vfs_handle_struct
*handle
,
1107 struct smb_filename
*smb_fname
)
1112 ret
= glfs_lstat(handle
->data
, smb_fname
->base_name
, &st
);
1114 smb_stat_ex_from_stat(&smb_fname
->st
, &st
);
1116 if (ret
< 0 && errno
!= ENOENT
) {
1117 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1118 smb_fname
->base_name
, strerror(errno
)));
1123 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct
*handle
,
1125 const SMB_STRUCT_STAT
*sbuf
)
1127 return sbuf
->st_ex_blocks
* 512;
1130 static int vfs_gluster_unlink(struct vfs_handle_struct
*handle
,
1131 const struct smb_filename
*smb_fname
)
1133 return glfs_unlink(handle
->data
, smb_fname
->base_name
);
1136 static int vfs_gluster_chmod(struct vfs_handle_struct
*handle
,
1137 const struct smb_filename
*smb_fname
,
1140 return glfs_chmod(handle
->data
, smb_fname
->base_name
, mode
);
1143 static int vfs_gluster_fchmod(struct vfs_handle_struct
*handle
,
1144 files_struct
*fsp
, mode_t mode
)
1146 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1149 DBG_ERR("Failed to fetch gluster fd\n");
1153 return glfs_fchmod(glfd
, mode
);
1156 static int vfs_gluster_chown(struct vfs_handle_struct
*handle
,
1157 const struct smb_filename
*smb_fname
,
1161 return glfs_chown(handle
->data
, smb_fname
->base_name
, uid
, gid
);
1164 static int vfs_gluster_fchown(struct vfs_handle_struct
*handle
,
1165 files_struct
*fsp
, uid_t uid
, gid_t gid
)
1167 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1169 DBG_ERR("Failed to fetch gluster fd\n");
1173 return glfs_fchown(glfd
, uid
, gid
);
1176 static int vfs_gluster_lchown(struct vfs_handle_struct
*handle
,
1177 const struct smb_filename
*smb_fname
,
1181 return glfs_lchown(handle
->data
, smb_fname
->base_name
, uid
, gid
);
1184 static int vfs_gluster_chdir(struct vfs_handle_struct
*handle
,
1185 const struct smb_filename
*smb_fname
)
1187 return glfs_chdir(handle
->data
, smb_fname
->base_name
);
1190 static struct smb_filename
*vfs_gluster_getwd(struct vfs_handle_struct
*handle
,
1195 struct smb_filename
*smb_fname
= NULL
;
1197 cwd
= SMB_CALLOC_ARRAY(char, PATH_MAX
);
1202 ret
= glfs_getcwd(handle
->data
, cwd
, PATH_MAX
- 1);
1207 smb_fname
= synthetic_smb_fname(ctx
,
1216 static int vfs_gluster_ntimes(struct vfs_handle_struct
*handle
,
1217 const struct smb_filename
*smb_fname
,
1218 struct smb_file_time
*ft
)
1220 struct timespec times
[2];
1222 if (null_timespec(ft
->atime
)) {
1223 times
[0].tv_sec
= smb_fname
->st
.st_ex_atime
.tv_sec
;
1224 times
[0].tv_nsec
= smb_fname
->st
.st_ex_atime
.tv_nsec
;
1226 times
[0].tv_sec
= ft
->atime
.tv_sec
;
1227 times
[0].tv_nsec
= ft
->atime
.tv_nsec
;
1230 if (null_timespec(ft
->mtime
)) {
1231 times
[1].tv_sec
= smb_fname
->st
.st_ex_mtime
.tv_sec
;
1232 times
[1].tv_nsec
= smb_fname
->st
.st_ex_mtime
.tv_nsec
;
1234 times
[1].tv_sec
= ft
->mtime
.tv_sec
;
1235 times
[1].tv_nsec
= ft
->mtime
.tv_nsec
;
1238 if ((timespec_compare(×
[0],
1239 &smb_fname
->st
.st_ex_atime
) == 0) &&
1240 (timespec_compare(×
[1],
1241 &smb_fname
->st
.st_ex_mtime
) == 0)) {
1245 return glfs_utimens(handle
->data
, smb_fname
->base_name
, times
);
1248 static int vfs_gluster_ftruncate(struct vfs_handle_struct
*handle
,
1249 files_struct
*fsp
, off_t offset
)
1251 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1253 DBG_ERR("Failed to fetch gluster fd\n");
1257 #ifdef HAVE_GFAPI_VER_7_6
1258 return glfs_ftruncate(glfd
, offset
, NULL
, NULL
);
1260 return glfs_ftruncate(glfd
, offset
);
1264 static int vfs_gluster_fallocate(struct vfs_handle_struct
*handle
,
1265 struct files_struct
*fsp
,
1267 off_t offset
, off_t len
)
1269 #ifdef HAVE_GFAPI_VER_6
1270 int keep_size
, punch_hole
;
1271 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1273 DBG_ERR("Failed to fetch gluster fd\n");
1277 keep_size
= mode
& VFS_FALLOCATE_FL_KEEP_SIZE
;
1278 punch_hole
= mode
& VFS_FALLOCATE_FL_PUNCH_HOLE
;
1280 mode
&= ~(VFS_FALLOCATE_FL_KEEP_SIZE
|VFS_FALLOCATE_FL_PUNCH_HOLE
);
1287 return glfs_discard(glfd
, offset
, len
);
1290 return glfs_fallocate(glfd
, keep_size
, offset
, len
);
1297 static struct smb_filename
*vfs_gluster_realpath(struct vfs_handle_struct
*handle
,
1299 const struct smb_filename
*smb_fname
)
1301 char *result
= NULL
;
1302 struct smb_filename
*result_fname
= NULL
;
1303 char *resolved_path
= SMB_MALLOC_ARRAY(char, PATH_MAX
+1);
1305 if (resolved_path
== NULL
) {
1310 result
= glfs_realpath(handle
->data
,
1311 smb_fname
->base_name
,
1313 if (result
!= NULL
) {
1314 result_fname
= synthetic_smb_fname(ctx
, result
, NULL
, NULL
, 0);
1317 SAFE_FREE(resolved_path
);
1318 return result_fname
;
1321 static bool vfs_gluster_lock(struct vfs_handle_struct
*handle
,
1322 files_struct
*fsp
, int op
, off_t offset
,
1323 off_t count
, int type
)
1325 struct flock flock
= { 0, };
1327 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1329 DBG_ERR("Failed to fetch gluster fd\n");
1333 flock
.l_type
= type
;
1334 flock
.l_whence
= SEEK_SET
;
1335 flock
.l_start
= offset
;
1336 flock
.l_len
= count
;
1339 ret
= glfs_posix_lock(glfd
, op
, &flock
);
1341 if (op
== F_GETLK
) {
1342 /* lock query, true if someone else has locked */
1344 (flock
.l_type
!= F_UNLCK
) &&
1345 (flock
.l_pid
!= 0) && (flock
.l_pid
!= getpid()))
1358 static int vfs_gluster_kernel_flock(struct vfs_handle_struct
*handle
,
1359 files_struct
*fsp
, uint32_t share_mode
,
1360 uint32_t access_mask
)
1366 static int vfs_gluster_linux_setlease(struct vfs_handle_struct
*handle
,
1367 files_struct
*fsp
, int leasetype
)
1373 static bool vfs_gluster_getlock(struct vfs_handle_struct
*handle
,
1374 files_struct
*fsp
, off_t
*poffset
,
1375 off_t
*pcount
, int *ptype
, pid_t
*ppid
)
1377 struct flock flock
= { 0, };
1379 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1381 DBG_ERR("Failed to fetch gluster fd\n");
1385 flock
.l_type
= *ptype
;
1386 flock
.l_whence
= SEEK_SET
;
1387 flock
.l_start
= *poffset
;
1388 flock
.l_len
= *pcount
;
1391 ret
= glfs_posix_lock(glfd
, F_GETLK
, &flock
);
1397 *ptype
= flock
.l_type
;
1398 *poffset
= flock
.l_start
;
1399 *pcount
= flock
.l_len
;
1400 *ppid
= flock
.l_pid
;
1405 static int vfs_gluster_symlink(struct vfs_handle_struct
*handle
,
1406 const char *link_target
,
1407 const struct smb_filename
*new_smb_fname
)
1409 return glfs_symlink(handle
->data
,
1411 new_smb_fname
->base_name
);
1414 static int vfs_gluster_readlink(struct vfs_handle_struct
*handle
,
1415 const struct smb_filename
*smb_fname
,
1419 return glfs_readlink(handle
->data
, smb_fname
->base_name
, buf
, bufsiz
);
1422 static int vfs_gluster_link(struct vfs_handle_struct
*handle
,
1423 const struct smb_filename
*old_smb_fname
,
1424 const struct smb_filename
*new_smb_fname
)
1426 return glfs_link(handle
->data
,
1427 old_smb_fname
->base_name
,
1428 new_smb_fname
->base_name
);
1431 static int vfs_gluster_mknod(struct vfs_handle_struct
*handle
,
1432 const struct smb_filename
*smb_fname
,
1436 return glfs_mknod(handle
->data
, smb_fname
->base_name
, mode
, dev
);
1439 static int vfs_gluster_chflags(struct vfs_handle_struct
*handle
,
1440 const struct smb_filename
*smb_fname
,
1447 static int vfs_gluster_get_real_filename(struct vfs_handle_struct
*handle
,
1448 const char *path
, const char *name
,
1449 TALLOC_CTX
*mem_ctx
, char **found_name
)
1452 char key_buf
[NAME_MAX
+ 64];
1453 char val_buf
[NAME_MAX
+ 1];
1455 if (strlen(name
) >= NAME_MAX
) {
1456 errno
= ENAMETOOLONG
;
1460 snprintf(key_buf
, NAME_MAX
+ 64,
1461 "glusterfs.get_real_filename:%s", name
);
1463 ret
= glfs_getxattr(handle
->data
, path
, key_buf
, val_buf
, NAME_MAX
+ 1);
1465 if (errno
== ENOATTR
) {
1471 *found_name
= talloc_strdup(mem_ctx
, val_buf
);
1472 if (found_name
[0] == NULL
) {
1479 static const char *vfs_gluster_connectpath(struct vfs_handle_struct
*handle
,
1480 const struct smb_filename
*smb_fname
)
1482 return handle
->conn
->connectpath
;
1487 static ssize_t
vfs_gluster_getxattr(struct vfs_handle_struct
*handle
,
1488 const struct smb_filename
*smb_fname
,
1493 return glfs_getxattr(handle
->data
, smb_fname
->base_name
,
1497 static ssize_t
vfs_gluster_fgetxattr(struct vfs_handle_struct
*handle
,
1498 files_struct
*fsp
, const char *name
,
1499 void *value
, size_t size
)
1501 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1503 DBG_ERR("Failed to fetch gluster fd\n");
1507 return glfs_fgetxattr(glfd
, name
, value
, size
);
1510 static ssize_t
vfs_gluster_listxattr(struct vfs_handle_struct
*handle
,
1511 const struct smb_filename
*smb_fname
,
1515 return glfs_listxattr(handle
->data
, smb_fname
->base_name
, list
, size
);
1518 static ssize_t
vfs_gluster_flistxattr(struct vfs_handle_struct
*handle
,
1519 files_struct
*fsp
, char *list
,
1522 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1524 DBG_ERR("Failed to fetch gluster fd\n");
1528 return glfs_flistxattr(glfd
, list
, size
);
1531 static int vfs_gluster_removexattr(struct vfs_handle_struct
*handle
,
1532 const struct smb_filename
*smb_fname
,
1535 return glfs_removexattr(handle
->data
, smb_fname
->base_name
, name
);
1538 static int vfs_gluster_fremovexattr(struct vfs_handle_struct
*handle
,
1539 files_struct
*fsp
, const char *name
)
1541 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1543 DBG_ERR("Failed to fetch gluster fd\n");
1547 return glfs_fremovexattr(glfd
, name
);
1550 static int vfs_gluster_setxattr(struct vfs_handle_struct
*handle
,
1551 const struct smb_filename
*smb_fname
,
1553 const void *value
, size_t size
, int flags
)
1555 return glfs_setxattr(handle
->data
, smb_fname
->base_name
, name
, value
, size
, flags
);
1558 static int vfs_gluster_fsetxattr(struct vfs_handle_struct
*handle
,
1559 files_struct
*fsp
, const char *name
,
1560 const void *value
, size_t size
, int flags
)
1562 glfs_fd_t
*glfd
= vfs_gluster_fetch_glfd(handle
, fsp
);
1564 DBG_ERR("Failed to fetch gluster fd\n");
1568 return glfs_fsetxattr(glfd
, name
, value
, size
, flags
);
1571 /* AIO Operations */
1573 static bool vfs_gluster_aio_force(struct vfs_handle_struct
*handle
,
1579 static struct vfs_fn_pointers glusterfs_fns
= {
1581 /* Disk Operations */
1583 .connect_fn
= vfs_gluster_connect
,
1584 .disconnect_fn
= vfs_gluster_disconnect
,
1585 .disk_free_fn
= vfs_gluster_disk_free
,
1586 .get_quota_fn
= vfs_gluster_get_quota
,
1587 .set_quota_fn
= vfs_gluster_set_quota
,
1588 .statvfs_fn
= vfs_gluster_statvfs
,
1589 .fs_capabilities_fn
= vfs_gluster_fs_capabilities
,
1591 .get_dfs_referrals_fn
= NULL
,
1593 /* Directory Operations */
1595 .opendir_fn
= vfs_gluster_opendir
,
1596 .fdopendir_fn
= vfs_gluster_fdopendir
,
1597 .readdir_fn
= vfs_gluster_readdir
,
1598 .seekdir_fn
= vfs_gluster_seekdir
,
1599 .telldir_fn
= vfs_gluster_telldir
,
1600 .rewind_dir_fn
= vfs_gluster_rewinddir
,
1601 .mkdir_fn
= vfs_gluster_mkdir
,
1602 .rmdir_fn
= vfs_gluster_rmdir
,
1603 .closedir_fn
= vfs_gluster_closedir
,
1605 /* File Operations */
1607 .open_fn
= vfs_gluster_open
,
1608 .create_file_fn
= NULL
,
1609 .close_fn
= vfs_gluster_close
,
1610 .pread_fn
= vfs_gluster_pread
,
1611 .pread_send_fn
= vfs_gluster_pread_send
,
1612 .pread_recv_fn
= vfs_gluster_recv
,
1613 .pwrite_fn
= vfs_gluster_pwrite
,
1614 .pwrite_send_fn
= vfs_gluster_pwrite_send
,
1615 .pwrite_recv_fn
= vfs_gluster_recv
,
1616 .lseek_fn
= vfs_gluster_lseek
,
1617 .sendfile_fn
= vfs_gluster_sendfile
,
1618 .recvfile_fn
= vfs_gluster_recvfile
,
1619 .rename_fn
= vfs_gluster_rename
,
1620 .fsync_send_fn
= vfs_gluster_fsync_send
,
1621 .fsync_recv_fn
= vfs_gluster_fsync_recv
,
1623 .stat_fn
= vfs_gluster_stat
,
1624 .fstat_fn
= vfs_gluster_fstat
,
1625 .lstat_fn
= vfs_gluster_lstat
,
1626 .get_alloc_size_fn
= vfs_gluster_get_alloc_size
,
1627 .unlink_fn
= vfs_gluster_unlink
,
1629 .chmod_fn
= vfs_gluster_chmod
,
1630 .fchmod_fn
= vfs_gluster_fchmod
,
1631 .chown_fn
= vfs_gluster_chown
,
1632 .fchown_fn
= vfs_gluster_fchown
,
1633 .lchown_fn
= vfs_gluster_lchown
,
1634 .chdir_fn
= vfs_gluster_chdir
,
1635 .getwd_fn
= vfs_gluster_getwd
,
1636 .ntimes_fn
= vfs_gluster_ntimes
,
1637 .ftruncate_fn
= vfs_gluster_ftruncate
,
1638 .fallocate_fn
= vfs_gluster_fallocate
,
1639 .lock_fn
= vfs_gluster_lock
,
1640 .kernel_flock_fn
= vfs_gluster_kernel_flock
,
1641 .linux_setlease_fn
= vfs_gluster_linux_setlease
,
1642 .getlock_fn
= vfs_gluster_getlock
,
1643 .symlink_fn
= vfs_gluster_symlink
,
1644 .readlink_fn
= vfs_gluster_readlink
,
1645 .link_fn
= vfs_gluster_link
,
1646 .mknod_fn
= vfs_gluster_mknod
,
1647 .realpath_fn
= vfs_gluster_realpath
,
1648 .chflags_fn
= vfs_gluster_chflags
,
1649 .file_id_create_fn
= NULL
,
1650 .streaminfo_fn
= NULL
,
1651 .get_real_filename_fn
= vfs_gluster_get_real_filename
,
1652 .connectpath_fn
= vfs_gluster_connectpath
,
1654 .brl_lock_windows_fn
= NULL
,
1655 .brl_unlock_windows_fn
= NULL
,
1656 .brl_cancel_windows_fn
= NULL
,
1657 .strict_lock_check_fn
= NULL
,
1658 .translate_name_fn
= NULL
,
1661 /* NT ACL Operations */
1662 .fget_nt_acl_fn
= NULL
,
1663 .get_nt_acl_fn
= NULL
,
1664 .fset_nt_acl_fn
= NULL
,
1665 .audit_file_fn
= NULL
,
1667 /* Posix ACL Operations */
1668 .sys_acl_get_file_fn
= posixacl_xattr_acl_get_file
,
1669 .sys_acl_get_fd_fn
= posixacl_xattr_acl_get_fd
,
1670 .sys_acl_blob_get_file_fn
= posix_sys_acl_blob_get_file
,
1671 .sys_acl_blob_get_fd_fn
= posix_sys_acl_blob_get_fd
,
1672 .sys_acl_set_file_fn
= posixacl_xattr_acl_set_file
,
1673 .sys_acl_set_fd_fn
= posixacl_xattr_acl_set_fd
,
1674 .sys_acl_delete_def_file_fn
= posixacl_xattr_acl_delete_def_file
,
1677 .getxattr_fn
= vfs_gluster_getxattr
,
1678 .getxattrat_send_fn
= vfs_not_implemented_getxattrat_send
,
1679 .getxattrat_recv_fn
= vfs_not_implemented_getxattrat_recv
,
1680 .fgetxattr_fn
= vfs_gluster_fgetxattr
,
1681 .listxattr_fn
= vfs_gluster_listxattr
,
1682 .flistxattr_fn
= vfs_gluster_flistxattr
,
1683 .removexattr_fn
= vfs_gluster_removexattr
,
1684 .fremovexattr_fn
= vfs_gluster_fremovexattr
,
1685 .setxattr_fn
= vfs_gluster_setxattr
,
1686 .fsetxattr_fn
= vfs_gluster_fsetxattr
,
1688 /* AIO Operations */
1689 .aio_force_fn
= vfs_gluster_aio_force
,
1691 /* Durable handle Operations */
1692 .durable_cookie_fn
= NULL
,
1693 .durable_disconnect_fn
= NULL
,
1694 .durable_reconnect_fn
= NULL
,
1698 NTSTATUS
vfs_glusterfs_init(TALLOC_CTX
*ctx
)
1700 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
1701 "glusterfs", &glusterfs_fns
);