Merge branch 'master' of git://git.samba.org/samba into minschema
[amitay/samba.git] / source3 / modules / gpfs.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Provide a connection to GPFS specific features
4  *  Copyright (C) Volker Lendecke 2005
5  *
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.
10  *  
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.
15  *  
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/>.
18  */
19
20 #include "includes.h"
21
22 #ifdef HAVE_GPFS
23
24 #include "gpfs_gpl.h"
25 #include "vfs_gpfs.h"
26
27 static bool gpfs_share_modes;
28 static bool gpfs_leases;
29 static bool gpfs_getrealfilename;
30
31 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
32 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
33 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
34 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
35 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
36                                             int *buflen);
37
38
39 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
40                         uint32 share_access)
41 {
42         unsigned int allow = GPFS_SHARE_NONE;
43         unsigned int deny = GPFS_DENY_NONE;
44         int result;
45
46         if (!gpfs_share_modes) {
47                 return True;
48         }
49
50         if (gpfs_set_share_fn == NULL) {
51                 return False;
52         }
53
54         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
55                 /* No real file, don't disturb */
56                 return True;
57         }
58
59         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
60                                  DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
61         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
62                 GPFS_SHARE_READ : 0;
63
64         if (allow == GPFS_SHARE_NONE) {
65                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
66         }
67         else {  
68                 deny |= (share_access & FILE_SHARE_WRITE) ?
69                         0 : GPFS_DENY_WRITE;
70                 deny |= (share_access & (FILE_SHARE_READ)) ?
71                         0 : GPFS_DENY_READ;
72         }
73         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
74                    access_mask, allow, share_access, deny));
75
76         result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
77         if (result != 0) {
78                 if (errno == ENOSYS) {
79                         DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
80                                   "support has been compiled into Samba. Allowing access\n"));
81                         return True;
82                 } else {
83                         DEBUG(10, ("gpfs_set_share failed: %s\n",
84                                    strerror(errno)));
85                 }
86         }
87
88         return (result == 0);
89 }
90
91 int set_gpfs_lease(int fd, int leasetype)
92 {
93         int gpfs_type = GPFS_LEASE_NONE;
94
95         if (!gpfs_leases) {
96                 return True;
97         }
98
99         if (gpfs_set_lease_fn == NULL) {
100                 errno = EINVAL;
101                 return -1;
102         }
103
104         if (leasetype == F_RDLCK) {
105                 gpfs_type = GPFS_LEASE_READ;
106         }
107         if (leasetype == F_WRLCK) {
108                 gpfs_type = GPFS_LEASE_WRITE;
109         }
110
111         /* we unconditionally set CAP_LEASE, rather than looking for
112            -1/EACCES as there is a bug in some versions of
113            libgpfs_gpl.so which results in a leaked fd on /dev/ss0
114            each time we try this with the wrong capabilities set
115         */
116         linux_set_lease_capability();
117         return gpfs_set_lease_fn(fd, gpfs_type);
118 }
119
120 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
121 {
122         if (gpfs_getacl_fn == NULL) {
123                 errno = ENOSYS;
124                 return -1;
125         }
126
127         return gpfs_getacl_fn(pathname, flags, acl);
128 }
129
130 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
131 {
132         if (gpfs_putacl_fn == NULL) {
133                 errno = ENOSYS;
134                 return -1;
135         }
136
137         return gpfs_putacl_fn(pathname, flags, acl);
138 }
139
140 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
141                                     int *buflen)
142 {
143         if ((!gpfs_getrealfilename)
144             || (gpfs_get_realfilename_path_fn == NULL)) {
145                 errno = ENOSYS;
146                 return -1;
147         }
148
149         return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
150 }
151
152 static bool init_gpfs_function_lib(void *plibhandle_pointer,
153                                    const char *libname,
154                                    void *pfn_pointer, const char *fn_name)
155 {
156         bool did_open_here = false;
157         void **libhandle_pointer = (void **)plibhandle_pointer;
158         void **fn_pointer = (void **)pfn_pointer;
159
160         DEBUG(10, ("trying to load name %s from %s\n",
161                    fn_name, libname));
162
163         if (*libhandle_pointer == NULL) {
164                 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
165                 did_open_here = true;
166         }
167         if (*libhandle_pointer == NULL) {
168                 DEBUG(10, ("Could not open lib %s\n", libname));
169                 return false;
170         }
171
172         *fn_pointer = dlsym(*libhandle_pointer, fn_name);
173         if (*fn_pointer == NULL) {
174                 DEBUG(10, ("Did not find symbol %s in lib %s\n",
175                            fn_name, libname));
176                 if (did_open_here) {
177                         dlclose(*libhandle_pointer);
178                         *libhandle_pointer = NULL;
179                 }
180                 return false;
181         }
182
183         return true;
184 }
185
186 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
187 {
188         static void *libgpfs_handle = NULL;
189         static void *libgpfs_gpl_handle = NULL;
190
191         if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
192                                    fn_pointer, fn_name)) {
193                 return true;
194         }
195         if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
196                                    fn_pointer, fn_name)) {
197                 return true;
198         }
199         return false;
200 }
201
202 void init_gpfs(void)
203 {
204         init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
205         init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
206         init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
207         init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
208         init_gpfs_function(&gpfs_get_realfilename_path_fn,
209                            "gpfs_get_realfilename_path");
210
211         gpfs_share_modes = lp_parm_bool(-1, "gpfs", "sharemodes", True);
212         gpfs_leases      = lp_parm_bool(-1, "gpfs", "leases", True);
213         gpfs_getrealfilename = lp_parm_bool(-1, "gpfs", "getrealfilename",
214                                             True);
215
216         return;
217 }
218
219 #else
220
221 int set_gpfs_lease(int snum, int leasetype)
222 {
223         DEBUG(0, ("'VFS module smbgpfs loaded, without gpfs support compiled\n"));
224
225         /* We need to indicate that no GPFS is around by returning ENOSYS, so
226          * that the normal linux kernel oplock code is called. */
227         errno = ENOSYS;
228         return -1;
229 }
230
231 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
232                         uint32 share_access)
233 {
234         DEBUG(0, ("VFS module - smbgpfs.so loaded, without gpfs support compiled\n"));
235         /* Don't disturb but complain */
236         return True;
237 }
238
239 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
240 {
241         errno = ENOSYS;
242         return -1;
243 }
244
245 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
246 {
247         errno = ENOSYS;
248         return -1;
249 }
250
251 int smbd_gpfs_get_realfilename_path(char *pathname, char *fileamep,
252                                     int *buflen)
253 {
254         errno = ENOSYS;
255         return -1;
256 }
257
258 void init_gpfs(void)
259 {
260         return;
261 }
262
263 #endif /* HAVE_GPFS */