vfs_glusterfs: Replace eventfd with pipes, for AIO use
[Samba.git] / source3 / modules / vfs_glusterfs.c
blob12ec0b7f9c37dbb163d841b2a5e14118956a8f61
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/sys_rw.h"
48 #define DEFAULT_VOLFILE_SERVER "localhost"
50 static int read_fd = -1;
51 static int write_fd = -1;
52 static struct tevent_fd *aio_read_event = NULL;
54 /**
55 * Helper to convert struct stat to struct stat_ex.
57 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
59 ZERO_STRUCTP(dst);
61 dst->st_ex_dev = src->st_dev;
62 dst->st_ex_ino = src->st_ino;
63 dst->st_ex_mode = src->st_mode;
64 dst->st_ex_nlink = src->st_nlink;
65 dst->st_ex_uid = src->st_uid;
66 dst->st_ex_gid = src->st_gid;
67 dst->st_ex_rdev = src->st_rdev;
68 dst->st_ex_size = src->st_size;
69 dst->st_ex_atime.tv_sec = src->st_atime;
70 dst->st_ex_mtime.tv_sec = src->st_mtime;
71 dst->st_ex_ctime.tv_sec = src->st_ctime;
72 dst->st_ex_btime.tv_sec = src->st_mtime;
73 dst->st_ex_blksize = src->st_blksize;
74 dst->st_ex_blocks = src->st_blocks;
75 #ifdef STAT_HAVE_NSEC
76 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
77 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
78 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
79 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
80 #endif
83 /* pre-opened glfs_t */
85 static struct glfs_preopened {
86 char *volume;
87 char *connectpath;
88 glfs_t *fs;
89 int ref;
90 struct glfs_preopened *next, *prev;
91 } *glfs_preopened;
94 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
96 struct glfs_preopened *entry = NULL;
98 entry = talloc_zero(NULL, struct glfs_preopened);
99 if (!entry) {
100 errno = ENOMEM;
101 return -1;
104 entry->volume = talloc_strdup(entry, volume);
105 if (!entry->volume) {
106 talloc_free(entry);
107 errno = ENOMEM;
108 return -1;
111 entry->connectpath = talloc_strdup(entry, connectpath);
112 if (entry->connectpath == NULL) {
113 talloc_free(entry);
114 errno = ENOMEM;
115 return -1;
118 entry->fs = fs;
119 entry->ref = 1;
121 DLIST_ADD(glfs_preopened, entry);
123 return 0;
126 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
128 struct glfs_preopened *entry = NULL;
130 for (entry = glfs_preopened; entry; entry = entry->next) {
131 if (strcmp(entry->volume, volume) == 0 &&
132 strcmp(entry->connectpath, connectpath) == 0)
134 entry->ref++;
135 return entry->fs;
139 return NULL;
142 static void glfs_clear_preopened(glfs_t *fs)
144 struct glfs_preopened *entry = NULL;
146 for (entry = glfs_preopened; entry; entry = entry->next) {
147 if (entry->fs == fs) {
148 if (--entry->ref)
149 return;
151 DLIST_REMOVE(glfs_preopened, entry);
153 glfs_fini(entry->fs);
154 talloc_free(entry);
159 /* Disk Operations */
161 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
162 const char *service,
163 const char *user)
165 const char *volfile_server;
166 const char *volume;
167 char *logfile;
168 int loglevel;
169 glfs_t *fs = NULL;
170 TALLOC_CTX *tmp_ctx;
171 int ret = 0;
173 tmp_ctx = talloc_new(NULL);
174 if (tmp_ctx == NULL) {
175 ret = -1;
176 goto done;
178 logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
179 "logfile", NULL);
181 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
183 volfile_server = lp_parm_const_string(SNUM(handle->conn), "glusterfs",
184 "volfile_server", NULL);
185 if (volfile_server == NULL) {
186 volfile_server = DEFAULT_VOLFILE_SERVER;
189 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
190 NULL);
191 if (volume == NULL) {
192 volume = service;
195 fs = glfs_find_preopened(volume, handle->conn->connectpath);
196 if (fs) {
197 goto done;
200 fs = glfs_new(volume);
201 if (fs == NULL) {
202 ret = -1;
203 goto done;
206 ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 0);
207 if (ret < 0) {
208 DEBUG(0, ("Failed to set volfile_server %s\n", volfile_server));
209 goto done;
212 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
213 "true");
214 if (ret < 0) {
215 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
216 goto done;
220 ret = glfs_set_xlator_option(fs, "*-snapview-client",
221 "snapdir-entry-path",
222 handle->conn->connectpath);
223 if (ret < 0) {
224 DEBUG(0, ("%s: Failed to set xlator option:"
225 " snapdir-entry-path\n", volume));
226 glfs_fini(fs);
227 return -1;
230 ret = glfs_set_logging(fs, logfile, loglevel);
231 if (ret < 0) {
232 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
233 volume, logfile, loglevel));
234 goto done;
237 ret = glfs_init(fs);
238 if (ret < 0) {
239 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
240 volume, strerror(errno)));
241 goto done;
244 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
245 if (ret < 0) {
246 DEBUG(0, ("%s: Failed to register volume (%s)\n",
247 volume, strerror(errno)));
248 goto done;
250 done:
251 talloc_free(tmp_ctx);
252 if (ret < 0) {
253 if (fs)
254 glfs_fini(fs);
255 return -1;
256 } else {
257 DEBUG(0, ("%s: Initialized volume from server %s\n",
258 volume, volfile_server));
259 handle->data = fs;
260 return 0;
264 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
266 glfs_t *fs = NULL;
268 fs = handle->data;
270 glfs_clear_preopened(fs);
273 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
274 const char *path, bool small_query,
275 uint64_t *bsize_p, uint64_t *dfree_p,
276 uint64_t *dsize_p)
278 struct statvfs statvfs = { 0, };
279 int ret;
281 ret = glfs_statvfs(handle->data, path, &statvfs);
282 if (ret < 0) {
283 return -1;
286 if (bsize_p != NULL) {
287 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
289 if (dfree_p != NULL) {
290 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
292 if (dsize_p != NULL) {
293 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
296 return (uint64_t)statvfs.f_bavail;
299 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
300 enum SMB_QUOTA_TYPE qtype, unid_t id,
301 SMB_DISK_QUOTA *qt)
303 errno = ENOSYS;
304 return -1;
307 static int
308 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
309 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
311 errno = ENOSYS;
312 return -1;
315 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
316 const char *path,
317 struct vfs_statvfs_struct *vfs_statvfs)
319 struct statvfs statvfs = { 0, };
320 int ret;
322 ret = glfs_statvfs(handle->data, path, &statvfs);
323 if (ret < 0) {
324 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
325 path, strerror(errno)));
326 return -1;
329 ZERO_STRUCTP(vfs_statvfs);
331 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
332 vfs_statvfs->BlockSize = statvfs.f_bsize;
333 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
334 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
335 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
336 vfs_statvfs->TotalFileNodes = statvfs.f_files;
337 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
338 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
339 vfs_statvfs->FsCapabilities =
340 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
342 return ret;
345 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
346 enum timestamp_set_resolution *p_ts_res)
348 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
350 #ifdef STAT_HAVE_NSEC
351 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
352 #endif
354 return caps;
357 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
358 const char *path, const char *mask,
359 uint32 attributes)
361 glfs_fd_t *fd;
363 fd = glfs_opendir(handle->data, path);
364 if (fd == NULL) {
365 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
366 path, strerror(errno)));
369 return (DIR *) fd;
372 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
373 files_struct *fsp, const char *mask,
374 uint32 attributes)
376 return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
379 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
381 return glfs_closedir((void *)dirp);
384 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
385 DIR *dirp, SMB_STRUCT_STAT *sbuf)
387 static char direntbuf[512];
388 int ret;
389 struct stat stat;
390 struct dirent *dirent = 0;
392 if (sbuf != NULL) {
393 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
394 &dirent);
395 } else {
396 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
399 if ((ret < 0) || (dirent == NULL)) {
400 return NULL;
403 if (sbuf != NULL) {
404 smb_stat_ex_from_stat(sbuf, &stat);
407 return dirent;
410 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
412 return glfs_telldir((void *)dirp);
415 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
416 long offset)
418 glfs_seekdir((void *)dirp, offset);
421 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
423 glfs_seekdir((void *)dirp, 0);
426 static void vfs_gluster_init_search_op(struct vfs_handle_struct *handle,
427 DIR *dirp)
429 return;
432 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle, const char *path,
433 mode_t mode)
435 return glfs_mkdir(handle->data, path, mode);
438 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle, const char *path)
440 return glfs_rmdir(handle->data, path);
443 static int vfs_gluster_open(struct vfs_handle_struct *handle,
444 struct smb_filename *smb_fname, files_struct *fsp,
445 int flags, mode_t mode)
447 glfs_fd_t *glfd;
448 glfs_fd_t **p_tmp;
450 if (flags & O_DIRECTORY) {
451 glfd = glfs_opendir(handle->data, smb_fname->base_name);
452 } else if (flags & O_CREAT) {
453 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
454 mode);
455 } else {
456 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
459 if (glfd == NULL) {
460 return -1;
462 p_tmp = (glfs_fd_t **)VFS_ADD_FSP_EXTENSION(handle, fsp,
463 glfs_fd_t *, NULL);
464 *p_tmp = glfd;
465 /* An arbitrary value for error reporting, so you know its us. */
466 return 13371337;
469 static int vfs_gluster_close(struct vfs_handle_struct *handle,
470 files_struct *fsp)
472 glfs_fd_t *glfd;
473 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
474 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
475 return glfs_close(glfd);
478 static ssize_t vfs_gluster_read(struct vfs_handle_struct *handle,
479 files_struct *fsp, void *data, size_t n)
481 return glfs_read(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
484 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
485 files_struct *fsp, void *data, size_t n,
486 off_t offset)
488 return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
491 struct glusterfs_aio_state {
492 ssize_t ret;
493 int err;
497 * This function is the callback that will be called on glusterfs
498 * threads once the async IO submitted is complete. To notify
499 * Samba of the completion we use a pipe pased queue.
501 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
503 struct tevent_req *req = NULL;
504 struct glusterfs_aio_state *state = NULL;
505 int sts = 0;
507 req = talloc_get_type_abort(data, struct tevent_req);
508 state = tevent_req_data(req, struct glusterfs_aio_state);
510 if (ret < 0) {
511 state->ret = -1;
512 state->err = errno;
513 } else {
514 state->ret = ret;
515 state->err = 0;
519 * Write the pointer to each req that needs to be completed
520 * by calling tevent_req_done(). tevent_req_done() cannot
521 * be called here, as it is not designed to be executed
522 * in the multithread environment, tevent_req_done() must be
523 * executed from the smbd main thread.
526 sts = sys_write (write_fd, &req, sizeof(struct tevent_req *));
527 if (sts < 0) {
528 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
531 return;
535 * Read each req off the pipe and process it.
537 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
538 struct tevent_fd *fde,
539 uint16 flags, void *data)
541 struct tevent_req *req = NULL;
542 int sts = 0;
544 sts = sys_read(read_fd, &req, sizeof(struct tevent_req *));
545 if (sts < 0) {
546 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
549 if (req) {
550 tevent_req_done(req);
552 return;
555 static bool init_gluster_aio(struct vfs_handle_struct *handle)
557 int fds[2];
558 int ret = -1;
560 if (read_fd != -1) {
562 * Already initialized.
564 return true;
567 ret = pipe(fds);
568 if (ret == -1) {
569 goto fail;
572 read_fd = fds[0];
573 write_fd = fds[1];
575 aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
576 NULL,
577 read_fd,
578 TEVENT_FD_READ,
579 aio_tevent_fd_done,
580 NULL);
581 if (aio_read_event == NULL) {
582 goto fail;
585 return true;
586 fail:
587 TALLOC_FREE(aio_read_event);
588 if (read_fd != -1) {
589 close(read_fd);
590 close(write_fd);
591 read_fd = -1;
592 write_fd = -1;
594 return false;
597 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
598 *handle, TALLOC_CTX *mem_ctx,
599 struct tevent_context *ev,
600 files_struct *fsp, void *data,
601 size_t n, off_t offset)
603 struct tevent_req *req = NULL;
604 struct glusterfs_aio_state *state = NULL;
605 int ret = 0;
607 req = tevent_req_create(mem_ctx, &state, struct glusterfs_aio_state);
608 if (req == NULL) {
609 return NULL;
612 if (!init_gluster_aio(handle)) {
613 tevent_req_error(req, EIO);
614 return tevent_req_post(req, ev);
616 ret = glfs_pread_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
617 fsp), data, n, offset, 0, aio_glusterfs_done,
618 req);
619 if (ret < 0) {
620 tevent_req_error(req, -ret);
621 return tevent_req_post(req, ev);
624 return req;
627 static ssize_t vfs_gluster_write(struct vfs_handle_struct *handle,
628 files_struct *fsp, const void *data, size_t n)
630 return glfs_write(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
633 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
634 files_struct *fsp, const void *data,
635 size_t n, off_t offset)
637 return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
640 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
641 *handle, TALLOC_CTX *mem_ctx,
642 struct tevent_context *ev,
643 files_struct *fsp,
644 const void *data, size_t n,
645 off_t offset)
647 struct tevent_req *req = NULL;
648 struct glusterfs_aio_state *state = NULL;
649 int ret = 0;
651 req = tevent_req_create(mem_ctx, &state, struct glusterfs_aio_state);
652 if (req == NULL) {
653 return NULL;
655 if (!init_gluster_aio(handle)) {
656 tevent_req_error(req, EIO);
657 return tevent_req_post(req, ev);
659 ret = glfs_pwrite_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
660 fsp), data, n, offset, 0, aio_glusterfs_done,
661 req);
662 if (ret < 0) {
663 tevent_req_error(req, -ret);
664 return tevent_req_post(req, ev);
666 return req;
669 static ssize_t vfs_gluster_recv(struct tevent_req *req, int *err)
671 struct glusterfs_aio_state *state = NULL;
673 state = tevent_req_data(req, struct glusterfs_aio_state);
674 if (state == NULL) {
675 return -1;
678 if (tevent_req_is_unix_error(req, err)) {
679 return -1;
681 if (state->ret == -1) {
682 *err = state->err;
684 return state->ret;
687 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
688 files_struct *fsp, off_t offset, int whence)
690 return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
693 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
694 files_struct *fromfsp,
695 const DATA_BLOB *hdr,
696 off_t offset, size_t n)
698 errno = ENOTSUP;
699 return -1;
702 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
703 int fromfd, files_struct *tofsp,
704 off_t offset, size_t n)
706 errno = ENOTSUP;
707 return -1;
710 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
711 const struct smb_filename *smb_fname_src,
712 const struct smb_filename *smb_fname_dst)
714 return glfs_rename(handle->data, smb_fname_src->base_name,
715 smb_fname_dst->base_name);
718 static int vfs_gluster_fsync(struct vfs_handle_struct *handle,
719 files_struct *fsp)
721 return glfs_fsync(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp));
724 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
725 *handle, TALLOC_CTX *mem_ctx,
726 struct tevent_context *ev,
727 files_struct *fsp)
729 struct tevent_req *req = NULL;
730 struct glusterfs_aio_state *state = NULL;
731 int ret = 0;
733 req = tevent_req_create(mem_ctx, &state, struct glusterfs_aio_state);
734 if (req == NULL) {
735 return NULL;
737 if (!init_gluster_aio(handle)) {
738 tevent_req_error(req, EIO);
739 return tevent_req_post(req, ev);
741 ret = glfs_fsync_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
742 fsp), aio_glusterfs_done, req);
743 if (ret < 0) {
744 tevent_req_error(req, -ret);
745 return tevent_req_post(req, ev);
747 return req;
750 static int vfs_gluster_fsync_recv(struct tevent_req *req, int *err)
753 * Use implicit conversion ssize_t->int
755 return vfs_gluster_recv(req, err);
758 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
759 struct smb_filename *smb_fname)
761 struct stat st;
762 int ret;
764 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
765 if (ret == 0) {
766 smb_stat_ex_from_stat(&smb_fname->st, &st);
768 if (ret < 0 && errno != ENOENT) {
769 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
770 smb_fname->base_name, strerror(errno)));
772 return ret;
775 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
776 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
778 struct stat st;
779 int ret;
781 ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
782 if (ret == 0) {
783 smb_stat_ex_from_stat(sbuf, &st);
785 if (ret < 0) {
786 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
787 fsp->fh->fd, strerror(errno)));
789 return ret;
792 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
793 struct smb_filename *smb_fname)
795 struct stat st;
796 int ret;
798 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
799 if (ret == 0) {
800 smb_stat_ex_from_stat(&smb_fname->st, &st);
802 if (ret < 0 && errno != ENOENT) {
803 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
804 smb_fname->base_name, strerror(errno)));
806 return ret;
809 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
810 files_struct *fsp,
811 const SMB_STRUCT_STAT *sbuf)
813 return sbuf->st_ex_blocks * 512;
816 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
817 const struct smb_filename *smb_fname)
819 return glfs_unlink(handle->data, smb_fname->base_name);
822 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
823 const char *path, mode_t mode)
825 return glfs_chmod(handle->data, path, mode);
828 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
829 files_struct *fsp, mode_t mode)
831 return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
834 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
835 const char *path, uid_t uid, gid_t gid)
837 return glfs_chown(handle->data, path, uid, gid);
840 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
841 files_struct *fsp, uid_t uid, gid_t gid)
843 return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
846 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
847 const char *path, uid_t uid, gid_t gid)
849 return glfs_lchown(handle->data, path, uid, gid);
852 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
854 return glfs_chdir(handle->data, path);
857 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
859 char *cwd;
860 char *ret;
862 cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
863 if (cwd == NULL) {
864 return NULL;
867 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
868 if (ret == 0) {
869 free(cwd);
871 return ret;
874 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
875 const struct smb_filename *smb_fname,
876 struct smb_file_time *ft)
878 struct timespec times[2];
880 if (null_timespec(ft->atime)) {
881 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
882 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
883 } else {
884 times[0].tv_sec = ft->atime.tv_sec;
885 times[0].tv_nsec = ft->atime.tv_nsec;
888 if (null_timespec(ft->mtime)) {
889 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
890 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
891 } else {
892 times[1].tv_sec = ft->mtime.tv_sec;
893 times[1].tv_nsec = ft->mtime.tv_nsec;
896 if ((timespec_compare(&times[0],
897 &smb_fname->st.st_ex_atime) == 0) &&
898 (timespec_compare(&times[1],
899 &smb_fname->st.st_ex_mtime) == 0)) {
900 return 0;
903 return glfs_utimens(handle->data, smb_fname->base_name, times);
906 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
907 files_struct *fsp, off_t offset)
909 return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
912 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
913 struct files_struct *fsp,
914 enum vfs_fallocate_mode mode,
915 off_t offset, off_t len)
917 errno = ENOTSUP;
918 return -1;
921 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
922 const char *path)
924 return glfs_realpath(handle->data, path, 0);
927 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
928 files_struct *fsp, int op, off_t offset,
929 off_t count, int type)
931 struct flock flock = { 0, };
932 int ret;
934 flock.l_type = type;
935 flock.l_whence = SEEK_SET;
936 flock.l_start = offset;
937 flock.l_len = count;
938 flock.l_pid = 0;
940 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
942 if (op == F_GETLK) {
943 /* lock query, true if someone else has locked */
944 if ((ret != -1) &&
945 (flock.l_type != F_UNLCK) &&
946 (flock.l_pid != 0) && (flock.l_pid != getpid()))
947 return true;
948 /* not me */
949 return false;
952 if (ret == -1) {
953 return false;
956 return true;
959 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
960 files_struct *fsp, uint32 share_mode,
961 uint32_t access_mask)
963 errno = ENOSYS;
964 return -1;
967 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
968 files_struct *fsp, int leasetype)
970 errno = ENOSYS;
971 return -1;
974 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
975 files_struct *fsp, off_t *poffset,
976 off_t *pcount, int *ptype, pid_t *ppid)
978 struct flock flock = { 0, };
979 int ret;
981 flock.l_type = *ptype;
982 flock.l_whence = SEEK_SET;
983 flock.l_start = *poffset;
984 flock.l_len = *pcount;
985 flock.l_pid = 0;
987 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
989 if (ret == -1) {
990 return false;
993 *ptype = flock.l_type;
994 *poffset = flock.l_start;
995 *pcount = flock.l_len;
996 *ppid = flock.l_pid;
998 return true;
1001 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1002 const char *oldpath, const char *newpath)
1004 return glfs_symlink(handle->data, oldpath, newpath);
1007 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1008 const char *path, char *buf, size_t bufsiz)
1010 return glfs_readlink(handle->data, path, buf, bufsiz);
1013 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1014 const char *oldpath, const char *newpath)
1016 return glfs_link(handle->data, oldpath, newpath);
1019 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
1020 mode_t mode, SMB_DEV_T dev)
1022 return glfs_mknod(handle->data, path, mode, dev);
1025 static NTSTATUS vfs_gluster_notify_watch(struct vfs_handle_struct *handle,
1026 struct sys_notify_context *ctx,
1027 const char *path, uint32_t *filter,
1028 uint32_t *subdir_filter,
1029 void (*callback) (struct sys_notify_context *ctx,
1030 void *private_data,
1031 struct notify_event *ev),
1032 void *private_data, void *handle_p)
1034 return NT_STATUS_NOT_IMPLEMENTED;
1037 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1038 const char *path, unsigned int flags)
1040 errno = ENOSYS;
1041 return -1;
1044 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1045 const char *path, const char *name,
1046 TALLOC_CTX *mem_ctx, char **found_name)
1048 int ret;
1049 char key_buf[NAME_MAX + 64];
1050 char val_buf[NAME_MAX + 1];
1052 if (strlen(name) >= NAME_MAX) {
1053 errno = ENAMETOOLONG;
1054 return -1;
1057 snprintf(key_buf, NAME_MAX + 64,
1058 "glusterfs.get_real_filename:%s", name);
1060 ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1061 if (ret == -1) {
1062 if (errno == ENODATA) {
1063 errno = EOPNOTSUPP;
1065 return -1;
1068 *found_name = talloc_strdup(mem_ctx, val_buf);
1069 if (found_name[0] == NULL) {
1070 errno = ENOMEM;
1071 return -1;
1073 return 0;
1076 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1077 const char *filename)
1079 return handle->conn->connectpath;
1082 /* EA Operations */
1084 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1085 const char *path, const char *name,
1086 void *value, size_t size)
1088 return glfs_getxattr(handle->data, path, name, value, size);
1091 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1092 files_struct *fsp, const char *name,
1093 void *value, size_t size)
1095 return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
1098 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1099 const char *path, char *list, size_t size)
1101 return glfs_listxattr(handle->data, path, list, size);
1104 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1105 files_struct *fsp, char *list,
1106 size_t size)
1108 return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
1111 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1112 const char *path, const char *name)
1114 return glfs_removexattr(handle->data, path, name);
1117 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1118 files_struct *fsp, const char *name)
1120 return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
1123 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1124 const char *path, const char *name,
1125 const void *value, size_t size, int flags)
1127 return glfs_setxattr(handle->data, path, name, value, size, flags);
1130 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1131 files_struct *fsp, const char *name,
1132 const void *value, size_t size, int flags)
1134 return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
1135 flags);
1138 /* AIO Operations */
1140 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1141 files_struct *fsp)
1143 return false;
1146 /* Offline Operations */
1148 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
1149 const struct smb_filename *fname,
1150 SMB_STRUCT_STAT *sbuf)
1152 return false;
1155 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
1156 const struct smb_filename *fname)
1158 errno = ENOTSUP;
1159 return -1;
1163 Gluster ACL Format:
1165 Size = 4 (header) + N * 8 (entry)
1167 Offset Size Field (Little Endian)
1168 -------------------------------------
1169 0-3 4-byte Version
1171 4-5 2-byte Entry-1 tag
1172 6-7 2-byte Entry-1 perm
1173 8-11 4-byte Entry-1 id
1175 12-13 2-byte Entry-2 tag
1176 14-15 2-byte Entry-2 perm
1177 16-19 4-byte Entry-2 id
1183 /* header version */
1184 #define GLUSTER_ACL_VERSION 2
1186 /* perm bits */
1187 #define GLUSTER_ACL_READ 0x04
1188 #define GLUSTER_ACL_WRITE 0x02
1189 #define GLUSTER_ACL_EXECUTE 0x01
1191 /* tag values */
1192 #define GLUSTER_ACL_UNDEFINED_TAG 0x00
1193 #define GLUSTER_ACL_USER_OBJ 0x01
1194 #define GLUSTER_ACL_USER 0x02
1195 #define GLUSTER_ACL_GROUP_OBJ 0x04
1196 #define GLUSTER_ACL_GROUP 0x08
1197 #define GLUSTER_ACL_MASK 0x10
1198 #define GLUSTER_ACL_OTHER 0x20
1200 #define GLUSTER_ACL_UNDEFINED_ID (-1)
1202 #define GLUSTER_ACL_HEADER_SIZE 4
1203 #define GLUSTER_ACL_ENTRY_SIZE 8
1205 #define GLUSTER_ACL_SIZE(n) (GLUSTER_ACL_HEADER_SIZE + (n * GLUSTER_ACL_ENTRY_SIZE))
1207 static SMB_ACL_T mode_to_smb_acls(const struct stat *mode, TALLOC_CTX *mem_ctx)
1209 struct smb_acl_t *result;
1210 int count;
1212 count = 3;
1213 result = sys_acl_init(mem_ctx);
1214 if (!result) {
1215 errno = ENOMEM;
1216 return NULL;
1219 result->acl = talloc_array(result, struct smb_acl_entry, count);
1220 if (!result->acl) {
1221 errno = ENOMEM;
1222 talloc_free(result);
1223 return NULL;
1226 result->count = count;
1228 result->acl[0].a_type = SMB_ACL_USER_OBJ;
1229 result->acl[0].a_perm = (mode->st_mode & S_IRWXU) >> 6;;
1231 result->acl[1].a_type = SMB_ACL_GROUP_OBJ;
1232 result->acl[1].a_perm = (mode->st_mode & S_IRWXG) >> 3;;
1234 result->acl[2].a_type = SMB_ACL_OTHER;
1235 result->acl[2].a_perm = mode->st_mode & S_IRWXO;;
1237 return result;
1240 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1241 TALLOC_CTX *mem_ctx)
1243 int count;
1244 size_t size;
1245 struct smb_acl_entry *smb_ace;
1246 struct smb_acl_t *result;
1247 int i;
1248 int offset;
1249 uint16_t tag;
1250 uint16_t perm;
1251 uint32_t id;
1253 size = xattr_size;
1255 if (size < GLUSTER_ACL_HEADER_SIZE) {
1256 /* ACL should be at least as big as the header (4 bytes) */
1257 errno = EINVAL;
1258 return NULL;
1261 size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1263 if (size % GLUSTER_ACL_ENTRY_SIZE) {
1264 /* Size of entries must strictly be a multiple of
1265 size of an ACE (8 bytes)
1267 errno = EINVAL;
1268 return NULL;
1271 count = size / GLUSTER_ACL_ENTRY_SIZE;
1273 /* Version is the first 4 bytes of the ACL */
1274 if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1275 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1276 IVAL(buf, 0)));
1277 return NULL;
1279 offset = GLUSTER_ACL_HEADER_SIZE;
1281 result = sys_acl_init(mem_ctx);
1282 if (!result) {
1283 errno = ENOMEM;
1284 return NULL;
1287 result->acl = talloc_array(result, struct smb_acl_entry, count);
1288 if (!result->acl) {
1289 errno = ENOMEM;
1290 talloc_free(result);
1291 return NULL;
1294 result->count = count;
1296 smb_ace = result->acl;
1298 for (i = 0; i < count; i++) {
1299 /* TAG is the first 2 bytes of an entry */
1300 tag = SVAL(buf, offset);
1301 offset += 2;
1303 /* PERM is the next 2 bytes of an entry */
1304 perm = SVAL(buf, offset);
1305 offset += 2;
1307 /* ID is the last 4 bytes of an entry */
1308 id = IVAL(buf, offset);
1309 offset += 4;
1311 switch(tag) {
1312 case GLUSTER_ACL_USER:
1313 smb_ace->a_type = SMB_ACL_USER;
1314 break;
1315 case GLUSTER_ACL_USER_OBJ:
1316 smb_ace->a_type = SMB_ACL_USER_OBJ;
1317 break;
1318 case GLUSTER_ACL_GROUP:
1319 smb_ace->a_type = SMB_ACL_GROUP;
1320 break;
1321 case GLUSTER_ACL_GROUP_OBJ:
1322 smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1323 break;
1324 case GLUSTER_ACL_OTHER:
1325 smb_ace->a_type = SMB_ACL_OTHER;
1326 break;
1327 case GLUSTER_ACL_MASK:
1328 smb_ace->a_type = SMB_ACL_MASK;
1329 break;
1330 default:
1331 DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1332 return NULL;
1336 switch(smb_ace->a_type) {
1337 case SMB_ACL_USER:
1338 smb_ace->info.user.uid = id;
1339 break;
1340 case SMB_ACL_GROUP:
1341 smb_ace->info.group.gid = id;
1342 break;
1343 default:
1344 break;
1347 smb_ace->a_perm = 0;
1348 smb_ace->a_perm |=
1349 ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1350 smb_ace->a_perm |=
1351 ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1352 smb_ace->a_perm |=
1353 ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1355 smb_ace++;
1358 return result;
1362 static int gluster_ace_cmp(const void *left, const void *right)
1364 int ret = 0;
1365 uint16_t tag_left, tag_right;
1366 uint32_t id_left, id_right;
1369 Sorting precedence:
1371 - Smaller TAG values must be earlier.
1373 - Within same TAG, smaller identifiers must be earlier, E.g:
1374 UID 0 entry must be earlier than UID 200
1375 GID 17 entry must be earlier than GID 19
1378 /* TAG is the first element in the entry */
1379 tag_left = SVAL(left, 0);
1380 tag_right = SVAL(right, 0);
1382 ret = (tag_left - tag_right);
1383 if (!ret) {
1384 /* ID is the third element in the entry, after two short
1385 integers (tag and perm), i.e at offset 4.
1387 id_left = IVAL(left, 4);
1388 id_right = IVAL(right, 4);
1389 ret = id_left - id_right;
1392 return ret;
1396 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1398 ssize_t size;
1399 struct smb_acl_entry *smb_ace;
1400 int i;
1401 int count;
1402 uint16_t tag;
1403 uint16_t perm;
1404 uint32_t id;
1405 int offset;
1407 count = theacl->count;
1409 size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1410 if (!buf) {
1411 return size;
1414 if (len < size) {
1415 errno = ERANGE;
1416 return -1;
1419 smb_ace = theacl->acl;
1421 /* Version is the first 4 bytes of the ACL */
1422 SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1423 offset = GLUSTER_ACL_HEADER_SIZE;
1425 for (i = 0; i < count; i++) {
1426 /* Calculate tag */
1427 switch(smb_ace->a_type) {
1428 case SMB_ACL_USER:
1429 tag = GLUSTER_ACL_USER;
1430 break;
1431 case SMB_ACL_USER_OBJ:
1432 tag = GLUSTER_ACL_USER_OBJ;
1433 break;
1434 case SMB_ACL_GROUP:
1435 tag = GLUSTER_ACL_GROUP;
1436 break;
1437 case SMB_ACL_GROUP_OBJ:
1438 tag = GLUSTER_ACL_GROUP_OBJ;
1439 break;
1440 case SMB_ACL_OTHER:
1441 tag = GLUSTER_ACL_OTHER;
1442 break;
1443 case SMB_ACL_MASK:
1444 tag = GLUSTER_ACL_MASK;
1445 break;
1446 default:
1447 DEBUG(0, ("Unknown tag value %d\n",
1448 smb_ace->a_type));
1449 errno = EINVAL;
1450 return -1;
1454 /* Calculate id */
1455 switch(smb_ace->a_type) {
1456 case SMB_ACL_USER:
1457 id = smb_ace->info.user.uid;
1458 break;
1459 case SMB_ACL_GROUP:
1460 id = smb_ace->info.group.gid;
1461 break;
1462 default:
1463 id = GLUSTER_ACL_UNDEFINED_ID;
1464 break;
1467 /* Calculate perm */
1468 perm = 0;
1470 perm |=
1471 ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1472 perm |=
1473 ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1474 perm |=
1475 ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1478 /* TAG is the first 2 bytes of an entry */
1479 SSVAL(buf, offset, tag);
1480 offset += 2;
1482 /* PERM is the next 2 bytes of an entry */
1483 SSVAL(buf, offset, perm);
1484 offset += 2;
1486 /* ID is the last 4 bytes of an entry */
1487 SIVAL(buf, offset, id);
1488 offset += 4;
1490 smb_ace++;
1493 /* Skip the header, sort @count number of 8-byte entries */
1494 qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1495 gluster_ace_cmp);
1497 return size;
1501 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1502 const char *path_p,
1503 SMB_ACL_TYPE_T type,
1504 TALLOC_CTX *mem_ctx)
1506 struct smb_acl_t *result;
1507 struct stat st;
1508 char *buf;
1509 const char *key;
1510 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1512 switch (type) {
1513 case SMB_ACL_TYPE_ACCESS:
1514 key = "system.posix_acl_access";
1515 break;
1516 case SMB_ACL_TYPE_DEFAULT:
1517 key = "system.posix_acl_default";
1518 break;
1519 default:
1520 errno = EINVAL;
1521 return NULL;
1524 buf = alloca(size);
1525 if (!buf) {
1526 return NULL;
1529 ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1530 if (ret == -1 && errno == ERANGE) {
1531 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1532 if (ret > 0) {
1533 buf = alloca(ret);
1534 if (!buf) {
1535 return NULL;
1537 ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1541 /* retrieving the ACL from the xattr has finally failed, do a
1542 * mode-to-acl mapping */
1544 if (ret == -1 && errno == ENODATA) {
1545 ret = glfs_stat(handle->data, path_p, &st);
1546 if (ret == 0) {
1547 result = mode_to_smb_acls(&st, mem_ctx);
1548 return result;
1552 if (ret <= 0) {
1553 return NULL;
1556 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1558 return result;
1561 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1562 struct files_struct *fsp,
1563 TALLOC_CTX *mem_ctx)
1565 struct smb_acl_t *result;
1566 struct stat st;
1567 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1568 char *buf;
1569 glfs_fd_t *glfd;
1571 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1573 buf = alloca(size);
1574 if (!buf) {
1575 return NULL;
1578 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1579 if (ret == -1 && errno == ERANGE) {
1580 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1581 if (ret > 0) {
1582 buf = alloca(ret);
1583 if (!buf) {
1584 return NULL;
1586 ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1587 buf, ret);
1591 /* retrieving the ACL from the xattr has finally failed, do a
1592 * mode-to-acl mapping */
1594 if (ret == -1 && errno == ENODATA) {
1595 ret = glfs_fstat(glfd, &st);
1596 if (ret == 0) {
1597 result = mode_to_smb_acls(&st, mem_ctx);
1598 return result;
1602 if (ret <= 0) {
1603 return NULL;
1606 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1608 return result;
1611 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1612 const char *name,
1613 SMB_ACL_TYPE_T acltype,
1614 SMB_ACL_T theacl)
1616 int ret;
1617 const char *key;
1618 char *buf;
1619 ssize_t size;
1621 switch (acltype) {
1622 case SMB_ACL_TYPE_ACCESS:
1623 key = "system.posix_acl_access";
1624 break;
1625 case SMB_ACL_TYPE_DEFAULT:
1626 key = "system.posix_acl_default";
1627 break;
1628 default:
1629 errno = EINVAL;
1630 return -1;
1633 size = smb_to_gluster_acl(theacl, 0, 0);
1634 buf = alloca(size);
1636 size = smb_to_gluster_acl(theacl, buf, size);
1637 if (size == -1) {
1638 return -1;
1641 ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1643 return ret;
1646 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1647 struct files_struct *fsp,
1648 SMB_ACL_T theacl)
1650 int ret;
1651 char *buf;
1652 ssize_t size;
1654 size = smb_to_gluster_acl(theacl, 0, 0);
1655 buf = alloca(size);
1657 size = smb_to_gluster_acl(theacl, buf, size);
1658 if (size == -1) {
1659 return -1;
1662 ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1663 "system.posix_acl_access", buf, size, 0);
1664 return ret;
1667 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1668 const char *path)
1670 return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1673 static struct vfs_fn_pointers glusterfs_fns = {
1675 /* Disk Operations */
1677 .connect_fn = vfs_gluster_connect,
1678 .disconnect_fn = vfs_gluster_disconnect,
1679 .disk_free_fn = vfs_gluster_disk_free,
1680 .get_quota_fn = vfs_gluster_get_quota,
1681 .set_quota_fn = vfs_gluster_set_quota,
1682 .statvfs_fn = vfs_gluster_statvfs,
1683 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1685 .get_dfs_referrals_fn = NULL,
1687 /* Directory Operations */
1689 .opendir_fn = vfs_gluster_opendir,
1690 .fdopendir_fn = vfs_gluster_fdopendir,
1691 .readdir_fn = vfs_gluster_readdir,
1692 .seekdir_fn = vfs_gluster_seekdir,
1693 .telldir_fn = vfs_gluster_telldir,
1694 .rewind_dir_fn = vfs_gluster_rewinddir,
1695 .mkdir_fn = vfs_gluster_mkdir,
1696 .rmdir_fn = vfs_gluster_rmdir,
1697 .closedir_fn = vfs_gluster_closedir,
1698 .init_search_op_fn = vfs_gluster_init_search_op,
1700 /* File Operations */
1702 .open_fn = vfs_gluster_open,
1703 .create_file_fn = NULL,
1704 .close_fn = vfs_gluster_close,
1705 .read_fn = vfs_gluster_read,
1706 .pread_fn = vfs_gluster_pread,
1707 .pread_send_fn = vfs_gluster_pread_send,
1708 .pread_recv_fn = vfs_gluster_recv,
1709 .write_fn = vfs_gluster_write,
1710 .pwrite_fn = vfs_gluster_pwrite,
1711 .pwrite_send_fn = vfs_gluster_pwrite_send,
1712 .pwrite_recv_fn = vfs_gluster_recv,
1713 .lseek_fn = vfs_gluster_lseek,
1714 .sendfile_fn = vfs_gluster_sendfile,
1715 .recvfile_fn = vfs_gluster_recvfile,
1716 .rename_fn = vfs_gluster_rename,
1717 .fsync_fn = vfs_gluster_fsync,
1718 .fsync_send_fn = vfs_gluster_fsync_send,
1719 .fsync_recv_fn = vfs_gluster_fsync_recv,
1721 .stat_fn = vfs_gluster_stat,
1722 .fstat_fn = vfs_gluster_fstat,
1723 .lstat_fn = vfs_gluster_lstat,
1724 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1725 .unlink_fn = vfs_gluster_unlink,
1727 .chmod_fn = vfs_gluster_chmod,
1728 .fchmod_fn = vfs_gluster_fchmod,
1729 .chown_fn = vfs_gluster_chown,
1730 .fchown_fn = vfs_gluster_fchown,
1731 .lchown_fn = vfs_gluster_lchown,
1732 .chdir_fn = vfs_gluster_chdir,
1733 .getwd_fn = vfs_gluster_getwd,
1734 .ntimes_fn = vfs_gluster_ntimes,
1735 .ftruncate_fn = vfs_gluster_ftruncate,
1736 .fallocate_fn = vfs_gluster_fallocate,
1737 .lock_fn = vfs_gluster_lock,
1738 .kernel_flock_fn = vfs_gluster_kernel_flock,
1739 .linux_setlease_fn = vfs_gluster_linux_setlease,
1740 .getlock_fn = vfs_gluster_getlock,
1741 .symlink_fn = vfs_gluster_symlink,
1742 .readlink_fn = vfs_gluster_readlink,
1743 .link_fn = vfs_gluster_link,
1744 .mknod_fn = vfs_gluster_mknod,
1745 .realpath_fn = vfs_gluster_realpath,
1746 .notify_watch_fn = vfs_gluster_notify_watch,
1747 .chflags_fn = vfs_gluster_chflags,
1748 .file_id_create_fn = NULL,
1749 .copy_chunk_send_fn = NULL,
1750 .copy_chunk_recv_fn = NULL,
1751 .streaminfo_fn = NULL,
1752 .get_real_filename_fn = vfs_gluster_get_real_filename,
1753 .connectpath_fn = vfs_gluster_connectpath,
1755 .brl_lock_windows_fn = NULL,
1756 .brl_unlock_windows_fn = NULL,
1757 .brl_cancel_windows_fn = NULL,
1758 .strict_lock_fn = NULL,
1759 .strict_unlock_fn = NULL,
1760 .translate_name_fn = NULL,
1761 .fsctl_fn = NULL,
1763 /* NT ACL Operations */
1764 .fget_nt_acl_fn = NULL,
1765 .get_nt_acl_fn = NULL,
1766 .fset_nt_acl_fn = NULL,
1767 .audit_file_fn = NULL,
1769 /* Posix ACL Operations */
1770 .chmod_acl_fn = NULL, /* passthrough to default */
1771 .fchmod_acl_fn = NULL, /* passthrough to default */
1772 .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1773 .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1774 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1775 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1776 .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1777 .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1778 .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1780 /* EA Operations */
1781 .getxattr_fn = vfs_gluster_getxattr,
1782 .fgetxattr_fn = vfs_gluster_fgetxattr,
1783 .listxattr_fn = vfs_gluster_listxattr,
1784 .flistxattr_fn = vfs_gluster_flistxattr,
1785 .removexattr_fn = vfs_gluster_removexattr,
1786 .fremovexattr_fn = vfs_gluster_fremovexattr,
1787 .setxattr_fn = vfs_gluster_setxattr,
1788 .fsetxattr_fn = vfs_gluster_fsetxattr,
1790 /* AIO Operations */
1791 .aio_force_fn = vfs_gluster_aio_force,
1793 /* Offline Operations */
1794 .is_offline_fn = vfs_gluster_is_offline,
1795 .set_offline_fn = vfs_gluster_set_offline,
1797 /* Durable handle Operations */
1798 .durable_cookie_fn = NULL,
1799 .durable_disconnect_fn = NULL,
1800 .durable_reconnect_fn = NULL,
1803 NTSTATUS vfs_glusterfs_init(void);
1804 NTSTATUS vfs_glusterfs_init(void)
1806 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1807 "glusterfs", &glusterfs_fns);