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