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