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