s3: Avoid oplock break by storing timestamps with gpfs_set_times
[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 <fcntl.h>
25 #include "libcli/security/security.h"
26 #include "gpfs_fcntl.h"
27 #include "gpfs_gpl.h"
28 #include "vfs_gpfs.h"
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 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
37 static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
38 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
39 static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t startOffset, gpfs_off64_t bytesToPrealloc);
40 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
41 static int (*gpfs_lib_init_fn)(int flags);
42 static int (*gpfs_set_times_path_fn)(char *pathname, int flags,
43                                      gpfs_timestruc_t times[4]);
44 static int (*gpfs_quotactl_fn)(char *pathname, int cmd, int id, void *bufferP);
45 static int (*gpfs_fcntl_fn)(gpfs_file_t fileDesc, void *fcntlArgP);
46 static int (*gpfs_getfilesetid_fn)(char *pathname, char *name, int *idP);
47
48 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
49                         uint32 share_access)
50 {
51         unsigned int allow = GPFS_SHARE_NONE;
52         unsigned int deny = GPFS_DENY_NONE;
53         int result;
54
55         if (gpfs_set_share_fn == NULL) {
56                 return False;
57         }
58
59         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
60                 /* No real file, don't disturb */
61                 return True;
62         }
63
64         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
65                                  DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
66         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
67                 GPFS_SHARE_READ : 0;
68
69         if (allow == GPFS_SHARE_NONE) {
70                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
71         }
72         else {  
73                 deny |= (share_access & FILE_SHARE_WRITE) ?
74                         0 : GPFS_DENY_WRITE;
75                 deny |= (share_access & (FILE_SHARE_READ)) ?
76                         0 : GPFS_DENY_READ;
77         }
78         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
79                    access_mask, allow, share_access, deny));
80
81         result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
82         if (result != 0) {
83                 if (errno == ENOSYS) {
84                         DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
85                                   "support has been compiled into Samba. Allowing access\n"));
86                         return True;
87                 } else {
88                         DEBUG(10, ("gpfs_set_share failed: %s\n",
89                                    strerror(errno)));
90                 }
91         }
92
93         return (result == 0);
94 }
95
96 int set_gpfs_lease(int fd, int leasetype)
97 {
98         int gpfs_type = GPFS_LEASE_NONE;
99
100         if (gpfs_set_lease_fn == NULL) {
101                 errno = EINVAL;
102                 return -1;
103         }
104
105         if (leasetype == F_RDLCK) {
106                 gpfs_type = GPFS_LEASE_READ;
107         }
108         if (leasetype == F_WRLCK) {
109                 gpfs_type = GPFS_LEASE_WRITE;
110         }
111
112         /* we unconditionally set CAP_LEASE, rather than looking for
113            -1/EACCES as there is a bug in some versions of
114            libgpfs_gpl.so which results in a leaked fd on /dev/ss0
115            each time we try this with the wrong capabilities set
116         */
117         linux_set_lease_capability();
118         return gpfs_set_lease_fn(fd, gpfs_type);
119 }
120
121 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
122 {
123         if (gpfs_getacl_fn == NULL) {
124                 errno = ENOSYS;
125                 return -1;
126         }
127
128         return gpfs_getacl_fn(pathname, flags, acl);
129 }
130
131 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
132 {
133         if (gpfs_putacl_fn == NULL) {
134                 errno = ENOSYS;
135                 return -1;
136         }
137
138         return gpfs_putacl_fn(pathname, flags, acl);
139 }
140
141 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
142 {
143         if (gpfs_ftruncate_fn == NULL) {
144                 errno = ENOSYS;
145                 return -1;
146         }
147
148         return gpfs_ftruncate_fn(fd, length);
149 }
150
151 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
152                                     int *buflen)
153 {
154         if (gpfs_get_realfilename_path_fn == NULL) {
155                 errno = ENOSYS;
156                 return -1;
157         }
158
159         return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
160 }
161
162 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
163 {
164
165         if (gpfs_get_winattrs_path_fn == NULL) {
166                 errno = ENOSYS;
167                 return -1;
168         }
169         DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
170         return gpfs_get_winattrs_path_fn(pathname, attrs);
171 }
172
173 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
174 {
175
176         if (gpfs_get_winattrs_fn == NULL) {
177                 errno = ENOSYS;
178                 return -1;
179         }
180         DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
181         return gpfs_get_winattrs_fn(fd, attrs);
182 }
183
184 int smbd_gpfs_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
185 {
186         if (gpfs_prealloc_fn == NULL) {
187                 errno = ENOSYS;
188                 return -1;
189         }
190
191         return gpfs_prealloc_fn(fd, start, bytes);
192 }
193
194 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
195 {
196         if (gpfs_set_winattrs_path_fn == NULL) {
197                 errno = ENOSYS;
198                 return -1;
199         }
200
201         DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
202         return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
203 }
204
205 int get_gpfs_quota(const char *pathname, int type, int id,
206                    struct gpfs_quotaInfo *qi)
207 {
208         int ret;
209
210         if (!gpfs_quotactl_fn) {
211                 errno = ENOSYS;
212                 return -1;
213         }
214
215         ZERO_STRUCTP(qi);
216         ret = gpfs_quotactl_fn(discard_const_p(char, pathname),
217                                GPFS_QCMD(Q_GETQUOTA, type), id, qi);
218
219         if (ret) {
220                 if (errno == GPFS_E_NO_QUOTA_INST) {
221                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
222                 } else {
223                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
224                                   "errno %d.\n", type, id, errno));
225                 }
226
227                 return ret;
228         }
229
230         DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
231                    type, id, qi->blockUsage, qi->blockHardLimit,
232                    qi->blockSoftLimit, qi->blockGraceTime));
233
234         return ret;
235 }
236
237 int get_gpfs_fset_id(const char *pathname, int *fset_id)
238 {
239         int err, fd, errno_fcntl;
240
241         struct {
242                 gpfsFcntlHeader_t hdr;
243                 gpfsGetFilesetName_t fsn;
244         } arg;
245
246         if (!gpfs_fcntl_fn || !gpfs_getfilesetid_fn) {
247                 errno = ENOSYS;
248                 return -1;
249         }
250
251         arg.hdr.totalLength = sizeof(arg);
252         arg.hdr.fcntlVersion = GPFS_FCNTL_CURRENT_VERSION;
253         arg.hdr.fcntlReserved = 0;
254         arg.fsn.structLen = sizeof(arg.fsn);
255         arg.fsn.structType = GPFS_FCNTL_GET_FILESETNAME;
256
257         fd = open(pathname, O_RDONLY);
258         if (fd == -1) {
259                 DEBUG(1, ("Could not open %s: %s\n",
260                           pathname, strerror(errno)));
261                 return fd;
262         }
263
264         err = gpfs_fcntl_fn(fd, &arg);
265         errno_fcntl = errno;
266         close(fd);
267
268         if (err) {
269                 errno = errno_fcntl;
270                 DEBUG(1, ("GPFS_FCNTL_GET_FILESETNAME for %s failed: %s\n",
271                           pathname, strerror(errno)));
272                 return err;
273         }
274
275         err = gpfs_getfilesetid_fn(discard_const_p(char, pathname),
276                                    arg.fsn.buffer, fset_id);
277         if (err) {
278                 DEBUG(1, ("gpfs_getfilesetid for %s failed: %s\n",
279                           pathname, strerror(errno)));
280         }
281         return err;
282 }
283
284 void smbd_gpfs_lib_init()
285 {
286         if (gpfs_lib_init_fn) {
287                 int rc = gpfs_lib_init_fn(0);
288                 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
289                            "and errno %d\n", rc, errno));
290         } else {
291                 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
292         }
293 }
294
295 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
296                                   int idx, int *flags)
297 {
298         if (!null_timespec(ts)) {
299                 *flags |= 1 << idx;
300                 gt[idx].tv_sec = ts.tv_sec;
301                 gt[idx].tv_nsec = ts.tv_nsec;
302                 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
303         }
304 }
305
306 int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
307 {
308         gpfs_timestruc_t gpfs_times[4];
309         int flags = 0;
310         int rc;
311
312         if (!gpfs_set_times_path_fn) {
313                 errno = ENOSYS;
314                 return -1;
315         }
316
317         ZERO_ARRAY(gpfs_times);
318         timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
319         timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
320         /* No good mapping from LastChangeTime to ctime, not storing */
321         timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
322
323         if (!flags) {
324                 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
325                 return 0;
326         }
327
328         rc = gpfs_set_times_path_fn(path, flags, gpfs_times);
329
330         if (rc != 0) {
331                 DEBUG(1,("gpfs_set_times() returned with error %s\n",
332                         strerror(errno)));
333         }
334
335         return rc;
336 }
337
338 static bool init_gpfs_function_lib(void *plibhandle_pointer,
339                                    const char *libname,
340                                    void *pfn_pointer, const char *fn_name)
341 {
342         bool did_open_here = false;
343         void **libhandle_pointer = (void **)plibhandle_pointer;
344         void **fn_pointer = (void **)pfn_pointer;
345
346         DEBUG(10, ("trying to load name %s from %s\n",
347                    fn_name, libname));
348
349         if (*libhandle_pointer == NULL) {
350                 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
351                 did_open_here = true;
352         }
353         if (*libhandle_pointer == NULL) {
354                 DEBUG(10, ("Could not open lib %s\n", libname));
355                 return false;
356         }
357
358         *fn_pointer = dlsym(*libhandle_pointer, fn_name);
359         if (*fn_pointer == NULL) {
360                 DEBUG(10, ("Did not find symbol %s in lib %s\n",
361                            fn_name, libname));
362                 if (did_open_here) {
363                         dlclose(*libhandle_pointer);
364                         *libhandle_pointer = NULL;
365                 }
366                 return false;
367         }
368
369         return true;
370 }
371
372 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
373 {
374         static void *libgpfs_handle = NULL;
375         static void *libgpfs_gpl_handle = NULL;
376
377         if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
378                                    fn_pointer, fn_name)) {
379                 return true;
380         }
381         if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
382                                    fn_pointer, fn_name)) {
383                 return true;
384         }
385         return false;
386 }
387
388 void init_gpfs(void)
389 {
390         init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
391         init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
392         init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
393         init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
394         init_gpfs_function(&gpfs_get_realfilename_path_fn,
395                            "gpfs_get_realfilename_path");
396         init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
397         init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
398         init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
399         init_gpfs_function(&gpfs_prealloc_fn, "gpfs_prealloc");
400         init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
401         init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
402         init_gpfs_function(&gpfs_set_times_path_fn, "gpfs_set_times_path");
403         init_gpfs_function(&gpfs_quotactl_fn, "gpfs_quotactl");
404         init_gpfs_function(&gpfs_fcntl_fn, "gpfs_fcntl");
405         init_gpfs_function(&gpfs_getfilesetid_fn, "gpfs_getfilesetid");
406
407         return;
408 }