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