smbd: use async dos_mode_at_send in smbd_smb2_query_directory_send()
[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
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         int   server_count = 0;
165         int   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 %d %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 %d out of %d 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 DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
500                                   files_struct *fsp, const char *mask,
501                                   uint32_t attributes)
502 {
503         return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
504 }
505
506 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
507 {
508         return glfs_closedir((void *)dirp);
509 }
510
511 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
512                                           DIR *dirp, SMB_STRUCT_STAT *sbuf)
513 {
514         static char direntbuf[512];
515         int ret;
516         struct stat stat;
517         struct dirent *dirent = 0;
518
519         if (sbuf != NULL) {
520                 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
521                                          &dirent);
522         } else {
523                 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
524         }
525
526         if ((ret < 0) || (dirent == NULL)) {
527                 return NULL;
528         }
529
530         if (sbuf != NULL) {
531                 smb_stat_ex_from_stat(sbuf, &stat);
532         }
533
534         return dirent;
535 }
536
537 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
538 {
539         return glfs_telldir((void *)dirp);
540 }
541
542 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
543                                 long offset)
544 {
545         glfs_seekdir((void *)dirp, offset);
546 }
547
548 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
549 {
550         glfs_seekdir((void *)dirp, 0);
551 }
552
553 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle,
554                              const struct smb_filename *smb_fname,
555                              mode_t mode)
556 {
557         return glfs_mkdir(handle->data, smb_fname->base_name, mode);
558 }
559
560 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle,
561                         const struct smb_filename *smb_fname)
562 {
563         return glfs_rmdir(handle->data, smb_fname->base_name);
564 }
565
566 static int vfs_gluster_open(struct vfs_handle_struct *handle,
567                             struct smb_filename *smb_fname, files_struct *fsp,
568                             int flags, mode_t mode)
569 {
570         glfs_fd_t *glfd;
571         glfs_fd_t **p_tmp;
572
573         if (flags & O_DIRECTORY) {
574                 glfd = glfs_opendir(handle->data, smb_fname->base_name);
575         } else if (flags & O_CREAT) {
576                 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
577                                   mode);
578         } else {
579                 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
580         }
581
582         if (glfd == NULL) {
583                 return -1;
584         }
585         p_tmp = VFS_ADD_FSP_EXTENSION(handle, fsp, glfs_fd_t *, NULL);
586         *p_tmp = glfd;
587         /* An arbitrary value for error reporting, so you know its us. */
588         return 13371337;
589 }
590
591 static int vfs_gluster_close(struct vfs_handle_struct *handle,
592                              files_struct *fsp)
593 {
594         glfs_fd_t *glfd;
595         glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
596         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
597         return glfs_close(glfd);
598 }
599
600 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
601                                  files_struct *fsp, void *data, size_t n,
602                                  off_t offset)
603 {
604         return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
605 }
606
607 struct glusterfs_aio_state;
608
609 struct glusterfs_aio_wrapper {
610         struct glusterfs_aio_state *state;
611 };
612
613 struct glusterfs_aio_state {
614         ssize_t ret;
615         struct tevent_req *req;
616         bool cancelled;
617         struct vfs_aio_state vfs_aio_state;
618         struct timespec start;
619 };
620
621 static int aio_wrapper_destructor(struct glusterfs_aio_wrapper *wrap)
622 {
623         if (wrap->state != NULL) {
624                 wrap->state->cancelled = true;
625         }
626
627         return 0;
628 }
629
630 /*
631  * This function is the callback that will be called on glusterfs
632  * threads once the async IO submitted is complete. To notify
633  * Samba of the completion we use a pipe based queue.
634  */
635 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
636 {
637         struct glusterfs_aio_state *state = NULL;
638         int sts = 0;
639         struct timespec end;
640
641         state = (struct glusterfs_aio_state *)data;
642
643         PROFILE_TIMESTAMP(&end);
644
645         if (ret < 0) {
646                 state->ret = -1;
647                 state->vfs_aio_state.error = errno;
648         } else {
649                 state->ret = ret;
650         }
651         state->vfs_aio_state.duration = nsec_time_diff(&end, &state->start);
652
653         /*
654          * Write the state pointer to glusterfs_aio_state to the
655          * pipe, so we can call tevent_req_done() from the main thread,
656          * because tevent_req_done() is not designed to be executed in
657          * the multithread environment, so tevent_req_done() must be
658          * executed from the smbd main thread.
659          *
660          * write(2) on pipes with sizes under _POSIX_PIPE_BUF
661          * in size is atomic, without this, the use op pipes in this
662          * code would not work.
663          *
664          * sys_write is a thin enough wrapper around write(2)
665          * that we can trust it here.
666          */
667
668         sts = sys_write(write_fd, &state, sizeof(struct glusterfs_aio_state *));
669         if (sts < 0) {
670                 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
671         }
672
673         return;
674 }
675
676 /*
677  * Read each req off the pipe and process it.
678  */
679 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
680                                 struct tevent_fd *fde,
681                                 uint16_t flags, void *data)
682 {
683         struct tevent_req *req = NULL;
684         struct glusterfs_aio_state *state = NULL;
685         int sts = 0;
686
687         /*
688          * read(2) on pipes is atomic if the needed data is available
689          * in the pipe, per SUS and POSIX.  Because we always write
690          * to the pipe in sizeof(struct tevent_req *) chunks, we can
691          * always read in those chunks, atomically.
692          *
693          * sys_read is a thin enough wrapper around read(2) that we
694          * can trust it here.
695          */
696
697         sts = sys_read(read_fd, &state, sizeof(struct glusterfs_aio_state *));
698
699         if (sts < 0) {
700                 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
701         }
702
703         /* if we've cancelled the op, there is no req, so just clean up. */
704         if (state->cancelled == true) {
705                 TALLOC_FREE(state);
706                 return;
707         }
708
709         req = state->req;
710
711         if (req) {
712                 tevent_req_done(req);
713         }
714         return;
715 }
716
717 static bool init_gluster_aio(struct vfs_handle_struct *handle)
718 {
719         int fds[2];
720         int ret = -1;
721
722         if (read_fd != -1) {
723                 /*
724                  * Already initialized.
725                  */
726                 return true;
727         }
728
729         ret = pipe(fds);
730         if (ret == -1) {
731                 goto fail;
732         }
733
734         read_fd = fds[0];
735         write_fd = fds[1];
736
737         /*
738          * We use the raw tevent context here,
739          * as this is a global event handler.
740          *
741          * The tevent_req_defer_callback()
742          * calls will make sure the results
743          * of async calls are propagated
744          * to the correct tevent_context.
745          */
746         aio_read_event = tevent_add_fd(handle->conn->sconn->raw_ev_ctx,
747                                         NULL,
748                                         read_fd,
749                                         TEVENT_FD_READ,
750                                         aio_tevent_fd_done,
751                                         NULL);
752         if (aio_read_event == NULL) {
753                 goto fail;
754         }
755
756         return true;
757 fail:
758         TALLOC_FREE(aio_read_event);
759         if (read_fd != -1) {
760                 close(read_fd);
761                 close(write_fd);
762                 read_fd = -1;
763                 write_fd = -1;
764         }
765         return false;
766 }
767
768 static struct glusterfs_aio_state *aio_state_create(TALLOC_CTX *mem_ctx)
769 {
770         struct tevent_req *req = NULL;
771         struct glusterfs_aio_state *state = NULL;
772         struct glusterfs_aio_wrapper *wrapper = NULL;
773
774         req = tevent_req_create(mem_ctx, &wrapper, struct glusterfs_aio_wrapper);
775
776         if (req == NULL) {
777                 return NULL;
778         }
779
780         state = talloc_zero(NULL, struct glusterfs_aio_state);
781
782         if (state == NULL) {
783                 TALLOC_FREE(req);
784                 return NULL;
785         }
786
787         talloc_set_destructor(wrapper, aio_wrapper_destructor);
788         state->cancelled = false;
789         state->req = req;
790
791         wrapper->state = state;
792
793         return state;
794 }
795
796 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
797                                                   *handle, TALLOC_CTX *mem_ctx,
798                                                   struct tevent_context *ev,
799                                                   files_struct *fsp,
800                                                   void *data, size_t n,
801                                                   off_t offset)
802 {
803         struct glusterfs_aio_state *state = NULL;
804         struct tevent_req *req = NULL;
805         int ret = 0;
806
807         state = aio_state_create(mem_ctx);
808
809         if (state == NULL) {
810                 return NULL;
811         }
812
813         req = state->req;
814
815         if (!init_gluster_aio(handle)) {
816                 tevent_req_error(req, EIO);
817                 return tevent_req_post(req, ev);
818         }
819
820         /*
821          * aio_glusterfs_done and aio_tevent_fd_done()
822          * use the raw tevent context. We need to use
823          * tevent_req_defer_callback() in order to
824          * use the event context we're started with.
825          */
826         tevent_req_defer_callback(req, ev);
827
828         PROFILE_TIMESTAMP(&state->start);
829         ret = glfs_pread_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
830                                 fsp), data, n, offset, 0, aio_glusterfs_done,
831                                 state);
832         if (ret < 0) {
833                 tevent_req_error(req, -ret);
834                 return tevent_req_post(req, ev);
835         }
836
837         return req;
838 }
839
840 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
841                                                   *handle, TALLOC_CTX *mem_ctx,
842                                                   struct tevent_context *ev,
843                                                   files_struct *fsp,
844                                                   const void *data, size_t n,
845                                                   off_t offset)
846 {
847         struct glusterfs_aio_state *state = NULL;
848         struct tevent_req *req = NULL;
849         int ret = 0;
850
851         state = aio_state_create(mem_ctx);
852
853         if (state == NULL) {
854                 return NULL;
855         }
856
857         req = state->req;
858
859         if (!init_gluster_aio(handle)) {
860                 tevent_req_error(req, EIO);
861                 return tevent_req_post(req, ev);
862         }
863
864         /*
865          * aio_glusterfs_done and aio_tevent_fd_done()
866          * use the raw tevent context. We need to use
867          * tevent_req_defer_callback() in order to
868          * use the event context we're started with.
869          */
870         tevent_req_defer_callback(req, ev);
871
872         PROFILE_TIMESTAMP(&state->start);
873         ret = glfs_pwrite_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
874                                 fsp), data, n, offset, 0, aio_glusterfs_done,
875                                 state);
876         if (ret < 0) {
877                 tevent_req_error(req, -ret);
878                 return tevent_req_post(req, ev);
879         }
880
881         return req;
882 }
883
884 static ssize_t vfs_gluster_recv(struct tevent_req *req,
885                                 struct vfs_aio_state *vfs_aio_state)
886 {
887         struct glusterfs_aio_wrapper *wrapper = NULL;
888         int ret = 0;
889
890         wrapper = tevent_req_data(req, struct glusterfs_aio_wrapper);
891
892         if (wrapper == NULL) {
893                 return -1;
894         }
895
896         if (wrapper->state == NULL) {
897                 return -1;
898         }
899
900         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
901                 return -1;
902         }
903
904         *vfs_aio_state = wrapper->state->vfs_aio_state;
905         ret = wrapper->state->ret;
906
907         /* Clean up the state, it is in a NULL context. */
908
909         TALLOC_FREE(wrapper->state);
910
911         return ret;
912 }
913
914 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
915                                   files_struct *fsp, const void *data,
916                                   size_t n, off_t offset)
917 {
918         return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
919 }
920
921 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
922                                files_struct *fsp, off_t offset, int whence)
923 {
924         return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
925 }
926
927 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
928                                     files_struct *fromfsp,
929                                     const DATA_BLOB *hdr,
930                                     off_t offset, size_t n)
931 {
932         errno = ENOTSUP;
933         return -1;
934 }
935
936 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
937                                     int fromfd, files_struct *tofsp,
938                                     off_t offset, size_t n)
939 {
940         errno = ENOTSUP;
941         return -1;
942 }
943
944 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
945                               const struct smb_filename *smb_fname_src,
946                               const struct smb_filename *smb_fname_dst)
947 {
948         return glfs_rename(handle->data, smb_fname_src->base_name,
949                            smb_fname_dst->base_name);
950 }
951
952 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
953                                                  *handle, TALLOC_CTX *mem_ctx,
954                                                  struct tevent_context *ev,
955                                                  files_struct *fsp)
956 {
957         struct tevent_req *req = NULL;
958         struct glusterfs_aio_state *state = NULL;
959         int ret = 0;
960
961         state = aio_state_create(mem_ctx);
962
963         if (state == NULL) {
964                 return NULL;
965         }
966
967         req = state->req;
968
969         if (!init_gluster_aio(handle)) {
970                 tevent_req_error(req, EIO);
971                 return tevent_req_post(req, ev);
972         }
973
974         /*
975          * aio_glusterfs_done and aio_tevent_fd_done()
976          * use the raw tevent context. We need to use
977          * tevent_req_defer_callback() in order to
978          * use the event context we're started with.
979          */
980         tevent_req_defer_callback(req, ev);
981
982         PROFILE_TIMESTAMP(&state->start);
983         ret = glfs_fsync_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
984                                 fsp), aio_glusterfs_done, state);
985         if (ret < 0) {
986                 tevent_req_error(req, -ret);
987                 return tevent_req_post(req, ev);
988         }
989         return req;
990 }
991
992 static int vfs_gluster_fsync_recv(struct tevent_req *req,
993                                   struct vfs_aio_state *vfs_aio_state)
994 {
995         /*
996          * Use implicit conversion ssize_t->int
997          */
998         return vfs_gluster_recv(req, vfs_aio_state);
999 }
1000
1001 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
1002                             struct smb_filename *smb_fname)
1003 {
1004         struct stat st;
1005         int ret;
1006
1007         ret = glfs_stat(handle->data, smb_fname->base_name, &st);
1008         if (ret == 0) {
1009                 smb_stat_ex_from_stat(&smb_fname->st, &st);
1010         }
1011         if (ret < 0 && errno != ENOENT) {
1012                 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
1013                           smb_fname->base_name, strerror(errno)));
1014         }
1015         return ret;
1016 }
1017
1018 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
1019                              files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1020 {
1021         struct stat st;
1022         int ret;
1023
1024         ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
1025         if (ret == 0) {
1026                 smb_stat_ex_from_stat(sbuf, &st);
1027         }
1028         if (ret < 0) {
1029                 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
1030                           fsp->fh->fd, strerror(errno)));
1031         }
1032         return ret;
1033 }
1034
1035 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
1036                              struct smb_filename *smb_fname)
1037 {
1038         struct stat st;
1039         int ret;
1040
1041         ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
1042         if (ret == 0) {
1043                 smb_stat_ex_from_stat(&smb_fname->st, &st);
1044         }
1045         if (ret < 0 && errno != ENOENT) {
1046                 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
1047                           smb_fname->base_name, strerror(errno)));
1048         }
1049         return ret;
1050 }
1051
1052 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
1053                                            files_struct *fsp,
1054                                            const SMB_STRUCT_STAT *sbuf)
1055 {
1056         return sbuf->st_ex_blocks * 512;
1057 }
1058
1059 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
1060                               const struct smb_filename *smb_fname)
1061 {
1062         return glfs_unlink(handle->data, smb_fname->base_name);
1063 }
1064
1065 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
1066                                 const struct smb_filename *smb_fname,
1067                                 mode_t mode)
1068 {
1069         return glfs_chmod(handle->data, smb_fname->base_name, mode);
1070 }
1071
1072 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
1073                               files_struct *fsp, mode_t mode)
1074 {
1075         return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
1076 }
1077
1078 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
1079                         const struct smb_filename *smb_fname,
1080                         uid_t uid,
1081                         gid_t gid)
1082 {
1083         return glfs_chown(handle->data, smb_fname->base_name, uid, gid);
1084 }
1085
1086 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
1087                               files_struct *fsp, uid_t uid, gid_t gid)
1088 {
1089         return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
1090 }
1091
1092 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
1093                         const struct smb_filename *smb_fname,
1094                         uid_t uid,
1095                         gid_t gid)
1096 {
1097         return glfs_lchown(handle->data, smb_fname->base_name, uid, gid);
1098 }
1099
1100 static int vfs_gluster_chdir(struct vfs_handle_struct *handle,
1101                         const struct smb_filename *smb_fname)
1102 {
1103         return glfs_chdir(handle->data, smb_fname->base_name);
1104 }
1105
1106 static struct smb_filename *vfs_gluster_getwd(struct vfs_handle_struct *handle,
1107                                 TALLOC_CTX *ctx)
1108 {
1109         char *cwd;
1110         char *ret;
1111         struct smb_filename *smb_fname = NULL;
1112
1113         cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
1114         if (cwd == NULL) {
1115                 return NULL;
1116         }
1117
1118         ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
1119         if (ret == NULL) {
1120                 SAFE_FREE(cwd);
1121                 return NULL;
1122         }
1123         smb_fname = synthetic_smb_fname(ctx,
1124                                         ret,
1125                                         NULL,
1126                                         NULL,
1127                                         0);
1128         SAFE_FREE(cwd);
1129         return smb_fname;
1130 }
1131
1132 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
1133                               const struct smb_filename *smb_fname,
1134                               struct smb_file_time *ft)
1135 {
1136         struct timespec times[2];
1137
1138         if (null_timespec(ft->atime)) {
1139                 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
1140                 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
1141         } else {
1142                 times[0].tv_sec = ft->atime.tv_sec;
1143                 times[0].tv_nsec = ft->atime.tv_nsec;
1144         }
1145
1146         if (null_timespec(ft->mtime)) {
1147                 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
1148                 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
1149         } else {
1150                 times[1].tv_sec = ft->mtime.tv_sec;
1151                 times[1].tv_nsec = ft->mtime.tv_nsec;
1152         }
1153
1154         if ((timespec_compare(&times[0],
1155                               &smb_fname->st.st_ex_atime) == 0) &&
1156             (timespec_compare(&times[1],
1157                               &smb_fname->st.st_ex_mtime) == 0)) {
1158                 return 0;
1159         }
1160
1161         return glfs_utimens(handle->data, smb_fname->base_name, times);
1162 }
1163
1164 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1165                                  files_struct *fsp, off_t offset)
1166 {
1167         return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
1168 }
1169
1170 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1171                                  struct files_struct *fsp,
1172                                  uint32_t mode,
1173                                  off_t offset, off_t len)
1174 {
1175 #ifdef HAVE_GFAPI_VER_6
1176         int keep_size, punch_hole;
1177
1178         keep_size = mode & VFS_FALLOCATE_FL_KEEP_SIZE;
1179         punch_hole = mode & VFS_FALLOCATE_FL_PUNCH_HOLE;
1180
1181         mode &= ~(VFS_FALLOCATE_FL_KEEP_SIZE|VFS_FALLOCATE_FL_PUNCH_HOLE);
1182         if (mode != 0) {
1183                 errno = ENOTSUP;
1184                 return -1;
1185         }
1186
1187         if (punch_hole) {
1188                 return glfs_discard(*(glfs_fd_t **)
1189                                     VFS_FETCH_FSP_EXTENSION(handle, fsp),
1190                                     offset, len);
1191         }
1192
1193         return glfs_fallocate(*(glfs_fd_t **)
1194                               VFS_FETCH_FSP_EXTENSION(handle, fsp),
1195                               keep_size, offset, len);
1196 #else
1197         errno = ENOTSUP;
1198         return -1;
1199 #endif
1200 }
1201
1202 static struct smb_filename *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1203                                 TALLOC_CTX *ctx,
1204                                 const struct smb_filename *smb_fname)
1205 {
1206         char *result = NULL;
1207         struct smb_filename *result_fname = NULL;
1208         char *resolved_path = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1209
1210         if (resolved_path == NULL) {
1211                 errno = ENOMEM;
1212                 return NULL;
1213         }
1214
1215         result = glfs_realpath(handle->data,
1216                         smb_fname->base_name,
1217                         resolved_path);
1218         if (result != NULL) {
1219                 result_fname = synthetic_smb_fname(ctx, result, NULL, NULL, 0);
1220         }
1221
1222         SAFE_FREE(resolved_path);
1223         return result_fname;
1224 }
1225
1226 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1227                              files_struct *fsp, int op, off_t offset,
1228                              off_t count, int type)
1229 {
1230         struct flock flock = { 0, };
1231         int ret;
1232
1233         flock.l_type = type;
1234         flock.l_whence = SEEK_SET;
1235         flock.l_start = offset;
1236         flock.l_len = count;
1237         flock.l_pid = 0;
1238
1239         ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
1240
1241         if (op == F_GETLK) {
1242                 /* lock query, true if someone else has locked */
1243                 if ((ret != -1) &&
1244                     (flock.l_type != F_UNLCK) &&
1245                     (flock.l_pid != 0) && (flock.l_pid != getpid()))
1246                         return true;
1247                 /* not me */
1248                 return false;
1249         }
1250
1251         if (ret == -1) {
1252                 return false;
1253         }
1254
1255         return true;
1256 }
1257
1258 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1259                                     files_struct *fsp, uint32_t share_mode,
1260                                     uint32_t access_mask)
1261 {
1262         errno = ENOSYS;
1263         return -1;
1264 }
1265
1266 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1267                                       files_struct *fsp, int leasetype)
1268 {
1269         errno = ENOSYS;
1270         return -1;
1271 }
1272
1273 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1274                                 files_struct *fsp, off_t *poffset,
1275                                 off_t *pcount, int *ptype, pid_t *ppid)
1276 {
1277         struct flock flock = { 0, };
1278         int ret;
1279
1280         flock.l_type = *ptype;
1281         flock.l_whence = SEEK_SET;
1282         flock.l_start = *poffset;
1283         flock.l_len = *pcount;
1284         flock.l_pid = 0;
1285
1286         ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
1287
1288         if (ret == -1) {
1289                 return false;
1290         }
1291
1292         *ptype = flock.l_type;
1293         *poffset = flock.l_start;
1294         *pcount = flock.l_len;
1295         *ppid = flock.l_pid;
1296
1297         return true;
1298 }
1299
1300 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1301                                 const char *link_target,
1302                                 const struct smb_filename *new_smb_fname)
1303 {
1304         return glfs_symlink(handle->data,
1305                         link_target,
1306                         new_smb_fname->base_name);
1307 }
1308
1309 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1310                                 const struct smb_filename *smb_fname,
1311                                 char *buf,
1312                                 size_t bufsiz)
1313 {
1314         return glfs_readlink(handle->data, smb_fname->base_name, buf, bufsiz);
1315 }
1316
1317 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1318                                 const struct smb_filename *old_smb_fname,
1319                                 const struct smb_filename *new_smb_fname)
1320 {
1321         return glfs_link(handle->data,
1322                         old_smb_fname->base_name,
1323                         new_smb_fname->base_name);
1324 }
1325
1326 static int vfs_gluster_mknod(struct vfs_handle_struct *handle,
1327                                 const struct smb_filename *smb_fname,
1328                                 mode_t mode,
1329                                 SMB_DEV_T dev)
1330 {
1331         return glfs_mknod(handle->data, smb_fname->base_name, mode, dev);
1332 }
1333
1334 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1335                                 const struct smb_filename *smb_fname,
1336                                 unsigned int flags)
1337 {
1338         errno = ENOSYS;
1339         return -1;
1340 }
1341
1342 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1343                                          const char *path, const char *name,
1344                                          TALLOC_CTX *mem_ctx, char **found_name)
1345 {
1346         int ret;
1347         char key_buf[NAME_MAX + 64];
1348         char val_buf[NAME_MAX + 1];
1349
1350         if (strlen(name) >= NAME_MAX) {
1351                 errno = ENAMETOOLONG;
1352                 return -1;
1353         }
1354
1355         snprintf(key_buf, NAME_MAX + 64,
1356                  "glusterfs.get_real_filename:%s", name);
1357
1358         ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1359         if (ret == -1) {
1360                 if (errno == ENODATA) {
1361                         errno = EOPNOTSUPP;
1362                 }
1363                 return -1;
1364         }
1365
1366         *found_name = talloc_strdup(mem_ctx, val_buf);
1367         if (found_name[0] == NULL) {
1368                 errno = ENOMEM;
1369                 return -1;
1370         }
1371         return 0;
1372 }
1373
1374 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1375                                 const struct smb_filename *smb_fname)
1376 {
1377         return handle->conn->connectpath;
1378 }
1379
1380 /* EA Operations */
1381
1382 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1383                                 const struct smb_filename *smb_fname,
1384                                 const char *name,
1385                                 void *value,
1386                                 size_t size)
1387 {
1388         return glfs_getxattr(handle->data, smb_fname->base_name,
1389                         name, value, size);
1390 }
1391
1392 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1393                                      files_struct *fsp, const char *name,
1394                                      void *value, size_t size)
1395 {
1396         return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
1397 }
1398
1399 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1400                                 const struct smb_filename *smb_fname,
1401                                 char *list,
1402                                 size_t size)
1403 {
1404         return glfs_listxattr(handle->data, smb_fname->base_name, list, size);
1405 }
1406
1407 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1408                                       files_struct *fsp, char *list,
1409                                       size_t size)
1410 {
1411         return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
1412 }
1413
1414 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1415                                 const struct smb_filename *smb_fname,
1416                                 const char *name)
1417 {
1418         return glfs_removexattr(handle->data, smb_fname->base_name, name);
1419 }
1420
1421 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1422                                     files_struct *fsp, const char *name)
1423 {
1424         return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
1425 }
1426
1427 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1428                                 const struct smb_filename *smb_fname,
1429                                 const char *name,
1430                                 const void *value, size_t size, int flags)
1431 {
1432         return glfs_setxattr(handle->data, smb_fname->base_name, name, value, size, flags);
1433 }
1434
1435 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1436                                  files_struct *fsp, const char *name,
1437                                  const void *value, size_t size, int flags)
1438 {
1439         return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
1440                               flags);
1441 }
1442
1443 /* AIO Operations */
1444
1445 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1446                                   files_struct *fsp)
1447 {
1448         return false;
1449 }
1450
1451 static struct vfs_fn_pointers glusterfs_fns = {
1452
1453         /* Disk Operations */
1454
1455         .connect_fn = vfs_gluster_connect,
1456         .disconnect_fn = vfs_gluster_disconnect,
1457         .disk_free_fn = vfs_gluster_disk_free,
1458         .get_quota_fn = vfs_gluster_get_quota,
1459         .set_quota_fn = vfs_gluster_set_quota,
1460         .statvfs_fn = vfs_gluster_statvfs,
1461         .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1462
1463         .get_dfs_referrals_fn = NULL,
1464
1465         /* Directory Operations */
1466
1467         .opendir_fn = vfs_gluster_opendir,
1468         .fdopendir_fn = vfs_gluster_fdopendir,
1469         .readdir_fn = vfs_gluster_readdir,
1470         .seekdir_fn = vfs_gluster_seekdir,
1471         .telldir_fn = vfs_gluster_telldir,
1472         .rewind_dir_fn = vfs_gluster_rewinddir,
1473         .mkdir_fn = vfs_gluster_mkdir,
1474         .rmdir_fn = vfs_gluster_rmdir,
1475         .closedir_fn = vfs_gluster_closedir,
1476
1477         /* File Operations */
1478
1479         .open_fn = vfs_gluster_open,
1480         .create_file_fn = NULL,
1481         .close_fn = vfs_gluster_close,
1482         .pread_fn = vfs_gluster_pread,
1483         .pread_send_fn = vfs_gluster_pread_send,
1484         .pread_recv_fn = vfs_gluster_recv,
1485         .pwrite_fn = vfs_gluster_pwrite,
1486         .pwrite_send_fn = vfs_gluster_pwrite_send,
1487         .pwrite_recv_fn = vfs_gluster_recv,
1488         .lseek_fn = vfs_gluster_lseek,
1489         .sendfile_fn = vfs_gluster_sendfile,
1490         .recvfile_fn = vfs_gluster_recvfile,
1491         .rename_fn = vfs_gluster_rename,
1492         .fsync_send_fn = vfs_gluster_fsync_send,
1493         .fsync_recv_fn = vfs_gluster_fsync_recv,
1494
1495         .stat_fn = vfs_gluster_stat,
1496         .fstat_fn = vfs_gluster_fstat,
1497         .lstat_fn = vfs_gluster_lstat,
1498         .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1499         .unlink_fn = vfs_gluster_unlink,
1500
1501         .chmod_fn = vfs_gluster_chmod,
1502         .fchmod_fn = vfs_gluster_fchmod,
1503         .chown_fn = vfs_gluster_chown,
1504         .fchown_fn = vfs_gluster_fchown,
1505         .lchown_fn = vfs_gluster_lchown,
1506         .chdir_fn = vfs_gluster_chdir,
1507         .getwd_fn = vfs_gluster_getwd,
1508         .ntimes_fn = vfs_gluster_ntimes,
1509         .ftruncate_fn = vfs_gluster_ftruncate,
1510         .fallocate_fn = vfs_gluster_fallocate,
1511         .lock_fn = vfs_gluster_lock,
1512         .kernel_flock_fn = vfs_gluster_kernel_flock,
1513         .linux_setlease_fn = vfs_gluster_linux_setlease,
1514         .getlock_fn = vfs_gluster_getlock,
1515         .symlink_fn = vfs_gluster_symlink,
1516         .readlink_fn = vfs_gluster_readlink,
1517         .link_fn = vfs_gluster_link,
1518         .mknod_fn = vfs_gluster_mknod,
1519         .realpath_fn = vfs_gluster_realpath,
1520         .chflags_fn = vfs_gluster_chflags,
1521         .file_id_create_fn = NULL,
1522         .streaminfo_fn = NULL,
1523         .get_real_filename_fn = vfs_gluster_get_real_filename,
1524         .connectpath_fn = vfs_gluster_connectpath,
1525
1526         .brl_lock_windows_fn = NULL,
1527         .brl_unlock_windows_fn = NULL,
1528         .brl_cancel_windows_fn = NULL,
1529         .strict_lock_check_fn = NULL,
1530         .translate_name_fn = NULL,
1531         .fsctl_fn = NULL,
1532
1533         /* NT ACL Operations */
1534         .fget_nt_acl_fn = NULL,
1535         .get_nt_acl_fn = NULL,
1536         .fset_nt_acl_fn = NULL,
1537         .audit_file_fn = NULL,
1538
1539         /* Posix ACL Operations */
1540         .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
1541         .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1542         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1543         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1544         .sys_acl_set_file_fn = posixacl_xattr_acl_set_file,
1545         .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1546         .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
1547
1548         /* EA Operations */
1549         .getxattr_fn = vfs_gluster_getxattr,
1550         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1551         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1552         .fgetxattr_fn = vfs_gluster_fgetxattr,
1553         .listxattr_fn = vfs_gluster_listxattr,
1554         .flistxattr_fn = vfs_gluster_flistxattr,
1555         .removexattr_fn = vfs_gluster_removexattr,
1556         .fremovexattr_fn = vfs_gluster_fremovexattr,
1557         .setxattr_fn = vfs_gluster_setxattr,
1558         .fsetxattr_fn = vfs_gluster_fsetxattr,
1559
1560         /* AIO Operations */
1561         .aio_force_fn = vfs_gluster_aio_force,
1562
1563         /* Durable handle Operations */
1564         .durable_cookie_fn = NULL,
1565         .durable_disconnect_fn = NULL,
1566         .durable_reconnect_fn = NULL,
1567 };
1568
1569 static_decl_vfs;
1570 NTSTATUS vfs_glusterfs_init(TALLOC_CTX *ctx)
1571 {
1572         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1573                                 "glusterfs", &glusterfs_fns);
1574 }