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));
213 ret = glfs_set_logging(fs, logfile, loglevel);
215 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
216 volume, logfile, loglevel));
222 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
223 volume, strerror(errno)));
227 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
229 DEBUG(0, ("%s: Failed to register volume (%s)\n",
230 volume, strerror(errno)));
234 talloc_free(tmp_ctx);
240 DEBUG(0, ("%s: Initialized volume from server %s\n",
241 volume, volfile_server));
247 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
253 glfs_clear_preopened(fs);
256 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
257 const char *path, bool small_query,
258 uint64_t *bsize_p, uint64_t *dfree_p,
261 struct statvfs statvfs = { 0, };
264 ret = glfs_statvfs(handle->data, path, &statvfs);
269 if (bsize_p != NULL) {
270 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
272 if (dfree_p != NULL) {
273 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
275 if (dsize_p != NULL) {
276 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
279 return (uint64_t)statvfs.f_bavail;
282 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
283 enum SMB_QUOTA_TYPE qtype, unid_t id,
291 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
292 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
298 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
300 struct vfs_statvfs_struct *vfs_statvfs)
302 struct statvfs statvfs = { 0, };
305 ret = glfs_statvfs(handle->data, path, &statvfs);
307 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
308 path, strerror(errno)));
312 ZERO_STRUCTP(vfs_statvfs);
314 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
315 vfs_statvfs->BlockSize = statvfs.f_bsize;
316 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
317 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
318 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
319 vfs_statvfs->TotalFileNodes = statvfs.f_files;
320 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
321 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
322 vfs_statvfs->FsCapabilities =
323 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
328 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
329 enum timestamp_set_resolution *p_ts_res)
331 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
333 #ifdef STAT_HAVE_NSEC
334 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
340 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
341 const char *path, const char *mask,
346 fd = glfs_opendir(handle->data, path);
348 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
349 path, strerror(errno)));
355 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
356 files_struct *fsp, const char *mask,
359 return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
362 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
364 return glfs_closedir((void *)dirp);
367 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
368 DIR *dirp, SMB_STRUCT_STAT *sbuf)
370 static char direntbuf[512];
373 struct dirent *dirent = 0;
376 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
379 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
382 if ((ret < 0) || (dirent == NULL)) {
387 smb_stat_ex_from_stat(sbuf, &stat);
393 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
395 return glfs_telldir((void *)dirp);
398 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
401 glfs_seekdir((void *)dirp, offset);
404 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
406 glfs_seekdir((void *)dirp, 0);
409 static void vfs_gluster_init_search_op(struct vfs_handle_struct *handle,
415 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle, const char *path,
418 return glfs_mkdir(handle->data, path, mode);
421 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle, const char *path)
423 return glfs_rmdir(handle->data, path);
426 static int vfs_gluster_open(struct vfs_handle_struct *handle,
427 struct smb_filename *smb_fname, files_struct *fsp,
428 int flags, mode_t mode)
433 if (flags & O_DIRECTORY) {
434 glfd = glfs_opendir(handle->data, smb_fname->base_name);
435 } else if (flags & O_CREAT) {
436 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
439 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
445 p_tmp = (glfs_fd_t **)VFS_ADD_FSP_EXTENSION(handle, fsp,
448 /* An arbitrary value for error reporting, so you know its us. */
452 static int vfs_gluster_close(struct vfs_handle_struct *handle,
456 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
457 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
458 return glfs_close(glfd);
461 static ssize_t vfs_gluster_read(struct vfs_handle_struct *handle,
462 files_struct *fsp, void *data, size_t n)
464 return glfs_read(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
467 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
468 files_struct *fsp, void *data, size_t n,
471 return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
474 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
475 *handle, TALLOC_CTX *mem_ctx,
476 struct tevent_context *ev,
477 files_struct *fsp, void *data,
478 size_t n, off_t offset)
484 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req, int *err)
490 static ssize_t vfs_gluster_write(struct vfs_handle_struct *handle,
491 files_struct *fsp, const void *data, size_t n)
493 return glfs_write(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
496 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
497 files_struct *fsp, const void *data,
498 size_t n, off_t offset)
500 return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
503 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
504 *handle, TALLOC_CTX *mem_ctx,
505 struct tevent_context *ev,
507 const void *data, size_t n,
514 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req, int *err)
520 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
521 files_struct *fsp, off_t offset, int whence)
523 return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
526 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
527 files_struct *fromfsp,
528 const DATA_BLOB *hdr,
529 off_t offset, size_t n)
535 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
536 int fromfd, files_struct *tofsp,
537 off_t offset, size_t n)
543 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
544 const struct smb_filename *smb_fname_src,
545 const struct smb_filename *smb_fname_dst)
547 return glfs_rename(handle->data, smb_fname_src->base_name,
548 smb_fname_dst->base_name);
551 static int vfs_gluster_fsync(struct vfs_handle_struct *handle,
554 return glfs_fsync(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp));
557 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
558 *handle, TALLOC_CTX *mem_ctx,
559 struct tevent_context *ev,
566 static int vfs_gluster_fsync_recv(struct tevent_req *req, int *err)
572 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
573 struct smb_filename *smb_fname)
578 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
580 smb_stat_ex_from_stat(&smb_fname->st, &st);
582 if (ret < 0 && errno != ENOENT) {
583 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
584 smb_fname->base_name, strerror(errno)));
589 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
590 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
595 ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
597 smb_stat_ex_from_stat(sbuf, &st);
600 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
601 fsp->fh->fd, strerror(errno)));
606 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
607 struct smb_filename *smb_fname)
612 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
614 smb_stat_ex_from_stat(&smb_fname->st, &st);
616 if (ret < 0 && errno != ENOENT) {
617 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
618 smb_fname->base_name, strerror(errno)));
623 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
625 const SMB_STRUCT_STAT *sbuf)
627 return sbuf->st_ex_blocks * 512;
630 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
631 const struct smb_filename *smb_fname)
633 return glfs_unlink(handle->data, smb_fname->base_name);
636 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
637 const char *path, mode_t mode)
639 return glfs_chmod(handle->data, path, mode);
642 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
643 files_struct *fsp, mode_t mode)
645 return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
648 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
649 const char *path, uid_t uid, gid_t gid)
651 return glfs_chown(handle->data, path, uid, gid);
654 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
655 files_struct *fsp, uid_t uid, gid_t gid)
657 return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
660 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
661 const char *path, uid_t uid, gid_t gid)
663 return glfs_lchown(handle->data, path, uid, gid);
666 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
668 return glfs_chdir(handle->data, path);
671 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
676 cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
681 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
688 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
689 const struct smb_filename *smb_fname,
690 struct smb_file_time *ft)
692 struct timespec times[2];
694 if (null_timespec(ft->atime)) {
695 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
696 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
698 times[0].tv_sec = ft->atime.tv_sec;
699 times[0].tv_nsec = ft->atime.tv_nsec;
702 if (null_timespec(ft->mtime)) {
703 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
704 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
706 times[1].tv_sec = ft->mtime.tv_sec;
707 times[1].tv_nsec = ft->mtime.tv_nsec;
710 if ((timespec_compare(×[0],
711 &smb_fname->st.st_ex_atime) == 0) &&
712 (timespec_compare(×[1],
713 &smb_fname->st.st_ex_mtime) == 0)) {
717 return glfs_utimens(handle->data, smb_fname->base_name, times);
720 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
721 files_struct *fsp, off_t offset)
723 return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
726 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
727 struct files_struct *fsp,
728 enum vfs_fallocate_mode mode,
729 off_t offset, off_t len)
735 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
738 return glfs_realpath(handle->data, path, 0);
741 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
742 files_struct *fsp, int op, off_t offset,
743 off_t count, int type)
745 struct flock flock = { 0, };
749 flock.l_whence = SEEK_SET;
750 flock.l_start = offset;
754 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
757 /* lock query, true if someone else has locked */
759 (flock.l_type != F_UNLCK) &&
760 (flock.l_pid != 0) && (flock.l_pid != getpid()))
773 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
774 files_struct *fsp, uint32 share_mode,
775 uint32_t access_mask)
781 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
782 files_struct *fsp, int leasetype)
788 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
789 files_struct *fsp, off_t *poffset,
790 off_t *pcount, int *ptype, pid_t *ppid)
792 struct flock flock = { 0, };
795 flock.l_type = *ptype;
796 flock.l_whence = SEEK_SET;
797 flock.l_start = *poffset;
798 flock.l_len = *pcount;
801 ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
807 *ptype = flock.l_type;
808 *poffset = flock.l_start;
809 *pcount = flock.l_len;
815 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
816 const char *oldpath, const char *newpath)
818 return glfs_symlink(handle->data, oldpath, newpath);
821 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
822 const char *path, char *buf, size_t bufsiz)
824 return glfs_readlink(handle->data, path, buf, bufsiz);
827 static int vfs_gluster_link(struct vfs_handle_struct *handle,
828 const char *oldpath, const char *newpath)
830 return glfs_link(handle->data, oldpath, newpath);
833 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
834 mode_t mode, SMB_DEV_T dev)
836 return glfs_mknod(handle->data, path, mode, dev);
839 static NTSTATUS vfs_gluster_notify_watch(struct vfs_handle_struct *handle,
840 struct sys_notify_context *ctx,
841 const char *path, uint32_t *filter,
842 uint32_t *subdir_filter,
843 void (*callback) (struct sys_notify_context *ctx,
845 struct notify_event *ev),
846 void *private_data, void *handle_p)
848 return NT_STATUS_NOT_IMPLEMENTED;
851 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
852 const char *path, unsigned int flags)
858 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
859 const char *path, const char *name,
860 TALLOC_CTX *mem_ctx, char **found_name)
863 char key_buf[NAME_MAX + 64];
864 char val_buf[NAME_MAX + 1];
866 if (strlen(name) >= NAME_MAX) {
867 errno = ENAMETOOLONG;
871 snprintf(key_buf, NAME_MAX + 64,
872 "user.glusterfs.get_real_filename:%s", name);
874 ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
876 if (errno == ENODATA) {
882 *found_name = talloc_strdup(mem_ctx, val_buf);
883 if (found_name[0] == NULL) {
890 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
891 const char *filename)
893 return handle->conn->connectpath;
898 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
899 const char *path, const char *name,
900 void *value, size_t size)
902 return glfs_getxattr(handle->data, path, name, value, size);
905 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
906 files_struct *fsp, const char *name,
907 void *value, size_t size)
909 return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
912 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
913 const char *path, char *list, size_t size)
915 return glfs_listxattr(handle->data, path, list, size);
918 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
919 files_struct *fsp, char *list,
922 return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
925 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
926 const char *path, const char *name)
928 return glfs_removexattr(handle->data, path, name);
931 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
932 files_struct *fsp, const char *name)
934 return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
937 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
938 const char *path, const char *name,
939 const void *value, size_t size, int flags)
941 return glfs_setxattr(handle->data, path, name, value, size, flags);
944 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
945 files_struct *fsp, const char *name,
946 const void *value, size_t size, int flags)
948 return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
954 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
960 /* Offline Operations */
962 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
963 const struct smb_filename *fname,
964 SMB_STRUCT_STAT *sbuf)
969 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
970 const struct smb_filename *fname)
979 Size = 4 (header) + N * 8 (entry)
981 Offset Size Field (Little Endian)
982 -------------------------------------
985 4-5 2-byte Entry-1 tag
986 6-7 2-byte Entry-1 perm
987 8-11 4-byte Entry-1 id
989 12-13 2-byte Entry-2 tag
990 14-15 2-byte Entry-2 perm
991 16-19 4-byte Entry-2 id
998 #define GLUSTER_ACL_VERSION 2
1001 #define GLUSTER_ACL_READ 0x04
1002 #define GLUSTER_ACL_WRITE 0x02
1003 #define GLUSTER_ACL_EXECUTE 0x01
1006 #define GLUSTER_ACL_UNDEFINED_TAG 0x00
1007 #define GLUSTER_ACL_USER_OBJ 0x01
1008 #define GLUSTER_ACL_USER 0x02
1009 #define GLUSTER_ACL_GROUP_OBJ 0x04
1010 #define GLUSTER_ACL_GROUP 0x08
1011 #define GLUSTER_ACL_MASK 0x10
1012 #define GLUSTER_ACL_OTHER 0x20
1014 #define GLUSTER_ACL_UNDEFINED_ID (-1)
1016 #define GLUSTER_ACL_HEADER_SIZE 4
1017 #define GLUSTER_ACL_ENTRY_SIZE 8
1019 #define GLUSTER_ACL_SIZE(n) (GLUSTER_ACL_HEADER_SIZE + (n * GLUSTER_ACL_ENTRY_SIZE))
1021 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1022 TALLOC_CTX *mem_ctx)
1026 struct smb_acl_entry *smb_ace;
1027 struct smb_acl_t *result;
1036 if (size < GLUSTER_ACL_HEADER_SIZE) {
1037 /* ACL should be at least as big as the header (4 bytes) */
1042 size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1044 if (size % GLUSTER_ACL_ENTRY_SIZE) {
1045 /* Size of entries must strictly be a multiple of
1046 size of an ACE (8 bytes)
1052 count = size / GLUSTER_ACL_ENTRY_SIZE;
1054 /* Version is the first 4 bytes of the ACL */
1055 if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1056 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1060 offset = GLUSTER_ACL_HEADER_SIZE;
1062 result = sys_acl_init(mem_ctx);
1068 result->acl = talloc_array(result, struct smb_acl_entry, count);
1071 talloc_free(result);
1075 result->count = count;
1077 smb_ace = result->acl;
1079 for (i = 0; i < count; i++) {
1080 /* TAG is the first 2 bytes of an entry */
1081 tag = SVAL(buf, offset);
1084 /* PERM is the next 2 bytes of an entry */
1085 perm = SVAL(buf, offset);
1088 /* ID is the last 4 bytes of an entry */
1089 id = IVAL(buf, offset);
1093 case GLUSTER_ACL_USER:
1094 smb_ace->a_type = SMB_ACL_USER;
1096 case GLUSTER_ACL_USER_OBJ:
1097 smb_ace->a_type = SMB_ACL_USER_OBJ;
1099 case GLUSTER_ACL_GROUP:
1100 smb_ace->a_type = SMB_ACL_GROUP;
1102 case GLUSTER_ACL_GROUP_OBJ:
1103 smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1105 case GLUSTER_ACL_OTHER:
1106 smb_ace->a_type = SMB_ACL_OTHER;
1108 case GLUSTER_ACL_MASK:
1109 smb_ace->a_type = SMB_ACL_MASK;
1112 DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1117 switch(smb_ace->a_type) {
1119 smb_ace->info.user.uid = id;
1122 smb_ace->info.group.gid = id;
1128 smb_ace->a_perm = 0;
1130 ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1132 ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1134 ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1143 static int gluster_ace_cmp(const void *left, const void *right)
1146 uint16_t tag_left, tag_right;
1147 uint32_t id_left, id_right;
1152 - Smaller TAG values must be earlier.
1154 - Within same TAG, smaller identifiers must be earlier, E.g:
1155 UID 0 entry must be earlier than UID 200
1156 GID 17 entry must be earlier than GID 19
1159 /* TAG is the first element in the entry */
1160 tag_left = SVAL(left, 0);
1161 tag_right = SVAL(right, 0);
1163 ret = (tag_left - tag_right);
1165 /* ID is the third element in the entry, after two short
1166 integers (tag and perm), i.e at offset 4.
1168 id_left = IVAL(left, 4);
1169 id_right = IVAL(right, 4);
1170 ret = id_left - id_right;
1177 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1180 struct smb_acl_entry *smb_ace;
1188 count = theacl->count;
1190 size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1200 smb_ace = theacl->acl;
1202 /* Version is the first 4 bytes of the ACL */
1203 SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1204 offset = GLUSTER_ACL_HEADER_SIZE;
1206 for (i = 0; i < count; i++) {
1208 switch(smb_ace->a_type) {
1210 tag = GLUSTER_ACL_USER;
1212 case SMB_ACL_USER_OBJ:
1213 tag = GLUSTER_ACL_USER_OBJ;
1216 tag = GLUSTER_ACL_GROUP;
1218 case SMB_ACL_GROUP_OBJ:
1219 tag = GLUSTER_ACL_GROUP_OBJ;
1222 tag = GLUSTER_ACL_OTHER;
1225 tag = GLUSTER_ACL_MASK;
1228 DEBUG(0, ("Unknown tag value %d\n",
1236 switch(smb_ace->a_type) {
1238 id = smb_ace->info.user.uid;
1241 id = smb_ace->info.group.gid;
1244 id = GLUSTER_ACL_UNDEFINED_ID;
1248 /* Calculate perm */
1252 ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1254 ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1256 ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1259 /* TAG is the first 2 bytes of an entry */
1260 SSVAL(buf, offset, tag);
1263 /* PERM is the next 2 bytes of an entry */
1264 SSVAL(buf, offset, perm);
1267 /* ID is the last 4 bytes of an entry */
1268 SIVAL(buf, offset, id);
1274 /* Skip the header, sort @count number of 8-byte entries */
1275 qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1282 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1284 SMB_ACL_TYPE_T type,
1285 TALLOC_CTX *mem_ctx)
1287 struct smb_acl_t *result;
1290 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1293 case SMB_ACL_TYPE_ACCESS:
1294 key = "system.posix_acl_access";
1296 case SMB_ACL_TYPE_DEFAULT:
1297 key = "system.posix_acl_default";
1309 ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1310 if (ret == -1 && errno == ERANGE) {
1311 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1317 ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1324 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1329 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1330 struct files_struct *fsp,
1331 TALLOC_CTX *mem_ctx)
1333 struct smb_acl_t *result;
1334 ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1338 glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1345 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1346 if (ret == -1 && errno == ERANGE) {
1347 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1353 ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1361 result = gluster_to_smb_acl(buf, ret, mem_ctx);
1366 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1368 SMB_ACL_TYPE_T acltype,
1377 case SMB_ACL_TYPE_ACCESS:
1378 key = "system.posix_acl_access";
1380 case SMB_ACL_TYPE_DEFAULT:
1381 key = "system.posix_acl_default";
1388 size = smb_to_gluster_acl(theacl, 0, 0);
1391 size = smb_to_gluster_acl(theacl, buf, size);
1396 ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1401 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1402 struct files_struct *fsp,
1409 size = smb_to_gluster_acl(theacl, 0, 0);
1412 size = smb_to_gluster_acl(theacl, buf, size);
1417 ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1418 "system.posix_acl_access", buf, size, 0);
1422 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1425 return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1428 static struct vfs_fn_pointers glusterfs_fns = {
1430 /* Disk Operations */
1432 .connect_fn = vfs_gluster_connect,
1433 .disconnect_fn = vfs_gluster_disconnect,
1434 .disk_free_fn = vfs_gluster_disk_free,
1435 .get_quota_fn = vfs_gluster_get_quota,
1436 .set_quota_fn = vfs_gluster_set_quota,
1437 .statvfs_fn = vfs_gluster_statvfs,
1438 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1440 .get_dfs_referrals_fn = NULL,
1442 /* Directory Operations */
1444 .opendir_fn = vfs_gluster_opendir,
1445 .fdopendir_fn = vfs_gluster_fdopendir,
1446 .readdir_fn = vfs_gluster_readdir,
1447 .seekdir_fn = vfs_gluster_seekdir,
1448 .telldir_fn = vfs_gluster_telldir,
1449 .rewind_dir_fn = vfs_gluster_rewinddir,
1450 .mkdir_fn = vfs_gluster_mkdir,
1451 .rmdir_fn = vfs_gluster_rmdir,
1452 .closedir_fn = vfs_gluster_closedir,
1453 .init_search_op_fn = vfs_gluster_init_search_op,
1455 /* File Operations */
1457 .open_fn = vfs_gluster_open,
1458 .create_file_fn = NULL,
1459 .close_fn = vfs_gluster_close,
1460 .read_fn = vfs_gluster_read,
1461 .pread_fn = vfs_gluster_pread,
1462 .pread_send_fn = vfs_gluster_pread_send,
1463 .pread_recv_fn = vfs_gluster_pread_recv,
1464 .write_fn = vfs_gluster_write,
1465 .pwrite_fn = vfs_gluster_pwrite,
1466 .pwrite_send_fn = vfs_gluster_pwrite_send,
1467 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
1468 .lseek_fn = vfs_gluster_lseek,
1469 .sendfile_fn = vfs_gluster_sendfile,
1470 .recvfile_fn = vfs_gluster_recvfile,
1471 .rename_fn = vfs_gluster_rename,
1472 .fsync_fn = vfs_gluster_fsync,
1473 .fsync_send_fn = vfs_gluster_fsync_send,
1474 .fsync_recv_fn = vfs_gluster_fsync_recv,
1476 .stat_fn = vfs_gluster_stat,
1477 .fstat_fn = vfs_gluster_fstat,
1478 .lstat_fn = vfs_gluster_lstat,
1479 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1480 .unlink_fn = vfs_gluster_unlink,
1482 .chmod_fn = vfs_gluster_chmod,
1483 .fchmod_fn = vfs_gluster_fchmod,
1484 .chown_fn = vfs_gluster_chown,
1485 .fchown_fn = vfs_gluster_fchown,
1486 .lchown_fn = vfs_gluster_lchown,
1487 .chdir_fn = vfs_gluster_chdir,
1488 .getwd_fn = vfs_gluster_getwd,
1489 .ntimes_fn = vfs_gluster_ntimes,
1490 .ftruncate_fn = vfs_gluster_ftruncate,
1491 .fallocate_fn = vfs_gluster_fallocate,
1492 .lock_fn = vfs_gluster_lock,
1493 .kernel_flock_fn = vfs_gluster_kernel_flock,
1494 .linux_setlease_fn = vfs_gluster_linux_setlease,
1495 .getlock_fn = vfs_gluster_getlock,
1496 .symlink_fn = vfs_gluster_symlink,
1497 .readlink_fn = vfs_gluster_readlink,
1498 .link_fn = vfs_gluster_link,
1499 .mknod_fn = vfs_gluster_mknod,
1500 .realpath_fn = vfs_gluster_realpath,
1501 .notify_watch_fn = vfs_gluster_notify_watch,
1502 .chflags_fn = vfs_gluster_chflags,
1503 .file_id_create_fn = NULL,
1504 .copy_chunk_send_fn = NULL,
1505 .copy_chunk_recv_fn = NULL,
1506 .streaminfo_fn = NULL,
1507 .get_real_filename_fn = vfs_gluster_get_real_filename,
1508 .connectpath_fn = vfs_gluster_connectpath,
1510 .brl_lock_windows_fn = NULL,
1511 .brl_unlock_windows_fn = NULL,
1512 .brl_cancel_windows_fn = NULL,
1513 .strict_lock_fn = NULL,
1514 .strict_unlock_fn = NULL,
1515 .translate_name_fn = NULL,
1518 /* NT ACL Operations */
1519 .fget_nt_acl_fn = NULL,
1520 .get_nt_acl_fn = NULL,
1521 .fset_nt_acl_fn = NULL,
1522 .audit_file_fn = NULL,
1524 /* Posix ACL Operations */
1525 .chmod_acl_fn = NULL, /* passthrough to default */
1526 .fchmod_acl_fn = NULL, /* passthrough to default */
1527 .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1528 .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1529 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1530 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1531 .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1532 .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1533 .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1536 .getxattr_fn = vfs_gluster_getxattr,
1537 .fgetxattr_fn = vfs_gluster_fgetxattr,
1538 .listxattr_fn = vfs_gluster_listxattr,
1539 .flistxattr_fn = vfs_gluster_flistxattr,
1540 .removexattr_fn = vfs_gluster_removexattr,
1541 .fremovexattr_fn = vfs_gluster_fremovexattr,
1542 .setxattr_fn = vfs_gluster_setxattr,
1543 .fsetxattr_fn = vfs_gluster_fsetxattr,
1545 /* AIO Operations */
1546 .aio_force_fn = vfs_gluster_aio_force,
1548 /* Offline Operations */
1549 .is_offline_fn = vfs_gluster_is_offline,
1550 .set_offline_fn = vfs_gluster_set_offline,
1552 /* Durable handle Operations */
1553 .durable_cookie_fn = NULL,
1554 .durable_disconnect_fn = NULL,
1555 .durable_reconnect_fn = NULL,
1558 NTSTATUS vfs_glusterfs_init(void);
1559 NTSTATUS vfs_glusterfs_init(void)
1561 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1562 "glusterfs", &glusterfs_fns);