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