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