s3:vfs_gpfs: Fix compile error in gpfs module
[Samba/gebeck_regimport.git] / source3 / modules / gpfs.c
blobdb6025642b140d63026d8bcea176e30d958c22e9
1 /*
2 * Unix SMB/CIFS implementation.
3 * Provide a connection to GPFS specific features
4 * Copyright (C) Volker Lendecke 2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
24 #include <fcntl.h>
25 #include "libcli/security/security.h"
26 #include "gpfs_fcntl.h"
27 #include "gpfs_gpl.h"
28 #include "vfs_gpfs.h"
30 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
31 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
32 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
33 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
34 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
35 int *buflen);
36 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
37 static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
38 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
39 static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t startOffset, gpfs_off64_t bytesToPrealloc);
40 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
41 static int (*gpfs_lib_init_fn)(int flags);
42 static int (*gpfs_quotactl_fn)(char *pathname, int cmd, int id, void *bufferP);
43 static int (*gpfs_fcntl_fn)(gpfs_file_t fileDesc, void *fcntlArgP);
44 static int (*gpfs_getfilesetid_fn)(char *pathname, char *name, int *idP);
46 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
47 uint32 share_access)
49 unsigned int allow = GPFS_SHARE_NONE;
50 unsigned int deny = GPFS_DENY_NONE;
51 int result;
53 if (gpfs_set_share_fn == NULL) {
54 return False;
57 if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
58 /* No real file, don't disturb */
59 return True;
62 allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
63 DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
64 allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
65 GPFS_SHARE_READ : 0;
67 if (allow == GPFS_SHARE_NONE) {
68 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
70 else {
71 deny |= (share_access & FILE_SHARE_WRITE) ?
72 0 : GPFS_DENY_WRITE;
73 deny |= (share_access & (FILE_SHARE_READ)) ?
74 0 : GPFS_DENY_READ;
76 DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
77 access_mask, allow, share_access, deny));
79 result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
80 if (result != 0) {
81 if (errno == ENOSYS) {
82 DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
83 "support has been compiled into Samba. Allowing access\n"));
84 return True;
85 } else {
86 DEBUG(10, ("gpfs_set_share failed: %s\n",
87 strerror(errno)));
91 return (result == 0);
94 int set_gpfs_lease(int fd, int leasetype)
96 int gpfs_type = GPFS_LEASE_NONE;
98 if (gpfs_set_lease_fn == NULL) {
99 errno = EINVAL;
100 return -1;
103 if (leasetype == F_RDLCK) {
104 gpfs_type = GPFS_LEASE_READ;
106 if (leasetype == F_WRLCK) {
107 gpfs_type = GPFS_LEASE_WRITE;
110 /* we unconditionally set CAP_LEASE, rather than looking for
111 -1/EACCES as there is a bug in some versions of
112 libgpfs_gpl.so which results in a leaked fd on /dev/ss0
113 each time we try this with the wrong capabilities set
115 linux_set_lease_capability();
116 return gpfs_set_lease_fn(fd, gpfs_type);
119 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
121 if (gpfs_getacl_fn == NULL) {
122 errno = ENOSYS;
123 return -1;
126 return gpfs_getacl_fn(pathname, flags, acl);
129 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
131 if (gpfs_putacl_fn == NULL) {
132 errno = ENOSYS;
133 return -1;
136 return gpfs_putacl_fn(pathname, flags, acl);
139 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
141 if (gpfs_ftruncate_fn == NULL) {
142 errno = ENOSYS;
143 return -1;
146 return gpfs_ftruncate_fn(fd, length);
149 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
150 int *buflen)
152 if (gpfs_get_realfilename_path_fn == NULL) {
153 errno = ENOSYS;
154 return -1;
157 return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
160 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
163 if (gpfs_get_winattrs_path_fn == NULL) {
164 errno = ENOSYS;
165 return -1;
167 DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
168 return gpfs_get_winattrs_path_fn(pathname, attrs);
171 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
174 if (gpfs_get_winattrs_fn == NULL) {
175 errno = ENOSYS;
176 return -1;
178 DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
179 return gpfs_get_winattrs_fn(fd, attrs);
182 int smbd_gpfs_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
184 if (gpfs_prealloc_fn == NULL) {
185 errno = ENOSYS;
186 return -1;
189 return gpfs_prealloc_fn(fd, start, bytes);
192 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
194 if (gpfs_set_winattrs_path_fn == NULL) {
195 errno = ENOSYS;
196 return -1;
199 DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
200 return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
203 int get_gpfs_quota(const char *pathname, int type, int id,
204 struct gpfs_quotaInfo *qi)
206 int ret;
208 if (!gpfs_quotactl_fn) {
209 errno = ENOSYS;
210 return -1;
213 ZERO_STRUCTP(qi);
214 ret = gpfs_quotactl_fn(discard_const_p(char, pathname),
215 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
217 if (ret) {
218 if (errno == GPFS_E_NO_QUOTA_INST) {
219 DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
220 } else {
221 DEBUG(0, ("Get quota failed, type %d, id, %d, "
222 "errno %d.\n", type, id, errno));
225 return ret;
228 DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
229 type, id, qi->blockUsage, qi->blockHardLimit,
230 qi->blockSoftLimit, qi->blockGraceTime));
232 return ret;
235 int get_gpfs_fset_id(const char *pathname, int *fset_id)
237 int err, fd, errno_fcntl;
239 struct {
240 gpfsFcntlHeader_t hdr;
241 gpfsGetFilesetName_t fsn;
242 } arg;
244 if (!gpfs_fcntl_fn || !gpfs_getfilesetid_fn) {
245 errno = ENOSYS;
246 return -1;
249 arg.hdr.totalLength = sizeof(arg);
250 arg.hdr.fcntlVersion = GPFS_FCNTL_CURRENT_VERSION;
251 arg.hdr.fcntlReserved = 0;
252 arg.fsn.structLen = sizeof(arg.fsn);
253 arg.fsn.structType = GPFS_FCNTL_GET_FILESETNAME;
255 fd = open(pathname, O_RDONLY);
256 if (fd == -1)
257 return fd;
259 err = gpfs_fcntl_fn(fd, &arg);
260 errno_fcntl = errno;
261 close(fd);
263 if (err) {
264 errno = errno_fcntl;
265 return err;
268 return gpfs_getfilesetid_fn(discard_const_p(char, pathname),
269 arg.fsn.buffer, fset_id);
272 void smbd_gpfs_lib_init()
274 if (gpfs_lib_init_fn) {
275 int rc = gpfs_lib_init_fn(0);
276 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
277 "and errno %d\n", rc, errno));
278 } else {
279 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
283 static bool init_gpfs_function_lib(void *plibhandle_pointer,
284 const char *libname,
285 void *pfn_pointer, const char *fn_name)
287 bool did_open_here = false;
288 void **libhandle_pointer = (void **)plibhandle_pointer;
289 void **fn_pointer = (void **)pfn_pointer;
291 DEBUG(10, ("trying to load name %s from %s\n",
292 fn_name, libname));
294 if (*libhandle_pointer == NULL) {
295 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
296 did_open_here = true;
298 if (*libhandle_pointer == NULL) {
299 DEBUG(10, ("Could not open lib %s\n", libname));
300 return false;
303 *fn_pointer = dlsym(*libhandle_pointer, fn_name);
304 if (*fn_pointer == NULL) {
305 DEBUG(10, ("Did not find symbol %s in lib %s\n",
306 fn_name, libname));
307 if (did_open_here) {
308 dlclose(*libhandle_pointer);
309 *libhandle_pointer = NULL;
311 return false;
314 return true;
317 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
319 static void *libgpfs_handle = NULL;
320 static void *libgpfs_gpl_handle = NULL;
322 if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
323 fn_pointer, fn_name)) {
324 return true;
326 if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
327 fn_pointer, fn_name)) {
328 return true;
330 return false;
333 void init_gpfs(void)
335 init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
336 init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
337 init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
338 init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
339 init_gpfs_function(&gpfs_get_realfilename_path_fn,
340 "gpfs_get_realfilename_path");
341 init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
342 init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
343 init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
344 init_gpfs_function(&gpfs_prealloc_fn, "gpfs_prealloc");
345 init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
346 init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
347 init_gpfs_function(&gpfs_quotactl_fn, "gpfs_quotactl");
348 init_gpfs_function(&gpfs_fcntl_fn, "gpfs_fcntl");
349 init_gpfs_function(&gpfs_getfilesetid_fn, "gpfs_getfilesetid");
351 return;