s3:vfs_gpfs: fix ACL length calculation
[Samba/bjacke.git] / source3 / modules / vfs_gpfs.c
blob4e4df22ebebdc1cb9cb5ed8e49fe82cfd833f945
1 /*
2 Unix SMB/CIFS implementation.
3 Wrap gpfs calls in vfs functions.
5 Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
7 Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
8 and Gomati Mohanan <gomati.mohanan@in.ibm.com>
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.
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.
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/>.
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
27 #include "include/smbprofile.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 #include <gpfs_gpl.h>
33 #include "nfs4_acls.h"
34 #include "vfs_gpfs.h"
35 #include "system/filesys.h"
36 #include "auth.h"
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;
52 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
53 uint32 share_mode, uint32 access_mask)
56 struct gpfs_config_data *config;
57 int ret = 0;
59 SMB_VFS_HANDLE_GET_DATA(handle, config,
60 struct gpfs_config_data,
61 return -1);
63 START_PROFILE(syscall_kernel_flock);
65 kernel_flock(fsp->fh->fd, share_mode, access_mask);
67 if (config->sharemodes
68 && !set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
69 ret = -1;
72 END_PROFILE(syscall_kernel_flock);
74 return ret;
77 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
80 struct gpfs_config_data *config;
82 SMB_VFS_HANDLE_GET_DATA(handle, config,
83 struct gpfs_config_data,
84 return -1);
86 if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
87 set_gpfs_sharemode(fsp, 0, 0);
90 return SMB_VFS_NEXT_CLOSE(handle, fsp);
93 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
94 int leasetype)
96 struct gpfs_config_data *config;
97 int ret=0;
99 SMB_VFS_HANDLE_GET_DATA(handle, config,
100 struct gpfs_config_data,
101 return -1);
103 if (linux_set_lease_sighandler(fsp->fh->fd) == -1)
104 return -1;
106 START_PROFILE(syscall_linux_setlease);
108 if (config->leases) {
109 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
112 END_PROFILE(syscall_linux_setlease);
114 return ret;
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)
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;
130 SMB_VFS_HANDLE_GET_DATA(handle, config,
131 struct gpfs_config_data,
132 return -1);
134 if (!config->getrealfilename) {
135 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
136 mem_ctx, found_name);
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);
145 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
146 if (full_path == NULL) {
147 errno = ENOMEM;
148 return -1;
151 buflen = sizeof(real_pathname) - 1;
153 result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
154 &buflen);
156 TALLOC_FREE(full_path);
158 if ((result == -1) && (errno == ENOSYS)) {
159 return SMB_VFS_NEXT_GET_REAL_FILENAME(
160 handle, path, name, mem_ctx, found_name);
163 if (result == -1) {
164 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
165 strerror(errno)));
166 return -1;
170 * GPFS does not necessarily null-terminate the returned path
171 * but instead returns the buffer length in buflen.
174 if (buflen < sizeof(real_pathname)) {
175 real_pathname[buflen] = '\0';
176 } else {
177 real_pathname[sizeof(real_pathname)-1] = '\0';
180 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
181 path, name, real_pathname));
183 name = strrchr_m(real_pathname, '/');
184 if (name == NULL) {
185 errno = ENOENT;
186 return -1;
189 *found_name = talloc_strdup(mem_ctx, name+1);
190 if (*found_name == NULL) {
191 errno = ENOMEM;
192 return -1;
195 return 0;
198 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
200 gpfs_aclCount_t i;
201 if (gacl==NULL)
203 DEBUG(0, ("gpfs acl is NULL\n"));
204 return;
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++)
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));
218 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
220 struct gpfs_acl *acl;
221 size_t len = 200;
222 int ret;
223 TALLOC_CTX *mem_ctx = talloc_tos();
225 acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
226 if (acl == NULL) {
227 errno = ENOMEM;
228 return NULL;
231 acl->acl_len = len;
232 acl->acl_level = 0;
233 acl->acl_version = 0;
234 acl->acl_type = type;
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;
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;
251 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
253 if (ret != 0)
255 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
256 return NULL;
259 return acl;
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
268 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
270 gpfs_aclCount_t i;
271 struct gpfs_acl *gacl = NULL;
272 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
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;
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;
288 *ppacl = smb_create_smb4acl();
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));
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));
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 */
319 } else {
320 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
321 smbace.who.gid = gace->aceWho;
322 else
323 smbace.who.uid = gace->aceWho;
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;
339 smbace.aceType = gace->aceType;
340 smbace.aceFlags = gace->aceFlags;
341 smbace.aceMask = gace->aceMask;
342 smb_add_ace4(*ppacl, &smbace);
345 return 0;
348 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
349 files_struct *fsp, uint32 security_info,
350 struct security_descriptor **ppdesc)
352 SMB4ACL_T *pacl = NULL;
353 int result;
354 struct gpfs_config_data *config;
356 *ppdesc = NULL;
358 SMB_VFS_HANDLE_GET_DATA(handle, config,
359 struct gpfs_config_data,
360 return NT_STATUS_INTERNAL_ERROR);
362 if (!config->acl) {
363 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info, ppdesc);
366 result = gpfs_get_nfs4_acl(fsp->fsp_name->base_name, &pacl);
368 if (result == 0)
369 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
371 if (result > 0) {
372 DEBUG(10, ("retrying with posix acl...\n"));
373 return posix_fget_nt_acl(fsp, security_info, ppdesc);
376 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
377 return map_nt_error_from_unix(errno);
380 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
381 const char *name,
382 uint32 security_info, struct security_descriptor **ppdesc)
384 SMB4ACL_T *pacl = NULL;
385 int result;
386 struct gpfs_config_data *config;
388 *ppdesc = NULL;
390 SMB_VFS_HANDLE_GET_DATA(handle, config,
391 struct gpfs_config_data,
392 return NT_STATUS_INTERNAL_ERROR);
394 if (!config->acl) {
395 return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info, ppdesc);
398 result = gpfs_get_nfs4_acl(name, &pacl);
400 if (result == 0)
401 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
403 if (result > 0) {
404 DEBUG(10, ("retrying with posix acl...\n"));
405 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
408 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
409 return map_nt_error_from_unix(errno);
412 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
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();
420 gacl_len = offsetof(gpfs_acl_t, ace_v4) + smb_get_naces(smbacl) *
421 sizeof(gpfs_ace_v4_t);
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;
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... */
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);
440 gace->aceType = aceprop->aceType;
441 gace->aceFlags = aceprop->aceFlags;
442 gace->aceMask = aceprop->aceMask;
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 :-)
450 if (!fsp->is_directory
452 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
453 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
455 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
456 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
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;
466 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
468 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
470 switch(aceprop->who.special_id)
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 !!! */
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;
493 gacl->acl_nace++;
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;
504 DEBUG(10, ("gpfs_putacl succeeded\n"));
505 return True;
508 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
510 struct gpfs_acl *acl;
511 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
513 acl = gpfs_getacl_alloc(fsp->fsp_name->base_name, 0);
514 if (acl == NULL)
515 return result;
517 if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
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;
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);
533 return result;
536 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
538 struct gpfs_config_data *config;
540 SMB_VFS_HANDLE_GET_DATA(handle, config,
541 struct gpfs_config_data,
542 return NT_STATUS_INTERNAL_ERROR);
544 if (!config->acl) {
545 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
548 return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
551 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
553 SMB_ACL_T result;
554 gpfs_aclCount_t i;
556 result = sys_acl_init(pacl->acl_nace);
557 if (result == NULL) {
558 errno = ENOMEM;
559 return NULL;
562 result->count = pacl->acl_nace;
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];
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));
572 switch (g_ace->ace_type) {
573 case GPFS_ACL_USER:
574 ace->a_type = SMB_ACL_USER;
575 ace->uid = (uid_t)g_ace->ace_who;
576 break;
577 case GPFS_ACL_USER_OBJ:
578 ace->a_type = SMB_ACL_USER_OBJ;
579 break;
580 case GPFS_ACL_GROUP:
581 ace->a_type = SMB_ACL_GROUP;
582 ace->gid = (gid_t)g_ace->ace_who;
583 break;
584 case GPFS_ACL_GROUP_OBJ:
585 ace->a_type = SMB_ACL_GROUP_OBJ;
586 break;
587 case GPFS_ACL_OTHER:
588 ace->a_type = SMB_ACL_OTHER;
589 break;
590 case GPFS_ACL_MASK:
591 ace->a_type = SMB_ACL_MASK;
592 break;
593 default:
594 DEBUG(10, ("Got invalid ace_type: %d\n",
595 g_ace->ace_type));
596 errno = EINVAL;
597 SAFE_FREE(result);
598 return NULL;
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;
609 DEBUGADD(10, ("Converted to %d perm %x\n",
610 ace->a_type, ace->a_perm));
613 return result;
616 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
618 struct gpfs_acl *pacl;
619 SMB_ACL_T result = NULL;
621 pacl = gpfs_getacl_alloc(path, type);
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;
629 goto done;
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;
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));
643 result = gpfs2smb_acl(pacl);
644 if (result != NULL) {
645 errno = 0;
648 done:
650 if (errno != 0) {
651 SAFE_FREE(result);
653 return result;
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)
660 gpfs_aclType_t gpfs_type;
661 struct gpfs_config_data *config;
663 SMB_VFS_HANDLE_GET_DATA(handle, config,
664 struct gpfs_config_data,
665 return NULL);
667 if (!config->acl) {
668 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p, type);
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");
683 return gpfsacl_get_posix_acl(path_p, gpfs_type);
686 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
687 files_struct *fsp)
689 struct gpfs_config_data *config;
691 SMB_VFS_HANDLE_GET_DATA(handle, config,
692 struct gpfs_config_data,
693 return NULL);
695 if (!config->acl) {
696 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp);
699 return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
700 GPFS_ACL_TYPE_ACCESS);
703 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
704 SMB_ACL_TYPE_T type)
706 gpfs_aclLen_t len;
707 struct gpfs_acl *result;
708 int i;
710 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
712 len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
713 sizeof(gpfs_ace_v1_t);
715 result = (struct gpfs_acl *)SMB_MALLOC(len);
716 if (result == NULL) {
717 errno = ENOMEM;
718 return result;
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;
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];
732 DEBUG(10, ("Converting type %d perm %x\n",
733 (int)ace->a_type, (int)ace->a_perm));
735 g_ace->ace_perm = 0;
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;
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;
778 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
779 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
782 return result;
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)
790 struct gpfs_acl *gpfs_acl;
791 int result;
792 struct gpfs_config_data *config;
794 SMB_VFS_HANDLE_GET_DATA(handle, config,
795 struct gpfs_config_data,
796 return -1);
798 if (!config->acl) {
799 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, type, theacl);
802 gpfs_acl = smb2gpfs_acl(theacl, type);
803 if (gpfs_acl == NULL) {
804 return -1;
807 result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
809 SAFE_FREE(gpfs_acl);
810 return result;
813 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
814 files_struct *fsp,
815 SMB_ACL_T theacl)
817 struct gpfs_config_data *config;
819 SMB_VFS_HANDLE_GET_DATA(handle, config,
820 struct gpfs_config_data,
821 return -1);
823 if (!config->acl) {
824 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
827 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
828 SMB_ACL_TYPE_ACCESS, theacl);
831 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
832 const char *path)
834 struct gpfs_config_data *config;
836 SMB_VFS_HANDLE_GET_DATA(handle, config,
837 struct gpfs_config_data,
838 return -1);
840 if (!config->acl) {
841 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
844 errno = ENOTSUP;
845 return -1;
849 * Assumed: mode bits are shiftable and standard
850 * Output: the new aceMask field for an smb nfs4 ace
852 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
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 */
859 int i;
860 uint32_t posix_mask = 0x01;
861 uint32_t posix_bit;
862 uint32_t nfs4_bits;
864 for(i=0; i<3; i++) {
865 nfs4_bits = posix_nfs4map[i];
866 posix_bit = rwx & posix_mask;
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 */
881 posix_mask <<= 1;
884 return aceMask;
887 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
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;
897 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
899 result = gpfs_get_nfs4_acl(path, &pacl);
900 if (result)
901 return result;
903 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
904 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
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;
911 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
912 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
913 specid <= SMB_ACE4_WHO_EVERYONE) {
915 uint32_t newMask;
917 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
918 haveAllowEntry[specid] = True;
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));
928 ace->aceMask = newMask;
932 /* make sure we have at least ALLOW entries
933 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
934 * - if necessary
936 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
937 SMB_ACE4PROP_T ace;
939 if (haveAllowEntry[i]==True)
940 continue;
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;
947 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
948 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
950 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
951 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
953 /* don't add unnecessary aces */
954 if (!ace.aceMask)
955 continue;
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));
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;
971 /* put the acl */
972 if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False) {
973 TALLOC_FREE(fake_fsp.fsp_name);
974 return -1;
977 TALLOC_FREE(fake_fsp.fsp_name);
978 return 0; /* ok for [f]chmod */
981 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
983 struct smb_filename *smb_fname_cpath;
984 int rc;
985 NTSTATUS status;
987 status = create_synthetic_smb_fname(
988 talloc_tos(), path, NULL, NULL, &smb_fname_cpath);
990 if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
991 return -1;
994 /* avoid chmod() if possible, to preserve acls */
995 if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
996 return 0;
999 rc = gpfsacl_emu_chmod(path, mode);
1000 if (rc == 1)
1001 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1002 return rc;
1005 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1007 SMB_STRUCT_STAT st;
1008 int rc;
1010 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1011 return -1;
1014 /* avoid chmod() if possible, to preserve acls */
1015 if ((st.st_ex_mode & ~S_IFMT) == mode) {
1016 return 0;
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;
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;
1036 SMB_VFS_HANDLE_GET_DATA(handle, config,
1037 struct gpfs_config_data,
1038 return -1);
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);
1045 DEBUG(10, ("gpfs_set_xattr: %s \n",path));
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);
1053 blob.data = (uint8_t *)attrstr;
1054 blob.length = size;
1056 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
1057 (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
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;
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;
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;
1077 dosmode = dosattrib.info.info3.attrib;
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;
1084 if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1085 attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1087 if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1088 attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1090 if (dosmode & FILE_ATTRIBUTE_READONLY){
1091 attrs.winAttrs |= GPFS_WINATTR_READONLY;
1093 if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1094 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
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);
1106 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1107 return -1;
1110 DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1111 return 0;
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;
1122 SMB_VFS_HANDLE_GET_DATA(handle, config,
1123 struct gpfs_config_data,
1124 return -1);
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);
1131 DEBUG(10, ("gpfs_get_xattr: %s \n",path));
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);
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);
1146 DEBUG(1, ("gpfs_get_xattr: Get GPFS attributes failed: "
1147 "%d (%s)\n", ret, strerror(errno)));
1148 return -1;
1151 DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
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;
1157 if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1158 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1160 if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1161 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1163 if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1164 dosmode |= FILE_ATTRIBUTE_READONLY;
1166 if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1167 dosmode |= FILE_ATTRIBUTE_SPARSE;
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;
1176 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1177 struct smb_filename *smb_fname)
1179 struct gpfs_winattr attrs;
1180 char *fname = NULL;
1181 NTSTATUS status;
1182 int ret;
1183 struct gpfs_config_data *config;
1185 SMB_VFS_HANDLE_GET_DATA(handle, config,
1186 struct gpfs_config_data,
1187 return -1);
1189 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1190 if (ret == -1) {
1191 return -1;
1194 if (!config->winattr) {
1195 return 0;
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;
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;
1211 return 0;
1214 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1215 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1217 struct gpfs_winattr attrs;
1218 int ret;
1219 struct gpfs_config_data *config;
1221 SMB_VFS_HANDLE_GET_DATA(handle, config,
1222 struct gpfs_config_data,
1223 return -1);
1225 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1226 if (ret == -1) {
1227 return -1;
1229 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1230 return 0;
1232 if (!config->winattr) {
1233 return 0;
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;
1242 return 0;
1245 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1246 struct smb_filename *smb_fname)
1248 struct gpfs_winattr attrs;
1249 char *path = NULL;
1250 NTSTATUS status;
1251 int ret;
1252 struct gpfs_config_data *config;
1254 SMB_VFS_HANDLE_GET_DATA(handle, config,
1255 struct gpfs_config_data,
1256 return -1);
1258 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1259 if (ret == -1) {
1260 return -1;
1262 if (!config->winattr) {
1263 return 0;
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;
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;
1279 return 0;
1282 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1283 const struct smb_filename *smb_fname,
1284 struct smb_file_time *ft)
1287 struct gpfs_winattr attrs;
1288 int ret;
1289 char *path = NULL;
1290 NTSTATUS status;
1291 struct gpfs_config_data *config;
1293 SMB_VFS_HANDLE_GET_DATA(handle, config,
1294 struct gpfs_config_data,
1295 return -1);
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)));
1304 return -1;
1307 if(null_timespec(ft->create_time)){
1308 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1309 return 0;
1312 if (!config->winattr) {
1313 return 0;
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;
1322 attrs.winAttrs = 0;
1323 attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1324 attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
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;
1332 return 0;
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)
1340 int ret;
1341 struct gpfs_config_data *config;
1343 SMB_VFS_HANDLE_GET_DATA(handle, config,
1344 struct gpfs_config_data,
1345 return -1);
1347 if (!config->prealloc) {
1348 /* you should better not run fallocate() on GPFS at all */
1349 errno = ENOTSUP;
1350 return -1;
1353 if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1354 DEBUG(10, ("Unsupported VFS_FALLOCATE_KEEP_SIZE\n"));
1355 errno = ENOTSUP;
1356 return -1;
1359 ret = smbd_gpfs_prealloc(fsp->fh->fd, offset, len);
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"));
1369 return ret;
1372 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1373 off_t len)
1375 int result;
1376 struct gpfs_config_data *config;
1378 SMB_VFS_HANDLE_GET_DATA(handle, config,
1379 struct gpfs_config_data,
1380 return -1);
1382 if (!config->ftruncate) {
1383 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1386 result = smbd_gpfs_ftruncate(fsp->fh->fd, len);
1387 if ((result == -1) && (errno == ENOSYS)) {
1388 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1390 return result;
1393 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1394 const struct smb_filename *fname,
1395 SMB_STRUCT_STAT *sbuf)
1397 struct gpfs_winattr attrs;
1398 char *path = NULL;
1399 NTSTATUS status;
1400 struct gpfs_config_data *config;
1402 SMB_VFS_HANDLE_GET_DATA(handle, config,
1403 struct gpfs_config_data,
1404 return -1);
1406 if (!config->winattr) {
1407 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
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;
1416 if (VALID_STAT(*sbuf)) {
1417 attrs.winAttrs = sbuf->vfs_private;
1418 } else {
1419 int ret;
1420 ret = get_gpfs_winattrs(path, &attrs);
1422 if (ret == -1) {
1423 TALLOC_FREE(path);
1424 return false;
1427 if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1428 DEBUG(10, ("%s is offline\n", path));
1429 TALLOC_FREE(path);
1430 return true;
1432 DEBUG(10, ("%s is online\n", path));
1433 TALLOC_FREE(path);
1434 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1437 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1438 struct files_struct *fsp)
1440 return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
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)
1447 if ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0) {
1448 errno = ENOSYS;
1449 return -1;
1451 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1454 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1455 const char *service, const char *user)
1457 struct gpfs_config_data *config;
1458 int ret;
1460 smbd_gpfs_lib_init();
1462 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1464 if (ret < 0) {
1465 return ret;
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;
1475 config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1476 "sharemodes", true);
1478 config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1479 "leases", true);
1481 config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1482 "hsm", false);
1484 config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1485 "syncio", false);
1487 config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1488 "winattr", false);
1490 config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1491 "ftruncate", true);
1493 config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1494 "getrealfilename", true);
1496 config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1497 "dfreequota", false);
1499 config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
1500 "prealloc", true);
1502 config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
1504 SMB_VFS_HANDLE_SET_DATA(handle, config,
1505 NULL, struct gpfs_config_data,
1506 return -1);
1508 return 0;
1511 static int vfs_gpfs_get_quotas(const char *path, uid_t uid, gid_t gid,
1512 int *fset_id,
1513 struct gpfs_quotaInfo *qi_user,
1514 struct gpfs_quotaInfo *qi_group,
1515 struct gpfs_quotaInfo *qi_fset)
1517 int err;
1519 err = get_gpfs_fset_id(path, fset_id);
1520 if (err) {
1521 DEBUG(0, ("Get fset id failed, errno %d.\n", errno));
1522 return err;
1525 err = get_gpfs_quota(path, GPFS_USRQUOTA, uid, qi_user);
1526 if (err) {
1527 return err;
1530 err = get_gpfs_quota(path, GPFS_GRPQUOTA, gid, qi_group);
1531 if (err) {
1532 return err;
1535 err = get_gpfs_quota(path, GPFS_FILESETQUOTA, *fset_id, qi_fset);
1536 if (err) {
1537 return err;
1540 return 0;
1543 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
1544 uint64_t *dfree, uint64_t *dsize)
1546 uint64_t usage, limit;
1549 * The quota reporting is done in units of 1024 byte blocks, but
1550 * sys_fsusage uses units of 512 byte blocks, adjust the block number
1551 * accordingly. Also filter possibly negative usage counts from gpfs.
1553 usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
1554 limit = (uint64_t)qi.blockHardLimit * 2;
1557 * When the grace time for the exceeded soft block quota has been
1558 * exceeded, the soft block quota becomes an additional hard limit.
1560 if (qi.blockSoftLimit &&
1561 qi.blockGraceTime && cur_time > qi.blockGraceTime) {
1562 /* report disk as full */
1563 *dfree = 0;
1564 *dsize = MIN(*dsize, usage);
1567 if (!qi.blockHardLimit)
1568 return;
1570 if (usage >= limit) {
1571 /* report disk as full */
1572 *dfree = 0;
1573 *dsize = MIN(*dsize, usage);
1575 } else {
1576 /* limit has not been reached, determine "free space" */
1577 *dfree = MIN(*dfree, limit - usage);
1578 *dsize = MIN(*dsize, limit);
1582 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
1583 bool small_query, uint64_t *bsize,
1584 uint64_t *dfree, uint64_t *dsize)
1586 struct security_unix_token *utok;
1587 struct gpfs_quotaInfo qi_user, qi_group, qi_fset;
1588 struct gpfs_config_data *config;
1589 int err, fset_id;
1590 time_t cur_time;
1592 SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
1593 return (uint64_t)-1);
1594 if (!config->dfreequota) {
1595 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1596 bsize, dfree, dsize);
1599 err = sys_fsusage(path, dfree, dsize);
1600 if (err) {
1601 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
1602 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1603 bsize, dfree, dsize);
1606 /* sys_fsusage returns units of 512 bytes */
1607 *bsize = 512;
1609 DEBUG(10, ("fs dfree %llu, dsize %llu\n",
1610 (unsigned long long)*dfree, (unsigned long long)*dsize));
1612 utok = handle->conn->session_info->unix_token;
1613 err = vfs_gpfs_get_quotas(path, utok->uid, utok->gid, &fset_id,
1614 &qi_user, &qi_group, &qi_fset);
1615 if (err) {
1616 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1617 bsize, dfree, dsize);
1620 cur_time = time(NULL);
1622 /* Adjust free space and size according to quota limits. */
1623 vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
1624 vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
1626 /* Id 0 indicates the default quota, not an actual quota */
1627 if (fset_id != 0) {
1628 vfs_gpfs_disk_free_quota(qi_fset, cur_time, dfree, dsize);
1631 disk_norm(small_query, bsize, dfree, dsize);
1632 return *dfree;
1635 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
1636 enum timestamp_set_resolution *p_ts_res)
1638 struct gpfs_config_data *config;
1639 uint32_t next;
1641 next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
1643 SMB_VFS_HANDLE_GET_DATA(handle, config,
1644 struct gpfs_config_data,
1645 return next);
1647 if (config->hsm) {
1648 next |= FILE_SUPPORTS_REMOTE_STORAGE;
1650 return next;
1653 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
1654 struct smb_filename *smb_fname, files_struct *fsp,
1655 int flags, mode_t mode)
1657 struct gpfs_config_data *config;
1659 SMB_VFS_HANDLE_GET_DATA(handle, config,
1660 struct gpfs_config_data,
1661 return -1);
1663 if (config->syncio) {
1664 flags |= O_SYNC;
1666 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1670 static struct vfs_fn_pointers vfs_gpfs_fns = {
1671 .connect_fn = vfs_gpfs_connect,
1672 .disk_free_fn = vfs_gpfs_disk_free,
1673 .fs_capabilities_fn = vfs_gpfs_capabilities,
1674 .kernel_flock_fn = vfs_gpfs_kernel_flock,
1675 .linux_setlease_fn = vfs_gpfs_setlease,
1676 .get_real_filename_fn = vfs_gpfs_get_real_filename,
1677 .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
1678 .get_nt_acl_fn = gpfsacl_get_nt_acl,
1679 .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
1680 .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
1681 .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
1682 .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
1683 .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
1684 .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
1685 .chmod_fn = vfs_gpfs_chmod,
1686 .fchmod_fn = vfs_gpfs_fchmod,
1687 .close_fn = vfs_gpfs_close,
1688 .setxattr_fn = gpfs_set_xattr,
1689 .getxattr_fn = gpfs_get_xattr,
1690 .stat_fn = vfs_gpfs_stat,
1691 .fstat_fn = vfs_gpfs_fstat,
1692 .lstat_fn = vfs_gpfs_lstat,
1693 .ntimes_fn = vfs_gpfs_ntimes,
1694 .is_offline_fn = vfs_gpfs_is_offline,
1695 .aio_force_fn = vfs_gpfs_aio_force,
1696 .sendfile_fn = vfs_gpfs_sendfile,
1697 .fallocate_fn = vfs_gpfs_fallocate,
1698 .open_fn = vfs_gpfs_open,
1699 .ftruncate_fn = vfs_gpfs_ftruncate
1702 NTSTATUS vfs_gpfs_init(void);
1703 NTSTATUS vfs_gpfs_init(void)
1705 init_gpfs();
1707 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
1708 &vfs_gpfs_fns);