vfs_acl_common: check for ignore_system_acls before fetching filesystem ACL
[Samba.git] / source3 / modules / vfs_gpfs.c
blob2216d1d15db0332bdabd7dffde77f895e4f9e306
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba VFS module for GPFS filesystem
4 * Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
5 * Copyright (C) Christof Schmitt 2015
6 * Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
7 * and Gomati Mohanan <gomati.mohanan@in.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "smbd/smbd.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "include/smbprofile.h"
27 #include "modules/non_posix_acls.h"
28 #include "libcli/security/security.h"
29 #include "nfs4_acls.h"
30 #include "system/filesys.h"
31 #include "auth.h"
32 #include "lib/util/tevent_unix.h"
33 #include "lib/util/gpfswrap.h"
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_VFS
38 #ifndef GPFS_GETACL_NATIVE
39 #define GPFS_GETACL_NATIVE 0x00000004
40 #endif
42 struct gpfs_config_data {
43 bool sharemodes;
44 bool leases;
45 bool hsm;
46 bool syncio;
47 bool winattr;
48 bool ftruncate;
49 bool getrealfilename;
50 bool dfreequota;
51 bool prealloc;
52 bool acl;
53 bool settimes;
54 bool recalls;
57 struct gpfs_fsp_extension {
58 bool offline;
61 static inline unsigned int gpfs_acl_flags(gpfs_acl_t *gacl)
63 if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
64 return gacl->v4Level1.acl_flags;
66 return 0;
69 static inline gpfs_ace_v4_t *gpfs_ace_ptr(gpfs_acl_t *gacl, unsigned int i)
71 if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
72 return &gacl->v4Level1.ace_v4[i];
74 return &gacl->ace_v4[i];
77 static bool set_gpfs_sharemode(files_struct *fsp, uint32_t access_mask,
78 uint32_t share_access)
80 unsigned int allow = GPFS_SHARE_NONE;
81 unsigned int deny = GPFS_DENY_NONE;
82 int result;
84 if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
85 /* No real file, don't disturb */
86 return True;
89 allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
90 DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
91 allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
92 GPFS_SHARE_READ : 0;
94 if (allow == GPFS_SHARE_NONE) {
95 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
97 else {
98 deny |= (share_access & FILE_SHARE_WRITE) ?
99 0 : GPFS_DENY_WRITE;
100 deny |= (share_access & (FILE_SHARE_READ)) ?
101 0 : GPFS_DENY_READ;
103 DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
104 access_mask, allow, share_access, deny));
106 result = gpfswrap_set_share(fsp->fh->fd, allow, deny);
107 if (result != 0) {
108 if (errno == ENOSYS) {
109 DEBUG(5, ("VFS module vfs_gpfs loaded, but gpfs "
110 "set_share function support not available. "
111 "Allowing access\n"));
112 return True;
113 } else {
114 DEBUG(10, ("gpfs_set_share failed: %s\n",
115 strerror(errno)));
119 return (result == 0);
122 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
123 uint32_t share_mode, uint32_t access_mask)
126 struct gpfs_config_data *config;
127 int ret = 0;
129 START_PROFILE(syscall_kernel_flock);
131 SMB_VFS_HANDLE_GET_DATA(handle, config,
132 struct gpfs_config_data,
133 return -1);
135 if(!config->sharemodes) {
136 return 0;
140 * A named stream fsp will have the basefile open in the fsp
141 * fd, so lacking a distinct fd for the stream we have to skip
142 * kernel_flock and set_gpfs_sharemode for stream.
144 if (is_ntfs_stream_smb_fname(fsp->fsp_name) &&
145 !is_ntfs_default_stream_smb_fname(fsp->fsp_name)) {
146 DEBUG(2,("%s: kernel_flock on stream\n", fsp_str_dbg(fsp)));
147 return 0;
150 kernel_flock(fsp->fh->fd, share_mode, access_mask);
152 if (!set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
153 ret = -1;
156 END_PROFILE(syscall_kernel_flock);
158 return ret;
161 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
164 struct gpfs_config_data *config;
166 SMB_VFS_HANDLE_GET_DATA(handle, config,
167 struct gpfs_config_data,
168 return -1);
170 if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
171 set_gpfs_sharemode(fsp, 0, 0);
174 return SMB_VFS_NEXT_CLOSE(handle, fsp);
177 static int set_gpfs_lease(int fd, int leasetype)
179 int gpfs_type = GPFS_LEASE_NONE;
181 if (leasetype == F_RDLCK) {
182 gpfs_type = GPFS_LEASE_READ;
184 if (leasetype == F_WRLCK) {
185 gpfs_type = GPFS_LEASE_WRITE;
188 /* we unconditionally set CAP_LEASE, rather than looking for
189 -1/EACCES as there is a bug in some versions of
190 libgpfs_gpl.so which results in a leaked fd on /dev/ss0
191 each time we try this with the wrong capabilities set
193 linux_set_lease_capability();
194 return gpfswrap_set_lease(fd, gpfs_type);
197 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
198 int leasetype)
200 struct gpfs_config_data *config;
201 int ret=0;
203 START_PROFILE(syscall_linux_setlease);
205 SMB_VFS_HANDLE_GET_DATA(handle, config,
206 struct gpfs_config_data,
207 return -1);
209 if (linux_set_lease_sighandler(fsp->fh->fd) == -1) {
210 ret = -1;
211 goto failure;
214 if (config->leases) {
216 * Ensure the lease owner is root to allow
217 * correct delivery of lease-break signals.
219 become_root();
220 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
221 unbecome_root();
224 failure:
225 END_PROFILE(syscall_linux_setlease);
227 return ret;
230 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
231 const char *path,
232 const char *name,
233 TALLOC_CTX *mem_ctx,
234 char **found_name)
236 int result;
237 char *full_path;
238 char real_pathname[PATH_MAX+1];
239 int buflen;
240 bool mangled;
241 struct gpfs_config_data *config;
243 SMB_VFS_HANDLE_GET_DATA(handle, config,
244 struct gpfs_config_data,
245 return -1);
247 if (!config->getrealfilename) {
248 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
249 mem_ctx, found_name);
252 mangled = mangle_is_mangled(name, handle->conn->params);
253 if (mangled) {
254 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
255 mem_ctx, found_name);
258 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
259 if (full_path == NULL) {
260 errno = ENOMEM;
261 return -1;
264 buflen = sizeof(real_pathname) - 1;
266 result = gpfswrap_get_realfilename_path(full_path, real_pathname,
267 &buflen);
269 TALLOC_FREE(full_path);
271 if ((result == -1) && (errno == ENOSYS)) {
272 return SMB_VFS_NEXT_GET_REAL_FILENAME(
273 handle, path, name, mem_ctx, found_name);
276 if (result == -1) {
277 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
278 strerror(errno)));
279 return -1;
283 * GPFS does not necessarily null-terminate the returned path
284 * but instead returns the buffer length in buflen.
287 if (buflen < sizeof(real_pathname)) {
288 real_pathname[buflen] = '\0';
289 } else {
290 real_pathname[sizeof(real_pathname)-1] = '\0';
293 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
294 path, name, real_pathname));
296 name = strrchr_m(real_pathname, '/');
297 if (name == NULL) {
298 errno = ENOENT;
299 return -1;
302 *found_name = talloc_strdup(mem_ctx, name+1);
303 if (*found_name == NULL) {
304 errno = ENOMEM;
305 return -1;
308 return 0;
311 static void sd2gpfs_control(uint16_t control, struct gpfs_acl *gacl)
313 unsigned int gpfs_aclflags = 0;
314 control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
315 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
316 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
317 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
318 gpfs_aclflags = control << 8;
319 if (!(control & SEC_DESC_DACL_PRESENT))
320 gpfs_aclflags |= ACL4_FLAG_NULL_DACL;
321 if (!(control & SEC_DESC_SACL_PRESENT))
322 gpfs_aclflags |= ACL4_FLAG_NULL_SACL;
323 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
324 gacl->v4Level1.acl_flags = gpfs_aclflags;
327 static uint16_t gpfs2sd_control(unsigned int gpfs_aclflags)
329 uint16_t control = gpfs_aclflags >> 8;
330 control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
331 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
332 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
333 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
334 control |= SEC_DESC_SELF_RELATIVE;
335 return control;
338 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
340 gpfs_aclCount_t i;
341 if (gacl==NULL)
343 DEBUG(0, ("gpfs acl is NULL\n"));
344 return;
347 DEBUG(level, ("len: %d, level: %d, version: %d, nace: %d, "
348 "control: %x\n",
349 gacl->acl_len, gacl->acl_level, gacl->acl_version,
350 gacl->acl_nace, gpfs_acl_flags(gacl)));
352 for(i=0; i<gacl->acl_nace; i++)
354 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
355 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, "
356 "iflags:0x%x, who:%u\n",
357 i, gace->aceType, gace->aceFlags, gace->aceMask,
358 gace->aceIFlags, gace->aceWho));
363 * get the ACL from GPFS, allocated on the specified mem_ctx
364 * internally retries when initial buffer was too small
366 * caller needs to cast result to either
367 * raw = yes: struct gpfs_opaque_acl
368 * raw = no: struct gpfs_acl
371 static void *vfs_gpfs_getacl(TALLOC_CTX *mem_ctx,
372 const char *fname,
373 const bool raw,
374 const gpfs_aclType_t type)
377 void *aclbuf;
378 size_t size = 512;
379 int ret, flags;
380 unsigned int *len;
381 size_t struct_size;
383 again:
385 aclbuf = talloc_zero_size(mem_ctx, size);
386 if (aclbuf == NULL) {
387 errno = ENOMEM;
388 return NULL;
391 if (raw) {
392 struct gpfs_opaque_acl *buf = (struct gpfs_opaque_acl *) aclbuf;
393 buf->acl_type = type;
394 flags = GPFS_GETACL_NATIVE;
395 len = (unsigned int *) &(buf->acl_buffer_len);
396 struct_size = sizeof(struct gpfs_opaque_acl);
397 } else {
398 struct gpfs_acl *buf = (struct gpfs_acl *) aclbuf;
399 buf->acl_type = type;
400 buf->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
401 flags = GPFS_GETACL_STRUCT;
402 len = &(buf->acl_len);
403 /* reserve space for control flags in gpfs 3.5 and beyond */
404 struct_size = sizeof(struct gpfs_acl) + sizeof(unsigned int);
407 /* set the length of the buffer as input value */
408 *len = size;
410 errno = 0;
411 ret = gpfswrap_getacl(discard_const_p(char, fname), flags, aclbuf);
412 if ((ret != 0) && (errno == ENOSPC)) {
414 * get the size needed to accommodate the complete buffer
416 * the value returned only applies to the ACL blob in the
417 * struct so make sure to also have headroom for the first
418 * struct members by adding room for the complete struct
419 * (might be a few bytes too much then)
421 size = *len + struct_size;
422 talloc_free(aclbuf);
423 DEBUG(10, ("Increasing ACL buffer size to %zu\n", size));
424 goto again;
427 if (ret != 0) {
428 DEBUG(5, ("smbd_gpfs_getacl failed with %s\n",
429 strerror(errno)));
430 talloc_free(aclbuf);
431 return NULL;
434 return aclbuf;
437 /* Tries to get nfs4 acls and returns SMB ACL allocated.
438 * On failure returns 1 if it got non-NFSv4 ACL to prompt
439 * retry with POSIX ACL checks.
440 * On failure returns -1 if there is system (GPFS) error, check errno.
441 * Returns 0 on success
443 static int gpfs_get_nfs4_acl(TALLOC_CTX *mem_ctx, const char *fname,
444 struct SMB4ACL_T **ppacl)
446 gpfs_aclCount_t i;
447 struct gpfs_acl *gacl = NULL;
448 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
450 /* Get the ACL */
451 gacl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(), fname,
452 false, 0);
453 if (gacl == NULL) {
454 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
455 fname, strerror(errno)));
456 if (errno == ENODATA) {
458 * GPFS returns ENODATA for snapshot
459 * directories. Retry with POSIX ACLs check.
461 return 1;
464 return -1;
467 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
468 DEBUG(10, ("Got non-nfsv4 acl\n"));
469 /* Retry with POSIX ACLs check */
470 talloc_free(gacl);
471 return 1;
474 *ppacl = smb_create_smb4acl(mem_ctx);
476 if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
477 uint16_t control = gpfs2sd_control(gpfs_acl_flags(gacl));
478 smbacl4_set_controlflags(*ppacl, control);
481 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d, control: %x\n",
482 gacl->acl_len, gacl->acl_level, gacl->acl_version,
483 gacl->acl_nace, gpfs_acl_flags(gacl)));
485 for (i=0; i<gacl->acl_nace; i++) {
486 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
487 SMB_ACE4PROP_T smbace = { 0 };
488 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
489 "who: %d\n", gace->aceType, gace->aceIFlags,
490 gace->aceFlags, gace->aceMask, gace->aceWho));
492 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
493 smbace.flags |= SMB_ACE4_ID_SPECIAL;
494 switch (gace->aceWho) {
495 case ACE4_SPECIAL_OWNER:
496 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
497 break;
498 case ACE4_SPECIAL_GROUP:
499 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
500 break;
501 case ACE4_SPECIAL_EVERYONE:
502 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
503 break;
504 default:
505 DEBUG(8, ("invalid special gpfs id %d "
506 "ignored\n", gace->aceWho));
507 continue; /* don't add it */
509 } else {
510 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
511 smbace.who.gid = gace->aceWho;
512 else
513 smbace.who.uid = gace->aceWho;
516 /* remove redundant deny entries */
517 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
518 struct gpfs_ace_v4 *prev = gpfs_ace_ptr(gacl, i - 1);
519 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
520 prev->aceFlags == gace->aceFlags &&
521 prev->aceIFlags == gace->aceIFlags &&
522 (gace->aceMask & prev->aceMask) == 0 &&
523 gace->aceWho == prev->aceWho) {
524 /* it's redundant - skip it */
525 continue;
529 smbace.aceType = gace->aceType;
530 smbace.aceFlags = gace->aceFlags;
531 smbace.aceMask = gace->aceMask;
532 smb_add_ace4(*ppacl, &smbace);
535 talloc_free(gacl);
537 return 0;
540 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
541 files_struct *fsp, uint32_t security_info,
542 TALLOC_CTX *mem_ctx,
543 struct security_descriptor **ppdesc)
545 struct SMB4ACL_T *pacl = NULL;
546 int result;
547 struct gpfs_config_data *config;
548 TALLOC_CTX *frame = talloc_stackframe();
549 NTSTATUS status;
551 *ppdesc = NULL;
553 SMB_VFS_HANDLE_GET_DATA(handle, config,
554 struct gpfs_config_data,
555 return NT_STATUS_INTERNAL_ERROR);
557 if (!config->acl) {
558 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
559 mem_ctx, ppdesc);
560 TALLOC_FREE(frame);
561 return status;
564 result = gpfs_get_nfs4_acl(frame, fsp->fsp_name->base_name, &pacl);
566 if (result == 0) {
567 status = smb_fget_nt_acl_nfs4(fsp, security_info, mem_ctx,
568 ppdesc, pacl);
569 TALLOC_FREE(frame);
570 return status;
573 if (result > 0) {
574 DEBUG(10, ("retrying with posix acl...\n"));
575 status = posix_fget_nt_acl(fsp, security_info,
576 mem_ctx, ppdesc);
577 TALLOC_FREE(frame);
578 return status;
581 TALLOC_FREE(frame);
583 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
584 return map_nt_error_from_unix(errno);
587 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
588 const char *name,
589 uint32_t security_info,
590 TALLOC_CTX *mem_ctx, struct security_descriptor **ppdesc)
592 struct SMB4ACL_T *pacl = NULL;
593 int result;
594 struct gpfs_config_data *config;
595 TALLOC_CTX *frame = talloc_stackframe();
596 NTSTATUS status;
598 *ppdesc = NULL;
600 SMB_VFS_HANDLE_GET_DATA(handle, config,
601 struct gpfs_config_data,
602 return NT_STATUS_INTERNAL_ERROR);
604 if (!config->acl) {
605 status = SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info,
606 mem_ctx, ppdesc);
607 TALLOC_FREE(frame);
608 return status;
611 result = gpfs_get_nfs4_acl(frame, name, &pacl);
613 if (result == 0) {
614 status = smb_get_nt_acl_nfs4(handle->conn, name, security_info,
615 mem_ctx, ppdesc, pacl);
616 TALLOC_FREE(frame);
617 return status;
620 if (result > 0) {
621 DEBUG(10, ("retrying with posix acl...\n"));
622 status = posix_get_nt_acl(handle->conn, name, security_info,
623 mem_ctx, ppdesc);
624 TALLOC_FREE(frame);
625 return status;
628 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
629 TALLOC_FREE(frame);
630 return map_nt_error_from_unix(errno);
633 static struct gpfs_acl *vfs_gpfs_smbacl2gpfsacl(TALLOC_CTX *mem_ctx,
634 files_struct *fsp,
635 struct SMB4ACL_T *smbacl,
636 bool controlflags)
638 struct gpfs_acl *gacl;
639 gpfs_aclLen_t gacl_len;
640 struct SMB4ACE_T *smbace;
642 gacl_len = offsetof(gpfs_acl_t, ace_v4) + sizeof(unsigned int)
643 + smb_get_naces(smbacl) * sizeof(gpfs_ace_v4_t);
645 gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
646 if (gacl == NULL) {
647 DEBUG(0, ("talloc failed\n"));
648 errno = ENOMEM;
649 return NULL;
652 gacl->acl_level = GPFS_ACL_LEVEL_BASE;
653 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
654 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
655 gacl->acl_nace = 0; /* change later... */
657 if (controlflags) {
658 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
659 sd2gpfs_control(smbacl4_get_controlflags(smbacl), gacl);
662 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
663 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, gacl->acl_nace);
664 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
666 gace->aceType = aceprop->aceType;
667 gace->aceFlags = aceprop->aceFlags;
668 gace->aceMask = aceprop->aceMask;
671 * GPFS can't distinguish between WRITE and APPEND on
672 * files, so one being set without the other is an
673 * error. Sorry for the many ()'s :-)
676 if (!fsp->is_directory
678 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
679 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
681 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
682 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
684 lp_parm_bool(fsp->conn->params->service, "gpfs",
685 "merge_writeappend", True)) {
686 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
687 "WRITE^APPEND, setting WRITE|APPEND\n",
688 fsp_str_dbg(fsp)));
689 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
692 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
694 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
696 switch(aceprop->who.special_id)
698 case SMB_ACE4_WHO_EVERYONE:
699 gace->aceWho = ACE4_SPECIAL_EVERYONE;
700 break;
701 case SMB_ACE4_WHO_OWNER:
702 gace->aceWho = ACE4_SPECIAL_OWNER;
703 break;
704 case SMB_ACE4_WHO_GROUP:
705 gace->aceWho = ACE4_SPECIAL_GROUP;
706 break;
707 default:
708 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
709 continue; /* don't add it !!! */
711 } else {
712 /* just only for the type safety... */
713 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
714 gace->aceWho = aceprop->who.gid;
715 else
716 gace->aceWho = aceprop->who.uid;
719 gacl->acl_nace++;
721 gacl->acl_len = (char *)gpfs_ace_ptr(gacl, gacl->acl_nace)
722 - (char *)gacl;
723 return gacl;
726 static bool gpfsacl_process_smbacl(vfs_handle_struct *handle,
727 files_struct *fsp,
728 struct SMB4ACL_T *smbacl)
730 int ret;
731 struct gpfs_acl *gacl;
732 TALLOC_CTX *mem_ctx = talloc_tos();
734 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, true);
735 if (gacl == NULL) { /* out of memory */
736 return False;
738 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
739 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
741 if ((ret != 0) && (errno == EINVAL)) {
742 DEBUG(10, ("Retry without nfs41 control flags\n"));
743 talloc_free(gacl);
744 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, false);
745 if (gacl == NULL) { /* out of memory */
746 return False;
748 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
749 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA,
750 gacl);
753 if (ret != 0) {
754 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
755 gpfs_dumpacl(8, gacl);
756 return False;
759 DEBUG(10, ("gpfs_putacl succeeded\n"));
760 return True;
763 static NTSTATUS gpfsacl_set_nt_acl_internal(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
765 struct gpfs_acl *acl;
766 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
768 acl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(),
769 fsp->fsp_name->base_name,
770 false, 0);
771 if (acl == NULL) {
772 return map_nt_error_from_unix(errno);
775 if (acl->acl_version == GPFS_ACL_VERSION_NFS4) {
776 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
777 "refuse_dacl_protected", false)
778 && (psd->type&SEC_DESC_DACL_PROTECTED)) {
779 DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
780 talloc_free(acl);
781 return NT_STATUS_NOT_SUPPORTED;
784 result = smb_set_nt_acl_nfs4(handle,
785 fsp, security_info_sent, psd,
786 gpfsacl_process_smbacl);
787 } else { /* assume POSIX ACL - by default... */
788 result = set_nt_acl(fsp, security_info_sent, psd);
791 talloc_free(acl);
792 return result;
795 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
797 struct gpfs_config_data *config;
799 SMB_VFS_HANDLE_GET_DATA(handle, config,
800 struct gpfs_config_data,
801 return NT_STATUS_INTERNAL_ERROR);
803 if (!config->acl) {
804 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
807 return gpfsacl_set_nt_acl_internal(handle, fsp, security_info_sent, psd);
810 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl, TALLOC_CTX *mem_ctx)
812 SMB_ACL_T result;
813 gpfs_aclCount_t i;
815 result = sys_acl_init(mem_ctx);
816 if (result == NULL) {
817 errno = ENOMEM;
818 return NULL;
821 result->count = pacl->acl_nace;
822 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry,
823 result->count);
824 if (result->acl == NULL) {
825 TALLOC_FREE(result);
826 errno = ENOMEM;
827 return NULL;
830 for (i=0; i<pacl->acl_nace; i++) {
831 struct smb_acl_entry *ace = &result->acl[i];
832 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
834 DEBUG(10, ("Converting type %d id %lu perm %x\n",
835 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
836 (int)g_ace->ace_perm));
838 switch (g_ace->ace_type) {
839 case GPFS_ACL_USER:
840 ace->a_type = SMB_ACL_USER;
841 ace->info.user.uid = (uid_t)g_ace->ace_who;
842 break;
843 case GPFS_ACL_USER_OBJ:
844 ace->a_type = SMB_ACL_USER_OBJ;
845 break;
846 case GPFS_ACL_GROUP:
847 ace->a_type = SMB_ACL_GROUP;
848 ace->info.group.gid = (gid_t)g_ace->ace_who;
849 break;
850 case GPFS_ACL_GROUP_OBJ:
851 ace->a_type = SMB_ACL_GROUP_OBJ;
852 break;
853 case GPFS_ACL_OTHER:
854 ace->a_type = SMB_ACL_OTHER;
855 break;
856 case GPFS_ACL_MASK:
857 ace->a_type = SMB_ACL_MASK;
858 break;
859 default:
860 DEBUG(10, ("Got invalid ace_type: %d\n",
861 g_ace->ace_type));
862 TALLOC_FREE(result);
863 errno = EINVAL;
864 return NULL;
867 ace->a_perm = 0;
868 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
869 SMB_ACL_READ : 0;
870 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
871 SMB_ACL_WRITE : 0;
872 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
873 SMB_ACL_EXECUTE : 0;
875 DEBUGADD(10, ("Converted to %d perm %x\n",
876 ace->a_type, ace->a_perm));
879 return result;
882 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type,
883 TALLOC_CTX *mem_ctx)
885 struct gpfs_acl *pacl;
886 SMB_ACL_T result = NULL;
888 pacl = vfs_gpfs_getacl(talloc_tos(), path, false, type);
890 if (pacl == NULL) {
891 DEBUG(10, ("vfs_gpfs_getacl failed for %s with %s\n",
892 path, strerror(errno)));
893 if (errno == 0) {
894 errno = EINVAL;
896 goto done;
899 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
900 DEBUG(10, ("Got acl version %d, expected %d\n",
901 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
902 errno = EINVAL;
903 goto done;
906 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
907 pacl->acl_len, pacl->acl_level, pacl->acl_version,
908 pacl->acl_nace));
910 result = gpfs2smb_acl(pacl, mem_ctx);
911 if (result != NULL) {
912 errno = 0;
915 done:
917 if (pacl != NULL) {
918 talloc_free(pacl);
920 if (errno != 0) {
921 TALLOC_FREE(result);
923 return result;
926 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
927 const char *path_p,
928 SMB_ACL_TYPE_T type,
929 TALLOC_CTX *mem_ctx)
931 gpfs_aclType_t gpfs_type;
932 struct gpfs_config_data *config;
934 SMB_VFS_HANDLE_GET_DATA(handle, config,
935 struct gpfs_config_data,
936 return NULL);
938 if (!config->acl) {
939 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p,
940 type, mem_ctx);
943 switch(type) {
944 case SMB_ACL_TYPE_ACCESS:
945 gpfs_type = GPFS_ACL_TYPE_ACCESS;
946 break;
947 case SMB_ACL_TYPE_DEFAULT:
948 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
949 break;
950 default:
951 DEBUG(0, ("Got invalid type: %d\n", type));
952 smb_panic("exiting");
955 return gpfsacl_get_posix_acl(path_p, gpfs_type, mem_ctx);
958 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
959 files_struct *fsp,
960 TALLOC_CTX *mem_ctx)
962 struct gpfs_config_data *config;
964 SMB_VFS_HANDLE_GET_DATA(handle, config,
965 struct gpfs_config_data,
966 return NULL);
968 if (!config->acl) {
969 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
972 return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
973 GPFS_ACL_TYPE_ACCESS, mem_ctx);
976 static int gpfsacl_sys_acl_blob_get_file(vfs_handle_struct *handle,
977 const char *path_p,
978 TALLOC_CTX *mem_ctx,
979 char **blob_description,
980 DATA_BLOB *blob)
982 struct gpfs_config_data *config;
983 struct gpfs_opaque_acl *acl = NULL;
984 DATA_BLOB aclblob;
985 int result;
987 SMB_VFS_HANDLE_GET_DATA(handle, config,
988 struct gpfs_config_data,
989 return -1);
991 if (!config->acl) {
992 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, path_p,
993 mem_ctx,
994 blob_description,
995 blob);
998 errno = 0;
999 acl = (struct gpfs_opaque_acl *)
1000 vfs_gpfs_getacl(mem_ctx,
1001 path_p,
1002 true,
1003 GPFS_ACL_TYPE_NFS4);
1005 if (errno) {
1006 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1007 errno, strerror(errno)));
1009 /* EINVAL means POSIX ACL, bail out on other cases */
1010 if (errno != EINVAL) {
1011 return -1;
1015 if (acl != NULL) {
1017 * file has NFSv4 ACL
1019 * we only need the actual ACL blob here
1020 * acl_version will always be NFS4 because we asked
1021 * for NFS4
1022 * acl_type is only used for POSIX ACLs
1024 aclblob.data = (uint8_t*) acl->acl_var_data;
1025 aclblob.length = acl->acl_buffer_len;
1027 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1028 if (!*blob_description) {
1029 talloc_free(acl);
1030 errno = ENOMEM;
1031 return -1;
1034 result = non_posix_sys_acl_blob_get_file_helper(handle, path_p,
1035 aclblob,
1036 mem_ctx, blob);
1038 talloc_free(acl);
1039 return result;
1042 /* fall back to POSIX ACL */
1043 return posix_sys_acl_blob_get_file(handle, path_p, mem_ctx,
1044 blob_description, blob);
1047 static int gpfsacl_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1048 files_struct *fsp,
1049 TALLOC_CTX *mem_ctx,
1050 char **blob_description,
1051 DATA_BLOB *blob)
1053 struct gpfs_config_data *config;
1054 struct gpfs_opaque_acl *acl = NULL;
1055 DATA_BLOB aclblob;
1056 int result;
1058 SMB_VFS_HANDLE_GET_DATA(handle, config,
1059 struct gpfs_config_data,
1060 return -1);
1062 if (!config->acl) {
1063 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1064 blob_description, blob);
1067 errno = 0;
1068 acl = (struct gpfs_opaque_acl *) vfs_gpfs_getacl(mem_ctx,
1069 fsp->fsp_name->base_name,
1070 true,
1071 GPFS_ACL_TYPE_NFS4);
1073 if (errno) {
1074 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1075 errno, strerror(errno)));
1077 /* EINVAL means POSIX ACL, bail out on other cases */
1078 if (errno != EINVAL) {
1079 return -1;
1083 if (acl != NULL) {
1085 * file has NFSv4 ACL
1087 * we only need the actual ACL blob here
1088 * acl_version will always be NFS4 because we asked
1089 * for NFS4
1090 * acl_type is only used for POSIX ACLs
1092 aclblob.data = (uint8_t*) acl->acl_var_data;
1093 aclblob.length = acl->acl_buffer_len;
1095 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1096 if (!*blob_description) {
1097 talloc_free(acl);
1098 errno = ENOMEM;
1099 return -1;
1102 result = non_posix_sys_acl_blob_get_fd_helper(handle, fsp,
1103 aclblob, mem_ctx,
1104 blob);
1106 talloc_free(acl);
1107 return result;
1110 /* fall back to POSIX ACL */
1111 return posix_sys_acl_blob_get_fd(handle, fsp, mem_ctx,
1112 blob_description, blob);
1115 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
1116 SMB_ACL_TYPE_T type)
1118 gpfs_aclLen_t len;
1119 struct gpfs_acl *result;
1120 int i;
1122 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
1124 len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
1125 sizeof(gpfs_ace_v1_t);
1127 result = (struct gpfs_acl *)SMB_MALLOC(len);
1128 if (result == NULL) {
1129 errno = ENOMEM;
1130 return result;
1133 result->acl_len = len;
1134 result->acl_level = 0;
1135 result->acl_version = GPFS_ACL_VERSION_POSIX;
1136 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
1137 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
1138 result->acl_nace = pacl->count;
1140 for (i=0; i<pacl->count; i++) {
1141 const struct smb_acl_entry *ace = &pacl->acl[i];
1142 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
1144 DEBUG(10, ("Converting type %d perm %x\n",
1145 (int)ace->a_type, (int)ace->a_perm));
1147 g_ace->ace_perm = 0;
1149 switch(ace->a_type) {
1150 case SMB_ACL_USER:
1151 g_ace->ace_type = GPFS_ACL_USER;
1152 g_ace->ace_who = (gpfs_uid_t)ace->info.user.uid;
1153 break;
1154 case SMB_ACL_USER_OBJ:
1155 g_ace->ace_type = GPFS_ACL_USER_OBJ;
1156 g_ace->ace_perm |= ACL_PERM_CONTROL;
1157 g_ace->ace_who = 0;
1158 break;
1159 case SMB_ACL_GROUP:
1160 g_ace->ace_type = GPFS_ACL_GROUP;
1161 g_ace->ace_who = (gpfs_uid_t)ace->info.group.gid;
1162 break;
1163 case SMB_ACL_GROUP_OBJ:
1164 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
1165 g_ace->ace_who = 0;
1166 break;
1167 case SMB_ACL_MASK:
1168 g_ace->ace_type = GPFS_ACL_MASK;
1169 g_ace->ace_perm = 0x8f;
1170 g_ace->ace_who = 0;
1171 break;
1172 case SMB_ACL_OTHER:
1173 g_ace->ace_type = GPFS_ACL_OTHER;
1174 g_ace->ace_who = 0;
1175 break;
1176 default:
1177 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
1178 errno = EINVAL;
1179 SAFE_FREE(result);
1180 return NULL;
1183 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
1184 ACL_PERM_READ : 0;
1185 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
1186 ACL_PERM_WRITE : 0;
1187 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
1188 ACL_PERM_EXECUTE : 0;
1190 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
1191 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
1194 return result;
1197 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
1198 const char *name,
1199 SMB_ACL_TYPE_T type,
1200 SMB_ACL_T theacl)
1202 struct gpfs_acl *gpfs_acl;
1203 int result;
1204 struct gpfs_config_data *config;
1206 SMB_VFS_HANDLE_GET_DATA(handle, config,
1207 struct gpfs_config_data,
1208 return -1);
1210 if (!config->acl) {
1211 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, type, theacl);
1214 gpfs_acl = smb2gpfs_acl(theacl, type);
1215 if (gpfs_acl == NULL) {
1216 return -1;
1219 result = gpfswrap_putacl(discard_const_p(char, name),
1220 GPFS_PUTACL_STRUCT|GPFS_ACL_SAMBA, gpfs_acl);
1222 SAFE_FREE(gpfs_acl);
1223 return result;
1226 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
1227 files_struct *fsp,
1228 SMB_ACL_T theacl)
1230 struct gpfs_config_data *config;
1232 SMB_VFS_HANDLE_GET_DATA(handle, config,
1233 struct gpfs_config_data,
1234 return -1);
1236 if (!config->acl) {
1237 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1240 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
1241 SMB_ACL_TYPE_ACCESS, theacl);
1244 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
1245 const char *path)
1247 struct gpfs_config_data *config;
1249 SMB_VFS_HANDLE_GET_DATA(handle, config,
1250 struct gpfs_config_data,
1251 return -1);
1253 if (!config->acl) {
1254 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
1257 errno = ENOTSUP;
1258 return -1;
1262 * Assumed: mode bits are shiftable and standard
1263 * Output: the new aceMask field for an smb nfs4 ace
1265 static uint32_t gpfsacl_mask_filter(uint32_t aceType, uint32_t aceMask, uint32_t rwx)
1267 const uint32_t posix_nfs4map[3] = {
1268 SMB_ACE4_EXECUTE, /* execute */
1269 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
1270 SMB_ACE4_READ_DATA /* read */
1272 int i;
1273 uint32_t posix_mask = 0x01;
1274 uint32_t posix_bit;
1275 uint32_t nfs4_bits;
1277 for(i=0; i<3; i++) {
1278 nfs4_bits = posix_nfs4map[i];
1279 posix_bit = rwx & posix_mask;
1281 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
1282 if (posix_bit)
1283 aceMask |= nfs4_bits;
1284 else
1285 aceMask &= ~nfs4_bits;
1286 } else {
1287 /* add deny bits when suitable */
1288 if (!posix_bit)
1289 aceMask |= nfs4_bits;
1290 else
1291 aceMask &= ~nfs4_bits;
1292 } /* other ace types are unexpected */
1294 posix_mask <<= 1;
1297 return aceMask;
1300 static int gpfsacl_emu_chmod(vfs_handle_struct *handle,
1301 const char *path, mode_t mode)
1303 struct SMB4ACL_T *pacl = NULL;
1304 int result;
1305 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
1306 int i;
1307 files_struct fake_fsp = { 0 }; /* TODO: rationalize parametrization */
1308 struct SMB4ACE_T *smbace;
1309 TALLOC_CTX *frame = talloc_stackframe();
1311 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
1313 result = gpfs_get_nfs4_acl(frame, path, &pacl);
1314 if (result) {
1315 TALLOC_FREE(frame);
1316 return result;
1319 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
1320 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
1323 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
1324 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
1325 uint32_t specid = ace->who.special_id;
1327 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
1328 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
1329 specid <= SMB_ACE4_WHO_EVERYONE) {
1331 uint32_t newMask;
1333 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
1334 haveAllowEntry[specid] = True;
1336 /* mode >> 6 for @owner, mode >> 3 for @group,
1337 * mode >> 0 for @everyone */
1338 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
1339 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
1340 if (ace->aceMask!=newMask) {
1341 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
1342 path, ace->aceMask, newMask, specid));
1344 ace->aceMask = newMask;
1348 /* make sure we have at least ALLOW entries
1349 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
1350 * - if necessary
1352 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
1353 SMB_ACE4PROP_T ace = { 0 };
1355 if (haveAllowEntry[i]==True)
1356 continue;
1358 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
1359 ace.flags |= SMB_ACE4_ID_SPECIAL;
1360 ace.who.special_id = i;
1362 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
1363 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
1365 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
1366 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
1368 /* don't add unnecessary aces */
1369 if (!ace.aceMask)
1370 continue;
1372 /* we add it to the END - as windows expects allow aces */
1373 smb_add_ace4(pacl, &ace);
1374 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
1375 path, mode, i, ace.aceMask));
1378 /* don't add complementary DENY ACEs here */
1379 fake_fsp.fsp_name = synthetic_smb_fname(
1380 frame, path, NULL, NULL);
1381 if (fake_fsp.fsp_name == NULL) {
1382 errno = ENOMEM;
1383 TALLOC_FREE(frame);
1384 return -1;
1386 /* put the acl */
1387 if (gpfsacl_process_smbacl(handle, &fake_fsp, pacl) == False) {
1388 TALLOC_FREE(frame);
1389 return -1;
1392 TALLOC_FREE(frame);
1393 return 0; /* ok for [f]chmod */
1396 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
1398 struct smb_filename *smb_fname_cpath;
1399 int rc;
1401 smb_fname_cpath = synthetic_smb_fname(talloc_tos(), path, NULL, NULL);
1402 if (smb_fname_cpath == NULL) {
1403 errno = ENOMEM;
1404 return -1;
1407 if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
1408 return -1;
1411 /* avoid chmod() if possible, to preserve acls */
1412 if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
1413 return 0;
1416 rc = gpfsacl_emu_chmod(handle, path, mode);
1417 if (rc == 1)
1418 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1419 return rc;
1422 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1424 SMB_STRUCT_STAT st;
1425 int rc;
1427 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1428 return -1;
1431 /* avoid chmod() if possible, to preserve acls */
1432 if ((st.st_ex_mode & ~S_IFMT) == mode) {
1433 return 0;
1436 rc = gpfsacl_emu_chmod(handle, fsp->fsp_name->base_name,
1437 mode);
1438 if (rc == 1)
1439 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1440 return rc;
1443 static int gpfs_set_xattr(struct vfs_handle_struct *handle, const char *path,
1444 const char *name, const void *value, size_t size, int flags){
1445 struct xattr_DOSATTRIB dosattrib;
1446 enum ndr_err_code ndr_err;
1447 DATA_BLOB blob;
1448 unsigned int dosmode=0;
1449 struct gpfs_winattr attrs;
1450 int ret = 0;
1451 struct gpfs_config_data *config;
1453 SMB_VFS_HANDLE_GET_DATA(handle, config,
1454 struct gpfs_config_data,
1455 return -1);
1457 if (!config->winattr) {
1458 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
1459 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1462 DEBUG(10, ("gpfs_set_xattr: %s \n",path));
1464 /* Only handle DOS Attributes */
1465 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1466 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
1467 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1470 blob.data = discard_const_p(uint8_t, value);
1471 blob.length = size;
1473 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
1474 (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
1476 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1477 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
1478 "from EA on file %s: Error = %s\n",
1479 path, ndr_errstr(ndr_err)));
1480 return false;
1483 if (dosattrib.version != 3) {
1484 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
1485 "%d\n", (int)dosattrib.version));
1486 return false;
1488 if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
1489 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
1490 "valid, ignoring\n"));
1491 return true;
1494 dosmode = dosattrib.info.info3.attrib;
1496 attrs.winAttrs = 0;
1497 /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1498 if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1499 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1501 if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1502 attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1504 if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1505 attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1507 if (dosmode & FILE_ATTRIBUTE_READONLY){
1508 attrs.winAttrs |= GPFS_WINATTR_READONLY;
1510 if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1511 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1515 ret = gpfswrap_set_winattrs_path(discard_const_p(char, path),
1516 GPFS_WINATTR_SET_ATTRS, &attrs);
1517 if ( ret == -1){
1518 if (errno == ENOSYS) {
1519 return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1520 size, flags);
1523 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1524 return -1;
1527 DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1528 return 0;
1531 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle, const char *path,
1532 const char *name, void *value, size_t size){
1533 char *attrstr = value;
1534 unsigned int dosmode = 0;
1535 struct gpfs_winattr attrs;
1536 int ret = 0;
1537 struct gpfs_config_data *config;
1539 SMB_VFS_HANDLE_GET_DATA(handle, config,
1540 struct gpfs_config_data,
1541 return -1);
1543 if (!config->winattr) {
1544 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1545 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1548 DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1550 /* Only handle DOS Attributes */
1551 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1552 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1553 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1556 ret = gpfswrap_get_winattrs_path(discard_const_p(char, path), &attrs);
1557 if ( ret == -1){
1558 int dbg_lvl;
1560 if (errno == ENOSYS) {
1561 return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1562 size);
1565 if (errno != EPERM && errno != EACCES) {
1566 dbg_lvl = 1;
1567 } else {
1568 dbg_lvl = 5;
1570 DEBUG(dbg_lvl, ("gpfs_get_xattr: Get GPFS attributes failed: "
1571 "%d (%s)\n", ret, strerror(errno)));
1572 return -1;
1575 DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1577 /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1578 if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1579 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1581 if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1582 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1584 if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1585 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1587 if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1588 dosmode |= FILE_ATTRIBUTE_READONLY;
1590 if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1591 dosmode |= FILE_ATTRIBUTE_SPARSE;
1594 snprintf(attrstr, size, "0x%2.2x",
1595 (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1596 DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1597 return 4;
1600 #if defined(HAVE_FSTATAT)
1601 static int stat_with_capability(struct vfs_handle_struct *handle,
1602 struct smb_filename *smb_fname, int flag)
1604 int fd = -1;
1605 bool b;
1606 char *dir_name;
1607 const char *rel_name = NULL;
1608 struct stat st;
1609 int ret = -1;
1611 b = parent_dirname(talloc_tos(), smb_fname->base_name,
1612 &dir_name, &rel_name);
1613 if (!b) {
1614 errno = ENOMEM;
1615 return -1;
1618 fd = open(dir_name, O_RDONLY, 0);
1619 TALLOC_FREE(dir_name);
1620 if (fd == -1) {
1621 return -1;
1624 set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1625 ret = fstatat(fd, rel_name, &st, flag);
1626 drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1628 close(fd);
1630 if (ret == 0) {
1631 init_stat_ex_from_stat(
1632 &smb_fname->st, &st,
1633 lp_fake_directory_create_times(SNUM(handle->conn)));
1636 return ret;
1638 #endif
1640 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1641 struct smb_filename *smb_fname)
1643 struct gpfs_winattr attrs;
1644 char *fname = NULL;
1645 NTSTATUS status;
1646 int ret;
1647 struct gpfs_config_data *config;
1649 SMB_VFS_HANDLE_GET_DATA(handle, config,
1650 struct gpfs_config_data,
1651 return -1);
1653 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1654 #if defined(HAVE_FSTATAT)
1655 if (ret == -1 && errno == EACCES) {
1656 DEBUG(10, ("Trying stat with capability for %s\n",
1657 smb_fname->base_name));
1658 ret = stat_with_capability(handle, smb_fname, 0);
1660 #endif
1661 if (ret == -1) {
1662 return -1;
1665 if (!config->winattr) {
1666 return 0;
1669 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1670 if (!NT_STATUS_IS_OK(status)) {
1671 errno = map_errno_from_nt_status(status);
1672 return -1;
1674 ret = gpfswrap_get_winattrs_path(discard_const_p(char, fname), &attrs);
1675 TALLOC_FREE(fname);
1676 if (ret == 0) {
1677 smb_fname->st.st_ex_calculated_birthtime = false;
1678 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1679 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1681 return 0;
1684 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1685 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1687 struct gpfs_winattr attrs;
1688 int ret;
1689 struct gpfs_config_data *config;
1691 SMB_VFS_HANDLE_GET_DATA(handle, config,
1692 struct gpfs_config_data,
1693 return -1);
1695 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1696 if (ret == -1) {
1697 return -1;
1699 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1700 return 0;
1702 if (!config->winattr) {
1703 return 0;
1706 ret = gpfswrap_get_winattrs(fsp->fh->fd, &attrs);
1707 if (ret == 0) {
1708 sbuf->st_ex_calculated_birthtime = false;
1709 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1710 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1712 return 0;
1715 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1716 struct smb_filename *smb_fname)
1718 struct gpfs_winattr attrs;
1719 char *path = NULL;
1720 NTSTATUS status;
1721 int ret;
1722 struct gpfs_config_data *config;
1724 SMB_VFS_HANDLE_GET_DATA(handle, config,
1725 struct gpfs_config_data,
1726 return -1);
1728 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1729 #if defined(HAVE_FSTATAT)
1730 if (ret == -1 && errno == EACCES) {
1731 DEBUG(10, ("Trying lstat with capability for %s\n",
1732 smb_fname->base_name));
1733 ret = stat_with_capability(handle, smb_fname,
1734 AT_SYMLINK_NOFOLLOW);
1736 #endif
1738 if (ret == -1) {
1739 return -1;
1741 if (!config->winattr) {
1742 return 0;
1745 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1746 if (!NT_STATUS_IS_OK(status)) {
1747 errno = map_errno_from_nt_status(status);
1748 return -1;
1750 ret = gpfswrap_get_winattrs_path(discard_const_p(char, path), &attrs);
1751 TALLOC_FREE(path);
1752 if (ret == 0) {
1753 smb_fname->st.st_ex_calculated_birthtime = false;
1754 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1755 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1757 return 0;
1760 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
1761 int idx, int *flags)
1763 if (!null_timespec(ts)) {
1764 *flags |= 1 << idx;
1765 gt[idx].tv_sec = ts.tv_sec;
1766 gt[idx].tv_nsec = ts.tv_nsec;
1767 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
1771 static int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
1773 gpfs_timestruc_t gpfs_times[4];
1774 int flags = 0;
1775 int rc;
1777 ZERO_ARRAY(gpfs_times);
1778 timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
1779 timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
1780 /* No good mapping from LastChangeTime to ctime, not storing */
1781 timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
1783 if (!flags) {
1784 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
1785 return 0;
1788 rc = gpfswrap_set_times_path(path, flags, gpfs_times);
1790 if (rc != 0 && errno != ENOSYS) {
1791 DEBUG(1,("gpfs_set_times() returned with error %s\n",
1792 strerror(errno)));
1795 return rc;
1798 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1799 const struct smb_filename *smb_fname,
1800 struct smb_file_time *ft)
1803 struct gpfs_winattr attrs;
1804 int ret;
1805 char *path = NULL;
1806 NTSTATUS status;
1807 struct gpfs_config_data *config;
1809 SMB_VFS_HANDLE_GET_DATA(handle, config,
1810 struct gpfs_config_data,
1811 return -1);
1813 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1814 if (!NT_STATUS_IS_OK(status)) {
1815 errno = map_errno_from_nt_status(status);
1816 return -1;
1819 /* Try to use gpfs_set_times if it is enabled and available */
1820 if (config->settimes) {
1821 ret = smbd_gpfs_set_times_path(path, ft);
1823 if (ret == 0 || (ret == -1 && errno != ENOSYS)) {
1824 return ret;
1828 DEBUG(10,("gpfs_set_times() not available or disabled, "
1829 "use ntimes and winattr\n"));
1831 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1832 if(ret == -1){
1833 /* don't complain if access was denied */
1834 if (errno != EPERM && errno != EACCES) {
1835 DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1836 "%s", strerror(errno)));
1838 return -1;
1841 if(null_timespec(ft->create_time)){
1842 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1843 return 0;
1846 if (!config->winattr) {
1847 return 0;
1850 attrs.winAttrs = 0;
1851 attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1852 attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1854 ret = gpfswrap_set_winattrs_path(discard_const_p(char, path),
1855 GPFS_WINATTR_SET_CREATION_TIME,
1856 &attrs);
1857 if(ret == -1 && errno != ENOSYS){
1858 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1859 return -1;
1861 return 0;
1865 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1866 struct files_struct *fsp, uint32_t mode,
1867 off_t offset, off_t len)
1869 int ret;
1870 struct gpfs_config_data *config;
1872 SMB_VFS_HANDLE_GET_DATA(handle, config,
1873 struct gpfs_config_data,
1874 return -1);
1876 if (!config->prealloc) {
1877 /* you should better not run fallocate() on GPFS at all */
1878 errno = ENOTSUP;
1879 return -1;
1882 if (mode != 0) {
1883 DEBUG(10, ("unmapped fallocate flags: %lx\n",
1884 (unsigned long)mode));
1885 errno = ENOTSUP;
1886 return -1;
1889 ret = gpfswrap_prealloc(fsp->fh->fd, offset, len);
1891 if (ret == -1 && errno != ENOSYS) {
1892 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1893 } else if (ret == -1 && errno == ENOSYS) {
1894 DEBUG(10, ("GPFS prealloc not supported.\n"));
1895 } else {
1896 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1899 return ret;
1902 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1903 off_t len)
1905 int result;
1906 struct gpfs_config_data *config;
1908 SMB_VFS_HANDLE_GET_DATA(handle, config,
1909 struct gpfs_config_data,
1910 return -1);
1912 if (!config->ftruncate) {
1913 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1916 result = gpfswrap_ftruncate(fsp->fh->fd, len);
1917 if ((result == -1) && (errno == ENOSYS)) {
1918 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1920 return result;
1923 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1924 const struct smb_filename *fname,
1925 SMB_STRUCT_STAT *sbuf)
1927 struct gpfs_winattr attrs;
1928 char *path = NULL;
1929 NTSTATUS status;
1930 struct gpfs_config_data *config;
1931 int ret;
1933 SMB_VFS_HANDLE_GET_DATA(handle, config,
1934 struct gpfs_config_data,
1935 return -1);
1937 if (!config->winattr) {
1938 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1941 status = get_full_smb_filename(talloc_tos(), fname, &path);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 errno = map_errno_from_nt_status(status);
1944 return -1;
1947 ret = gpfswrap_get_winattrs_path(path, &attrs);
1948 if (ret == -1) {
1949 TALLOC_FREE(path);
1950 return false;
1953 if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1954 DEBUG(10, ("%s is offline\n", path));
1955 TALLOC_FREE(path);
1956 return true;
1958 DEBUG(10, ("%s is online\n", path));
1959 TALLOC_FREE(path);
1960 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1963 static bool vfs_gpfs_fsp_is_offline(struct vfs_handle_struct *handle,
1964 struct files_struct *fsp)
1966 struct gpfs_fsp_extension *ext;
1968 ext = VFS_FETCH_FSP_EXTENSION(handle, fsp);
1969 if (ext == NULL) {
1971 * Something bad happened, always ask.
1973 return vfs_gpfs_is_offline(handle, fsp->fsp_name,
1974 &fsp->fsp_name->st);
1977 if (ext->offline) {
1979 * As long as it's offline, ask.
1981 ext->offline = vfs_gpfs_is_offline(handle, fsp->fsp_name,
1982 &fsp->fsp_name->st);
1985 return ext->offline;
1988 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1989 struct files_struct *fsp)
1991 return vfs_gpfs_fsp_is_offline(handle, fsp);
1994 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1995 files_struct *fsp, const DATA_BLOB *hdr,
1996 off_t offset, size_t n)
1998 if (vfs_gpfs_fsp_is_offline(handle, fsp)) {
1999 errno = ENOSYS;
2000 return -1;
2002 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
2005 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
2006 const char *service, const char *user)
2008 struct gpfs_config_data *config;
2009 int ret;
2011 gpfswrap_lib_init(0);
2013 config = talloc_zero(handle->conn, struct gpfs_config_data);
2014 if (!config) {
2015 DEBUG(0, ("talloc_zero() failed\n"));
2016 errno = ENOMEM;
2017 return -1;
2020 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2021 if (ret < 0) {
2022 TALLOC_FREE(config);
2023 return ret;
2026 config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2027 "sharemodes", true);
2029 config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
2030 "leases", true);
2032 config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
2033 "hsm", false);
2035 config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
2036 "syncio", false);
2038 config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
2039 "winattr", false);
2041 config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
2042 "ftruncate", true);
2044 config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
2045 "getrealfilename", true);
2047 config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
2048 "dfreequota", false);
2050 config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
2051 "prealloc", true);
2053 config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
2055 config->settimes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2056 "settimes", true);
2057 config->recalls = lp_parm_bool(SNUM(handle->conn), "gpfs",
2058 "recalls", true);
2060 SMB_VFS_HANDLE_SET_DATA(handle, config,
2061 NULL, struct gpfs_config_data,
2062 return -1);
2064 if (config->leases) {
2066 * GPFS lease code is based on kernel oplock code
2067 * so make sure it is turned on
2069 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
2070 DEBUG(5, ("Enabling kernel oplocks for "
2071 "gpfs:leases to work\n"));
2072 lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
2073 "true");
2077 * as the kernel does not properly support Level II oplocks
2078 * and GPFS leases code is based on kernel infrastructure, we
2079 * need to turn off Level II oplocks if gpfs:leases is enabled
2081 if (lp_level2_oplocks(SNUM(handle->conn))) {
2082 DEBUG(5, ("gpfs:leases are enabled, disabling "
2083 "Level II oplocks\n"));
2084 lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
2085 "false");
2089 return 0;
2092 static int get_gpfs_quota(const char *pathname, int type, int id,
2093 struct gpfs_quotaInfo *qi)
2095 int ret;
2097 ret = gpfswrap_quotactl(discard_const_p(char, pathname),
2098 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
2100 if (ret) {
2101 if (errno == GPFS_E_NO_QUOTA_INST) {
2102 DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
2103 } else if (errno != ENOSYS) {
2104 DEBUG(0, ("Get quota failed, type %d, id, %d, "
2105 "errno %d.\n", type, id, errno));
2108 return ret;
2111 DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
2112 type, id, qi->blockUsage, qi->blockHardLimit,
2113 qi->blockSoftLimit, qi->blockGraceTime));
2115 return ret;
2118 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
2119 uint64_t *dfree, uint64_t *dsize)
2121 uint64_t usage, limit;
2124 * The quota reporting is done in units of 1024 byte blocks, but
2125 * sys_fsusage uses units of 512 byte blocks, adjust the block number
2126 * accordingly. Also filter possibly negative usage counts from gpfs.
2128 usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
2129 limit = (uint64_t)qi.blockHardLimit * 2;
2132 * When the grace time for the exceeded soft block quota has been
2133 * exceeded, the soft block quota becomes an additional hard limit.
2135 if (qi.blockSoftLimit &&
2136 qi.blockGraceTime && cur_time > qi.blockGraceTime) {
2137 /* report disk as full */
2138 *dfree = 0;
2139 *dsize = MIN(*dsize, usage);
2142 if (!qi.blockHardLimit)
2143 return;
2145 if (usage >= limit) {
2146 /* report disk as full */
2147 *dfree = 0;
2148 *dsize = MIN(*dsize, usage);
2150 } else {
2151 /* limit has not been reached, determine "free space" */
2152 *dfree = MIN(*dfree, limit - usage);
2153 *dsize = MIN(*dsize, limit);
2157 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
2158 uint64_t *bsize,
2159 uint64_t *dfree, uint64_t *dsize)
2161 struct security_unix_token *utok;
2162 struct gpfs_quotaInfo qi_user = { 0 }, qi_group = { 0 };
2163 struct gpfs_config_data *config;
2164 int err;
2165 time_t cur_time;
2167 SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
2168 return (uint64_t)-1);
2169 if (!config->dfreequota) {
2170 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2171 bsize, dfree, dsize);
2174 err = sys_fsusage(path, dfree, dsize);
2175 if (err) {
2176 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
2177 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2178 bsize, dfree, dsize);
2181 /* sys_fsusage returns units of 512 bytes */
2182 *bsize = 512;
2184 DEBUG(10, ("fs dfree %llu, dsize %llu\n",
2185 (unsigned long long)*dfree, (unsigned long long)*dsize));
2187 utok = handle->conn->session_info->unix_token;
2189 err = get_gpfs_quota(path, GPFS_USRQUOTA, utok->uid, &qi_user);
2190 if (err) {
2191 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2192 bsize, dfree, dsize);
2195 err = get_gpfs_quota(path, GPFS_GRPQUOTA, utok->gid, &qi_group);
2196 if (err) {
2197 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2198 bsize, dfree, dsize);
2201 cur_time = time(NULL);
2203 /* Adjust free space and size according to quota limits. */
2204 vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
2205 vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
2207 return *dfree / 2;
2210 static int vfs_gpfs_get_quota(vfs_handle_struct *handle, const char *path,
2211 enum SMB_QUOTA_TYPE qtype, unid_t id,
2212 SMB_DISK_QUOTA *dq)
2214 switch(qtype) {
2216 * User/group quota are being used for disk-free
2217 * determination, which in this module is done directly
2218 * by the disk-free function. It's important that this
2219 * module does not return wrong quota values by mistake,
2220 * which would modify the correct values set by disk-free.
2221 * User/group quota are also being used for processing
2222 * NT_TRANSACT_GET_USER_QUOTA in smb1 protocol, which is
2223 * currently not supported by this module.
2225 case SMB_USER_QUOTA_TYPE:
2226 case SMB_GROUP_QUOTA_TYPE:
2227 errno = ENOSYS;
2228 return -1;
2229 default:
2230 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2234 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
2235 enum timestamp_set_resolution *p_ts_res)
2237 struct gpfs_config_data *config;
2238 uint32_t next;
2240 next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
2242 SMB_VFS_HANDLE_GET_DATA(handle, config,
2243 struct gpfs_config_data,
2244 return next);
2246 if (config->hsm) {
2247 next |= FILE_SUPPORTS_REMOTE_STORAGE;
2249 return next;
2252 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
2253 struct smb_filename *smb_fname, files_struct *fsp,
2254 int flags, mode_t mode)
2256 struct gpfs_config_data *config;
2257 int ret;
2258 struct gpfs_fsp_extension *ext;
2260 SMB_VFS_HANDLE_GET_DATA(handle, config,
2261 struct gpfs_config_data,
2262 return -1);
2264 if (config->hsm && !config->recalls &&
2265 vfs_gpfs_fsp_is_offline(handle, fsp)) {
2266 DEBUG(10, ("Refusing access to offline file %s\n",
2267 fsp_str_dbg(fsp)));
2268 errno = EACCES;
2269 return -1;
2272 if (config->syncio) {
2273 flags |= O_SYNC;
2276 ext = VFS_ADD_FSP_EXTENSION(handle, fsp, struct gpfs_fsp_extension,
2277 NULL);
2278 if (ext == NULL) {
2279 errno = ENOMEM;
2280 return -1;
2284 * Assume the file is offline until gpfs tells us it's online.
2286 *ext = (struct gpfs_fsp_extension) { .offline = true };
2288 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2289 if (ret == -1) {
2290 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
2292 return ret;
2295 static ssize_t vfs_gpfs_pread(vfs_handle_struct *handle, files_struct *fsp,
2296 void *data, size_t n, off_t offset)
2298 ssize_t ret;
2299 bool was_offline;
2301 was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2303 ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2305 if ((ret != -1) && was_offline) {
2306 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2307 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2308 fsp->fsp_name->base_name);
2311 return ret;
2314 struct vfs_gpfs_pread_state {
2315 struct files_struct *fsp;
2316 ssize_t ret;
2317 int err;
2318 bool was_offline;
2321 static void vfs_gpfs_pread_done(struct tevent_req *subreq);
2323 static struct tevent_req *vfs_gpfs_pread_send(struct vfs_handle_struct *handle,
2324 TALLOC_CTX *mem_ctx,
2325 struct tevent_context *ev,
2326 struct files_struct *fsp,
2327 void *data, size_t n,
2328 off_t offset)
2330 struct tevent_req *req, *subreq;
2331 struct vfs_gpfs_pread_state *state;
2333 req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
2334 if (req == NULL) {
2335 return NULL;
2337 state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2338 state->fsp = fsp;
2339 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
2340 n, offset);
2341 if (tevent_req_nomem(subreq, req)) {
2342 return tevent_req_post(req, ev);
2344 tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
2345 return req;
2348 static void vfs_gpfs_pread_done(struct tevent_req *subreq)
2350 struct tevent_req *req = tevent_req_callback_data(
2351 subreq, struct tevent_req);
2352 struct vfs_gpfs_pread_state *state = tevent_req_data(
2353 req, struct vfs_gpfs_pread_state);
2355 state->ret = SMB_VFS_PREAD_RECV(subreq, &state->err);
2356 TALLOC_FREE(subreq);
2357 tevent_req_done(req);
2360 static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req, int *err)
2362 struct vfs_gpfs_pread_state *state = tevent_req_data(
2363 req, struct vfs_gpfs_pread_state);
2364 struct files_struct *fsp = state->fsp;
2366 if (tevent_req_is_unix_error(req, err)) {
2367 return -1;
2369 *err = state->err;
2371 if ((state->ret != -1) && state->was_offline) {
2372 DEBUG(10, ("sending notify\n"));
2373 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2374 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2375 fsp->fsp_name->base_name);
2378 return state->ret;
2381 static ssize_t vfs_gpfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
2382 const void *data, size_t n, off_t offset)
2384 ssize_t ret;
2385 bool was_offline;
2387 was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2389 ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2391 if ((ret != -1) && was_offline) {
2392 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2393 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2394 fsp->fsp_name->base_name);
2397 return ret;
2400 struct vfs_gpfs_pwrite_state {
2401 struct files_struct *fsp;
2402 ssize_t ret;
2403 int err;
2404 bool was_offline;
2407 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq);
2409 static struct tevent_req *vfs_gpfs_pwrite_send(
2410 struct vfs_handle_struct *handle,
2411 TALLOC_CTX *mem_ctx,
2412 struct tevent_context *ev,
2413 struct files_struct *fsp,
2414 const void *data, size_t n,
2415 off_t offset)
2417 struct tevent_req *req, *subreq;
2418 struct vfs_gpfs_pwrite_state *state;
2420 req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
2421 if (req == NULL) {
2422 return NULL;
2424 state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2425 state->fsp = fsp;
2426 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
2427 n, offset);
2428 if (tevent_req_nomem(subreq, req)) {
2429 return tevent_req_post(req, ev);
2431 tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
2432 return req;
2435 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
2437 struct tevent_req *req = tevent_req_callback_data(
2438 subreq, struct tevent_req);
2439 struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2440 req, struct vfs_gpfs_pwrite_state);
2442 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->err);
2443 TALLOC_FREE(subreq);
2444 tevent_req_done(req);
2447 static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req, int *err)
2449 struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2450 req, struct vfs_gpfs_pwrite_state);
2451 struct files_struct *fsp = state->fsp;
2453 if (tevent_req_is_unix_error(req, err)) {
2454 return -1;
2456 *err = state->err;
2458 if ((state->ret != -1) && state->was_offline) {
2459 DEBUG(10, ("sending notify\n"));
2460 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2461 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2462 fsp->fsp_name->base_name);
2465 return state->ret;
2469 static struct vfs_fn_pointers vfs_gpfs_fns = {
2470 .connect_fn = vfs_gpfs_connect,
2471 .disk_free_fn = vfs_gpfs_disk_free,
2472 .get_quota_fn = vfs_gpfs_get_quota,
2473 .fs_capabilities_fn = vfs_gpfs_capabilities,
2474 .kernel_flock_fn = vfs_gpfs_kernel_flock,
2475 .linux_setlease_fn = vfs_gpfs_setlease,
2476 .get_real_filename_fn = vfs_gpfs_get_real_filename,
2477 .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
2478 .get_nt_acl_fn = gpfsacl_get_nt_acl,
2479 .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
2480 .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
2481 .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
2482 .sys_acl_blob_get_file_fn = gpfsacl_sys_acl_blob_get_file,
2483 .sys_acl_blob_get_fd_fn = gpfsacl_sys_acl_blob_get_fd,
2484 .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
2485 .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
2486 .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
2487 .chmod_fn = vfs_gpfs_chmod,
2488 .fchmod_fn = vfs_gpfs_fchmod,
2489 .close_fn = vfs_gpfs_close,
2490 .setxattr_fn = gpfs_set_xattr,
2491 .getxattr_fn = gpfs_get_xattr,
2492 .stat_fn = vfs_gpfs_stat,
2493 .fstat_fn = vfs_gpfs_fstat,
2494 .lstat_fn = vfs_gpfs_lstat,
2495 .ntimes_fn = vfs_gpfs_ntimes,
2496 .is_offline_fn = vfs_gpfs_is_offline,
2497 .aio_force_fn = vfs_gpfs_aio_force,
2498 .sendfile_fn = vfs_gpfs_sendfile,
2499 .fallocate_fn = vfs_gpfs_fallocate,
2500 .open_fn = vfs_gpfs_open,
2501 .pread_fn = vfs_gpfs_pread,
2502 .pread_send_fn = vfs_gpfs_pread_send,
2503 .pread_recv_fn = vfs_gpfs_pread_recv,
2504 .pwrite_fn = vfs_gpfs_pwrite,
2505 .pwrite_send_fn = vfs_gpfs_pwrite_send,
2506 .pwrite_recv_fn = vfs_gpfs_pwrite_recv,
2507 .ftruncate_fn = vfs_gpfs_ftruncate
2510 NTSTATUS vfs_gpfs_init(void);
2511 NTSTATUS vfs_gpfs_init(void)
2513 int ret;
2515 ret = gpfswrap_init();
2516 if (ret != 0) {
2517 DEBUG(1, ("Could not initialize GPFS library wrapper\n"));
2520 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
2521 &vfs_gpfs_fns);