s3:vfs_gpfs: make sure parameters are set correctly for leases
[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 = offsetof(gpfs_acl_t, ace_v4) + smb_get_naces(smbacl) *
421                 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                         TALLOC_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                 TALLOC_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
710         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
711
712         len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
713                 sizeof(gpfs_ace_v1_t);
714
715         result = (struct gpfs_acl *)SMB_MALLOC(len);
716         if (result == NULL) {
717                 errno = ENOMEM;
718                 return result;
719         }
720
721         result->acl_len = len;
722         result->acl_level = 0;
723         result->acl_version = GPFS_ACL_VERSION_POSIX;
724         result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
725                 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
726         result->acl_nace = pacl->count;
727
728         for (i=0; i<pacl->count; i++) {
729                 const struct smb_acl_entry *ace = &pacl->acl[i];
730                 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
731
732                 DEBUG(10, ("Converting type %d perm %x\n",
733                            (int)ace->a_type, (int)ace->a_perm));
734
735                 g_ace->ace_perm = 0;
736
737                 switch(ace->a_type) {
738                 case SMB_ACL_USER:
739                         g_ace->ace_type = GPFS_ACL_USER;
740                         g_ace->ace_who = (gpfs_uid_t)ace->uid;
741                         break;
742                 case SMB_ACL_USER_OBJ:
743                         g_ace->ace_type = GPFS_ACL_USER_OBJ;
744                         g_ace->ace_perm |= ACL_PERM_CONTROL;
745                         g_ace->ace_who = 0;
746                         break;
747                 case SMB_ACL_GROUP:
748                         g_ace->ace_type = GPFS_ACL_GROUP;
749                         g_ace->ace_who = (gpfs_uid_t)ace->gid;
750                         break;
751                 case SMB_ACL_GROUP_OBJ:
752                         g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
753                         g_ace->ace_who = 0;
754                         break;
755                 case SMB_ACL_MASK:
756                         g_ace->ace_type = GPFS_ACL_MASK;
757                         g_ace->ace_perm = 0x8f;
758                         g_ace->ace_who = 0;
759                         break;
760                 case SMB_ACL_OTHER:
761                         g_ace->ace_type = GPFS_ACL_OTHER;
762                         g_ace->ace_who = 0;
763                         break;
764                 default:
765                         DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
766                         errno = EINVAL;
767                         SAFE_FREE(result);
768                         return NULL;
769                 }
770
771                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
772                         ACL_PERM_READ : 0;
773                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
774                         ACL_PERM_WRITE : 0;
775                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
776                         ACL_PERM_EXECUTE : 0;
777
778                 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
779                               g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
780         }
781
782         return result;
783 }
784
785 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
786                                     const char *name,
787                                     SMB_ACL_TYPE_T type,
788                                     SMB_ACL_T theacl)
789 {
790         struct gpfs_acl *gpfs_acl;
791         int result;
792         struct gpfs_config_data *config;
793
794         SMB_VFS_HANDLE_GET_DATA(handle, config,
795                                 struct gpfs_config_data,
796                                 return -1);
797
798         if (!config->acl) {
799                 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, type, theacl);
800         }
801
802         gpfs_acl = smb2gpfs_acl(theacl, type);
803         if (gpfs_acl == NULL) {
804                 return -1;
805         }
806
807         result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
808
809         SAFE_FREE(gpfs_acl);
810         return result;
811 }
812
813 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
814                                   files_struct *fsp,
815                                   SMB_ACL_T theacl)
816 {
817         struct gpfs_config_data *config;
818
819         SMB_VFS_HANDLE_GET_DATA(handle, config,
820                                 struct gpfs_config_data,
821                                 return -1);
822
823         if (!config->acl) {
824                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
825         }
826
827         return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
828                                         SMB_ACL_TYPE_ACCESS, theacl);
829 }
830
831 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
832                                            const char *path)
833 {
834         struct gpfs_config_data *config;
835
836         SMB_VFS_HANDLE_GET_DATA(handle, config,
837                                 struct gpfs_config_data,
838                                 return -1);
839
840         if (!config->acl) {
841                 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
842         }
843
844         errno = ENOTSUP;
845         return -1;
846 }
847
848 /*
849  * Assumed: mode bits are shiftable and standard
850  * Output: the new aceMask field for an smb nfs4 ace
851  */
852 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
853 {
854         const uint32 posix_nfs4map[3] = {
855                 SMB_ACE4_EXECUTE, /* execute */
856                 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
857                 SMB_ACE4_READ_DATA /* read */
858         };
859         int     i;
860         uint32_t        posix_mask = 0x01;
861         uint32_t        posix_bit;
862         uint32_t        nfs4_bits;
863
864         for(i=0; i<3; i++) {
865                 nfs4_bits = posix_nfs4map[i];
866                 posix_bit = rwx & posix_mask;
867
868                 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
869                         if (posix_bit)
870                                 aceMask |= nfs4_bits;
871                         else
872                                 aceMask &= ~nfs4_bits;
873                 } else {
874                         /* add deny bits when suitable */
875                         if (!posix_bit)
876                                 aceMask |= nfs4_bits;
877                         else
878                                 aceMask &= ~nfs4_bits;
879                 } /* other ace types are unexpected */
880
881                 posix_mask <<= 1;
882         }
883
884         return aceMask;
885 }
886
887 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
888 {
889         SMB4ACL_T *pacl = NULL;
890         int     result;
891         bool    haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
892         int     i;
893         files_struct    fake_fsp; /* TODO: rationalize parametrization */
894         SMB4ACE_T       *smbace;
895         NTSTATUS status;
896
897         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
898
899         result = gpfs_get_nfs4_acl(path, &pacl);
900         if (result)
901                 return result;
902
903         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
904                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
905         }
906
907         for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
908                 SMB_ACE4PROP_T  *ace = smb_get_ace4(smbace);
909                 uint32_t        specid = ace->who.special_id;
910
911                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
912                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
913                     specid <= SMB_ACE4_WHO_EVERYONE) {
914
915                         uint32_t newMask;
916
917                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
918                                 haveAllowEntry[specid] = True;
919
920                         /* mode >> 6 for @owner, mode >> 3 for @group,
921                          * mode >> 0 for @everyone */
922                         newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
923                                                       mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
924                         if (ace->aceMask!=newMask) {
925                                 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
926                                            path, ace->aceMask, newMask, specid));
927                         }
928                         ace->aceMask = newMask;
929                 }
930         }
931
932         /* make sure we have at least ALLOW entries
933          * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
934          * - if necessary
935          */
936         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
937                 SMB_ACE4PROP_T  ace;
938
939                 if (haveAllowEntry[i]==True)
940                         continue;
941
942                 ZERO_STRUCT(ace);
943                 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
944                 ace.flags |= SMB_ACE4_ID_SPECIAL;
945                 ace.who.special_id = i;
946
947                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
948                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
949
950                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
951                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
952
953                 /* don't add unnecessary aces */
954                 if (!ace.aceMask)
955                         continue;
956
957                 /* we add it to the END - as windows expects allow aces */
958                 smb_add_ace4(pacl, &ace);
959                 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
960                            path, mode, i, ace.aceMask));
961         }
962
963         /* don't add complementary DENY ACEs here */
964         ZERO_STRUCT(fake_fsp);
965         status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
966                                             &fake_fsp.fsp_name);
967         if (!NT_STATUS_IS_OK(status)) {
968                 errno = map_errno_from_nt_status(status);
969                 return -1;
970         }
971         /* put the acl */
972         if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False) {
973                 TALLOC_FREE(fake_fsp.fsp_name);
974                 return -1;
975         }
976
977         TALLOC_FREE(fake_fsp.fsp_name);
978         return 0; /* ok for [f]chmod */
979 }
980
981 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
982 {
983         struct smb_filename *smb_fname_cpath;
984         int rc;
985         NTSTATUS status;
986
987         status = create_synthetic_smb_fname(
988                 talloc_tos(), path, NULL, NULL, &smb_fname_cpath);
989
990         if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
991                 return -1;
992         }
993
994         /* avoid chmod() if possible, to preserve acls */
995         if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
996                 return 0;
997         }
998
999         rc = gpfsacl_emu_chmod(path, mode);
1000         if (rc == 1)
1001                 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1002         return rc;
1003 }
1004
1005 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1006 {
1007                  SMB_STRUCT_STAT st;
1008                  int rc;
1009
1010                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1011                          return -1;
1012                  }
1013
1014                  /* avoid chmod() if possible, to preserve acls */
1015                  if ((st.st_ex_mode & ~S_IFMT) == mode) {
1016                          return 0;
1017                  }
1018
1019                  rc = gpfsacl_emu_chmod(fsp->fsp_name->base_name, mode);
1020                  if (rc == 1)
1021                          return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1022                  return rc;
1023 }
1024
1025 static int gpfs_set_xattr(struct vfs_handle_struct *handle,  const char *path,
1026                            const char *name, const void *value, size_t size,  int flags){
1027         struct xattr_DOSATTRIB dosattrib;
1028         enum ndr_err_code ndr_err;
1029         DATA_BLOB blob;
1030         const char *attrstr = value;
1031         unsigned int dosmode=0;
1032         struct gpfs_winattr attrs;
1033         int ret = 0;
1034         struct gpfs_config_data *config;
1035
1036         SMB_VFS_HANDLE_GET_DATA(handle, config,
1037                                 struct gpfs_config_data,
1038                                 return -1);
1039
1040         if (!config->winattr) {
1041                 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
1042                 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1043         }
1044
1045         DEBUG(10, ("gpfs_set_xattr: %s \n",path));
1046
1047         /* Only handle DOS Attributes */
1048         if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1049                 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
1050                 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1051         }
1052
1053         blob.data = (uint8_t *)attrstr;
1054         blob.length = size;
1055
1056         ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
1057                         (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
1058
1059         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1060                 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
1061                           "from EA on file %s: Error = %s\n",
1062                           path, ndr_errstr(ndr_err)));
1063                 return false;
1064         }
1065
1066         if (dosattrib.version != 3) {
1067                 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
1068                           "%d\n", (int)dosattrib.version));
1069                 return false;
1070         }
1071         if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
1072                 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
1073                            "valid, ignoring\n"));
1074                 return true;
1075         }
1076
1077         dosmode = dosattrib.info.info3.attrib;
1078
1079         attrs.winAttrs = 0;
1080         /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1081         if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1082                 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1083         }
1084         if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1085                         attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1086                 }
1087         if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1088                         attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1089                 }
1090         if (dosmode & FILE_ATTRIBUTE_READONLY){
1091                         attrs.winAttrs |= GPFS_WINATTR_READONLY;
1092         }
1093         if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1094                 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1095         }
1096
1097
1098         ret = set_gpfs_winattrs(discard_const_p(char, path),
1099                                 GPFS_WINATTR_SET_ATTRS, &attrs);
1100         if ( ret == -1){
1101                 if (errno == ENOSYS) {
1102                         return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1103                                                      size, flags);
1104                 }
1105
1106                 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1107                 return -1;
1108         }
1109
1110         DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1111         return 0;
1112 }
1113
1114 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle,  const char *path,
1115                               const char *name, void *value, size_t size){
1116         char *attrstr = value;
1117         unsigned int dosmode = 0;
1118         struct gpfs_winattr attrs;
1119         int ret = 0;
1120         struct gpfs_config_data *config;
1121
1122         SMB_VFS_HANDLE_GET_DATA(handle, config,
1123                                 struct gpfs_config_data,
1124                                 return -1);
1125
1126         if (!config->winattr) {
1127                 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1128                 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1129         }
1130
1131         DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1132
1133         /* Only handle DOS Attributes */
1134         if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1135                 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1136                 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1137         }
1138
1139         ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1140         if ( ret == -1){
1141                 if (errno == ENOSYS) {
1142                         return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1143                                                      size);
1144                 }
1145
1146                 DEBUG(1, ("gpfs_get_xattr: Get GPFS attributes failed: "
1147                           "%d (%s)\n", ret, strerror(errno)));
1148                 return -1;
1149         }
1150
1151         DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1152
1153         /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1154         if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1155                 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1156         }
1157         if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1158                 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1159         }
1160         if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1161                 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1162         }
1163         if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1164                 dosmode |= FILE_ATTRIBUTE_READONLY;
1165         }
1166         if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1167                 dosmode |= FILE_ATTRIBUTE_SPARSE;
1168         }
1169
1170         snprintf(attrstr, size, "0x%2.2x",
1171                  (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1172         DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1173         return 4;
1174 }
1175
1176 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1177                          struct smb_filename *smb_fname)
1178 {
1179         struct gpfs_winattr attrs;
1180         char *fname = NULL;
1181         NTSTATUS status;
1182         int ret;
1183         struct gpfs_config_data *config;
1184
1185         SMB_VFS_HANDLE_GET_DATA(handle, config,
1186                                 struct gpfs_config_data,
1187                                 return -1);
1188
1189         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1190         if (ret == -1) {
1191                 return -1;
1192         }
1193
1194         if (!config->winattr) {
1195                 return 0;
1196         }
1197
1198         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1199         if (!NT_STATUS_IS_OK(status)) {
1200                 errno = map_errno_from_nt_status(status);
1201                 return -1;
1202         }
1203         ret = get_gpfs_winattrs(discard_const_p(char, fname), &attrs);
1204         TALLOC_FREE(fname);
1205         if (ret == 0) {
1206                 smb_fname->st.st_ex_calculated_birthtime = false;
1207                 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1208                 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1209                 smb_fname->st.vfs_private = attrs.winAttrs;
1210         }
1211         return 0;
1212 }
1213
1214 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1215                           struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1216 {
1217         struct gpfs_winattr attrs;
1218         int ret;
1219         struct gpfs_config_data *config;
1220
1221         SMB_VFS_HANDLE_GET_DATA(handle, config,
1222                                 struct gpfs_config_data,
1223                                 return -1);
1224
1225         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1226         if (ret == -1) {
1227                 return -1;
1228         }
1229         if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1230                 return 0;
1231         }
1232         if (!config->winattr) {
1233                 return 0;
1234         }
1235
1236         ret = smbd_fget_gpfs_winattrs(fsp->fh->fd, &attrs);
1237         if (ret == 0) {
1238                 sbuf->st_ex_calculated_birthtime = false;
1239                 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1240                 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1241         }
1242         return 0;
1243 }
1244
1245 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1246                           struct smb_filename *smb_fname)
1247 {
1248         struct gpfs_winattr attrs;
1249         char *path = NULL;
1250         NTSTATUS status;
1251         int ret;
1252         struct gpfs_config_data *config;
1253
1254         SMB_VFS_HANDLE_GET_DATA(handle, config,
1255                                 struct gpfs_config_data,
1256                                 return -1);
1257
1258         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1259         if (ret == -1) {
1260                 return -1;
1261         }
1262         if (!config->winattr) {
1263                 return 0;
1264         }
1265
1266         status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1267         if (!NT_STATUS_IS_OK(status)) {
1268                 errno = map_errno_from_nt_status(status);
1269                 return -1;
1270         }
1271         ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1272         TALLOC_FREE(path);
1273         if (ret == 0) {
1274                 smb_fname->st.st_ex_calculated_birthtime = false;
1275                 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1276                 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1277                 smb_fname->st.vfs_private = attrs.winAttrs;
1278         }
1279         return 0;
1280 }
1281
1282 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1283                         const struct smb_filename *smb_fname,
1284                         struct smb_file_time *ft)
1285 {
1286
1287         struct gpfs_winattr attrs;
1288         int ret;
1289         char *path = NULL;
1290         NTSTATUS status;
1291         struct gpfs_config_data *config;
1292
1293         SMB_VFS_HANDLE_GET_DATA(handle, config,
1294                                 struct gpfs_config_data,
1295                                 return -1);
1296
1297         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1298         if(ret == -1){
1299                 /* don't complain if access was denied */
1300                 if (errno != EPERM && errno != EACCES) {
1301                         DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1302                                  "%s", strerror(errno)));
1303                 }
1304                 return -1;
1305         }
1306
1307         if(null_timespec(ft->create_time)){
1308                 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1309                 return 0;
1310         }
1311
1312         if (!config->winattr) {
1313                 return 0;
1314         }
1315
1316         status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1317         if (!NT_STATUS_IS_OK(status)) {
1318                 errno = map_errno_from_nt_status(status);
1319                 return -1;
1320         }
1321
1322         attrs.winAttrs = 0;
1323         attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1324         attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1325
1326         ret = set_gpfs_winattrs(discard_const_p(char, path),
1327                                 GPFS_WINATTR_SET_CREATION_TIME, &attrs);
1328         if(ret == -1 && errno != ENOSYS){
1329                 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1330                 return -1;
1331         }
1332         return 0;
1333
1334 }
1335
1336 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1337                        struct files_struct *fsp, enum vfs_fallocate_mode mode,
1338                        off_t offset, off_t len)
1339 {
1340         int ret;
1341         struct gpfs_config_data *config;
1342
1343         SMB_VFS_HANDLE_GET_DATA(handle, config,
1344                                 struct gpfs_config_data,
1345                                 return -1);
1346
1347         if (!config->prealloc) {
1348                 /* you should better not run fallocate() on GPFS at all */
1349                 errno = ENOTSUP;
1350                 return -1;
1351         }
1352
1353         if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1354                 DEBUG(10, ("Unsupported VFS_FALLOCATE_KEEP_SIZE\n"));
1355                 errno = ENOTSUP;
1356                 return -1;
1357         }
1358
1359         ret = smbd_gpfs_prealloc(fsp->fh->fd, offset, len);
1360
1361         if (ret == -1 && errno != ENOSYS) {
1362                 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1363         } else if (ret == -1 && errno == ENOSYS) {
1364                 DEBUG(10, ("GPFS prealloc not supported.\n"));
1365         } else {
1366                 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1367         }
1368
1369         return ret;
1370 }
1371
1372 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1373                                 off_t len)
1374 {
1375         int result;
1376         struct gpfs_config_data *config;
1377
1378         SMB_VFS_HANDLE_GET_DATA(handle, config,
1379                                 struct gpfs_config_data,
1380                                 return -1);
1381
1382         if (!config->ftruncate) {
1383                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1384         }
1385
1386         result = smbd_gpfs_ftruncate(fsp->fh->fd, len);
1387         if ((result == -1) && (errno == ENOSYS)) {
1388                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1389         }
1390         return result;
1391 }
1392
1393 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1394                                 const struct smb_filename *fname,
1395                                 SMB_STRUCT_STAT *sbuf)
1396 {
1397         struct gpfs_winattr attrs;
1398         char *path = NULL;
1399         NTSTATUS status;
1400         struct gpfs_config_data *config;
1401
1402         SMB_VFS_HANDLE_GET_DATA(handle, config,
1403                                 struct gpfs_config_data,
1404                                 return -1);
1405
1406         if (!config->winattr) {
1407                 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1408         }
1409
1410         status = get_full_smb_filename(talloc_tos(), fname, &path);
1411         if (!NT_STATUS_IS_OK(status)) {
1412                 errno = map_errno_from_nt_status(status);
1413                 return -1;
1414         }
1415
1416         if (VALID_STAT(*sbuf)) {
1417                 attrs.winAttrs = sbuf->vfs_private;
1418         } else {
1419                 int ret;
1420                 ret = get_gpfs_winattrs(path, &attrs);
1421
1422                 if (ret == -1) {
1423                         TALLOC_FREE(path);
1424                         return false;
1425                 }
1426         }
1427         if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1428                 DEBUG(10, ("%s is offline\n", path));
1429                 TALLOC_FREE(path);
1430                 return true;
1431         }
1432         DEBUG(10, ("%s is online\n", path));
1433         TALLOC_FREE(path);
1434         return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1435 }
1436
1437 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1438                                struct files_struct *fsp)
1439 {
1440         return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1441 }
1442
1443 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1444                                  files_struct *fsp, const DATA_BLOB *hdr,
1445                                  off_t offset, size_t n)
1446 {
1447         if ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0) {
1448                 errno = ENOSYS;
1449                 return -1;
1450         }
1451         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1452 }
1453
1454 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1455                             const char *service, const char *user)
1456 {
1457         struct gpfs_config_data *config;
1458         int ret;
1459
1460         smbd_gpfs_lib_init();
1461
1462         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1463
1464         if (ret < 0) {
1465                 return ret;
1466         }
1467
1468         config = talloc_zero(handle->conn, struct gpfs_config_data);
1469         if (!config) {
1470                 SMB_VFS_NEXT_DISCONNECT(handle);
1471                 DEBUG(0, ("talloc_zero() failed\n"));
1472                 return -1;
1473         }
1474
1475         config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1476                                         "sharemodes", true);
1477
1478         config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1479                                         "leases", true);
1480
1481         config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1482                                    "hsm", false);
1483
1484         config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1485                                       "syncio", false);
1486
1487         config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1488                                        "winattr", false);
1489
1490         config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1491                                          "ftruncate", true);
1492
1493         config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1494                                                "getrealfilename", true);
1495
1496         config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1497                                           "dfreequota", false);
1498
1499         config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
1500                                    "prealloc", true);
1501
1502         config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
1503
1504         SMB_VFS_HANDLE_SET_DATA(handle, config,
1505                                 NULL, struct gpfs_config_data,
1506                                 return -1);
1507
1508         if (config->leases) {
1509                 /*
1510                  * GPFS lease code is based on kernel oplock code
1511                  * so make sure it is turned on
1512                  */
1513                 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
1514                         DEBUG(5, ("Enabling kernel oplocks for "
1515                                   "gpfs:leases to work\n"));
1516                         lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
1517                                         "true");
1518                 }
1519
1520                 /*
1521                  * as the kernel does not properly support Level II oplocks
1522                  * and GPFS leases code is based on kernel infrastructure, we
1523                  * need to turn off Level II oplocks if gpfs:leases is enabled
1524                  */
1525                 if (lp_level2_oplocks(SNUM(handle->conn))) {
1526                         DEBUG(5, ("gpfs:leases are enabled, disabling "
1527                                   "Level II oplocks\n"));
1528                         lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
1529                                         "false");
1530                 }
1531         }
1532
1533         return 0;
1534 }
1535
1536 static int vfs_gpfs_get_quotas(const char *path, uid_t uid, gid_t gid,
1537                                int *fset_id,
1538                                struct gpfs_quotaInfo *qi_user,
1539                                struct gpfs_quotaInfo *qi_group,
1540                                struct gpfs_quotaInfo *qi_fset)
1541 {
1542         int err;
1543
1544         err = get_gpfs_fset_id(path, fset_id);
1545         if (err) {
1546                 DEBUG(0, ("Get fset id failed, errno %d.\n", errno));
1547                 return err;
1548         }
1549
1550         err = get_gpfs_quota(path, GPFS_USRQUOTA, uid, qi_user);
1551         if (err) {
1552                 return err;
1553         }
1554
1555         err = get_gpfs_quota(path, GPFS_GRPQUOTA, gid, qi_group);
1556         if (err) {
1557                 return err;
1558         }
1559
1560         err = get_gpfs_quota(path, GPFS_FILESETQUOTA, *fset_id, qi_fset);
1561         if (err) {
1562                 return err;
1563         }
1564
1565         return 0;
1566 }
1567
1568 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
1569                                      uint64_t *dfree, uint64_t *dsize)
1570 {
1571         uint64_t usage, limit;
1572
1573         /*
1574          * The quota reporting is done in units of 1024 byte blocks, but
1575          * sys_fsusage uses units of 512 byte blocks, adjust the block number
1576          * accordingly. Also filter possibly negative usage counts from gpfs.
1577          */
1578         usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
1579         limit = (uint64_t)qi.blockHardLimit * 2;
1580
1581         /*
1582          * When the grace time for the exceeded soft block quota has been
1583          * exceeded, the soft block quota becomes an additional hard limit.
1584          */
1585         if (qi.blockSoftLimit &&
1586             qi.blockGraceTime && cur_time > qi.blockGraceTime) {
1587                 /* report disk as full */
1588                 *dfree = 0;
1589                 *dsize = MIN(*dsize, usage);
1590         }
1591
1592         if (!qi.blockHardLimit)
1593                 return;
1594
1595         if (usage >= limit) {
1596                 /* report disk as full */
1597                 *dfree = 0;
1598                 *dsize = MIN(*dsize, usage);
1599
1600         } else {
1601                 /* limit has not been reached, determine "free space" */
1602                 *dfree = MIN(*dfree, limit - usage);
1603                 *dsize = MIN(*dsize, limit);
1604         }
1605 }
1606
1607 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
1608                                    bool small_query, uint64_t *bsize,
1609                                    uint64_t *dfree, uint64_t *dsize)
1610 {
1611         struct security_unix_token *utok;
1612         struct gpfs_quotaInfo qi_user, qi_group, qi_fset;
1613         struct gpfs_config_data *config;
1614         int err, fset_id;
1615         time_t cur_time;
1616
1617         SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
1618                                 return (uint64_t)-1);
1619         if (!config->dfreequota) {
1620                 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1621                                               bsize, dfree, dsize);
1622         }
1623
1624         err = sys_fsusage(path, dfree, dsize);
1625         if (err) {
1626                 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
1627                 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1628                                               bsize, dfree, dsize);
1629         }
1630
1631         /* sys_fsusage returns units of 512 bytes */
1632         *bsize = 512;
1633
1634         DEBUG(10, ("fs dfree %llu, dsize %llu\n",
1635                    (unsigned long long)*dfree, (unsigned long long)*dsize));
1636
1637         utok = handle->conn->session_info->unix_token;
1638         err = vfs_gpfs_get_quotas(path, utok->uid, utok->gid, &fset_id,
1639                                   &qi_user, &qi_group, &qi_fset);
1640         if (err) {
1641                 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1642                                               bsize, dfree, dsize);
1643         }
1644
1645         cur_time = time(NULL);
1646
1647         /* Adjust free space and size according to quota limits. */
1648         vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
1649         vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
1650
1651         /* Id 0 indicates the default quota, not an actual quota */
1652         if (fset_id != 0) {
1653                 vfs_gpfs_disk_free_quota(qi_fset, cur_time, dfree, dsize);
1654         }
1655
1656         disk_norm(small_query, bsize, dfree, dsize);
1657         return *dfree;
1658 }
1659
1660 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
1661                                       enum timestamp_set_resolution *p_ts_res)
1662 {
1663         struct gpfs_config_data *config;
1664         uint32_t next;
1665
1666         next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
1667
1668         SMB_VFS_HANDLE_GET_DATA(handle, config,
1669                                 struct gpfs_config_data,
1670                                 return next);
1671
1672         if (config->hsm) {
1673                 next |= FILE_SUPPORTS_REMOTE_STORAGE;
1674         }
1675         return next;
1676 }
1677
1678 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
1679                          struct smb_filename *smb_fname, files_struct *fsp,
1680                          int flags, mode_t mode)
1681 {
1682         struct gpfs_config_data *config;
1683
1684         SMB_VFS_HANDLE_GET_DATA(handle, config,
1685                                 struct gpfs_config_data,
1686                                 return -1);
1687
1688         if (config->syncio) {
1689                 flags |= O_SYNC;
1690         }
1691         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1692 }
1693
1694
1695 static struct vfs_fn_pointers vfs_gpfs_fns = {
1696         .connect_fn = vfs_gpfs_connect,
1697         .disk_free_fn = vfs_gpfs_disk_free,
1698         .fs_capabilities_fn = vfs_gpfs_capabilities,
1699         .kernel_flock_fn = vfs_gpfs_kernel_flock,
1700         .linux_setlease_fn = vfs_gpfs_setlease,
1701         .get_real_filename_fn = vfs_gpfs_get_real_filename,
1702         .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
1703         .get_nt_acl_fn = gpfsacl_get_nt_acl,
1704         .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
1705         .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
1706         .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
1707         .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
1708         .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
1709         .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
1710         .chmod_fn = vfs_gpfs_chmod,
1711         .fchmod_fn = vfs_gpfs_fchmod,
1712         .close_fn = vfs_gpfs_close,
1713         .setxattr_fn = gpfs_set_xattr,
1714         .getxattr_fn = gpfs_get_xattr,
1715         .stat_fn = vfs_gpfs_stat,
1716         .fstat_fn = vfs_gpfs_fstat,
1717         .lstat_fn = vfs_gpfs_lstat,
1718         .ntimes_fn = vfs_gpfs_ntimes,
1719         .is_offline_fn = vfs_gpfs_is_offline,
1720         .aio_force_fn = vfs_gpfs_aio_force,
1721         .sendfile_fn = vfs_gpfs_sendfile,
1722         .fallocate_fn = vfs_gpfs_fallocate,
1723         .open_fn = vfs_gpfs_open,
1724         .ftruncate_fn = vfs_gpfs_ftruncate
1725 };
1726
1727 NTSTATUS vfs_gpfs_init(void);
1728 NTSTATUS vfs_gpfs_init(void)
1729 {
1730         init_gpfs();
1731
1732         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
1733                                 &vfs_gpfs_fns);
1734 }