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