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