vfs_gpfs: Use update_stat_ex_create_time
[samba.git] / source3 / modules / vfs_gpfs.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Samba VFS module for GPFS filesystem
4  *  Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
5  *  Copyright (C) Christof Schmitt 2015
6  *  Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
7  *                           and Gomati Mohanan <gomati.mohanan@in.ibm.com>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24 #include "smbd/smbd.h"
25 #include "include/smbprofile.h"
26 #include "modules/non_posix_acls.h"
27 #include "libcli/security/security.h"
28 #include "nfs4_acls.h"
29 #include "system/filesys.h"
30 #include "auth.h"
31 #include "lib/util/tevent_unix.h"
32 #include "lib/util/gpfswrap.h"
33
34 #include <gnutls/gnutls.h>
35 #include <gnutls/crypto.h>
36 #include "lib/crypto/gnutls_helpers.h"
37
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_VFS
40
41 #ifndef GPFS_GETACL_NATIVE
42 #define GPFS_GETACL_NATIVE 0x00000004
43 #endif
44
45 struct gpfs_config_data {
46         struct smbacl4_vfs_params nfs4_params;
47         bool sharemodes;
48         bool leases;
49         bool hsm;
50         bool syncio;
51         bool winattr;
52         bool ftruncate;
53         bool getrealfilename;
54         bool dfreequota;
55         bool acl;
56         bool settimes;
57         bool recalls;
58 };
59
60 struct gpfs_fsp_extension {
61         bool offline;
62 };
63
64 static inline unsigned int gpfs_acl_flags(gpfs_acl_t *gacl)
65 {
66         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
67                 return gacl->v4Level1.acl_flags;
68         }
69         return 0;
70 }
71
72 static inline gpfs_ace_v4_t *gpfs_ace_ptr(gpfs_acl_t *gacl, unsigned int i)
73 {
74         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
75                 return &gacl->v4Level1.ace_v4[i];
76         }
77         return &gacl->ace_v4[i];
78 }
79
80 static bool set_gpfs_sharemode(files_struct *fsp, uint32_t access_mask,
81                                uint32_t share_access)
82 {
83         unsigned int allow = GPFS_SHARE_NONE;
84         unsigned int deny = GPFS_DENY_NONE;
85         int result;
86
87         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
88                 /* No real file, don't disturb */
89                 return True;
90         }
91
92         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) ?
93                 GPFS_SHARE_WRITE : 0;
94         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
95                 GPFS_SHARE_READ : 0;
96
97         if (allow == GPFS_SHARE_NONE) {
98                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
99         }
100         else {
101                 deny |= (share_access & FILE_SHARE_WRITE) ?
102                         0 : GPFS_DENY_WRITE;
103                 deny |= (share_access & (FILE_SHARE_READ)) ?
104                         0 : GPFS_DENY_READ;
105
106                 /*
107                  * GPFS_DENY_DELETE can only be set together with either
108                  * GPFS_DENY_WRITE or GPFS_DENY_READ.
109                  */
110                 if (deny & (GPFS_DENY_WRITE|GPFS_DENY_READ)) {
111                         deny |= (share_access & (FILE_SHARE_DELETE)) ?
112                                 0 : GPFS_DENY_DELETE;
113                 }
114         }
115         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
116                    access_mask, allow, share_access, deny));
117
118         result = gpfswrap_set_share(fsp->fh->fd, allow, deny);
119         if (result != 0) {
120                 if (errno == ENOSYS) {
121                         DEBUG(5, ("VFS module vfs_gpfs loaded, but gpfs "
122                                   "set_share function support not available. "
123                                   "Allowing access\n"));
124                         return True;
125                 } else {
126                         DEBUG(10, ("gpfs_set_share failed: %s\n",
127                                    strerror(errno)));
128                 }
129         }
130
131         return (result == 0);
132 }
133
134 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
135                                  uint32_t share_mode, uint32_t access_mask)
136 {
137
138         struct gpfs_config_data *config;
139         int ret = 0;
140
141         START_PROFILE(syscall_kernel_flock);
142
143         SMB_VFS_HANDLE_GET_DATA(handle, config,
144                                 struct gpfs_config_data,
145                                 return -1);
146
147         if(!config->sharemodes) {
148                 return 0;
149         }
150
151         /*
152          * A named stream fsp will have the basefile open in the fsp
153          * fd, so lacking a distinct fd for the stream we have to skip
154          * kernel_flock and set_gpfs_sharemode for stream.
155          */
156         if (is_named_stream(fsp->fsp_name)) {
157                 DEBUG(2,("%s: kernel_flock on stream\n", fsp_str_dbg(fsp)));
158                 return 0;
159         }
160
161         kernel_flock(fsp->fh->fd, share_mode, access_mask);
162
163         if (!set_gpfs_sharemode(fsp, access_mask, share_mode)) {
164                 ret = -1;
165         }
166
167         END_PROFILE(syscall_kernel_flock);
168
169         return ret;
170 }
171
172 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
173 {
174
175         struct gpfs_config_data *config;
176
177         SMB_VFS_HANDLE_GET_DATA(handle, config,
178                                 struct gpfs_config_data,
179                                 return -1);
180
181         if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
182                 set_gpfs_sharemode(fsp, 0, 0);
183         }
184
185         return SMB_VFS_NEXT_CLOSE(handle, fsp);
186 }
187
188 static int set_gpfs_lease(int fd, int leasetype)
189 {
190         int gpfs_type = GPFS_LEASE_NONE;
191
192         if (leasetype == F_RDLCK) {
193                 gpfs_type = GPFS_LEASE_READ;
194         }
195         if (leasetype == F_WRLCK) {
196                 gpfs_type = GPFS_LEASE_WRITE;
197         }
198
199         /* we unconditionally set CAP_LEASE, rather than looking for
200            -1/EACCES as there is a bug in some versions of
201            libgpfs_gpl.so which results in a leaked fd on /dev/ss0
202            each time we try this with the wrong capabilities set
203         */
204         linux_set_lease_capability();
205         return gpfswrap_set_lease(fd, gpfs_type);
206 }
207
208 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp, 
209                              int leasetype)
210 {
211         struct gpfs_config_data *config;
212         int ret=0;
213
214         START_PROFILE(syscall_linux_setlease);
215
216         SMB_VFS_HANDLE_GET_DATA(handle, config,
217                                 struct gpfs_config_data,
218                                 return -1);
219
220         if (linux_set_lease_sighandler(fsp->fh->fd) == -1) {
221                 ret = -1;
222                 goto failure;
223         }
224
225         if (config->leases) {
226                 /*
227                  * Ensure the lease owner is root to allow
228                  * correct delivery of lease-break signals.
229                  */
230                 become_root();
231                 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
232                 unbecome_root();
233         }
234
235 failure:
236         END_PROFILE(syscall_linux_setlease);
237
238         return ret;
239 }
240
241 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
242                                       const char *path,
243                                       const char *name,
244                                       TALLOC_CTX *mem_ctx,
245                                       char **found_name)
246 {
247         int result;
248         char *full_path = NULL;
249         char *to_free = NULL;
250         char real_pathname[PATH_MAX+1], tmpbuf[PATH_MAX];
251         size_t full_path_len;
252         int buflen;
253         bool mangled;
254         struct gpfs_config_data *config;
255
256         SMB_VFS_HANDLE_GET_DATA(handle, config,
257                                 struct gpfs_config_data,
258                                 return -1);
259
260         if (!config->getrealfilename) {
261                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
262                                                       mem_ctx, found_name);
263         }
264
265         mangled = mangle_is_mangled(name, handle->conn->params);
266         if (mangled) {
267                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
268                                                       mem_ctx, found_name);
269         }
270
271         full_path_len = full_path_tos(path, name, tmpbuf, sizeof(tmpbuf),
272                                       &full_path, &to_free);
273         if (full_path_len == -1) {
274                 errno = ENOMEM;
275                 return -1;
276         }
277
278         buflen = sizeof(real_pathname) - 1;
279
280         result = gpfswrap_get_realfilename_path(full_path, real_pathname,
281                                                 &buflen);
282
283         TALLOC_FREE(to_free);
284
285         if ((result == -1) && (errno == ENOSYS)) {
286                 return SMB_VFS_NEXT_GET_REAL_FILENAME(
287                         handle, path, name, mem_ctx, found_name);
288         }
289
290         if (result == -1) {
291                 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
292                            strerror(errno)));
293                 return -1;
294         }
295
296         /*
297          * GPFS does not necessarily null-terminate the returned path
298          * but instead returns the buffer length in buflen.
299          */
300
301         if (buflen < sizeof(real_pathname)) {
302                 real_pathname[buflen] = '\0';
303         } else {
304                 real_pathname[sizeof(real_pathname)-1] = '\0';
305         }
306
307         DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
308                    path, name, real_pathname));
309
310         name = strrchr_m(real_pathname, '/');
311         if (name == NULL) {
312                 errno = ENOENT;
313                 return -1;
314         }
315
316         *found_name = talloc_strdup(mem_ctx, name+1);
317         if (*found_name == NULL) {
318                 errno = ENOMEM;
319                 return -1;
320         }
321
322         return 0;
323 }
324
325 static void sd2gpfs_control(uint16_t control, struct gpfs_acl *gacl)
326 {
327         unsigned int gpfs_aclflags = 0;
328         control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
329                 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
330                 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
331                 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
332         gpfs_aclflags = control << 8;
333         if (!(control & SEC_DESC_DACL_PRESENT))
334                 gpfs_aclflags |= ACL4_FLAG_NULL_DACL;
335         if (!(control & SEC_DESC_SACL_PRESENT))
336                 gpfs_aclflags |= ACL4_FLAG_NULL_SACL;
337         gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
338         gacl->v4Level1.acl_flags = gpfs_aclflags;
339 }
340
341 static uint16_t gpfs2sd_control(unsigned int gpfs_aclflags)
342 {
343         uint16_t control = gpfs_aclflags >> 8;
344         control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
345                 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
346                 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
347                 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
348         control |= SEC_DESC_SELF_RELATIVE;
349         return control;
350 }
351
352 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
353 {
354         gpfs_aclCount_t i;
355         if (gacl==NULL)
356         {
357                 DEBUG(0, ("gpfs acl is NULL\n"));
358                 return;
359         }
360
361         DEBUG(level, ("len: %d, level: %d, version: %d, nace: %d, "
362                       "control: %x\n",
363                       gacl->acl_len, gacl->acl_level, gacl->acl_version,
364                       gacl->acl_nace, gpfs_acl_flags(gacl)));
365
366         for(i=0; i<gacl->acl_nace; i++)
367         {
368                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
369                 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, "
370                               "iflags:0x%x, who:%u\n",
371                               i, gace->aceType, gace->aceFlags, gace->aceMask,
372                               gace->aceIFlags, gace->aceWho));
373         }
374 }
375
376 static int gpfs_getacl_with_capability(const char *fname, int flags, void *buf)
377 {
378         int ret, saved_errno;
379
380         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
381
382         ret = gpfswrap_getacl(discard_const_p(char, fname), flags, buf);
383         saved_errno = errno;
384
385         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
386
387         errno = saved_errno;
388         return ret;
389 }
390
391 /*
392  * get the ACL from GPFS, allocated on the specified mem_ctx
393  * internally retries when initial buffer was too small
394  *
395  * caller needs to cast result to either
396  * raw = yes: struct gpfs_opaque_acl
397  * raw = no: struct gpfs_acl
398  *
399  */
400 static void *vfs_gpfs_getacl(TALLOC_CTX *mem_ctx,
401                          const char *fname,
402                          const bool raw,
403                          const gpfs_aclType_t type)
404 {
405
406         void *aclbuf;
407         size_t size = 512;
408         int ret, flags;
409         unsigned int *len;
410         size_t struct_size;
411         bool use_capability = false;
412
413 again:
414
415         aclbuf = talloc_zero_size(mem_ctx, size);
416         if (aclbuf == NULL) {
417                 errno = ENOMEM;
418                 return NULL;
419         }
420
421         if (raw) {
422                 struct gpfs_opaque_acl *buf = (struct gpfs_opaque_acl *) aclbuf;
423                 buf->acl_type = type;
424                 flags = GPFS_GETACL_NATIVE;
425                 len = (unsigned int *) &(buf->acl_buffer_len);
426                 struct_size = sizeof(struct gpfs_opaque_acl);
427         } else {
428                 struct gpfs_acl *buf = (struct gpfs_acl *) aclbuf;
429                 buf->acl_type = type;
430                 buf->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
431                 flags = GPFS_GETACL_STRUCT;
432                 len = &(buf->acl_len);
433                 /* reserve space for control flags in gpfs 3.5 and beyond */
434                 struct_size = sizeof(struct gpfs_acl) + sizeof(unsigned int);
435         }
436
437         /* set the length of the buffer as input value */
438         *len = size;
439
440         if (use_capability) {
441                 ret = gpfs_getacl_with_capability(fname, flags, aclbuf);
442         } else {
443                 ret = gpfswrap_getacl(discard_const_p(char, fname),
444                                       flags, aclbuf);
445                 if ((ret != 0) && (errno == EACCES)) {
446                         DBG_DEBUG("Retry with DAC capability for %s\n", fname);
447                         use_capability = true;
448                         ret = gpfs_getacl_with_capability(fname, flags, aclbuf);
449                 }
450         }
451
452         if ((ret != 0) && (errno == ENOSPC)) {
453                 /*
454                  * get the size needed to accommodate the complete buffer
455                  *
456                  * the value returned only applies to the ACL blob in the
457                  * struct so make sure to also have headroom for the first
458                  * struct members by adding room for the complete struct
459                  * (might be a few bytes too much then)
460                  */
461                 size = *len + struct_size;
462                 talloc_free(aclbuf);
463                 DEBUG(10, ("Increasing ACL buffer size to %zu\n", size));
464                 goto again;
465         }
466
467         if (ret != 0) {
468                 DEBUG(5, ("smbd_gpfs_getacl failed with %s\n",
469                           strerror(errno)));
470                 talloc_free(aclbuf);
471                 return NULL;
472         }
473
474         return aclbuf;
475 }
476
477 /* Tries to get nfs4 acls and returns SMB ACL allocated.
478  * On failure returns 1 if it got non-NFSv4 ACL to prompt 
479  * retry with POSIX ACL checks.
480  * On failure returns -1 if there is system (GPFS) error, check errno.
481  * Returns 0 on success
482  */
483 static int gpfs_get_nfs4_acl(TALLOC_CTX *mem_ctx, const char *fname,
484                              struct SMB4ACL_T **ppacl)
485 {
486         gpfs_aclCount_t i;
487         struct gpfs_acl *gacl = NULL;
488         DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
489
490         /* Get the ACL */
491         gacl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(), fname,
492                                                   false, 0);
493         if (gacl == NULL) {
494                 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
495                            fname, strerror(errno)));
496                 if (errno == ENODATA) {
497                         /*
498                          * GPFS returns ENODATA for snapshot
499                          * directories. Retry with POSIX ACLs check.
500                          */
501                         return 1;
502                 }
503
504                 return -1;
505         }
506
507         if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
508                 DEBUG(10, ("Got non-nfsv4 acl\n"));
509                 /* Retry with POSIX ACLs check */
510                 talloc_free(gacl);
511                 return 1;
512         }
513
514         *ppacl = smb_create_smb4acl(mem_ctx);
515
516         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
517                 uint16_t control = gpfs2sd_control(gpfs_acl_flags(gacl));
518                 smbacl4_set_controlflags(*ppacl, control);
519         }
520
521         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d, control: %x\n",
522                    gacl->acl_len, gacl->acl_level, gacl->acl_version,
523                    gacl->acl_nace, gpfs_acl_flags(gacl)));
524
525         for (i=0; i<gacl->acl_nace; i++) {
526                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
527                 SMB_ACE4PROP_T smbace = { 0 };
528                 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
529                            "who: %d\n", gace->aceType, gace->aceIFlags,
530                            gace->aceFlags, gace->aceMask, gace->aceWho));
531
532                 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
533                         smbace.flags |= SMB_ACE4_ID_SPECIAL;
534                         switch (gace->aceWho) {
535                         case ACE4_SPECIAL_OWNER:
536                                 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
537                                 break;
538                         case ACE4_SPECIAL_GROUP:
539                                 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
540                                 break;
541                         case ACE4_SPECIAL_EVERYONE:
542                                 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
543                                 break;
544                         default:
545                                 DEBUG(8, ("invalid special gpfs id %d "
546                                           "ignored\n", gace->aceWho));
547                                 continue; /* don't add it */
548                         }
549                 } else {
550                         if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
551                                 smbace.who.gid = gace->aceWho;
552                         else
553                                 smbace.who.uid = gace->aceWho;
554                 }
555
556                 /* remove redundant deny entries */
557                 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
558                         struct gpfs_ace_v4 *prev = gpfs_ace_ptr(gacl, i - 1);
559                         if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
560                             prev->aceFlags == gace->aceFlags &&
561                             prev->aceIFlags == gace->aceIFlags &&
562                             (gace->aceMask & prev->aceMask) == 0 &&
563                             gace->aceWho == prev->aceWho) {
564                                 /* it's redundant - skip it */
565                                 continue;
566                         }
567                 }
568
569                 smbace.aceType = gace->aceType;
570                 smbace.aceFlags = gace->aceFlags;
571                 smbace.aceMask = gace->aceMask;
572                 smb_add_ace4(*ppacl, &smbace);
573         }
574
575         talloc_free(gacl);
576
577         return 0;
578 }
579
580 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
581         files_struct *fsp, uint32_t security_info,
582         TALLOC_CTX *mem_ctx,
583         struct security_descriptor **ppdesc)
584 {
585         struct SMB4ACL_T *pacl = NULL;
586         int     result;
587         struct gpfs_config_data *config;
588         TALLOC_CTX *frame = talloc_stackframe();
589         NTSTATUS status;
590
591         *ppdesc = NULL;
592
593         SMB_VFS_HANDLE_GET_DATA(handle, config,
594                                 struct gpfs_config_data,
595                                 return NT_STATUS_INTERNAL_ERROR);
596
597         if (!config->acl) {
598                 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
599                                                   mem_ctx, ppdesc);
600                 TALLOC_FREE(frame);
601                 return status;
602         }
603
604         result = gpfs_get_nfs4_acl(frame, fsp->fsp_name->base_name, &pacl);
605
606         if (result == 0) {
607                 status = smb_fget_nt_acl_nfs4(fsp, &config->nfs4_params,
608                                               security_info,
609                                               mem_ctx, ppdesc, pacl);
610                 TALLOC_FREE(frame);
611                 return status;
612         }
613
614         if (result > 0) {
615                 DEBUG(10, ("retrying with posix acl...\n"));
616                 status = posix_fget_nt_acl(fsp, security_info,
617                                            mem_ctx, ppdesc);
618                 TALLOC_FREE(frame);
619                 return status;
620         }
621
622         TALLOC_FREE(frame);
623
624         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
625         return map_nt_error_from_unix(errno);
626 }
627
628 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
629                                    const struct smb_filename *smb_fname,
630                                    uint32_t security_info,
631                                    TALLOC_CTX *mem_ctx,
632                                    struct security_descriptor **ppdesc)
633 {
634         struct SMB4ACL_T *pacl = NULL;
635         int     result;
636         struct gpfs_config_data *config;
637         TALLOC_CTX *frame = talloc_stackframe();
638         NTSTATUS status;
639
640         *ppdesc = NULL;
641
642         SMB_VFS_HANDLE_GET_DATA(handle, config,
643                                 struct gpfs_config_data,
644                                 return NT_STATUS_INTERNAL_ERROR);
645
646         if (!config->acl) {
647                 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname,
648                                                  security_info,
649                                                  mem_ctx, ppdesc);
650                 TALLOC_FREE(frame);
651                 return status;
652         }
653
654         result = gpfs_get_nfs4_acl(frame, smb_fname->base_name, &pacl);
655
656         if (result == 0) {
657                 status = smb_get_nt_acl_nfs4(handle->conn, smb_fname,
658                                              &config->nfs4_params,
659                                              security_info, mem_ctx, ppdesc,
660                                              pacl);
661                 TALLOC_FREE(frame);
662                 return status;
663         }
664
665         if (result > 0) {
666                 DEBUG(10, ("retrying with posix acl...\n"));
667                 status = posix_get_nt_acl(handle->conn, smb_fname,
668                                           security_info, mem_ctx, ppdesc);
669                 TALLOC_FREE(frame);
670                 return status;
671         }
672
673         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
674         TALLOC_FREE(frame);
675         return map_nt_error_from_unix(errno);
676 }
677
678 static bool vfs_gpfs_nfs4_ace_to_gpfs_ace(SMB_ACE4PROP_T *nfs4_ace,
679                                           struct gpfs_ace_v4 *gace,
680                                           uid_t owner_uid)
681 {
682         gace->aceType = nfs4_ace->aceType;
683         gace->aceFlags = nfs4_ace->aceFlags;
684         gace->aceMask = nfs4_ace->aceMask;
685
686         if (nfs4_ace->flags & SMB_ACE4_ID_SPECIAL) {
687                 switch(nfs4_ace->who.special_id) {
688                 case SMB_ACE4_WHO_EVERYONE:
689                         gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
690                         gace->aceWho = ACE4_SPECIAL_EVERYONE;
691                         break;
692                 case SMB_ACE4_WHO_OWNER:
693                         /*
694                          * With GPFS it is not possible to deny ACL or
695                          * attribute access to the owner. Setting an
696                          * ACL with such an entry is not possible.
697                          * Denying ACL or attribute access for the
698                          * owner through a named ACL entry can be
699                          * stored in an ACL, it is just not effective.
700                          *
701                          * Map this case to a named entry to allow at
702                          * least setting this ACL, which will be
703                          * enforced by the smbd permission check. Do
704                          * not do this for an inheriting OWNER entry,
705                          * as this represents a CREATOR OWNER ACE. The
706                          * remaining limitation is that CREATOR OWNER
707                          * cannot deny ACL or attribute access.
708                          */
709                         if (!nfs_ace_is_inherit(nfs4_ace) &&
710                             nfs4_ace->aceType ==
711                                         SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
712                             nfs4_ace->aceMask & (SMB_ACE4_READ_ATTRIBUTES|
713                                                  SMB_ACE4_WRITE_ATTRIBUTES|
714                                                  SMB_ACE4_READ_ACL|
715                                                  SMB_ACE4_WRITE_ACL)) {
716                                 gace->aceIFlags = 0;
717                                 gace->aceWho = owner_uid;
718                         } else {
719                                 gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
720                                 gace->aceWho = ACE4_SPECIAL_OWNER;
721                         }
722                         break;
723                 case SMB_ACE4_WHO_GROUP:
724                         gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
725                         gace->aceWho = ACE4_SPECIAL_GROUP;
726                         break;
727                 default:
728                         DBG_WARNING("Unsupported special_id %d\n",
729                                     nfs4_ace->who.special_id);
730                         return false;
731                 }
732
733                 return true;
734         }
735
736         gace->aceIFlags = 0;
737         gace->aceWho = (nfs4_ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) ?
738                 nfs4_ace->who.gid : nfs4_ace->who.uid;
739
740         return true;
741 }
742
743 static struct gpfs_acl *vfs_gpfs_smbacl2gpfsacl(TALLOC_CTX *mem_ctx,
744                                                 files_struct *fsp,
745                                                 struct SMB4ACL_T *smbacl,
746                                                 bool controlflags)
747 {
748         struct gpfs_acl *gacl;
749         gpfs_aclLen_t gacl_len;
750         struct SMB4ACE_T *smbace;
751
752         gacl_len = offsetof(gpfs_acl_t, ace_v4) + sizeof(unsigned int)
753                 + smb_get_naces(smbacl) * sizeof(gpfs_ace_v4_t);
754
755         gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
756         if (gacl == NULL) {
757                 DEBUG(0, ("talloc failed\n"));
758                 errno = ENOMEM;
759                 return NULL;
760         }
761
762         gacl->acl_level = GPFS_ACL_LEVEL_BASE;
763         gacl->acl_version = GPFS_ACL_VERSION_NFS4;
764         gacl->acl_type = GPFS_ACL_TYPE_NFS4;
765         gacl->acl_nace = 0; /* change later... */
766
767         if (controlflags) {
768                 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
769                 sd2gpfs_control(smbacl4_get_controlflags(smbacl), gacl);
770         }
771
772         for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
773                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, gacl->acl_nace);
774                 SMB_ACE4PROP_T  *aceprop = smb_get_ace4(smbace);
775                 bool add_ace;
776
777                 add_ace = vfs_gpfs_nfs4_ace_to_gpfs_ace(aceprop, gace,
778                                                         fsp->fsp_name->st.st_ex_uid);
779                 if (!add_ace) {
780                         continue;
781                 }
782
783                 gacl->acl_nace++;
784         }
785         gacl->acl_len = (char *)gpfs_ace_ptr(gacl, gacl->acl_nace)
786                 - (char *)gacl;
787         return gacl;
788 }
789
790 static bool gpfsacl_process_smbacl(vfs_handle_struct *handle,
791                                    files_struct *fsp,
792                                    struct SMB4ACL_T *smbacl)
793 {
794         int ret;
795         struct gpfs_acl *gacl;
796         TALLOC_CTX *mem_ctx = talloc_tos();
797
798         gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, true);
799         if (gacl == NULL) { /* out of memory */
800                 return False;
801         }
802         ret = gpfswrap_putacl(fsp->fsp_name->base_name,
803                               GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
804
805         if ((ret != 0) && (errno == EINVAL)) {
806                 DEBUG(10, ("Retry without nfs41 control flags\n"));
807                 talloc_free(gacl);
808                 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, false);
809                 if (gacl == NULL) { /* out of memory */
810                         return False;
811                 }
812                 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
813                                       GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA,
814                                       gacl);
815         }
816
817         if (ret != 0) {
818                 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
819                 gpfs_dumpacl(8, gacl);
820                 return False;
821         }
822
823         DEBUG(10, ("gpfs_putacl succeeded\n"));
824         return True;
825 }
826
827 static NTSTATUS gpfsacl_set_nt_acl_internal(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
828 {
829         struct gpfs_acl *acl;
830         NTSTATUS result = NT_STATUS_ACCESS_DENIED;
831
832         acl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(),
833                                                  fsp->fsp_name->base_name,
834                                                  false, 0);
835         if (acl == NULL) {
836                 return map_nt_error_from_unix(errno);
837         }
838
839         if (acl->acl_version == GPFS_ACL_VERSION_NFS4) {
840                 struct gpfs_config_data *config;
841
842                 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
843                                  "refuse_dacl_protected", false)
844                     && (psd->type&SEC_DESC_DACL_PROTECTED)) {
845                         DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
846                         talloc_free(acl);
847                         return NT_STATUS_NOT_SUPPORTED;
848                 }
849
850                 SMB_VFS_HANDLE_GET_DATA(handle, config,
851                                         struct gpfs_config_data,
852                                         return NT_STATUS_INTERNAL_ERROR);
853
854                 result = smb_set_nt_acl_nfs4(handle,
855                         fsp, &config->nfs4_params, security_info_sent, psd,
856                         gpfsacl_process_smbacl);
857         } else { /* assume POSIX ACL - by default... */
858                 result = set_nt_acl(fsp, security_info_sent, psd);
859         }
860
861         talloc_free(acl);
862         return result;
863 }
864
865 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
866 {
867         struct gpfs_config_data *config;
868
869         SMB_VFS_HANDLE_GET_DATA(handle, config,
870                                 struct gpfs_config_data,
871                                 return NT_STATUS_INTERNAL_ERROR);
872
873         if (!config->acl) {
874                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
875         }
876
877         return gpfsacl_set_nt_acl_internal(handle, fsp, security_info_sent, psd);
878 }
879
880 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl, TALLOC_CTX *mem_ctx)
881 {
882         SMB_ACL_T result;
883         gpfs_aclCount_t i;
884
885         result = sys_acl_init(mem_ctx);
886         if (result == NULL) {
887                 errno = ENOMEM;
888                 return NULL;
889         }
890
891         result->count = pacl->acl_nace;
892         result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry,
893                                      result->count);
894         if (result->acl == NULL) {
895                 TALLOC_FREE(result);
896                 errno = ENOMEM;
897                 return NULL;
898         }
899
900         for (i=0; i<pacl->acl_nace; i++) {
901                 struct smb_acl_entry *ace = &result->acl[i];
902                 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
903
904                 DEBUG(10, ("Converting type %d id %lu perm %x\n",
905                            (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
906                            (int)g_ace->ace_perm));
907
908                 switch (g_ace->ace_type) {
909                 case GPFS_ACL_USER:
910                         ace->a_type = SMB_ACL_USER;
911                         ace->info.user.uid = (uid_t)g_ace->ace_who;
912                         break;
913                 case GPFS_ACL_USER_OBJ:
914                         ace->a_type = SMB_ACL_USER_OBJ;
915                         break;
916                 case GPFS_ACL_GROUP:
917                         ace->a_type = SMB_ACL_GROUP;
918                         ace->info.group.gid = (gid_t)g_ace->ace_who;
919                         break;
920                 case GPFS_ACL_GROUP_OBJ:
921                         ace->a_type = SMB_ACL_GROUP_OBJ;
922                         break;
923                 case GPFS_ACL_OTHER:
924                         ace->a_type = SMB_ACL_OTHER;
925                         break;
926                 case GPFS_ACL_MASK:
927                         ace->a_type = SMB_ACL_MASK;
928                         break;
929                 default:
930                         DEBUG(10, ("Got invalid ace_type: %d\n",
931                                    g_ace->ace_type));
932                         TALLOC_FREE(result);
933                         errno = EINVAL;
934                         return NULL;
935                 }
936
937                 ace->a_perm = 0;
938                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
939                         SMB_ACL_READ : 0;
940                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
941                         SMB_ACL_WRITE : 0;
942                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
943                         SMB_ACL_EXECUTE : 0;
944
945                 DEBUGADD(10, ("Converted to %d perm %x\n",
946                               ace->a_type, ace->a_perm));
947         }
948
949         return result;
950 }
951
952 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type,
953                                        TALLOC_CTX *mem_ctx)
954 {
955         struct gpfs_acl *pacl;
956         SMB_ACL_T result = NULL;
957
958         pacl = vfs_gpfs_getacl(talloc_tos(), path, false, type);
959
960         if (pacl == NULL) {
961                 DEBUG(10, ("vfs_gpfs_getacl failed for %s with %s\n",
962                            path, strerror(errno)));
963                 if (errno == 0) {
964                         errno = EINVAL;
965                 }
966                 goto done;
967         }
968
969         if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
970                 DEBUG(10, ("Got acl version %d, expected %d\n",
971                            pacl->acl_version, GPFS_ACL_VERSION_POSIX));
972                 errno = EINVAL;
973                 goto done;
974         }
975
976         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
977                    pacl->acl_len, pacl->acl_level, pacl->acl_version,
978                    pacl->acl_nace));
979
980         result = gpfs2smb_acl(pacl, mem_ctx);
981         if (result != NULL) {
982                 errno = 0;
983         }
984
985  done:
986
987         if (pacl != NULL) {
988                 talloc_free(pacl);
989         }
990         if (errno != 0) {
991                 TALLOC_FREE(result);
992         }
993         return result;
994 }
995
996 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
997                                           const struct smb_filename *smb_fname,
998                                           SMB_ACL_TYPE_T type,
999                                           TALLOC_CTX *mem_ctx)
1000 {
1001         gpfs_aclType_t gpfs_type;
1002         struct gpfs_config_data *config;
1003
1004         SMB_VFS_HANDLE_GET_DATA(handle, config,
1005                                 struct gpfs_config_data,
1006                                 return NULL);
1007
1008         if (!config->acl) {
1009                 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, smb_fname,
1010                                                      type, mem_ctx);
1011         }
1012
1013         switch(type) {
1014         case SMB_ACL_TYPE_ACCESS:
1015                 gpfs_type = GPFS_ACL_TYPE_ACCESS;
1016                 break;
1017         case SMB_ACL_TYPE_DEFAULT:
1018                 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
1019                 break;
1020         default:
1021                 DEBUG(0, ("Got invalid type: %d\n", type));
1022                 smb_panic("exiting");
1023         }
1024
1025         return gpfsacl_get_posix_acl(smb_fname->base_name, gpfs_type, mem_ctx);
1026 }
1027
1028 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
1029                                         files_struct *fsp,
1030                                         TALLOC_CTX *mem_ctx)
1031 {
1032         struct gpfs_config_data *config;
1033
1034         SMB_VFS_HANDLE_GET_DATA(handle, config,
1035                                 struct gpfs_config_data,
1036                                 return NULL);
1037
1038         if (!config->acl) {
1039                 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1040         }
1041
1042         return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
1043                                      GPFS_ACL_TYPE_ACCESS, mem_ctx);
1044 }
1045
1046 static int gpfsacl_sys_acl_blob_get_file(vfs_handle_struct *handle,
1047                                       const struct smb_filename *smb_fname,
1048                                       TALLOC_CTX *mem_ctx,
1049                                       char **blob_description,
1050                                       DATA_BLOB *blob)
1051 {
1052         struct gpfs_config_data *config;
1053         struct gpfs_opaque_acl *acl = NULL;
1054         DATA_BLOB aclblob;
1055         int result;
1056         const char *path_p = smb_fname->base_name;
1057
1058         SMB_VFS_HANDLE_GET_DATA(handle, config,
1059                                 struct gpfs_config_data,
1060                                 return -1);
1061
1062         if (!config->acl) {
1063                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, smb_fname,
1064                                                           mem_ctx,
1065                                                           blob_description,
1066                                                           blob);
1067         }
1068
1069         errno = 0;
1070         acl = (struct gpfs_opaque_acl *)
1071                         vfs_gpfs_getacl(mem_ctx,
1072                                         path_p,
1073                                         true,
1074                                         GPFS_ACL_TYPE_NFS4);
1075
1076         if (errno) {
1077                 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1078                                         errno, strerror(errno)));
1079
1080                 /* EINVAL means POSIX ACL, bail out on other cases */
1081                 if (errno != EINVAL) {
1082                         return -1;
1083                 }
1084         }
1085
1086         if (acl != NULL) {
1087                 /*
1088                  * file has NFSv4 ACL
1089                  *
1090                  * we only need the actual ACL blob here
1091                  * acl_version will always be NFS4 because we asked
1092                  * for NFS4
1093                  * acl_type is only used for POSIX ACLs
1094                  */
1095                 aclblob.data = (uint8_t*) acl->acl_var_data;
1096                 aclblob.length = acl->acl_buffer_len;
1097
1098                 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1099                 if (!*blob_description) {
1100                         talloc_free(acl);
1101                         errno = ENOMEM;
1102                         return -1;
1103                 }
1104
1105                 result = non_posix_sys_acl_blob_get_file_helper(handle, smb_fname,
1106                                                                 aclblob,
1107                                                                 mem_ctx, blob);
1108
1109                 talloc_free(acl);
1110                 return result;
1111         }
1112
1113         /* fall back to POSIX ACL */
1114         return posix_sys_acl_blob_get_file(handle, smb_fname, mem_ctx,
1115                                            blob_description, blob);
1116 }
1117
1118 static int gpfsacl_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1119                                       files_struct *fsp,
1120                                       TALLOC_CTX *mem_ctx,
1121                                       char **blob_description,
1122                                       DATA_BLOB *blob)
1123 {
1124         struct gpfs_config_data *config;
1125         struct gpfs_opaque_acl *acl = NULL;
1126         DATA_BLOB aclblob;
1127         int result;
1128
1129         SMB_VFS_HANDLE_GET_DATA(handle, config,
1130                                 struct gpfs_config_data,
1131                                 return -1);
1132
1133         if (!config->acl) {
1134                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1135                                                         blob_description, blob);
1136         }
1137
1138         errno = 0;
1139         acl = (struct gpfs_opaque_acl *) vfs_gpfs_getacl(mem_ctx,
1140                                                 fsp->fsp_name->base_name,
1141                                                 true,
1142                                                 GPFS_ACL_TYPE_NFS4);
1143
1144         if (errno) {
1145                 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1146                                         errno, strerror(errno)));
1147
1148                 /* EINVAL means POSIX ACL, bail out on other cases */
1149                 if (errno != EINVAL) {
1150                         return -1;
1151                 }
1152         }
1153
1154         if (acl != NULL) {
1155                 /*
1156                  * file has NFSv4 ACL
1157                  *
1158                  * we only need the actual ACL blob here
1159                  * acl_version will always be NFS4 because we asked
1160                  * for NFS4
1161                  * acl_type is only used for POSIX ACLs
1162                  */
1163                 aclblob.data = (uint8_t*) acl->acl_var_data;
1164                 aclblob.length = acl->acl_buffer_len;
1165
1166                 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1167                 if (!*blob_description) {
1168                         talloc_free(acl);
1169                         errno = ENOMEM;
1170                         return -1;
1171                 }
1172
1173                 result = non_posix_sys_acl_blob_get_fd_helper(handle, fsp,
1174                                                               aclblob, mem_ctx,
1175                                                               blob);
1176
1177                 talloc_free(acl);
1178                 return result;
1179         }
1180
1181         /* fall back to POSIX ACL */
1182         return posix_sys_acl_blob_get_fd(handle, fsp, mem_ctx,
1183                                          blob_description, blob);
1184 }
1185
1186 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
1187                                      SMB_ACL_TYPE_T type)
1188 {
1189         gpfs_aclLen_t len;
1190         struct gpfs_acl *result;
1191         int i;
1192
1193         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
1194
1195         len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
1196                 sizeof(gpfs_ace_v1_t);
1197
1198         result = (struct gpfs_acl *)SMB_MALLOC(len);
1199         if (result == NULL) {
1200                 errno = ENOMEM;
1201                 return result;
1202         }
1203
1204         result->acl_len = len;
1205         result->acl_level = 0;
1206         result->acl_version = GPFS_ACL_VERSION_POSIX;
1207         result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
1208                 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
1209         result->acl_nace = pacl->count;
1210
1211         for (i=0; i<pacl->count; i++) {
1212                 const struct smb_acl_entry *ace = &pacl->acl[i];
1213                 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
1214
1215                 DEBUG(10, ("Converting type %d perm %x\n",
1216                            (int)ace->a_type, (int)ace->a_perm));
1217
1218                 g_ace->ace_perm = 0;
1219
1220                 switch(ace->a_type) {
1221                 case SMB_ACL_USER:
1222                         g_ace->ace_type = GPFS_ACL_USER;
1223                         g_ace->ace_who = (gpfs_uid_t)ace->info.user.uid;
1224                         break;
1225                 case SMB_ACL_USER_OBJ:
1226                         g_ace->ace_type = GPFS_ACL_USER_OBJ;
1227                         g_ace->ace_perm |= ACL_PERM_CONTROL;
1228                         g_ace->ace_who = 0;
1229                         break;
1230                 case SMB_ACL_GROUP:
1231                         g_ace->ace_type = GPFS_ACL_GROUP;
1232                         g_ace->ace_who = (gpfs_uid_t)ace->info.group.gid;
1233                         break;
1234                 case SMB_ACL_GROUP_OBJ:
1235                         g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
1236                         g_ace->ace_who = 0;
1237                         break;
1238                 case SMB_ACL_MASK:
1239                         g_ace->ace_type = GPFS_ACL_MASK;
1240                         g_ace->ace_perm = 0x8f;
1241                         g_ace->ace_who = 0;
1242                         break;
1243                 case SMB_ACL_OTHER:
1244                         g_ace->ace_type = GPFS_ACL_OTHER;
1245                         g_ace->ace_who = 0;
1246                         break;
1247                 default:
1248                         DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
1249                         errno = EINVAL;
1250                         SAFE_FREE(result);
1251                         return NULL;
1252                 }
1253
1254                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
1255                         ACL_PERM_READ : 0;
1256                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
1257                         ACL_PERM_WRITE : 0;
1258                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
1259                         ACL_PERM_EXECUTE : 0;
1260
1261                 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
1262                               g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
1263         }
1264
1265         return result;
1266 }
1267
1268 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
1269                                     const struct smb_filename *smb_fname,
1270                                     SMB_ACL_TYPE_T type,
1271                                     SMB_ACL_T theacl)
1272 {
1273         struct gpfs_acl *gpfs_acl;
1274         int result;
1275         struct gpfs_config_data *config;
1276
1277         SMB_VFS_HANDLE_GET_DATA(handle, config,
1278                                 struct gpfs_config_data,
1279                                 return -1);
1280
1281         if (!config->acl) {
1282                 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, smb_fname,
1283                                 type, theacl);
1284         }
1285
1286         gpfs_acl = smb2gpfs_acl(theacl, type);
1287         if (gpfs_acl == NULL) {
1288                 return -1;
1289         }
1290
1291         result = gpfswrap_putacl(discard_const_p(char, smb_fname->base_name),
1292                                  GPFS_PUTACL_STRUCT|GPFS_ACL_SAMBA, gpfs_acl);
1293
1294         SAFE_FREE(gpfs_acl);
1295         return result;
1296 }
1297
1298 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
1299                                   files_struct *fsp,
1300                                   SMB_ACL_T theacl)
1301 {
1302         struct gpfs_config_data *config;
1303
1304         SMB_VFS_HANDLE_GET_DATA(handle, config,
1305                                 struct gpfs_config_data,
1306                                 return -1);
1307
1308         if (!config->acl) {
1309                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1310         }
1311
1312         return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name,
1313                                         SMB_ACL_TYPE_ACCESS, theacl);
1314 }
1315
1316 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
1317                                 const struct smb_filename *smb_fname)
1318 {
1319         struct gpfs_config_data *config;
1320
1321         SMB_VFS_HANDLE_GET_DATA(handle, config,
1322                                 struct gpfs_config_data,
1323                                 return -1);
1324
1325         if (!config->acl) {
1326                 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, smb_fname);
1327         }
1328
1329         errno = ENOTSUP;
1330         return -1;
1331 }
1332
1333 /*
1334  * Assumed: mode bits are shiftable and standard
1335  * Output: the new aceMask field for an smb nfs4 ace
1336  */
1337 static uint32_t gpfsacl_mask_filter(uint32_t aceType, uint32_t aceMask, uint32_t rwx)
1338 {
1339         const uint32_t posix_nfs4map[3] = {
1340                 SMB_ACE4_EXECUTE, /* execute */
1341                 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
1342                 SMB_ACE4_READ_DATA /* read */
1343         };
1344         int     i;
1345         uint32_t        posix_mask = 0x01;
1346         uint32_t        posix_bit;
1347         uint32_t        nfs4_bits;
1348
1349         for(i=0; i<3; i++) {
1350                 nfs4_bits = posix_nfs4map[i];
1351                 posix_bit = rwx & posix_mask;
1352
1353                 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
1354                         if (posix_bit)
1355                                 aceMask |= nfs4_bits;
1356                         else
1357                                 aceMask &= ~nfs4_bits;
1358                 } else {
1359                         /* add deny bits when suitable */
1360                         if (!posix_bit)
1361                                 aceMask |= nfs4_bits;
1362                         else
1363                                 aceMask &= ~nfs4_bits;
1364                 } /* other ace types are unexpected */
1365
1366                 posix_mask <<= 1;
1367         }
1368
1369         return aceMask;
1370 }
1371
1372 static int gpfsacl_emu_chmod(vfs_handle_struct *handle,
1373                              const char *path, mode_t mode)
1374 {
1375         struct SMB4ACL_T *pacl = NULL;
1376         int     result;
1377         bool    haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
1378         int     i;
1379         files_struct fake_fsp = { 0 }; /* TODO: rationalize parametrization */
1380         struct SMB4ACE_T *smbace;
1381         TALLOC_CTX *frame = talloc_stackframe();
1382
1383         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
1384
1385         result = gpfs_get_nfs4_acl(frame, path, &pacl);
1386         if (result) {
1387                 TALLOC_FREE(frame);
1388                 return result;
1389         }
1390
1391         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
1392                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
1393         }
1394
1395         for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
1396                 SMB_ACE4PROP_T  *ace = smb_get_ace4(smbace);
1397                 uint32_t        specid = ace->who.special_id;
1398
1399                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
1400                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
1401                     specid <= SMB_ACE4_WHO_EVERYONE) {
1402
1403                         uint32_t newMask;
1404
1405                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
1406                                 haveAllowEntry[specid] = True;
1407
1408                         /* mode >> 6 for @owner, mode >> 3 for @group,
1409                          * mode >> 0 for @everyone */
1410                         newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
1411                                                       mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
1412                         if (ace->aceMask!=newMask) {
1413                                 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
1414                                            path, ace->aceMask, newMask, specid));
1415                         }
1416                         ace->aceMask = newMask;
1417                 }
1418         }
1419
1420         /* make sure we have at least ALLOW entries
1421          * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
1422          * - if necessary
1423          */
1424         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
1425                 SMB_ACE4PROP_T ace = { 0 };
1426
1427                 if (haveAllowEntry[i]==True)
1428                         continue;
1429
1430                 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
1431                 ace.flags |= SMB_ACE4_ID_SPECIAL;
1432                 ace.who.special_id = i;
1433
1434                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
1435                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
1436
1437                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
1438                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
1439
1440                 /* don't add unnecessary aces */
1441                 if (!ace.aceMask)
1442                         continue;
1443
1444                 /* we add it to the END - as windows expects allow aces */
1445                 smb_add_ace4(pacl, &ace);
1446                 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
1447                            path, mode, i, ace.aceMask));
1448         }
1449
1450         /* don't add complementary DENY ACEs here */
1451         fake_fsp.fsp_name = synthetic_smb_fname(
1452                 frame, path, NULL, NULL, 0);
1453         if (fake_fsp.fsp_name == NULL) {
1454                 errno = ENOMEM;
1455                 TALLOC_FREE(frame);
1456                 return -1;
1457         }
1458         /* put the acl */
1459         if (gpfsacl_process_smbacl(handle, &fake_fsp, pacl) == False) {
1460                 TALLOC_FREE(frame);
1461                 return -1;
1462         }
1463
1464         TALLOC_FREE(frame);
1465         return 0; /* ok for [f]chmod */
1466 }
1467
1468 static int vfs_gpfs_chmod(vfs_handle_struct *handle,
1469                         const struct smb_filename *smb_fname,
1470                         mode_t mode)
1471 {
1472         struct smb_filename *smb_fname_cpath;
1473         int rc;
1474
1475         smb_fname_cpath = cp_smb_filename(talloc_tos(), smb_fname);
1476         if (smb_fname_cpath == NULL) {
1477                 errno = ENOMEM;
1478                 return -1;
1479         }
1480
1481         if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
1482                 TALLOC_FREE(smb_fname_cpath);
1483                 return -1;
1484         }
1485
1486         /* avoid chmod() if possible, to preserve acls */
1487         if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
1488                 TALLOC_FREE(smb_fname_cpath);
1489                 return 0;
1490         }
1491
1492         rc = gpfsacl_emu_chmod(handle, smb_fname->base_name, mode);
1493         if (rc == 1)
1494                 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1495
1496         TALLOC_FREE(smb_fname_cpath);
1497         return rc;
1498 }
1499
1500 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1501 {
1502                  SMB_STRUCT_STAT st;
1503                  int rc;
1504
1505                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1506                          return -1;
1507                  }
1508
1509                  /* avoid chmod() if possible, to preserve acls */
1510                  if ((st.st_ex_mode & ~S_IFMT) == mode) {
1511                          return 0;
1512                  }
1513
1514                  rc = gpfsacl_emu_chmod(handle, fsp->fsp_name->base_name,
1515                                         mode);
1516                  if (rc == 1)
1517                          return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1518                  return rc;
1519 }
1520
1521 static uint32_t vfs_gpfs_winattrs_to_dosmode(unsigned int winattrs)
1522 {
1523         uint32_t dosmode = 0;
1524
1525         if (winattrs & GPFS_WINATTR_ARCHIVE){
1526                 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1527         }
1528         if (winattrs & GPFS_WINATTR_HIDDEN){
1529                 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1530         }
1531         if (winattrs & GPFS_WINATTR_SYSTEM){
1532                 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1533         }
1534         if (winattrs & GPFS_WINATTR_READONLY){
1535                 dosmode |= FILE_ATTRIBUTE_READONLY;
1536         }
1537         if (winattrs & GPFS_WINATTR_SPARSE_FILE) {
1538                 dosmode |= FILE_ATTRIBUTE_SPARSE;
1539         }
1540         if (winattrs & GPFS_WINATTR_OFFLINE) {
1541                 dosmode |= FILE_ATTRIBUTE_OFFLINE;
1542         }
1543
1544         return dosmode;
1545 }
1546
1547 static unsigned int vfs_gpfs_dosmode_to_winattrs(uint32_t dosmode)
1548 {
1549         unsigned int winattrs = 0;
1550
1551         if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1552                 winattrs |= GPFS_WINATTR_ARCHIVE;
1553         }
1554         if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1555                 winattrs |= GPFS_WINATTR_HIDDEN;
1556         }
1557         if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1558                 winattrs |= GPFS_WINATTR_SYSTEM;
1559         }
1560         if (dosmode & FILE_ATTRIBUTE_READONLY){
1561                 winattrs |= GPFS_WINATTR_READONLY;
1562         }
1563         if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1564                 winattrs |= GPFS_WINATTR_SPARSE_FILE;
1565         }
1566         if (dosmode & FILE_ATTRIBUTE_OFFLINE) {
1567                 winattrs |= GPFS_WINATTR_OFFLINE;
1568         }
1569
1570         return winattrs;
1571 }
1572
1573 static int get_dos_attr_with_capability(struct smb_filename *smb_fname,
1574                                         unsigned int *litemask,
1575                                         struct gpfs_iattr64 *iattr)
1576 {
1577         int saved_errno = 0;
1578         int ret;
1579
1580         /*
1581          * According to MS-FSA 2.1.5.1.2.1 "Algorithm to Check Access to an
1582          * Existing File" FILE_LIST_DIRECTORY on a directory implies
1583          * FILE_READ_ATTRIBUTES for directory entries. Being able to stat() a
1584          * file implies FILE_LIST_DIRECTORY for the directory containing the
1585          * file.
1586          */
1587
1588         if (!VALID_STAT(smb_fname->st)) {
1589                 /*
1590                  * Safety net: dos_mode() already checks this, but as we set
1591                  * DAC_OVERRIDE_CAPABILITY based on this, add an additional
1592                  * layer of defense.
1593                  */
1594                 DBG_ERR("Rejecting DAC override, invalid stat [%s]\n",
1595                         smb_fname_str_dbg(smb_fname));
1596                 errno = EACCES;
1597                 return -1;
1598         }
1599
1600         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1601
1602         ret = gpfswrap_stat_x(smb_fname->base_name, litemask,
1603                               iattr, sizeof(*iattr));
1604         if (ret == -1) {
1605                 saved_errno = errno;
1606         }
1607
1608         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1609
1610         if (saved_errno != 0) {
1611                 errno = saved_errno;
1612         }
1613         return ret;
1614 }
1615
1616 static NTSTATUS vfs_gpfs_get_file_id(struct gpfs_iattr64 *iattr,
1617                                      uint64_t *fileid)
1618 {
1619         uint8_t input[sizeof(gpfs_ino64_t) +
1620                       sizeof(gpfs_gen64_t) +
1621                       sizeof(gpfs_snapid64_t)];
1622         uint8_t digest[gnutls_hash_get_len(GNUTLS_DIG_SHA1)];
1623         int rc;
1624
1625         DBG_DEBUG("ia_inode 0x%llx, ia_gen 0x%llx, ia_modsnapid 0x%llx\n",
1626                   iattr->ia_inode, iattr->ia_gen, iattr->ia_modsnapid);
1627
1628         SBVAL(input,
1629               0, iattr->ia_inode);
1630         SBVAL(input,
1631               sizeof(gpfs_ino64_t), iattr->ia_gen);
1632         SBVAL(input,
1633               sizeof(gpfs_ino64_t) + sizeof(gpfs_gen64_t), iattr->ia_modsnapid);
1634
1635         GNUTLS_FIPS140_SET_LAX_MODE();
1636         rc = gnutls_hash_fast(GNUTLS_DIG_SHA1, input, sizeof(input), &digest);
1637         GNUTLS_FIPS140_SET_STRICT_MODE();
1638
1639         if (rc != 0) {
1640                 return gnutls_error_to_ntstatus(rc,
1641                                                 NT_STATUS_HASH_NOT_SUPPORTED);
1642         }
1643
1644         memcpy(fileid, &digest, sizeof(*fileid));
1645         DBG_DEBUG("file_id 0x%" PRIx64 "\n", *fileid);
1646
1647         return NT_STATUS_OK;
1648 }
1649
1650 static struct timespec gpfs_timestruc64_to_timespec(struct gpfs_timestruc64 g)
1651 {
1652         return (struct timespec) { .tv_sec = g.tv_sec, .tv_nsec = g.tv_nsec };
1653 }
1654
1655 static NTSTATUS vfs_gpfs_get_dos_attributes(struct vfs_handle_struct *handle,
1656                                             struct smb_filename *smb_fname,
1657                                             uint32_t *dosmode)
1658 {
1659         struct gpfs_config_data *config;
1660         struct gpfs_iattr64 iattr = { };
1661         unsigned int litemask = 0;
1662         struct timespec ts;
1663         uint64_t file_id;
1664         NTSTATUS status;
1665         int ret;
1666
1667         SMB_VFS_HANDLE_GET_DATA(handle, config,
1668                                 struct gpfs_config_data,
1669                                 return NT_STATUS_INTERNAL_ERROR);
1670
1671         if (!config->winattr) {
1672                 return SMB_VFS_NEXT_GET_DOS_ATTRIBUTES(handle,
1673                                                        smb_fname, dosmode);
1674         }
1675
1676         ret = gpfswrap_stat_x(smb_fname->base_name, &litemask,
1677                               &iattr, sizeof(iattr));
1678         if (ret == -1 && errno == ENOSYS) {
1679                 return SMB_VFS_NEXT_GET_DOS_ATTRIBUTES(handle, smb_fname,
1680                                                        dosmode);
1681         }
1682         if (ret == -1 && errno == EACCES) {
1683                 ret = get_dos_attr_with_capability(smb_fname, &litemask,
1684                                                    &iattr);
1685         }
1686
1687         if (ret == -1 && errno == EBADF) {
1688                 /*
1689                  * Returned for directory listings in gpfs root for
1690                  * .. entry which steps out of gpfs.
1691                  */
1692                 DBG_DEBUG("Getting winattrs for %s returned EBADF.\n",
1693                           smb_fname->base_name);
1694                 return map_nt_error_from_unix(errno);
1695         } else if (ret == -1) {
1696                 DBG_WARNING("Getting winattrs failed for %s: %s\n",
1697                             smb_fname->base_name, strerror(errno));
1698                 return map_nt_error_from_unix(errno);
1699         }
1700
1701         status = vfs_gpfs_get_file_id(&iattr, &file_id);
1702         if (!NT_STATUS_IS_OK(status)) {
1703                 return status;
1704         }
1705
1706         ts = gpfs_timestruc64_to_timespec(iattr.ia_createtime);
1707
1708         *dosmode |= vfs_gpfs_winattrs_to_dosmode(iattr.ia_winflags);
1709         update_stat_ex_create_time(&smb_fname->st, ts);
1710         update_stat_ex_file_id(&smb_fname->st, file_id);
1711
1712         return NT_STATUS_OK;
1713 }
1714
1715 static NTSTATUS vfs_gpfs_fget_dos_attributes(struct vfs_handle_struct *handle,
1716                                              struct files_struct *fsp,
1717                                              uint32_t *dosmode)
1718 {
1719         struct gpfs_config_data *config;
1720         struct gpfs_iattr64 iattr = { };
1721         unsigned int litemask;
1722         struct timespec ts;
1723         uint64_t file_id;
1724         NTSTATUS status;
1725         int ret;
1726
1727         SMB_VFS_HANDLE_GET_DATA(handle, config,
1728                                 struct gpfs_config_data,
1729                                 return NT_STATUS_INTERNAL_ERROR);
1730
1731         if (!config->winattr) {
1732                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1733         }
1734
1735         ret = gpfswrap_fstat_x(fsp->fh->fd, &litemask, &iattr, sizeof(iattr));
1736         if (ret == -1 && errno == ENOSYS) {
1737                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1738         }
1739
1740         if (ret == -1 && errno == EACCES) {
1741                 int saved_errno = 0;
1742
1743                 /*
1744                  * According to MS-FSA 2.1.5.1.2.1 "Algorithm to Check Access to
1745                  * an Existing File" FILE_LIST_DIRECTORY on a directory implies
1746                  * FILE_READ_ATTRIBUTES for directory entries. Being able to
1747                  * open a file implies FILE_LIST_DIRECTORY.
1748                  */
1749
1750                 set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1751
1752                 ret = gpfswrap_fstat_x(fsp->fh->fd, &litemask,
1753                                        &iattr, sizeof(iattr));
1754                 if (ret == -1) {
1755                         saved_errno = errno;
1756                 }
1757
1758                 drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1759
1760                 if (saved_errno != 0) {
1761                         errno = saved_errno;
1762                 }
1763         }
1764
1765         if (ret == -1) {
1766                 DBG_WARNING("Getting winattrs failed for %s: %s\n",
1767                             fsp->fsp_name->base_name, strerror(errno));
1768                 return map_nt_error_from_unix(errno);
1769         }
1770
1771         status = vfs_gpfs_get_file_id(&iattr, &file_id);
1772         if (!NT_STATUS_IS_OK(status)) {
1773                 return status;
1774         }
1775
1776         ts = gpfs_timestruc64_to_timespec(iattr.ia_createtime);
1777
1778         *dosmode |= vfs_gpfs_winattrs_to_dosmode(iattr.ia_winflags);
1779         update_stat_ex_create_time(&fsp->fsp_name->st, ts);
1780         update_stat_ex_file_id(&fsp->fsp_name->st, file_id);
1781
1782         return NT_STATUS_OK;
1783 }
1784
1785 static NTSTATUS vfs_gpfs_set_dos_attributes(struct vfs_handle_struct *handle,
1786                                            const struct smb_filename *smb_fname,
1787                                            uint32_t dosmode)
1788 {
1789         struct gpfs_config_data *config;
1790         struct gpfs_winattr attrs = { };
1791         int ret;
1792
1793         SMB_VFS_HANDLE_GET_DATA(handle, config,
1794                                 struct gpfs_config_data,
1795                                 return NT_STATUS_INTERNAL_ERROR);
1796
1797         if (!config->winattr) {
1798                 return SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
1799                                                        smb_fname, dosmode);
1800         }
1801
1802         attrs.winAttrs = vfs_gpfs_dosmode_to_winattrs(dosmode);
1803         ret = gpfswrap_set_winattrs_path(smb_fname->base_name,
1804                                          GPFS_WINATTR_SET_ATTRS, &attrs);
1805
1806         if (ret == -1 && errno == ENOSYS) {
1807                 return SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
1808                                                        smb_fname, dosmode);
1809         }
1810
1811         if (ret == -1) {
1812                 DBG_WARNING("Setting winattrs failed for %s: %s\n",
1813                             smb_fname->base_name, strerror(errno));
1814                 return map_nt_error_from_unix(errno);
1815         }
1816
1817         return NT_STATUS_OK;
1818 }
1819
1820 static NTSTATUS vfs_gpfs_fset_dos_attributes(struct vfs_handle_struct *handle,
1821                                              struct files_struct *fsp,
1822                                              uint32_t dosmode)
1823 {
1824         struct gpfs_config_data *config;
1825         struct gpfs_winattr attrs = { };
1826         int ret;
1827
1828         SMB_VFS_HANDLE_GET_DATA(handle, config,
1829                                 struct gpfs_config_data,
1830                                 return NT_STATUS_INTERNAL_ERROR);
1831
1832         if (!config->winattr) {
1833                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1834         }
1835
1836         attrs.winAttrs = vfs_gpfs_dosmode_to_winattrs(dosmode);
1837         ret = gpfswrap_set_winattrs(fsp->fh->fd,
1838                                     GPFS_WINATTR_SET_ATTRS, &attrs);
1839
1840         if (ret == -1 && errno == ENOSYS) {
1841                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1842         }
1843
1844         if (ret == -1) {
1845                 DBG_WARNING("Setting winattrs failed for %s: %s\n",
1846                             fsp->fsp_name->base_name, strerror(errno));
1847                 return map_nt_error_from_unix(errno);
1848         }
1849
1850         return NT_STATUS_OK;
1851 }
1852
1853 static int stat_with_capability(struct vfs_handle_struct *handle,
1854                                 struct smb_filename *smb_fname, int flag)
1855 {
1856 #if defined(HAVE_FSTATAT)
1857         int fd = -1;
1858         bool b;
1859         char *dir_name;
1860         const char *rel_name = NULL;
1861         struct stat st;
1862         int ret = -1;
1863
1864         b = parent_dirname(talloc_tos(), smb_fname->base_name,
1865                            &dir_name, &rel_name);
1866         if (!b) {
1867                 errno = ENOMEM;
1868                 return -1;
1869         }
1870
1871         fd = open(dir_name, O_RDONLY, 0);
1872         TALLOC_FREE(dir_name);
1873         if (fd == -1) {
1874                 return -1;
1875         }
1876
1877         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1878         ret = fstatat(fd, rel_name, &st, flag);
1879         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1880
1881         close(fd);
1882
1883         if (ret == 0) {
1884                 init_stat_ex_from_stat(
1885                         &smb_fname->st, &st,
1886                         lp_fake_directory_create_times(SNUM(handle->conn)));
1887         }
1888
1889         return ret;
1890 #else
1891         return -1;
1892 #endif
1893 }
1894
1895 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1896                          struct smb_filename *smb_fname)
1897 {
1898         int ret;
1899         struct gpfs_config_data *config;
1900
1901         SMB_VFS_HANDLE_GET_DATA(handle, config,
1902                                 struct gpfs_config_data,
1903                                 return -1);
1904
1905         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1906         if (ret == -1 && errno == EACCES) {
1907                 DEBUG(10, ("Trying stat with capability for %s\n",
1908                            smb_fname->base_name));
1909                 ret = stat_with_capability(handle, smb_fname, 0);
1910         }
1911         return ret;
1912 }
1913
1914 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1915                           struct smb_filename *smb_fname)
1916 {
1917         int ret;
1918         struct gpfs_config_data *config;
1919
1920         SMB_VFS_HANDLE_GET_DATA(handle, config,
1921                                 struct gpfs_config_data,
1922                                 return -1);
1923
1924         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1925         if (ret == -1 && errno == EACCES) {
1926                 DEBUG(10, ("Trying lstat with capability for %s\n",
1927                            smb_fname->base_name));
1928                 ret = stat_with_capability(handle, smb_fname,
1929                                            AT_SYMLINK_NOFOLLOW);
1930         }
1931         return ret;
1932 }
1933
1934 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
1935                                   int idx, int *flags)
1936 {
1937         if (!null_timespec(ts)) {
1938                 *flags |= 1 << idx;
1939                 gt[idx].tv_sec = ts.tv_sec;
1940                 gt[idx].tv_nsec = ts.tv_nsec;
1941                 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
1942         }
1943 }
1944
1945 static int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
1946 {
1947         gpfs_timestruc_t gpfs_times[4];
1948         int flags = 0;
1949         int rc;
1950
1951         ZERO_ARRAY(gpfs_times);
1952         timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
1953         timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
1954         /* No good mapping from LastChangeTime to ctime, not storing */
1955         timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
1956
1957         if (!flags) {
1958                 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
1959                 return 0;
1960         }
1961
1962         rc = gpfswrap_set_times_path(path, flags, gpfs_times);
1963
1964         if (rc != 0 && errno != ENOSYS) {
1965                 DEBUG(1,("gpfs_set_times() returned with error %s\n",
1966                         strerror(errno)));
1967         }
1968
1969         return rc;
1970 }
1971
1972 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1973                         const struct smb_filename *smb_fname,
1974                         struct smb_file_time *ft)
1975 {
1976
1977         struct gpfs_winattr attrs;
1978         int ret;
1979         struct gpfs_config_data *config;
1980
1981         SMB_VFS_HANDLE_GET_DATA(handle, config,
1982                                 struct gpfs_config_data,
1983                                 return -1);
1984
1985         /* Try to use gpfs_set_times if it is enabled and available */
1986         if (config->settimes) {
1987                 ret = smbd_gpfs_set_times_path(smb_fname->base_name, ft);
1988
1989                 if (ret == 0 || (ret == -1 && errno != ENOSYS)) {
1990                         return ret;
1991                 }
1992         }
1993
1994         DEBUG(10,("gpfs_set_times() not available or disabled, "
1995                   "use ntimes and winattr\n"));
1996
1997         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1998         if(ret == -1){
1999                 /* don't complain if access was denied */
2000                 if (errno != EPERM && errno != EACCES) {
2001                         DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
2002                                  "%s", strerror(errno)));
2003                 }
2004                 return -1;
2005         }
2006
2007         if(null_timespec(ft->create_time)){
2008                 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
2009                 return 0;
2010         }
2011
2012         if (!config->winattr) {
2013                 return 0;
2014         }
2015
2016         attrs.winAttrs = 0;
2017         attrs.creationTime.tv_sec = ft->create_time.tv_sec;
2018         attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
2019
2020         ret = gpfswrap_set_winattrs_path(smb_fname->base_name,
2021                                          GPFS_WINATTR_SET_CREATION_TIME,
2022                                          &attrs);
2023         if(ret == -1 && errno != ENOSYS){
2024                 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
2025                 return -1;
2026         }
2027         return 0;
2028
2029 }
2030
2031 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
2032                               struct files_struct *fsp, uint32_t mode,
2033                               off_t offset, off_t len)
2034 {
2035         if (mode == (VFS_FALLOCATE_FL_PUNCH_HOLE|VFS_FALLOCATE_FL_KEEP_SIZE) &&
2036             !fsp->is_sparse &&
2037             lp_strict_allocate(SNUM(fsp->conn))) {
2038                 /*
2039                  * This is from a ZERO_DATA request on a non-sparse
2040                  * file. GPFS does not support FL_KEEP_SIZE and thus
2041                  * cannot fill the whole again in the subsequent
2042                  * fallocate(FL_KEEP_SIZE). Deny this FL_PUNCH_HOLE
2043                  * call to not end up with a hole in a non-sparse
2044                  * file.
2045                  */
2046                 errno = ENOTSUP;
2047                 return -1;
2048         }
2049
2050         return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
2051 }
2052
2053 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
2054                                 off_t len)
2055 {
2056         int result;
2057         struct gpfs_config_data *config;
2058
2059         SMB_VFS_HANDLE_GET_DATA(handle, config,
2060                                 struct gpfs_config_data,
2061                                 return -1);
2062
2063         if (!config->ftruncate) {
2064                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
2065         }
2066
2067         result = gpfswrap_ftruncate(fsp->fh->fd, len);
2068         if ((result == -1) && (errno == ENOSYS)) {
2069                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
2070         }
2071         return result;
2072 }
2073
2074 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
2075                                 const struct smb_filename *fname,
2076                                 SMB_STRUCT_STAT *sbuf)
2077 {
2078         struct gpfs_winattr attrs;
2079         struct gpfs_config_data *config;
2080         int ret;
2081
2082         SMB_VFS_HANDLE_GET_DATA(handle, config,
2083                                 struct gpfs_config_data,
2084                                 return false);
2085
2086         if (!config->winattr) {
2087                 return false;
2088         }
2089
2090         ret = gpfswrap_get_winattrs_path(fname->base_name, &attrs);
2091         if (ret == -1) {
2092                 return false;
2093         }
2094
2095         if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
2096                 DBG_DEBUG("%s is offline\n", fname->base_name);
2097                 return true;
2098         }
2099
2100         DBG_DEBUG("%s is online\n", fname->base_name);
2101         return false;
2102 }
2103
2104 static bool vfs_gpfs_fsp_is_offline(struct vfs_handle_struct *handle,
2105                                     struct files_struct *fsp)
2106 {
2107         struct gpfs_fsp_extension *ext;
2108
2109         ext = VFS_FETCH_FSP_EXTENSION(handle, fsp);
2110         if (ext == NULL) {
2111                 /*
2112                  * Something bad happened, always ask.
2113                  */
2114                 return vfs_gpfs_is_offline(handle, fsp->fsp_name,
2115                                            &fsp->fsp_name->st);
2116         }
2117
2118         if (ext->offline) {
2119                 /*
2120                  * As long as it's offline, ask.
2121                  */
2122                 ext->offline = vfs_gpfs_is_offline(handle, fsp->fsp_name,
2123                                                    &fsp->fsp_name->st);
2124         }
2125
2126         return ext->offline;
2127 }
2128
2129 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
2130                                struct files_struct *fsp)
2131 {
2132         return vfs_gpfs_fsp_is_offline(handle, fsp);
2133 }
2134
2135 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
2136                                  files_struct *fsp, const DATA_BLOB *hdr,
2137                                  off_t offset, size_t n)
2138 {
2139         if (vfs_gpfs_fsp_is_offline(handle, fsp)) {
2140                 errno = ENOSYS;
2141                 return -1;
2142         }
2143         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
2144 }
2145
2146 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
2147                             const char *service, const char *user)
2148 {
2149         struct gpfs_config_data *config;
2150         int ret;
2151         bool check_fstype;
2152
2153         gpfswrap_lib_init(0);
2154
2155         config = talloc_zero(handle->conn, struct gpfs_config_data);
2156         if (!config) {
2157                 DEBUG(0, ("talloc_zero() failed\n"));
2158                 errno = ENOMEM;
2159                 return -1;
2160         }
2161
2162         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2163         if (ret < 0) {
2164                 TALLOC_FREE(config);
2165                 return ret;
2166         }
2167
2168         check_fstype = lp_parm_bool(SNUM(handle->conn), "gpfs",
2169                                     "check_fstype", true);
2170
2171         if (check_fstype && !IS_IPC(handle->conn)) {
2172                 const char *connectpath = handle->conn->connectpath;
2173                 struct statfs buf = { 0 };
2174
2175                 ret = statfs(connectpath, &buf);
2176                 if (ret != 0) {
2177                         DBG_ERR("statfs failed for share %s at path %s: %s\n",
2178                                 service, connectpath, strerror(errno));
2179                         TALLOC_FREE(config);
2180                         return ret;
2181                 }
2182
2183                 if (buf.f_type != GPFS_SUPER_MAGIC) {
2184                         DBG_ERR("SMB share %s, path %s not in GPFS file system."
2185                                 " statfs magic: 0x%jx\n",
2186                                 service,
2187                                 connectpath,
2188                                 (uintmax_t)buf.f_type);
2189                         errno = EINVAL;
2190                         TALLOC_FREE(config);
2191                         return -1;
2192                 }
2193         }
2194
2195         ret = smbacl4_get_vfs_params(handle->conn, &config->nfs4_params);
2196         if (ret < 0) {
2197                 TALLOC_FREE(config);
2198                 return ret;
2199         }
2200
2201         config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2202                                         "sharemodes", true);
2203
2204         config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
2205                                         "leases", true);
2206
2207         config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
2208                                    "hsm", false);
2209
2210         config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
2211                                       "syncio", false);
2212
2213         config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
2214                                        "winattr", false);
2215
2216         config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
2217                                          "ftruncate", true);
2218
2219         config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
2220                                                "getrealfilename", true);
2221
2222         config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
2223                                           "dfreequota", false);
2224
2225         config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
2226
2227         config->settimes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2228                                         "settimes", true);
2229         config->recalls = lp_parm_bool(SNUM(handle->conn), "gpfs",
2230                                        "recalls", true);
2231
2232         SMB_VFS_HANDLE_SET_DATA(handle, config,
2233                                 NULL, struct gpfs_config_data,
2234                                 return -1);
2235
2236         if (config->leases) {
2237                 /*
2238                  * GPFS lease code is based on kernel oplock code
2239                  * so make sure it is turned on
2240                  */
2241                 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
2242                         DEBUG(5, ("Enabling kernel oplocks for "
2243                                   "gpfs:leases to work\n"));
2244                         lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
2245                                         "true");
2246                 }
2247
2248                 /*
2249                  * as the kernel does not properly support Level II oplocks
2250                  * and GPFS leases code is based on kernel infrastructure, we
2251                  * need to turn off Level II oplocks if gpfs:leases is enabled
2252                  */
2253                 if (lp_level2_oplocks(SNUM(handle->conn))) {
2254                         DEBUG(5, ("gpfs:leases are enabled, disabling "
2255                                   "Level II oplocks\n"));
2256                         lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
2257                                         "false");
2258                 }
2259         }
2260
2261         /*
2262          * Unless we have an async implementation of get_dos_attributes turn
2263          * this off.
2264          */
2265         lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
2266
2267         return 0;
2268 }
2269
2270 static int get_gpfs_quota(const char *pathname, int type, int id,
2271                           struct gpfs_quotaInfo *qi)
2272 {
2273         int ret;
2274
2275         ret = gpfswrap_quotactl(discard_const_p(char, pathname),
2276                                 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
2277
2278         if (ret) {
2279                 if (errno == GPFS_E_NO_QUOTA_INST) {
2280                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
2281                 } else if (errno != ENOSYS) {
2282                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
2283                                   "errno %d.\n", type, id, errno));
2284                 }
2285
2286                 return ret;
2287         }
2288
2289         DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
2290                    type, id, qi->blockUsage, qi->blockHardLimit,
2291                    qi->blockSoftLimit, qi->blockGraceTime));
2292
2293         return ret;
2294 }
2295
2296 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
2297                                      uint64_t *dfree, uint64_t *dsize)
2298 {
2299         uint64_t usage, limit;
2300
2301         /*
2302          * The quota reporting is done in units of 1024 byte blocks, but
2303          * sys_fsusage uses units of 512 byte blocks, adjust the block number
2304          * accordingly. Also filter possibly negative usage counts from gpfs.
2305          */
2306         usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
2307         limit = (uint64_t)qi.blockHardLimit * 2;
2308
2309         /*
2310          * When the grace time for the exceeded soft block quota has been
2311          * exceeded, the soft block quota becomes an additional hard limit.
2312          */
2313         if (qi.blockSoftLimit &&
2314             qi.blockGraceTime && cur_time > qi.blockGraceTime) {
2315                 /* report disk as full */
2316                 *dfree = 0;
2317                 *dsize = MIN(*dsize, usage);
2318         }
2319
2320         if (!qi.blockHardLimit)
2321                 return;
2322
2323         if (usage >= limit) {
2324                 /* report disk as full */
2325                 *dfree = 0;
2326                 *dsize = MIN(*dsize, usage);
2327
2328         } else {
2329                 /* limit has not been reached, determine "free space" */
2330                 *dfree = MIN(*dfree, limit - usage);
2331                 *dsize = MIN(*dsize, limit);
2332         }
2333 }
2334
2335 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle,
2336                                 const struct smb_filename *smb_fname,
2337                                 uint64_t *bsize,
2338                                 uint64_t *dfree,
2339                                 uint64_t *dsize)
2340 {
2341         struct security_unix_token *utok;
2342         struct gpfs_quotaInfo qi_user = { 0 }, qi_group = { 0 };
2343         struct gpfs_config_data *config;
2344         int err;
2345         time_t cur_time;
2346
2347         SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
2348                                 return (uint64_t)-1);
2349         if (!config->dfreequota) {
2350                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2351                                               bsize, dfree, dsize);
2352         }
2353
2354         err = sys_fsusage(smb_fname->base_name, dfree, dsize);
2355         if (err) {
2356                 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
2357                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2358                                               bsize, dfree, dsize);
2359         }
2360
2361         /* sys_fsusage returns units of 512 bytes */
2362         *bsize = 512;
2363
2364         DEBUG(10, ("fs dfree %llu, dsize %llu\n",
2365                    (unsigned long long)*dfree, (unsigned long long)*dsize));
2366
2367         utok = handle->conn->session_info->unix_token;
2368
2369         err = get_gpfs_quota(smb_fname->base_name,
2370                         GPFS_USRQUOTA, utok->uid, &qi_user);
2371         if (err) {
2372                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2373                                               bsize, dfree, dsize);
2374         }
2375
2376         /*
2377          * If new files created under this folder get this folder's
2378          * GID, then available space is governed by the quota of the
2379          * folder's GID, not the primary group of the creating user.
2380          */
2381         if (VALID_STAT(smb_fname->st) &&
2382             S_ISDIR(smb_fname->st.st_ex_mode) &&
2383             smb_fname->st.st_ex_mode & S_ISGID) {
2384                 become_root();
2385                 err = get_gpfs_quota(smb_fname->base_name, GPFS_GRPQUOTA,
2386                                      smb_fname->st.st_ex_gid, &qi_group);
2387                 unbecome_root();
2388
2389         } else {
2390                 err = get_gpfs_quota(smb_fname->base_name, GPFS_GRPQUOTA,
2391                                      utok->gid, &qi_group);
2392         }
2393
2394         if (err) {
2395                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2396                                               bsize, dfree, dsize);
2397         }
2398
2399         cur_time = time(NULL);
2400
2401         /* Adjust free space and size according to quota limits. */
2402         vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
2403         vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
2404
2405         return *dfree / 2;
2406 }
2407
2408 static int vfs_gpfs_get_quota(vfs_handle_struct *handle,
2409                                 const struct smb_filename *smb_fname,
2410                                 enum SMB_QUOTA_TYPE qtype,
2411                                 unid_t id,
2412                                 SMB_DISK_QUOTA *dq)
2413 {
2414         switch(qtype) {
2415                 /*
2416                  * User/group quota are being used for disk-free
2417                  * determination, which in this module is done directly
2418                  * by the disk-free function. It's important that this
2419                  * module does not return wrong quota values by mistake,
2420                  * which would modify the correct values set by disk-free.
2421                  * User/group quota are also being used for processing
2422                  * NT_TRANSACT_GET_USER_QUOTA in smb1 protocol, which is
2423                  * currently not supported by this module.
2424                  */
2425                 case SMB_USER_QUOTA_TYPE:
2426                 case SMB_GROUP_QUOTA_TYPE:
2427                         errno = ENOSYS;
2428                         return -1;
2429                 default:
2430                         return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname,
2431                                         qtype, id, dq);
2432         }
2433 }
2434
2435 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
2436                                       enum timestamp_set_resolution *p_ts_res)
2437 {
2438         struct gpfs_config_data *config;
2439         uint32_t next;
2440
2441         next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
2442
2443         SMB_VFS_HANDLE_GET_DATA(handle, config,
2444                                 struct gpfs_config_data,
2445                                 return next);
2446
2447         if (config->hsm) {
2448                 next |= FILE_SUPPORTS_REMOTE_STORAGE;
2449         }
2450         return next;
2451 }
2452
2453 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
2454                          struct smb_filename *smb_fname, files_struct *fsp,
2455                          int flags, mode_t mode)
2456 {
2457         struct gpfs_config_data *config;
2458         int ret;
2459         struct gpfs_fsp_extension *ext;
2460
2461         SMB_VFS_HANDLE_GET_DATA(handle, config,
2462                                 struct gpfs_config_data,
2463                                 return -1);
2464
2465         if (config->hsm && !config->recalls &&
2466             vfs_gpfs_fsp_is_offline(handle, fsp)) {
2467                 DEBUG(10, ("Refusing access to offline file %s\n",
2468                            fsp_str_dbg(fsp)));
2469                 errno = EACCES;
2470                 return -1;
2471         }
2472
2473         if (config->syncio) {
2474                 flags |= O_SYNC;
2475         }
2476
2477         ext = VFS_ADD_FSP_EXTENSION(handle, fsp, struct gpfs_fsp_extension,
2478                                     NULL);
2479         if (ext == NULL) {
2480                 errno = ENOMEM;
2481                 return -1;
2482         }
2483
2484         /*
2485          * Assume the file is offline until gpfs tells us it's online.
2486          */
2487         *ext = (struct gpfs_fsp_extension) { .offline = true };
2488
2489         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2490         if (ret == -1) {
2491                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
2492         }
2493         return ret;
2494 }
2495
2496 static ssize_t vfs_gpfs_pread(vfs_handle_struct *handle, files_struct *fsp,
2497                               void *data, size_t n, off_t offset)
2498 {
2499         ssize_t ret;
2500         bool was_offline;
2501
2502         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2503
2504         ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2505
2506         if ((ret != -1) && was_offline) {
2507                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2508                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2509                              fsp->fsp_name->base_name);
2510         }
2511
2512         return ret;
2513 }
2514
2515 struct vfs_gpfs_pread_state {
2516         struct files_struct *fsp;
2517         ssize_t ret;
2518         bool was_offline;
2519         struct vfs_aio_state vfs_aio_state;
2520 };
2521
2522 static void vfs_gpfs_pread_done(struct tevent_req *subreq);
2523
2524 static struct tevent_req *vfs_gpfs_pread_send(struct vfs_handle_struct *handle,
2525                                               TALLOC_CTX *mem_ctx,
2526                                               struct tevent_context *ev,
2527                                               struct files_struct *fsp,
2528                                               void *data, size_t n,
2529                                               off_t offset)
2530 {
2531         struct tevent_req *req, *subreq;
2532         struct vfs_gpfs_pread_state *state;
2533
2534         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
2535         if (req == NULL) {
2536                 return NULL;
2537         }
2538         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2539         state->fsp = fsp;
2540         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
2541                                          n, offset);
2542         if (tevent_req_nomem(subreq, req)) {
2543                 return tevent_req_post(req, ev);
2544         }
2545         tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
2546         return req;
2547 }
2548
2549 static void vfs_gpfs_pread_done(struct tevent_req *subreq)
2550 {
2551         struct tevent_req *req = tevent_req_callback_data(
2552                 subreq, struct tevent_req);
2553         struct vfs_gpfs_pread_state *state = tevent_req_data(
2554                 req, struct vfs_gpfs_pread_state);
2555
2556         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2557         TALLOC_FREE(subreq);
2558         tevent_req_done(req);
2559 }
2560
2561 static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req,
2562                                    struct vfs_aio_state *vfs_aio_state)
2563 {
2564         struct vfs_gpfs_pread_state *state = tevent_req_data(
2565                 req, struct vfs_gpfs_pread_state);
2566         struct files_struct *fsp = state->fsp;
2567
2568         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2569                 return -1;
2570         }
2571         *vfs_aio_state = state->vfs_aio_state;
2572
2573         if ((state->ret != -1) && state->was_offline) {
2574                 DEBUG(10, ("sending notify\n"));
2575                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2576                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2577                              fsp->fsp_name->base_name);
2578         }
2579
2580         return state->ret;
2581 }
2582
2583 static ssize_t vfs_gpfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
2584                                const void *data, size_t n, off_t offset)
2585 {
2586         ssize_t ret;
2587         bool was_offline;
2588
2589         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2590
2591         ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2592
2593         if ((ret != -1) && was_offline) {
2594                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2595                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2596                              fsp->fsp_name->base_name);
2597         }
2598
2599         return ret;
2600 }
2601
2602 struct vfs_gpfs_pwrite_state {
2603         struct files_struct *fsp;
2604         ssize_t ret;
2605         bool was_offline;
2606         struct vfs_aio_state vfs_aio_state;
2607 };
2608
2609 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq);
2610
2611 static struct tevent_req *vfs_gpfs_pwrite_send(
2612         struct vfs_handle_struct *handle,
2613         TALLOC_CTX *mem_ctx,
2614         struct tevent_context *ev,
2615         struct files_struct *fsp,
2616         const void *data, size_t n,
2617         off_t offset)
2618 {
2619         struct tevent_req *req, *subreq;
2620         struct vfs_gpfs_pwrite_state *state;
2621
2622         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
2623         if (req == NULL) {
2624                 return NULL;
2625         }
2626         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2627         state->fsp = fsp;
2628         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
2629                                          n, offset);
2630         if (tevent_req_nomem(subreq, req)) {
2631                 return tevent_req_post(req, ev);
2632         }
2633         tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
2634         return req;
2635 }
2636
2637 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
2638 {
2639         struct tevent_req *req = tevent_req_callback_data(
2640                 subreq, struct tevent_req);
2641         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2642                 req, struct vfs_gpfs_pwrite_state);
2643
2644         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2645         TALLOC_FREE(subreq);
2646         tevent_req_done(req);
2647 }
2648
2649 static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req,
2650                                     struct vfs_aio_state *vfs_aio_state)
2651 {
2652         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2653                 req, struct vfs_gpfs_pwrite_state);
2654         struct files_struct *fsp = state->fsp;
2655
2656         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2657                 return -1;
2658         }
2659         *vfs_aio_state = state->vfs_aio_state;
2660
2661         if ((state->ret != -1) && state->was_offline) {
2662                 DEBUG(10, ("sending notify\n"));
2663                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2664                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2665                              fsp->fsp_name->base_name);
2666         }
2667
2668         return state->ret;
2669 }
2670
2671
2672 static struct vfs_fn_pointers vfs_gpfs_fns = {
2673         .connect_fn = vfs_gpfs_connect,
2674         .disk_free_fn = vfs_gpfs_disk_free,
2675         .get_quota_fn = vfs_gpfs_get_quota,
2676         .fs_capabilities_fn = vfs_gpfs_capabilities,
2677         .kernel_flock_fn = vfs_gpfs_kernel_flock,
2678         .linux_setlease_fn = vfs_gpfs_setlease,
2679         .get_real_filename_fn = vfs_gpfs_get_real_filename,
2680         .get_dos_attributes_fn = vfs_gpfs_get_dos_attributes,
2681         .get_dos_attributes_send_fn = vfs_not_implemented_get_dos_attributes_send,
2682         .get_dos_attributes_recv_fn = vfs_not_implemented_get_dos_attributes_recv,
2683         .fget_dos_attributes_fn = vfs_gpfs_fget_dos_attributes,
2684         .set_dos_attributes_fn = vfs_gpfs_set_dos_attributes,
2685         .fset_dos_attributes_fn = vfs_gpfs_fset_dos_attributes,
2686         .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
2687         .get_nt_acl_fn = gpfsacl_get_nt_acl,
2688         .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
2689         .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
2690         .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
2691         .sys_acl_blob_get_file_fn = gpfsacl_sys_acl_blob_get_file,
2692         .sys_acl_blob_get_fd_fn = gpfsacl_sys_acl_blob_get_fd,
2693         .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
2694         .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
2695         .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
2696         .chmod_fn = vfs_gpfs_chmod,
2697         .fchmod_fn = vfs_gpfs_fchmod,
2698         .close_fn = vfs_gpfs_close,
2699         .stat_fn = vfs_gpfs_stat,
2700         .lstat_fn = vfs_gpfs_lstat,
2701         .ntimes_fn = vfs_gpfs_ntimes,
2702         .aio_force_fn = vfs_gpfs_aio_force,
2703         .sendfile_fn = vfs_gpfs_sendfile,
2704         .fallocate_fn = vfs_gpfs_fallocate,
2705         .open_fn = vfs_gpfs_open,
2706         .pread_fn = vfs_gpfs_pread,
2707         .pread_send_fn = vfs_gpfs_pread_send,
2708         .pread_recv_fn = vfs_gpfs_pread_recv,
2709         .pwrite_fn = vfs_gpfs_pwrite,
2710         .pwrite_send_fn = vfs_gpfs_pwrite_send,
2711         .pwrite_recv_fn = vfs_gpfs_pwrite_recv,
2712         .ftruncate_fn = vfs_gpfs_ftruncate
2713 };
2714
2715 static_decl_vfs;
2716 NTSTATUS vfs_gpfs_init(TALLOC_CTX *ctx)
2717 {
2718         int ret;
2719
2720         ret = gpfswrap_init();
2721         if (ret != 0) {
2722                 DEBUG(1, ("Could not initialize GPFS library wrapper\n"));
2723         }
2724
2725         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
2726                                 &vfs_gpfs_fns);
2727 }