kcc: Add corresponding methods for repsTo
[Samba.git] / source3 / modules / vfs_glusterfs.c
blobfbcaf5caea078167574cdbdb6cb46e59b95652af
1 /*
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/>.
22 /**
23 * @file vfs_glusterfs.c
24 * @author Anand Avati <avati@redhat.com>
25 * @date May 2013
26 * @brief Samba VFS module for glusterfs
28 * @todo
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.
38 #include "includes.h"
39 #include "smbd/smbd.h"
40 #include <stdio.h>
41 #include "api/glfs.h"
42 #include "lib/util/dlinklist.h"
43 #include "lib/util/tevent_unix.h"
44 #include "lib/tevent/tevent_internal.h"
45 #include "smbd/globals.h"
46 #include "lib/util/sys_rw.h"
47 #include "smbprofile.h"
48 #include "modules/posixacl_xattr.h"
50 #define DEFAULT_VOLFILE_SERVER "localhost"
52 static int read_fd = -1;
53 static int write_fd = -1;
54 static struct tevent_fd *aio_read_event = NULL;
56 /**
57 * Helper to convert struct stat to struct stat_ex.
59 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
61 ZERO_STRUCTP(dst);
63 dst->st_ex_dev = src->st_dev;
64 dst->st_ex_ino = src->st_ino;
65 dst->st_ex_mode = src->st_mode;
66 dst->st_ex_nlink = src->st_nlink;
67 dst->st_ex_uid = src->st_uid;
68 dst->st_ex_gid = src->st_gid;
69 dst->st_ex_rdev = src->st_rdev;
70 dst->st_ex_size = src->st_size;
71 dst->st_ex_atime.tv_sec = src->st_atime;
72 dst->st_ex_mtime.tv_sec = src->st_mtime;
73 dst->st_ex_ctime.tv_sec = src->st_ctime;
74 dst->st_ex_btime.tv_sec = src->st_mtime;
75 dst->st_ex_blksize = src->st_blksize;
76 dst->st_ex_blocks = src->st_blocks;
77 #ifdef STAT_HAVE_NSEC
78 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
79 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
80 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
81 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
82 #endif
85 /* pre-opened glfs_t */
87 static struct glfs_preopened {
88 char *volume;
89 char *connectpath;
90 glfs_t *fs;
91 int ref;
92 struct glfs_preopened *next, *prev;
93 } *glfs_preopened;
96 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
98 struct glfs_preopened *entry = NULL;
100 entry = talloc_zero(NULL, struct glfs_preopened);
101 if (!entry) {
102 errno = ENOMEM;
103 return -1;
106 entry->volume = talloc_strdup(entry, volume);
107 if (!entry->volume) {
108 talloc_free(entry);
109 errno = ENOMEM;
110 return -1;
113 entry->connectpath = talloc_strdup(entry, connectpath);
114 if (entry->connectpath == NULL) {
115 talloc_free(entry);
116 errno = ENOMEM;
117 return -1;
120 entry->fs = fs;
121 entry->ref = 1;
123 DLIST_ADD(glfs_preopened, entry);
125 return 0;
128 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
130 struct glfs_preopened *entry = NULL;
132 for (entry = glfs_preopened; entry; entry = entry->next) {
133 if (strcmp(entry->volume, volume) == 0 &&
134 strcmp(entry->connectpath, connectpath) == 0)
136 entry->ref++;
137 return entry->fs;
141 return NULL;
144 static void glfs_clear_preopened(glfs_t *fs)
146 struct glfs_preopened *entry = NULL;
148 for (entry = glfs_preopened; entry; entry = entry->next) {
149 if (entry->fs == fs) {
150 if (--entry->ref)
151 return;
153 DLIST_REMOVE(glfs_preopened, entry);
155 glfs_fini(entry->fs);
156 talloc_free(entry);
161 /* Disk Operations */
163 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
164 const char *service,
165 const char *user)
167 const char *volfile_server;
168 const char *volume;
169 char *logfile;
170 int loglevel;
171 glfs_t *fs = NULL;
172 TALLOC_CTX *tmp_ctx;
173 int ret = 0;
175 tmp_ctx = talloc_new(NULL);
176 if (tmp_ctx == NULL) {
177 ret = -1;
178 goto done;
180 logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
181 "logfile", NULL);
183 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
185 volfile_server = lp_parm_const_string(SNUM(handle->conn), "glusterfs",
186 "volfile_server", NULL);
187 if (volfile_server == NULL) {
188 volfile_server = DEFAULT_VOLFILE_SERVER;
191 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
192 NULL);
193 if (volume == NULL) {
194 volume = service;
197 fs = glfs_find_preopened(volume, handle->conn->connectpath);
198 if (fs) {
199 goto done;
202 fs = glfs_new(volume);
203 if (fs == NULL) {
204 ret = -1;
205 goto done;
208 ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 0);
209 if (ret < 0) {
210 DEBUG(0, ("Failed to set volfile_server %s\n", volfile_server));
211 goto done;
214 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
215 "true");
216 if (ret < 0) {
217 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
218 goto done;
222 ret = glfs_set_xlator_option(fs, "*-snapview-client",
223 "snapdir-entry-path",
224 handle->conn->connectpath);
225 if (ret < 0) {
226 DEBUG(0, ("%s: Failed to set xlator option:"
227 " snapdir-entry-path\n", volume));
228 glfs_fini(fs);
229 return -1;
232 ret = glfs_set_logging(fs, logfile, loglevel);
233 if (ret < 0) {
234 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
235 volume, logfile, loglevel));
236 goto done;
239 ret = glfs_init(fs);
240 if (ret < 0) {
241 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
242 volume, strerror(errno)));
243 goto done;
246 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
247 if (ret < 0) {
248 DEBUG(0, ("%s: Failed to register volume (%s)\n",
249 volume, strerror(errno)));
250 goto done;
252 done:
253 talloc_free(tmp_ctx);
254 if (ret < 0) {
255 if (fs)
256 glfs_fini(fs);
257 return -1;
258 } else {
259 DEBUG(0, ("%s: Initialized volume from server %s\n",
260 volume, volfile_server));
261 handle->data = fs;
262 return 0;
266 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
268 glfs_t *fs = NULL;
270 fs = handle->data;
272 glfs_clear_preopened(fs);
275 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
276 const char *path, uint64_t *bsize_p,
277 uint64_t *dfree_p, uint64_t *dsize_p)
279 struct statvfs statvfs = { 0, };
280 int ret;
282 ret = glfs_statvfs(handle->data, path, &statvfs);
283 if (ret < 0) {
284 return -1;
287 if (bsize_p != NULL) {
288 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
290 if (dfree_p != NULL) {
291 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
293 if (dsize_p != NULL) {
294 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
297 return (uint64_t)statvfs.f_bavail;
300 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
301 const char *path,
302 enum SMB_QUOTA_TYPE qtype, unid_t id,
303 SMB_DISK_QUOTA *qt)
305 errno = ENOSYS;
306 return -1;
309 static int
310 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
311 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
313 errno = ENOSYS;
314 return -1;
317 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
318 const char *path,
319 struct vfs_statvfs_struct *vfs_statvfs)
321 struct statvfs statvfs = { 0, };
322 int ret;
324 ret = glfs_statvfs(handle->data, path, &statvfs);
325 if (ret < 0) {
326 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
327 path, strerror(errno)));
328 return -1;
331 ZERO_STRUCTP(vfs_statvfs);
333 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
334 vfs_statvfs->BlockSize = statvfs.f_bsize;
335 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
336 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
337 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
338 vfs_statvfs->TotalFileNodes = statvfs.f_files;
339 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
340 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
341 vfs_statvfs->FsCapabilities =
342 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
344 return ret;
347 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
348 enum timestamp_set_resolution *p_ts_res)
350 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
352 #ifdef STAT_HAVE_NSEC
353 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
354 #endif
356 return caps;
359 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
360 const struct smb_filename *smb_fname,
361 const char *mask,
362 uint32_t attributes)
364 glfs_fd_t *fd;
366 fd = glfs_opendir(handle->data, smb_fname->base_name);
367 if (fd == NULL) {
368 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
369 smb_fname->base_name, strerror(errno)));
372 return (DIR *) fd;
375 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
376 files_struct *fsp, const char *mask,
377 uint32_t attributes)
379 return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
382 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
384 return glfs_closedir((void *)dirp);
387 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
388 DIR *dirp, SMB_STRUCT_STAT *sbuf)
390 static char direntbuf[512];
391 int ret;
392 struct stat stat;
393 struct dirent *dirent = 0;
395 if (sbuf != NULL) {
396 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
397 &dirent);
398 } else {
399 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
402 if ((ret < 0) || (dirent == NULL)) {
403 return NULL;
406 if (sbuf != NULL) {
407 smb_stat_ex_from_stat(sbuf, &stat);
410 return dirent;
413 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
415 return glfs_telldir((void *)dirp);
418 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
419 long offset)
421 glfs_seekdir((void *)dirp, offset);
424 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
426 glfs_seekdir((void *)dirp, 0);
429 static void vfs_gluster_init_search_op(struct vfs_handle_struct *handle,
430 DIR *dirp)
432 return;
435 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle,
436 const struct smb_filename *smb_fname,
437 mode_t mode)
439 return glfs_mkdir(handle->data, smb_fname->base_name, mode);
442 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle,
443 const struct smb_filename *smb_fname)
445 return glfs_rmdir(handle->data, smb_fname->base_name);
448 static int vfs_gluster_open(struct vfs_handle_struct *handle,
449 struct smb_filename *smb_fname, files_struct *fsp,
450 int flags, mode_t mode)
452 glfs_fd_t *glfd;
453 glfs_fd_t **p_tmp;
455 if (flags & O_DIRECTORY) {
456 glfd = glfs_opendir(handle->data, smb_fname->base_name);
457 } else if (flags & O_CREAT) {
458 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
459 mode);
460 } else {
461 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
464 if (glfd == NULL) {
465 return -1;
467 p_tmp = (glfs_fd_t **)VFS_ADD_FSP_EXTENSION(handle, fsp,
468 glfs_fd_t *, NULL);
469 *p_tmp = glfd;
470 /* An arbitrary value for error reporting, so you know its us. */
471 return 13371337;
474 static int vfs_gluster_close(struct vfs_handle_struct *handle,
475 files_struct *fsp)
477 glfs_fd_t *glfd;
478 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
479 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
480 return glfs_close(glfd);
483 static ssize_t vfs_gluster_read(struct vfs_handle_struct *handle,
484 files_struct *fsp, void *data, size_t n)
486 return glfs_read(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
489 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
490 files_struct *fsp, void *data, size_t n,
491 off_t offset)
493 return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
496 struct glusterfs_aio_state;
498 struct glusterfs_aio_wrapper {
499 struct glusterfs_aio_state *state;
502 struct glusterfs_aio_state {
503 ssize_t ret;
504 struct tevent_req *req;
505 bool cancelled;
506 struct vfs_aio_state vfs_aio_state;
507 struct timespec start;
510 static int aio_wrapper_destructor(struct glusterfs_aio_wrapper *wrap)
512 if (wrap->state != NULL) {
513 wrap->state->cancelled = true;
516 return 0;
520 * This function is the callback that will be called on glusterfs
521 * threads once the async IO submitted is complete. To notify
522 * Samba of the completion we use a pipe based queue.
524 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
526 struct glusterfs_aio_state *state = NULL;
527 int sts = 0;
528 struct timespec end;
530 state = (struct glusterfs_aio_state *)data;
532 PROFILE_TIMESTAMP(&end);
534 if (ret < 0) {
535 state->ret = -1;
536 state->vfs_aio_state.error = errno;
537 } else {
538 state->ret = ret;
540 state->vfs_aio_state.duration = nsec_time_diff(&end, &state->start);
543 * Write the state pointer to glusterfs_aio_state to the
544 * pipe, so we can call tevent_req_done() from the main thread,
545 * because tevent_req_done() is not designed to be executed in
546 * the multithread environment, so tevent_req_done() must be
547 * executed from the smbd main thread.
549 * write(2) on pipes with sizes under _POSIX_PIPE_BUF
550 * in size is atomic, without this, the use op pipes in this
551 * code would not work.
553 * sys_write is a thin enough wrapper around write(2)
554 * that we can trust it here.
557 sts = sys_write(write_fd, &state, sizeof(struct glusterfs_aio_state *));
558 if (sts < 0) {
559 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
562 return;
566 * Read each req off the pipe and process it.
568 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
569 struct tevent_fd *fde,
570 uint16_t flags, void *data)
572 struct tevent_req *req = NULL;
573 struct glusterfs_aio_state *state = NULL;
574 int sts = 0;
577 * read(2) on pipes is atomic if the needed data is available
578 * in the pipe, per SUS and POSIX. Because we always write
579 * to the pipe in sizeof(struct tevent_req *) chunks, we can
580 * always read in those chunks, atomically.
582 * sys_read is a thin enough wrapper around read(2) that we
583 * can trust it here.
586 sts = sys_read(read_fd, &state, sizeof(struct glusterfs_aio_state *));
588 if (sts < 0) {
589 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
592 /* if we've cancelled the op, there is no req, so just clean up. */
593 if (state->cancelled == true) {
594 TALLOC_FREE(state);
595 return;
598 req = state->req;
600 if (req) {
601 tevent_req_done(req);
603 return;
606 static bool init_gluster_aio(struct vfs_handle_struct *handle)
608 int fds[2];
609 int ret = -1;
611 if (read_fd != -1) {
613 * Already initialized.
615 return true;
618 ret = pipe(fds);
619 if (ret == -1) {
620 goto fail;
623 read_fd = fds[0];
624 write_fd = fds[1];
626 aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
627 NULL,
628 read_fd,
629 TEVENT_FD_READ,
630 aio_tevent_fd_done,
631 NULL);
632 if (aio_read_event == NULL) {
633 goto fail;
636 return true;
637 fail:
638 TALLOC_FREE(aio_read_event);
639 if (read_fd != -1) {
640 close(read_fd);
641 close(write_fd);
642 read_fd = -1;
643 write_fd = -1;
645 return false;
648 static struct glusterfs_aio_state *aio_state_create(TALLOC_CTX *mem_ctx)
650 struct tevent_req *req = NULL;
651 struct glusterfs_aio_state *state = NULL;
652 struct glusterfs_aio_wrapper *wrapper = NULL;
654 req = tevent_req_create(mem_ctx, &wrapper, struct glusterfs_aio_wrapper);
656 if (req == NULL) {
657 return NULL;
660 state = talloc_zero(NULL, struct glusterfs_aio_state);
662 if (state == NULL) {
663 TALLOC_FREE(req);
664 return NULL;
667 talloc_set_destructor(wrapper, aio_wrapper_destructor);
668 state->cancelled = false;
669 state->req = req;
671 wrapper->state = state;
673 return state;
676 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
677 *handle, TALLOC_CTX *mem_ctx,
678 struct tevent_context *ev,
679 files_struct *fsp,
680 void *data, size_t n,
681 off_t offset)
683 struct glusterfs_aio_state *state = NULL;
684 struct tevent_req *req = NULL;
685 int ret = 0;
687 state = aio_state_create(mem_ctx);
689 if (state == NULL) {
690 return NULL;
693 req = state->req;
695 if (!init_gluster_aio(handle)) {
696 tevent_req_error(req, EIO);
697 return tevent_req_post(req, ev);
700 PROFILE_TIMESTAMP(&state->start);
701 ret = glfs_pread_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
702 fsp), data, n, offset, 0, aio_glusterfs_done,
703 state);
704 if (ret < 0) {
705 tevent_req_error(req, -ret);
706 return tevent_req_post(req, ev);
709 return req;
712 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
713 *handle, TALLOC_CTX *mem_ctx,
714 struct tevent_context *ev,
715 files_struct *fsp,
716 const void *data, size_t n,
717 off_t offset)
719 struct glusterfs_aio_state *state = NULL;
720 struct tevent_req *req = NULL;
721 int ret = 0;
723 state = aio_state_create(mem_ctx);
725 if (state == NULL) {
726 return NULL;
729 req = state->req;
731 if (!init_gluster_aio(handle)) {
732 tevent_req_error(req, EIO);
733 return tevent_req_post(req, ev);
736 PROFILE_TIMESTAMP(&state->start);
737 ret = glfs_pwrite_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
738 fsp), data, n, offset, 0, aio_glusterfs_done,
739 state);
740 if (ret < 0) {
741 tevent_req_error(req, -ret);
742 return tevent_req_post(req, ev);
745 return req;
748 static ssize_t vfs_gluster_recv(struct tevent_req *req,
749 struct vfs_aio_state *vfs_aio_state)
751 struct glusterfs_aio_wrapper *wrapper = NULL;
752 int ret = 0;
754 wrapper = tevent_req_data(req, struct glusterfs_aio_wrapper);
756 if (wrapper == NULL) {
757 return -1;
760 if (wrapper->state == NULL) {
761 return -1;
764 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
765 return -1;
768 *vfs_aio_state = wrapper->state->vfs_aio_state;
769 ret = wrapper->state->ret;
771 /* Clean up the state, it is in a NULL context. */
773 TALLOC_FREE(wrapper->state);
775 return ret;
778 static ssize_t vfs_gluster_write(struct vfs_handle_struct *handle,
779 files_struct *fsp, const void *data, size_t n)
781 return glfs_write(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
784 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
785 files_struct *fsp, const void *data,
786 size_t n, off_t offset)
788 return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
791 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
792 files_struct *fsp, off_t offset, int whence)
794 return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
797 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
798 files_struct *fromfsp,
799 const DATA_BLOB *hdr,
800 off_t offset, size_t n)
802 errno = ENOTSUP;
803 return -1;
806 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
807 int fromfd, files_struct *tofsp,
808 off_t offset, size_t n)
810 errno = ENOTSUP;
811 return -1;
814 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
815 const struct smb_filename *smb_fname_src,
816 const struct smb_filename *smb_fname_dst)
818 return glfs_rename(handle->data, smb_fname_src->base_name,
819 smb_fname_dst->base_name);
822 static int vfs_gluster_fsync(struct vfs_handle_struct *handle,
823 files_struct *fsp)
825 return glfs_fsync(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp));
828 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
829 *handle, TALLOC_CTX *mem_ctx,
830 struct tevent_context *ev,
831 files_struct *fsp)
833 struct tevent_req *req = NULL;
834 struct glusterfs_aio_state *state = NULL;
835 int ret = 0;
837 state = aio_state_create(mem_ctx);
839 if (state == NULL) {
840 return NULL;
843 req = state->req;
845 if (!init_gluster_aio(handle)) {
846 tevent_req_error(req, EIO);
847 return tevent_req_post(req, ev);
850 PROFILE_TIMESTAMP(&state->start);
851 ret = glfs_fsync_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
852 fsp), aio_glusterfs_done, req);
853 if (ret < 0) {
854 tevent_req_error(req, -ret);
855 return tevent_req_post(req, ev);
857 return req;
860 static int vfs_gluster_fsync_recv(struct tevent_req *req,
861 struct vfs_aio_state *vfs_aio_state)
864 * Use implicit conversion ssize_t->int
866 return vfs_gluster_recv(req, vfs_aio_state);
869 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
870 struct smb_filename *smb_fname)
872 struct stat st;
873 int ret;
875 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
876 if (ret == 0) {
877 smb_stat_ex_from_stat(&smb_fname->st, &st);
879 if (ret < 0 && errno != ENOENT) {
880 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
881 smb_fname->base_name, strerror(errno)));
883 return ret;
886 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
887 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
889 struct stat st;
890 int ret;
892 ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
893 if (ret == 0) {
894 smb_stat_ex_from_stat(sbuf, &st);
896 if (ret < 0) {
897 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
898 fsp->fh->fd, strerror(errno)));
900 return ret;
903 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
904 struct smb_filename *smb_fname)
906 struct stat st;
907 int ret;
909 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
910 if (ret == 0) {
911 smb_stat_ex_from_stat(&smb_fname->st, &st);
913 if (ret < 0 && errno != ENOENT) {
914 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
915 smb_fname->base_name, strerror(errno)));
917 return ret;
920 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
921 files_struct *fsp,
922 const SMB_STRUCT_STAT *sbuf)
924 return sbuf->st_ex_blocks * 512;
927 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
928 const struct smb_filename *smb_fname)
930 return glfs_unlink(handle->data, smb_fname->base_name);
933 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
934 const struct smb_filename *smb_fname,
935 mode_t mode)
937 return glfs_chmod(handle->data, smb_fname->base_name, mode);
940 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
941 files_struct *fsp, mode_t mode)
943 return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
946 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
947 const struct smb_filename *smb_fname,
948 uid_t uid,
949 gid_t gid)
951 return glfs_chown(handle->data, smb_fname->base_name, uid, gid);
954 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
955 files_struct *fsp, uid_t uid, gid_t gid)
957 return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
960 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
961 const struct smb_filename *smb_fname,
962 uid_t uid,
963 gid_t gid)
965 return glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
968 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
970 return glfs_chdir(handle->data, path);
973 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
975 char *cwd;
976 char *ret;
978 cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
979 if (cwd == NULL) {
980 return NULL;
983 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
984 if (ret == 0) {
985 free(cwd);
987 return ret;
990 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
991 const struct smb_filename *smb_fname,
992 struct smb_file_time *ft)
994 struct timespec times[2];
996 if (null_timespec(ft->atime)) {
997 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
998 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
999 } else {
1000 times[0].tv_sec = ft->atime.tv_sec;
1001 times[0].tv_nsec = ft->atime.tv_nsec;
1004 if (null_timespec(ft->mtime)) {
1005 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
1006 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
1007 } else {
1008 times[1].tv_sec = ft->mtime.tv_sec;
1009 times[1].tv_nsec = ft->mtime.tv_nsec;
1012 if ((timespec_compare(&times[0],
1013 &smb_fname->st.st_ex_atime) == 0) &&
1014 (timespec_compare(&times[1],
1015 &smb_fname->st.st_ex_mtime) == 0)) {
1016 return 0;
1019 return glfs_utimens(handle->data, smb_fname->base_name, times);
1022 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1023 files_struct *fsp, off_t offset)
1025 return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
1028 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1029 struct files_struct *fsp,
1030 uint32_t mode,
1031 off_t offset, off_t len)
1033 /* TODO: add support using glfs_fallocate() and glfs_zerofill() */
1034 errno = ENOTSUP;
1035 return -1;
1038 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1039 const char *path)
1041 return glfs_realpath(handle->data, path, 0);
1044 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1045 files_struct *fsp, int op, off_t offset,
1046 off_t count, int type)
1048 struct flock flock = { 0, };
1049 int ret;
1051 flock.l_type = type;
1052 flock.l_whence = SEEK_SET;
1053 flock.l_start = offset;
1054 flock.l_len = count;
1055 flock.l_pid = 0;
1057 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
1059 if (op == F_GETLK) {
1060 /* lock query, true if someone else has locked */
1061 if ((ret != -1) &&
1062 (flock.l_type != F_UNLCK) &&
1063 (flock.l_pid != 0) && (flock.l_pid != getpid()))
1064 return true;
1065 /* not me */
1066 return false;
1069 if (ret == -1) {
1070 return false;
1073 return true;
1076 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1077 files_struct *fsp, uint32_t share_mode,
1078 uint32_t access_mask)
1080 errno = ENOSYS;
1081 return -1;
1084 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1085 files_struct *fsp, int leasetype)
1087 errno = ENOSYS;
1088 return -1;
1091 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1092 files_struct *fsp, off_t *poffset,
1093 off_t *pcount, int *ptype, pid_t *ppid)
1095 struct flock flock = { 0, };
1096 int ret;
1098 flock.l_type = *ptype;
1099 flock.l_whence = SEEK_SET;
1100 flock.l_start = *poffset;
1101 flock.l_len = *pcount;
1102 flock.l_pid = 0;
1104 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
1106 if (ret == -1) {
1107 return false;
1110 *ptype = flock.l_type;
1111 *poffset = flock.l_start;
1112 *pcount = flock.l_len;
1113 *ppid = flock.l_pid;
1115 return true;
1118 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1119 const char *oldpath, const char *newpath)
1121 return glfs_symlink(handle->data, oldpath, newpath);
1124 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1125 const char *path, char *buf, size_t bufsiz)
1127 return glfs_readlink(handle->data, path, buf, bufsiz);
1130 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1131 const char *oldpath, const char *newpath)
1133 return glfs_link(handle->data, oldpath, newpath);
1136 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
1137 mode_t mode, SMB_DEV_T dev)
1139 return glfs_mknod(handle->data, path, mode, dev);
1142 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1143 const char *path, unsigned int flags)
1145 errno = ENOSYS;
1146 return -1;
1149 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1150 const char *path, const char *name,
1151 TALLOC_CTX *mem_ctx, char **found_name)
1153 int ret;
1154 char key_buf[NAME_MAX + 64];
1155 char val_buf[NAME_MAX + 1];
1157 if (strlen(name) >= NAME_MAX) {
1158 errno = ENAMETOOLONG;
1159 return -1;
1162 snprintf(key_buf, NAME_MAX + 64,
1163 "glusterfs.get_real_filename:%s", name);
1165 ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1166 if (ret == -1) {
1167 if (errno == ENODATA) {
1168 errno = EOPNOTSUPP;
1170 return -1;
1173 *found_name = talloc_strdup(mem_ctx, val_buf);
1174 if (found_name[0] == NULL) {
1175 errno = ENOMEM;
1176 return -1;
1178 return 0;
1181 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1182 const char *filename)
1184 return handle->conn->connectpath;
1187 /* EA Operations */
1189 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1190 const char *path, const char *name,
1191 void *value, size_t size)
1193 return glfs_getxattr(handle->data, path, name, value, size);
1196 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1197 files_struct *fsp, const char *name,
1198 void *value, size_t size)
1200 return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
1203 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1204 const char *path, char *list, size_t size)
1206 return glfs_listxattr(handle->data, path, list, size);
1209 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1210 files_struct *fsp, char *list,
1211 size_t size)
1213 return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
1216 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1217 const char *path, const char *name)
1219 return glfs_removexattr(handle->data, path, name);
1222 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1223 files_struct *fsp, const char *name)
1225 return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
1228 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1229 const char *path, const char *name,
1230 const void *value, size_t size, int flags)
1232 return glfs_setxattr(handle->data, path, name, value, size, flags);
1235 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1236 files_struct *fsp, const char *name,
1237 const void *value, size_t size, int flags)
1239 return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
1240 flags);
1243 /* AIO Operations */
1245 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1246 files_struct *fsp)
1248 return false;
1251 /* Offline Operations */
1253 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
1254 const struct smb_filename *fname,
1255 SMB_STRUCT_STAT *sbuf)
1257 return false;
1260 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
1261 const struct smb_filename *fname)
1263 errno = ENOTSUP;
1264 return -1;
1267 static struct vfs_fn_pointers glusterfs_fns = {
1269 /* Disk Operations */
1271 .connect_fn = vfs_gluster_connect,
1272 .disconnect_fn = vfs_gluster_disconnect,
1273 .disk_free_fn = vfs_gluster_disk_free,
1274 .get_quota_fn = vfs_gluster_get_quota,
1275 .set_quota_fn = vfs_gluster_set_quota,
1276 .statvfs_fn = vfs_gluster_statvfs,
1277 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1279 .get_dfs_referrals_fn = NULL,
1281 /* Directory Operations */
1283 .opendir_fn = vfs_gluster_opendir,
1284 .fdopendir_fn = vfs_gluster_fdopendir,
1285 .readdir_fn = vfs_gluster_readdir,
1286 .seekdir_fn = vfs_gluster_seekdir,
1287 .telldir_fn = vfs_gluster_telldir,
1288 .rewind_dir_fn = vfs_gluster_rewinddir,
1289 .mkdir_fn = vfs_gluster_mkdir,
1290 .rmdir_fn = vfs_gluster_rmdir,
1291 .closedir_fn = vfs_gluster_closedir,
1292 .init_search_op_fn = vfs_gluster_init_search_op,
1294 /* File Operations */
1296 .open_fn = vfs_gluster_open,
1297 .create_file_fn = NULL,
1298 .close_fn = vfs_gluster_close,
1299 .read_fn = vfs_gluster_read,
1300 .pread_fn = vfs_gluster_pread,
1301 .pread_send_fn = vfs_gluster_pread_send,
1302 .pread_recv_fn = vfs_gluster_recv,
1303 .write_fn = vfs_gluster_write,
1304 .pwrite_fn = vfs_gluster_pwrite,
1305 .pwrite_send_fn = vfs_gluster_pwrite_send,
1306 .pwrite_recv_fn = vfs_gluster_recv,
1307 .lseek_fn = vfs_gluster_lseek,
1308 .sendfile_fn = vfs_gluster_sendfile,
1309 .recvfile_fn = vfs_gluster_recvfile,
1310 .rename_fn = vfs_gluster_rename,
1311 .fsync_fn = vfs_gluster_fsync,
1312 .fsync_send_fn = vfs_gluster_fsync_send,
1313 .fsync_recv_fn = vfs_gluster_fsync_recv,
1315 .stat_fn = vfs_gluster_stat,
1316 .fstat_fn = vfs_gluster_fstat,
1317 .lstat_fn = vfs_gluster_lstat,
1318 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1319 .unlink_fn = vfs_gluster_unlink,
1321 .chmod_fn = vfs_gluster_chmod,
1322 .fchmod_fn = vfs_gluster_fchmod,
1323 .chown_fn = vfs_gluster_chown,
1324 .fchown_fn = vfs_gluster_fchown,
1325 .lchown_fn = vfs_gluster_lchown,
1326 .chdir_fn = vfs_gluster_chdir,
1327 .getwd_fn = vfs_gluster_getwd,
1328 .ntimes_fn = vfs_gluster_ntimes,
1329 .ftruncate_fn = vfs_gluster_ftruncate,
1330 .fallocate_fn = vfs_gluster_fallocate,
1331 .lock_fn = vfs_gluster_lock,
1332 .kernel_flock_fn = vfs_gluster_kernel_flock,
1333 .linux_setlease_fn = vfs_gluster_linux_setlease,
1334 .getlock_fn = vfs_gluster_getlock,
1335 .symlink_fn = vfs_gluster_symlink,
1336 .readlink_fn = vfs_gluster_readlink,
1337 .link_fn = vfs_gluster_link,
1338 .mknod_fn = vfs_gluster_mknod,
1339 .realpath_fn = vfs_gluster_realpath,
1340 .chflags_fn = vfs_gluster_chflags,
1341 .file_id_create_fn = NULL,
1342 .copy_chunk_send_fn = NULL,
1343 .copy_chunk_recv_fn = NULL,
1344 .streaminfo_fn = NULL,
1345 .get_real_filename_fn = vfs_gluster_get_real_filename,
1346 .connectpath_fn = vfs_gluster_connectpath,
1348 .brl_lock_windows_fn = NULL,
1349 .brl_unlock_windows_fn = NULL,
1350 .brl_cancel_windows_fn = NULL,
1351 .strict_lock_fn = NULL,
1352 .strict_unlock_fn = NULL,
1353 .translate_name_fn = NULL,
1354 .fsctl_fn = NULL,
1356 /* NT ACL Operations */
1357 .fget_nt_acl_fn = NULL,
1358 .get_nt_acl_fn = NULL,
1359 .fset_nt_acl_fn = NULL,
1360 .audit_file_fn = NULL,
1362 /* Posix ACL Operations */
1363 .chmod_acl_fn = NULL, /* passthrough to default */
1364 .fchmod_acl_fn = NULL, /* passthrough to default */
1365 .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
1366 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1367 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1368 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1369 .sys_acl_set_file_fn = posixacl_xattr_acl_set_file,
1370 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1371 .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
1373 /* EA Operations */
1374 .getxattr_fn = vfs_gluster_getxattr,
1375 .fgetxattr_fn = vfs_gluster_fgetxattr,
1376 .listxattr_fn = vfs_gluster_listxattr,
1377 .flistxattr_fn = vfs_gluster_flistxattr,
1378 .removexattr_fn = vfs_gluster_removexattr,
1379 .fremovexattr_fn = vfs_gluster_fremovexattr,
1380 .setxattr_fn = vfs_gluster_setxattr,
1381 .fsetxattr_fn = vfs_gluster_fsetxattr,
1383 /* AIO Operations */
1384 .aio_force_fn = vfs_gluster_aio_force,
1386 /* Offline Operations */
1387 .is_offline_fn = vfs_gluster_is_offline,
1388 .set_offline_fn = vfs_gluster_set_offline,
1390 /* Durable handle Operations */
1391 .durable_cookie_fn = NULL,
1392 .durable_disconnect_fn = NULL,
1393 .durable_reconnect_fn = NULL,
1396 NTSTATUS vfs_glusterfs_init(void);
1397 NTSTATUS vfs_glusterfs_init(void)
1399 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1400 "glusterfs", &glusterfs_fns);