s3:vfs_gpfs: Implement fallocate callback for GPFS
[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 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23
24 #include "libcli/security/security.h"
25 #include "gpfs_fcntl.h"
26 #include "gpfs_gpl.h"
27 #include "vfs_gpfs.h"
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 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
34                                             int *buflen);
35 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
36 static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
37 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
38 static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t startOffset, gpfs_off64_t bytesToPrealloc);
39 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
40 static int (*gpfs_lib_init_fn)(int flags);
41 static int (*gpfs_quotactl_fn)(char *pathname, int cmd, int id, void *bufferP);
42 static int (*gpfs_fcntl_fn)(gpfs_file_t fileDesc, void *fcntlArgP);
43 static int (*gpfs_getfilesetid_fn)(char *pathname, char *name, int *idP);
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_set_share_fn == NULL) {
53                 return False;
54         }
55
56         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
57                 /* No real file, don't disturb */
58                 return True;
59         }
60
61         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
62                                  DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
63         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
64                 GPFS_SHARE_READ : 0;
65
66         if (allow == GPFS_SHARE_NONE) {
67                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
68         }
69         else {  
70                 deny |= (share_access & FILE_SHARE_WRITE) ?
71                         0 : GPFS_DENY_WRITE;
72                 deny |= (share_access & (FILE_SHARE_READ)) ?
73                         0 : GPFS_DENY_READ;
74         }
75         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
76                    access_mask, allow, share_access, deny));
77
78         result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
79         if (result != 0) {
80                 if (errno == ENOSYS) {
81                         DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
82                                   "support has been compiled into Samba. Allowing access\n"));
83                         return True;
84                 } else {
85                         DEBUG(10, ("gpfs_set_share failed: %s\n",
86                                    strerror(errno)));
87                 }
88         }
89
90         return (result == 0);
91 }
92
93 int set_gpfs_lease(int fd, int leasetype)
94 {
95         int gpfs_type = GPFS_LEASE_NONE;
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 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
139 {
140         if (gpfs_ftruncate_fn == NULL) {
141                 errno = ENOSYS;
142                 return -1;
143         }
144
145         return gpfs_ftruncate_fn(fd, length);
146 }
147
148 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
149                                     int *buflen)
150 {
151         if (gpfs_get_realfilename_path_fn == NULL) {
152                 errno = ENOSYS;
153                 return -1;
154         }
155
156         return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
157 }
158
159 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
160 {
161
162         if (gpfs_get_winattrs_path_fn == NULL) {
163                 errno = ENOSYS;
164                 return -1;
165         }
166         DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
167         return gpfs_get_winattrs_path_fn(pathname, attrs);
168 }
169
170 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
171 {
172
173         if (gpfs_get_winattrs_fn == NULL) {
174                 errno = ENOSYS;
175                 return -1;
176         }
177         DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
178         return gpfs_get_winattrs_fn(fd, attrs);
179 }
180
181 int smbd_gpfs_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
182 {
183         if (gpfs_prealloc_fn == NULL) {
184                 errno = ENOSYS;
185                 return -1;
186         }
187
188         return gpfs_prealloc_fn(fd, start, bytes);
189 }
190
191 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
192 {
193         if (gpfs_set_winattrs_path_fn == NULL) {
194                 errno = ENOSYS;
195                 return -1;
196         }
197
198         DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
199         return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
200 }
201
202 int get_gpfs_quota(const char *pathname, int type, int id,
203                    struct gpfs_quotaInfo *qi)
204 {
205         int ret;
206
207         if (!gpfs_quotactl_fn) {
208                 errno = ENOSYS;
209                 return -1;
210         }
211
212         ZERO_STRUCTP(qi);
213         ret = gpfs_quotactl_fn(discard_const_p(char, pathname),
214                                GPFS_QCMD(Q_GETQUOTA, type), id, qi);
215
216         if (ret) {
217                 if (errno == GPFS_E_NO_QUOTA_INST) {
218                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
219                 } else {
220                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
221                                   "errno %d.\n", type, id, errno));
222                 }
223
224                 return ret;
225         }
226
227         DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
228                    type, id, qi->blockUsage, qi->blockHardLimit,
229                    qi->blockSoftLimit, qi->blockGraceTime));
230
231         return ret;
232 }
233
234 int get_gpfs_fset_id(const char *pathname, int *fset_id)
235 {
236         int err, fd, errno_fcntl;
237
238         struct {
239                 gpfsFcntlHeader_t hdr;
240                 gpfsGetFilesetName_t fsn;
241         } arg;
242
243         if (!gpfs_fcntl_fn || !gpfs_getfilesetid_fn) {
244                 errno = ENOSYS;
245                 return -1;
246         }
247
248         arg.hdr.totalLength = sizeof(arg);
249         arg.hdr.fcntlVersion = GPFS_FCNTL_CURRENT_VERSION;
250         arg.hdr.fcntlReserved = 0;
251         arg.fsn.structLen = sizeof(arg.fsn);
252         arg.fsn.structType = GPFS_FCNTL_GET_FILESETNAME;
253
254         fd = open(pathname, O_RDONLY);
255         if (fd == -1)
256                 return fd;
257
258         err = gpfs_fcntl_fn(fd, &arg);
259         errno_fcntl = errno;
260         close(fd);
261
262         if (err) {
263                 errno = errno_fcntl;
264                 return err;
265         }
266
267         return gpfs_getfilesetid_fn(discard_const_p(char, pathname),
268                                     arg.fsn.buffer, fset_id);
269 }
270
271 void smbd_gpfs_lib_init()
272 {
273         if (gpfs_lib_init_fn) {
274                 int rc = gpfs_lib_init_fn(0);
275                 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
276                            "and errno %d\n", rc, errno));
277         } else {
278                 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
279         }
280 }
281
282 static bool init_gpfs_function_lib(void *plibhandle_pointer,
283                                    const char *libname,
284                                    void *pfn_pointer, const char *fn_name)
285 {
286         bool did_open_here = false;
287         void **libhandle_pointer = (void **)plibhandle_pointer;
288         void **fn_pointer = (void **)pfn_pointer;
289
290         DEBUG(10, ("trying to load name %s from %s\n",
291                    fn_name, libname));
292
293         if (*libhandle_pointer == NULL) {
294                 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
295                 did_open_here = true;
296         }
297         if (*libhandle_pointer == NULL) {
298                 DEBUG(10, ("Could not open lib %s\n", libname));
299                 return false;
300         }
301
302         *fn_pointer = dlsym(*libhandle_pointer, fn_name);
303         if (*fn_pointer == NULL) {
304                 DEBUG(10, ("Did not find symbol %s in lib %s\n",
305                            fn_name, libname));
306                 if (did_open_here) {
307                         dlclose(*libhandle_pointer);
308                         *libhandle_pointer = NULL;
309                 }
310                 return false;
311         }
312
313         return true;
314 }
315
316 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
317 {
318         static void *libgpfs_handle = NULL;
319         static void *libgpfs_gpl_handle = NULL;
320
321         if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
322                                    fn_pointer, fn_name)) {
323                 return true;
324         }
325         if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
326                                    fn_pointer, fn_name)) {
327                 return true;
328         }
329         return false;
330 }
331
332 void init_gpfs(void)
333 {
334         init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
335         init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
336         init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
337         init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
338         init_gpfs_function(&gpfs_get_realfilename_path_fn,
339                            "gpfs_get_realfilename_path");
340         init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
341         init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
342         init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
343         init_gpfs_function(&gpfs_prealloc_fn, "gpfs_prealloc");
344         init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
345         init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
346         init_gpfs_function(&gpfs_quotactl_fn, "gpfs_quotactl");
347         init_gpfs_function(&gpfs_fcntl_fn, "gpfs_fcntl");
348         init_gpfs_function(&gpfs_getfilesetid_fn, "gpfs_getfilesetid");
349
350         return;
351 }