summaryrefslogtreecommitdiff
path: root/sys/xfs/xfs_syscalls.c
blob: 7dc820ea65840cc17993851674325ad1c100053a (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*	$OpenBSD: xfs_syscalls.c,v 1.6 1998/09/18 02:41:44 art Exp $	*/
/*
 * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Kungliga Tekniska
 *      Högskolan and its contributors.
 *
 * 4. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/namei.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <sys/signal.h>
#include <sys/syscall.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/syscallargs.h>

#ifndef XFS
int
sys_xfspioctl(struct proc *p, void *v, register_t *i)
{
	return ENOSYS;
}
#else

#include <xfs/xfs_common.h>

RCSID("$KTH: xfs_syscalls.c,v 1.20 1998/07/19 21:18:30 art Exp $");

/*
 * XFS system calls.
 */
#include <xfs/xfs_message.h>
#include <xfs/xfs_syscalls.h>
#include <xfs/xfs_dev.h>
#include <xfs/xfs_node.h>
#include <xfs/xfs_deb.h>

/* Misc syscalls */
#include <xfs/xfs_pioctl.h>

#ifdef ACTUALLY_LKM_NOT_KERNEL

/* XXX really defined in kern/kern_lkm.c */
extern int sys_lkmnosys(struct proc *p, void *v, register_t *retval);

#ifndef SYS_MAXSYSCALL		       /* Workaround for OpenBSD */
#define SYS_MAXSYSCALL 255
#endif

#endif /* ACTUALLY_LKM_NOT_KERNEL */


/*
 * Def pag:
 *  33536 <= g0 <= 34560
 *  32512 <= g1 <= 48896
 */

#define XFS_PAG1_LLIM 33536
#define XFS_PAG1_ULIM 34560
#define XFS_PAG2_LLIM 32512
#define XFS_PAG2_ULIM 48896

static gid_t pag_part_one = XFS_PAG1_LLIM;
static gid_t pag_part_two = XFS_PAG2_LLIM;

static int
xfs_is_pag(struct ucred *cred)
{
	/* The first group is the gid of the user ? */

	if (cred->cr_ngroups >= 3 &&
		cred->cr_groups[1] >= XFS_PAG1_LLIM &&
		cred->cr_groups[1] <= XFS_PAG1_ULIM &&
		cred->cr_groups[2] >= XFS_PAG2_LLIM &&
		cred->cr_groups[2] <= XFS_PAG2_ULIM)
		return 1;
	else
		return 0;
}


pag_t
xfs_get_pag(struct ucred *cred)
{
	if (xfs_is_pag(cred)) {
		return (((cred->cr_groups[1] << 16) & 0xFFFF0000) |
			((cred->cr_groups[2] & 0x0000FFFF)));
	} else
		return cred->cr_uid;	       /* XXX */
}

static int
xfs_setpag_call(struct ucred **ret_cred)
{
	struct ucred	*cred = *ret_cred;
	int		i;

	if (!xfs_is_pag(cred)) {
		/* Check if it fits */
		if (cred->cr_ngroups + 2 >= NGROUPS)
			return E2BIG;	       /* XXX Hmmm, better error ? */

		cred = crcopy (cred);

		/* Copy the groups */
		for (i = cred->cr_ngroups - 1; i > 0; i--) {
			cred->cr_groups[i + 2] = cred->cr_groups[i];
		}
		cred->cr_ngroups += 2;

	} else
		cred = crcopy(cred);

	cred->cr_groups[1] = pag_part_one;
	cred->cr_groups[2] = pag_part_two++;

	if (pag_part_two > XFS_PAG2_ULIM) {
		pag_part_one++;
		pag_part_two = XFS_PAG2_LLIM;
	}
	*ret_cred = cred;

	return 0;
}

#ifdef ACTUALLY_LKM_NOT_KERNEL
#if defined(__NetBSD__) || defined(__OpenBSD__)

#define syscallarg(x)   union { x datum; register_t pad; }

struct sys_xfspioctl_args {
	syscallarg(int) operation;
	syscallarg(char *) a_pathP;
	syscallarg(int) a_opcode;
	syscallarg(struct ViceIoctl *) a_paramsP;
	syscallarg(int) a_followSymlinks;
};

#elif defined(__FreeBSD__)

struct sys_xfspioctl_args {
	int operation;
	char *a_pathP;
	int a_opcode;
	struct ViceIoctl *a_paramsP;
	int a_followSymlinks;
};

#ifndef SCARG
#define SCARG(a, b) (a->b)
#endif

#endif
#endif /* ACTUALLY_LKM_NOT_KERNEL */

static int
xfs_pioctl_call(struct proc *p, void *v, register_t *i)
{
	int error;
	struct ViceIoctl vice_ioctl;
	struct xfs_message_pioctl msg;
	struct xfs_message_wakeup_data *msg2;
	char *pathptr;

	struct sys_xfspioctl_args *arg = (struct sys_xfspioctl_args *) v;

	/* Copy in the data structure for us */

	error = copyin(SCARG(arg, a_paramsP),
		       &vice_ioctl,
		       sizeof(vice_ioctl));
	if (error)
		return error;

	if (vice_ioctl.in_size > 2048) {
		printf("xfs_pioctl_call: got a humongous inpacket: opcode: %d",
		       SCARG(arg, a_opcode));
		return EINVAL;
	}
	if (vice_ioctl.in_size != 0) {
		error = copyin(vice_ioctl.in,
			       &msg.msg,
			       vice_ioctl.in_size);

		if (error)
			return error;
	}

	pathptr = SCARG(arg, a_pathP);

	if (pathptr != NULL) {
		char		path[MAXPATHLEN];
		struct xfs_node	*xn;
		struct nameidata nd;
		struct vnode	*vp;
		size_t		done;

		XFSDEB(XDEBSYS, ("xfs_syscall: looking up: %p\n", pathptr));

		error = copyinstr(pathptr, path, sizeof(path) - 1, &done);
		path[sizeof(path) - 1] = '\0';
		XFSDEB(XDEBSYS, ("xfs_syscall: path: %s len: %d error: %d\n", 
				 path, done, error));

		if (error)
			return error;

		NDINIT(&nd, LOOKUP,
		       SCARG(arg, a_followSymlinks) ? FOLLOW : 0,
		       UIO_SYSSPACE, path, p);

		error = namei(&nd);
		if (error != 0) {
			XFSDEB(XDEBSYS, ("xfs_syscall: error in namei: %d\n",
					error));
			return EINVAL;
		}

		vp = nd.ni_vp;

		if (vp->v_tag != VT_XFS) {
			XFSDEB(XDEBSYS, ("xfs_syscall: %s not in xfs\n",
					path));
			vrele(vp);
			return EINVAL;
		}

		xn = VNODE_TO_XNODE(vp);

		msg.handle = xn->handle;
		vrele(vp);
	}

	msg.header.opcode = XFS_MSG_PIOCTL;
	msg.opcode = SCARG(arg, a_opcode);
	msg.insize = vice_ioctl.in_size;
	msg.outsize = vice_ioctl.out_size;
	msg.cred.uid = p->p_cred->p_ruid;
	msg.cred.pag = xfs_get_pag(p->p_ucred);

	error = xfs_message_rpc(0, &msg.header, sizeof(msg)); /* XXX */
	msg2 = (struct xfs_message_wakeup_data *) &msg;
	if (error == 0)
		error = msg2->error;
	else
		error = EINVAL; /* return EINVAL to not confuse applications */

	if (error == 0 && msg2->header.opcode == XFS_MSG_WAKEUP_DATA)
		error = copyout(msg2->msg, vice_ioctl.out,
				min(msg2->len, vice_ioctl.out_size));
	return error;
}


#ifdef ACTUALLY_LKM_NOT_KERNEL
static int
xfs_syscall(struct proc *p, void *v, register_t *i)
#else
int
sys_xfspioctl(struct proc *p, void *v, register_t *i)
#endif
{
	struct sys_xfspioctl_args *arg = (struct sys_xfspioctl_args *) v;
	int		error = EINVAL;

	switch (SCARG(arg, operation)) {
	case AFSCALL_PROBE:
		error = 0;
		break;
	case AFSCALL_PIOCTL:
		error = xfs_pioctl_call(p, v, i);
		break;
	case AFSCALL_SETPAG:
		error = xfs_setpag_call(&p->p_cred->pc_ucred);
		break;
	default:
		uprintf("Unimplemeted call: %d\n", SCARG(arg, operation));
		error = EINVAL;
		break;
	}

	return error;
}

#ifdef ACTUALLY_LKM_NOT_KERNEL
#if defined(__NetBSD__) || defined(__OpenBSD__)

static int syscall_offset;
static struct sysent syscall_oldent;

static struct sysent xfs_syscallent = {
	4,					/* number of args */
	sizeof(struct sys_xfspioctl_args),	/* size of args */
	xfs_syscall				/* function pointer */
};

static int
find_first_free_syscall(int *ret)
{
	int i;

	/*
	 * Search the table looking for a slot...
	 */
	for (i = 0; i < SYS_MAXSYSCALL; i++)
		if (sysent[i].sy_call == sys_lkmnosys) {
			*ret = i;
			return 0;
		}

	return ENFILE;
}

int
xfs_install_syscalls(void)
{
	int error;

#ifdef AFS_SYSCALL
	syscall_offset = AFS_SYSCALL;
#else
	error = find_first_free_syscall(&syscall_offset);
	if (error)
		return error;
#endif

	syscall_oldent = sysent[syscall_offset];

	/* replace with new */

	sysent[syscall_offset] = xfs_syscallent;

	printf("syscall %d\n", syscall_offset);
	return 0;
}

int
xfs_uninstall_syscalls(void)
{
	/* replace current slot contents with old contents */
	if (syscall_offset)
		sysent[syscall_offset] = syscall_oldent;

	return 0;
}

int
xfs_stat_syscalls(void)
{
	return 0;
}

#elif defined(__FreeBSD__)

static int syscall_offset;
static struct sysent syscall_oldent;

static struct sysent xfs_syscallent = {
	4,
	xfs_syscall
};

static int
find_first_free_syscall(int *ret)
{
	int i;

	/*
	 * Search the table looking for a slot...
	 */
	for (i = 0; i < aout_sysvec.sv_size; i++)
		if (aout_sysvec.sv_table[i].sy_call ==
		    (sy_call_t *) lkmnosys) {
			*ret = i;
			return 0;
		}

	return ENFILE;
}

int
xfs_install_syscalls(void)
{
	int i;
	int error;

#ifdef AFS_SYSCALL
	i = AFS_SYSCALL;
#else
	error = find_first_free_syscall(&i);
	if (error)
		return error;
#endif

	syscall_oldent = aout_sysvec.sv_table[i];

	aout_sysvec.sv_table[i] = xfs_syscallent;

	syscall_offset = i;
	printf("syscall %d\n", i);

	return 0;
}

int
xfs_uninstall_syscalls(void)
{
	if (syscall_offset) {
		aout_sysvec.sv_table[syscall_offset].sy_call =
			(sy_call_t *) lkmnosys;
	}
	return 0;
}

int
xfs_stat_syscalls(void)
{
    return 0;
}

#endif
#endif /* ACTUALLY_LKM_NOT_KERNEL */
#endif /* !XFS */