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