LCOV - code coverage report
Current view: top level - source3/modules - vfs_gpfs.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 35 1136 3.1 %
Date: 2021-09-23 10:06:22 Functions: 2 74 2.7 %

          Line data    Source code
       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           0 : static inline unsigned int gpfs_acl_flags(gpfs_acl_t *gacl)
      68             : {
      69           0 :         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
      70           0 :                 return gacl->v4Level1.acl_flags;
      71             :         }
      72           0 :         return 0;
      73             : }
      74             : 
      75           0 : static inline gpfs_ace_v4_t *gpfs_ace_ptr(gpfs_acl_t *gacl, unsigned int i)
      76             : {
      77           0 :         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
      78           0 :                 return &gacl->v4Level1.ace_v4[i];
      79             :         }
      80           0 :         return &gacl->ace_v4[i];
      81             : }
      82             : 
      83           0 : static unsigned int vfs_gpfs_access_mask_to_allow(uint32_t access_mask)
      84             : {
      85           0 :         unsigned int allow = GPFS_SHARE_NONE;
      86             : 
      87           0 :         if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
      88           0 :                 allow |= GPFS_SHARE_WRITE;
      89             :         }
      90           0 :         if (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) {
      91           0 :                 allow |= GPFS_SHARE_READ;
      92             :         }
      93             : 
      94           0 :         return allow;
      95             : }
      96             : 
      97           0 : static unsigned int vfs_gpfs_share_access_to_deny(uint32_t share_access)
      98             : {
      99           7 :         unsigned int deny = GPFS_DENY_NONE;
     100             : 
     101           0 :         if (!(share_access & FILE_SHARE_WRITE)) {
     102           4 :                 deny |= GPFS_DENY_WRITE;
     103             :         }
     104           0 :         if (!(share_access & FILE_SHARE_READ)) {
     105           4 :                 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           0 :         if ((deny & (GPFS_DENY_WRITE|GPFS_DENY_READ)) &&
     113           0 :             !(share_access & FILE_SHARE_DELETE)) {
     114           3 :                 deny |= GPFS_DENY_DELETE;
     115             :         }
     116             : 
     117           0 :         return deny;
     118             : }
     119             : 
     120           0 : static int set_gpfs_sharemode(files_struct *fsp, uint32_t access_mask,
     121             :                               uint32_t share_access)
     122             : {
     123           0 :         unsigned int allow = GPFS_SHARE_NONE;
     124           0 :         unsigned int deny = GPFS_DENY_NONE;
     125             :         int result;
     126             : 
     127           0 :         if (access_mask == 0) {
     128           0 :                 DBG_DEBUG("Clearing file system share mode.\n");
     129             :         } else {
     130           0 :                 allow = vfs_gpfs_access_mask_to_allow(access_mask);
     131           0 :                 deny = vfs_gpfs_share_access_to_deny(share_access);
     132             :         }
     133           0 :         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           0 :         result = gpfswrap_set_share(fsp_get_io_fd(fsp), allow, deny);
     137           0 :         if (result == 0) {
     138           0 :                 return 0;
     139             :         }
     140             : 
     141           0 :         if (errno == EACCES) {
     142           0 :                 DBG_NOTICE("GPFS share mode denied for %s/%s.\n",
     143             :                            fsp->conn->connectpath,
     144             :                            fsp->fsp_name->base_name);
     145           0 :         } else if (errno == EPERM) {
     146           0 :                 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           0 :                 DBG_ERR("gpfs_set_share failed: %s\n", strerror(errno));
     154             :         }
     155             : 
     156           0 :         return result;
     157             : }
     158             : 
     159           0 : 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           0 :         int ret = 0;
     167             : 
     168           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     169             :                                 struct gpfs_config_data,
     170             :                                 return -1);
     171             : 
     172           0 :         if(!config->sharemodes) {
     173           0 :                 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           0 :         if (is_named_stream(fsp->fsp_name)) {
     182           0 :                 DBG_NOTICE("Not requesting GPFS sharemode on stream: %s/%s\n",
     183             :                            fsp->conn->connectpath,
     184             :                            fsp_str_dbg(fsp));
     185           0 :                 return 0;
     186             :         }
     187             : 
     188           0 :         ret = set_gpfs_sharemode(fsp, access_mask, share_access);
     189             : 
     190           0 :         return ret;
     191             : }
     192             : 
     193           0 : static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
     194             : {
     195             : 
     196             :         struct gpfs_config_data *config;
     197             : 
     198           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     199             :                                 struct gpfs_config_data,
     200             :                                 return -1);
     201             : 
     202           0 :         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           0 :                 int ret = gpfswrap_set_share(fsp_get_io_fd(fsp), 0, 0);
     211           0 :                 if (ret != 0) {
     212           0 :                         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           0 :         return SMB_VFS_NEXT_CLOSE(handle, fsp);
     221             : }
     222             : 
     223           0 : static int lease_type_to_gpfs(int leasetype)
     224             : {
     225           0 :         if (leasetype == F_RDLCK) {
     226           0 :                 return GPFS_LEASE_READ;
     227             :         }
     228             : 
     229           0 :         if (leasetype == F_WRLCK) {
     230           0 :                 return GPFS_LEASE_WRITE;
     231             :         }
     232             : 
     233           0 :         return GPFS_LEASE_NONE;
     234             : }
     235             : 
     236           0 : static int vfs_gpfs_setlease(vfs_handle_struct *handle,
     237             :                              files_struct *fsp,
     238             :                              int leasetype)
     239             : {
     240             :         struct gpfs_config_data *config;
     241           0 :         int ret=0;
     242             : 
     243           0 :         START_PROFILE(syscall_linux_setlease);
     244             : 
     245           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     246             :                                 struct gpfs_config_data,
     247             :                                 return -1);
     248             : 
     249           0 :         ret = linux_set_lease_sighandler(fsp_get_io_fd(fsp));
     250           0 :         if (ret == -1) {
     251           0 :                 goto failure;
     252             :         }
     253             : 
     254           0 :         if (config->leases) {
     255           0 :                 int gpfs_lease_type = lease_type_to_gpfs(leasetype);
     256           0 :                 int saved_errno = 0;
     257             : 
     258             :                 /*
     259             :                  * Ensure the lease owner is root to allow
     260             :                  * correct delivery of lease-break signals.
     261             :                  */
     262           0 :                 become_root();
     263           0 :                 ret = gpfswrap_set_lease(fsp_get_io_fd(fsp), gpfs_lease_type);
     264           0 :                 if (ret < 0) {
     265           0 :                         saved_errno = errno;
     266             :                 }
     267           0 :                 unbecome_root();
     268             : 
     269           0 :                 if (saved_errno != 0) {
     270           0 :                         errno = saved_errno;
     271             :                 }
     272             :         }
     273             : 
     274           0 : failure:
     275           0 :         END_PROFILE(syscall_linux_setlease);
     276             : 
     277           0 :         return ret;
     278             : }
     279             : 
     280           0 : 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           0 :         char *full_path = NULL;
     288           0 :         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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     296             :                                 struct gpfs_config_data,
     297             :                                 return -1);
     298             : 
     299           0 :         if (!config->getrealfilename) {
     300           0 :                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
     301             :                                                       mem_ctx, found_name);
     302             :         }
     303             : 
     304           0 :         mangled = mangle_is_mangled(name, handle->conn->params);
     305           0 :         if (mangled) {
     306           0 :                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
     307             :                                                       mem_ctx, found_name);
     308             :         }
     309             : 
     310           0 :         full_path_len = full_path_tos(path->base_name, name,
     311             :                                       tmpbuf, sizeof(tmpbuf),
     312             :                                       &full_path, &to_free);
     313           0 :         if (full_path_len == -1) {
     314           0 :                 errno = ENOMEM;
     315           0 :                 return -1;
     316             :         }
     317             : 
     318           0 :         buflen = sizeof(real_pathname) - 1;
     319             : 
     320           0 :         result = gpfswrap_get_realfilename_path(full_path, real_pathname,
     321             :                                                 &buflen);
     322             : 
     323           0 :         TALLOC_FREE(to_free);
     324             : 
     325           0 :         if ((result == -1) && (errno == ENOSYS)) {
     326           0 :                 return SMB_VFS_NEXT_GET_REAL_FILENAME(
     327             :                         handle, path, name, mem_ctx, found_name);
     328             :         }
     329             : 
     330           0 :         if (result == -1) {
     331           0 :                 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
     332             :                            strerror(errno)));
     333           0 :                 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           0 :         if (buflen < sizeof(real_pathname)) {
     342           0 :                 real_pathname[buflen] = '\0';
     343             :         } else {
     344           0 :                 real_pathname[sizeof(real_pathname)-1] = '\0';
     345             :         }
     346             : 
     347           0 :         DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
     348             :                    path->base_name, name, real_pathname));
     349             : 
     350           0 :         name = strrchr_m(real_pathname, '/');
     351           0 :         if (name == NULL) {
     352           0 :                 errno = ENOENT;
     353           0 :                 return -1;
     354             :         }
     355             : 
     356           0 :         *found_name = talloc_strdup(mem_ctx, name+1);
     357           0 :         if (*found_name == NULL) {
     358           0 :                 errno = ENOMEM;
     359           0 :                 return -1;
     360             :         }
     361             : 
     362           0 :         return 0;
     363             : }
     364             : 
     365           0 : static void sd2gpfs_control(uint16_t control, struct gpfs_acl *gacl)
     366             : {
     367           0 :         unsigned int gpfs_aclflags = 0;
     368           0 :         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           0 :         gpfs_aclflags = control << 8;
     373           0 :         if (!(control & SEC_DESC_DACL_PRESENT))
     374           0 :                 gpfs_aclflags |= ACL4_FLAG_NULL_DACL;
     375           0 :         if (!(control & SEC_DESC_SACL_PRESENT))
     376           0 :                 gpfs_aclflags |= ACL4_FLAG_NULL_SACL;
     377           0 :         gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
     378           0 :         gacl->v4Level1.acl_flags = gpfs_aclflags;
     379           0 : }
     380             : 
     381           0 : static uint16_t gpfs2sd_control(unsigned int gpfs_aclflags)
     382             : {
     383           0 :         uint16_t control = gpfs_aclflags >> 8;
     384           0 :         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           0 :         control |= SEC_DESC_SELF_RELATIVE;
     389           0 :         return control;
     390             : }
     391             : 
     392           0 : static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
     393             : {
     394             :         gpfs_aclCount_t i;
     395           0 :         if (gacl==NULL)
     396             :         {
     397           0 :                 DEBUG(0, ("gpfs acl is NULL\n"));
     398           0 :                 return;
     399             :         }
     400             : 
     401           0 :         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           0 :         for(i=0; i<gacl->acl_nace; i++)
     407             :         {
     408           0 :                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
     409           0 :                 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           0 : static int gpfs_getacl_with_capability(const char *fname, int flags, void *buf)
     417             : {
     418             :         int ret, saved_errno;
     419             : 
     420           0 :         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
     421             : 
     422           0 :         ret = gpfswrap_getacl(fname, flags, buf);
     423           0 :         saved_errno = errno;
     424             : 
     425           0 :         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
     426             : 
     427           0 :         errno = saved_errno;
     428           0 :         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           0 : 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           0 :         size_t size = 512;
     448             :         int ret, flags;
     449             :         unsigned int *len;
     450             :         size_t struct_size;
     451           0 :         bool use_capability = false;
     452             : 
     453           0 : again:
     454             : 
     455           0 :         aclbuf = talloc_zero_size(mem_ctx, size);
     456           0 :         if (aclbuf == NULL) {
     457           0 :                 errno = ENOMEM;
     458           0 :                 return NULL;
     459             :         }
     460             : 
     461           0 :         if (raw) {
     462           0 :                 struct gpfs_opaque_acl *buf = (struct gpfs_opaque_acl *) aclbuf;
     463           0 :                 buf->acl_type = type;
     464           0 :                 flags = GPFS_GETACL_NATIVE;
     465           0 :                 len = (unsigned int *) &(buf->acl_buffer_len);
     466           0 :                 struct_size = sizeof(struct gpfs_opaque_acl);
     467             :         } else {
     468           0 :                 struct gpfs_acl *buf = (struct gpfs_acl *) aclbuf;
     469           0 :                 buf->acl_type = type;
     470           0 :                 buf->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
     471           0 :                 flags = GPFS_GETACL_STRUCT;
     472           0 :                 len = &(buf->acl_len);
     473             :                 /* reserve space for control flags in gpfs 3.5 and beyond */
     474           0 :                 struct_size = sizeof(struct gpfs_acl) + sizeof(unsigned int);
     475             :         }
     476             : 
     477             :         /* set the length of the buffer as input value */
     478           0 :         *len = size;
     479             : 
     480           0 :         if (use_capability) {
     481           0 :                 ret = gpfs_getacl_with_capability(fname, flags, aclbuf);
     482             :         } else {
     483           0 :                 ret = gpfswrap_getacl(fname, flags, aclbuf);
     484           0 :                 if ((ret != 0) && (errno == EACCES)) {
     485           0 :                         DBG_DEBUG("Retry with DAC capability for %s\n", fname);
     486           0 :                         use_capability = true;
     487           0 :                         ret = gpfs_getacl_with_capability(fname, flags, aclbuf);
     488             :                 }
     489             :         }
     490             : 
     491           0 :         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           0 :                 size = *len + struct_size;
     501           0 :                 talloc_free(aclbuf);
     502           0 :                 DEBUG(10, ("Increasing ACL buffer size to %zu\n", size));
     503           0 :                 goto again;
     504             :         }
     505             : 
     506           0 :         if (ret != 0) {
     507           0 :                 DEBUG(5, ("smbd_gpfs_getacl failed with %s\n",
     508             :                           strerror(errno)));
     509           0 :                 talloc_free(aclbuf);
     510           0 :                 return NULL;
     511             :         }
     512             : 
     513           0 :         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           0 : 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           0 :         struct gpfs_acl *gacl = NULL;
     527           0 :         DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
     528             : 
     529             :         /* Get the ACL */
     530           0 :         gacl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(), fname,
     531             :                                                   false, 0);
     532           0 :         if (gacl == NULL) {
     533           0 :                 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
     534             :                            fname, strerror(errno)));
     535           0 :                 if (errno == ENODATA) {
     536             :                         /*
     537             :                          * GPFS returns ENODATA for snapshot
     538             :                          * directories. Retry with POSIX ACLs check.
     539             :                          */
     540           0 :                         return 1;
     541             :                 }
     542             : 
     543           0 :                 return -1;
     544             :         }
     545             : 
     546           0 :         if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
     547           0 :                 DEBUG(10, ("Got non-nfsv4 acl\n"));
     548             :                 /* Retry with POSIX ACLs check */
     549           0 :                 talloc_free(gacl);
     550           0 :                 return 1;
     551             :         }
     552             : 
     553           0 :         *ppacl = smb_create_smb4acl(mem_ctx);
     554             : 
     555           0 :         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
     556           0 :                 uint16_t control = gpfs2sd_control(gpfs_acl_flags(gacl));
     557           0 :                 smbacl4_set_controlflags(*ppacl, control);
     558             :         }
     559             : 
     560           0 :         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           0 :         for (i=0; i<gacl->acl_nace; i++) {
     565           0 :                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
     566           0 :                 SMB_ACE4PROP_T smbace = { 0 };
     567           0 :                 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           0 :                 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
     572           0 :                         smbace.flags |= SMB_ACE4_ID_SPECIAL;
     573           0 :                         switch (gace->aceWho) {
     574           0 :                         case ACE4_SPECIAL_OWNER:
     575           0 :                                 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
     576           0 :                                 break;
     577           0 :                         case ACE4_SPECIAL_GROUP:
     578           0 :                                 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
     579           0 :                                 break;
     580           0 :                         case ACE4_SPECIAL_EVERYONE:
     581           0 :                                 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
     582           0 :                                 break;
     583           0 :                         default:
     584           0 :                                 DEBUG(8, ("invalid special gpfs id %d "
     585             :                                           "ignored\n", gace->aceWho));
     586           0 :                                 continue; /* don't add it */
     587             :                         }
     588             :                 } else {
     589           0 :                         if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
     590           0 :                                 smbace.who.gid = gace->aceWho;
     591             :                         else
     592           0 :                                 smbace.who.uid = gace->aceWho;
     593             :                 }
     594             : 
     595             :                 /* remove redundant deny entries */
     596           0 :                 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
     597           0 :                         struct gpfs_ace_v4 *prev = gpfs_ace_ptr(gacl, i - 1);
     598           0 :                         if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
     599           0 :                             prev->aceFlags == gace->aceFlags &&
     600           0 :                             prev->aceIFlags == gace->aceIFlags &&
     601           0 :                             (gace->aceMask & prev->aceMask) == 0 &&
     602           0 :                             gace->aceWho == prev->aceWho) {
     603             :                                 /* it's redundant - skip it */
     604           0 :                                 continue;
     605             :                         }
     606             :                 }
     607             : 
     608           0 :                 smbace.aceType = gace->aceType;
     609           0 :                 smbace.aceFlags = gace->aceFlags;
     610           0 :                 smbace.aceMask = gace->aceMask;
     611           0 :                 smb_add_ace4(*ppacl, &smbace);
     612             :         }
     613             : 
     614           0 :         talloc_free(gacl);
     615             : 
     616           0 :         return 0;
     617             : }
     618             : 
     619           0 : 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           0 :         struct SMB4ACL_T *pacl = NULL;
     625             :         int     result;
     626             :         struct gpfs_config_data *config;
     627           0 :         TALLOC_CTX *frame = talloc_stackframe();
     628             :         NTSTATUS status;
     629             : 
     630           0 :         *ppdesc = NULL;
     631             : 
     632           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     633             :                                 struct gpfs_config_data,
     634             :                                 return NT_STATUS_INTERNAL_ERROR);
     635             : 
     636           0 :         if (!config->acl) {
     637           0 :                 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
     638             :                                                   mem_ctx, ppdesc);
     639           0 :                 TALLOC_FREE(frame);
     640           0 :                 return status;
     641             :         }
     642             : 
     643           0 :         result = gpfs_get_nfs4_acl(frame, fsp->fsp_name->base_name, &pacl);
     644             : 
     645           0 :         if (result == 0) {
     646           0 :                 status = smb_fget_nt_acl_nfs4(fsp, &config->nfs4_params,
     647             :                                               security_info,
     648             :                                               mem_ctx, ppdesc, pacl);
     649           0 :                 TALLOC_FREE(frame);
     650           0 :                 return status;
     651             :         }
     652             : 
     653           0 :         if (result > 0) {
     654           0 :                 DEBUG(10, ("retrying with posix acl...\n"));
     655           0 :                 status = posix_fget_nt_acl(fsp, security_info,
     656             :                                            mem_ctx, ppdesc);
     657           0 :                 TALLOC_FREE(frame);
     658           0 :                 return status;
     659             :         }
     660             : 
     661           0 :         TALLOC_FREE(frame);
     662             : 
     663             :         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
     664           0 :         return map_nt_error_from_unix(errno);
     665             : }
     666             : 
     667           0 : 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           0 :         gace->aceType = nfs4_ace->aceType;
     672           0 :         gace->aceFlags = nfs4_ace->aceFlags;
     673           0 :         gace->aceMask = nfs4_ace->aceMask;
     674             : 
     675           0 :         if (nfs4_ace->flags & SMB_ACE4_ID_SPECIAL) {
     676           0 :                 switch(nfs4_ace->who.special_id) {
     677           0 :                 case SMB_ACE4_WHO_EVERYONE:
     678           0 :                         gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
     679           0 :                         gace->aceWho = ACE4_SPECIAL_EVERYONE;
     680           0 :                         break;
     681           0 :                 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           0 :                         if (!nfs_ace_is_inherit(nfs4_ace) &&
     699           0 :                             nfs4_ace->aceType ==
     700           0 :                                         SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
     701           0 :                             nfs4_ace->aceMask & (SMB_ACE4_READ_ATTRIBUTES|
     702             :                                                  SMB_ACE4_WRITE_ATTRIBUTES|
     703             :                                                  SMB_ACE4_READ_ACL|
     704             :                                                  SMB_ACE4_WRITE_ACL)) {
     705           0 :                                 gace->aceIFlags = 0;
     706           0 :                                 gace->aceWho = owner_uid;
     707             :                         } else {
     708           0 :                                 gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
     709           0 :                                 gace->aceWho = ACE4_SPECIAL_OWNER;
     710             :                         }
     711           0 :                         break;
     712           0 :                 case SMB_ACE4_WHO_GROUP:
     713           0 :                         gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
     714           0 :                         gace->aceWho = ACE4_SPECIAL_GROUP;
     715           0 :                         break;
     716           0 :                 default:
     717           0 :                         DBG_WARNING("Unsupported special_id %d\n",
     718             :                                     nfs4_ace->who.special_id);
     719           0 :                         return false;
     720             :                 }
     721             : 
     722           0 :                 return true;
     723             :         }
     724             : 
     725           0 :         gace->aceIFlags = 0;
     726           0 :         gace->aceWho = (nfs4_ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) ?
     727           0 :                 nfs4_ace->who.gid : nfs4_ace->who.uid;
     728             : 
     729           0 :         return true;
     730             : }
     731             : 
     732           0 : 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           0 :         gacl_len = offsetof(gpfs_acl_t, ace_v4) + sizeof(unsigned int)
     742           0 :                 + smb_get_naces(smbacl) * sizeof(gpfs_ace_v4_t);
     743             : 
     744           0 :         gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
     745           0 :         if (gacl == NULL) {
     746           0 :                 DEBUG(0, ("talloc failed\n"));
     747           0 :                 errno = ENOMEM;
     748           0 :                 return NULL;
     749             :         }
     750             : 
     751           0 :         gacl->acl_level = GPFS_ACL_LEVEL_BASE;
     752           0 :         gacl->acl_version = GPFS_ACL_VERSION_NFS4;
     753           0 :         gacl->acl_type = GPFS_ACL_TYPE_NFS4;
     754           0 :         gacl->acl_nace = 0; /* change later... */
     755             : 
     756           0 :         if (controlflags) {
     757           0 :                 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
     758           0 :                 sd2gpfs_control(smbacl4_get_controlflags(smbacl), gacl);
     759             :         }
     760             : 
     761           0 :         for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
     762           0 :                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, gacl->acl_nace);
     763           0 :                 SMB_ACE4PROP_T  *aceprop = smb_get_ace4(smbace);
     764             :                 bool add_ace;
     765             : 
     766           0 :                 add_ace = vfs_gpfs_nfs4_ace_to_gpfs_ace(aceprop, gace,
     767           0 :                                                         fsp->fsp_name->st.st_ex_uid);
     768           0 :                 if (!add_ace) {
     769           0 :                         continue;
     770             :                 }
     771             : 
     772           0 :                 gacl->acl_nace++;
     773             :         }
     774           0 :         gacl->acl_len = (char *)gpfs_ace_ptr(gacl, gacl->acl_nace)
     775           0 :                 - (char *)gacl;
     776           0 :         return gacl;
     777             : }
     778             : 
     779           0 : 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           0 :         TALLOC_CTX *mem_ctx = talloc_tos();
     786             : 
     787           0 :         gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, true);
     788           0 :         if (gacl == NULL) { /* out of memory */
     789           0 :                 return False;
     790             :         }
     791           0 :         ret = gpfswrap_putacl(fsp->fsp_name->base_name,
     792             :                               GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
     793             : 
     794           0 :         if ((ret != 0) && (errno == EINVAL)) {
     795           0 :                 DEBUG(10, ("Retry without nfs41 control flags\n"));
     796           0 :                 talloc_free(gacl);
     797           0 :                 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, false);
     798           0 :                 if (gacl == NULL) { /* out of memory */
     799           0 :                         return False;
     800             :                 }
     801           0 :                 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
     802             :                                       GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA,
     803             :                                       gacl);
     804             :         }
     805             : 
     806           0 :         if (ret != 0) {
     807           0 :                 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
     808           0 :                 gpfs_dumpacl(8, gacl);
     809           0 :                 return False;
     810             :         }
     811             : 
     812           0 :         DEBUG(10, ("gpfs_putacl succeeded\n"));
     813           0 :         return True;
     814             : }
     815             : 
     816           0 : 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           0 :         NTSTATUS result = NT_STATUS_ACCESS_DENIED;
     820             : 
     821           0 :         acl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(),
     822           0 :                                                  fsp->fsp_name->base_name,
     823             :                                                  false, 0);
     824           0 :         if (acl == NULL) {
     825           0 :                 return map_nt_error_from_unix(errno);
     826             :         }
     827             : 
     828           0 :         if (acl->acl_version == GPFS_ACL_VERSION_NFS4) {
     829             :                 struct gpfs_config_data *config;
     830             : 
     831           0 :                 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
     832             :                                  "refuse_dacl_protected", false)
     833           0 :                     && (psd->type&SEC_DESC_DACL_PROTECTED)) {
     834           0 :                         DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
     835           0 :                         talloc_free(acl);
     836           0 :                         return NT_STATUS_NOT_SUPPORTED;
     837             :                 }
     838             : 
     839           0 :                 SMB_VFS_HANDLE_GET_DATA(handle, config,
     840             :                                         struct gpfs_config_data,
     841             :                                         return NT_STATUS_INTERNAL_ERROR);
     842             : 
     843           0 :                 result = smb_set_nt_acl_nfs4(handle,
     844           0 :                         fsp, &config->nfs4_params, security_info_sent, psd,
     845             :                         gpfsacl_process_smbacl);
     846             :         } else { /* assume POSIX ACL - by default... */
     847           0 :                 result = set_nt_acl(fsp, security_info_sent, psd);
     848             :         }
     849             : 
     850           0 :         talloc_free(acl);
     851           0 :         return result;
     852             : }
     853             : 
     854           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     859             :                                 struct gpfs_config_data,
     860             :                                 return NT_STATUS_INTERNAL_ERROR);
     861             : 
     862           0 :         if (!config->acl) {
     863           0 :                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
     864             :         }
     865             : 
     866           0 :         return gpfsacl_set_nt_acl_internal(handle, fsp, security_info_sent, psd);
     867             : }
     868             : 
     869           0 : 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           0 :         result = sys_acl_init(mem_ctx);
     875           0 :         if (result == NULL) {
     876           0 :                 errno = ENOMEM;
     877           0 :                 return NULL;
     878             :         }
     879             : 
     880           0 :         result->count = pacl->acl_nace;
     881           0 :         result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry,
     882             :                                      result->count);
     883           0 :         if (result->acl == NULL) {
     884           0 :                 TALLOC_FREE(result);
     885           0 :                 errno = ENOMEM;
     886           0 :                 return NULL;
     887             :         }
     888             : 
     889           0 :         for (i=0; i<pacl->acl_nace; i++) {
     890           0 :                 struct smb_acl_entry *ace = &result->acl[i];
     891           0 :                 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
     892             : 
     893           0 :                 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           0 :                 switch (g_ace->ace_type) {
     898           0 :                 case GPFS_ACL_USER:
     899           0 :                         ace->a_type = SMB_ACL_USER;
     900           0 :                         ace->info.user.uid = (uid_t)g_ace->ace_who;
     901           0 :                         break;
     902           0 :                 case GPFS_ACL_USER_OBJ:
     903           0 :                         ace->a_type = SMB_ACL_USER_OBJ;
     904           0 :                         break;
     905           0 :                 case GPFS_ACL_GROUP:
     906           0 :                         ace->a_type = SMB_ACL_GROUP;
     907           0 :                         ace->info.group.gid = (gid_t)g_ace->ace_who;
     908           0 :                         break;
     909           0 :                 case GPFS_ACL_GROUP_OBJ:
     910           0 :                         ace->a_type = SMB_ACL_GROUP_OBJ;
     911           0 :                         break;
     912           0 :                 case GPFS_ACL_OTHER:
     913           0 :                         ace->a_type = SMB_ACL_OTHER;
     914           0 :                         break;
     915           0 :                 case GPFS_ACL_MASK:
     916           0 :                         ace->a_type = SMB_ACL_MASK;
     917           0 :                         break;
     918           0 :                 default:
     919           0 :                         DEBUG(10, ("Got invalid ace_type: %d\n",
     920             :                                    g_ace->ace_type));
     921           0 :                         TALLOC_FREE(result);
     922           0 :                         errno = EINVAL;
     923           0 :                         return NULL;
     924             :                 }
     925             : 
     926           0 :                 ace->a_perm = 0;
     927           0 :                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
     928           0 :                         SMB_ACL_READ : 0;
     929           0 :                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
     930           0 :                         SMB_ACL_WRITE : 0;
     931           0 :                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
     932           0 :                         SMB_ACL_EXECUTE : 0;
     933             : 
     934           0 :                 DEBUGADD(10, ("Converted to %d perm %x\n",
     935             :                               ace->a_type, ace->a_perm));
     936             :         }
     937             : 
     938           0 :         return result;
     939             : }
     940             : 
     941           0 : 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           0 :         SMB_ACL_T result = NULL;
     946             : 
     947           0 :         pacl = vfs_gpfs_getacl(talloc_tos(), path, false, type);
     948             : 
     949           0 :         if (pacl == NULL) {
     950           0 :                 DEBUG(10, ("vfs_gpfs_getacl failed for %s with %s\n",
     951             :                            path, strerror(errno)));
     952           0 :                 if (errno == 0) {
     953           0 :                         errno = EINVAL;
     954             :                 }
     955           0 :                 goto done;
     956             :         }
     957             : 
     958           0 :         if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
     959           0 :                 DEBUG(10, ("Got acl version %d, expected %d\n",
     960             :                            pacl->acl_version, GPFS_ACL_VERSION_POSIX));
     961           0 :                 errno = EINVAL;
     962           0 :                 goto done;
     963             :         }
     964             : 
     965           0 :         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           0 :         result = gpfs2smb_acl(pacl, mem_ctx);
     970           0 :         if (result != NULL) {
     971           0 :                 errno = 0;
     972             :         }
     973             : 
     974           0 :  done:
     975             : 
     976           0 :         if (pacl != NULL) {
     977           0 :                 talloc_free(pacl);
     978             :         }
     979           0 :         if (errno != 0) {
     980           0 :                 TALLOC_FREE(result);
     981             :         }
     982           0 :         return result;
     983             : }
     984             : 
     985           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
     994             :                                 struct gpfs_config_data,
     995             :                                 return NULL);
     996             : 
     997           0 :         if (!config->acl) {
     998           0 :                 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, type, mem_ctx);
     999             :         }
    1000             : 
    1001           0 :         switch(type) {
    1002           0 :         case SMB_ACL_TYPE_ACCESS:
    1003           0 :                 gpfs_type = GPFS_ACL_TYPE_ACCESS;
    1004           0 :                 break;
    1005           0 :         case SMB_ACL_TYPE_DEFAULT:
    1006           0 :                 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
    1007           0 :                 break;
    1008           0 :         default:
    1009           0 :                 DEBUG(0, ("Got invalid type: %d\n", type));
    1010           0 :                 smb_panic("exiting");
    1011             :         }
    1012           0 :         return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
    1013             :                                      gpfs_type, mem_ctx);
    1014             : }
    1015             : 
    1016           0 : 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           0 :         struct gpfs_opaque_acl *acl = NULL;
    1024             :         DATA_BLOB aclblob;
    1025             :         int result;
    1026             : 
    1027           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1028             :                                 struct gpfs_config_data,
    1029             :                                 return -1);
    1030             : 
    1031           0 :         if (!config->acl) {
    1032           0 :                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
    1033             :                                                         blob_description, blob);
    1034             :         }
    1035             : 
    1036           0 :         errno = 0;
    1037           0 :         acl = (struct gpfs_opaque_acl *) vfs_gpfs_getacl(mem_ctx,
    1038           0 :                                                 fsp->fsp_name->base_name,
    1039             :                                                 true,
    1040             :                                                 GPFS_ACL_TYPE_NFS4);
    1041             : 
    1042           0 :         if (errno) {
    1043           0 :                 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           0 :                 if (errno != EINVAL) {
    1048           0 :                         return -1;
    1049             :                 }
    1050             :         }
    1051             : 
    1052           0 :         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           0 :                 aclblob.data = (uint8_t*) acl->acl_var_data;
    1062           0 :                 aclblob.length = acl->acl_buffer_len;
    1063             : 
    1064           0 :                 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
    1065           0 :                 if (!*blob_description) {
    1066           0 :                         talloc_free(acl);
    1067           0 :                         errno = ENOMEM;
    1068           0 :                         return -1;
    1069             :                 }
    1070             : 
    1071           0 :                 result = non_posix_sys_acl_blob_get_fd_helper(handle, fsp,
    1072             :                                                               aclblob, mem_ctx,
    1073             :                                                               blob);
    1074             : 
    1075           0 :                 talloc_free(acl);
    1076           0 :                 return result;
    1077             :         }
    1078             : 
    1079             :         /* fall back to POSIX ACL */
    1080           0 :         return posix_sys_acl_blob_get_fd(handle, fsp, mem_ctx,
    1081             :                                          blob_description, blob);
    1082             : }
    1083             : 
    1084           0 : 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           0 :         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
    1092             : 
    1093           0 :         len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
    1094             :                 sizeof(gpfs_ace_v1_t);
    1095             : 
    1096           0 :         result = (struct gpfs_acl *)SMB_MALLOC(len);
    1097           0 :         if (result == NULL) {
    1098           0 :                 errno = ENOMEM;
    1099           0 :                 return result;
    1100             :         }
    1101             : 
    1102           0 :         result->acl_len = len;
    1103           0 :         result->acl_level = 0;
    1104           0 :         result->acl_version = GPFS_ACL_VERSION_POSIX;
    1105           0 :         result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
    1106           0 :                 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
    1107           0 :         result->acl_nace = pacl->count;
    1108             : 
    1109           0 :         for (i=0; i<pacl->count; i++) {
    1110           0 :                 const struct smb_acl_entry *ace = &pacl->acl[i];
    1111           0 :                 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
    1112             : 
    1113           0 :                 DEBUG(10, ("Converting type %d perm %x\n",
    1114             :                            (int)ace->a_type, (int)ace->a_perm));
    1115             : 
    1116           0 :                 g_ace->ace_perm = 0;
    1117             : 
    1118           0 :                 switch(ace->a_type) {
    1119           0 :                 case SMB_ACL_USER:
    1120           0 :                         g_ace->ace_type = GPFS_ACL_USER;
    1121           0 :                         g_ace->ace_who = (gpfs_uid_t)ace->info.user.uid;
    1122           0 :                         break;
    1123           0 :                 case SMB_ACL_USER_OBJ:
    1124           0 :                         g_ace->ace_type = GPFS_ACL_USER_OBJ;
    1125           0 :                         g_ace->ace_perm |= ACL_PERM_CONTROL;
    1126           0 :                         g_ace->ace_who = 0;
    1127           0 :                         break;
    1128           0 :                 case SMB_ACL_GROUP:
    1129           0 :                         g_ace->ace_type = GPFS_ACL_GROUP;
    1130           0 :                         g_ace->ace_who = (gpfs_uid_t)ace->info.group.gid;
    1131           0 :                         break;
    1132           0 :                 case SMB_ACL_GROUP_OBJ:
    1133           0 :                         g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
    1134           0 :                         g_ace->ace_who = 0;
    1135           0 :                         break;
    1136           0 :                 case SMB_ACL_MASK:
    1137           0 :                         g_ace->ace_type = GPFS_ACL_MASK;
    1138           0 :                         g_ace->ace_perm = 0x8f;
    1139           0 :                         g_ace->ace_who = 0;
    1140           0 :                         break;
    1141           0 :                 case SMB_ACL_OTHER:
    1142           0 :                         g_ace->ace_type = GPFS_ACL_OTHER;
    1143           0 :                         g_ace->ace_who = 0;
    1144           0 :                         break;
    1145           0 :                 default:
    1146           0 :                         DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
    1147           0 :                         errno = EINVAL;
    1148           0 :                         SAFE_FREE(result);
    1149           0 :                         return NULL;
    1150             :                 }
    1151             : 
    1152           0 :                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
    1153           0 :                         ACL_PERM_READ : 0;
    1154           0 :                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
    1155           0 :                         ACL_PERM_WRITE : 0;
    1156           0 :                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
    1157           0 :                         ACL_PERM_EXECUTE : 0;
    1158             : 
    1159           0 :                 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           0 :         return result;
    1164             : }
    1165             : 
    1166           0 : 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           0 :         struct gpfs_acl *gpfs_acl = NULL;
    1173             :         int result;
    1174             : 
    1175           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1176             :                                 struct gpfs_config_data,
    1177             :                                 return -1);
    1178             : 
    1179           0 :         if (!config->acl) {
    1180           0 :                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
    1181             :         }
    1182             : 
    1183           0 :         gpfs_acl = smb2gpfs_acl(theacl, type);
    1184           0 :         if (gpfs_acl == NULL) {
    1185           0 :                 return -1;
    1186             :         }
    1187             : 
    1188             :         /*
    1189             :          * This is no longer a handle based call.
    1190             :          */
    1191           0 :         result = gpfswrap_putacl(fsp->fsp_name->base_name,
    1192             :                                  GPFS_PUTACL_STRUCT|GPFS_ACL_SAMBA,
    1193             :                                  gpfs_acl);
    1194           0 :         SAFE_FREE(gpfs_acl);
    1195           0 :         return result;
    1196             : }
    1197             : 
    1198           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1204             :                                 struct gpfs_config_data,
    1205             :                                 return -1);
    1206             : 
    1207           0 :         if (!config->acl) {
    1208           0 :                 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FD(handle, fsp);
    1209             :         }
    1210             : 
    1211           0 :         errno = ENOTSUP;
    1212           0 :         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           0 : static uint32_t gpfsacl_mask_filter(uint32_t aceType, uint32_t aceMask, uint32_t rwx)
    1221             : {
    1222           0 :         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           0 :         uint32_t        posix_mask = 0x01;
    1229             :         uint32_t        posix_bit;
    1230             :         uint32_t        nfs4_bits;
    1231             : 
    1232           0 :         for(i=0; i<3; i++) {
    1233           0 :                 nfs4_bits = posix_nfs4map[i];
    1234           0 :                 posix_bit = rwx & posix_mask;
    1235             : 
    1236           0 :                 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
    1237           0 :                         if (posix_bit)
    1238           0 :                                 aceMask |= nfs4_bits;
    1239             :                         else
    1240           0 :                                 aceMask &= ~nfs4_bits;
    1241             :                 } else {
    1242             :                         /* add deny bits when suitable */
    1243           0 :                         if (!posix_bit)
    1244           0 :                                 aceMask |= nfs4_bits;
    1245             :                         else
    1246           0 :                                 aceMask &= ~nfs4_bits;
    1247             :                 } /* other ace types are unexpected */
    1248             : 
    1249           0 :                 posix_mask <<= 1;
    1250             :         }
    1251             : 
    1252           0 :         return aceMask;
    1253             : }
    1254             : 
    1255           0 : static int gpfsacl_emu_chmod(vfs_handle_struct *handle,
    1256             :                              const struct smb_filename *fname, mode_t mode)
    1257             : {
    1258           0 :         char *path = fname->base_name;
    1259           0 :         struct SMB4ACL_T *pacl = NULL;
    1260             :         int     result;
    1261           0 :         bool    haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
    1262             :         int     i;
    1263           0 :         files_struct fake_fsp = { 0 }; /* TODO: rationalize parametrization */
    1264             :         struct SMB4ACE_T *smbace;
    1265           0 :         TALLOC_CTX *frame = talloc_stackframe();
    1266             : 
    1267           0 :         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
    1268             : 
    1269           0 :         result = gpfs_get_nfs4_acl(frame, path, &pacl);
    1270           0 :         if (result) {
    1271           0 :                 TALLOC_FREE(frame);
    1272           0 :                 return result;
    1273             :         }
    1274             : 
    1275           0 :         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
    1276           0 :                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
    1277             :         }
    1278             : 
    1279           0 :         for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
    1280           0 :                 SMB_ACE4PROP_T  *ace = smb_get_ace4(smbace);
    1281           0 :                 uint32_t        specid = ace->who.special_id;
    1282             : 
    1283           0 :                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
    1284           0 :                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
    1285             :                     specid <= SMB_ACE4_WHO_EVERYONE) {
    1286             : 
    1287             :                         uint32_t newMask;
    1288             : 
    1289           0 :                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
    1290           0 :                                 haveAllowEntry[specid] = True;
    1291             : 
    1292             :                         /* mode >> 6 for @owner, mode >> 3 for @group,
    1293             :                          * mode >> 0 for @everyone */
    1294           0 :                         newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
    1295           0 :                                                       mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
    1296           0 :                         if (ace->aceMask!=newMask) {
    1297           0 :                                 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
    1298             :                                            path, ace->aceMask, newMask, specid));
    1299             :                         }
    1300           0 :                         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           0 :         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
    1309           0 :                 SMB_ACE4PROP_T ace = { 0 };
    1310             : 
    1311           0 :                 if (haveAllowEntry[i]==True)
    1312           0 :                         continue;
    1313             : 
    1314           0 :                 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
    1315           0 :                 ace.flags |= SMB_ACE4_ID_SPECIAL;
    1316           0 :                 ace.who.special_id = i;
    1317             : 
    1318           0 :                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
    1319           0 :                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
    1320             : 
    1321           0 :                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
    1322           0 :                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
    1323             : 
    1324             :                 /* don't add unnecessary aces */
    1325           0 :                 if (!ace.aceMask)
    1326           0 :                         continue;
    1327             : 
    1328             :                 /* we add it to the END - as windows expects allow aces */
    1329           0 :                 smb_add_ace4(pacl, &ace);
    1330           0 :                 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           0 :         fake_fsp.fsp_name = synthetic_smb_fname(frame,
    1336             :                                                 path,
    1337             :                                                 NULL,
    1338             :                                                 NULL,
    1339             :                                                 fname->twrp,
    1340             :                                                 0);
    1341           0 :         if (fake_fsp.fsp_name == NULL) {
    1342           0 :                 errno = ENOMEM;
    1343           0 :                 TALLOC_FREE(frame);
    1344           0 :                 return -1;
    1345             :         }
    1346             :         /* put the acl */
    1347           0 :         if (gpfsacl_process_smbacl(handle, &fake_fsp, pacl) == False) {
    1348           0 :                 TALLOC_FREE(frame);
    1349           0 :                 return -1;
    1350             :         }
    1351             : 
    1352           0 :         TALLOC_FREE(frame);
    1353           0 :         return 0; /* ok for [f]chmod */
    1354             : }
    1355             : 
    1356           0 : 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           0 :                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
    1362           0 :                          return -1;
    1363             :                  }
    1364             : 
    1365             :                  /* avoid chmod() if possible, to preserve acls */
    1366           0 :                  if ((st.st_ex_mode & ~S_IFMT) == mode) {
    1367           0 :                          return 0;
    1368             :                  }
    1369             : 
    1370           0 :                  rc = gpfsacl_emu_chmod(handle, fsp->fsp_name,
    1371             :                                         mode);
    1372           0 :                  if (rc == 1)
    1373           0 :                          return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
    1374           0 :                  return rc;
    1375             : }
    1376             : 
    1377           0 : static uint32_t vfs_gpfs_winattrs_to_dosmode(unsigned int winattrs)
    1378             : {
    1379           6 :         uint32_t dosmode = 0;
    1380             : 
    1381           0 :         if (winattrs & GPFS_WINATTR_ARCHIVE){
    1382           1 :                 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
    1383             :         }
    1384           0 :         if (winattrs & GPFS_WINATTR_HIDDEN){
    1385           1 :                 dosmode |= FILE_ATTRIBUTE_HIDDEN;
    1386             :         }
    1387           0 :         if (winattrs & GPFS_WINATTR_SYSTEM){
    1388           1 :                 dosmode |= FILE_ATTRIBUTE_SYSTEM;
    1389             :         }
    1390           0 :         if (winattrs & GPFS_WINATTR_READONLY){
    1391           1 :                 dosmode |= FILE_ATTRIBUTE_READONLY;
    1392             :         }
    1393           0 :         if (winattrs & GPFS_WINATTR_SPARSE_FILE) {
    1394           1 :                 dosmode |= FILE_ATTRIBUTE_SPARSE;
    1395             :         }
    1396           0 :         if (winattrs & GPFS_WINATTR_OFFLINE) {
    1397           1 :                 dosmode |= FILE_ATTRIBUTE_OFFLINE;
    1398             :         }
    1399             : 
    1400           0 :         return dosmode;
    1401             : }
    1402             : 
    1403           0 : static unsigned int vfs_gpfs_dosmode_to_winattrs(uint32_t dosmode)
    1404             : {
    1405           6 :         unsigned int winattrs = 0;
    1406             : 
    1407           0 :         if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
    1408           1 :                 winattrs |= GPFS_WINATTR_ARCHIVE;
    1409             :         }
    1410           0 :         if (dosmode & FILE_ATTRIBUTE_HIDDEN){
    1411           1 :                 winattrs |= GPFS_WINATTR_HIDDEN;
    1412             :         }
    1413           0 :         if (dosmode & FILE_ATTRIBUTE_SYSTEM){
    1414           1 :                 winattrs |= GPFS_WINATTR_SYSTEM;
    1415             :         }
    1416           0 :         if (dosmode & FILE_ATTRIBUTE_READONLY){
    1417           1 :                 winattrs |= GPFS_WINATTR_READONLY;
    1418             :         }
    1419           0 :         if (dosmode & FILE_ATTRIBUTE_SPARSE) {
    1420           1 :                 winattrs |= GPFS_WINATTR_SPARSE_FILE;
    1421             :         }
    1422           0 :         if (dosmode & FILE_ATTRIBUTE_OFFLINE) {
    1423           1 :                 winattrs |= GPFS_WINATTR_OFFLINE;
    1424             :         }
    1425             : 
    1426           0 :         return winattrs;
    1427             : }
    1428             : 
    1429           5 : static NTSTATUS vfs_gpfs_get_file_id(struct gpfs_iattr64 *iattr,
    1430             :                                      uint64_t *fileid)
    1431           5 : {
    1432             :         uint8_t input[sizeof(gpfs_ino64_t) +
    1433             :                       sizeof(gpfs_gen64_t) +
    1434             :                       sizeof(gpfs_snapid64_t)];
    1435           5 :         uint8_t digest[gnutls_hash_get_len(GNUTLS_DIG_SHA1)];
    1436             :         int rc;
    1437             : 
    1438           5 :         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           5 :         SBVAL(input,
    1442             :               0, iattr->ia_inode);
    1443           5 :         SBVAL(input,
    1444             :               sizeof(gpfs_ino64_t), iattr->ia_gen);
    1445           5 :         SBVAL(input,
    1446             :               sizeof(gpfs_ino64_t) + sizeof(gpfs_gen64_t), iattr->ia_modsnapid);
    1447             : 
    1448             :         GNUTLS_FIPS140_SET_LAX_MODE();
    1449           5 :         rc = gnutls_hash_fast(GNUTLS_DIG_SHA1, input, sizeof(input), &digest);
    1450             :         GNUTLS_FIPS140_SET_STRICT_MODE();
    1451             : 
    1452           5 :         if (rc != 0) {
    1453           0 :                 return gnutls_error_to_ntstatus(rc,
    1454             :                                                 NT_STATUS_HASH_NOT_SUPPORTED);
    1455             :         }
    1456             : 
    1457          10 :         memcpy(fileid, &digest, sizeof(*fileid));
    1458           5 :         DBG_DEBUG("file_id 0x%" PRIx64 "\n", *fileid);
    1459             : 
    1460           5 :         return NT_STATUS_OK;
    1461             : }
    1462             : 
    1463           0 : static struct timespec gpfs_timestruc64_to_timespec(struct gpfs_timestruc64 g)
    1464             : {
    1465           0 :         return (struct timespec) { .tv_sec = g.tv_sec, .tv_nsec = g.tv_nsec };
    1466             : }
    1467             : 
    1468           0 : 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           0 :         int fd = fsp_get_pathref_fd(fsp);
    1474             :         char buf[PATH_MAX];
    1475           0 :         const char *p = NULL;
    1476           0 :         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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1484             :                                 struct gpfs_config_data,
    1485             :                                 return NT_STATUS_INTERNAL_ERROR);
    1486             : 
    1487           0 :         if (!config->winattr) {
    1488           0 :                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
    1489             :         }
    1490             : 
    1491           0 :         if (fsp->fsp_flags.is_pathref && !config->pathref_ok.gpfs_fstat_x) {
    1492           0 :                 if (fsp->fsp_flags.have_proc_fds) {
    1493           0 :                         p = sys_proc_fd_path(fd, buf, sizeof(buf));
    1494           0 :                         if (p == NULL) {
    1495           0 :                                 return NT_STATUS_NO_MEMORY;
    1496             :                         }
    1497             :                 } else {
    1498           0 :                         p = fsp->fsp_name->base_name;
    1499             :                 }
    1500             :         }
    1501             : 
    1502           0 :         if (p != NULL) {
    1503           0 :                 ret = gpfswrap_stat_x(p, &litemask, &iattr, sizeof(iattr));
    1504             :         } else {
    1505           0 :                 ret = gpfswrap_fstat_x(fd, &litemask, &iattr, sizeof(iattr));
    1506             :         }
    1507           0 :         if (ret == -1 && errno == ENOSYS) {
    1508           0 :                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
    1509             :         }
    1510             : 
    1511           0 :         if (ret == -1 && errno == EACCES) {
    1512           0 :                 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           0 :                 set_effective_capability(DAC_OVERRIDE_CAPABILITY);
    1522             : 
    1523           0 :                 if (p != NULL) {
    1524           0 :                         ret = gpfswrap_stat_x(p,
    1525             :                                               &litemask,
    1526             :                                               &iattr,
    1527             :                                               sizeof(iattr));
    1528             :                 } else {
    1529           0 :                         ret = gpfswrap_fstat_x(fd,
    1530             :                                                &litemask,
    1531             :                                                &iattr,
    1532             :                                                sizeof(iattr));
    1533             :                 }
    1534           0 :                 if (ret == -1) {
    1535           0 :                         saved_errno = errno;
    1536             :                 }
    1537             : 
    1538           0 :                 drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
    1539             : 
    1540           0 :                 if (saved_errno != 0) {
    1541           0 :                         errno = saved_errno;
    1542             :                 }
    1543             :         }
    1544             : 
    1545           0 :         if (ret == -1) {
    1546           0 :                 DBG_WARNING("Getting winattrs failed for %s: %s\n",
    1547             :                             fsp->fsp_name->base_name, strerror(errno));
    1548           0 :                 return map_nt_error_from_unix(errno);
    1549             :         }
    1550             : 
    1551           0 :         ZERO_STRUCT(file_id);
    1552           0 :         status = vfs_gpfs_get_file_id(&iattr, &file_id);
    1553           0 :         if (!NT_STATUS_IS_OK(status)) {
    1554           0 :                 return status;
    1555             :         }
    1556             : 
    1557           0 :         ts = gpfs_timestruc64_to_timespec(iattr.ia_createtime);
    1558             : 
    1559           0 :         *dosmode |= vfs_gpfs_winattrs_to_dosmode(iattr.ia_winflags);
    1560           0 :         update_stat_ex_create_time(&fsp->fsp_name->st, ts);
    1561           0 :         update_stat_ex_file_id(&fsp->fsp_name->st, file_id);
    1562             : 
    1563           0 :         return NT_STATUS_OK;
    1564             : }
    1565             : 
    1566           0 : 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           0 :         struct gpfs_winattr attrs = { };
    1572             :         int ret;
    1573             : 
    1574           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1575             :                                 struct gpfs_config_data,
    1576             :                                 return NT_STATUS_INTERNAL_ERROR);
    1577             : 
    1578           0 :         if (!config->winattr) {
    1579           0 :                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
    1580             :         }
    1581             : 
    1582           0 :         attrs.winAttrs = vfs_gpfs_dosmode_to_winattrs(dosmode);
    1583             : 
    1584           0 :         if (!fsp->fsp_flags.is_pathref) {
    1585           0 :                 ret = gpfswrap_set_winattrs(fsp_get_io_fd(fsp),
    1586             :                                             GPFS_WINATTR_SET_ATTRS, &attrs);
    1587           0 :                 if (ret == -1) {
    1588           0 :                         DBG_WARNING("Setting winattrs failed for %s: %s\n",
    1589             :                                     fsp_str_dbg(fsp), strerror(errno));
    1590           0 :                         return map_nt_error_from_unix(errno);
    1591             :                 }
    1592           0 :                 return NT_STATUS_OK;
    1593             :         }
    1594             : 
    1595           0 :         if (fsp->fsp_flags.have_proc_fds) {
    1596           0 :                 int fd = fsp_get_pathref_fd(fsp);
    1597           0 :                 const char *p = NULL;
    1598             :                 char buf[PATH_MAX];
    1599             : 
    1600           0 :                 p = sys_proc_fd_path(fd, buf, sizeof(buf));
    1601           0 :                 if (p == NULL) {
    1602           0 :                         return NT_STATUS_NO_MEMORY;
    1603             :                 }
    1604             : 
    1605           0 :                 ret = gpfswrap_set_winattrs_path(p,
    1606             :                                                  GPFS_WINATTR_SET_ATTRS,
    1607             :                                                  &attrs);
    1608           0 :                 if (ret == -1) {
    1609           0 :                         DBG_WARNING("Setting winattrs failed for [%s][%s]: %s\n",
    1610             :                                     p, fsp_str_dbg(fsp), strerror(errno));
    1611           0 :                         return map_nt_error_from_unix(errno);
    1612             :                 }
    1613           0 :                 return NT_STATUS_OK;
    1614             :         }
    1615             : 
    1616             :         /*
    1617             :          * This is no longer a handle based call.
    1618             :          */
    1619           0 :         ret = gpfswrap_set_winattrs_path(fsp->fsp_name->base_name,
    1620             :                                          GPFS_WINATTR_SET_ATTRS,
    1621             :                                          &attrs);
    1622           0 :         if (ret == -1) {
    1623           0 :                 DBG_WARNING("Setting winattrs failed for [%s]: %s\n",
    1624             :                             fsp_str_dbg(fsp), strerror(errno));
    1625           0 :                 return map_nt_error_from_unix(errno);
    1626             :         }
    1627             : 
    1628           0 :         return NT_STATUS_OK;
    1629             : }
    1630             : 
    1631           0 : static int stat_with_capability(struct vfs_handle_struct *handle,
    1632             :                                 struct smb_filename *smb_fname, int flag)
    1633             : {
    1634           0 :         int fd = -1;
    1635             :         NTSTATUS status;
    1636           0 :         struct smb_filename *dir_name = NULL;
    1637           0 :         struct smb_filename *rel_name = NULL;
    1638             :         struct stat st;
    1639           0 :         int ret = -1;
    1640             : 
    1641           0 :         status = SMB_VFS_PARENT_PATHNAME(handle->conn,
    1642             :                                          talloc_tos(),
    1643             :                                          smb_fname,
    1644             :                                          &dir_name,
    1645             :                                          &rel_name);
    1646           0 :         if (!NT_STATUS_IS_OK(status)) {
    1647           0 :                 errno = map_errno_from_nt_status(status);
    1648           0 :                 return -1;
    1649             :         }
    1650             : 
    1651           0 :         fd = open(dir_name->base_name, O_RDONLY, 0);
    1652           0 :         if (fd == -1) {
    1653           0 :                 TALLOC_FREE(dir_name);
    1654           0 :                 return -1;
    1655             :         }
    1656             : 
    1657           0 :         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
    1658           0 :         ret = fstatat(fd, rel_name->base_name, &st, flag);
    1659           0 :         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
    1660             : 
    1661           0 :         TALLOC_FREE(dir_name);
    1662           0 :         close(fd);
    1663             : 
    1664           0 :         if (ret == 0) {
    1665           0 :                 init_stat_ex_from_stat(
    1666           0 :                         &smb_fname->st, &st,
    1667           0 :                         lp_fake_directory_create_times(SNUM(handle->conn)));
    1668             :         }
    1669             : 
    1670           0 :         return ret;
    1671             : }
    1672             : 
    1673           0 : static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
    1674             :                          struct smb_filename *smb_fname)
    1675             : {
    1676             :         int ret;
    1677             : 
    1678           0 :         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
    1679           0 :         if (ret == -1 && errno == EACCES) {
    1680           0 :                 DEBUG(10, ("Trying stat with capability for %s\n",
    1681             :                            smb_fname->base_name));
    1682           0 :                 ret = stat_with_capability(handle, smb_fname, 0);
    1683             :         }
    1684           0 :         return ret;
    1685             : }
    1686             : 
    1687           0 : static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
    1688             :                           struct smb_filename *smb_fname)
    1689             : {
    1690             :         int ret;
    1691             : 
    1692           0 :         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
    1693           0 :         if (ret == -1 && errno == EACCES) {
    1694           0 :                 DEBUG(10, ("Trying lstat with capability for %s\n",
    1695             :                            smb_fname->base_name));
    1696           0 :                 ret = stat_with_capability(handle, smb_fname,
    1697             :                                            AT_SYMLINK_NOFOLLOW);
    1698             :         }
    1699           0 :         return ret;
    1700             : }
    1701             : 
    1702           0 : static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
    1703             :                                   int idx, int *flags)
    1704             : {
    1705           0 :         if (!is_omit_timespec(&ts)) {
    1706           0 :                 *flags |= 1 << idx;
    1707           0 :                 gt[idx].tv_sec = ts.tv_sec;
    1708           0 :                 gt[idx].tv_nsec = ts.tv_nsec;
    1709           0 :                 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
    1710             :         }
    1711           0 : }
    1712             : 
    1713           0 : 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           0 :         int flags = 0;
    1718             :         int rc;
    1719             : 
    1720           0 :         ZERO_ARRAY(gpfs_times);
    1721           0 :         timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
    1722           0 :         timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
    1723             :         /* No good mapping from LastChangeTime to ctime, not storing */
    1724           0 :         timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
    1725             : 
    1726           0 :         if (!flags) {
    1727           0 :                 DBG_DEBUG("nothing to do, return to avoid EINVAL\n");
    1728           0 :                 return 0;
    1729             :         }
    1730             : 
    1731           0 :         if (!fsp->fsp_flags.is_pathref) {
    1732           0 :                 rc = gpfswrap_set_times(fsp_get_io_fd(fsp), flags, gpfs_times);
    1733           0 :                 if (rc != 0) {
    1734           0 :                         DBG_WARNING("gpfs_set_times(%s) failed: %s\n",
    1735             :                                     fsp_str_dbg(fsp), strerror(errno));
    1736             :                 }
    1737           0 :                 return rc;
    1738             :         }
    1739             : 
    1740             : 
    1741           0 :         if (fsp->fsp_flags.have_proc_fds) {
    1742           0 :                 int fd = fsp_get_pathref_fd(fsp);
    1743           0 :                 const char *p = NULL;
    1744             :                 char buf[PATH_MAX];
    1745             : 
    1746           0 :                 p = sys_proc_fd_path(fd, buf, sizeof(buf));
    1747           0 :                 if (p == NULL) {
    1748           0 :                         return -1;
    1749             :                 }
    1750             : 
    1751           0 :                 rc = gpfswrap_set_times_path(buf, flags, gpfs_times);
    1752           0 :                 if (rc != 0) {
    1753           0 :                         DBG_WARNING("gpfs_set_times_path(%s,%s) failed: %s\n",
    1754             :                                     fsp_str_dbg(fsp), p, strerror(errno));
    1755             :                 }
    1756           0 :                 return rc;
    1757             :         }
    1758             : 
    1759             :         /*
    1760             :          * This is no longer a handle based call.
    1761             :          */
    1762             : 
    1763           0 :         rc = gpfswrap_set_times_path(fsp->fsp_name->base_name,
    1764             :                                      flags,
    1765             :                                      gpfs_times);
    1766           0 :         if (rc != 0) {
    1767           0 :                 DBG_WARNING("gpfs_set_times_path(%s) failed: %s\n",
    1768             :                             fsp_str_dbg(fsp), strerror(errno));
    1769             :         }
    1770           0 :         return rc;
    1771             : }
    1772             : 
    1773           0 : 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           0 :         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           0 :         if (config->settimes) {
    1789           0 :                 return smbd_gpfs_set_times(fsp, ft);
    1790             :         }
    1791             : 
    1792           0 :         DBG_DEBUG("gpfs_set_times() not available or disabled, "
    1793             :                   "use ntimes and winattr\n");
    1794             : 
    1795           0 :         ret = SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
    1796           0 :         if (ret == -1) {
    1797             :                 /* don't complain if access was denied */
    1798           0 :                 if (errno != EPERM && errno != EACCES) {
    1799           0 :                         DBG_WARNING("SMB_VFS_NEXT_FNTIMES failed: %s",
    1800             :                                     strerror(errno));
    1801             :                 }
    1802           0 :                 return -1;
    1803             :         }
    1804             : 
    1805           0 :         if (is_omit_timespec(&ft->create_time)) {
    1806           0 :                 DBG_DEBUG("Create Time is NULL\n");
    1807           0 :                 return 0;
    1808             :         }
    1809             : 
    1810           0 :         if (!config->winattr) {
    1811           0 :                 return 0;
    1812             :         }
    1813             : 
    1814           0 :         attrs.winAttrs = 0;
    1815           0 :         attrs.creationTime.tv_sec = ft->create_time.tv_sec;
    1816           0 :         attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
    1817             : 
    1818           0 :         if (!fsp->fsp_flags.is_pathref) {
    1819           0 :                 ret = gpfswrap_set_winattrs(fsp_get_io_fd(fsp),
    1820             :                                             GPFS_WINATTR_SET_CREATION_TIME,
    1821             :                                             &attrs);
    1822           0 :                 if (ret == -1 && errno != ENOSYS) {
    1823           0 :                         DBG_WARNING("Set GPFS ntimes failed %d\n", ret);
    1824           0 :                         return -1;
    1825             :                 }
    1826           0 :                 return ret;
    1827             :         }
    1828             : 
    1829           0 :         if (fsp->fsp_flags.have_proc_fds) {
    1830           0 :                 int fd = fsp_get_pathref_fd(fsp);
    1831           0 :                 const char *p = NULL;
    1832             :                 char buf[PATH_MAX];
    1833             : 
    1834           0 :                 p = sys_proc_fd_path(fd, buf, sizeof(buf));
    1835           0 :                 if (p == NULL) {
    1836           0 :                         return -1;
    1837             :                 }
    1838             : 
    1839           0 :                 ret = gpfswrap_set_winattrs_path(p,
    1840             :                                                  GPFS_WINATTR_SET_CREATION_TIME,
    1841             :                                                  &attrs);
    1842           0 :                 if (ret == -1 && errno != ENOSYS) {
    1843           0 :                         DBG_WARNING("Set GPFS ntimes failed %d\n", ret);
    1844           0 :                         return -1;
    1845             :                 }
    1846           0 :                 return ret;
    1847             :         }
    1848             : 
    1849             :         /*
    1850             :          * This is no longer a handle based call.
    1851             :          */
    1852           0 :         ret = gpfswrap_set_winattrs_path(fsp->fsp_name->base_name,
    1853             :                                          GPFS_WINATTR_SET_CREATION_TIME,
    1854             :                                          &attrs);
    1855           0 :         if (ret == -1 && errno != ENOSYS) {
    1856           0 :                 DBG_WARNING("Set GPFS ntimes failed %d\n", ret);
    1857           0 :                 return -1;
    1858             :         }
    1859             : 
    1860           0 :         return 0;
    1861             : }
    1862             : 
    1863           0 : 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           0 :         if (mode == (VFS_FALLOCATE_FL_PUNCH_HOLE|VFS_FALLOCATE_FL_KEEP_SIZE) &&
    1868           0 :             !fsp->fsp_flags.is_sparse &&
    1869           0 :             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           0 :                 errno = ENOTSUP;
    1879           0 :                 return -1;
    1880             :         }
    1881             : 
    1882           0 :         return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
    1883             : }
    1884             : 
    1885           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1892             :                                 struct gpfs_config_data,
    1893             :                                 return -1);
    1894             : 
    1895           0 :         if (!config->ftruncate) {
    1896           0 :                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
    1897             :         }
    1898             : 
    1899           0 :         result = gpfswrap_ftruncate(fsp_get_io_fd(fsp), len);
    1900           0 :         if ((result == -1) && (errno == ENOSYS)) {
    1901           0 :                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
    1902             :         }
    1903           0 :         return result;
    1904             : }
    1905             : 
    1906           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    1915             :                                 struct gpfs_config_data,
    1916             :                                 return false);
    1917             : 
    1918           0 :         if (!config->winattr) {
    1919           0 :                 return false;
    1920             :         }
    1921             : 
    1922           0 :         ret = gpfswrap_get_winattrs_path(fname->base_name, &attrs);
    1923           0 :         if (ret == -1) {
    1924           0 :                 return false;
    1925             :         }
    1926             : 
    1927           0 :         if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
    1928           0 :                 DBG_DEBUG("%s is offline\n", fname->base_name);
    1929           0 :                 return true;
    1930             :         }
    1931             : 
    1932           0 :         DBG_DEBUG("%s is online\n", fname->base_name);
    1933           0 :         return false;
    1934             : }
    1935             : 
    1936           0 : 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           0 :         ext = VFS_FETCH_FSP_EXTENSION(handle, fsp);
    1942           0 :         if (ext == NULL) {
    1943             :                 /*
    1944             :                  * Something bad happened, always ask.
    1945             :                  */
    1946           0 :                 return vfs_gpfs_is_offline(handle, fsp->fsp_name,
    1947           0 :                                            &fsp->fsp_name->st);
    1948             :         }
    1949             : 
    1950           0 :         if (ext->offline) {
    1951             :                 /*
    1952             :                  * As long as it's offline, ask.
    1953             :                  */
    1954           0 :                 ext->offline = vfs_gpfs_is_offline(handle, fsp->fsp_name,
    1955           0 :                                                    &fsp->fsp_name->st);
    1956             :         }
    1957             : 
    1958           0 :         return ext->offline;
    1959             : }
    1960             : 
    1961           0 : static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
    1962             :                                struct files_struct *fsp)
    1963             : {
    1964           0 :         return vfs_gpfs_fsp_is_offline(handle, fsp);
    1965             : }
    1966             : 
    1967           0 : 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           0 :         if (vfs_gpfs_fsp_is_offline(handle, fsp)) {
    1972           0 :                 errno = ENOSYS;
    1973           0 :                 return -1;
    1974             :         }
    1975           0 :         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
    1976             : }
    1977             : 
    1978             : #ifdef O_PATH
    1979           0 : static int vfs_gpfs_check_pathref_fstat_x(struct gpfs_config_data *config,
    1980             :                                           struct connection_struct *conn)
    1981             : {
    1982           0 :         struct gpfs_iattr64 iattr = {0};
    1983             :         unsigned int litemask;
    1984             :         int saved_errno;
    1985             :         int fd;
    1986             :         int ret;
    1987             : 
    1988           0 :         fd = open(conn->connectpath, O_PATH);
    1989           0 :         if (fd == -1) {
    1990           0 :                 DBG_ERR("openat() of share with O_PATH failed: %s\n",
    1991             :                         strerror(errno));
    1992           0 :                 return -1;
    1993             :         }
    1994             : 
    1995           0 :         ret = gpfswrap_fstat_x(fd, &litemask, &iattr, sizeof(iattr));
    1996           0 :         if (ret == 0) {
    1997           0 :                 close(fd);
    1998           0 :                 config->pathref_ok.gpfs_fstat_x = true;
    1999           0 :                 return 0;
    2000             :         }
    2001             : 
    2002           0 :         saved_errno = errno;
    2003           0 :         ret = close(fd);
    2004           0 :         if (ret != 0) {
    2005           0 :                 DBG_ERR("close failed: %s\n", strerror(errno));
    2006           0 :                 return -1;
    2007             :         }
    2008             : 
    2009           0 :         if (saved_errno != EBADF) {
    2010           0 :                 DBG_ERR("gpfswrap_fstat_x() of O_PATH handle failed: %s\n",
    2011             :                         strerror(saved_errno));
    2012           0 :                 return -1;
    2013             :         }
    2014             : 
    2015           0 :         return 0;
    2016             : }
    2017             : #endif
    2018             : 
    2019           0 : 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           0 :         ret = vfs_gpfs_check_pathref_fstat_x(config, conn);
    2032           0 :         if (ret != 0) {
    2033           0 :                 return -1;
    2034             :         }
    2035             : 
    2036           0 :         return 0;
    2037             : #endif
    2038             : }
    2039             : 
    2040           0 : 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           0 :         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
    2048           0 :         if (ret < 0) {
    2049           0 :                 return ret;
    2050             :         }
    2051             : 
    2052           0 :         if (IS_IPC(handle->conn)) {
    2053           0 :                 return 0;
    2054             :         }
    2055             : 
    2056           0 :         gpfswrap_lib_init(0);
    2057             : 
    2058           0 :         config = talloc_zero(handle->conn, struct gpfs_config_data);
    2059           0 :         if (!config) {
    2060           0 :                 DEBUG(0, ("talloc_zero() failed\n"));
    2061           0 :                 errno = ENOMEM;
    2062           0 :                 return -1;
    2063             :         }
    2064             : 
    2065           0 :         check_fstype = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2066             :                                     "check_fstype", true);
    2067             : 
    2068           0 :         if (check_fstype) {
    2069           0 :                 const char *connectpath = handle->conn->connectpath;
    2070           0 :                 struct statfs buf = { 0 };
    2071             : 
    2072           0 :                 ret = statfs(connectpath, &buf);
    2073           0 :                 if (ret != 0) {
    2074           0 :                         DBG_ERR("statfs failed for share %s at path %s: %s\n",
    2075             :                                 service, connectpath, strerror(errno));
    2076           0 :                         TALLOC_FREE(config);
    2077           0 :                         return ret;
    2078             :                 }
    2079             : 
    2080           0 :                 if (buf.f_type != GPFS_SUPER_MAGIC) {
    2081           0 :                         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           0 :                         errno = EINVAL;
    2087           0 :                         TALLOC_FREE(config);
    2088           0 :                         return -1;
    2089             :                 }
    2090             :         }
    2091             : 
    2092           0 :         ret = smbacl4_get_vfs_params(handle->conn, &config->nfs4_params);
    2093           0 :         if (ret < 0) {
    2094           0 :                 TALLOC_FREE(config);
    2095           0 :                 return ret;
    2096             :         }
    2097             : 
    2098           0 :         config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2099             :                                         "sharemodes", true);
    2100             : 
    2101           0 :         config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2102             :                                         "leases", true);
    2103             : 
    2104           0 :         config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2105             :                                    "hsm", false);
    2106             : 
    2107           0 :         config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2108             :                                       "syncio", false);
    2109             : 
    2110           0 :         config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2111             :                                        "winattr", false);
    2112             : 
    2113           0 :         config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2114             :                                          "ftruncate", true);
    2115             : 
    2116           0 :         config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2117             :                                                "getrealfilename", true);
    2118             : 
    2119           0 :         config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2120             :                                           "dfreequota", false);
    2121             : 
    2122           0 :         config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
    2123             : 
    2124           0 :         config->settimes = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2125             :                                         "settimes", true);
    2126           0 :         config->recalls = lp_parm_bool(SNUM(handle->conn), "gpfs",
    2127             :                                        "recalls", true);
    2128             : 
    2129           0 :         ret = vfs_gpfs_check_pathref(config, handle->conn);
    2130           0 :         if (ret != 0) {
    2131           0 :                 DBG_ERR("vfs_gpfs_check_pathref() on [%s] failed\n",
    2132             :                         handle->conn->connectpath);
    2133           0 :                 TALLOC_FREE(config);
    2134           0 :                 return -1;
    2135             :         }
    2136             : 
    2137           0 :         SMB_VFS_HANDLE_SET_DATA(handle, config,
    2138             :                                 NULL, struct gpfs_config_data,
    2139             :                                 return -1);
    2140             : 
    2141           0 :         if (config->leases) {
    2142             :                 /*
    2143             :                  * GPFS lease code is based on kernel oplock code
    2144             :                  * so make sure it is turned on
    2145             :                  */
    2146           0 :                 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
    2147           0 :                         DEBUG(5, ("Enabling kernel oplocks for "
    2148             :                                   "gpfs:leases to work\n"));
    2149           0 :                         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           0 :                 if (lp_level2_oplocks(SNUM(handle->conn))) {
    2159           0 :                         DEBUG(5, ("gpfs:leases are enabled, disabling "
    2160             :                                   "Level II oplocks\n"));
    2161           0 :                         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           0 :         lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
    2171             : 
    2172           0 :         return 0;
    2173             : }
    2174             : 
    2175           0 : static int get_gpfs_quota(const char *pathname, int type, int id,
    2176             :                           struct gpfs_quotaInfo *qi)
    2177             : {
    2178             :         int ret;
    2179             : 
    2180           0 :         ret = gpfswrap_quotactl(pathname, GPFS_QCMD(Q_GETQUOTA, type), id, qi);
    2181             : 
    2182           0 :         if (ret) {
    2183           0 :                 if (errno == GPFS_E_NO_QUOTA_INST) {
    2184           0 :                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
    2185           0 :                 } else if (errno != ENOSYS) {
    2186           0 :                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
    2187             :                                   "errno %d.\n", type, id, errno));
    2188             :                 }
    2189             : 
    2190           0 :                 return ret;
    2191             :         }
    2192             : 
    2193           0 :         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           0 :         return ret;
    2198             : }
    2199             : 
    2200           0 : 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           0 :         usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
    2211           0 :         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           0 :         if (qi.blockSoftLimit &&
    2218           0 :             qi.blockGraceTime && cur_time > qi.blockGraceTime) {
    2219             :                 /* report disk as full */
    2220           0 :                 *dfree = 0;
    2221           0 :                 *dsize = MIN(*dsize, usage);
    2222             :         }
    2223             : 
    2224           0 :         if (!qi.blockHardLimit)
    2225           0 :                 return;
    2226             : 
    2227           0 :         if (usage >= limit) {
    2228             :                 /* report disk as full */
    2229           0 :                 *dfree = 0;
    2230           0 :                 *dsize = MIN(*dsize, usage);
    2231             : 
    2232             :         } else {
    2233             :                 /* limit has not been reached, determine "free space" */
    2234           0 :                 *dfree = MIN(*dfree, limit - usage);
    2235           0 :                 *dsize = MIN(*dsize, limit);
    2236             :         }
    2237             : }
    2238             : 
    2239           0 : 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           0 :         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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
    2252             :                                 return (uint64_t)-1);
    2253           0 :         if (!config->dfreequota) {
    2254           0 :                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
    2255             :                                               bsize, dfree, dsize);
    2256             :         }
    2257             : 
    2258           0 :         err = sys_fsusage(smb_fname->base_name, dfree, dsize);
    2259           0 :         if (err) {
    2260           0 :                 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
    2261           0 :                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
    2262             :                                               bsize, dfree, dsize);
    2263             :         }
    2264             : 
    2265             :         /* sys_fsusage returns units of 512 bytes */
    2266           0 :         *bsize = 512;
    2267             : 
    2268           0 :         DEBUG(10, ("fs dfree %llu, dsize %llu\n",
    2269             :                    (unsigned long long)*dfree, (unsigned long long)*dsize));
    2270             : 
    2271           0 :         utok = handle->conn->session_info->unix_token;
    2272             : 
    2273           0 :         err = get_gpfs_quota(smb_fname->base_name,
    2274           0 :                         GPFS_USRQUOTA, utok->uid, &qi_user);
    2275           0 :         if (err) {
    2276           0 :                 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           0 :         if (VALID_STAT(smb_fname->st) &&
    2286           0 :             S_ISDIR(smb_fname->st.st_ex_mode) &&
    2287           0 :             smb_fname->st.st_ex_mode & S_ISGID) {
    2288           0 :                 become_root();
    2289           0 :                 err = get_gpfs_quota(smb_fname->base_name, GPFS_GRPQUOTA,
    2290           0 :                                      smb_fname->st.st_ex_gid, &qi_group);
    2291           0 :                 unbecome_root();
    2292             : 
    2293             :         } else {
    2294           0 :                 err = get_gpfs_quota(smb_fname->base_name, GPFS_GRPQUOTA,
    2295           0 :                                      utok->gid, &qi_group);
    2296             :         }
    2297             : 
    2298           0 :         if (err) {
    2299           0 :                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
    2300             :                                               bsize, dfree, dsize);
    2301             :         }
    2302             : 
    2303           0 :         cur_time = time(NULL);
    2304             : 
    2305             :         /* Adjust free space and size according to quota limits. */
    2306           0 :         vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
    2307           0 :         vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
    2308             : 
    2309           0 :         return *dfree / 2;
    2310             : }
    2311             : 
    2312           0 : 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           0 :         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           0 :                 case SMB_USER_QUOTA_TYPE:
    2330             :                 case SMB_GROUP_QUOTA_TYPE:
    2331           0 :                         errno = ENOSYS;
    2332           0 :                         return -1;
    2333           0 :                 default:
    2334           0 :                         return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname,
    2335             :                                         qtype, id, dq);
    2336             :         }
    2337             : }
    2338             : 
    2339           0 : 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           0 :         next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
    2346             : 
    2347           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    2348             :                                 struct gpfs_config_data,
    2349             :                                 return next);
    2350             : 
    2351           0 :         if (config->hsm) {
    2352           0 :                 next |= FILE_SUPPORTS_REMOTE_STORAGE;
    2353             :         }
    2354           0 :         return next;
    2355             : }
    2356             : 
    2357           0 : 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           0 :         struct gpfs_config_data *config = NULL;
    2365           0 :         struct gpfs_fsp_extension *ext = NULL;
    2366             :         int ret;
    2367             : 
    2368           0 :         SMB_VFS_HANDLE_GET_DATA(handle, config,
    2369             :                                 struct gpfs_config_data,
    2370             :                                 return -1);
    2371             : 
    2372           0 :         if (config->hsm && !config->recalls &&
    2373           0 :             vfs_gpfs_fsp_is_offline(handle, fsp))
    2374             :         {
    2375           0 :                 DBG_DEBUG("Refusing access to offline file %s\n",
    2376             :                           fsp_str_dbg(fsp));
    2377           0 :                 errno = EACCES;
    2378           0 :                 return -1;
    2379             :         }
    2380             : 
    2381           0 :         if (config->syncio) {
    2382           0 :                 flags |= O_SYNC;
    2383             :         }
    2384             : 
    2385           0 :         ext = VFS_ADD_FSP_EXTENSION(handle, fsp, struct gpfs_fsp_extension,
    2386             :                                     NULL);
    2387           0 :         if (ext == NULL) {
    2388           0 :                 errno = ENOMEM;
    2389           0 :                 return -1;
    2390             :         }
    2391             : 
    2392             :         /*
    2393             :          * Assume the file is offline until gpfs tells us it's online.
    2394             :          */
    2395           0 :         *ext = (struct gpfs_fsp_extension) { .offline = true };
    2396             : 
    2397           0 :         ret = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
    2398           0 :         if (ret == -1) {
    2399           0 :                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
    2400             :         }
    2401           0 :         return ret;
    2402             : }
    2403             : 
    2404           0 : 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           0 :         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
    2411             : 
    2412           0 :         ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
    2413             : 
    2414           0 :         if ((ret != -1) && was_offline) {
    2415           0 :                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
    2416             :                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
    2417           0 :                              fsp->fsp_name->base_name);
    2418             :         }
    2419             : 
    2420           0 :         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           0 : 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           0 :         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
    2443           0 :         if (req == NULL) {
    2444           0 :                 return NULL;
    2445             :         }
    2446           0 :         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
    2447           0 :         state->fsp = fsp;
    2448           0 :         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
    2449             :                                          n, offset);
    2450           0 :         if (tevent_req_nomem(subreq, req)) {
    2451           0 :                 return tevent_req_post(req, ev);
    2452             :         }
    2453           0 :         tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
    2454           0 :         return req;
    2455             : }
    2456             : 
    2457           0 : static void vfs_gpfs_pread_done(struct tevent_req *subreq)
    2458             : {
    2459           0 :         struct tevent_req *req = tevent_req_callback_data(
    2460             :                 subreq, struct tevent_req);
    2461           0 :         struct vfs_gpfs_pread_state *state = tevent_req_data(
    2462             :                 req, struct vfs_gpfs_pread_state);
    2463             : 
    2464           0 :         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
    2465           0 :         TALLOC_FREE(subreq);
    2466           0 :         tevent_req_done(req);
    2467           0 : }
    2468             : 
    2469           0 : static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req,
    2470             :                                    struct vfs_aio_state *vfs_aio_state)
    2471             : {
    2472           0 :         struct vfs_gpfs_pread_state *state = tevent_req_data(
    2473             :                 req, struct vfs_gpfs_pread_state);
    2474           0 :         struct files_struct *fsp = state->fsp;
    2475             : 
    2476           0 :         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
    2477           0 :                 return -1;
    2478             :         }
    2479           0 :         *vfs_aio_state = state->vfs_aio_state;
    2480             : 
    2481           0 :         if ((state->ret != -1) && state->was_offline) {
    2482           0 :                 DEBUG(10, ("sending notify\n"));
    2483           0 :                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
    2484             :                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
    2485           0 :                              fsp->fsp_name->base_name);
    2486             :         }
    2487             : 
    2488           0 :         return state->ret;
    2489             : }
    2490             : 
    2491           0 : 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           0 :         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
    2498             : 
    2499           0 :         ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
    2500             : 
    2501           0 :         if ((ret != -1) && was_offline) {
    2502           0 :                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
    2503             :                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
    2504           0 :                              fsp->fsp_name->base_name);
    2505             :         }
    2506             : 
    2507           0 :         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           0 : 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           0 :         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
    2531           0 :         if (req == NULL) {
    2532           0 :                 return NULL;
    2533             :         }
    2534           0 :         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
    2535           0 :         state->fsp = fsp;
    2536           0 :         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
    2537             :                                          n, offset);
    2538           0 :         if (tevent_req_nomem(subreq, req)) {
    2539           0 :                 return tevent_req_post(req, ev);
    2540             :         }
    2541           0 :         tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
    2542           0 :         return req;
    2543             : }
    2544             : 
    2545           0 : static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
    2546             : {
    2547           0 :         struct tevent_req *req = tevent_req_callback_data(
    2548             :                 subreq, struct tevent_req);
    2549           0 :         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
    2550             :                 req, struct vfs_gpfs_pwrite_state);
    2551             : 
    2552           0 :         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
    2553           0 :         TALLOC_FREE(subreq);
    2554           0 :         tevent_req_done(req);
    2555           0 : }
    2556             : 
    2557           0 : static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req,
    2558             :                                     struct vfs_aio_state *vfs_aio_state)
    2559             : {
    2560           0 :         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
    2561             :                 req, struct vfs_gpfs_pwrite_state);
    2562           0 :         struct files_struct *fsp = state->fsp;
    2563             : 
    2564           0 :         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
    2565           0 :                 return -1;
    2566             :         }
    2567           0 :         *vfs_aio_state = state->vfs_aio_state;
    2568             : 
    2569           0 :         if ((state->ret != -1) && state->was_offline) {
    2570           0 :                 DEBUG(10, ("sending notify\n"));
    2571           0 :                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
    2572             :                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
    2573           0 :                              fsp->fsp_name->base_name);
    2574             :         }
    2575             : 
    2576           0 :         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          20 : NTSTATUS vfs_gpfs_init(TALLOC_CTX *ctx)
    2618             : {
    2619             :         int ret;
    2620             : 
    2621          20 :         ret = gpfswrap_init();
    2622          20 :         if (ret != 0) {
    2623          20 :                 DEBUG(1, ("Could not initialize GPFS library wrapper\n"));
    2624             :         }
    2625             : 
    2626          20 :         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
    2627             :                                 &vfs_gpfs_fns);
    2628             : }

Generated by: LCOV version 1.13