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