f7434c95d820c5d303d8427529c2997a0fed1186
[ambi/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         smb_fname->st.st_ex_calculated_birthtime = false;
1569         smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1570         smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1571
1572         return NT_STATUS_OK;
1573 }
1574
1575 static NTSTATUS vfs_gpfs_fget_dos_attributes(struct vfs_handle_struct *handle,
1576                                              struct files_struct *fsp,
1577                                              uint32_t *dosmode)
1578 {
1579         struct gpfs_config_data *config;
1580         struct gpfs_winattr attrs = { };
1581         int ret;
1582
1583         SMB_VFS_HANDLE_GET_DATA(handle, config,
1584                                 struct gpfs_config_data,
1585                                 return NT_STATUS_INTERNAL_ERROR);
1586
1587         if (!config->winattr) {
1588                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1589         }
1590
1591         ret = gpfswrap_get_winattrs(fsp->fh->fd, &attrs);
1592         if (ret == -1 && errno == ENOSYS) {
1593                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1594         }
1595
1596         if (ret == -1) {
1597                 DBG_WARNING("Getting winattrs failed for %s: %s\n",
1598                             fsp->fsp_name->base_name, strerror(errno));
1599                 return map_nt_error_from_unix(errno);
1600         }
1601
1602         *dosmode |= vfs_gpfs_winattrs_to_dosmode(attrs.winAttrs);
1603         fsp->fsp_name->st.st_ex_calculated_birthtime = false;
1604         fsp->fsp_name->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1605         fsp->fsp_name->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1606
1607         return NT_STATUS_OK;
1608 }
1609
1610 static NTSTATUS vfs_gpfs_set_dos_attributes(struct vfs_handle_struct *handle,
1611                                            const struct smb_filename *smb_fname,
1612                                            uint32_t dosmode)
1613 {
1614         struct gpfs_config_data *config;
1615         struct gpfs_winattr attrs = { };
1616         int ret;
1617
1618         SMB_VFS_HANDLE_GET_DATA(handle, config,
1619                                 struct gpfs_config_data,
1620                                 return NT_STATUS_INTERNAL_ERROR);
1621
1622         if (!config->winattr) {
1623                 return SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
1624                                                        smb_fname, dosmode);
1625         }
1626
1627         attrs.winAttrs = vfs_gpfs_dosmode_to_winattrs(dosmode);
1628         ret = gpfswrap_set_winattrs_path(smb_fname->base_name,
1629                                          GPFS_WINATTR_SET_ATTRS, &attrs);
1630
1631         if (ret == -1 && errno == ENOSYS) {
1632                 return SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
1633                                                        smb_fname, dosmode);
1634         }
1635
1636         if (ret == -1) {
1637                 DBG_WARNING("Setting winattrs failed for %s: %s\n",
1638                             smb_fname->base_name, strerror(errno));
1639                 return map_nt_error_from_unix(errno);
1640         }
1641
1642         return NT_STATUS_OK;
1643 }
1644
1645 static NTSTATUS vfs_gpfs_fset_dos_attributes(struct vfs_handle_struct *handle,
1646                                              struct files_struct *fsp,
1647                                              uint32_t dosmode)
1648 {
1649         struct gpfs_config_data *config;
1650         struct gpfs_winattr attrs = { };
1651         int ret;
1652
1653         SMB_VFS_HANDLE_GET_DATA(handle, config,
1654                                 struct gpfs_config_data,
1655                                 return NT_STATUS_INTERNAL_ERROR);
1656
1657         if (!config->winattr) {
1658                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1659         }
1660
1661         attrs.winAttrs = vfs_gpfs_dosmode_to_winattrs(dosmode);
1662         ret = gpfswrap_set_winattrs(fsp->fh->fd,
1663                                     GPFS_WINATTR_SET_ATTRS, &attrs);
1664
1665         if (ret == -1 && errno == ENOSYS) {
1666                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1667         }
1668
1669         if (ret == -1) {
1670                 DBG_WARNING("Setting winattrs failed for %s: %s\n",
1671                             fsp->fsp_name->base_name, strerror(errno));
1672                 return map_nt_error_from_unix(errno);
1673         }
1674
1675         return NT_STATUS_OK;
1676 }
1677
1678 static int stat_with_capability(struct vfs_handle_struct *handle,
1679                                 struct smb_filename *smb_fname, int flag)
1680 {
1681 #if defined(HAVE_FSTATAT)
1682         int fd = -1;
1683         bool b;
1684         char *dir_name;
1685         const char *rel_name = NULL;
1686         struct stat st;
1687         int ret = -1;
1688
1689         b = parent_dirname(talloc_tos(), smb_fname->base_name,
1690                            &dir_name, &rel_name);
1691         if (!b) {
1692                 errno = ENOMEM;
1693                 return -1;
1694         }
1695
1696         fd = open(dir_name, O_RDONLY, 0);
1697         TALLOC_FREE(dir_name);
1698         if (fd == -1) {
1699                 return -1;
1700         }
1701
1702         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1703         ret = fstatat(fd, rel_name, &st, flag);
1704         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1705
1706         close(fd);
1707
1708         if (ret == 0) {
1709                 init_stat_ex_from_stat(
1710                         &smb_fname->st, &st,
1711                         lp_fake_directory_create_times(SNUM(handle->conn)));
1712         }
1713
1714         return ret;
1715 #else
1716         return -1;
1717 #endif
1718 }
1719
1720 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1721                          struct smb_filename *smb_fname)
1722 {
1723         int ret;
1724         struct gpfs_config_data *config;
1725
1726         SMB_VFS_HANDLE_GET_DATA(handle, config,
1727                                 struct gpfs_config_data,
1728                                 return -1);
1729
1730         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1731         if (ret == -1 && errno == EACCES) {
1732                 DEBUG(10, ("Trying stat with capability for %s\n",
1733                            smb_fname->base_name));
1734                 ret = stat_with_capability(handle, smb_fname, 0);
1735         }
1736         return ret;
1737 }
1738
1739 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1740                           struct smb_filename *smb_fname)
1741 {
1742         int ret;
1743         struct gpfs_config_data *config;
1744
1745         SMB_VFS_HANDLE_GET_DATA(handle, config,
1746                                 struct gpfs_config_data,
1747                                 return -1);
1748
1749         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1750         if (ret == -1 && errno == EACCES) {
1751                 DEBUG(10, ("Trying lstat with capability for %s\n",
1752                            smb_fname->base_name));
1753                 ret = stat_with_capability(handle, smb_fname,
1754                                            AT_SYMLINK_NOFOLLOW);
1755         }
1756         return ret;
1757 }
1758
1759 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
1760                                   int idx, int *flags)
1761 {
1762         if (!null_timespec(ts)) {
1763                 *flags |= 1 << idx;
1764                 gt[idx].tv_sec = ts.tv_sec;
1765                 gt[idx].tv_nsec = ts.tv_nsec;
1766                 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
1767         }
1768 }
1769
1770 static int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
1771 {
1772         gpfs_timestruc_t gpfs_times[4];
1773         int flags = 0;
1774         int rc;
1775
1776         ZERO_ARRAY(gpfs_times);
1777         timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
1778         timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
1779         /* No good mapping from LastChangeTime to ctime, not storing */
1780         timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
1781
1782         if (!flags) {
1783                 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
1784                 return 0;
1785         }
1786
1787         rc = gpfswrap_set_times_path(path, flags, gpfs_times);
1788
1789         if (rc != 0 && errno != ENOSYS) {
1790                 DEBUG(1,("gpfs_set_times() returned with error %s\n",
1791                         strerror(errno)));
1792         }
1793
1794         return rc;
1795 }
1796
1797 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1798                         const struct smb_filename *smb_fname,
1799                         struct smb_file_time *ft)
1800 {
1801
1802         struct gpfs_winattr attrs;
1803         int ret;
1804         char *path = NULL;
1805         NTSTATUS status;
1806         struct gpfs_config_data *config;
1807
1808         SMB_VFS_HANDLE_GET_DATA(handle, config,
1809                                 struct gpfs_config_data,
1810                                 return -1);
1811
1812         status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1813         if (!NT_STATUS_IS_OK(status)) {
1814                 errno = map_errno_from_nt_status(status);
1815                 return -1;
1816         }
1817
1818         /* Try to use gpfs_set_times if it is enabled and available */
1819         if (config->settimes) {
1820                 ret = smbd_gpfs_set_times_path(path, ft);
1821
1822                 if (ret == 0 || (ret == -1 && errno != ENOSYS)) {
1823                         return ret;
1824                 }
1825         }
1826
1827         DEBUG(10,("gpfs_set_times() not available or disabled, "
1828                   "use ntimes and winattr\n"));
1829
1830         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1831         if(ret == -1){
1832                 /* don't complain if access was denied */
1833                 if (errno != EPERM && errno != EACCES) {
1834                         DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1835                                  "%s", strerror(errno)));
1836                 }
1837                 return -1;
1838         }
1839
1840         if(null_timespec(ft->create_time)){
1841                 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1842                 return 0;
1843         }
1844
1845         if (!config->winattr) {
1846                 return 0;
1847         }
1848
1849         attrs.winAttrs = 0;
1850         attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1851         attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1852
1853         ret = gpfswrap_set_winattrs_path(discard_const_p(char, path),
1854                                          GPFS_WINATTR_SET_CREATION_TIME,
1855                                          &attrs);
1856         if(ret == -1 && errno != ENOSYS){
1857                 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1858                 return -1;
1859         }
1860         return 0;
1861
1862 }
1863
1864 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1865                        struct files_struct *fsp, uint32_t mode,
1866                        off_t offset, off_t len)
1867 {
1868         int ret;
1869         struct gpfs_config_data *config;
1870
1871         SMB_VFS_HANDLE_GET_DATA(handle, config,
1872                                 struct gpfs_config_data,
1873                                 return -1);
1874
1875         if (!config->prealloc) {
1876                 /* you should better not run fallocate() on GPFS at all */
1877                 errno = ENOTSUP;
1878                 return -1;
1879         }
1880
1881         if (mode != 0) {
1882                 DEBUG(10, ("unmapped fallocate flags: %lx\n",
1883                       (unsigned long)mode));
1884                 errno = ENOTSUP;
1885                 return -1;
1886         }
1887
1888         ret = gpfswrap_prealloc(fsp->fh->fd, offset, len);
1889
1890         if (ret == -1 && errno != ENOSYS) {
1891                 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1892         } else if (ret == -1 && errno == ENOSYS) {
1893                 DEBUG(10, ("GPFS prealloc not supported.\n"));
1894         } else {
1895                 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1896         }
1897
1898         return ret;
1899 }
1900
1901 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1902                                 off_t len)
1903 {
1904         int result;
1905         struct gpfs_config_data *config;
1906
1907         SMB_VFS_HANDLE_GET_DATA(handle, config,
1908                                 struct gpfs_config_data,
1909                                 return -1);
1910
1911         if (!config->ftruncate) {
1912                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1913         }
1914
1915         result = gpfswrap_ftruncate(fsp->fh->fd, len);
1916         if ((result == -1) && (errno == ENOSYS)) {
1917                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1918         }
1919         return result;
1920 }
1921
1922 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1923                                 const struct smb_filename *fname,
1924                                 SMB_STRUCT_STAT *sbuf)
1925 {
1926         struct gpfs_winattr attrs;
1927         char *path = NULL;
1928         NTSTATUS status;
1929         struct gpfs_config_data *config;
1930         int ret;
1931
1932         SMB_VFS_HANDLE_GET_DATA(handle, config,
1933                                 struct gpfs_config_data,
1934                                 return -1);
1935
1936         if (!config->winattr) {
1937                 return false;
1938         }
1939
1940         status = get_full_smb_filename(talloc_tos(), fname, &path);
1941         if (!NT_STATUS_IS_OK(status)) {
1942                 return false;
1943         }
1944
1945         ret = gpfswrap_get_winattrs_path(path, &attrs);
1946         if (ret == -1) {
1947                 TALLOC_FREE(path);
1948                 return false;
1949         }
1950
1951         if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1952                 DEBUG(10, ("%s is offline\n", path));
1953                 TALLOC_FREE(path);
1954                 return true;
1955         }
1956         DEBUG(10, ("%s is online\n", path));
1957         TALLOC_FREE(path);
1958         return false;
1959 }
1960
1961 static bool vfs_gpfs_fsp_is_offline(struct vfs_handle_struct *handle,
1962                                     struct files_struct *fsp)
1963 {
1964         struct gpfs_fsp_extension *ext;
1965
1966         ext = VFS_FETCH_FSP_EXTENSION(handle, fsp);
1967         if (ext == NULL) {
1968                 /*
1969                  * Something bad happened, always ask.
1970                  */
1971                 return vfs_gpfs_is_offline(handle, fsp->fsp_name,
1972                                            &fsp->fsp_name->st);
1973         }
1974
1975         if (ext->offline) {
1976                 /*
1977                  * As long as it's offline, ask.
1978                  */
1979                 ext->offline = vfs_gpfs_is_offline(handle, fsp->fsp_name,
1980                                                    &fsp->fsp_name->st);
1981         }
1982
1983         return ext->offline;
1984 }
1985
1986 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1987                                struct files_struct *fsp)
1988 {
1989         return vfs_gpfs_fsp_is_offline(handle, fsp);
1990 }
1991
1992 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1993                                  files_struct *fsp, const DATA_BLOB *hdr,
1994                                  off_t offset, size_t n)
1995 {
1996         if (vfs_gpfs_fsp_is_offline(handle, fsp)) {
1997                 errno = ENOSYS;
1998                 return -1;
1999         }
2000         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
2001 }
2002
2003 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
2004                             const char *service, const char *user)
2005 {
2006         struct gpfs_config_data *config;
2007         int ret;
2008
2009         gpfswrap_lib_init(0);
2010
2011         config = talloc_zero(handle->conn, struct gpfs_config_data);
2012         if (!config) {
2013                 DEBUG(0, ("talloc_zero() failed\n"));
2014                 errno = ENOMEM;
2015                 return -1;
2016         }
2017
2018         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2019         if (ret < 0) {
2020                 TALLOC_FREE(config);
2021                 return ret;
2022         }
2023
2024         ret = smbacl4_get_vfs_params(handle->conn, &config->nfs4_params);
2025         if (ret < 0) {
2026                 TALLOC_FREE(config);
2027                 return ret;
2028         }
2029
2030         config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2031                                         "sharemodes", true);
2032
2033         config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
2034                                         "leases", true);
2035
2036         config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
2037                                    "hsm", false);
2038
2039         config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
2040                                       "syncio", false);
2041
2042         config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
2043                                        "winattr", false);
2044
2045         config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
2046                                          "ftruncate", true);
2047
2048         config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
2049                                                "getrealfilename", true);
2050
2051         config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
2052                                           "dfreequota", false);
2053
2054         config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
2055                                    "prealloc", true);
2056
2057         config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
2058
2059         config->settimes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2060                                         "settimes", true);
2061         config->recalls = lp_parm_bool(SNUM(handle->conn), "gpfs",
2062                                        "recalls", true);
2063
2064         SMB_VFS_HANDLE_SET_DATA(handle, config,
2065                                 NULL, struct gpfs_config_data,
2066                                 return -1);
2067
2068         if (config->leases) {
2069                 /*
2070                  * GPFS lease code is based on kernel oplock code
2071                  * so make sure it is turned on
2072                  */
2073                 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
2074                         DEBUG(5, ("Enabling kernel oplocks for "
2075                                   "gpfs:leases to work\n"));
2076                         lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
2077                                         "true");
2078                 }
2079
2080                 /*
2081                  * as the kernel does not properly support Level II oplocks
2082                  * and GPFS leases code is based on kernel infrastructure, we
2083                  * need to turn off Level II oplocks if gpfs:leases is enabled
2084                  */
2085                 if (lp_level2_oplocks(SNUM(handle->conn))) {
2086                         DEBUG(5, ("gpfs:leases are enabled, disabling "
2087                                   "Level II oplocks\n"));
2088                         lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
2089                                         "false");
2090                 }
2091         }
2092
2093         return 0;
2094 }
2095
2096 static int get_gpfs_quota(const char *pathname, int type, int id,
2097                           struct gpfs_quotaInfo *qi)
2098 {
2099         int ret;
2100
2101         ret = gpfswrap_quotactl(discard_const_p(char, pathname),
2102                                 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
2103
2104         if (ret) {
2105                 if (errno == GPFS_E_NO_QUOTA_INST) {
2106                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
2107                 } else if (errno != ENOSYS) {
2108                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
2109                                   "errno %d.\n", type, id, errno));
2110                 }
2111
2112                 return ret;
2113         }
2114
2115         DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
2116                    type, id, qi->blockUsage, qi->blockHardLimit,
2117                    qi->blockSoftLimit, qi->blockGraceTime));
2118
2119         return ret;
2120 }
2121
2122 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
2123                                      uint64_t *dfree, uint64_t *dsize)
2124 {
2125         uint64_t usage, limit;
2126
2127         /*
2128          * The quota reporting is done in units of 1024 byte blocks, but
2129          * sys_fsusage uses units of 512 byte blocks, adjust the block number
2130          * accordingly. Also filter possibly negative usage counts from gpfs.
2131          */
2132         usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
2133         limit = (uint64_t)qi.blockHardLimit * 2;
2134
2135         /*
2136          * When the grace time for the exceeded soft block quota has been
2137          * exceeded, the soft block quota becomes an additional hard limit.
2138          */
2139         if (qi.blockSoftLimit &&
2140             qi.blockGraceTime && cur_time > qi.blockGraceTime) {
2141                 /* report disk as full */
2142                 *dfree = 0;
2143                 *dsize = MIN(*dsize, usage);
2144         }
2145
2146         if (!qi.blockHardLimit)
2147                 return;
2148
2149         if (usage >= limit) {
2150                 /* report disk as full */
2151                 *dfree = 0;
2152                 *dsize = MIN(*dsize, usage);
2153
2154         } else {
2155                 /* limit has not been reached, determine "free space" */
2156                 *dfree = MIN(*dfree, limit - usage);
2157                 *dsize = MIN(*dsize, limit);
2158         }
2159 }
2160
2161 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
2162                                    uint64_t *bsize,
2163                                    uint64_t *dfree, uint64_t *dsize)
2164 {
2165         struct security_unix_token *utok;
2166         struct gpfs_quotaInfo qi_user = { 0 }, qi_group = { 0 };
2167         struct gpfs_config_data *config;
2168         int err;
2169         time_t cur_time;
2170
2171         SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
2172                                 return (uint64_t)-1);
2173         if (!config->dfreequota) {
2174                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2175                                               bsize, dfree, dsize);
2176         }
2177
2178         err = sys_fsusage(path, dfree, dsize);
2179         if (err) {
2180                 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
2181                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2182                                               bsize, dfree, dsize);
2183         }
2184
2185         /* sys_fsusage returns units of 512 bytes */
2186         *bsize = 512;
2187
2188         DEBUG(10, ("fs dfree %llu, dsize %llu\n",
2189                    (unsigned long long)*dfree, (unsigned long long)*dsize));
2190
2191         utok = handle->conn->session_info->unix_token;
2192
2193         err = get_gpfs_quota(path, GPFS_USRQUOTA, utok->uid, &qi_user);
2194         if (err) {
2195                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2196                                               bsize, dfree, dsize);
2197         }
2198
2199         err = get_gpfs_quota(path, GPFS_GRPQUOTA, utok->gid, &qi_group);
2200         if (err) {
2201                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2202                                               bsize, dfree, dsize);
2203         }
2204
2205         cur_time = time(NULL);
2206
2207         /* Adjust free space and size according to quota limits. */
2208         vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
2209         vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
2210
2211         return *dfree / 2;
2212 }
2213
2214 static int vfs_gpfs_get_quota(vfs_handle_struct *handle, const char *path,
2215                           enum SMB_QUOTA_TYPE qtype, unid_t id,
2216                           SMB_DISK_QUOTA *dq)
2217 {
2218         switch(qtype) {
2219                 /*
2220                  * User/group quota are being used for disk-free
2221                  * determination, which in this module is done directly
2222                  * by the disk-free function. It's important that this
2223                  * module does not return wrong quota values by mistake,
2224                  * which would modify the correct values set by disk-free.
2225                  * User/group quota are also being used for processing
2226                  * NT_TRANSACT_GET_USER_QUOTA in smb1 protocol, which is
2227                  * currently not supported by this module.
2228                  */
2229                 case SMB_USER_QUOTA_TYPE:
2230                 case SMB_GROUP_QUOTA_TYPE:
2231                         errno = ENOSYS;
2232                         return -1;
2233                 default:
2234                         return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2235         }
2236 }
2237
2238 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
2239                                       enum timestamp_set_resolution *p_ts_res)
2240 {
2241         struct gpfs_config_data *config;
2242         uint32_t next;
2243
2244         next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
2245
2246         SMB_VFS_HANDLE_GET_DATA(handle, config,
2247                                 struct gpfs_config_data,
2248                                 return next);
2249
2250         if (config->hsm) {
2251                 next |= FILE_SUPPORTS_REMOTE_STORAGE;
2252         }
2253         return next;
2254 }
2255
2256 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
2257                          struct smb_filename *smb_fname, files_struct *fsp,
2258                          int flags, mode_t mode)
2259 {
2260         struct gpfs_config_data *config;
2261         int ret;
2262         struct gpfs_fsp_extension *ext;
2263
2264         SMB_VFS_HANDLE_GET_DATA(handle, config,
2265                                 struct gpfs_config_data,
2266                                 return -1);
2267
2268         if (config->hsm && !config->recalls &&
2269             vfs_gpfs_fsp_is_offline(handle, fsp)) {
2270                 DEBUG(10, ("Refusing access to offline file %s\n",
2271                            fsp_str_dbg(fsp)));
2272                 errno = EACCES;
2273                 return -1;
2274         }
2275
2276         if (config->syncio) {
2277                 flags |= O_SYNC;
2278         }
2279
2280         ext = VFS_ADD_FSP_EXTENSION(handle, fsp, struct gpfs_fsp_extension,
2281                                     NULL);
2282         if (ext == NULL) {
2283                 errno = ENOMEM;
2284                 return -1;
2285         }
2286
2287         /*
2288          * Assume the file is offline until gpfs tells us it's online.
2289          */
2290         *ext = (struct gpfs_fsp_extension) { .offline = true };
2291
2292         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2293         if (ret == -1) {
2294                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
2295         }
2296         return ret;
2297 }
2298
2299 static ssize_t vfs_gpfs_pread(vfs_handle_struct *handle, files_struct *fsp,
2300                               void *data, size_t n, off_t offset)
2301 {
2302         ssize_t ret;
2303         bool was_offline;
2304
2305         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2306
2307         ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2308
2309         if ((ret != -1) && was_offline) {
2310                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2311                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2312                              fsp->fsp_name->base_name);
2313         }
2314
2315         return ret;
2316 }
2317
2318 struct vfs_gpfs_pread_state {
2319         struct files_struct *fsp;
2320         ssize_t ret;
2321         bool was_offline;
2322         struct vfs_aio_state vfs_aio_state;
2323 };
2324
2325 static void vfs_gpfs_pread_done(struct tevent_req *subreq);
2326
2327 static struct tevent_req *vfs_gpfs_pread_send(struct vfs_handle_struct *handle,
2328                                               TALLOC_CTX *mem_ctx,
2329                                               struct tevent_context *ev,
2330                                               struct files_struct *fsp,
2331                                               void *data, size_t n,
2332                                               off_t offset)
2333 {
2334         struct tevent_req *req, *subreq;
2335         struct vfs_gpfs_pread_state *state;
2336
2337         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
2338         if (req == NULL) {
2339                 return NULL;
2340         }
2341         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2342         state->fsp = fsp;
2343         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
2344                                          n, offset);
2345         if (tevent_req_nomem(subreq, req)) {
2346                 return tevent_req_post(req, ev);
2347         }
2348         tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
2349         return req;
2350 }
2351
2352 static void vfs_gpfs_pread_done(struct tevent_req *subreq)
2353 {
2354         struct tevent_req *req = tevent_req_callback_data(
2355                 subreq, struct tevent_req);
2356         struct vfs_gpfs_pread_state *state = tevent_req_data(
2357                 req, struct vfs_gpfs_pread_state);
2358
2359         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2360         TALLOC_FREE(subreq);
2361         tevent_req_done(req);
2362 }
2363
2364 static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req,
2365                                    struct vfs_aio_state *vfs_aio_state)
2366 {
2367         struct vfs_gpfs_pread_state *state = tevent_req_data(
2368                 req, struct vfs_gpfs_pread_state);
2369         struct files_struct *fsp = state->fsp;
2370
2371         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2372                 return -1;
2373         }
2374         *vfs_aio_state = state->vfs_aio_state;
2375
2376         if ((state->ret != -1) && state->was_offline) {
2377                 DEBUG(10, ("sending notify\n"));
2378                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2379                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2380                              fsp->fsp_name->base_name);
2381         }
2382
2383         return state->ret;
2384 }
2385
2386 static ssize_t vfs_gpfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
2387                                const void *data, size_t n, off_t offset)
2388 {
2389         ssize_t ret;
2390         bool was_offline;
2391
2392         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2393
2394         ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2395
2396         if ((ret != -1) && was_offline) {
2397                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2398                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2399                              fsp->fsp_name->base_name);
2400         }
2401
2402         return ret;
2403 }
2404
2405 struct vfs_gpfs_pwrite_state {
2406         struct files_struct *fsp;
2407         ssize_t ret;
2408         bool was_offline;
2409         struct vfs_aio_state vfs_aio_state;
2410 };
2411
2412 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq);
2413
2414 static struct tevent_req *vfs_gpfs_pwrite_send(
2415         struct vfs_handle_struct *handle,
2416         TALLOC_CTX *mem_ctx,
2417         struct tevent_context *ev,
2418         struct files_struct *fsp,
2419         const void *data, size_t n,
2420         off_t offset)
2421 {
2422         struct tevent_req *req, *subreq;
2423         struct vfs_gpfs_pwrite_state *state;
2424
2425         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
2426         if (req == NULL) {
2427                 return NULL;
2428         }
2429         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2430         state->fsp = fsp;
2431         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
2432                                          n, offset);
2433         if (tevent_req_nomem(subreq, req)) {
2434                 return tevent_req_post(req, ev);
2435         }
2436         tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
2437         return req;
2438 }
2439
2440 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
2441 {
2442         struct tevent_req *req = tevent_req_callback_data(
2443                 subreq, struct tevent_req);
2444         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2445                 req, struct vfs_gpfs_pwrite_state);
2446
2447         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2448         TALLOC_FREE(subreq);
2449         tevent_req_done(req);
2450 }
2451
2452 static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req,
2453                                     struct vfs_aio_state *vfs_aio_state)
2454 {
2455         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2456                 req, struct vfs_gpfs_pwrite_state);
2457         struct files_struct *fsp = state->fsp;
2458
2459         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2460                 return -1;
2461         }
2462         *vfs_aio_state = state->vfs_aio_state;
2463
2464         if ((state->ret != -1) && state->was_offline) {
2465                 DEBUG(10, ("sending notify\n"));
2466                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2467                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2468                              fsp->fsp_name->base_name);
2469         }
2470
2471         return state->ret;
2472 }
2473
2474
2475 static struct vfs_fn_pointers vfs_gpfs_fns = {
2476         .connect_fn = vfs_gpfs_connect,
2477         .disk_free_fn = vfs_gpfs_disk_free,
2478         .get_quota_fn = vfs_gpfs_get_quota,
2479         .fs_capabilities_fn = vfs_gpfs_capabilities,
2480         .kernel_flock_fn = vfs_gpfs_kernel_flock,
2481         .linux_setlease_fn = vfs_gpfs_setlease,
2482         .get_real_filename_fn = vfs_gpfs_get_real_filename,
2483         .get_dos_attributes_fn = vfs_gpfs_get_dos_attributes,
2484         .fget_dos_attributes_fn = vfs_gpfs_fget_dos_attributes,
2485         .set_dos_attributes_fn = vfs_gpfs_set_dos_attributes,
2486         .fset_dos_attributes_fn = vfs_gpfs_fset_dos_attributes,
2487         .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
2488         .get_nt_acl_fn = gpfsacl_get_nt_acl,
2489         .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
2490         .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
2491         .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
2492         .sys_acl_blob_get_file_fn = gpfsacl_sys_acl_blob_get_file,
2493         .sys_acl_blob_get_fd_fn = gpfsacl_sys_acl_blob_get_fd,
2494         .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
2495         .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
2496         .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
2497         .chmod_fn = vfs_gpfs_chmod,
2498         .fchmod_fn = vfs_gpfs_fchmod,
2499         .close_fn = vfs_gpfs_close,
2500         .stat_fn = vfs_gpfs_stat,
2501         .lstat_fn = vfs_gpfs_lstat,
2502         .ntimes_fn = vfs_gpfs_ntimes,
2503         .aio_force_fn = vfs_gpfs_aio_force,
2504         .sendfile_fn = vfs_gpfs_sendfile,
2505         .fallocate_fn = vfs_gpfs_fallocate,
2506         .open_fn = vfs_gpfs_open,
2507         .pread_fn = vfs_gpfs_pread,
2508         .pread_send_fn = vfs_gpfs_pread_send,
2509         .pread_recv_fn = vfs_gpfs_pread_recv,
2510         .pwrite_fn = vfs_gpfs_pwrite,
2511         .pwrite_send_fn = vfs_gpfs_pwrite_send,
2512         .pwrite_recv_fn = vfs_gpfs_pwrite_recv,
2513         .ftruncate_fn = vfs_gpfs_ftruncate
2514 };
2515
2516 NTSTATUS vfs_gpfs_init(void);
2517 NTSTATUS vfs_gpfs_init(void)
2518 {
2519         int ret;
2520
2521         ret = gpfswrap_init();
2522         if (ret != 0) {
2523                 DEBUG(1, ("Could not initialize GPFS library wrapper\n"));
2524         }
2525
2526         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
2527                                 &vfs_gpfs_fns);
2528 }