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"
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
)));
360 DBG_ERR("%s: Initialized volume from servers %s\n",
361 volume
, volfile_servers
);
364 talloc_free(tmp_ctx
);
368 static void vfs_gluster_disconnect(struct vfs_handle_struct
*handle
)
374 glfs_clear_preopened(fs
);
377 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct
*handle
,
378 const struct smb_filename
*smb_fname
,
383 struct statvfs statvfs
= { 0, };
386 ret
= glfs_statvfs(handle
->data
, smb_fname
->base_name
, &statvfs
);
391 if (bsize_p
!= NULL
) {
392 *bsize_p
= (uint64_t)statvfs
.f_bsize
; /* Block size */
394 if (dfree_p
!= NULL
) {
395 *dfree_p
= (uint64_t)statvfs
.f_bavail
; /* Available Block units */
397 if (dsize_p
!= NULL
) {
398 *dsize_p
= (uint64_t)statvfs
.f_blocks
; /* Total Block units */
401 return (uint64_t)statvfs
.f_bavail
;
404 static int vfs_gluster_get_quota(struct vfs_handle_struct
*handle
,
405 const struct smb_filename
*smb_fname
,
406 enum SMB_QUOTA_TYPE qtype
,
415 vfs_gluster_set_quota(struct vfs_handle_struct
*handle
,
416 enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*qt
)
422 static int vfs_gluster_statvfs(struct vfs_handle_struct
*handle
,
423 const struct smb_filename
*smb_fname
,
424 struct vfs_statvfs_struct
*vfs_statvfs
)
426 struct statvfs statvfs
= { 0, };
429 ret
= glfs_statvfs(handle
->data
, smb_fname
->base_name
, &statvfs
);
431 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
432 smb_fname
->base_name
, strerror(errno
)));
436 ZERO_STRUCTP(vfs_statvfs
);
438 vfs_statvfs
->OptimalTransferSize
= statvfs
.f_frsize
;
439 vfs_statvfs
->BlockSize
= statvfs
.f_bsize
;
440 vfs_statvfs
->TotalBlocks
= statvfs
.f_blocks
;
441 vfs_statvfs
->BlocksAvail
= statvfs
.f_bfree
;
442 vfs_statvfs
->UserBlocksAvail
= statvfs
.f_bavail
;
443 vfs_statvfs
->TotalFileNodes
= statvfs
.f_files
;
444 vfs_statvfs
->FreeFileNodes
= statvfs
.f_ffree
;
445 vfs_statvfs
->FsIdentifier
= statvfs
.f_fsid
;
446 vfs_statvfs
->FsCapabilities
=
447 FILE_CASE_SENSITIVE_SEARCH
| FILE_CASE_PRESERVED_NAMES
;
452 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct
*handle
,
453 enum timestamp_set_resolution
*p_ts_res
)
455 uint32_t caps
= FILE_CASE_SENSITIVE_SEARCH
| FILE_CASE_PRESERVED_NAMES
;
457 #ifdef STAT_HAVE_NSEC
458 *p_ts_res
= TIMESTAMP_SET_NT_OR_BETTER
;
464 static DIR *vfs_gluster_opendir(struct vfs_handle_struct
*handle
,
465 const struct smb_filename
*smb_fname
,
471 fd
= glfs_opendir(handle
->data
, smb_fname
->base_name
);
473 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
474 smb_fname
->base_name
, strerror(errno
)));
480 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct
*handle
,
481 files_struct
*fsp
, const char *mask
,
484 return (DIR *) *(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
487 static int vfs_gluster_closedir(struct vfs_handle_struct
*handle
, DIR *dirp
)
489 return glfs_closedir((void *)dirp
);
492 static struct dirent
*vfs_gluster_readdir(struct vfs_handle_struct
*handle
,
493 DIR *dirp
, SMB_STRUCT_STAT
*sbuf
)
495 static char direntbuf
[512];
498 struct dirent
*dirent
= 0;
501 ret
= glfs_readdirplus_r((void *)dirp
, &stat
, (void *)direntbuf
,
504 ret
= glfs_readdir_r((void *)dirp
, (void *)direntbuf
, &dirent
);
507 if ((ret
< 0) || (dirent
== NULL
)) {
512 smb_stat_ex_from_stat(sbuf
, &stat
);
518 static long vfs_gluster_telldir(struct vfs_handle_struct
*handle
, DIR *dirp
)
520 return glfs_telldir((void *)dirp
);
523 static void vfs_gluster_seekdir(struct vfs_handle_struct
*handle
, DIR *dirp
,
526 glfs_seekdir((void *)dirp
, offset
);
529 static void vfs_gluster_rewinddir(struct vfs_handle_struct
*handle
, DIR *dirp
)
531 glfs_seekdir((void *)dirp
, 0);
534 static void vfs_gluster_init_search_op(struct vfs_handle_struct
*handle
,
540 static int vfs_gluster_mkdir(struct vfs_handle_struct
*handle
,
541 const struct smb_filename
*smb_fname
,
544 return glfs_mkdir(handle
->data
, smb_fname
->base_name
, mode
);
547 static int vfs_gluster_rmdir(struct vfs_handle_struct
*handle
,
548 const struct smb_filename
*smb_fname
)
550 return glfs_rmdir(handle
->data
, smb_fname
->base_name
);
553 static int vfs_gluster_open(struct vfs_handle_struct
*handle
,
554 struct smb_filename
*smb_fname
, files_struct
*fsp
,
555 int flags
, mode_t mode
)
560 if (flags
& O_DIRECTORY
) {
561 glfd
= glfs_opendir(handle
->data
, smb_fname
->base_name
);
562 } else if (flags
& O_CREAT
) {
563 glfd
= glfs_creat(handle
->data
, smb_fname
->base_name
, flags
,
566 glfd
= glfs_open(handle
->data
, smb_fname
->base_name
, flags
);
572 p_tmp
= (glfs_fd_t
**)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
575 /* An arbitrary value for error reporting, so you know its us. */
579 static int vfs_gluster_close(struct vfs_handle_struct
*handle
,
583 glfd
= *(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
584 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
585 return glfs_close(glfd
);
588 static ssize_t
vfs_gluster_read(struct vfs_handle_struct
*handle
,
589 files_struct
*fsp
, void *data
, size_t n
)
591 return glfs_read(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), data
, n
, 0);
594 static ssize_t
vfs_gluster_pread(struct vfs_handle_struct
*handle
,
595 files_struct
*fsp
, void *data
, size_t n
,
598 return glfs_pread(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), data
, n
, offset
, 0);
601 struct glusterfs_aio_state
;
603 struct glusterfs_aio_wrapper
{
604 struct glusterfs_aio_state
*state
;
607 struct glusterfs_aio_state
{
609 struct tevent_req
*req
;
611 struct vfs_aio_state vfs_aio_state
;
612 struct timespec start
;
615 static int aio_wrapper_destructor(struct glusterfs_aio_wrapper
*wrap
)
617 if (wrap
->state
!= NULL
) {
618 wrap
->state
->cancelled
= true;
625 * This function is the callback that will be called on glusterfs
626 * threads once the async IO submitted is complete. To notify
627 * Samba of the completion we use a pipe based queue.
629 static void aio_glusterfs_done(glfs_fd_t
*fd
, ssize_t ret
, void *data
)
631 struct glusterfs_aio_state
*state
= NULL
;
635 state
= (struct glusterfs_aio_state
*)data
;
637 PROFILE_TIMESTAMP(&end
);
641 state
->vfs_aio_state
.error
= errno
;
645 state
->vfs_aio_state
.duration
= nsec_time_diff(&end
, &state
->start
);
648 * Write the state pointer to glusterfs_aio_state to the
649 * pipe, so we can call tevent_req_done() from the main thread,
650 * because tevent_req_done() is not designed to be executed in
651 * the multithread environment, so tevent_req_done() must be
652 * executed from the smbd main thread.
654 * write(2) on pipes with sizes under _POSIX_PIPE_BUF
655 * in size is atomic, without this, the use op pipes in this
656 * code would not work.
658 * sys_write is a thin enough wrapper around write(2)
659 * that we can trust it here.
662 sts
= sys_write(write_fd
, &state
, sizeof(struct glusterfs_aio_state
*));
664 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno
)));
671 * Read each req off the pipe and process it.
673 static void aio_tevent_fd_done(struct tevent_context
*event_ctx
,
674 struct tevent_fd
*fde
,
675 uint16_t flags
, void *data
)
677 struct tevent_req
*req
= NULL
;
678 struct glusterfs_aio_state
*state
= NULL
;
682 * read(2) on pipes is atomic if the needed data is available
683 * in the pipe, per SUS and POSIX. Because we always write
684 * to the pipe in sizeof(struct tevent_req *) chunks, we can
685 * always read in those chunks, atomically.
687 * sys_read is a thin enough wrapper around read(2) that we
691 sts
= sys_read(read_fd
, &state
, sizeof(struct glusterfs_aio_state
*));
694 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno
)));
697 /* if we've cancelled the op, there is no req, so just clean up. */
698 if (state
->cancelled
== true) {
706 tevent_req_done(req
);
711 static bool init_gluster_aio(struct vfs_handle_struct
*handle
)
718 * Already initialized.
731 aio_read_event
= tevent_add_fd(handle
->conn
->sconn
->ev_ctx
,
737 if (aio_read_event
== NULL
) {
743 TALLOC_FREE(aio_read_event
);
753 static struct glusterfs_aio_state
*aio_state_create(TALLOC_CTX
*mem_ctx
)
755 struct tevent_req
*req
= NULL
;
756 struct glusterfs_aio_state
*state
= NULL
;
757 struct glusterfs_aio_wrapper
*wrapper
= NULL
;
759 req
= tevent_req_create(mem_ctx
, &wrapper
, struct glusterfs_aio_wrapper
);
765 state
= talloc_zero(NULL
, struct glusterfs_aio_state
);
772 talloc_set_destructor(wrapper
, aio_wrapper_destructor
);
773 state
->cancelled
= false;
776 wrapper
->state
= state
;
781 static struct tevent_req
*vfs_gluster_pread_send(struct vfs_handle_struct
782 *handle
, TALLOC_CTX
*mem_ctx
,
783 struct tevent_context
*ev
,
785 void *data
, size_t n
,
788 struct glusterfs_aio_state
*state
= NULL
;
789 struct tevent_req
*req
= NULL
;
792 state
= aio_state_create(mem_ctx
);
800 if (!init_gluster_aio(handle
)) {
801 tevent_req_error(req
, EIO
);
802 return tevent_req_post(req
, ev
);
805 PROFILE_TIMESTAMP(&state
->start
);
806 ret
= glfs_pread_async(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
,
807 fsp
), data
, n
, offset
, 0, aio_glusterfs_done
,
810 tevent_req_error(req
, -ret
);
811 return tevent_req_post(req
, ev
);
817 static struct tevent_req
*vfs_gluster_pwrite_send(struct vfs_handle_struct
818 *handle
, TALLOC_CTX
*mem_ctx
,
819 struct tevent_context
*ev
,
821 const void *data
, size_t n
,
824 struct glusterfs_aio_state
*state
= NULL
;
825 struct tevent_req
*req
= NULL
;
828 state
= aio_state_create(mem_ctx
);
836 if (!init_gluster_aio(handle
)) {
837 tevent_req_error(req
, EIO
);
838 return tevent_req_post(req
, ev
);
841 PROFILE_TIMESTAMP(&state
->start
);
842 ret
= glfs_pwrite_async(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
,
843 fsp
), data
, n
, offset
, 0, aio_glusterfs_done
,
846 tevent_req_error(req
, -ret
);
847 return tevent_req_post(req
, ev
);
853 static ssize_t
vfs_gluster_recv(struct tevent_req
*req
,
854 struct vfs_aio_state
*vfs_aio_state
)
856 struct glusterfs_aio_wrapper
*wrapper
= NULL
;
859 wrapper
= tevent_req_data(req
, struct glusterfs_aio_wrapper
);
861 if (wrapper
== NULL
) {
865 if (wrapper
->state
== NULL
) {
869 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
873 *vfs_aio_state
= wrapper
->state
->vfs_aio_state
;
874 ret
= wrapper
->state
->ret
;
876 /* Clean up the state, it is in a NULL context. */
878 TALLOC_FREE(wrapper
->state
);
883 static ssize_t
vfs_gluster_write(struct vfs_handle_struct
*handle
,
884 files_struct
*fsp
, const void *data
, size_t n
)
886 return glfs_write(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), data
, n
, 0);
889 static ssize_t
vfs_gluster_pwrite(struct vfs_handle_struct
*handle
,
890 files_struct
*fsp
, const void *data
,
891 size_t n
, off_t offset
)
893 return glfs_pwrite(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), data
, n
, offset
, 0);
896 static off_t
vfs_gluster_lseek(struct vfs_handle_struct
*handle
,
897 files_struct
*fsp
, off_t offset
, int whence
)
899 return glfs_lseek(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), offset
, whence
);
902 static ssize_t
vfs_gluster_sendfile(struct vfs_handle_struct
*handle
, int tofd
,
903 files_struct
*fromfsp
,
904 const DATA_BLOB
*hdr
,
905 off_t offset
, size_t n
)
911 static ssize_t
vfs_gluster_recvfile(struct vfs_handle_struct
*handle
,
912 int fromfd
, files_struct
*tofsp
,
913 off_t offset
, size_t n
)
919 static int vfs_gluster_rename(struct vfs_handle_struct
*handle
,
920 const struct smb_filename
*smb_fname_src
,
921 const struct smb_filename
*smb_fname_dst
)
923 return glfs_rename(handle
->data
, smb_fname_src
->base_name
,
924 smb_fname_dst
->base_name
);
927 static int vfs_gluster_fsync(struct vfs_handle_struct
*handle
,
930 return glfs_fsync(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
));
933 static struct tevent_req
*vfs_gluster_fsync_send(struct vfs_handle_struct
934 *handle
, TALLOC_CTX
*mem_ctx
,
935 struct tevent_context
*ev
,
938 struct tevent_req
*req
= NULL
;
939 struct glusterfs_aio_state
*state
= NULL
;
942 state
= aio_state_create(mem_ctx
);
950 if (!init_gluster_aio(handle
)) {
951 tevent_req_error(req
, EIO
);
952 return tevent_req_post(req
, ev
);
955 PROFILE_TIMESTAMP(&state
->start
);
956 ret
= glfs_fsync_async(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
,
957 fsp
), aio_glusterfs_done
, req
);
959 tevent_req_error(req
, -ret
);
960 return tevent_req_post(req
, ev
);
965 static int vfs_gluster_fsync_recv(struct tevent_req
*req
,
966 struct vfs_aio_state
*vfs_aio_state
)
969 * Use implicit conversion ssize_t->int
971 return vfs_gluster_recv(req
, vfs_aio_state
);
974 static int vfs_gluster_stat(struct vfs_handle_struct
*handle
,
975 struct smb_filename
*smb_fname
)
980 ret
= glfs_stat(handle
->data
, smb_fname
->base_name
, &st
);
982 smb_stat_ex_from_stat(&smb_fname
->st
, &st
);
984 if (ret
< 0 && errno
!= ENOENT
) {
985 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
986 smb_fname
->base_name
, strerror(errno
)));
991 static int vfs_gluster_fstat(struct vfs_handle_struct
*handle
,
992 files_struct
*fsp
, SMB_STRUCT_STAT
*sbuf
)
997 ret
= glfs_fstat(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), &st
);
999 smb_stat_ex_from_stat(sbuf
, &st
);
1002 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1003 fsp
->fh
->fd
, strerror(errno
)));
1008 static int vfs_gluster_lstat(struct vfs_handle_struct
*handle
,
1009 struct smb_filename
*smb_fname
)
1014 ret
= glfs_lstat(handle
->data
, smb_fname
->base_name
, &st
);
1016 smb_stat_ex_from_stat(&smb_fname
->st
, &st
);
1018 if (ret
< 0 && errno
!= ENOENT
) {
1019 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1020 smb_fname
->base_name
, strerror(errno
)));
1025 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct
*handle
,
1027 const SMB_STRUCT_STAT
*sbuf
)
1029 return sbuf
->st_ex_blocks
* 512;
1032 static int vfs_gluster_unlink(struct vfs_handle_struct
*handle
,
1033 const struct smb_filename
*smb_fname
)
1035 return glfs_unlink(handle
->data
, smb_fname
->base_name
);
1038 static int vfs_gluster_chmod(struct vfs_handle_struct
*handle
,
1039 const struct smb_filename
*smb_fname
,
1042 return glfs_chmod(handle
->data
, smb_fname
->base_name
, mode
);
1045 static int vfs_gluster_fchmod(struct vfs_handle_struct
*handle
,
1046 files_struct
*fsp
, mode_t mode
)
1048 return glfs_fchmod(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), mode
);
1051 static int vfs_gluster_chown(struct vfs_handle_struct
*handle
,
1052 const struct smb_filename
*smb_fname
,
1056 return glfs_chown(handle
->data
, smb_fname
->base_name
, uid
, gid
);
1059 static int vfs_gluster_fchown(struct vfs_handle_struct
*handle
,
1060 files_struct
*fsp
, uid_t uid
, gid_t gid
)
1062 return glfs_fchown(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), uid
, gid
);
1065 static int vfs_gluster_lchown(struct vfs_handle_struct
*handle
,
1066 const struct smb_filename
*smb_fname
,
1070 return glfs_lchown(handle
->data
, smb_fname
->base_name
, uid
, gid
);
1073 static int vfs_gluster_chdir(struct vfs_handle_struct
*handle
,
1074 const struct smb_filename
*smb_fname
)
1076 return glfs_chdir(handle
->data
, smb_fname
->base_name
);
1079 static struct smb_filename
*vfs_gluster_getwd(struct vfs_handle_struct
*handle
,
1084 struct smb_filename
*smb_fname
= NULL
;
1086 cwd
= SMB_CALLOC_ARRAY(char, PATH_MAX
);
1091 ret
= glfs_getcwd(handle
->data
, cwd
, PATH_MAX
- 1);
1095 smb_fname
= synthetic_smb_fname(ctx
,
1104 static int vfs_gluster_ntimes(struct vfs_handle_struct
*handle
,
1105 const struct smb_filename
*smb_fname
,
1106 struct smb_file_time
*ft
)
1108 struct timespec times
[2];
1110 if (null_timespec(ft
->atime
)) {
1111 times
[0].tv_sec
= smb_fname
->st
.st_ex_atime
.tv_sec
;
1112 times
[0].tv_nsec
= smb_fname
->st
.st_ex_atime
.tv_nsec
;
1114 times
[0].tv_sec
= ft
->atime
.tv_sec
;
1115 times
[0].tv_nsec
= ft
->atime
.tv_nsec
;
1118 if (null_timespec(ft
->mtime
)) {
1119 times
[1].tv_sec
= smb_fname
->st
.st_ex_mtime
.tv_sec
;
1120 times
[1].tv_nsec
= smb_fname
->st
.st_ex_mtime
.tv_nsec
;
1122 times
[1].tv_sec
= ft
->mtime
.tv_sec
;
1123 times
[1].tv_nsec
= ft
->mtime
.tv_nsec
;
1126 if ((timespec_compare(×
[0],
1127 &smb_fname
->st
.st_ex_atime
) == 0) &&
1128 (timespec_compare(×
[1],
1129 &smb_fname
->st
.st_ex_mtime
) == 0)) {
1133 return glfs_utimens(handle
->data
, smb_fname
->base_name
, times
);
1136 static int vfs_gluster_ftruncate(struct vfs_handle_struct
*handle
,
1137 files_struct
*fsp
, off_t offset
)
1139 return glfs_ftruncate(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), offset
);
1142 static int vfs_gluster_fallocate(struct vfs_handle_struct
*handle
,
1143 struct files_struct
*fsp
,
1145 off_t offset
, off_t len
)
1147 /* TODO: add support using glfs_fallocate() and glfs_zerofill() */
1152 static struct smb_filename
*vfs_gluster_realpath(struct vfs_handle_struct
*handle
,
1154 const struct smb_filename
*smb_fname
)
1156 char *result
= NULL
;
1157 struct smb_filename
*result_fname
= NULL
;
1158 char *resolved_path
= SMB_MALLOC_ARRAY(char, PATH_MAX
+1);
1160 if (resolved_path
== NULL
) {
1165 result
= glfs_realpath(handle
->data
,
1166 smb_fname
->base_name
,
1168 if (result
!= NULL
) {
1169 result_fname
= synthetic_smb_fname(ctx
, result
, NULL
, NULL
, 0);
1172 SAFE_FREE(resolved_path
);
1173 return result_fname
;
1176 static bool vfs_gluster_lock(struct vfs_handle_struct
*handle
,
1177 files_struct
*fsp
, int op
, off_t offset
,
1178 off_t count
, int type
)
1180 struct flock flock
= { 0, };
1183 flock
.l_type
= type
;
1184 flock
.l_whence
= SEEK_SET
;
1185 flock
.l_start
= offset
;
1186 flock
.l_len
= count
;
1189 ret
= glfs_posix_lock(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), op
, &flock
);
1191 if (op
== F_GETLK
) {
1192 /* lock query, true if someone else has locked */
1194 (flock
.l_type
!= F_UNLCK
) &&
1195 (flock
.l_pid
!= 0) && (flock
.l_pid
!= getpid()))
1208 static int vfs_gluster_kernel_flock(struct vfs_handle_struct
*handle
,
1209 files_struct
*fsp
, uint32_t share_mode
,
1210 uint32_t access_mask
)
1216 static int vfs_gluster_linux_setlease(struct vfs_handle_struct
*handle
,
1217 files_struct
*fsp
, int leasetype
)
1223 static bool vfs_gluster_getlock(struct vfs_handle_struct
*handle
,
1224 files_struct
*fsp
, off_t
*poffset
,
1225 off_t
*pcount
, int *ptype
, pid_t
*ppid
)
1227 struct flock flock
= { 0, };
1230 flock
.l_type
= *ptype
;
1231 flock
.l_whence
= SEEK_SET
;
1232 flock
.l_start
= *poffset
;
1233 flock
.l_len
= *pcount
;
1236 ret
= glfs_posix_lock(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), F_GETLK
, &flock
);
1242 *ptype
= flock
.l_type
;
1243 *poffset
= flock
.l_start
;
1244 *pcount
= flock
.l_len
;
1245 *ppid
= flock
.l_pid
;
1250 static int vfs_gluster_symlink(struct vfs_handle_struct
*handle
,
1251 const char *link_target
,
1252 const struct smb_filename
*new_smb_fname
)
1254 return glfs_symlink(handle
->data
,
1256 new_smb_fname
->base_name
);
1259 static int vfs_gluster_readlink(struct vfs_handle_struct
*handle
,
1260 const struct smb_filename
*smb_fname
,
1264 return glfs_readlink(handle
->data
, smb_fname
->base_name
, buf
, bufsiz
);
1267 static int vfs_gluster_link(struct vfs_handle_struct
*handle
,
1268 const struct smb_filename
*old_smb_fname
,
1269 const struct smb_filename
*new_smb_fname
)
1271 return glfs_link(handle
->data
,
1272 old_smb_fname
->base_name
,
1273 new_smb_fname
->base_name
);
1276 static int vfs_gluster_mknod(struct vfs_handle_struct
*handle
,
1277 const struct smb_filename
*smb_fname
,
1281 return glfs_mknod(handle
->data
, smb_fname
->base_name
, mode
, dev
);
1284 static int vfs_gluster_chflags(struct vfs_handle_struct
*handle
,
1285 const struct smb_filename
*smb_fname
,
1292 static int vfs_gluster_get_real_filename(struct vfs_handle_struct
*handle
,
1293 const char *path
, const char *name
,
1294 TALLOC_CTX
*mem_ctx
, char **found_name
)
1297 char key_buf
[NAME_MAX
+ 64];
1298 char val_buf
[NAME_MAX
+ 1];
1300 if (strlen(name
) >= NAME_MAX
) {
1301 errno
= ENAMETOOLONG
;
1305 snprintf(key_buf
, NAME_MAX
+ 64,
1306 "glusterfs.get_real_filename:%s", name
);
1308 ret
= glfs_getxattr(handle
->data
, path
, key_buf
, val_buf
, NAME_MAX
+ 1);
1310 if (errno
== ENODATA
) {
1316 *found_name
= talloc_strdup(mem_ctx
, val_buf
);
1317 if (found_name
[0] == NULL
) {
1324 static const char *vfs_gluster_connectpath(struct vfs_handle_struct
*handle
,
1325 const struct smb_filename
*smb_fname
)
1327 return handle
->conn
->connectpath
;
1332 static ssize_t
vfs_gluster_getxattr(struct vfs_handle_struct
*handle
,
1333 const struct smb_filename
*smb_fname
,
1338 return glfs_getxattr(handle
->data
, smb_fname
->base_name
,
1342 static ssize_t
vfs_gluster_fgetxattr(struct vfs_handle_struct
*handle
,
1343 files_struct
*fsp
, const char *name
,
1344 void *value
, size_t size
)
1346 return glfs_fgetxattr(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), name
, value
, size
);
1349 static ssize_t
vfs_gluster_listxattr(struct vfs_handle_struct
*handle
,
1350 const struct smb_filename
*smb_fname
,
1354 return glfs_listxattr(handle
->data
, smb_fname
->base_name
, list
, size
);
1357 static ssize_t
vfs_gluster_flistxattr(struct vfs_handle_struct
*handle
,
1358 files_struct
*fsp
, char *list
,
1361 return glfs_flistxattr(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), list
, size
);
1364 static int vfs_gluster_removexattr(struct vfs_handle_struct
*handle
,
1365 const struct smb_filename
*smb_fname
,
1368 return glfs_removexattr(handle
->data
, smb_fname
->base_name
, name
);
1371 static int vfs_gluster_fremovexattr(struct vfs_handle_struct
*handle
,
1372 files_struct
*fsp
, const char *name
)
1374 return glfs_fremovexattr(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), name
);
1377 static int vfs_gluster_setxattr(struct vfs_handle_struct
*handle
,
1378 const struct smb_filename
*smb_fname
,
1380 const void *value
, size_t size
, int flags
)
1382 return glfs_setxattr(handle
->data
, smb_fname
->base_name
, name
, value
, size
, flags
);
1385 static int vfs_gluster_fsetxattr(struct vfs_handle_struct
*handle
,
1386 files_struct
*fsp
, const char *name
,
1387 const void *value
, size_t size
, int flags
)
1389 return glfs_fsetxattr(*(glfs_fd_t
**)VFS_FETCH_FSP_EXTENSION(handle
, fsp
), name
, value
, size
,
1393 /* AIO Operations */
1395 static bool vfs_gluster_aio_force(struct vfs_handle_struct
*handle
,
1401 static struct vfs_fn_pointers glusterfs_fns
= {
1403 /* Disk Operations */
1405 .connect_fn
= vfs_gluster_connect
,
1406 .disconnect_fn
= vfs_gluster_disconnect
,
1407 .disk_free_fn
= vfs_gluster_disk_free
,
1408 .get_quota_fn
= vfs_gluster_get_quota
,
1409 .set_quota_fn
= vfs_gluster_set_quota
,
1410 .statvfs_fn
= vfs_gluster_statvfs
,
1411 .fs_capabilities_fn
= vfs_gluster_fs_capabilities
,
1413 .get_dfs_referrals_fn
= NULL
,
1415 /* Directory Operations */
1417 .opendir_fn
= vfs_gluster_opendir
,
1418 .fdopendir_fn
= vfs_gluster_fdopendir
,
1419 .readdir_fn
= vfs_gluster_readdir
,
1420 .seekdir_fn
= vfs_gluster_seekdir
,
1421 .telldir_fn
= vfs_gluster_telldir
,
1422 .rewind_dir_fn
= vfs_gluster_rewinddir
,
1423 .mkdir_fn
= vfs_gluster_mkdir
,
1424 .rmdir_fn
= vfs_gluster_rmdir
,
1425 .closedir_fn
= vfs_gluster_closedir
,
1426 .init_search_op_fn
= vfs_gluster_init_search_op
,
1428 /* File Operations */
1430 .open_fn
= vfs_gluster_open
,
1431 .create_file_fn
= NULL
,
1432 .close_fn
= vfs_gluster_close
,
1433 .read_fn
= vfs_gluster_read
,
1434 .pread_fn
= vfs_gluster_pread
,
1435 .pread_send_fn
= vfs_gluster_pread_send
,
1436 .pread_recv_fn
= vfs_gluster_recv
,
1437 .write_fn
= vfs_gluster_write
,
1438 .pwrite_fn
= vfs_gluster_pwrite
,
1439 .pwrite_send_fn
= vfs_gluster_pwrite_send
,
1440 .pwrite_recv_fn
= vfs_gluster_recv
,
1441 .lseek_fn
= vfs_gluster_lseek
,
1442 .sendfile_fn
= vfs_gluster_sendfile
,
1443 .recvfile_fn
= vfs_gluster_recvfile
,
1444 .rename_fn
= vfs_gluster_rename
,
1445 .fsync_fn
= vfs_gluster_fsync
,
1446 .fsync_send_fn
= vfs_gluster_fsync_send
,
1447 .fsync_recv_fn
= vfs_gluster_fsync_recv
,
1449 .stat_fn
= vfs_gluster_stat
,
1450 .fstat_fn
= vfs_gluster_fstat
,
1451 .lstat_fn
= vfs_gluster_lstat
,
1452 .get_alloc_size_fn
= vfs_gluster_get_alloc_size
,
1453 .unlink_fn
= vfs_gluster_unlink
,
1455 .chmod_fn
= vfs_gluster_chmod
,
1456 .fchmod_fn
= vfs_gluster_fchmod
,
1457 .chown_fn
= vfs_gluster_chown
,
1458 .fchown_fn
= vfs_gluster_fchown
,
1459 .lchown_fn
= vfs_gluster_lchown
,
1460 .chdir_fn
= vfs_gluster_chdir
,
1461 .getwd_fn
= vfs_gluster_getwd
,
1462 .ntimes_fn
= vfs_gluster_ntimes
,
1463 .ftruncate_fn
= vfs_gluster_ftruncate
,
1464 .fallocate_fn
= vfs_gluster_fallocate
,
1465 .lock_fn
= vfs_gluster_lock
,
1466 .kernel_flock_fn
= vfs_gluster_kernel_flock
,
1467 .linux_setlease_fn
= vfs_gluster_linux_setlease
,
1468 .getlock_fn
= vfs_gluster_getlock
,
1469 .symlink_fn
= vfs_gluster_symlink
,
1470 .readlink_fn
= vfs_gluster_readlink
,
1471 .link_fn
= vfs_gluster_link
,
1472 .mknod_fn
= vfs_gluster_mknod
,
1473 .realpath_fn
= vfs_gluster_realpath
,
1474 .chflags_fn
= vfs_gluster_chflags
,
1475 .file_id_create_fn
= NULL
,
1476 .streaminfo_fn
= NULL
,
1477 .get_real_filename_fn
= vfs_gluster_get_real_filename
,
1478 .connectpath_fn
= vfs_gluster_connectpath
,
1480 .brl_lock_windows_fn
= NULL
,
1481 .brl_unlock_windows_fn
= NULL
,
1482 .brl_cancel_windows_fn
= NULL
,
1483 .strict_lock_check_fn
= NULL
,
1484 .translate_name_fn
= NULL
,
1487 /* NT ACL Operations */
1488 .fget_nt_acl_fn
= NULL
,
1489 .get_nt_acl_fn
= NULL
,
1490 .fset_nt_acl_fn
= NULL
,
1491 .audit_file_fn
= NULL
,
1493 /* Posix ACL Operations */
1494 .chmod_acl_fn
= NULL
, /* passthrough to default */
1495 .fchmod_acl_fn
= NULL
, /* passthrough to default */
1496 .sys_acl_get_file_fn
= posixacl_xattr_acl_get_file
,
1497 .sys_acl_get_fd_fn
= posixacl_xattr_acl_get_fd
,
1498 .sys_acl_blob_get_file_fn
= posix_sys_acl_blob_get_file
,
1499 .sys_acl_blob_get_fd_fn
= posix_sys_acl_blob_get_fd
,
1500 .sys_acl_set_file_fn
= posixacl_xattr_acl_set_file
,
1501 .sys_acl_set_fd_fn
= posixacl_xattr_acl_set_fd
,
1502 .sys_acl_delete_def_file_fn
= posixacl_xattr_acl_delete_def_file
,
1505 .getxattr_fn
= vfs_gluster_getxattr
,
1506 .fgetxattr_fn
= vfs_gluster_fgetxattr
,
1507 .listxattr_fn
= vfs_gluster_listxattr
,
1508 .flistxattr_fn
= vfs_gluster_flistxattr
,
1509 .removexattr_fn
= vfs_gluster_removexattr
,
1510 .fremovexattr_fn
= vfs_gluster_fremovexattr
,
1511 .setxattr_fn
= vfs_gluster_setxattr
,
1512 .fsetxattr_fn
= vfs_gluster_fsetxattr
,
1514 /* AIO Operations */
1515 .aio_force_fn
= vfs_gluster_aio_force
,
1517 /* Durable handle Operations */
1518 .durable_cookie_fn
= NULL
,
1519 .durable_disconnect_fn
= NULL
,
1520 .durable_reconnect_fn
= NULL
,
1523 NTSTATUS
vfs_glusterfs_init(TALLOC_CTX
*);
1524 NTSTATUS
vfs_glusterfs_init(TALLOC_CTX
*ctx
)
1526 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
1527 "glusterfs", &glusterfs_fns
);