Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[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 void *libgpfs_handle = NULL;
28 static bool gpfs_share_modes;
29 static bool gpfs_leases;
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
36
37 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
38                         uint32 share_access)
39 {
40         unsigned int allow = GPFS_SHARE_NONE;
41         unsigned int deny = GPFS_DENY_NONE;
42         int result;
43
44         if (!gpfs_share_modes) {
45                 return True;
46         }
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_leases) {
94                 return True;
95         }
96
97         if (gpfs_set_lease_fn == NULL) {
98                 errno = EINVAL;
99                 return -1;
100         }
101
102         if (leasetype == F_RDLCK) {
103                 gpfs_type = GPFS_LEASE_READ;
104         }
105         if (leasetype == F_WRLCK) {
106                 gpfs_type = GPFS_LEASE_WRITE;
107         }
108         
109         /* we unconditionally set CAP_LEASE, rather than looking for
110            -1/EACCES as there is a bug in some versions of
111            libgpfs_gpl.so which results in a leaked fd on /dev/ss0
112            each time we try this with the wrong capabilities set
113         */
114         linux_set_lease_capability();
115         return gpfs_set_lease_fn(fd, gpfs_type);
116 }
117
118 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
119 {
120         if (gpfs_getacl_fn == NULL) {
121                 errno = ENOSYS;
122                 return -1;
123         }
124
125         return gpfs_getacl_fn(pathname, flags, acl);
126 }
127
128 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
129 {
130         if (gpfs_putacl_fn == NULL) {
131                 errno = ENOSYS;
132                 return -1;
133         }
134
135         return gpfs_putacl_fn(pathname, flags, acl);
136 }
137
138 void init_gpfs(void)
139 {
140         if (libgpfs_handle != NULL) {
141                 return;
142         }
143
144         libgpfs_handle = sys_dlopen("libgpfs_gpl.so", RTLD_LAZY);
145
146         if (libgpfs_handle == NULL) {
147                 DEBUG(10, ("sys_dlopen for libgpfs_gpl failed: %s\n",
148                            strerror(errno)));
149                 return;
150         }
151
152         DEBUG(10, ("libgpfs_gpl.so loaded\n"));
153
154         gpfs_set_share_fn = sys_dlsym(libgpfs_handle, "gpfs_set_share");
155         if (gpfs_set_share_fn == NULL) {
156                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
157                           "'gpfs_set_share'\n"));
158                 goto failed;
159         }
160
161         gpfs_set_lease_fn = sys_dlsym(libgpfs_handle, "gpfs_set_lease");
162         if (gpfs_set_lease_fn == NULL) {
163                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
164                           "'gpfs_set_lease'\n"));
165                 sys_dlclose(libgpfs_handle);
166
167                 goto failed;
168         }
169
170         gpfs_getacl_fn = sys_dlsym(libgpfs_handle, "gpfs_getacl");
171         if (gpfs_getacl_fn == NULL) {
172                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
173                           "'gpfs_getacl'\n"));
174                 goto failed;
175         }
176
177         gpfs_putacl_fn = sys_dlsym(libgpfs_handle, "gpfs_putacl");
178         if (gpfs_putacl_fn == NULL) {
179                 DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
180                           "'gpfs_putacl'\n"));
181                 goto failed;
182         }
183
184         gpfs_share_modes = lp_parm_bool(-1, "gpfs", "sharemodes", True);
185         gpfs_leases      = lp_parm_bool(-1, "gpfs", "leases", True);
186
187         return;
188
189 failed:
190         sys_dlclose(libgpfs_handle);
191         /* leave libgpfs_handle != NULL around, no point
192            in trying twice */
193         gpfs_set_share_fn = NULL;
194         gpfs_set_lease_fn = NULL;
195         gpfs_getacl_fn = NULL;
196         gpfs_putacl_fn = NULL;
197 }
198
199 #else
200
201 int set_gpfs_lease(int snum, int leasetype)
202 {
203         DEBUG(0, ("'VFS module smbgpfs loaded, without gpfs support compiled\n"));
204
205         /* We need to indicate that no GPFS is around by returning ENOSYS, so
206          * that the normal linux kernel oplock code is called. */
207         errno = ENOSYS;
208         return -1;
209 }
210
211 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
212                         uint32 share_access)
213 {
214         DEBUG(0, ("VFS module - smbgpfs.so loaded, without gpfs support compiled\n"));
215         /* Don't disturb but complain */
216         return True;
217 }
218
219 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
220 {
221         errno = ENOSYS;
222         return -1;
223 }
224
225 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
226 {
227         errno = ENOSYS;
228         return -1;
229 }
230
231 void init_gpfs(void)
232 {
233         return;
234 }
235
236 #endif /* HAVE_GPFS */