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