smbd: reject FILE_ATTRIBUTE_TEMPORARY on directories
[Samba.git] / source3 / modules / vfs_glusterfs.c
blob18bd79ff41ba8c339080cd0c974726996e170a57
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 "smbd/globals.h"
45 #include "lib/util/sys_rw.h"
46 #include "smbprofile.h"
47 #include "modules/posixacl_xattr.h"
48 #include "lib/pthreadpool/pthreadpool_tevent.h"
50 #define DEFAULT_VOLFILE_SERVER "localhost"
51 #define GLUSTER_NAME_MAX 255
53 /**
54 * Helper to convert struct stat to struct stat_ex.
56 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
58 ZERO_STRUCTP(dst);
60 dst->st_ex_dev = src->st_dev;
61 dst->st_ex_ino = src->st_ino;
62 dst->st_ex_mode = src->st_mode;
63 dst->st_ex_nlink = src->st_nlink;
64 dst->st_ex_uid = src->st_uid;
65 dst->st_ex_gid = src->st_gid;
66 dst->st_ex_rdev = src->st_rdev;
67 dst->st_ex_size = src->st_size;
68 dst->st_ex_atime.tv_sec = src->st_atime;
69 dst->st_ex_mtime.tv_sec = src->st_mtime;
70 dst->st_ex_ctime.tv_sec = src->st_ctime;
71 dst->st_ex_btime.tv_sec = src->st_mtime;
72 dst->st_ex_blksize = src->st_blksize;
73 dst->st_ex_blocks = src->st_blocks;
74 #ifdef STAT_HAVE_NSEC
75 dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
76 dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
77 dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
78 dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
79 #endif
82 /* pre-opened glfs_t */
84 static struct glfs_preopened {
85 char *volume;
86 char *connectpath;
87 glfs_t *fs;
88 int ref;
89 struct glfs_preopened *next, *prev;
90 } *glfs_preopened;
93 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
95 struct glfs_preopened *entry = NULL;
97 entry = talloc_zero(NULL, struct glfs_preopened);
98 if (!entry) {
99 errno = ENOMEM;
100 return -1;
103 entry->volume = talloc_strdup(entry, volume);
104 if (!entry->volume) {
105 talloc_free(entry);
106 errno = ENOMEM;
107 return -1;
110 entry->connectpath = talloc_strdup(entry, connectpath);
111 if (entry->connectpath == NULL) {
112 talloc_free(entry);
113 errno = ENOMEM;
114 return -1;
117 entry->fs = fs;
118 entry->ref = 1;
120 DLIST_ADD(glfs_preopened, entry);
122 return 0;
125 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
127 struct glfs_preopened *entry = NULL;
129 for (entry = glfs_preopened; entry; entry = entry->next) {
130 if (strcmp(entry->volume, volume) == 0 &&
131 strcmp(entry->connectpath, connectpath) == 0)
133 entry->ref++;
134 return entry->fs;
138 return NULL;
141 static void glfs_clear_preopened(glfs_t *fs)
143 struct glfs_preopened *entry = NULL;
145 for (entry = glfs_preopened; entry; entry = entry->next) {
146 if (entry->fs == fs) {
147 if (--entry->ref)
148 return;
150 DLIST_REMOVE(glfs_preopened, entry);
152 glfs_fini(entry->fs);
153 talloc_free(entry);
158 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
159 const char *volfile_servers)
161 char *server = NULL;
162 size_t server_count = 0;
163 size_t server_success = 0;
164 int ret = -1;
165 TALLOC_CTX *frame = talloc_stackframe();
167 DBG_INFO("servers list %s\n", volfile_servers);
169 while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
170 char *transport = NULL;
171 char *host = NULL;
172 int port = 0;
174 server_count++;
175 DBG_INFO("server %zu %s\n", server_count, server);
177 /* Determine the transport type */
178 if (strncmp(server, "unix+", 5) == 0) {
179 port = 0;
180 transport = talloc_strdup(frame, "unix");
181 if (!transport) {
182 errno = ENOMEM;
183 goto out;
185 host = talloc_strdup(frame, server + 5);
186 if (!host) {
187 errno = ENOMEM;
188 goto out;
190 } else {
191 char *p = NULL;
192 char *port_index = NULL;
194 if (strncmp(server, "tcp+", 4) == 0) {
195 server += 4;
198 /* IPv6 is enclosed in []
199 * ':' before ']' is part of IPv6
200 * ':' after ']' indicates port
202 p = server;
203 if (server[0] == '[') {
204 server++;
205 p = index(server, ']');
206 if (p == NULL) {
207 /* Malformed IPv6 */
208 continue;
210 p[0] = '\0';
211 p++;
214 port_index = index(p, ':');
216 if (port_index == NULL) {
217 port = 0;
218 } else {
219 port = atoi(port_index + 1);
220 port_index[0] = '\0';
222 transport = talloc_strdup(frame, "tcp");
223 if (!transport) {
224 errno = ENOMEM;
225 goto out;
227 host = talloc_strdup(frame, server);
228 if (!host) {
229 errno = ENOMEM;
230 goto out;
234 DBG_INFO("Calling set volfile server with params "
235 "transport=%s, host=%s, port=%d\n", transport,
236 host, port);
238 ret = glfs_set_volfile_server(fs, transport, host, port);
239 if (ret < 0) {
240 DBG_WARNING("Failed to set volfile_server "
241 "transport=%s, host=%s, port=%d (%s)\n",
242 transport, host, port, strerror(errno));
243 } else {
244 server_success++;
248 out:
249 if (server_count == 0) {
250 ret = -1;
251 } else if (server_success < server_count) {
252 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
253 server_count - server_success, server_count);
254 ret = 0;
257 TALLOC_FREE(frame);
258 return ret;
261 /* Disk Operations */
263 static int check_for_write_behind_translator(TALLOC_CTX *mem_ctx,
264 glfs_t *fs,
265 const char *volume)
267 char *buf = NULL;
268 char **lines = NULL;
269 int numlines = 0;
270 int i;
271 char *option;
272 bool write_behind_present = false;
273 size_t newlen;
274 int ret;
276 ret = glfs_get_volfile(fs, NULL, 0);
277 if (ret == 0) {
278 DBG_ERR("%s: Failed to get volfile for "
279 "volume (%s): No volfile\n",
280 volume,
281 strerror(errno));
282 return -1;
284 if (ret > 0) {
285 DBG_ERR("%s: Invalid return %d for glfs_get_volfile for "
286 "volume (%s): No volfile\n",
287 volume,
288 ret,
289 strerror(errno));
290 return -1;
293 newlen = 0 - ret;
295 buf = talloc_zero_array(mem_ctx, char, newlen);
296 if (buf == NULL) {
297 return -1;
300 ret = glfs_get_volfile(fs, buf, newlen);
301 if (ret != newlen) {
302 TALLOC_FREE(buf);
303 DBG_ERR("%s: Failed to get volfile for volume (%s)\n",
304 volume, strerror(errno));
305 return -1;
308 option = talloc_asprintf(mem_ctx, "volume %s-write-behind", volume);
309 if (option == NULL) {
310 TALLOC_FREE(buf);
311 return -1;
315 * file_lines_parse() plays horrible tricks with
316 * the passed-in talloc pointers and the hierarcy
317 * which makes freeing hard to get right.
319 * As we know mem_ctx is freed by the caller, after
320 * this point don't free on exit and let the caller
321 * handle it. This violates good Samba coding practice
322 * but we know we're not leaking here.
325 lines = file_lines_parse(buf,
326 newlen,
327 &numlines,
328 mem_ctx);
329 if (lines == NULL || numlines <= 0) {
330 return -1;
332 /* On success, buf is now a talloc child of lines !! */
334 for (i=0; i < numlines; i++) {
335 if (strequal(lines[i], option)) {
336 write_behind_present = true;
337 break;
341 if (write_behind_present) {
342 DBG_ERR("Write behind translator is enabled for "
343 "volume (%s), refusing to connect! "
344 "Please turn off the write behind translator by calling "
345 "'gluster volume set %s performance.write-behind off' "
346 "on the commandline. "
347 "Check the vfs_glusterfs(8) manpage for "
348 "further details.\n",
349 volume, volume);
350 return -1;
353 return 0;
356 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
357 const char *service,
358 const char *user)
360 const struct loadparm_substitution *lp_sub =
361 loadparm_s3_global_substitution();
362 const char *volfile_servers;
363 const char *volume;
364 char *logfile;
365 int loglevel;
366 glfs_t *fs = NULL;
367 TALLOC_CTX *tmp_ctx;
368 int ret = 0;
369 bool write_behind_pass_through_set = false;
371 tmp_ctx = talloc_new(NULL);
372 if (tmp_ctx == NULL) {
373 ret = -1;
374 goto done;
376 logfile = lp_parm_substituted_string(tmp_ctx,
377 lp_sub,
378 SNUM(handle->conn),
379 "glusterfs",
380 "logfile",
381 NULL);
383 loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
385 volfile_servers = lp_parm_substituted_string(tmp_ctx,
386 lp_sub,
387 SNUM(handle->conn),
388 "glusterfs",
389 "volfile_server",
390 NULL);
391 if (volfile_servers == NULL) {
392 volfile_servers = DEFAULT_VOLFILE_SERVER;
395 volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
396 NULL);
397 if (volume == NULL) {
398 volume = service;
401 fs = glfs_find_preopened(volume, handle->conn->connectpath);
402 if (fs) {
403 goto done;
406 fs = glfs_new(volume);
407 if (fs == NULL) {
408 ret = -1;
409 goto done;
412 ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
413 if (ret < 0) {
414 DBG_ERR("Failed to set volfile_servers from list %s\n",
415 volfile_servers);
416 goto done;
419 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
420 "true");
421 if (ret < 0) {
422 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
423 goto done;
426 ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-selinux",
427 "true");
428 if (ret < 0) {
429 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
430 goto done;
433 ret = glfs_set_xlator_option(fs, "*-snapview-client",
434 "snapdir-entry-path",
435 handle->conn->connectpath);
436 if (ret < 0) {
437 DEBUG(0, ("%s: Failed to set xlator option:"
438 " snapdir-entry-path\n", volume));
439 goto done;
442 #ifdef HAVE_GFAPI_VER_7_9
443 ret = glfs_set_xlator_option(fs, "*-write-behind", "pass-through",
444 "true");
445 if (ret < 0) {
446 DBG_ERR("%s: Failed to set xlator option: pass-through\n",
447 volume);
448 goto done;
450 write_behind_pass_through_set = true;
451 #endif
453 ret = glfs_set_logging(fs, logfile, loglevel);
454 if (ret < 0) {
455 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
456 volume, logfile, loglevel));
457 goto done;
460 ret = glfs_init(fs);
461 if (ret < 0) {
462 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
463 volume, strerror(errno)));
464 goto done;
467 if (!write_behind_pass_through_set) {
468 ret = check_for_write_behind_translator(tmp_ctx, fs, volume);
469 if (ret < 0) {
470 goto done;
474 ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
475 if (ret < 0) {
476 DEBUG(0, ("%s: Failed to register volume (%s)\n",
477 volume, strerror(errno)));
478 goto done;
482 * The shadow_copy2 module will fail to export subdirectories
483 * of a gluster volume unless we specify the mount point,
484 * because the detection fails if the file system is not
485 * locally mounted:
486 * https://bugzilla.samba.org/show_bug.cgi?id=13091
488 lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
491 * Unless we have an async implementation of getxattrat turn this off.
493 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
495 done:
496 if (ret < 0) {
497 if (fs)
498 glfs_fini(fs);
499 } else {
500 DBG_ERR("%s: Initialized volume from servers %s\n",
501 volume, volfile_servers);
502 handle->data = fs;
504 talloc_free(tmp_ctx);
505 return ret;
508 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
510 glfs_t *fs = NULL;
512 fs = handle->data;
514 glfs_clear_preopened(fs);
517 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
518 const struct smb_filename *smb_fname,
519 uint64_t *bsize_p,
520 uint64_t *dfree_p,
521 uint64_t *dsize_p)
523 struct statvfs statvfs = { 0, };
524 int ret;
526 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
527 if (ret < 0) {
528 return -1;
531 if (bsize_p != NULL) {
532 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
534 if (dfree_p != NULL) {
535 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
537 if (dsize_p != NULL) {
538 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
541 return (uint64_t)statvfs.f_bavail;
544 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
545 const struct smb_filename *smb_fname,
546 enum SMB_QUOTA_TYPE qtype,
547 unid_t id,
548 SMB_DISK_QUOTA *qt)
550 errno = ENOSYS;
551 return -1;
554 static int
555 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
556 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
558 errno = ENOSYS;
559 return -1;
562 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
563 const struct smb_filename *smb_fname,
564 struct vfs_statvfs_struct *vfs_statvfs)
566 struct statvfs statvfs = { 0, };
567 int ret;
569 ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
570 if (ret < 0) {
571 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
572 smb_fname->base_name, strerror(errno)));
573 return -1;
576 ZERO_STRUCTP(vfs_statvfs);
578 vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
579 vfs_statvfs->BlockSize = statvfs.f_bsize;
580 vfs_statvfs->TotalBlocks = statvfs.f_blocks;
581 vfs_statvfs->BlocksAvail = statvfs.f_bfree;
582 vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
583 vfs_statvfs->TotalFileNodes = statvfs.f_files;
584 vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
585 vfs_statvfs->FsIdentifier = statvfs.f_fsid;
586 vfs_statvfs->FsCapabilities =
587 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
589 return ret;
592 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
593 enum timestamp_set_resolution *p_ts_res)
595 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
597 #ifdef HAVE_GFAPI_VER_6
598 caps |= FILE_SUPPORTS_SPARSE_FILES;
599 #endif
601 #ifdef STAT_HAVE_NSEC
602 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
603 #endif
605 return caps;
608 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
609 const files_struct *fsp)
611 glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
612 if (glfd == NULL) {
613 DBG_INFO("Failed to fetch fsp extension\n");
614 return NULL;
616 if (*glfd == NULL) {
617 DBG_INFO("Empty glfs_fd_t pointer\n");
618 return NULL;
621 return *glfd;
624 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
625 files_struct *fsp, const char *mask,
626 uint32_t attributes)
628 glfs_fd_t *glfd = NULL;
630 glfd = glfs_opendir(handle->data, fsp->fsp_name->base_name);
631 if (glfd == NULL) {
632 return NULL;
635 return (DIR *)glfd;
638 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
640 int ret;
642 START_PROFILE(syscall_closedir);
643 ret = glfs_closedir((void *)dirp);
644 END_PROFILE(syscall_closedir);
646 return ret;
649 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
650 struct files_struct *dirfsp,
651 DIR *dirp,
652 SMB_STRUCT_STAT *sbuf)
654 static char direntbuf[512];
655 int ret;
656 struct stat stat;
657 struct dirent *dirent = 0;
659 START_PROFILE(syscall_readdir);
660 if (sbuf != NULL) {
661 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
662 &dirent);
663 } else {
664 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
667 if ((ret < 0) || (dirent == NULL)) {
668 END_PROFILE(syscall_readdir);
669 return NULL;
672 if (sbuf != NULL) {
673 SET_STAT_INVALID(*sbuf);
674 if (!S_ISLNK(stat.st_mode)) {
675 smb_stat_ex_from_stat(sbuf, &stat);
679 END_PROFILE(syscall_readdir);
680 return dirent;
683 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
685 long ret;
687 START_PROFILE(syscall_telldir);
688 ret = glfs_telldir((void *)dirp);
689 END_PROFILE(syscall_telldir);
691 return ret;
694 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
695 long offset)
697 START_PROFILE(syscall_seekdir);
698 glfs_seekdir((void *)dirp, offset);
699 END_PROFILE(syscall_seekdir);
702 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
704 START_PROFILE(syscall_rewinddir);
705 glfs_seekdir((void *)dirp, 0);
706 END_PROFILE(syscall_rewinddir);
709 static int vfs_gluster_mkdirat(struct vfs_handle_struct *handle,
710 struct files_struct *dirfsp,
711 const struct smb_filename *smb_fname,
712 mode_t mode)
714 int ret;
716 #ifdef HAVE_GFAPI_VER_7_11
717 glfs_fd_t *pglfd = NULL;
719 START_PROFILE(syscall_mkdirat);
721 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
722 if (pglfd == NULL) {
723 END_PROFILE(syscall_mkdirat);
724 DBG_ERR("Failed to fetch gluster fd\n");
725 return -1;
728 ret = glfs_mkdirat(pglfd, smb_fname->base_name, mode);
729 #else
730 struct smb_filename *full_fname = NULL;
732 START_PROFILE(syscall_mkdirat);
734 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
735 dirfsp,
736 smb_fname);
737 if (full_fname == NULL) {
738 END_PROFILE(syscall_mkdirat);
739 return -1;
742 ret = glfs_mkdir(handle->data, full_fname->base_name, mode);
744 TALLOC_FREE(full_fname);
745 #endif
747 END_PROFILE(syscall_mkdirat);
749 return ret;
752 static int vfs_gluster_openat(struct vfs_handle_struct *handle,
753 const struct files_struct *dirfsp,
754 const struct smb_filename *smb_fname,
755 files_struct *fsp,
756 const struct vfs_open_how *how)
758 int flags = how->flags;
759 struct smb_filename *full_fname = NULL;
760 bool have_opath = false;
761 bool became_root = false;
762 glfs_fd_t *glfd;
763 glfs_fd_t *pglfd = NULL;
764 glfs_fd_t **p_tmp;
766 START_PROFILE(syscall_openat);
768 if (how->resolve != 0) {
769 END_PROFILE(syscall_openat);
770 errno = ENOSYS;
771 return -1;
774 p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
775 if (p_tmp == NULL) {
776 END_PROFILE(syscall_openat);
777 errno = ENOMEM;
778 return -1;
781 #ifdef O_PATH
782 have_opath = true;
783 if (fsp->fsp_flags.is_pathref) {
784 flags |= O_PATH;
786 #endif
788 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
789 dirfsp,
790 smb_fname);
791 if (full_fname == NULL) {
792 END_PROFILE(syscall_openat);
793 return -1;
796 if (fsp->fsp_flags.is_pathref && !have_opath) {
797 become_root();
798 became_root = true;
801 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
802 #ifdef HAVE_GFAPI_VER_7_11
804 * Fetch Gluster fd for parent directory using dirfsp
805 * before calling glfs_openat();
807 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
808 if (pglfd == NULL) {
809 END_PROFILE(syscall_openat);
810 DBG_ERR("Failed to fetch gluster fd\n");
811 return -1;
814 glfd = glfs_openat(pglfd,
815 smb_fname->base_name,
816 flags,
817 how->mode);
818 #else
820 * Replace smb_fname with full_path constructed above.
822 smb_fname = full_fname;
823 #endif
826 if (pglfd == NULL) {
828 * smb_fname can either be a full_path or the same one
829 * as received from the caller. In the latter case we
830 * are operating at current working directory.
832 if (flags & O_CREAT) {
833 glfd = glfs_creat(handle->data,
834 smb_fname->base_name,
835 flags,
836 how->mode);
837 } else {
838 glfd = glfs_open(handle->data,
839 smb_fname->base_name,
840 flags);
844 if (became_root) {
845 unbecome_root();
848 TALLOC_FREE(full_fname);
850 fsp->fsp_flags.have_proc_fds = false;
852 if (glfd == NULL) {
853 END_PROFILE(syscall_openat);
854 /* no extension destroy_fn, so no need to save errno */
855 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
856 return -1;
859 *p_tmp = glfd;
861 END_PROFILE(syscall_openat);
862 /* An arbitrary value for error reporting, so you know its us. */
863 return 13371337;
866 static int vfs_gluster_close(struct vfs_handle_struct *handle,
867 files_struct *fsp)
869 int ret;
870 glfs_fd_t *glfd = NULL;
872 START_PROFILE(syscall_close);
874 glfd = vfs_gluster_fetch_glfd(handle, fsp);
875 if (glfd == NULL) {
876 END_PROFILE(syscall_close);
877 DBG_ERR("Failed to fetch gluster fd\n");
878 return -1;
881 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
883 ret = glfs_close(glfd);
884 END_PROFILE(syscall_close);
886 return ret;
889 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
890 files_struct *fsp, void *data, size_t n,
891 off_t offset)
893 ssize_t ret;
894 glfs_fd_t *glfd = NULL;
896 START_PROFILE_BYTES(syscall_pread, n);
898 glfd = vfs_gluster_fetch_glfd(handle, fsp);
899 if (glfd == NULL) {
900 END_PROFILE_BYTES(syscall_pread);
901 DBG_ERR("Failed to fetch gluster fd\n");
902 return -1;
905 #ifdef HAVE_GFAPI_VER_7_6
906 ret = glfs_pread(glfd, data, n, offset, 0, NULL);
907 #else
908 ret = glfs_pread(glfd, data, n, offset, 0);
909 #endif
910 END_PROFILE_BYTES(syscall_pread);
912 return ret;
915 struct vfs_gluster_pread_state {
916 ssize_t ret;
917 glfs_fd_t *fd;
918 void *buf;
919 size_t count;
920 off_t offset;
922 struct vfs_aio_state vfs_aio_state;
923 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
926 static void vfs_gluster_pread_do(void *private_data);
927 static void vfs_gluster_pread_done(struct tevent_req *subreq);
928 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state);
930 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
931 *handle, TALLOC_CTX *mem_ctx,
932 struct tevent_context *ev,
933 files_struct *fsp,
934 void *data, size_t n,
935 off_t offset)
937 struct vfs_gluster_pread_state *state;
938 struct tevent_req *req, *subreq;
940 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
941 if (glfd == NULL) {
942 DBG_ERR("Failed to fetch gluster fd\n");
943 return NULL;
946 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pread_state);
947 if (req == NULL) {
948 return NULL;
951 state->ret = -1;
952 state->fd = glfd;
953 state->buf = data;
954 state->count = n;
955 state->offset = offset;
957 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
958 state->profile_bytes, n);
959 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
961 subreq = pthreadpool_tevent_job_send(
962 state, ev, handle->conn->sconn->pool,
963 vfs_gluster_pread_do, state);
964 if (tevent_req_nomem(subreq, req)) {
965 return tevent_req_post(req, ev);
967 tevent_req_set_callback(subreq, vfs_gluster_pread_done, req);
969 talloc_set_destructor(state, vfs_gluster_pread_state_destructor);
971 return req;
974 static void vfs_gluster_pread_do(void *private_data)
976 struct vfs_gluster_pread_state *state = talloc_get_type_abort(
977 private_data, struct vfs_gluster_pread_state);
978 struct timespec start_time;
979 struct timespec end_time;
981 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
983 PROFILE_TIMESTAMP(&start_time);
985 do {
986 #ifdef HAVE_GFAPI_VER_7_6
987 state->ret = glfs_pread(state->fd, state->buf, state->count,
988 state->offset, 0, NULL);
989 #else
990 state->ret = glfs_pread(state->fd, state->buf, state->count,
991 state->offset, 0);
992 #endif
993 } while ((state->ret == -1) && (errno == EINTR));
995 if (state->ret == -1) {
996 state->vfs_aio_state.error = errno;
999 PROFILE_TIMESTAMP(&end_time);
1001 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1003 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1006 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state)
1008 return -1;
1011 static void vfs_gluster_pread_done(struct tevent_req *subreq)
1013 struct tevent_req *req = tevent_req_callback_data(
1014 subreq, struct tevent_req);
1015 struct vfs_gluster_pread_state *state = tevent_req_data(
1016 req, struct vfs_gluster_pread_state);
1017 int ret;
1019 ret = pthreadpool_tevent_job_recv(subreq);
1020 TALLOC_FREE(subreq);
1021 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1022 talloc_set_destructor(state, NULL);
1023 if (ret != 0) {
1024 if (ret != EAGAIN) {
1025 tevent_req_error(req, ret);
1026 return;
1029 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1030 * means the lower level pthreadpool failed to create a new
1031 * thread. Fallback to sync processing in that case to allow
1032 * some progress for the client.
1034 vfs_gluster_pread_do(state);
1037 tevent_req_done(req);
1040 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req,
1041 struct vfs_aio_state *vfs_aio_state)
1043 struct vfs_gluster_pread_state *state = tevent_req_data(
1044 req, struct vfs_gluster_pread_state);
1046 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1047 return -1;
1050 *vfs_aio_state = state->vfs_aio_state;
1051 return state->ret;
1054 struct vfs_gluster_pwrite_state {
1055 ssize_t ret;
1056 glfs_fd_t *fd;
1057 const void *buf;
1058 size_t count;
1059 off_t offset;
1061 struct vfs_aio_state vfs_aio_state;
1062 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1065 static void vfs_gluster_pwrite_do(void *private_data);
1066 static void vfs_gluster_pwrite_done(struct tevent_req *subreq);
1067 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state);
1069 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
1070 *handle, TALLOC_CTX *mem_ctx,
1071 struct tevent_context *ev,
1072 files_struct *fsp,
1073 const void *data, size_t n,
1074 off_t offset)
1076 struct tevent_req *req, *subreq;
1077 struct vfs_gluster_pwrite_state *state;
1079 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1080 if (glfd == NULL) {
1081 DBG_ERR("Failed to fetch gluster fd\n");
1082 return NULL;
1085 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pwrite_state);
1086 if (req == NULL) {
1087 return NULL;
1090 state->ret = -1;
1091 state->fd = glfd;
1092 state->buf = data;
1093 state->count = n;
1094 state->offset = offset;
1096 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
1097 state->profile_bytes, n);
1098 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1100 subreq = pthreadpool_tevent_job_send(
1101 state, ev, handle->conn->sconn->pool,
1102 vfs_gluster_pwrite_do, state);
1103 if (tevent_req_nomem(subreq, req)) {
1104 return tevent_req_post(req, ev);
1106 tevent_req_set_callback(subreq, vfs_gluster_pwrite_done, req);
1108 talloc_set_destructor(state, vfs_gluster_pwrite_state_destructor);
1110 return req;
1113 static void vfs_gluster_pwrite_do(void *private_data)
1115 struct vfs_gluster_pwrite_state *state = talloc_get_type_abort(
1116 private_data, struct vfs_gluster_pwrite_state);
1117 struct timespec start_time;
1118 struct timespec end_time;
1120 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1122 PROFILE_TIMESTAMP(&start_time);
1124 do {
1125 #ifdef HAVE_GFAPI_VER_7_6
1126 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1127 state->offset, 0, NULL, NULL);
1128 #else
1129 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1130 state->offset, 0);
1131 #endif
1132 } while ((state->ret == -1) && (errno == EINTR));
1134 if (state->ret == -1) {
1135 state->vfs_aio_state.error = errno;
1138 PROFILE_TIMESTAMP(&end_time);
1140 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1142 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1145 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state)
1147 return -1;
1150 static void vfs_gluster_pwrite_done(struct tevent_req *subreq)
1152 struct tevent_req *req = tevent_req_callback_data(
1153 subreq, struct tevent_req);
1154 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1155 req, struct vfs_gluster_pwrite_state);
1156 int ret;
1158 ret = pthreadpool_tevent_job_recv(subreq);
1159 TALLOC_FREE(subreq);
1160 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1161 talloc_set_destructor(state, NULL);
1162 if (ret != 0) {
1163 if (ret != EAGAIN) {
1164 tevent_req_error(req, ret);
1165 return;
1168 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1169 * means the lower level pthreadpool failed to create a new
1170 * thread. Fallback to sync processing in that case to allow
1171 * some progress for the client.
1173 vfs_gluster_pwrite_do(state);
1176 tevent_req_done(req);
1179 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req,
1180 struct vfs_aio_state *vfs_aio_state)
1182 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1183 req, struct vfs_gluster_pwrite_state);
1185 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1186 return -1;
1189 *vfs_aio_state = state->vfs_aio_state;
1191 return state->ret;
1194 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
1195 files_struct *fsp, const void *data,
1196 size_t n, off_t offset)
1198 ssize_t ret;
1199 glfs_fd_t *glfd = NULL;
1201 START_PROFILE_BYTES(syscall_pwrite, n);
1203 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1204 if (glfd == NULL) {
1205 END_PROFILE_BYTES(syscall_pwrite);
1206 DBG_ERR("Failed to fetch gluster fd\n");
1207 return -1;
1210 #ifdef HAVE_GFAPI_VER_7_6
1211 ret = glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
1212 #else
1213 ret = glfs_pwrite(glfd, data, n, offset, 0);
1214 #endif
1215 END_PROFILE_BYTES(syscall_pwrite);
1217 return ret;
1220 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
1221 files_struct *fsp, off_t offset, int whence)
1223 off_t ret = 0;
1224 glfs_fd_t *glfd = NULL;
1226 START_PROFILE(syscall_lseek);
1228 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1229 if (glfd == NULL) {
1230 END_PROFILE(syscall_lseek);
1231 DBG_ERR("Failed to fetch gluster fd\n");
1232 return -1;
1235 ret = glfs_lseek(glfd, offset, whence);
1236 END_PROFILE(syscall_lseek);
1238 return ret;
1241 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
1242 files_struct *fromfsp,
1243 const DATA_BLOB *hdr,
1244 off_t offset, size_t n)
1246 errno = ENOTSUP;
1247 return -1;
1250 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1251 int fromfd, files_struct *tofsp,
1252 off_t offset, size_t n)
1254 errno = ENOTSUP;
1255 return -1;
1258 static int vfs_gluster_renameat(struct vfs_handle_struct *handle,
1259 files_struct *srcfsp,
1260 const struct smb_filename *smb_fname_src,
1261 files_struct *dstfsp,
1262 const struct smb_filename *smb_fname_dst)
1264 int ret;
1266 #ifdef HAVE_GFAPI_VER_7_11
1267 glfs_fd_t *src_pglfd = NULL;
1268 glfs_fd_t *dst_pglfd = NULL;
1270 START_PROFILE(syscall_renameat);
1272 src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
1273 if (src_pglfd == NULL) {
1274 END_PROFILE(syscall_renameat);
1275 DBG_ERR("Failed to fetch gluster fd\n");
1276 return -1;
1279 dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
1280 if (dst_pglfd == NULL) {
1281 END_PROFILE(syscall_renameat);
1282 DBG_ERR("Failed to fetch gluster fd\n");
1283 return -1;
1286 ret = glfs_renameat(src_pglfd, smb_fname_src->base_name,
1287 dst_pglfd, smb_fname_dst->base_name);
1288 #else
1289 struct smb_filename *full_fname_src = NULL;
1290 struct smb_filename *full_fname_dst = NULL;
1292 START_PROFILE(syscall_renameat);
1294 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1295 srcfsp,
1296 smb_fname_src);
1297 if (full_fname_src == NULL) {
1298 END_PROFILE(syscall_renameat);
1299 errno = ENOMEM;
1300 return -1;
1303 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1304 dstfsp,
1305 smb_fname_dst);
1306 if (full_fname_dst == NULL) {
1307 END_PROFILE(syscall_renameat);
1308 TALLOC_FREE(full_fname_src);
1309 errno = ENOMEM;
1310 return -1;
1312 ret = glfs_rename(handle->data,
1313 full_fname_src->base_name,
1314 full_fname_dst->base_name);
1316 TALLOC_FREE(full_fname_src);
1317 TALLOC_FREE(full_fname_dst);
1318 #endif
1320 END_PROFILE(syscall_renameat);
1322 return ret;
1325 struct vfs_gluster_fsync_state {
1326 ssize_t ret;
1327 glfs_fd_t *fd;
1329 struct vfs_aio_state vfs_aio_state;
1330 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1333 static void vfs_gluster_fsync_do(void *private_data);
1334 static void vfs_gluster_fsync_done(struct tevent_req *subreq);
1335 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state);
1337 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1338 *handle, TALLOC_CTX *mem_ctx,
1339 struct tevent_context *ev,
1340 files_struct *fsp)
1342 struct tevent_req *req, *subreq;
1343 struct vfs_gluster_fsync_state *state;
1345 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1346 if (glfd == NULL) {
1347 DBG_ERR("Failed to fetch gluster fd\n");
1348 return NULL;
1351 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_fsync_state);
1352 if (req == NULL) {
1353 return NULL;
1356 state->ret = -1;
1357 state->fd = glfd;
1359 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
1360 state->profile_bytes, 0);
1361 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1363 subreq = pthreadpool_tevent_job_send(
1364 state, ev, handle->conn->sconn->pool, vfs_gluster_fsync_do, state);
1365 if (tevent_req_nomem(subreq, req)) {
1366 return tevent_req_post(req, ev);
1368 tevent_req_set_callback(subreq, vfs_gluster_fsync_done, req);
1370 talloc_set_destructor(state, vfs_gluster_fsync_state_destructor);
1372 return req;
1375 static void vfs_gluster_fsync_do(void *private_data)
1377 struct vfs_gluster_fsync_state *state = talloc_get_type_abort(
1378 private_data, struct vfs_gluster_fsync_state);
1379 struct timespec start_time;
1380 struct timespec end_time;
1382 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1384 PROFILE_TIMESTAMP(&start_time);
1386 do {
1387 #ifdef HAVE_GFAPI_VER_7_6
1388 state->ret = glfs_fsync(state->fd, NULL, NULL);
1389 #else
1390 state->ret = glfs_fsync(state->fd);
1391 #endif
1392 } while ((state->ret == -1) && (errno == EINTR));
1394 if (state->ret == -1) {
1395 state->vfs_aio_state.error = errno;
1398 PROFILE_TIMESTAMP(&end_time);
1400 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1402 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1405 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state)
1407 return -1;
1410 static void vfs_gluster_fsync_done(struct tevent_req *subreq)
1412 struct tevent_req *req = tevent_req_callback_data(
1413 subreq, struct tevent_req);
1414 struct vfs_gluster_fsync_state *state = tevent_req_data(
1415 req, struct vfs_gluster_fsync_state);
1416 int ret;
1418 ret = pthreadpool_tevent_job_recv(subreq);
1419 TALLOC_FREE(subreq);
1420 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1421 talloc_set_destructor(state, NULL);
1422 if (ret != 0) {
1423 if (ret != EAGAIN) {
1424 tevent_req_error(req, ret);
1425 return;
1428 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1429 * means the lower level pthreadpool failed to create a new
1430 * thread. Fallback to sync processing in that case to allow
1431 * some progress for the client.
1433 vfs_gluster_fsync_do(state);
1436 tevent_req_done(req);
1439 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1440 struct vfs_aio_state *vfs_aio_state)
1442 struct vfs_gluster_fsync_state *state = tevent_req_data(
1443 req, struct vfs_gluster_fsync_state);
1445 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1446 return -1;
1449 *vfs_aio_state = state->vfs_aio_state;
1450 return state->ret;
1453 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1454 struct smb_filename *smb_fname)
1456 struct stat st;
1457 int ret;
1459 START_PROFILE(syscall_stat);
1460 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1461 if (ret == 0) {
1462 smb_stat_ex_from_stat(&smb_fname->st, &st);
1464 if (ret < 0 && errno != ENOENT) {
1465 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1466 smb_fname->base_name, strerror(errno)));
1468 END_PROFILE(syscall_stat);
1470 return ret;
1473 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1474 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1476 struct stat st;
1477 int ret;
1478 glfs_fd_t *glfd = NULL;
1480 START_PROFILE(syscall_fstat);
1482 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1483 if (glfd == NULL) {
1484 END_PROFILE(syscall_fstat);
1485 DBG_ERR("Failed to fetch gluster fd\n");
1486 return -1;
1489 ret = glfs_fstat(glfd, &st);
1490 if (ret == 0) {
1491 smb_stat_ex_from_stat(sbuf, &st);
1493 if (ret < 0) {
1494 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1495 fsp_get_io_fd(fsp), strerror(errno)));
1497 END_PROFILE(syscall_fstat);
1499 return ret;
1502 static int vfs_gluster_fstatat(struct vfs_handle_struct *handle,
1503 const struct files_struct *dirfsp,
1504 const struct smb_filename *smb_fname,
1505 SMB_STRUCT_STAT *sbuf,
1506 int flags)
1508 struct stat st;
1509 int ret;
1511 #ifdef HAVE_GFAPI_VER_7_11
1512 glfs_fd_t *pglfd = NULL;
1514 START_PROFILE(syscall_fstatat);
1516 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1517 if (pglfd == NULL) {
1518 END_PROFILE(syscall_fstatat);
1519 DBG_ERR("Failed to fetch gluster fd\n");
1520 return -1;
1523 ret = glfs_fstatat(pglfd, smb_fname->base_name, &st, flags);
1524 #else
1525 struct smb_filename *full_fname = NULL;
1527 START_PROFILE(syscall_fstatat);
1529 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1530 dirfsp,
1531 smb_fname);
1532 if (full_fname == NULL) {
1533 END_PROFILE(syscall_fstatat);
1534 return -1;
1537 ret = glfs_stat(handle->data, full_fname->base_name, &st);
1539 TALLOC_FREE(full_fname->base_name);
1540 #endif
1542 if (ret == 0) {
1543 smb_stat_ex_from_stat(sbuf, &st);
1546 END_PROFILE(syscall_fstatat);
1548 return ret;
1551 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1552 struct smb_filename *smb_fname)
1554 struct stat st;
1555 int ret;
1557 START_PROFILE(syscall_lstat);
1558 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1559 if (ret == 0) {
1560 smb_stat_ex_from_stat(&smb_fname->st, &st);
1562 if (ret < 0 && errno != ENOENT) {
1563 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1564 smb_fname->base_name, strerror(errno)));
1566 END_PROFILE(syscall_lstat);
1568 return ret;
1571 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1572 files_struct *fsp,
1573 const SMB_STRUCT_STAT *sbuf)
1575 uint64_t ret;
1577 START_PROFILE(syscall_get_alloc_size);
1578 ret = sbuf->st_ex_blocks * 512;
1579 END_PROFILE(syscall_get_alloc_size);
1581 return ret;
1584 static int vfs_gluster_unlinkat(struct vfs_handle_struct *handle,
1585 struct files_struct *dirfsp,
1586 const struct smb_filename *smb_fname,
1587 int flags)
1589 int ret;
1591 #ifdef HAVE_GFAPI_VER_7_11
1592 glfs_fd_t *pglfd = NULL;
1594 START_PROFILE(syscall_unlinkat);
1596 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1597 if (pglfd == NULL) {
1598 END_PROFILE(syscall_unlinkat);
1599 DBG_ERR("Failed to fetch gluster fd\n");
1600 return -1;
1603 ret = glfs_unlinkat(pglfd, smb_fname->base_name, flags);
1604 #else
1605 struct smb_filename *full_fname = NULL;
1607 START_PROFILE(syscall_unlinkat);
1609 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1610 dirfsp,
1611 smb_fname);
1612 if (full_fname == NULL) {
1613 END_PROFILE(syscall_unlinkat);
1614 return -1;
1617 if (flags & AT_REMOVEDIR) {
1618 ret = glfs_rmdir(handle->data, full_fname->base_name);
1619 } else {
1620 ret = glfs_unlink(handle->data, full_fname->base_name);
1623 TALLOC_FREE(full_fname);
1624 #endif
1626 END_PROFILE(syscall_unlinkat);
1628 return ret;
1631 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1632 files_struct *fsp, mode_t mode)
1634 int ret;
1635 glfs_fd_t *glfd = NULL;
1637 START_PROFILE(syscall_fchmod);
1639 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1640 if (glfd == NULL) {
1641 END_PROFILE(syscall_fchmod);
1642 DBG_ERR("Failed to fetch gluster fd\n");
1643 return -1;
1646 if (!fsp->fsp_flags.is_pathref) {
1648 * We can use an io_fd to remove xattrs.
1650 ret = glfs_fchmod(glfd, mode);
1651 } else {
1653 * This is no longer a handle based call.
1655 ret = glfs_chmod(handle->data, fsp->fsp_name->base_name, mode);
1657 END_PROFILE(syscall_fchmod);
1659 return ret;
1662 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1663 files_struct *fsp, uid_t uid, gid_t gid)
1665 int ret;
1666 glfs_fd_t *glfd = NULL;
1668 START_PROFILE(syscall_fchown);
1670 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1671 if (glfd == NULL) {
1672 END_PROFILE(syscall_fchown);
1673 DBG_ERR("Failed to fetch gluster fd\n");
1674 return -1;
1677 ret = glfs_fchown(glfd, uid, gid);
1678 END_PROFILE(syscall_fchown);
1680 return ret;
1683 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1684 const struct smb_filename *smb_fname,
1685 uid_t uid,
1686 gid_t gid)
1688 int ret;
1690 START_PROFILE(syscall_lchown);
1691 ret = glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1692 END_PROFILE(syscall_lchown);
1694 return ret;
1697 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1698 const struct smb_filename *smb_fname)
1700 int ret;
1702 START_PROFILE(syscall_chdir);
1703 ret = glfs_chdir(handle->data, smb_fname->base_name);
1704 END_PROFILE(syscall_chdir);
1706 return ret;
1709 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1710 TALLOC_CTX *ctx)
1712 char cwd[PATH_MAX] = { '\0' };
1713 char *ret;
1714 struct smb_filename *smb_fname = NULL;
1716 START_PROFILE(syscall_getwd);
1718 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1719 END_PROFILE(syscall_getwd);
1721 if (ret == NULL) {
1722 return NULL;
1724 smb_fname = synthetic_smb_fname(ctx,
1725 ret,
1726 NULL,
1727 NULL,
1730 return smb_fname;
1733 static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
1734 files_struct *fsp,
1735 struct smb_file_time *ft)
1737 int ret = -1;
1738 struct timespec times[2];
1739 glfs_fd_t *glfd = NULL;
1741 START_PROFILE(syscall_fntimes);
1743 if (is_omit_timespec(&ft->atime)) {
1744 times[0].tv_sec = fsp->fsp_name->st.st_ex_atime.tv_sec;
1745 times[0].tv_nsec = fsp->fsp_name->st.st_ex_atime.tv_nsec;
1746 } else {
1747 times[0].tv_sec = ft->atime.tv_sec;
1748 times[0].tv_nsec = ft->atime.tv_nsec;
1751 if (is_omit_timespec(&ft->mtime)) {
1752 times[1].tv_sec = fsp->fsp_name->st.st_ex_mtime.tv_sec;
1753 times[1].tv_nsec = fsp->fsp_name->st.st_ex_mtime.tv_nsec;
1754 } else {
1755 times[1].tv_sec = ft->mtime.tv_sec;
1756 times[1].tv_nsec = ft->mtime.tv_nsec;
1759 if ((timespec_compare(&times[0],
1760 &fsp->fsp_name->st.st_ex_atime) == 0) &&
1761 (timespec_compare(&times[1],
1762 &fsp->fsp_name->st.st_ex_mtime) == 0)) {
1763 END_PROFILE(syscall_fntimes);
1764 return 0;
1767 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1768 if (glfd == NULL) {
1769 END_PROFILE(syscall_fntimes);
1770 DBG_ERR("Failed to fetch gluster fd\n");
1771 return -1;
1774 if (!fsp->fsp_flags.is_pathref) {
1775 ret = glfs_futimens(glfd, times);
1776 } else {
1777 ret = glfs_utimens(handle->data,
1778 fsp->fsp_name->base_name,
1779 times);
1781 END_PROFILE(syscall_fntimes);
1783 return ret;
1786 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1787 files_struct *fsp, off_t offset)
1789 int ret;
1790 glfs_fd_t *glfd = NULL;
1792 START_PROFILE(syscall_ftruncate);
1794 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1795 if (glfd == NULL) {
1796 END_PROFILE(syscall_ftruncate);
1797 DBG_ERR("Failed to fetch gluster fd\n");
1798 return -1;
1801 #ifdef HAVE_GFAPI_VER_7_6
1802 ret = glfs_ftruncate(glfd, offset, NULL, NULL);
1803 #else
1804 ret = glfs_ftruncate(glfd, offset);
1805 #endif
1806 END_PROFILE(syscall_ftruncate);
1808 return ret;
1811 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1812 struct files_struct *fsp,
1813 uint32_t mode,
1814 off_t offset, off_t len)
1816 int ret;
1817 #ifdef HAVE_GFAPI_VER_6
1818 glfs_fd_t *glfd = NULL;
1819 int keep_size, punch_hole;
1821 START_PROFILE(syscall_fallocate);
1823 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1824 if (glfd == NULL) {
1825 END_PROFILE(syscall_fallocate);
1826 DBG_ERR("Failed to fetch gluster fd\n");
1827 return -1;
1830 keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1831 punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1833 mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1834 if (mode != 0) {
1835 END_PROFILE(syscall_fallocate);
1836 errno = ENOTSUP;
1837 return -1;
1840 if (punch_hole) {
1841 ret = glfs_discard(glfd, offset, len);
1842 if (ret != 0) {
1843 DBG_DEBUG("glfs_discard failed: %s\n",
1844 strerror(errno));
1848 ret = glfs_fallocate(glfd, keep_size, offset, len);
1849 END_PROFILE(syscall_fallocate);
1850 #else
1851 errno = ENOTSUP;
1852 ret = -1;
1853 #endif
1854 return ret;
1857 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1858 TALLOC_CTX *ctx,
1859 const struct smb_filename *smb_fname)
1861 char *result = NULL;
1862 struct smb_filename *result_fname = NULL;
1863 char *resolved_path = NULL;
1865 START_PROFILE(syscall_realpath);
1867 resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1868 if (resolved_path == NULL) {
1869 END_PROFILE(syscall_realpath);
1870 errno = ENOMEM;
1871 return NULL;
1874 result = glfs_realpath(handle->data,
1875 smb_fname->base_name,
1876 resolved_path);
1877 if (result != NULL) {
1878 result_fname = synthetic_smb_fname(ctx,
1879 result,
1880 NULL,
1881 NULL,
1886 SAFE_FREE(resolved_path);
1887 END_PROFILE(syscall_realpath);
1889 return result_fname;
1892 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1893 files_struct *fsp, int op, off_t offset,
1894 off_t count, int type)
1896 struct flock flock = { 0, };
1897 int ret;
1898 glfs_fd_t *glfd = NULL;
1899 bool ok = false;
1901 START_PROFILE(syscall_fcntl_lock);
1903 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1904 if (glfd == NULL) {
1905 DBG_ERR("Failed to fetch gluster fd\n");
1906 ok = false;
1907 goto out;
1910 flock.l_type = type;
1911 flock.l_whence = SEEK_SET;
1912 flock.l_start = offset;
1913 flock.l_len = count;
1914 flock.l_pid = 0;
1916 ret = glfs_posix_lock(glfd, op, &flock);
1918 if (op == F_GETLK) {
1919 /* lock query, true if someone else has locked */
1920 if ((ret != -1) &&
1921 (flock.l_type != F_UNLCK) &&
1922 (flock.l_pid != 0) && (flock.l_pid != getpid())) {
1923 ok = true;
1924 goto out;
1926 /* not me */
1927 ok = false;
1928 goto out;
1931 if (ret == -1) {
1932 ok = false;
1933 goto out;
1936 ok = true;
1937 out:
1938 END_PROFILE(syscall_fcntl_lock);
1940 return ok;
1943 static int vfs_gluster_filesystem_sharemode(struct vfs_handle_struct *handle,
1944 files_struct *fsp,
1945 uint32_t share_access,
1946 uint32_t access_mask)
1948 errno = ENOSYS;
1949 return -1;
1952 static int vfs_gluster_fcntl(vfs_handle_struct *handle,
1953 files_struct *fsp, int cmd, va_list cmd_arg)
1956 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1957 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1959 if (cmd == F_GETFL) {
1960 return 0;
1961 } else if (cmd == F_SETFL) {
1962 va_list dup_cmd_arg;
1963 int opt;
1965 va_copy(dup_cmd_arg, cmd_arg);
1966 opt = va_arg(dup_cmd_arg, int);
1967 va_end(dup_cmd_arg);
1968 if (opt == 0) {
1969 return 0;
1971 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1972 goto err_out;
1974 DBG_ERR("unexpected fcntl: %d\n", cmd);
1975 err_out:
1976 errno = EINVAL;
1977 return -1;
1980 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1981 files_struct *fsp, int leasetype)
1983 errno = ENOSYS;
1984 return -1;
1987 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1988 files_struct *fsp, off_t *poffset,
1989 off_t *pcount, int *ptype, pid_t *ppid)
1991 struct flock flock = { 0, };
1992 int ret;
1993 glfs_fd_t *glfd = NULL;
1995 START_PROFILE(syscall_fcntl_getlock);
1997 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1998 if (glfd == NULL) {
1999 END_PROFILE(syscall_fcntl_getlock);
2000 DBG_ERR("Failed to fetch gluster fd\n");
2001 return false;
2004 flock.l_type = *ptype;
2005 flock.l_whence = SEEK_SET;
2006 flock.l_start = *poffset;
2007 flock.l_len = *pcount;
2008 flock.l_pid = 0;
2010 ret = glfs_posix_lock(glfd, F_GETLK, &flock);
2012 if (ret == -1) {
2013 END_PROFILE(syscall_fcntl_getlock);
2014 return false;
2017 *ptype = flock.l_type;
2018 *poffset = flock.l_start;
2019 *pcount = flock.l_len;
2020 *ppid = flock.l_pid;
2021 END_PROFILE(syscall_fcntl_getlock);
2023 return true;
2026 static int vfs_gluster_symlinkat(struct vfs_handle_struct *handle,
2027 const struct smb_filename *link_target,
2028 struct files_struct *dirfsp,
2029 const struct smb_filename *new_smb_fname)
2031 int ret;
2033 #ifdef HAVE_GFAPI_VER_7_11
2034 glfs_fd_t *pglfd = NULL;
2036 START_PROFILE(syscall_symlinkat);
2038 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2039 if (pglfd == NULL) {
2040 END_PROFILE(syscall_symlinkat);
2041 DBG_ERR("Failed to fetch gluster fd\n");
2042 return -1;
2045 ret = glfs_symlinkat(link_target->base_name,
2046 pglfd,
2047 new_smb_fname->base_name);
2048 #else
2049 struct smb_filename *full_fname = NULL;
2051 START_PROFILE(syscall_symlinkat);
2053 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2054 dirfsp,
2055 new_smb_fname);
2056 if (full_fname == NULL) {
2057 END_PROFILE(syscall_symlinkat);
2058 return -1;
2061 ret = glfs_symlink(handle->data,
2062 link_target->base_name,
2063 full_fname->base_name);
2065 TALLOC_FREE(full_fname);
2066 #endif
2068 END_PROFILE(syscall_symlinkat);
2070 return ret;
2073 static int vfs_gluster_readlinkat(struct vfs_handle_struct *handle,
2074 const struct files_struct *dirfsp,
2075 const struct smb_filename *smb_fname,
2076 char *buf,
2077 size_t bufsiz)
2079 int ret;
2081 #ifdef HAVE_GFAPI_VER_7_11
2082 glfs_fd_t *pglfd = NULL;
2084 START_PROFILE(syscall_readlinkat);
2086 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2087 if (pglfd == NULL) {
2088 END_PROFILE(syscall_readlinkat);
2089 DBG_ERR("Failed to fetch gluster fd\n");
2090 return -1;
2093 ret = glfs_readlinkat(pglfd, smb_fname->base_name, buf, bufsiz);
2094 #else
2095 struct smb_filename *full_fname = NULL;
2097 START_PROFILE(syscall_readlinkat);
2099 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2100 dirfsp,
2101 smb_fname);
2102 if (full_fname == NULL) {
2103 END_PROFILE(syscall_readlinkat);
2104 return -1;
2107 ret = glfs_readlink(handle->data, full_fname->base_name, buf, bufsiz);
2109 TALLOC_FREE(full_fname);
2110 #endif
2112 END_PROFILE(syscall_readlinkat);
2114 return ret;
2117 static int vfs_gluster_linkat(struct vfs_handle_struct *handle,
2118 files_struct *srcfsp,
2119 const struct smb_filename *old_smb_fname,
2120 files_struct *dstfsp,
2121 const struct smb_filename *new_smb_fname,
2122 int flags)
2124 int ret;
2126 #ifdef HAVE_GFAPI_VER_7_11
2127 glfs_fd_t *src_pglfd = NULL;
2128 glfs_fd_t *dst_pglfd = NULL;
2130 START_PROFILE(syscall_linkat);
2132 src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
2133 if (src_pglfd == NULL) {
2134 END_PROFILE(syscall_linkat);
2135 DBG_ERR("Failed to fetch gluster fd\n");
2136 return -1;
2139 dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
2140 if (dst_pglfd == NULL) {
2141 END_PROFILE(syscall_linkat);
2142 DBG_ERR("Failed to fetch gluster fd\n");
2143 return -1;
2146 ret = glfs_linkat(src_pglfd,
2147 old_smb_fname->base_name,
2148 dst_pglfd,
2149 new_smb_fname->base_name,
2150 flags);
2151 #else
2152 struct smb_filename *full_fname_old = NULL;
2153 struct smb_filename *full_fname_new = NULL;
2155 START_PROFILE(syscall_linkat);
2157 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
2158 srcfsp,
2159 old_smb_fname);
2160 if (full_fname_old == NULL) {
2161 END_PROFILE(syscall_linkat);
2162 return -1;
2165 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
2166 dstfsp,
2167 new_smb_fname);
2168 if (full_fname_new == NULL) {
2169 END_PROFILE(syscall_linkat);
2170 TALLOC_FREE(full_fname_old);
2171 return -1;
2174 ret = glfs_link(handle->data,
2175 full_fname_old->base_name,
2176 full_fname_new->base_name);
2178 TALLOC_FREE(full_fname_old);
2179 TALLOC_FREE(full_fname_new);
2180 #endif
2182 END_PROFILE(syscall_linkat);
2184 return ret;
2187 static int vfs_gluster_mknodat(struct vfs_handle_struct *handle,
2188 files_struct *dirfsp,
2189 const struct smb_filename *smb_fname,
2190 mode_t mode,
2191 SMB_DEV_T dev)
2193 int ret;
2195 #ifdef HAVE_GFAPI_VER_7_11
2196 glfs_fd_t *pglfd = NULL;
2198 START_PROFILE(syscall_mknodat);
2200 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2201 if (pglfd == NULL) {
2202 END_PROFILE(syscall_mknodat);
2203 DBG_ERR("Failed to fetch gluster fd\n");
2204 return -1;
2207 ret = glfs_mknodat(pglfd, smb_fname->base_name, mode, dev);
2208 #else
2209 struct smb_filename *full_fname = NULL;
2211 START_PROFILE(syscall_mknodat);
2213 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2214 dirfsp,
2215 smb_fname);
2216 if (full_fname == NULL) {
2217 END_PROFILE(syscall_mknodat);
2218 return -1;
2221 ret = glfs_mknod(handle->data, full_fname->base_name, mode, dev);
2223 TALLOC_FREE(full_fname);
2224 #endif
2226 END_PROFILE(syscall_mknodat);
2228 return ret;
2231 static int vfs_gluster_fchflags(struct vfs_handle_struct *handle,
2232 struct files_struct *fsp,
2233 unsigned int flags)
2235 errno = ENOSYS;
2236 return -1;
2239 static NTSTATUS vfs_gluster_get_real_filename_at(
2240 struct vfs_handle_struct *handle,
2241 struct files_struct *dirfsp,
2242 const char *name,
2243 TALLOC_CTX *mem_ctx,
2244 char **found_name)
2246 int ret;
2247 char key_buf[GLUSTER_NAME_MAX + 64];
2248 char val_buf[GLUSTER_NAME_MAX + 1];
2250 if (strlen(name) >= GLUSTER_NAME_MAX) {
2251 return NT_STATUS_OBJECT_NAME_INVALID;
2254 snprintf(key_buf, GLUSTER_NAME_MAX + 64,
2255 "glusterfs.get_real_filename:%s", name);
2257 ret = glfs_getxattr(handle->data,
2258 dirfsp->fsp_name->base_name,
2259 key_buf,
2260 val_buf,
2261 GLUSTER_NAME_MAX + 1);
2262 if (ret == -1) {
2263 if (errno == ENOATTR) {
2264 errno = ENOENT;
2266 return map_nt_error_from_unix(errno);
2269 *found_name = talloc_strdup(mem_ctx, val_buf);
2270 if (found_name[0] == NULL) {
2271 return NT_STATUS_NO_MEMORY;
2274 return NT_STATUS_OK;
2277 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
2278 const struct smb_filename *smb_fname)
2280 return handle->conn->connectpath;
2283 /* EA Operations */
2285 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
2286 files_struct *fsp, const char *name,
2287 void *value, size_t size)
2289 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2290 if (glfd == NULL) {
2291 DBG_ERR("Failed to fetch gluster fd\n");
2292 return -1;
2295 if (!fsp->fsp_flags.is_pathref) {
2297 * We can use an io_fd to retrieve xattr value.
2299 return glfs_fgetxattr(glfd, name, value, size);
2303 * This is no longer a handle based call.
2305 return glfs_getxattr(handle->data,
2306 fsp->fsp_name->base_name,
2307 name,
2308 value,
2309 size);
2312 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
2313 files_struct *fsp, char *list,
2314 size_t size)
2316 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2317 if (glfd == NULL) {
2318 DBG_ERR("Failed to fetch gluster fd\n");
2319 return -1;
2321 if (!fsp->fsp_flags.is_pathref) {
2323 * We can use an io_fd to list xattrs.
2325 return glfs_flistxattr(glfd, list, size);
2326 } else {
2328 * This is no longer a handle based call.
2330 return glfs_listxattr(handle->data,
2331 fsp->fsp_name->base_name,
2332 list,
2333 size);
2337 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
2338 files_struct *fsp, const char *name)
2340 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2341 if (glfd == NULL) {
2342 DBG_ERR("Failed to fetch gluster fd\n");
2343 return -1;
2345 if (!fsp->fsp_flags.is_pathref) {
2347 * We can use an io_fd to remove xattrs.
2349 return glfs_fremovexattr(glfd, name);
2350 } else {
2352 * This is no longer a handle based call.
2354 return glfs_removexattr(handle->data,
2355 fsp->fsp_name->base_name,
2356 name);
2360 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
2361 files_struct *fsp, const char *name,
2362 const void *value, size_t size, int flags)
2364 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2365 if (glfd == NULL) {
2366 DBG_ERR("Failed to fetch gluster fd\n");
2367 return -1;
2370 if (!fsp->fsp_flags.is_pathref) {
2372 * We can use an io_fd to set xattrs.
2374 return glfs_fsetxattr(glfd, name, value, size, flags);
2375 } else {
2377 * This is no longer a handle based call.
2379 return glfs_setxattr(handle->data,
2380 fsp->fsp_name->base_name,
2381 name,
2382 value,
2383 size,
2384 flags);
2388 /* AIO Operations */
2390 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
2391 files_struct *fsp)
2393 return false;
2396 static NTSTATUS vfs_gluster_create_dfs_pathat(struct vfs_handle_struct *handle,
2397 struct files_struct *dirfsp,
2398 const struct smb_filename *smb_fname,
2399 const struct referral *reflist,
2400 size_t referral_count)
2402 TALLOC_CTX *frame = talloc_stackframe();
2403 NTSTATUS status = NT_STATUS_NO_MEMORY;
2404 int ret;
2405 char *msdfs_link = NULL;
2406 #ifdef HAVE_GFAPI_VER_7_11
2407 glfs_fd_t *pglfd = NULL;
2408 #else
2409 struct smb_filename *full_fname = NULL;
2410 #endif
2412 /* Form the msdfs_link contents */
2413 msdfs_link = msdfs_link_string(frame,
2414 reflist,
2415 referral_count);
2416 if (msdfs_link == NULL) {
2417 goto out;
2420 #ifdef HAVE_GFAPI_VER_7_11
2421 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2422 if (pglfd == NULL) {
2423 DBG_ERR("Failed to fetch gluster fd\n");
2424 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2425 goto out;
2428 ret = glfs_symlinkat(msdfs_link, pglfd, smb_fname->base_name);
2429 #else
2430 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2431 dirfsp,
2432 smb_fname);
2433 if (full_fname == NULL) {
2434 goto out;
2437 ret = glfs_symlink(handle->data, msdfs_link, full_fname->base_name);
2439 TALLOC_FREE(full_fname);
2440 #endif
2441 if (ret == 0) {
2442 status = NT_STATUS_OK;
2443 } else {
2444 status = map_nt_error_from_unix(errno);
2447 out:
2449 TALLOC_FREE(frame);
2450 return status;
2454 * Read and return the contents of a DFS redirect given a
2455 * pathname. A caller can pass in NULL for ppreflist and
2456 * preferral_count but still determine if this was a
2457 * DFS redirect point by getting NT_STATUS_OK back
2458 * without incurring the overhead of reading and parsing
2459 * the referral contents.
2462 static NTSTATUS vfs_gluster_read_dfs_pathat(struct vfs_handle_struct *handle,
2463 TALLOC_CTX *mem_ctx,
2464 struct files_struct *dirfsp,
2465 struct smb_filename *smb_fname,
2466 struct referral **ppreflist,
2467 size_t *preferral_count)
2469 NTSTATUS status = NT_STATUS_NO_MEMORY;
2470 size_t bufsize;
2471 char *link_target = NULL;
2472 int referral_len;
2473 bool ok;
2474 #if defined(HAVE_BROKEN_READLINK)
2475 char link_target_buf[PATH_MAX];
2476 #else
2477 char link_target_buf[7];
2478 #endif
2479 struct stat st;
2480 struct smb_filename *full_fname = NULL;
2481 int ret;
2482 #ifdef HAVE_GFAPI_VER_7_11
2483 glfs_fd_t *pglfd = NULL;
2484 #endif
2486 if (is_named_stream(smb_fname)) {
2487 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2488 goto err;
2491 if (ppreflist == NULL && preferral_count == NULL) {
2493 * We're only checking if this is a DFS
2494 * redirect. We don't need to return data.
2496 bufsize = sizeof(link_target_buf);
2497 link_target = link_target_buf;
2498 } else {
2499 bufsize = PATH_MAX;
2500 link_target = talloc_array(mem_ctx, char, bufsize);
2501 if (!link_target) {
2502 goto err;
2506 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2507 dirfsp,
2508 smb_fname);
2509 if (full_fname == NULL) {
2510 status = NT_STATUS_NO_MEMORY;
2511 goto err;
2514 ret = glfs_lstat(handle->data, full_fname->base_name, &st);
2515 if (ret < 0) {
2516 status = map_nt_error_from_unix(errno);
2517 goto err;
2520 #ifdef HAVE_GFAPI_VER_7_11
2521 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2522 if (pglfd == NULL) {
2523 DBG_ERR("Failed to fetch gluster fd\n");
2524 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2527 referral_len = glfs_readlinkat(pglfd,
2528 smb_fname->base_name,
2529 link_target,
2530 bufsize - 1);
2531 #else
2532 referral_len = glfs_readlink(handle->data,
2533 full_fname->base_name,
2534 link_target,
2535 bufsize - 1);
2536 #endif
2537 if (referral_len < 0) {
2538 if (errno == EINVAL) {
2539 DBG_INFO("%s is not a link.\n", full_fname->base_name);
2540 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2541 } else {
2542 status = map_nt_error_from_unix(errno);
2543 DBG_ERR("Error reading "
2544 "msdfs link %s: %s\n",
2545 full_fname->base_name,
2546 strerror(errno));
2548 goto err;
2550 link_target[referral_len] = '\0';
2552 DBG_INFO("%s -> %s\n",
2553 full_fname->base_name,
2554 link_target);
2556 if (!strnequal(link_target, "msdfs:", 6)) {
2557 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2558 goto err;
2561 if (ppreflist == NULL && preferral_count == NULL) {
2562 /* Early return for checking if this is a DFS link. */
2563 TALLOC_FREE(full_fname);
2564 smb_stat_ex_from_stat(&smb_fname->st, &st);
2565 return NT_STATUS_OK;
2568 ok = parse_msdfs_symlink(mem_ctx,
2569 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
2570 link_target,
2571 ppreflist,
2572 preferral_count);
2574 if (ok) {
2575 smb_stat_ex_from_stat(&smb_fname->st, &st);
2576 status = NT_STATUS_OK;
2577 } else {
2578 status = NT_STATUS_NO_MEMORY;
2581 err:
2583 if (link_target != link_target_buf) {
2584 TALLOC_FREE(link_target);
2586 TALLOC_FREE(full_fname);
2587 return status;
2590 static struct vfs_fn_pointers glusterfs_fns = {
2592 /* Disk Operations */
2594 .connect_fn = vfs_gluster_connect,
2595 .disconnect_fn = vfs_gluster_disconnect,
2596 .disk_free_fn = vfs_gluster_disk_free,
2597 .get_quota_fn = vfs_gluster_get_quota,
2598 .set_quota_fn = vfs_gluster_set_quota,
2599 .statvfs_fn = vfs_gluster_statvfs,
2600 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
2602 .get_dfs_referrals_fn = NULL,
2604 /* Directory Operations */
2606 .fdopendir_fn = vfs_gluster_fdopendir,
2607 .readdir_fn = vfs_gluster_readdir,
2608 .seekdir_fn = vfs_gluster_seekdir,
2609 .telldir_fn = vfs_gluster_telldir,
2610 .rewind_dir_fn = vfs_gluster_rewinddir,
2611 .mkdirat_fn = vfs_gluster_mkdirat,
2612 .closedir_fn = vfs_gluster_closedir,
2614 /* File Operations */
2616 .openat_fn = vfs_gluster_openat,
2617 .create_file_fn = NULL,
2618 .close_fn = vfs_gluster_close,
2619 .pread_fn = vfs_gluster_pread,
2620 .pread_send_fn = vfs_gluster_pread_send,
2621 .pread_recv_fn = vfs_gluster_pread_recv,
2622 .pwrite_fn = vfs_gluster_pwrite,
2623 .pwrite_send_fn = vfs_gluster_pwrite_send,
2624 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
2625 .lseek_fn = vfs_gluster_lseek,
2626 .sendfile_fn = vfs_gluster_sendfile,
2627 .recvfile_fn = vfs_gluster_recvfile,
2628 .renameat_fn = vfs_gluster_renameat,
2629 .fsync_send_fn = vfs_gluster_fsync_send,
2630 .fsync_recv_fn = vfs_gluster_fsync_recv,
2632 .stat_fn = vfs_gluster_stat,
2633 .fstat_fn = vfs_gluster_fstat,
2634 .fstatat_fn = vfs_gluster_fstatat,
2635 .lstat_fn = vfs_gluster_lstat,
2636 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
2637 .unlinkat_fn = vfs_gluster_unlinkat,
2639 .fchmod_fn = vfs_gluster_fchmod,
2640 .fchown_fn = vfs_gluster_fchown,
2641 .lchown_fn = vfs_gluster_lchown,
2642 .chdir_fn = vfs_gluster_chdir,
2643 .getwd_fn = vfs_gluster_getwd,
2644 .fntimes_fn = vfs_gluster_fntimes,
2645 .ftruncate_fn = vfs_gluster_ftruncate,
2646 .fallocate_fn = vfs_gluster_fallocate,
2647 .lock_fn = vfs_gluster_lock,
2648 .filesystem_sharemode_fn = vfs_gluster_filesystem_sharemode,
2649 .fcntl_fn = vfs_gluster_fcntl,
2650 .linux_setlease_fn = vfs_gluster_linux_setlease,
2651 .getlock_fn = vfs_gluster_getlock,
2652 .symlinkat_fn = vfs_gluster_symlinkat,
2653 .readlinkat_fn = vfs_gluster_readlinkat,
2654 .linkat_fn = vfs_gluster_linkat,
2655 .mknodat_fn = vfs_gluster_mknodat,
2656 .realpath_fn = vfs_gluster_realpath,
2657 .fchflags_fn = vfs_gluster_fchflags,
2658 .file_id_create_fn = NULL,
2659 .fstreaminfo_fn = NULL,
2660 .get_real_filename_at_fn = vfs_gluster_get_real_filename_at,
2661 .connectpath_fn = vfs_gluster_connectpath,
2662 .create_dfs_pathat_fn = vfs_gluster_create_dfs_pathat,
2663 .read_dfs_pathat_fn = vfs_gluster_read_dfs_pathat,
2665 .brl_lock_windows_fn = NULL,
2666 .brl_unlock_windows_fn = NULL,
2667 .strict_lock_check_fn = NULL,
2668 .translate_name_fn = NULL,
2669 .fsctl_fn = NULL,
2671 /* NT ACL Operations */
2672 .fget_nt_acl_fn = NULL,
2673 .fset_nt_acl_fn = NULL,
2674 .audit_file_fn = NULL,
2676 /* Posix ACL Operations */
2677 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
2678 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2679 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
2680 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
2682 /* EA Operations */
2683 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2684 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2685 .fgetxattr_fn = vfs_gluster_fgetxattr,
2686 .flistxattr_fn = vfs_gluster_flistxattr,
2687 .fremovexattr_fn = vfs_gluster_fremovexattr,
2688 .fsetxattr_fn = vfs_gluster_fsetxattr,
2690 /* AIO Operations */
2691 .aio_force_fn = vfs_gluster_aio_force,
2693 /* Durable handle Operations */
2694 .durable_cookie_fn = NULL,
2695 .durable_disconnect_fn = NULL,
2696 .durable_reconnect_fn = NULL,
2699 static_decl_vfs;
2700 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
2702 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2703 "glusterfs", &glusterfs_fns);