vfs_gpfs: Introduce vfs_gpfs_fsp_is_offline
[Samba.git] / source3 / modules / vfs_gpfs.c
blobf40bba76bd0523bb21faefd54802d6665bb32c0a
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 static inline unsigned int gpfs_acl_flags(gpfs_acl_t *gacl)
59 if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
60 return gacl->v4Level1.acl_flags;
62 return 0;
65 static inline gpfs_ace_v4_t *gpfs_ace_ptr(gpfs_acl_t *gacl, unsigned int i)
67 if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
68 return &gacl->v4Level1.ace_v4[i];
70 return &gacl->ace_v4[i];
73 static bool set_gpfs_sharemode(files_struct *fsp, uint32_t access_mask,
74 uint32_t share_access)
76 unsigned int allow = GPFS_SHARE_NONE;
77 unsigned int deny = GPFS_DENY_NONE;
78 int result;
80 if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
81 /* No real file, don't disturb */
82 return True;
85 allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
86 DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
87 allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
88 GPFS_SHARE_READ : 0;
90 if (allow == GPFS_SHARE_NONE) {
91 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
93 else {
94 deny |= (share_access & FILE_SHARE_WRITE) ?
95 0 : GPFS_DENY_WRITE;
96 deny |= (share_access & (FILE_SHARE_READ)) ?
97 0 : GPFS_DENY_READ;
99 DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
100 access_mask, allow, share_access, deny));
102 result = gpfswrap_set_share(fsp->fh->fd, allow, deny);
103 if (result != 0) {
104 if (errno == ENOSYS) {
105 DEBUG(5, ("VFS module vfs_gpfs loaded, but gpfs "
106 "set_share function support not available. "
107 "Allowing access\n"));
108 return True;
109 } else {
110 DEBUG(10, ("gpfs_set_share failed: %s\n",
111 strerror(errno)));
115 return (result == 0);
118 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
119 uint32_t share_mode, uint32_t access_mask)
122 struct gpfs_config_data *config;
123 int ret = 0;
125 SMB_VFS_HANDLE_GET_DATA(handle, config,
126 struct gpfs_config_data,
127 return -1);
129 if(!config->sharemodes) {
130 return 0;
134 * A named stream fsp will have the basefile open in the fsp
135 * fd, so lacking a distinct fd for the stream we have to skip
136 * kernel_flock and set_gpfs_sharemode for stream.
138 if (!is_ntfs_default_stream_smb_fname(fsp->fsp_name)) {
139 DEBUG(2,("%s: kernel_flock on stream\n", fsp_str_dbg(fsp)));
140 return 0;
143 START_PROFILE(syscall_kernel_flock);
145 kernel_flock(fsp->fh->fd, share_mode, access_mask);
147 if (!set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
148 ret = -1;
151 END_PROFILE(syscall_kernel_flock);
153 return ret;
156 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
159 struct gpfs_config_data *config;
161 SMB_VFS_HANDLE_GET_DATA(handle, config,
162 struct gpfs_config_data,
163 return -1);
165 if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
166 set_gpfs_sharemode(fsp, 0, 0);
169 return SMB_VFS_NEXT_CLOSE(handle, fsp);
172 static int set_gpfs_lease(int fd, int leasetype)
174 int gpfs_type = GPFS_LEASE_NONE;
176 if (leasetype == F_RDLCK) {
177 gpfs_type = GPFS_LEASE_READ;
179 if (leasetype == F_WRLCK) {
180 gpfs_type = GPFS_LEASE_WRITE;
183 /* we unconditionally set CAP_LEASE, rather than looking for
184 -1/EACCES as there is a bug in some versions of
185 libgpfs_gpl.so which results in a leaked fd on /dev/ss0
186 each time we try this with the wrong capabilities set
188 linux_set_lease_capability();
189 return gpfswrap_set_lease(fd, gpfs_type);
192 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
193 int leasetype)
195 struct gpfs_config_data *config;
196 int ret=0;
198 SMB_VFS_HANDLE_GET_DATA(handle, config,
199 struct gpfs_config_data,
200 return -1);
202 START_PROFILE(syscall_linux_setlease);
204 if (linux_set_lease_sighandler(fsp->fh->fd) == -1) {
205 ret = -1;
206 goto failure;
209 if (config->leases) {
211 * Ensure the lease owner is root to allow
212 * correct delivery of lease-break signals.
214 become_root();
215 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
216 unbecome_root();
219 failure:
220 END_PROFILE(syscall_linux_setlease);
222 return ret;
225 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
226 const char *path,
227 const char *name,
228 TALLOC_CTX *mem_ctx,
229 char **found_name)
231 int result;
232 char *full_path;
233 char real_pathname[PATH_MAX+1];
234 int buflen;
235 bool mangled;
236 struct gpfs_config_data *config;
238 SMB_VFS_HANDLE_GET_DATA(handle, config,
239 struct gpfs_config_data,
240 return -1);
242 if (!config->getrealfilename) {
243 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
244 mem_ctx, found_name);
247 mangled = mangle_is_mangled(name, handle->conn->params);
248 if (mangled) {
249 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
250 mem_ctx, found_name);
253 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
254 if (full_path == NULL) {
255 errno = ENOMEM;
256 return -1;
259 buflen = sizeof(real_pathname) - 1;
261 result = gpfswrap_get_realfilename_path(full_path, real_pathname,
262 &buflen);
264 TALLOC_FREE(full_path);
266 if ((result == -1) && (errno == ENOSYS)) {
267 return SMB_VFS_NEXT_GET_REAL_FILENAME(
268 handle, path, name, mem_ctx, found_name);
271 if (result == -1) {
272 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
273 strerror(errno)));
274 return -1;
278 * GPFS does not necessarily null-terminate the returned path
279 * but instead returns the buffer length in buflen.
282 if (buflen < sizeof(real_pathname)) {
283 real_pathname[buflen] = '\0';
284 } else {
285 real_pathname[sizeof(real_pathname)-1] = '\0';
288 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
289 path, name, real_pathname));
291 name = strrchr_m(real_pathname, '/');
292 if (name == NULL) {
293 errno = ENOENT;
294 return -1;
297 *found_name = talloc_strdup(mem_ctx, name+1);
298 if (*found_name == NULL) {
299 errno = ENOMEM;
300 return -1;
303 return 0;
306 static void sd2gpfs_control(uint16_t control, struct gpfs_acl *gacl)
308 unsigned int gpfs_aclflags = 0;
309 control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
310 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
311 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
312 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
313 gpfs_aclflags = control << 8;
314 if (!(control & SEC_DESC_DACL_PRESENT))
315 gpfs_aclflags |= ACL4_FLAG_NULL_DACL;
316 if (!(control & SEC_DESC_SACL_PRESENT))
317 gpfs_aclflags |= ACL4_FLAG_NULL_SACL;
318 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
319 gacl->v4Level1.acl_flags = gpfs_aclflags;
322 static uint16_t gpfs2sd_control(unsigned int gpfs_aclflags)
324 uint16_t control = gpfs_aclflags >> 8;
325 control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
326 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
327 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
328 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
329 control |= SEC_DESC_SELF_RELATIVE;
330 return control;
333 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
335 gpfs_aclCount_t i;
336 if (gacl==NULL)
338 DEBUG(0, ("gpfs acl is NULL\n"));
339 return;
342 DEBUG(level, ("len: %d, level: %d, version: %d, nace: %d, "
343 "control: %x\n",
344 gacl->acl_len, gacl->acl_level, gacl->acl_version,
345 gacl->acl_nace, gpfs_acl_flags(gacl)));
347 for(i=0; i<gacl->acl_nace; i++)
349 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
350 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, "
351 "iflags:0x%x, who:%u\n",
352 i, gace->aceType, gace->aceFlags, gace->aceMask,
353 gace->aceIFlags, gace->aceWho));
358 * get the ACL from GPFS, allocated on the specified mem_ctx
359 * internally retries when initial buffer was too small
361 * caller needs to cast result to either
362 * raw = yes: struct gpfs_opaque_acl
363 * raw = no: struct gpfs_acl
366 static void *vfs_gpfs_getacl(TALLOC_CTX *mem_ctx,
367 const char *fname,
368 const bool raw,
369 const gpfs_aclType_t type)
372 void *aclbuf;
373 size_t size = 512;
374 int ret, flags;
375 unsigned int *len;
376 size_t struct_size;
378 again:
380 aclbuf = talloc_zero_size(mem_ctx, size);
381 if (aclbuf == NULL) {
382 errno = ENOMEM;
383 return NULL;
386 if (raw) {
387 struct gpfs_opaque_acl *buf = (struct gpfs_opaque_acl *) aclbuf;
388 buf->acl_type = type;
389 flags = GPFS_GETACL_NATIVE;
390 len = (unsigned int *) &(buf->acl_buffer_len);
391 struct_size = sizeof(struct gpfs_opaque_acl);
392 } else {
393 struct gpfs_acl *buf = (struct gpfs_acl *) aclbuf;
394 buf->acl_type = type;
395 buf->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
396 flags = GPFS_GETACL_STRUCT;
397 len = &(buf->acl_len);
398 /* reserve space for control flags in gpfs 3.5 and beyond */
399 struct_size = sizeof(struct gpfs_acl) + sizeof(unsigned int);
402 /* set the length of the buffer as input value */
403 *len = size;
405 errno = 0;
406 ret = gpfswrap_getacl(discard_const_p(char, fname), flags, aclbuf);
407 if ((ret != 0) && (errno == ENOSPC)) {
409 * get the size needed to accommodate the complete buffer
411 * the value returned only applies to the ACL blob in the
412 * struct so make sure to also have headroom for the first
413 * struct members by adding room for the complete struct
414 * (might be a few bytes too much then)
416 size = *len + struct_size;
417 talloc_free(aclbuf);
418 DEBUG(10, ("Increasing ACL buffer size to %zu\n", size));
419 goto again;
422 if (ret != 0) {
423 DEBUG(5, ("smbd_gpfs_getacl failed with %s\n",
424 strerror(errno)));
425 talloc_free(aclbuf);
426 return NULL;
429 return aclbuf;
432 /* Tries to get nfs4 acls and returns SMB ACL allocated.
433 * On failure returns 1 if it got non-NFSv4 ACL to prompt
434 * retry with POSIX ACL checks.
435 * On failure returns -1 if there is system (GPFS) error, check errno.
436 * Returns 0 on success
438 static int gpfs_get_nfs4_acl(TALLOC_CTX *mem_ctx, const char *fname,
439 struct SMB4ACL_T **ppacl)
441 gpfs_aclCount_t i;
442 struct gpfs_acl *gacl = NULL;
443 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
445 /* Get the ACL */
446 gacl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(), fname,
447 false, 0);
448 if (gacl == NULL) {
449 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
450 fname, strerror(errno)));
451 if (errno == ENODATA) {
453 * GPFS returns ENODATA for snapshot
454 * directories. Retry with POSIX ACLs check.
456 return 1;
459 return -1;
462 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
463 DEBUG(10, ("Got non-nfsv4 acl\n"));
464 /* Retry with POSIX ACLs check */
465 talloc_free(gacl);
466 return 1;
469 *ppacl = smb_create_smb4acl(mem_ctx);
471 if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
472 uint16_t control = gpfs2sd_control(gpfs_acl_flags(gacl));
473 smbacl4_set_controlflags(*ppacl, control);
476 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d, control: %x\n",
477 gacl->acl_len, gacl->acl_level, gacl->acl_version,
478 gacl->acl_nace, gpfs_acl_flags(gacl)));
480 for (i=0; i<gacl->acl_nace; i++) {
481 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
482 SMB_ACE4PROP_T smbace = { 0 };
483 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
484 "who: %d\n", gace->aceType, gace->aceIFlags,
485 gace->aceFlags, gace->aceMask, gace->aceWho));
487 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
488 smbace.flags |= SMB_ACE4_ID_SPECIAL;
489 switch (gace->aceWho) {
490 case ACE4_SPECIAL_OWNER:
491 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
492 break;
493 case ACE4_SPECIAL_GROUP:
494 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
495 break;
496 case ACE4_SPECIAL_EVERYONE:
497 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
498 break;
499 default:
500 DEBUG(8, ("invalid special gpfs id %d "
501 "ignored\n", gace->aceWho));
502 continue; /* don't add it */
504 } else {
505 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
506 smbace.who.gid = gace->aceWho;
507 else
508 smbace.who.uid = gace->aceWho;
511 /* remove redundant deny entries */
512 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
513 struct gpfs_ace_v4 *prev = gpfs_ace_ptr(gacl, i - 1);
514 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
515 prev->aceFlags == gace->aceFlags &&
516 prev->aceIFlags == gace->aceIFlags &&
517 (gace->aceMask & prev->aceMask) == 0 &&
518 gace->aceWho == prev->aceWho) {
519 /* it's redundant - skip it */
520 continue;
524 smbace.aceType = gace->aceType;
525 smbace.aceFlags = gace->aceFlags;
526 smbace.aceMask = gace->aceMask;
527 smb_add_ace4(*ppacl, &smbace);
530 talloc_free(gacl);
532 return 0;
535 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
536 files_struct *fsp, uint32_t security_info,
537 TALLOC_CTX *mem_ctx,
538 struct security_descriptor **ppdesc)
540 struct SMB4ACL_T *pacl = NULL;
541 int result;
542 struct gpfs_config_data *config;
543 TALLOC_CTX *frame = talloc_stackframe();
544 NTSTATUS status;
546 *ppdesc = NULL;
548 SMB_VFS_HANDLE_GET_DATA(handle, config,
549 struct gpfs_config_data,
550 return NT_STATUS_INTERNAL_ERROR);
552 if (!config->acl) {
553 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
554 mem_ctx, ppdesc);
555 TALLOC_FREE(frame);
556 return status;
559 result = gpfs_get_nfs4_acl(frame, fsp->fsp_name->base_name, &pacl);
561 if (result == 0) {
562 status = smb_fget_nt_acl_nfs4(fsp, security_info, mem_ctx,
563 ppdesc, pacl);
564 TALLOC_FREE(frame);
565 return status;
568 if (result > 0) {
569 DEBUG(10, ("retrying with posix acl...\n"));
570 status = posix_fget_nt_acl(fsp, security_info,
571 mem_ctx, ppdesc);
572 TALLOC_FREE(frame);
573 return status;
576 TALLOC_FREE(frame);
578 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
579 return map_nt_error_from_unix(errno);
582 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
583 const char *name,
584 uint32_t security_info,
585 TALLOC_CTX *mem_ctx, struct security_descriptor **ppdesc)
587 struct SMB4ACL_T *pacl = NULL;
588 int result;
589 struct gpfs_config_data *config;
590 TALLOC_CTX *frame = talloc_stackframe();
591 NTSTATUS status;
593 *ppdesc = NULL;
595 SMB_VFS_HANDLE_GET_DATA(handle, config,
596 struct gpfs_config_data,
597 return NT_STATUS_INTERNAL_ERROR);
599 if (!config->acl) {
600 status = SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info,
601 mem_ctx, ppdesc);
602 TALLOC_FREE(frame);
603 return status;
606 result = gpfs_get_nfs4_acl(frame, name, &pacl);
608 if (result == 0) {
609 status = smb_get_nt_acl_nfs4(handle->conn, name, security_info,
610 mem_ctx, ppdesc, pacl);
611 TALLOC_FREE(frame);
612 return status;
615 if (result > 0) {
616 DEBUG(10, ("retrying with posix acl...\n"));
617 status = posix_get_nt_acl(handle->conn, name, security_info,
618 mem_ctx, ppdesc);
619 TALLOC_FREE(frame);
620 return status;
623 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
624 TALLOC_FREE(frame);
625 return map_nt_error_from_unix(errno);
628 static struct gpfs_acl *vfs_gpfs_smbacl2gpfsacl(TALLOC_CTX *mem_ctx,
629 files_struct *fsp,
630 struct SMB4ACL_T *smbacl,
631 bool controlflags)
633 struct gpfs_acl *gacl;
634 gpfs_aclLen_t gacl_len;
635 struct SMB4ACE_T *smbace;
637 gacl_len = offsetof(gpfs_acl_t, ace_v4) + sizeof(unsigned int)
638 + smb_get_naces(smbacl) * sizeof(gpfs_ace_v4_t);
640 gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
641 if (gacl == NULL) {
642 DEBUG(0, ("talloc failed\n"));
643 errno = ENOMEM;
644 return NULL;
647 gacl->acl_level = GPFS_ACL_LEVEL_BASE;
648 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
649 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
650 gacl->acl_nace = 0; /* change later... */
652 if (controlflags) {
653 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
654 sd2gpfs_control(smbacl4_get_controlflags(smbacl), gacl);
657 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
658 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, gacl->acl_nace);
659 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
661 gace->aceType = aceprop->aceType;
662 gace->aceFlags = aceprop->aceFlags;
663 gace->aceMask = aceprop->aceMask;
666 * GPFS can't distinguish between WRITE and APPEND on
667 * files, so one being set without the other is an
668 * error. Sorry for the many ()'s :-)
671 if (!fsp->is_directory
673 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
674 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
676 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
677 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
679 lp_parm_bool(fsp->conn->params->service, "gpfs",
680 "merge_writeappend", True)) {
681 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
682 "WRITE^APPEND, setting WRITE|APPEND\n",
683 fsp_str_dbg(fsp)));
684 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
687 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
689 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
691 switch(aceprop->who.special_id)
693 case SMB_ACE4_WHO_EVERYONE:
694 gace->aceWho = ACE4_SPECIAL_EVERYONE;
695 break;
696 case SMB_ACE4_WHO_OWNER:
697 gace->aceWho = ACE4_SPECIAL_OWNER;
698 break;
699 case SMB_ACE4_WHO_GROUP:
700 gace->aceWho = ACE4_SPECIAL_GROUP;
701 break;
702 default:
703 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
704 continue; /* don't add it !!! */
706 } else {
707 /* just only for the type safety... */
708 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
709 gace->aceWho = aceprop->who.gid;
710 else
711 gace->aceWho = aceprop->who.uid;
714 gacl->acl_nace++;
716 gacl->acl_len = (char *)gpfs_ace_ptr(gacl, gacl->acl_nace)
717 - (char *)gacl;
718 return gacl;
721 static bool gpfsacl_process_smbacl(vfs_handle_struct *handle,
722 files_struct *fsp,
723 struct SMB4ACL_T *smbacl)
725 int ret;
726 struct gpfs_acl *gacl;
727 TALLOC_CTX *mem_ctx = talloc_tos();
729 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, true);
730 if (gacl == NULL) { /* out of memory */
731 return False;
733 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
734 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
736 if ((ret != 0) && (errno == EINVAL)) {
737 DEBUG(10, ("Retry without nfs41 control flags\n"));
738 talloc_free(gacl);
739 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, false);
740 if (gacl == NULL) { /* out of memory */
741 return False;
743 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
744 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA,
745 gacl);
748 if (ret != 0) {
749 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
750 gpfs_dumpacl(8, gacl);
751 return False;
754 DEBUG(10, ("gpfs_putacl succeeded\n"));
755 return True;
758 static NTSTATUS gpfsacl_set_nt_acl_internal(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
760 struct gpfs_acl *acl;
761 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
763 acl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(),
764 fsp->fsp_name->base_name,
765 false, 0);
766 if (acl == NULL) {
767 return map_nt_error_from_unix(errno);
770 if (acl->acl_version == GPFS_ACL_VERSION_NFS4) {
771 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
772 "refuse_dacl_protected", false)
773 && (psd->type&SEC_DESC_DACL_PROTECTED)) {
774 DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
775 talloc_free(acl);
776 return NT_STATUS_NOT_SUPPORTED;
779 result = smb_set_nt_acl_nfs4(handle,
780 fsp, security_info_sent, psd,
781 gpfsacl_process_smbacl);
782 } else { /* assume POSIX ACL - by default... */
783 result = set_nt_acl(fsp, security_info_sent, psd);
786 talloc_free(acl);
787 return result;
790 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
792 struct gpfs_config_data *config;
794 SMB_VFS_HANDLE_GET_DATA(handle, config,
795 struct gpfs_config_data,
796 return NT_STATUS_INTERNAL_ERROR);
798 if (!config->acl) {
799 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
802 return gpfsacl_set_nt_acl_internal(handle, fsp, security_info_sent, psd);
805 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl, TALLOC_CTX *mem_ctx)
807 SMB_ACL_T result;
808 gpfs_aclCount_t i;
810 result = sys_acl_init(mem_ctx);
811 if (result == NULL) {
812 errno = ENOMEM;
813 return NULL;
816 result->count = pacl->acl_nace;
817 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry,
818 result->count);
819 if (result->acl == NULL) {
820 TALLOC_FREE(result);
821 errno = ENOMEM;
822 return NULL;
825 for (i=0; i<pacl->acl_nace; i++) {
826 struct smb_acl_entry *ace = &result->acl[i];
827 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
829 DEBUG(10, ("Converting type %d id %lu perm %x\n",
830 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
831 (int)g_ace->ace_perm));
833 switch (g_ace->ace_type) {
834 case GPFS_ACL_USER:
835 ace->a_type = SMB_ACL_USER;
836 ace->info.user.uid = (uid_t)g_ace->ace_who;
837 break;
838 case GPFS_ACL_USER_OBJ:
839 ace->a_type = SMB_ACL_USER_OBJ;
840 break;
841 case GPFS_ACL_GROUP:
842 ace->a_type = SMB_ACL_GROUP;
843 ace->info.group.gid = (gid_t)g_ace->ace_who;
844 break;
845 case GPFS_ACL_GROUP_OBJ:
846 ace->a_type = SMB_ACL_GROUP_OBJ;
847 break;
848 case GPFS_ACL_OTHER:
849 ace->a_type = SMB_ACL_OTHER;
850 break;
851 case GPFS_ACL_MASK:
852 ace->a_type = SMB_ACL_MASK;
853 break;
854 default:
855 DEBUG(10, ("Got invalid ace_type: %d\n",
856 g_ace->ace_type));
857 TALLOC_FREE(result);
858 errno = EINVAL;
859 return NULL;
862 ace->a_perm = 0;
863 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
864 SMB_ACL_READ : 0;
865 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
866 SMB_ACL_WRITE : 0;
867 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
868 SMB_ACL_EXECUTE : 0;
870 DEBUGADD(10, ("Converted to %d perm %x\n",
871 ace->a_type, ace->a_perm));
874 return result;
877 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type,
878 TALLOC_CTX *mem_ctx)
880 struct gpfs_acl *pacl;
881 SMB_ACL_T result = NULL;
883 pacl = vfs_gpfs_getacl(talloc_tos(), path, false, type);
885 if (pacl == NULL) {
886 DEBUG(10, ("vfs_gpfs_getacl failed for %s with %s\n",
887 path, strerror(errno)));
888 if (errno == 0) {
889 errno = EINVAL;
891 goto done;
894 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
895 DEBUG(10, ("Got acl version %d, expected %d\n",
896 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
897 errno = EINVAL;
898 goto done;
901 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
902 pacl->acl_len, pacl->acl_level, pacl->acl_version,
903 pacl->acl_nace));
905 result = gpfs2smb_acl(pacl, mem_ctx);
906 if (result != NULL) {
907 errno = 0;
910 done:
912 if (pacl != NULL) {
913 talloc_free(pacl);
915 if (errno != 0) {
916 TALLOC_FREE(result);
918 return result;
921 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
922 const char *path_p,
923 SMB_ACL_TYPE_T type,
924 TALLOC_CTX *mem_ctx)
926 gpfs_aclType_t gpfs_type;
927 struct gpfs_config_data *config;
929 SMB_VFS_HANDLE_GET_DATA(handle, config,
930 struct gpfs_config_data,
931 return NULL);
933 if (!config->acl) {
934 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p,
935 type, mem_ctx);
938 switch(type) {
939 case SMB_ACL_TYPE_ACCESS:
940 gpfs_type = GPFS_ACL_TYPE_ACCESS;
941 break;
942 case SMB_ACL_TYPE_DEFAULT:
943 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
944 break;
945 default:
946 DEBUG(0, ("Got invalid type: %d\n", type));
947 smb_panic("exiting");
950 return gpfsacl_get_posix_acl(path_p, gpfs_type, mem_ctx);
953 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
954 files_struct *fsp,
955 TALLOC_CTX *mem_ctx)
957 struct gpfs_config_data *config;
959 SMB_VFS_HANDLE_GET_DATA(handle, config,
960 struct gpfs_config_data,
961 return NULL);
963 if (!config->acl) {
964 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
967 return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
968 GPFS_ACL_TYPE_ACCESS, mem_ctx);
971 static int gpfsacl_sys_acl_blob_get_file(vfs_handle_struct *handle,
972 const char *path_p,
973 TALLOC_CTX *mem_ctx,
974 char **blob_description,
975 DATA_BLOB *blob)
977 struct gpfs_config_data *config;
978 struct gpfs_opaque_acl *acl = NULL;
979 DATA_BLOB aclblob;
980 int result;
982 SMB_VFS_HANDLE_GET_DATA(handle, config,
983 struct gpfs_config_data,
984 return -1);
986 if (!config->acl) {
987 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, path_p,
988 mem_ctx,
989 blob_description,
990 blob);
993 errno = 0;
994 acl = (struct gpfs_opaque_acl *)
995 vfs_gpfs_getacl(mem_ctx,
996 path_p,
997 true,
998 GPFS_ACL_TYPE_NFS4);
1000 if (errno) {
1001 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1002 errno, strerror(errno)));
1004 /* EINVAL means POSIX ACL, bail out on other cases */
1005 if (errno != EINVAL) {
1006 return -1;
1010 if (acl != NULL) {
1012 * file has NFSv4 ACL
1014 * we only need the actual ACL blob here
1015 * acl_version will always be NFS4 because we asked
1016 * for NFS4
1017 * acl_type is only used for POSIX ACLs
1019 aclblob.data = (uint8_t*) acl->acl_var_data;
1020 aclblob.length = acl->acl_buffer_len;
1022 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1023 if (!*blob_description) {
1024 talloc_free(acl);
1025 errno = ENOMEM;
1026 return -1;
1029 result = non_posix_sys_acl_blob_get_file_helper(handle, path_p,
1030 aclblob,
1031 mem_ctx, blob);
1033 talloc_free(acl);
1034 return result;
1037 /* fall back to POSIX ACL */
1038 return posix_sys_acl_blob_get_file(handle, path_p, mem_ctx,
1039 blob_description, blob);
1042 static int gpfsacl_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1043 files_struct *fsp,
1044 TALLOC_CTX *mem_ctx,
1045 char **blob_description,
1046 DATA_BLOB *blob)
1048 struct gpfs_config_data *config;
1049 struct gpfs_opaque_acl *acl = NULL;
1050 DATA_BLOB aclblob;
1051 int result;
1053 SMB_VFS_HANDLE_GET_DATA(handle, config,
1054 struct gpfs_config_data,
1055 return -1);
1057 if (!config->acl) {
1058 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1059 blob_description, blob);
1062 errno = 0;
1063 acl = (struct gpfs_opaque_acl *) vfs_gpfs_getacl(mem_ctx,
1064 fsp->fsp_name->base_name,
1065 true,
1066 GPFS_ACL_TYPE_NFS4);
1068 if (errno) {
1069 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1070 errno, strerror(errno)));
1072 /* EINVAL means POSIX ACL, bail out on other cases */
1073 if (errno != EINVAL) {
1074 return -1;
1078 if (acl != NULL) {
1080 * file has NFSv4 ACL
1082 * we only need the actual ACL blob here
1083 * acl_version will always be NFS4 because we asked
1084 * for NFS4
1085 * acl_type is only used for POSIX ACLs
1087 aclblob.data = (uint8_t*) acl->acl_var_data;
1088 aclblob.length = acl->acl_buffer_len;
1090 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1091 if (!*blob_description) {
1092 talloc_free(acl);
1093 errno = ENOMEM;
1094 return -1;
1097 result = non_posix_sys_acl_blob_get_fd_helper(handle, fsp,
1098 aclblob, mem_ctx,
1099 blob);
1101 talloc_free(acl);
1102 return result;
1105 /* fall back to POSIX ACL */
1106 return posix_sys_acl_blob_get_fd(handle, fsp, mem_ctx,
1107 blob_description, blob);
1110 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
1111 SMB_ACL_TYPE_T type)
1113 gpfs_aclLen_t len;
1114 struct gpfs_acl *result;
1115 int i;
1117 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
1119 len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
1120 sizeof(gpfs_ace_v1_t);
1122 result = (struct gpfs_acl *)SMB_MALLOC(len);
1123 if (result == NULL) {
1124 errno = ENOMEM;
1125 return result;
1128 result->acl_len = len;
1129 result->acl_level = 0;
1130 result->acl_version = GPFS_ACL_VERSION_POSIX;
1131 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
1132 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
1133 result->acl_nace = pacl->count;
1135 for (i=0; i<pacl->count; i++) {
1136 const struct smb_acl_entry *ace = &pacl->acl[i];
1137 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
1139 DEBUG(10, ("Converting type %d perm %x\n",
1140 (int)ace->a_type, (int)ace->a_perm));
1142 g_ace->ace_perm = 0;
1144 switch(ace->a_type) {
1145 case SMB_ACL_USER:
1146 g_ace->ace_type = GPFS_ACL_USER;
1147 g_ace->ace_who = (gpfs_uid_t)ace->info.user.uid;
1148 break;
1149 case SMB_ACL_USER_OBJ:
1150 g_ace->ace_type = GPFS_ACL_USER_OBJ;
1151 g_ace->ace_perm |= ACL_PERM_CONTROL;
1152 g_ace->ace_who = 0;
1153 break;
1154 case SMB_ACL_GROUP:
1155 g_ace->ace_type = GPFS_ACL_GROUP;
1156 g_ace->ace_who = (gpfs_uid_t)ace->info.group.gid;
1157 break;
1158 case SMB_ACL_GROUP_OBJ:
1159 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
1160 g_ace->ace_who = 0;
1161 break;
1162 case SMB_ACL_MASK:
1163 g_ace->ace_type = GPFS_ACL_MASK;
1164 g_ace->ace_perm = 0x8f;
1165 g_ace->ace_who = 0;
1166 break;
1167 case SMB_ACL_OTHER:
1168 g_ace->ace_type = GPFS_ACL_OTHER;
1169 g_ace->ace_who = 0;
1170 break;
1171 default:
1172 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
1173 errno = EINVAL;
1174 SAFE_FREE(result);
1175 return NULL;
1178 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
1179 ACL_PERM_READ : 0;
1180 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
1181 ACL_PERM_WRITE : 0;
1182 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
1183 ACL_PERM_EXECUTE : 0;
1185 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
1186 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
1189 return result;
1192 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
1193 const char *name,
1194 SMB_ACL_TYPE_T type,
1195 SMB_ACL_T theacl)
1197 struct gpfs_acl *gpfs_acl;
1198 int result;
1199 struct gpfs_config_data *config;
1201 SMB_VFS_HANDLE_GET_DATA(handle, config,
1202 struct gpfs_config_data,
1203 return -1);
1205 if (!config->acl) {
1206 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, type, theacl);
1209 gpfs_acl = smb2gpfs_acl(theacl, type);
1210 if (gpfs_acl == NULL) {
1211 return -1;
1214 result = gpfswrap_putacl(discard_const_p(char, name),
1215 GPFS_PUTACL_STRUCT|GPFS_ACL_SAMBA, gpfs_acl);
1217 SAFE_FREE(gpfs_acl);
1218 return result;
1221 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
1222 files_struct *fsp,
1223 SMB_ACL_T theacl)
1225 struct gpfs_config_data *config;
1227 SMB_VFS_HANDLE_GET_DATA(handle, config,
1228 struct gpfs_config_data,
1229 return -1);
1231 if (!config->acl) {
1232 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1235 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
1236 SMB_ACL_TYPE_ACCESS, theacl);
1239 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
1240 const char *path)
1242 struct gpfs_config_data *config;
1244 SMB_VFS_HANDLE_GET_DATA(handle, config,
1245 struct gpfs_config_data,
1246 return -1);
1248 if (!config->acl) {
1249 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
1252 errno = ENOTSUP;
1253 return -1;
1257 * Assumed: mode bits are shiftable and standard
1258 * Output: the new aceMask field for an smb nfs4 ace
1260 static uint32_t gpfsacl_mask_filter(uint32_t aceType, uint32_t aceMask, uint32_t rwx)
1262 const uint32_t posix_nfs4map[3] = {
1263 SMB_ACE4_EXECUTE, /* execute */
1264 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
1265 SMB_ACE4_READ_DATA /* read */
1267 int i;
1268 uint32_t posix_mask = 0x01;
1269 uint32_t posix_bit;
1270 uint32_t nfs4_bits;
1272 for(i=0; i<3; i++) {
1273 nfs4_bits = posix_nfs4map[i];
1274 posix_bit = rwx & posix_mask;
1276 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
1277 if (posix_bit)
1278 aceMask |= nfs4_bits;
1279 else
1280 aceMask &= ~nfs4_bits;
1281 } else {
1282 /* add deny bits when suitable */
1283 if (!posix_bit)
1284 aceMask |= nfs4_bits;
1285 else
1286 aceMask &= ~nfs4_bits;
1287 } /* other ace types are unexpected */
1289 posix_mask <<= 1;
1292 return aceMask;
1295 static int gpfsacl_emu_chmod(vfs_handle_struct *handle,
1296 const char *path, mode_t mode)
1298 struct SMB4ACL_T *pacl = NULL;
1299 int result;
1300 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
1301 int i;
1302 files_struct fake_fsp = { 0 }; /* TODO: rationalize parametrization */
1303 struct SMB4ACE_T *smbace;
1304 TALLOC_CTX *frame = talloc_stackframe();
1306 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
1308 result = gpfs_get_nfs4_acl(frame, path, &pacl);
1309 if (result) {
1310 TALLOC_FREE(frame);
1311 return result;
1314 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
1315 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
1318 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
1319 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
1320 uint32_t specid = ace->who.special_id;
1322 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
1323 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
1324 specid <= SMB_ACE4_WHO_EVERYONE) {
1326 uint32_t newMask;
1328 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
1329 haveAllowEntry[specid] = True;
1331 /* mode >> 6 for @owner, mode >> 3 for @group,
1332 * mode >> 0 for @everyone */
1333 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
1334 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
1335 if (ace->aceMask!=newMask) {
1336 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
1337 path, ace->aceMask, newMask, specid));
1339 ace->aceMask = newMask;
1343 /* make sure we have at least ALLOW entries
1344 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
1345 * - if necessary
1347 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
1348 SMB_ACE4PROP_T ace = { 0 };
1350 if (haveAllowEntry[i]==True)
1351 continue;
1353 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
1354 ace.flags |= SMB_ACE4_ID_SPECIAL;
1355 ace.who.special_id = i;
1357 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
1358 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
1360 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
1361 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
1363 /* don't add unnecessary aces */
1364 if (!ace.aceMask)
1365 continue;
1367 /* we add it to the END - as windows expects allow aces */
1368 smb_add_ace4(pacl, &ace);
1369 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
1370 path, mode, i, ace.aceMask));
1373 /* don't add complementary DENY ACEs here */
1374 fake_fsp.fsp_name = synthetic_smb_fname(
1375 frame, path, NULL, NULL);
1376 if (fake_fsp.fsp_name == NULL) {
1377 errno = ENOMEM;
1378 TALLOC_FREE(frame);
1379 return -1;
1381 /* put the acl */
1382 if (gpfsacl_process_smbacl(handle, &fake_fsp, pacl) == False) {
1383 TALLOC_FREE(frame);
1384 return -1;
1387 TALLOC_FREE(frame);
1388 return 0; /* ok for [f]chmod */
1391 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
1393 struct smb_filename *smb_fname_cpath;
1394 int rc;
1396 smb_fname_cpath = synthetic_smb_fname(talloc_tos(), path, NULL, NULL);
1397 if (smb_fname_cpath == NULL) {
1398 errno = ENOMEM;
1399 return -1;
1402 if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
1403 return -1;
1406 /* avoid chmod() if possible, to preserve acls */
1407 if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
1408 return 0;
1411 rc = gpfsacl_emu_chmod(handle, path, mode);
1412 if (rc == 1)
1413 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1414 return rc;
1417 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1419 SMB_STRUCT_STAT st;
1420 int rc;
1422 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1423 return -1;
1426 /* avoid chmod() if possible, to preserve acls */
1427 if ((st.st_ex_mode & ~S_IFMT) == mode) {
1428 return 0;
1431 rc = gpfsacl_emu_chmod(handle, fsp->fsp_name->base_name,
1432 mode);
1433 if (rc == 1)
1434 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1435 return rc;
1438 static int gpfs_set_xattr(struct vfs_handle_struct *handle, const char *path,
1439 const char *name, const void *value, size_t size, int flags){
1440 struct xattr_DOSATTRIB dosattrib;
1441 enum ndr_err_code ndr_err;
1442 DATA_BLOB blob;
1443 unsigned int dosmode=0;
1444 struct gpfs_winattr attrs;
1445 int ret = 0;
1446 struct gpfs_config_data *config;
1448 SMB_VFS_HANDLE_GET_DATA(handle, config,
1449 struct gpfs_config_data,
1450 return -1);
1452 if (!config->winattr) {
1453 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
1454 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1457 DEBUG(10, ("gpfs_set_xattr: %s \n",path));
1459 /* Only handle DOS Attributes */
1460 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1461 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
1462 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1465 blob.data = discard_const_p(uint8_t, value);
1466 blob.length = size;
1468 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
1469 (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
1471 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1472 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
1473 "from EA on file %s: Error = %s\n",
1474 path, ndr_errstr(ndr_err)));
1475 return false;
1478 if (dosattrib.version != 3) {
1479 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
1480 "%d\n", (int)dosattrib.version));
1481 return false;
1483 if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
1484 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
1485 "valid, ignoring\n"));
1486 return true;
1489 dosmode = dosattrib.info.info3.attrib;
1491 attrs.winAttrs = 0;
1492 /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1493 if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1494 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1496 if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1497 attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1499 if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1500 attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1502 if (dosmode & FILE_ATTRIBUTE_READONLY){
1503 attrs.winAttrs |= GPFS_WINATTR_READONLY;
1505 if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1506 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1510 ret = gpfswrap_set_winattrs_path(discard_const_p(char, path),
1511 GPFS_WINATTR_SET_ATTRS, &attrs);
1512 if ( ret == -1){
1513 if (errno == ENOSYS) {
1514 return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1515 size, flags);
1518 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1519 return -1;
1522 DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1523 return 0;
1526 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle, const char *path,
1527 const char *name, void *value, size_t size){
1528 char *attrstr = value;
1529 unsigned int dosmode = 0;
1530 struct gpfs_winattr attrs;
1531 int ret = 0;
1532 struct gpfs_config_data *config;
1534 SMB_VFS_HANDLE_GET_DATA(handle, config,
1535 struct gpfs_config_data,
1536 return -1);
1538 if (!config->winattr) {
1539 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1540 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1543 DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1545 /* Only handle DOS Attributes */
1546 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1547 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1548 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1551 ret = gpfswrap_get_winattrs_path(discard_const_p(char, path), &attrs);
1552 if ( ret == -1){
1553 int dbg_lvl;
1555 if (errno == ENOSYS) {
1556 return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1557 size);
1560 if (errno != EPERM && errno != EACCES) {
1561 dbg_lvl = 1;
1562 } else {
1563 dbg_lvl = 5;
1565 DEBUG(dbg_lvl, ("gpfs_get_xattr: Get GPFS attributes failed: "
1566 "%d (%s)\n", ret, strerror(errno)));
1567 return -1;
1570 DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1572 /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1573 if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1574 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1576 if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1577 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1579 if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1580 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1582 if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1583 dosmode |= FILE_ATTRIBUTE_READONLY;
1585 if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1586 dosmode |= FILE_ATTRIBUTE_SPARSE;
1589 snprintf(attrstr, size, "0x%2.2x",
1590 (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1591 DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1592 return 4;
1595 #if defined(HAVE_FSTATAT)
1596 static int stat_with_capability(struct vfs_handle_struct *handle,
1597 struct smb_filename *smb_fname, int flag)
1599 int fd = -1;
1600 bool b;
1601 char *dir_name;
1602 const char *rel_name = NULL;
1603 struct stat st;
1604 int ret = -1;
1606 b = parent_dirname(talloc_tos(), smb_fname->base_name,
1607 &dir_name, &rel_name);
1608 if (!b) {
1609 errno = ENOMEM;
1610 return -1;
1613 fd = open(dir_name, O_RDONLY, 0);
1614 TALLOC_FREE(dir_name);
1615 if (fd == -1) {
1616 return -1;
1619 set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1620 ret = fstatat(fd, rel_name, &st, flag);
1621 drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1623 close(fd);
1625 if (ret == 0) {
1626 init_stat_ex_from_stat(
1627 &smb_fname->st, &st,
1628 lp_fake_directory_create_times(SNUM(handle->conn)));
1631 return ret;
1633 #endif
1635 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1636 struct smb_filename *smb_fname)
1638 struct gpfs_winattr attrs;
1639 char *fname = NULL;
1640 NTSTATUS status;
1641 int ret;
1642 struct gpfs_config_data *config;
1644 SMB_VFS_HANDLE_GET_DATA(handle, config,
1645 struct gpfs_config_data,
1646 return -1);
1648 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1649 #if defined(HAVE_FSTATAT)
1650 if (ret == -1 && errno == EACCES) {
1651 DEBUG(10, ("Trying stat with capability for %s\n",
1652 smb_fname->base_name));
1653 ret = stat_with_capability(handle, smb_fname, 0);
1655 #endif
1656 if (ret == -1) {
1657 return -1;
1660 if (!config->winattr) {
1661 return 0;
1664 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1665 if (!NT_STATUS_IS_OK(status)) {
1666 errno = map_errno_from_nt_status(status);
1667 return -1;
1669 ret = gpfswrap_get_winattrs_path(discard_const_p(char, fname), &attrs);
1670 TALLOC_FREE(fname);
1671 if (ret == 0) {
1672 smb_fname->st.st_ex_calculated_birthtime = false;
1673 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1674 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1676 return 0;
1679 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1680 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1682 struct gpfs_winattr attrs;
1683 int ret;
1684 struct gpfs_config_data *config;
1686 SMB_VFS_HANDLE_GET_DATA(handle, config,
1687 struct gpfs_config_data,
1688 return -1);
1690 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1691 if (ret == -1) {
1692 return -1;
1694 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1695 return 0;
1697 if (!config->winattr) {
1698 return 0;
1701 ret = gpfswrap_get_winattrs(fsp->fh->fd, &attrs);
1702 if (ret == 0) {
1703 sbuf->st_ex_calculated_birthtime = false;
1704 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1705 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1707 return 0;
1710 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1711 struct smb_filename *smb_fname)
1713 struct gpfs_winattr attrs;
1714 char *path = NULL;
1715 NTSTATUS status;
1716 int ret;
1717 struct gpfs_config_data *config;
1719 SMB_VFS_HANDLE_GET_DATA(handle, config,
1720 struct gpfs_config_data,
1721 return -1);
1723 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1724 #if defined(HAVE_FSTATAT)
1725 if (ret == -1 && errno == EACCES) {
1726 DEBUG(10, ("Trying lstat with capability for %s\n",
1727 smb_fname->base_name));
1728 ret = stat_with_capability(handle, smb_fname,
1729 AT_SYMLINK_NOFOLLOW);
1731 #endif
1733 if (ret == -1) {
1734 return -1;
1736 if (!config->winattr) {
1737 return 0;
1740 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1741 if (!NT_STATUS_IS_OK(status)) {
1742 errno = map_errno_from_nt_status(status);
1743 return -1;
1745 ret = gpfswrap_get_winattrs_path(discard_const_p(char, path), &attrs);
1746 TALLOC_FREE(path);
1747 if (ret == 0) {
1748 smb_fname->st.st_ex_calculated_birthtime = false;
1749 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1750 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1752 return 0;
1755 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
1756 int idx, int *flags)
1758 if (!null_timespec(ts)) {
1759 *flags |= 1 << idx;
1760 gt[idx].tv_sec = ts.tv_sec;
1761 gt[idx].tv_nsec = ts.tv_nsec;
1762 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
1766 static int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
1768 gpfs_timestruc_t gpfs_times[4];
1769 int flags = 0;
1770 int rc;
1772 ZERO_ARRAY(gpfs_times);
1773 timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
1774 timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
1775 /* No good mapping from LastChangeTime to ctime, not storing */
1776 timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
1778 if (!flags) {
1779 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
1780 return 0;
1783 rc = gpfswrap_set_times_path(path, flags, gpfs_times);
1785 if (rc != 0 && errno != ENOSYS) {
1786 DEBUG(1,("gpfs_set_times() returned with error %s\n",
1787 strerror(errno)));
1790 return rc;
1793 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1794 const struct smb_filename *smb_fname,
1795 struct smb_file_time *ft)
1798 struct gpfs_winattr attrs;
1799 int ret;
1800 char *path = NULL;
1801 NTSTATUS status;
1802 struct gpfs_config_data *config;
1804 SMB_VFS_HANDLE_GET_DATA(handle, config,
1805 struct gpfs_config_data,
1806 return -1);
1808 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1809 if (!NT_STATUS_IS_OK(status)) {
1810 errno = map_errno_from_nt_status(status);
1811 return -1;
1814 /* Try to use gpfs_set_times if it is enabled and available */
1815 if (config->settimes) {
1816 ret = smbd_gpfs_set_times_path(path, ft);
1818 if (ret == 0 || (ret == -1 && errno != ENOSYS)) {
1819 return ret;
1823 DEBUG(10,("gpfs_set_times() not available or disabled, "
1824 "use ntimes and winattr\n"));
1826 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1827 if(ret == -1){
1828 /* don't complain if access was denied */
1829 if (errno != EPERM && errno != EACCES) {
1830 DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1831 "%s", strerror(errno)));
1833 return -1;
1836 if(null_timespec(ft->create_time)){
1837 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1838 return 0;
1841 if (!config->winattr) {
1842 return 0;
1845 attrs.winAttrs = 0;
1846 attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1847 attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1849 ret = gpfswrap_set_winattrs_path(discard_const_p(char, path),
1850 GPFS_WINATTR_SET_CREATION_TIME,
1851 &attrs);
1852 if(ret == -1 && errno != ENOSYS){
1853 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1854 return -1;
1856 return 0;
1860 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1861 struct files_struct *fsp, uint32_t mode,
1862 off_t offset, off_t len)
1864 int ret;
1865 struct gpfs_config_data *config;
1867 SMB_VFS_HANDLE_GET_DATA(handle, config,
1868 struct gpfs_config_data,
1869 return -1);
1871 if (!config->prealloc) {
1872 /* you should better not run fallocate() on GPFS at all */
1873 errno = ENOTSUP;
1874 return -1;
1877 if (mode != 0) {
1878 DEBUG(10, ("unmapped fallocate flags: %lx\n",
1879 (unsigned long)mode));
1880 errno = ENOTSUP;
1881 return -1;
1884 ret = gpfswrap_prealloc(fsp->fh->fd, offset, len);
1886 if (ret == -1 && errno != ENOSYS) {
1887 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1888 } else if (ret == -1 && errno == ENOSYS) {
1889 DEBUG(10, ("GPFS prealloc not supported.\n"));
1890 } else {
1891 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1894 return ret;
1897 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1898 off_t len)
1900 int result;
1901 struct gpfs_config_data *config;
1903 SMB_VFS_HANDLE_GET_DATA(handle, config,
1904 struct gpfs_config_data,
1905 return -1);
1907 if (!config->ftruncate) {
1908 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1911 result = gpfswrap_ftruncate(fsp->fh->fd, len);
1912 if ((result == -1) && (errno == ENOSYS)) {
1913 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1915 return result;
1918 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1919 const struct smb_filename *fname,
1920 SMB_STRUCT_STAT *sbuf)
1922 struct gpfs_winattr attrs;
1923 char *path = NULL;
1924 NTSTATUS status;
1925 struct gpfs_config_data *config;
1926 int ret;
1928 SMB_VFS_HANDLE_GET_DATA(handle, config,
1929 struct gpfs_config_data,
1930 return -1);
1932 if (!config->winattr) {
1933 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1936 status = get_full_smb_filename(talloc_tos(), fname, &path);
1937 if (!NT_STATUS_IS_OK(status)) {
1938 errno = map_errno_from_nt_status(status);
1939 return -1;
1942 ret = gpfswrap_get_winattrs_path(path, &attrs);
1943 if (ret == -1) {
1944 TALLOC_FREE(path);
1945 return false;
1948 if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1949 DEBUG(10, ("%s is offline\n", path));
1950 TALLOC_FREE(path);
1951 return true;
1953 DEBUG(10, ("%s is online\n", path));
1954 TALLOC_FREE(path);
1955 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1958 static bool vfs_gpfs_fsp_is_offline(struct vfs_handle_struct *handle,
1959 struct files_struct *fsp)
1961 return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1964 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1965 struct files_struct *fsp)
1967 return vfs_gpfs_fsp_is_offline(handle, fsp);
1970 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1971 files_struct *fsp, const DATA_BLOB *hdr,
1972 off_t offset, size_t n)
1974 if (vfs_gpfs_fsp_is_offline(handle, fsp)) {
1975 errno = ENOSYS;
1976 return -1;
1978 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1981 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1982 const char *service, const char *user)
1984 struct gpfs_config_data *config;
1985 int ret;
1987 gpfswrap_lib_init(0);
1989 config = talloc_zero(handle->conn, struct gpfs_config_data);
1990 if (!config) {
1991 DEBUG(0, ("talloc_zero() failed\n"));
1992 errno = ENOMEM;
1993 return -1;
1996 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1997 if (ret < 0) {
1998 TALLOC_FREE(config);
1999 return ret;
2002 config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2003 "sharemodes", true);
2005 config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
2006 "leases", true);
2008 config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
2009 "hsm", false);
2011 config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
2012 "syncio", false);
2014 config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
2015 "winattr", false);
2017 config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
2018 "ftruncate", true);
2020 config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
2021 "getrealfilename", true);
2023 config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
2024 "dfreequota", false);
2026 config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
2027 "prealloc", true);
2029 config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
2031 config->settimes = lp_parm_bool(SNUM(handle->conn), "gpfs",
2032 "settimes", true);
2033 config->recalls = lp_parm_bool(SNUM(handle->conn), "gpfs",
2034 "recalls", true);
2036 SMB_VFS_HANDLE_SET_DATA(handle, config,
2037 NULL, struct gpfs_config_data,
2038 return -1);
2040 if (config->leases) {
2042 * GPFS lease code is based on kernel oplock code
2043 * so make sure it is turned on
2045 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
2046 DEBUG(5, ("Enabling kernel oplocks for "
2047 "gpfs:leases to work\n"));
2048 lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
2049 "true");
2053 * as the kernel does not properly support Level II oplocks
2054 * and GPFS leases code is based on kernel infrastructure, we
2055 * need to turn off Level II oplocks if gpfs:leases is enabled
2057 if (lp_level2_oplocks(SNUM(handle->conn))) {
2058 DEBUG(5, ("gpfs:leases are enabled, disabling "
2059 "Level II oplocks\n"));
2060 lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
2061 "false");
2065 return 0;
2068 static int get_gpfs_quota(const char *pathname, int type, int id,
2069 struct gpfs_quotaInfo *qi)
2071 int ret;
2073 ret = gpfswrap_quotactl(discard_const_p(char, pathname),
2074 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
2076 if (ret) {
2077 if (errno == GPFS_E_NO_QUOTA_INST) {
2078 DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
2079 } else if (errno != ENOSYS) {
2080 DEBUG(0, ("Get quota failed, type %d, id, %d, "
2081 "errno %d.\n", type, id, errno));
2084 return ret;
2087 DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
2088 type, id, qi->blockUsage, qi->blockHardLimit,
2089 qi->blockSoftLimit, qi->blockGraceTime));
2091 return ret;
2094 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
2095 uint64_t *dfree, uint64_t *dsize)
2097 uint64_t usage, limit;
2100 * The quota reporting is done in units of 1024 byte blocks, but
2101 * sys_fsusage uses units of 512 byte blocks, adjust the block number
2102 * accordingly. Also filter possibly negative usage counts from gpfs.
2104 usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
2105 limit = (uint64_t)qi.blockHardLimit * 2;
2108 * When the grace time for the exceeded soft block quota has been
2109 * exceeded, the soft block quota becomes an additional hard limit.
2111 if (qi.blockSoftLimit &&
2112 qi.blockGraceTime && cur_time > qi.blockGraceTime) {
2113 /* report disk as full */
2114 *dfree = 0;
2115 *dsize = MIN(*dsize, usage);
2118 if (!qi.blockHardLimit)
2119 return;
2121 if (usage >= limit) {
2122 /* report disk as full */
2123 *dfree = 0;
2124 *dsize = MIN(*dsize, usage);
2126 } else {
2127 /* limit has not been reached, determine "free space" */
2128 *dfree = MIN(*dfree, limit - usage);
2129 *dsize = MIN(*dsize, limit);
2133 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
2134 uint64_t *bsize,
2135 uint64_t *dfree, uint64_t *dsize)
2137 struct security_unix_token *utok;
2138 struct gpfs_quotaInfo qi_user = { 0 }, qi_group = { 0 };
2139 struct gpfs_config_data *config;
2140 int err;
2141 time_t cur_time;
2143 SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
2144 return (uint64_t)-1);
2145 if (!config->dfreequota) {
2146 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2147 bsize, dfree, dsize);
2150 err = sys_fsusage(path, dfree, dsize);
2151 if (err) {
2152 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
2153 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2154 bsize, dfree, dsize);
2157 /* sys_fsusage returns units of 512 bytes */
2158 *bsize = 512;
2160 DEBUG(10, ("fs dfree %llu, dsize %llu\n",
2161 (unsigned long long)*dfree, (unsigned long long)*dsize));
2163 utok = handle->conn->session_info->unix_token;
2165 err = get_gpfs_quota(path, GPFS_USRQUOTA, utok->uid, &qi_user);
2166 if (err) {
2167 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2168 bsize, dfree, dsize);
2171 err = get_gpfs_quota(path, GPFS_GRPQUOTA, utok->gid, &qi_group);
2172 if (err) {
2173 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2174 bsize, dfree, dsize);
2177 cur_time = time(NULL);
2179 /* Adjust free space and size according to quota limits. */
2180 vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
2181 vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
2183 disk_norm(bsize, dfree, dsize);
2184 return *dfree;
2187 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
2188 enum timestamp_set_resolution *p_ts_res)
2190 struct gpfs_config_data *config;
2191 uint32_t next;
2193 next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
2195 SMB_VFS_HANDLE_GET_DATA(handle, config,
2196 struct gpfs_config_data,
2197 return next);
2199 if (config->hsm) {
2200 next |= FILE_SUPPORTS_REMOTE_STORAGE;
2202 return next;
2205 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
2206 struct smb_filename *smb_fname, files_struct *fsp,
2207 int flags, mode_t mode)
2209 struct gpfs_config_data *config;
2211 SMB_VFS_HANDLE_GET_DATA(handle, config,
2212 struct gpfs_config_data,
2213 return -1);
2215 if (config->hsm && !config->recalls &&
2216 vfs_gpfs_fsp_is_offline(handle, fsp)) {
2217 DEBUG(10, ("Refusing access to offline file %s\n",
2218 fsp_str_dbg(fsp)));
2219 errno = EACCES;
2220 return -1;
2223 if (config->syncio) {
2224 flags |= O_SYNC;
2226 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2229 static ssize_t vfs_gpfs_pread(vfs_handle_struct *handle, files_struct *fsp,
2230 void *data, size_t n, off_t offset)
2232 ssize_t ret;
2233 bool was_offline;
2235 was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2237 ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2239 if ((ret != -1) && was_offline) {
2240 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2241 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2242 fsp->fsp_name->base_name);
2245 return ret;
2248 struct vfs_gpfs_pread_state {
2249 struct files_struct *fsp;
2250 ssize_t ret;
2251 int err;
2252 bool was_offline;
2255 static void vfs_gpfs_pread_done(struct tevent_req *subreq);
2257 static struct tevent_req *vfs_gpfs_pread_send(struct vfs_handle_struct *handle,
2258 TALLOC_CTX *mem_ctx,
2259 struct tevent_context *ev,
2260 struct files_struct *fsp,
2261 void *data, size_t n,
2262 off_t offset)
2264 struct tevent_req *req, *subreq;
2265 struct vfs_gpfs_pread_state *state;
2267 req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
2268 if (req == NULL) {
2269 return NULL;
2271 state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2272 state->fsp = fsp;
2273 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
2274 n, offset);
2275 if (tevent_req_nomem(subreq, req)) {
2276 return tevent_req_post(req, ev);
2278 tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
2279 return req;
2282 static void vfs_gpfs_pread_done(struct tevent_req *subreq)
2284 struct tevent_req *req = tevent_req_callback_data(
2285 subreq, struct tevent_req);
2286 struct vfs_gpfs_pread_state *state = tevent_req_data(
2287 req, struct vfs_gpfs_pread_state);
2289 state->ret = SMB_VFS_PREAD_RECV(subreq, &state->err);
2290 TALLOC_FREE(subreq);
2291 tevent_req_done(req);
2294 static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req, int *err)
2296 struct vfs_gpfs_pread_state *state = tevent_req_data(
2297 req, struct vfs_gpfs_pread_state);
2298 struct files_struct *fsp = state->fsp;
2300 if (tevent_req_is_unix_error(req, err)) {
2301 return -1;
2303 *err = state->err;
2305 if ((state->ret != -1) && state->was_offline) {
2306 DEBUG(10, ("sending notify\n"));
2307 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2308 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2309 fsp->fsp_name->base_name);
2312 return state->ret;
2315 static ssize_t vfs_gpfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
2316 const void *data, size_t n, off_t offset)
2318 ssize_t ret;
2319 bool was_offline;
2321 was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2323 ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2325 if ((ret != -1) && was_offline) {
2326 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2327 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2328 fsp->fsp_name->base_name);
2331 return ret;
2334 struct vfs_gpfs_pwrite_state {
2335 struct files_struct *fsp;
2336 ssize_t ret;
2337 int err;
2338 bool was_offline;
2341 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq);
2343 static struct tevent_req *vfs_gpfs_pwrite_send(
2344 struct vfs_handle_struct *handle,
2345 TALLOC_CTX *mem_ctx,
2346 struct tevent_context *ev,
2347 struct files_struct *fsp,
2348 const void *data, size_t n,
2349 off_t offset)
2351 struct tevent_req *req, *subreq;
2352 struct vfs_gpfs_pwrite_state *state;
2354 req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
2355 if (req == NULL) {
2356 return NULL;
2358 state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2359 state->fsp = fsp;
2360 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
2361 n, offset);
2362 if (tevent_req_nomem(subreq, req)) {
2363 return tevent_req_post(req, ev);
2365 tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
2366 return req;
2369 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
2371 struct tevent_req *req = tevent_req_callback_data(
2372 subreq, struct tevent_req);
2373 struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2374 req, struct vfs_gpfs_pwrite_state);
2376 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->err);
2377 TALLOC_FREE(subreq);
2378 tevent_req_done(req);
2381 static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req, int *err)
2383 struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2384 req, struct vfs_gpfs_pwrite_state);
2385 struct files_struct *fsp = state->fsp;
2387 if (tevent_req_is_unix_error(req, err)) {
2388 return -1;
2390 *err = state->err;
2392 if ((state->ret != -1) && state->was_offline) {
2393 DEBUG(10, ("sending notify\n"));
2394 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2395 FILE_NOTIFY_CHANGE_ATTRIBUTES,
2396 fsp->fsp_name->base_name);
2399 return state->ret;
2403 static struct vfs_fn_pointers vfs_gpfs_fns = {
2404 .connect_fn = vfs_gpfs_connect,
2405 .disk_free_fn = vfs_gpfs_disk_free,
2406 .fs_capabilities_fn = vfs_gpfs_capabilities,
2407 .kernel_flock_fn = vfs_gpfs_kernel_flock,
2408 .linux_setlease_fn = vfs_gpfs_setlease,
2409 .get_real_filename_fn = vfs_gpfs_get_real_filename,
2410 .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
2411 .get_nt_acl_fn = gpfsacl_get_nt_acl,
2412 .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
2413 .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
2414 .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
2415 .sys_acl_blob_get_file_fn = gpfsacl_sys_acl_blob_get_file,
2416 .sys_acl_blob_get_fd_fn = gpfsacl_sys_acl_blob_get_fd,
2417 .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
2418 .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
2419 .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
2420 .chmod_fn = vfs_gpfs_chmod,
2421 .fchmod_fn = vfs_gpfs_fchmod,
2422 .close_fn = vfs_gpfs_close,
2423 .setxattr_fn = gpfs_set_xattr,
2424 .getxattr_fn = gpfs_get_xattr,
2425 .stat_fn = vfs_gpfs_stat,
2426 .fstat_fn = vfs_gpfs_fstat,
2427 .lstat_fn = vfs_gpfs_lstat,
2428 .ntimes_fn = vfs_gpfs_ntimes,
2429 .is_offline_fn = vfs_gpfs_is_offline,
2430 .aio_force_fn = vfs_gpfs_aio_force,
2431 .sendfile_fn = vfs_gpfs_sendfile,
2432 .fallocate_fn = vfs_gpfs_fallocate,
2433 .open_fn = vfs_gpfs_open,
2434 .pread_fn = vfs_gpfs_pread,
2435 .pread_send_fn = vfs_gpfs_pread_send,
2436 .pread_recv_fn = vfs_gpfs_pread_recv,
2437 .pwrite_fn = vfs_gpfs_pwrite,
2438 .pwrite_send_fn = vfs_gpfs_pwrite_send,
2439 .pwrite_recv_fn = vfs_gpfs_pwrite_recv,
2440 .ftruncate_fn = vfs_gpfs_ftruncate
2443 NTSTATUS vfs_gpfs_init(void);
2444 NTSTATUS vfs_gpfs_init(void)
2446 int ret;
2448 ret = gpfswrap_init();
2449 if (ret != 0) {
2450 DEBUG(1, ("Could not initialize GPFS library wrapper\n"));
2453 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
2454 &vfs_gpfs_fns);