s3-vfs_gpfs: Fix bug #9003, posix acl on gpfs
[ddiss/samba.git] / source3 / modules / vfs_gpfs.c
1 /*
2    Unix SMB/CIFS implementation.
3    Wrap gpfs calls in vfs functions.
4
5    Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
6
7    Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
8                             and Gomati Mohanan <gomati.mohanan@in.ibm.com>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
27 #include "include/smbprofile.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
31
32 #include <gpfs_gpl.h>
33 #include "nfs4_acls.h"
34 #include "vfs_gpfs.h"
35 #include "system/filesys.h"
36 #include "auth.h"
37
38 struct gpfs_config_data {
39         bool sharemodes;
40         bool leases;
41         bool hsm;
42         bool syncio;
43         bool winattr;
44         bool ftruncate;
45         bool getrealfilename;
46         bool dfreequota;
47         bool prealloc;
48         bool acl;
49 };
50
51
52 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp, 
53                                  uint32 share_mode, uint32 access_mask)
54 {
55
56         struct gpfs_config_data *config;
57         int ret = 0;
58
59         SMB_VFS_HANDLE_GET_DATA(handle, config,
60                                 struct gpfs_config_data,
61                                 return -1);
62
63         START_PROFILE(syscall_kernel_flock);
64
65         kernel_flock(fsp->fh->fd, share_mode, access_mask);
66
67         if (config->sharemodes
68                 && !set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
69                 ret = -1;
70         }
71
72         END_PROFILE(syscall_kernel_flock);
73
74         return ret;
75 }
76
77 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
78 {
79
80         struct gpfs_config_data *config;
81
82         SMB_VFS_HANDLE_GET_DATA(handle, config,
83                                 struct gpfs_config_data,
84                                 return -1);
85
86         if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
87                 set_gpfs_sharemode(fsp, 0, 0);
88         }
89
90         return SMB_VFS_NEXT_CLOSE(handle, fsp);
91 }
92
93 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp, 
94                              int leasetype)
95 {
96         struct gpfs_config_data *config;
97         int ret=0;
98
99         SMB_VFS_HANDLE_GET_DATA(handle, config,
100                                 struct gpfs_config_data,
101                                 return -1);
102
103         if (linux_set_lease_sighandler(fsp->fh->fd) == -1)
104                 return -1;
105
106         START_PROFILE(syscall_linux_setlease);
107
108         if (config->leases) {
109                 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
110         }
111
112         END_PROFILE(syscall_linux_setlease);
113
114         return ret;
115 }
116
117 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
118                                       const char *path,
119                                       const char *name,
120                                       TALLOC_CTX *mem_ctx,
121                                       char **found_name)
122 {
123         int result;
124         char *full_path;
125         char real_pathname[PATH_MAX+1];
126         int buflen;
127         bool mangled;
128         struct gpfs_config_data *config;
129
130         SMB_VFS_HANDLE_GET_DATA(handle, config,
131                                 struct gpfs_config_data,
132                                 return -1);
133
134         if (!config->getrealfilename) {
135                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
136                                                       mem_ctx, found_name);
137         }
138
139         mangled = mangle_is_mangled(name, handle->conn->params);
140         if (mangled) {
141                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
142                                                       mem_ctx, found_name);
143         }
144
145         full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
146         if (full_path == NULL) {
147                 errno = ENOMEM;
148                 return -1;
149         }
150
151         buflen = sizeof(real_pathname) - 1;
152
153         result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
154                                                  &buflen);
155
156         TALLOC_FREE(full_path);
157
158         if ((result == -1) && (errno == ENOSYS)) {
159                 return SMB_VFS_NEXT_GET_REAL_FILENAME(
160                         handle, path, name, mem_ctx, found_name);
161         }
162
163         if (result == -1) {
164                 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
165                            strerror(errno)));
166                 return -1;
167         }
168
169         /*
170          * GPFS does not necessarily null-terminate the returned path
171          * but instead returns the buffer length in buflen.
172          */
173
174         if (buflen < sizeof(real_pathname)) {
175                 real_pathname[buflen] = '\0';
176         } else {
177                 real_pathname[sizeof(real_pathname)-1] = '\0';
178         }
179
180         DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
181                    path, name, real_pathname));
182
183         name = strrchr_m(real_pathname, '/');
184         if (name == NULL) {
185                 errno = ENOENT;
186                 return -1;
187         }
188
189         *found_name = talloc_strdup(mem_ctx, name+1);
190         if (*found_name == NULL) {
191                 errno = ENOMEM;
192                 return -1;
193         }
194
195         return 0;
196 }
197
198 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
199 {
200         gpfs_aclCount_t i;
201         if (gacl==NULL)
202         {
203                 DEBUG(0, ("gpfs acl is NULL\n"));
204                 return;
205         }
206
207         DEBUG(level, ("gpfs acl: nace: %d, type:%d, version:%d, level:%d, len:%d\n",
208                 gacl->acl_nace, gacl->acl_type, gacl->acl_version, gacl->acl_level, gacl->acl_len));
209         for(i=0; i<gacl->acl_nace; i++)
210         {
211                 struct gpfs_ace_v4 *gace = gacl->ace_v4 + i;
212                 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, iflags:0x%x, who:%u\n",
213                         i, gace->aceType, gace->aceFlags, gace->aceMask,
214                         gace->aceIFlags, gace->aceWho));
215         }
216 }
217
218 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
219 {
220         struct gpfs_acl *acl;
221         size_t len = 200;
222         int ret;
223         TALLOC_CTX *mem_ctx = talloc_tos();
224
225         acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
226         if (acl == NULL) {
227                 errno = ENOMEM;
228                 return NULL;
229         }
230
231         acl->acl_len = len;
232         acl->acl_level = 0;
233         acl->acl_version = 0;
234         acl->acl_type = type;
235
236         ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
237         if ((ret != 0) && (errno == ENOSPC)) {
238                 struct gpfs_acl *new_acl = (struct gpfs_acl *)TALLOC_SIZE(
239                         mem_ctx, acl->acl_len + sizeof(struct gpfs_acl));
240                 if (new_acl == NULL) {
241                         errno = ENOMEM;
242                         return NULL;
243                 }
244
245                 new_acl->acl_len = acl->acl_len;
246                 new_acl->acl_level = acl->acl_level;
247                 new_acl->acl_version = acl->acl_version;
248                 new_acl->acl_type = acl->acl_type;
249                 acl = new_acl;
250
251                 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
252         }
253         if (ret != 0)
254         {
255                 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
256                 return NULL;
257         }
258
259         return acl;
260 }
261
262 /* Tries to get nfs4 acls and returns SMB ACL allocated.
263  * On failure returns 1 if it got non-NFSv4 ACL to prompt 
264  * retry with POSIX ACL checks.
265  * On failure returns -1 if there is system (GPFS) error, check errno.
266  * Returns 0 on success
267  */
268 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
269 {
270         gpfs_aclCount_t i;
271         struct gpfs_acl *gacl = NULL;
272         DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
273
274         /* First get the real acl length */
275         gacl = gpfs_getacl_alloc(fname, 0);
276         if (gacl == NULL) {
277                 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
278                            fname, strerror(errno)));
279                 return -1;
280         }
281
282         if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
283                 DEBUG(10, ("Got non-nfsv4 acl\n"));
284                 /* Retry with POSIX ACLs check */
285                 return 1;
286         }
287
288         *ppacl = smb_create_smb4acl();
289
290         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
291                    gacl->acl_len, gacl->acl_level, gacl->acl_version,
292                    gacl->acl_nace));
293
294         for (i=0; i<gacl->acl_nace; i++) {
295                 struct gpfs_ace_v4 *gace = &gacl->ace_v4[i];
296                 SMB_ACE4PROP_T smbace;
297                 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
298                            "who: %d\n", gace->aceType, gace->aceIFlags,
299                            gace->aceFlags, gace->aceMask, gace->aceWho));
300
301                 ZERO_STRUCT(smbace);
302                 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
303                         smbace.flags |= SMB_ACE4_ID_SPECIAL;
304                         switch (gace->aceWho) {
305                         case ACE4_SPECIAL_OWNER:
306                                 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
307                                 break;
308                         case ACE4_SPECIAL_GROUP:
309                                 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
310                                 break;
311                         case ACE4_SPECIAL_EVERYONE:
312                                 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
313                                 break;
314                         default:
315                                 DEBUG(8, ("invalid special gpfs id %d "
316                                           "ignored\n", gace->aceWho));
317                                 continue; /* don't add it */
318                         }
319                 } else {
320                         if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
321                                 smbace.who.gid = gace->aceWho;
322                         else
323                                 smbace.who.uid = gace->aceWho;
324                 }
325
326                 /* remove redundent deny entries */
327                 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
328                         struct gpfs_ace_v4 *prev = &gacl->ace_v4[i-1];
329                         if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
330                             prev->aceFlags == gace->aceFlags &&
331                             prev->aceIFlags == gace->aceIFlags &&
332                             (gace->aceMask & prev->aceMask) == 0 &&
333                             gace->aceWho == prev->aceWho) {
334                                 /* its redundent - skip it */
335                                 continue;
336                         }                                                
337                 }
338
339                 smbace.aceType = gace->aceType;
340                 smbace.aceFlags = gace->aceFlags;
341                 smbace.aceMask = gace->aceMask;
342                 smb_add_ace4(*ppacl, &smbace);
343         }
344
345         return 0;
346 }
347
348 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
349         files_struct *fsp, uint32 security_info,
350         struct security_descriptor **ppdesc)
351 {
352         SMB4ACL_T *pacl = NULL;
353         int     result;
354         struct gpfs_config_data *config;
355
356         *ppdesc = NULL;
357
358         SMB_VFS_HANDLE_GET_DATA(handle, config,
359                                 struct gpfs_config_data,
360                                 return NT_STATUS_INTERNAL_ERROR);
361
362         if (!config->acl) {
363                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info, ppdesc);
364         }
365
366         result = gpfs_get_nfs4_acl(fsp->fsp_name->base_name, &pacl);
367
368         if (result == 0)
369                 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
370
371         if (result > 0) {
372                 DEBUG(10, ("retrying with posix acl...\n"));
373                 return posix_fget_nt_acl(fsp, security_info, ppdesc);
374         }
375
376         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
377         return map_nt_error_from_unix(errno);
378 }
379
380 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
381         const char *name,
382         uint32 security_info, struct security_descriptor **ppdesc)
383 {
384         SMB4ACL_T *pacl = NULL;
385         int     result;
386         struct gpfs_config_data *config;
387
388         *ppdesc = NULL;
389
390         SMB_VFS_HANDLE_GET_DATA(handle, config,
391                                 struct gpfs_config_data,
392                                 return NT_STATUS_INTERNAL_ERROR);
393
394         if (!config->acl) {
395                 return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info, ppdesc);
396         }
397
398         result = gpfs_get_nfs4_acl(name, &pacl);
399
400         if (result == 0)
401                 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
402
403         if (result > 0) {
404                 DEBUG(10, ("retrying with posix acl...\n"));
405                 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
406         }
407
408         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
409         return map_nt_error_from_unix(errno);
410 }
411
412 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
413 {
414         int ret;
415         gpfs_aclLen_t gacl_len;
416         SMB4ACE_T       *smbace;
417         struct gpfs_acl *gacl;
418         TALLOC_CTX *mem_ctx  = talloc_tos();
419
420         gacl_len = sizeof(struct gpfs_acl) +
421                 (smb_get_naces(smbacl)-1)*sizeof(gpfs_ace_v4_t);
422
423         gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
424         if (gacl == NULL) {
425                 DEBUG(0, ("talloc failed\n"));
426                 errno = ENOMEM;
427                 return False;
428         }
429
430         gacl->acl_len = gacl_len;
431         gacl->acl_level = 0;
432         gacl->acl_version = GPFS_ACL_VERSION_NFS4;
433         gacl->acl_type = GPFS_ACL_TYPE_NFS4;
434         gacl->acl_nace = 0; /* change later... */
435
436         for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
437                 struct gpfs_ace_v4 *gace = &gacl->ace_v4[gacl->acl_nace];
438                 SMB_ACE4PROP_T  *aceprop = smb_get_ace4(smbace);
439
440                 gace->aceType = aceprop->aceType;
441                 gace->aceFlags = aceprop->aceFlags;
442                 gace->aceMask = aceprop->aceMask;
443
444                 /*
445                  * GPFS can't distinguish between WRITE and APPEND on
446                  * files, so one being set without the other is an
447                  * error. Sorry for the many ()'s :-)
448                  */
449
450                 if (!fsp->is_directory
451                     &&
452                     ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
453                       && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
454                      ||
455                      (((gace->aceMask & ACE4_MASK_WRITE) != 0)
456                       && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
457                     &&
458                     lp_parm_bool(fsp->conn->params->service, "gpfs",
459                                  "merge_writeappend", True)) {
460                         DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
461                                   "WRITE^APPEND, setting WRITE|APPEND\n",
462                                   fsp_str_dbg(fsp)));
463                         gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
464                 }
465
466                 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
467
468                 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
469                 {
470                         switch(aceprop->who.special_id)
471                         {
472                         case SMB_ACE4_WHO_EVERYONE:
473                                 gace->aceWho = ACE4_SPECIAL_EVERYONE;
474                                 break;
475                         case SMB_ACE4_WHO_OWNER:
476                                 gace->aceWho = ACE4_SPECIAL_OWNER;
477                                 break;
478                         case SMB_ACE4_WHO_GROUP:
479                                 gace->aceWho = ACE4_SPECIAL_GROUP;
480                                 break;
481                         default:
482                                 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
483                                 continue; /* don't add it !!! */
484                         }
485                 } else {
486                         /* just only for the type safety... */
487                         if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
488                                 gace->aceWho = aceprop->who.gid;
489                         else
490                                 gace->aceWho = aceprop->who.uid;
491                 }
492
493                 gacl->acl_nace++;
494         }
495
496         ret = smbd_gpfs_putacl(fsp->fsp_name->base_name,
497                                GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
498         if (ret != 0) {
499                 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
500                 gpfs_dumpacl(8, gacl);
501                 return False;
502         }
503
504         DEBUG(10, ("gpfs_putacl succeeded\n"));
505         return True;
506 }
507
508 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
509 {
510         struct gpfs_acl *acl;
511         NTSTATUS result = NT_STATUS_ACCESS_DENIED;
512
513         acl = gpfs_getacl_alloc(fsp->fsp_name->base_name, 0);
514         if (acl == NULL)
515                 return result;
516
517         if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
518         {
519                 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
520                                  "refuse_dacl_protected", false)
521                     && (psd->type&SEC_DESC_DACL_PROTECTED)) {
522                         DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
523                         return NT_STATUS_NOT_SUPPORTED;
524                 }
525
526                 result = smb_set_nt_acl_nfs4(
527                         fsp, security_info_sent, psd,
528                         gpfsacl_process_smbacl);
529         } else { /* assume POSIX ACL - by default... */
530                 result = set_nt_acl(fsp, security_info_sent, psd);
531         }
532
533         return result;
534 }
535
536 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
537 {
538         struct gpfs_config_data *config;
539
540         SMB_VFS_HANDLE_GET_DATA(handle, config,
541                                 struct gpfs_config_data,
542                                 return NT_STATUS_INTERNAL_ERROR);
543
544         if (!config->acl) {
545                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
546         }
547
548         return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
549 }
550
551 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
552 {
553         SMB_ACL_T result;
554         gpfs_aclCount_t i;
555
556         result = sys_acl_init(pacl->acl_nace);
557         if (result == NULL) {
558                 errno = ENOMEM;
559                 return NULL;
560         }
561
562         result->count = pacl->acl_nace;
563
564         for (i=0; i<pacl->acl_nace; i++) {
565                 struct smb_acl_entry *ace = &result->acl[i];
566                 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
567
568                 DEBUG(10, ("Converting type %d id %lu perm %x\n",
569                            (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
570                            (int)g_ace->ace_perm));
571
572                 switch (g_ace->ace_type) {
573                 case GPFS_ACL_USER:
574                         ace->a_type = SMB_ACL_USER;
575                         ace->uid = (uid_t)g_ace->ace_who;
576                         break;
577                 case GPFS_ACL_USER_OBJ:
578                         ace->a_type = SMB_ACL_USER_OBJ;
579                         break;
580                 case GPFS_ACL_GROUP:
581                         ace->a_type = SMB_ACL_GROUP;
582                         ace->gid = (gid_t)g_ace->ace_who;
583                         break;
584                 case GPFS_ACL_GROUP_OBJ:
585                         ace->a_type = SMB_ACL_GROUP_OBJ;
586                         break;
587                 case GPFS_ACL_OTHER:
588                         ace->a_type = SMB_ACL_OTHER;
589                         break;
590                 case GPFS_ACL_MASK:
591                         ace->a_type = SMB_ACL_MASK;
592                         break;
593                 default:
594                         DEBUG(10, ("Got invalid ace_type: %d\n",
595                                    g_ace->ace_type));
596                         errno = EINVAL;
597                         SAFE_FREE(result);
598                         return NULL;
599                 }
600
601                 ace->a_perm = 0;
602                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
603                         SMB_ACL_READ : 0;
604                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
605                         SMB_ACL_WRITE : 0;
606                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
607                         SMB_ACL_EXECUTE : 0;
608
609                 DEBUGADD(10, ("Converted to %d perm %x\n",
610                               ace->a_type, ace->a_perm));
611         }
612
613         return result;
614 }
615
616 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
617 {
618         struct gpfs_acl *pacl;
619         SMB_ACL_T result = NULL;
620
621         pacl = gpfs_getacl_alloc(path, type);
622
623         if (pacl == NULL) {
624                 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
625                            path, strerror(errno)));
626                 if (errno == 0) {
627                         errno = EINVAL;
628                 }
629                 goto done;
630         }
631
632         if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
633                 DEBUG(10, ("Got acl version %d, expected %d\n",
634                            pacl->acl_version, GPFS_ACL_VERSION_POSIX));
635                 errno = EINVAL;
636                 goto done;
637         }
638
639         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
640                    pacl->acl_len, pacl->acl_level, pacl->acl_version,
641                    pacl->acl_nace));
642
643         result = gpfs2smb_acl(pacl);
644         if (result != NULL) {
645                 errno = 0;
646         }
647
648  done:
649
650         if (errno != 0) {
651                 SAFE_FREE(result);
652         }
653         return result;  
654 }
655
656 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
657                                           const char *path_p,
658                                           SMB_ACL_TYPE_T type)
659 {
660         gpfs_aclType_t gpfs_type;
661         struct gpfs_config_data *config;
662
663         SMB_VFS_HANDLE_GET_DATA(handle, config,
664                                 struct gpfs_config_data,
665                                 return NULL);
666
667         if (!config->acl) {
668                 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p, type);
669         }
670
671         switch(type) {
672         case SMB_ACL_TYPE_ACCESS:
673                 gpfs_type = GPFS_ACL_TYPE_ACCESS;
674                 break;
675         case SMB_ACL_TYPE_DEFAULT:
676                 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
677                 break;
678         default:
679                 DEBUG(0, ("Got invalid type: %d\n", type));
680                 smb_panic("exiting");
681         }
682
683         return gpfsacl_get_posix_acl(path_p, gpfs_type);
684 }
685
686 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
687                                         files_struct *fsp)
688 {
689         struct gpfs_config_data *config;
690
691         SMB_VFS_HANDLE_GET_DATA(handle, config,
692                                 struct gpfs_config_data,
693                                 return NULL);
694
695         if (!config->acl) {
696                 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp);
697         }
698
699         return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
700                                      GPFS_ACL_TYPE_ACCESS);
701 }
702
703 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
704                                      SMB_ACL_TYPE_T type)
705 {
706         gpfs_aclLen_t len;
707         struct gpfs_acl *result;
708         int i;
709         union gpfs_ace_union
710         {
711                 gpfs_ace_v1_t  ace_v1[1]; /* when GPFS_ACL_VERSION_POSIX */
712                 gpfs_ace_v4_t  ace_v4[1]; /* when GPFS_ACL_VERSION_NFS4  */
713         };
714
715         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
716
717         len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
718                 (pacl->count)*sizeof(gpfs_ace_v1_t);
719
720         result = (struct gpfs_acl *)SMB_MALLOC(len);
721         if (result == NULL) {
722                 errno = ENOMEM;
723                 return result;
724         }
725
726         result->acl_len = len;
727         result->acl_level = 0;
728         result->acl_version = GPFS_ACL_VERSION_POSIX;
729         result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
730                 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
731         result->acl_nace = pacl->count;
732
733         for (i=0; i<pacl->count; i++) {
734                 const struct smb_acl_entry *ace = &pacl->acl[i];
735                 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
736
737                 DEBUG(10, ("Converting type %d perm %x\n",
738                            (int)ace->a_type, (int)ace->a_perm));
739
740                 g_ace->ace_perm = 0;
741
742                 switch(ace->a_type) {
743                 case SMB_ACL_USER:
744                         g_ace->ace_type = GPFS_ACL_USER;
745                         g_ace->ace_who = (gpfs_uid_t)ace->uid;
746                         break;
747                 case SMB_ACL_USER_OBJ:
748                         g_ace->ace_type = GPFS_ACL_USER_OBJ;
749                         g_ace->ace_perm |= ACL_PERM_CONTROL;
750                         g_ace->ace_who = 0;
751                         break;
752                 case SMB_ACL_GROUP:
753                         g_ace->ace_type = GPFS_ACL_GROUP;
754                         g_ace->ace_who = (gpfs_uid_t)ace->gid;
755                         break;
756                 case SMB_ACL_GROUP_OBJ:
757                         g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
758                         g_ace->ace_who = 0;
759                         break;
760                 case SMB_ACL_MASK:
761                         g_ace->ace_type = GPFS_ACL_MASK;
762                         g_ace->ace_perm = 0x8f;
763                         g_ace->ace_who = 0;
764                         break;
765                 case SMB_ACL_OTHER:
766                         g_ace->ace_type = GPFS_ACL_OTHER;
767                         g_ace->ace_who = 0;
768                         break;
769                 default:
770                         DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
771                         errno = EINVAL;
772                         SAFE_FREE(result);
773                         return NULL;
774                 }
775
776                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
777                         ACL_PERM_READ : 0;
778                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
779                         ACL_PERM_WRITE : 0;
780                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
781                         ACL_PERM_EXECUTE : 0;
782
783                 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
784                               g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
785         }
786
787         return result;
788 }
789
790 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
791                                     const char *name,
792                                     SMB_ACL_TYPE_T type,
793                                     SMB_ACL_T theacl)
794 {
795         struct gpfs_acl *gpfs_acl;
796         int result;
797         struct gpfs_config_data *config;
798
799         SMB_VFS_HANDLE_GET_DATA(handle, config,
800                                 struct gpfs_config_data,
801                                 return -1);
802
803         if (!config->acl) {
804                 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, type, theacl);
805         }
806
807         gpfs_acl = smb2gpfs_acl(theacl, type);
808         if (gpfs_acl == NULL) {
809                 return -1;
810         }
811
812         result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
813
814         SAFE_FREE(gpfs_acl);
815         return result;
816 }
817
818 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
819                                   files_struct *fsp,
820                                   SMB_ACL_T theacl)
821 {
822         struct gpfs_config_data *config;
823
824         SMB_VFS_HANDLE_GET_DATA(handle, config,
825                                 struct gpfs_config_data,
826                                 return -1);
827
828         if (!config->acl) {
829                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
830         }
831
832         return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
833                                         SMB_ACL_TYPE_ACCESS, theacl);
834 }
835
836 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
837                                            const char *path)
838 {
839         struct gpfs_config_data *config;
840
841         SMB_VFS_HANDLE_GET_DATA(handle, config,
842                                 struct gpfs_config_data,
843                                 return -1);
844
845         if (!config->acl) {
846                 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
847         }
848
849         errno = ENOTSUP;
850         return -1;
851 }
852
853 /*
854  * Assumed: mode bits are shiftable and standard
855  * Output: the new aceMask field for an smb nfs4 ace
856  */
857 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
858 {
859         const uint32 posix_nfs4map[3] = {
860                 SMB_ACE4_EXECUTE, /* execute */
861                 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
862                 SMB_ACE4_READ_DATA /* read */
863         };
864         int     i;
865         uint32_t        posix_mask = 0x01;
866         uint32_t        posix_bit;
867         uint32_t        nfs4_bits;
868
869         for(i=0; i<3; i++) {
870                 nfs4_bits = posix_nfs4map[i];
871                 posix_bit = rwx & posix_mask;
872
873                 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
874                         if (posix_bit)
875                                 aceMask |= nfs4_bits;
876                         else
877                                 aceMask &= ~nfs4_bits;
878                 } else {
879                         /* add deny bits when suitable */
880                         if (!posix_bit)
881                                 aceMask |= nfs4_bits;
882                         else
883                                 aceMask &= ~nfs4_bits;
884                 } /* other ace types are unexpected */
885
886                 posix_mask <<= 1;
887         }
888
889         return aceMask;
890 }
891
892 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
893 {
894         SMB4ACL_T *pacl = NULL;
895         int     result;
896         bool    haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
897         int     i;
898         files_struct    fake_fsp; /* TODO: rationalize parametrization */
899         SMB4ACE_T       *smbace;
900         NTSTATUS status;
901
902         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
903
904         result = gpfs_get_nfs4_acl(path, &pacl);
905         if (result)
906                 return result;
907
908         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
909                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
910         }
911
912         for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
913                 SMB_ACE4PROP_T  *ace = smb_get_ace4(smbace);
914                 uint32_t        specid = ace->who.special_id;
915
916                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
917                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
918                     specid <= SMB_ACE4_WHO_EVERYONE) {
919
920                         uint32_t newMask;
921
922                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
923                                 haveAllowEntry[specid] = True;
924
925                         /* mode >> 6 for @owner, mode >> 3 for @group,
926                          * mode >> 0 for @everyone */
927                         newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
928                                                       mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
929                         if (ace->aceMask!=newMask) {
930                                 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
931                                            path, ace->aceMask, newMask, specid));
932                         }
933                         ace->aceMask = newMask;
934                 }
935         }
936
937         /* make sure we have at least ALLOW entries
938          * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
939          * - if necessary
940          */
941         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
942                 SMB_ACE4PROP_T  ace;
943
944                 if (haveAllowEntry[i]==True)
945                         continue;
946
947                 ZERO_STRUCT(ace);
948                 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
949                 ace.flags |= SMB_ACE4_ID_SPECIAL;
950                 ace.who.special_id = i;
951
952                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
953                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
954
955                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
956                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
957
958                 /* don't add unnecessary aces */
959                 if (!ace.aceMask)
960                         continue;
961
962                 /* we add it to the END - as windows expects allow aces */
963                 smb_add_ace4(pacl, &ace);
964                 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
965                            path, mode, i, ace.aceMask));
966         }
967
968         /* don't add complementary DENY ACEs here */
969         ZERO_STRUCT(fake_fsp);
970         status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
971                                             &fake_fsp.fsp_name);
972         if (!NT_STATUS_IS_OK(status)) {
973                 errno = map_errno_from_nt_status(status);
974                 return -1;
975         }
976         /* put the acl */
977         if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False) {
978                 TALLOC_FREE(fake_fsp.fsp_name);
979                 return -1;
980         }
981
982         TALLOC_FREE(fake_fsp.fsp_name);
983         return 0; /* ok for [f]chmod */
984 }
985
986 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
987 {
988         struct smb_filename *smb_fname_cpath;
989         int rc;
990         NTSTATUS status;
991
992         status = create_synthetic_smb_fname(
993                 talloc_tos(), path, NULL, NULL, &smb_fname_cpath);
994
995         if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
996                 return -1;
997         }
998
999         /* avoid chmod() if possible, to preserve acls */
1000         if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
1001                 return 0;
1002         }
1003
1004         rc = gpfsacl_emu_chmod(path, mode);
1005         if (rc == 1)
1006                 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1007         return rc;
1008 }
1009
1010 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1011 {
1012                  SMB_STRUCT_STAT st;
1013                  int rc;
1014
1015                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1016                          return -1;
1017                  }
1018
1019                  /* avoid chmod() if possible, to preserve acls */
1020                  if ((st.st_ex_mode & ~S_IFMT) == mode) {
1021                          return 0;
1022                  }
1023
1024                  rc = gpfsacl_emu_chmod(fsp->fsp_name->base_name, mode);
1025                  if (rc == 1)
1026                          return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1027                  return rc;
1028 }
1029
1030 static int gpfs_set_xattr(struct vfs_handle_struct *handle,  const char *path,
1031                            const char *name, const void *value, size_t size,  int flags){
1032         struct xattr_DOSATTRIB dosattrib;
1033         enum ndr_err_code ndr_err;
1034         DATA_BLOB blob;
1035         const char *attrstr = value;
1036         unsigned int dosmode=0;
1037         struct gpfs_winattr attrs;
1038         int ret = 0;
1039         struct gpfs_config_data *config;
1040
1041         SMB_VFS_HANDLE_GET_DATA(handle, config,
1042                                 struct gpfs_config_data,
1043                                 return -1);
1044
1045         if (!config->winattr) {
1046                 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
1047                 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1048         }
1049
1050         DEBUG(10, ("gpfs_set_xattr: %s \n",path));
1051
1052         /* Only handle DOS Attributes */
1053         if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1054                 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
1055                 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1056         }
1057
1058         blob.data = (uint8_t *)attrstr;
1059         blob.length = size;
1060
1061         ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
1062                         (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
1063
1064         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1065                 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
1066                           "from EA on file %s: Error = %s\n",
1067                           path, ndr_errstr(ndr_err)));
1068                 return false;
1069         }
1070
1071         if (dosattrib.version != 3) {
1072                 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
1073                           "%d\n", (int)dosattrib.version));
1074                 return false;
1075         }
1076         if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
1077                 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
1078                            "valid, ignoring\n"));
1079                 return true;
1080         }
1081
1082         dosmode = dosattrib.info.info3.attrib;
1083
1084         attrs.winAttrs = 0;
1085         /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1086         if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1087                 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1088         }
1089         if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1090                         attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1091                 }
1092         if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1093                         attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1094                 }
1095         if (dosmode & FILE_ATTRIBUTE_READONLY){
1096                         attrs.winAttrs |= GPFS_WINATTR_READONLY;
1097         }
1098         if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1099                 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1100         }
1101
1102
1103         ret = set_gpfs_winattrs(discard_const_p(char, path),
1104                                 GPFS_WINATTR_SET_ATTRS, &attrs);
1105         if ( ret == -1){
1106                 if (errno == ENOSYS) {
1107                         return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1108                                                      size, flags);
1109                 }
1110
1111                 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1112                 return -1;
1113         }
1114
1115         DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1116         return 0;
1117 }
1118
1119 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle,  const char *path,
1120                               const char *name, void *value, size_t size){
1121         char *attrstr = value;
1122         unsigned int dosmode = 0;
1123         struct gpfs_winattr attrs;
1124         int ret = 0;
1125         struct gpfs_config_data *config;
1126
1127         SMB_VFS_HANDLE_GET_DATA(handle, config,
1128                                 struct gpfs_config_data,
1129                                 return -1);
1130
1131         if (!config->winattr) {
1132                 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1133                 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1134         }
1135
1136         DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1137
1138         /* Only handle DOS Attributes */
1139         if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1140                 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1141                 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1142         }
1143
1144         ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1145         if ( ret == -1){
1146                 if (errno == ENOSYS) {
1147                         return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1148                                                      size);
1149                 }
1150
1151                 DEBUG(1, ("gpfs_get_xattr: Get GPFS attributes failed: "
1152                           "%d (%s)\n", ret, strerror(errno)));
1153                 return -1;
1154         }
1155
1156         DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1157
1158         /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1159         if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1160                 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1161         }
1162         if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1163                 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1164         }
1165         if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1166                 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1167         }
1168         if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1169                 dosmode |= FILE_ATTRIBUTE_READONLY;
1170         }
1171         if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1172                 dosmode |= FILE_ATTRIBUTE_SPARSE;
1173         }
1174
1175         snprintf(attrstr, size, "0x%2.2x",
1176                  (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1177         DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1178         return 4;
1179 }
1180
1181 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1182                          struct smb_filename *smb_fname)
1183 {
1184         struct gpfs_winattr attrs;
1185         char *fname = NULL;
1186         NTSTATUS status;
1187         int ret;
1188         struct gpfs_config_data *config;
1189
1190         SMB_VFS_HANDLE_GET_DATA(handle, config,
1191                                 struct gpfs_config_data,
1192                                 return -1);
1193
1194         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1195         if (ret == -1) {
1196                 return -1;
1197         }
1198
1199         if (!config->winattr) {
1200                 return 0;
1201         }
1202
1203         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1204         if (!NT_STATUS_IS_OK(status)) {
1205                 errno = map_errno_from_nt_status(status);
1206                 return -1;
1207         }
1208         ret = get_gpfs_winattrs(discard_const_p(char, fname), &attrs);
1209         TALLOC_FREE(fname);
1210         if (ret == 0) {
1211                 smb_fname->st.st_ex_calculated_birthtime = false;
1212                 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1213                 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1214                 smb_fname->st.vfs_private = attrs.winAttrs;
1215         }
1216         return 0;
1217 }
1218
1219 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1220                           struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1221 {
1222         struct gpfs_winattr attrs;
1223         int ret;
1224         struct gpfs_config_data *config;
1225
1226         SMB_VFS_HANDLE_GET_DATA(handle, config,
1227                                 struct gpfs_config_data,
1228                                 return -1);
1229
1230         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1231         if (ret == -1) {
1232                 return -1;
1233         }
1234         if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1235                 return 0;
1236         }
1237         if (!config->winattr) {
1238                 return 0;
1239         }
1240
1241         ret = smbd_fget_gpfs_winattrs(fsp->fh->fd, &attrs);
1242         if (ret == 0) {
1243                 sbuf->st_ex_calculated_birthtime = false;
1244                 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1245                 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1246         }
1247         return 0;
1248 }
1249
1250 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1251                           struct smb_filename *smb_fname)
1252 {
1253         struct gpfs_winattr attrs;
1254         char *path = NULL;
1255         NTSTATUS status;
1256         int ret;
1257         struct gpfs_config_data *config;
1258
1259         SMB_VFS_HANDLE_GET_DATA(handle, config,
1260                                 struct gpfs_config_data,
1261                                 return -1);
1262
1263         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1264         if (ret == -1) {
1265                 return -1;
1266         }
1267         if (!config->winattr) {
1268                 return 0;
1269         }
1270
1271         status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1272         if (!NT_STATUS_IS_OK(status)) {
1273                 errno = map_errno_from_nt_status(status);
1274                 return -1;
1275         }
1276         ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1277         TALLOC_FREE(path);
1278         if (ret == 0) {
1279                 smb_fname->st.st_ex_calculated_birthtime = false;
1280                 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1281                 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1282                 smb_fname->st.vfs_private = attrs.winAttrs;
1283         }
1284         return 0;
1285 }
1286
1287 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1288                         const struct smb_filename *smb_fname,
1289                         struct smb_file_time *ft)
1290 {
1291
1292         struct gpfs_winattr attrs;
1293         int ret;
1294         char *path = NULL;
1295         NTSTATUS status;
1296         struct gpfs_config_data *config;
1297
1298         SMB_VFS_HANDLE_GET_DATA(handle, config,
1299                                 struct gpfs_config_data,
1300                                 return -1);
1301
1302         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1303         if(ret == -1){
1304                 /* don't complain if access was denied */
1305                 if (errno != EPERM && errno != EACCES) {
1306                         DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1307                                  "%s", strerror(errno)));
1308                 }
1309                 return -1;
1310         }
1311
1312         if(null_timespec(ft->create_time)){
1313                 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1314                 return 0;
1315         }
1316
1317         if (!config->winattr) {
1318                 return 0;
1319         }
1320
1321         status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1322         if (!NT_STATUS_IS_OK(status)) {
1323                 errno = map_errno_from_nt_status(status);
1324                 return -1;
1325         }
1326
1327         attrs.winAttrs = 0;
1328         attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1329         attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1330
1331         ret = set_gpfs_winattrs(discard_const_p(char, path),
1332                                 GPFS_WINATTR_SET_CREATION_TIME, &attrs);
1333         if(ret == -1 && errno != ENOSYS){
1334                 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1335                 return -1;
1336         }
1337         return 0;
1338
1339 }
1340
1341 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1342                        struct files_struct *fsp, enum vfs_fallocate_mode mode,
1343                        off_t offset, off_t len)
1344 {
1345         int ret;
1346         struct gpfs_config_data *config;
1347
1348         SMB_VFS_HANDLE_GET_DATA(handle, config,
1349                                 struct gpfs_config_data,
1350                                 return -1);
1351
1352         if (!config->prealloc) {
1353                 /* you should better not run fallocate() on GPFS at all */
1354                 errno = ENOTSUP;
1355                 return -1;
1356         }
1357
1358         if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1359                 DEBUG(10, ("Unsupported VFS_FALLOCATE_KEEP_SIZE\n"));
1360                 errno = ENOTSUP;
1361                 return -1;
1362         }
1363
1364         ret = smbd_gpfs_prealloc(fsp->fh->fd, offset, len);
1365
1366         if (ret == -1 && errno != ENOSYS) {
1367                 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1368         } else if (ret == -1 && errno == ENOSYS) {
1369                 DEBUG(10, ("GPFS prealloc not supported.\n"));
1370         } else {
1371                 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1372         }
1373
1374         return ret;
1375 }
1376
1377 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1378                                 off_t len)
1379 {
1380         int result;
1381         struct gpfs_config_data *config;
1382
1383         SMB_VFS_HANDLE_GET_DATA(handle, config,
1384                                 struct gpfs_config_data,
1385                                 return -1);
1386
1387         if (!config->ftruncate) {
1388                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1389         }
1390
1391         result = smbd_gpfs_ftruncate(fsp->fh->fd, len);
1392         if ((result == -1) && (errno == ENOSYS)) {
1393                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1394         }
1395         return result;
1396 }
1397
1398 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1399                                 const struct smb_filename *fname,
1400                                 SMB_STRUCT_STAT *sbuf)
1401 {
1402         struct gpfs_winattr attrs;
1403         char *path = NULL;
1404         NTSTATUS status;
1405         struct gpfs_config_data *config;
1406
1407         SMB_VFS_HANDLE_GET_DATA(handle, config,
1408                                 struct gpfs_config_data,
1409                                 return -1);
1410
1411         if (!config->winattr) {
1412                 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1413         }
1414
1415         status = get_full_smb_filename(talloc_tos(), fname, &path);
1416         if (!NT_STATUS_IS_OK(status)) {
1417                 errno = map_errno_from_nt_status(status);
1418                 return -1;
1419         }
1420
1421         if (VALID_STAT(*sbuf)) {
1422                 attrs.winAttrs = sbuf->vfs_private;
1423         } else {
1424                 int ret;
1425                 ret = get_gpfs_winattrs(path, &attrs);
1426
1427                 if (ret == -1) {
1428                         TALLOC_FREE(path);
1429                         return false;
1430                 }
1431         }
1432         if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1433                 DEBUG(10, ("%s is offline\n", path));
1434                 TALLOC_FREE(path);
1435                 return true;
1436         }
1437         DEBUG(10, ("%s is online\n", path));
1438         TALLOC_FREE(path);
1439         return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1440 }
1441
1442 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1443                                struct files_struct *fsp)
1444 {
1445         return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1446 }
1447
1448 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1449                                  files_struct *fsp, const DATA_BLOB *hdr,
1450                                  off_t offset, size_t n)
1451 {
1452         if ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0) {
1453                 errno = ENOSYS;
1454                 return -1;
1455         }
1456         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1457 }
1458
1459 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1460                             const char *service, const char *user)
1461 {
1462         struct gpfs_config_data *config;
1463         int ret;
1464
1465         smbd_gpfs_lib_init();
1466
1467         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1468
1469         if (ret < 0) {
1470                 return ret;
1471         }
1472
1473         config = talloc_zero(handle->conn, struct gpfs_config_data);
1474         if (!config) {
1475                 SMB_VFS_NEXT_DISCONNECT(handle);
1476                 DEBUG(0, ("talloc_zero() failed\n"));
1477                 return -1;
1478         }
1479
1480         config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1481                                         "sharemodes", true);
1482
1483         config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1484                                         "leases", true);
1485
1486         config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1487                                    "hsm", false);
1488
1489         config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1490                                       "syncio", false);
1491
1492         config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1493                                        "winattr", false);
1494
1495         config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1496                                          "ftruncate", true);
1497
1498         config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1499                                                "getrealfilename", true);
1500
1501         config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1502                                           "dfreequota", false);
1503
1504         config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
1505                                    "prealloc", true);
1506
1507         config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
1508
1509         SMB_VFS_HANDLE_SET_DATA(handle, config,
1510                                 NULL, struct gpfs_config_data,
1511                                 return -1);
1512
1513         return 0;
1514 }
1515
1516 static int vfs_gpfs_get_quotas(const char *path, uid_t uid, gid_t gid,
1517                                int *fset_id,
1518                                struct gpfs_quotaInfo *qi_user,
1519                                struct gpfs_quotaInfo *qi_group,
1520                                struct gpfs_quotaInfo *qi_fset)
1521 {
1522         int err;
1523
1524         err = get_gpfs_fset_id(path, fset_id);
1525         if (err) {
1526                 DEBUG(0, ("Get fset id failed, errno %d.\n", errno));
1527                 return err;
1528         }
1529
1530         err = get_gpfs_quota(path, GPFS_USRQUOTA, uid, qi_user);
1531         if (err) {
1532                 return err;
1533         }
1534
1535         err = get_gpfs_quota(path, GPFS_GRPQUOTA, gid, qi_group);
1536         if (err) {
1537                 return err;
1538         }
1539
1540         err = get_gpfs_quota(path, GPFS_FILESETQUOTA, *fset_id, qi_fset);
1541         if (err) {
1542                 return err;
1543         }
1544
1545         return 0;
1546 }
1547
1548 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
1549                                      uint64_t *dfree, uint64_t *dsize)
1550 {
1551         uint64_t usage, limit;
1552
1553         /*
1554          * The quota reporting is done in units of 1024 byte blocks, but
1555          * sys_fsusage uses units of 512 byte blocks, adjust the block number
1556          * accordingly. Also filter possibly negative usage counts from gpfs.
1557          */
1558         usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
1559         limit = (uint64_t)qi.blockHardLimit * 2;
1560
1561         /*
1562          * When the grace time for the exceeded soft block quota has been
1563          * exceeded, the soft block quota becomes an additional hard limit.
1564          */
1565         if (qi.blockGraceTime && cur_time > qi.blockGraceTime) {
1566                 /* report disk as full */
1567                 *dfree = 0;
1568                 *dsize = MIN(*dsize, usage);
1569         }
1570
1571         if (!qi.blockHardLimit)
1572                 return;
1573
1574         if (usage >= limit) {
1575                 /* report disk as full */
1576                 *dfree = 0;
1577                 *dsize = MIN(*dsize, usage);
1578
1579         } else {
1580                 /* limit has not been reached, determine "free space" */
1581                 *dfree = MIN(*dfree, limit - usage);
1582                 *dsize = MIN(*dsize, limit);
1583         }
1584 }
1585
1586 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
1587                                    bool small_query, uint64_t *bsize,
1588                                    uint64_t *dfree, uint64_t *dsize)
1589 {
1590         struct security_unix_token *utok;
1591         struct gpfs_quotaInfo qi_user, qi_group, qi_fset;
1592         struct gpfs_config_data *config;
1593         int err, fset_id;
1594         time_t cur_time;
1595
1596         SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
1597                                 return (uint64_t)-1);
1598         if (!config->dfreequota) {
1599                 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1600                                               bsize, dfree, dsize);
1601         }
1602
1603         err = sys_fsusage(path, dfree, dsize);
1604         if (err) {
1605                 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
1606                 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1607                                               bsize, dfree, dsize);
1608         }
1609
1610         /* sys_fsusage returns units of 512 bytes */
1611         *bsize = 512;
1612
1613         DEBUG(10, ("fs dfree %llu, dsize %llu\n",
1614                    (unsigned long long)*dfree, (unsigned long long)*dsize));
1615
1616         utok = handle->conn->session_info->unix_token;
1617         err = vfs_gpfs_get_quotas(path, utok->uid, utok->gid, &fset_id,
1618                                   &qi_user, &qi_group, &qi_fset);
1619         if (err) {
1620                 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1621                                               bsize, dfree, dsize);
1622         }
1623
1624         cur_time = time(NULL);
1625
1626         /* Adjust free space and size according to quota limits. */
1627         vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
1628         vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
1629
1630         /* Id 0 indicates the default quota, not an actual quota */
1631         if (fset_id != 0) {
1632                 vfs_gpfs_disk_free_quota(qi_fset, cur_time, dfree, dsize);
1633         }
1634
1635         disk_norm(small_query, bsize, dfree, dsize);
1636         return *dfree;
1637 }
1638
1639 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
1640                                       enum timestamp_set_resolution *p_ts_res)
1641 {
1642         struct gpfs_config_data *config;
1643         uint32_t next;
1644
1645         next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
1646
1647         SMB_VFS_HANDLE_GET_DATA(handle, config,
1648                                 struct gpfs_config_data,
1649                                 return next);
1650
1651         if (config->hsm) {
1652                 next |= FILE_SUPPORTS_REMOTE_STORAGE;
1653         }
1654         return next;
1655 }
1656
1657 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
1658                          struct smb_filename *smb_fname, files_struct *fsp,
1659                          int flags, mode_t mode)
1660 {
1661         struct gpfs_config_data *config;
1662
1663         SMB_VFS_HANDLE_GET_DATA(handle, config,
1664                                 struct gpfs_config_data,
1665                                 return -1);
1666
1667         if (config->syncio) {
1668                 flags |= O_SYNC;
1669         }
1670         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1671 }
1672
1673
1674 static struct vfs_fn_pointers vfs_gpfs_fns = {
1675         .connect_fn = vfs_gpfs_connect,
1676         .disk_free_fn = vfs_gpfs_disk_free,
1677         .fs_capabilities_fn = vfs_gpfs_capabilities,
1678         .kernel_flock_fn = vfs_gpfs_kernel_flock,
1679         .linux_setlease_fn = vfs_gpfs_setlease,
1680         .get_real_filename_fn = vfs_gpfs_get_real_filename,
1681         .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
1682         .get_nt_acl_fn = gpfsacl_get_nt_acl,
1683         .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
1684         .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
1685         .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
1686         .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
1687         .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
1688         .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
1689         .chmod_fn = vfs_gpfs_chmod,
1690         .fchmod_fn = vfs_gpfs_fchmod,
1691         .close_fn = vfs_gpfs_close,
1692         .setxattr_fn = gpfs_set_xattr,
1693         .getxattr_fn = gpfs_get_xattr,
1694         .stat_fn = vfs_gpfs_stat,
1695         .fstat_fn = vfs_gpfs_fstat,
1696         .lstat_fn = vfs_gpfs_lstat,
1697         .ntimes_fn = vfs_gpfs_ntimes,
1698         .is_offline_fn = vfs_gpfs_is_offline,
1699         .aio_force_fn = vfs_gpfs_aio_force,
1700         .sendfile_fn = vfs_gpfs_sendfile,
1701         .fallocate_fn = vfs_gpfs_fallocate,
1702         .open_fn = vfs_gpfs_open,
1703         .ftruncate_fn = vfs_gpfs_ftruncate
1704 };
1705
1706 NTSTATUS vfs_gpfs_init(void);
1707 NTSTATUS vfs_gpfs_init(void)
1708 {
1709         init_gpfs();
1710
1711         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
1712                                 &vfs_gpfs_fns);
1713 }