2 Unix SMB/CIFS implementation.
4 Wrap GlusterFS GFAPI calls in vfs functions.
6 Copyright (c) 2013 Anand Avati <avati@redhat.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @file vfs_glusterfs.c
24 * @author Anand Avati <avati@redhat.com>
26 * @brief Samba VFS module for glusterfs
30 * See, for example \c vfs_aio_linux.c in the \c sourc3/modules directory
31 * - sendfile/recvfile support
33 * A Samba VFS module for GlusterFS, based on Gluster's libgfapi.
34 * This is a "bottom" vfs module (not something to be stacked on top of
35 * another module), and translates (most) calls to the closest actions
36 * available in libgfapi.
41 #include "smbd/smbd.h"
44 #include "lib/util/dlinklist.h"
46 #define DEFAULT_VOLFILE_SERVER "localhost"
49 * Helper to convert struct stat to struct stat_ex.
51 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
55 dst->st_ex_dev = src->st_dev;
56 dst->st_ex_ino = src->st_ino;
57 dst->st_ex_mode = src->st_mode;
58 dst->st_ex_nlink = src->st_nlink;
59 dst->st_ex_uid = src->st_uid;
60 dst->st_ex_gid = src->st_gid;
61 dst->st_ex_rdev = src->st_rdev;
62 dst->st_ex_size = src->st_size;
63 dst->st_ex_atime.tv_sec = src->st_atime;
64 dst->st_ex_mtime.tv_sec = src->st_mtime;
65 dst->st_ex_ctime.tv_sec = src->st_ctime;
66 dst->st_ex_btime.tv_sec = src->st_mtime;
67 dst->st_ex_blksize = src->st_blksize;
68 dst->st_ex_blocks = src->st_blocks;
70 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
71 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
72 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
73 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
77 /* pre-opened glfs_t */
79 static struct glfs_preopened {
84 struct glfs_preopened *next, *prev;
88 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
90 struct glfs_preopened *entry = NULL;
92 entry = talloc_zero(NULL, struct glfs_preopened);
98 entry->volume = talloc_strdup(entry, volume);
105 entry->connectpath = talloc_strdup(entry, connectpath);
106 if (entry->connectpath == NULL) {
115 DLIST_ADD(glfs_preopened, entry);
120 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
122 struct glfs_preopened *entry = NULL;
124 for (entry = glfs_preopened; entry; entry = entry->next) {
125 if (strcmp(entry->volume, volume) == 0 &&
126 strcmp(entry->connectpath, connectpath) == 0)
136 static void glfs_clear_preopened(glfs_t *fs)
138 struct glfs_preopened *entry = NULL;
140 for (entry = glfs_preopened; entry; entry = entry->next) {
141 if (entry->fs == fs) {
145 DLIST_REMOVE(glfs_preopened, entry);
147 glfs_fini(entry->fs);
153 /* Disk Operations */
155 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
159 const char *volfile_server;
167 tmp_ctx = talloc_new(NULL);
168 if (tmp_ctx == NULL) {
172 logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
175 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
177 volfile_server = lp_parm_const_string(SNUM(handle->conn), "glusterfs",
178 "volfile_server", NULL);
179 if (volfile_server == NULL) {
180 volfile_server = DEFAULT_VOLFILE_SERVER;
183 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
185 if (volume == NULL) {
189 fs = glfs_find_preopened(volume, handle->conn->connectpath);
194 fs = glfs_new(volume);
200 ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 0);
202 DEBUG(0, ("Failed to set volfile_server %s\n", volfile_server));
206 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
209 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
214 ret = glfs_set_xlator_option(fs, "*-snapview-client",
215 "snapdir-entry-path",
216 handle->conn->connectpath);
218 DEBUG(0, ("%s: Failed to set xlator option:"
219 " snapdir-entry-path\n", volume));
224 ret = glfs_set_logging(fs, logfile, loglevel);
226 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
227 volume, logfile, loglevel));
233 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
234 volume, strerror(errno)));
238 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
240 DEBUG(0, ("%s: Failed to register volume (%s)\n",
241 volume, strerror(errno)));
245 talloc_free(tmp_ctx);
251 DEBUG(0, ("%s: Initialized volume from server %s\n",
252 volume, volfile_server));
258 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
264 glfs_clear_preopened(fs);
267 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
268 const char *path, bool small_query,
269 uint64_t *bsize_p, uint64_t *dfree_p,
272 struct statvfs statvfs = { 0, };
275 ret = glfs_statvfs(handle->data, path, &statvfs);
280 if (bsize_p != NULL) {
281 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
283 if (dfree_p != NULL) {
284 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
286 if (dsize_p != NULL) {
287 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
290 return (uint64_t)statvfs.f_bavail;
293 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
294 enum SMB_QUOTA_TYPE qtype, unid_t id,
302 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
303 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
309 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
311 struct vfs_statvfs_struct *vfs_statvfs)
313 struct statvfs statvfs = { 0, };
316 ret = glfs_statvfs(handle->data, path, &statvfs);
318 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
319 path, strerror(errno)));
323 ZERO_STRUCTP(vfs_statvfs);
325 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
326 vfs_statvfs->BlockSize = statvfs.f_bsize;
327 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
328 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
329 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
330 vfs_statvfs->TotalFileNodes = statvfs.f_files;
331 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
332 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
333 vfs_statvfs->FsCapabilities =
334 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
339 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
340 enum timestamp_set_resolution *p_ts_res)
342 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
344 #ifdef STAT_HAVE_NSEC
345 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
351 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
352 const char *path, const char *mask,
357 fd = glfs_opendir(handle->data, path);
359 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
360 path, strerror(errno)));
366 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
367 files_struct *fsp, const char *mask,
370 return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
373 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
375 return glfs_closedir((void *)dirp);
378 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
379 DIR *dirp, SMB_STRUCT_STAT *sbuf)
381 static char direntbuf[512];
384 struct dirent *dirent = 0;
387 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
390 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
393 if ((ret < 0) || (dirent == NULL)) {
398 smb_stat_ex_from_stat(sbuf, &stat);
404 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
406 return glfs_telldir((void *)dirp);
409 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
412 glfs_seekdir((void *)dirp, offset);
415 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
417 glfs_seekdir((void *)dirp, 0);
420 static void vfs_gluster_init_search_op(struct vfs_handle_struct *handle,
426 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle, const char *path,
429 return glfs_mkdir(handle->data, path, mode);
432 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle, const char *path)
434 return glfs_rmdir(handle->data, path);
437 static int vfs_gluster_open(struct vfs_handle_struct *handle,
438 struct smb_filename *smb_fname, files_struct *fsp,
439 int flags, mode_t mode)
444 if (flags & O_DIRECTORY) {
445 glfd = glfs_opendir(handle->data, smb_fname->base_name);
446 } else if (flags & O_CREAT) {
447 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
450 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
456 p_tmp = (glfs_fd_t **)VFS_ADD_FSP_EXTENSION(handle, fsp,
459 /* An arbitrary value for error reporting, so you know its us. */
463 static int vfs_gluster_close(struct vfs_handle_struct *handle,
467 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
468 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
469 return glfs_close(glfd);
472 static ssize_t vfs_gluster_read(struct vfs_handle_struct *handle,
473 files_struct *fsp, void *data, size_t n)
475 return glfs_read(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
478 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
479 files_struct *fsp, void *data, size_t n,
482 return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
485 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
486 *handle, TALLOC_CTX *mem_ctx,
487 struct tevent_context *ev,
488 files_struct *fsp, void *data,
489 size_t n, off_t offset)
495 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req, int *err)
501 static ssize_t vfs_gluster_write(struct vfs_handle_struct *handle,
502 files_struct *fsp, const void *data, size_t n)
504 return glfs_write(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
507 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
508 files_struct *fsp, const void *data,
509 size_t n, off_t offset)
511 return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
514 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
515 *handle, TALLOC_CTX *mem_ctx,
516 struct tevent_context *ev,
518 const void *data, size_t n,
525 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req, int *err)
531 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
532 files_struct *fsp, off_t offset, int whence)
534 return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
537 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
538 files_struct *fromfsp,
539 const DATA_BLOB *hdr,
540 off_t offset, size_t n)
546 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
547 int fromfd, files_struct *tofsp,
548 off_t offset, size_t n)
554 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
555 const struct smb_filename *smb_fname_src,
556 const struct smb_filename *smb_fname_dst)
558 return glfs_rename(handle->data, smb_fname_src->base_name,
559 smb_fname_dst->base_name);
562 static int vfs_gluster_fsync(struct vfs_handle_struct *handle,
565 return glfs_fsync(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp));
568 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
569 *handle, TALLOC_CTX *mem_ctx,
570 struct tevent_context *ev,
577 static int vfs_gluster_fsync_recv(struct tevent_req *req, int *err)
583 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
584 struct smb_filename *smb_fname)
589 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
591 smb_stat_ex_from_stat(&smb_fname->st, &st);
593 if (ret < 0 && errno != ENOENT) {
594 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
595 smb_fname->base_name, strerror(errno)));
600 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
601 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
606 ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
608 smb_stat_ex_from_stat(sbuf, &st);
611 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
612 fsp->fh->fd, strerror(errno)));
617 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
618 struct smb_filename *smb_fname)
623 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
625 smb_stat_ex_from_stat(&smb_fname->st, &st);
627 if (ret < 0 && errno != ENOENT) {
628 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
629 smb_fname->base_name, strerror(errno)));
634 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
636 const SMB_STRUCT_STAT *sbuf)
638 return sbuf->st_ex_blocks * 512;
641 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
642 const struct smb_filename *smb_fname)
644 return glfs_unlink(handle->data, smb_fname->base_name);
647 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
648 const char *path, mode_t mode)
650 return glfs_chmod(handle->data, path, mode);
653 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
654 files_struct *fsp, mode_t mode)
656 return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
659 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
660 const char *path, uid_t uid, gid_t gid)
662 return glfs_chown(handle->data, path, uid, gid);
665 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
666 files_struct *fsp, uid_t uid, gid_t gid)
668 return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
671 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
672 const char *path, uid_t uid, gid_t gid)
674 return glfs_lchown(handle->data, path, uid, gid);
677 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
679 return glfs_chdir(handle->data, path);
682 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
687 cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
692 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
699 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
700 const struct smb_filename *smb_fname,
701 struct smb_file_time *ft)
703 struct timespec times[2];
705 if (null_timespec(ft->atime)) {
706 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
707 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
709 times[0].tv_sec = ft->atime.tv_sec;
710 times[0].tv_nsec = ft->atime.tv_nsec;
713 if (null_timespec(ft->mtime)) {
714 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
715 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
717 times[1].tv_sec = ft->mtime.tv_sec;
718 times[1].tv_nsec = ft->mtime.tv_nsec;
721 if ((timespec_compare(×[0],
722 &smb_fname->st.st_ex_atime) == 0) &&
723 (timespec_compare(×[1],
724 &smb_fname->st.st_ex_mtime) == 0)) {
728 return glfs_utimens(handle->data, smb_fname->base_name, times);
731 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
732 files_struct *fsp, off_t offset)
734 return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
737 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
738 struct files_struct *fsp,
739 enum vfs_fallocate_mode mode,
740 off_t offset, off_t len)
746 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
749 return glfs_realpath(handle->data, path, 0);
752 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
753 files_struct *fsp, int op, off_t offset,
754 off_t count, int type)
756 struct flock flock = { 0, };
760 flock.l_whence = SEEK_SET;
761 flock.l_start = offset;
765 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
768 /* lock query, true if someone else has locked */
770 (flock.l_type != F_UNLCK) &&
771 (flock.l_pid != 0) && (flock.l_pid != getpid()))
784 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
785 files_struct *fsp, uint32 share_mode,
786 uint32_t access_mask)
792 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
793 files_struct *fsp, int leasetype)
799 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
800 files_struct *fsp, off_t *poffset,
801 off_t *pcount, int *ptype, pid_t *ppid)
803 struct flock flock = { 0, };
806 flock.l_type = *ptype;
807 flock.l_whence = SEEK_SET;
808 flock.l_start = *poffset;
809 flock.l_len = *pcount;
812 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
818 *ptype = flock.l_type;
819 *poffset = flock.l_start;
820 *pcount = flock.l_len;
826 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
827 const char *oldpath, const char *newpath)
829 return glfs_symlink(handle->data, oldpath, newpath);
832 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
833 const char *path, char *buf, size_t bufsiz)
835 return glfs_readlink(handle->data, path, buf, bufsiz);
838 static int vfs_gluster_link(struct vfs_handle_struct *handle,
839 const char *oldpath, const char *newpath)
841 return glfs_link(handle->data, oldpath, newpath);
844 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
845 mode_t mode, SMB_DEV_T dev)
847 return glfs_mknod(handle->data, path, mode, dev);
850 static NTSTATUS vfs_gluster_notify_watch(struct vfs_handle_struct *handle,
851 struct sys_notify_context *ctx,
852 const char *path, uint32_t *filter,
853 uint32_t *subdir_filter,
854 void (*callback) (struct sys_notify_context *ctx,
856 struct notify_event *ev),
857 void *private_data, void *handle_p)
859 return NT_STATUS_NOT_IMPLEMENTED;
862 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
863 const char *path, unsigned int flags)
869 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
870 const char *path, const char *name,
871 TALLOC_CTX *mem_ctx, char **found_name)
874 char key_buf[NAME_MAX + 64];
875 char val_buf[NAME_MAX + 1];
877 if (strlen(name) >= NAME_MAX) {
878 errno = ENAMETOOLONG;
882 snprintf(key_buf, NAME_MAX + 64,
883 "user.glusterfs.get_real_filename:%s", name);
885 ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
887 if (errno == ENODATA) {
893 *found_name = talloc_strdup(mem_ctx, val_buf);
894 if (found_name[0] == NULL) {
901 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
902 const char *filename)
904 return handle->conn->connectpath;
909 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
910 const char *path, const char *name,
911 void *value, size_t size)
913 return glfs_getxattr(handle->data, path, name, value, size);
916 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
917 files_struct *fsp, const char *name,
918 void *value, size_t size)
920 return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
923 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
924 const char *path, char *list, size_t size)
926 return glfs_listxattr(handle->data, path, list, size);
929 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
930 files_struct *fsp, char *list,
933 return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
936 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
937 const char *path, const char *name)
939 return glfs_removexattr(handle->data, path, name);
942 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
943 files_struct *fsp, const char *name)
945 return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
948 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
949 const char *path, const char *name,
950 const void *value, size_t size, int flags)
952 return glfs_setxattr(handle->data, path, name, value, size, flags);
955 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
956 files_struct *fsp, const char *name,
957 const void *value, size_t size, int flags)
959 return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
965 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
971 /* Offline Operations */
973 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
974 const struct smb_filename *fname,
975 SMB_STRUCT_STAT *sbuf)
980 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
981 const struct smb_filename *fname)
990 Size = 4 (header) + N * 8 (entry)
992 Offset Size Field (Little Endian)
993 -------------------------------------
996 4-5 2-byte Entry-1 tag
997 6-7 2-byte Entry-1 perm
998 8-11 4-byte Entry-1 id
1000 12-13 2-byte Entry-2 tag
1001 14-15 2-byte Entry-2 perm
1002 16-19 4-byte Entry-2 id
1008 /* header version */
1009 #define GLUSTER_ACL_VERSION 2
1012 #define GLUSTER_ACL_READ 0x04
1013 #define GLUSTER_ACL_WRITE 0x02
1014 #define GLUSTER_ACL_EXECUTE 0x01
1017 #define GLUSTER_ACL_UNDEFINED_TAG 0x00
1018 #define GLUSTER_ACL_USER_OBJ 0x01
1019 #define GLUSTER_ACL_USER 0x02
1020 #define GLUSTER_ACL_GROUP_OBJ 0x04
1021 #define GLUSTER_ACL_GROUP 0x08
1022 #define GLUSTER_ACL_MASK 0x10
1023 #define GLUSTER_ACL_OTHER 0x20
1025 #define GLUSTER_ACL_UNDEFINED_ID (-1)
1027 #define GLUSTER_ACL_HEADER_SIZE 4
1028 #define GLUSTER_ACL_ENTRY_SIZE 8
1030 #define GLUSTER_ACL_SIZE(n) (GLUSTER_ACL_HEADER_SIZE + (n * GLUSTER_ACL_ENTRY_SIZE))
1032 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1033 TALLOC_CTX *mem_ctx)
1037 struct smb_acl_entry *smb_ace;
1038 struct smb_acl_t *result;
1047 if (size < GLUSTER_ACL_HEADER_SIZE) {
1048 /* ACL should be at least as big as the header (4 bytes) */
1053 size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1055 if (size % GLUSTER_ACL_ENTRY_SIZE) {
1056 /* Size of entries must strictly be a multiple of
1057 size of an ACE (8 bytes)
1063 count = size / GLUSTER_ACL_ENTRY_SIZE;
1065 /* Version is the first 4 bytes of the ACL */
1066 if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1067 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1071 offset = GLUSTER_ACL_HEADER_SIZE;
1073 result = sys_acl_init(mem_ctx);
1079 result->acl = talloc_array(result, struct smb_acl_entry, count);
1082 talloc_free(result);
1086 result->count = count;
1088 smb_ace = result->acl;
1090 for (i = 0; i < count; i++) {
1091 /* TAG is the first 2 bytes of an entry */
1092 tag = SVAL(buf, offset);
1095 /* PERM is the next 2 bytes of an entry */
1096 perm = SVAL(buf, offset);
1099 /* ID is the last 4 bytes of an entry */
1100 id = IVAL(buf, offset);
1104 case GLUSTER_ACL_USER:
1105 smb_ace->a_type = SMB_ACL_USER;
1107 case GLUSTER_ACL_USER_OBJ:
1108 smb_ace->a_type = SMB_ACL_USER_OBJ;
1110 case GLUSTER_ACL_GROUP:
1111 smb_ace->a_type = SMB_ACL_GROUP;
1113 case GLUSTER_ACL_GROUP_OBJ:
1114 smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1116 case GLUSTER_ACL_OTHER:
1117 smb_ace->a_type = SMB_ACL_OTHER;
1119 case GLUSTER_ACL_MASK:
1120 smb_ace->a_type = SMB_ACL_MASK;
1123 DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1128 switch(smb_ace->a_type) {
1130 smb_ace->info.user.uid = id;
1133 smb_ace->info.group.gid = id;
1139 smb_ace->a_perm = 0;
1141 ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1143 ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1145 ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1154 static int gluster_ace_cmp(const void *left, const void *right)
1157 uint16_t tag_left, tag_right;
1158 uint32_t id_left, id_right;
1163 - Smaller TAG values must be earlier.
1165 - Within same TAG, smaller identifiers must be earlier, E.g:
1166 UID 0 entry must be earlier than UID 200
1167 GID 17 entry must be earlier than GID 19
1170 /* TAG is the first element in the entry */
1171 tag_left = SVAL(left, 0);
1172 tag_right = SVAL(right, 0);
1174 ret = (tag_left - tag_right);
1176 /* ID is the third element in the entry, after two short
1177 integers (tag and perm), i.e at offset 4.
1179 id_left = IVAL(left, 4);
1180 id_right = IVAL(right, 4);
1181 ret = id_left - id_right;
1188 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1191 struct smb_acl_entry *smb_ace;
1199 count = theacl->count;
1201 size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1211 smb_ace = theacl->acl;
1213 /* Version is the first 4 bytes of the ACL */
1214 SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1215 offset = GLUSTER_ACL_HEADER_SIZE;
1217 for (i = 0; i < count; i++) {
1219 switch(smb_ace->a_type) {
1221 tag = GLUSTER_ACL_USER;
1223 case SMB_ACL_USER_OBJ:
1224 tag = GLUSTER_ACL_USER_OBJ;
1227 tag = GLUSTER_ACL_GROUP;
1229 case SMB_ACL_GROUP_OBJ:
1230 tag = GLUSTER_ACL_GROUP_OBJ;
1233 tag = GLUSTER_ACL_OTHER;
1236 tag = GLUSTER_ACL_MASK;
1239 DEBUG(0, ("Unknown tag value %d\n",
1247 switch(smb_ace->a_type) {
1249 id = smb_ace->info.user.uid;
1252 id = smb_ace->info.group.gid;
1255 id = GLUSTER_ACL_UNDEFINED_ID;
1259 /* Calculate perm */
1263 ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1265 ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1267 ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1270 /* TAG is the first 2 bytes of an entry */
1271 SSVAL(buf, offset, tag);
1274 /* PERM is the next 2 bytes of an entry */
1275 SSVAL(buf, offset, perm);
1278 /* ID is the last 4 bytes of an entry */
1279 SIVAL(buf, offset, id);
1285 /* Skip the header, sort @count number of 8-byte entries */
1286 qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1293 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1295 SMB_ACL_TYPE_T type,
1296 TALLOC_CTX *mem_ctx)
1298 struct smb_acl_t *result;
1301 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1304 case SMB_ACL_TYPE_ACCESS:
1305 key = "system.posix_acl_access";
1307 case SMB_ACL_TYPE_DEFAULT:
1308 key = "system.posix_acl_default";
1320 ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1321 if (ret == -1 && errno == ERANGE) {
1322 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1328 ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1335 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1340 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1341 struct files_struct *fsp,
1342 TALLOC_CTX *mem_ctx)
1344 struct smb_acl_t *result;
1345 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1349 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1356 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1357 if (ret == -1 && errno == ERANGE) {
1358 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1364 ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1372 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1377 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1379 SMB_ACL_TYPE_T acltype,
1388 case SMB_ACL_TYPE_ACCESS:
1389 key = "system.posix_acl_access";
1391 case SMB_ACL_TYPE_DEFAULT:
1392 key = "system.posix_acl_default";
1399 size = smb_to_gluster_acl(theacl, 0, 0);
1402 size = smb_to_gluster_acl(theacl, buf, size);
1407 ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1412 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1413 struct files_struct *fsp,
1420 size = smb_to_gluster_acl(theacl, 0, 0);
1423 size = smb_to_gluster_acl(theacl, buf, size);
1428 ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1429 "system.posix_acl_access", buf, size, 0);
1433 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1436 return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1439 static struct vfs_fn_pointers glusterfs_fns = {
1441 /* Disk Operations */
1443 .connect_fn = vfs_gluster_connect,
1444 .disconnect_fn = vfs_gluster_disconnect,
1445 .disk_free_fn = vfs_gluster_disk_free,
1446 .get_quota_fn = vfs_gluster_get_quota,
1447 .set_quota_fn = vfs_gluster_set_quota,
1448 .statvfs_fn = vfs_gluster_statvfs,
1449 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1451 .get_dfs_referrals_fn = NULL,
1453 /* Directory Operations */
1455 .opendir_fn = vfs_gluster_opendir,
1456 .fdopendir_fn = vfs_gluster_fdopendir,
1457 .readdir_fn = vfs_gluster_readdir,
1458 .seekdir_fn = vfs_gluster_seekdir,
1459 .telldir_fn = vfs_gluster_telldir,
1460 .rewind_dir_fn = vfs_gluster_rewinddir,
1461 .mkdir_fn = vfs_gluster_mkdir,
1462 .rmdir_fn = vfs_gluster_rmdir,
1463 .closedir_fn = vfs_gluster_closedir,
1464 .init_search_op_fn = vfs_gluster_init_search_op,
1466 /* File Operations */
1468 .open_fn = vfs_gluster_open,
1469 .create_file_fn = NULL,
1470 .close_fn = vfs_gluster_close,
1471 .read_fn = vfs_gluster_read,
1472 .pread_fn = vfs_gluster_pread,
1473 .pread_send_fn = vfs_gluster_pread_send,
1474 .pread_recv_fn = vfs_gluster_pread_recv,
1475 .write_fn = vfs_gluster_write,
1476 .pwrite_fn = vfs_gluster_pwrite,
1477 .pwrite_send_fn = vfs_gluster_pwrite_send,
1478 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
1479 .lseek_fn = vfs_gluster_lseek,
1480 .sendfile_fn = vfs_gluster_sendfile,
1481 .recvfile_fn = vfs_gluster_recvfile,
1482 .rename_fn = vfs_gluster_rename,
1483 .fsync_fn = vfs_gluster_fsync,
1484 .fsync_send_fn = vfs_gluster_fsync_send,
1485 .fsync_recv_fn = vfs_gluster_fsync_recv,
1487 .stat_fn = vfs_gluster_stat,
1488 .fstat_fn = vfs_gluster_fstat,
1489 .lstat_fn = vfs_gluster_lstat,
1490 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1491 .unlink_fn = vfs_gluster_unlink,
1493 .chmod_fn = vfs_gluster_chmod,
1494 .fchmod_fn = vfs_gluster_fchmod,
1495 .chown_fn = vfs_gluster_chown,
1496 .fchown_fn = vfs_gluster_fchown,
1497 .lchown_fn = vfs_gluster_lchown,
1498 .chdir_fn = vfs_gluster_chdir,
1499 .getwd_fn = vfs_gluster_getwd,
1500 .ntimes_fn = vfs_gluster_ntimes,
1501 .ftruncate_fn = vfs_gluster_ftruncate,
1502 .fallocate_fn = vfs_gluster_fallocate,
1503 .lock_fn = vfs_gluster_lock,
1504 .kernel_flock_fn = vfs_gluster_kernel_flock,
1505 .linux_setlease_fn = vfs_gluster_linux_setlease,
1506 .getlock_fn = vfs_gluster_getlock,
1507 .symlink_fn = vfs_gluster_symlink,
1508 .readlink_fn = vfs_gluster_readlink,
1509 .link_fn = vfs_gluster_link,
1510 .mknod_fn = vfs_gluster_mknod,
1511 .realpath_fn = vfs_gluster_realpath,
1512 .notify_watch_fn = vfs_gluster_notify_watch,
1513 .chflags_fn = vfs_gluster_chflags,
1514 .file_id_create_fn = NULL,
1515 .copy_chunk_send_fn = NULL,
1516 .copy_chunk_recv_fn = NULL,
1517 .streaminfo_fn = NULL,
1518 .get_real_filename_fn = vfs_gluster_get_real_filename,
1519 .connectpath_fn = vfs_gluster_connectpath,
1521 .brl_lock_windows_fn = NULL,
1522 .brl_unlock_windows_fn = NULL,
1523 .brl_cancel_windows_fn = NULL,
1524 .strict_lock_fn = NULL,
1525 .strict_unlock_fn = NULL,
1526 .translate_name_fn = NULL,
1529 /* NT ACL Operations */
1530 .fget_nt_acl_fn = NULL,
1531 .get_nt_acl_fn = NULL,
1532 .fset_nt_acl_fn = NULL,
1533 .audit_file_fn = NULL,
1535 /* Posix ACL Operations */
1536 .chmod_acl_fn = NULL, /* passthrough to default */
1537 .fchmod_acl_fn = NULL, /* passthrough to default */
1538 .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1539 .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1540 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1541 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1542 .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1543 .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1544 .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1547 .getxattr_fn = vfs_gluster_getxattr,
1548 .fgetxattr_fn = vfs_gluster_fgetxattr,
1549 .listxattr_fn = vfs_gluster_listxattr,
1550 .flistxattr_fn = vfs_gluster_flistxattr,
1551 .removexattr_fn = vfs_gluster_removexattr,
1552 .fremovexattr_fn = vfs_gluster_fremovexattr,
1553 .setxattr_fn = vfs_gluster_setxattr,
1554 .fsetxattr_fn = vfs_gluster_fsetxattr,
1556 /* AIO Operations */
1557 .aio_force_fn = vfs_gluster_aio_force,
1559 /* Offline Operations */
1560 .is_offline_fn = vfs_gluster_is_offline,
1561 .set_offline_fn = vfs_gluster_set_offline,
1563 /* Durable handle Operations */
1564 .durable_cookie_fn = NULL,
1565 .durable_disconnect_fn = NULL,
1566 .durable_reconnect_fn = NULL,
1569 NTSTATUS vfs_glusterfs_init(void);
1570 NTSTATUS vfs_glusterfs_init(void)
1572 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1573 "glusterfs", &glusterfs_fns);