vfs:glusterfs: whitespace fix.
[Samba.git] / source3 / modules / vfs_glusterfs.c
blob96f555946ec75d6ed639c24e00b94bcfd6bfbef3
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"
47 #define DEFAULT_VOLFILE_SERVER "localhost"
49 static int read_fd = -1;
50 static int write_fd = -1;
51 static struct tevent_fd *aio_read_event = NULL;
53 /**
54 * Helper to convert struct stat to struct stat_ex.
56 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
58 ZERO_STRUCTP(dst);
60 dst->st_ex_dev = src->st_dev;
61 dst->st_ex_ino = src->st_ino;
62 dst->st_ex_mode = src->st_mode;
63 dst->st_ex_nlink = src->st_nlink;
64 dst->st_ex_uid = src->st_uid;
65 dst->st_ex_gid = src->st_gid;
66 dst->st_ex_rdev = src->st_rdev;
67 dst->st_ex_size = src->st_size;
68 dst->st_ex_atime.tv_sec = src->st_atime;
69 dst->st_ex_mtime.tv_sec = src->st_mtime;
70 dst->st_ex_ctime.tv_sec = src->st_ctime;
71 dst->st_ex_btime.tv_sec = src->st_mtime;
72 dst->st_ex_blksize = src->st_blksize;
73 dst->st_ex_blocks = src->st_blocks;
74 #ifdef STAT_HAVE_NSEC
75 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
76 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
77 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
78 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
79 #endif
82 /* pre-opened glfs_t */
84 static struct glfs_preopened {
85 char *volume;
86 char *connectpath;
87 glfs_t *fs;
88 int ref;
89 struct glfs_preopened *next, *prev;
90 } *glfs_preopened;
93 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
95 struct glfs_preopened *entry = NULL;
97 entry = talloc_zero(NULL, struct glfs_preopened);
98 if (!entry) {
99 errno = ENOMEM;
100 return -1;
103 entry->volume = talloc_strdup(entry, volume);
104 if (!entry->volume) {
105 talloc_free(entry);
106 errno = ENOMEM;
107 return -1;
110 entry->connectpath = talloc_strdup(entry, connectpath);
111 if (entry->connectpath == NULL) {
112 talloc_free(entry);
113 errno = ENOMEM;
114 return -1;
117 entry->fs = fs;
118 entry->ref = 1;
120 DLIST_ADD(glfs_preopened, entry);
122 return 0;
125 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
127 struct glfs_preopened *entry = NULL;
129 for (entry = glfs_preopened; entry; entry = entry->next) {
130 if (strcmp(entry->volume, volume) == 0 &&
131 strcmp(entry->connectpath, connectpath) == 0)
133 entry->ref++;
134 return entry->fs;
138 return NULL;
141 static void glfs_clear_preopened(glfs_t *fs)
143 struct glfs_preopened *entry = NULL;
145 for (entry = glfs_preopened; entry; entry = entry->next) {
146 if (entry->fs == fs) {
147 if (--entry->ref)
148 return;
150 DLIST_REMOVE(glfs_preopened, entry);
152 glfs_fini(entry->fs);
153 talloc_free(entry);
158 /* Disk Operations */
160 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
161 const char *service,
162 const char *user)
164 const char *volfile_server;
165 const char *volume;
166 char *logfile;
167 int loglevel;
168 glfs_t *fs = NULL;
169 TALLOC_CTX *tmp_ctx;
170 int ret = 0;
172 tmp_ctx = talloc_new(NULL);
173 if (tmp_ctx == NULL) {
174 ret = -1;
175 goto done;
177 logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
178 "logfile", NULL);
180 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
182 volfile_server = lp_parm_const_string(SNUM(handle->conn), "glusterfs",
183 "volfile_server", NULL);
184 if (volfile_server == NULL) {
185 volfile_server = DEFAULT_VOLFILE_SERVER;
188 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
189 NULL);
190 if (volume == NULL) {
191 volume = service;
194 fs = glfs_find_preopened(volume, handle->conn->connectpath);
195 if (fs) {
196 goto done;
199 fs = glfs_new(volume);
200 if (fs == NULL) {
201 ret = -1;
202 goto done;
205 ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 0);
206 if (ret < 0) {
207 DEBUG(0, ("Failed to set volfile_server %s\n", volfile_server));
208 goto done;
211 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
212 "true");
213 if (ret < 0) {
214 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
215 goto done;
219 ret = glfs_set_xlator_option(fs, "*-snapview-client",
220 "snapdir-entry-path",
221 handle->conn->connectpath);
222 if (ret < 0) {
223 DEBUG(0, ("%s: Failed to set xlator option:"
224 " snapdir-entry-path\n", volume));
225 glfs_fini(fs);
226 return -1;
229 ret = glfs_set_logging(fs, logfile, loglevel);
230 if (ret < 0) {
231 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
232 volume, logfile, loglevel));
233 goto done;
236 ret = glfs_init(fs);
237 if (ret < 0) {
238 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
239 volume, strerror(errno)));
240 goto done;
243 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
244 if (ret < 0) {
245 DEBUG(0, ("%s: Failed to register volume (%s)\n",
246 volume, strerror(errno)));
247 goto done;
249 done:
250 talloc_free(tmp_ctx);
251 if (ret < 0) {
252 if (fs)
253 glfs_fini(fs);
254 return -1;
255 } else {
256 DEBUG(0, ("%s: Initialized volume from server %s\n",
257 volume, volfile_server));
258 handle->data = fs;
259 return 0;
263 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
265 glfs_t *fs = NULL;
267 fs = handle->data;
269 glfs_clear_preopened(fs);
272 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
273 const char *path, bool small_query,
274 uint64_t *bsize_p, uint64_t *dfree_p,
275 uint64_t *dsize_p)
277 struct statvfs statvfs = { 0, };
278 int ret;
280 ret = glfs_statvfs(handle->data, path, &statvfs);
281 if (ret < 0) {
282 return -1;
285 if (bsize_p != NULL) {
286 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
288 if (dfree_p != NULL) {
289 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
291 if (dsize_p != NULL) {
292 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
295 return (uint64_t)statvfs.f_bavail;
298 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
299 enum SMB_QUOTA_TYPE qtype, unid_t id,
300 SMB_DISK_QUOTA *qt)
302 errno = ENOSYS;
303 return -1;
306 static int
307 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
308 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
310 errno = ENOSYS;
311 return -1;
314 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
315 const char *path,
316 struct vfs_statvfs_struct *vfs_statvfs)
318 struct statvfs statvfs = { 0, };
319 int ret;
321 ret = glfs_statvfs(handle->data, path, &statvfs);
322 if (ret < 0) {
323 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
324 path, strerror(errno)));
325 return -1;
328 ZERO_STRUCTP(vfs_statvfs);
330 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
331 vfs_statvfs->BlockSize = statvfs.f_bsize;
332 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
333 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
334 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
335 vfs_statvfs->TotalFileNodes = statvfs.f_files;
336 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
337 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
338 vfs_statvfs->FsCapabilities =
339 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
341 return ret;
344 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
345 enum timestamp_set_resolution *p_ts_res)
347 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
349 #ifdef STAT_HAVE_NSEC
350 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
351 #endif
353 return caps;
356 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
357 const char *path, const char *mask,
358 uint32 attributes)
360 glfs_fd_t *fd;
362 fd = glfs_opendir(handle->data, path);
363 if (fd == NULL) {
364 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
365 path, strerror(errno)));
368 return (DIR *) fd;
371 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
372 files_struct *fsp, const char *mask,
373 uint32 attributes)
375 return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
378 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
380 return glfs_closedir((void *)dirp);
383 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
384 DIR *dirp, SMB_STRUCT_STAT *sbuf)
386 static char direntbuf[512];
387 int ret;
388 struct stat stat;
389 struct dirent *dirent = 0;
391 if (sbuf != NULL) {
392 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
393 &dirent);
394 } else {
395 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
398 if ((ret < 0) || (dirent == NULL)) {
399 return NULL;
402 if (sbuf != NULL) {
403 smb_stat_ex_from_stat(sbuf, &stat);
406 return dirent;
409 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
411 return glfs_telldir((void *)dirp);
414 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
415 long offset)
417 glfs_seekdir((void *)dirp, offset);
420 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
422 glfs_seekdir((void *)dirp, 0);
425 static void vfs_gluster_init_search_op(struct vfs_handle_struct *handle,
426 DIR *dirp)
428 return;
431 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle, const char *path,
432 mode_t mode)
434 return glfs_mkdir(handle->data, path, mode);
437 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle, const char *path)
439 return glfs_rmdir(handle->data, path);
442 static int vfs_gluster_open(struct vfs_handle_struct *handle,
443 struct smb_filename *smb_fname, files_struct *fsp,
444 int flags, mode_t mode)
446 glfs_fd_t *glfd;
447 glfs_fd_t **p_tmp;
449 if (flags & O_DIRECTORY) {
450 glfd = glfs_opendir(handle->data, smb_fname->base_name);
451 } else if (flags & O_CREAT) {
452 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
453 mode);
454 } else {
455 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
458 if (glfd == NULL) {
459 return -1;
461 p_tmp = (glfs_fd_t **)VFS_ADD_FSP_EXTENSION(handle, fsp,
462 glfs_fd_t *, NULL);
463 *p_tmp = glfd;
464 /* An arbitrary value for error reporting, so you know its us. */
465 return 13371337;
468 static int vfs_gluster_close(struct vfs_handle_struct *handle,
469 files_struct *fsp)
471 glfs_fd_t *glfd;
472 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
473 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
474 return glfs_close(glfd);
477 static ssize_t vfs_gluster_read(struct vfs_handle_struct *handle,
478 files_struct *fsp, void *data, size_t n)
480 return glfs_read(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
483 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
484 files_struct *fsp, void *data, size_t n,
485 off_t offset)
487 return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
490 struct glusterfs_aio_state {
491 ssize_t ret;
492 int err;
496 * This function is the callback that will be called on glusterfs
497 * threads once the async IO submitted is complete. To notify
498 * Samba of the completion we use a pipe pased queue.
500 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
502 struct tevent_req *req = NULL;
503 struct glusterfs_aio_state *state = NULL;
504 int sts = 0;
506 req = talloc_get_type_abort(data, struct tevent_req);
507 state = tevent_req_data(req, struct glusterfs_aio_state);
509 if (ret < 0) {
510 state->ret = -1;
511 state->err = errno;
512 } else {
513 state->ret = ret;
514 state->err = 0;
518 * Write the pointer to each req that needs to be completed
519 * by calling tevent_req_done(). tevent_req_done() cannot
520 * be called here, as it is not designed to be executed
521 * in the multithread environment, tevent_req_done() must be
522 * executed from the smbd main thread.
525 sts = sys_write(write_fd, &req, sizeof(struct tevent_req *));
526 if (sts < 0) {
527 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
530 return;
534 * Read each req off the pipe and process it.
536 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
537 struct tevent_fd *fde,
538 uint16 flags, void *data)
540 struct tevent_req *req = NULL;
541 int sts = 0;
543 sts = sys_read(read_fd, &req, sizeof(struct tevent_req *));
544 if (sts < 0) {
545 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
548 if (req) {
549 tevent_req_done(req);
551 return;
554 static bool init_gluster_aio(struct vfs_handle_struct *handle)
556 int fds[2];
557 int ret = -1;
559 if (read_fd != -1) {
561 * Already initialized.
563 return true;
566 ret = pipe(fds);
567 if (ret == -1) {
568 goto fail;
571 read_fd = fds[0];
572 write_fd = fds[1];
574 aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
575 NULL,
576 read_fd,
577 TEVENT_FD_READ,
578 aio_tevent_fd_done,
579 NULL);
580 if (aio_read_event == NULL) {
581 goto fail;
584 return true;
585 fail:
586 TALLOC_FREE(aio_read_event);
587 if (read_fd != -1) {
588 close(read_fd);
589 close(write_fd);
590 read_fd = -1;
591 write_fd = -1;
593 return false;
596 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
597 *handle, TALLOC_CTX *mem_ctx,
598 struct tevent_context *ev,
599 files_struct *fsp, void *data,
600 size_t n, off_t offset)
602 struct tevent_req *req = NULL;
603 struct glusterfs_aio_state *state = NULL;
604 int ret = 0;
606 req = tevent_req_create(mem_ctx, &state, struct glusterfs_aio_state);
607 if (req == NULL) {
608 return NULL;
611 if (!init_gluster_aio(handle)) {
612 tevent_req_error(req, EIO);
613 return tevent_req_post(req, ev);
615 ret = glfs_pread_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
616 fsp), data, n, offset, 0, aio_glusterfs_done,
617 req);
618 if (ret < 0) {
619 tevent_req_error(req, -ret);
620 return tevent_req_post(req, ev);
623 return req;
626 static ssize_t vfs_gluster_write(struct vfs_handle_struct *handle,
627 files_struct *fsp, const void *data, size_t n)
629 return glfs_write(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
632 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
633 files_struct *fsp, const void *data,
634 size_t n, off_t offset)
636 return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
639 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
640 *handle, TALLOC_CTX *mem_ctx,
641 struct tevent_context *ev,
642 files_struct *fsp,
643 const void *data, size_t n,
644 off_t offset)
646 struct tevent_req *req = NULL;
647 struct glusterfs_aio_state *state = NULL;
648 int ret = 0;
650 req = tevent_req_create(mem_ctx, &state, struct glusterfs_aio_state);
651 if (req == NULL) {
652 return NULL;
654 if (!init_gluster_aio(handle)) {
655 tevent_req_error(req, EIO);
656 return tevent_req_post(req, ev);
658 ret = glfs_pwrite_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
659 fsp), data, n, offset, 0, aio_glusterfs_done,
660 req);
661 if (ret < 0) {
662 tevent_req_error(req, -ret);
663 return tevent_req_post(req, ev);
665 return req;
668 static ssize_t vfs_gluster_recv(struct tevent_req *req, int *err)
670 struct glusterfs_aio_state *state = NULL;
672 state = tevent_req_data(req, struct glusterfs_aio_state);
673 if (state == NULL) {
674 return -1;
677 if (tevent_req_is_unix_error(req, err)) {
678 return -1;
680 if (state->ret == -1) {
681 *err = state->err;
683 return state->ret;
686 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
687 files_struct *fsp, off_t offset, int whence)
689 return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
692 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
693 files_struct *fromfsp,
694 const DATA_BLOB *hdr,
695 off_t offset, size_t n)
697 errno = ENOTSUP;
698 return -1;
701 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
702 int fromfd, files_struct *tofsp,
703 off_t offset, size_t n)
705 errno = ENOTSUP;
706 return -1;
709 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
710 const struct smb_filename *smb_fname_src,
711 const struct smb_filename *smb_fname_dst)
713 return glfs_rename(handle->data, smb_fname_src->base_name,
714 smb_fname_dst->base_name);
717 static int vfs_gluster_fsync(struct vfs_handle_struct *handle,
718 files_struct *fsp)
720 return glfs_fsync(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp));
723 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
724 *handle, TALLOC_CTX *mem_ctx,
725 struct tevent_context *ev,
726 files_struct *fsp)
728 struct tevent_req *req = NULL;
729 struct glusterfs_aio_state *state = NULL;
730 int ret = 0;
732 req = tevent_req_create(mem_ctx, &state, struct glusterfs_aio_state);
733 if (req == NULL) {
734 return NULL;
736 if (!init_gluster_aio(handle)) {
737 tevent_req_error(req, EIO);
738 return tevent_req_post(req, ev);
740 ret = glfs_fsync_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
741 fsp), aio_glusterfs_done, req);
742 if (ret < 0) {
743 tevent_req_error(req, -ret);
744 return tevent_req_post(req, ev);
746 return req;
749 static int vfs_gluster_fsync_recv(struct tevent_req *req, int *err)
752 * Use implicit conversion ssize_t->int
754 return vfs_gluster_recv(req, err);
757 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
758 struct smb_filename *smb_fname)
760 struct stat st;
761 int ret;
763 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
764 if (ret == 0) {
765 smb_stat_ex_from_stat(&smb_fname->st, &st);
767 if (ret < 0 && errno != ENOENT) {
768 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
769 smb_fname->base_name, strerror(errno)));
771 return ret;
774 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
775 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
777 struct stat st;
778 int ret;
780 ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
781 if (ret == 0) {
782 smb_stat_ex_from_stat(sbuf, &st);
784 if (ret < 0) {
785 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
786 fsp->fh->fd, strerror(errno)));
788 return ret;
791 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
792 struct smb_filename *smb_fname)
794 struct stat st;
795 int ret;
797 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
798 if (ret == 0) {
799 smb_stat_ex_from_stat(&smb_fname->st, &st);
801 if (ret < 0 && errno != ENOENT) {
802 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
803 smb_fname->base_name, strerror(errno)));
805 return ret;
808 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
809 files_struct *fsp,
810 const SMB_STRUCT_STAT *sbuf)
812 return sbuf->st_ex_blocks * 512;
815 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
816 const struct smb_filename *smb_fname)
818 return glfs_unlink(handle->data, smb_fname->base_name);
821 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
822 const char *path, mode_t mode)
824 return glfs_chmod(handle->data, path, mode);
827 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
828 files_struct *fsp, mode_t mode)
830 return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
833 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
834 const char *path, uid_t uid, gid_t gid)
836 return glfs_chown(handle->data, path, uid, gid);
839 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
840 files_struct *fsp, uid_t uid, gid_t gid)
842 return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
845 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
846 const char *path, uid_t uid, gid_t gid)
848 return glfs_lchown(handle->data, path, uid, gid);
851 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
853 return glfs_chdir(handle->data, path);
856 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
858 char *cwd;
859 char *ret;
861 cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
862 if (cwd == NULL) {
863 return NULL;
866 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
867 if (ret == 0) {
868 free(cwd);
870 return ret;
873 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
874 const struct smb_filename *smb_fname,
875 struct smb_file_time *ft)
877 struct timespec times[2];
879 if (null_timespec(ft->atime)) {
880 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
881 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
882 } else {
883 times[0].tv_sec = ft->atime.tv_sec;
884 times[0].tv_nsec = ft->atime.tv_nsec;
887 if (null_timespec(ft->mtime)) {
888 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
889 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
890 } else {
891 times[1].tv_sec = ft->mtime.tv_sec;
892 times[1].tv_nsec = ft->mtime.tv_nsec;
895 if ((timespec_compare(&times[0],
896 &smb_fname->st.st_ex_atime) == 0) &&
897 (timespec_compare(&times[1],
898 &smb_fname->st.st_ex_mtime) == 0)) {
899 return 0;
902 return glfs_utimens(handle->data, smb_fname->base_name, times);
905 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
906 files_struct *fsp, off_t offset)
908 return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
911 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
912 struct files_struct *fsp,
913 enum vfs_fallocate_mode mode,
914 off_t offset, off_t len)
916 errno = ENOTSUP;
917 return -1;
920 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
921 const char *path)
923 return glfs_realpath(handle->data, path, 0);
926 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
927 files_struct *fsp, int op, off_t offset,
928 off_t count, int type)
930 struct flock flock = { 0, };
931 int ret;
933 flock.l_type = type;
934 flock.l_whence = SEEK_SET;
935 flock.l_start = offset;
936 flock.l_len = count;
937 flock.l_pid = 0;
939 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
941 if (op == F_GETLK) {
942 /* lock query, true if someone else has locked */
943 if ((ret != -1) &&
944 (flock.l_type != F_UNLCK) &&
945 (flock.l_pid != 0) && (flock.l_pid != getpid()))
946 return true;
947 /* not me */
948 return false;
951 if (ret == -1) {
952 return false;
955 return true;
958 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
959 files_struct *fsp, uint32 share_mode,
960 uint32_t access_mask)
962 errno = ENOSYS;
963 return -1;
966 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
967 files_struct *fsp, int leasetype)
969 errno = ENOSYS;
970 return -1;
973 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
974 files_struct *fsp, off_t *poffset,
975 off_t *pcount, int *ptype, pid_t *ppid)
977 struct flock flock = { 0, };
978 int ret;
980 flock.l_type = *ptype;
981 flock.l_whence = SEEK_SET;
982 flock.l_start = *poffset;
983 flock.l_len = *pcount;
984 flock.l_pid = 0;
986 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
988 if (ret == -1) {
989 return false;
992 *ptype = flock.l_type;
993 *poffset = flock.l_start;
994 *pcount = flock.l_len;
995 *ppid = flock.l_pid;
997 return true;
1000 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1001 const char *oldpath, const char *newpath)
1003 return glfs_symlink(handle->data, oldpath, newpath);
1006 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1007 const char *path, char *buf, size_t bufsiz)
1009 return glfs_readlink(handle->data, path, buf, bufsiz);
1012 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1013 const char *oldpath, const char *newpath)
1015 return glfs_link(handle->data, oldpath, newpath);
1018 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
1019 mode_t mode, SMB_DEV_T dev)
1021 return glfs_mknod(handle->data, path, mode, dev);
1024 static NTSTATUS vfs_gluster_notify_watch(struct vfs_handle_struct *handle,
1025 struct sys_notify_context *ctx,
1026 const char *path, uint32_t *filter,
1027 uint32_t *subdir_filter,
1028 void (*callback) (struct sys_notify_context *ctx,
1029 void *private_data,
1030 struct notify_event *ev),
1031 void *private_data, void *handle_p)
1033 return NT_STATUS_NOT_IMPLEMENTED;
1036 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1037 const char *path, unsigned int flags)
1039 errno = ENOSYS;
1040 return -1;
1043 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1044 const char *path, const char *name,
1045 TALLOC_CTX *mem_ctx, char **found_name)
1047 int ret;
1048 char key_buf[NAME_MAX + 64];
1049 char val_buf[NAME_MAX + 1];
1051 if (strlen(name) >= NAME_MAX) {
1052 errno = ENAMETOOLONG;
1053 return -1;
1056 snprintf(key_buf, NAME_MAX + 64,
1057 "glusterfs.get_real_filename:%s", name);
1059 ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1060 if (ret == -1) {
1061 if (errno == ENODATA) {
1062 errno = EOPNOTSUPP;
1064 return -1;
1067 *found_name = talloc_strdup(mem_ctx, val_buf);
1068 if (found_name[0] == NULL) {
1069 errno = ENOMEM;
1070 return -1;
1072 return 0;
1075 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1076 const char *filename)
1078 return handle->conn->connectpath;
1081 /* EA Operations */
1083 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1084 const char *path, const char *name,
1085 void *value, size_t size)
1087 return glfs_getxattr(handle->data, path, name, value, size);
1090 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1091 files_struct *fsp, const char *name,
1092 void *value, size_t size)
1094 return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
1097 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1098 const char *path, char *list, size_t size)
1100 return glfs_listxattr(handle->data, path, list, size);
1103 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1104 files_struct *fsp, char *list,
1105 size_t size)
1107 return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
1110 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1111 const char *path, const char *name)
1113 return glfs_removexattr(handle->data, path, name);
1116 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1117 files_struct *fsp, const char *name)
1119 return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
1122 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1123 const char *path, const char *name,
1124 const void *value, size_t size, int flags)
1126 return glfs_setxattr(handle->data, path, name, value, size, flags);
1129 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1130 files_struct *fsp, const char *name,
1131 const void *value, size_t size, int flags)
1133 return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
1134 flags);
1137 /* AIO Operations */
1139 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1140 files_struct *fsp)
1142 return false;
1145 /* Offline Operations */
1147 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
1148 const struct smb_filename *fname,
1149 SMB_STRUCT_STAT *sbuf)
1151 return false;
1154 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
1155 const struct smb_filename *fname)
1157 errno = ENOTSUP;
1158 return -1;
1162 Gluster ACL Format:
1164 Size = 4 (header) + N * 8 (entry)
1166 Offset Size Field (Little Endian)
1167 -------------------------------------
1168 0-3 4-byte Version
1170 4-5 2-byte Entry-1 tag
1171 6-7 2-byte Entry-1 perm
1172 8-11 4-byte Entry-1 id
1174 12-13 2-byte Entry-2 tag
1175 14-15 2-byte Entry-2 perm
1176 16-19 4-byte Entry-2 id
1182 /* header version */
1183 #define GLUSTER_ACL_VERSION 2
1185 /* perm bits */
1186 #define GLUSTER_ACL_READ 0x04
1187 #define GLUSTER_ACL_WRITE 0x02
1188 #define GLUSTER_ACL_EXECUTE 0x01
1190 /* tag values */
1191 #define GLUSTER_ACL_UNDEFINED_TAG 0x00
1192 #define GLUSTER_ACL_USER_OBJ 0x01
1193 #define GLUSTER_ACL_USER 0x02
1194 #define GLUSTER_ACL_GROUP_OBJ 0x04
1195 #define GLUSTER_ACL_GROUP 0x08
1196 #define GLUSTER_ACL_MASK 0x10
1197 #define GLUSTER_ACL_OTHER 0x20
1199 #define GLUSTER_ACL_UNDEFINED_ID (-1)
1201 #define GLUSTER_ACL_HEADER_SIZE 4
1202 #define GLUSTER_ACL_ENTRY_SIZE 8
1204 #define GLUSTER_ACL_SIZE(n) (GLUSTER_ACL_HEADER_SIZE + (n * GLUSTER_ACL_ENTRY_SIZE))
1206 static SMB_ACL_T mode_to_smb_acls(const struct stat *mode, TALLOC_CTX *mem_ctx)
1208 struct smb_acl_t *result;
1209 int count;
1211 count = 3;
1212 result = sys_acl_init(mem_ctx);
1213 if (!result) {
1214 errno = ENOMEM;
1215 return NULL;
1218 result->acl = talloc_array(result, struct smb_acl_entry, count);
1219 if (!result->acl) {
1220 errno = ENOMEM;
1221 talloc_free(result);
1222 return NULL;
1225 result->count = count;
1227 result->acl[0].a_type = SMB_ACL_USER_OBJ;
1228 result->acl[0].a_perm = (mode->st_mode & S_IRWXU) >> 6;;
1230 result->acl[1].a_type = SMB_ACL_GROUP_OBJ;
1231 result->acl[1].a_perm = (mode->st_mode & S_IRWXG) >> 3;;
1233 result->acl[2].a_type = SMB_ACL_OTHER;
1234 result->acl[2].a_perm = mode->st_mode & S_IRWXO;;
1236 return result;
1239 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1240 TALLOC_CTX *mem_ctx)
1242 int count;
1243 size_t size;
1244 struct smb_acl_entry *smb_ace;
1245 struct smb_acl_t *result;
1246 int i;
1247 int offset;
1248 uint16_t tag;
1249 uint16_t perm;
1250 uint32_t id;
1252 size = xattr_size;
1254 if (size < GLUSTER_ACL_HEADER_SIZE) {
1255 /* ACL should be at least as big as the header (4 bytes) */
1256 errno = EINVAL;
1257 return NULL;
1260 size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1262 if (size % GLUSTER_ACL_ENTRY_SIZE) {
1263 /* Size of entries must strictly be a multiple of
1264 size of an ACE (8 bytes)
1266 errno = EINVAL;
1267 return NULL;
1270 count = size / GLUSTER_ACL_ENTRY_SIZE;
1272 /* Version is the first 4 bytes of the ACL */
1273 if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1274 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1275 IVAL(buf, 0)));
1276 return NULL;
1278 offset = GLUSTER_ACL_HEADER_SIZE;
1280 result = sys_acl_init(mem_ctx);
1281 if (!result) {
1282 errno = ENOMEM;
1283 return NULL;
1286 result->acl = talloc_array(result, struct smb_acl_entry, count);
1287 if (!result->acl) {
1288 errno = ENOMEM;
1289 talloc_free(result);
1290 return NULL;
1293 result->count = count;
1295 smb_ace = result->acl;
1297 for (i = 0; i < count; i++) {
1298 /* TAG is the first 2 bytes of an entry */
1299 tag = SVAL(buf, offset);
1300 offset += 2;
1302 /* PERM is the next 2 bytes of an entry */
1303 perm = SVAL(buf, offset);
1304 offset += 2;
1306 /* ID is the last 4 bytes of an entry */
1307 id = IVAL(buf, offset);
1308 offset += 4;
1310 switch(tag) {
1311 case GLUSTER_ACL_USER:
1312 smb_ace->a_type = SMB_ACL_USER;
1313 break;
1314 case GLUSTER_ACL_USER_OBJ:
1315 smb_ace->a_type = SMB_ACL_USER_OBJ;
1316 break;
1317 case GLUSTER_ACL_GROUP:
1318 smb_ace->a_type = SMB_ACL_GROUP;
1319 break;
1320 case GLUSTER_ACL_GROUP_OBJ:
1321 smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1322 break;
1323 case GLUSTER_ACL_OTHER:
1324 smb_ace->a_type = SMB_ACL_OTHER;
1325 break;
1326 case GLUSTER_ACL_MASK:
1327 smb_ace->a_type = SMB_ACL_MASK;
1328 break;
1329 default:
1330 DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1331 return NULL;
1335 switch(smb_ace->a_type) {
1336 case SMB_ACL_USER:
1337 smb_ace->info.user.uid = id;
1338 break;
1339 case SMB_ACL_GROUP:
1340 smb_ace->info.group.gid = id;
1341 break;
1342 default:
1343 break;
1346 smb_ace->a_perm = 0;
1347 smb_ace->a_perm |=
1348 ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1349 smb_ace->a_perm |=
1350 ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1351 smb_ace->a_perm |=
1352 ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1354 smb_ace++;
1357 return result;
1361 static int gluster_ace_cmp(const void *left, const void *right)
1363 int ret = 0;
1364 uint16_t tag_left, tag_right;
1365 uint32_t id_left, id_right;
1368 Sorting precedence:
1370 - Smaller TAG values must be earlier.
1372 - Within same TAG, smaller identifiers must be earlier, E.g:
1373 UID 0 entry must be earlier than UID 200
1374 GID 17 entry must be earlier than GID 19
1377 /* TAG is the first element in the entry */
1378 tag_left = SVAL(left, 0);
1379 tag_right = SVAL(right, 0);
1381 ret = (tag_left - tag_right);
1382 if (!ret) {
1383 /* ID is the third element in the entry, after two short
1384 integers (tag and perm), i.e at offset 4.
1386 id_left = IVAL(left, 4);
1387 id_right = IVAL(right, 4);
1388 ret = id_left - id_right;
1391 return ret;
1395 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1397 ssize_t size;
1398 struct smb_acl_entry *smb_ace;
1399 int i;
1400 int count;
1401 uint16_t tag;
1402 uint16_t perm;
1403 uint32_t id;
1404 int offset;
1406 count = theacl->count;
1408 size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1409 if (!buf) {
1410 return size;
1413 if (len < size) {
1414 errno = ERANGE;
1415 return -1;
1418 smb_ace = theacl->acl;
1420 /* Version is the first 4 bytes of the ACL */
1421 SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1422 offset = GLUSTER_ACL_HEADER_SIZE;
1424 for (i = 0; i < count; i++) {
1425 /* Calculate tag */
1426 switch(smb_ace->a_type) {
1427 case SMB_ACL_USER:
1428 tag = GLUSTER_ACL_USER;
1429 break;
1430 case SMB_ACL_USER_OBJ:
1431 tag = GLUSTER_ACL_USER_OBJ;
1432 break;
1433 case SMB_ACL_GROUP:
1434 tag = GLUSTER_ACL_GROUP;
1435 break;
1436 case SMB_ACL_GROUP_OBJ:
1437 tag = GLUSTER_ACL_GROUP_OBJ;
1438 break;
1439 case SMB_ACL_OTHER:
1440 tag = GLUSTER_ACL_OTHER;
1441 break;
1442 case SMB_ACL_MASK:
1443 tag = GLUSTER_ACL_MASK;
1444 break;
1445 default:
1446 DEBUG(0, ("Unknown tag value %d\n",
1447 smb_ace->a_type));
1448 errno = EINVAL;
1449 return -1;
1453 /* Calculate id */
1454 switch(smb_ace->a_type) {
1455 case SMB_ACL_USER:
1456 id = smb_ace->info.user.uid;
1457 break;
1458 case SMB_ACL_GROUP:
1459 id = smb_ace->info.group.gid;
1460 break;
1461 default:
1462 id = GLUSTER_ACL_UNDEFINED_ID;
1463 break;
1466 /* Calculate perm */
1467 perm = 0;
1469 perm |=
1470 ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1471 perm |=
1472 ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1473 perm |=
1474 ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1477 /* TAG is the first 2 bytes of an entry */
1478 SSVAL(buf, offset, tag);
1479 offset += 2;
1481 /* PERM is the next 2 bytes of an entry */
1482 SSVAL(buf, offset, perm);
1483 offset += 2;
1485 /* ID is the last 4 bytes of an entry */
1486 SIVAL(buf, offset, id);
1487 offset += 4;
1489 smb_ace++;
1492 /* Skip the header, sort @count number of 8-byte entries */
1493 qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1494 gluster_ace_cmp);
1496 return size;
1500 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1501 const char *path_p,
1502 SMB_ACL_TYPE_T type,
1503 TALLOC_CTX *mem_ctx)
1505 struct smb_acl_t *result;
1506 struct stat st;
1507 char *buf;
1508 const char *key;
1509 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1511 switch (type) {
1512 case SMB_ACL_TYPE_ACCESS:
1513 key = "system.posix_acl_access";
1514 break;
1515 case SMB_ACL_TYPE_DEFAULT:
1516 key = "system.posix_acl_default";
1517 break;
1518 default:
1519 errno = EINVAL;
1520 return NULL;
1523 buf = alloca(size);
1524 if (!buf) {
1525 return NULL;
1528 ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1529 if (ret == -1 && errno == ERANGE) {
1530 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1531 if (ret > 0) {
1532 buf = alloca(ret);
1533 if (!buf) {
1534 return NULL;
1536 ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1540 /* retrieving the ACL from the xattr has finally failed, do a
1541 * mode-to-acl mapping */
1543 if (ret == -1 && errno == ENODATA) {
1544 ret = glfs_stat(handle->data, path_p, &st);
1545 if (ret == 0) {
1546 result = mode_to_smb_acls(&st, mem_ctx);
1547 return result;
1551 if (ret <= 0) {
1552 return NULL;
1555 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1557 return result;
1560 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1561 struct files_struct *fsp,
1562 TALLOC_CTX *mem_ctx)
1564 struct smb_acl_t *result;
1565 struct stat st;
1566 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1567 char *buf;
1568 glfs_fd_t *glfd;
1570 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1572 buf = alloca(size);
1573 if (!buf) {
1574 return NULL;
1577 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1578 if (ret == -1 && errno == ERANGE) {
1579 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1580 if (ret > 0) {
1581 buf = alloca(ret);
1582 if (!buf) {
1583 return NULL;
1585 ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1586 buf, ret);
1590 /* retrieving the ACL from the xattr has finally failed, do a
1591 * mode-to-acl mapping */
1593 if (ret == -1 && errno == ENODATA) {
1594 ret = glfs_fstat(glfd, &st);
1595 if (ret == 0) {
1596 result = mode_to_smb_acls(&st, mem_ctx);
1597 return result;
1601 if (ret <= 0) {
1602 return NULL;
1605 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1607 return result;
1610 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1611 const char *name,
1612 SMB_ACL_TYPE_T acltype,
1613 SMB_ACL_T theacl)
1615 int ret;
1616 const char *key;
1617 char *buf;
1618 ssize_t size;
1620 switch (acltype) {
1621 case SMB_ACL_TYPE_ACCESS:
1622 key = "system.posix_acl_access";
1623 break;
1624 case SMB_ACL_TYPE_DEFAULT:
1625 key = "system.posix_acl_default";
1626 break;
1627 default:
1628 errno = EINVAL;
1629 return -1;
1632 size = smb_to_gluster_acl(theacl, 0, 0);
1633 buf = alloca(size);
1635 size = smb_to_gluster_acl(theacl, buf, size);
1636 if (size == -1) {
1637 return -1;
1640 ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1642 return ret;
1645 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1646 struct files_struct *fsp,
1647 SMB_ACL_T theacl)
1649 int ret;
1650 char *buf;
1651 ssize_t size;
1653 size = smb_to_gluster_acl(theacl, 0, 0);
1654 buf = alloca(size);
1656 size = smb_to_gluster_acl(theacl, buf, size);
1657 if (size == -1) {
1658 return -1;
1661 ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1662 "system.posix_acl_access", buf, size, 0);
1663 return ret;
1666 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1667 const char *path)
1669 return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1672 static struct vfs_fn_pointers glusterfs_fns = {
1674 /* Disk Operations */
1676 .connect_fn = vfs_gluster_connect,
1677 .disconnect_fn = vfs_gluster_disconnect,
1678 .disk_free_fn = vfs_gluster_disk_free,
1679 .get_quota_fn = vfs_gluster_get_quota,
1680 .set_quota_fn = vfs_gluster_set_quota,
1681 .statvfs_fn = vfs_gluster_statvfs,
1682 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1684 .get_dfs_referrals_fn = NULL,
1686 /* Directory Operations */
1688 .opendir_fn = vfs_gluster_opendir,
1689 .fdopendir_fn = vfs_gluster_fdopendir,
1690 .readdir_fn = vfs_gluster_readdir,
1691 .seekdir_fn = vfs_gluster_seekdir,
1692 .telldir_fn = vfs_gluster_telldir,
1693 .rewind_dir_fn = vfs_gluster_rewinddir,
1694 .mkdir_fn = vfs_gluster_mkdir,
1695 .rmdir_fn = vfs_gluster_rmdir,
1696 .closedir_fn = vfs_gluster_closedir,
1697 .init_search_op_fn = vfs_gluster_init_search_op,
1699 /* File Operations */
1701 .open_fn = vfs_gluster_open,
1702 .create_file_fn = NULL,
1703 .close_fn = vfs_gluster_close,
1704 .read_fn = vfs_gluster_read,
1705 .pread_fn = vfs_gluster_pread,
1706 .pread_send_fn = vfs_gluster_pread_send,
1707 .pread_recv_fn = vfs_gluster_recv,
1708 .write_fn = vfs_gluster_write,
1709 .pwrite_fn = vfs_gluster_pwrite,
1710 .pwrite_send_fn = vfs_gluster_pwrite_send,
1711 .pwrite_recv_fn = vfs_gluster_recv,
1712 .lseek_fn = vfs_gluster_lseek,
1713 .sendfile_fn = vfs_gluster_sendfile,
1714 .recvfile_fn = vfs_gluster_recvfile,
1715 .rename_fn = vfs_gluster_rename,
1716 .fsync_fn = vfs_gluster_fsync,
1717 .fsync_send_fn = vfs_gluster_fsync_send,
1718 .fsync_recv_fn = vfs_gluster_fsync_recv,
1720 .stat_fn = vfs_gluster_stat,
1721 .fstat_fn = vfs_gluster_fstat,
1722 .lstat_fn = vfs_gluster_lstat,
1723 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1724 .unlink_fn = vfs_gluster_unlink,
1726 .chmod_fn = vfs_gluster_chmod,
1727 .fchmod_fn = vfs_gluster_fchmod,
1728 .chown_fn = vfs_gluster_chown,
1729 .fchown_fn = vfs_gluster_fchown,
1730 .lchown_fn = vfs_gluster_lchown,
1731 .chdir_fn = vfs_gluster_chdir,
1732 .getwd_fn = vfs_gluster_getwd,
1733 .ntimes_fn = vfs_gluster_ntimes,
1734 .ftruncate_fn = vfs_gluster_ftruncate,
1735 .fallocate_fn = vfs_gluster_fallocate,
1736 .lock_fn = vfs_gluster_lock,
1737 .kernel_flock_fn = vfs_gluster_kernel_flock,
1738 .linux_setlease_fn = vfs_gluster_linux_setlease,
1739 .getlock_fn = vfs_gluster_getlock,
1740 .symlink_fn = vfs_gluster_symlink,
1741 .readlink_fn = vfs_gluster_readlink,
1742 .link_fn = vfs_gluster_link,
1743 .mknod_fn = vfs_gluster_mknod,
1744 .realpath_fn = vfs_gluster_realpath,
1745 .notify_watch_fn = vfs_gluster_notify_watch,
1746 .chflags_fn = vfs_gluster_chflags,
1747 .file_id_create_fn = NULL,
1748 .copy_chunk_send_fn = NULL,
1749 .copy_chunk_recv_fn = NULL,
1750 .streaminfo_fn = NULL,
1751 .get_real_filename_fn = vfs_gluster_get_real_filename,
1752 .connectpath_fn = vfs_gluster_connectpath,
1754 .brl_lock_windows_fn = NULL,
1755 .brl_unlock_windows_fn = NULL,
1756 .brl_cancel_windows_fn = NULL,
1757 .strict_lock_fn = NULL,
1758 .strict_unlock_fn = NULL,
1759 .translate_name_fn = NULL,
1760 .fsctl_fn = NULL,
1762 /* NT ACL Operations */
1763 .fget_nt_acl_fn = NULL,
1764 .get_nt_acl_fn = NULL,
1765 .fset_nt_acl_fn = NULL,
1766 .audit_file_fn = NULL,
1768 /* Posix ACL Operations */
1769 .chmod_acl_fn = NULL, /* passthrough to default */
1770 .fchmod_acl_fn = NULL, /* passthrough to default */
1771 .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1772 .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1773 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1774 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1775 .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1776 .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1777 .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1779 /* EA Operations */
1780 .getxattr_fn = vfs_gluster_getxattr,
1781 .fgetxattr_fn = vfs_gluster_fgetxattr,
1782 .listxattr_fn = vfs_gluster_listxattr,
1783 .flistxattr_fn = vfs_gluster_flistxattr,
1784 .removexattr_fn = vfs_gluster_removexattr,
1785 .fremovexattr_fn = vfs_gluster_fremovexattr,
1786 .setxattr_fn = vfs_gluster_setxattr,
1787 .fsetxattr_fn = vfs_gluster_fsetxattr,
1789 /* AIO Operations */
1790 .aio_force_fn = vfs_gluster_aio_force,
1792 /* Offline Operations */
1793 .is_offline_fn = vfs_gluster_is_offline,
1794 .set_offline_fn = vfs_gluster_set_offline,
1796 /* Durable handle Operations */
1797 .durable_cookie_fn = NULL,
1798 .durable_disconnect_fn = NULL,
1799 .durable_reconnect_fn = NULL,
1802 NTSTATUS vfs_glusterfs_init(void);
1803 NTSTATUS vfs_glusterfs_init(void)
1805 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1806 "glusterfs", &glusterfs_fns);