summaryrefslogtreecommitdiff
path: root/source3/modules/gpfs.c
blob: 585c522f641e9bb7f0060db3e56826dc28165f0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 *  Unix SMB/CIFS implementation.
 *  Provide a connection to GPFS specific features
 *  Copyright (C) Volker Lendecke 2005
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "system/filesys.h"
#include "smbd/smbd.h"

#include <fcntl.h>
#include <gpfs_fcntl.h>
#include "vfs_gpfs.h"

static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
static int (*gpfs_set_lease_fn)(int fd, unsigned int type);
static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
					    int *len);
static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags,
					struct gpfs_winattr *attrs);
static int (*gpfs_get_winattrs_path_fn)(char *pathname,
					struct gpfs_winattr *attrs);
static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t start, gpfs_off64_t bytes);
static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
static int (*gpfs_lib_init_fn)(int flags);
static int (*gpfs_set_times_path_fn)(char *pathname, int flags,
				     gpfs_timestruc_t times[4]);
static int (*gpfs_quotactl_fn)(char *pathname, int cmd, int id, void *bufp);
static int (*gpfs_fcntl_fn)(int fd, void *argp);
static int (*gpfs_getfilesetid_fn)(char *pathname, char *name, int *idp);

int gpfswrap_init(void)
{
	static void *l;

	if (l != NULL) {
		return 0;
	}

	l = dlopen("libgpfs.so", RTLD_LAZY);
	if (l == NULL) {
		return -1;
	}

	gpfs_set_share_fn	      = dlsym(l, "gpfs_set_share");
	gpfs_set_lease_fn	      = dlsym(l, "gpfs_set_lease");
	gpfs_getacl_fn		      = dlsym(l, "gpfs_getacl");
	gpfs_putacl_fn		      = dlsym(l, "gpfs_putacl");
	gpfs_get_realfilename_path_fn = dlsym(l, "gpfs_get_realfilename_path");
	gpfs_set_winattrs_path_fn     = dlsym(l, "gpfs_set_winattrs_path");
	gpfs_get_winattrs_path_fn     = dlsym(l, "gpfs_get_winattrs_path");
	gpfs_get_winattrs_fn	      = dlsym(l, "gpfs_get_winattrs");
	gpfs_prealloc_fn	      = dlsym(l, "gpfs_prealloc");
	gpfs_ftruncate_fn	      = dlsym(l, "gpfs_ftruncate");
	gpfs_lib_init_fn	      = dlsym(l, "gpfs_lib_init");
	gpfs_set_times_path_fn	      = dlsym(l, "gpfs_set_times_path");
	gpfs_quotactl_fn	      = dlsym(l, "gpfs_quotactl");
	gpfs_fcntl_fn		      = dlsym(l, "gpfs_fcntl");
	gpfs_getfilesetid_fn	      = dlsym(l, "gpfs_getfilesetid");

	return 0;
}

int gpfswrap_set_share(int fd, unsigned int allow, unsigned int deny)
{
	if (gpfs_set_share_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_set_share_fn(fd, allow, deny);
}

int gpfswrap_set_lease(int fd, unsigned int type)
{
	if (gpfs_set_lease_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_set_lease_fn(fd, type);
}

int gpfswrap_getacl(char *pathname, int flags, void *acl)
{
	if (gpfs_getacl_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_getacl_fn(pathname, flags, acl);
}

int gpfswrap_putacl(char *pathname, int flags, void *acl)
{
	if (gpfs_putacl_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_putacl_fn(pathname, flags, acl);
}

int gpfswrap_get_realfilename_path(char *pathname, char *filenamep, int *len)
{
	if (gpfs_get_realfilename_path_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_get_realfilename_path_fn(pathname, filenamep, len);
}

int gpfswrap_set_winattrs_path(char *pathname, int flags,
			       struct gpfs_winattr *attrs)
{
	if (gpfs_set_winattrs_path_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_set_winattrs_path_fn(pathname, flags, attrs);
}

int gpfswrap_get_winattrs_path(char *pathname, struct gpfs_winattr *attrs)
{
	if (gpfs_get_winattrs_path_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_get_winattrs_path_fn(pathname, attrs);
}

int gpfswrap_get_winattrs(int fd, struct gpfs_winattr *attrs)
{
	if (gpfs_get_winattrs_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_get_winattrs_fn(fd, attrs);
}

int gpfswrap_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
{
	if (gpfs_prealloc_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_prealloc_fn(fd, start, bytes);
}

int gpfswrap_ftruncate(int fd, gpfs_off64_t length)
{
	if (gpfs_ftruncate_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_ftruncate_fn(fd, length);
}

int gpfswrap_lib_init(int flags)
{
	if (gpfs_lib_init_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_lib_init_fn(flags);
}

int gpfswrap_set_times_path(char *pathname, int flags,
			    gpfs_timestruc_t times[4])
{
	if (gpfs_set_times_path_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_set_times_path_fn(pathname, flags, times);
}

int gpfswrap_quotactl(char *pathname, int cmd, int id, void *bufp)
{
	if (gpfs_quotactl_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_quotactl_fn(pathname, cmd, id, bufp);
}

int gpfswrap_fcntl(int fd, void *argp)
{
	if (gpfs_fcntl_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_fcntl_fn(fd, argp);
}

int gpfswrap_getfilesetid(char *pathname, char *name, int *idp)
{
	if (gpfs_getfilesetid_fn == NULL) {
		errno = ENOSYS;
		return -1;
	}

	return gpfs_getfilesetid_fn(pathname, name, idp);
}

int set_gpfs_lease(int fd, int leasetype)
{
	int gpfs_type = GPFS_LEASE_NONE;

	if (leasetype == F_RDLCK) {
		gpfs_type = GPFS_LEASE_READ;
	}
	if (leasetype == F_WRLCK) {
		gpfs_type = GPFS_LEASE_WRITE;
	}

	/* we unconditionally set CAP_LEASE, rather than looking for
	   -1/EACCES as there is a bug in some versions of
	   libgpfs_gpl.so which results in a leaked fd on /dev/ss0
	   each time we try this with the wrong capabilities set
	*/
	linux_set_lease_capability();
	return gpfswrap_set_lease(fd, gpfs_type);
}

int get_gpfs_quota(const char *pathname, int type, int id,
		   struct gpfs_quotaInfo *qi)
{
	int ret;

	ZERO_STRUCTP(qi);
	ret = gpfswrap_quotactl(discard_const_p(char, pathname),
				GPFS_QCMD(Q_GETQUOTA, type), id, qi);

	if (ret) {
		if (errno == GPFS_E_NO_QUOTA_INST) {
			DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
		} else if (errno != ENOSYS) {
			DEBUG(0, ("Get quota failed, type %d, id, %d, "
				  "errno %d.\n", type, id, errno));
		}

		return ret;
	}

	DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
		   type, id, qi->blockUsage, qi->blockHardLimit,
		   qi->blockSoftLimit, qi->blockGraceTime));

	return ret;
}

int get_gpfs_fset_id(const char *pathname, int *fset_id)
{
	int err, fd, errno_fcntl;

	struct {
		gpfsFcntlHeader_t hdr;
		gpfsGetFilesetName_t fsn;
	} arg;

	arg.hdr.totalLength = sizeof(arg);
	arg.hdr.fcntlVersion = GPFS_FCNTL_CURRENT_VERSION;
	arg.hdr.fcntlReserved = 0;
	arg.fsn.structLen = sizeof(arg.fsn);
	arg.fsn.structType = GPFS_FCNTL_GET_FILESETNAME;

	fd = open(pathname, O_RDONLY);
	if (fd == -1) {
		DEBUG(1, ("Could not open %s: %s\n",
			  pathname, strerror(errno)));
		return fd;
	}

	err = gpfswrap_fcntl(fd, &arg);
	errno_fcntl = errno;
	close(fd);

	if (err) {
		errno = errno_fcntl;
		if (errno != ENOSYS) {
			DEBUG(1, ("GPFS_FCNTL_GET_FILESETNAME for %s failed: "
				  "%s\n", pathname, strerror(errno)));
		}
		return err;
	}

	err = gpfswrap_getfilesetid(discard_const_p(char, pathname),
				    arg.fsn.buffer, fset_id);
	if (err && errno != ENOSYS) {
		DEBUG(1, ("gpfs_getfilesetid for %s failed: %s\n",
			  pathname, strerror(errno)));
	}
	return err;
}

static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
				  int idx, int *flags)
{
	if (!null_timespec(ts)) {
		*flags |= 1 << idx;
		gt[idx].tv_sec = ts.tv_sec;
		gt[idx].tv_nsec = ts.tv_nsec;
		DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
	}
}

int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
{
	gpfs_timestruc_t gpfs_times[4];
	int flags = 0;
	int rc;

	ZERO_ARRAY(gpfs_times);
	timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
	timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
	/* No good mapping from LastChangeTime to ctime, not storing */
	timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);

	if (!flags) {
		DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
		return 0;
	}

	rc = gpfswrap_set_times_path(path, flags, gpfs_times);

	if (rc != 0 && errno != ENOSYS) {
		DEBUG(1,("gpfs_set_times() returned with error %s\n",
			strerror(errno)));
	}

	return rc;
}