VFS: Modify chmod to take a const struct smb_filename * instead of const char *
[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
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         wrap->state->cancelled = true;
511
512         return 0;
513 }
514
515 /*
516  * This function is the callback that will be called on glusterfs
517  * threads once the async IO submitted is complete. To notify
518  * Samba of the completion we use a pipe based queue.
519  */
520 static void aio_glusterfs_done(glfs_fd_t *fd, ssize_t ret, void *data)
521 {
522         struct glusterfs_aio_state *state = NULL;
523         int sts = 0;
524         struct timespec end;
525
526         state = (struct glusterfs_aio_state *)data;
527
528         clock_gettime_mono(&end);
529
530         if (ret < 0) {
531                 state->ret = -1;
532                 state->vfs_aio_state.error = errno;
533         } else {
534                 state->ret = ret;
535         }
536         state->vfs_aio_state.duration = nsec_time_diff(&end, &state->start);
537
538         /*
539          * Write the state pointer to glusterfs_aio_state to the
540          * pipe, so we can call tevent_req_done() from the main thread,
541          * because tevent_req_done() is not designed to be executed in
542          * the multithread environment, so tevent_req_done() must be
543          * executed from the smbd main thread.
544          *
545          * write(2) on pipes with sizes under _POSIX_PIPE_BUF
546          * in size is atomic, without this, the use op pipes in this
547          * code would not work.
548          *
549          * sys_write is a thin enough wrapper around write(2)
550          * that we can trust it here.
551          */
552
553         sts = sys_write(write_fd, &state, sizeof(struct glusterfs_aio_state *));
554         if (sts < 0) {
555                 DEBUG(0,("\nWrite to pipe failed (%s)", strerror(errno)));
556         }
557
558         return;
559 }
560
561 /*
562  * Read each req off the pipe and process it.
563  */
564 static void aio_tevent_fd_done(struct tevent_context *event_ctx,
565                                 struct tevent_fd *fde,
566                                 uint16_t flags, void *data)
567 {
568         struct tevent_req *req = NULL;
569         struct glusterfs_aio_state *state = NULL;
570         int sts = 0;
571
572         /*
573          * read(2) on pipes is atomic if the needed data is available
574          * in the pipe, per SUS and POSIX.  Because we always write
575          * to the pipe in sizeof(struct tevent_req *) chunks, we can
576          * always read in those chunks, atomically.
577          *
578          * sys_read is a thin enough wrapper around read(2) that we
579          * can trust it here.
580          */
581
582         sts = sys_read(read_fd, &state, sizeof(struct glusterfs_aio_state *));
583
584         if (sts < 0) {
585                 DEBUG(0,("\nRead from pipe failed (%s)", strerror(errno)));
586         }
587
588         /* if we've cancelled the op, there is no req, so just clean up. */
589         if (state->cancelled == true) {
590                 TALLOC_FREE(state);
591                 return;
592         }
593
594         req = state->req;
595
596         if (req) {
597                 tevent_req_done(req);
598         }
599         return;
600 }
601
602 static bool init_gluster_aio(struct vfs_handle_struct *handle)
603 {
604         int fds[2];
605         int ret = -1;
606
607         if (read_fd != -1) {
608                 /*
609                  * Already initialized.
610                  */
611                 return true;
612         }
613
614         ret = pipe(fds);
615         if (ret == -1) {
616                 goto fail;
617         }
618
619         read_fd = fds[0];
620         write_fd = fds[1];
621
622         aio_read_event = tevent_add_fd(handle->conn->sconn->ev_ctx,
623                                         NULL,
624                                         read_fd,
625                                         TEVENT_FD_READ,
626                                         aio_tevent_fd_done,
627                                         NULL);
628         if (aio_read_event == NULL) {
629                 goto fail;
630         }
631
632         return true;
633 fail:
634         TALLOC_FREE(aio_read_event);
635         if (read_fd != -1) {
636                 close(read_fd);
637                 close(write_fd);
638                 read_fd = -1;
639                 write_fd = -1;
640         }
641         return false;
642 }
643
644 static struct glusterfs_aio_state *aio_state_create(TALLOC_CTX *mem_ctx)
645 {
646         struct tevent_req *req = NULL;
647         struct glusterfs_aio_state *state = NULL;
648         struct glusterfs_aio_wrapper *wrapper = NULL;
649
650         req = tevent_req_create(mem_ctx, &wrapper, struct glusterfs_aio_wrapper);
651
652         if (req == NULL) {
653                 return NULL;
654         }
655
656         state = talloc_zero(NULL, struct glusterfs_aio_state);
657
658         if (state == NULL) {
659                 TALLOC_FREE(req);
660                 return NULL;
661         }
662
663         talloc_set_destructor(wrapper, aio_wrapper_destructor);
664         state->cancelled = false;
665         state->req = req;
666
667         wrapper->state = state;
668
669         return state;
670 }
671
672 static struct tevent_req *vfs_gluster_pread_send(struct vfs_handle_struct
673                                                   *handle, TALLOC_CTX *mem_ctx,
674                                                   struct tevent_context *ev,
675                                                   files_struct *fsp,
676                                                   void *data, size_t n,
677                                                   off_t offset)
678 {
679         struct glusterfs_aio_state *state = NULL;
680         struct tevent_req *req = NULL;
681         int ret = 0;
682
683         state = aio_state_create(mem_ctx);
684
685         if (state == NULL) {
686                 return NULL;
687         }
688
689         req = state->req;
690
691         if (!init_gluster_aio(handle)) {
692                 tevent_req_error(req, EIO);
693                 return tevent_req_post(req, ev);
694         }
695
696         clock_gettime_mono(&state->start);
697         ret = glfs_pread_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
698                                 fsp), data, n, offset, 0, aio_glusterfs_done,
699                                 state);
700         if (ret < 0) {
701                 tevent_req_error(req, -ret);
702                 return tevent_req_post(req, ev);
703         }
704
705         return req;
706 }
707
708 static struct tevent_req *vfs_gluster_pwrite_send(struct vfs_handle_struct
709                                                   *handle, TALLOC_CTX *mem_ctx,
710                                                   struct tevent_context *ev,
711                                                   files_struct *fsp,
712                                                   const void *data, size_t n,
713                                                   off_t offset)
714 {
715         struct glusterfs_aio_state *state = NULL;
716         struct tevent_req *req = NULL;
717         int ret = 0;
718
719         state = aio_state_create(mem_ctx);
720
721         if (state == NULL) {
722                 return NULL;
723         }
724
725         req = state->req;
726
727         if (!init_gluster_aio(handle)) {
728                 tevent_req_error(req, EIO);
729                 return tevent_req_post(req, ev);
730         }
731
732         clock_gettime_mono(&state->start);
733         ret = glfs_pwrite_async(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle,
734                                 fsp), data, n, offset, 0, aio_glusterfs_done,
735                                 state);
736         if (ret < 0) {
737                 tevent_req_error(req, -ret);
738                 return tevent_req_post(req, ev);
739         }
740
741         return req;
742 }
743
744 static ssize_t vfs_gluster_recv(struct tevent_req *req,
745                                 struct vfs_aio_state *vfs_aio_state)
746 {
747         struct glusterfs_aio_state *state = NULL;
748         struct glusterfs_aio_wrapper *wrapper = NULL;
749         int ret = 0;
750
751         wrapper = tevent_req_data(req, struct glusterfs_aio_wrapper);
752
753         if (wrapper == NULL) {
754                 return -1;
755         }
756
757         state = wrapper->state;
758
759         if (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 = state->vfs_aio_state;
768         ret = state->ret;
769
770         /* Clean up the state, it is in a NULL context. */
771
772         TALLOC_FREE(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         clock_gettime_mono(&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 char *path, uid_t uid, gid_t gid)
947 {
948         return glfs_chown(handle->data, path, uid, gid);
949 }
950
951 static int vfs_gluster_fchown(struct vfs_handle_struct *handle,
952                               files_struct *fsp, uid_t uid, gid_t gid)
953 {
954         return glfs_fchown(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), uid, gid);
955 }
956
957 static int vfs_gluster_lchown(struct vfs_handle_struct *handle,
958                               const char *path, uid_t uid, gid_t gid)
959 {
960         return glfs_lchown(handle->data, path, uid, gid);
961 }
962
963 static int vfs_gluster_chdir(struct vfs_handle_struct *handle, const char *path)
964 {
965         return glfs_chdir(handle->data, path);
966 }
967
968 static char *vfs_gluster_getwd(struct vfs_handle_struct *handle)
969 {
970         char *cwd;
971         char *ret;
972
973         cwd = SMB_CALLOC_ARRAY(char, PATH_MAX);
974         if (cwd == NULL) {
975                 return NULL;
976         }
977
978         ret = glfs_getcwd(handle->data, cwd, PATH_MAX - 1);
979         if (ret == 0) {
980                 free(cwd);
981         }
982         return ret;
983 }
984
985 static int vfs_gluster_ntimes(struct vfs_handle_struct *handle,
986                               const struct smb_filename *smb_fname,
987                               struct smb_file_time *ft)
988 {
989         struct timespec times[2];
990
991         if (null_timespec(ft->atime)) {
992                 times[0].tv_sec = smb_fname->st.st_ex_atime.tv_sec;
993                 times[0].tv_nsec = smb_fname->st.st_ex_atime.tv_nsec;
994         } else {
995                 times[0].tv_sec = ft->atime.tv_sec;
996                 times[0].tv_nsec = ft->atime.tv_nsec;
997         }
998
999         if (null_timespec(ft->mtime)) {
1000                 times[1].tv_sec = smb_fname->st.st_ex_mtime.tv_sec;
1001                 times[1].tv_nsec = smb_fname->st.st_ex_mtime.tv_nsec;
1002         } else {
1003                 times[1].tv_sec = ft->mtime.tv_sec;
1004                 times[1].tv_nsec = ft->mtime.tv_nsec;
1005         }
1006
1007         if ((timespec_compare(&times[0],
1008                               &smb_fname->st.st_ex_atime) == 0) &&
1009             (timespec_compare(&times[1],
1010                               &smb_fname->st.st_ex_mtime) == 0)) {
1011                 return 0;
1012         }
1013
1014         return glfs_utimens(handle->data, smb_fname->base_name, times);
1015 }
1016
1017 static int vfs_gluster_ftruncate(struct vfs_handle_struct *handle,
1018                                  files_struct *fsp, off_t offset)
1019 {
1020         return glfs_ftruncate(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), offset);
1021 }
1022
1023 static int vfs_gluster_fallocate(struct vfs_handle_struct *handle,
1024                                  struct files_struct *fsp,
1025                                  uint32_t mode,
1026                                  off_t offset, off_t len)
1027 {
1028         /* TODO: add support using glfs_fallocate() and glfs_zerofill() */
1029         errno = ENOTSUP;
1030         return -1;
1031 }
1032
1033 static char *vfs_gluster_realpath(struct vfs_handle_struct *handle,
1034                                   const char *path)
1035 {
1036         return glfs_realpath(handle->data, path, 0);
1037 }
1038
1039 static bool vfs_gluster_lock(struct vfs_handle_struct *handle,
1040                              files_struct *fsp, int op, off_t offset,
1041                              off_t count, int type)
1042 {
1043         struct flock flock = { 0, };
1044         int ret;
1045
1046         flock.l_type = type;
1047         flock.l_whence = SEEK_SET;
1048         flock.l_start = offset;
1049         flock.l_len = count;
1050         flock.l_pid = 0;
1051
1052         ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), op, &flock);
1053
1054         if (op == F_GETLK) {
1055                 /* lock query, true if someone else has locked */
1056                 if ((ret != -1) &&
1057                     (flock.l_type != F_UNLCK) &&
1058                     (flock.l_pid != 0) && (flock.l_pid != getpid()))
1059                         return true;
1060                 /* not me */
1061                 return false;
1062         }
1063
1064         if (ret == -1) {
1065                 return false;
1066         }
1067
1068         return true;
1069 }
1070
1071 static int vfs_gluster_kernel_flock(struct vfs_handle_struct *handle,
1072                                     files_struct *fsp, uint32_t share_mode,
1073                                     uint32_t access_mask)
1074 {
1075         errno = ENOSYS;
1076         return -1;
1077 }
1078
1079 static int vfs_gluster_linux_setlease(struct vfs_handle_struct *handle,
1080                                       files_struct *fsp, int leasetype)
1081 {
1082         errno = ENOSYS;
1083         return -1;
1084 }
1085
1086 static bool vfs_gluster_getlock(struct vfs_handle_struct *handle,
1087                                 files_struct *fsp, off_t *poffset,
1088                                 off_t *pcount, int *ptype, pid_t *ppid)
1089 {
1090         struct flock flock = { 0, };
1091         int ret;
1092
1093         flock.l_type = *ptype;
1094         flock.l_whence = SEEK_SET;
1095         flock.l_start = *poffset;
1096         flock.l_len = *pcount;
1097         flock.l_pid = 0;
1098
1099         ret = glfs_posix_lock(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), F_GETLK, &flock);
1100
1101         if (ret == -1) {
1102                 return false;
1103         }
1104
1105         *ptype = flock.l_type;
1106         *poffset = flock.l_start;
1107         *pcount = flock.l_len;
1108         *ppid = flock.l_pid;
1109
1110         return true;
1111 }
1112
1113 static int vfs_gluster_symlink(struct vfs_handle_struct *handle,
1114                                const char *oldpath, const char *newpath)
1115 {
1116         return glfs_symlink(handle->data, oldpath, newpath);
1117 }
1118
1119 static int vfs_gluster_readlink(struct vfs_handle_struct *handle,
1120                                 const char *path, char *buf, size_t bufsiz)
1121 {
1122         return glfs_readlink(handle->data, path, buf, bufsiz);
1123 }
1124
1125 static int vfs_gluster_link(struct vfs_handle_struct *handle,
1126                             const char *oldpath, const char *newpath)
1127 {
1128         return glfs_link(handle->data, oldpath, newpath);
1129 }
1130
1131 static int vfs_gluster_mknod(struct vfs_handle_struct *handle, const char *path,
1132                              mode_t mode, SMB_DEV_T dev)
1133 {
1134         return glfs_mknod(handle->data, path, mode, dev);
1135 }
1136
1137 static int vfs_gluster_chflags(struct vfs_handle_struct *handle,
1138                                const char *path, unsigned int flags)
1139 {
1140         errno = ENOSYS;
1141         return -1;
1142 }
1143
1144 static int vfs_gluster_get_real_filename(struct vfs_handle_struct *handle,
1145                                          const char *path, const char *name,
1146                                          TALLOC_CTX *mem_ctx, char **found_name)
1147 {
1148         int ret;
1149         char key_buf[NAME_MAX + 64];
1150         char val_buf[NAME_MAX + 1];
1151
1152         if (strlen(name) >= NAME_MAX) {
1153                 errno = ENAMETOOLONG;
1154                 return -1;
1155         }
1156
1157         snprintf(key_buf, NAME_MAX + 64,
1158                  "glusterfs.get_real_filename:%s", name);
1159
1160         ret = glfs_getxattr(handle->data, path, key_buf, val_buf, NAME_MAX + 1);
1161         if (ret == -1) {
1162                 if (errno == ENODATA) {
1163                         errno = EOPNOTSUPP;
1164                 }
1165                 return -1;
1166         }
1167
1168         *found_name = talloc_strdup(mem_ctx, val_buf);
1169         if (found_name[0] == NULL) {
1170                 errno = ENOMEM;
1171                 return -1;
1172         }
1173         return 0;
1174 }
1175
1176 static const char *vfs_gluster_connectpath(struct vfs_handle_struct *handle,
1177                                            const char *filename)
1178 {
1179         return handle->conn->connectpath;
1180 }
1181
1182 /* EA Operations */
1183
1184 static ssize_t vfs_gluster_getxattr(struct vfs_handle_struct *handle,
1185                                     const char *path, const char *name,
1186                                     void *value, size_t size)
1187 {
1188         return glfs_getxattr(handle->data, path, name, value, size);
1189 }
1190
1191 static ssize_t vfs_gluster_fgetxattr(struct vfs_handle_struct *handle,
1192                                      files_struct *fsp, const char *name,
1193                                      void *value, size_t size)
1194 {
1195         return glfs_fgetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size);
1196 }
1197
1198 static ssize_t vfs_gluster_listxattr(struct vfs_handle_struct *handle,
1199                                      const char *path, char *list, size_t size)
1200 {
1201         return glfs_listxattr(handle->data, path, list, size);
1202 }
1203
1204 static ssize_t vfs_gluster_flistxattr(struct vfs_handle_struct *handle,
1205                                       files_struct *fsp, char *list,
1206                                       size_t size)
1207 {
1208         return glfs_flistxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), list, size);
1209 }
1210
1211 static int vfs_gluster_removexattr(struct vfs_handle_struct *handle,
1212                                    const char *path, const char *name)
1213 {
1214         return glfs_removexattr(handle->data, path, name);
1215 }
1216
1217 static int vfs_gluster_fremovexattr(struct vfs_handle_struct *handle,
1218                                     files_struct *fsp, const char *name)
1219 {
1220         return glfs_fremovexattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name);
1221 }
1222
1223 static int vfs_gluster_setxattr(struct vfs_handle_struct *handle,
1224                                 const char *path, const char *name,
1225                                 const void *value, size_t size, int flags)
1226 {
1227         return glfs_setxattr(handle->data, path, name, value, size, flags);
1228 }
1229
1230 static int vfs_gluster_fsetxattr(struct vfs_handle_struct *handle,
1231                                  files_struct *fsp, const char *name,
1232                                  const void *value, size_t size, int flags)
1233 {
1234         return glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp), name, value, size,
1235                               flags);
1236 }
1237
1238 /* AIO Operations */
1239
1240 static bool vfs_gluster_aio_force(struct vfs_handle_struct *handle,
1241                                   files_struct *fsp)
1242 {
1243         return false;
1244 }
1245
1246 /* Offline Operations */
1247
1248 static bool vfs_gluster_is_offline(struct vfs_handle_struct *handle,
1249                                    const struct smb_filename *fname,
1250                                    SMB_STRUCT_STAT *sbuf)
1251 {
1252         return false;
1253 }
1254
1255 static int vfs_gluster_set_offline(struct vfs_handle_struct *handle,
1256                                    const struct smb_filename *fname)
1257 {
1258         errno = ENOTSUP;
1259         return -1;
1260 }
1261
1262 /*
1263   Gluster ACL Format:
1264
1265   Size = 4 (header) + N * 8 (entry)
1266
1267   Offset  Size    Field (Little Endian)
1268   -------------------------------------
1269   0-3     4-byte  Version
1270
1271   4-5     2-byte  Entry-1 tag
1272   6-7     2-byte  Entry-1 perm
1273   8-11    4-byte  Entry-1 id
1274
1275   12-13   2-byte  Entry-2 tag
1276   14-15   2-byte  Entry-2 perm
1277   16-19   4-byte  Entry-2 id
1278
1279   ...
1280
1281  */
1282
1283 /* header version */
1284 #define GLUSTER_ACL_VERSION 2
1285
1286 /* perm bits */
1287 #define GLUSTER_ACL_READ    0x04
1288 #define GLUSTER_ACL_WRITE   0x02
1289 #define GLUSTER_ACL_EXECUTE 0x01
1290
1291 /* tag values */
1292 #define GLUSTER_ACL_UNDEFINED_TAG  0x00
1293 #define GLUSTER_ACL_USER_OBJ       0x01
1294 #define GLUSTER_ACL_USER           0x02
1295 #define GLUSTER_ACL_GROUP_OBJ      0x04
1296 #define GLUSTER_ACL_GROUP          0x08
1297 #define GLUSTER_ACL_MASK           0x10
1298 #define GLUSTER_ACL_OTHER          0x20
1299
1300 #define GLUSTER_ACL_UNDEFINED_ID  (-1)
1301
1302 #define GLUSTER_ACL_HEADER_SIZE    4
1303 #define GLUSTER_ACL_ENTRY_SIZE     8
1304
1305 #define GLUSTER_ACL_SIZE(n)       (GLUSTER_ACL_HEADER_SIZE + (n * GLUSTER_ACL_ENTRY_SIZE))
1306
1307 static SMB_ACL_T mode_to_smb_acls(const struct stat *mode, TALLOC_CTX *mem_ctx)
1308 {
1309         struct smb_acl_t *result;
1310         int count;
1311
1312         count = 3;
1313         result = sys_acl_init(mem_ctx);
1314         if (!result) {
1315                 errno = ENOMEM;
1316                 return NULL;
1317         }
1318
1319         result->acl = talloc_array(result, struct smb_acl_entry, count);
1320         if (!result->acl) {
1321                 errno = ENOMEM;
1322                 talloc_free(result);
1323                 return NULL;
1324         }
1325
1326         result->count = count;
1327
1328         result->acl[0].a_type = SMB_ACL_USER_OBJ;
1329         result->acl[0].a_perm = (mode->st_mode & S_IRWXU) >> 6;;
1330
1331         result->acl[1].a_type = SMB_ACL_GROUP_OBJ;
1332         result->acl[1].a_perm = (mode->st_mode & S_IRWXG) >> 3;;
1333
1334         result->acl[2].a_type = SMB_ACL_OTHER;
1335         result->acl[2].a_perm = mode->st_mode & S_IRWXO;;
1336
1337         return result;
1338 }
1339
1340 static SMB_ACL_T gluster_to_smb_acl(const char *buf, size_t xattr_size,
1341                                     TALLOC_CTX *mem_ctx)
1342 {
1343         int count;
1344         size_t size;
1345         struct smb_acl_entry *smb_ace;
1346         struct smb_acl_t *result;
1347         int i;
1348         int offset;
1349         uint16_t tag;
1350         uint16_t perm;
1351         uint32_t id;
1352
1353         size = xattr_size;
1354
1355         if (size < GLUSTER_ACL_HEADER_SIZE) {
1356                 /* ACL should be at least as big as the header (4 bytes) */
1357                 errno = EINVAL;
1358                 return NULL;
1359         }
1360
1361         size -= GLUSTER_ACL_HEADER_SIZE; /* size of header = 4 bytes */
1362
1363         if (size % GLUSTER_ACL_ENTRY_SIZE) {
1364                 /* Size of entries must strictly be a multiple of
1365                    size of an ACE (8 bytes)
1366                 */
1367                 errno = EINVAL;
1368                 return NULL;
1369         }
1370
1371         count = size / GLUSTER_ACL_ENTRY_SIZE;
1372
1373         /* Version is the first 4 bytes of the ACL */
1374         if (IVAL(buf, 0) != GLUSTER_ACL_VERSION) {
1375                 DEBUG(0, ("Unknown gluster ACL version: %d\n",
1376                           IVAL(buf, 0)));
1377                 return NULL;
1378         }
1379         offset = GLUSTER_ACL_HEADER_SIZE;
1380
1381         result = sys_acl_init(mem_ctx);
1382         if (!result) {
1383                 errno = ENOMEM;
1384                 return NULL;
1385         }
1386
1387         result->acl = talloc_array(result, struct smb_acl_entry, count);
1388         if (!result->acl) {
1389                 errno = ENOMEM;
1390                 talloc_free(result);
1391                 return NULL;
1392         }
1393
1394         result->count = count;
1395
1396         smb_ace = result->acl;
1397
1398         for (i = 0; i < count; i++) {
1399                 /* TAG is the first 2 bytes of an entry */
1400                 tag = SVAL(buf, offset);
1401                 offset += 2;
1402
1403                 /* PERM is the next 2 bytes of an entry */
1404                 perm = SVAL(buf, offset);
1405                 offset += 2;
1406
1407                 /* ID is the last 4 bytes of an entry */
1408                 id = IVAL(buf, offset);
1409                 offset += 4;
1410
1411                 switch(tag) {
1412                 case GLUSTER_ACL_USER:
1413                         smb_ace->a_type = SMB_ACL_USER;
1414                         break;
1415                 case GLUSTER_ACL_USER_OBJ:
1416                         smb_ace->a_type = SMB_ACL_USER_OBJ;
1417                         break;
1418                 case GLUSTER_ACL_GROUP:
1419                         smb_ace->a_type = SMB_ACL_GROUP;
1420                         break;
1421                 case GLUSTER_ACL_GROUP_OBJ:
1422                         smb_ace->a_type = SMB_ACL_GROUP_OBJ;
1423                         break;
1424                 case GLUSTER_ACL_OTHER:
1425                         smb_ace->a_type = SMB_ACL_OTHER;
1426                         break;
1427                 case GLUSTER_ACL_MASK:
1428                         smb_ace->a_type = SMB_ACL_MASK;
1429                         break;
1430                 default:
1431                         DEBUG(0, ("unknown tag type %d\n", (unsigned int) tag));
1432                         return NULL;
1433                 }
1434
1435
1436                 switch(smb_ace->a_type) {
1437                 case SMB_ACL_USER:
1438                         smb_ace->info.user.uid = id;
1439                         break;
1440                 case SMB_ACL_GROUP:
1441                         smb_ace->info.group.gid = id;
1442                         break;
1443                 default:
1444                         break;
1445                 }
1446
1447                 smb_ace->a_perm = 0;
1448                 smb_ace->a_perm |=
1449                         ((perm & GLUSTER_ACL_READ) ? SMB_ACL_READ : 0);
1450                 smb_ace->a_perm |=
1451                         ((perm & GLUSTER_ACL_WRITE) ? SMB_ACL_WRITE : 0);
1452                 smb_ace->a_perm |=
1453                         ((perm & GLUSTER_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
1454
1455                 smb_ace++;
1456         }
1457
1458         return result;
1459 }
1460
1461
1462 static int gluster_ace_cmp(const void *left, const void *right)
1463 {
1464         int ret = 0;
1465         uint16_t tag_left, tag_right;
1466         uint32_t id_left, id_right;
1467
1468         /*
1469           Sorting precedence:
1470
1471            - Smaller TAG values must be earlier.
1472
1473            - Within same TAG, smaller identifiers must be earlier, E.g:
1474              UID 0 entry must be earlier than UID 200
1475              GID 17 entry must be earlier than GID 19
1476         */
1477
1478         /* TAG is the first element in the entry */
1479         tag_left = SVAL(left, 0);
1480         tag_right = SVAL(right, 0);
1481
1482         ret = (tag_left - tag_right);
1483         if (!ret) {
1484                 /* ID is the third element in the entry, after two short
1485                    integers (tag and perm), i.e at offset 4.
1486                 */
1487                 id_left = IVAL(left, 4);
1488                 id_right = IVAL(right, 4);
1489                 ret = id_left - id_right;
1490         }
1491
1492         return ret;
1493 }
1494
1495
1496 static ssize_t smb_to_gluster_acl(SMB_ACL_T theacl, char *buf, size_t len)
1497 {
1498         ssize_t size;
1499         struct smb_acl_entry *smb_ace;
1500         int i;
1501         int count;
1502         uint16_t tag;
1503         uint16_t perm;
1504         uint32_t id;
1505         int offset;
1506
1507         count = theacl->count;
1508
1509         size = GLUSTER_ACL_HEADER_SIZE + (count * GLUSTER_ACL_ENTRY_SIZE);
1510         if (!buf) {
1511                 return size;
1512         }
1513
1514         if (len < size) {
1515                 errno = ERANGE;
1516                 return -1;
1517         }
1518
1519         smb_ace = theacl->acl;
1520
1521         /* Version is the first 4 bytes of the ACL */
1522         SIVAL(buf, 0, GLUSTER_ACL_VERSION);
1523         offset = GLUSTER_ACL_HEADER_SIZE;
1524
1525         for (i = 0; i < count; i++) {
1526                 /* Calculate tag */
1527                 switch(smb_ace->a_type) {
1528                 case SMB_ACL_USER:
1529                         tag = GLUSTER_ACL_USER;
1530                         break;
1531                 case SMB_ACL_USER_OBJ:
1532                         tag = GLUSTER_ACL_USER_OBJ;
1533                         break;
1534                 case SMB_ACL_GROUP:
1535                         tag = GLUSTER_ACL_GROUP;
1536                         break;
1537                 case SMB_ACL_GROUP_OBJ:
1538                         tag = GLUSTER_ACL_GROUP_OBJ;
1539                         break;
1540                 case SMB_ACL_OTHER:
1541                         tag = GLUSTER_ACL_OTHER;
1542                         break;
1543                 case SMB_ACL_MASK:
1544                         tag = GLUSTER_ACL_MASK;
1545                         break;
1546                 default:
1547                         DEBUG(0, ("Unknown tag value %d\n",
1548                                   smb_ace->a_type));
1549                         errno = EINVAL;
1550                         return -1;
1551                 }
1552
1553
1554                 /* Calculate id */
1555                 switch(smb_ace->a_type) {
1556                 case SMB_ACL_USER:
1557                         id = smb_ace->info.user.uid;
1558                         break;
1559                 case SMB_ACL_GROUP:
1560                         id = smb_ace->info.group.gid;
1561                         break;
1562                 default:
1563                         id = GLUSTER_ACL_UNDEFINED_ID;
1564                         break;
1565                 }
1566
1567                 /* Calculate perm */
1568                 perm = 0;
1569
1570                 perm |=
1571                         ((smb_ace->a_perm & SMB_ACL_READ) ? GLUSTER_ACL_READ : 0);
1572                 perm |=
1573                         ((smb_ace->a_perm & SMB_ACL_WRITE) ? GLUSTER_ACL_WRITE : 0);
1574                 perm |=
1575                         ((smb_ace->a_perm & SMB_ACL_EXECUTE) ? GLUSTER_ACL_EXECUTE : 0);
1576
1577
1578                 /* TAG is the first 2 bytes of an entry */
1579                 SSVAL(buf, offset, tag);
1580                 offset += 2;
1581
1582                 /* PERM is the next 2 bytes of an entry */
1583                 SSVAL(buf, offset, perm);
1584                 offset += 2;
1585
1586                 /* ID is the last 4 bytes of an entry */
1587                 SIVAL(buf, offset, id);
1588                 offset += 4;
1589
1590                 smb_ace++;
1591         }
1592
1593         /* Skip the header, sort @count number of 8-byte entries */
1594         qsort(buf+GLUSTER_ACL_HEADER_SIZE, count, GLUSTER_ACL_ENTRY_SIZE,
1595               gluster_ace_cmp);
1596
1597         return size;
1598 }
1599
1600
1601 static SMB_ACL_T vfs_gluster_sys_acl_get_file(struct vfs_handle_struct *handle,
1602                                               const char *path_p,
1603                                               SMB_ACL_TYPE_T type,
1604                                               TALLOC_CTX *mem_ctx)
1605 {
1606         struct smb_acl_t *result;
1607         struct stat st;
1608         char *buf;
1609         const char *key;
1610         ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1611
1612         switch (type) {
1613         case SMB_ACL_TYPE_ACCESS:
1614                 key = "system.posix_acl_access";
1615                 break;
1616         case SMB_ACL_TYPE_DEFAULT:
1617                 key = "system.posix_acl_default";
1618                 break;
1619         default:
1620                 errno = EINVAL;
1621                 return NULL;
1622         }
1623
1624         buf = alloca(size);
1625         if (!buf) {
1626                 return NULL;
1627         }
1628
1629         ret = glfs_getxattr(handle->data, path_p, key, buf, size);
1630         if (ret == -1 && errno == ERANGE) {
1631                 ret = glfs_getxattr(handle->data, path_p, key, 0, 0);
1632                 if (ret > 0) {
1633                         buf = alloca(ret);
1634                         if (!buf) {
1635                                 return NULL;
1636                         }
1637                         ret = glfs_getxattr(handle->data, path_p, key, buf, ret);
1638                 }
1639         }
1640
1641         /* retrieving the ACL from the xattr has finally failed, do a
1642          * mode-to-acl mapping */
1643
1644         if (ret == -1 && errno == ENODATA) {
1645                 ret = glfs_stat(handle->data, path_p, &st);
1646                 if (ret == 0) {
1647                         result = mode_to_smb_acls(&st, mem_ctx);
1648                         return result;
1649                 }
1650         }
1651
1652         if (ret <= 0) {
1653                 return NULL;
1654         }
1655
1656         result = gluster_to_smb_acl(buf, ret, mem_ctx);
1657
1658         return result;
1659 }
1660
1661 static SMB_ACL_T vfs_gluster_sys_acl_get_fd(struct vfs_handle_struct *handle,
1662                                             struct files_struct *fsp,
1663                                             TALLOC_CTX *mem_ctx)
1664 {
1665         struct smb_acl_t *result;
1666         struct stat st;
1667         ssize_t ret, size = GLUSTER_ACL_SIZE(20);
1668         char *buf;
1669         glfs_fd_t *glfd;
1670
1671         glfd = *(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1672
1673         buf = alloca(size);
1674         if (!buf) {
1675                 return NULL;
1676         }
1677
1678         ret = glfs_fgetxattr(glfd, "system.posix_acl_access", buf, size);
1679         if (ret == -1 && errno == ERANGE) {
1680                 ret = glfs_fgetxattr(glfd, "system.posix_acl_access", 0, 0);
1681                 if (ret > 0) {
1682                         buf = alloca(ret);
1683                         if (!buf) {
1684                                 return NULL;
1685                         }
1686                         ret = glfs_fgetxattr(glfd, "system.posix_acl_access",
1687                                              buf, ret);
1688                 }
1689         }
1690
1691         /* retrieving the ACL from the xattr has finally failed, do a
1692          * mode-to-acl mapping */
1693
1694         if (ret == -1 && errno == ENODATA) {
1695                 ret = glfs_fstat(glfd, &st);
1696                 if (ret == 0) {
1697                         result = mode_to_smb_acls(&st, mem_ctx);
1698                         return result;
1699                 }
1700         }
1701
1702         if (ret <= 0) {
1703                 return NULL;
1704         }
1705
1706         result = gluster_to_smb_acl(buf, ret, mem_ctx);
1707
1708         return result;
1709 }
1710
1711 static int vfs_gluster_sys_acl_set_file(struct vfs_handle_struct *handle,
1712                                         const char *name,
1713                                         SMB_ACL_TYPE_T acltype,
1714                                         SMB_ACL_T theacl)
1715 {
1716         int ret;
1717         const char *key;
1718         char *buf;
1719         ssize_t size;
1720
1721         switch (acltype) {
1722         case SMB_ACL_TYPE_ACCESS:
1723                 key = "system.posix_acl_access";
1724                 break;
1725         case SMB_ACL_TYPE_DEFAULT:
1726                 key = "system.posix_acl_default";
1727                 break;
1728         default:
1729                 errno = EINVAL;
1730                 return -1;
1731         }
1732
1733         size = smb_to_gluster_acl(theacl, 0, 0);
1734         buf = alloca(size);
1735
1736         size = smb_to_gluster_acl(theacl, buf, size);
1737         if (size == -1) {
1738                 return -1;
1739         }
1740
1741         ret = glfs_setxattr(handle->data, name, key, buf, size, 0);
1742
1743         return ret;
1744 }
1745
1746 static int vfs_gluster_sys_acl_set_fd(struct vfs_handle_struct *handle,
1747                                       struct files_struct *fsp,
1748                                       SMB_ACL_T theacl)
1749 {
1750         int ret;
1751         char *buf;
1752         ssize_t size;
1753
1754         size = smb_to_gluster_acl(theacl, 0, 0);
1755         buf = alloca(size);
1756
1757         size = smb_to_gluster_acl(theacl, buf, size);
1758         if (size == -1) {
1759                 return -1;
1760         }
1761
1762         ret = glfs_fsetxattr(*(glfs_fd_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp),
1763                              "system.posix_acl_access", buf, size, 0);
1764         return ret;
1765 }
1766
1767 static int vfs_gluster_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1768                                                const char *path)
1769 {
1770         return glfs_removexattr(handle->data, path, "system.posix_acl_default");
1771 }
1772
1773 static struct vfs_fn_pointers glusterfs_fns = {
1774
1775         /* Disk Operations */
1776
1777         .connect_fn = vfs_gluster_connect,
1778         .disconnect_fn = vfs_gluster_disconnect,
1779         .disk_free_fn = vfs_gluster_disk_free,
1780         .get_quota_fn = vfs_gluster_get_quota,
1781         .set_quota_fn = vfs_gluster_set_quota,
1782         .statvfs_fn = vfs_gluster_statvfs,
1783         .fs_capabilities_fn = vfs_gluster_fs_capabilities,
1784
1785         .get_dfs_referrals_fn = NULL,
1786
1787         /* Directory Operations */
1788
1789         .opendir_fn = vfs_gluster_opendir,
1790         .fdopendir_fn = vfs_gluster_fdopendir,
1791         .readdir_fn = vfs_gluster_readdir,
1792         .seekdir_fn = vfs_gluster_seekdir,
1793         .telldir_fn = vfs_gluster_telldir,
1794         .rewind_dir_fn = vfs_gluster_rewinddir,
1795         .mkdir_fn = vfs_gluster_mkdir,
1796         .rmdir_fn = vfs_gluster_rmdir,
1797         .closedir_fn = vfs_gluster_closedir,
1798         .init_search_op_fn = vfs_gluster_init_search_op,
1799
1800         /* File Operations */
1801
1802         .open_fn = vfs_gluster_open,
1803         .create_file_fn = NULL,
1804         .close_fn = vfs_gluster_close,
1805         .read_fn = vfs_gluster_read,
1806         .pread_fn = vfs_gluster_pread,
1807         .pread_send_fn = vfs_gluster_pread_send,
1808         .pread_recv_fn = vfs_gluster_recv,
1809         .write_fn = vfs_gluster_write,
1810         .pwrite_fn = vfs_gluster_pwrite,
1811         .pwrite_send_fn = vfs_gluster_pwrite_send,
1812         .pwrite_recv_fn = vfs_gluster_recv,
1813         .lseek_fn = vfs_gluster_lseek,
1814         .sendfile_fn = vfs_gluster_sendfile,
1815         .recvfile_fn = vfs_gluster_recvfile,
1816         .rename_fn = vfs_gluster_rename,
1817         .fsync_fn = vfs_gluster_fsync,
1818         .fsync_send_fn = vfs_gluster_fsync_send,
1819         .fsync_recv_fn = vfs_gluster_fsync_recv,
1820
1821         .stat_fn = vfs_gluster_stat,
1822         .fstat_fn = vfs_gluster_fstat,
1823         .lstat_fn = vfs_gluster_lstat,
1824         .get_alloc_size_fn = vfs_gluster_get_alloc_size,
1825         .unlink_fn = vfs_gluster_unlink,
1826
1827         .chmod_fn = vfs_gluster_chmod,
1828         .fchmod_fn = vfs_gluster_fchmod,
1829         .chown_fn = vfs_gluster_chown,
1830         .fchown_fn = vfs_gluster_fchown,
1831         .lchown_fn = vfs_gluster_lchown,
1832         .chdir_fn = vfs_gluster_chdir,
1833         .getwd_fn = vfs_gluster_getwd,
1834         .ntimes_fn = vfs_gluster_ntimes,
1835         .ftruncate_fn = vfs_gluster_ftruncate,
1836         .fallocate_fn = vfs_gluster_fallocate,
1837         .lock_fn = vfs_gluster_lock,
1838         .kernel_flock_fn = vfs_gluster_kernel_flock,
1839         .linux_setlease_fn = vfs_gluster_linux_setlease,
1840         .getlock_fn = vfs_gluster_getlock,
1841         .symlink_fn = vfs_gluster_symlink,
1842         .readlink_fn = vfs_gluster_readlink,
1843         .link_fn = vfs_gluster_link,
1844         .mknod_fn = vfs_gluster_mknod,
1845         .realpath_fn = vfs_gluster_realpath,
1846         .chflags_fn = vfs_gluster_chflags,
1847         .file_id_create_fn = NULL,
1848         .copy_chunk_send_fn = NULL,
1849         .copy_chunk_recv_fn = NULL,
1850         .streaminfo_fn = NULL,
1851         .get_real_filename_fn = vfs_gluster_get_real_filename,
1852         .connectpath_fn = vfs_gluster_connectpath,
1853
1854         .brl_lock_windows_fn = NULL,
1855         .brl_unlock_windows_fn = NULL,
1856         .brl_cancel_windows_fn = NULL,
1857         .strict_lock_fn = NULL,
1858         .strict_unlock_fn = NULL,
1859         .translate_name_fn = NULL,
1860         .fsctl_fn = NULL,
1861
1862         /* NT ACL Operations */
1863         .fget_nt_acl_fn = NULL,
1864         .get_nt_acl_fn = NULL,
1865         .fset_nt_acl_fn = NULL,
1866         .audit_file_fn = NULL,
1867
1868         /* Posix ACL Operations */
1869         .chmod_acl_fn = NULL,   /* passthrough to default */
1870         .fchmod_acl_fn = NULL,  /* passthrough to default */
1871         .sys_acl_get_file_fn = vfs_gluster_sys_acl_get_file,
1872         .sys_acl_get_fd_fn = vfs_gluster_sys_acl_get_fd,
1873         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1874         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1875         .sys_acl_set_file_fn = vfs_gluster_sys_acl_set_file,
1876         .sys_acl_set_fd_fn = vfs_gluster_sys_acl_set_fd,
1877         .sys_acl_delete_def_file_fn = vfs_gluster_sys_acl_delete_def_file,
1878
1879         /* EA Operations */
1880         .getxattr_fn = vfs_gluster_getxattr,
1881         .fgetxattr_fn = vfs_gluster_fgetxattr,
1882         .listxattr_fn = vfs_gluster_listxattr,
1883         .flistxattr_fn = vfs_gluster_flistxattr,
1884         .removexattr_fn = vfs_gluster_removexattr,
1885         .fremovexattr_fn = vfs_gluster_fremovexattr,
1886         .setxattr_fn = vfs_gluster_setxattr,
1887         .fsetxattr_fn = vfs_gluster_fsetxattr,
1888
1889         /* AIO Operations */
1890         .aio_force_fn = vfs_gluster_aio_force,
1891
1892         /* Offline Operations */
1893         .is_offline_fn = vfs_gluster_is_offline,
1894         .set_offline_fn = vfs_gluster_set_offline,
1895
1896         /* Durable handle Operations */
1897         .durable_cookie_fn = NULL,
1898         .durable_disconnect_fn = NULL,
1899         .durable_reconnect_fn = NULL,
1900 };
1901
1902 NTSTATUS vfs_glusterfs_init(void);
1903 NTSTATUS vfs_glusterfs_init(void)
1904 {
1905         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1906                                 "glusterfs", &glusterfs_fns);
1907 }