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