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