summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/sockfs/sockcommon_vnops.c
blob: 532a24c223c30f942b4680b6218124d39ede7573 (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright (c) 2017 by Delphix. All rights reserved.
 * Copyright 2021 Racktop Systems, Inc.
 */

#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bitmap.h>
#include <sys/debug.h>
#include <sys/errno.h>
#include <sys/strsubr.h>
#include <sys/cmn_err.h>
#include <sys/sysmacros.h>
#include <sys/filio.h>
#include <sys/flock.h>
#include <sys/stat.h>
#include <sys/share.h>

#include <sys/vfs.h>
#include <sys/vfs_opreg.h>

#include <sys/sockio.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/strsun.h>

#include <fs/sockfs/sockcommon.h>
#include <fs/sockfs/socktpi.h>

/*
 * Generic vnode ops
 */
static int	socket_vop_open(struct vnode **, int, struct cred *,
		    caller_context_t *);
static int	socket_vop_close(struct vnode *, int, int, offset_t,
		    struct cred *, caller_context_t *);
static int	socket_vop_read(struct vnode *, struct uio *, int,
		    struct cred *, caller_context_t *);
static int	socket_vop_write(struct vnode *, struct uio *, int,
		    struct cred *, caller_context_t *);
static int	socket_vop_ioctl(struct vnode *, int, intptr_t, int,
		    struct cred *, int32_t *, caller_context_t *);
static int	socket_vop_setfl(struct vnode *, int, int, cred_t *,
		    caller_context_t *);
static int	socket_vop_getattr(struct vnode *, struct vattr *, int,
		    struct cred *, caller_context_t *);
static int	socket_vop_setattr(struct vnode *, struct vattr *, int,
		    struct cred *, caller_context_t *);
static int	socket_vop_access(struct vnode *, int, int, struct cred *,
		    caller_context_t *);
static int	socket_vop_fsync(struct vnode *, int, struct cred *,
		    caller_context_t *);
static void	socket_vop_inactive(struct vnode *, struct cred *,
		    caller_context_t *);
static int	socket_vop_fid(struct vnode *, struct fid *,
		    caller_context_t *);
static int	socket_vop_seek(struct vnode *, offset_t, offset_t *,
		    caller_context_t *);
static int	socket_vop_poll(struct vnode *, short, int, short *,
		    struct pollhead **, caller_context_t *);

extern int	socket_close_internal(struct sonode *, int, cred_t *);
extern void	socket_destroy_internal(struct sonode *, cred_t *);

struct vnodeops *socket_vnodeops;
const fs_operation_def_t socket_vnodeops_template[] = {
	VOPNAME_OPEN,		{ .vop_open = socket_vop_open },
	VOPNAME_CLOSE,		{ .vop_close = socket_vop_close },
	VOPNAME_READ,		{ .vop_read = socket_vop_read },
	VOPNAME_WRITE,		{ .vop_write = socket_vop_write },
	VOPNAME_IOCTL,		{ .vop_ioctl = socket_vop_ioctl },
	VOPNAME_SETFL,		{ .vop_setfl = socket_vop_setfl },
	VOPNAME_GETATTR,	{ .vop_getattr = socket_vop_getattr },
	VOPNAME_SETATTR,	{ .vop_setattr = socket_vop_setattr },
	VOPNAME_ACCESS,		{ .vop_access = socket_vop_access },
	VOPNAME_FSYNC,		{ .vop_fsync = socket_vop_fsync },
	VOPNAME_INACTIVE,	{ .vop_inactive = socket_vop_inactive },
	VOPNAME_FID,		{ .vop_fid = socket_vop_fid },
	VOPNAME_SEEK,		{ .vop_seek = socket_vop_seek },
	VOPNAME_POLL,		{ .vop_poll = socket_vop_poll },
	VOPNAME_DISPOSE,	{ .error = fs_error },
	NULL,			NULL
};


/*
 * generic vnode ops
 */

/*ARGSUSED*/
static int
socket_vop_open(struct vnode **vpp, int flag, struct cred *cr,
    caller_context_t *ct)
{
	struct vnode *vp = *vpp;
	struct sonode *so = VTOSO(vp);

	flag &= ~FCREAT;		/* paranoia */
	mutex_enter(&so->so_lock);
	so->so_count++;
	mutex_exit(&so->so_lock);

	ASSERT(so->so_count != 0);	/* wraparound */
	ASSERT(vp->v_type == VSOCK);

	return (0);
}

/*ARGSUSED*/
static int
socket_vop_close(struct vnode *vp, int flag, int count, offset_t offset,
    struct cred *cr, caller_context_t *ct)
{
	struct sonode *so;
	int error = 0;

	so = VTOSO(vp);
	ASSERT(vp->v_type == VSOCK);

	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
	cleanshares(vp, ttoproc(curthread)->p_pid);

	if (vp->v_stream)
		strclean(vp);

	if (count > 1) {
		dprint(2, ("socket_vop_close: count %d\n", count));
		return (0);
	}

	mutex_enter(&so->so_lock);
	if (--so->so_count == 0) {
		/*
		 * Initiate connection shutdown.
		 */
		mutex_exit(&so->so_lock);
		error = socket_close_internal(so, flag, cr);
	} else {
		mutex_exit(&so->so_lock);
	}

	return (error);
}

/*ARGSUSED2*/
static int
socket_vop_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
    caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);
	struct nmsghdr lmsg;

	ASSERT(vp->v_type == VSOCK);
	bzero((void *)&lmsg, sizeof (lmsg));

	return (socket_recvmsg(so, &lmsg, uiop, cr));
}

/*ARGSUSED2*/
static int
socket_vop_write(struct vnode *vp, struct uio *uiop, int ioflag,
    struct cred *cr, caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);
	struct nmsghdr lmsg;

	ASSERT(vp->v_type == VSOCK);
	bzero((void *)&lmsg, sizeof (lmsg));

	if (!(so->so_mode & SM_BYTESTREAM)) {
		/*
		 * If the socket is not byte stream set MSG_EOR
		 */
		lmsg.msg_flags = MSG_EOR;
	}

	return (socket_sendmsg(so, &lmsg, uiop, cr));
}

/*ARGSUSED4*/
static int
socket_vop_ioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
    struct cred *cr, int32_t *rvalp, caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);

	ASSERT(vp->v_type == VSOCK);

	return (socket_ioctl(so, cmd, arg, mode, cr, rvalp));
}

/*
 * Allow any flags. Record FNDELAY and FNONBLOCK so that they can be inherited
 * from listener to acceptor.
 */
/* ARGSUSED */
static int
socket_vop_setfl(vnode_t *vp, int oflags, int nflags, cred_t *cr,
    caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);
	int error = 0;

	ASSERT(vp->v_type == VSOCK);

	mutex_enter(&so->so_lock);
	if (nflags & FNDELAY)
		so->so_state |= SS_NDELAY;
	else
		so->so_state &= ~SS_NDELAY;
	if (nflags & FNONBLOCK)
		so->so_state |= SS_NONBLOCK;
	else
		so->so_state &= ~SS_NONBLOCK;
	mutex_exit(&so->so_lock);

	if (so->so_state & SS_ASYNC)
		oflags |= FASYNC;
	/*
	 * Sets/clears the SS_ASYNC flag based on the presence/absence
	 * of the FASYNC flag passed to fcntl(F_SETFL).
	 * This exists solely for BSD fcntl() FASYNC compatibility.
	 */
	if ((oflags ^ nflags) & FASYNC && so->so_version != SOV_STREAM) {
		int async = nflags & FASYNC;
		int32_t rv;

		/*
		 * For non-TPI sockets all we have to do is set/remove the
		 * SS_ASYNC bit, but for TPI it is more involved. For that
		 * reason we delegate the job to the protocol's ioctl handler.
		 */
		error = socket_ioctl(so, FIOASYNC, (intptr_t)&async, FKIOCTL,
		    cr, &rv);
	}
	return (error);
}


/*
 * Get the made up attributes for the vnode.
 * 4.3BSD returns the current time for all the timestamps.
 * 4.4BSD returns 0 for all the timestamps.
 * Here we use the access and modified times recorded in the sonode.
 *
 * Just like in BSD there is not effect on the underlying file system node
 * bound to an AF_UNIX pathname.
 *
 * When sockmod has been popped this will act just like a stream. Since
 * a socket is always a clone there is no need to inspect the attributes
 * of the "realvp".
 */
/* ARGSUSED */
int
socket_vop_getattr(struct vnode *vp, struct vattr *vap, int flags,
    struct cred *cr, caller_context_t *ct)
{
	dev_t		fsid;
	struct sonode	*so;
	static int	sonode_shift = 0;

	/*
	 * Calculate the amount of bitshift to a sonode pointer which will
	 * still keep it unique.  See below. Note that highbit() uses
	 * 1-based indexing for the highest bit set (and 0 for 'no bits set').
	 * To use the result of highbit() as a shift value, we must subtract 1
	 * from the result.
	 */
	if (sonode_shift == 0) {
		int bit = highbit(sizeof (struct sonode));

		/* Sanity check */
		VERIFY3S(bit, >, 0);
		sonode_shift = bit - 1;
	}

	so = VTOSO(vp);
	fsid = sockdev;

	if (so->so_version == SOV_STREAM) {
		/*
		 * The imaginary "sockmod" has been popped - act
		 * as a stream
		 */
		vap->va_type = VCHR;
		vap->va_mode = 0;
	} else {
		vap->va_type = vp->v_type;
		vap->va_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
		    S_IROTH|S_IWOTH;
	}
	vap->va_uid = vap->va_gid = 0;
	vap->va_fsid = fsid;
	/*
	 * If the va_nodeid is > UINT32_MAX, then stat(2) might fail in
	 * unexpected ways inside non-largefile aware 32-bit processes --
	 * historically, socket inode values (va_nodeid values) were capped at
	 * UINT16_MAX (for even more ancient reasons long since unnecessary).
	 * To avoid the potential of surprise failures, we shift down
	 * the sonode pointer address to try and get the most
	 * uniqueness into 32-bits. In practice, this represents the unique
	 * portion of the kernel address space, so the chance of duplicate
	 * socket inode values is minimized.
	 */
	vap->va_nodeid = ((ino_t)so >> sonode_shift) & 0xFFFFFFFF;
	vap->va_nlink = 0;
	vap->va_size = 0;

	/*
	 * We need to zero out the va_rdev to avoid some fstats getting
	 * EOVERFLOW.  This also mimics SunOS 4.x and BSD behavior.
	 */
	vap->va_rdev = (dev_t)0;
	vap->va_blksize = MAXBSIZE;
	vap->va_nblocks = btod(vap->va_size);

	if (!SOCK_IS_NONSTR(so)) {
		sotpi_info_t *sti = SOTOTPI(so);

		mutex_enter(&so->so_lock);
		vap->va_atime.tv_sec = sti->sti_atime;
		vap->va_mtime.tv_sec = sti->sti_mtime;
		vap->va_ctime.tv_sec = sti->sti_ctime;
		mutex_exit(&so->so_lock);
	} else {
		vap->va_atime.tv_sec = 0;
		vap->va_mtime.tv_sec = 0;
		vap->va_ctime.tv_sec = 0;
	}

	vap->va_atime.tv_nsec = 0;
	vap->va_mtime.tv_nsec = 0;
	vap->va_ctime.tv_nsec = 0;
	vap->va_seq = 0;

	return (0);
}

/*
 * Set attributes.
 * Just like in BSD there is not effect on the underlying file system node
 * bound to an AF_UNIX pathname.
 *
 * When sockmod has been popped this will act just like a stream. Since
 * a socket is always a clone there is no need to modify the attributes
 * of the "realvp".
 */
/* ARGSUSED */
int
socket_vop_setattr(struct vnode *vp, struct vattr *vap, int flags,
    struct cred *cr, caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);

	/*
	 * If times were changed, and we have a STREAMS socket, then update
	 * the sonode.
	 */
	if (!SOCK_IS_NONSTR(so)) {
		sotpi_info_t *sti = SOTOTPI(so);

		mutex_enter(&so->so_lock);
		if (vap->va_mask & AT_ATIME)
			sti->sti_atime = vap->va_atime.tv_sec;
		if (vap->va_mask & AT_MTIME) {
			sti->sti_mtime = vap->va_mtime.tv_sec;
			sti->sti_ctime = gethrestime_sec();
		}
		mutex_exit(&so->so_lock);
	}

	return (0);
}

/*
 * Check if user is allowed to access vp. For non-STREAMS based sockets,
 * there might not be a device attached to the file system. So for those
 * types of sockets there are no permissions to check.
 *
 * XXX Should there be some other mechanism to check access rights?
 */
/*ARGSUSED*/
int
socket_vop_access(struct vnode *vp, int mode, int flags, struct cred *cr,
    caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);

	if (!SOCK_IS_NONSTR(so)) {
		ASSERT(so->so_sockparams->sp_sdev_info.sd_vnode != NULL);
		return (VOP_ACCESS(so->so_sockparams->sp_sdev_info.sd_vnode,
		    mode, flags, cr, NULL));
	}
	return (0);
}

/*
 * 4.3BSD and 4.4BSD fail a fsync on a socket with EINVAL.
 * This code does the same to be compatible and also to not give an
 * application the impression that the data has actually been "synced"
 * to the other end of the connection.
 */
/* ARGSUSED */
int
socket_vop_fsync(struct vnode *vp, int syncflag, struct cred *cr,
    caller_context_t *ct)
{
	return (EINVAL);
}

/*ARGSUSED*/
static void
socket_vop_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);

	ASSERT(vp->v_type == VSOCK);

	mutex_enter(&vp->v_lock);
	/*
	 * If no one has reclaimed the vnode, remove from the
	 * cache now.
	 */
	if (vp->v_count < 1)
		cmn_err(CE_PANIC, "socket_inactive: Bad v_count");

	VN_RELE_LOCKED(vp);
	if (vp->v_count != 0) {
		mutex_exit(&vp->v_lock);
		return;
	}
	mutex_exit(&vp->v_lock);


	ASSERT(!vn_has_cached_data(vp));

	/* socket specfic clean-up */
	socket_destroy_internal(so, cr);
}

/* ARGSUSED */
int
socket_vop_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
{
	return (EINVAL);
}

/*
 * Sockets are not seekable.
 * (and there is a bug to fix STREAMS to make them fail this as well).
 */
/*ARGSUSED*/
int
socket_vop_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
    caller_context_t *ct)
{
	return (ESPIPE);
}

/*ARGSUSED*/
static int
socket_vop_poll(struct vnode *vp, short events, int anyyet, short *reventsp,
    struct pollhead **phpp, caller_context_t *ct)
{
	struct sonode *so = VTOSO(vp);

	ASSERT(vp->v_type == VSOCK);

	return (socket_poll(so, events, anyyet, reventsp, phpp));
}