summaryrefslogtreecommitdiff
path: root/sys/fs/puffs/puffs_compat.c
blob: 31072b67f06177b118e0af1d738054d1e8a2413c (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
/*	$NetBSD: puffs_compat.c,v 1.4 2015/04/22 17:07:24 pooka Exp $	*/

/*
 * Copyright (c) 2010 Antti Kantee.  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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 */

/*
 * This file handles puffs PDUs so that they are compatible between
 * 32bit<->64bit time_t/dev_t.  It enables running a -current kernel
 * against a 5.0 userland (assuming the protocol otherwise matches!).
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: puffs_compat.c,v 1.4 2015/04/22 17:07:24 pooka Exp $");

#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/atomic.h>

#include <dev/putter/putter_sys.h>

#include <fs/puffs/puffs_msgif.h>
#include <fs/puffs/puffs_sys.h>

#include <compat/sys/time.h>

/*
 * compat types
 */
struct vattr50 {
	enum vtype		va_type;
	mode_t			va_mode;
	nlink_t			va_nlink;
	uid_t			va_uid;
	gid_t			va_gid;
	uint32_t		va_fsid;
	ino_t			va_fileid;
	u_quad_t		va_size;
	long			va_blocksize;
	struct timespec50	va_atime;
	struct timespec50	va_mtime;
	struct timespec50	va_ctime;
	struct timespec50	va_birthtime;
	u_long			va_gen;
	u_long			va_flags;
	uint32_t		va_rdev;
	u_quad_t		va_bytes;
	u_quad_t		va_filerev;
	u_int			va_vaflags;
	long			va_spare;
};

struct puffs50_vfsmsg_fhtonode {
	struct puffs_req	pvfsr_pr;

	void			*pvfsr_fhcookie;	/* IN   */
	enum vtype		pvfsr_vtype;		/* IN   */
	voff_t			pvfsr_size;		/* IN   */
	uint32_t		pvfsr_rdev;		/* IN   */

	size_t			pvfsr_dsize;		/* OUT */
	uint8_t			pvfsr_data[0]		/* OUT, XXX */
					__aligned(ALIGNBYTES+1);
};

struct puffs50_vnmsg_lookup {
	struct puffs_req	pvn_pr;

	struct puffs_kcn	pvnr_cn;		/* OUT	*/
	struct puffs_kcred	pvnr_cn_cred;		/* OUT	*/

	puffs_cookie_t		pvnr_newnode;		/* IN	*/
	enum vtype		pvnr_vtype;		/* IN	*/
	voff_t			pvnr_size;		/* IN	*/
	uint32_t		pvnr_rdev;		/* IN	*/
};

struct puffs50_vnmsg_create {
	struct puffs_req	pvn_pr;

	struct puffs_kcn	pvnr_cn;		/* OUT	*/
	struct puffs_kcred	pvnr_cn_cred;		/* OUT	*/

	struct vattr50		pvnr_va;		/* OUT	*/
	puffs_cookie_t		pvnr_newnode;		/* IN	*/
};

struct puffs50_vnmsg_mknod {
	struct puffs_req	pvn_pr;

	struct puffs_kcn	pvnr_cn;		/* OUT	*/
	struct puffs_kcred	pvnr_cn_cred;		/* OUT	*/

	struct vattr50		pvnr_va;		/* OUT	*/
	puffs_cookie_t		pvnr_newnode;		/* IN	*/
};

#define puffs50_vnmsg_setattr puffs50_vnmsg_setgetattr
#define puffs50_vnmsg_getattr puffs50_vnmsg_setgetattr
struct puffs50_vnmsg_setgetattr {
	struct puffs_req	pvn_pr;

	struct puffs_kcred	pvnr_cred;		/* OUT	*/
	struct vattr50		pvnr_va;		/* IN/OUT (op depend) */
};

struct puffs50_vnmsg_mkdir {
	struct puffs_req	pvn_pr;

	struct puffs_kcn	pvnr_cn;		/* OUT	*/
	struct puffs_kcred	pvnr_cn_cred;		/* OUT	*/

	struct vattr50		pvnr_va;		/* OUT	*/
	puffs_cookie_t		pvnr_newnode;		/* IN	*/
};

struct puffs50_vnmsg_symlink {
	struct puffs_req	pvn_pr;

	struct puffs_kcn	pvnr_cn;		/* OUT	*/
	struct puffs_kcred	pvnr_cn_cred;		/* OUT	*/

	struct vattr50		pvnr_va;		/* OUT	*/
	puffs_cookie_t		pvnr_newnode;		/* IN	*/
	char			pvnr_link[MAXPATHLEN];	/* OUT	*/
};

/*
 * vattr translation routines
 */

#ifdef COMPAT_50
static void
vattr_to_50(const struct vattr *va, struct vattr50 *va50)
{

	va50->va_type = va->va_type;
	va50->va_mode = va->va_mode;
	va50->va_nlink = va->va_nlink;
	va50->va_uid = va->va_uid;
	va50->va_gid = va->va_gid;
	va50->va_fsid = (uint64_t)va->va_fsid;
	va50->va_fileid = va->va_fileid;
	va50->va_size = va->va_size;
	va50->va_blocksize = va->va_blocksize;
	timespec_to_timespec50(&va->va_atime, &va50->va_atime);
	timespec_to_timespec50(&va->va_ctime, &va50->va_ctime);
	timespec_to_timespec50(&va->va_mtime, &va50->va_mtime);
	timespec_to_timespec50(&va->va_birthtime, &va50->va_birthtime);
	va50->va_gen = va->va_gen;
	va50->va_flags = va->va_flags;
	va50->va_rdev = (int32_t)va->va_rdev;
	va50->va_bytes = va->va_bytes;
	va50->va_filerev = va->va_filerev;
	va50->va_vaflags = va->va_flags;
}

static void
vattr_from_50(const struct vattr50 *va50, struct vattr *va)
{

	va->va_type = va50->va_type;
	va->va_mode = va50->va_mode;
	va->va_nlink = va50->va_nlink;
	va->va_uid = va50->va_uid;
	va->va_gid = va50->va_gid;
	va->va_fsid = (uint32_t)va50->va_fsid;
	va->va_fileid = va50->va_fileid;
	va->va_size = va50->va_size;
	va->va_blocksize = va50->va_blocksize;
	timespec50_to_timespec(&va50->va_atime, &va->va_atime);
	timespec50_to_timespec(&va50->va_ctime, &va->va_ctime);
	timespec50_to_timespec(&va50->va_mtime, &va->va_mtime);
	timespec50_to_timespec(&va50->va_birthtime, &va->va_birthtime);
	va->va_gen = va50->va_gen;
	va->va_flags = va50->va_flags;
	va->va_rdev = (uint32_t)va50->va_rdev;
	va->va_bytes = va50->va_bytes;
	va->va_filerev = va50->va_filerev;
	va->va_vaflags = va50->va_flags;
}
#endif /* COMPAT_50 */

/*
 * XXX: cannot assert that sleeping is possible
 * (this always a valid assumption for now)
 */
#define INIT(name, extra)						\
	struct puffs50_##name *cmsg;					\
	struct puffs_##name *omsg;					\
	creq =kmem_zalloc(sizeof(struct puffs50_##name)+extra,KM_SLEEP);\
	cmsg = (struct puffs50_##name *)creq;				\
	omsg = (struct puffs_##name *)oreq;				\
	delta = sizeof(struct puffs50_##name)-sizeof(struct puffs_##name);
#define ASSIGN(field)							\
	cmsg->field = omsg->field;

bool
puffs_compat_outgoing(struct puffs_req *oreq,
	struct puffs_req **creqp, ssize_t *deltap)
{
	bool rv = false;
#ifdef COMPAT_50
	struct puffs_req *creq = NULL;
	ssize_t delta = 0;

	if (PUFFSOP_OPCLASS(oreq->preq_opclass) == PUFFSOP_VFS
	    && oreq->preq_optype == PUFFS_VFS_FHTOVP) {
		INIT(vfsmsg_fhtonode,
		    ((struct puffs_vfsmsg_fhtonode *)oreq)->pvfsr_dsize);

		ASSIGN(pvfsr_pr);
		ASSIGN(pvfsr_dsize);
		memcpy(cmsg->pvfsr_data, omsg->pvfsr_data, cmsg->pvfsr_dsize);
	} else if (PUFFSOP_OPCLASS(oreq->preq_opclass) == PUFFSOP_VN) {
		switch (oreq->preq_optype) {
		case PUFFS_VN_LOOKUP:
		{
			INIT(vnmsg_lookup, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cn);
			ASSIGN(pvnr_cn_cred);

			break;
		}

		case PUFFS_VN_CREATE:
		{
			INIT(vnmsg_create, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cn);
			ASSIGN(pvnr_cn_cred);
			vattr_to_50(&omsg->pvnr_va, &cmsg->pvnr_va);

			break;
		}

		case PUFFS_VN_MKNOD:
		{
			INIT(vnmsg_mknod, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cn);
			ASSIGN(pvnr_cn_cred);
			vattr_to_50(&omsg->pvnr_va, &cmsg->pvnr_va);

			break;
		}

		case PUFFS_VN_MKDIR:
		{
			INIT(vnmsg_mkdir, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cn);
			ASSIGN(pvnr_cn_cred);
			vattr_to_50(&omsg->pvnr_va, &cmsg->pvnr_va);

			break;
		}

		case PUFFS_VN_SYMLINK:
		{
			INIT(vnmsg_symlink, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cn);
			ASSIGN(pvnr_cn_cred);
			vattr_to_50(&omsg->pvnr_va, &cmsg->pvnr_va);
			memcpy(cmsg->pvnr_link, omsg->pvnr_link,
			    sizeof(cmsg->pvnr_link));

			break;
		}

		case PUFFS_VN_SETATTR:
		{
			INIT(vnmsg_setattr, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cred);
			vattr_to_50(&omsg->pvnr_va, &cmsg->pvnr_va);

			break;
		}
		case PUFFS_VN_GETATTR:
		{
			INIT(vnmsg_getattr, 0);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_cred);

			break;
		}

		default:
			break;
		}
	}

	if (creq) {
		*creqp = creq;
		*deltap = delta;
		rv = true;
	}
#endif

	return rv;
}
#undef INIT
#undef ASSIGN

#define INIT(name)							\
	struct puffs50_##name *cmsg = (void *)preq;			\
	struct puffs_##name *omsg = (void *)creq;
#define ASSIGN(field)							\
	omsg->field = cmsg->field;

void
puffs_compat_incoming(struct puffs_req *preq, struct puffs_req *creq)
{

#ifdef COMPAT_50
	if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS
	    && preq->preq_optype == PUFFS_VFS_FHTOVP) {
		INIT(vfsmsg_fhtonode);

		ASSIGN(pvfsr_pr);

		ASSIGN(pvfsr_fhcookie);
		ASSIGN(pvfsr_vtype);
		ASSIGN(pvfsr_size);
		ASSIGN(pvfsr_rdev);
	} else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
		switch (preq->preq_optype) {
		case PUFFS_VN_LOOKUP:
		{
			INIT(vnmsg_lookup);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_newnode);
			ASSIGN(pvnr_vtype);
			ASSIGN(pvnr_size);
			ASSIGN(pvnr_rdev);

			break;
		}

		case PUFFS_VN_CREATE:
		{
			INIT(vnmsg_create);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_newnode);

			break;
		}

		case PUFFS_VN_MKNOD:
		{
			INIT(vnmsg_mknod);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_newnode);

			break;
		}

		case PUFFS_VN_MKDIR:
		{
			INIT(vnmsg_mkdir);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_newnode);

			break;
		}

		case PUFFS_VN_SYMLINK:
		{
			INIT(vnmsg_symlink);

			ASSIGN(pvn_pr);
			ASSIGN(pvnr_newnode);

			break;
		}

		case PUFFS_VN_SETATTR:
		{
			INIT(vnmsg_setattr);

			ASSIGN(pvn_pr);

			break;
		}
		case PUFFS_VN_GETATTR:
		{
			INIT(vnmsg_getattr);

			ASSIGN(pvn_pr);
			vattr_from_50(&cmsg->pvnr_va, &omsg->pvnr_va);

			break;
		}

		default:
			panic("puffs compat ops come in pairs");
		}
	}
#endif /* COMPAT_50 */
}