Merge lsarpc.idl from samba4 and rerun make idl.
[ira/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
26 static void *libgpfs_handle = NULL;
27 static bool gpfs_share_modes;
28
29 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
30 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
31 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
32 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
33
34
35 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
36                         uint32 share_access)
37 {
38         unsigned int allow = GPFS_SHARE_NONE;
39         unsigned int deny = GPFS_DENY_NONE;
40         int result;
41
42         if (!gpfs_share_modes) {
43                 return True;
44         }
45
46         if (gpfs_set_share_fn == NULL) {
47                 return False;
48         }
49
50         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
51                 /* No real file, don't disturb */
52                 return True;
53         }
54
55         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
56                                  DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
57         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
58                 GPFS_SHARE_READ : 0;
59
60         if (allow == GPFS_SHARE_NONE) {
61                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
62         }
63         else {  
64                 deny |= (share_access & FILE_SHARE_WRITE) ?
65                         0 : GPFS_DENY_WRITE;
66                 deny |= (share_access & (FILE_SHARE_READ)) ?
67                         0 : GPFS_DENY_READ;
68         }
69         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
70                    access_mask, allow, share_access, deny));
71
72         result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
73         if (result != 0) {
74                 if (errno == ENOSYS) {
75                         DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
76                                   "support has been compiled into Samba. Allowing access\n"));
77                         return True;
78                 } else {
79                         DEBUG(10, ("gpfs_set_share failed: %s\n",
80                                    strerror(errno)));
81                 }
82         }
83
84         return (result == 0);
85 }
86
87 int set_gpfs_lease(int fd, int leasetype)
88 {
89         int gpfs_type = GPFS_LEASE_NONE;
90
91         if (!gpfs_share_modes) {
92                 return True;
93         }
94
95         if (gpfs_set_lease_fn == NULL) {
96                 errno = EINVAL;
97                 return -1;
98         }
99
100         if (leasetype == F_RDLCK) {
101                 gpfs_type = GPFS_LEASE_READ;
102         }
103         if (leasetype == F_WRLCK) {
104                 gpfs_type = GPFS_LEASE_WRITE;
105         }
106         return gpfs_set_lease_fn(fd, gpfs_type);
107 }
108
109 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
110 {
111         if (gpfs_getacl_fn == NULL) {
112                 errno = ENOSYS;
113                 return -1;
114         }
115
116         return gpfs_getacl_fn(pathname, flags, acl);
117 }
118
119 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
120 {
121         if (gpfs_putacl_fn == NULL) {
122                 errno = ENOSYS;
123                 return -1;
124         }
125
126         return gpfs_putacl_fn(pathname, flags, acl);
127 }
128
129 void init_gpfs(void)
130 {
131         if (libgpfs_handle != NULL) {
132                 return;
133         }
134
135         libgpfs_handle = sys_dlopen("libgpfs_gpl.so", RTLD_LAZY);
136
137         if (libgpfs_handle == NULL) {
138                 DEBUG(10, ("sys_dlopen for libgpfs_gpl failed: %s\n",
139                            strerror(errno)));
140                 return;
141         }
142
143         DEBUG(10, ("libgpfs_gpl.so loaded\n"));
144
145         gpfs_set_share_fn = sys_dlsym(libgpfs_handle, "gpfs_set_share");
146         if (gpfs_set_share_fn == NULL) {
147                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
148                           "'gpfs_set_share'\n"));
149                 goto failed;
150         }
151
152         gpfs_set_lease_fn = sys_dlsym(libgpfs_handle, "gpfs_set_lease");
153         if (gpfs_set_lease_fn == NULL) {
154                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
155                           "'gpfs_set_lease'\n"));
156                 sys_dlclose(libgpfs_handle);
157
158                 goto failed;
159         }
160
161         gpfs_getacl_fn = sys_dlsym(libgpfs_handle, "gpfs_getacl");
162         if (gpfs_getacl_fn == NULL) {
163                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
164                           "'gpfs_getacl'\n"));
165                 goto failed;
166         }
167
168         gpfs_putacl_fn = sys_dlsym(libgpfs_handle, "gpfs_putacl");
169         if (gpfs_putacl_fn == NULL) {
170                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
171                           "'gpfs_putacl'\n"));
172                 goto failed;
173         }
174
175         if (lp_parm_bool(-1, "gpfs", "sharemodes", True)) {
176                 gpfs_share_modes = True;
177         } else {
178                 gpfs_share_modes = False;
179         }
180
181         return;
182
183 failed:
184         sys_dlclose(libgpfs_handle);
185         /* leave libgpfs_handle != NULL around, no point
186            in trying twice */
187         gpfs_set_share_fn = NULL;
188         gpfs_set_lease_fn = NULL;
189         gpfs_getacl_fn = NULL;
190         gpfs_putacl_fn = NULL;
191 }
192
193 #else
194
195 int set_gpfs_lease(int snum, int leasetype)
196 {
197         DEBUG(0, ("'VFS module smbgpfs loaded, without gpfs support compiled\n"));
198
199         /* We need to indicate that no GPFS is around by returning ENOSYS, so
200          * that the normal linux kernel oplock code is called. */
201         errno = ENOSYS;
202         return -1;
203 }
204
205 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
206                         uint32 share_access)
207 {
208         DEBUG(0, ("VFS module - smbgpfs.so loaded, without gpfs support compiled\n"));
209         /* Don't disturb but complain */
210         return True;
211 }
212
213 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
214 {
215         errno = ENOSYS;
216         return -1;
217 }
218
219 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
220 {
221         errno = ENOSYS;
222         return -1;
223 }
224
225 void init_gpfs(void)
226 {
227         return;
228 }
229
230 #endif /* HAVE_GPFS */