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