vfs_glusterfs: Use glfs_readlinkat() for SMB_VFS_READLINKAT
[Samba.git] / source3 / modules / vfs_glusterfs.c
blob1f2aab256ede19769238a86f2f5d9cf6c73c6a6e
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;
629 struct smb_filename *full_fname = NULL;
630 struct smb_filename *smb_fname_dot = NULL;
632 smb_fname_dot = synthetic_smb_fname(fsp->fsp_name,
633 ".",
634 NULL,
635 NULL,
639 if (smb_fname_dot == NULL) {
640 return NULL;
643 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
644 fsp,
645 smb_fname_dot);
646 if (full_fname == NULL) {
647 TALLOC_FREE(smb_fname_dot);
648 return NULL;
651 glfd = glfs_opendir(handle->data, full_fname->base_name);
652 if (glfd == NULL) {
653 TALLOC_FREE(full_fname);
654 TALLOC_FREE(smb_fname_dot);
655 return NULL;
658 TALLOC_FREE(full_fname);
659 TALLOC_FREE(smb_fname_dot);
661 return (DIR *)glfd;
664 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
666 int ret;
668 START_PROFILE(syscall_closedir);
669 ret = glfs_closedir((void *)dirp);
670 END_PROFILE(syscall_closedir);
672 return ret;
675 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
676 struct files_struct *dirfsp,
677 DIR *dirp,
678 SMB_STRUCT_STAT *sbuf)
680 static char direntbuf[512];
681 int ret;
682 struct stat stat;
683 struct dirent *dirent = 0;
685 START_PROFILE(syscall_readdir);
686 if (sbuf != NULL) {
687 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
688 &dirent);
689 } else {
690 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
693 if ((ret < 0) || (dirent == NULL)) {
694 END_PROFILE(syscall_readdir);
695 return NULL;
698 if (sbuf != NULL) {
699 SET_STAT_INVALID(*sbuf);
700 if (!S_ISLNK(stat.st_mode)) {
701 smb_stat_ex_from_stat(sbuf, &stat);
705 END_PROFILE(syscall_readdir);
706 return dirent;
709 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
711 long ret;
713 START_PROFILE(syscall_telldir);
714 ret = glfs_telldir((void *)dirp);
715 END_PROFILE(syscall_telldir);
717 return ret;
720 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
721 long offset)
723 START_PROFILE(syscall_seekdir);
724 glfs_seekdir((void *)dirp, offset);
725 END_PROFILE(syscall_seekdir);
728 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
730 START_PROFILE(syscall_rewinddir);
731 glfs_seekdir((void *)dirp, 0);
732 END_PROFILE(syscall_rewinddir);
735 static int vfs_gluster_mkdirat(struct vfs_handle_struct *handle,
736 struct files_struct *dirfsp,
737 const struct smb_filename *smb_fname,
738 mode_t mode)
740 int ret;
742 #ifdef HAVE_GFAPI_VER_7_11
743 glfs_fd_t *pglfd = NULL;
745 START_PROFILE(syscall_mkdirat);
747 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
748 if (pglfd == NULL) {
749 END_PROFILE(syscall_mkdirat);
750 DBG_ERR("Failed to fetch gluster fd\n");
751 return -1;
754 ret = glfs_mkdirat(pglfd, smb_fname->base_name, mode);
755 #else
756 struct smb_filename *full_fname = NULL;
758 START_PROFILE(syscall_mkdirat);
760 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
761 dirfsp,
762 smb_fname);
763 if (full_fname == NULL) {
764 END_PROFILE(syscall_mkdirat);
765 return -1;
768 ret = glfs_mkdir(handle->data, full_fname->base_name, mode);
770 TALLOC_FREE(full_fname);
771 #endif
773 END_PROFILE(syscall_mkdirat);
775 return ret;
778 static int vfs_gluster_openat(struct vfs_handle_struct *handle,
779 const struct files_struct *dirfsp,
780 const struct smb_filename *smb_fname,
781 files_struct *fsp,
782 const struct vfs_open_how *how)
784 int flags = how->flags;
785 struct smb_filename *full_fname = NULL;
786 bool have_opath = false;
787 bool became_root = false;
788 glfs_fd_t *glfd;
789 glfs_fd_t *pglfd = NULL;
790 glfs_fd_t **p_tmp;
792 START_PROFILE(syscall_openat);
794 if (how->resolve != 0) {
795 END_PROFILE(syscall_openat);
796 errno = ENOSYS;
797 return -1;
800 p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
801 if (p_tmp == NULL) {
802 END_PROFILE(syscall_openat);
803 errno = ENOMEM;
804 return -1;
807 #ifdef O_PATH
808 have_opath = true;
809 if (fsp->fsp_flags.is_pathref) {
810 flags |= O_PATH;
812 #endif
814 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
815 dirfsp,
816 smb_fname);
817 if (full_fname == NULL) {
818 END_PROFILE(syscall_openat);
819 return -1;
822 if (fsp->fsp_flags.is_pathref && !have_opath) {
823 become_root();
824 became_root = true;
828 * O_CREAT flag in open is handled differently in a way which is *NOT*
829 * safe against symlink race situations. We use glfs_creat() instead
830 * for correctness as glfs_openat() is broken with O_CREAT present
831 * in open flags.
833 if (flags & O_CREAT) {
834 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
836 * Replace smb_fname with full_path constructed above.
838 smb_fname = full_fname;
842 * smb_fname can either be a full_path or the same one
843 * as received from the caller. In the latter case we
844 * are operating at current working directory.
846 glfd = glfs_creat(handle->data,
847 smb_fname->base_name,
848 flags,
849 how->mode);
850 } else {
851 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
852 #ifdef HAVE_GFAPI_VER_7_11
854 * Fetch Gluster fd for parent directory using dirfsp
855 * before calling glfs_openat();
857 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
858 if (pglfd == NULL) {
859 END_PROFILE(syscall_openat);
860 DBG_ERR("Failed to fetch gluster fd\n");
861 return -1;
864 glfd = glfs_openat(pglfd,
865 smb_fname->base_name,
866 flags,
867 how->mode);
868 #else
870 * Replace smb_fname with full_path constructed above.
872 smb_fname = full_fname;
873 #endif
876 if (pglfd == NULL) {
878 * smb_fname can either be a full_path or the same one
879 * as received from the caller. In the latter case we
880 * are operating at current working directory.
882 glfd = glfs_open(handle->data,
883 smb_fname->base_name,
884 flags);
888 if (became_root) {
889 unbecome_root();
892 TALLOC_FREE(full_fname);
894 fsp->fsp_flags.have_proc_fds = false;
896 if (glfd == NULL) {
897 END_PROFILE(syscall_openat);
898 /* no extension destroy_fn, so no need to save errno */
899 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
900 return -1;
903 *p_tmp = glfd;
905 END_PROFILE(syscall_openat);
906 /* An arbitrary value for error reporting, so you know its us. */
907 return 13371337;
910 static int vfs_gluster_close(struct vfs_handle_struct *handle,
911 files_struct *fsp)
913 int ret;
914 glfs_fd_t *glfd = NULL;
916 START_PROFILE(syscall_close);
918 glfd = vfs_gluster_fetch_glfd(handle, fsp);
919 if (glfd == NULL) {
920 END_PROFILE(syscall_close);
921 DBG_ERR("Failed to fetch gluster fd\n");
922 return -1;
925 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
927 ret = glfs_close(glfd);
928 END_PROFILE(syscall_close);
930 return ret;
933 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
934 files_struct *fsp, void *data, size_t n,
935 off_t offset)
937 ssize_t ret;
938 glfs_fd_t *glfd = NULL;
940 START_PROFILE_BYTES(syscall_pread, n);
942 glfd = vfs_gluster_fetch_glfd(handle, fsp);
943 if (glfd == NULL) {
944 END_PROFILE_BYTES(syscall_pread);
945 DBG_ERR("Failed to fetch gluster fd\n");
946 return -1;
949 #ifdef HAVE_GFAPI_VER_7_6
950 ret = glfs_pread(glfd, data, n, offset, 0, NULL);
951 #else
952 ret = glfs_pread(glfd, data, n, offset, 0);
953 #endif
954 END_PROFILE_BYTES(syscall_pread);
956 return ret;
959 struct vfs_gluster_pread_state {
960 ssize_t ret;
961 glfs_fd_t *fd;
962 void *buf;
963 size_t count;
964 off_t offset;
966 struct vfs_aio_state vfs_aio_state;
967 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
970 static void vfs_gluster_pread_do(void *private_data);
971 static void vfs_gluster_pread_done(struct tevent_req *subreq);
972 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state);
974 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
975 *handle, TALLOC_CTX *mem_ctx,
976 struct tevent_context *ev,
977 files_struct *fsp,
978 void *data, size_t n,
979 off_t offset)
981 struct vfs_gluster_pread_state *state;
982 struct tevent_req *req, *subreq;
984 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
985 if (glfd == NULL) {
986 DBG_ERR("Failed to fetch gluster fd\n");
987 return NULL;
990 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pread_state);
991 if (req == NULL) {
992 return NULL;
995 state->ret = -1;
996 state->fd = glfd;
997 state->buf = data;
998 state->count = n;
999 state->offset = offset;
1001 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
1002 state->profile_bytes, n);
1003 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1005 subreq = pthreadpool_tevent_job_send(
1006 state, ev, handle->conn->sconn->pool,
1007 vfs_gluster_pread_do, state);
1008 if (tevent_req_nomem(subreq, req)) {
1009 return tevent_req_post(req, ev);
1011 tevent_req_set_callback(subreq, vfs_gluster_pread_done, req);
1013 talloc_set_destructor(state, vfs_gluster_pread_state_destructor);
1015 return req;
1018 static void vfs_gluster_pread_do(void *private_data)
1020 struct vfs_gluster_pread_state *state = talloc_get_type_abort(
1021 private_data, struct vfs_gluster_pread_state);
1022 struct timespec start_time;
1023 struct timespec end_time;
1025 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1027 PROFILE_TIMESTAMP(&start_time);
1029 do {
1030 #ifdef HAVE_GFAPI_VER_7_6
1031 state->ret = glfs_pread(state->fd, state->buf, state->count,
1032 state->offset, 0, NULL);
1033 #else
1034 state->ret = glfs_pread(state->fd, state->buf, state->count,
1035 state->offset, 0);
1036 #endif
1037 } while ((state->ret == -1) && (errno == EINTR));
1039 if (state->ret == -1) {
1040 state->vfs_aio_state.error = errno;
1043 PROFILE_TIMESTAMP(&end_time);
1045 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1047 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1050 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state)
1052 return -1;
1055 static void vfs_gluster_pread_done(struct tevent_req *subreq)
1057 struct tevent_req *req = tevent_req_callback_data(
1058 subreq, struct tevent_req);
1059 struct vfs_gluster_pread_state *state = tevent_req_data(
1060 req, struct vfs_gluster_pread_state);
1061 int ret;
1063 ret = pthreadpool_tevent_job_recv(subreq);
1064 TALLOC_FREE(subreq);
1065 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1066 talloc_set_destructor(state, NULL);
1067 if (ret != 0) {
1068 if (ret != EAGAIN) {
1069 tevent_req_error(req, ret);
1070 return;
1073 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1074 * means the lower level pthreadpool failed to create a new
1075 * thread. Fallback to sync processing in that case to allow
1076 * some progress for the client.
1078 vfs_gluster_pread_do(state);
1081 tevent_req_done(req);
1084 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req,
1085 struct vfs_aio_state *vfs_aio_state)
1087 struct vfs_gluster_pread_state *state = tevent_req_data(
1088 req, struct vfs_gluster_pread_state);
1090 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1091 return -1;
1094 *vfs_aio_state = state->vfs_aio_state;
1095 return state->ret;
1098 struct vfs_gluster_pwrite_state {
1099 ssize_t ret;
1100 glfs_fd_t *fd;
1101 const void *buf;
1102 size_t count;
1103 off_t offset;
1105 struct vfs_aio_state vfs_aio_state;
1106 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1109 static void vfs_gluster_pwrite_do(void *private_data);
1110 static void vfs_gluster_pwrite_done(struct tevent_req *subreq);
1111 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state);
1113 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
1114 *handle, TALLOC_CTX *mem_ctx,
1115 struct tevent_context *ev,
1116 files_struct *fsp,
1117 const void *data, size_t n,
1118 off_t offset)
1120 struct tevent_req *req, *subreq;
1121 struct vfs_gluster_pwrite_state *state;
1123 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1124 if (glfd == NULL) {
1125 DBG_ERR("Failed to fetch gluster fd\n");
1126 return NULL;
1129 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pwrite_state);
1130 if (req == NULL) {
1131 return NULL;
1134 state->ret = -1;
1135 state->fd = glfd;
1136 state->buf = data;
1137 state->count = n;
1138 state->offset = offset;
1140 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
1141 state->profile_bytes, n);
1142 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1144 subreq = pthreadpool_tevent_job_send(
1145 state, ev, handle->conn->sconn->pool,
1146 vfs_gluster_pwrite_do, state);
1147 if (tevent_req_nomem(subreq, req)) {
1148 return tevent_req_post(req, ev);
1150 tevent_req_set_callback(subreq, vfs_gluster_pwrite_done, req);
1152 talloc_set_destructor(state, vfs_gluster_pwrite_state_destructor);
1154 return req;
1157 static void vfs_gluster_pwrite_do(void *private_data)
1159 struct vfs_gluster_pwrite_state *state = talloc_get_type_abort(
1160 private_data, struct vfs_gluster_pwrite_state);
1161 struct timespec start_time;
1162 struct timespec end_time;
1164 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1166 PROFILE_TIMESTAMP(&start_time);
1168 do {
1169 #ifdef HAVE_GFAPI_VER_7_6
1170 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1171 state->offset, 0, NULL, NULL);
1172 #else
1173 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1174 state->offset, 0);
1175 #endif
1176 } while ((state->ret == -1) && (errno == EINTR));
1178 if (state->ret == -1) {
1179 state->vfs_aio_state.error = errno;
1182 PROFILE_TIMESTAMP(&end_time);
1184 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1186 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1189 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state)
1191 return -1;
1194 static void vfs_gluster_pwrite_done(struct tevent_req *subreq)
1196 struct tevent_req *req = tevent_req_callback_data(
1197 subreq, struct tevent_req);
1198 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1199 req, struct vfs_gluster_pwrite_state);
1200 int ret;
1202 ret = pthreadpool_tevent_job_recv(subreq);
1203 TALLOC_FREE(subreq);
1204 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1205 talloc_set_destructor(state, NULL);
1206 if (ret != 0) {
1207 if (ret != EAGAIN) {
1208 tevent_req_error(req, ret);
1209 return;
1212 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1213 * means the lower level pthreadpool failed to create a new
1214 * thread. Fallback to sync processing in that case to allow
1215 * some progress for the client.
1217 vfs_gluster_pwrite_do(state);
1220 tevent_req_done(req);
1223 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req,
1224 struct vfs_aio_state *vfs_aio_state)
1226 struct vfs_gluster_pwrite_state *state = tevent_req_data(
1227 req, struct vfs_gluster_pwrite_state);
1229 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1230 return -1;
1233 *vfs_aio_state = state->vfs_aio_state;
1235 return state->ret;
1238 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
1239 files_struct *fsp, const void *data,
1240 size_t n, off_t offset)
1242 ssize_t ret;
1243 glfs_fd_t *glfd = NULL;
1245 START_PROFILE_BYTES(syscall_pwrite, n);
1247 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1248 if (glfd == NULL) {
1249 END_PROFILE_BYTES(syscall_pwrite);
1250 DBG_ERR("Failed to fetch gluster fd\n");
1251 return -1;
1254 #ifdef HAVE_GFAPI_VER_7_6
1255 ret = glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
1256 #else
1257 ret = glfs_pwrite(glfd, data, n, offset, 0);
1258 #endif
1259 END_PROFILE_BYTES(syscall_pwrite);
1261 return ret;
1264 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
1265 files_struct *fsp, off_t offset, int whence)
1267 off_t ret = 0;
1268 glfs_fd_t *glfd = NULL;
1270 START_PROFILE(syscall_lseek);
1272 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1273 if (glfd == NULL) {
1274 END_PROFILE(syscall_lseek);
1275 DBG_ERR("Failed to fetch gluster fd\n");
1276 return -1;
1279 ret = glfs_lseek(glfd, offset, whence);
1280 END_PROFILE(syscall_lseek);
1282 return ret;
1285 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
1286 files_struct *fromfsp,
1287 const DATA_BLOB *hdr,
1288 off_t offset, size_t n)
1290 errno = ENOTSUP;
1291 return -1;
1294 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1295 int fromfd, files_struct *tofsp,
1296 off_t offset, size_t n)
1298 errno = ENOTSUP;
1299 return -1;
1302 static int vfs_gluster_renameat(struct vfs_handle_struct *handle,
1303 files_struct *srcfsp,
1304 const struct smb_filename *smb_fname_src,
1305 files_struct *dstfsp,
1306 const struct smb_filename *smb_fname_dst)
1308 int ret;
1310 #ifdef HAVE_GFAPI_VER_7_11
1311 glfs_fd_t *src_pglfd = NULL;
1312 glfs_fd_t *dst_pglfd = NULL;
1314 START_PROFILE(syscall_renameat);
1316 src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
1317 if (src_pglfd == NULL) {
1318 END_PROFILE(syscall_renameat);
1319 DBG_ERR("Failed to fetch gluster fd\n");
1320 return -1;
1323 dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
1324 if (dst_pglfd == NULL) {
1325 END_PROFILE(syscall_renameat);
1326 DBG_ERR("Failed to fetch gluster fd\n");
1327 return -1;
1330 ret = glfs_renameat(src_pglfd, smb_fname_src->base_name,
1331 dst_pglfd, smb_fname_dst->base_name);
1332 #else
1333 struct smb_filename *full_fname_src = NULL;
1334 struct smb_filename *full_fname_dst = NULL;
1336 START_PROFILE(syscall_renameat);
1338 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1339 srcfsp,
1340 smb_fname_src);
1341 if (full_fname_src == NULL) {
1342 END_PROFILE(syscall_renameat);
1343 errno = ENOMEM;
1344 return -1;
1347 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1348 dstfsp,
1349 smb_fname_dst);
1350 if (full_fname_dst == NULL) {
1351 END_PROFILE(syscall_renameat);
1352 TALLOC_FREE(full_fname_src);
1353 errno = ENOMEM;
1354 return -1;
1356 ret = glfs_rename(handle->data,
1357 full_fname_src->base_name,
1358 full_fname_dst->base_name);
1360 TALLOC_FREE(full_fname_src);
1361 TALLOC_FREE(full_fname_dst);
1362 #endif
1364 END_PROFILE(syscall_renameat);
1366 return ret;
1369 struct vfs_gluster_fsync_state {
1370 ssize_t ret;
1371 glfs_fd_t *fd;
1373 struct vfs_aio_state vfs_aio_state;
1374 SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1377 static void vfs_gluster_fsync_do(void *private_data);
1378 static void vfs_gluster_fsync_done(struct tevent_req *subreq);
1379 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state);
1381 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1382 *handle, TALLOC_CTX *mem_ctx,
1383 struct tevent_context *ev,
1384 files_struct *fsp)
1386 struct tevent_req *req, *subreq;
1387 struct vfs_gluster_fsync_state *state;
1389 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1390 if (glfd == NULL) {
1391 DBG_ERR("Failed to fetch gluster fd\n");
1392 return NULL;
1395 req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_fsync_state);
1396 if (req == NULL) {
1397 return NULL;
1400 state->ret = -1;
1401 state->fd = glfd;
1403 SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
1404 state->profile_bytes, 0);
1405 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1407 subreq = pthreadpool_tevent_job_send(
1408 state, ev, handle->conn->sconn->pool, vfs_gluster_fsync_do, state);
1409 if (tevent_req_nomem(subreq, req)) {
1410 return tevent_req_post(req, ev);
1412 tevent_req_set_callback(subreq, vfs_gluster_fsync_done, req);
1414 talloc_set_destructor(state, vfs_gluster_fsync_state_destructor);
1416 return req;
1419 static void vfs_gluster_fsync_do(void *private_data)
1421 struct vfs_gluster_fsync_state *state = talloc_get_type_abort(
1422 private_data, struct vfs_gluster_fsync_state);
1423 struct timespec start_time;
1424 struct timespec end_time;
1426 SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1428 PROFILE_TIMESTAMP(&start_time);
1430 do {
1431 #ifdef HAVE_GFAPI_VER_7_6
1432 state->ret = glfs_fsync(state->fd, NULL, NULL);
1433 #else
1434 state->ret = glfs_fsync(state->fd);
1435 #endif
1436 } while ((state->ret == -1) && (errno == EINTR));
1438 if (state->ret == -1) {
1439 state->vfs_aio_state.error = errno;
1442 PROFILE_TIMESTAMP(&end_time);
1444 state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1446 SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1449 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state)
1451 return -1;
1454 static void vfs_gluster_fsync_done(struct tevent_req *subreq)
1456 struct tevent_req *req = tevent_req_callback_data(
1457 subreq, struct tevent_req);
1458 struct vfs_gluster_fsync_state *state = tevent_req_data(
1459 req, struct vfs_gluster_fsync_state);
1460 int ret;
1462 ret = pthreadpool_tevent_job_recv(subreq);
1463 TALLOC_FREE(subreq);
1464 SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1465 talloc_set_destructor(state, NULL);
1466 if (ret != 0) {
1467 if (ret != EAGAIN) {
1468 tevent_req_error(req, ret);
1469 return;
1472 * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1473 * means the lower level pthreadpool failed to create a new
1474 * thread. Fallback to sync processing in that case to allow
1475 * some progress for the client.
1477 vfs_gluster_fsync_do(state);
1480 tevent_req_done(req);
1483 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1484 struct vfs_aio_state *vfs_aio_state)
1486 struct vfs_gluster_fsync_state *state = tevent_req_data(
1487 req, struct vfs_gluster_fsync_state);
1489 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1490 return -1;
1493 *vfs_aio_state = state->vfs_aio_state;
1494 return state->ret;
1497 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1498 struct smb_filename *smb_fname)
1500 struct stat st;
1501 int ret;
1503 START_PROFILE(syscall_stat);
1504 ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1505 if (ret == 0) {
1506 smb_stat_ex_from_stat(&smb_fname->st, &st);
1508 if (ret < 0 && errno != ENOENT) {
1509 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1510 smb_fname->base_name, strerror(errno)));
1512 END_PROFILE(syscall_stat);
1514 return ret;
1517 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1518 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1520 struct stat st;
1521 int ret;
1522 glfs_fd_t *glfd = NULL;
1524 START_PROFILE(syscall_fstat);
1526 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1527 if (glfd == NULL) {
1528 END_PROFILE(syscall_fstat);
1529 DBG_ERR("Failed to fetch gluster fd\n");
1530 return -1;
1533 ret = glfs_fstat(glfd, &st);
1534 if (ret == 0) {
1535 smb_stat_ex_from_stat(sbuf, &st);
1537 if (ret < 0) {
1538 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1539 fsp_get_io_fd(fsp), strerror(errno)));
1541 END_PROFILE(syscall_fstat);
1543 return ret;
1546 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1547 struct smb_filename *smb_fname)
1549 struct stat st;
1550 int ret;
1552 START_PROFILE(syscall_lstat);
1553 ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1554 if (ret == 0) {
1555 smb_stat_ex_from_stat(&smb_fname->st, &st);
1557 if (ret < 0 && errno != ENOENT) {
1558 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1559 smb_fname->base_name, strerror(errno)));
1561 END_PROFILE(syscall_lstat);
1563 return ret;
1566 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1567 files_struct *fsp,
1568 const SMB_STRUCT_STAT *sbuf)
1570 uint64_t ret;
1572 START_PROFILE(syscall_get_alloc_size);
1573 ret = sbuf->st_ex_blocks * 512;
1574 END_PROFILE(syscall_get_alloc_size);
1576 return ret;
1579 static int vfs_gluster_unlinkat(struct vfs_handle_struct *handle,
1580 struct files_struct *dirfsp,
1581 const struct smb_filename *smb_fname,
1582 int flags)
1584 int ret;
1586 #ifdef HAVE_GFAPI_VER_7_11
1587 glfs_fd_t *pglfd = NULL;
1589 START_PROFILE(syscall_unlinkat);
1591 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1592 if (pglfd == NULL) {
1593 END_PROFILE(syscall_unlinkat);
1594 DBG_ERR("Failed to fetch gluster fd\n");
1595 return -1;
1598 ret = glfs_unlinkat(pglfd, smb_fname->base_name, flags);
1599 #else
1600 struct smb_filename *full_fname = NULL;
1602 START_PROFILE(syscall_unlinkat);
1604 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1605 dirfsp,
1606 smb_fname);
1607 if (full_fname == NULL) {
1608 END_PROFILE(syscall_unlinkat);
1609 return -1;
1612 if (flags & AT_REMOVEDIR) {
1613 ret = glfs_rmdir(handle->data, full_fname->base_name);
1614 } else {
1615 ret = glfs_unlink(handle->data, full_fname->base_name);
1618 TALLOC_FREE(full_fname);
1619 #endif
1621 END_PROFILE(syscall_unlinkat);
1623 return ret;
1626 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1627 files_struct *fsp, mode_t mode)
1629 int ret;
1630 glfs_fd_t *glfd = NULL;
1632 START_PROFILE(syscall_fchmod);
1634 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1635 if (glfd == NULL) {
1636 END_PROFILE(syscall_fchmod);
1637 DBG_ERR("Failed to fetch gluster fd\n");
1638 return -1;
1641 if (!fsp->fsp_flags.is_pathref) {
1643 * We can use an io_fd to remove xattrs.
1645 ret = glfs_fchmod(glfd, mode);
1646 } else {
1648 * This is no longer a handle based call.
1650 ret = glfs_chmod(handle->data, fsp->fsp_name->base_name, mode);
1652 END_PROFILE(syscall_fchmod);
1654 return ret;
1657 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1658 files_struct *fsp, uid_t uid, gid_t gid)
1660 int ret;
1661 glfs_fd_t *glfd = NULL;
1663 START_PROFILE(syscall_fchown);
1665 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1666 if (glfd == NULL) {
1667 END_PROFILE(syscall_fchown);
1668 DBG_ERR("Failed to fetch gluster fd\n");
1669 return -1;
1672 ret = glfs_fchown(glfd, uid, gid);
1673 END_PROFILE(syscall_fchown);
1675 return ret;
1678 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1679 const struct smb_filename *smb_fname,
1680 uid_t uid,
1681 gid_t gid)
1683 int ret;
1685 START_PROFILE(syscall_lchown);
1686 ret = glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1687 END_PROFILE(syscall_lchown);
1689 return ret;
1692 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1693 const struct smb_filename *smb_fname)
1695 int ret;
1697 START_PROFILE(syscall_chdir);
1698 ret = glfs_chdir(handle->data, smb_fname->base_name);
1699 END_PROFILE(syscall_chdir);
1701 return ret;
1704 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1705 TALLOC_CTX *ctx)
1707 char cwd[PATH_MAX] = { '\0' };
1708 char *ret;
1709 struct smb_filename *smb_fname = NULL;
1711 START_PROFILE(syscall_getwd);
1713 ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1714 END_PROFILE(syscall_getwd);
1716 if (ret == NULL) {
1717 return NULL;
1719 smb_fname = synthetic_smb_fname(ctx,
1720 ret,
1721 NULL,
1722 NULL,
1725 return smb_fname;
1728 static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
1729 files_struct *fsp,
1730 struct smb_file_time *ft)
1732 int ret = -1;
1733 struct timespec times[2];
1734 glfs_fd_t *glfd = NULL;
1736 START_PROFILE(syscall_fntimes);
1738 if (is_omit_timespec(&ft->atime)) {
1739 times[0].tv_sec = fsp->fsp_name->st.st_ex_atime.tv_sec;
1740 times[0].tv_nsec = fsp->fsp_name->st.st_ex_atime.tv_nsec;
1741 } else {
1742 times[0].tv_sec = ft->atime.tv_sec;
1743 times[0].tv_nsec = ft->atime.tv_nsec;
1746 if (is_omit_timespec(&ft->mtime)) {
1747 times[1].tv_sec = fsp->fsp_name->st.st_ex_mtime.tv_sec;
1748 times[1].tv_nsec = fsp->fsp_name->st.st_ex_mtime.tv_nsec;
1749 } else {
1750 times[1].tv_sec = ft->mtime.tv_sec;
1751 times[1].tv_nsec = ft->mtime.tv_nsec;
1754 if ((timespec_compare(&times[0],
1755 &fsp->fsp_name->st.st_ex_atime) == 0) &&
1756 (timespec_compare(&times[1],
1757 &fsp->fsp_name->st.st_ex_mtime) == 0)) {
1758 END_PROFILE(syscall_fntimes);
1759 return 0;
1762 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1763 if (glfd == NULL) {
1764 END_PROFILE(syscall_fntimes);
1765 DBG_ERR("Failed to fetch gluster fd\n");
1766 return -1;
1769 ret = glfs_futimens(glfd, times);
1770 END_PROFILE(syscall_fntimes);
1772 return ret;
1775 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1776 files_struct *fsp, off_t offset)
1778 int ret;
1779 glfs_fd_t *glfd = NULL;
1781 START_PROFILE(syscall_ftruncate);
1783 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1784 if (glfd == NULL) {
1785 END_PROFILE(syscall_ftruncate);
1786 DBG_ERR("Failed to fetch gluster fd\n");
1787 return -1;
1790 #ifdef HAVE_GFAPI_VER_7_6
1791 ret = glfs_ftruncate(glfd, offset, NULL, NULL);
1792 #else
1793 ret = glfs_ftruncate(glfd, offset);
1794 #endif
1795 END_PROFILE(syscall_ftruncate);
1797 return ret;
1800 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1801 struct files_struct *fsp,
1802 uint32_t mode,
1803 off_t offset, off_t len)
1805 int ret;
1806 #ifdef HAVE_GFAPI_VER_6
1807 glfs_fd_t *glfd = NULL;
1808 int keep_size, punch_hole;
1810 START_PROFILE(syscall_fallocate);
1812 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1813 if (glfd == NULL) {
1814 END_PROFILE(syscall_fallocate);
1815 DBG_ERR("Failed to fetch gluster fd\n");
1816 return -1;
1819 keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1820 punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1822 mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1823 if (mode != 0) {
1824 END_PROFILE(syscall_fallocate);
1825 errno = ENOTSUP;
1826 return -1;
1829 if (punch_hole) {
1830 ret = glfs_discard(glfd, offset, len);
1831 if (ret != 0) {
1832 DBG_DEBUG("glfs_discard failed: %s\n",
1833 strerror(errno));
1837 ret = glfs_fallocate(glfd, keep_size, offset, len);
1838 END_PROFILE(syscall_fallocate);
1839 #else
1840 errno = ENOTSUP;
1841 ret = -1;
1842 #endif
1843 return ret;
1846 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1847 TALLOC_CTX *ctx,
1848 const struct smb_filename *smb_fname)
1850 char *result = NULL;
1851 struct smb_filename *result_fname = NULL;
1852 char *resolved_path = NULL;
1854 START_PROFILE(syscall_realpath);
1856 resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1857 if (resolved_path == NULL) {
1858 END_PROFILE(syscall_realpath);
1859 errno = ENOMEM;
1860 return NULL;
1863 result = glfs_realpath(handle->data,
1864 smb_fname->base_name,
1865 resolved_path);
1866 if (result != NULL) {
1867 result_fname = synthetic_smb_fname(ctx,
1868 result,
1869 NULL,
1870 NULL,
1875 SAFE_FREE(resolved_path);
1876 END_PROFILE(syscall_realpath);
1878 return result_fname;
1881 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1882 files_struct *fsp, int op, off_t offset,
1883 off_t count, int type)
1885 struct flock flock = { 0, };
1886 int ret;
1887 glfs_fd_t *glfd = NULL;
1888 bool ok = false;
1890 START_PROFILE(syscall_fcntl_lock);
1892 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1893 if (glfd == NULL) {
1894 DBG_ERR("Failed to fetch gluster fd\n");
1895 ok = false;
1896 goto out;
1899 flock.l_type = type;
1900 flock.l_whence = SEEK_SET;
1901 flock.l_start = offset;
1902 flock.l_len = count;
1903 flock.l_pid = 0;
1905 ret = glfs_posix_lock(glfd, op, &flock);
1907 if (op == F_GETLK) {
1908 /* lock query, true if someone else has locked */
1909 if ((ret != -1) &&
1910 (flock.l_type != F_UNLCK) &&
1911 (flock.l_pid != 0) && (flock.l_pid != getpid())) {
1912 ok = true;
1913 goto out;
1915 /* not me */
1916 ok = false;
1917 goto out;
1920 if (ret == -1) {
1921 ok = false;
1922 goto out;
1925 ok = true;
1926 out:
1927 END_PROFILE(syscall_fcntl_lock);
1929 return ok;
1932 static int vfs_gluster_filesystem_sharemode(struct vfs_handle_struct *handle,
1933 files_struct *fsp,
1934 uint32_t share_access,
1935 uint32_t access_mask)
1937 errno = ENOSYS;
1938 return -1;
1941 static int vfs_gluster_fcntl(vfs_handle_struct *handle,
1942 files_struct *fsp, int cmd, va_list cmd_arg)
1945 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1946 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1948 if (cmd == F_GETFL) {
1949 return 0;
1950 } else if (cmd == F_SETFL) {
1951 va_list dup_cmd_arg;
1952 int opt;
1954 va_copy(dup_cmd_arg, cmd_arg);
1955 opt = va_arg(dup_cmd_arg, int);
1956 va_end(dup_cmd_arg);
1957 if (opt == 0) {
1958 return 0;
1960 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1961 goto err_out;
1963 DBG_ERR("unexpected fcntl: %d\n", cmd);
1964 err_out:
1965 errno = EINVAL;
1966 return -1;
1969 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1970 files_struct *fsp, int leasetype)
1972 errno = ENOSYS;
1973 return -1;
1976 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1977 files_struct *fsp, off_t *poffset,
1978 off_t *pcount, int *ptype, pid_t *ppid)
1980 struct flock flock = { 0, };
1981 int ret;
1982 glfs_fd_t *glfd = NULL;
1984 START_PROFILE(syscall_fcntl_getlock);
1986 glfd = vfs_gluster_fetch_glfd(handle, fsp);
1987 if (glfd == NULL) {
1988 END_PROFILE(syscall_fcntl_getlock);
1989 DBG_ERR("Failed to fetch gluster fd\n");
1990 return false;
1993 flock.l_type = *ptype;
1994 flock.l_whence = SEEK_SET;
1995 flock.l_start = *poffset;
1996 flock.l_len = *pcount;
1997 flock.l_pid = 0;
1999 ret = glfs_posix_lock(glfd, F_GETLK, &flock);
2001 if (ret == -1) {
2002 END_PROFILE(syscall_fcntl_getlock);
2003 return false;
2006 *ptype = flock.l_type;
2007 *poffset = flock.l_start;
2008 *pcount = flock.l_len;
2009 *ppid = flock.l_pid;
2010 END_PROFILE(syscall_fcntl_getlock);
2012 return true;
2015 static int vfs_gluster_symlinkat(struct vfs_handle_struct *handle,
2016 const struct smb_filename *link_target,
2017 struct files_struct *dirfsp,
2018 const struct smb_filename *new_smb_fname)
2020 int ret;
2022 #ifdef HAVE_GFAPI_VER_7_11
2023 glfs_fd_t *pglfd = NULL;
2025 START_PROFILE(syscall_symlinkat);
2027 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2028 if (pglfd == NULL) {
2029 END_PROFILE(syscall_symlinkat);
2030 DBG_ERR("Failed to fetch gluster fd\n");
2031 return -1;
2034 ret = glfs_symlinkat(link_target->base_name,
2035 pglfd,
2036 new_smb_fname->base_name);
2037 #else
2038 struct smb_filename *full_fname = NULL;
2040 START_PROFILE(syscall_symlinkat);
2042 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2043 dirfsp,
2044 new_smb_fname);
2045 if (full_fname == NULL) {
2046 END_PROFILE(syscall_symlinkat);
2047 return -1;
2050 ret = glfs_symlink(handle->data,
2051 link_target->base_name,
2052 full_fname->base_name);
2054 TALLOC_FREE(full_fname);
2055 #endif
2057 END_PROFILE(syscall_symlinkat);
2059 return ret;
2062 static int vfs_gluster_readlinkat(struct vfs_handle_struct *handle,
2063 const struct files_struct *dirfsp,
2064 const struct smb_filename *smb_fname,
2065 char *buf,
2066 size_t bufsiz)
2068 int ret;
2070 #ifdef HAVE_GFAPI_VER_7_11
2071 glfs_fd_t *pglfd = NULL;
2073 START_PROFILE(syscall_readlinkat);
2075 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2076 if (pglfd == NULL) {
2077 END_PROFILE(syscall_readlinkat);
2078 DBG_ERR("Failed to fetch gluster fd\n");
2079 return -1;
2082 ret = glfs_readlinkat(pglfd, smb_fname->base_name, buf, bufsiz);
2083 #else
2084 struct smb_filename *full_fname = NULL;
2086 START_PROFILE(syscall_readlinkat);
2088 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2089 dirfsp,
2090 smb_fname);
2091 if (full_fname == NULL) {
2092 END_PROFILE(syscall_readlinkat);
2093 return -1;
2096 ret = glfs_readlink(handle->data, full_fname->base_name, buf, bufsiz);
2098 TALLOC_FREE(full_fname);
2099 #endif
2101 END_PROFILE(syscall_readlinkat);
2103 return ret;
2106 static int vfs_gluster_linkat(struct vfs_handle_struct *handle,
2107 files_struct *srcfsp,
2108 const struct smb_filename *old_smb_fname,
2109 files_struct *dstfsp,
2110 const struct smb_filename *new_smb_fname,
2111 int flags)
2113 int ret;
2114 struct smb_filename *full_fname_old = NULL;
2115 struct smb_filename *full_fname_new = NULL;
2117 START_PROFILE(syscall_linkat);
2119 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
2120 srcfsp,
2121 old_smb_fname);
2122 if (full_fname_old == NULL) {
2123 END_PROFILE(syscall_linkat);
2124 return -1;
2126 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
2127 dstfsp,
2128 new_smb_fname);
2129 if (full_fname_new == NULL) {
2130 TALLOC_FREE(full_fname_old);
2131 END_PROFILE(syscall_linkat);
2132 return -1;
2135 ret = glfs_link(handle->data,
2136 full_fname_old->base_name,
2137 full_fname_new->base_name);
2139 TALLOC_FREE(full_fname_old);
2140 TALLOC_FREE(full_fname_new);
2141 END_PROFILE(syscall_linkat);
2143 return ret;
2146 static int vfs_gluster_mknodat(struct vfs_handle_struct *handle,
2147 files_struct *dirfsp,
2148 const struct smb_filename *smb_fname,
2149 mode_t mode,
2150 SMB_DEV_T dev)
2152 struct smb_filename *full_fname = NULL;
2153 int ret;
2155 START_PROFILE(syscall_mknodat);
2157 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2158 dirfsp,
2159 smb_fname);
2160 if (full_fname == NULL) {
2161 END_PROFILE(syscall_mknodat);
2162 return -1;
2165 ret = glfs_mknod(handle->data, full_fname->base_name, mode, dev);
2167 TALLOC_FREE(full_fname);
2169 END_PROFILE(syscall_mknodat);
2171 return ret;
2174 static int vfs_gluster_fchflags(struct vfs_handle_struct *handle,
2175 struct files_struct *fsp,
2176 unsigned int flags)
2178 errno = ENOSYS;
2179 return -1;
2182 static NTSTATUS vfs_gluster_get_real_filename_at(
2183 struct vfs_handle_struct *handle,
2184 struct files_struct *dirfsp,
2185 const char *name,
2186 TALLOC_CTX *mem_ctx,
2187 char **found_name)
2189 int ret;
2190 char key_buf[GLUSTER_NAME_MAX + 64];
2191 char val_buf[GLUSTER_NAME_MAX + 1];
2192 NTSTATUS status = NT_STATUS_OK;
2193 struct smb_filename *smb_fname_dot = NULL;
2194 struct smb_filename *full_fname = NULL;
2196 smb_fname_dot = synthetic_smb_fname(mem_ctx,
2197 ".",
2198 NULL,
2199 NULL,
2202 if (smb_fname_dot == NULL) {
2203 return NT_STATUS_NO_MEMORY;
2206 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2207 dirfsp,
2208 smb_fname_dot);
2209 if (full_fname == NULL) {
2210 TALLOC_FREE(smb_fname_dot);
2211 return NT_STATUS_NO_MEMORY;
2214 if (strlen(name) >= GLUSTER_NAME_MAX) {
2215 status = NT_STATUS_OBJECT_NAME_INVALID;
2216 goto out;
2219 snprintf(key_buf, GLUSTER_NAME_MAX + 64,
2220 "glusterfs.get_real_filename:%s", name);
2222 ret = glfs_getxattr(handle->data, full_fname->base_name,
2223 key_buf, val_buf, GLUSTER_NAME_MAX + 1);
2224 if (ret == -1) {
2225 if (errno == ENOATTR) {
2226 errno = ENOENT;
2228 status = map_nt_error_from_unix(errno);
2229 goto out;
2232 *found_name = talloc_strdup(mem_ctx, val_buf);
2233 if (found_name[0] == NULL) {
2234 status = NT_STATUS_NO_MEMORY;
2235 goto out;
2238 out:
2239 TALLOC_FREE(smb_fname_dot);
2240 TALLOC_FREE(full_fname);
2242 return status;
2245 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
2246 const struct smb_filename *smb_fname)
2248 return handle->conn->connectpath;
2251 /* EA Operations */
2253 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
2254 files_struct *fsp, const char *name,
2255 void *value, size_t size)
2257 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2258 if (glfd == NULL) {
2259 DBG_ERR("Failed to fetch gluster fd\n");
2260 return -1;
2263 return glfs_fgetxattr(glfd, name, value, size);
2266 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
2267 files_struct *fsp, char *list,
2268 size_t size)
2270 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2271 if (glfd == NULL) {
2272 DBG_ERR("Failed to fetch gluster fd\n");
2273 return -1;
2275 if (!fsp->fsp_flags.is_pathref) {
2277 * We can use an io_fd to list xattrs.
2279 return glfs_flistxattr(glfd, list, size);
2280 } else {
2282 * This is no longer a handle based call.
2284 return glfs_listxattr(handle->data,
2285 fsp->fsp_name->base_name,
2286 list,
2287 size);
2291 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
2292 files_struct *fsp, const char *name)
2294 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2295 if (glfd == NULL) {
2296 DBG_ERR("Failed to fetch gluster fd\n");
2297 return -1;
2299 if (!fsp->fsp_flags.is_pathref) {
2301 * We can use an io_fd to remove xattrs.
2303 return glfs_fremovexattr(glfd, name);
2304 } else {
2306 * This is no longer a handle based call.
2308 return glfs_removexattr(handle->data,
2309 fsp->fsp_name->base_name,
2310 name);
2314 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
2315 files_struct *fsp, const char *name,
2316 const void *value, size_t size, int flags)
2318 glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2319 if (glfd == NULL) {
2320 DBG_ERR("Failed to fetch gluster fd\n");
2321 return -1;
2324 if (!fsp->fsp_flags.is_pathref) {
2326 * We can use an io_fd to set xattrs.
2328 return glfs_fsetxattr(glfd, name, value, size, flags);
2329 } else {
2331 * This is no longer a handle based call.
2333 return glfs_setxattr(handle->data,
2334 fsp->fsp_name->base_name,
2335 name,
2336 value,
2337 size,
2338 flags);
2342 /* AIO Operations */
2344 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
2345 files_struct *fsp)
2347 return false;
2350 static NTSTATUS vfs_gluster_create_dfs_pathat(struct vfs_handle_struct *handle,
2351 struct files_struct *dirfsp,
2352 const struct smb_filename *smb_fname,
2353 const struct referral *reflist,
2354 size_t referral_count)
2356 TALLOC_CTX *frame = talloc_stackframe();
2357 NTSTATUS status = NT_STATUS_NO_MEMORY;
2358 int ret;
2359 char *msdfs_link = NULL;
2360 struct smb_filename *full_fname = NULL;
2362 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2363 dirfsp,
2364 smb_fname);
2365 if (full_fname == NULL) {
2366 goto out;
2369 /* Form the msdfs_link contents */
2370 msdfs_link = msdfs_link_string(frame,
2371 reflist,
2372 referral_count);
2373 if (msdfs_link == NULL) {
2374 goto out;
2377 ret = glfs_symlink(handle->data,
2378 msdfs_link,
2379 full_fname->base_name);
2380 if (ret == 0) {
2381 status = NT_STATUS_OK;
2382 } else {
2383 status = map_nt_error_from_unix(errno);
2386 out:
2388 TALLOC_FREE(frame);
2389 return status;
2393 * Read and return the contents of a DFS redirect given a
2394 * pathname. A caller can pass in NULL for ppreflist and
2395 * preferral_count but still determine if this was a
2396 * DFS redirect point by getting NT_STATUS_OK back
2397 * without incurring the overhead of reading and parsing
2398 * the referral contents.
2401 static NTSTATUS vfs_gluster_read_dfs_pathat(struct vfs_handle_struct *handle,
2402 TALLOC_CTX *mem_ctx,
2403 struct files_struct *dirfsp,
2404 struct smb_filename *smb_fname,
2405 struct referral **ppreflist,
2406 size_t *preferral_count)
2408 NTSTATUS status = NT_STATUS_NO_MEMORY;
2409 size_t bufsize;
2410 char *link_target = NULL;
2411 int referral_len;
2412 bool ok;
2413 #if defined(HAVE_BROKEN_READLINK)
2414 char link_target_buf[PATH_MAX];
2415 #else
2416 char link_target_buf[7];
2417 #endif
2418 struct stat st;
2419 struct smb_filename *full_fname = NULL;
2420 int ret;
2422 if (is_named_stream(smb_fname)) {
2423 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2424 goto err;
2427 if (ppreflist == NULL && preferral_count == NULL) {
2429 * We're only checking if this is a DFS
2430 * redirect. We don't need to return data.
2432 bufsize = sizeof(link_target_buf);
2433 link_target = link_target_buf;
2434 } else {
2435 bufsize = PATH_MAX;
2436 link_target = talloc_array(mem_ctx, char, bufsize);
2437 if (!link_target) {
2438 goto err;
2442 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2443 dirfsp,
2444 smb_fname);
2445 if (full_fname == NULL) {
2446 status = NT_STATUS_NO_MEMORY;
2447 goto err;
2450 ret = glfs_lstat(handle->data, full_fname->base_name, &st);
2451 if (ret < 0) {
2452 status = map_nt_error_from_unix(errno);
2453 goto err;
2456 referral_len = glfs_readlink(handle->data,
2457 full_fname->base_name,
2458 link_target,
2459 bufsize - 1);
2460 if (referral_len < 0) {
2461 if (errno == EINVAL) {
2462 DBG_INFO("%s is not a link.\n", full_fname->base_name);
2463 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2464 } else {
2465 status = map_nt_error_from_unix(errno);
2466 DBG_ERR("Error reading "
2467 "msdfs link %s: %s\n",
2468 full_fname->base_name,
2469 strerror(errno));
2471 goto err;
2473 link_target[referral_len] = '\0';
2475 DBG_INFO("%s -> %s\n",
2476 full_fname->base_name,
2477 link_target);
2479 if (!strnequal(link_target, "msdfs:", 6)) {
2480 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2481 goto err;
2484 if (ppreflist == NULL && preferral_count == NULL) {
2485 /* Early return for checking if this is a DFS link. */
2486 TALLOC_FREE(full_fname);
2487 smb_stat_ex_from_stat(&smb_fname->st, &st);
2488 return NT_STATUS_OK;
2491 ok = parse_msdfs_symlink(mem_ctx,
2492 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
2493 link_target,
2494 ppreflist,
2495 preferral_count);
2497 if (ok) {
2498 smb_stat_ex_from_stat(&smb_fname->st, &st);
2499 status = NT_STATUS_OK;
2500 } else {
2501 status = NT_STATUS_NO_MEMORY;
2504 err:
2506 if (link_target != link_target_buf) {
2507 TALLOC_FREE(link_target);
2509 TALLOC_FREE(full_fname);
2510 return status;
2513 static struct vfs_fn_pointers glusterfs_fns = {
2515 /* Disk Operations */
2517 .connect_fn = vfs_gluster_connect,
2518 .disconnect_fn = vfs_gluster_disconnect,
2519 .disk_free_fn = vfs_gluster_disk_free,
2520 .get_quota_fn = vfs_gluster_get_quota,
2521 .set_quota_fn = vfs_gluster_set_quota,
2522 .statvfs_fn = vfs_gluster_statvfs,
2523 .fs_capabilities_fn = vfs_gluster_fs_capabilities,
2525 .get_dfs_referrals_fn = NULL,
2527 /* Directory Operations */
2529 .fdopendir_fn = vfs_gluster_fdopendir,
2530 .readdir_fn = vfs_gluster_readdir,
2531 .seekdir_fn = vfs_gluster_seekdir,
2532 .telldir_fn = vfs_gluster_telldir,
2533 .rewind_dir_fn = vfs_gluster_rewinddir,
2534 .mkdirat_fn = vfs_gluster_mkdirat,
2535 .closedir_fn = vfs_gluster_closedir,
2537 /* File Operations */
2539 .openat_fn = vfs_gluster_openat,
2540 .create_file_fn = NULL,
2541 .close_fn = vfs_gluster_close,
2542 .pread_fn = vfs_gluster_pread,
2543 .pread_send_fn = vfs_gluster_pread_send,
2544 .pread_recv_fn = vfs_gluster_pread_recv,
2545 .pwrite_fn = vfs_gluster_pwrite,
2546 .pwrite_send_fn = vfs_gluster_pwrite_send,
2547 .pwrite_recv_fn = vfs_gluster_pwrite_recv,
2548 .lseek_fn = vfs_gluster_lseek,
2549 .sendfile_fn = vfs_gluster_sendfile,
2550 .recvfile_fn = vfs_gluster_recvfile,
2551 .renameat_fn = vfs_gluster_renameat,
2552 .fsync_send_fn = vfs_gluster_fsync_send,
2553 .fsync_recv_fn = vfs_gluster_fsync_recv,
2555 .stat_fn = vfs_gluster_stat,
2556 .fstat_fn = vfs_gluster_fstat,
2557 .lstat_fn = vfs_gluster_lstat,
2558 .get_alloc_size_fn = vfs_gluster_get_alloc_size,
2559 .unlinkat_fn = vfs_gluster_unlinkat,
2561 .fchmod_fn = vfs_gluster_fchmod,
2562 .fchown_fn = vfs_gluster_fchown,
2563 .lchown_fn = vfs_gluster_lchown,
2564 .chdir_fn = vfs_gluster_chdir,
2565 .getwd_fn = vfs_gluster_getwd,
2566 .fntimes_fn = vfs_gluster_fntimes,
2567 .ftruncate_fn = vfs_gluster_ftruncate,
2568 .fallocate_fn = vfs_gluster_fallocate,
2569 .lock_fn = vfs_gluster_lock,
2570 .filesystem_sharemode_fn = vfs_gluster_filesystem_sharemode,
2571 .fcntl_fn = vfs_gluster_fcntl,
2572 .linux_setlease_fn = vfs_gluster_linux_setlease,
2573 .getlock_fn = vfs_gluster_getlock,
2574 .symlinkat_fn = vfs_gluster_symlinkat,
2575 .readlinkat_fn = vfs_gluster_readlinkat,
2576 .linkat_fn = vfs_gluster_linkat,
2577 .mknodat_fn = vfs_gluster_mknodat,
2578 .realpath_fn = vfs_gluster_realpath,
2579 .fchflags_fn = vfs_gluster_fchflags,
2580 .file_id_create_fn = NULL,
2581 .fstreaminfo_fn = NULL,
2582 .get_real_filename_at_fn = vfs_gluster_get_real_filename_at,
2583 .connectpath_fn = vfs_gluster_connectpath,
2584 .create_dfs_pathat_fn = vfs_gluster_create_dfs_pathat,
2585 .read_dfs_pathat_fn = vfs_gluster_read_dfs_pathat,
2587 .brl_lock_windows_fn = NULL,
2588 .brl_unlock_windows_fn = NULL,
2589 .strict_lock_check_fn = NULL,
2590 .translate_name_fn = NULL,
2591 .fsctl_fn = NULL,
2593 /* NT ACL Operations */
2594 .fget_nt_acl_fn = NULL,
2595 .fset_nt_acl_fn = NULL,
2596 .audit_file_fn = NULL,
2598 /* Posix ACL Operations */
2599 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
2600 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2601 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
2602 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
2604 /* EA Operations */
2605 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2606 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2607 .fgetxattr_fn = vfs_gluster_fgetxattr,
2608 .flistxattr_fn = vfs_gluster_flistxattr,
2609 .fremovexattr_fn = vfs_gluster_fremovexattr,
2610 .fsetxattr_fn = vfs_gluster_fsetxattr,
2612 /* AIO Operations */
2613 .aio_force_fn = vfs_gluster_aio_force,
2615 /* Durable handle Operations */
2616 .durable_cookie_fn = NULL,
2617 .durable_disconnect_fn = NULL,
2618 .durable_reconnect_fn = NULL,
2621 static_decl_vfs;
2622 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
2624 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2625 "glusterfs", &glusterfs_fns);