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