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