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