s3:vfs_gpfs: Implement fallocate callback for GPFS
[Samba/gebeck_regimport.git] / source3 / modules / vfs_gpfs.c
blob65cd33a394ebbee3438bb6ae9f4aaab7180bf4b1
1 /*
2 Unix SMB/CIFS implementation.
3 Wrap gpfs calls in vfs functions.
5 Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
7 Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
8 and Gomati Mohanan <gomati.mohanan@in.ibm.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
27 #include "include/smbprofile.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 #include <gpfs_gpl.h>
33 #include "nfs4_acls.h"
34 #include "vfs_gpfs.h"
35 #include "system/filesys.h"
36 #include "auth.h"
38 struct gpfs_config_data {
39 bool sharemodes;
40 bool leases;
41 bool hsm;
42 bool syncio;
43 bool winattr;
44 bool ftruncate;
45 bool getrealfilename;
46 bool dfreequota;
47 bool prealloc;
51 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
52 uint32 share_mode, uint32 access_mask)
55 struct gpfs_config_data *config;
57 SMB_VFS_HANDLE_GET_DATA(handle, config,
58 struct gpfs_config_data,
59 return -1);
61 START_PROFILE(syscall_kernel_flock);
63 kernel_flock(fsp->fh->fd, share_mode, access_mask);
65 if (config->sharemodes
66 && !set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
67 return -1;
70 END_PROFILE(syscall_kernel_flock);
72 return 0;
75 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
78 struct gpfs_config_data *config;
80 SMB_VFS_HANDLE_GET_DATA(handle, config,
81 struct gpfs_config_data,
82 return -1);
84 if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
85 set_gpfs_sharemode(fsp, 0, 0);
88 return SMB_VFS_NEXT_CLOSE(handle, fsp);
91 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
92 int leasetype)
94 struct gpfs_config_data *config;
95 int ret=0;
97 SMB_VFS_HANDLE_GET_DATA(handle, config,
98 struct gpfs_config_data,
99 return -1);
101 START_PROFILE(syscall_linux_setlease);
103 if (linux_set_lease_sighandler(fsp->fh->fd) == -1)
104 return -1;
106 if (config->leases) {
107 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
110 if (ret < 0) {
111 /* This must have come from GPFS not being available */
112 /* or some other error, hence call the default */
113 ret = linux_setlease(fsp->fh->fd, leasetype);
116 END_PROFILE(syscall_linux_setlease);
118 return ret;
121 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
122 const char *path,
123 const char *name,
124 TALLOC_CTX *mem_ctx,
125 char **found_name)
127 int result;
128 char *full_path;
129 char real_pathname[PATH_MAX+1];
130 int buflen;
131 bool mangled;
132 struct gpfs_config_data *config;
134 SMB_VFS_HANDLE_GET_DATA(handle, config,
135 struct gpfs_config_data,
136 return -1);
138 if (!config->getrealfilename) {
139 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
140 mem_ctx, found_name);
143 mangled = mangle_is_mangled(name, handle->conn->params);
144 if (mangled) {
145 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
146 mem_ctx, found_name);
149 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
150 if (full_path == NULL) {
151 errno = ENOMEM;
152 return -1;
155 buflen = sizeof(real_pathname) - 1;
157 result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
158 &buflen);
160 TALLOC_FREE(full_path);
162 if ((result == -1) && (errno == ENOSYS)) {
163 return SMB_VFS_NEXT_GET_REAL_FILENAME(
164 handle, path, name, mem_ctx, found_name);
167 if (result == -1) {
168 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
169 strerror(errno)));
170 return -1;
174 * GPFS does not necessarily null-terminate the returned path
175 * but instead returns the buffer length in buflen.
178 if (buflen < sizeof(real_pathname)) {
179 real_pathname[buflen] = '\0';
180 } else {
181 real_pathname[sizeof(real_pathname)-1] = '\0';
184 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
185 path, name, real_pathname));
187 name = strrchr_m(real_pathname, '/');
188 if (name == NULL) {
189 errno = ENOENT;
190 return -1;
193 *found_name = talloc_strdup(mem_ctx, name+1);
194 if (*found_name == NULL) {
195 errno = ENOMEM;
196 return -1;
199 return 0;
202 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
204 gpfs_aclCount_t i;
205 if (gacl==NULL)
207 DEBUG(0, ("gpfs acl is NULL\n"));
208 return;
211 DEBUG(level, ("gpfs acl: nace: %d, type:%d, version:%d, level:%d, len:%d\n",
212 gacl->acl_nace, gacl->acl_type, gacl->acl_version, gacl->acl_level, gacl->acl_len));
213 for(i=0; i<gacl->acl_nace; i++)
215 struct gpfs_ace_v4 *gace = gacl->ace_v4 + i;
216 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, iflags:0x%x, who:%u\n",
217 i, gace->aceType, gace->aceFlags, gace->aceMask,
218 gace->aceIFlags, gace->aceWho));
222 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
224 struct gpfs_acl *acl;
225 size_t len = 200;
226 int ret;
227 TALLOC_CTX *mem_ctx = talloc_tos();
229 acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
230 if (acl == NULL) {
231 errno = ENOMEM;
232 return NULL;
235 acl->acl_len = len;
236 acl->acl_level = 0;
237 acl->acl_version = 0;
238 acl->acl_type = type;
240 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
241 if ((ret != 0) && (errno == ENOSPC)) {
242 struct gpfs_acl *new_acl = (struct gpfs_acl *)TALLOC_SIZE(
243 mem_ctx, acl->acl_len + sizeof(struct gpfs_acl));
244 if (new_acl == NULL) {
245 errno = ENOMEM;
246 return NULL;
249 new_acl->acl_len = acl->acl_len;
250 new_acl->acl_level = acl->acl_level;
251 new_acl->acl_version = acl->acl_version;
252 new_acl->acl_type = acl->acl_type;
253 acl = new_acl;
255 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
257 if (ret != 0)
259 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
260 return NULL;
263 return acl;
266 /* Tries to get nfs4 acls and returns SMB ACL allocated.
267 * On failure returns 1 if it got non-NFSv4 ACL to prompt
268 * retry with POSIX ACL checks.
269 * On failure returns -1 if there is system (GPFS) error, check errno.
270 * Returns 0 on success
272 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
274 gpfs_aclCount_t i;
275 struct gpfs_acl *gacl = NULL;
276 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
278 /* First get the real acl length */
279 gacl = gpfs_getacl_alloc(fname, 0);
280 if (gacl == NULL) {
281 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
282 fname, strerror(errno)));
283 return -1;
286 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
287 DEBUG(10, ("Got non-nfsv4 acl\n"));
288 /* Retry with POSIX ACLs check */
289 return 1;
292 *ppacl = smb_create_smb4acl();
294 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
295 gacl->acl_len, gacl->acl_level, gacl->acl_version,
296 gacl->acl_nace));
298 for (i=0; i<gacl->acl_nace; i++) {
299 struct gpfs_ace_v4 *gace = &gacl->ace_v4[i];
300 SMB_ACE4PROP_T smbace;
301 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
302 "who: %d\n", gace->aceType, gace->aceIFlags,
303 gace->aceFlags, gace->aceMask, gace->aceWho));
305 ZERO_STRUCT(smbace);
306 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
307 smbace.flags |= SMB_ACE4_ID_SPECIAL;
308 switch (gace->aceWho) {
309 case ACE4_SPECIAL_OWNER:
310 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
311 break;
312 case ACE4_SPECIAL_GROUP:
313 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
314 break;
315 case ACE4_SPECIAL_EVERYONE:
316 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
317 break;
318 default:
319 DEBUG(8, ("invalid special gpfs id %d "
320 "ignored\n", gace->aceWho));
321 continue; /* don't add it */
323 } else {
324 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
325 smbace.who.gid = gace->aceWho;
326 else
327 smbace.who.uid = gace->aceWho;
330 /* remove redundent deny entries */
331 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
332 struct gpfs_ace_v4 *prev = &gacl->ace_v4[i-1];
333 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
334 prev->aceFlags == gace->aceFlags &&
335 prev->aceIFlags == gace->aceIFlags &&
336 (gace->aceMask & prev->aceMask) == 0 &&
337 gace->aceWho == prev->aceWho) {
338 /* its redundent - skip it */
339 continue;
343 smbace.aceType = gace->aceType;
344 smbace.aceFlags = gace->aceFlags;
345 smbace.aceMask = gace->aceMask;
346 smb_add_ace4(*ppacl, &smbace);
349 return 0;
352 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
353 files_struct *fsp, uint32 security_info,
354 struct security_descriptor **ppdesc)
356 SMB4ACL_T *pacl = NULL;
357 int result;
359 *ppdesc = NULL;
360 result = gpfs_get_nfs4_acl(fsp->fsp_name->base_name, &pacl);
362 if (result == 0)
363 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
365 if (result > 0) {
366 DEBUG(10, ("retrying with posix acl...\n"));
367 return posix_fget_nt_acl(fsp, security_info, ppdesc);
370 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
371 return map_nt_error_from_unix(errno);
374 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
375 const char *name,
376 uint32 security_info, struct security_descriptor **ppdesc)
378 SMB4ACL_T *pacl = NULL;
379 int result;
381 *ppdesc = NULL;
382 result = gpfs_get_nfs4_acl(name, &pacl);
384 if (result == 0)
385 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
387 if (result > 0) {
388 DEBUG(10, ("retrying with posix acl...\n"));
389 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
392 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
393 return map_nt_error_from_unix(errno);
396 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
398 int ret;
399 gpfs_aclLen_t gacl_len;
400 SMB4ACE_T *smbace;
401 struct gpfs_acl *gacl;
402 TALLOC_CTX *mem_ctx = talloc_tos();
404 gacl_len = sizeof(struct gpfs_acl) +
405 (smb_get_naces(smbacl)-1)*sizeof(gpfs_ace_v4_t);
407 gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
408 if (gacl == NULL) {
409 DEBUG(0, ("talloc failed\n"));
410 errno = ENOMEM;
411 return False;
414 gacl->acl_len = gacl_len;
415 gacl->acl_level = 0;
416 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
417 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
418 gacl->acl_nace = 0; /* change later... */
420 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
421 struct gpfs_ace_v4 *gace = &gacl->ace_v4[gacl->acl_nace];
422 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
424 gace->aceType = aceprop->aceType;
425 gace->aceFlags = aceprop->aceFlags;
426 gace->aceMask = aceprop->aceMask;
429 * GPFS can't distinguish between WRITE and APPEND on
430 * files, so one being set without the other is an
431 * error. Sorry for the many ()'s :-)
434 if (!fsp->is_directory
436 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
437 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
439 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
440 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
442 lp_parm_bool(fsp->conn->params->service, "gpfs",
443 "merge_writeappend", True)) {
444 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
445 "WRITE^APPEND, setting WRITE|APPEND\n",
446 fsp_str_dbg(fsp)));
447 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
450 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
452 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
454 switch(aceprop->who.special_id)
456 case SMB_ACE4_WHO_EVERYONE:
457 gace->aceWho = ACE4_SPECIAL_EVERYONE;
458 break;
459 case SMB_ACE4_WHO_OWNER:
460 gace->aceWho = ACE4_SPECIAL_OWNER;
461 break;
462 case SMB_ACE4_WHO_GROUP:
463 gace->aceWho = ACE4_SPECIAL_GROUP;
464 break;
465 default:
466 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
467 continue; /* don't add it !!! */
469 } else {
470 /* just only for the type safety... */
471 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
472 gace->aceWho = aceprop->who.gid;
473 else
474 gace->aceWho = aceprop->who.uid;
477 gacl->acl_nace++;
480 ret = smbd_gpfs_putacl(fsp->fsp_name->base_name,
481 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
482 if (ret != 0) {
483 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
484 gpfs_dumpacl(8, gacl);
485 return False;
488 DEBUG(10, ("gpfs_putacl succeeded\n"));
489 return True;
492 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
494 struct gpfs_acl *acl;
495 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
497 acl = gpfs_getacl_alloc(fsp->fsp_name->base_name, 0);
498 if (acl == NULL)
499 return result;
501 if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
503 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
504 "refuse_dacl_protected", false)
505 && (psd->type&SEC_DESC_DACL_PROTECTED)) {
506 DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
507 return NT_STATUS_NOT_SUPPORTED;
510 result = smb_set_nt_acl_nfs4(
511 fsp, security_info_sent, psd,
512 gpfsacl_process_smbacl);
513 } else { /* assume POSIX ACL - by default... */
514 result = set_nt_acl(fsp, security_info_sent, psd);
517 return result;
520 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
522 return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
525 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
527 SMB_ACL_T result;
528 gpfs_aclCount_t i;
530 result = sys_acl_init(pacl->acl_nace);
531 if (result == NULL) {
532 errno = ENOMEM;
533 return NULL;
536 result->count = pacl->acl_nace;
538 for (i=0; i<pacl->acl_nace; i++) {
539 struct smb_acl_entry *ace = &result->acl[i];
540 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
542 DEBUG(10, ("Converting type %d id %lu perm %x\n",
543 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
544 (int)g_ace->ace_perm));
546 switch (g_ace->ace_type) {
547 case GPFS_ACL_USER:
548 ace->a_type = SMB_ACL_USER;
549 ace->uid = (uid_t)g_ace->ace_who;
550 break;
551 case GPFS_ACL_USER_OBJ:
552 ace->a_type = SMB_ACL_USER_OBJ;
553 break;
554 case GPFS_ACL_GROUP:
555 ace->a_type = SMB_ACL_GROUP;
556 ace->gid = (gid_t)g_ace->ace_who;
557 break;
558 case GPFS_ACL_GROUP_OBJ:
559 ace->a_type = SMB_ACL_GROUP_OBJ;
560 break;
561 case GPFS_ACL_OTHER:
562 ace->a_type = SMB_ACL_OTHER;
563 break;
564 case GPFS_ACL_MASK:
565 ace->a_type = SMB_ACL_MASK;
566 break;
567 default:
568 DEBUG(10, ("Got invalid ace_type: %d\n",
569 g_ace->ace_type));
570 errno = EINVAL;
571 SAFE_FREE(result);
572 return NULL;
575 ace->a_perm = 0;
576 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
577 SMB_ACL_READ : 0;
578 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
579 SMB_ACL_WRITE : 0;
580 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
581 SMB_ACL_EXECUTE : 0;
583 DEBUGADD(10, ("Converted to %d perm %x\n",
584 ace->a_type, ace->a_perm));
587 return result;
590 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
592 struct gpfs_acl *pacl;
593 SMB_ACL_T result = NULL;
595 pacl = gpfs_getacl_alloc(path, type);
597 if (pacl == NULL) {
598 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
599 path, strerror(errno)));
600 if (errno == 0) {
601 errno = EINVAL;
603 goto done;
606 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
607 DEBUG(10, ("Got acl version %d, expected %d\n",
608 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
609 errno = EINVAL;
610 goto done;
613 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
614 pacl->acl_len, pacl->acl_level, pacl->acl_version,
615 pacl->acl_nace));
617 result = gpfs2smb_acl(pacl);
618 if (result == NULL) {
619 goto done;
622 done:
624 if (errno != 0) {
625 SAFE_FREE(result);
627 return result;
630 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
631 const char *path_p,
632 SMB_ACL_TYPE_T type)
634 gpfs_aclType_t gpfs_type;
636 switch(type) {
637 case SMB_ACL_TYPE_ACCESS:
638 gpfs_type = GPFS_ACL_TYPE_ACCESS;
639 break;
640 case SMB_ACL_TYPE_DEFAULT:
641 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
642 break;
643 default:
644 DEBUG(0, ("Got invalid type: %d\n", type));
645 smb_panic("exiting");
648 return gpfsacl_get_posix_acl(path_p, gpfs_type);
651 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
652 files_struct *fsp)
654 return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
655 GPFS_ACL_TYPE_ACCESS);
658 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
659 SMB_ACL_TYPE_T type)
661 gpfs_aclLen_t len;
662 struct gpfs_acl *result;
663 int i;
664 union gpfs_ace_union
666 gpfs_ace_v1_t ace_v1[1]; /* when GPFS_ACL_VERSION_POSIX */
667 gpfs_ace_v4_t ace_v4[1]; /* when GPFS_ACL_VERSION_NFS4 */
670 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
672 len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
673 (pacl->count)*sizeof(gpfs_ace_v1_t);
675 result = (struct gpfs_acl *)SMB_MALLOC(len);
676 if (result == NULL) {
677 errno = ENOMEM;
678 return result;
681 result->acl_len = len;
682 result->acl_level = 0;
683 result->acl_version = GPFS_ACL_VERSION_POSIX;
684 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
685 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
686 result->acl_nace = pacl->count;
688 for (i=0; i<pacl->count; i++) {
689 const struct smb_acl_entry *ace = &pacl->acl[i];
690 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
692 DEBUG(10, ("Converting type %d perm %x\n",
693 (int)ace->a_type, (int)ace->a_perm));
695 g_ace->ace_perm = 0;
697 switch(ace->a_type) {
698 case SMB_ACL_USER:
699 g_ace->ace_type = GPFS_ACL_USER;
700 g_ace->ace_who = (gpfs_uid_t)ace->uid;
701 break;
702 case SMB_ACL_USER_OBJ:
703 g_ace->ace_type = GPFS_ACL_USER_OBJ;
704 g_ace->ace_perm |= ACL_PERM_CONTROL;
705 g_ace->ace_who = 0;
706 break;
707 case SMB_ACL_GROUP:
708 g_ace->ace_type = GPFS_ACL_GROUP;
709 g_ace->ace_who = (gpfs_uid_t)ace->gid;
710 break;
711 case SMB_ACL_GROUP_OBJ:
712 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
713 g_ace->ace_who = 0;
714 break;
715 case SMB_ACL_MASK:
716 g_ace->ace_type = GPFS_ACL_MASK;
717 g_ace->ace_perm = 0x8f;
718 g_ace->ace_who = 0;
719 break;
720 case SMB_ACL_OTHER:
721 g_ace->ace_type = GPFS_ACL_OTHER;
722 g_ace->ace_who = 0;
723 break;
724 default:
725 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
726 errno = EINVAL;
727 SAFE_FREE(result);
728 return NULL;
731 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
732 ACL_PERM_READ : 0;
733 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
734 ACL_PERM_WRITE : 0;
735 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
736 ACL_PERM_EXECUTE : 0;
738 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
739 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
742 return result;
745 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
746 const char *name,
747 SMB_ACL_TYPE_T type,
748 SMB_ACL_T theacl)
750 struct gpfs_acl *gpfs_acl;
751 int result;
753 gpfs_acl = smb2gpfs_acl(theacl, type);
754 if (gpfs_acl == NULL) {
755 return -1;
758 result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
760 SAFE_FREE(gpfs_acl);
761 return result;
764 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
765 files_struct *fsp,
766 SMB_ACL_T theacl)
768 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
769 SMB_ACL_TYPE_ACCESS, theacl);
772 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
773 const char *path)
775 errno = ENOTSUP;
776 return -1;
780 * Assumed: mode bits are shiftable and standard
781 * Output: the new aceMask field for an smb nfs4 ace
783 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
785 const uint32 posix_nfs4map[3] = {
786 SMB_ACE4_EXECUTE, /* execute */
787 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
788 SMB_ACE4_READ_DATA /* read */
790 int i;
791 uint32_t posix_mask = 0x01;
792 uint32_t posix_bit;
793 uint32_t nfs4_bits;
795 for(i=0; i<3; i++) {
796 nfs4_bits = posix_nfs4map[i];
797 posix_bit = rwx & posix_mask;
799 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
800 if (posix_bit)
801 aceMask |= nfs4_bits;
802 else
803 aceMask &= ~nfs4_bits;
804 } else {
805 /* add deny bits when suitable */
806 if (!posix_bit)
807 aceMask |= nfs4_bits;
808 else
809 aceMask &= ~nfs4_bits;
810 } /* other ace types are unexpected */
812 posix_mask <<= 1;
815 return aceMask;
818 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
820 SMB4ACL_T *pacl = NULL;
821 int result;
822 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
823 int i;
824 files_struct fake_fsp; /* TODO: rationalize parametrization */
825 SMB4ACE_T *smbace;
826 NTSTATUS status;
828 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
830 result = gpfs_get_nfs4_acl(path, &pacl);
831 if (result)
832 return result;
834 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
835 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
838 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
839 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
840 uint32_t specid = ace->who.special_id;
842 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
843 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
844 specid <= SMB_ACE4_WHO_EVERYONE) {
846 uint32_t newMask;
848 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
849 haveAllowEntry[specid] = True;
851 /* mode >> 6 for @owner, mode >> 3 for @group,
852 * mode >> 0 for @everyone */
853 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
854 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
855 if (ace->aceMask!=newMask) {
856 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
857 path, ace->aceMask, newMask, specid));
859 ace->aceMask = newMask;
863 /* make sure we have at least ALLOW entries
864 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
865 * - if necessary
867 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
868 SMB_ACE4PROP_T ace;
870 if (haveAllowEntry[i]==True)
871 continue;
873 ZERO_STRUCT(ace);
874 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
875 ace.flags |= SMB_ACE4_ID_SPECIAL;
876 ace.who.special_id = i;
878 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
879 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
881 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
882 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
884 /* don't add unnecessary aces */
885 if (!ace.aceMask)
886 continue;
888 /* we add it to the END - as windows expects allow aces */
889 smb_add_ace4(pacl, &ace);
890 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
891 path, mode, i, ace.aceMask));
894 /* don't add complementary DENY ACEs here */
895 ZERO_STRUCT(fake_fsp);
896 status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
897 &fake_fsp.fsp_name);
898 if (!NT_STATUS_IS_OK(status)) {
899 errno = map_errno_from_nt_status(status);
900 return -1;
902 /* put the acl */
903 if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False) {
904 TALLOC_FREE(fake_fsp.fsp_name);
905 return -1;
908 TALLOC_FREE(fake_fsp.fsp_name);
909 return 0; /* ok for [f]chmod */
912 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
914 struct smb_filename *smb_fname_cpath;
915 int rc;
916 NTSTATUS status;
918 status = create_synthetic_smb_fname(
919 talloc_tos(), path, NULL, NULL, &smb_fname_cpath);
921 if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
922 return -1;
925 /* avoid chmod() if possible, to preserve acls */
926 if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
927 return 0;
930 rc = gpfsacl_emu_chmod(path, mode);
931 if (rc == 1)
932 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
933 return rc;
936 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
938 SMB_STRUCT_STAT st;
939 int rc;
941 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
942 return -1;
945 /* avoid chmod() if possible, to preserve acls */
946 if ((st.st_ex_mode & ~S_IFMT) == mode) {
947 return 0;
950 rc = gpfsacl_emu_chmod(fsp->fsp_name->base_name, mode);
951 if (rc == 1)
952 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
953 return rc;
956 static int gpfs_set_xattr(struct vfs_handle_struct *handle, const char *path,
957 const char *name, const void *value, size_t size, int flags){
958 struct xattr_DOSATTRIB dosattrib;
959 enum ndr_err_code ndr_err;
960 DATA_BLOB blob;
961 const char *attrstr = value;
962 unsigned int dosmode=0;
963 struct gpfs_winattr attrs;
964 int ret = 0;
965 struct gpfs_config_data *config;
967 SMB_VFS_HANDLE_GET_DATA(handle, config,
968 struct gpfs_config_data,
969 return -1);
971 if (!config->winattr) {
972 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
973 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
976 DEBUG(10, ("gpfs_set_xattr: %s \n",path));
978 /* Only handle DOS Attributes */
979 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
980 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
981 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
984 blob.data = (uint8_t *)attrstr;
985 blob.length = size;
987 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
988 (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
990 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
991 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
992 "from EA on file %s: Error = %s\n",
993 path, ndr_errstr(ndr_err)));
994 return false;
997 if (dosattrib.version != 3) {
998 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
999 "%d\n", (int)dosattrib.version));
1000 return false;
1002 if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
1003 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
1004 "valid, ignoring\n"));
1005 return true;
1008 dosmode = dosattrib.info.info3.attrib;
1010 attrs.winAttrs = 0;
1011 /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1012 if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1013 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1015 if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1016 attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1018 if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1019 attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1021 if (dosmode & FILE_ATTRIBUTE_READONLY){
1022 attrs.winAttrs |= GPFS_WINATTR_READONLY;
1024 if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1025 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1029 ret = set_gpfs_winattrs(discard_const_p(char, path),
1030 GPFS_WINATTR_SET_ATTRS, &attrs);
1031 if ( ret == -1){
1032 if (errno == ENOSYS) {
1033 return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1034 size, flags);
1037 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1038 return -1;
1041 DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1042 return 0;
1045 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle, const char *path,
1046 const char *name, void *value, size_t size){
1047 char *attrstr = value;
1048 unsigned int dosmode = 0;
1049 struct gpfs_winattr attrs;
1050 int ret = 0;
1051 struct gpfs_config_data *config;
1053 SMB_VFS_HANDLE_GET_DATA(handle, config,
1054 struct gpfs_config_data,
1055 return -1);
1057 if (!config->winattr) {
1058 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1059 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1062 DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1064 /* Only handle DOS Attributes */
1065 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1066 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1067 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1070 ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1071 if ( ret == -1){
1072 if (errno == ENOSYS) {
1073 return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1074 size);
1077 DEBUG(1, ("gpfs_get_xattr: Get GPFS attributes failed: "
1078 "%d (%s)\n", ret, strerror(errno)));
1079 return -1;
1082 DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1084 /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1085 if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1086 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1088 if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1089 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1091 if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1092 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1094 if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1095 dosmode |= FILE_ATTRIBUTE_READONLY;
1097 if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1098 dosmode |= FILE_ATTRIBUTE_SPARSE;
1101 snprintf(attrstr, size, "0x%2.2x",
1102 (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1103 DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1104 return 4;
1107 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1108 struct smb_filename *smb_fname)
1110 struct gpfs_winattr attrs;
1111 char *fname = NULL;
1112 NTSTATUS status;
1113 int ret;
1114 struct gpfs_config_data *config;
1116 SMB_VFS_HANDLE_GET_DATA(handle, config,
1117 struct gpfs_config_data,
1118 return -1);
1120 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1121 if (ret == -1) {
1122 return -1;
1125 if (!config->winattr) {
1126 return 0;
1129 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1130 if (!NT_STATUS_IS_OK(status)) {
1131 errno = map_errno_from_nt_status(status);
1132 return -1;
1134 ret = get_gpfs_winattrs(discard_const_p(char, fname), &attrs);
1135 TALLOC_FREE(fname);
1136 if (ret == 0) {
1137 smb_fname->st.st_ex_calculated_birthtime = false;
1138 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1139 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1140 smb_fname->st.vfs_private = attrs.winAttrs;
1142 return 0;
1145 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1146 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1148 struct gpfs_winattr attrs;
1149 int ret;
1150 struct gpfs_config_data *config;
1152 SMB_VFS_HANDLE_GET_DATA(handle, config,
1153 struct gpfs_config_data,
1154 return -1);
1156 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1157 if (ret == -1) {
1158 return -1;
1160 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1161 return 0;
1163 if (!config->winattr) {
1164 return 0;
1167 ret = smbd_fget_gpfs_winattrs(fsp->fh->fd, &attrs);
1168 if (ret == 0) {
1169 sbuf->st_ex_calculated_birthtime = false;
1170 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1171 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1173 return 0;
1176 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1177 struct smb_filename *smb_fname)
1179 struct gpfs_winattr attrs;
1180 char *path = NULL;
1181 NTSTATUS status;
1182 int ret;
1183 struct gpfs_config_data *config;
1185 SMB_VFS_HANDLE_GET_DATA(handle, config,
1186 struct gpfs_config_data,
1187 return -1);
1189 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1190 if (ret == -1) {
1191 return -1;
1193 if (!config->winattr) {
1194 return 0;
1197 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1198 if (!NT_STATUS_IS_OK(status)) {
1199 errno = map_errno_from_nt_status(status);
1200 return -1;
1202 ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1203 TALLOC_FREE(path);
1204 if (ret == 0) {
1205 smb_fname->st.st_ex_calculated_birthtime = false;
1206 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1207 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1208 smb_fname->st.vfs_private = attrs.winAttrs;
1210 return 0;
1213 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1214 const struct smb_filename *smb_fname,
1215 struct smb_file_time *ft)
1218 struct gpfs_winattr attrs;
1219 int ret;
1220 char *path = NULL;
1221 NTSTATUS status;
1222 struct gpfs_config_data *config;
1224 SMB_VFS_HANDLE_GET_DATA(handle, config,
1225 struct gpfs_config_data,
1226 return -1);
1228 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1229 if(ret == -1){
1230 /* don't complain if access was denied */
1231 if (errno != EPERM && errno != EACCES) {
1232 DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1233 "%s", strerror(errno)));
1235 return -1;
1238 if(null_timespec(ft->create_time)){
1239 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1240 return 0;
1243 if (!config->winattr) {
1244 return 0;
1247 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 errno = map_errno_from_nt_status(status);
1250 return -1;
1253 attrs.winAttrs = 0;
1254 attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1255 attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1257 ret = set_gpfs_winattrs(discard_const_p(char, path),
1258 GPFS_WINATTR_SET_CREATION_TIME, &attrs);
1259 if(ret == -1 && errno != ENOSYS){
1260 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1261 return -1;
1263 return 0;
1267 int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1268 struct files_struct *fsp, enum vfs_fallocate_mode mode,
1269 SMB_OFF_T offset, SMB_OFF_T len)
1271 int ret;
1272 struct gpfs_config_data *config;
1274 SMB_VFS_HANDLE_GET_DATA(handle, config,
1275 struct gpfs_config_data,
1276 return -1);
1278 if (!config->prealloc) {
1279 /* you should better not run fallocate() on GPFS at all */
1280 errno = ENOTSUP;
1281 return -1;
1284 if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1285 DEBUG(10, ("Unsupported VFS_FALLOCATE_KEEP_SIZE\n"));
1286 errno = ENOTSUP;
1287 return -1;
1290 ret = smbd_gpfs_prealloc(fsp->fh->fd, offset, len);
1292 if (ret == -1 && errno != ENOSYS) {
1293 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1294 } else if (ret == -1 && errno == ENOSYS) {
1295 DEBUG(10, ("GPFS prealloc not supported.\n"));
1296 } else {
1297 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1300 return ret;
1303 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1304 SMB_OFF_T len)
1306 int result;
1307 struct gpfs_config_data *config;
1309 SMB_VFS_HANDLE_GET_DATA(handle, config,
1310 struct gpfs_config_data,
1311 return -1);
1313 if (!config->ftruncate) {
1314 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1317 result = smbd_gpfs_ftruncate(fsp->fh->fd, len);
1318 if ((result == -1) && (errno == ENOSYS)) {
1319 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1321 return result;
1324 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1325 const struct smb_filename *fname,
1326 SMB_STRUCT_STAT *sbuf)
1328 struct gpfs_winattr attrs;
1329 char *path = NULL;
1330 NTSTATUS status;
1331 struct gpfs_config_data *config;
1333 SMB_VFS_HANDLE_GET_DATA(handle, config,
1334 struct gpfs_config_data,
1335 return -1);
1337 if (!config->winattr) {
1338 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1341 status = get_full_smb_filename(talloc_tos(), fname, &path);
1342 if (!NT_STATUS_IS_OK(status)) {
1343 errno = map_errno_from_nt_status(status);
1344 return -1;
1347 if (VALID_STAT(*sbuf)) {
1348 attrs.winAttrs = sbuf->vfs_private;
1349 } else {
1350 int ret;
1351 ret = get_gpfs_winattrs(path, &attrs);
1353 if (ret == -1) {
1354 TALLOC_FREE(path);
1355 return false;
1358 if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1359 DEBUG(10, ("%s is offline\n", path));
1360 TALLOC_FREE(path);
1361 return true;
1363 DEBUG(10, ("%s is online\n", path));
1364 TALLOC_FREE(path);
1365 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1368 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1369 struct files_struct *fsp)
1371 return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1374 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1375 files_struct *fsp, const DATA_BLOB *hdr,
1376 SMB_OFF_T offset, size_t n)
1378 if ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0) {
1379 errno = ENOSYS;
1380 return -1;
1382 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1385 int vfs_gpfs_connect(struct vfs_handle_struct *handle, const char *service,
1386 const char *user)
1388 struct gpfs_config_data *config;
1389 int ret;
1391 smbd_gpfs_lib_init();
1393 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1395 if (ret < 0) {
1396 return ret;
1399 config = talloc_zero(handle->conn, struct gpfs_config_data);
1400 if (!config) {
1401 SMB_VFS_NEXT_DISCONNECT(handle);
1402 DEBUG(0, ("talloc_zero() failed\n"));
1403 return -1;
1406 config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1407 "sharemodes", true);
1409 config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1410 "leases", true);
1412 config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1413 "hsm", false);
1415 config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1416 "syncio", false);
1418 config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1419 "winattr", false);
1421 config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1422 "ftruncate", true);
1424 config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1425 "getrealfilename", true);
1427 config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1428 "dfreequota", false);
1430 config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
1431 "prealloc", true);
1433 SMB_VFS_HANDLE_SET_DATA(handle, config,
1434 NULL, struct gpfs_config_data,
1435 return -1);
1437 return 0;
1440 static int vfs_gpfs_get_quotas(const char *path, uid_t uid, gid_t gid,
1441 int *fset_id,
1442 struct gpfs_quotaInfo *qi_user,
1443 struct gpfs_quotaInfo *qi_group,
1444 struct gpfs_quotaInfo *qi_fset)
1446 int err;
1448 err = get_gpfs_fset_id(path, fset_id);
1449 if (err) {
1450 DEBUG(0, ("Get fset id failed, errno %d.\n", errno));
1451 return err;
1454 err = get_gpfs_quota(path, GPFS_USRQUOTA, uid, qi_user);
1455 if (err) {
1456 return err;
1459 err = get_gpfs_quota(path, GPFS_GRPQUOTA, gid, qi_group);
1460 if (err) {
1461 return err;
1464 err = get_gpfs_quota(path, GPFS_FILESETQUOTA, *fset_id, qi_fset);
1465 if (err) {
1466 return err;
1469 return 0;
1472 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
1473 uint64_t *dfree, uint64_t *dsize)
1475 uint64_t usage, limit;
1478 * The quota reporting is done in units of 1024 byte blocks, but
1479 * sys_fsusage uses units of 512 byte blocks, adjust the block number
1480 * accordingly. Also filter possibly negative usage counts from gpfs.
1482 usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
1483 limit = (uint64_t)qi.blockHardLimit * 2;
1486 * When the grace time for the exceeded soft block quota has been
1487 * exceeded, the soft block quota becomes an additional hard limit.
1489 if (qi.blockGraceTime && cur_time > qi.blockGraceTime) {
1490 /* report disk as full */
1491 *dfree = 0;
1492 *dsize = MIN(*dsize, usage);
1495 if (!qi.blockHardLimit)
1496 return;
1498 if (usage >= limit) {
1499 /* report disk as full */
1500 *dfree = 0;
1501 *dsize = MIN(*dsize, usage);
1503 } else {
1504 /* limit has not been reached, determine "free space" */
1505 *dfree = MIN(*dfree, limit - usage);
1506 *dsize = MIN(*dsize, limit);
1510 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
1511 bool small_query, uint64_t *bsize,
1512 uint64_t *dfree, uint64_t *dsize)
1514 struct security_unix_token *utok;
1515 struct gpfs_quotaInfo qi_user, qi_group, qi_fset;
1516 struct gpfs_config_data *config;
1517 int err, fset_id;
1518 time_t cur_time;
1520 SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
1521 return (uint64_t)-1);
1522 if (!config->dfreequota) {
1523 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1524 bsize, dfree, dsize);
1527 err = sys_fsusage(path, dfree, dsize);
1528 if (err) {
1529 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
1530 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1531 bsize, dfree, dsize);
1534 /* sys_fsusage returns units of 512 bytes */
1535 *bsize = 512;
1537 DEBUG(10, ("fs dfree %llu, dsize %llu\n",
1538 (unsigned long long)*dfree, (unsigned long long)*dsize));
1540 utok = handle->conn->session_info->unix_token;
1541 err = vfs_gpfs_get_quotas(path, utok->uid, utok->gid, &fset_id,
1542 &qi_user, &qi_group, &qi_fset);
1543 if (err) {
1544 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1545 bsize, dfree, dsize);
1548 cur_time = time(NULL);
1550 /* Adjust free space and size according to quota limits. */
1551 vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
1552 vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
1554 /* Id 0 indicates the default quota, not an actual quota */
1555 if (fset_id != 0) {
1556 vfs_gpfs_disk_free_quota(qi_fset, cur_time, dfree, dsize);
1559 disk_norm(small_query, bsize, dfree, dsize);
1560 return *dfree;
1563 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
1564 enum timestamp_set_resolution *p_ts_res)
1566 struct gpfs_config_data *config;
1567 uint32_t next;
1569 next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
1571 SMB_VFS_HANDLE_GET_DATA(handle, config,
1572 struct gpfs_config_data,
1573 return next);
1575 if (config->hsm) {
1576 next |= FILE_SUPPORTS_REMOTE_STORAGE;
1578 return next;
1581 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
1582 struct smb_filename *smb_fname, files_struct *fsp,
1583 int flags, mode_t mode)
1585 struct gpfs_config_data *config;
1587 SMB_VFS_HANDLE_GET_DATA(handle, config,
1588 struct gpfs_config_data,
1589 return -1);
1591 if (config->syncio) {
1592 flags |= O_SYNC;
1594 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1598 static struct vfs_fn_pointers vfs_gpfs_fns = {
1599 .connect_fn = vfs_gpfs_connect,
1600 .disk_free_fn = vfs_gpfs_disk_free,
1601 .fs_capabilities_fn = vfs_gpfs_capabilities,
1602 .kernel_flock_fn = vfs_gpfs_kernel_flock,
1603 .linux_setlease_fn = vfs_gpfs_setlease,
1604 .get_real_filename_fn = vfs_gpfs_get_real_filename,
1605 .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
1606 .get_nt_acl_fn = gpfsacl_get_nt_acl,
1607 .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
1608 .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
1609 .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
1610 .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
1611 .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
1612 .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
1613 .chmod_fn = vfs_gpfs_chmod,
1614 .fchmod_fn = vfs_gpfs_fchmod,
1615 .close_fn = vfs_gpfs_close,
1616 .setxattr_fn = gpfs_set_xattr,
1617 .getxattr_fn = gpfs_get_xattr,
1618 .stat_fn = vfs_gpfs_stat,
1619 .fstat_fn = vfs_gpfs_fstat,
1620 .lstat_fn = vfs_gpfs_lstat,
1621 .ntimes_fn = vfs_gpfs_ntimes,
1622 .is_offline_fn = vfs_gpfs_is_offline,
1623 .aio_force_fn = vfs_gpfs_aio_force,
1624 .sendfile_fn = vfs_gpfs_sendfile,
1625 .fallocate_fn = vfs_gpfs_fallocate,
1626 .open_fn = vfs_gpfs_open,
1627 .ftruncate_fn = vfs_gpfs_ftruncate
1630 NTSTATUS vfs_gpfs_init(void);
1631 NTSTATUS vfs_gpfs_init(void)
1633 init_gpfs();
1635 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
1636 &vfs_gpfs_fns);