s3:vfs:gpfs convert sharemodes/leases parameter
[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_getrealfilename;
28 static bool gpfs_winattr;
29
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
40
41 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
42                         uint32 share_access)
43 {
44         unsigned int allow = GPFS_SHARE_NONE;
45         unsigned int deny = GPFS_DENY_NONE;
46         int result;
47
48         if (gpfs_set_share_fn == NULL) {
49                 return False;
50         }
51
52         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
53                 /* No real file, don't disturb */
54                 return True;
55         }
56
57         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
58                                  DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
59         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
60                 GPFS_SHARE_READ : 0;
61
62         if (allow == GPFS_SHARE_NONE) {
63                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
64         }
65         else {  
66                 deny |= (share_access & FILE_SHARE_WRITE) ?
67                         0 : GPFS_DENY_WRITE;
68                 deny |= (share_access & (FILE_SHARE_READ)) ?
69                         0 : GPFS_DENY_READ;
70         }
71         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
72                    access_mask, allow, share_access, deny));
73
74         result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
75         if (result != 0) {
76                 if (errno == ENOSYS) {
77                         DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
78                                   "support has been compiled into Samba. Allowing access\n"));
79                         return True;
80                 } else {
81                         DEBUG(10, ("gpfs_set_share failed: %s\n",
82                                    strerror(errno)));
83                 }
84         }
85
86         return (result == 0);
87 }
88
89 int set_gpfs_lease(int fd, int leasetype)
90 {
91         int gpfs_type = GPFS_LEASE_NONE;
92
93         if (gpfs_set_lease_fn == NULL) {
94                 errno = EINVAL;
95                 return -1;
96         }
97
98         if (leasetype == F_RDLCK) {
99                 gpfs_type = GPFS_LEASE_READ;
100         }
101         if (leasetype == F_WRLCK) {
102                 gpfs_type = GPFS_LEASE_WRITE;
103         }
104
105         /* we unconditionally set CAP_LEASE, rather than looking for
106            -1/EACCES as there is a bug in some versions of
107            libgpfs_gpl.so which results in a leaked fd on /dev/ss0
108            each time we try this with the wrong capabilities set
109         */
110         linux_set_lease_capability();
111         return gpfs_set_lease_fn(fd, gpfs_type);
112 }
113
114 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
115 {
116         if (gpfs_getacl_fn == NULL) {
117                 errno = ENOSYS;
118                 return -1;
119         }
120
121         return gpfs_getacl_fn(pathname, flags, acl);
122 }
123
124 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
125 {
126         if (gpfs_putacl_fn == NULL) {
127                 errno = ENOSYS;
128                 return -1;
129         }
130
131         return gpfs_putacl_fn(pathname, flags, acl);
132 }
133
134 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
135                                     int *buflen)
136 {
137         if ((!gpfs_getrealfilename)
138             || (gpfs_get_realfilename_path_fn == NULL)) {
139                 errno = ENOSYS;
140                 return -1;
141         }
142
143         return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
144 }
145
146 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
147 {
148
149         if ((!gpfs_winattr) || (gpfs_get_winattrs_path_fn == NULL)) {
150                 errno = ENOSYS;
151                 return -1;
152         }
153         DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
154         return gpfs_get_winattrs_path_fn(pathname, attrs);
155 }
156
157 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
158 {
159
160         if ((!gpfs_winattr) || (gpfs_get_winattrs_fn == NULL)) {
161                 errno = ENOSYS;
162                 return -1;
163         }
164         DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
165         return gpfs_get_winattrs_fn(fd, attrs);
166 }
167
168 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
169 {
170         if ((!gpfs_winattr) || (gpfs_set_winattrs_path_fn == NULL)) {
171                 errno = ENOSYS;
172                 return -1;
173         }
174
175         DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
176         return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
177 }
178
179 static bool init_gpfs_function_lib(void *plibhandle_pointer,
180                                    const char *libname,
181                                    void *pfn_pointer, const char *fn_name)
182 {
183         bool did_open_here = false;
184         void **libhandle_pointer = (void **)plibhandle_pointer;
185         void **fn_pointer = (void **)pfn_pointer;
186
187         DEBUG(10, ("trying to load name %s from %s\n",
188                    fn_name, libname));
189
190         if (*libhandle_pointer == NULL) {
191                 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
192                 did_open_here = true;
193         }
194         if (*libhandle_pointer == NULL) {
195                 DEBUG(10, ("Could not open lib %s\n", libname));
196                 return false;
197         }
198
199         *fn_pointer = dlsym(*libhandle_pointer, fn_name);
200         if (*fn_pointer == NULL) {
201                 DEBUG(10, ("Did not find symbol %s in lib %s\n",
202                            fn_name, libname));
203                 if (did_open_here) {
204                         dlclose(*libhandle_pointer);
205                         *libhandle_pointer = NULL;
206                 }
207                 return false;
208         }
209
210         return true;
211 }
212
213 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
214 {
215         static void *libgpfs_handle = NULL;
216         static void *libgpfs_gpl_handle = NULL;
217
218         if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
219                                    fn_pointer, fn_name)) {
220                 return true;
221         }
222         if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
223                                    fn_pointer, fn_name)) {
224                 return true;
225         }
226         return false;
227 }
228
229 void init_gpfs(void)
230 {
231         init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
232         init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
233         init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
234         init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
235         init_gpfs_function(&gpfs_get_realfilename_path_fn,
236                            "gpfs_get_realfilename_path");
237         init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
238         init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
239         init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
240
241
242         gpfs_getrealfilename = lp_parm_bool(-1, "gpfs", "getrealfilename",
243                                             True);
244         gpfs_winattr = lp_parm_bool(-1, "gpfs", "winattr", False);
245
246         return;
247 }
248
249 #else
250
251 int set_gpfs_lease(int snum, int leasetype)
252 {
253         DEBUG(0, ("'VFS module smbgpfs loaded, without gpfs support compiled\n"));
254
255         /* We need to indicate that no GPFS is around by returning ENOSYS, so
256          * that the normal linux kernel oplock code is called. */
257         errno = ENOSYS;
258         return -1;
259 }
260
261 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
262                         uint32 share_access)
263 {
264         DEBUG(0, ("VFS module - smbgpfs.so loaded, without gpfs support compiled\n"));
265         /* Don't disturb but complain */
266         return True;
267 }
268
269 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
270 {
271         errno = ENOSYS;
272         return -1;
273 }
274
275 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
276 {
277         errno = ENOSYS;
278         return -1;
279 }
280
281 int smbd_gpfs_get_realfilename_path(char *pathname, char *fileamep,
282                                     int *buflen)
283 {
284         errno = ENOSYS;
285         return -1;
286 }
287
288 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
289 {
290         errno = ENOSYS;
291         return -1;
292 }
293
294 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
295 {
296         errno = ENOSYS;
297         return -1;
298 }
299
300 void init_gpfs(void)
301 {
302         return;
303 }
304
305 #endif /* HAVE_GPFS */