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 mode_to_smb_acls(const struct stat *mode, TALLOC_CTX *mem_ctx)
1034 struct smb_acl_t *result;
1038 result = sys_acl_init(mem_ctx);
1044 result->acl = talloc_array(result, struct smb_acl_entry, count);
1047 talloc_free(result);
1051 result->count = count;
1053 result->acl[0].a_type = SMB_ACL_USER_OBJ;
1054 result->acl[0].a_perm = (mode->st_mode & S_IRWXU) >> 6;;
1056 result->acl[1].a_type = SMB_ACL_GROUP_OBJ;
1057 result->acl[1].a_perm = (mode->st_mode & S_IRWXG) >> 3;;
1059 result->acl[2].a_type = SMB_ACL_OTHER;
1060 result->acl[2].a_perm = mode->st_mode & S_IRWXO;;
1065 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1066 TALLOC_CTX *mem_ctx)
1070 struct smb_acl_entry *smb_ace;
1071 struct smb_acl_t *result;
1080 if (size < GLUSTER_ACL_HEADER_SIZE) {
1081 /* ACL should be at least as big as the header (4 bytes) */
1086 size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1088 if (size % GLUSTER_ACL_ENTRY_SIZE) {
1089 /* Size of entries must strictly be a multiple of
1090 size of an ACE (8 bytes)
1096 count = size / GLUSTER_ACL_ENTRY_SIZE;
1098 /* Version is the first 4 bytes of the ACL */
1099 if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1100 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1104 offset = GLUSTER_ACL_HEADER_SIZE;
1106 result = sys_acl_init(mem_ctx);
1112 result->acl = talloc_array(result, struct smb_acl_entry, count);
1115 talloc_free(result);
1119 result->count = count;
1121 smb_ace = result->acl;
1123 for (i = 0; i < count; i++) {
1124 /* TAG is the first 2 bytes of an entry */
1125 tag = SVAL(buf, offset);
1128 /* PERM is the next 2 bytes of an entry */
1129 perm = SVAL(buf, offset);
1132 /* ID is the last 4 bytes of an entry */
1133 id = IVAL(buf, offset);
1137 case GLUSTER_ACL_USER:
1138 smb_ace->a_type = SMB_ACL_USER;
1140 case GLUSTER_ACL_USER_OBJ:
1141 smb_ace->a_type = SMB_ACL_USER_OBJ;
1143 case GLUSTER_ACL_GROUP:
1144 smb_ace->a_type = SMB_ACL_GROUP;
1146 case GLUSTER_ACL_GROUP_OBJ:
1147 smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1149 case GLUSTER_ACL_OTHER:
1150 smb_ace->a_type = SMB_ACL_OTHER;
1152 case GLUSTER_ACL_MASK:
1153 smb_ace->a_type = SMB_ACL_MASK;
1156 DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1161 switch(smb_ace->a_type) {
1163 smb_ace->info.user.uid = id;
1166 smb_ace->info.group.gid = id;
1172 smb_ace->a_perm = 0;
1174 ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1176 ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1178 ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1187 static int gluster_ace_cmp(const void *left, const void *right)
1190 uint16_t tag_left, tag_right;
1191 uint32_t id_left, id_right;
1196 - Smaller TAG values must be earlier.
1198 - Within same TAG, smaller identifiers must be earlier, E.g:
1199 UID 0 entry must be earlier than UID 200
1200 GID 17 entry must be earlier than GID 19
1203 /* TAG is the first element in the entry */
1204 tag_left = SVAL(left, 0);
1205 tag_right = SVAL(right, 0);
1207 ret = (tag_left - tag_right);
1209 /* ID is the third element in the entry, after two short
1210 integers (tag and perm), i.e at offset 4.
1212 id_left = IVAL(left, 4);
1213 id_right = IVAL(right, 4);
1214 ret = id_left - id_right;
1221 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1224 struct smb_acl_entry *smb_ace;
1232 count = theacl->count;
1234 size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1244 smb_ace = theacl->acl;
1246 /* Version is the first 4 bytes of the ACL */
1247 SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1248 offset = GLUSTER_ACL_HEADER_SIZE;
1250 for (i = 0; i < count; i++) {
1252 switch(smb_ace->a_type) {
1254 tag = GLUSTER_ACL_USER;
1256 case SMB_ACL_USER_OBJ:
1257 tag = GLUSTER_ACL_USER_OBJ;
1260 tag = GLUSTER_ACL_GROUP;
1262 case SMB_ACL_GROUP_OBJ:
1263 tag = GLUSTER_ACL_GROUP_OBJ;
1266 tag = GLUSTER_ACL_OTHER;
1269 tag = GLUSTER_ACL_MASK;
1272 DEBUG(0, ("Unknown tag value %d\n",
1280 switch(smb_ace->a_type) {
1282 id = smb_ace->info.user.uid;
1285 id = smb_ace->info.group.gid;
1288 id = GLUSTER_ACL_UNDEFINED_ID;
1292 /* Calculate perm */
1296 ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1298 ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1300 ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1303 /* TAG is the first 2 bytes of an entry */
1304 SSVAL(buf, offset, tag);
1307 /* PERM is the next 2 bytes of an entry */
1308 SSVAL(buf, offset, perm);
1311 /* ID is the last 4 bytes of an entry */
1312 SIVAL(buf, offset, id);
1318 /* Skip the header, sort @count number of 8-byte entries */
1319 qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1326 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1328 SMB_ACL_TYPE_T type,
1329 TALLOC_CTX *mem_ctx)
1331 struct smb_acl_t *result;
1335 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1338 case SMB_ACL_TYPE_ACCESS:
1339 key = "system.posix_acl_access";
1341 case SMB_ACL_TYPE_DEFAULT:
1342 key = "system.posix_acl_default";
1354 ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1355 if (ret == -1 && errno == ERANGE) {
1356 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1362 ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1366 /* retrieving the ACL from the xattr has finally failed, do a
1367 * mode-to-acl mapping */
1369 if (ret == -1 && errno == ENODATA) {
1370 ret = glfs_stat(handle->data, path_p, &st);
1372 result = mode_to_smb_acls(&st, mem_ctx);
1381 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1386 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1387 struct files_struct *fsp,
1388 TALLOC_CTX *mem_ctx)
1390 struct smb_acl_t *result;
1392 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1396 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1403 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1404 if (ret == -1 && errno == ERANGE) {
1405 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1411 ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1416 /* retrieving the ACL from the xattr has finally failed, do a
1417 * mode-to-acl mapping */
1419 if (ret == -1 && errno == ENODATA) {
1420 ret = glfs_fstat(glfd, &st);
1422 result = mode_to_smb_acls(&st, mem_ctx);
1431 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1436 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1438 SMB_ACL_TYPE_T acltype,
1447 case SMB_ACL_TYPE_ACCESS:
1448 key = "system.posix_acl_access";
1450 case SMB_ACL_TYPE_DEFAULT:
1451 key = "system.posix_acl_default";
1458 size = smb_to_gluster_acl(theacl, 0, 0);
1461 size = smb_to_gluster_acl(theacl, buf, size);
1466 ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1471 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1472 struct files_struct *fsp,
1479 size = smb_to_gluster_acl(theacl, 0, 0);
1482 size = smb_to_gluster_acl(theacl, buf, size);
1487 ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1488 "system.posix_acl_access", buf, size, 0);
1492 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1495 return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1498 static struct vfs_fn_pointers glusterfs_fns = {
1500 /* Disk Operations */
1502 .connect_fn = vfs_gluster_connect,
1503 .disconnect_fn = vfs_gluster_disconnect,
1504 .disk_free_fn = vfs_gluster_disk_free,
1505 .get_quota_fn = vfs_gluster_get_quota,
1506 .set_quota_fn = vfs_gluster_set_quota,
1507 .statvfs_fn = vfs_gluster_statvfs,
1508 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1510 .get_dfs_referrals_fn = NULL,
1512 /* Directory Operations */
1514 .opendir_fn = vfs_gluster_opendir,
1515 .fdopendir_fn = vfs_gluster_fdopendir,
1516 .readdir_fn = vfs_gluster_readdir,
1517 .seekdir_fn = vfs_gluster_seekdir,
1518 .telldir_fn = vfs_gluster_telldir,
1519 .rewind_dir_fn = vfs_gluster_rewinddir,
1520 .mkdir_fn = vfs_gluster_mkdir,
1521 .rmdir_fn = vfs_gluster_rmdir,
1522 .closedir_fn = vfs_gluster_closedir,
1523 .init_search_op_fn = vfs_gluster_init_search_op,
1525 /* File Operations */
1527 .open_fn = vfs_gluster_open,
1528 .create_file_fn = NULL,
1529 .close_fn = vfs_gluster_close,
1530 .read_fn = vfs_gluster_read,
1531 .pread_fn = vfs_gluster_pread,
1532 .pread_send_fn = vfs_gluster_pread_send,
1533 .pread_recv_fn = vfs_gluster_pread_recv,
1534 .write_fn = vfs_gluster_write,
1535 .pwrite_fn = vfs_gluster_pwrite,
1536 .pwrite_send_fn = vfs_gluster_pwrite_send,
1537 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
1538 .lseek_fn = vfs_gluster_lseek,
1539 .sendfile_fn = vfs_gluster_sendfile,
1540 .recvfile_fn = vfs_gluster_recvfile,
1541 .rename_fn = vfs_gluster_rename,
1542 .fsync_fn = vfs_gluster_fsync,
1543 .fsync_send_fn = vfs_gluster_fsync_send,
1544 .fsync_recv_fn = vfs_gluster_fsync_recv,
1546 .stat_fn = vfs_gluster_stat,
1547 .fstat_fn = vfs_gluster_fstat,
1548 .lstat_fn = vfs_gluster_lstat,
1549 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1550 .unlink_fn = vfs_gluster_unlink,
1552 .chmod_fn = vfs_gluster_chmod,
1553 .fchmod_fn = vfs_gluster_fchmod,
1554 .chown_fn = vfs_gluster_chown,
1555 .fchown_fn = vfs_gluster_fchown,
1556 .lchown_fn = vfs_gluster_lchown,
1557 .chdir_fn = vfs_gluster_chdir,
1558 .getwd_fn = vfs_gluster_getwd,
1559 .ntimes_fn = vfs_gluster_ntimes,
1560 .ftruncate_fn = vfs_gluster_ftruncate,
1561 .fallocate_fn = vfs_gluster_fallocate,
1562 .lock_fn = vfs_gluster_lock,
1563 .kernel_flock_fn = vfs_gluster_kernel_flock,
1564 .linux_setlease_fn = vfs_gluster_linux_setlease,
1565 .getlock_fn = vfs_gluster_getlock,
1566 .symlink_fn = vfs_gluster_symlink,
1567 .readlink_fn = vfs_gluster_readlink,
1568 .link_fn = vfs_gluster_link,
1569 .mknod_fn = vfs_gluster_mknod,
1570 .realpath_fn = vfs_gluster_realpath,
1571 .notify_watch_fn = vfs_gluster_notify_watch,
1572 .chflags_fn = vfs_gluster_chflags,
1573 .file_id_create_fn = NULL,
1574 .copy_chunk_send_fn = NULL,
1575 .copy_chunk_recv_fn = NULL,
1576 .streaminfo_fn = NULL,
1577 .get_real_filename_fn = vfs_gluster_get_real_filename,
1578 .connectpath_fn = vfs_gluster_connectpath,
1580 .brl_lock_windows_fn = NULL,
1581 .brl_unlock_windows_fn = NULL,
1582 .brl_cancel_windows_fn = NULL,
1583 .strict_lock_fn = NULL,
1584 .strict_unlock_fn = NULL,
1585 .translate_name_fn = NULL,
1588 /* NT ACL Operations */
1589 .fget_nt_acl_fn = NULL,
1590 .get_nt_acl_fn = NULL,
1591 .fset_nt_acl_fn = NULL,
1592 .audit_file_fn = NULL,
1594 /* Posix ACL Operations */
1595 .chmod_acl_fn = NULL, /* passthrough to default */
1596 .fchmod_acl_fn = NULL, /* passthrough to default */
1597 .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1598 .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1599 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1600 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1601 .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1602 .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1603 .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1606 .getxattr_fn = vfs_gluster_getxattr,
1607 .fgetxattr_fn = vfs_gluster_fgetxattr,
1608 .listxattr_fn = vfs_gluster_listxattr,
1609 .flistxattr_fn = vfs_gluster_flistxattr,
1610 .removexattr_fn = vfs_gluster_removexattr,
1611 .fremovexattr_fn = vfs_gluster_fremovexattr,
1612 .setxattr_fn = vfs_gluster_setxattr,
1613 .fsetxattr_fn = vfs_gluster_fsetxattr,
1615 /* AIO Operations */
1616 .aio_force_fn = vfs_gluster_aio_force,
1618 /* Offline Operations */
1619 .is_offline_fn = vfs_gluster_is_offline,
1620 .set_offline_fn = vfs_gluster_set_offline,
1622 /* Durable handle Operations */
1623 .durable_cookie_fn = NULL,
1624 .durable_disconnect_fn = NULL,
1625 .durable_reconnect_fn = NULL,
1628 NTSTATUS vfs_glusterfs_init(void);
1629 NTSTATUS vfs_glusterfs_init(void)
1631 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1632 "glusterfs", &glusterfs_fns);