vfs_glusterfs: Fix AIO crash on smb.conf reload.
[nivanova/samba-autobuild/.git] / source3 / modules / vfs_glusterfs.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Wrap GlusterFS GFAPI calls in vfs functions.
5
6    Copyright (c) 2013 Anand Avati <avati@redhat.com>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /**
23  * @file   vfs_glusterfs.c
24  * @author Anand Avati <avati@redhat.com>
25  * @date   May 2013
26  * @brief  Samba VFS module for glusterfs
27  *
28  * @todo
29  *   - sendfile/recvfile support
30  *
31  * A Samba VFS module for GlusterFS, based on Gluster's libgfapi.
32  * This is a "bottom" vfs module (not something to be stacked on top of
33  * another module), and translates (most) calls to the closest actions
34  * available in libgfapi.
35  *
36  */
37
38 #include "includes.h"
39 #include "smbd/smbd.h"
40 #include <stdio.h>
41 #include "api/glfs.h"
42 #include "lib/util/dlinklist.h"
43 #include "lib/util/tevent_unix.h"
44 #include "lib/tevent/tevent_internal.h"
45 #include "smbd/globals.h"
46 #include "lib/util/sys_rw.h"
47
48 #define DEFAULT_VOLFILE_SERVER "localhost"
49
50 static int read_fd = -1;
51 static int write_fd = -1;
52 static struct tevent_fd *aio_read_event = NULL;
53
54 /**
55  * Helper to convert struct stat to struct stat_ex.
56  */
57 static void smb_stat_ex_from_stat(struct stat_ex *dst, const struct stat *src)
58 {
59         ZERO_STRUCTP(dst);
60
61         dst->st_ex_dev = src->st_dev;
62         dst->st_ex_ino = src->st_ino;
63         dst->st_ex_mode = src->st_mode;
64         dst->st_ex_nlink = src->st_nlink;
65         dst->st_ex_uid = src->st_uid;
66         dst->st_ex_gid = src->st_gid;
67         dst->st_ex_rdev = src->st_rdev;
68         dst->st_ex_size = src->st_size;
69         dst->st_ex_atime.tv_sec = src->st_atime;
70         dst->st_ex_mtime.tv_sec = src->st_mtime;
71         dst->st_ex_ctime.tv_sec = src->st_ctime;
72         dst->st_ex_btime.tv_sec = src->st_mtime;
73         dst->st_ex_blksize = src->st_blksize;
74         dst->st_ex_blocks = src->st_blocks;
75 #ifdef STAT_HAVE_NSEC
76         dst->st_ex_atime.tv_nsec = src->st_atime_nsec;
77         dst->st_ex_mtime.tv_nsec = src->st_mtime_nsec;
78         dst->st_ex_ctime.tv_nsec = src->st_ctime_nsec;
79         dst->st_ex_btime.tv_nsec = src->st_mtime_nsec;
80 #endif
81 }
82
83 /* pre-opened glfs_t */
84
85 static struct glfs_preopened {
86         char *volume;
87         char *connectpath;
88         glfs_t *fs;
89         int ref;
90         struct glfs_preopened *next, *prev;
91 } *glfs_preopened;
92
93
94 static int glfs_set_preopened(const char *volume, const char *connectpath, glfs_t *fs)
95 {
96         struct glfs_preopened *entry = NULL;
97
98         entry = talloc_zero(NULL, struct glfs_preopened);
99         if (!entry) {
100                 errno = ENOMEM;
101                 return -1;
102         }
103
104         entry->volume = talloc_strdup(entry, volume);
105         if (!entry->volume) {
106                 talloc_free(entry);
107                 errno = ENOMEM;
108                 return -1;
109         }
110
111         entry->connectpath = talloc_strdup(entry, connectpath);
112         if (entry->connectpath == NULL) {
113                 talloc_free(entry);
114                 errno = ENOMEM;
115                 return -1;
116         }
117
118         entry->fs = fs;
119         entry->ref = 1;
120
121         DLIST_ADD(glfs_preopened, entry);
122
123         return 0;
124 }
125
126 static glfs_t *glfs_find_preopened(const char *volume, const char *connectpath)
127 {
128         struct glfs_preopened *entry = NULL;
129
130         for (entry = glfs_preopened; entry; entry = entry->next) {
131                 if (strcmp(entry->volume, volume) == 0 &&
132                     strcmp(entry->connectpath, connectpath) == 0)
133                 {
134                         entry->ref++;
135                         return entry->fs;
136                 }
137         }
138
139         return NULL;
140 }
141
142 static void glfs_clear_preopened(glfs_t *fs)
143 {
144         struct glfs_preopened *entry = NULL;
145
146         for (entry = glfs_preopened; entry; entry = entry->next) {
147                 if (entry->fs == fs) {
148                         if (--entry->ref)
149                                 return;
150
151                         DLIST_REMOVE(glfs_preopened, entry);
152
153                         glfs_fini(entry->fs);
154                         talloc_free(entry);
155                 }
156         }
157 }
158
159 /* Disk Operations */
160
161 static int vfs_gluster_connect(struct vfs_handle_struct *handle,
162                                const char *service,
163                                const char *user)
164 {
165         const char *volfile_server;
166         const char *volume;
167         char *logfile;
168         int loglevel;
169         glfs_t *fs = NULL;
170         TALLOC_CTX *tmp_ctx;
171         int ret = 0;
172
173         tmp_ctx = talloc_new(NULL);
174         if (tmp_ctx == NULL) {
175                 ret = -1;
176                 goto done;
177         }
178         logfile = lp_parm_talloc_string(tmp_ctx, SNUM(handle->conn), "glusterfs",
179                                        "logfile", NULL);
180
181         loglevel = lp_parm_int(SNUM(handle->conn), "glusterfs", "loglevel", -1);
182
183         volfile_server = lp_parm_const_string(SNUM(handle->conn), "glusterfs",
184                                                "volfile_server", NULL);
185         if (volfile_server == NULL) {
186                 volfile_server = DEFAULT_VOLFILE_SERVER;
187         }
188
189         volume = lp_parm_const_string(SNUM(handle->conn), "glusterfs", "volume",
190                                       NULL);
191         if (volume == NULL) {
192                 volume = service;
193         }
194
195         fs = glfs_find_preopened(volume, handle->conn->connectpath);
196         if (fs) {
197                 goto done;
198         }
199
200         fs = glfs_new(volume);
201         if (fs == NULL) {
202                 ret = -1;
203                 goto done;
204         }
205
206         ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 0);
207         if (ret < 0) {
208                 DEBUG(0, ("Failed to set volfile_server %s\n", volfile_server));
209                 goto done;
210         }
211
212         ret = glfs_set_xlator_option(fs, "*-md-cache", "cache-posix-acl",
213                                      "true");
214         if (ret < 0) {
215                 DEBUG(0, ("%s: Failed to set xlator options\n", volume));
216                 goto done;
217         }
218
219
220         ret = glfs_set_xlator_option(fs, "*-snapview-client",
221                                      "snapdir-entry-path",
222                                      handle->conn->connectpath);
223         if (ret < 0) {
224                 DEBUG(0, ("%s: Failed to set xlator option:"
225                           " snapdir-entry-path\n", volume));
226                 glfs_fini(fs);
227                 return -1;
228         }
229
230         ret = glfs_set_logging(fs, logfile, loglevel);
231         if (ret < 0) {
232                 DEBUG(0, ("%s: Failed to set logfile %s loglevel %d\n",
233                           volume, logfile, loglevel));
234                 goto done;
235         }
236
237         ret = glfs_init(fs);
238         if (ret < 0) {
239                 DEBUG(0, ("%s: Failed to initialize volume (%s)\n",
240                           volume, strerror(errno)));
241                 goto done;
242         }
243
244         ret = glfs_set_preopened(volume, handle->conn->connectpath, fs);
245         if (ret < 0) {
246                 DEBUG(0, ("%s: Failed to register volume (%s)\n",
247                           volume, strerror(errno)));
248                 goto done;
249         }
250 done:
251         talloc_free(tmp_ctx);
252         if (ret < 0) {
253                 if (fs)
254                         glfs_fini(fs);
255                 return -1;
256         } else {
257                 DEBUG(0, ("%s: Initialized volume from server %s\n",
258                          volume, volfile_server));
259                 handle->data = fs;
260                 return 0;
261         }
262 }
263
264 static void vfs_gluster_disconnect(struct vfs_handle_struct *handle)
265 {
266         glfs_t *fs = NULL;
267
268         fs = handle->data;
269
270         glfs_clear_preopened(fs);
271 }
272
273 static uint64_t vfs_gluster_disk_free(struct vfs_handle_struct *handle,
274                                       const char *path, uint64_t *bsize_p,
275                                       uint64_t *dfree_p, uint64_t *dsize_p)
276 {
277         struct statvfs statvfs = { 0, };
278         int ret;
279
280         ret = glfs_statvfs(handle->data, path, &statvfs);
281         if (ret < 0) {
282                 return -1;
283         }
284
285         if (bsize_p != NULL) {
286                 *bsize_p = (uint64_t)statvfs.f_bsize; /* Block size */
287         }
288         if (dfree_p != NULL) {
289                 *dfree_p = (uint64_t)statvfs.f_bavail; /* Available Block units */
290         }
291         if (dsize_p != NULL) {
292                 *dsize_p = (uint64_t)statvfs.f_blocks; /* Total Block units */
293         }
294
295         return (uint64_t)statvfs.f_bavail;
296 }
297
298 static int vfs_gluster_get_quota(struct vfs_handle_struct *handle,
299                                  enum SMB_QUOTA_TYPE qtype, unid_t id,
300                                  SMB_DISK_QUOTA *qt)
301 {
302         errno = ENOSYS;
303         return -1;
304 }
305
306 static int
307 vfs_gluster_set_quota(struct vfs_handle_struct *handle,
308                       enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
309 {
310         errno = ENOSYS;
311         return -1;
312 }
313
314 static int vfs_gluster_statvfs(struct vfs_handle_struct *handle,
315                                const char *path,
316                                struct vfs_statvfs_struct *vfs_statvfs)
317 {
318         struct statvfs statvfs = { 0, };
319         int ret;
320
321         ret = glfs_statvfs(handle->data, path, &statvfs);
322         if (ret < 0) {
323                 DEBUG(0, ("glfs_statvfs(%s) failed: %s\n",
324                           path, strerror(errno)));
325                 return -1;
326         }
327
328         ZERO_STRUCTP(vfs_statvfs);
329
330         vfs_statvfs->OptimalTransferSize = statvfs.f_frsize;
331         vfs_statvfs->BlockSize = statvfs.f_bsize;
332         vfs_statvfs->TotalBlocks = statvfs.f_blocks;
333         vfs_statvfs->BlocksAvail = statvfs.f_bfree;
334         vfs_statvfs->UserBlocksAvail = statvfs.f_bavail;
335         vfs_statvfs->TotalFileNodes = statvfs.f_files;
336         vfs_statvfs->FreeFileNodes = statvfs.f_ffree;
337         vfs_statvfs->FsIdentifier = statvfs.f_fsid;
338         vfs_statvfs->FsCapabilities =
339             FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
340
341         return ret;
342 }
343
344 static uint32_t vfs_gluster_fs_capabilities(struct vfs_handle_struct *handle,
345                                             enum timestamp_set_resolution *p_ts_res)
346 {
347         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
348
349 #ifdef STAT_HAVE_NSEC
350         *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
351 #endif
352
353         return caps;
354 }
355
356 static DIR *vfs_gluster_opendir(struct vfs_handle_struct *handle,
357                                 const char *path, const char *mask,
358                                 uint32_t attributes)
359 {
360         glfs_fd_t *fd;
361
362         fd = glfs_opendir(handle->data, path);
363         if (fd == NULL) {
364                 DEBUG(0, ("glfs_opendir(%s) failed: %s\n",
365                           path, strerror(errno)));
366         }
367
368         return (DIR *) fd;
369 }
370
371 static DIR *vfs_gluster_fdopendir(struct vfs_handle_struct *handle,
372                                   files_struct *fsp, const char *mask,
373                                   uint32_t attributes)
374 {
375         return (DIR *) *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
376 }
377
378 static int vfs_gluster_closedir(struct vfs_handle_struct *handle, DIR *dirp)
379 {
380         return glfs_closedir((void *)dirp);
381 }
382
383 static struct dirent *vfs_gluster_readdir(struct vfs_handle_struct *handle,
384                                           DIR *dirp, SMB_STRUCT_STAT *sbuf)
385 {
386         static char direntbuf[512];
387         int ret;
388         struct stat stat;
389         struct dirent *dirent = 0;
390
391         if (sbuf != NULL) {
392                 ret = glfs_readdirplus_r((void *)dirp, &stat, (void *)direntbuf,
393                                          &dirent);
394         } else {
395                 ret = glfs_readdir_r((void *)dirp, (void *)direntbuf, &dirent);
396         }
397
398         if ((ret < 0) || (dirent == NULL)) {
399                 return NULL;
400         }
401
402         if (sbuf != NULL) {
403                 smb_stat_ex_from_stat(sbuf, &stat);
404         }
405
406         return dirent;
407 }
408
409 static long vfs_gluster_telldir(struct vfs_handle_struct *handle, DIR *dirp)
410 {
411         return glfs_telldir((void *)dirp);
412 }
413
414 static void vfs_gluster_seekdir(struct vfs_handle_struct *handle, DIR *dirp,
415                                 long offset)
416 {
417         glfs_seekdir((void *)dirp, offset);
418 }
419
420 static void vfs_gluster_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
421 {
422         glfs_seekdir((void *)dirp, 0);
423 }
424
425 static void vfs_gluster_init_search_op(struct vfs_handle_struct *handle,
426                                        DIR *dirp)
427 {
428         return;
429 }
430
431 static int vfs_gluster_mkdir(struct vfs_handle_struct *handle, const char *path,
432                              mode_t mode)
433 {
434         return glfs_mkdir(handle->data, path, mode);
435 }
436
437 static int vfs_gluster_rmdir(struct vfs_handle_struct *handle, const char *path)
438 {
439         return glfs_rmdir(handle->data, path);
440 }
441
442 static int vfs_gluster_open(struct vfs_handle_struct *handle,
443                             struct smb_filename *smb_fname, files_struct *fsp,
444                             int flags, mode_t mode)
445 {
446         glfs_fd_t *glfd;
447         glfs_fd_t **p_tmp;
448
449         if (flags & O_DIRECTORY) {
450                 glfd = glfs_opendir(handle->data, smb_fname->base_name);
451         } else if (flags & O_CREAT) {
452                 glfd = glfs_creat(handle->data, smb_fname->base_name, flags,
453                                   mode);
454         } else {
455                 glfd = glfs_open(handle->data, smb_fname->base_name, flags);
456         }
457
458         if (glfd == NULL) {
459                 return -1;
460         }
461         p_tmp = (glfs_fd_t **)VFS_ADD_FSP_EXTENSION(handle, fsp,
462                                                           glfs_fd_t *, NULL);
463         *p_tmp = glfd;
464         /* An arbitrary value for error reporting, so you know its us. */
465         return 13371337;
466 }
467
468 static int vfs_gluster_close(struct vfs_handle_struct *handle,
469                              files_struct *fsp)
470 {
471         glfs_fd_t *glfd;
472         glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
473         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
474         return glfs_close(glfd);
475 }
476
477 static ssize_t vfs_gluster_read(struct vfs_handle_struct *handle,
478                                 files_struct *fsp, void *data, size_t n)
479 {
480         return glfs_read(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
481 }
482
483 static ssize_t vfs_gluster_pread(struct vfs_handle_struct *handle,
484                                  files_struct *fsp, void *data, size_t n,
485                                  off_t offset)
486 {
487         return glfs_pread(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
488 }
489
490 struct glusterfs_aio_state;
491
492 struct glusterfs_aio_wrapper {
493         struct glusterfs_aio_state *state;
494 };
495
496 struct glusterfs_aio_state {
497         ssize_t ret;
498         int err;
499         struct tevent_req *req;
500         bool cancelled;
501 };
502
503 static int aio_wrapper_destructor(void *ptr)
504 {
505         struct glusterfs_aio_wrapper *wrap = (struct glusterfs_aio_wrapper *)ptr;
506
507         wrap->state->cancelled = true;
508
509         return 0;
510 }
511
512 /*
513  * This function is the callback that will be called on glusterfs
514  * threads once the async IO submitted is complete. To notify
515  * Samba of the completion we use a pipe based queue.
516  */
517 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
518 {
519         struct glusterfs_aio_state *state = NULL;
520         int sts = 0;
521
522         state = (struct glusterfs_aio_state *)data;
523
524         if (ret < 0) {
525                 state->ret = -1;
526                 state->err = errno;
527         } else {
528                 state->ret = ret;
529                 state->err = 0;
530         }
531
532         /*
533          * Write the state pointer to glusterfs_aio_state to the
534          * pipe, so we can call tevent_req_done() from the main thread,
535          * because tevent_req_done() is not designed to be executed in
536          * the multithread environment, so tevent_req_done() must be
537          * executed from the smbd main thread.
538          *
539          * write(2) on pipes with sizes under _POSIX_PIPE_BUF
540          * in size is atomic, without this, the use op pipes in this
541          * code would not work.
542          *
543          * sys_write is a thin enough wrapper around write(2)
544          * that we can trust it here.
545          */
546
547         sts = sys_write(write_fd, &state, sizeof(struct glusterfs_aio_state *));
548         if (sts < 0) {
549                 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
550         }
551
552         return;
553 }
554
555 /*
556  * Read each req off the pipe and process it.
557  */
558 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
559                                 struct tevent_fd *fde,
560                                 uint16_t flags, void *data)
561 {
562         struct tevent_req *req = NULL;
563         struct glusterfs_aio_state *state = NULL;
564         int sts = 0;
565
566         /*
567          * read(2) on pipes is atomic if the needed data is available
568          * in the pipe, per SUS and POSIX.  Because we always write
569          * to the pipe in sizeof(struct tevent_req *) chunks, we can
570          * always read in those chunks, atomically.
571          *
572          * sys_read is a thin enough wrapper around read(2) that we
573          * can trust it here.
574          */
575
576         sts = sys_read(read_fd, &state, sizeof(struct glusterfs_aio_state *));
577
578         if (sts < 0) {
579                 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
580         }
581
582         if (state->cancelled) {
583                 return;
584         }
585
586         req = state->req;
587
588         /* if we've cancelled the op, there is no req, so just clean up. */
589         if (state->cancelled == true) {
590                 TALLOC_FREE(state);
591                 return;
592         }
593
594         if (req) {
595                 tevent_req_done(req);
596         }
597         return;
598 }
599
600 static bool init_gluster_aio(struct vfs_handle_struct *handle)
601 {
602         int fds[2];
603         int ret = -1;
604
605         if (read_fd != -1) {
606                 /*
607                  * Already initialized.
608                  */
609                 return true;
610         }
611
612         ret = pipe(fds);
613         if (ret == -1) {
614                 goto fail;
615         }
616
617         read_fd = fds[0];
618         write_fd = fds[1];
619
620         aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
621                                         NULL,
622                                         read_fd,
623                                         TEVENT_FD_READ,
624                                         aio_tevent_fd_done,
625                                         NULL);
626         if (aio_read_event == NULL) {
627                 goto fail;
628         }
629
630         return true;
631 fail:
632         TALLOC_FREE(aio_read_event);
633         if (read_fd != -1) {
634                 close(read_fd);
635                 close(write_fd);
636                 read_fd = -1;
637                 write_fd = -1;
638         }
639         return false;
640 }
641
642 static struct glusterfs_aio_state *aio_state_create(TALLOC_CTX *mem_ctx)
643 {
644         struct tevent_req *req = NULL;
645         struct glusterfs_aio_state *state = NULL;
646         struct glusterfs_aio_wrapper *wrapper = NULL;
647
648         req = tevent_req_create(mem_ctx, &wrapper, struct glusterfs_aio_wrapper);
649
650         if (req == NULL) {
651                 return NULL;
652         }
653
654         state = talloc(NULL, struct glusterfs_aio_state);
655
656         if (state == NULL) {
657                 TALLOC_FREE(req);
658                 return NULL;
659         }
660
661         state->cancelled = false;
662         state->ret = 0;
663         state->err = 0;
664         state->req = req;
665
666         wrapper->state = state;
667
668         return state;
669 }
670
671 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
672                                                   *handle, TALLOC_CTX *mem_ctx,
673                                                   struct tevent_context *ev,
674                                                   files_struct *fsp,
675                                                   void *data, size_t n,
676                                                   off_t offset)
677 {
678         struct glusterfs_aio_state *state = NULL;
679         struct tevent_req *req = NULL;
680         int ret = 0;
681
682         state = aio_state_create(mem_ctx);
683
684         if (state == NULL) {
685                 return NULL;
686         }
687
688         req = state->req;
689
690         if (!init_gluster_aio(handle)) {
691                 tevent_req_error(req, EIO);
692                 return tevent_req_post(req, ev);
693         }
694         ret = glfs_pread_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
695                                 fsp), data, n, offset, 0, aio_glusterfs_done,
696                                 state);
697
698         if (ret < 0) {
699                 tevent_req_error(req, -ret);
700                 return tevent_req_post(req, ev);
701         }
702
703         return req;
704 }
705
706 static ssize_t vfs_gluster_write(struct vfs_handle_struct *handle,
707                                  files_struct *fsp, const void *data, size_t n)
708 {
709         return glfs_write(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, 0);
710 }
711
712 static ssize_t vfs_gluster_pwrite(struct vfs_handle_struct *handle,
713                                   files_struct *fsp, const void *data,
714                                   size_t n, off_t offset)
715 {
716         return glfs_pwrite(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), data, n, offset, 0);
717 }
718
719 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
720                                                   *handle, TALLOC_CTX *mem_ctx,
721                                                   struct tevent_context *ev,
722                                                   files_struct *fsp,
723                                                   const void *data, size_t n,
724                                                   off_t offset)
725 {
726         struct glusterfs_aio_state *state = NULL;
727         struct tevent_req *req = NULL;
728         int ret = 0;
729
730         state = aio_state_create(mem_ctx);
731
732         if (state == NULL) {
733                 return NULL;
734         }
735
736         req = state->req;
737
738         if (!init_gluster_aio(handle)) {
739                 tevent_req_error(req, EIO);
740                 return tevent_req_post(req, ev);
741         }
742         ret = glfs_pwrite_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
743                                 fsp), data, n, offset, 0, aio_glusterfs_done,
744                                 state);
745         if (ret < 0) {
746                 tevent_req_error(req, -ret);
747                 return tevent_req_post(req, ev);
748         }
749         return req;
750 }
751
752 static ssize_t vfs_gluster_recv(struct tevent_req *req, int *err)
753 {
754         struct glusterfs_aio_state *state = NULL;
755         struct glusterfs_aio_wrapper *wrapper = NULL;
756         int ret = 0;
757
758         wrapper = tevent_req_data(req, struct glusterfs_aio_wrapper);
759
760         if (wrapper == NULL) {
761                 return -1;
762         }
763
764         state = wrapper->state;
765
766         if (state == NULL) {
767                 return -1;
768         }
769
770         if (tevent_req_is_unix_error(req, err)) {
771                 return -1;
772         }
773         if (state->ret == -1) {
774                 *err = state->err;
775         }
776
777         ret = state->ret;
778
779         /* Clean up the state, it is in a NULL context. */
780
781         TALLOC_FREE(state);
782
783         return ret;
784 }
785
786 static off_t vfs_gluster_lseek(struct vfs_handle_struct *handle,
787                                files_struct *fsp, off_t offset, int whence)
788 {
789         return glfs_lseek(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset, whence);
790 }
791
792 static ssize_t vfs_gluster_sendfile(struct vfs_handle_struct *handle, int tofd,
793                                     files_struct *fromfsp,
794                                     const DATA_BLOB *hdr,
795                                     off_t offset, size_t n)
796 {
797         errno = ENOTSUP;
798         return -1;
799 }
800
801 static ssize_t vfs_gluster_recvfile(struct vfs_handle_struct *handle,
802                                     int fromfd, files_struct *tofsp,
803                                     off_t offset, size_t n)
804 {
805         errno = ENOTSUP;
806         return -1;
807 }
808
809 static int vfs_gluster_rename(struct vfs_handle_struct *handle,
810                               const struct smb_filename *smb_fname_src,
811                               const struct smb_filename *smb_fname_dst)
812 {
813         return glfs_rename(handle->data, smb_fname_src->base_name,
814                            smb_fname_dst->base_name);
815 }
816
817 static int vfs_gluster_fsync(struct vfs_handle_struct *handle,
818                              files_struct *fsp)
819 {
820         return glfs_fsync(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp));
821 }
822
823 static struct tevent_req *vfs_gluster_fsync_send(struct vfs_handle_struct
824                                                  *handle, TALLOC_CTX *mem_ctx,
825                                                  struct tevent_context *ev,
826                                                  files_struct *fsp)
827 {
828         struct tevent_req *req = NULL;
829         struct glusterfs_aio_state *state = NULL;
830         int ret = 0;
831
832         state = aio_state_create(mem_ctx);
833
834         if (state == NULL) {
835                 return NULL;
836         }
837
838         req = state->req;
839
840         if (!init_gluster_aio(handle)) {
841                 tevent_req_error(req, EIO);
842                 return tevent_req_post(req, ev);
843         }
844         ret = glfs_fsync_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
845                                 fsp), aio_glusterfs_done, req);
846         if (ret < 0) {
847                 tevent_req_error(req, -ret);
848                 return tevent_req_post(req, ev);
849         }
850         return req;
851 }
852
853 static int vfs_gluster_fsync_recv(struct tevent_req *req, int *err)
854 {
855         /*
856          * Use implicit conversion ssize_t->int
857          */
858         return vfs_gluster_recv(req, err);
859 }
860
861 static int vfs_gluster_stat(struct vfs_handle_struct *handle,
862                             struct smb_filename *smb_fname)
863 {
864         struct stat st;
865         int ret;
866
867         ret = glfs_stat(handle->data, smb_fname->base_name, &st);
868         if (ret == 0) {
869                 smb_stat_ex_from_stat(&smb_fname->st, &st);
870         }
871         if (ret < 0 && errno != ENOENT) {
872                 DEBUG(0, ("glfs_stat(%s) failed: %s\n",
873                           smb_fname->base_name, strerror(errno)));
874         }
875         return ret;
876 }
877
878 static int vfs_gluster_fstat(struct vfs_handle_struct *handle,
879                              files_struct *fsp, SMB_STRUCT_STAT *sbuf)
880 {
881         struct stat st;
882         int ret;
883
884         ret = glfs_fstat(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), &st);
885         if (ret == 0) {
886                 smb_stat_ex_from_stat(sbuf, &st);
887         }
888         if (ret < 0) {
889                 DEBUG(0, ("glfs_fstat(%d) failed: %s\n",
890                           fsp->fh->fd, strerror(errno)));
891         }
892         return ret;
893 }
894
895 static int vfs_gluster_lstat(struct vfs_handle_struct *handle,
896                              struct smb_filename *smb_fname)
897 {
898         struct stat st;
899         int ret;
900
901         ret = glfs_lstat(handle->data, smb_fname->base_name, &st);
902         if (ret == 0) {
903                 smb_stat_ex_from_stat(&smb_fname->st, &st);
904         }
905         if (ret < 0 && errno != ENOENT) {
906                 DEBUG(0, ("glfs_lstat(%s) failed: %s\n",
907                           smb_fname->base_name, strerror(errno)));
908         }
909         return ret;
910 }
911
912 static uint64_t vfs_gluster_get_alloc_size(struct vfs_handle_struct *handle,
913                                            files_struct *fsp,
914                                            const SMB_STRUCT_STAT *sbuf)
915 {
916         return sbuf->st_ex_blocks * 512;
917 }
918
919 static int vfs_gluster_unlink(struct vfs_handle_struct *handle,
920                               const struct smb_filename *smb_fname)
921 {
922         return glfs_unlink(handle->data, smb_fname->base_name);
923 }
924
925 static int vfs_gluster_chmod(struct vfs_handle_struct *handle,
926                              const char *path, mode_t mode)
927 {
928         return glfs_chmod(handle->data, path, mode);
929 }
930
931 static int vfs_gluster_fchmod(struct vfs_handle_struct *handle,
932                               files_struct *fsp, mode_t mode)
933 {
934         return glfs_fchmod(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), mode);
935 }
936
937 static int vfs_gluster_chown(struct vfs_handle_struct *handle,
938                              const char *path, uid_t uid, gid_t gid)
939 {
940         return glfs_chown(handle->data, path, uid, gid);
941 }
942
943 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
944                               files_struct *fsp, uid_t uid, gid_t gid)
945 {
946         return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
947 }
948
949 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
950                               const char *path, uid_t uid, gid_t gid)
951 {
952         return glfs_lchown(handle->data, path, uid, gid);
953 }
954
955 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
956 {
957         return glfs_chdir(handle->data, path);
958 }
959
960 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
961 {
962         char *cwd;
963         char *ret;
964
965         cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
966         if (cwd == NULL) {
967                 return NULL;
968         }
969
970         ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
971         if (ret == 0) {
972                 free(cwd);
973         }
974         return ret;
975 }
976
977 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
978                               const struct smb_filename *smb_fname,
979                               struct smb_file_time *ft)
980 {
981         struct timespec times[2];
982
983         if (null_timespec(ft->atime)) {
984                 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
985                 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
986         } else {
987                 times[0].tv_sec = ft->atime.tv_sec;
988                 times[0].tv_nsec = ft->atime.tv_nsec;
989         }
990
991         if (null_timespec(ft->mtime)) {
992                 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
993                 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
994         } else {
995                 times[1].tv_sec = ft->mtime.tv_sec;
996                 times[1].tv_nsec = ft->mtime.tv_nsec;
997         }
998
999         if ((timespec_compare(&times[0],
1000                               &smb_fname->st.st_ex_atime) == 0) &&
1001             (timespec_compare(&times[1],
1002                               &smb_fname->st.st_ex_mtime) == 0)) {
1003                 return 0;
1004         }
1005
1006         return glfs_utimens(handle->data, smb_fname->base_name, times);
1007 }
1008
1009 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1010                                  files_struct *fsp, off_t offset)
1011 {
1012         return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
1013 }
1014
1015 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1016                                  struct files_struct *fsp,
1017                                  uint32_t mode,
1018                                  off_t offset, off_t len)
1019 {
1020         /* TODO: add support using glfs_fallocate() and glfs_zerofill() */
1021         errno = ENOTSUP;
1022         return -1;
1023 }
1024
1025 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1026                                   const char *path)
1027 {
1028         return glfs_realpath(handle->data, path, 0);
1029 }
1030
1031 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1032                              files_struct *fsp, int op, off_t offset,
1033                              off_t count, int type)
1034 {
1035         struct flock flock = { 0, };
1036         int ret;
1037
1038         flock.l_type = type;
1039         flock.l_whence = SEEK_SET;
1040         flock.l_start = offset;
1041         flock.l_len = count;
1042         flock.l_pid = 0;
1043
1044         ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
1045
1046         if (op == F_GETLK) {
1047                 /* lock query, true if someone else has locked */
1048                 if ((ret != -1) &&
1049                     (flock.l_type != F_UNLCK) &&
1050                     (flock.l_pid != 0) && (flock.l_pid != getpid()))
1051                         return true;
1052                 /* not me */
1053                 return false;
1054         }
1055
1056         if (ret == -1) {
1057                 return false;
1058         }
1059
1060         return true;
1061 }
1062
1063 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1064                                     files_struct *fsp, uint32_t share_mode,
1065                                     uint32_t access_mask)
1066 {
1067         errno = ENOSYS;
1068         return -1;
1069 }
1070
1071 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1072                                       files_struct *fsp, int leasetype)
1073 {
1074         errno = ENOSYS;
1075         return -1;
1076 }
1077
1078 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1079                                 files_struct *fsp, off_t *poffset,
1080                                 off_t *pcount, int *ptype, pid_t *ppid)
1081 {
1082         struct flock flock = { 0, };
1083         int ret;
1084
1085         flock.l_type = *ptype;
1086         flock.l_whence = SEEK_SET;
1087         flock.l_start = *poffset;
1088         flock.l_len = *pcount;
1089         flock.l_pid = 0;
1090
1091         ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
1092
1093         if (ret == -1) {
1094                 return false;
1095         }
1096
1097         *ptype = flock.l_type;
1098         *poffset = flock.l_start;
1099         *pcount = flock.l_len;
1100         *ppid = flock.l_pid;
1101
1102         return true;
1103 }
1104
1105 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1106                                const char *oldpath, const char *newpath)
1107 {
1108         return glfs_symlink(handle->data, oldpath, newpath);
1109 }
1110
1111 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1112                                 const char *path, char *buf, size_t bufsiz)
1113 {
1114         return glfs_readlink(handle->data, path, buf, bufsiz);
1115 }
1116
1117 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1118                             const char *oldpath, const char *newpath)
1119 {
1120         return glfs_link(handle->data, oldpath, newpath);
1121 }
1122
1123 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
1124                              mode_t mode, SMB_DEV_T dev)
1125 {
1126         return glfs_mknod(handle->data, path, mode, dev);
1127 }
1128
1129 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1130                                const char *path, unsigned int flags)
1131 {
1132         errno = ENOSYS;
1133         return -1;
1134 }
1135
1136 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1137                                          const char *path, const char *name,
1138                                          TALLOC_CTX *mem_ctx, char **found_name)
1139 {
1140         int ret;
1141         char key_buf[NAME_MAX + 64];
1142         char val_buf[NAME_MAX + 1];
1143
1144         if (strlen(name) >= NAME_MAX) {
1145                 errno = ENAMETOOLONG;
1146                 return -1;
1147         }
1148
1149         snprintf(key_buf, NAME_MAX + 64,
1150                  "glusterfs.get_real_filename:%s", name);
1151
1152         ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1153         if (ret == -1) {
1154                 if (errno == ENODATA) {
1155                         errno = EOPNOTSUPP;
1156                 }
1157                 return -1;
1158         }
1159
1160         *found_name = talloc_strdup(mem_ctx, val_buf);
1161         if (found_name[0] == NULL) {
1162                 errno = ENOMEM;
1163                 return -1;
1164         }
1165         return 0;
1166 }
1167
1168 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1169                                            const char *filename)
1170 {
1171         return handle->conn->connectpath;
1172 }
1173
1174 /* EA Operations */
1175
1176 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1177                                     const char *path, const char *name,
1178                                     void *value, size_t size)
1179 {
1180         return glfs_getxattr(handle->data, path, name, value, size);
1181 }
1182
1183 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1184                                      files_struct *fsp, const char *name,
1185                                      void *value, size_t size)
1186 {
1187         return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
1188 }
1189
1190 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1191                                      const char *path, char *list, size_t size)
1192 {
1193         return glfs_listxattr(handle->data, path, list, size);
1194 }
1195
1196 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1197                                       files_struct *fsp, char *list,
1198                                       size_t size)
1199 {
1200         return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
1201 }
1202
1203 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1204                                    const char *path, const char *name)
1205 {
1206         return glfs_removexattr(handle->data, path, name);
1207 }
1208
1209 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1210                                     files_struct *fsp, const char *name)
1211 {
1212         return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
1213 }
1214
1215 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1216                                 const char *path, const char *name,
1217                                 const void *value, size_t size, int flags)
1218 {
1219         return glfs_setxattr(handle->data, path, name, value, size, flags);
1220 }
1221
1222 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1223                                  files_struct *fsp, const char *name,
1224                                  const void *value, size_t size, int flags)
1225 {
1226         return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
1227                               flags);
1228 }
1229
1230 /* AIO Operations */
1231
1232 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1233                                   files_struct *fsp)
1234 {
1235         return false;
1236 }
1237
1238 /* Offline Operations */
1239
1240 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
1241                                    const struct smb_filename *fname,
1242                                    SMB_STRUCT_STAT *sbuf)
1243 {
1244         return false;
1245 }
1246
1247 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
1248                                    const struct smb_filename *fname)
1249 {
1250         errno = ENOTSUP;
1251         return -1;
1252 }
1253
1254 /*
1255   Gluster ACL Format:
1256
1257   Size = 4 (header) + N * 8 (entry)
1258
1259   Offset  Size    Field (Little Endian)
1260   -------------------------------------
1261   0-3     4-byte  Version
1262
1263   4-5     2-byte  Entry-1 tag
1264   6-7     2-byte  Entry-1 perm
1265   8-11    4-byte  Entry-1 id
1266
1267   12-13   2-byte  Entry-2 tag
1268   14-15   2-byte  Entry-2 perm
1269   16-19   4-byte  Entry-2 id
1270
1271   ...
1272
1273  */
1274
1275 /* header version */
1276 #define GLUSTER_ACL_VERSION 2
1277
1278 /* perm bits */
1279 #define GLUSTER_ACL_READ    0x04
1280 #define GLUSTER_ACL_WRITE   0x02
1281 #define GLUSTER_ACL_EXECUTE 0x01
1282
1283 /* tag values */
1284 #define GLUSTER_ACL_UNDEFINED_TAG  0x00
1285 #define GLUSTER_ACL_USER_OBJ       0x01
1286 #define GLUSTER_ACL_USER           0x02
1287 #define GLUSTER_ACL_GROUP_OBJ      0x04
1288 #define GLUSTER_ACL_GROUP          0x08
1289 #define GLUSTER_ACL_MASK           0x10
1290 #define GLUSTER_ACL_OTHER          0x20
1291
1292 #define GLUSTER_ACL_UNDEFINED_ID  (-1)
1293
1294 #define GLUSTER_ACL_HEADER_SIZE    4
1295 #define GLUSTER_ACL_ENTRY_SIZE     8
1296
1297 #define GLUSTER_ACL_SIZE(n)       (GLUSTER_ACL_HEADER_SIZE + (n * GLUSTER_ACL_ENTRY_SIZE))
1298
1299 static SMB_ACL_T mode_to_smb_acls(const struct stat *mode, TALLOC_CTX *mem_ctx)
1300 {
1301         struct smb_acl_t *result;
1302         int count;
1303
1304         count = 3;
1305         result = sys_acl_init(mem_ctx);
1306         if (!result) {
1307                 errno = ENOMEM;
1308                 return NULL;
1309         }
1310
1311         result->acl = talloc_array(result, struct smb_acl_entry, count);
1312         if (!result->acl) {
1313                 errno = ENOMEM;
1314                 talloc_free(result);
1315                 return NULL;
1316         }
1317
1318         result->count = count;
1319
1320         result->acl[0].a_type = SMB_ACL_USER_OBJ;
1321         result->acl[0].a_perm = (mode->st_mode & S_IRWXU) >> 6;;
1322
1323         result->acl[1].a_type = SMB_ACL_GROUP_OBJ;
1324         result->acl[1].a_perm = (mode->st_mode & S_IRWXG) >> 3;;
1325
1326         result->acl[2].a_type = SMB_ACL_OTHER;
1327         result->acl[2].a_perm = mode->st_mode & S_IRWXO;;
1328
1329         return result;
1330 }
1331
1332 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1333                                     TALLOC_CTX *mem_ctx)
1334 {
1335         int count;
1336         size_t size;
1337         struct smb_acl_entry *smb_ace;
1338         struct smb_acl_t *result;
1339         int i;
1340         int offset;
1341         uint16_t tag;
1342         uint16_t perm;
1343         uint32_t id;
1344
1345         size = xattr_size;
1346
1347         if (size < GLUSTER_ACL_HEADER_SIZE) {
1348                 /* ACL should be at least as big as the header (4 bytes) */
1349                 errno = EINVAL;
1350                 return NULL;
1351         }
1352
1353         size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1354
1355         if (size % GLUSTER_ACL_ENTRY_SIZE) {
1356                 /* Size of entries must strictly be a multiple of
1357                    size of an ACE (8 bytes)
1358                 */
1359                 errno = EINVAL;
1360                 return NULL;
1361         }
1362
1363         count = size / GLUSTER_ACL_ENTRY_SIZE;
1364
1365         /* Version is the first 4 bytes of the ACL */
1366         if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1367                 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1368                           IVAL(buf, 0)));
1369                 return NULL;
1370         }
1371         offset = GLUSTER_ACL_HEADER_SIZE;
1372
1373         result = sys_acl_init(mem_ctx);
1374         if (!result) {
1375                 errno = ENOMEM;
1376                 return NULL;
1377         }
1378
1379         result->acl = talloc_array(result, struct smb_acl_entry, count);
1380         if (!result->acl) {
1381                 errno = ENOMEM;
1382                 talloc_free(result);
1383                 return NULL;
1384         }
1385
1386         result->count = count;
1387
1388         smb_ace = result->acl;
1389
1390         for (i = 0; i < count; i++) {
1391                 /* TAG is the first 2 bytes of an entry */
1392                 tag = SVAL(buf, offset);
1393                 offset += 2;
1394
1395                 /* PERM is the next 2 bytes of an entry */
1396                 perm = SVAL(buf, offset);
1397                 offset += 2;
1398
1399                 /* ID is the last 4 bytes of an entry */
1400                 id = IVAL(buf, offset);
1401                 offset += 4;
1402
1403                 switch(tag) {
1404                 case GLUSTER_ACL_USER:
1405                         smb_ace->a_type = SMB_ACL_USER;
1406                         break;
1407                 case GLUSTER_ACL_USER_OBJ:
1408                         smb_ace->a_type = SMB_ACL_USER_OBJ;
1409                         break;
1410                 case GLUSTER_ACL_GROUP:
1411                         smb_ace->a_type = SMB_ACL_GROUP;
1412                         break;
1413                 case GLUSTER_ACL_GROUP_OBJ:
1414                         smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1415                         break;
1416                 case GLUSTER_ACL_OTHER:
1417                         smb_ace->a_type = SMB_ACL_OTHER;
1418                         break;
1419                 case GLUSTER_ACL_MASK:
1420                         smb_ace->a_type = SMB_ACL_MASK;
1421                         break;
1422                 default:
1423                         DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1424                         return NULL;
1425                 }
1426
1427
1428                 switch(smb_ace->a_type) {
1429                 case SMB_ACL_USER:
1430                         smb_ace->info.user.uid = id;
1431                         break;
1432                 case SMB_ACL_GROUP:
1433                         smb_ace->info.group.gid = id;
1434                         break;
1435                 default:
1436                         break;
1437                 }
1438
1439                 smb_ace->a_perm = 0;
1440                 smb_ace->a_perm |=
1441                         ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1442                 smb_ace->a_perm |=
1443                         ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1444                 smb_ace->a_perm |=
1445                         ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1446
1447                 smb_ace++;
1448         }
1449
1450         return result;
1451 }
1452
1453
1454 static int gluster_ace_cmp(const void *left, const void *right)
1455 {
1456         int ret = 0;
1457         uint16_t tag_left, tag_right;
1458         uint32_t id_left, id_right;
1459
1460         /*
1461           Sorting precedence:
1462
1463            - Smaller TAG values must be earlier.
1464
1465            - Within same TAG, smaller identifiers must be earlier, E.g:
1466              UID 0 entry must be earlier than UID 200
1467              GID 17 entry must be earlier than GID 19
1468         */
1469
1470         /* TAG is the first element in the entry */
1471         tag_left = SVAL(left, 0);
1472         tag_right = SVAL(right, 0);
1473
1474         ret = (tag_left - tag_right);
1475         if (!ret) {
1476                 /* ID is the third element in the entry, after two short
1477                    integers (tag and perm), i.e at offset 4.
1478                 */
1479                 id_left = IVAL(left, 4);
1480                 id_right = IVAL(right, 4);
1481                 ret = id_left - id_right;
1482         }
1483
1484         return ret;
1485 }
1486
1487
1488 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1489 {
1490         ssize_t size;
1491         struct smb_acl_entry *smb_ace;
1492         int i;
1493         int count;
1494         uint16_t tag;
1495         uint16_t perm;
1496         uint32_t id;
1497         int offset;
1498
1499         count = theacl->count;
1500
1501         size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1502         if (!buf) {
1503                 return size;
1504         }
1505
1506         if (len < size) {
1507                 errno = ERANGE;
1508                 return -1;
1509         }
1510
1511         smb_ace = theacl->acl;
1512
1513         /* Version is the first 4 bytes of the ACL */
1514         SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1515         offset = GLUSTER_ACL_HEADER_SIZE;
1516
1517         for (i = 0; i < count; i++) {
1518                 /* Calculate tag */
1519                 switch(smb_ace->a_type) {
1520                 case SMB_ACL_USER:
1521                         tag = GLUSTER_ACL_USER;
1522                         break;
1523                 case SMB_ACL_USER_OBJ:
1524                         tag = GLUSTER_ACL_USER_OBJ;
1525                         break;
1526                 case SMB_ACL_GROUP:
1527                         tag = GLUSTER_ACL_GROUP;
1528                         break;
1529                 case SMB_ACL_GROUP_OBJ:
1530                         tag = GLUSTER_ACL_GROUP_OBJ;
1531                         break;
1532                 case SMB_ACL_OTHER:
1533                         tag = GLUSTER_ACL_OTHER;
1534                         break;
1535                 case SMB_ACL_MASK:
1536                         tag = GLUSTER_ACL_MASK;
1537                         break;
1538                 default:
1539                         DEBUG(0, ("Unknown tag value %d\n",
1540                                   smb_ace->a_type));
1541                         errno = EINVAL;
1542                         return -1;
1543                 }
1544
1545
1546                 /* Calculate id */
1547                 switch(smb_ace->a_type) {
1548                 case SMB_ACL_USER:
1549                         id = smb_ace->info.user.uid;
1550                         break;
1551                 case SMB_ACL_GROUP:
1552                         id = smb_ace->info.group.gid;
1553                         break;
1554                 default:
1555                         id = GLUSTER_ACL_UNDEFINED_ID;
1556                         break;
1557                 }
1558
1559                 /* Calculate perm */
1560                 perm = 0;
1561
1562                 perm |=
1563                         ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1564                 perm |=
1565                         ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1566                 perm |=
1567                         ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1568
1569
1570                 /* TAG is the first 2 bytes of an entry */
1571                 SSVAL(buf, offset, tag);
1572                 offset += 2;
1573
1574                 /* PERM is the next 2 bytes of an entry */
1575                 SSVAL(buf, offset, perm);
1576                 offset += 2;
1577
1578                 /* ID is the last 4 bytes of an entry */
1579                 SIVAL(buf, offset, id);
1580                 offset += 4;
1581
1582                 smb_ace++;
1583         }
1584
1585         /* Skip the header, sort @count number of 8-byte entries */
1586         qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1587               gluster_ace_cmp);
1588
1589         return size;
1590 }
1591
1592
1593 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1594                                               const char *path_p,
1595                                               SMB_ACL_TYPE_T type,
1596                                               TALLOC_CTX *mem_ctx)
1597 {
1598         struct smb_acl_t *result;
1599         struct stat st;
1600         char *buf;
1601         const char *key;
1602         ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1603
1604         switch (type) {
1605         case SMB_ACL_TYPE_ACCESS:
1606                 key = "system.posix_acl_access";
1607                 break;
1608         case SMB_ACL_TYPE_DEFAULT:
1609                 key = "system.posix_acl_default";
1610                 break;
1611         default:
1612                 errno = EINVAL;
1613                 return NULL;
1614         }
1615
1616         buf = alloca(size);
1617         if (!buf) {
1618                 return NULL;
1619         }
1620
1621         ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1622         if (ret == -1 && errno == ERANGE) {
1623                 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1624                 if (ret > 0) {
1625                         buf = alloca(ret);
1626                         if (!buf) {
1627                                 return NULL;
1628                         }
1629                         ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1630                 }
1631         }
1632
1633         /* retrieving the ACL from the xattr has finally failed, do a
1634          * mode-to-acl mapping */
1635
1636         if (ret == -1 && errno == ENODATA) {
1637                 ret = glfs_stat(handle->data, path_p, &st);
1638                 if (ret == 0) {
1639                         result = mode_to_smb_acls(&st, mem_ctx);
1640                         return result;
1641                 }
1642         }
1643
1644         if (ret <= 0) {
1645                 return NULL;
1646         }
1647
1648         result = gluster_to_smb_acl(buf, ret, mem_ctx);
1649
1650         return result;
1651 }
1652
1653 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1654                                             struct files_struct *fsp,
1655                                             TALLOC_CTX *mem_ctx)
1656 {
1657         struct smb_acl_t *result;
1658         struct stat st;
1659         ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1660         char *buf;
1661         glfs_fd_t *glfd;
1662
1663         glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1664
1665         buf = alloca(size);
1666         if (!buf) {
1667                 return NULL;
1668         }
1669
1670         ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1671         if (ret == -1 && errno == ERANGE) {
1672                 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1673                 if (ret > 0) {
1674                         buf = alloca(ret);
1675                         if (!buf) {
1676                                 return NULL;
1677                         }
1678                         ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1679                                              buf, ret);
1680                 }
1681         }
1682
1683         /* retrieving the ACL from the xattr has finally failed, do a
1684          * mode-to-acl mapping */
1685
1686         if (ret == -1 && errno == ENODATA) {
1687                 ret = glfs_fstat(glfd, &st);
1688                 if (ret == 0) {
1689                         result = mode_to_smb_acls(&st, mem_ctx);
1690                         return result;
1691                 }
1692         }
1693
1694         if (ret <= 0) {
1695                 return NULL;
1696         }
1697
1698         result = gluster_to_smb_acl(buf, ret, mem_ctx);
1699
1700         return result;
1701 }
1702
1703 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1704                                         const char *name,
1705                                         SMB_ACL_TYPE_T acltype,
1706                                         SMB_ACL_T theacl)
1707 {
1708         int ret;
1709         const char *key;
1710         char *buf;
1711         ssize_t size;
1712
1713         switch (acltype) {
1714         case SMB_ACL_TYPE_ACCESS:
1715                 key = "system.posix_acl_access";
1716                 break;
1717         case SMB_ACL_TYPE_DEFAULT:
1718                 key = "system.posix_acl_default";
1719                 break;
1720         default:
1721                 errno = EINVAL;
1722                 return -1;
1723         }
1724
1725         size = smb_to_gluster_acl(theacl, 0, 0);
1726         buf = alloca(size);
1727
1728         size = smb_to_gluster_acl(theacl, buf, size);
1729         if (size == -1) {
1730                 return -1;
1731         }
1732
1733         ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1734
1735         return ret;
1736 }
1737
1738 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1739                                       struct files_struct *fsp,
1740                                       SMB_ACL_T theacl)
1741 {
1742         int ret;
1743         char *buf;
1744         ssize_t size;
1745
1746         size = smb_to_gluster_acl(theacl, 0, 0);
1747         buf = alloca(size);
1748
1749         size = smb_to_gluster_acl(theacl, buf, size);
1750         if (size == -1) {
1751                 return -1;
1752         }
1753
1754         ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1755                              "system.posix_acl_access", buf, size, 0);
1756         return ret;
1757 }
1758
1759 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1760                                                const char *path)
1761 {
1762         return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1763 }
1764
1765 static struct vfs_fn_pointers glusterfs_fns = {
1766
1767         /* Disk Operations */
1768
1769         .connect_fn = vfs_gluster_connect,
1770         .disconnect_fn = vfs_gluster_disconnect,
1771         .disk_free_fn = vfs_gluster_disk_free,
1772         .get_quota_fn = vfs_gluster_get_quota,
1773         .set_quota_fn = vfs_gluster_set_quota,
1774         .statvfs_fn = vfs_gluster_statvfs,
1775         .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1776
1777         .get_dfs_referrals_fn = NULL,
1778
1779         /* Directory Operations */
1780
1781         .opendir_fn = vfs_gluster_opendir,
1782         .fdopendir_fn = vfs_gluster_fdopendir,
1783         .readdir_fn = vfs_gluster_readdir,
1784         .seekdir_fn = vfs_gluster_seekdir,
1785         .telldir_fn = vfs_gluster_telldir,
1786         .rewind_dir_fn = vfs_gluster_rewinddir,
1787         .mkdir_fn = vfs_gluster_mkdir,
1788         .rmdir_fn = vfs_gluster_rmdir,
1789         .closedir_fn = vfs_gluster_closedir,
1790         .init_search_op_fn = vfs_gluster_init_search_op,
1791
1792         /* File Operations */
1793
1794         .open_fn = vfs_gluster_open,
1795         .create_file_fn = NULL,
1796         .close_fn = vfs_gluster_close,
1797         .read_fn = vfs_gluster_read,
1798         .pread_fn = vfs_gluster_pread,
1799         .pread_send_fn = vfs_gluster_pread_send,
1800         .pread_recv_fn = vfs_gluster_recv,
1801         .write_fn = vfs_gluster_write,
1802         .pwrite_fn = vfs_gluster_pwrite,
1803         .pwrite_send_fn = vfs_gluster_pwrite_send,
1804         .pwrite_recv_fn = vfs_gluster_recv,
1805         .lseek_fn = vfs_gluster_lseek,
1806         .sendfile_fn = vfs_gluster_sendfile,
1807         .recvfile_fn = vfs_gluster_recvfile,
1808         .rename_fn = vfs_gluster_rename,
1809         .fsync_fn = vfs_gluster_fsync,
1810         .fsync_send_fn = vfs_gluster_fsync_send,
1811         .fsync_recv_fn = vfs_gluster_fsync_recv,
1812
1813         .stat_fn = vfs_gluster_stat,
1814         .fstat_fn = vfs_gluster_fstat,
1815         .lstat_fn = vfs_gluster_lstat,
1816         .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1817         .unlink_fn = vfs_gluster_unlink,
1818
1819         .chmod_fn = vfs_gluster_chmod,
1820         .fchmod_fn = vfs_gluster_fchmod,
1821         .chown_fn = vfs_gluster_chown,
1822         .fchown_fn = vfs_gluster_fchown,
1823         .lchown_fn = vfs_gluster_lchown,
1824         .chdir_fn = vfs_gluster_chdir,
1825         .getwd_fn = vfs_gluster_getwd,
1826         .ntimes_fn = vfs_gluster_ntimes,
1827         .ftruncate_fn = vfs_gluster_ftruncate,
1828         .fallocate_fn = vfs_gluster_fallocate,
1829         .lock_fn = vfs_gluster_lock,
1830         .kernel_flock_fn = vfs_gluster_kernel_flock,
1831         .linux_setlease_fn = vfs_gluster_linux_setlease,
1832         .getlock_fn = vfs_gluster_getlock,
1833         .symlink_fn = vfs_gluster_symlink,
1834         .readlink_fn = vfs_gluster_readlink,
1835         .link_fn = vfs_gluster_link,
1836         .mknod_fn = vfs_gluster_mknod,
1837         .realpath_fn = vfs_gluster_realpath,
1838         .chflags_fn = vfs_gluster_chflags,
1839         .file_id_create_fn = NULL,
1840         .copy_chunk_send_fn = NULL,
1841         .copy_chunk_recv_fn = NULL,
1842         .streaminfo_fn = NULL,
1843         .get_real_filename_fn = vfs_gluster_get_real_filename,
1844         .connectpath_fn = vfs_gluster_connectpath,
1845
1846         .brl_lock_windows_fn = NULL,
1847         .brl_unlock_windows_fn = NULL,
1848         .brl_cancel_windows_fn = NULL,
1849         .strict_lock_fn = NULL,
1850         .strict_unlock_fn = NULL,
1851         .translate_name_fn = NULL,
1852         .fsctl_fn = NULL,
1853
1854         /* NT ACL Operations */
1855         .fget_nt_acl_fn = NULL,
1856         .get_nt_acl_fn = NULL,
1857         .fset_nt_acl_fn = NULL,
1858         .audit_file_fn = NULL,
1859
1860         /* Posix ACL Operations */
1861         .chmod_acl_fn = NULL,   /* passthrough to default */
1862         .fchmod_acl_fn = NULL,  /* passthrough to default */
1863         .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1864         .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1865         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1866         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1867         .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1868         .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1869         .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1870
1871         /* EA Operations */
1872         .getxattr_fn = vfs_gluster_getxattr,
1873         .fgetxattr_fn = vfs_gluster_fgetxattr,
1874         .listxattr_fn = vfs_gluster_listxattr,
1875         .flistxattr_fn = vfs_gluster_flistxattr,
1876         .removexattr_fn = vfs_gluster_removexattr,
1877         .fremovexattr_fn = vfs_gluster_fremovexattr,
1878         .setxattr_fn = vfs_gluster_setxattr,
1879         .fsetxattr_fn = vfs_gluster_fsetxattr,
1880
1881         /* AIO Operations */
1882         .aio_force_fn = vfs_gluster_aio_force,
1883
1884         /* Offline Operations */
1885         .is_offline_fn = vfs_gluster_is_offline,
1886         .set_offline_fn = vfs_gluster_set_offline,
1887
1888         /* Durable handle Operations */
1889         .durable_cookie_fn = NULL,
1890         .durable_disconnect_fn = NULL,
1891         .durable_reconnect_fn = NULL,
1892 };
1893
1894 NTSTATUS vfs_glusterfs_init(void);
1895 NTSTATUS vfs_glusterfs_init(void)
1896 {
1897         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1898                                 "glusterfs", &glusterfs_fns);
1899 }