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