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