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