utils: Fix up 14a533680245
[samba4-gss.git] / source3 / modules / vfs_glusterfs.c
blob2ac978eed8cc874f05874351347cbc31a5fdfd6a
1 /*
2 Unix SMB/CIFS implementation.
4 Wrap GlusterFS GFAPI calls in vfs functions.
6 Copyright (c) 2013 Anand Avati <avati@redhat.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /**
23 * @file vfs_glusterfs.c
24 * @author Anand Avati <avati@redhat.com>
25 * @date May 2013
26 * @brief Samba VFS module for glusterfs
28 * @todo
29 * - sendfile/recvfile support
31 * A Samba VFS module for GlusterFS, based on Gluster's libgfapi.
32 * This is a "bottom" vfs module (not something to be stacked on top of
33 * another module), and translates (most) calls to the closest actions
34 * available in libgfapi.
38 #include "includes.h"
39 #include "smbd/smbd.h"
40 #include <stdio.h>
41 #include <glusterfs/api/glfs.h>
42 #include "lib/util/dlinklist.h"
43 #include "lib/util/tevent_unix.h"
44 #include "lib/util/util_file.h"
45 #include "smbd/globals.h"
46 #include "lib/util/sys_rw.h"
47 #include "smbprofile.h"
48 #include "modules/posixacl_xattr.h"
49 #include "lib/pthreadpool/pthreadpool_tevent.h"
51 #define DEFAULT_VOLFILE_SERVER "localhost"
52 #define GLUSTER_NAME_MAX 255
54 /**
55 * Helper to convert struct stat to struct stat_ex.
57 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
59 ZERO_STRUCTP(dst);
61 dst->st_ex_dev = src->st_dev;
62 dst->st_ex_ino = src->st_ino;
63 dst->st_ex_mode = src->st_mode;
64 dst->st_ex_nlink = src->st_nlink;
65 dst->st_ex_uid = src->st_uid;
66 dst->st_ex_gid = src->st_gid;
67 dst->st_ex_rdev = src->st_rdev;
68 dst->st_ex_size = src->st_size;
69 dst->st_ex_atime.tv_sec = src->st_atime;
70 dst->st_ex_mtime.tv_sec = src->st_mtime;
71 dst->st_ex_ctime.tv_sec = src->st_ctime;
72 dst->st_ex_btime.tv_sec = src->st_mtime;
73 dst->st_ex_blksize = src->st_blksize;
74 dst->st_ex_blocks = src->st_blocks;
75 #ifdef STAT_HAVE_NSEC
76 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
77 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
78 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
79 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
80 #endif
83 /* pre-opened glfs_t */
85 static struct glfs_preopened {
86 char *volume;
87 char *connectpath;
88 glfs_t *fs;
89 int ref;
90 struct glfs_preopened *next, *prev;
91 } *glfs_preopened;
94 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
96 struct glfs_preopened *entry = NULL;
98 entry = talloc_zero(NULL, struct glfs_preopened);
99 if (!entry) {
100 errno = ENOMEM;
101 return -1;
104 entry->volume = talloc_strdup(entry, volume);
105 if (!entry->volume) {
106 talloc_free(entry);
107 errno = ENOMEM;
108 return -1;
111 entry->connectpath = talloc_strdup(entry, connectpath);
112 if (entry->connectpath == NULL) {
113 talloc_free(entry);
114 errno = ENOMEM;
115 return -1;
118 entry->fs = fs;
119 entry->ref = 1;
121 DLIST_ADD(glfs_preopened, entry);
123 return 0;
126 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
128 struct glfs_preopened *entry = NULL;
130 for (entry = glfs_preopened; entry; entry = entry->next) {
131 if (strcmp(entry->volume, volume) == 0 &&
132 strcmp(entry->connectpath, connectpath) == 0)
134 entry->ref++;
135 return entry->fs;
139 return NULL;
142 static void glfs_clear_preopened(glfs_t *fs)
144 struct glfs_preopened *entry = NULL;
146 for (entry = glfs_preopened; entry; entry = entry->next) {
147 if (entry->fs == fs) {
148 if (--entry->ref)
149 return;
151 DLIST_REMOVE(glfs_preopened, entry);
153 glfs_fini(entry->fs);
154 talloc_free(entry);
155 break;
160 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
161 const char *volfile_servers)
163 char *server = NULL;
164 size_t server_count = 0;
165 size_t server_success = 0;
166 int ret = -1;
167 TALLOC_CTX *frame = talloc_stackframe();
169 DBG_INFO("servers list %s\n", volfile_servers);
171 while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
172 char *transport = NULL;
173 char *host = NULL;
174 int port = 0;
176 server_count++;
177 DBG_INFO("server %zu %s\n", server_count, server);
179 /* Determine the transport type */
180 if (strncmp(server, "unix+", 5) == 0) {
181 port = 0;
182 transport = talloc_strdup(frame, "unix");
183 if (!transport) {
184 errno = ENOMEM;
185 goto out;
187 host = talloc_strdup(frame, server + 5);
188 if (!host) {
189 errno = ENOMEM;
190 goto out;
192 } else {
193 char *p = NULL;
194 char *port_index = NULL;
196 if (strncmp(server, "tcp+", 4) == 0) {
197 server += 4;
200 /* IPv6 is enclosed in []
201 * ':' before ']' is part of IPv6
202 * ':' after ']' indicates port
204 p = server;
205 if (server[0] == '[') {
206 server++;
207 p = index(server, ']');
208 if (p == NULL) {
209 /* Malformed IPv6 */
210 continue;
212 p[0] = '\0';
213 p++;
216 port_index = index(p, ':');
218 if (port_index == NULL) {
219 port = 0;
220 } else {
221 port = atoi(port_index + 1);
222 port_index[0] = '\0';
224 transport = talloc_strdup(frame, "tcp");
225 if (!transport) {
226 errno = ENOMEM;
227 goto out;
229 host = talloc_strdup(frame, server);
230 if (!host) {
231 errno = ENOMEM;
232 goto out;
236 DBG_INFO("Calling set volfile server with params "
237 "transport=%s, host=%s, port=%d\n", transport,
238 host, port);
240 ret = glfs_set_volfile_server(fs, transport, host, port);
241 if (ret < 0) {
242 DBG_WARNING("Failed to set volfile_server "
243 "transport=%s, host=%s, port=%d (%s)\n",
244 transport, host, port, strerror(errno));
245 } else {
246 server_success++;
250 out:
251 if (server_count == 0) {
252 ret = -1;
253 } else if (server_success < server_count) {
254 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
255 server_count - server_success, server_count);
256 ret = 0;
259 TALLOC_FREE(frame);
260 return ret;
263 /* Disk Operations */
265 static int check_for_write_behind_translator(TALLOC_CTX *mem_ctx,
266 glfs_t *fs,
267 const char *volume)
269 char *buf = NULL;
270 char **lines = NULL;
271 int numlines = 0;
272 int i;
273 char *option;
274 bool write_behind_present = false;
275 size_t newlen;
276 int ret;
278 ret = glfs_get_volfile(fs, NULL, 0);
279 if (ret == 0) {
280 DBG_ERR("%s: Failed to get volfile for "
281 "volume (%s): No volfile\n",
282 volume,
283 strerror(errno));
284 return -1;
286 if (ret > 0) {
287 DBG_ERR("%s: Invalid return %d for glfs_get_volfile for "
288 "volume (%s): No volfile\n",
289 volume,
290 ret,
291 strerror(errno));
292 return -1;
295 newlen = 0 - ret;
297 buf = talloc_zero_array(mem_ctx, char, newlen);
298 if (buf == NULL) {
299 return -1;
302 ret = glfs_get_volfile(fs, buf, newlen);
303 if (ret != newlen) {
304 TALLOC_FREE(buf);
305 DBG_ERR("%s: Failed to get volfile for volume (%s)\n",
306 volume, strerror(errno));
307 return -1;
310 option = talloc_asprintf(mem_ctx, "volume %s-write-behind", volume);
311 if (option == NULL) {
312 TALLOC_FREE(buf);
313 return -1;
317 * file_lines_parse() plays horrible tricks with
318 * the passed-in talloc pointers and the hierarchy
319 * which makes freeing hard to get right.
321 * As we know mem_ctx is freed by the caller, after
322 * this point don't free on exit and let the caller
323 * handle it. This violates good Samba coding practice
324 * but we know we're not leaking here.
327 lines = file_lines_parse(buf,
328 newlen,
329 &numlines,
330 mem_ctx);
331 if (lines == NULL || numlines <= 0) {
332 return -1;
334 /* On success, buf is now a talloc child of lines !! */
336 for (i=0; i < numlines; i++) {
337 if (strequal(lines[i], option)) {
338 write_behind_present = true;
339 break;
343 if (write_behind_present) {
344 DBG_ERR("Write behind translator is enabled for "
345 "volume (%s), refusing to connect! "
346 "Please turn off the write behind translator by calling "
347 "'gluster volume set %s performance.write-behind off' "
348 "on the commandline. "
349 "Check the vfs_glusterfs(8) manpage for "
350 "further details.\n",
351 volume, volume);
352 return -1;
355 return 0;
358 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
359 const char *service,
360 const char *user)
362 const struct loadparm_substitution *lp_sub =
363 loadparm_s3_global_substitution();
364 const char *volfile_servers;
365 const char *volume;
366 char *logfile;
367 int loglevel;
368 glfs_t *fs = NULL;
369 TALLOC_CTX *tmp_ctx;
370 int ret = 0;
371 bool write_behind_pass_through_set = false;
373 tmp_ctx = talloc_new(NULL);
374 if (tmp_ctx == NULL) {
375 ret = -1;
376 goto done;
378 logfile = lp_parm_substituted_string(tmp_ctx,
379 lp_sub,
380 SNUM(handle->conn),
381 "glusterfs",
382 "logfile",
383 NULL);
385 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
387 volfile_servers = lp_parm_substituted_string(tmp_ctx,
388 lp_sub,
389 SNUM(handle->conn),
390 "glusterfs",
391 "volfile_server",
392 NULL);
393 if (volfile_servers == NULL) {
394 volfile_servers = DEFAULT_VOLFILE_SERVER;
397 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
398 NULL);
399 if (volume == NULL) {
400 volume = service;
403 fs = glfs_find_preopened(volume, handle->conn->connectpath);
404 if (fs) {
405 goto done;
408 fs = glfs_new(volume);
409 if (fs == NULL) {
410 ret = -1;
411 goto done;
414 ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
415 if (ret < 0) {
416 DBG_ERR("Failed to set volfile_servers from list %s\n",
417 volfile_servers);
418 goto done;
421 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
422 "true");
423 if (ret < 0) {
424 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
425 goto done;
428 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-selinux",
429 "true");
430 if (ret < 0) {
431 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
432 goto done;
435 ret = glfs_set_xlator_option(fs, "*-snapview-client",
436 "snapdir-entry-path",
437 handle->conn->connectpath);
438 if (ret < 0) {
439 DEBUG(0, ("%s: Failed to set xlator option:"
440 " snapdir-entry-path\n", volume));
441 goto done;
444 #ifdef HAVE_GFAPI_VER_7_9
445 ret = glfs_set_xlator_option(fs, "*-write-behind", "pass-through",
446 "true");
447 if (ret < 0) {
448 DBG_ERR("%s: Failed to set xlator option: pass-through\n",
449 volume);
450 goto done;
452 write_behind_pass_through_set = true;
453 #endif
455 ret = glfs_set_logging(fs, logfile, loglevel);
456 if (ret < 0) {
457 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
458 volume, logfile, loglevel));
459 goto done;
462 ret = glfs_init(fs);
463 if (ret < 0) {
464 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
465 volume, strerror(errno)));
466 goto done;
469 if (!write_behind_pass_through_set) {
470 ret = check_for_write_behind_translator(tmp_ctx, fs, volume);
471 if (ret < 0) {
472 goto done;
476 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
477 if (ret < 0) {
478 DEBUG(0, ("%s: Failed to register volume (%s)\n",
479 volume, strerror(errno)));
480 goto done;
484 * The shadow_copy2 module will fail to export subdirectories
485 * of a gluster volume unless we specify the mount point,
486 * because the detection fails if the file system is not
487 * locally mounted:
488 * https://bugzilla.samba.org/show_bug.cgi?id=13091
490 lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
493 * Unless we have an async implementation of getxattrat turn this off.
495 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
497 done:
498 if (ret < 0) {
499 if (fs)
500 glfs_fini(fs);
501 } else {
502 DBG_ERR("%s: Initialized volume from servers %s\n",
503 volume, volfile_servers);
504 handle->data = fs;
506 talloc_free(tmp_ctx);
507 return ret;
510 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
512 glfs_t *fs = NULL;
514 fs = handle->data;
516 glfs_clear_preopened(fs);
519 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
520 const struct smb_filename *smb_fname,
521 uint64_t *bsize_p,
522 uint64_t *dfree_p,
523 uint64_t *dsize_p)
525 struct statvfs statvfs = { 0, };
526 int ret;
528 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
529 if (ret < 0) {
530 return -1;
533 if (bsize_p != NULL) {
534 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
536 if (dfree_p != NULL) {
537 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
539 if (dsize_p != NULL) {
540 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
543 return (uint64_t)statvfs.f_bavail;
546 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
547 const struct smb_filename *smb_fname,
548 enum SMB_QUOTA_TYPE qtype,
549 unid_t id,
550 SMB_DISK_QUOTA *qt)
552 errno = ENOSYS;
553 return -1;
556 static int
557 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
558 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
560 errno = ENOSYS;
561 return -1;
564 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
565 const struct smb_filename *smb_fname,
566 struct vfs_statvfs_struct *vfs_statvfs)
568 struct statvfs statvfs = { 0, };
569 int ret;
571 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
572 if (ret < 0) {
573 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
574 smb_fname->base_name, strerror(errno)));
575 return -1;
578 ZERO_STRUCTP(vfs_statvfs);
580 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
581 vfs_statvfs->BlockSize = statvfs.f_bsize;
582 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
583 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
584 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
585 vfs_statvfs->TotalFileNodes = statvfs.f_files;
586 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
587 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
588 vfs_statvfs->FsCapabilities =
589 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
591 return ret;
594 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
595 enum timestamp_set_resolution *p_ts_res)
597 uint32_t caps;
599 caps = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
601 #ifdef HAVE_GFAPI_VER_6
602 caps |= FILE_SUPPORTS_SPARSE_FILES;
603 #endif
605 #ifdef STAT_HAVE_NSEC
606 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
607 #endif
609 return caps;
612 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
613 const files_struct *fsp)
615 glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
616 if (glfd == NULL) {
617 DBG_INFO("Failed to fetch fsp extension\n");
618 return NULL;
620 if (*glfd == NULL) {
621 DBG_INFO("Empty glfs_fd_t pointer\n");
622 return NULL;
625 return *glfd;
628 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
629 files_struct *fsp, const char *mask,
630 uint32_t attributes)
632 glfs_fd_t *glfd = NULL;
634 glfd = glfs_opendir(handle->data, fsp->fsp_name->base_name);
635 if (glfd == NULL) {
636 return NULL;
639 return (DIR *)glfd;
642 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
644 int ret;
646 START_PROFILE(syscall_closedir);
647 ret = glfs_closedir((void *)dirp);
648 END_PROFILE(syscall_closedir);
650 return ret;
653 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
654 struct files_struct *dirfsp,
655 DIR *dirp)
657 static char direntbuf[512];
658 int ret;
659 struct dirent *dirent = 0;
661 START_PROFILE(syscall_readdir);
663 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
665 if ((ret < 0) || (dirent == NULL)) {
666 END_PROFILE(syscall_readdir);
667 return NULL;
670 END_PROFILE(syscall_readdir);
671 return dirent;
674 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
676 START_PROFILE(syscall_rewinddir);
677 glfs_seekdir((void *)dirp, 0);
678 END_PROFILE(syscall_rewinddir);
681 static int vfs_gluster_mkdirat(struct vfs_handle_struct *handle,
682 struct files_struct *dirfsp,
683 const struct smb_filename *smb_fname,
684 mode_t mode)
686 int ret;
688 #ifdef HAVE_GFAPI_VER_7_11
689 glfs_fd_t *pglfd = NULL;
691 START_PROFILE(syscall_mkdirat);
693 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
694 if (pglfd == NULL) {
695 END_PROFILE(syscall_mkdirat);
696 DBG_ERR("Failed to fetch gluster fd\n");
697 return -1;
700 ret = glfs_mkdirat(pglfd, smb_fname->base_name, mode);
701 #else
702 struct smb_filename *full_fname = NULL;
704 START_PROFILE(syscall_mkdirat);
706 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
707 dirfsp,
708 smb_fname);
709 if (full_fname == NULL) {
710 END_PROFILE(syscall_mkdirat);
711 return -1;
714 ret = glfs_mkdir(handle->data, full_fname->base_name, mode);
716 TALLOC_FREE(full_fname);
717 #endif
719 END_PROFILE(syscall_mkdirat);
721 return ret;
724 static int vfs_gluster_openat(struct vfs_handle_struct *handle,
725 const struct files_struct *dirfsp,
726 const struct smb_filename *smb_fname,
727 files_struct *fsp,
728 const struct vfs_open_how *how)
730 int flags = how->flags;
731 struct smb_filename *full_fname = NULL;
732 bool have_opath = false;
733 bool became_root = false;
734 glfs_fd_t *glfd = NULL;
735 glfs_fd_t *pglfd = NULL;
736 glfs_fd_t **p_tmp;
738 START_PROFILE(syscall_openat);
740 if (how->resolve != 0) {
741 END_PROFILE(syscall_openat);
742 errno = ENOSYS;
743 return -1;
746 p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
747 if (p_tmp == NULL) {
748 END_PROFILE(syscall_openat);
749 errno = ENOMEM;
750 return -1;
753 #ifdef O_PATH
754 have_opath = true;
755 if (fsp->fsp_flags.is_pathref) {
756 flags |= O_PATH;
758 #endif
760 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
761 dirfsp,
762 smb_fname);
763 if (full_fname == NULL) {
764 END_PROFILE(syscall_openat);
765 return -1;
768 if (fsp->fsp_flags.is_pathref && !have_opath) {
769 become_root();
770 became_root = true;
773 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
774 #ifdef HAVE_GFAPI_VER_7_11
776 * Fetch Gluster fd for parent directory using dirfsp
777 * before calling glfs_openat();
779 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
780 if (pglfd == NULL) {
781 END_PROFILE(syscall_openat);
782 DBG_ERR("Failed to fetch gluster fd\n");
783 return -1;
786 glfd = glfs_openat(pglfd,
787 smb_fname->base_name,
788 flags,
789 how->mode);
790 #else
792 * Replace smb_fname with full_path constructed above.
794 smb_fname = full_fname;
795 #endif
798 if (pglfd == NULL) {
800 * smb_fname can either be a full_path or the same one
801 * as received from the caller. In the latter case we
802 * are operating at current working directory.
804 if (flags & O_CREAT) {
805 glfd = glfs_creat(handle->data,
806 smb_fname->base_name,
807 flags,
808 how->mode);
809 } else {
810 glfd = glfs_open(handle->data,
811 smb_fname->base_name,
812 flags);
816 if (became_root) {
817 unbecome_root();
820 TALLOC_FREE(full_fname);
822 fsp->fsp_flags.have_proc_fds = false;
824 if (glfd == NULL) {
825 END_PROFILE(syscall_openat);
826 /* no extension destroy_fn, so no need to save errno */
827 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
828 return -1;
831 *p_tmp = glfd;
833 END_PROFILE(syscall_openat);
834 /* An arbitrary value for error reporting, so you know its us. */
835 return 13371337;
838 static int vfs_gluster_close(struct vfs_handle_struct *handle,
839 files_struct *fsp)
841 int ret;
842 glfs_fd_t *glfd = NULL;
844 START_PROFILE(syscall_close);
846 glfd = vfs_gluster_fetch_glfd(handle, fsp);
847 if (glfd == NULL) {
848 END_PROFILE(syscall_close);
849 DBG_ERR("Failed to fetch gluster fd\n");
850 return -1;
853 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
855 ret = glfs_close(glfd);
856 END_PROFILE(syscall_close);
858 return ret;
861 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
862 files_struct *fsp, void *data, size_t n,
863 off_t offset)
865 ssize_t ret;
866 glfs_fd_t *glfd = NULL;
868 START_PROFILE_BYTES(syscall_pread, n);
870 glfd = vfs_gluster_fetch_glfd(handle, fsp);
871 if (glfd == NULL) {
872 END_PROFILE_BYTES(syscall_pread);
873 DBG_ERR("Failed to fetch gluster fd\n");
874 return -1;
877 #ifdef HAVE_GFAPI_VER_7_6
878 ret = glfs_pread(glfd, data, n, offset, 0, NULL);
879 #else
880 ret = glfs_pread(glfd, data, n, offset, 0);
881 #endif
882 END_PROFILE_BYTES(syscall_pread);
884 return ret;
887 struct vfs_gluster_pread_state {
888 ssize_t ret;
889 glfs_fd_t *fd;
890 void *buf;
891 size_t count;
892 off_t offset;
894 struct vfs_aio_state vfs_aio_state;
895 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
898 static void vfs_gluster_pread_do(void *private_data);
899 static void vfs_gluster_pread_done(struct tevent_req *subreq);
900 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state);
902 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
903 *handle, TALLOC_CTX *mem_ctx,
904 struct tevent_context *ev,
905 files_struct *fsp,
906 void *data, size_t n,
907 off_t offset)
909 struct vfs_gluster_pread_state *state;
910 struct tevent_req *req, *subreq;
912 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
913 if (glfd == NULL) {
914 DBG_ERR("Failed to fetch gluster fd\n");
915 return NULL;
918 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pread_state);
919 if (req == NULL) {
920 return NULL;
923 state->ret = -1;
924 state->fd = glfd;
925 state->buf = data;
926 state->count = n;
927 state->offset = offset;
929 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
930 state->profile_bytes, n);
931 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
933 subreq = pthreadpool_tevent_job_send(
934 state, ev, handle->conn->sconn->pool,
935 vfs_gluster_pread_do, state);
936 if (tevent_req_nomem(subreq, req)) {
937 return tevent_req_post(req, ev);
939 tevent_req_set_callback(subreq, vfs_gluster_pread_done, req);
941 talloc_set_destructor(state, vfs_gluster_pread_state_destructor);
943 return req;
946 static void vfs_gluster_pread_do(void *private_data)
948 struct vfs_gluster_pread_state *state = talloc_get_type_abort(
949 private_data, struct vfs_gluster_pread_state);
950 struct timespec start_time;
951 struct timespec end_time;
953 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
955 PROFILE_TIMESTAMP(&start_time);
957 do {
958 #ifdef HAVE_GFAPI_VER_7_6
959 state->ret = glfs_pread(state->fd, state->buf, state->count,
960 state->offset, 0, NULL);
961 #else
962 state->ret = glfs_pread(state->fd, state->buf, state->count,
963 state->offset, 0);
964 #endif
965 } while ((state->ret == -1) && (errno == EINTR));
967 if (state->ret == -1) {
968 state->vfs_aio_state.error = errno;
971 PROFILE_TIMESTAMP(&end_time);
973 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
975 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
978 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state)
980 return -1;
983 static void vfs_gluster_pread_done(struct tevent_req *subreq)
985 struct tevent_req *req = tevent_req_callback_data(
986 subreq, struct tevent_req);
987 struct vfs_gluster_pread_state *state = tevent_req_data(
988 req, struct vfs_gluster_pread_state);
989 int ret;
991 ret = pthreadpool_tevent_job_recv(subreq);
992 TALLOC_FREE(subreq);
993 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
994 talloc_set_destructor(state, NULL);
995 if (ret != 0) {
996 if (ret != EAGAIN) {
997 tevent_req_error(req, ret);
998 return;
1001 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1002 * means the lower level pthreadpool failed to create a new
1003 * thread. Fallback to sync processing in that case to allow
1004 * some progress for the client.
1006 vfs_gluster_pread_do(state);
1009 tevent_req_done(req);
1012 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req,
1013 struct vfs_aio_state *vfs_aio_state)
1015 struct vfs_gluster_pread_state *state = tevent_req_data(
1016 req, struct vfs_gluster_pread_state);
1018 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1019 return -1;
1022 *vfs_aio_state = state->vfs_aio_state;
1023 return state->ret;
1026 struct vfs_gluster_pwrite_state {
1027 ssize_t ret;
1028 glfs_fd_t *fd;
1029 const void *buf;
1030 size_t count;
1031 off_t offset;
1033 struct vfs_aio_state vfs_aio_state;
1034 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1037 static void vfs_gluster_pwrite_do(void *private_data);
1038 static void vfs_gluster_pwrite_done(struct tevent_req *subreq);
1039 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state);
1041 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
1042 *handle, TALLOC_CTX *mem_ctx,
1043 struct tevent_context *ev,
1044 files_struct *fsp,
1045 const void *data, size_t n,
1046 off_t offset)
1048 struct tevent_req *req, *subreq;
1049 struct vfs_gluster_pwrite_state *state;
1051 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1052 if (glfd == NULL) {
1053 DBG_ERR("Failed to fetch gluster fd\n");
1054 return NULL;
1057 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pwrite_state);
1058 if (req == NULL) {
1059 return NULL;
1062 state->ret = -1;
1063 state->fd = glfd;
1064 state->buf = data;
1065 state->count = n;
1066 state->offset = offset;
1068 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
1069 state->profile_bytes, n);
1070 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1072 subreq = pthreadpool_tevent_job_send(
1073 state, ev, handle->conn->sconn->pool,
1074 vfs_gluster_pwrite_do, state);
1075 if (tevent_req_nomem(subreq, req)) {
1076 return tevent_req_post(req, ev);
1078 tevent_req_set_callback(subreq, vfs_gluster_pwrite_done, req);
1080 talloc_set_destructor(state, vfs_gluster_pwrite_state_destructor);
1082 return req;
1085 static void vfs_gluster_pwrite_do(void *private_data)
1087 struct vfs_gluster_pwrite_state *state = talloc_get_type_abort(
1088 private_data, struct vfs_gluster_pwrite_state);
1089 struct timespec start_time;
1090 struct timespec end_time;
1092 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1094 PROFILE_TIMESTAMP(&start_time);
1096 do {
1097 #ifdef HAVE_GFAPI_VER_7_6
1098 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1099 state->offset, 0, NULL, NULL);
1100 #else
1101 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1102 state->offset, 0);
1103 #endif
1104 } while ((state->ret == -1) && (errno == EINTR));
1106 if (state->ret == -1) {
1107 state->vfs_aio_state.error = errno;
1110 PROFILE_TIMESTAMP(&end_time);
1112 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1114 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1117 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state)
1119 return -1;
1122 static void vfs_gluster_pwrite_done(struct tevent_req *subreq)
1124 struct tevent_req *req = tevent_req_callback_data(
1125 subreq, struct tevent_req);
1126 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1127 req, struct vfs_gluster_pwrite_state);
1128 int ret;
1130 ret = pthreadpool_tevent_job_recv(subreq);
1131 TALLOC_FREE(subreq);
1132 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1133 talloc_set_destructor(state, NULL);
1134 if (ret != 0) {
1135 if (ret != EAGAIN) {
1136 tevent_req_error(req, ret);
1137 return;
1140 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1141 * means the lower level pthreadpool failed to create a new
1142 * thread. Fallback to sync processing in that case to allow
1143 * some progress for the client.
1145 vfs_gluster_pwrite_do(state);
1148 tevent_req_done(req);
1151 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req,
1152 struct vfs_aio_state *vfs_aio_state)
1154 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1155 req, struct vfs_gluster_pwrite_state);
1157 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1158 return -1;
1161 *vfs_aio_state = state->vfs_aio_state;
1163 return state->ret;
1166 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
1167 files_struct *fsp, const void *data,
1168 size_t n, off_t offset)
1170 ssize_t ret;
1171 glfs_fd_t *glfd = NULL;
1173 START_PROFILE_BYTES(syscall_pwrite, n);
1175 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1176 if (glfd == NULL) {
1177 END_PROFILE_BYTES(syscall_pwrite);
1178 DBG_ERR("Failed to fetch gluster fd\n");
1179 return -1;
1182 #ifdef HAVE_GFAPI_VER_7_6
1183 ret = glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
1184 #else
1185 ret = glfs_pwrite(glfd, data, n, offset, 0);
1186 #endif
1187 END_PROFILE_BYTES(syscall_pwrite);
1189 return ret;
1192 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
1193 files_struct *fsp, off_t offset, int whence)
1195 off_t ret = 0;
1196 glfs_fd_t *glfd = NULL;
1198 START_PROFILE(syscall_lseek);
1200 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1201 if (glfd == NULL) {
1202 END_PROFILE(syscall_lseek);
1203 DBG_ERR("Failed to fetch gluster fd\n");
1204 return -1;
1207 ret = glfs_lseek(glfd, offset, whence);
1208 END_PROFILE(syscall_lseek);
1210 return ret;
1213 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
1214 files_struct *fromfsp,
1215 const DATA_BLOB *hdr,
1216 off_t offset, size_t n)
1218 errno = ENOTSUP;
1219 return -1;
1222 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1223 int fromfd, files_struct *tofsp,
1224 off_t offset, size_t n)
1226 errno = ENOTSUP;
1227 return -1;
1230 static int vfs_gluster_renameat(struct vfs_handle_struct *handle,
1231 files_struct *srcfsp,
1232 const struct smb_filename *smb_fname_src,
1233 files_struct *dstfsp,
1234 const struct smb_filename *smb_fname_dst,
1235 const struct vfs_rename_how *how)
1237 int ret;
1239 #ifdef HAVE_GFAPI_VER_7_11
1240 glfs_fd_t *src_pglfd = NULL;
1241 glfs_fd_t *dst_pglfd = NULL;
1243 START_PROFILE(syscall_renameat);
1245 if (how->flags != 0) {
1246 END_PROFILE(syscall_renameat);
1247 errno = EINVAL;
1248 return -1;
1251 src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
1252 if (src_pglfd == NULL) {
1253 END_PROFILE(syscall_renameat);
1254 DBG_ERR("Failed to fetch gluster fd\n");
1255 return -1;
1258 dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
1259 if (dst_pglfd == NULL) {
1260 END_PROFILE(syscall_renameat);
1261 DBG_ERR("Failed to fetch gluster fd\n");
1262 return -1;
1265 ret = glfs_renameat(src_pglfd, smb_fname_src->base_name,
1266 dst_pglfd, smb_fname_dst->base_name);
1267 #else
1268 struct smb_filename *full_fname_src = NULL;
1269 struct smb_filename *full_fname_dst = NULL;
1271 START_PROFILE(syscall_renameat);
1273 if (how->flags != 0) {
1274 END_PROFILE(syscall_renameat);
1275 errno = EINVAL;
1276 return -1;
1279 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1280 srcfsp,
1281 smb_fname_src);
1282 if (full_fname_src == NULL) {
1283 END_PROFILE(syscall_renameat);
1284 errno = ENOMEM;
1285 return -1;
1288 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1289 dstfsp,
1290 smb_fname_dst);
1291 if (full_fname_dst == NULL) {
1292 END_PROFILE(syscall_renameat);
1293 TALLOC_FREE(full_fname_src);
1294 errno = ENOMEM;
1295 return -1;
1297 ret = glfs_rename(handle->data,
1298 full_fname_src->base_name,
1299 full_fname_dst->base_name);
1301 TALLOC_FREE(full_fname_src);
1302 TALLOC_FREE(full_fname_dst);
1303 #endif
1305 END_PROFILE(syscall_renameat);
1307 return ret;
1310 struct vfs_gluster_fsync_state {
1311 ssize_t ret;
1312 glfs_fd_t *fd;
1314 struct vfs_aio_state vfs_aio_state;
1315 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1318 static void vfs_gluster_fsync_do(void *private_data);
1319 static void vfs_gluster_fsync_done(struct tevent_req *subreq);
1320 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state);
1322 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1323 *handle, TALLOC_CTX *mem_ctx,
1324 struct tevent_context *ev,
1325 files_struct *fsp)
1327 struct tevent_req *req, *subreq;
1328 struct vfs_gluster_fsync_state *state;
1330 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1331 if (glfd == NULL) {
1332 DBG_ERR("Failed to fetch gluster fd\n");
1333 return NULL;
1336 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_fsync_state);
1337 if (req == NULL) {
1338 return NULL;
1341 state->ret = -1;
1342 state->fd = glfd;
1344 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
1345 state->profile_bytes, 0);
1346 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1348 subreq = pthreadpool_tevent_job_send(
1349 state, ev, handle->conn->sconn->pool, vfs_gluster_fsync_do, state);
1350 if (tevent_req_nomem(subreq, req)) {
1351 return tevent_req_post(req, ev);
1353 tevent_req_set_callback(subreq, vfs_gluster_fsync_done, req);
1355 talloc_set_destructor(state, vfs_gluster_fsync_state_destructor);
1357 return req;
1360 static void vfs_gluster_fsync_do(void *private_data)
1362 struct vfs_gluster_fsync_state *state = talloc_get_type_abort(
1363 private_data, struct vfs_gluster_fsync_state);
1364 struct timespec start_time;
1365 struct timespec end_time;
1367 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1369 PROFILE_TIMESTAMP(&start_time);
1371 do {
1372 #ifdef HAVE_GFAPI_VER_7_6
1373 state->ret = glfs_fsync(state->fd, NULL, NULL);
1374 #else
1375 state->ret = glfs_fsync(state->fd);
1376 #endif
1377 } while ((state->ret == -1) && (errno == EINTR));
1379 if (state->ret == -1) {
1380 state->vfs_aio_state.error = errno;
1383 PROFILE_TIMESTAMP(&end_time);
1385 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1387 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1390 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state)
1392 return -1;
1395 static void vfs_gluster_fsync_done(struct tevent_req *subreq)
1397 struct tevent_req *req = tevent_req_callback_data(
1398 subreq, struct tevent_req);
1399 struct vfs_gluster_fsync_state *state = tevent_req_data(
1400 req, struct vfs_gluster_fsync_state);
1401 int ret;
1403 ret = pthreadpool_tevent_job_recv(subreq);
1404 TALLOC_FREE(subreq);
1405 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1406 talloc_set_destructor(state, NULL);
1407 if (ret != 0) {
1408 if (ret != EAGAIN) {
1409 tevent_req_error(req, ret);
1410 return;
1413 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1414 * means the lower level pthreadpool failed to create a new
1415 * thread. Fallback to sync processing in that case to allow
1416 * some progress for the client.
1418 vfs_gluster_fsync_do(state);
1421 tevent_req_done(req);
1424 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1425 struct vfs_aio_state *vfs_aio_state)
1427 struct vfs_gluster_fsync_state *state = tevent_req_data(
1428 req, struct vfs_gluster_fsync_state);
1430 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1431 return -1;
1434 *vfs_aio_state = state->vfs_aio_state;
1435 return state->ret;
1438 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1439 struct smb_filename *smb_fname)
1441 struct stat st;
1442 int ret;
1444 START_PROFILE(syscall_stat);
1445 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1446 if (ret == 0) {
1447 smb_stat_ex_from_stat(&smb_fname->st, &st);
1449 if (ret < 0 && errno != ENOENT) {
1450 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1451 smb_fname->base_name, strerror(errno)));
1453 END_PROFILE(syscall_stat);
1455 return ret;
1458 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1459 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1461 struct stat st;
1462 int ret;
1463 glfs_fd_t *glfd = NULL;
1465 START_PROFILE(syscall_fstat);
1467 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1468 if (glfd == NULL) {
1469 END_PROFILE(syscall_fstat);
1470 DBG_ERR("Failed to fetch gluster fd\n");
1471 return -1;
1474 ret = glfs_fstat(glfd, &st);
1475 if (ret == 0) {
1476 smb_stat_ex_from_stat(sbuf, &st);
1478 if (ret < 0) {
1479 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1480 fsp_get_io_fd(fsp), strerror(errno)));
1482 END_PROFILE(syscall_fstat);
1484 return ret;
1487 static int vfs_gluster_fstatat(struct vfs_handle_struct *handle,
1488 const struct files_struct *dirfsp,
1489 const struct smb_filename *smb_fname,
1490 SMB_STRUCT_STAT *sbuf,
1491 int flags)
1493 struct stat st;
1494 int ret;
1496 #ifdef HAVE_GFAPI_VER_7_11
1497 glfs_fd_t *pglfd = NULL;
1499 START_PROFILE(syscall_fstatat);
1501 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1502 if (pglfd == NULL) {
1503 END_PROFILE(syscall_fstatat);
1504 DBG_ERR("Failed to fetch gluster fd\n");
1505 return -1;
1508 ret = glfs_fstatat(pglfd, smb_fname->base_name, &st, flags);
1509 #else
1510 struct smb_filename *full_fname = NULL;
1512 START_PROFILE(syscall_fstatat);
1514 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1515 dirfsp,
1516 smb_fname);
1517 if (full_fname == NULL) {
1518 END_PROFILE(syscall_fstatat);
1519 return -1;
1522 ret = glfs_stat(handle->data, full_fname->base_name, &st);
1524 TALLOC_FREE(full_fname->base_name);
1525 #endif
1527 if (ret == 0) {
1528 smb_stat_ex_from_stat(sbuf, &st);
1531 END_PROFILE(syscall_fstatat);
1533 return ret;
1536 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1537 struct smb_filename *smb_fname)
1539 struct stat st;
1540 int ret;
1542 START_PROFILE(syscall_lstat);
1543 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1544 if (ret == 0) {
1545 smb_stat_ex_from_stat(&smb_fname->st, &st);
1547 if (ret < 0 && errno != ENOENT) {
1548 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1549 smb_fname->base_name, strerror(errno)));
1551 END_PROFILE(syscall_lstat);
1553 return ret;
1556 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1557 files_struct *fsp,
1558 const SMB_STRUCT_STAT *sbuf)
1560 uint64_t ret;
1562 START_PROFILE(syscall_get_alloc_size);
1563 ret = sbuf->st_ex_blocks * 512;
1564 END_PROFILE(syscall_get_alloc_size);
1566 return ret;
1569 static int vfs_gluster_unlinkat(struct vfs_handle_struct *handle,
1570 struct files_struct *dirfsp,
1571 const struct smb_filename *smb_fname,
1572 int flags)
1574 int ret;
1576 #ifdef HAVE_GFAPI_VER_7_11
1577 glfs_fd_t *pglfd = NULL;
1579 START_PROFILE(syscall_unlinkat);
1581 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1582 if (pglfd == NULL) {
1583 END_PROFILE(syscall_unlinkat);
1584 DBG_ERR("Failed to fetch gluster fd\n");
1585 return -1;
1588 ret = glfs_unlinkat(pglfd, smb_fname->base_name, flags);
1589 #else
1590 struct smb_filename *full_fname = NULL;
1592 START_PROFILE(syscall_unlinkat);
1594 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1595 dirfsp,
1596 smb_fname);
1597 if (full_fname == NULL) {
1598 END_PROFILE(syscall_unlinkat);
1599 return -1;
1602 if (flags & AT_REMOVEDIR) {
1603 ret = glfs_rmdir(handle->data, full_fname->base_name);
1604 } else {
1605 ret = glfs_unlink(handle->data, full_fname->base_name);
1608 TALLOC_FREE(full_fname);
1609 #endif
1611 END_PROFILE(syscall_unlinkat);
1613 return ret;
1616 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1617 files_struct *fsp, mode_t mode)
1619 int ret;
1620 glfs_fd_t *glfd = NULL;
1622 START_PROFILE(syscall_fchmod);
1624 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1625 if (glfd == NULL) {
1626 END_PROFILE(syscall_fchmod);
1627 DBG_ERR("Failed to fetch gluster fd\n");
1628 return -1;
1631 if (!fsp->fsp_flags.is_pathref) {
1633 * We can use an io_fd to remove xattrs.
1635 ret = glfs_fchmod(glfd, mode);
1636 } else {
1638 * This is no longer a handle based call.
1640 ret = glfs_chmod(handle->data, fsp->fsp_name->base_name, mode);
1642 END_PROFILE(syscall_fchmod);
1644 return ret;
1647 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1648 files_struct *fsp, uid_t uid, gid_t gid)
1650 int ret;
1651 glfs_fd_t *glfd = NULL;
1653 START_PROFILE(syscall_fchown);
1655 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1656 if (glfd == NULL) {
1657 END_PROFILE(syscall_fchown);
1658 DBG_ERR("Failed to fetch gluster fd\n");
1659 return -1;
1662 ret = glfs_fchown(glfd, uid, gid);
1663 END_PROFILE(syscall_fchown);
1665 return ret;
1668 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1669 const struct smb_filename *smb_fname,
1670 uid_t uid,
1671 gid_t gid)
1673 int ret;
1675 START_PROFILE(syscall_lchown);
1676 ret = glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1677 END_PROFILE(syscall_lchown);
1679 return ret;
1682 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1683 const struct smb_filename *smb_fname)
1685 int ret;
1687 START_PROFILE(syscall_chdir);
1688 ret = glfs_chdir(handle->data, smb_fname->base_name);
1689 END_PROFILE(syscall_chdir);
1691 return ret;
1694 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1695 TALLOC_CTX *ctx)
1697 char cwd[PATH_MAX] = { '\0' };
1698 char *ret;
1699 struct smb_filename *smb_fname = NULL;
1701 START_PROFILE(syscall_getwd);
1703 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1704 END_PROFILE(syscall_getwd);
1706 if (ret == NULL) {
1707 return NULL;
1709 smb_fname = synthetic_smb_fname(ctx,
1710 ret,
1711 NULL,
1712 NULL,
1715 return smb_fname;
1718 static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
1719 files_struct *fsp,
1720 struct smb_file_time *ft)
1722 int ret = -1;
1723 struct timespec times[2];
1724 glfs_fd_t *glfd = NULL;
1726 START_PROFILE(syscall_fntimes);
1728 if (is_omit_timespec(&ft->atime)) {
1729 times[0].tv_sec = fsp->fsp_name->st.st_ex_atime.tv_sec;
1730 times[0].tv_nsec = fsp->fsp_name->st.st_ex_atime.tv_nsec;
1731 } else {
1732 times[0].tv_sec = ft->atime.tv_sec;
1733 times[0].tv_nsec = ft->atime.tv_nsec;
1736 if (is_omit_timespec(&ft->mtime)) {
1737 times[1].tv_sec = fsp->fsp_name->st.st_ex_mtime.tv_sec;
1738 times[1].tv_nsec = fsp->fsp_name->st.st_ex_mtime.tv_nsec;
1739 } else {
1740 times[1].tv_sec = ft->mtime.tv_sec;
1741 times[1].tv_nsec = ft->mtime.tv_nsec;
1744 if ((timespec_compare(&times[0],
1745 &fsp->fsp_name->st.st_ex_atime) == 0) &&
1746 (timespec_compare(&times[1],
1747 &fsp->fsp_name->st.st_ex_mtime) == 0)) {
1748 END_PROFILE(syscall_fntimes);
1749 return 0;
1752 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1753 if (glfd == NULL) {
1754 END_PROFILE(syscall_fntimes);
1755 DBG_ERR("Failed to fetch gluster fd\n");
1756 return -1;
1759 if (!fsp->fsp_flags.is_pathref) {
1760 ret = glfs_futimens(glfd, times);
1761 } else {
1762 ret = glfs_utimens(handle->data,
1763 fsp->fsp_name->base_name,
1764 times);
1766 END_PROFILE(syscall_fntimes);
1768 return ret;
1771 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1772 files_struct *fsp, off_t offset)
1774 int ret;
1775 glfs_fd_t *glfd = NULL;
1777 START_PROFILE(syscall_ftruncate);
1779 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1780 if (glfd == NULL) {
1781 END_PROFILE(syscall_ftruncate);
1782 DBG_ERR("Failed to fetch gluster fd\n");
1783 return -1;
1786 #ifdef HAVE_GFAPI_VER_7_6
1787 ret = glfs_ftruncate(glfd, offset, NULL, NULL);
1788 #else
1789 ret = glfs_ftruncate(glfd, offset);
1790 #endif
1791 END_PROFILE(syscall_ftruncate);
1793 return ret;
1796 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1797 struct files_struct *fsp,
1798 uint32_t mode,
1799 off_t offset, off_t len)
1801 int ret;
1802 #ifdef HAVE_GFAPI_VER_6
1803 glfs_fd_t *glfd = NULL;
1804 int keep_size, punch_hole;
1806 START_PROFILE(syscall_fallocate);
1808 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1809 if (glfd == NULL) {
1810 END_PROFILE(syscall_fallocate);
1811 DBG_ERR("Failed to fetch gluster fd\n");
1812 return -1;
1815 keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1816 punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1818 mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1819 if (mode != 0) {
1820 END_PROFILE(syscall_fallocate);
1821 errno = ENOTSUP;
1822 return -1;
1825 if (punch_hole) {
1826 ret = glfs_discard(glfd, offset, len);
1827 if (ret != 0) {
1828 DBG_DEBUG("glfs_discard failed: %s\n",
1829 strerror(errno));
1833 ret = glfs_fallocate(glfd, keep_size, offset, len);
1834 END_PROFILE(syscall_fallocate);
1835 #else
1836 errno = ENOTSUP;
1837 ret = -1;
1838 #endif
1839 return ret;
1842 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1843 TALLOC_CTX *ctx,
1844 const struct smb_filename *smb_fname)
1846 char *result = NULL;
1847 struct smb_filename *result_fname = NULL;
1848 char *resolved_path = NULL;
1850 START_PROFILE(syscall_realpath);
1852 resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1853 if (resolved_path == NULL) {
1854 END_PROFILE(syscall_realpath);
1855 errno = ENOMEM;
1856 return NULL;
1859 result = glfs_realpath(handle->data,
1860 smb_fname->base_name,
1861 resolved_path);
1862 if (result != NULL) {
1863 result_fname = synthetic_smb_fname(ctx,
1864 result,
1865 NULL,
1866 NULL,
1871 SAFE_FREE(resolved_path);
1872 END_PROFILE(syscall_realpath);
1874 return result_fname;
1877 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1878 files_struct *fsp, int op, off_t offset,
1879 off_t count, int type)
1881 struct flock flock = { 0, };
1882 int ret;
1883 glfs_fd_t *glfd = NULL;
1884 bool ok = false;
1886 START_PROFILE(syscall_fcntl_lock);
1888 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1889 if (glfd == NULL) {
1890 DBG_ERR("Failed to fetch gluster fd\n");
1891 ok = false;
1892 goto out;
1895 flock.l_type = type;
1896 flock.l_whence = SEEK_SET;
1897 flock.l_start = offset;
1898 flock.l_len = count;
1899 flock.l_pid = 0;
1901 ret = glfs_posix_lock(glfd, op, &flock);
1903 if (op == F_GETLK) {
1904 /* lock query, true if someone else has locked */
1905 if ((ret != -1) &&
1906 (flock.l_type != F_UNLCK) &&
1907 (flock.l_pid != 0) && (flock.l_pid != getpid())) {
1908 ok = true;
1909 goto out;
1911 /* not me */
1912 ok = false;
1913 goto out;
1916 if (ret == -1) {
1917 ok = false;
1918 goto out;
1921 ok = true;
1922 out:
1923 END_PROFILE(syscall_fcntl_lock);
1925 return ok;
1928 static int vfs_gluster_filesystem_sharemode(struct vfs_handle_struct *handle,
1929 files_struct *fsp,
1930 uint32_t share_access,
1931 uint32_t access_mask)
1933 errno = ENOSYS;
1934 return -1;
1937 static int vfs_gluster_fcntl(vfs_handle_struct *handle,
1938 files_struct *fsp, int cmd, va_list cmd_arg)
1941 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1942 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1944 if (cmd == F_GETFL) {
1945 return 0;
1946 } else if (cmd == F_SETFL) {
1947 va_list dup_cmd_arg;
1948 int opt;
1950 va_copy(dup_cmd_arg, cmd_arg);
1951 opt = va_arg(dup_cmd_arg, int);
1952 va_end(dup_cmd_arg);
1953 if (opt == 0) {
1954 return 0;
1956 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1957 goto err_out;
1959 DBG_ERR("unexpected fcntl: %d\n", cmd);
1960 err_out:
1961 errno = EINVAL;
1962 return -1;
1965 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1966 files_struct *fsp, int leasetype)
1968 errno = ENOSYS;
1969 return -1;
1972 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1973 files_struct *fsp, off_t *poffset,
1974 off_t *pcount, int *ptype, pid_t *ppid)
1976 struct flock flock = { 0, };
1977 int ret;
1978 glfs_fd_t *glfd = NULL;
1980 START_PROFILE(syscall_fcntl_getlock);
1982 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1983 if (glfd == NULL) {
1984 END_PROFILE(syscall_fcntl_getlock);
1985 DBG_ERR("Failed to fetch gluster fd\n");
1986 return false;
1989 flock.l_type = *ptype;
1990 flock.l_whence = SEEK_SET;
1991 flock.l_start = *poffset;
1992 flock.l_len = *pcount;
1993 flock.l_pid = 0;
1995 ret = glfs_posix_lock(glfd, F_GETLK, &flock);
1997 if (ret == -1) {
1998 END_PROFILE(syscall_fcntl_getlock);
1999 return false;
2002 *ptype = flock.l_type;
2003 *poffset = flock.l_start;
2004 *pcount = flock.l_len;
2005 *ppid = flock.l_pid;
2006 END_PROFILE(syscall_fcntl_getlock);
2008 return true;
2011 static int vfs_gluster_symlinkat(struct vfs_handle_struct *handle,
2012 const struct smb_filename *link_target,
2013 struct files_struct *dirfsp,
2014 const struct smb_filename *new_smb_fname)
2016 int ret;
2018 #ifdef HAVE_GFAPI_VER_7_11
2019 glfs_fd_t *pglfd = NULL;
2021 START_PROFILE(syscall_symlinkat);
2023 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2024 if (pglfd == NULL) {
2025 END_PROFILE(syscall_symlinkat);
2026 DBG_ERR("Failed to fetch gluster fd\n");
2027 return -1;
2030 ret = glfs_symlinkat(link_target->base_name,
2031 pglfd,
2032 new_smb_fname->base_name);
2033 #else
2034 struct smb_filename *full_fname = NULL;
2036 START_PROFILE(syscall_symlinkat);
2038 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2039 dirfsp,
2040 new_smb_fname);
2041 if (full_fname == NULL) {
2042 END_PROFILE(syscall_symlinkat);
2043 return -1;
2046 ret = glfs_symlink(handle->data,
2047 link_target->base_name,
2048 full_fname->base_name);
2050 TALLOC_FREE(full_fname);
2051 #endif
2053 END_PROFILE(syscall_symlinkat);
2055 return ret;
2058 static int vfs_gluster_readlinkat(struct vfs_handle_struct *handle,
2059 const struct files_struct *dirfsp,
2060 const struct smb_filename *smb_fname,
2061 char *buf,
2062 size_t bufsiz)
2064 int ret;
2066 #ifdef HAVE_GFAPI_VER_7_11
2067 glfs_fd_t *pglfd = NULL;
2069 START_PROFILE(syscall_readlinkat);
2071 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2072 if (pglfd == NULL) {
2073 END_PROFILE(syscall_readlinkat);
2074 DBG_ERR("Failed to fetch gluster fd\n");
2075 return -1;
2078 ret = glfs_readlinkat(pglfd, smb_fname->base_name, buf, bufsiz);
2079 #else
2080 struct smb_filename *full_fname = NULL;
2082 START_PROFILE(syscall_readlinkat);
2084 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2085 dirfsp,
2086 smb_fname);
2087 if (full_fname == NULL) {
2088 END_PROFILE(syscall_readlinkat);
2089 return -1;
2092 ret = glfs_readlink(handle->data, full_fname->base_name, buf, bufsiz);
2094 TALLOC_FREE(full_fname);
2095 #endif
2097 END_PROFILE(syscall_readlinkat);
2099 return ret;
2102 static int vfs_gluster_linkat(struct vfs_handle_struct *handle,
2103 files_struct *srcfsp,
2104 const struct smb_filename *old_smb_fname,
2105 files_struct *dstfsp,
2106 const struct smb_filename *new_smb_fname,
2107 int flags)
2109 int ret;
2111 #ifdef HAVE_GFAPI_VER_7_11
2112 glfs_fd_t *src_pglfd = NULL;
2113 glfs_fd_t *dst_pglfd = NULL;
2115 START_PROFILE(syscall_linkat);
2117 src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
2118 if (src_pglfd == NULL) {
2119 END_PROFILE(syscall_linkat);
2120 DBG_ERR("Failed to fetch gluster fd\n");
2121 return -1;
2124 dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
2125 if (dst_pglfd == NULL) {
2126 END_PROFILE(syscall_linkat);
2127 DBG_ERR("Failed to fetch gluster fd\n");
2128 return -1;
2131 ret = glfs_linkat(src_pglfd,
2132 old_smb_fname->base_name,
2133 dst_pglfd,
2134 new_smb_fname->base_name,
2135 flags);
2136 #else
2137 struct smb_filename *full_fname_old = NULL;
2138 struct smb_filename *full_fname_new = NULL;
2140 START_PROFILE(syscall_linkat);
2142 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
2143 srcfsp,
2144 old_smb_fname);
2145 if (full_fname_old == NULL) {
2146 END_PROFILE(syscall_linkat);
2147 return -1;
2150 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
2151 dstfsp,
2152 new_smb_fname);
2153 if (full_fname_new == NULL) {
2154 END_PROFILE(syscall_linkat);
2155 TALLOC_FREE(full_fname_old);
2156 return -1;
2159 ret = glfs_link(handle->data,
2160 full_fname_old->base_name,
2161 full_fname_new->base_name);
2163 TALLOC_FREE(full_fname_old);
2164 TALLOC_FREE(full_fname_new);
2165 #endif
2167 END_PROFILE(syscall_linkat);
2169 return ret;
2172 static int vfs_gluster_mknodat(struct vfs_handle_struct *handle,
2173 files_struct *dirfsp,
2174 const struct smb_filename *smb_fname,
2175 mode_t mode,
2176 SMB_DEV_T dev)
2178 int ret;
2180 #ifdef HAVE_GFAPI_VER_7_11
2181 glfs_fd_t *pglfd = NULL;
2183 START_PROFILE(syscall_mknodat);
2185 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2186 if (pglfd == NULL) {
2187 END_PROFILE(syscall_mknodat);
2188 DBG_ERR("Failed to fetch gluster fd\n");
2189 return -1;
2192 ret = glfs_mknodat(pglfd, smb_fname->base_name, mode, dev);
2193 #else
2194 struct smb_filename *full_fname = NULL;
2196 START_PROFILE(syscall_mknodat);
2198 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2199 dirfsp,
2200 smb_fname);
2201 if (full_fname == NULL) {
2202 END_PROFILE(syscall_mknodat);
2203 return -1;
2206 ret = glfs_mknod(handle->data, full_fname->base_name, mode, dev);
2208 TALLOC_FREE(full_fname);
2209 #endif
2211 END_PROFILE(syscall_mknodat);
2213 return ret;
2216 static int vfs_gluster_fchflags(struct vfs_handle_struct *handle,
2217 struct files_struct *fsp,
2218 unsigned int flags)
2220 errno = ENOSYS;
2221 return -1;
2224 static NTSTATUS vfs_gluster_get_real_filename_at(
2225 struct vfs_handle_struct *handle,
2226 struct files_struct *dirfsp,
2227 const char *name,
2228 TALLOC_CTX *mem_ctx,
2229 char **found_name)
2231 int ret;
2232 char key_buf[GLUSTER_NAME_MAX + 64];
2233 char val_buf[GLUSTER_NAME_MAX + 1];
2235 if (strlen(name) >= GLUSTER_NAME_MAX) {
2236 return NT_STATUS_OBJECT_NAME_INVALID;
2239 snprintf(key_buf, GLUSTER_NAME_MAX + 64,
2240 "glusterfs.get_real_filename:%s", name);
2242 ret = glfs_getxattr(handle->data,
2243 dirfsp->fsp_name->base_name,
2244 key_buf,
2245 val_buf,
2246 GLUSTER_NAME_MAX + 1);
2247 if (ret == -1) {
2248 if (errno == ENOATTR) {
2249 errno = ENOENT;
2251 return map_nt_error_from_unix(errno);
2254 *found_name = talloc_strdup(mem_ctx, val_buf);
2255 if (found_name[0] == NULL) {
2256 return NT_STATUS_NO_MEMORY;
2259 return NT_STATUS_OK;
2262 static const char *vfs_gluster_connectpath(
2263 struct vfs_handle_struct *handle,
2264 const struct files_struct *dirfsp,
2265 const struct smb_filename *smb_fname)
2267 return handle->conn->connectpath;
2270 /* EA Operations */
2272 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
2273 files_struct *fsp, const char *name,
2274 void *value, size_t size)
2276 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2277 if (glfd == NULL) {
2278 DBG_ERR("Failed to fetch gluster fd\n");
2279 return -1;
2282 if (!fsp->fsp_flags.is_pathref) {
2284 * We can use an io_fd to retrieve xattr value.
2286 return glfs_fgetxattr(glfd, name, value, size);
2290 * This is no longer a handle based call.
2292 return glfs_getxattr(handle->data,
2293 fsp->fsp_name->base_name,
2294 name,
2295 value,
2296 size);
2299 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
2300 files_struct *fsp, char *list,
2301 size_t size)
2303 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2304 if (glfd == NULL) {
2305 DBG_ERR("Failed to fetch gluster fd\n");
2306 return -1;
2308 if (!fsp->fsp_flags.is_pathref) {
2310 * We can use an io_fd to list xattrs.
2312 return glfs_flistxattr(glfd, list, size);
2313 } else {
2315 * This is no longer a handle based call.
2317 return glfs_listxattr(handle->data,
2318 fsp->fsp_name->base_name,
2319 list,
2320 size);
2324 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
2325 files_struct *fsp, const char *name)
2327 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2328 if (glfd == NULL) {
2329 DBG_ERR("Failed to fetch gluster fd\n");
2330 return -1;
2332 if (!fsp->fsp_flags.is_pathref) {
2334 * We can use an io_fd to remove xattrs.
2336 return glfs_fremovexattr(glfd, name);
2337 } else {
2339 * This is no longer a handle based call.
2341 return glfs_removexattr(handle->data,
2342 fsp->fsp_name->base_name,
2343 name);
2347 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
2348 files_struct *fsp, const char *name,
2349 const void *value, size_t size, int flags)
2351 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2352 if (glfd == NULL) {
2353 DBG_ERR("Failed to fetch gluster fd\n");
2354 return -1;
2357 if (!fsp->fsp_flags.is_pathref) {
2359 * We can use an io_fd to set xattrs.
2361 return glfs_fsetxattr(glfd, name, value, size, flags);
2362 } else {
2364 * This is no longer a handle based call.
2366 return glfs_setxattr(handle->data,
2367 fsp->fsp_name->base_name,
2368 name,
2369 value,
2370 size,
2371 flags);
2375 /* AIO Operations */
2377 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
2378 files_struct *fsp)
2380 return false;
2383 static NTSTATUS vfs_gluster_create_dfs_pathat(struct vfs_handle_struct *handle,
2384 struct files_struct *dirfsp,
2385 const struct smb_filename *smb_fname,
2386 const struct referral *reflist,
2387 size_t referral_count)
2389 TALLOC_CTX *frame = talloc_stackframe();
2390 NTSTATUS status = NT_STATUS_NO_MEMORY;
2391 int ret;
2392 char *msdfs_link = NULL;
2393 #ifdef HAVE_GFAPI_VER_7_11
2394 glfs_fd_t *pglfd = NULL;
2395 #else
2396 struct smb_filename *full_fname = NULL;
2397 #endif
2399 /* Form the msdfs_link contents */
2400 msdfs_link = msdfs_link_string(frame,
2401 reflist,
2402 referral_count);
2403 if (msdfs_link == NULL) {
2404 goto out;
2407 #ifdef HAVE_GFAPI_VER_7_11
2408 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2409 if (pglfd == NULL) {
2410 DBG_ERR("Failed to fetch gluster fd\n");
2411 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2412 goto out;
2415 ret = glfs_symlinkat(msdfs_link, pglfd, smb_fname->base_name);
2416 #else
2417 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2418 dirfsp,
2419 smb_fname);
2420 if (full_fname == NULL) {
2421 goto out;
2424 ret = glfs_symlink(handle->data, msdfs_link, full_fname->base_name);
2426 TALLOC_FREE(full_fname);
2427 #endif
2428 if (ret == 0) {
2429 status = NT_STATUS_OK;
2430 } else {
2431 status = map_nt_error_from_unix(errno);
2434 out:
2436 TALLOC_FREE(frame);
2437 return status;
2441 * Read and return the contents of a DFS redirect given a
2442 * pathname. A caller can pass in NULL for ppreflist and
2443 * preferral_count but still determine if this was a
2444 * DFS redirect point by getting NT_STATUS_OK back
2445 * without incurring the overhead of reading and parsing
2446 * the referral contents.
2449 static NTSTATUS vfs_gluster_read_dfs_pathat(struct vfs_handle_struct *handle,
2450 TALLOC_CTX *mem_ctx,
2451 struct files_struct *dirfsp,
2452 struct smb_filename *smb_fname,
2453 struct referral **ppreflist,
2454 size_t *preferral_count)
2456 NTSTATUS status = NT_STATUS_NO_MEMORY;
2457 size_t bufsize;
2458 char *link_target = NULL;
2459 int referral_len;
2460 bool ok;
2461 #if defined(HAVE_BROKEN_READLINK)
2462 char link_target_buf[PATH_MAX];
2463 #else
2464 char link_target_buf[7];
2465 #endif
2466 struct stat st;
2467 struct smb_filename *full_fname = NULL;
2468 int ret;
2469 #ifdef HAVE_GFAPI_VER_7_11
2470 glfs_fd_t *pglfd = NULL;
2471 #endif
2473 if (is_named_stream(smb_fname)) {
2474 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2475 goto err;
2478 if (ppreflist == NULL && preferral_count == NULL) {
2480 * We're only checking if this is a DFS
2481 * redirect. We don't need to return data.
2483 bufsize = sizeof(link_target_buf);
2484 link_target = link_target_buf;
2485 } else {
2486 bufsize = PATH_MAX;
2487 link_target = talloc_array(mem_ctx, char, bufsize);
2488 if (!link_target) {
2489 goto err;
2493 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2494 dirfsp,
2495 smb_fname);
2496 if (full_fname == NULL) {
2497 status = NT_STATUS_NO_MEMORY;
2498 goto err;
2501 ret = glfs_lstat(handle->data, full_fname->base_name, &st);
2502 if (ret < 0) {
2503 status = map_nt_error_from_unix(errno);
2504 goto err;
2507 #ifdef HAVE_GFAPI_VER_7_11
2508 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2509 if (pglfd == NULL) {
2510 DBG_ERR("Failed to fetch gluster fd\n");
2511 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2514 referral_len = glfs_readlinkat(pglfd,
2515 smb_fname->base_name,
2516 link_target,
2517 bufsize - 1);
2518 #else
2519 referral_len = glfs_readlink(handle->data,
2520 full_fname->base_name,
2521 link_target,
2522 bufsize - 1);
2523 #endif
2524 if (referral_len < 0) {
2525 if (errno == EINVAL) {
2526 DBG_INFO("%s is not a link.\n", full_fname->base_name);
2527 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2528 } else {
2529 status = map_nt_error_from_unix(errno);
2530 DBG_ERR("Error reading "
2531 "msdfs link %s: %s\n",
2532 full_fname->base_name,
2533 strerror(errno));
2535 goto err;
2537 link_target[referral_len] = '\0';
2539 DBG_INFO("%s -> %s\n",
2540 full_fname->base_name,
2541 link_target);
2543 if (!strnequal(link_target, "msdfs:", 6)) {
2544 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2545 goto err;
2548 if (ppreflist == NULL && preferral_count == NULL) {
2549 /* Early return for checking if this is a DFS link. */
2550 TALLOC_FREE(full_fname);
2551 smb_stat_ex_from_stat(&smb_fname->st, &st);
2552 return NT_STATUS_OK;
2555 ok = parse_msdfs_symlink(mem_ctx,
2556 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
2557 link_target,
2558 ppreflist,
2559 preferral_count);
2561 if (ok) {
2562 smb_stat_ex_from_stat(&smb_fname->st, &st);
2563 status = NT_STATUS_OK;
2564 } else {
2565 status = NT_STATUS_NO_MEMORY;
2568 err:
2570 if (link_target != link_target_buf) {
2571 TALLOC_FREE(link_target);
2573 TALLOC_FREE(full_fname);
2574 return status;
2577 static struct vfs_fn_pointers glusterfs_fns = {
2579 /* Disk Operations */
2581 .connect_fn = vfs_gluster_connect,
2582 .disconnect_fn = vfs_gluster_disconnect,
2583 .disk_free_fn = vfs_gluster_disk_free,
2584 .get_quota_fn = vfs_gluster_get_quota,
2585 .set_quota_fn = vfs_gluster_set_quota,
2586 .statvfs_fn = vfs_gluster_statvfs,
2587 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
2589 .get_dfs_referrals_fn = NULL,
2591 /* Directory Operations */
2593 .fdopendir_fn = vfs_gluster_fdopendir,
2594 .readdir_fn = vfs_gluster_readdir,
2595 .rewind_dir_fn = vfs_gluster_rewinddir,
2596 .mkdirat_fn = vfs_gluster_mkdirat,
2597 .closedir_fn = vfs_gluster_closedir,
2599 /* File Operations */
2601 .openat_fn = vfs_gluster_openat,
2602 .create_file_fn = NULL,
2603 .close_fn = vfs_gluster_close,
2604 .pread_fn = vfs_gluster_pread,
2605 .pread_send_fn = vfs_gluster_pread_send,
2606 .pread_recv_fn = vfs_gluster_pread_recv,
2607 .pwrite_fn = vfs_gluster_pwrite,
2608 .pwrite_send_fn = vfs_gluster_pwrite_send,
2609 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
2610 .lseek_fn = vfs_gluster_lseek,
2611 .sendfile_fn = vfs_gluster_sendfile,
2612 .recvfile_fn = vfs_gluster_recvfile,
2613 .renameat_fn = vfs_gluster_renameat,
2614 .fsync_send_fn = vfs_gluster_fsync_send,
2615 .fsync_recv_fn = vfs_gluster_fsync_recv,
2617 .stat_fn = vfs_gluster_stat,
2618 .fstat_fn = vfs_gluster_fstat,
2619 .fstatat_fn = vfs_gluster_fstatat,
2620 .lstat_fn = vfs_gluster_lstat,
2621 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
2622 .unlinkat_fn = vfs_gluster_unlinkat,
2624 .fchmod_fn = vfs_gluster_fchmod,
2625 .fchown_fn = vfs_gluster_fchown,
2626 .lchown_fn = vfs_gluster_lchown,
2627 .chdir_fn = vfs_gluster_chdir,
2628 .getwd_fn = vfs_gluster_getwd,
2629 .fntimes_fn = vfs_gluster_fntimes,
2630 .ftruncate_fn = vfs_gluster_ftruncate,
2631 .fallocate_fn = vfs_gluster_fallocate,
2632 .lock_fn = vfs_gluster_lock,
2633 .filesystem_sharemode_fn = vfs_gluster_filesystem_sharemode,
2634 .fcntl_fn = vfs_gluster_fcntl,
2635 .linux_setlease_fn = vfs_gluster_linux_setlease,
2636 .getlock_fn = vfs_gluster_getlock,
2637 .symlinkat_fn = vfs_gluster_symlinkat,
2638 .readlinkat_fn = vfs_gluster_readlinkat,
2639 .linkat_fn = vfs_gluster_linkat,
2640 .mknodat_fn = vfs_gluster_mknodat,
2641 .realpath_fn = vfs_gluster_realpath,
2642 .fchflags_fn = vfs_gluster_fchflags,
2643 .file_id_create_fn = NULL,
2644 .fstreaminfo_fn = NULL,
2645 .get_real_filename_at_fn = vfs_gluster_get_real_filename_at,
2646 .connectpath_fn = vfs_gluster_connectpath,
2647 .create_dfs_pathat_fn = vfs_gluster_create_dfs_pathat,
2648 .read_dfs_pathat_fn = vfs_gluster_read_dfs_pathat,
2650 .brl_lock_windows_fn = NULL,
2651 .brl_unlock_windows_fn = NULL,
2652 .strict_lock_check_fn = NULL,
2653 .translate_name_fn = NULL,
2654 .fsctl_fn = NULL,
2656 /* NT ACL Operations */
2657 .fget_nt_acl_fn = NULL,
2658 .fset_nt_acl_fn = NULL,
2659 .audit_file_fn = NULL,
2661 /* Posix ACL Operations */
2662 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
2663 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2664 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
2665 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
2667 /* EA Operations */
2668 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2669 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2670 .fgetxattr_fn = vfs_gluster_fgetxattr,
2671 .flistxattr_fn = vfs_gluster_flistxattr,
2672 .fremovexattr_fn = vfs_gluster_fremovexattr,
2673 .fsetxattr_fn = vfs_gluster_fsetxattr,
2675 /* AIO Operations */
2676 .aio_force_fn = vfs_gluster_aio_force,
2678 /* Durable handle Operations */
2679 .durable_cookie_fn = NULL,
2680 .durable_disconnect_fn = NULL,
2681 .durable_reconnect_fn = NULL,
2684 static_decl_vfs;
2685 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
2687 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2688 "glusterfs", &glusterfs_fns);