libcli/smb: move smb2cli_ioctl.c from source3 to the toplevel
[Samba/vl.git] / source3 / modules / vfs_gpfs.c
blob4b0f9ebaf724c564f7eb2beb4c7262b363fb0d8a
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;
56 int ret = 0;
58 SMB_VFS_HANDLE_GET_DATA(handle, config,
59 struct gpfs_config_data,
60 return -1);
62 START_PROFILE(syscall_kernel_flock);
64 kernel_flock(fsp->fh->fd, share_mode, access_mask);
66 if (config->sharemodes
67 && !set_gpfs_sharemode(fsp, access_mask, fsp->share_access)) {
68 ret = -1;
71 END_PROFILE(syscall_kernel_flock);
73 return ret;
76 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
79 struct gpfs_config_data *config;
81 SMB_VFS_HANDLE_GET_DATA(handle, config,
82 struct gpfs_config_data,
83 return -1);
85 if (config->sharemodes && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
86 set_gpfs_sharemode(fsp, 0, 0);
89 return SMB_VFS_NEXT_CLOSE(handle, fsp);
92 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
93 int leasetype)
95 struct gpfs_config_data *config;
96 int ret=0;
98 SMB_VFS_HANDLE_GET_DATA(handle, config,
99 struct gpfs_config_data,
100 return -1);
102 if (linux_set_lease_sighandler(fsp->fh->fd) == -1)
103 return -1;
105 START_PROFILE(syscall_linux_setlease);
107 if (config->leases) {
108 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
111 END_PROFILE(syscall_linux_setlease);
113 return ret;
116 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
117 const char *path,
118 const char *name,
119 TALLOC_CTX *mem_ctx,
120 char **found_name)
122 int result;
123 char *full_path;
124 char real_pathname[PATH_MAX+1];
125 int buflen;
126 bool mangled;
127 struct gpfs_config_data *config;
129 SMB_VFS_HANDLE_GET_DATA(handle, config,
130 struct gpfs_config_data,
131 return -1);
133 if (!config->getrealfilename) {
134 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
135 mem_ctx, found_name);
138 mangled = mangle_is_mangled(name, handle->conn->params);
139 if (mangled) {
140 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
141 mem_ctx, found_name);
144 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
145 if (full_path == NULL) {
146 errno = ENOMEM;
147 return -1;
150 buflen = sizeof(real_pathname) - 1;
152 result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
153 &buflen);
155 TALLOC_FREE(full_path);
157 if ((result == -1) && (errno == ENOSYS)) {
158 return SMB_VFS_NEXT_GET_REAL_FILENAME(
159 handle, path, name, mem_ctx, found_name);
162 if (result == -1) {
163 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
164 strerror(errno)));
165 return -1;
169 * GPFS does not necessarily null-terminate the returned path
170 * but instead returns the buffer length in buflen.
173 if (buflen < sizeof(real_pathname)) {
174 real_pathname[buflen] = '\0';
175 } else {
176 real_pathname[sizeof(real_pathname)-1] = '\0';
179 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
180 path, name, real_pathname));
182 name = strrchr_m(real_pathname, '/');
183 if (name == NULL) {
184 errno = ENOENT;
185 return -1;
188 *found_name = talloc_strdup(mem_ctx, name+1);
189 if (*found_name == NULL) {
190 errno = ENOMEM;
191 return -1;
194 return 0;
197 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
199 gpfs_aclCount_t i;
200 if (gacl==NULL)
202 DEBUG(0, ("gpfs acl is NULL\n"));
203 return;
206 DEBUG(level, ("gpfs acl: nace: %d, type:%d, version:%d, level:%d, len:%d\n",
207 gacl->acl_nace, gacl->acl_type, gacl->acl_version, gacl->acl_level, gacl->acl_len));
208 for(i=0; i<gacl->acl_nace; i++)
210 struct gpfs_ace_v4 *gace = gacl->ace_v4 + i;
211 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, iflags:0x%x, who:%u\n",
212 i, gace->aceType, gace->aceFlags, gace->aceMask,
213 gace->aceIFlags, gace->aceWho));
217 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
219 struct gpfs_acl *acl;
220 size_t len = 200;
221 int ret;
222 TALLOC_CTX *mem_ctx = talloc_tos();
224 acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
225 if (acl == NULL) {
226 errno = ENOMEM;
227 return NULL;
230 acl->acl_len = len;
231 acl->acl_level = 0;
232 acl->acl_version = 0;
233 acl->acl_type = type;
235 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
236 if ((ret != 0) && (errno == ENOSPC)) {
237 struct gpfs_acl *new_acl = (struct gpfs_acl *)TALLOC_SIZE(
238 mem_ctx, acl->acl_len + sizeof(struct gpfs_acl));
239 if (new_acl == NULL) {
240 errno = ENOMEM;
241 return NULL;
244 new_acl->acl_len = acl->acl_len;
245 new_acl->acl_level = acl->acl_level;
246 new_acl->acl_version = acl->acl_version;
247 new_acl->acl_type = acl->acl_type;
248 acl = new_acl;
250 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT, acl);
252 if (ret != 0)
254 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
255 return NULL;
258 return acl;
261 /* Tries to get nfs4 acls and returns SMB ACL allocated.
262 * On failure returns 1 if it got non-NFSv4 ACL to prompt
263 * retry with POSIX ACL checks.
264 * On failure returns -1 if there is system (GPFS) error, check errno.
265 * Returns 0 on success
267 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
269 gpfs_aclCount_t i;
270 struct gpfs_acl *gacl = NULL;
271 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
273 /* First get the real acl length */
274 gacl = gpfs_getacl_alloc(fname, 0);
275 if (gacl == NULL) {
276 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
277 fname, strerror(errno)));
278 return -1;
281 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
282 DEBUG(10, ("Got non-nfsv4 acl\n"));
283 /* Retry with POSIX ACLs check */
284 return 1;
287 *ppacl = smb_create_smb4acl();
289 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
290 gacl->acl_len, gacl->acl_level, gacl->acl_version,
291 gacl->acl_nace));
293 for (i=0; i<gacl->acl_nace; i++) {
294 struct gpfs_ace_v4 *gace = &gacl->ace_v4[i];
295 SMB_ACE4PROP_T smbace;
296 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
297 "who: %d\n", gace->aceType, gace->aceIFlags,
298 gace->aceFlags, gace->aceMask, gace->aceWho));
300 ZERO_STRUCT(smbace);
301 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
302 smbace.flags |= SMB_ACE4_ID_SPECIAL;
303 switch (gace->aceWho) {
304 case ACE4_SPECIAL_OWNER:
305 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
306 break;
307 case ACE4_SPECIAL_GROUP:
308 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
309 break;
310 case ACE4_SPECIAL_EVERYONE:
311 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
312 break;
313 default:
314 DEBUG(8, ("invalid special gpfs id %d "
315 "ignored\n", gace->aceWho));
316 continue; /* don't add it */
318 } else {
319 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
320 smbace.who.gid = gace->aceWho;
321 else
322 smbace.who.uid = gace->aceWho;
325 /* remove redundent deny entries */
326 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
327 struct gpfs_ace_v4 *prev = &gacl->ace_v4[i-1];
328 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
329 prev->aceFlags == gace->aceFlags &&
330 prev->aceIFlags == gace->aceIFlags &&
331 (gace->aceMask & prev->aceMask) == 0 &&
332 gace->aceWho == prev->aceWho) {
333 /* its redundent - skip it */
334 continue;
338 smbace.aceType = gace->aceType;
339 smbace.aceFlags = gace->aceFlags;
340 smbace.aceMask = gace->aceMask;
341 smb_add_ace4(*ppacl, &smbace);
344 return 0;
347 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
348 files_struct *fsp, uint32 security_info,
349 struct security_descriptor **ppdesc)
351 SMB4ACL_T *pacl = NULL;
352 int result;
354 *ppdesc = NULL;
355 result = gpfs_get_nfs4_acl(fsp->fsp_name->base_name, &pacl);
357 if (result == 0)
358 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
360 if (result > 0) {
361 DEBUG(10, ("retrying with posix acl...\n"));
362 return posix_fget_nt_acl(fsp, security_info, ppdesc);
365 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
366 return map_nt_error_from_unix(errno);
369 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
370 const char *name,
371 uint32 security_info, struct security_descriptor **ppdesc)
373 SMB4ACL_T *pacl = NULL;
374 int result;
376 *ppdesc = NULL;
377 result = gpfs_get_nfs4_acl(name, &pacl);
379 if (result == 0)
380 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
382 if (result > 0) {
383 DEBUG(10, ("retrying with posix acl...\n"));
384 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
387 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
388 return map_nt_error_from_unix(errno);
391 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
393 int ret;
394 gpfs_aclLen_t gacl_len;
395 SMB4ACE_T *smbace;
396 struct gpfs_acl *gacl;
397 TALLOC_CTX *mem_ctx = talloc_tos();
399 gacl_len = sizeof(struct gpfs_acl) +
400 (smb_get_naces(smbacl)-1)*sizeof(gpfs_ace_v4_t);
402 gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
403 if (gacl == NULL) {
404 DEBUG(0, ("talloc failed\n"));
405 errno = ENOMEM;
406 return False;
409 gacl->acl_len = gacl_len;
410 gacl->acl_level = 0;
411 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
412 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
413 gacl->acl_nace = 0; /* change later... */
415 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
416 struct gpfs_ace_v4 *gace = &gacl->ace_v4[gacl->acl_nace];
417 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
419 gace->aceType = aceprop->aceType;
420 gace->aceFlags = aceprop->aceFlags;
421 gace->aceMask = aceprop->aceMask;
424 * GPFS can't distinguish between WRITE and APPEND on
425 * files, so one being set without the other is an
426 * error. Sorry for the many ()'s :-)
429 if (!fsp->is_directory
431 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
432 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
434 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
435 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
437 lp_parm_bool(fsp->conn->params->service, "gpfs",
438 "merge_writeappend", True)) {
439 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
440 "WRITE^APPEND, setting WRITE|APPEND\n",
441 fsp_str_dbg(fsp)));
442 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
445 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
447 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
449 switch(aceprop->who.special_id)
451 case SMB_ACE4_WHO_EVERYONE:
452 gace->aceWho = ACE4_SPECIAL_EVERYONE;
453 break;
454 case SMB_ACE4_WHO_OWNER:
455 gace->aceWho = ACE4_SPECIAL_OWNER;
456 break;
457 case SMB_ACE4_WHO_GROUP:
458 gace->aceWho = ACE4_SPECIAL_GROUP;
459 break;
460 default:
461 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
462 continue; /* don't add it !!! */
464 } else {
465 /* just only for the type safety... */
466 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
467 gace->aceWho = aceprop->who.gid;
468 else
469 gace->aceWho = aceprop->who.uid;
472 gacl->acl_nace++;
475 ret = smbd_gpfs_putacl(fsp->fsp_name->base_name,
476 GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
477 if (ret != 0) {
478 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
479 gpfs_dumpacl(8, gacl);
480 return False;
483 DEBUG(10, ("gpfs_putacl succeeded\n"));
484 return True;
487 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
489 struct gpfs_acl *acl;
490 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
492 acl = gpfs_getacl_alloc(fsp->fsp_name->base_name, 0);
493 if (acl == NULL)
494 return result;
496 if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
498 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
499 "refuse_dacl_protected", false)
500 && (psd->type&SEC_DESC_DACL_PROTECTED)) {
501 DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
502 return NT_STATUS_NOT_SUPPORTED;
505 result = smb_set_nt_acl_nfs4(
506 fsp, security_info_sent, psd,
507 gpfsacl_process_smbacl);
508 } else { /* assume POSIX ACL - by default... */
509 result = set_nt_acl(fsp, security_info_sent, psd);
512 return result;
515 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
517 return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
520 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
522 SMB_ACL_T result;
523 gpfs_aclCount_t i;
525 result = sys_acl_init(pacl->acl_nace);
526 if (result == NULL) {
527 errno = ENOMEM;
528 return NULL;
531 result->count = pacl->acl_nace;
533 for (i=0; i<pacl->acl_nace; i++) {
534 struct smb_acl_entry *ace = &result->acl[i];
535 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
537 DEBUG(10, ("Converting type %d id %lu perm %x\n",
538 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
539 (int)g_ace->ace_perm));
541 switch (g_ace->ace_type) {
542 case GPFS_ACL_USER:
543 ace->a_type = SMB_ACL_USER;
544 ace->uid = (uid_t)g_ace->ace_who;
545 break;
546 case GPFS_ACL_USER_OBJ:
547 ace->a_type = SMB_ACL_USER_OBJ;
548 break;
549 case GPFS_ACL_GROUP:
550 ace->a_type = SMB_ACL_GROUP;
551 ace->gid = (gid_t)g_ace->ace_who;
552 break;
553 case GPFS_ACL_GROUP_OBJ:
554 ace->a_type = SMB_ACL_GROUP_OBJ;
555 break;
556 case GPFS_ACL_OTHER:
557 ace->a_type = SMB_ACL_OTHER;
558 break;
559 case GPFS_ACL_MASK:
560 ace->a_type = SMB_ACL_MASK;
561 break;
562 default:
563 DEBUG(10, ("Got invalid ace_type: %d\n",
564 g_ace->ace_type));
565 errno = EINVAL;
566 SAFE_FREE(result);
567 return NULL;
570 ace->a_perm = 0;
571 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
572 SMB_ACL_READ : 0;
573 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
574 SMB_ACL_WRITE : 0;
575 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
576 SMB_ACL_EXECUTE : 0;
578 DEBUGADD(10, ("Converted to %d perm %x\n",
579 ace->a_type, ace->a_perm));
582 return result;
585 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
587 struct gpfs_acl *pacl;
588 SMB_ACL_T result = NULL;
590 pacl = gpfs_getacl_alloc(path, type);
592 if (pacl == NULL) {
593 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
594 path, strerror(errno)));
595 if (errno == 0) {
596 errno = EINVAL;
598 goto done;
601 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
602 DEBUG(10, ("Got acl version %d, expected %d\n",
603 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
604 errno = EINVAL;
605 goto done;
608 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
609 pacl->acl_len, pacl->acl_level, pacl->acl_version,
610 pacl->acl_nace));
612 result = gpfs2smb_acl(pacl);
613 if (result == NULL) {
614 goto done;
617 done:
619 if (errno != 0) {
620 SAFE_FREE(result);
622 return result;
625 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
626 const char *path_p,
627 SMB_ACL_TYPE_T type)
629 gpfs_aclType_t gpfs_type;
631 switch(type) {
632 case SMB_ACL_TYPE_ACCESS:
633 gpfs_type = GPFS_ACL_TYPE_ACCESS;
634 break;
635 case SMB_ACL_TYPE_DEFAULT:
636 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
637 break;
638 default:
639 DEBUG(0, ("Got invalid type: %d\n", type));
640 smb_panic("exiting");
643 return gpfsacl_get_posix_acl(path_p, gpfs_type);
646 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
647 files_struct *fsp)
649 return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
650 GPFS_ACL_TYPE_ACCESS);
653 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
654 SMB_ACL_TYPE_T type)
656 gpfs_aclLen_t len;
657 struct gpfs_acl *result;
658 int i;
659 union gpfs_ace_union
661 gpfs_ace_v1_t ace_v1[1]; /* when GPFS_ACL_VERSION_POSIX */
662 gpfs_ace_v4_t ace_v4[1]; /* when GPFS_ACL_VERSION_NFS4 */
665 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
667 len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
668 (pacl->count)*sizeof(gpfs_ace_v1_t);
670 result = (struct gpfs_acl *)SMB_MALLOC(len);
671 if (result == NULL) {
672 errno = ENOMEM;
673 return result;
676 result->acl_len = len;
677 result->acl_level = 0;
678 result->acl_version = GPFS_ACL_VERSION_POSIX;
679 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
680 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
681 result->acl_nace = pacl->count;
683 for (i=0; i<pacl->count; i++) {
684 const struct smb_acl_entry *ace = &pacl->acl[i];
685 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
687 DEBUG(10, ("Converting type %d perm %x\n",
688 (int)ace->a_type, (int)ace->a_perm));
690 g_ace->ace_perm = 0;
692 switch(ace->a_type) {
693 case SMB_ACL_USER:
694 g_ace->ace_type = GPFS_ACL_USER;
695 g_ace->ace_who = (gpfs_uid_t)ace->uid;
696 break;
697 case SMB_ACL_USER_OBJ:
698 g_ace->ace_type = GPFS_ACL_USER_OBJ;
699 g_ace->ace_perm |= ACL_PERM_CONTROL;
700 g_ace->ace_who = 0;
701 break;
702 case SMB_ACL_GROUP:
703 g_ace->ace_type = GPFS_ACL_GROUP;
704 g_ace->ace_who = (gpfs_uid_t)ace->gid;
705 break;
706 case SMB_ACL_GROUP_OBJ:
707 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
708 g_ace->ace_who = 0;
709 break;
710 case SMB_ACL_MASK:
711 g_ace->ace_type = GPFS_ACL_MASK;
712 g_ace->ace_perm = 0x8f;
713 g_ace->ace_who = 0;
714 break;
715 case SMB_ACL_OTHER:
716 g_ace->ace_type = GPFS_ACL_OTHER;
717 g_ace->ace_who = 0;
718 break;
719 default:
720 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
721 errno = EINVAL;
722 SAFE_FREE(result);
723 return NULL;
726 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
727 ACL_PERM_READ : 0;
728 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
729 ACL_PERM_WRITE : 0;
730 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
731 ACL_PERM_EXECUTE : 0;
733 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
734 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
737 return result;
740 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
741 const char *name,
742 SMB_ACL_TYPE_T type,
743 SMB_ACL_T theacl)
745 struct gpfs_acl *gpfs_acl;
746 int result;
748 gpfs_acl = smb2gpfs_acl(theacl, type);
749 if (gpfs_acl == NULL) {
750 return -1;
753 result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
755 SAFE_FREE(gpfs_acl);
756 return result;
759 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
760 files_struct *fsp,
761 SMB_ACL_T theacl)
763 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name->base_name,
764 SMB_ACL_TYPE_ACCESS, theacl);
767 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
768 const char *path)
770 errno = ENOTSUP;
771 return -1;
775 * Assumed: mode bits are shiftable and standard
776 * Output: the new aceMask field for an smb nfs4 ace
778 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
780 const uint32 posix_nfs4map[3] = {
781 SMB_ACE4_EXECUTE, /* execute */
782 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
783 SMB_ACE4_READ_DATA /* read */
785 int i;
786 uint32_t posix_mask = 0x01;
787 uint32_t posix_bit;
788 uint32_t nfs4_bits;
790 for(i=0; i<3; i++) {
791 nfs4_bits = posix_nfs4map[i];
792 posix_bit = rwx & posix_mask;
794 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
795 if (posix_bit)
796 aceMask |= nfs4_bits;
797 else
798 aceMask &= ~nfs4_bits;
799 } else {
800 /* add deny bits when suitable */
801 if (!posix_bit)
802 aceMask |= nfs4_bits;
803 else
804 aceMask &= ~nfs4_bits;
805 } /* other ace types are unexpected */
807 posix_mask <<= 1;
810 return aceMask;
813 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
815 SMB4ACL_T *pacl = NULL;
816 int result;
817 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
818 int i;
819 files_struct fake_fsp; /* TODO: rationalize parametrization */
820 SMB4ACE_T *smbace;
821 NTSTATUS status;
823 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
825 result = gpfs_get_nfs4_acl(path, &pacl);
826 if (result)
827 return result;
829 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
830 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
833 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
834 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
835 uint32_t specid = ace->who.special_id;
837 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
838 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
839 specid <= SMB_ACE4_WHO_EVERYONE) {
841 uint32_t newMask;
843 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
844 haveAllowEntry[specid] = True;
846 /* mode >> 6 for @owner, mode >> 3 for @group,
847 * mode >> 0 for @everyone */
848 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
849 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
850 if (ace->aceMask!=newMask) {
851 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
852 path, ace->aceMask, newMask, specid));
854 ace->aceMask = newMask;
858 /* make sure we have at least ALLOW entries
859 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
860 * - if necessary
862 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
863 SMB_ACE4PROP_T ace;
865 if (haveAllowEntry[i]==True)
866 continue;
868 ZERO_STRUCT(ace);
869 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
870 ace.flags |= SMB_ACE4_ID_SPECIAL;
871 ace.who.special_id = i;
873 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
874 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
876 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
877 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
879 /* don't add unnecessary aces */
880 if (!ace.aceMask)
881 continue;
883 /* we add it to the END - as windows expects allow aces */
884 smb_add_ace4(pacl, &ace);
885 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
886 path, mode, i, ace.aceMask));
889 /* don't add complementary DENY ACEs here */
890 ZERO_STRUCT(fake_fsp);
891 status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
892 &fake_fsp.fsp_name);
893 if (!NT_STATUS_IS_OK(status)) {
894 errno = map_errno_from_nt_status(status);
895 return -1;
897 /* put the acl */
898 if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False) {
899 TALLOC_FREE(fake_fsp.fsp_name);
900 return -1;
903 TALLOC_FREE(fake_fsp.fsp_name);
904 return 0; /* ok for [f]chmod */
907 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
909 struct smb_filename *smb_fname_cpath;
910 int rc;
911 NTSTATUS status;
913 status = create_synthetic_smb_fname(
914 talloc_tos(), path, NULL, NULL, &smb_fname_cpath);
916 if (SMB_VFS_NEXT_STAT(handle, smb_fname_cpath) != 0) {
917 return -1;
920 /* avoid chmod() if possible, to preserve acls */
921 if ((smb_fname_cpath->st.st_ex_mode & ~S_IFMT) == mode) {
922 return 0;
925 rc = gpfsacl_emu_chmod(path, mode);
926 if (rc == 1)
927 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
928 return rc;
931 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
933 SMB_STRUCT_STAT st;
934 int rc;
936 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
937 return -1;
940 /* avoid chmod() if possible, to preserve acls */
941 if ((st.st_ex_mode & ~S_IFMT) == mode) {
942 return 0;
945 rc = gpfsacl_emu_chmod(fsp->fsp_name->base_name, mode);
946 if (rc == 1)
947 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
948 return rc;
951 static int gpfs_set_xattr(struct vfs_handle_struct *handle, const char *path,
952 const char *name, const void *value, size_t size, int flags){
953 struct xattr_DOSATTRIB dosattrib;
954 enum ndr_err_code ndr_err;
955 DATA_BLOB blob;
956 const char *attrstr = value;
957 unsigned int dosmode=0;
958 struct gpfs_winattr attrs;
959 int ret = 0;
960 struct gpfs_config_data *config;
962 SMB_VFS_HANDLE_GET_DATA(handle, config,
963 struct gpfs_config_data,
964 return -1);
966 if (!config->winattr) {
967 DEBUG(10, ("gpfs_set_xattr:name is %s -> next\n",name));
968 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
971 DEBUG(10, ("gpfs_set_xattr: %s \n",path));
973 /* Only handle DOS Attributes */
974 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
975 DEBUG(5, ("gpfs_set_xattr:name is %s\n",name));
976 return SMB_VFS_NEXT_SETXATTR(handle,path,name,value,size,flags);
979 blob.data = (uint8_t *)attrstr;
980 blob.length = size;
982 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
983 (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
985 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
986 DEBUG(1, ("gpfs_set_xattr: bad ndr decode "
987 "from EA on file %s: Error = %s\n",
988 path, ndr_errstr(ndr_err)));
989 return false;
992 if (dosattrib.version != 3) {
993 DEBUG(1, ("gpfs_set_xattr: expected dosattrib version 3, got "
994 "%d\n", (int)dosattrib.version));
995 return false;
997 if (!(dosattrib.info.info3.valid_flags & XATTR_DOSINFO_ATTRIB)) {
998 DEBUG(10, ("gpfs_set_xattr: XATTR_DOSINFO_ATTRIB not "
999 "valid, ignoring\n"));
1000 return true;
1003 dosmode = dosattrib.info.info3.attrib;
1005 attrs.winAttrs = 0;
1006 /*Just map RD_ONLY, ARCHIVE, SYSTEM HIDDEN and SPARSE. Ignore the others*/
1007 if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1008 attrs.winAttrs |= GPFS_WINATTR_ARCHIVE;
1010 if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1011 attrs.winAttrs |= GPFS_WINATTR_HIDDEN;
1013 if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1014 attrs.winAttrs |= GPFS_WINATTR_SYSTEM;
1016 if (dosmode & FILE_ATTRIBUTE_READONLY){
1017 attrs.winAttrs |= GPFS_WINATTR_READONLY;
1019 if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1020 attrs.winAttrs |= GPFS_WINATTR_SPARSE_FILE;
1024 ret = set_gpfs_winattrs(discard_const_p(char, path),
1025 GPFS_WINATTR_SET_ATTRS, &attrs);
1026 if ( ret == -1){
1027 if (errno == ENOSYS) {
1028 return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1029 size, flags);
1032 DEBUG(1, ("gpfs_set_xattr:Set GPFS attributes failed %d\n",ret));
1033 return -1;
1036 DEBUG(10, ("gpfs_set_xattr:Set attributes: 0x%x\n",attrs.winAttrs));
1037 return 0;
1040 static ssize_t gpfs_get_xattr(struct vfs_handle_struct *handle, const char *path,
1041 const char *name, void *value, size_t size){
1042 char *attrstr = value;
1043 unsigned int dosmode = 0;
1044 struct gpfs_winattr attrs;
1045 int ret = 0;
1046 struct gpfs_config_data *config;
1048 SMB_VFS_HANDLE_GET_DATA(handle, config,
1049 struct gpfs_config_data,
1050 return -1);
1052 if (!config->winattr) {
1053 DEBUG(10, ("gpfs_get_xattr:name is %s -> next\n",name));
1054 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1057 DEBUG(10, ("gpfs_get_xattr: %s \n",path));
1059 /* Only handle DOS Attributes */
1060 if (strcmp(name,SAMBA_XATTR_DOS_ATTRIB) != 0){
1061 DEBUG(5, ("gpfs_get_xattr:name is %s\n",name));
1062 return SMB_VFS_NEXT_GETXATTR(handle,path,name,value,size);
1065 ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1066 if ( ret == -1){
1067 if (errno == ENOSYS) {
1068 return SMB_VFS_NEXT_GETXATTR(handle, path, name, value,
1069 size);
1072 DEBUG(1, ("gpfs_get_xattr: Get GPFS attributes failed: "
1073 "%d (%s)\n", ret, strerror(errno)));
1074 return -1;
1077 DEBUG(10, ("gpfs_get_xattr:Got attributes: 0x%x\n",attrs.winAttrs));
1079 /*Just map RD_ONLY, ARCHIVE, SYSTEM, HIDDEN and SPARSE. Ignore the others*/
1080 if (attrs.winAttrs & GPFS_WINATTR_ARCHIVE){
1081 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1083 if (attrs.winAttrs & GPFS_WINATTR_HIDDEN){
1084 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1086 if (attrs.winAttrs & GPFS_WINATTR_SYSTEM){
1087 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1089 if (attrs.winAttrs & GPFS_WINATTR_READONLY){
1090 dosmode |= FILE_ATTRIBUTE_READONLY;
1092 if (attrs.winAttrs & GPFS_WINATTR_SPARSE_FILE) {
1093 dosmode |= FILE_ATTRIBUTE_SPARSE;
1096 snprintf(attrstr, size, "0x%2.2x",
1097 (unsigned int)(dosmode & SAMBA_ATTRIBUTES_MASK));
1098 DEBUG(10, ("gpfs_get_xattr: returning %s\n",attrstr));
1099 return 4;
1102 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1103 struct smb_filename *smb_fname)
1105 struct gpfs_winattr attrs;
1106 char *fname = NULL;
1107 NTSTATUS status;
1108 int ret;
1109 struct gpfs_config_data *config;
1111 SMB_VFS_HANDLE_GET_DATA(handle, config,
1112 struct gpfs_config_data,
1113 return -1);
1115 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1116 if (ret == -1) {
1117 return -1;
1120 if (!config->winattr) {
1121 return 0;
1124 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 errno = map_errno_from_nt_status(status);
1127 return -1;
1129 ret = get_gpfs_winattrs(discard_const_p(char, fname), &attrs);
1130 TALLOC_FREE(fname);
1131 if (ret == 0) {
1132 smb_fname->st.st_ex_calculated_birthtime = false;
1133 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1134 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1135 smb_fname->st.vfs_private = attrs.winAttrs;
1137 return 0;
1140 static int vfs_gpfs_fstat(struct vfs_handle_struct *handle,
1141 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1143 struct gpfs_winattr attrs;
1144 int ret;
1145 struct gpfs_config_data *config;
1147 SMB_VFS_HANDLE_GET_DATA(handle, config,
1148 struct gpfs_config_data,
1149 return -1);
1151 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1152 if (ret == -1) {
1153 return -1;
1155 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1156 return 0;
1158 if (!config->winattr) {
1159 return 0;
1162 ret = smbd_fget_gpfs_winattrs(fsp->fh->fd, &attrs);
1163 if (ret == 0) {
1164 sbuf->st_ex_calculated_birthtime = false;
1165 sbuf->st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1166 sbuf->st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1168 return 0;
1171 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1172 struct smb_filename *smb_fname)
1174 struct gpfs_winattr attrs;
1175 char *path = NULL;
1176 NTSTATUS status;
1177 int ret;
1178 struct gpfs_config_data *config;
1180 SMB_VFS_HANDLE_GET_DATA(handle, config,
1181 struct gpfs_config_data,
1182 return -1);
1184 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1185 if (ret == -1) {
1186 return -1;
1188 if (!config->winattr) {
1189 return 0;
1192 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 errno = map_errno_from_nt_status(status);
1195 return -1;
1197 ret = get_gpfs_winattrs(discard_const_p(char, path), &attrs);
1198 TALLOC_FREE(path);
1199 if (ret == 0) {
1200 smb_fname->st.st_ex_calculated_birthtime = false;
1201 smb_fname->st.st_ex_btime.tv_sec = attrs.creationTime.tv_sec;
1202 smb_fname->st.st_ex_btime.tv_nsec = attrs.creationTime.tv_nsec;
1203 smb_fname->st.vfs_private = attrs.winAttrs;
1205 return 0;
1208 static int vfs_gpfs_ntimes(struct vfs_handle_struct *handle,
1209 const struct smb_filename *smb_fname,
1210 struct smb_file_time *ft)
1213 struct gpfs_winattr attrs;
1214 int ret;
1215 char *path = NULL;
1216 NTSTATUS status;
1217 struct gpfs_config_data *config;
1219 SMB_VFS_HANDLE_GET_DATA(handle, config,
1220 struct gpfs_config_data,
1221 return -1);
1223 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1224 if(ret == -1){
1225 /* don't complain if access was denied */
1226 if (errno != EPERM && errno != EACCES) {
1227 DEBUG(1,("vfs_gpfs_ntimes: SMB_VFS_NEXT_NTIMES failed:"
1228 "%s", strerror(errno)));
1230 return -1;
1233 if(null_timespec(ft->create_time)){
1234 DEBUG(10,("vfs_gpfs_ntimes:Create Time is NULL\n"));
1235 return 0;
1238 if (!config->winattr) {
1239 return 0;
1242 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
1243 if (!NT_STATUS_IS_OK(status)) {
1244 errno = map_errno_from_nt_status(status);
1245 return -1;
1248 attrs.winAttrs = 0;
1249 attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1250 attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1252 ret = set_gpfs_winattrs(discard_const_p(char, path),
1253 GPFS_WINATTR_SET_CREATION_TIME, &attrs);
1254 if(ret == -1 && errno != ENOSYS){
1255 DEBUG(1,("vfs_gpfs_ntimes: set GPFS ntimes failed %d\n",ret));
1256 return -1;
1258 return 0;
1262 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1263 struct files_struct *fsp, enum vfs_fallocate_mode mode,
1264 off_t offset, off_t len)
1266 int ret;
1267 struct gpfs_config_data *config;
1269 SMB_VFS_HANDLE_GET_DATA(handle, config,
1270 struct gpfs_config_data,
1271 return -1);
1273 if (!config->prealloc) {
1274 /* you should better not run fallocate() on GPFS at all */
1275 errno = ENOTSUP;
1276 return -1;
1279 if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1280 DEBUG(10, ("Unsupported VFS_FALLOCATE_KEEP_SIZE\n"));
1281 errno = ENOTSUP;
1282 return -1;
1285 ret = smbd_gpfs_prealloc(fsp->fh->fd, offset, len);
1287 if (ret == -1 && errno != ENOSYS) {
1288 DEBUG(0, ("GPFS prealloc failed: %s\n", strerror(errno)));
1289 } else if (ret == -1 && errno == ENOSYS) {
1290 DEBUG(10, ("GPFS prealloc not supported.\n"));
1291 } else {
1292 DEBUG(10, ("GPFS prealloc succeeded.\n"));
1295 return ret;
1298 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1299 off_t len)
1301 int result;
1302 struct gpfs_config_data *config;
1304 SMB_VFS_HANDLE_GET_DATA(handle, config,
1305 struct gpfs_config_data,
1306 return -1);
1308 if (!config->ftruncate) {
1309 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1312 result = smbd_gpfs_ftruncate(fsp->fh->fd, len);
1313 if ((result == -1) && (errno == ENOSYS)) {
1314 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1316 return result;
1319 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1320 const struct smb_filename *fname,
1321 SMB_STRUCT_STAT *sbuf)
1323 struct gpfs_winattr attrs;
1324 char *path = NULL;
1325 NTSTATUS status;
1326 struct gpfs_config_data *config;
1328 SMB_VFS_HANDLE_GET_DATA(handle, config,
1329 struct gpfs_config_data,
1330 return -1);
1332 if (!config->winattr) {
1333 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1336 status = get_full_smb_filename(talloc_tos(), fname, &path);
1337 if (!NT_STATUS_IS_OK(status)) {
1338 errno = map_errno_from_nt_status(status);
1339 return -1;
1342 if (VALID_STAT(*sbuf)) {
1343 attrs.winAttrs = sbuf->vfs_private;
1344 } else {
1345 int ret;
1346 ret = get_gpfs_winattrs(path, &attrs);
1348 if (ret == -1) {
1349 TALLOC_FREE(path);
1350 return false;
1353 if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1354 DEBUG(10, ("%s is offline\n", path));
1355 TALLOC_FREE(path);
1356 return true;
1358 DEBUG(10, ("%s is online\n", path));
1359 TALLOC_FREE(path);
1360 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1363 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1364 struct files_struct *fsp)
1366 return vfs_gpfs_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
1369 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1370 files_struct *fsp, const DATA_BLOB *hdr,
1371 off_t offset, size_t n)
1373 if ((fsp->fsp_name->st.vfs_private & GPFS_WINATTR_OFFLINE) != 0) {
1374 errno = ENOSYS;
1375 return -1;
1377 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1380 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1381 const char *service, const char *user)
1383 struct gpfs_config_data *config;
1384 int ret;
1386 smbd_gpfs_lib_init();
1388 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1390 if (ret < 0) {
1391 return ret;
1394 config = talloc_zero(handle->conn, struct gpfs_config_data);
1395 if (!config) {
1396 SMB_VFS_NEXT_DISCONNECT(handle);
1397 DEBUG(0, ("talloc_zero() failed\n"));
1398 return -1;
1401 config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1402 "sharemodes", true);
1404 config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1405 "leases", true);
1407 config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1408 "hsm", false);
1410 config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1411 "syncio", false);
1413 config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1414 "winattr", false);
1416 config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1417 "ftruncate", true);
1419 config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1420 "getrealfilename", true);
1422 config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1423 "dfreequota", false);
1425 config->prealloc = lp_parm_bool(SNUM(handle->conn), "gpfs",
1426 "prealloc", true);
1428 SMB_VFS_HANDLE_SET_DATA(handle, config,
1429 NULL, struct gpfs_config_data,
1430 return -1);
1432 return 0;
1435 static int vfs_gpfs_get_quotas(const char *path, uid_t uid, gid_t gid,
1436 int *fset_id,
1437 struct gpfs_quotaInfo *qi_user,
1438 struct gpfs_quotaInfo *qi_group,
1439 struct gpfs_quotaInfo *qi_fset)
1441 int err;
1443 err = get_gpfs_fset_id(path, fset_id);
1444 if (err) {
1445 DEBUG(0, ("Get fset id failed, errno %d.\n", errno));
1446 return err;
1449 err = get_gpfs_quota(path, GPFS_USRQUOTA, uid, qi_user);
1450 if (err) {
1451 return err;
1454 err = get_gpfs_quota(path, GPFS_GRPQUOTA, gid, qi_group);
1455 if (err) {
1456 return err;
1459 err = get_gpfs_quota(path, GPFS_FILESETQUOTA, *fset_id, qi_fset);
1460 if (err) {
1461 return err;
1464 return 0;
1467 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
1468 uint64_t *dfree, uint64_t *dsize)
1470 uint64_t usage, limit;
1473 * The quota reporting is done in units of 1024 byte blocks, but
1474 * sys_fsusage uses units of 512 byte blocks, adjust the block number
1475 * accordingly. Also filter possibly negative usage counts from gpfs.
1477 usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
1478 limit = (uint64_t)qi.blockHardLimit * 2;
1481 * When the grace time for the exceeded soft block quota has been
1482 * exceeded, the soft block quota becomes an additional hard limit.
1484 if (qi.blockGraceTime && cur_time > qi.blockGraceTime) {
1485 /* report disk as full */
1486 *dfree = 0;
1487 *dsize = MIN(*dsize, usage);
1490 if (!qi.blockHardLimit)
1491 return;
1493 if (usage >= limit) {
1494 /* report disk as full */
1495 *dfree = 0;
1496 *dsize = MIN(*dsize, usage);
1498 } else {
1499 /* limit has not been reached, determine "free space" */
1500 *dfree = MIN(*dfree, limit - usage);
1501 *dsize = MIN(*dsize, limit);
1505 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle, const char *path,
1506 bool small_query, uint64_t *bsize,
1507 uint64_t *dfree, uint64_t *dsize)
1509 struct security_unix_token *utok;
1510 struct gpfs_quotaInfo qi_user, qi_group, qi_fset;
1511 struct gpfs_config_data *config;
1512 int err, fset_id;
1513 time_t cur_time;
1515 SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
1516 return (uint64_t)-1);
1517 if (!config->dfreequota) {
1518 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1519 bsize, dfree, dsize);
1522 err = sys_fsusage(path, dfree, dsize);
1523 if (err) {
1524 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
1525 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1526 bsize, dfree, dsize);
1529 /* sys_fsusage returns units of 512 bytes */
1530 *bsize = 512;
1532 DEBUG(10, ("fs dfree %llu, dsize %llu\n",
1533 (unsigned long long)*dfree, (unsigned long long)*dsize));
1535 utok = handle->conn->session_info->unix_token;
1536 err = vfs_gpfs_get_quotas(path, utok->uid, utok->gid, &fset_id,
1537 &qi_user, &qi_group, &qi_fset);
1538 if (err) {
1539 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1540 bsize, dfree, dsize);
1543 cur_time = time(NULL);
1545 /* Adjust free space and size according to quota limits. */
1546 vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
1547 vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
1549 /* Id 0 indicates the default quota, not an actual quota */
1550 if (fset_id != 0) {
1551 vfs_gpfs_disk_free_quota(qi_fset, cur_time, dfree, dsize);
1554 disk_norm(small_query, bsize, dfree, dsize);
1555 return *dfree;
1558 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
1559 enum timestamp_set_resolution *p_ts_res)
1561 struct gpfs_config_data *config;
1562 uint32_t next;
1564 next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
1566 SMB_VFS_HANDLE_GET_DATA(handle, config,
1567 struct gpfs_config_data,
1568 return next);
1570 if (config->hsm) {
1571 next |= FILE_SUPPORTS_REMOTE_STORAGE;
1573 return next;
1576 static int vfs_gpfs_open(struct vfs_handle_struct *handle,
1577 struct smb_filename *smb_fname, files_struct *fsp,
1578 int flags, mode_t mode)
1580 struct gpfs_config_data *config;
1582 SMB_VFS_HANDLE_GET_DATA(handle, config,
1583 struct gpfs_config_data,
1584 return -1);
1586 if (config->syncio) {
1587 flags |= O_SYNC;
1589 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1593 static struct vfs_fn_pointers vfs_gpfs_fns = {
1594 .connect_fn = vfs_gpfs_connect,
1595 .disk_free_fn = vfs_gpfs_disk_free,
1596 .fs_capabilities_fn = vfs_gpfs_capabilities,
1597 .kernel_flock_fn = vfs_gpfs_kernel_flock,
1598 .linux_setlease_fn = vfs_gpfs_setlease,
1599 .get_real_filename_fn = vfs_gpfs_get_real_filename,
1600 .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
1601 .get_nt_acl_fn = gpfsacl_get_nt_acl,
1602 .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
1603 .sys_acl_get_file_fn = gpfsacl_sys_acl_get_file,
1604 .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
1605 .sys_acl_set_file_fn = gpfsacl_sys_acl_set_file,
1606 .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
1607 .sys_acl_delete_def_file_fn = gpfsacl_sys_acl_delete_def_file,
1608 .chmod_fn = vfs_gpfs_chmod,
1609 .fchmod_fn = vfs_gpfs_fchmod,
1610 .close_fn = vfs_gpfs_close,
1611 .setxattr_fn = gpfs_set_xattr,
1612 .getxattr_fn = gpfs_get_xattr,
1613 .stat_fn = vfs_gpfs_stat,
1614 .fstat_fn = vfs_gpfs_fstat,
1615 .lstat_fn = vfs_gpfs_lstat,
1616 .ntimes_fn = vfs_gpfs_ntimes,
1617 .is_offline_fn = vfs_gpfs_is_offline,
1618 .aio_force_fn = vfs_gpfs_aio_force,
1619 .sendfile_fn = vfs_gpfs_sendfile,
1620 .fallocate_fn = vfs_gpfs_fallocate,
1621 .open_fn = vfs_gpfs_open,
1622 .ftruncate_fn = vfs_gpfs_ftruncate
1625 NTSTATUS vfs_gpfs_init(void);
1626 NTSTATUS vfs_gpfs_init(void)
1628 init_gpfs();
1630 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
1631 &vfs_gpfs_fns);