gpfs: Rename wrapper for gpfs_set_winattrs_path
[Samba.git] / source3 / modules / gpfs.c
blobdbcfa323203a2106d7289a05320f259c4b880596
1 /*
2 * Unix SMB/CIFS implementation.
3 * Provide a connection to GPFS specific features
4 * Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
24 #include <fcntl.h>
25 #include <gpfs_fcntl.h>
26 #include "libcli/security/security.h"
27 #include "vfs_gpfs.h"
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, 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);
48 int gpfswrap_init(void)
50 static void *l;
52 if (l != NULL) {
53 return 0;
56 l = dlopen("libgpfs.so", RTLD_LAZY);
57 if (l == NULL) {
58 return -1;
61 gpfs_set_share_fn = dlsym(l, "gpfs_set_share");
62 gpfs_set_lease_fn = dlsym(l, "gpfs_set_lease");
63 gpfs_getacl_fn = dlsym(l, "gpfs_getacl");
64 gpfs_putacl_fn = dlsym(l, "gpfs_putacl");
65 gpfs_get_realfilename_path_fn = dlsym(l, "gpfs_get_realfilename_path");
66 gpfs_set_winattrs_path_fn = dlsym(l, "gpfs_set_winattrs_path");
67 gpfs_get_winattrs_path_fn = dlsym(l, "gpfs_get_winattrs_path");
68 gpfs_get_winattrs_fn = dlsym(l, "gpfs_get_winattrs");
69 gpfs_prealloc_fn = dlsym(l, "gpfs_prealloc");
70 gpfs_ftruncate_fn = dlsym(l, "gpfs_ftruncate");
71 gpfs_lib_init_fn = dlsym(l, "gpfs_lib_init");
72 gpfs_set_times_path_fn = dlsym(l, "gpfs_set_times_path");
73 gpfs_quotactl_fn = dlsym(l, "gpfs_quotactl");
74 gpfs_fcntl_fn = dlsym(l, "gpfs_fcntl");
75 gpfs_getfilesetid_fn = dlsym(l, "gpfs_getfilesetid");
77 return 0;
80 int gpfswrap_set_share(int fd, unsigned int allow, unsigned int deny)
82 if (gpfs_set_share_fn == NULL) {
83 errno = ENOSYS;
84 return -1;
87 return gpfs_set_share_fn(fd, allow, deny);
90 int gpfswrap_set_lease(int fd, unsigned int type)
92 if (gpfs_set_lease_fn == NULL) {
93 errno = ENOSYS;
94 return -1;
97 return gpfs_set_lease_fn(fd, type);
100 int gpfswrap_getacl(char *pathname, int flags, void *acl)
102 if (gpfs_getacl_fn == NULL) {
103 errno = ENOSYS;
104 return -1;
107 return gpfs_getacl_fn(pathname, flags, acl);
110 int gpfswrap_putacl(char *pathname, int flags, void *acl)
112 if (gpfs_putacl_fn == NULL) {
113 errno = ENOSYS;
114 return -1;
117 return gpfs_putacl_fn(pathname, flags, acl);
120 int gpfswrap_get_realfilename_path(char *pathname, char *filenamep, int *len)
122 if (gpfs_get_realfilename_path_fn == NULL) {
123 errno = ENOSYS;
124 return -1;
127 return gpfs_get_realfilename_path_fn(pathname, filenamep, len);
130 int gpfswrap_set_winattrs_path(char *pathname, int flags,
131 struct gpfs_winattr *attrs)
133 if (gpfs_set_winattrs_path_fn == NULL) {
134 errno = ENOSYS;
135 return -1;
138 return gpfs_set_winattrs_path_fn(pathname, flags, attrs);
141 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
142 uint32 share_access)
144 unsigned int allow = GPFS_SHARE_NONE;
145 unsigned int deny = GPFS_DENY_NONE;
146 int result;
148 if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
149 /* No real file, don't disturb */
150 return True;
153 allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
154 DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
155 allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
156 GPFS_SHARE_READ : 0;
158 if (allow == GPFS_SHARE_NONE) {
159 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
161 else {
162 deny |= (share_access & FILE_SHARE_WRITE) ?
163 0 : GPFS_DENY_WRITE;
164 deny |= (share_access & (FILE_SHARE_READ)) ?
165 0 : GPFS_DENY_READ;
167 DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
168 access_mask, allow, share_access, deny));
170 result = gpfswrap_set_share(fsp->fh->fd, allow, deny);
171 if (result != 0) {
172 if (errno == ENOSYS) {
173 DEBUG(5, ("VFS module vfs_gpfs loaded, but gpfs "
174 "set_share function support not available. "
175 "Allowing access\n"));
176 return True;
177 } else {
178 DEBUG(10, ("gpfs_set_share failed: %s\n",
179 strerror(errno)));
183 return (result == 0);
186 int set_gpfs_lease(int fd, int leasetype)
188 int gpfs_type = GPFS_LEASE_NONE;
190 if (leasetype == F_RDLCK) {
191 gpfs_type = GPFS_LEASE_READ;
193 if (leasetype == F_WRLCK) {
194 gpfs_type = GPFS_LEASE_WRITE;
197 /* we unconditionally set CAP_LEASE, rather than looking for
198 -1/EACCES as there is a bug in some versions of
199 libgpfs_gpl.so which results in a leaked fd on /dev/ss0
200 each time we try this with the wrong capabilities set
202 linux_set_lease_capability();
203 return gpfswrap_set_lease(fd, gpfs_type);
206 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
208 if (gpfs_ftruncate_fn == NULL) {
209 errno = ENOSYS;
210 return -1;
213 return gpfs_ftruncate_fn(fd, length);
216 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
218 if (gpfs_get_winattrs_path_fn == NULL) {
219 errno = ENOSYS;
220 return -1;
222 DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
223 return gpfs_get_winattrs_path_fn(pathname, attrs);
226 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
228 if (gpfs_get_winattrs_fn == NULL) {
229 errno = ENOSYS;
230 return -1;
232 DEBUG(10, ("gpfs_get_winattrs:open call %d\n", fd));
233 return gpfs_get_winattrs_fn(fd, attrs);
236 int smbd_gpfs_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
238 if (gpfs_prealloc_fn == NULL) {
239 errno = ENOSYS;
240 return -1;
243 return gpfs_prealloc_fn(fd, start, bytes);
246 int get_gpfs_quota(const char *pathname, int type, int id,
247 struct gpfs_quotaInfo *qi)
249 int ret;
251 if (!gpfs_quotactl_fn) {
252 errno = ENOSYS;
253 return -1;
256 ZERO_STRUCTP(qi);
257 ret = gpfs_quotactl_fn(discard_const_p(char, pathname),
258 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
260 if (ret) {
261 if (errno == GPFS_E_NO_QUOTA_INST) {
262 DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
263 } else {
264 DEBUG(0, ("Get quota failed, type %d, id, %d, "
265 "errno %d.\n", type, id, errno));
268 return ret;
271 DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
272 type, id, qi->blockUsage, qi->blockHardLimit,
273 qi->blockSoftLimit, qi->blockGraceTime));
275 return ret;
278 int get_gpfs_fset_id(const char *pathname, int *fset_id)
280 int err, fd, errno_fcntl;
282 struct {
283 gpfsFcntlHeader_t hdr;
284 gpfsGetFilesetName_t fsn;
285 } arg;
287 if (!gpfs_fcntl_fn || !gpfs_getfilesetid_fn) {
288 errno = ENOSYS;
289 return -1;
292 arg.hdr.totalLength = sizeof(arg);
293 arg.hdr.fcntlVersion = GPFS_FCNTL_CURRENT_VERSION;
294 arg.hdr.fcntlReserved = 0;
295 arg.fsn.structLen = sizeof(arg.fsn);
296 arg.fsn.structType = GPFS_FCNTL_GET_FILESETNAME;
298 fd = open(pathname, O_RDONLY);
299 if (fd == -1) {
300 DEBUG(1, ("Could not open %s: %s\n",
301 pathname, strerror(errno)));
302 return fd;
305 err = gpfs_fcntl_fn(fd, &arg);
306 errno_fcntl = errno;
307 close(fd);
309 if (err) {
310 errno = errno_fcntl;
311 DEBUG(1, ("GPFS_FCNTL_GET_FILESETNAME for %s failed: %s\n",
312 pathname, strerror(errno)));
313 return err;
316 err = gpfs_getfilesetid_fn(discard_const_p(char, pathname),
317 arg.fsn.buffer, fset_id);
318 if (err) {
319 DEBUG(1, ("gpfs_getfilesetid for %s failed: %s\n",
320 pathname, strerror(errno)));
322 return err;
325 void smbd_gpfs_lib_init()
327 if (gpfs_lib_init_fn) {
328 int rc = gpfs_lib_init_fn(0);
329 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
330 "and errno %d\n", rc, errno));
331 } else {
332 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
336 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
337 int idx, int *flags)
339 if (!null_timespec(ts)) {
340 *flags |= 1 << idx;
341 gt[idx].tv_sec = ts.tv_sec;
342 gt[idx].tv_nsec = ts.tv_nsec;
343 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
347 int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
349 gpfs_timestruc_t gpfs_times[4];
350 int flags = 0;
351 int rc;
353 if (!gpfs_set_times_path_fn) {
354 errno = ENOSYS;
355 return -1;
358 ZERO_ARRAY(gpfs_times);
359 timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
360 timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
361 /* No good mapping from LastChangeTime to ctime, not storing */
362 timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
364 if (!flags) {
365 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
366 return 0;
369 rc = gpfs_set_times_path_fn(path, flags, gpfs_times);
371 if (rc != 0) {
372 DEBUG(1,("gpfs_set_times() returned with error %s\n",
373 strerror(errno)));
376 return rc;