vfs: Remove SMB_VFS_BRL_CANCEL_WINDOWS
[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
49 #define DEFAULT_VOLFILE_SERVER "localhost"
50 #define GLUSTER_NAME_MAX 255
51
52 static int read_fd = -1;
53 static int write_fd = -1;
54 static struct tevent_fd *aio_read_event = NULL;
55
56 /**
57  * Helper to convert struct stat to struct stat_ex.
58  */
59 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
60 {
61         ZERO_STRUCTP(dst);
62
63         dst->st_ex_dev = src->st_dev;
64         dst->st_ex_ino = src->st_ino;
65         dst->st_ex_mode = src->st_mode;
66         dst->st_ex_nlink = src->st_nlink;
67         dst->st_ex_uid = src->st_uid;
68         dst->st_ex_gid = src->st_gid;
69         dst->st_ex_rdev = src->st_rdev;
70         dst->st_ex_size = src->st_size;
71         dst->st_ex_atime.tv_sec = src->st_atime;
72         dst->st_ex_mtime.tv_sec = src->st_mtime;
73         dst->st_ex_ctime.tv_sec = src->st_ctime;
74         dst->st_ex_btime.tv_sec = src->st_mtime;
75         dst->st_ex_blksize = src->st_blksize;
76         dst->st_ex_blocks = src->st_blocks;
77 #ifdef STAT_HAVE_NSEC
78         dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
79         dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
80         dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
81         dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
82 #endif
83 }
84
85 /* pre-opened glfs_t */
86
87 static struct glfs_preopened {
88         char *volume;
89         char *connectpath;
90         glfs_t *fs;
91         int ref;
92         struct glfs_preopened *next, *prev;
93 } *glfs_preopened;
94
95
96 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
97 {
98         struct glfs_preopened *entry = NULL;
99
100         entry = talloc_zero(NULL, struct glfs_preopened);
101         if (!entry) {
102                 errno = ENOMEM;
103                 return -1;
104         }
105
106         entry->volume = talloc_strdup(entry, volume);
107         if (!entry->volume) {
108                 talloc_free(entry);
109                 errno = ENOMEM;
110                 return -1;
111         }
112
113         entry->connectpath = talloc_strdup(entry, connectpath);
114         if (entry->connectpath == NULL) {
115                 talloc_free(entry);
116                 errno = ENOMEM;
117                 return -1;
118         }
119
120         entry->fs = fs;
121         entry->ref = 1;
122
123         DLIST_ADD(glfs_preopened, entry);
124
125         return 0;
126 }
127
128 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
129 {
130         struct glfs_preopened *entry = NULL;
131
132         for (entry = glfs_preopened; entry; entry = entry->next) {
133                 if (strcmp(entry->volume, volume) == 0 &&
134                     strcmp(entry->connectpath, connectpath) == 0)
135                 {
136                         entry->ref++;
137                         return entry->fs;
138                 }
139         }
140
141         return NULL;
142 }
143
144 static void glfs_clear_preopened(glfs_t *fs)
145 {
146         struct glfs_preopened *entry = NULL;
147
148         for (entry = glfs_preopened; entry; entry = entry->next) {
149                 if (entry->fs == fs) {
150                         if (--entry->ref)
151                                 return;
152
153                         DLIST_REMOVE(glfs_preopened, entry);
154
155                         glfs_fini(entry->fs);
156                         talloc_free(entry);
157                 }
158         }
159 }
160
161 static int vfs_gluster_set_volfile_servers(glfs_t *fs,
162                                            const char *volfile_servers)
163 {
164         char *server = NULL;
165         size_t server_count = 0;
166         size_t server_success = 0;
167         int   ret = -1;
168         TALLOC_CTX *frame = talloc_stackframe();
169
170         DBG_INFO("servers list %s\n", volfile_servers);
171
172         while (next_token_talloc(frame, &volfile_servers, &server, " \t")) {
173                 char *transport = NULL;
174                 char *host = NULL;
175                 int   port = 0;
176
177                 server_count++;
178                 DBG_INFO("server %zu %s\n", server_count, server);
179
180                 /* Determine the transport type */
181                 if (strncmp(server, "unix+", 5) == 0) {
182                         port = 0;
183                         transport = talloc_strdup(frame, "unix");
184                         if (!transport) {
185                                 errno = ENOMEM;
186                                 goto out;
187                         }
188                         host = talloc_strdup(frame, server + 5);
189                         if (!host) {
190                                 errno = ENOMEM;
191                                 goto out;
192                         }
193                 } else {
194                         char *p = NULL;
195                         char *port_index = NULL;
196
197                         if (strncmp(server, "tcp+", 4) == 0) {
198                                 server += 4;
199                         }
200
201                         /* IPv6 is enclosed in []
202                          * ':' before ']' is part of IPv6
203                          * ':' after  ']' indicates port
204                          */
205                         p = server;
206                         if (server[0] == '[') {
207                                 server++;
208                                 p = index(server, ']');
209                                 if (p == NULL) {
210                                         /* Malformed IPv6 */
211                                         continue;
212                                 }
213                                 p[0] = '\0';
214                                 p++;
215                         }
216
217                         port_index = index(p, ':');
218
219                         if (port_index == NULL) {
220                                 port = 0;
221                         } else {
222                                 port = atoi(port_index + 1);
223                                 port_index[0] = '\0';
224                         }
225                         transport = talloc_strdup(frame, "tcp");
226                         if (!transport) {
227                                 errno = ENOMEM;
228                                 goto out;
229                         }
230                         host = talloc_strdup(frame, server);
231                         if (!host) {
232                                 errno = ENOMEM;
233                                 goto out;
234                         }
235                 }
236
237                 DBG_INFO("Calling set volfile server with params "
238                          "transport=%s, host=%s, port=%d\n", transport,
239                           host, port);
240
241                 ret = glfs_set_volfile_server(fs, transport, host, port);
242                 if (ret < 0) {
243                         DBG_WARNING("Failed to set volfile_server "
244                                     "transport=%s, host=%s, port=%d (%s)\n",
245                                     transport, host, port, strerror(errno));
246                 } else {
247                         server_success++;
248                 }
249         }
250
251 out:
252         if (server_count == 0) {
253                 ret = -1;
254         } else if (server_success < server_count) {
255                 DBG_WARNING("Failed to set %zu out of %zu servers parsed\n",
256                             server_count - server_success, server_count);
257                 ret = 0;
258         }
259
260         TALLOC_FREE(frame);
261         return ret;
262 }
263
264 /* Disk Operations */
265
266 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
267                                const char *service,
268                                const char *user)
269 {
270         const char *volfile_servers;
271         const char *volume;
272         char *logfile;
273         int loglevel;
274         glfs_t *fs = NULL;
275         TALLOC_CTX *tmp_ctx;
276         int ret = 0;
277
278         tmp_ctx = talloc_new(NULL);
279         if (tmp_ctx == NULL) {
280                 ret = -1;
281                 goto done;
282         }
283         logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
284                                        "logfile", NULL);
285
286         loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
287
288         volfile_servers = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn),
289                                                "glusterfs", "volfile_server",
290                                                NULL);
291         if (volfile_servers == NULL) {
292                 volfile_servers = DEFAULT_VOLFILE_SERVER;
293         }
294
295         volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
296                                       NULL);
297         if (volume == NULL) {
298                 volume = service;
299         }
300
301         fs = glfs_find_preopened(volume, handle->conn->connectpath);
302         if (fs) {
303                 goto done;
304         }
305
306         fs = glfs_new(volume);
307         if (fs == NULL) {
308                 ret = -1;
309                 goto done;
310         }
311
312         ret = vfs_gluster_set_volfile_servers(fs, volfile_servers);
313         if (ret < 0) {
314                 DBG_ERR("Failed to set volfile_servers from list %s\n",
315                         volfile_servers);
316                 goto done;
317         }
318
319         ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
320                                      "true");
321         if (ret < 0) {
322                 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
323                 goto done;
324         }
325
326
327         ret = glfs_set_xlator_option(fs, "*-snapview-client",
328                                      "snapdir-entry-path",
329                                      handle->conn->connectpath);
330         if (ret < 0) {
331                 DEBUG(0, ("%s: Failed to set xlator option:"
332                           " snapdir-entry-path\n", volume));
333                 goto done;
334         }
335
336         ret = glfs_set_logging(fs, logfile, loglevel);
337         if (ret < 0) {
338                 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
339                           volume, logfile, loglevel));
340                 goto done;
341         }
342
343         ret = glfs_init(fs);
344         if (ret < 0) {
345                 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
346                           volume, strerror(errno)));
347                 goto done;
348         }
349
350         ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
351         if (ret < 0) {
352                 DEBUG(0, ("%s: Failed to register volume (%s)\n",
353                           volume, strerror(errno)));
354                 goto done;
355         }
356
357         /*
358          * The shadow_copy2 module will fail to export subdirectories
359          * of a gluster volume unless we specify the mount point,
360          * because the detection fails if the file system is not
361          * locally mounted:
362          * https://bugzilla.samba.org/show_bug.cgi?id=13091
363          */
364         lp_do_parameter(SNUM(handle->conn), "shadow:mountpoint", "/");
365
366         /*
367          * Unless we have an async implementation of getxattrat turn this off.
368          */
369         lp_do_parameter(SNUM(handle->conn), "smbd:async dosmode", "false");
370
371 done:
372         if (ret < 0) {
373                 if (fs)
374                         glfs_fini(fs);
375         } else {
376                 DBG_ERR("%s: Initialized volume from servers %s\n",
377                         volume, volfile_servers);
378                 handle->data = fs;
379         }
380         talloc_free(tmp_ctx);
381         return ret;
382 }
383
384 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
385 {
386         glfs_t *fs = NULL;
387
388         fs = handle->data;
389
390         glfs_clear_preopened(fs);
391 }
392
393 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
394                                 const struct smb_filename *smb_fname,
395                                 uint64_t *bsize_p,
396                                 uint64_t *dfree_p,
397                                 uint64_t *dsize_p)
398 {
399         struct statvfs statvfs = { 0, };
400         int ret;
401
402         ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
403         if (ret < 0) {
404                 return -1;
405         }
406
407         if (bsize_p != NULL) {
408                 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
409         }
410         if (dfree_p != NULL) {
411                 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
412         }
413         if (dsize_p != NULL) {
414                 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
415         }
416
417         return (uint64_t)statvfs.f_bavail;
418 }
419
420 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
421                                 const struct smb_filename *smb_fname,
422                                 enum SMB_QUOTA_TYPE qtype,
423                                 unid_t id,
424                                 SMB_DISK_QUOTA *qt)
425 {
426         errno = ENOSYS;
427         return -1;
428 }
429
430 static int
431 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
432                       enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
433 {
434         errno = ENOSYS;
435         return -1;
436 }
437
438 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
439                                 const struct smb_filename *smb_fname,
440                                 struct vfs_statvfs_struct *vfs_statvfs)
441 {
442         struct statvfs statvfs = { 0, };
443         int ret;
444
445         ret = glfs_statvfs(handle->data, smb_fname->base_name, &statvfs);
446         if (ret < 0) {
447                 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
448                           smb_fname->base_name, strerror(errno)));
449                 return -1;
450         }
451
452         ZERO_STRUCTP(vfs_statvfs);
453
454         vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
455         vfs_statvfs->BlockSize = statvfs.f_bsize;
456         vfs_statvfs->TotalBlocks = statvfs.f_blocks;
457         vfs_statvfs->BlocksAvail = statvfs.f_bfree;
458         vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
459         vfs_statvfs->TotalFileNodes = statvfs.f_files;
460         vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
461         vfs_statvfs->FsIdentifier = statvfs.f_fsid;
462         vfs_statvfs->FsCapabilities =
463             FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
464
465         return ret;
466 }
467
468 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
469                                             enum timestamp_set_resolution *p_ts_res)
470 {
471         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
472
473 #ifdef HAVE_GFAPI_VER_6
474         caps |= FILE_SUPPORTS_SPARSE_FILES;
475 #endif
476
477 #ifdef STAT_HAVE_NSEC
478         *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
479 #endif
480
481         return caps;
482 }
483
484 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
485                                 const struct smb_filename *smb_fname,
486                                 const char *mask,
487                                 uint32_t attributes)
488 {
489         glfs_fd_t *fd;
490
491         fd = glfs_opendir(handle->data, smb_fname->base_name);
492         if (fd == NULL) {
493                 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
494                           smb_fname->base_name, strerror(errno)));
495         }
496
497         return (DIR *) fd;
498 }
499
500 static glfs_fd_t *vfs_gluster_fetch_glfd(struct vfs_handle_struct *handle,
501                                          files_struct *fsp)
502 {
503         glfs_fd_t **glfd = (glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
504         if (glfd == NULL) {
505                 DBG_INFO("Failed to fetch fsp extension\n");
506                 return NULL;
507         }
508         if (*glfd == NULL) {
509                 DBG_INFO("Empty glfs_fd_t pointer\n");
510                 return NULL;
511         }
512
513         return *glfd;
514 }
515
516 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
517                                   files_struct *fsp, const char *mask,
518                                   uint32_t attributes)
519 {
520         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
521         if (glfd == NULL) {
522                 DBG_ERR("Failed to fetch gluster fd\n");
523                 return NULL;
524         }
525
526         return (DIR *)glfd;
527 }
528
529 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
530 {
531         return glfs_closedir((void *)dirp);
532 }
533
534 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
535                                           DIR *dirp, SMB_STRUCT_STAT *sbuf)
536 {
537         static char direntbuf[512];
538         int ret;
539         struct stat stat;
540         struct dirent *dirent = 0;
541
542         if (sbuf != NULL) {
543                 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
544                                          &dirent);
545         } else {
546                 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
547         }
548
549         if ((ret < 0) || (dirent == NULL)) {
550                 return NULL;
551         }
552
553         if (sbuf != NULL) {
554                 smb_stat_ex_from_stat(sbuf, &stat);
555         }
556
557         return dirent;
558 }
559
560 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
561 {
562         return glfs_telldir((void *)dirp);
563 }
564
565 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
566                                 long offset)
567 {
568         glfs_seekdir((void *)dirp, offset);
569 }
570
571 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
572 {
573         glfs_seekdir((void *)dirp, 0);
574 }
575
576 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle,
577                              const struct smb_filename *smb_fname,
578                              mode_t mode)
579 {
580         return glfs_mkdir(handle->data, smb_fname->base_name, mode);
581 }
582
583 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle,
584                         const struct smb_filename *smb_fname)
585 {
586         return glfs_rmdir(handle->data, smb_fname->base_name);
587 }
588
589 static int vfs_gluster_open(struct vfs_handle_struct *handle,
590                             struct smb_filename *smb_fname, files_struct *fsp,
591                             int flags, mode_t mode)
592 {
593         glfs_fd_t *glfd;
594         glfs_fd_t **p_tmp;
595
596         p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
597         if (p_tmp == NULL) {
598                 errno = ENOMEM;
599                 return -1;
600         }
601
602         if (flags & O_DIRECTORY) {
603                 glfd = glfs_opendir(handle->data, smb_fname->base_name);
604         } else if (flags & O_CREAT) {
605                 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
606                                   mode);
607         } else {
608                 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
609         }
610
611         if (glfd == NULL) {
612                 /* no extension destroy_fn, so no need to save errno */
613                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
614                 return -1;
615         }
616
617         *p_tmp = glfd;
618         /* An arbitrary value for error reporting, so you know its us. */
619         return 13371337;
620 }
621
622 static int vfs_gluster_close(struct vfs_handle_struct *handle,
623                              files_struct *fsp)
624 {
625         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
626         if (glfd == NULL) {
627                 DBG_ERR("Failed to fetch gluster fd\n");
628                 return -1;
629         }
630
631         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
632         return glfs_close(glfd);
633 }
634
635 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
636                                  files_struct *fsp, void *data, size_t n,
637                                  off_t offset)
638 {
639         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
640         if (glfd == NULL) {
641                 DBG_ERR("Failed to fetch gluster fd\n");
642                 return -1;
643         }
644
645 #ifdef HAVE_GFAPI_VER_7_6
646         return glfs_pread(glfd, data, n, offset, 0, NULL);
647 #else
648         return glfs_pread(glfd, data, n, offset, 0);
649 #endif
650 }
651
652 struct glusterfs_aio_state;
653
654 struct glusterfs_aio_wrapper {
655         struct glusterfs_aio_state *state;
656 };
657
658 struct glusterfs_aio_state {
659         ssize_t ret;
660         struct tevent_req *req;
661         bool cancelled;
662         struct vfs_aio_state vfs_aio_state;
663         struct timespec start;
664 };
665
666 static int aio_wrapper_destructor(struct glusterfs_aio_wrapper *wrap)
667 {
668         if (wrap->state != NULL) {
669                 wrap->state->cancelled = true;
670         }
671
672         return 0;
673 }
674
675 /*
676  * This function is the callback that will be called on glusterfs
677  * threads once the async IO submitted is complete. To notify
678  * Samba of the completion we use a pipe based queue.
679  */
680 #ifdef HAVE_GFAPI_VER_7_6
681 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret,
682                                struct glfs_stat *prestat,
683                                struct glfs_stat *poststat,
684                                void *data)
685 #else
686 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
687 #endif
688 {
689         struct glusterfs_aio_state *state = NULL;
690         int sts = 0;
691         struct timespec end;
692
693         state = (struct glusterfs_aio_state *)data;
694
695         PROFILE_TIMESTAMP(&end);
696
697         if (ret < 0) {
698                 state->ret = -1;
699                 state->vfs_aio_state.error = errno;
700         } else {
701                 state->ret = ret;
702         }
703         state->vfs_aio_state.duration = nsec_time_diff(&end, &state->start);
704
705         /*
706          * Write the state pointer to glusterfs_aio_state to the
707          * pipe, so we can call tevent_req_done() from the main thread,
708          * because tevent_req_done() is not designed to be executed in
709          * the multithread environment, so tevent_req_done() must be
710          * executed from the smbd main thread.
711          *
712          * write(2) on pipes with sizes under _POSIX_PIPE_BUF
713          * in size is atomic, without this, the use op pipes in this
714          * code would not work.
715          *
716          * sys_write is a thin enough wrapper around write(2)
717          * that we can trust it here.
718          */
719
720         sts = sys_write(write_fd, &state, sizeof(struct glusterfs_aio_state *));
721         if (sts < 0) {
722                 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
723         }
724
725         return;
726 }
727
728 /*
729  * Read each req off the pipe and process it.
730  */
731 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
732                                 struct tevent_fd *fde,
733                                 uint16_t flags, void *data)
734 {
735         struct tevent_req *req = NULL;
736         struct glusterfs_aio_state *state = NULL;
737         int sts = 0;
738
739         /*
740          * read(2) on pipes is atomic if the needed data is available
741          * in the pipe, per SUS and POSIX.  Because we always write
742          * to the pipe in sizeof(struct tevent_req *) chunks, we can
743          * always read in those chunks, atomically.
744          *
745          * sys_read is a thin enough wrapper around read(2) that we
746          * can trust it here.
747          */
748
749         sts = sys_read(read_fd, &state, sizeof(struct glusterfs_aio_state *));
750
751         if (sts < 0) {
752                 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
753         }
754
755         /* if we've cancelled the op, there is no req, so just clean up. */
756         if (state->cancelled == true) {
757                 TALLOC_FREE(state);
758                 return;
759         }
760
761         req = state->req;
762
763         if (req) {
764                 tevent_req_done(req);
765         }
766         return;
767 }
768
769 static bool init_gluster_aio(struct vfs_handle_struct *handle)
770 {
771         int fds[2];
772         int ret = -1;
773
774         if (read_fd != -1) {
775                 /*
776                  * Already initialized.
777                  */
778                 return true;
779         }
780
781         ret = pipe(fds);
782         if (ret == -1) {
783                 goto fail;
784         }
785
786         read_fd = fds[0];
787         write_fd = fds[1];
788
789         aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
790                                         NULL,
791                                         read_fd,
792                                         TEVENT_FD_READ,
793                                         aio_tevent_fd_done,
794                                         NULL);
795         if (aio_read_event == NULL) {
796                 goto fail;
797         }
798
799         return true;
800 fail:
801         TALLOC_FREE(aio_read_event);
802         if (read_fd != -1) {
803                 close(read_fd);
804                 close(write_fd);
805                 read_fd = -1;
806                 write_fd = -1;
807         }
808         return false;
809 }
810
811 static struct glusterfs_aio_state *aio_state_create(TALLOC_CTX *mem_ctx)
812 {
813         struct tevent_req *req = NULL;
814         struct glusterfs_aio_state *state = NULL;
815         struct glusterfs_aio_wrapper *wrapper = NULL;
816
817         req = tevent_req_create(mem_ctx, &wrapper, struct glusterfs_aio_wrapper);
818
819         if (req == NULL) {
820                 return NULL;
821         }
822
823         state = talloc_zero(NULL, struct glusterfs_aio_state);
824
825         if (state == NULL) {
826                 TALLOC_FREE(req);
827                 return NULL;
828         }
829
830         talloc_set_destructor(wrapper, aio_wrapper_destructor);
831         state->cancelled = false;
832         state->req = req;
833
834         wrapper->state = state;
835
836         return state;
837 }
838
839 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
840                                                   *handle, TALLOC_CTX *mem_ctx,
841                                                   struct tevent_context *ev,
842                                                   files_struct *fsp,
843                                                   void *data, size_t n,
844                                                   off_t offset)
845 {
846         struct glusterfs_aio_state *state = NULL;
847         struct tevent_req *req = NULL;
848         int ret = 0;
849         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
850
851         if (glfd == NULL) {
852                 DBG_ERR("Failed to fetch gluster fd\n");
853                 return NULL;
854         }
855
856         state = aio_state_create(mem_ctx);
857
858         if (state == NULL) {
859                 return NULL;
860         }
861
862         req = state->req;
863
864         if (!init_gluster_aio(handle)) {
865                 tevent_req_error(req, EIO);
866                 return tevent_req_post(req, ev);
867         }
868
869         /*
870          * aio_glusterfs_done and aio_tevent_fd_done()
871          * use the raw tevent context. We need to use
872          * tevent_req_defer_callback() in order to
873          * use the event context we're started with.
874          */
875         tevent_req_defer_callback(req, ev);
876
877         PROFILE_TIMESTAMP(&state->start);
878         ret = glfs_pread_async(glfd, data, n, offset, 0, aio_glusterfs_done,
879                                 state);
880         if (ret < 0) {
881                 tevent_req_error(req, -ret);
882                 return tevent_req_post(req, ev);
883         }
884
885         return req;
886 }
887
888 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
889                                                   *handle, TALLOC_CTX *mem_ctx,
890                                                   struct tevent_context *ev,
891                                                   files_struct *fsp,
892                                                   const void *data, size_t n,
893                                                   off_t offset)
894 {
895         struct glusterfs_aio_state *state = NULL;
896         struct tevent_req *req = NULL;
897         int ret = 0;
898         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
899
900         if (glfd == NULL) {
901                 DBG_ERR("Failed to fetch gluster fd\n");
902                 return NULL;
903         }
904
905         state = aio_state_create(mem_ctx);
906
907         if (state == NULL) {
908                 return NULL;
909         }
910
911         req = state->req;
912
913         if (!init_gluster_aio(handle)) {
914                 tevent_req_error(req, EIO);
915                 return tevent_req_post(req, ev);
916         }
917
918         /*
919          * aio_glusterfs_done and aio_tevent_fd_done()
920          * use the raw tevent context. We need to use
921          * tevent_req_defer_callback() in order to
922          * use the event context we're started with.
923          */
924         tevent_req_defer_callback(req, ev);
925
926         PROFILE_TIMESTAMP(&state->start);
927         ret = glfs_pwrite_async(glfd, data, n, offset, 0, aio_glusterfs_done,
928                                 state);
929         if (ret < 0) {
930                 tevent_req_error(req, -ret);
931                 return tevent_req_post(req, ev);
932         }
933
934         return req;
935 }
936
937 static ssize_t vfs_gluster_recv(struct tevent_req *req,
938                                 struct vfs_aio_state *vfs_aio_state)
939 {
940         struct glusterfs_aio_wrapper *wrapper = NULL;
941         int ret = 0;
942
943         wrapper = tevent_req_data(req, struct glusterfs_aio_wrapper);
944
945         if (wrapper == NULL) {
946                 return -1;
947         }
948
949         if (wrapper->state == NULL) {
950                 return -1;
951         }
952
953         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
954                 return -1;
955         }
956
957         *vfs_aio_state = wrapper->state->vfs_aio_state;
958         ret = wrapper->state->ret;
959
960         /* Clean up the state, it is in a NULL context. */
961
962         TALLOC_FREE(wrapper->state);
963
964         return ret;
965 }
966
967 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
968                                   files_struct *fsp, const void *data,
969                                   size_t n, off_t offset)
970 {
971         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
972         if (glfd == NULL) {
973                 DBG_ERR("Failed to fetch gluster fd\n");
974                 return -1;
975         }
976
977 #ifdef HAVE_GFAPI_VER_7_6
978         return glfs_pwrite(glfd, data, n, offset, 0, NULL, NULL);
979 #else
980         return glfs_pwrite(glfd, data, n, offset, 0);
981 #endif
982 }
983
984 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
985                                files_struct *fsp, off_t offset, int whence)
986 {
987         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
988         if (glfd == NULL) {
989                 DBG_ERR("Failed to fetch gluster fd\n");
990                 return -1;
991         }
992
993         return glfs_lseek(glfd, offset, whence);
994 }
995
996 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
997                                     files_struct *fromfsp,
998                                     const DATA_BLOB *hdr,
999                                     off_t offset, size_t n)
1000 {
1001         errno = ENOTSUP;
1002         return -1;
1003 }
1004
1005 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
1006                                     int fromfd, files_struct *tofsp,
1007                                     off_t offset, size_t n)
1008 {
1009         errno = ENOTSUP;
1010         return -1;
1011 }
1012
1013 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
1014                               const struct smb_filename *smb_fname_src,
1015                               const struct smb_filename *smb_fname_dst)
1016 {
1017         return glfs_rename(handle->data, smb_fname_src->base_name,
1018                            smb_fname_dst->base_name);
1019 }
1020
1021 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
1022                                                  *handle, TALLOC_CTX *mem_ctx,
1023                                                  struct tevent_context *ev,
1024                                                  files_struct *fsp)
1025 {
1026         struct tevent_req *req = NULL;
1027         struct glusterfs_aio_state *state = NULL;
1028         int ret = 0;
1029         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1030
1031         if (glfd == NULL) {
1032                 DBG_ERR("Failed to fetch gluster fd\n");
1033                 return NULL;
1034         }
1035
1036         state = aio_state_create(mem_ctx);
1037
1038         if (state == NULL) {
1039                 return NULL;
1040         }
1041
1042         req = state->req;
1043
1044         if (!init_gluster_aio(handle)) {
1045                 tevent_req_error(req, EIO);
1046                 return tevent_req_post(req, ev);
1047         }
1048
1049         /*
1050          * aio_glusterfs_done and aio_tevent_fd_done()
1051          * use the raw tevent context. We need to use
1052          * tevent_req_defer_callback() in order to
1053          * use the event context we're started with.
1054          */
1055         tevent_req_defer_callback(req, ev);
1056
1057         PROFILE_TIMESTAMP(&state->start);
1058         ret = glfs_fsync_async(glfd, aio_glusterfs_done, state);
1059         if (ret < 0) {
1060                 tevent_req_error(req, -ret);
1061                 return tevent_req_post(req, ev);
1062         }
1063         return req;
1064 }
1065
1066 static int vfs_gluster_fsync_recv(struct tevent_req *req,
1067                                   struct vfs_aio_state *vfs_aio_state)
1068 {
1069         /*
1070          * Use implicit conversion ssize_t->int
1071          */
1072         return vfs_gluster_recv(req, vfs_aio_state);
1073 }
1074
1075 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1076                             struct smb_filename *smb_fname)
1077 {
1078         struct stat st;
1079         int ret;
1080
1081         ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1082         if (ret == 0) {
1083                 smb_stat_ex_from_stat(&smb_fname->st, &st);
1084         }
1085         if (ret < 0 && errno != ENOENT) {
1086                 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1087                           smb_fname->base_name, strerror(errno)));
1088         }
1089         return ret;
1090 }
1091
1092 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1093                              files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1094 {
1095         struct stat st;
1096         int ret;
1097         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1098
1099         if (glfd == NULL) {
1100                 DBG_ERR("Failed to fetch gluster fd\n");
1101                 return -1;
1102         }
1103
1104         ret = glfs_fstat(glfd, &st);
1105         if (ret == 0) {
1106                 smb_stat_ex_from_stat(sbuf, &st);
1107         }
1108         if (ret < 0) {
1109                 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1110                           fsp->fh->fd, strerror(errno)));
1111         }
1112         return ret;
1113 }
1114
1115 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1116                              struct smb_filename *smb_fname)
1117 {
1118         struct stat st;
1119         int ret;
1120
1121         ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1122         if (ret == 0) {
1123                 smb_stat_ex_from_stat(&smb_fname->st, &st);
1124         }
1125         if (ret < 0 && errno != ENOENT) {
1126                 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1127                           smb_fname->base_name, strerror(errno)));
1128         }
1129         return ret;
1130 }
1131
1132 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1133                                            files_struct *fsp,
1134                                            const SMB_STRUCT_STAT *sbuf)
1135 {
1136         return sbuf->st_ex_blocks * 512;
1137 }
1138
1139 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
1140                               const struct smb_filename *smb_fname)
1141 {
1142         return glfs_unlink(handle->data, smb_fname->base_name);
1143 }
1144
1145 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
1146                                 const struct smb_filename *smb_fname,
1147                                 mode_t mode)
1148 {
1149         return glfs_chmod(handle->data, smb_fname->base_name, mode);
1150 }
1151
1152 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1153                               files_struct *fsp, mode_t mode)
1154 {
1155         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1156
1157         if (glfd == NULL) {
1158                 DBG_ERR("Failed to fetch gluster fd\n");
1159                 return -1;
1160         }
1161
1162         return glfs_fchmod(glfd, mode);
1163 }
1164
1165 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
1166                         const struct smb_filename *smb_fname,
1167                         uid_t uid,
1168                         gid_t gid)
1169 {
1170         return glfs_chown(handle->data, smb_fname->base_name, uid, gid);
1171 }
1172
1173 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1174                               files_struct *fsp, uid_t uid, gid_t gid)
1175 {
1176         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1177         if (glfd == NULL) {
1178                 DBG_ERR("Failed to fetch gluster fd\n");
1179                 return -1;
1180         }
1181
1182         return glfs_fchown(glfd, uid, gid);
1183 }
1184
1185 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1186                         const struct smb_filename *smb_fname,
1187                         uid_t uid,
1188                         gid_t gid)
1189 {
1190         return glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1191 }
1192
1193 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1194                         const struct smb_filename *smb_fname)
1195 {
1196         return glfs_chdir(handle->data, smb_fname->base_name);
1197 }
1198
1199 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1200                                 TALLOC_CTX *ctx)
1201 {
1202         char *cwd;
1203         char *ret;
1204         struct smb_filename *smb_fname = NULL;
1205
1206         cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
1207         if (cwd == NULL) {
1208                 return NULL;
1209         }
1210
1211         ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1212         if (ret == NULL) {
1213                 SAFE_FREE(cwd);
1214                 return NULL;
1215         }
1216         smb_fname = synthetic_smb_fname(ctx,
1217                                         ret,
1218                                         NULL,
1219                                         NULL,
1220                                         0);
1221         SAFE_FREE(cwd);
1222         return smb_fname;
1223 }
1224
1225 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
1226                               const struct smb_filename *smb_fname,
1227                               struct smb_file_time *ft)
1228 {
1229         struct timespec times[2];
1230
1231         if (null_timespec(ft->atime)) {
1232                 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
1233                 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
1234         } else {
1235                 times[0].tv_sec = ft->atime.tv_sec;
1236                 times[0].tv_nsec = ft->atime.tv_nsec;
1237         }
1238
1239         if (null_timespec(ft->mtime)) {
1240                 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
1241                 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
1242         } else {
1243                 times[1].tv_sec = ft->mtime.tv_sec;
1244                 times[1].tv_nsec = ft->mtime.tv_nsec;
1245         }
1246
1247         if ((timespec_compare(&times[0],
1248                               &smb_fname->st.st_ex_atime) == 0) &&
1249             (timespec_compare(&times[1],
1250                               &smb_fname->st.st_ex_mtime) == 0)) {
1251                 return 0;
1252         }
1253
1254         return glfs_utimens(handle->data, smb_fname->base_name, times);
1255 }
1256
1257 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1258                                  files_struct *fsp, off_t offset)
1259 {
1260         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1261         if (glfd == NULL) {
1262                 DBG_ERR("Failed to fetch gluster fd\n");
1263                 return -1;
1264         }
1265
1266 #ifdef HAVE_GFAPI_VER_7_6
1267         return glfs_ftruncate(glfd, offset, NULL, NULL);
1268 #else
1269         return glfs_ftruncate(glfd, offset);
1270 #endif
1271 }
1272
1273 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1274                                  struct files_struct *fsp,
1275                                  uint32_t mode,
1276                                  off_t offset, off_t len)
1277 {
1278 #ifdef HAVE_GFAPI_VER_6
1279         int keep_size, punch_hole;
1280         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1281         if (glfd == NULL) {
1282                 DBG_ERR("Failed to fetch gluster fd\n");
1283                 return -1;
1284         }
1285
1286         keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1287         punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1288
1289         mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1290         if (mode != 0) {
1291                 errno = ENOTSUP;
1292                 return -1;
1293         }
1294
1295         if (punch_hole) {
1296                 return glfs_discard(glfd, offset, len);
1297         }
1298
1299         return glfs_fallocate(glfd, keep_size, offset, len);
1300 #else
1301         errno = ENOTSUP;
1302         return -1;
1303 #endif
1304 }
1305
1306 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1307                                 TALLOC_CTX *ctx,
1308                                 const struct smb_filename *smb_fname)
1309 {
1310         char *result = NULL;
1311         struct smb_filename *result_fname = NULL;
1312         char *resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1313
1314         if (resolved_path == NULL) {
1315                 errno = ENOMEM;
1316                 return NULL;
1317         }
1318
1319         result = glfs_realpath(handle->data,
1320                         smb_fname->base_name,
1321                         resolved_path);
1322         if (result != NULL) {
1323                 result_fname = synthetic_smb_fname(ctx, result, NULL, NULL, 0);
1324         }
1325
1326         SAFE_FREE(resolved_path);
1327         return result_fname;
1328 }
1329
1330 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1331                              files_struct *fsp, int op, off_t offset,
1332                              off_t count, int type)
1333 {
1334         struct flock flock = { 0, };
1335         int ret;
1336         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1337         if (glfd == NULL) {
1338                 DBG_ERR("Failed to fetch gluster fd\n");
1339                 return false;
1340         }
1341
1342         flock.l_type = type;
1343         flock.l_whence = SEEK_SET;
1344         flock.l_start = offset;
1345         flock.l_len = count;
1346         flock.l_pid = 0;
1347
1348         ret = glfs_posix_lock(glfd, op, &flock);
1349
1350         if (op == F_GETLK) {
1351                 /* lock query, true if someone else has locked */
1352                 if ((ret != -1) &&
1353                     (flock.l_type != F_UNLCK) &&
1354                     (flock.l_pid != 0) && (flock.l_pid != getpid()))
1355                         return true;
1356                 /* not me */
1357                 return false;
1358         }
1359
1360         if (ret == -1) {
1361                 return false;
1362         }
1363
1364         return true;
1365 }
1366
1367 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1368                                     files_struct *fsp, uint32_t share_mode,
1369                                     uint32_t access_mask)
1370 {
1371         errno = ENOSYS;
1372         return -1;
1373 }
1374
1375 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1376                                       files_struct *fsp, int leasetype)
1377 {
1378         errno = ENOSYS;
1379         return -1;
1380 }
1381
1382 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1383                                 files_struct *fsp, off_t *poffset,
1384                                 off_t *pcount, int *ptype, pid_t *ppid)
1385 {
1386         struct flock flock = { 0, };
1387         int ret;
1388         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1389         if (glfd == NULL) {
1390                 DBG_ERR("Failed to fetch gluster fd\n");
1391                 return false;
1392         }
1393
1394         flock.l_type = *ptype;
1395         flock.l_whence = SEEK_SET;
1396         flock.l_start = *poffset;
1397         flock.l_len = *pcount;
1398         flock.l_pid = 0;
1399
1400         ret = glfs_posix_lock(glfd, F_GETLK, &flock);
1401
1402         if (ret == -1) {
1403                 return false;
1404         }
1405
1406         *ptype = flock.l_type;
1407         *poffset = flock.l_start;
1408         *pcount = flock.l_len;
1409         *ppid = flock.l_pid;
1410
1411         return true;
1412 }
1413
1414 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1415                                 const char *link_target,
1416                                 const struct smb_filename *new_smb_fname)
1417 {
1418         return glfs_symlink(handle->data,
1419                         link_target,
1420                         new_smb_fname->base_name);
1421 }
1422
1423 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1424                                 const struct smb_filename *smb_fname,
1425                                 char *buf,
1426                                 size_t bufsiz)
1427 {
1428         return glfs_readlink(handle->data, smb_fname->base_name, buf, bufsiz);
1429 }
1430
1431 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1432                                 const struct smb_filename *old_smb_fname,
1433                                 const struct smb_filename *new_smb_fname)
1434 {
1435         return glfs_link(handle->data,
1436                         old_smb_fname->base_name,
1437                         new_smb_fname->base_name);
1438 }
1439
1440 static int vfs_gluster_mknod(struct vfs_handle_struct *handle,
1441                                 const struct smb_filename *smb_fname,
1442                                 mode_t mode,
1443                                 SMB_DEV_T dev)
1444 {
1445         return glfs_mknod(handle->data, smb_fname->base_name, mode, dev);
1446 }
1447
1448 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1449                                 const struct smb_filename *smb_fname,
1450                                 unsigned int flags)
1451 {
1452         errno = ENOSYS;
1453         return -1;
1454 }
1455
1456 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1457                                          const char *path, const char *name,
1458                                          TALLOC_CTX *mem_ctx, char **found_name)
1459 {
1460         int ret;
1461         char key_buf[GLUSTER_NAME_MAX + 64];
1462         char val_buf[GLUSTER_NAME_MAX + 1];
1463
1464         if (strlen(name) >= GLUSTER_NAME_MAX) {
1465                 errno = ENAMETOOLONG;
1466                 return -1;
1467         }
1468
1469         snprintf(key_buf, GLUSTER_NAME_MAX + 64,
1470                  "glusterfs.get_real_filename:%s", name);
1471
1472         ret = glfs_getxattr(handle->data, path, key_buf, val_buf,
1473                             GLUSTER_NAME_MAX + 1);
1474         if (ret == -1) {
1475                 if (errno == ENOATTR) {
1476                         errno = EOPNOTSUPP;
1477                 }
1478                 return -1;
1479         }
1480
1481         *found_name = talloc_strdup(mem_ctx, val_buf);
1482         if (found_name[0] == NULL) {
1483                 errno = ENOMEM;
1484                 return -1;
1485         }
1486         return 0;
1487 }
1488
1489 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1490                                 const struct smb_filename *smb_fname)
1491 {
1492         return handle->conn->connectpath;
1493 }
1494
1495 /* EA Operations */
1496
1497 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1498                                 const struct smb_filename *smb_fname,
1499                                 const char *name,
1500                                 void *value,
1501                                 size_t size)
1502 {
1503         return glfs_getxattr(handle->data, smb_fname->base_name,
1504                         name, value, size);
1505 }
1506
1507 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1508                                      files_struct *fsp, const char *name,
1509                                      void *value, size_t size)
1510 {
1511         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1512         if (glfd == NULL) {
1513                 DBG_ERR("Failed to fetch gluster fd\n");
1514                 return -1;
1515         }
1516
1517         return glfs_fgetxattr(glfd, name, value, size);
1518 }
1519
1520 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1521                                 const struct smb_filename *smb_fname,
1522                                 char *list,
1523                                 size_t size)
1524 {
1525         return glfs_listxattr(handle->data, smb_fname->base_name, list, size);
1526 }
1527
1528 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1529                                       files_struct *fsp, char *list,
1530                                       size_t size)
1531 {
1532         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1533         if (glfd == NULL) {
1534                 DBG_ERR("Failed to fetch gluster fd\n");
1535                 return -1;
1536         }
1537
1538         return glfs_flistxattr(glfd, list, size);
1539 }
1540
1541 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1542                                 const struct smb_filename *smb_fname,
1543                                 const char *name)
1544 {
1545         return glfs_removexattr(handle->data, smb_fname->base_name, name);
1546 }
1547
1548 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1549                                     files_struct *fsp, const char *name)
1550 {
1551         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1552         if (glfd == NULL) {
1553                 DBG_ERR("Failed to fetch gluster fd\n");
1554                 return -1;
1555         }
1556
1557         return glfs_fremovexattr(glfd, name);
1558 }
1559
1560 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1561                                 const struct smb_filename *smb_fname,
1562                                 const char *name,
1563                                 const void *value, size_t size, int flags)
1564 {
1565         return glfs_setxattr(handle->data, smb_fname->base_name, name, value, size, flags);
1566 }
1567
1568 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1569                                  files_struct *fsp, const char *name,
1570                                  const void *value, size_t size, int flags)
1571 {
1572         glfs_fd_t *glfd = vfs_gluster_fetch_glfd(handle, fsp);
1573         if (glfd == NULL) {
1574                 DBG_ERR("Failed to fetch gluster fd\n");
1575                 return -1;
1576         }
1577
1578         return glfs_fsetxattr(glfd, name, value, size, flags);
1579 }
1580
1581 /* AIO Operations */
1582
1583 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1584                                   files_struct *fsp)
1585 {
1586         return false;
1587 }
1588
1589 static struct vfs_fn_pointers glusterfs_fns = {
1590
1591         /* Disk Operations */
1592
1593         .connect_fn = vfs_gluster_connect,
1594         .disconnect_fn = vfs_gluster_disconnect,
1595         .disk_free_fn = vfs_gluster_disk_free,
1596         .get_quota_fn = vfs_gluster_get_quota,
1597         .set_quota_fn = vfs_gluster_set_quota,
1598         .statvfs_fn = vfs_gluster_statvfs,
1599         .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1600
1601         .get_dfs_referrals_fn = NULL,
1602
1603         /* Directory Operations */
1604
1605         .opendir_fn = vfs_gluster_opendir,
1606         .fdopendir_fn = vfs_gluster_fdopendir,
1607         .readdir_fn = vfs_gluster_readdir,
1608         .seekdir_fn = vfs_gluster_seekdir,
1609         .telldir_fn = vfs_gluster_telldir,
1610         .rewind_dir_fn = vfs_gluster_rewinddir,
1611         .mkdir_fn = vfs_gluster_mkdir,
1612         .rmdir_fn = vfs_gluster_rmdir,
1613         .closedir_fn = vfs_gluster_closedir,
1614
1615         /* File Operations */
1616
1617         .open_fn = vfs_gluster_open,
1618         .create_file_fn = NULL,
1619         .close_fn = vfs_gluster_close,
1620         .pread_fn = vfs_gluster_pread,
1621         .pread_send_fn = vfs_gluster_pread_send,
1622         .pread_recv_fn = vfs_gluster_recv,
1623         .pwrite_fn = vfs_gluster_pwrite,
1624         .pwrite_send_fn = vfs_gluster_pwrite_send,
1625         .pwrite_recv_fn = vfs_gluster_recv,
1626         .lseek_fn = vfs_gluster_lseek,
1627         .sendfile_fn = vfs_gluster_sendfile,
1628         .recvfile_fn = vfs_gluster_recvfile,
1629         .rename_fn = vfs_gluster_rename,
1630         .fsync_send_fn = vfs_gluster_fsync_send,
1631         .fsync_recv_fn = vfs_gluster_fsync_recv,
1632
1633         .stat_fn = vfs_gluster_stat,
1634         .fstat_fn = vfs_gluster_fstat,
1635         .lstat_fn = vfs_gluster_lstat,
1636         .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1637         .unlink_fn = vfs_gluster_unlink,
1638
1639         .chmod_fn = vfs_gluster_chmod,
1640         .fchmod_fn = vfs_gluster_fchmod,
1641         .chown_fn = vfs_gluster_chown,
1642         .fchown_fn = vfs_gluster_fchown,
1643         .lchown_fn = vfs_gluster_lchown,
1644         .chdir_fn = vfs_gluster_chdir,
1645         .getwd_fn = vfs_gluster_getwd,
1646         .ntimes_fn = vfs_gluster_ntimes,
1647         .ftruncate_fn = vfs_gluster_ftruncate,
1648         .fallocate_fn = vfs_gluster_fallocate,
1649         .lock_fn = vfs_gluster_lock,
1650         .kernel_flock_fn = vfs_gluster_kernel_flock,
1651         .linux_setlease_fn = vfs_gluster_linux_setlease,
1652         .getlock_fn = vfs_gluster_getlock,
1653         .symlink_fn = vfs_gluster_symlink,
1654         .readlink_fn = vfs_gluster_readlink,
1655         .link_fn = vfs_gluster_link,
1656         .mknod_fn = vfs_gluster_mknod,
1657         .realpath_fn = vfs_gluster_realpath,
1658         .chflags_fn = vfs_gluster_chflags,
1659         .file_id_create_fn = NULL,
1660         .streaminfo_fn = NULL,
1661         .get_real_filename_fn = vfs_gluster_get_real_filename,
1662         .connectpath_fn = vfs_gluster_connectpath,
1663
1664         .brl_lock_windows_fn = NULL,
1665         .brl_unlock_windows_fn = NULL,
1666         .strict_lock_check_fn = NULL,
1667         .translate_name_fn = NULL,
1668         .fsctl_fn = NULL,
1669
1670         /* NT ACL Operations */
1671         .fget_nt_acl_fn = NULL,
1672         .get_nt_acl_fn = NULL,
1673         .fset_nt_acl_fn = NULL,
1674         .audit_file_fn = NULL,
1675
1676         /* Posix ACL Operations */
1677         .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
1678         .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1679         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1680         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1681         .sys_acl_set_file_fn = posixacl_xattr_acl_set_file,
1682         .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1683         .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
1684
1685         /* EA Operations */
1686         .getxattr_fn = vfs_gluster_getxattr,
1687         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1688         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1689         .fgetxattr_fn = vfs_gluster_fgetxattr,
1690         .listxattr_fn = vfs_gluster_listxattr,
1691         .flistxattr_fn = vfs_gluster_flistxattr,
1692         .removexattr_fn = vfs_gluster_removexattr,
1693         .fremovexattr_fn = vfs_gluster_fremovexattr,
1694         .setxattr_fn = vfs_gluster_setxattr,
1695         .fsetxattr_fn = vfs_gluster_fsetxattr,
1696
1697         /* AIO Operations */
1698         .aio_force_fn = vfs_gluster_aio_force,
1699
1700         /* Durable handle Operations */
1701         .durable_cookie_fn = NULL,
1702         .durable_disconnect_fn = NULL,
1703         .durable_reconnect_fn = NULL,
1704 };
1705
1706 static_decl_vfs;
1707 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
1708 {
1709         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1710                                 "glusterfs", &glusterfs_fns);
1711 }