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