vfs_glusterfs: Add path based fallback mechanism for SMB_VFS_FNTIMES
[samba.git] / source3 / modules / vfs_glusterfs.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Wrap GlusterFS GFAPI calls in vfs functions.
5
6    Copyright (c) 2013 Anand Avati <avati@redhat.com>
7
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.
12
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.
17
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/>.
20 */
21
22 /**
23  * @file   vfs_glusterfs.c
24  * @author Anand Avati <avati@redhat.com>
25  * @date   May 2013
26  * @brief  Samba VFS module for glusterfs
27  *
28  * @todo
29  *   - sendfile/recvfile support
30  *
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.
35  *
36  */
37
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"
49
50 #define DEFAULT_VOLFILE_SERVER "localhost"
51 #define GLUSTER_NAME_MAX 255
52
53 /**
54  * Helper to convert struct stat to struct stat_ex.
55  */
56 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
57 {
58         ZERO_STRUCTP(dst);
59
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
80 }
81
82 /* pre-opened glfs_t */
83
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;
91
92
93 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
94 {
95         struct glfs_preopened *entry = NULL;
96
97         entry = talloc_zero(NULL, struct glfs_preopened);
98         if (!entry) {
99                 errno = ENOMEM;
100                 return -1;
101         }
102
103         entry->volume = talloc_strdup(entry, volume);
104         if (!entry->volume) {
105                 talloc_free(entry);
106                 errno = ENOMEM;
107                 return -1;
108         }
109
110         entry->connectpath = talloc_strdup(entry, connectpath);
111         if (entry->connectpath == NULL) {
112                 talloc_free(entry);
113                 errno = ENOMEM;
114                 return -1;
115         }
116
117         entry->fs = fs;
118         entry->ref = 1;
119
120         DLIST_ADD(glfs_preopened, entry);
121
122         return 0;
123 }
124
125 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
126 {
127         struct glfs_preopened *entry = NULL;
128
129         for (entry = glfs_preopened; entry; entry = entry->next) {
130                 if (strcmp(entry->volume, volume) == 0 &&
131                     strcmp(entry->connectpath, connectpath) == 0)
132                 {
133                         entry->ref++;
134                         return entry->fs;
135                 }
136         }
137
138         return NULL;
139 }
140
141 static void glfs_clear_preopened(glfs_t *fs)
142 {
143         struct glfs_preopened *entry = NULL;
144
145         for (entry = glfs_preopened; entry; entry = entry->next) {
146                 if (entry->fs == fs) {
147                         if (--entry->ref)
148                                 return;
149
150                         DLIST_REMOVE(glfs_preopened, entry);
151
152                         glfs_fini(entry->fs);
153                         talloc_free(entry);
154                         break;
155                 }
156         }
157 }
158
159 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
160                                            const char *volfile_servers)
161 {
162         char *server = NULL;
163         size_t server_count = 0;
164         size_t server_success = 0;
165         int   ret = -1;
166         TALLOC_CTX *frame = talloc_stackframe();
167
168         DBG_INFO("servers list %s\n", volfile_servers);
169
170         while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
171                 char *transport = NULL;
172                 char *host = NULL;
173                 int   port = 0;
174
175                 server_count++;
176                 DBG_INFO("server %zu %s\n", server_count, server);
177
178                 /* Determine the transport type */
179                 if (strncmp(server, "unix+", 5) == 0) {
180                         port = 0;
181                         transport = talloc_strdup(frame, "unix");
182                         if (!transport) {
183                                 errno = ENOMEM;
184                                 goto out;
185                         }
186                         host = talloc_strdup(frame, server + 5);
187                         if (!host) {
188                                 errno = ENOMEM;
189                                 goto out;
190                         }
191                 } else {
192                         char *p = NULL;
193                         char *port_index = NULL;
194
195                         if (strncmp(server, "tcp+", 4) == 0) {
196                                 server += 4;
197                         }
198
199                         /* IPv6 is enclosed in []
200                          * ':' before ']' is part of IPv6
201                          * ':' after  ']' indicates port
202                          */
203                         p = server;
204                         if (server[0] == '[') {
205                                 server++;
206                                 p = index(server, ']');
207                                 if (p == NULL) {
208                                         /* Malformed IPv6 */
209                                         continue;
210                                 }
211                                 p[0] = '\0';
212                                 p++;
213                         }
214
215                         port_index = index(p, ':');
216
217                         if (port_index == NULL) {
218                                 port = 0;
219                         } else {
220                                 port = atoi(port_index + 1);
221                                 port_index[0] = '\0';
222                         }
223                         transport = talloc_strdup(frame, "tcp");
224                         if (!transport) {
225                                 errno = ENOMEM;
226                                 goto out;
227                         }
228                         host = talloc_strdup(frame, server);
229                         if (!host) {
230                                 errno = ENOMEM;
231                                 goto out;
232                         }
233                 }
234
235                 DBG_INFO("Calling set volfile server with params "
236                          "transport=%s, host=%s, port=%d\n", transport,
237                           host, port);
238
239                 ret = glfs_set_volfile_server(fs, transport, host, port);
240                 if (ret < 0) {
241                         DBG_WARNING("Failed to set volfile_server "
242                                     "transport=%s, host=%s, port=%d (%s)\n",
243                                     transport, host, port, strerror(errno));
244                 } else {
245                         server_success++;
246                 }
247         }
248
249 out:
250         if (server_count == 0) {
251                 ret = -1;
252         } else if (server_success < server_count) {
253                 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
254                             server_count - server_success, server_count);
255                 ret = 0;
256         }
257
258         TALLOC_FREE(frame);
259         return ret;
260 }
261
262 /* Disk Operations */
263
264 static int check_for_write_behind_translator(TALLOC_CTX *mem_ctx,
265                                              glfs_t *fs,
266                                              const char *volume)
267 {
268         char *buf = NULL;
269         char **lines = NULL;
270         int numlines = 0;
271         int i;
272         char *option;
273         bool write_behind_present = false;
274         size_t newlen;
275         int ret;
276
277         ret = glfs_get_volfile(fs, NULL, 0);
278         if (ret == 0) {
279                 DBG_ERR("%s: Failed to get volfile for "
280                         "volume (%s): No volfile\n",
281                         volume,
282                         strerror(errno));
283                 return -1;
284         }
285         if (ret > 0) {
286                 DBG_ERR("%s: Invalid return %d for glfs_get_volfile for "
287                         "volume (%s): No volfile\n",
288                         volume,
289                         ret,
290                         strerror(errno));
291                 return -1;
292         }
293
294         newlen = 0 - ret;
295
296         buf = talloc_zero_array(mem_ctx, char, newlen);
297         if (buf == NULL) {
298                 return -1;
299         }
300
301         ret = glfs_get_volfile(fs, buf, newlen);
302         if (ret != newlen) {
303                 TALLOC_FREE(buf);
304                 DBG_ERR("%s: Failed to get volfile for volume (%s)\n",
305                         volume, strerror(errno));
306                 return -1;
307         }
308
309         option = talloc_asprintf(mem_ctx, "volume %s-write-behind", volume);
310         if (option == NULL) {
311                 TALLOC_FREE(buf);
312                 return -1;
313         }
314
315         /*
316          * file_lines_parse() plays horrible tricks with
317          * the passed-in talloc pointers and the hierarcy
318          * which makes freeing hard to get right.
319          *
320          * As we know mem_ctx is freed by the caller, after
321          * this point don't free on exit and let the caller
322          * handle it. This violates good Samba coding practice
323          * but we know we're not leaking here.
324          */
325
326         lines = file_lines_parse(buf,
327                                 newlen,
328                                 &numlines,
329                                 mem_ctx);
330         if (lines == NULL || numlines <= 0) {
331                 return -1;
332         }
333         /* On success, buf is now a talloc child of lines !! */
334
335         for (i=0; i < numlines; i++) {
336                 if (strequal(lines[i], option)) {
337                         write_behind_present = true;
338                         break;
339                 }
340         }
341
342         if (write_behind_present) {
343                 DBG_ERR("Write behind translator is enabled for "
344                         "volume (%s), refusing to connect! "
345                         "Please turn off the write behind translator by calling "
346                         "'gluster volume set %s performance.write-behind off' "
347                         "on the commandline. "
348                         "Check the vfs_glusterfs(8) manpage for "
349                         "further details.\n",
350                         volume, volume);
351                 return -1;
352         }
353
354         return 0;
355 }
356
357 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
358                                const char *service,
359                                const char *user)
360 {
361         const struct loadparm_substitution *lp_sub =
362                 loadparm_s3_global_substitution();
363         const char *volfile_servers;
364         const char *volume;
365         char *logfile;
366         int loglevel;
367         glfs_t *fs = NULL;
368         TALLOC_CTX *tmp_ctx;
369         int ret = 0;
370         bool write_behind_pass_through_set = false;
371
372         tmp_ctx = talloc_new(NULL);
373         if (tmp_ctx == NULL) {
374                 ret = -1;
375                 goto done;
376         }
377         logfile = lp_parm_substituted_string(tmp_ctx,
378                                              lp_sub,
379                                              SNUM(handle->conn),
380                                              "glusterfs",
381                                              "logfile",
382                                              NULL);
383
384         loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
385
386         volfile_servers = lp_parm_substituted_string(tmp_ctx,
387                                                      lp_sub,
388                                                      SNUM(handle->conn),
389                                                      "glusterfs",
390                                                      "volfile_server",
391                                                      NULL);
392         if (volfile_servers == NULL) {
393                 volfile_servers = DEFAULT_VOLFILE_SERVER;
394         }
395
396         volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
397                                       NULL);
398         if (volume == NULL) {
399                 volume = service;
400         }
401
402         fs = glfs_find_preopened(volume, handle->conn->connectpath);
403         if (fs) {
404                 goto done;
405         }
406
407         fs = glfs_new(volume);
408         if (fs == NULL) {
409                 ret = -1;
410                 goto done;
411         }
412
413         ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
414         if (ret < 0) {
415                 DBG_ERR("Failed to set volfile_servers from list %s\n",
416                         volfile_servers);
417                 goto done;
418         }
419
420         ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
421                                      "true");
422         if (ret < 0) {
423                 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
424                 goto done;
425         }
426
427         ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-selinux",
428                                      "true");
429         if (ret < 0) {
430                 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
431                 goto done;
432         }
433
434         ret = glfs_set_xlator_option(fs, "*-snapview-client",
435                                      "snapdir-entry-path",
436                                      handle->conn->connectpath);
437         if (ret < 0) {
438                 DEBUG(0, ("%s: Failed to set xlator option:"
439                           " snapdir-entry-path\n", volume));
440                 goto done;
441         }
442
443 #ifdef HAVE_GFAPI_VER_7_9
444         ret = glfs_set_xlator_option(fs, "*-write-behind", "pass-through",
445                                      "true");
446         if (ret < 0) {
447                 DBG_ERR("%s: Failed to set xlator option: pass-through\n",
448                         volume);
449                 goto done;
450         }
451         write_behind_pass_through_set = true;
452 #endif
453
454         ret = glfs_set_logging(fs, logfile, loglevel);
455         if (ret < 0) {
456                 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
457                           volume, logfile, loglevel));
458                 goto done;
459         }
460
461         ret = glfs_init(fs);
462         if (ret < 0) {
463                 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
464                           volume, strerror(errno)));
465                 goto done;
466         }
467
468         if (!write_behind_pass_through_set) {
469                 ret = check_for_write_behind_translator(tmp_ctx, fs, volume);
470                 if (ret < 0) {
471                         goto done;
472                 }
473         }
474
475         ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
476         if (ret < 0) {
477                 DEBUG(0, ("%s: Failed to register volume (%s)\n",
478                           volume, strerror(errno)));
479                 goto done;
480         }
481
482         /*
483          * The shadow_copy2 module will fail to export subdirectories
484          * of a gluster volume unless we specify the mount point,
485          * because the detection fails if the file system is not
486          * locally mounted:
487          * https://bugzilla.samba.org/show_bug.cgi?id=13091
488          */
489         lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
490
491         /*
492          * Unless we have an async implementation of getxattrat turn this off.
493          */
494         lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
495
496 done:
497         if (ret < 0) {
498                 if (fs)
499                         glfs_fini(fs);
500         } else {
501                 DBG_ERR("%s: Initialized volume from servers %s\n",
502                         volume, volfile_servers);
503                 handle->data = fs;
504         }
505         talloc_free(tmp_ctx);
506         return ret;
507 }
508
509 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
510 {
511         glfs_t *fs = NULL;
512
513         fs = handle->data;
514
515         glfs_clear_preopened(fs);
516 }
517
518 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
519                                 const struct smb_filename *smb_fname,
520                                 uint64_t *bsize_p,
521                                 uint64_t *dfree_p,
522                                 uint64_t *dsize_p)
523 {
524         struct statvfs statvfs = { 0, };
525         int ret;
526
527         ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
528         if (ret < 0) {
529                 return -1;
530         }
531
532         if (bsize_p != NULL) {
533                 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
534         }
535         if (dfree_p != NULL) {
536                 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
537         }
538         if (dsize_p != NULL) {
539                 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
540         }
541
542         return (uint64_t)statvfs.f_bavail;
543 }
544
545 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
546                                 const struct smb_filename *smb_fname,
547                                 enum SMB_QUOTA_TYPE qtype,
548                                 unid_t id,
549                                 SMB_DISK_QUOTA *qt)
550 {
551         errno = ENOSYS;
552         return -1;
553 }
554
555 static int
556 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
557                       enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
558 {
559         errno = ENOSYS;
560         return -1;
561 }
562
563 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
564                                 const struct smb_filename *smb_fname,
565                                 struct vfs_statvfs_struct *vfs_statvfs)
566 {
567         struct statvfs statvfs = { 0, };
568         int ret;
569
570         ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
571         if (ret < 0) {
572                 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
573                           smb_fname->base_name, strerror(errno)));
574                 return -1;
575         }
576
577         ZERO_STRUCTP(vfs_statvfs);
578
579         vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
580         vfs_statvfs->BlockSize = statvfs.f_bsize;
581         vfs_statvfs->TotalBlocks = statvfs.f_blocks;
582         vfs_statvfs->BlocksAvail = statvfs.f_bfree;
583         vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
584         vfs_statvfs->TotalFileNodes = statvfs.f_files;
585         vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
586         vfs_statvfs->FsIdentifier = statvfs.f_fsid;
587         vfs_statvfs->FsCapabilities =
588             FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
589
590         return ret;
591 }
592
593 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
594                                             enum timestamp_set_resolution *p_ts_res)
595 {
596         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
597
598 #ifdef HAVE_GFAPI_VER_6
599         caps |= FILE_SUPPORTS_SPARSE_FILES;
600 #endif
601
602 #ifdef STAT_HAVE_NSEC
603         *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
604 #endif
605
606         return caps;
607 }
608
609 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
610                                          const files_struct *fsp)
611 {
612         glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
613         if (glfd == NULL) {
614                 DBG_INFO("Failed to fetch fsp extension\n");
615                 return NULL;
616         }
617         if (*glfd == NULL) {
618                 DBG_INFO("Empty glfs_fd_t pointer\n");
619                 return NULL;
620         }
621
622         return *glfd;
623 }
624
625 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
626                                   files_struct *fsp, const char *mask,
627                                   uint32_t attributes)
628 {
629         glfs_fd_t *glfd = NULL;
630
631         glfd = glfs_opendir(handle->data, fsp->fsp_name->base_name);
632         if (glfd == NULL) {
633                 return NULL;
634         }
635
636         return (DIR *)glfd;
637 }
638
639 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
640 {
641         int ret;
642
643         START_PROFILE(syscall_closedir);
644         ret = glfs_closedir((void *)dirp);
645         END_PROFILE(syscall_closedir);
646
647         return ret;
648 }
649
650 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
651                                           struct files_struct *dirfsp,
652                                           DIR *dirp,
653                                           SMB_STRUCT_STAT *sbuf)
654 {
655         static char direntbuf[512];
656         int ret;
657         struct stat stat;
658         struct dirent *dirent = 0;
659
660         START_PROFILE(syscall_readdir);
661         if (sbuf != NULL) {
662                 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
663                                          &dirent);
664         } else {
665                 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
666         }
667
668         if ((ret < 0) || (dirent == NULL)) {
669                 END_PROFILE(syscall_readdir);
670                 return NULL;
671         }
672
673         if (sbuf != NULL) {
674                 SET_STAT_INVALID(*sbuf);
675                 if (!S_ISLNK(stat.st_mode)) {
676                         smb_stat_ex_from_stat(sbuf, &stat);
677                 }
678         }
679
680         END_PROFILE(syscall_readdir);
681         return dirent;
682 }
683
684 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
685 {
686         long ret;
687
688         START_PROFILE(syscall_telldir);
689         ret = glfs_telldir((void *)dirp);
690         END_PROFILE(syscall_telldir);
691
692         return ret;
693 }
694
695 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
696                                 long offset)
697 {
698         START_PROFILE(syscall_seekdir);
699         glfs_seekdir((void *)dirp, offset);
700         END_PROFILE(syscall_seekdir);
701 }
702
703 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
704 {
705         START_PROFILE(syscall_rewinddir);
706         glfs_seekdir((void *)dirp, 0);
707         END_PROFILE(syscall_rewinddir);
708 }
709
710 static int vfs_gluster_mkdirat(struct vfs_handle_struct *handle,
711                         struct files_struct *dirfsp,
712                         const struct smb_filename *smb_fname,
713                         mode_t mode)
714 {
715         int ret;
716
717 #ifdef HAVE_GFAPI_VER_7_11
718         glfs_fd_t *pglfd = NULL;
719
720         START_PROFILE(syscall_mkdirat);
721
722         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
723         if (pglfd == NULL) {
724                 END_PROFILE(syscall_mkdirat);
725                 DBG_ERR("Failed to fetch gluster fd\n");
726                 return -1;
727         }
728
729         ret = glfs_mkdirat(pglfd, smb_fname->base_name, mode);
730 #else
731         struct smb_filename *full_fname = NULL;
732
733         START_PROFILE(syscall_mkdirat);
734
735         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
736                                                   dirfsp,
737                                                   smb_fname);
738         if (full_fname == NULL) {
739                 END_PROFILE(syscall_mkdirat);
740                 return -1;
741         }
742
743         ret = glfs_mkdir(handle->data, full_fname->base_name, mode);
744
745         TALLOC_FREE(full_fname);
746 #endif
747
748         END_PROFILE(syscall_mkdirat);
749
750         return ret;
751 }
752
753 static int vfs_gluster_openat(struct vfs_handle_struct *handle,
754                               const struct files_struct *dirfsp,
755                               const struct smb_filename *smb_fname,
756                               files_struct *fsp,
757                               const struct vfs_open_how *how)
758 {
759         int flags = how->flags;
760         struct smb_filename *full_fname = NULL;
761         bool have_opath = false;
762         bool became_root = false;
763         glfs_fd_t *glfd;
764         glfs_fd_t *pglfd = NULL;
765         glfs_fd_t **p_tmp;
766
767         START_PROFILE(syscall_openat);
768
769         if (how->resolve != 0) {
770                 END_PROFILE(syscall_openat);
771                 errno = ENOSYS;
772                 return -1;
773         }
774
775         p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
776         if (p_tmp == NULL) {
777                 END_PROFILE(syscall_openat);
778                 errno = ENOMEM;
779                 return -1;
780         }
781
782 #ifdef O_PATH
783         have_opath = true;
784         if (fsp->fsp_flags.is_pathref) {
785                 flags |= O_PATH;
786         }
787 #endif
788
789         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
790                                                   dirfsp,
791                                                   smb_fname);
792         if (full_fname == NULL) {
793                 END_PROFILE(syscall_openat);
794                 return -1;
795         }
796
797         if (fsp->fsp_flags.is_pathref && !have_opath) {
798                 become_root();
799                 became_root = true;
800         }
801
802         if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
803 #ifdef HAVE_GFAPI_VER_7_11
804                 /*
805                  * Fetch Gluster fd for parent directory using dirfsp
806                  * before calling glfs_openat();
807                  */
808                 pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
809                 if (pglfd == NULL) {
810                         END_PROFILE(syscall_openat);
811                         DBG_ERR("Failed to fetch gluster fd\n");
812                         return -1;
813                 }
814
815                 glfd = glfs_openat(pglfd,
816                                    smb_fname->base_name,
817                                    flags,
818                                    how->mode);
819 #else
820                 /*
821                  * Replace smb_fname with full_path constructed above.
822                  */
823                 smb_fname = full_fname;
824 #endif
825         }
826
827         if (pglfd == NULL) {
828                 /*
829                  * smb_fname can either be a full_path or the same one
830                  * as received from the caller. In the latter case we
831                  * are operating at current working directory.
832                  */
833                 if (flags & O_CREAT) {
834                         glfd = glfs_creat(handle->data,
835                                           smb_fname->base_name,
836                                           flags,
837                                           how->mode);
838                 } else {
839                         glfd = glfs_open(handle->data,
840                                          smb_fname->base_name,
841                                          flags);
842                 }
843         }
844
845         if (became_root) {
846                 unbecome_root();
847         }
848
849         TALLOC_FREE(full_fname);
850
851         fsp->fsp_flags.have_proc_fds = false;
852
853         if (glfd == NULL) {
854                 END_PROFILE(syscall_openat);
855                 /* no extension destroy_fn, so no need to save errno */
856                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
857                 return -1;
858         }
859
860         *p_tmp = glfd;
861
862         END_PROFILE(syscall_openat);
863         /* An arbitrary value for error reporting, so you know its us. */
864         return 13371337;
865 }
866
867 static int vfs_gluster_close(struct vfs_handle_struct *handle,
868                              files_struct *fsp)
869 {
870         int ret;
871         glfs_fd_t *glfd = NULL;
872
873         START_PROFILE(syscall_close);
874
875         glfd = vfs_gluster_fetch_glfd(handle, fsp);
876         if (glfd == NULL) {
877                 END_PROFILE(syscall_close);
878                 DBG_ERR("Failed to fetch gluster fd\n");
879                 return -1;
880         }
881
882         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
883
884         ret = glfs_close(glfd);
885         END_PROFILE(syscall_close);
886
887         return ret;
888 }
889
890 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
891                                  files_struct *fsp, void *data, size_t n,
892                                  off_t offset)
893 {
894         ssize_t ret;
895         glfs_fd_t *glfd = NULL;
896
897         START_PROFILE_BYTES(syscall_pread, n);
898
899         glfd = vfs_gluster_fetch_glfd(handle, fsp);
900         if (glfd == NULL) {
901                 END_PROFILE_BYTES(syscall_pread);
902                 DBG_ERR("Failed to fetch gluster fd\n");
903                 return -1;
904         }
905
906 #ifdef HAVE_GFAPI_VER_7_6
907         ret = glfs_pread(glfd, data, n, offset, 0, NULL);
908 #else
909         ret = glfs_pread(glfd, data, n, offset, 0);
910 #endif
911         END_PROFILE_BYTES(syscall_pread);
912
913         return ret;
914 }
915
916 struct vfs_gluster_pread_state {
917         ssize_t ret;
918         glfs_fd_t *fd;
919         void *buf;
920         size_t count;
921         off_t offset;
922
923         struct vfs_aio_state vfs_aio_state;
924         SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
925 };
926
927 static void vfs_gluster_pread_do(void *private_data);
928 static void vfs_gluster_pread_done(struct tevent_req *subreq);
929 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state);
930
931 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
932                                                   *handle, TALLOC_CTX *mem_ctx,
933                                                   struct tevent_context *ev,
934                                                   files_struct *fsp,
935                                                   void *data, size_t n,
936                                                   off_t offset)
937 {
938         struct vfs_gluster_pread_state *state;
939         struct tevent_req *req, *subreq;
940
941         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
942         if (glfd == NULL) {
943                 DBG_ERR("Failed to fetch gluster fd\n");
944                 return NULL;
945         }
946
947         req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pread_state);
948         if (req == NULL) {
949                 return NULL;
950         }
951
952         state->ret = -1;
953         state->fd = glfd;
954         state->buf = data;
955         state->count = n;
956         state->offset = offset;
957
958         SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
959                                      state->profile_bytes, n);
960         SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
961
962         subreq = pthreadpool_tevent_job_send(
963                 state, ev, handle->conn->sconn->pool,
964                 vfs_gluster_pread_do, state);
965         if (tevent_req_nomem(subreq, req)) {
966                 return tevent_req_post(req, ev);
967         }
968         tevent_req_set_callback(subreq, vfs_gluster_pread_done, req);
969
970         talloc_set_destructor(state, vfs_gluster_pread_state_destructor);
971
972         return req;
973 }
974
975 static void vfs_gluster_pread_do(void *private_data)
976 {
977         struct vfs_gluster_pread_state *state = talloc_get_type_abort(
978                 private_data, struct vfs_gluster_pread_state);
979         struct timespec start_time;
980         struct timespec end_time;
981
982         SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
983
984         PROFILE_TIMESTAMP(&start_time);
985
986         do {
987 #ifdef HAVE_GFAPI_VER_7_6
988                 state->ret = glfs_pread(state->fd, state->buf, state->count,
989                                         state->offset, 0, NULL);
990 #else
991                 state->ret = glfs_pread(state->fd, state->buf, state->count,
992                                         state->offset, 0);
993 #endif
994         } while ((state->ret == -1) && (errno == EINTR));
995
996         if (state->ret == -1) {
997                 state->vfs_aio_state.error = errno;
998         }
999
1000         PROFILE_TIMESTAMP(&end_time);
1001
1002         state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1003
1004         SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1005 }
1006
1007 static int vfs_gluster_pread_state_destructor(struct vfs_gluster_pread_state *state)
1008 {
1009         return -1;
1010 }
1011
1012 static void vfs_gluster_pread_done(struct tevent_req *subreq)
1013 {
1014         struct tevent_req *req = tevent_req_callback_data(
1015                 subreq, struct tevent_req);
1016         struct vfs_gluster_pread_state *state = tevent_req_data(
1017                 req, struct vfs_gluster_pread_state);
1018         int ret;
1019
1020         ret = pthreadpool_tevent_job_recv(subreq);
1021         TALLOC_FREE(subreq);
1022         SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1023         talloc_set_destructor(state, NULL);
1024         if (ret != 0) {
1025                 if (ret != EAGAIN) {
1026                         tevent_req_error(req, ret);
1027                         return;
1028                 }
1029                 /*
1030                  * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1031                  * means the lower level pthreadpool failed to create a new
1032                  * thread. Fallback to sync processing in that case to allow
1033                  * some progress for the client.
1034                  */
1035                 vfs_gluster_pread_do(state);
1036         }
1037
1038         tevent_req_done(req);
1039 }
1040
1041 static ssize_t vfs_gluster_pread_recv(struct tevent_req *req,
1042                                       struct vfs_aio_state *vfs_aio_state)
1043 {
1044         struct vfs_gluster_pread_state *state = tevent_req_data(
1045                 req, struct vfs_gluster_pread_state);
1046
1047         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1048                 return -1;
1049         }
1050
1051         *vfs_aio_state = state->vfs_aio_state;
1052         return state->ret;
1053 }
1054
1055 struct vfs_gluster_pwrite_state {
1056         ssize_t ret;
1057         glfs_fd_t *fd;
1058         const void *buf;
1059         size_t count;
1060         off_t offset;
1061
1062         struct vfs_aio_state vfs_aio_state;
1063         SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1064 };
1065
1066 static void vfs_gluster_pwrite_do(void *private_data);
1067 static void vfs_gluster_pwrite_done(struct tevent_req *subreq);
1068 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state);
1069
1070 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
1071                                                   *handle, TALLOC_CTX *mem_ctx,
1072                                                   struct tevent_context *ev,
1073                                                   files_struct *fsp,
1074                                                   const void *data, size_t n,
1075                                                   off_t offset)
1076 {
1077         struct tevent_req *req, *subreq;
1078         struct vfs_gluster_pwrite_state *state;
1079
1080         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1081         if (glfd == NULL) {
1082                 DBG_ERR("Failed to fetch gluster fd\n");
1083                 return NULL;
1084         }
1085
1086         req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_pwrite_state);
1087         if (req == NULL) {
1088                 return NULL;
1089         }
1090
1091         state->ret = -1;
1092         state->fd = glfd;
1093         state->buf = data;
1094         state->count = n;
1095         state->offset = offset;
1096
1097         SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
1098                                      state->profile_bytes, n);
1099         SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1100
1101         subreq = pthreadpool_tevent_job_send(
1102                 state, ev, handle->conn->sconn->pool,
1103                 vfs_gluster_pwrite_do, state);
1104         if (tevent_req_nomem(subreq, req)) {
1105                 return tevent_req_post(req, ev);
1106         }
1107         tevent_req_set_callback(subreq, vfs_gluster_pwrite_done, req);
1108
1109         talloc_set_destructor(state, vfs_gluster_pwrite_state_destructor);
1110
1111         return req;
1112 }
1113
1114 static void vfs_gluster_pwrite_do(void *private_data)
1115 {
1116         struct vfs_gluster_pwrite_state *state = talloc_get_type_abort(
1117                 private_data, struct vfs_gluster_pwrite_state);
1118         struct timespec start_time;
1119         struct timespec end_time;
1120
1121         SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1122
1123         PROFILE_TIMESTAMP(&start_time);
1124
1125         do {
1126 #ifdef HAVE_GFAPI_VER_7_6
1127                 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1128                                          state->offset, 0, NULL, NULL);
1129 #else
1130                 state->ret = glfs_pwrite(state->fd, state->buf, state->count,
1131                                          state->offset, 0);
1132 #endif
1133         } while ((state->ret == -1) && (errno == EINTR));
1134
1135         if (state->ret == -1) {
1136                 state->vfs_aio_state.error = errno;
1137         }
1138
1139         PROFILE_TIMESTAMP(&end_time);
1140
1141         state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1142
1143         SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1144 }
1145
1146 static int vfs_gluster_pwrite_state_destructor(struct vfs_gluster_pwrite_state *state)
1147 {
1148         return -1;
1149 }
1150
1151 static void vfs_gluster_pwrite_done(struct tevent_req *subreq)
1152 {
1153         struct tevent_req *req = tevent_req_callback_data(
1154                 subreq, struct tevent_req);
1155         struct vfs_gluster_pwrite_state *state = tevent_req_data(
1156                 req, struct vfs_gluster_pwrite_state);
1157         int ret;
1158
1159         ret = pthreadpool_tevent_job_recv(subreq);
1160         TALLOC_FREE(subreq);
1161         SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1162         talloc_set_destructor(state, NULL);
1163         if (ret != 0) {
1164                 if (ret != EAGAIN) {
1165                         tevent_req_error(req, ret);
1166                         return;
1167                 }
1168                 /*
1169                  * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1170                  * means the lower level pthreadpool failed to create a new
1171                  * thread. Fallback to sync processing in that case to allow
1172                  * some progress for the client.
1173                  */
1174                 vfs_gluster_pwrite_do(state);
1175         }
1176
1177         tevent_req_done(req);
1178 }
1179
1180 static ssize_t vfs_gluster_pwrite_recv(struct tevent_req *req,
1181                                        struct vfs_aio_state *vfs_aio_state)
1182 {
1183         struct vfs_gluster_pwrite_state *state = tevent_req_data(
1184                 req, struct vfs_gluster_pwrite_state);
1185
1186         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1187                 return -1;
1188         }
1189
1190         *vfs_aio_state = state->vfs_aio_state;
1191
1192         return state->ret;
1193 }
1194
1195 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
1196                                   files_struct *fsp, const void *data,
1197                                   size_t n, off_t offset)
1198 {
1199         ssize_t ret;
1200         glfs_fd_t *glfd = NULL;
1201
1202         START_PROFILE_BYTES(syscall_pwrite, n);
1203
1204         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1205         if (glfd == NULL) {
1206                 END_PROFILE_BYTES(syscall_pwrite);
1207                 DBG_ERR("Failed to fetch gluster fd\n");
1208                 return -1;
1209         }
1210
1211 #ifdef HAVE_GFAPI_VER_7_6
1212         ret = glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
1213 #else
1214         ret = glfs_pwrite(glfd, data, n, offset, 0);
1215 #endif
1216         END_PROFILE_BYTES(syscall_pwrite);
1217
1218         return ret;
1219 }
1220
1221 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
1222                                files_struct *fsp, off_t offset, int whence)
1223 {
1224         off_t ret = 0;
1225         glfs_fd_t *glfd = NULL;
1226
1227         START_PROFILE(syscall_lseek);
1228
1229         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1230         if (glfd == NULL) {
1231                 END_PROFILE(syscall_lseek);
1232                 DBG_ERR("Failed to fetch gluster fd\n");
1233                 return -1;
1234         }
1235
1236         ret = glfs_lseek(glfd, offset, whence);
1237         END_PROFILE(syscall_lseek);
1238
1239         return ret;
1240 }
1241
1242 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
1243                                     files_struct *fromfsp,
1244                                     const DATA_BLOB *hdr,
1245                                     off_t offset, size_t n)
1246 {
1247         errno = ENOTSUP;
1248         return -1;
1249 }
1250
1251 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1252                                     int fromfd, files_struct *tofsp,
1253                                     off_t offset, size_t n)
1254 {
1255         errno = ENOTSUP;
1256         return -1;
1257 }
1258
1259 static int vfs_gluster_renameat(struct vfs_handle_struct *handle,
1260                         files_struct *srcfsp,
1261                         const struct smb_filename *smb_fname_src,
1262                         files_struct *dstfsp,
1263                         const struct smb_filename *smb_fname_dst)
1264 {
1265         int ret;
1266
1267 #ifdef HAVE_GFAPI_VER_7_11
1268         glfs_fd_t *src_pglfd = NULL;
1269         glfs_fd_t *dst_pglfd = NULL;
1270
1271         START_PROFILE(syscall_renameat);
1272
1273         src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
1274         if (src_pglfd == NULL) {
1275                 END_PROFILE(syscall_renameat);
1276                 DBG_ERR("Failed to fetch gluster fd\n");
1277                 return -1;
1278         }
1279
1280         dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
1281         if (dst_pglfd == NULL) {
1282                 END_PROFILE(syscall_renameat);
1283                 DBG_ERR("Failed to fetch gluster fd\n");
1284                 return -1;
1285         }
1286
1287         ret = glfs_renameat(src_pglfd, smb_fname_src->base_name,
1288                             dst_pglfd, smb_fname_dst->base_name);
1289 #else
1290         struct smb_filename *full_fname_src = NULL;
1291         struct smb_filename *full_fname_dst = NULL;
1292
1293         START_PROFILE(syscall_renameat);
1294
1295         full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1296                                                       srcfsp,
1297                                                       smb_fname_src);
1298         if (full_fname_src == NULL) {
1299                 END_PROFILE(syscall_renameat);
1300                 errno = ENOMEM;
1301                 return -1;
1302         }
1303
1304         full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1305                                                       dstfsp,
1306                                                       smb_fname_dst);
1307         if (full_fname_dst == NULL) {
1308                 END_PROFILE(syscall_renameat);
1309                 TALLOC_FREE(full_fname_src);
1310                 errno = ENOMEM;
1311                 return -1;
1312         }
1313         ret = glfs_rename(handle->data,
1314                           full_fname_src->base_name,
1315                           full_fname_dst->base_name);
1316
1317         TALLOC_FREE(full_fname_src);
1318         TALLOC_FREE(full_fname_dst);
1319 #endif
1320
1321         END_PROFILE(syscall_renameat);
1322
1323         return ret;
1324 }
1325
1326 struct vfs_gluster_fsync_state {
1327         ssize_t ret;
1328         glfs_fd_t *fd;
1329
1330         struct vfs_aio_state vfs_aio_state;
1331         SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
1332 };
1333
1334 static void vfs_gluster_fsync_do(void *private_data);
1335 static void vfs_gluster_fsync_done(struct tevent_req *subreq);
1336 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state);
1337
1338 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1339                                                  *handle, TALLOC_CTX *mem_ctx,
1340                                                  struct tevent_context *ev,
1341                                                  files_struct *fsp)
1342 {
1343         struct tevent_req *req, *subreq;
1344         struct vfs_gluster_fsync_state *state;
1345
1346         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1347         if (glfd == NULL) {
1348                 DBG_ERR("Failed to fetch gluster fd\n");
1349                 return NULL;
1350         }
1351
1352         req = tevent_req_create(mem_ctx, &state, struct vfs_gluster_fsync_state);
1353         if (req == NULL) {
1354                 return NULL;
1355         }
1356
1357         state->ret = -1;
1358         state->fd = glfd;
1359
1360         SMBPROFILE_BYTES_ASYNC_START(syscall_asys_fsync, profile_p,
1361                                      state->profile_bytes, 0);
1362         SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1363
1364         subreq = pthreadpool_tevent_job_send(
1365                 state, ev, handle->conn->sconn->pool, vfs_gluster_fsync_do, state);
1366         if (tevent_req_nomem(subreq, req)) {
1367                 return tevent_req_post(req, ev);
1368         }
1369         tevent_req_set_callback(subreq, vfs_gluster_fsync_done, req);
1370
1371         talloc_set_destructor(state, vfs_gluster_fsync_state_destructor);
1372
1373         return req;
1374 }
1375
1376 static void vfs_gluster_fsync_do(void *private_data)
1377 {
1378         struct vfs_gluster_fsync_state *state = talloc_get_type_abort(
1379                 private_data, struct vfs_gluster_fsync_state);
1380         struct timespec start_time;
1381         struct timespec end_time;
1382
1383         SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
1384
1385         PROFILE_TIMESTAMP(&start_time);
1386
1387         do {
1388 #ifdef HAVE_GFAPI_VER_7_6
1389                 state->ret = glfs_fsync(state->fd, NULL, NULL);
1390 #else
1391                 state->ret = glfs_fsync(state->fd);
1392 #endif
1393         } while ((state->ret == -1) && (errno == EINTR));
1394
1395         if (state->ret == -1) {
1396                 state->vfs_aio_state.error = errno;
1397         }
1398
1399         PROFILE_TIMESTAMP(&end_time);
1400
1401         state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
1402
1403         SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
1404 }
1405
1406 static int vfs_gluster_fsync_state_destructor(struct vfs_gluster_fsync_state *state)
1407 {
1408         return -1;
1409 }
1410
1411 static void vfs_gluster_fsync_done(struct tevent_req *subreq)
1412 {
1413         struct tevent_req *req = tevent_req_callback_data(
1414                 subreq, struct tevent_req);
1415         struct vfs_gluster_fsync_state *state = tevent_req_data(
1416                 req, struct vfs_gluster_fsync_state);
1417         int ret;
1418
1419         ret = pthreadpool_tevent_job_recv(subreq);
1420         TALLOC_FREE(subreq);
1421         SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
1422         talloc_set_destructor(state, NULL);
1423         if (ret != 0) {
1424                 if (ret != EAGAIN) {
1425                         tevent_req_error(req, ret);
1426                         return;
1427                 }
1428                 /*
1429                  * If we get EAGAIN from pthreadpool_tevent_job_recv() this
1430                  * means the lower level pthreadpool failed to create a new
1431                  * thread. Fallback to sync processing in that case to allow
1432                  * some progress for the client.
1433                  */
1434                 vfs_gluster_fsync_do(state);
1435         }
1436
1437         tevent_req_done(req);
1438 }
1439
1440 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1441                                   struct vfs_aio_state *vfs_aio_state)
1442 {
1443         struct vfs_gluster_fsync_state *state = tevent_req_data(
1444                 req, struct vfs_gluster_fsync_state);
1445
1446         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1447                 return -1;
1448         }
1449
1450         *vfs_aio_state = state->vfs_aio_state;
1451         return state->ret;
1452 }
1453
1454 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1455                             struct smb_filename *smb_fname)
1456 {
1457         struct stat st;
1458         int ret;
1459
1460         START_PROFILE(syscall_stat);
1461         ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1462         if (ret == 0) {
1463                 smb_stat_ex_from_stat(&smb_fname->st, &st);
1464         }
1465         if (ret < 0 && errno != ENOENT) {
1466                 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1467                           smb_fname->base_name, strerror(errno)));
1468         }
1469         END_PROFILE(syscall_stat);
1470
1471         return ret;
1472 }
1473
1474 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1475                              files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1476 {
1477         struct stat st;
1478         int ret;
1479         glfs_fd_t *glfd = NULL;
1480
1481         START_PROFILE(syscall_fstat);
1482
1483         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1484         if (glfd == NULL) {
1485                 END_PROFILE(syscall_fstat);
1486                 DBG_ERR("Failed to fetch gluster fd\n");
1487                 return -1;
1488         }
1489
1490         ret = glfs_fstat(glfd, &st);
1491         if (ret == 0) {
1492                 smb_stat_ex_from_stat(sbuf, &st);
1493         }
1494         if (ret < 0) {
1495                 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1496                           fsp_get_io_fd(fsp), strerror(errno)));
1497         }
1498         END_PROFILE(syscall_fstat);
1499
1500         return ret;
1501 }
1502
1503 static int vfs_gluster_fstatat(struct vfs_handle_struct *handle,
1504                                const struct files_struct *dirfsp,
1505                                const struct smb_filename *smb_fname,
1506                                SMB_STRUCT_STAT *sbuf,
1507                                int flags)
1508 {
1509         struct stat st;
1510         int ret;
1511
1512 #ifdef HAVE_GFAPI_VER_7_11
1513         glfs_fd_t *pglfd = NULL;
1514
1515         START_PROFILE(syscall_fstatat);
1516
1517         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1518         if (pglfd == NULL) {
1519                 END_PROFILE(syscall_fstatat);
1520                 DBG_ERR("Failed to fetch gluster fd\n");
1521                 return -1;
1522         }
1523
1524         ret = glfs_fstatat(pglfd, smb_fname->base_name, &st, flags);
1525 #else
1526         struct smb_filename *full_fname = NULL;
1527
1528         START_PROFILE(syscall_fstatat);
1529
1530         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1531                                                   dirfsp,
1532                                                   smb_fname);
1533         if (full_fname == NULL) {
1534                 END_PROFILE(syscall_fstatat);
1535                 return -1;
1536         }
1537
1538         ret = glfs_stat(handle->data, full_fname->base_name, &st);
1539
1540         TALLOC_FREE(full_fname->base_name);
1541 #endif
1542
1543         if (ret == 0) {
1544                 smb_stat_ex_from_stat(sbuf, &st);
1545         }
1546
1547         END_PROFILE(syscall_fstatat);
1548
1549         return ret;
1550 }
1551
1552 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1553                              struct smb_filename *smb_fname)
1554 {
1555         struct stat st;
1556         int ret;
1557
1558         START_PROFILE(syscall_lstat);
1559         ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1560         if (ret == 0) {
1561                 smb_stat_ex_from_stat(&smb_fname->st, &st);
1562         }
1563         if (ret < 0 && errno != ENOENT) {
1564                 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1565                           smb_fname->base_name, strerror(errno)));
1566         }
1567         END_PROFILE(syscall_lstat);
1568
1569         return ret;
1570 }
1571
1572 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1573                                            files_struct *fsp,
1574                                            const SMB_STRUCT_STAT *sbuf)
1575 {
1576         uint64_t ret;
1577
1578         START_PROFILE(syscall_get_alloc_size);
1579         ret = sbuf->st_ex_blocks * 512;
1580         END_PROFILE(syscall_get_alloc_size);
1581
1582         return ret;
1583 }
1584
1585 static int vfs_gluster_unlinkat(struct vfs_handle_struct *handle,
1586                         struct files_struct *dirfsp,
1587                         const struct smb_filename *smb_fname,
1588                         int flags)
1589 {
1590         int ret;
1591
1592 #ifdef HAVE_GFAPI_VER_7_11
1593         glfs_fd_t *pglfd = NULL;
1594
1595         START_PROFILE(syscall_unlinkat);
1596
1597         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
1598         if (pglfd == NULL) {
1599                 END_PROFILE(syscall_unlinkat);
1600                 DBG_ERR("Failed to fetch gluster fd\n");
1601                 return -1;
1602         }
1603
1604         ret = glfs_unlinkat(pglfd, smb_fname->base_name, flags);
1605 #else
1606         struct smb_filename *full_fname = NULL;
1607
1608         START_PROFILE(syscall_unlinkat);
1609
1610         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1611                                                   dirfsp,
1612                                                   smb_fname);
1613         if (full_fname == NULL) {
1614                 END_PROFILE(syscall_unlinkat);
1615                 return -1;
1616         }
1617
1618         if (flags & AT_REMOVEDIR) {
1619                 ret = glfs_rmdir(handle->data, full_fname->base_name);
1620         } else {
1621                 ret = glfs_unlink(handle->data, full_fname->base_name);
1622         }
1623
1624         TALLOC_FREE(full_fname);
1625 #endif
1626
1627         END_PROFILE(syscall_unlinkat);
1628
1629         return ret;
1630 }
1631
1632 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1633                               files_struct *fsp, mode_t mode)
1634 {
1635         int ret;
1636         glfs_fd_t *glfd = NULL;
1637
1638         START_PROFILE(syscall_fchmod);
1639
1640         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1641         if (glfd == NULL) {
1642                 END_PROFILE(syscall_fchmod);
1643                 DBG_ERR("Failed to fetch gluster fd\n");
1644                 return -1;
1645         }
1646
1647         if (!fsp->fsp_flags.is_pathref) {
1648                 /*
1649                  * We can use an io_fd to remove xattrs.
1650                  */
1651                 ret = glfs_fchmod(glfd, mode);
1652         } else {
1653                 /*
1654                  * This is no longer a handle based call.
1655                  */
1656                 ret = glfs_chmod(handle->data, fsp->fsp_name->base_name, mode);
1657         }
1658         END_PROFILE(syscall_fchmod);
1659
1660         return ret;
1661 }
1662
1663 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1664                               files_struct *fsp, uid_t uid, gid_t gid)
1665 {
1666         int ret;
1667         glfs_fd_t *glfd = NULL;
1668
1669         START_PROFILE(syscall_fchown);
1670
1671         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1672         if (glfd == NULL) {
1673                 END_PROFILE(syscall_fchown);
1674                 DBG_ERR("Failed to fetch gluster fd\n");
1675                 return -1;
1676         }
1677
1678         ret = glfs_fchown(glfd, uid, gid);
1679         END_PROFILE(syscall_fchown);
1680
1681         return ret;
1682 }
1683
1684 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1685                         const struct smb_filename *smb_fname,
1686                         uid_t uid,
1687                         gid_t gid)
1688 {
1689         int ret;
1690
1691         START_PROFILE(syscall_lchown);
1692         ret = glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1693         END_PROFILE(syscall_lchown);
1694
1695         return ret;
1696 }
1697
1698 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1699                         const struct smb_filename *smb_fname)
1700 {
1701         int ret;
1702
1703         START_PROFILE(syscall_chdir);
1704         ret = glfs_chdir(handle->data, smb_fname->base_name);
1705         END_PROFILE(syscall_chdir);
1706
1707         return ret;
1708 }
1709
1710 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1711                                 TALLOC_CTX *ctx)
1712 {
1713         char cwd[PATH_MAX] = { '\0' };
1714         char *ret;
1715         struct smb_filename *smb_fname = NULL;
1716
1717         START_PROFILE(syscall_getwd);
1718
1719         ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1720         END_PROFILE(syscall_getwd);
1721
1722         if (ret == NULL) {
1723                 return NULL;
1724         }
1725         smb_fname = synthetic_smb_fname(ctx,
1726                                         ret,
1727                                         NULL,
1728                                         NULL,
1729                                         0,
1730                                         0);
1731         return smb_fname;
1732 }
1733
1734 static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
1735                                files_struct *fsp,
1736                                struct smb_file_time *ft)
1737 {
1738         int ret = -1;
1739         struct timespec times[2];
1740         glfs_fd_t *glfd = NULL;
1741
1742         START_PROFILE(syscall_fntimes);
1743
1744         if (is_omit_timespec(&ft->atime)) {
1745                 times[0].tv_sec = fsp->fsp_name->st.st_ex_atime.tv_sec;
1746                 times[0].tv_nsec = fsp->fsp_name->st.st_ex_atime.tv_nsec;
1747         } else {
1748                 times[0].tv_sec = ft->atime.tv_sec;
1749                 times[0].tv_nsec = ft->atime.tv_nsec;
1750         }
1751
1752         if (is_omit_timespec(&ft->mtime)) {
1753                 times[1].tv_sec = fsp->fsp_name->st.st_ex_mtime.tv_sec;
1754                 times[1].tv_nsec = fsp->fsp_name->st.st_ex_mtime.tv_nsec;
1755         } else {
1756                 times[1].tv_sec = ft->mtime.tv_sec;
1757                 times[1].tv_nsec = ft->mtime.tv_nsec;
1758         }
1759
1760         if ((timespec_compare(&times[0],
1761                               &fsp->fsp_name->st.st_ex_atime) == 0) &&
1762             (timespec_compare(&times[1],
1763                               &fsp->fsp_name->st.st_ex_mtime) == 0)) {
1764                 END_PROFILE(syscall_fntimes);
1765                 return 0;
1766         }
1767
1768         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1769         if (glfd == NULL) {
1770                 END_PROFILE(syscall_fntimes);
1771                 DBG_ERR("Failed to fetch gluster fd\n");
1772                 return -1;
1773         }
1774
1775         if (!fsp->fsp_flags.is_pathref) {
1776                 ret = glfs_futimens(glfd, times);
1777         } else {
1778                 ret = glfs_utimens(handle->data,
1779                                    fsp->fsp_name->base_name,
1780                                    times);
1781         }
1782         END_PROFILE(syscall_fntimes);
1783
1784         return ret;
1785 }
1786
1787 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1788                                  files_struct *fsp, off_t offset)
1789 {
1790         int ret;
1791         glfs_fd_t *glfd = NULL;
1792
1793         START_PROFILE(syscall_ftruncate);
1794
1795         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1796         if (glfd == NULL) {
1797                 END_PROFILE(syscall_ftruncate);
1798                 DBG_ERR("Failed to fetch gluster fd\n");
1799                 return -1;
1800         }
1801
1802 #ifdef HAVE_GFAPI_VER_7_6
1803         ret = glfs_ftruncate(glfd, offset, NULL, NULL);
1804 #else
1805         ret = glfs_ftruncate(glfd, offset);
1806 #endif
1807         END_PROFILE(syscall_ftruncate);
1808
1809         return ret;
1810 }
1811
1812 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1813                                  struct files_struct *fsp,
1814                                  uint32_t mode,
1815                                  off_t offset, off_t len)
1816 {
1817         int ret;
1818 #ifdef HAVE_GFAPI_VER_6
1819         glfs_fd_t *glfd = NULL;
1820         int keep_size, punch_hole;
1821
1822         START_PROFILE(syscall_fallocate);
1823
1824         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1825         if (glfd == NULL) {
1826                 END_PROFILE(syscall_fallocate);
1827                 DBG_ERR("Failed to fetch gluster fd\n");
1828                 return -1;
1829         }
1830
1831         keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1832         punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1833
1834         mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1835         if (mode != 0) {
1836                 END_PROFILE(syscall_fallocate);
1837                 errno = ENOTSUP;
1838                 return -1;
1839         }
1840
1841         if (punch_hole) {
1842                 ret = glfs_discard(glfd, offset, len);
1843                 if (ret != 0) {
1844                         DBG_DEBUG("glfs_discard failed: %s\n",
1845                                   strerror(errno));
1846                 }
1847         }
1848
1849         ret = glfs_fallocate(glfd, keep_size, offset, len);
1850         END_PROFILE(syscall_fallocate);
1851 #else
1852         errno = ENOTSUP;
1853         ret = -1;
1854 #endif
1855         return ret;
1856 }
1857
1858 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1859                                 TALLOC_CTX *ctx,
1860                                 const struct smb_filename *smb_fname)
1861 {
1862         char *result = NULL;
1863         struct smb_filename *result_fname = NULL;
1864         char *resolved_path = NULL;
1865
1866         START_PROFILE(syscall_realpath);
1867
1868         resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1869         if (resolved_path == NULL) {
1870                 END_PROFILE(syscall_realpath);
1871                 errno = ENOMEM;
1872                 return NULL;
1873         }
1874
1875         result = glfs_realpath(handle->data,
1876                         smb_fname->base_name,
1877                         resolved_path);
1878         if (result != NULL) {
1879                 result_fname = synthetic_smb_fname(ctx,
1880                                                    result,
1881                                                    NULL,
1882                                                    NULL,
1883                                                    0,
1884                                                    0);
1885         }
1886
1887         SAFE_FREE(resolved_path);
1888         END_PROFILE(syscall_realpath);
1889
1890         return result_fname;
1891 }
1892
1893 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1894                              files_struct *fsp, int op, off_t offset,
1895                              off_t count, int type)
1896 {
1897         struct flock flock = { 0, };
1898         int ret;
1899         glfs_fd_t *glfd = NULL;
1900         bool ok = false;
1901
1902         START_PROFILE(syscall_fcntl_lock);
1903
1904         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1905         if (glfd == NULL) {
1906                 DBG_ERR("Failed to fetch gluster fd\n");
1907                 ok = false;
1908                 goto out;
1909         }
1910
1911         flock.l_type = type;
1912         flock.l_whence = SEEK_SET;
1913         flock.l_start = offset;
1914         flock.l_len = count;
1915         flock.l_pid = 0;
1916
1917         ret = glfs_posix_lock(glfd, op, &flock);
1918
1919         if (op == F_GETLK) {
1920                 /* lock query, true if someone else has locked */
1921                 if ((ret != -1) &&
1922                     (flock.l_type != F_UNLCK) &&
1923                     (flock.l_pid != 0) && (flock.l_pid != getpid())) {
1924                         ok = true;
1925                         goto out;
1926                 }
1927                 /* not me */
1928                 ok = false;
1929                 goto out;
1930         }
1931
1932         if (ret == -1) {
1933                 ok = false;
1934                 goto out;
1935         }
1936
1937         ok = true;
1938 out:
1939         END_PROFILE(syscall_fcntl_lock);
1940
1941         return ok;
1942 }
1943
1944 static int vfs_gluster_filesystem_sharemode(struct vfs_handle_struct *handle,
1945                                             files_struct *fsp,
1946                                             uint32_t share_access,
1947                                             uint32_t access_mask)
1948 {
1949         errno = ENOSYS;
1950         return -1;
1951 }
1952
1953 static int vfs_gluster_fcntl(vfs_handle_struct *handle,
1954                              files_struct *fsp, int cmd, va_list cmd_arg)
1955 {
1956         /*
1957          * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1958          * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1959          */
1960         if (cmd == F_GETFL) {
1961                 return 0;
1962         } else if (cmd == F_SETFL) {
1963                 va_list dup_cmd_arg;
1964                 int opt;
1965
1966                 va_copy(dup_cmd_arg, cmd_arg);
1967                 opt = va_arg(dup_cmd_arg, int);
1968                 va_end(dup_cmd_arg);
1969                 if (opt == 0) {
1970                         return 0;
1971                 }
1972                 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1973                 goto err_out;
1974         }
1975         DBG_ERR("unexpected fcntl: %d\n", cmd);
1976 err_out:
1977         errno = EINVAL;
1978         return -1;
1979 }
1980
1981 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1982                                       files_struct *fsp, int leasetype)
1983 {
1984         errno = ENOSYS;
1985         return -1;
1986 }
1987
1988 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1989                                 files_struct *fsp, off_t *poffset,
1990                                 off_t *pcount, int *ptype, pid_t *ppid)
1991 {
1992         struct flock flock = { 0, };
1993         int ret;
1994         glfs_fd_t *glfd = NULL;
1995
1996         START_PROFILE(syscall_fcntl_getlock);
1997
1998         glfd = vfs_gluster_fetch_glfd(handle, fsp);
1999         if (glfd == NULL) {
2000                 END_PROFILE(syscall_fcntl_getlock);
2001                 DBG_ERR("Failed to fetch gluster fd\n");
2002                 return false;
2003         }
2004
2005         flock.l_type = *ptype;
2006         flock.l_whence = SEEK_SET;
2007         flock.l_start = *poffset;
2008         flock.l_len = *pcount;
2009         flock.l_pid = 0;
2010
2011         ret = glfs_posix_lock(glfd, F_GETLK, &flock);
2012
2013         if (ret == -1) {
2014                 END_PROFILE(syscall_fcntl_getlock);
2015                 return false;
2016         }
2017
2018         *ptype = flock.l_type;
2019         *poffset = flock.l_start;
2020         *pcount = flock.l_len;
2021         *ppid = flock.l_pid;
2022         END_PROFILE(syscall_fcntl_getlock);
2023
2024         return true;
2025 }
2026
2027 static int vfs_gluster_symlinkat(struct vfs_handle_struct *handle,
2028                                 const struct smb_filename *link_target,
2029                                 struct files_struct *dirfsp,
2030                                 const struct smb_filename *new_smb_fname)
2031 {
2032         int ret;
2033
2034 #ifdef HAVE_GFAPI_VER_7_11
2035         glfs_fd_t *pglfd = NULL;
2036
2037         START_PROFILE(syscall_symlinkat);
2038
2039         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2040         if (pglfd == NULL) {
2041                 END_PROFILE(syscall_symlinkat);
2042                 DBG_ERR("Failed to fetch gluster fd\n");
2043                 return -1;
2044         }
2045
2046         ret = glfs_symlinkat(link_target->base_name,
2047                              pglfd,
2048                              new_smb_fname->base_name);
2049 #else
2050         struct smb_filename *full_fname = NULL;
2051
2052         START_PROFILE(syscall_symlinkat);
2053
2054         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2055                                                   dirfsp,
2056                                                   new_smb_fname);
2057         if (full_fname == NULL) {
2058                 END_PROFILE(syscall_symlinkat);
2059                 return -1;
2060         }
2061
2062         ret = glfs_symlink(handle->data,
2063                            link_target->base_name,
2064                            full_fname->base_name);
2065
2066         TALLOC_FREE(full_fname);
2067 #endif
2068
2069         END_PROFILE(syscall_symlinkat);
2070
2071         return ret;
2072 }
2073
2074 static int vfs_gluster_readlinkat(struct vfs_handle_struct *handle,
2075                                 const struct files_struct *dirfsp,
2076                                 const struct smb_filename *smb_fname,
2077                                 char *buf,
2078                                 size_t bufsiz)
2079 {
2080         int ret;
2081
2082 #ifdef HAVE_GFAPI_VER_7_11
2083         glfs_fd_t *pglfd = NULL;
2084
2085         START_PROFILE(syscall_readlinkat);
2086
2087         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2088         if (pglfd == NULL) {
2089                 END_PROFILE(syscall_readlinkat);
2090                 DBG_ERR("Failed to fetch gluster fd\n");
2091                 return -1;
2092         }
2093
2094         ret = glfs_readlinkat(pglfd, smb_fname->base_name, buf, bufsiz);
2095 #else
2096         struct smb_filename *full_fname = NULL;
2097
2098         START_PROFILE(syscall_readlinkat);
2099
2100         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2101                                                   dirfsp,
2102                                                   smb_fname);
2103         if (full_fname == NULL) {
2104                 END_PROFILE(syscall_readlinkat);
2105                 return -1;
2106         }
2107
2108         ret = glfs_readlink(handle->data, full_fname->base_name, buf, bufsiz);
2109
2110         TALLOC_FREE(full_fname);
2111 #endif
2112
2113         END_PROFILE(syscall_readlinkat);
2114
2115         return ret;
2116 }
2117
2118 static int vfs_gluster_linkat(struct vfs_handle_struct *handle,
2119                                 files_struct *srcfsp,
2120                                 const struct smb_filename *old_smb_fname,
2121                                 files_struct *dstfsp,
2122                                 const struct smb_filename *new_smb_fname,
2123                                 int flags)
2124 {
2125         int ret;
2126
2127 #ifdef HAVE_GFAPI_VER_7_11
2128         glfs_fd_t *src_pglfd = NULL;
2129         glfs_fd_t *dst_pglfd = NULL;
2130
2131         START_PROFILE(syscall_linkat);
2132
2133         src_pglfd = vfs_gluster_fetch_glfd(handle, srcfsp);
2134         if (src_pglfd == NULL) {
2135                 END_PROFILE(syscall_linkat);
2136                 DBG_ERR("Failed to fetch gluster fd\n");
2137                 return -1;
2138         }
2139
2140         dst_pglfd = vfs_gluster_fetch_glfd(handle, dstfsp);
2141         if (dst_pglfd == NULL) {
2142                 END_PROFILE(syscall_linkat);
2143                 DBG_ERR("Failed to fetch gluster fd\n");
2144                 return -1;
2145         }
2146
2147         ret = glfs_linkat(src_pglfd,
2148                           old_smb_fname->base_name,
2149                           dst_pglfd,
2150                           new_smb_fname->base_name,
2151                           flags);
2152 #else
2153         struct smb_filename *full_fname_old = NULL;
2154         struct smb_filename *full_fname_new = NULL;
2155
2156         START_PROFILE(syscall_linkat);
2157
2158         full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
2159                                                       srcfsp,
2160                                                       old_smb_fname);
2161         if (full_fname_old == NULL) {
2162                 END_PROFILE(syscall_linkat);
2163                 return -1;
2164         }
2165
2166         full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
2167                                                       dstfsp,
2168                                                       new_smb_fname);
2169         if (full_fname_new == NULL) {
2170                 END_PROFILE(syscall_linkat);
2171                 TALLOC_FREE(full_fname_old);
2172                 return -1;
2173         }
2174
2175         ret = glfs_link(handle->data,
2176                         full_fname_old->base_name,
2177                         full_fname_new->base_name);
2178
2179         TALLOC_FREE(full_fname_old);
2180         TALLOC_FREE(full_fname_new);
2181 #endif
2182
2183         END_PROFILE(syscall_linkat);
2184
2185         return ret;
2186 }
2187
2188 static int vfs_gluster_mknodat(struct vfs_handle_struct *handle,
2189                                 files_struct *dirfsp,
2190                                 const struct smb_filename *smb_fname,
2191                                 mode_t mode,
2192                                 SMB_DEV_T dev)
2193 {
2194         int ret;
2195
2196 #ifdef HAVE_GFAPI_VER_7_11
2197         glfs_fd_t *pglfd = NULL;
2198
2199         START_PROFILE(syscall_mknodat);
2200
2201         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2202         if (pglfd == NULL) {
2203                 END_PROFILE(syscall_mknodat);
2204                 DBG_ERR("Failed to fetch gluster fd\n");
2205                 return -1;
2206         }
2207
2208         ret = glfs_mknodat(pglfd, smb_fname->base_name, mode, dev);
2209 #else
2210         struct smb_filename *full_fname = NULL;
2211
2212         START_PROFILE(syscall_mknodat);
2213
2214         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2215                                                   dirfsp,
2216                                                   smb_fname);
2217         if (full_fname == NULL) {
2218                 END_PROFILE(syscall_mknodat);
2219                 return -1;
2220         }
2221
2222         ret = glfs_mknod(handle->data, full_fname->base_name, mode, dev);
2223
2224         TALLOC_FREE(full_fname);
2225 #endif
2226
2227         END_PROFILE(syscall_mknodat);
2228
2229         return ret;
2230 }
2231
2232 static int vfs_gluster_fchflags(struct vfs_handle_struct *handle,
2233                                 struct files_struct *fsp,
2234                                 unsigned int flags)
2235 {
2236         errno = ENOSYS;
2237         return -1;
2238 }
2239
2240 static NTSTATUS vfs_gluster_get_real_filename_at(
2241         struct vfs_handle_struct *handle,
2242         struct files_struct *dirfsp,
2243         const char *name,
2244         TALLOC_CTX *mem_ctx,
2245         char **found_name)
2246 {
2247         int ret;
2248         char key_buf[GLUSTER_NAME_MAX + 64];
2249         char val_buf[GLUSTER_NAME_MAX + 1];
2250
2251         if (strlen(name) >= GLUSTER_NAME_MAX) {
2252                 return NT_STATUS_OBJECT_NAME_INVALID;
2253         }
2254
2255         snprintf(key_buf, GLUSTER_NAME_MAX + 64,
2256                  "glusterfs.get_real_filename:%s", name);
2257
2258         ret = glfs_getxattr(handle->data,
2259                             dirfsp->fsp_name->base_name,
2260                             key_buf,
2261                             val_buf,
2262                             GLUSTER_NAME_MAX + 1);
2263         if (ret == -1) {
2264                 if (errno == ENOATTR) {
2265                         errno = ENOENT;
2266                 }
2267                 return map_nt_error_from_unix(errno);
2268         }
2269
2270         *found_name = talloc_strdup(mem_ctx, val_buf);
2271         if (found_name[0] == NULL) {
2272                 return NT_STATUS_NO_MEMORY;
2273         }
2274
2275         return NT_STATUS_OK;
2276 }
2277
2278 static const char *vfs_gluster_connectpath(
2279         struct vfs_handle_struct *handle,
2280         const struct files_struct *dirfsp,
2281         const struct smb_filename *smb_fname)
2282 {
2283         return handle->conn->connectpath;
2284 }
2285
2286 /* EA Operations */
2287
2288 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
2289                                      files_struct *fsp, const char *name,
2290                                      void *value, size_t size)
2291 {
2292         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2293         if (glfd == NULL) {
2294                 DBG_ERR("Failed to fetch gluster fd\n");
2295                 return -1;
2296         }
2297
2298         if (!fsp->fsp_flags.is_pathref) {
2299                 /*
2300                  * We can use an io_fd to retrieve xattr value.
2301                  */
2302                 return glfs_fgetxattr(glfd, name, value, size);
2303         }
2304
2305         /*
2306          * This is no longer a handle based call.
2307          */
2308         return glfs_getxattr(handle->data,
2309                              fsp->fsp_name->base_name,
2310                              name,
2311                              value,
2312                              size);
2313 }
2314
2315 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
2316                                       files_struct *fsp, char *list,
2317                                       size_t size)
2318 {
2319         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2320         if (glfd == NULL) {
2321                 DBG_ERR("Failed to fetch gluster fd\n");
2322                 return -1;
2323         }
2324         if (!fsp->fsp_flags.is_pathref) {
2325                 /*
2326                  * We can use an io_fd to list xattrs.
2327                  */
2328                 return glfs_flistxattr(glfd, list, size);
2329         } else {
2330                 /*
2331                  * This is no longer a handle based call.
2332                  */
2333                 return glfs_listxattr(handle->data,
2334                                 fsp->fsp_name->base_name,
2335                                 list,
2336                                 size);
2337         }
2338 }
2339
2340 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
2341                                     files_struct *fsp, const char *name)
2342 {
2343         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2344         if (glfd == NULL) {
2345                 DBG_ERR("Failed to fetch gluster fd\n");
2346                 return -1;
2347         }
2348         if (!fsp->fsp_flags.is_pathref) {
2349                 /*
2350                  * We can use an io_fd to remove xattrs.
2351                  */
2352                 return glfs_fremovexattr(glfd, name);
2353         } else {
2354                 /*
2355                  * This is no longer a handle based call.
2356                  */
2357                 return glfs_removexattr(handle->data,
2358                                 fsp->fsp_name->base_name,
2359                                 name);
2360         }
2361 }
2362
2363 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
2364                                  files_struct *fsp, const char *name,
2365                                  const void *value, size_t size, int flags)
2366 {
2367         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
2368         if (glfd == NULL) {
2369                 DBG_ERR("Failed to fetch gluster fd\n");
2370                 return -1;
2371         }
2372
2373         if (!fsp->fsp_flags.is_pathref) {
2374                 /*
2375                  * We can use an io_fd to set xattrs.
2376                  */
2377                 return glfs_fsetxattr(glfd, name, value, size, flags);
2378         } else {
2379                 /*
2380                  * This is no longer a handle based call.
2381                  */
2382                 return glfs_setxattr(handle->data,
2383                                 fsp->fsp_name->base_name,
2384                                 name,
2385                                 value,
2386                                 size,
2387                                 flags);
2388         }
2389 }
2390
2391 /* AIO Operations */
2392
2393 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
2394                                   files_struct *fsp)
2395 {
2396         return false;
2397 }
2398
2399 static NTSTATUS vfs_gluster_create_dfs_pathat(struct vfs_handle_struct *handle,
2400                                 struct files_struct *dirfsp,
2401                                 const struct smb_filename *smb_fname,
2402                                 const struct referral *reflist,
2403                                 size_t referral_count)
2404 {
2405         TALLOC_CTX *frame = talloc_stackframe();
2406         NTSTATUS status = NT_STATUS_NO_MEMORY;
2407         int ret;
2408         char *msdfs_link = NULL;
2409 #ifdef HAVE_GFAPI_VER_7_11
2410         glfs_fd_t *pglfd = NULL;
2411 #else
2412         struct smb_filename *full_fname = NULL;
2413 #endif
2414
2415         /* Form the msdfs_link contents */
2416         msdfs_link = msdfs_link_string(frame,
2417                                         reflist,
2418                                         referral_count);
2419         if (msdfs_link == NULL) {
2420                 goto out;
2421         }
2422
2423 #ifdef HAVE_GFAPI_VER_7_11
2424         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2425         if (pglfd == NULL) {
2426                 DBG_ERR("Failed to fetch gluster fd\n");
2427                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2428                 goto out;
2429         }
2430
2431         ret = glfs_symlinkat(msdfs_link, pglfd, smb_fname->base_name);
2432 #else
2433         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2434                                                   dirfsp,
2435                                                   smb_fname);
2436         if (full_fname == NULL) {
2437                 goto out;
2438         }
2439
2440         ret = glfs_symlink(handle->data, msdfs_link, full_fname->base_name);
2441
2442         TALLOC_FREE(full_fname);
2443 #endif
2444         if (ret == 0) {
2445                 status = NT_STATUS_OK;
2446         } else {
2447                 status = map_nt_error_from_unix(errno);
2448         }
2449
2450   out:
2451
2452         TALLOC_FREE(frame);
2453         return status;
2454 }
2455
2456 /*
2457  * Read and return the contents of a DFS redirect given a
2458  * pathname. A caller can pass in NULL for ppreflist and
2459  * preferral_count but still determine if this was a
2460  * DFS redirect point by getting NT_STATUS_OK back
2461  * without incurring the overhead of reading and parsing
2462  * the referral contents.
2463  */
2464
2465 static NTSTATUS vfs_gluster_read_dfs_pathat(struct vfs_handle_struct *handle,
2466                                 TALLOC_CTX *mem_ctx,
2467                                 struct files_struct *dirfsp,
2468                                 struct smb_filename *smb_fname,
2469                                 struct referral **ppreflist,
2470                                 size_t *preferral_count)
2471 {
2472         NTSTATUS status = NT_STATUS_NO_MEMORY;
2473         size_t bufsize;
2474         char *link_target = NULL;
2475         int referral_len;
2476         bool ok;
2477 #if defined(HAVE_BROKEN_READLINK)
2478         char link_target_buf[PATH_MAX];
2479 #else
2480         char link_target_buf[7];
2481 #endif
2482         struct stat st;
2483         struct smb_filename *full_fname = NULL;
2484         int ret;
2485 #ifdef HAVE_GFAPI_VER_7_11
2486         glfs_fd_t *pglfd = NULL;
2487 #endif
2488
2489         if (is_named_stream(smb_fname)) {
2490                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2491                 goto err;
2492         }
2493
2494         if (ppreflist == NULL && preferral_count == NULL) {
2495                 /*
2496                  * We're only checking if this is a DFS
2497                  * redirect. We don't need to return data.
2498                  */
2499                 bufsize = sizeof(link_target_buf);
2500                 link_target = link_target_buf;
2501         } else {
2502                 bufsize = PATH_MAX;
2503                 link_target = talloc_array(mem_ctx, char, bufsize);
2504                 if (!link_target) {
2505                         goto err;
2506                 }
2507         }
2508
2509         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2510                                                   dirfsp,
2511                                                   smb_fname);
2512         if (full_fname == NULL) {
2513                 status = NT_STATUS_NO_MEMORY;
2514                 goto err;
2515         }
2516
2517         ret = glfs_lstat(handle->data, full_fname->base_name, &st);
2518         if (ret < 0) {
2519                 status = map_nt_error_from_unix(errno);
2520                 goto err;
2521         }
2522
2523 #ifdef HAVE_GFAPI_VER_7_11
2524         pglfd = vfs_gluster_fetch_glfd(handle, dirfsp);
2525         if (pglfd == NULL) {
2526                 DBG_ERR("Failed to fetch gluster fd\n");
2527                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2528         }
2529
2530         referral_len = glfs_readlinkat(pglfd,
2531                                        smb_fname->base_name,
2532                                        link_target,
2533                                        bufsize - 1);
2534 #else
2535         referral_len = glfs_readlink(handle->data,
2536                                 full_fname->base_name,
2537                                 link_target,
2538                                 bufsize - 1);
2539 #endif
2540         if (referral_len < 0) {
2541                 if (errno == EINVAL) {
2542                         DBG_INFO("%s is not a link.\n", full_fname->base_name);
2543                         status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2544                 } else {
2545                         status = map_nt_error_from_unix(errno);
2546                         DBG_ERR("Error reading "
2547                                 "msdfs link %s: %s\n",
2548                                 full_fname->base_name,
2549                                 strerror(errno));
2550                 }
2551                 goto err;
2552         }
2553         link_target[referral_len] = '\0';
2554
2555         DBG_INFO("%s -> %s\n",
2556                         full_fname->base_name,
2557                         link_target);
2558
2559         if (!strnequal(link_target, "msdfs:", 6)) {
2560                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
2561                 goto err;
2562         }
2563
2564         if (ppreflist == NULL && preferral_count == NULL) {
2565                 /* Early return for checking if this is a DFS link. */
2566                 TALLOC_FREE(full_fname);
2567                 smb_stat_ex_from_stat(&smb_fname->st, &st);
2568                 return NT_STATUS_OK;
2569         }
2570
2571         ok = parse_msdfs_symlink(mem_ctx,
2572                         lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
2573                         link_target,
2574                         ppreflist,
2575                         preferral_count);
2576
2577         if (ok) {
2578                 smb_stat_ex_from_stat(&smb_fname->st, &st);
2579                 status = NT_STATUS_OK;
2580         } else {
2581                 status = NT_STATUS_NO_MEMORY;
2582         }
2583
2584   err:
2585
2586         if (link_target != link_target_buf) {
2587                 TALLOC_FREE(link_target);
2588         }
2589         TALLOC_FREE(full_fname);
2590         return status;
2591 }
2592
2593 static struct vfs_fn_pointers glusterfs_fns = {
2594
2595         /* Disk Operations */
2596
2597         .connect_fn = vfs_gluster_connect,
2598         .disconnect_fn = vfs_gluster_disconnect,
2599         .disk_free_fn = vfs_gluster_disk_free,
2600         .get_quota_fn = vfs_gluster_get_quota,
2601         .set_quota_fn = vfs_gluster_set_quota,
2602         .statvfs_fn = vfs_gluster_statvfs,
2603         .fs_capabilities_fn = vfs_gluster_fs_capabilities,
2604
2605         .get_dfs_referrals_fn = NULL,
2606
2607         /* Directory Operations */
2608
2609         .fdopendir_fn = vfs_gluster_fdopendir,
2610         .readdir_fn = vfs_gluster_readdir,
2611         .seekdir_fn = vfs_gluster_seekdir,
2612         .telldir_fn = vfs_gluster_telldir,
2613         .rewind_dir_fn = vfs_gluster_rewinddir,
2614         .mkdirat_fn = vfs_gluster_mkdirat,
2615         .closedir_fn = vfs_gluster_closedir,
2616
2617         /* File Operations */
2618
2619         .openat_fn = vfs_gluster_openat,
2620         .create_file_fn = NULL,
2621         .close_fn = vfs_gluster_close,
2622         .pread_fn = vfs_gluster_pread,
2623         .pread_send_fn = vfs_gluster_pread_send,
2624         .pread_recv_fn = vfs_gluster_pread_recv,
2625         .pwrite_fn = vfs_gluster_pwrite,
2626         .pwrite_send_fn = vfs_gluster_pwrite_send,
2627         .pwrite_recv_fn = vfs_gluster_pwrite_recv,
2628         .lseek_fn = vfs_gluster_lseek,
2629         .sendfile_fn = vfs_gluster_sendfile,
2630         .recvfile_fn = vfs_gluster_recvfile,
2631         .renameat_fn = vfs_gluster_renameat,
2632         .fsync_send_fn = vfs_gluster_fsync_send,
2633         .fsync_recv_fn = vfs_gluster_fsync_recv,
2634
2635         .stat_fn = vfs_gluster_stat,
2636         .fstat_fn = vfs_gluster_fstat,
2637         .fstatat_fn = vfs_gluster_fstatat,
2638         .lstat_fn = vfs_gluster_lstat,
2639         .get_alloc_size_fn = vfs_gluster_get_alloc_size,
2640         .unlinkat_fn = vfs_gluster_unlinkat,
2641
2642         .fchmod_fn = vfs_gluster_fchmod,
2643         .fchown_fn = vfs_gluster_fchown,
2644         .lchown_fn = vfs_gluster_lchown,
2645         .chdir_fn = vfs_gluster_chdir,
2646         .getwd_fn = vfs_gluster_getwd,
2647         .fntimes_fn = vfs_gluster_fntimes,
2648         .ftruncate_fn = vfs_gluster_ftruncate,
2649         .fallocate_fn = vfs_gluster_fallocate,
2650         .lock_fn = vfs_gluster_lock,
2651         .filesystem_sharemode_fn = vfs_gluster_filesystem_sharemode,
2652         .fcntl_fn = vfs_gluster_fcntl,
2653         .linux_setlease_fn = vfs_gluster_linux_setlease,
2654         .getlock_fn = vfs_gluster_getlock,
2655         .symlinkat_fn = vfs_gluster_symlinkat,
2656         .readlinkat_fn = vfs_gluster_readlinkat,
2657         .linkat_fn = vfs_gluster_linkat,
2658         .mknodat_fn = vfs_gluster_mknodat,
2659         .realpath_fn = vfs_gluster_realpath,
2660         .fchflags_fn = vfs_gluster_fchflags,
2661         .file_id_create_fn = NULL,
2662         .fstreaminfo_fn = NULL,
2663         .get_real_filename_at_fn = vfs_gluster_get_real_filename_at,
2664         .connectpath_fn = vfs_gluster_connectpath,
2665         .create_dfs_pathat_fn = vfs_gluster_create_dfs_pathat,
2666         .read_dfs_pathat_fn = vfs_gluster_read_dfs_pathat,
2667
2668         .brl_lock_windows_fn = NULL,
2669         .brl_unlock_windows_fn = NULL,
2670         .strict_lock_check_fn = NULL,
2671         .translate_name_fn = NULL,
2672         .fsctl_fn = NULL,
2673
2674         /* NT ACL Operations */
2675         .fget_nt_acl_fn = NULL,
2676         .fset_nt_acl_fn = NULL,
2677         .audit_file_fn = NULL,
2678
2679         /* Posix ACL Operations */
2680         .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
2681         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2682         .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
2683         .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
2684
2685         /* EA Operations */
2686         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2687         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2688         .fgetxattr_fn = vfs_gluster_fgetxattr,
2689         .flistxattr_fn = vfs_gluster_flistxattr,
2690         .fremovexattr_fn = vfs_gluster_fremovexattr,
2691         .fsetxattr_fn = vfs_gluster_fsetxattr,
2692
2693         /* AIO Operations */
2694         .aio_force_fn = vfs_gluster_aio_force,
2695
2696         /* Durable handle Operations */
2697         .durable_cookie_fn = NULL,
2698         .durable_disconnect_fn = NULL,
2699         .durable_reconnect_fn = NULL,
2700 };
2701
2702 static_decl_vfs;
2703 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
2704 {
2705         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2706                                 "glusterfs", &glusterfs_fns);
2707 }