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