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