VERSION: Disable git snapshots for the 4.0.24 release.
[Samba.git] / source3 / modules / vfs_gpfs.c
blob90c4375b87cbc698a9e2969a7aa9b92f5472bdf2
1 /*
2 Unix SMB/CIFS implementation.
3 Wrap gpfs calls in vfs functions.
5 Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
7 Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
8 and Gomati Mohanan <gomati.mohanan@in.ibm.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
27 #include "include/smbprofile.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 #include <gpfs_gpl.h>
33 #include "nfs4_acls.h"
34 #include "vfs_gpfs.h"
35 #include "system/filesys.h"
36 #include "auth.h"
37 #include "lib/util/tevent_unix.h"
39 struct gpfs_config_data {
40 bool sharemodes;
41 bool leases;
42 bool hsm;
43 bool syncio;
44 bool winattr;
45 bool ftruncate;
46 bool getrealfilename;
47 bool dfreequota;
48 bool prealloc;
49 bool acl;
53 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
54 uint32 share_mode, uint32 access_mask)
57 struct gpfs_config_data *config;
58 int ret = 0;
60 SMB_VFS_HANDLE_GET_DATA(handle, config,
61 struct gpfs_config_data,
62 return -1);
64 START_PROFILE(syscall_kernel_flock);
66 kernel_flock(fsp->fh->fd, share_mode, access_mask);
68 if (config->sharemodes
69 && !set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
70 ret = -1;
73 END_PROFILE(syscall_kernel_flock);
75 return ret;
78 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
81 struct gpfs_config_data *config;
83 SMB_VFS_HANDLE_GET_DATA(handle, config,
84 struct gpfs_config_data,
85 return -1);
87 if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
88 set_gpfs_sharemode(fsp, 0, 0);
91 return SMB_VFS_NEXT_CLOSE(handle, fsp);
94 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
95 int leasetype)
97 struct gpfs_config_data *config;
98 int ret=0;
100 SMB_VFS_HANDLE_GET_DATA(handle, config,
101 struct gpfs_config_data,
102 return -1);
104 if (linux_set_lease_sighandler(fsp->fh->fd) == -1)
105 return -1;
107 START_PROFILE(syscall_linux_setlease);
109 if (config->leases) {
111 * Ensure the lease owner is root to allow
112 * correct delivery of lease-break signals.
114 become_root();
115 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
116 unbecome_root();
119 END_PROFILE(syscall_linux_setlease);
121 return ret;
124 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
125 const char *path,
126 const char *name,
127 TALLOC_CTX *mem_ctx,
128 char **found_name)
130 int result;
131 char *full_path;
132 char real_pathname[PATH_MAX+1];
133 int buflen;
134 bool mangled;
135 struct gpfs_config_data *config;
137 SMB_VFS_HANDLE_GET_DATA(handle, config,
138 struct gpfs_config_data,
139 return -1);
141 if (!config->getrealfilename) {
142 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
143 mem_ctx, found_name);
146 mangled = mangle_is_mangled(name, handle->conn->params);
147 if (mangled) {
148 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
149 mem_ctx, found_name);
152 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
153 if (full_path == NULL) {
154 errno = ENOMEM;
155 return -1;
158 buflen = sizeof(real_pathname) - 1;
160 result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
161 &buflen);
163 TALLOC_FREE(full_path);
165 if ((result == -1) && (errno == ENOSYS)) {
166 return SMB_VFS_NEXT_GET_REAL_FILENAME(
167 handle, path, name, mem_ctx, found_name);
170 if (result == -1) {
171 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
172 strerror(errno)));
173 return -1;
177 * GPFS does not necessarily null-terminate the returned path
178 * but instead returns the buffer length in buflen.
181 if (buflen < sizeof(real_pathname)) {
182 real_pathname[buflen] = '\0';
183 } else {
184 real_pathname[sizeof(real_pathname)-1] = '\0';
187 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
188 path, name, real_pathname));
190 name = strrchr_m(real_pathname, '/');
191 if (name == NULL) {
192 errno = ENOENT;
193 return -1;
196 *found_name = talloc_strdup(mem_ctx, name+1);
197 if (*found_name == NULL) {
198 errno = ENOMEM;
199 return -1;
202 return 0;
205 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
207 gpfs_aclCount_t i;
208 if (gacl==NULL)
210 DEBUG(0, ("gpfs acl is NULL\n"));
211 return;
214 DEBUG(level, ("gpfs acl: nace: %d, type:%d, version:%d, level:%d, len:%d\n",
215 gacl->acl_nace, gacl->acl_type, gacl->acl_version, gacl->acl_level, gacl->acl_len));
216 for(i=0; i<gacl->acl_nace; i++)
218 struct gpfs_ace_v4 *gace = gacl->ace_v4 + i;
219 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, iflags:0x%x, who:%u\n",
220 i, gace->aceType, gace->aceFlags, gace->aceMask,
221 gace->aceIFlags, gace->aceWho));
225 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
227 struct gpfs_acl *acl;
228 size_t len = 200;
229 int ret;
230 TALLOC_CTX *mem_ctx = talloc_tos();
232 acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
233 if (acl == NULL) {
234 errno = ENOMEM;
235 return NULL;
238 acl->acl_len = len;
239 acl->acl_level = 0;
240 acl->acl_version = 0;
241 acl->acl_type = type;
243 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
244 if ((ret != 0) && (errno == ENOSPC)) {
245 struct gpfs_acl *new_acl = (struct gpfs_acl *)TALLOC_SIZE(
246 mem_ctx, acl->acl_len + sizeof(struct gpfs_acl));
247 if (new_acl == NULL) {
248 errno = ENOMEM;
249 return NULL;
252 new_acl->acl_len = acl->acl_len;
253 new_acl->acl_level = acl->acl_level;
254 new_acl->acl_version = acl->acl_version;
255 new_acl->acl_type = acl->acl_type;
256 acl = new_acl;
258 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
260 if (ret != 0)
262 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
263 return NULL;
266 return acl;
269 /* Tries to get nfs4 acls and returns SMB ACL allocated.
270 * On failure returns 1 if it got non-NFSv4 ACL to prompt
271 * retry with POSIX ACL checks.
272 * On failure returns -1 if there is system (GPFS) error, check errno.
273 * Returns 0 on success
275 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
277 gpfs_aclCount_t i;
278 struct gpfs_acl *gacl = NULL;
279 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
281 /* First get the real acl length */
282 gacl = gpfs_getacl_alloc(fname, 0);
283 if (gacl == NULL) {
284 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
285 fname, strerror(errno)));
286 return -1;
289 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
290 DEBUG(10, ("Got non-nfsv4 acl\n"));
291 /* Retry with POSIX ACLs check */
292 talloc_free(gacl);
293 return 1;
296 *ppacl = smb_create_smb4acl();
298 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
299 gacl->acl_len, gacl->acl_level, gacl->acl_version,
300 gacl->acl_nace));
302 for (i=0; i<gacl->acl_nace; i++) {
303 struct gpfs_ace_v4 *gace = &gacl->ace_v4[i];
304 SMB_ACE4PROP_T smbace;
305 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
306 "who: %d\n", gace->aceType, gace->aceIFlags,
307 gace->aceFlags, gace->aceMask, gace->aceWho));
309 ZERO_STRUCT(smbace);
310 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
311 smbace.flags |= SMB_ACE4_ID_SPECIAL;
312 switch (gace->aceWho) {
313 case ACE4_SPECIAL_OWNER:
314 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
315 break;
316 case ACE4_SPECIAL_GROUP:
317 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
318 break;
319 case ACE4_SPECIAL_EVERYONE:
320 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
321 break;
322 default:
323 DEBUG(8, ("invalid special gpfs id %d "
324 "ignored\n", gace->aceWho));
325 continue; /* don't add it */
327 } else {
328 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
329 smbace.who.gid = gace->aceWho;
330 else
331 smbace.who.uid = gace->aceWho;
334 /* remove redundent deny entries */
335 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
336 struct gpfs_ace_v4 *prev = &gacl->ace_v4[i-1];
337 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
338 prev->aceFlags == gace->aceFlags &&
339 prev->aceIFlags == gace->aceIFlags &&
340 (gace->aceMask & prev->aceMask) == 0 &&
341 gace->aceWho == prev->aceWho) {
342 /* its redundent - skip it */
343 continue;
347 smbace.aceType = gace->aceType;
348 smbace.aceFlags = gace->aceFlags;
349 smbace.aceMask = gace->aceMask;
350 smb_add_ace4(*ppacl, &smbace);
353 return 0;
356 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
357 files_struct *fsp, uint32 security_info,
358 TALLOC_CTX *mem_ctx,
359 struct security_descriptor **ppdesc)
361 SMB4ACL_T *pacl = NULL;
362 int result;
363 struct gpfs_config_data *config;
365 *ppdesc = NULL;
367 SMB_VFS_HANDLE_GET_DATA(handle, config,
368 struct gpfs_config_data,
369 return NT_STATUS_INTERNAL_ERROR);
371 if (!config->acl) {
372 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
373 mem_ctx, ppdesc);
376 result = gpfs_get_nfs4_acl(fsp->fsp_name->base_name, &pacl);
378 if (result == 0)
379 return smb_fget_nt_acl_nfs4(fsp, security_info, mem_ctx, ppdesc, pacl);
381 if (result > 0) {
382 DEBUG(10, ("retrying with posix acl...\n"));
383 return posix_fget_nt_acl(fsp, security_info, mem_ctx, ppdesc);
386 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
387 return map_nt_error_from_unix(errno);
390 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
391 const char *name,
392 uint32 security_info,
393 TALLOC_CTX *mem_ctx, struct security_descriptor **ppdesc)
395 SMB4ACL_T *pacl = NULL;
396 int result;
397 struct gpfs_config_data *config;
399 *ppdesc = NULL;
401 SMB_VFS_HANDLE_GET_DATA(handle, config,
402 struct gpfs_config_data,
403 return NT_STATUS_INTERNAL_ERROR);
405 if (!config->acl) {
406 return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info,
407 mem_ctx, ppdesc);
410 result = gpfs_get_nfs4_acl(name, &pacl);
412 if (result == 0)
413 return smb_get_nt_acl_nfs4(handle->conn, name, security_info,
414 mem_ctx, ppdesc, pacl);
416 if (result > 0) {
417 DEBUG(10, ("retrying with posix acl...\n"));
418 return posix_get_nt_acl(handle->conn, name, security_info,
419 mem_ctx, ppdesc);
422 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
423 return map_nt_error_from_unix(errno);
426 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
428 int ret;
429 gpfs_aclLen_t gacl_len;
430 SMB4ACE_T *smbace;
431 struct gpfs_acl *gacl;
432 TALLOC_CTX *mem_ctx = talloc_tos();
434 gacl_len = offsetof(gpfs_acl_t, ace_v4) + smb_get_naces(smbacl) *
435 sizeof(gpfs_ace_v4_t);
437 gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
438 if (gacl == NULL) {
439 DEBUG(0, ("talloc failed\n"));
440 errno = ENOMEM;
441 return False;
444 gacl->acl_len = gacl_len;
445 gacl->acl_level = 0;
446 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
447 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
448 gacl->acl_nace = 0; /* change later... */
450 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
451 struct gpfs_ace_v4 *gace = &gacl->ace_v4[gacl->acl_nace];
452 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
454 gace->aceType = aceprop->aceType;
455 gace->aceFlags = aceprop->aceFlags;
456 gace->aceMask = aceprop->aceMask;
459 * GPFS can't distinguish between WRITE and APPEND on
460 * files, so one being set without the other is an
461 * error. Sorry for the many ()'s :-)
464 if (!fsp->is_directory
466 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
467 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
469 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
470 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
472 lp_parm_bool(fsp->conn->params->service, "gpfs",
473 "merge_writeappend", True)) {
474 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
475 "WRITE^APPEND, setting WRITE|APPEND\n",
476 fsp_str_dbg(fsp)));
477 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
480 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
482 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
484 switch(aceprop->who.special_id)
486 case SMB_ACE4_WHO_EVERYONE:
487 gace->aceWho = ACE4_SPECIAL_EVERYONE;
488 break;
489 case SMB_ACE4_WHO_OWNER:
490 gace->aceWho = ACE4_SPECIAL_OWNER;
491 break;
492 case SMB_ACE4_WHO_GROUP:
493 gace->aceWho = ACE4_SPECIAL_GROUP;
494 break;
495 default:
496 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
497 continue; /* don't add it !!! */
499 } else {
500 /* just only for the type safety... */
501 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
502 gace->aceWho = aceprop->who.gid;
503 else
504 gace->aceWho = aceprop->who.uid;
507 gacl->acl_nace++;
510 ret = smbd_gpfs_putacl(fsp->fsp_name->base_name,
511 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
512 if (ret != 0) {
513 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
514 gpfs_dumpacl(8, gacl);
515 return False;
518 DEBUG(10, ("gpfs_putacl succeeded\n"));
519 return True;
522 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
524 struct gpfs_acl *acl;
525 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
527 acl = gpfs_getacl_alloc(fsp->fsp_name->base_name, 0);
528 if (acl == NULL)
529 return result;
531 if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
533 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
534 "refuse_dacl_protected", false)
535 && (psd->type&SEC_DESC_DACL_PROTECTED)) {
536 DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
537 return NT_STATUS_NOT_SUPPORTED;
540 result = smb_set_nt_acl_nfs4(
541 fsp, security_info_sent, psd,
542 gpfsacl_process_smbacl);
543 } else { /* assume POSIX ACL - by default... */
544 result = set_nt_acl(fsp, security_info_sent, psd);
547 return result;
550 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
552 struct gpfs_config_data *config;
554 SMB_VFS_HANDLE_GET_DATA(handle, config,
555 struct gpfs_config_data,
556 return NT_STATUS_INTERNAL_ERROR);
558 if (!config->acl) {
559 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
562 return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
565 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl, TALLOC_CTX *mem_ctx)
567 SMB_ACL_T result;
568 gpfs_aclCount_t i;
570 result = sys_acl_init(mem_ctx);
571 if (result == NULL) {
572 errno = ENOMEM;
573 return NULL;
576 result->count = pacl->acl_nace;
577 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry,
578 result->count);
579 if (result->acl == NULL) {
580 TALLOC_FREE(result);
581 errno = ENOMEM;
582 return NULL;
585 for (i=0; i<pacl->acl_nace; i++) {
586 struct smb_acl_entry *ace = &result->acl[i];
587 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
589 DEBUG(10, ("Converting type %d id %lu perm %x\n",
590 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
591 (int)g_ace->ace_perm));
593 switch (g_ace->ace_type) {
594 case GPFS_ACL_USER:
595 ace->a_type = SMB_ACL_USER;
596 ace->info.user.uid = (uid_t)g_ace->ace_who;
597 break;
598 case GPFS_ACL_USER_OBJ:
599 ace->a_type = SMB_ACL_USER_OBJ;
600 break;
601 case GPFS_ACL_GROUP:
602 ace->a_type = SMB_ACL_GROUP;
603 ace->info.group.gid = (gid_t)g_ace->ace_who;
604 break;
605 case GPFS_ACL_GROUP_OBJ:
606 ace->a_type = SMB_ACL_GROUP_OBJ;
607 break;
608 case GPFS_ACL_OTHER:
609 ace->a_type = SMB_ACL_OTHER;
610 break;
611 case GPFS_ACL_MASK:
612 ace->a_type = SMB_ACL_MASK;
613 break;
614 default:
615 DEBUG(10, ("Got invalid ace_type: %d\n",
616 g_ace->ace_type));
617 TALLOC_FREE(result);
618 errno = EINVAL;
619 return NULL;
622 ace->a_perm = 0;
623 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
624 SMB_ACL_READ : 0;
625 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
626 SMB_ACL_WRITE : 0;
627 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
628 SMB_ACL_EXECUTE : 0;
630 DEBUGADD(10, ("Converted to %d perm %x\n",
631 ace->a_type, ace->a_perm));
634 return result;
637 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type,
638 TALLOC_CTX *mem_ctx)
640 struct gpfs_acl *pacl;
641 SMB_ACL_T result = NULL;
643 pacl = gpfs_getacl_alloc(path, type);
645 if (pacl == NULL) {
646 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
647 path, strerror(errno)));
648 if (errno == 0) {
649 errno = EINVAL;
651 goto done;
654 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
655 DEBUG(10, ("Got acl version %d, expected %d\n",
656 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
657 errno = EINVAL;
658 goto done;
661 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
662 pacl->acl_len, pacl->acl_level, pacl->acl_version,
663 pacl->acl_nace));
665 result = gpfs2smb_acl(pacl, mem_ctx);
666 if (result != NULL) {
667 errno = 0;
670 done:
672 if (pacl != NULL) {
673 talloc_free(pacl);
675 if (errno != 0) {
676 TALLOC_FREE(result);
678 return result;
681 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
682 const char *path_p,
683 SMB_ACL_TYPE_T type,
684 TALLOC_CTX *mem_ctx)
686 gpfs_aclType_t gpfs_type;
687 struct gpfs_config_data *config;
689 SMB_VFS_HANDLE_GET_DATA(handle, config,
690 struct gpfs_config_data,
691 return NULL);
693 if (!config->acl) {
694 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p,
695 type, mem_ctx);
698 switch(type) {
699 case SMB_ACL_TYPE_ACCESS:
700 gpfs_type = GPFS_ACL_TYPE_ACCESS;
701 break;
702 case SMB_ACL_TYPE_DEFAULT:
703 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
704 break;
705 default:
706 DEBUG(0, ("Got invalid type: %d\n", type));
707 smb_panic("exiting");
710 return gpfsacl_get_posix_acl(path_p, gpfs_type, mem_ctx);
713 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
714 files_struct *fsp,
715 TALLOC_CTX *mem_ctx)
717 struct gpfs_config_data *config;
719 SMB_VFS_HANDLE_GET_DATA(handle, config,
720 struct gpfs_config_data,
721 return NULL);
723 if (!config->acl) {
724 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
727 return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
728 GPFS_ACL_TYPE_ACCESS, mem_ctx);
731 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
732 SMB_ACL_TYPE_T type)
734 gpfs_aclLen_t len;
735 struct gpfs_acl *result;
736 int i;
738 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
740 len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
741 sizeof(gpfs_ace_v1_t);
743 result = (struct gpfs_acl *)SMB_MALLOC(len);
744 if (result == NULL) {
745 errno = ENOMEM;
746 return result;
749 result->acl_len = len;
750 result->acl_level = 0;
751 result->acl_version = GPFS_ACL_VERSION_POSIX;
752 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
753 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
754 result->acl_nace = pacl->count;
756 for (i=0; i<pacl->count; i++) {
757 const struct smb_acl_entry *ace = &pacl->acl[i];
758 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
760 DEBUG(10, ("Converting type %d perm %x\n",
761 (int)ace->a_type, (int)ace->a_perm));
763 g_ace->ace_perm = 0;
765 switch(ace->a_type) {
766 case SMB_ACL_USER:
767 g_ace->ace_type = GPFS_ACL_USER;
768 g_ace->ace_who = (gpfs_uid_t)ace->info.user.uid;
769 break;
770 case SMB_ACL_USER_OBJ:
771 g_ace->ace_type = GPFS_ACL_USER_OBJ;
772 g_ace->ace_perm |= ACL_PERM_CONTROL;
773 g_ace->ace_who = 0;
774 break;
775 case SMB_ACL_GROUP:
776 g_ace->ace_type = GPFS_ACL_GROUP;
777 g_ace->ace_who = (gpfs_uid_t)ace->info.group.gid;
778 break;
779 case SMB_ACL_GROUP_OBJ:
780 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
781 g_ace->ace_who = 0;
782 break;
783 case SMB_ACL_MASK:
784 g_ace->ace_type = GPFS_ACL_MASK;
785 g_ace->ace_perm = 0x8f;
786 g_ace->ace_who = 0;
787 break;
788 case SMB_ACL_OTHER:
789 g_ace->ace_type = GPFS_ACL_OTHER;
790 g_ace->ace_who = 0;
791 break;
792 default:
793 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
794 errno = EINVAL;
795 SAFE_FREE(result);
796 return NULL;
799 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
800 ACL_PERM_READ : 0;
801 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
802 ACL_PERM_WRITE : 0;
803 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
804 ACL_PERM_EXECUTE : 0;
806 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
807 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
810 return result;
813 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
814 const char *name,
815 SMB_ACL_TYPE_T type,
816 SMB_ACL_T theacl)
818 struct gpfs_acl *gpfs_acl;
819 int result;
820 struct gpfs_config_data *config;
822 SMB_VFS_HANDLE_GET_DATA(handle, config,
823 struct gpfs_config_data,
824 return -1);
826 if (!config->acl) {
827 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, type, theacl);
830 gpfs_acl = smb2gpfs_acl(theacl, type);
831 if (gpfs_acl == NULL) {
832 return -1;
835 result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
837 SAFE_FREE(gpfs_acl);
838 return result;
841 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
842 files_struct *fsp,
843 SMB_ACL_T theacl)
845 struct gpfs_config_data *config;
847 SMB_VFS_HANDLE_GET_DATA(handle, config,
848 struct gpfs_config_data,
849 return -1);
851 if (!config->acl) {
852 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
855 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
856 SMB_ACL_TYPE_ACCESS, theacl);
859 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
860 const char *path)
862 struct gpfs_config_data *config;
864 SMB_VFS_HANDLE_GET_DATA(handle, config,
865 struct gpfs_config_data,
866 return -1);
868 if (!config->acl) {
869 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
872 errno = ENOTSUP;
873 return -1;
877 * Assumed: mode bits are shiftable and standard
878 * Output: the new aceMask field for an smb nfs4 ace
880 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
882 const uint32 posix_nfs4map[3] = {
883 SMB_ACE4_EXECUTE, /* execute */
884 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
885 SMB_ACE4_READ_DATA /* read */
887 int i;
888 uint32_t posix_mask = 0x01;
889 uint32_t posix_bit;
890 uint32_t nfs4_bits;
892 for(i=0; i<3; i++) {
893 nfs4_bits = posix_nfs4map[i];
894 posix_bit = rwx & posix_mask;
896 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
897 if (posix_bit)
898 aceMask |= nfs4_bits;
899 else
900 aceMask &= ~nfs4_bits;
901 } else {
902 /* add deny bits when suitable */
903 if (!posix_bit)
904 aceMask |= nfs4_bits;
905 else
906 aceMask &= ~nfs4_bits;
907 } /* other ace types are unexpected */
909 posix_mask <<= 1;
912 return aceMask;
915 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
917 SMB4ACL_T *pacl = NULL;
918 int result;
919 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
920 int i;
921 files_struct fake_fsp; /* TODO: rationalize parametrization */
922 SMB4ACE_T *smbace;
923 NTSTATUS status;
925 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
927 result = gpfs_get_nfs4_acl(path, &pacl);
928 if (result)
929 return result;
931 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
932 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
935 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
936 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
937 uint32_t specid = ace->who.special_id;
939 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
940 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
941 specid <= SMB_ACE4_WHO_EVERYONE) {
943 uint32_t newMask;
945 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
946 haveAllowEntry[specid] = True;
948 /* mode >> 6 for @owner, mode >> 3 for @group,
949 * mode >> 0 for @everyone */
950 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
951 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
952 if (ace->aceMask!=newMask) {
953 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
954 path, ace->aceMask, newMask, specid));
956 ace->aceMask = newMask;
960 /* make sure we have at least ALLOW entries
961 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
962 * - if necessary
964 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
965 SMB_ACE4PROP_T ace;
967 if (haveAllowEntry[i]==True)
968 continue;
970 ZERO_STRUCT(ace);
971 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
972 ace.flags |= SMB_ACE4_ID_SPECIAL;
973 ace.who.special_id = i;
975 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
976 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
978 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
979 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
981 /* don't add unnecessary aces */
982 if (!ace.aceMask)
983 continue;
985 /* we add it to the END - as windows expects allow aces */
986 smb_add_ace4(pacl, &ace);
987 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
988 path, mode, i, ace.aceMask));
991 /* don't add complementary DENY ACEs here */
992 ZERO_STRUCT(fake_fsp);
993 status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
994 &fake_fsp.fsp_name);
995 if (!NT_STATUS_IS_OK(status)) {
996 errno = map_errno_from_nt_status(status);
997 return -1;
999 /* put the acl */
1000 if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False) {
1001 TALLOC_FREE(fake_fsp.fsp_name);
1002 return -1;
1005 TALLOC_FREE(fake_fsp.fsp_name);
1006 return 0; /* ok for [f]chmod */
1009 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
1011 struct smb_filename *smb_fname_cpath;
1012 int rc;
1013 NTSTATUS status;
1015 status = create_synthetic_smb_fname(
1016 talloc_tos(), path, NULL, NULL, &smb_fname_cpath);
1018 if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
1019 return -1;
1022 /* avoid chmod() if possible, to preserve acls */
1023 if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
1024 return 0;
1027 rc = gpfsacl_emu_chmod(path, mode);
1028 if (rc == 1)
1029 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1030 return rc;
1033 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1035 SMB_STRUCT_STAT st;
1036 int rc;
1038 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1039 return -1;
1042 /* avoid chmod() if possible, to preserve acls */
1043 if ((st.st_ex_mode & ~S_IFMT) == mode) {
1044 return 0;
1047 rc = gpfsacl_emu_chmod(fsp->fsp_name->base_name, mode);
1048 if (rc == 1)
1049 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1050 return rc;
1053 static int gpfs_set_xattr(struct vfs_handle_struct *handle, const char *path,
1054 const char *name, const void *value, size_t size, int flags){
1055 struct xattr_DOSATTRIB dosattrib;
1056 enum ndr_err_code ndr_err;
1057 DATA_BLOB blob;
1058 const char *attrstr = value;
1059 unsigned int dosmode=0;
1060 struct gpfs_winattr attrs;
1061 int ret = 0;
1062 struct gpfs_config_data *config;
1064 SMB_VFS_HANDLE_GET_DATA(handle, config,
1065 struct gpfs_config_data,
1066 return -1);
1068 if (!config->winattr) {
1069 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
1070 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1073 DEBUG(10, ("gpfs_set_xattr: %s \n",path));
1075 /* Only handle DOS Attributes */
1076 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1077 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
1078 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
1081 blob.data = (uint8_t *)attrstr;
1082 blob.length = size;
1084 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
1085 (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
1087 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1088 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
1089 "from EA on file %s: Error = %s\n",
1090 path, ndr_errstr(ndr_err)));
1091 return false;
1094 if (dosattrib.version != 3) {
1095 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
1096 "%d\n", (int)dosattrib.version));
1097 return false;
1099 if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
1100 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
1101 "valid, ignoring\n"));
1102 return true;
1105 dosmode = dosattrib.info.info3.attrib;
1107 attrs.winAttrs = 0;
1108 /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1109 if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1110 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1112 if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1113 attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1115 if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1116 attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1118 if (dosmode & FILE_ATTRIBUTE_READONLY){
1119 attrs.winAttrs |= GPFS_WINATTR_READONLY;
1121 if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1122 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1126 ret = set_gpfs_winattrs(discard_const_p(char, path),
1127 GPFS_WINATTR_SET_ATTRS, &attrs);
1128 if ( ret == -1){
1129 if (errno == ENOSYS) {
1130 return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1131 size, flags);
1134 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1135 return -1;
1138 DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1139 return 0;
1142 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle, const char *path,
1143 const char *name, void *value, size_t size){
1144 char *attrstr = value;
1145 unsigned int dosmode = 0;
1146 struct gpfs_winattr attrs;
1147 int ret = 0;
1148 struct gpfs_config_data *config;
1150 SMB_VFS_HANDLE_GET_DATA(handle, config,
1151 struct gpfs_config_data,
1152 return -1);
1154 if (!config->winattr) {
1155 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1156 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1159 DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1161 /* Only handle DOS Attributes */
1162 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1163 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1164 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1167 ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1168 if ( ret == -1){
1169 if (errno == ENOSYS) {
1170 return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1171 size);
1174 DEBUG(1, ("gpfs_get_xattr: Get GPFS attributes failed: "
1175 "%d (%s)\n", ret, strerror(errno)));
1176 return -1;
1179 DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1181 /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1182 if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1183 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1185 if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1186 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1188 if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1189 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1191 if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1192 dosmode |= FILE_ATTRIBUTE_READONLY;
1194 if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1195 dosmode |= FILE_ATTRIBUTE_SPARSE;
1198 snprintf(attrstr, size, "0x%2.2x",
1199 (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1200 DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1201 return 4;
1204 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1205 struct smb_filename *smb_fname)
1207 struct gpfs_winattr attrs;
1208 char *fname = NULL;
1209 NTSTATUS status;
1210 int ret;
1211 struct gpfs_config_data *config;
1213 SMB_VFS_HANDLE_GET_DATA(handle, config,
1214 struct gpfs_config_data,
1215 return -1);
1217 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1218 if (ret == -1) {
1219 return -1;
1222 if (!config->winattr) {
1223 return 0;
1226 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 errno = map_errno_from_nt_status(status);
1229 return -1;
1231 ret = get_gpfs_winattrs(discard_const_p(char, fname), &attrs);
1232 TALLOC_FREE(fname);
1233 if (ret == 0) {
1234 smb_fname->st.st_ex_calculated_birthtime = false;
1235 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1236 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1237 smb_fname->st.vfs_private = attrs.winAttrs;
1239 return 0;
1242 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1243 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1245 struct gpfs_winattr attrs;
1246 int ret;
1247 struct gpfs_config_data *config;
1249 SMB_VFS_HANDLE_GET_DATA(handle, config,
1250 struct gpfs_config_data,
1251 return -1);
1253 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1254 if (ret == -1) {
1255 return -1;
1257 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1258 return 0;
1260 if (!config->winattr) {
1261 return 0;
1264 ret = smbd_fget_gpfs_winattrs(fsp->fh->fd, &attrs);
1265 if (ret == 0) {
1266 sbuf->st_ex_calculated_birthtime = false;
1267 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1268 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1270 return 0;
1273 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1274 struct smb_filename *smb_fname)
1276 struct gpfs_winattr attrs;
1277 char *path = NULL;
1278 NTSTATUS status;
1279 int ret;
1280 struct gpfs_config_data *config;
1282 SMB_VFS_HANDLE_GET_DATA(handle, config,
1283 struct gpfs_config_data,
1284 return -1);
1286 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1287 if (ret == -1) {
1288 return -1;
1290 if (!config->winattr) {
1291 return 0;
1294 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 errno = map_errno_from_nt_status(status);
1297 return -1;
1299 ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1300 TALLOC_FREE(path);
1301 if (ret == 0) {
1302 smb_fname->st.st_ex_calculated_birthtime = false;
1303 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1304 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1305 smb_fname->st.vfs_private = attrs.winAttrs;
1307 return 0;
1310 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1311 const struct smb_filename *smb_fname,
1312 struct smb_file_time *ft)
1315 struct gpfs_winattr attrs;
1316 int ret;
1317 char *path = NULL;
1318 NTSTATUS status;
1319 struct gpfs_config_data *config;
1321 SMB_VFS_HANDLE_GET_DATA(handle, config,
1322 struct gpfs_config_data,
1323 return -1);
1325 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1326 if(ret == -1){
1327 /* don't complain if access was denied */
1328 if (errno != EPERM && errno != EACCES) {
1329 DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1330 "%s", strerror(errno)));
1332 return -1;
1335 if(null_timespec(ft->create_time)){
1336 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1337 return 0;
1340 if (!config->winattr) {
1341 return 0;
1344 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1345 if (!NT_STATUS_IS_OK(status)) {
1346 errno = map_errno_from_nt_status(status);
1347 return -1;
1350 attrs.winAttrs = 0;
1351 attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1352 attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1354 ret = set_gpfs_winattrs(discard_const_p(char, path),
1355 GPFS_WINATTR_SET_CREATION_TIME, &attrs);
1356 if(ret == -1 && errno != ENOSYS){
1357 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1358 return -1;
1360 return 0;
1364 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1365 struct files_struct *fsp, enum vfs_fallocate_mode mode,
1366 off_t offset, off_t len)
1368 int ret;
1369 struct gpfs_config_data *config;
1371 SMB_VFS_HANDLE_GET_DATA(handle, config,
1372 struct gpfs_config_data,
1373 return -1);
1375 if (!config->prealloc) {
1376 /* you should better not run fallocate() on GPFS at all */
1377 errno = ENOTSUP;
1378 return -1;
1381 if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1382 DEBUG(10, ("Unsupported VFS_FALLOCATE_KEEP_SIZE\n"));
1383 errno = ENOTSUP;
1384 return -1;
1387 ret = smbd_gpfs_prealloc(fsp->fh->fd, offset, len);
1389 if (ret == -1 && errno != ENOSYS) {
1390 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1391 } else if (ret == -1 && errno == ENOSYS) {
1392 DEBUG(10, ("GPFS prealloc not supported.\n"));
1393 } else {
1394 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1397 return ret;
1400 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1401 off_t len)
1403 int result;
1404 struct gpfs_config_data *config;
1406 SMB_VFS_HANDLE_GET_DATA(handle, config,
1407 struct gpfs_config_data,
1408 return -1);
1410 if (!config->ftruncate) {
1411 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1414 result = smbd_gpfs_ftruncate(fsp->fh->fd, len);
1415 if ((result == -1) && (errno == ENOSYS)) {
1416 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1418 return result;
1421 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1422 const struct smb_filename *fname,
1423 SMB_STRUCT_STAT *sbuf)
1425 struct gpfs_winattr attrs;
1426 char *path = NULL;
1427 NTSTATUS status;
1428 struct gpfs_config_data *config;
1430 SMB_VFS_HANDLE_GET_DATA(handle, config,
1431 struct gpfs_config_data,
1432 return -1);
1434 if (!config->winattr) {
1435 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1438 status = get_full_smb_filename(talloc_tos(), fname, &path);
1439 if (!NT_STATUS_IS_OK(status)) {
1440 errno = map_errno_from_nt_status(status);
1441 return -1;
1444 if (VALID_STAT(*sbuf)) {
1445 attrs.winAttrs = sbuf->vfs_private;
1446 } else {
1447 int ret;
1448 ret = get_gpfs_winattrs(path, &attrs);
1450 if (ret == -1) {
1451 TALLOC_FREE(path);
1452 return false;
1455 if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1456 DEBUG(10, ("%s is offline\n", path));
1457 TALLOC_FREE(path);
1458 return true;
1460 DEBUG(10, ("%s is online\n", path));
1461 TALLOC_FREE(path);
1462 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1465 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1466 struct files_struct *fsp)
1468 return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1471 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1472 files_struct *fsp, const DATA_BLOB *hdr,
1473 off_t offset, size_t n)
1475 if ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0) {
1476 errno = ENOSYS;
1477 return -1;
1479 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1482 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1483 const char *service, const char *user)
1485 struct gpfs_config_data *config;
1486 int ret;
1488 smbd_gpfs_lib_init();
1490 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1492 if (ret < 0) {
1493 return ret;
1496 config = talloc_zero(handle->conn, struct gpfs_config_data);
1497 if (!config) {
1498 SMB_VFS_NEXT_DISCONNECT(handle);
1499 DEBUG(0, ("talloc_zero() failed\n"));
1500 return -1;
1503 config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1504 "sharemodes", true);
1506 config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1507 "leases", true);
1509 config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1510 "hsm", false);
1512 config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1513 "syncio", false);
1515 config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1516 "winattr", false);
1518 config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1519 "ftruncate", true);
1521 config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1522 "getrealfilename", true);
1524 config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1525 "dfreequota", false);
1527 config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
1528 "prealloc", true);
1530 config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
1532 SMB_VFS_HANDLE_SET_DATA(handle, config,
1533 NULL, struct gpfs_config_data,
1534 return -1);
1536 if (config->leases) {
1538 * GPFS lease code is based on kernel oplock code
1539 * so make sure it is turned on
1541 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
1542 DEBUG(5, ("Enabling kernel oplocks for "
1543 "gpfs:leases to work\n"));
1544 lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
1545 "true");
1549 * as the kernel does not properly support Level II oplocks
1550 * and GPFS leases code is based on kernel infrastructure, we
1551 * need to turn off Level II oplocks if gpfs:leases is enabled
1553 if (lp_level2_oplocks(SNUM(handle->conn))) {
1554 DEBUG(5, ("gpfs:leases are enabled, disabling "
1555 "Level II oplocks\n"));
1556 lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
1557 "false");
1561 return 0;
1564 static int vfs_gpfs_get_quotas(const char *path, uid_t uid, gid_t gid,
1565 int *fset_id,
1566 struct gpfs_quotaInfo *qi_user,
1567 struct gpfs_quotaInfo *qi_group,
1568 struct gpfs_quotaInfo *qi_fset)
1570 int err;
1571 char *dir_path;
1572 bool b;
1575 * We want to always use the directory to get the fileset id,
1576 * because files might have a share mode. We also do not want
1577 * to get the parent directory when there is already a
1578 * directory to avoid stepping in a different fileset. The
1579 * path passed here is currently either "." or a filename, so
1580 * this is ok. The proper solution would be having a way to
1581 * query the fileset id without opening the file.
1583 b = parent_dirname(talloc_tos(), path, &dir_path, NULL);
1584 if (!b) {
1585 errno = ENOMEM;
1586 return -1;
1589 DEBUG(10, ("path %s, directory %s\n", path, dir_path));
1591 err = get_gpfs_fset_id(dir_path, fset_id);
1592 if (err) {
1593 DEBUG(0, ("Get fset id failed path %s, dir %s, errno %d.\n",
1594 path, dir_path, errno));
1595 return err;
1598 err = get_gpfs_quota(path, GPFS_USRQUOTA, uid, qi_user);
1599 if (err) {
1600 return err;
1603 err = get_gpfs_quota(path, GPFS_GRPQUOTA, gid, qi_group);
1604 if (err) {
1605 return err;
1608 err = get_gpfs_quota(path, GPFS_FILESETQUOTA, *fset_id, qi_fset);
1609 if (err) {
1610 return err;
1613 return 0;
1616 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
1617 uint64_t *dfree, uint64_t *dsize)
1619 uint64_t usage, limit;
1622 * The quota reporting is done in units of 1024 byte blocks, but
1623 * sys_fsusage uses units of 512 byte blocks, adjust the block number
1624 * accordingly. Also filter possibly negative usage counts from gpfs.
1626 usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
1627 limit = (uint64_t)qi.blockHardLimit * 2;
1630 * When the grace time for the exceeded soft block quota has been
1631 * exceeded, the soft block quota becomes an additional hard limit.
1633 if (qi.blockSoftLimit &&
1634 qi.blockGraceTime && cur_time > qi.blockGraceTime) {
1635 /* report disk as full */
1636 *dfree = 0;
1637 *dsize = MIN(*dsize, usage);
1640 if (!qi.blockHardLimit)
1641 return;
1643 if (usage >= limit) {
1644 /* report disk as full */
1645 *dfree = 0;
1646 *dsize = MIN(*dsize, usage);
1648 } else {
1649 /* limit has not been reached, determine "free space" */
1650 *dfree = MIN(*dfree, limit - usage);
1651 *dsize = MIN(*dsize, limit);
1655 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
1656 bool small_query, uint64_t *bsize,
1657 uint64_t *dfree, uint64_t *dsize)
1659 struct security_unix_token *utok;
1660 struct gpfs_quotaInfo qi_user, qi_group, qi_fset;
1661 struct gpfs_config_data *config;
1662 int err, fset_id;
1663 time_t cur_time;
1665 SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
1666 return (uint64_t)-1);
1667 if (!config->dfreequota) {
1668 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1669 bsize, dfree, dsize);
1672 err = sys_fsusage(path, dfree, dsize);
1673 if (err) {
1674 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
1675 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1676 bsize, dfree, dsize);
1679 /* sys_fsusage returns units of 512 bytes */
1680 *bsize = 512;
1682 DEBUG(10, ("fs dfree %llu, dsize %llu\n",
1683 (unsigned long long)*dfree, (unsigned long long)*dsize));
1685 utok = handle->conn->session_info->unix_token;
1686 err = vfs_gpfs_get_quotas(path, utok->uid, utok->gid, &fset_id,
1687 &qi_user, &qi_group, &qi_fset);
1688 if (err) {
1689 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1690 bsize, dfree, dsize);
1693 cur_time = time(NULL);
1695 /* Adjust free space and size according to quota limits. */
1696 vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
1697 vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
1699 /* Id 0 indicates the default quota, not an actual quota */
1700 if (fset_id != 0) {
1701 vfs_gpfs_disk_free_quota(qi_fset, cur_time, dfree, dsize);
1704 disk_norm(small_query, bsize, dfree, dsize);
1705 return *dfree;
1708 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
1709 enum timestamp_set_resolution *p_ts_res)
1711 struct gpfs_config_data *config;
1712 uint32_t next;
1714 next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
1716 SMB_VFS_HANDLE_GET_DATA(handle, config,
1717 struct gpfs_config_data,
1718 return next);
1720 if (config->hsm) {
1721 next |= FILE_SUPPORTS_REMOTE_STORAGE;
1723 return next;
1726 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
1727 struct smb_filename *smb_fname, files_struct *fsp,
1728 int flags, mode_t mode)
1730 struct gpfs_config_data *config;
1732 SMB_VFS_HANDLE_GET_DATA(handle, config,
1733 struct gpfs_config_data,
1734 return -1);
1736 if (config->syncio) {
1737 flags |= O_SYNC;
1739 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1742 static ssize_t vfs_gpfs_pread(vfs_handle_struct *handle, files_struct *fsp,
1743 void *data, size_t n, off_t offset)
1745 ssize_t ret;
1747 ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1749 DEBUG(10, ("vfs_private = %x\n",
1750 (unsigned int)fsp->fsp_name->st.vfs_private));
1752 if ((ret != -1) &&
1753 ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0)) {
1754 fsp->fsp_name->st.vfs_private &= ~GPFS_WINATTR_OFFLINE;
1755 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
1756 FILE_NOTIFY_CHANGE_ATTRIBUTES,
1757 fsp->fsp_name->base_name);
1760 return ret;
1763 struct vfs_gpfs_pread_state {
1764 struct files_struct *fsp;
1765 ssize_t ret;
1766 int err;
1769 static void vfs_gpfs_pread_done(struct tevent_req *subreq);
1771 static struct tevent_req *vfs_gpfs_pread_send(struct vfs_handle_struct *handle,
1772 TALLOC_CTX *mem_ctx,
1773 struct tevent_context *ev,
1774 struct files_struct *fsp,
1775 void *data, size_t n,
1776 off_t offset)
1778 struct tevent_req *req, *subreq;
1779 struct vfs_gpfs_pread_state *state;
1781 req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
1782 if (req == NULL) {
1783 return NULL;
1785 state->fsp = fsp;
1786 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
1787 n, offset);
1788 if (tevent_req_nomem(subreq, req)) {
1789 return tevent_req_post(req, ev);
1791 tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
1792 return req;
1795 static void vfs_gpfs_pread_done(struct tevent_req *subreq)
1797 struct tevent_req *req = tevent_req_callback_data(
1798 subreq, struct tevent_req);
1799 struct vfs_gpfs_pread_state *state = tevent_req_data(
1800 req, struct vfs_gpfs_pread_state);
1802 state->ret = SMB_VFS_PREAD_RECV(subreq, &state->err);
1803 TALLOC_FREE(subreq);
1804 tevent_req_done(req);
1807 static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req, int *err)
1809 struct vfs_gpfs_pread_state *state = tevent_req_data(
1810 req, struct vfs_gpfs_pread_state);
1811 struct files_struct *fsp = state->fsp;
1813 if (tevent_req_is_unix_error(req, err)) {
1814 return -1;
1816 *err = state->err;
1818 DEBUG(10, ("vfs_private = %x\n",
1819 (unsigned int)fsp->fsp_name->st.vfs_private));
1821 if ((state->ret != -1) &&
1822 ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0)) {
1823 fsp->fsp_name->st.vfs_private &= ~GPFS_WINATTR_OFFLINE;
1824 DEBUG(10, ("sending notify\n"));
1825 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
1826 FILE_NOTIFY_CHANGE_ATTRIBUTES,
1827 fsp->fsp_name->base_name);
1830 return state->ret;
1833 static ssize_t vfs_gpfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
1834 const void *data, size_t n, off_t offset)
1836 ssize_t ret;
1838 ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
1840 DEBUG(10, ("vfs_private = %x\n",
1841 (unsigned int)fsp->fsp_name->st.vfs_private));
1843 if ((ret != -1) &&
1844 ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0)) {
1845 fsp->fsp_name->st.vfs_private &= ~GPFS_WINATTR_OFFLINE;
1846 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
1847 FILE_NOTIFY_CHANGE_ATTRIBUTES,
1848 fsp->fsp_name->base_name);
1851 return ret;
1854 struct vfs_gpfs_pwrite_state {
1855 struct files_struct *fsp;
1856 ssize_t ret;
1857 int err;
1860 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq);
1862 static struct tevent_req *vfs_gpfs_pwrite_send(
1863 struct vfs_handle_struct *handle,
1864 TALLOC_CTX *mem_ctx,
1865 struct tevent_context *ev,
1866 struct files_struct *fsp,
1867 const void *data, size_t n,
1868 off_t offset)
1870 struct tevent_req *req, *subreq;
1871 struct vfs_gpfs_pwrite_state *state;
1873 req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
1874 if (req == NULL) {
1875 return NULL;
1877 state->fsp = fsp;
1878 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
1879 n, offset);
1880 if (tevent_req_nomem(subreq, req)) {
1881 return tevent_req_post(req, ev);
1883 tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
1884 return req;
1887 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
1889 struct tevent_req *req = tevent_req_callback_data(
1890 subreq, struct tevent_req);
1891 struct vfs_gpfs_pwrite_state *state = tevent_req_data(
1892 req, struct vfs_gpfs_pwrite_state);
1894 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->err);
1895 TALLOC_FREE(subreq);
1896 tevent_req_done(req);
1899 static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req, int *err)
1901 struct vfs_gpfs_pwrite_state *state = tevent_req_data(
1902 req, struct vfs_gpfs_pwrite_state);
1903 struct files_struct *fsp = state->fsp;
1905 if (tevent_req_is_unix_error(req, err)) {
1906 return -1;
1908 *err = state->err;
1910 DEBUG(10, ("vfs_private = %x\n",
1911 (unsigned int)fsp->fsp_name->st.vfs_private));
1913 if ((state->ret != -1) &&
1914 ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0)) {
1915 fsp->fsp_name->st.vfs_private &= ~GPFS_WINATTR_OFFLINE;
1916 DEBUG(10, ("sending notify\n"));
1917 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
1918 FILE_NOTIFY_CHANGE_ATTRIBUTES,
1919 fsp->fsp_name->base_name);
1922 return state->ret;
1926 static struct vfs_fn_pointers vfs_gpfs_fns = {
1927 .connect_fn = vfs_gpfs_connect,
1928 .disk_free_fn = vfs_gpfs_disk_free,
1929 .fs_capabilities_fn = vfs_gpfs_capabilities,
1930 .kernel_flock_fn = vfs_gpfs_kernel_flock,
1931 .linux_setlease_fn = vfs_gpfs_setlease,
1932 .get_real_filename_fn = vfs_gpfs_get_real_filename,
1933 .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
1934 .get_nt_acl_fn = gpfsacl_get_nt_acl,
1935 .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
1936 .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
1937 .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
1938 .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
1939 .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
1940 .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
1941 .chmod_fn = vfs_gpfs_chmod,
1942 .fchmod_fn = vfs_gpfs_fchmod,
1943 .close_fn = vfs_gpfs_close,
1944 .setxattr_fn = gpfs_set_xattr,
1945 .getxattr_fn = gpfs_get_xattr,
1946 .stat_fn = vfs_gpfs_stat,
1947 .fstat_fn = vfs_gpfs_fstat,
1948 .lstat_fn = vfs_gpfs_lstat,
1949 .ntimes_fn = vfs_gpfs_ntimes,
1950 .is_offline_fn = vfs_gpfs_is_offline,
1951 .aio_force_fn = vfs_gpfs_aio_force,
1952 .sendfile_fn = vfs_gpfs_sendfile,
1953 .fallocate_fn = vfs_gpfs_fallocate,
1954 .open_fn = vfs_gpfs_open,
1955 .pread_fn = vfs_gpfs_pread,
1956 .pread_send_fn = vfs_gpfs_pread_send,
1957 .pread_recv_fn = vfs_gpfs_pread_recv,
1958 .pwrite_fn = vfs_gpfs_pwrite,
1959 .pwrite_send_fn = vfs_gpfs_pwrite_send,
1960 .pwrite_recv_fn = vfs_gpfs_pwrite_recv,
1961 .ftruncate_fn = vfs_gpfs_ftruncate
1964 NTSTATUS vfs_gpfs_init(void);
1965 NTSTATUS vfs_gpfs_init(void)
1967 init_gpfs();
1969 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
1970 &vfs_gpfs_fns);