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