xref: /freebsd/sys/kern/uipc_mqueue.c (revision dc15eac046a11d0a831eedf97459900227c50fb8)
1655291f2SDavid Xu /*-
2655291f2SDavid Xu  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3655291f2SDavid Xu  * All rights reserved.
4655291f2SDavid Xu  *
5655291f2SDavid Xu  * Redistribution and use in source and binary forms, with or without
6655291f2SDavid Xu  * modification, are permitted provided that the following conditions
7655291f2SDavid Xu  * are met:
8655291f2SDavid Xu  * 1. Redistributions of source code must retain the above copyright
9655291f2SDavid Xu  *    notice, this list of conditions and the following disclaimer.
10655291f2SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
11655291f2SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
12655291f2SDavid Xu  *    documentation and/or other materials provided with the distribution.
13655291f2SDavid Xu  *
14655291f2SDavid Xu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15655291f2SDavid Xu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16655291f2SDavid Xu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17655291f2SDavid Xu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18655291f2SDavid Xu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19655291f2SDavid Xu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20655291f2SDavid Xu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21655291f2SDavid Xu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22655291f2SDavid Xu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23655291f2SDavid Xu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24655291f2SDavid Xu  * SUCH DAMAGE.
25655291f2SDavid Xu  *
26655291f2SDavid Xu  */
27655291f2SDavid Xu 
28655291f2SDavid Xu /*
29655291f2SDavid Xu  * POSIX message queue implementation.
30655291f2SDavid Xu  *
31655291f2SDavid Xu  * 1) A mqueue filesystem can be mounted, each message queue appears
32655291f2SDavid Xu  *    in mounted directory, user can change queue's permission and
33655291f2SDavid Xu  *    ownership, or remove a queue. Manually creating a file in the
34655291f2SDavid Xu  *    directory causes a message queue to be created in the kernel with
35655291f2SDavid Xu  *    default message queue attributes applied and same name used, this
36655291f2SDavid Xu  *    method is not advocated since mq_open syscall allows user to specify
37655291f2SDavid Xu  *    different attributes. Also the file system can be mounted multiple
38655291f2SDavid Xu  *    times at different mount points but shows same contents.
39655291f2SDavid Xu  *
40655291f2SDavid Xu  * 2) Standard POSIX message queue API. The syscalls do not use vfs layer,
41655291f2SDavid Xu  *    but directly operate on internal data structure, this allows user to
42655291f2SDavid Xu  *    use the IPC facility without having to mount mqueue file system.
43655291f2SDavid Xu  */
44655291f2SDavid Xu 
45655291f2SDavid Xu #include <sys/cdefs.h>
46655291f2SDavid Xu __FBSDID("$FreeBSD$");
47655291f2SDavid Xu 
48afde2b65SKonstantin Belousov #include "opt_compat.h"
49afde2b65SKonstantin Belousov 
50655291f2SDavid Xu #include <sys/param.h>
51655291f2SDavid Xu #include <sys/kernel.h>
52655291f2SDavid Xu #include <sys/systm.h>
53655291f2SDavid Xu #include <sys/limits.h>
54655291f2SDavid Xu #include <sys/buf.h>
55a9d2f8d8SRobert Watson #include <sys/capability.h>
56655291f2SDavid Xu #include <sys/dirent.h>
57655291f2SDavid Xu #include <sys/event.h>
58b2f92ef9SDavid Xu #include <sys/eventhandler.h>
59655291f2SDavid Xu #include <sys/fcntl.h>
60655291f2SDavid Xu #include <sys/file.h>
615ee2d4acSDavid Xu #include <sys/filedesc.h>
62655291f2SDavid Xu #include <sys/lock.h>
63655291f2SDavid Xu #include <sys/malloc.h>
64655291f2SDavid Xu #include <sys/module.h>
65655291f2SDavid Xu #include <sys/mount.h>
66655291f2SDavid Xu #include <sys/mqueue.h>
67655291f2SDavid Xu #include <sys/mutex.h>
68655291f2SDavid Xu #include <sys/namei.h>
696aeb05d7STom Rhodes #include <sys/posix4.h>
70655291f2SDavid Xu #include <sys/poll.h>
71acd3428bSRobert Watson #include <sys/priv.h>
72655291f2SDavid Xu #include <sys/proc.h>
73655291f2SDavid Xu #include <sys/queue.h>
74655291f2SDavid Xu #include <sys/sysproto.h>
75655291f2SDavid Xu #include <sys/stat.h>
76655291f2SDavid Xu #include <sys/syscall.h>
77655291f2SDavid Xu #include <sys/syscallsubr.h>
785ee2d4acSDavid Xu #include <sys/sysent.h>
79655291f2SDavid Xu #include <sys/sx.h>
80655291f2SDavid Xu #include <sys/sysctl.h>
81655291f2SDavid Xu #include <sys/taskqueue.h>
82655291f2SDavid Xu #include <sys/unistd.h>
83655291f2SDavid Xu #include <sys/vnode.h>
84655291f2SDavid Xu #include <machine/atomic.h>
85655291f2SDavid Xu 
86de5b1952SAlexander Leidinger FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
87de5b1952SAlexander Leidinger 
88655291f2SDavid Xu /*
89655291f2SDavid Xu  * Limits and constants
90655291f2SDavid Xu  */
91655291f2SDavid Xu #define	MQFS_NAMELEN		NAME_MAX
92655291f2SDavid Xu #define MQFS_DELEN		(8 + MQFS_NAMELEN)
93655291f2SDavid Xu 
94655291f2SDavid Xu /* node types */
95655291f2SDavid Xu typedef enum {
96655291f2SDavid Xu 	mqfstype_none = 0,
97655291f2SDavid Xu 	mqfstype_root,
98655291f2SDavid Xu 	mqfstype_dir,
99655291f2SDavid Xu 	mqfstype_this,
100655291f2SDavid Xu 	mqfstype_parent,
101655291f2SDavid Xu 	mqfstype_file,
102655291f2SDavid Xu 	mqfstype_symlink,
103655291f2SDavid Xu } mqfs_type_t;
104655291f2SDavid Xu 
105655291f2SDavid Xu struct mqfs_node;
106655291f2SDavid Xu 
107655291f2SDavid Xu /*
108655291f2SDavid Xu  * mqfs_info: describes a mqfs instance
109655291f2SDavid Xu  */
110655291f2SDavid Xu struct mqfs_info {
111655291f2SDavid Xu 	struct sx		mi_lock;
112655291f2SDavid Xu 	struct mqfs_node	*mi_root;
113655291f2SDavid Xu 	struct unrhdr		*mi_unrhdr;
114655291f2SDavid Xu };
115655291f2SDavid Xu 
116655291f2SDavid Xu struct mqfs_vdata {
117655291f2SDavid Xu 	LIST_ENTRY(mqfs_vdata)	mv_link;
118655291f2SDavid Xu 	struct mqfs_node	*mv_node;
119655291f2SDavid Xu 	struct vnode		*mv_vnode;
120655291f2SDavid Xu 	struct task		mv_task;
121655291f2SDavid Xu };
122655291f2SDavid Xu 
123655291f2SDavid Xu /*
124655291f2SDavid Xu  * mqfs_node: describes a node (file or directory) within a mqfs
125655291f2SDavid Xu  */
126655291f2SDavid Xu struct mqfs_node {
127655291f2SDavid Xu 	char			mn_name[MQFS_NAMELEN+1];
128655291f2SDavid Xu 	struct mqfs_info	*mn_info;
129655291f2SDavid Xu 	struct mqfs_node	*mn_parent;
130655291f2SDavid Xu 	LIST_HEAD(,mqfs_node)	mn_children;
131655291f2SDavid Xu 	LIST_ENTRY(mqfs_node)	mn_sibling;
132655291f2SDavid Xu 	LIST_HEAD(,mqfs_vdata)	mn_vnodes;
133655291f2SDavid Xu 	int			mn_refcount;
134655291f2SDavid Xu 	mqfs_type_t		mn_type;
135655291f2SDavid Xu 	int			mn_deleted;
13660ae52f7SEd Schouten 	uint32_t		mn_fileno;
137655291f2SDavid Xu 	void			*mn_data;
138655291f2SDavid Xu 	struct timespec		mn_birth;
139655291f2SDavid Xu 	struct timespec		mn_ctime;
140655291f2SDavid Xu 	struct timespec		mn_atime;
141655291f2SDavid Xu 	struct timespec		mn_mtime;
142655291f2SDavid Xu 	uid_t			mn_uid;
143655291f2SDavid Xu 	gid_t			mn_gid;
144655291f2SDavid Xu 	int			mn_mode;
145655291f2SDavid Xu };
146655291f2SDavid Xu 
147655291f2SDavid Xu #define	VTON(vp)	(((struct mqfs_vdata *)((vp)->v_data))->mv_node)
148655291f2SDavid Xu #define VTOMQ(vp) 	((struct mqueue *)(VTON(vp)->mn_data))
149655291f2SDavid Xu #define	VFSTOMQFS(m)	((struct mqfs_info *)((m)->mnt_data))
150b2f92ef9SDavid Xu #define	FPTOMQ(fp)	((struct mqueue *)(((struct mqfs_node *) \
151b2f92ef9SDavid Xu 				(fp)->f_data)->mn_data))
152b2f92ef9SDavid Xu 
153655291f2SDavid Xu TAILQ_HEAD(msgq, mqueue_msg);
154655291f2SDavid Xu 
155655291f2SDavid Xu struct mqueue;
156b2f92ef9SDavid Xu 
157b2f92ef9SDavid Xu struct mqueue_notifier {
158b2f92ef9SDavid Xu 	LIST_ENTRY(mqueue_notifier)	nt_link;
159b2f92ef9SDavid Xu 	struct sigevent			nt_sigev;
160b2f92ef9SDavid Xu 	ksiginfo_t			nt_ksi;
161b2f92ef9SDavid Xu 	struct proc			*nt_proc;
162655291f2SDavid Xu };
163655291f2SDavid Xu 
164655291f2SDavid Xu struct mqueue {
165655291f2SDavid Xu 	struct mtx	mq_mutex;
166655291f2SDavid Xu 	int		mq_flags;
167655291f2SDavid Xu 	long		mq_maxmsg;
168655291f2SDavid Xu 	long		mq_msgsize;
169655291f2SDavid Xu 	long		mq_curmsgs;
170655291f2SDavid Xu 	long		mq_totalbytes;
171655291f2SDavid Xu 	struct msgq	mq_msgq;
172655291f2SDavid Xu 	int		mq_receivers;
173655291f2SDavid Xu 	int		mq_senders;
174655291f2SDavid Xu 	struct selinfo	mq_rsel;
175655291f2SDavid Xu 	struct selinfo	mq_wsel;
176b2f92ef9SDavid Xu 	struct mqueue_notifier	*mq_notifier;
177655291f2SDavid Xu };
178655291f2SDavid Xu 
179655291f2SDavid Xu #define	MQ_RSEL		0x01
180655291f2SDavid Xu #define	MQ_WSEL		0x02
181655291f2SDavid Xu 
182655291f2SDavid Xu struct mqueue_msg {
183655291f2SDavid Xu 	TAILQ_ENTRY(mqueue_msg)	msg_link;
184655291f2SDavid Xu 	unsigned int	msg_prio;
185655291f2SDavid Xu 	unsigned int	msg_size;
186655291f2SDavid Xu 	/* following real data... */
187655291f2SDavid Xu };
188655291f2SDavid Xu 
1896472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
190655291f2SDavid Xu 	"POSIX real time message queue");
191655291f2SDavid Xu 
192655291f2SDavid Xu static int	default_maxmsg  = 10;
193655291f2SDavid Xu static int	default_msgsize = 1024;
194655291f2SDavid Xu 
1959947b459SDavid Xu static int	maxmsg = 100;
196655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
197655291f2SDavid Xu     &maxmsg, 0, "Default maximum messages in queue");
198655291f2SDavid Xu static int	maxmsgsize = 16384;
199655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
200655291f2SDavid Xu     &maxmsgsize, 0, "Default maximum message size");
201655291f2SDavid Xu static int	maxmq = 100;
202655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
203655291f2SDavid Xu     &maxmq, 0, "maximum message queues");
204655291f2SDavid Xu static int	curmq = 0;
205655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
206655291f2SDavid Xu     &curmq, 0, "current message queue number");
207655291f2SDavid Xu static int	unloadable = 0;
208655291f2SDavid Xu static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
209655291f2SDavid Xu 
210b2f92ef9SDavid Xu static eventhandler_tag exit_tag;
211b2f92ef9SDavid Xu 
212655291f2SDavid Xu /* Only one instance per-system */
213655291f2SDavid Xu static struct mqfs_info		mqfs_data;
214655291f2SDavid Xu static uma_zone_t		mqnode_zone;
215655291f2SDavid Xu static uma_zone_t		mqueue_zone;
216655291f2SDavid Xu static uma_zone_t		mvdata_zone;
217b2f92ef9SDavid Xu static uma_zone_t		mqnoti_zone;
218655291f2SDavid Xu static struct vop_vector	mqfs_vnodeops;
219655291f2SDavid Xu static struct fileops		mqueueops;
220655291f2SDavid Xu 
221655291f2SDavid Xu /*
222655291f2SDavid Xu  * Directory structure construction and manipulation
223655291f2SDavid Xu  */
224655291f2SDavid Xu #ifdef notyet
225655291f2SDavid Xu static struct mqfs_node	*mqfs_create_dir(struct mqfs_node *parent,
226ba0360b1SDavid Xu 	const char *name, int namelen, struct ucred *cred, int mode);
227102178d0SDavid Xu static struct mqfs_node	*mqfs_create_link(struct mqfs_node *parent,
228ba0360b1SDavid Xu 	const char *name, int namelen, struct ucred *cred, int mode);
229655291f2SDavid Xu #endif
230655291f2SDavid Xu 
231655291f2SDavid Xu static struct mqfs_node	*mqfs_create_file(struct mqfs_node *parent,
232ba0360b1SDavid Xu 	const char *name, int namelen, struct ucred *cred, int mode);
233655291f2SDavid Xu static int	mqfs_destroy(struct mqfs_node *mn);
234655291f2SDavid Xu static void	mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
235655291f2SDavid Xu static void	mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
236655291f2SDavid Xu static int	mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
237655291f2SDavid Xu 
238655291f2SDavid Xu /*
239655291f2SDavid Xu  * Message queue construction and maniplation
240655291f2SDavid Xu  */
241655291f2SDavid Xu static struct mqueue	*mqueue_alloc(const struct mq_attr *attr);
242655291f2SDavid Xu static void	mqueue_free(struct mqueue *mq);
243655291f2SDavid Xu static int	mqueue_send(struct mqueue *mq, const char *msg_ptr,
244655291f2SDavid Xu 			size_t msg_len, unsigned msg_prio, int waitok,
245655291f2SDavid Xu 			const struct timespec *abs_timeout);
246655291f2SDavid Xu static int	mqueue_receive(struct mqueue *mq, char *msg_ptr,
247655291f2SDavid Xu 			size_t msg_len, unsigned *msg_prio, int waitok,
248655291f2SDavid Xu 			const struct timespec *abs_timeout);
249655291f2SDavid Xu static int	_mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
250655291f2SDavid Xu 			int timo);
251655291f2SDavid Xu static int	_mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
252655291f2SDavid Xu 			int timo);
253655291f2SDavid Xu static void	mqueue_send_notification(struct mqueue *mq);
254b2f92ef9SDavid Xu static void	mqueue_fdclose(struct thread *td, int fd, struct file *fp);
255b2f92ef9SDavid Xu static void	mq_proc_exit(void *arg, struct proc *p);
256655291f2SDavid Xu 
257b2f92ef9SDavid Xu /*
258b2f92ef9SDavid Xu  * kqueue filters
259b2f92ef9SDavid Xu  */
260655291f2SDavid Xu static void	filt_mqdetach(struct knote *kn);
261655291f2SDavid Xu static int	filt_mqread(struct knote *kn, long hint);
262655291f2SDavid Xu static int	filt_mqwrite(struct knote *kn, long hint);
263655291f2SDavid Xu 
264e76d823bSRobert Watson struct filterops mq_rfiltops = {
265e76d823bSRobert Watson 	.f_isfd = 1,
266e76d823bSRobert Watson 	.f_detach = filt_mqdetach,
267e76d823bSRobert Watson 	.f_event = filt_mqread,
268e76d823bSRobert Watson };
269e76d823bSRobert Watson struct filterops mq_wfiltops = {
270e76d823bSRobert Watson 	.f_isfd = 1,
271e76d823bSRobert Watson 	.f_detach = filt_mqdetach,
272e76d823bSRobert Watson 	.f_event = filt_mqwrite,
273e76d823bSRobert Watson };
274655291f2SDavid Xu 
275655291f2SDavid Xu /*
276655291f2SDavid Xu  * Initialize fileno bitmap
277655291f2SDavid Xu  */
278655291f2SDavid Xu static void
279655291f2SDavid Xu mqfs_fileno_init(struct mqfs_info *mi)
280655291f2SDavid Xu {
281655291f2SDavid Xu 	struct unrhdr *up;
282655291f2SDavid Xu 
283655291f2SDavid Xu 	up = new_unrhdr(1, INT_MAX, NULL);
284655291f2SDavid Xu 	mi->mi_unrhdr = up;
285655291f2SDavid Xu }
286655291f2SDavid Xu 
287655291f2SDavid Xu /*
288655291f2SDavid Xu  * Tear down fileno bitmap
289655291f2SDavid Xu  */
290655291f2SDavid Xu static void
291655291f2SDavid Xu mqfs_fileno_uninit(struct mqfs_info *mi)
292655291f2SDavid Xu {
293655291f2SDavid Xu 	struct unrhdr *up;
294655291f2SDavid Xu 
295655291f2SDavid Xu 	up = mi->mi_unrhdr;
296655291f2SDavid Xu 	mi->mi_unrhdr = NULL;
297655291f2SDavid Xu 	delete_unrhdr(up);
298655291f2SDavid Xu }
299655291f2SDavid Xu 
300655291f2SDavid Xu /*
301655291f2SDavid Xu  * Allocate a file number
302655291f2SDavid Xu  */
303b2f92ef9SDavid Xu static void
304655291f2SDavid Xu mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
305655291f2SDavid Xu {
306655291f2SDavid Xu 	/* make sure our parent has a file number */
307655291f2SDavid Xu 	if (mn->mn_parent && !mn->mn_parent->mn_fileno)
308655291f2SDavid Xu 		mqfs_fileno_alloc(mi, mn->mn_parent);
309655291f2SDavid Xu 
310655291f2SDavid Xu 	switch (mn->mn_type) {
311655291f2SDavid Xu 	case mqfstype_root:
312655291f2SDavid Xu 	case mqfstype_dir:
313655291f2SDavid Xu 	case mqfstype_file:
314655291f2SDavid Xu 	case mqfstype_symlink:
315655291f2SDavid Xu 		mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
316655291f2SDavid Xu 		break;
317655291f2SDavid Xu 	case mqfstype_this:
318655291f2SDavid Xu 		KASSERT(mn->mn_parent != NULL,
319655291f2SDavid Xu 		    ("mqfstype_this node has no parent"));
320655291f2SDavid Xu 		mn->mn_fileno = mn->mn_parent->mn_fileno;
321655291f2SDavid Xu 		break;
322655291f2SDavid Xu 	case mqfstype_parent:
323655291f2SDavid Xu 		KASSERT(mn->mn_parent != NULL,
324655291f2SDavid Xu 		    ("mqfstype_parent node has no parent"));
325655291f2SDavid Xu 		if (mn->mn_parent == mi->mi_root) {
326655291f2SDavid Xu 			mn->mn_fileno = mn->mn_parent->mn_fileno;
327655291f2SDavid Xu 			break;
328655291f2SDavid Xu 		}
329655291f2SDavid Xu 		KASSERT(mn->mn_parent->mn_parent != NULL,
330655291f2SDavid Xu 		    ("mqfstype_parent node has no grandparent"));
331655291f2SDavid Xu 		mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
332655291f2SDavid Xu 		break;
333655291f2SDavid Xu 	default:
334655291f2SDavid Xu 		KASSERT(0,
335655291f2SDavid Xu 		    ("mqfs_fileno_alloc() called for unknown type node: %d",
336655291f2SDavid Xu 			mn->mn_type));
337655291f2SDavid Xu 		break;
338655291f2SDavid Xu 	}
339655291f2SDavid Xu }
340655291f2SDavid Xu 
341655291f2SDavid Xu /*
342655291f2SDavid Xu  * Release a file number
343655291f2SDavid Xu  */
344b2f92ef9SDavid Xu static void
345655291f2SDavid Xu mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
346655291f2SDavid Xu {
347655291f2SDavid Xu 	switch (mn->mn_type) {
348655291f2SDavid Xu 	case mqfstype_root:
349655291f2SDavid Xu 	case mqfstype_dir:
350655291f2SDavid Xu 	case mqfstype_file:
351655291f2SDavid Xu 	case mqfstype_symlink:
352655291f2SDavid Xu 		free_unr(mi->mi_unrhdr, mn->mn_fileno);
353655291f2SDavid Xu 		break;
354655291f2SDavid Xu 	case mqfstype_this:
355655291f2SDavid Xu 	case mqfstype_parent:
356655291f2SDavid Xu 		/* ignore these, as they don't "own" their file number */
357655291f2SDavid Xu 		break;
358655291f2SDavid Xu 	default:
359655291f2SDavid Xu 		KASSERT(0,
360655291f2SDavid Xu 		    ("mqfs_fileno_free() called for unknown type node: %d",
361655291f2SDavid Xu 			mn->mn_type));
362655291f2SDavid Xu 		break;
363655291f2SDavid Xu 	}
364655291f2SDavid Xu }
365655291f2SDavid Xu 
366655291f2SDavid Xu static __inline struct mqfs_node *
367655291f2SDavid Xu mqnode_alloc(void)
368655291f2SDavid Xu {
369655291f2SDavid Xu 	return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
370655291f2SDavid Xu }
371655291f2SDavid Xu 
372655291f2SDavid Xu static __inline void
373655291f2SDavid Xu mqnode_free(struct mqfs_node *node)
374655291f2SDavid Xu {
375655291f2SDavid Xu 	uma_zfree(mqnode_zone, node);
376655291f2SDavid Xu }
377655291f2SDavid Xu 
378655291f2SDavid Xu static __inline void
379655291f2SDavid Xu mqnode_addref(struct mqfs_node *node)
380655291f2SDavid Xu {
381655291f2SDavid Xu 	atomic_fetchadd_int(&node->mn_refcount, 1);
382655291f2SDavid Xu }
383655291f2SDavid Xu 
384655291f2SDavid Xu static __inline void
385655291f2SDavid Xu mqnode_release(struct mqfs_node *node)
386655291f2SDavid Xu {
387fbc48e97SDavid Xu 	struct mqfs_info *mqfs;
388655291f2SDavid Xu 	int old, exp;
389655291f2SDavid Xu 
390fbc48e97SDavid Xu 	mqfs = node->mn_info;
391655291f2SDavid Xu 	old = atomic_fetchadd_int(&node->mn_refcount, -1);
392655291f2SDavid Xu 	if (node->mn_type == mqfstype_dir ||
393655291f2SDavid Xu 	    node->mn_type == mqfstype_root)
394655291f2SDavid Xu 		exp = 3; /* include . and .. */
395655291f2SDavid Xu 	else
396655291f2SDavid Xu 		exp = 1;
397fbc48e97SDavid Xu 	if (old == exp) {
398fbc48e97SDavid Xu 		int locked = sx_xlocked(&mqfs->mi_lock);
399fbc48e97SDavid Xu 		if (!locked)
400fbc48e97SDavid Xu 			sx_xlock(&mqfs->mi_lock);
401655291f2SDavid Xu 		mqfs_destroy(node);
402fbc48e97SDavid Xu 		if (!locked)
403fbc48e97SDavid Xu 			sx_xunlock(&mqfs->mi_lock);
404fbc48e97SDavid Xu 	}
405655291f2SDavid Xu }
406655291f2SDavid Xu 
407655291f2SDavid Xu /*
408655291f2SDavid Xu  * Add a node to a directory
409655291f2SDavid Xu  */
410655291f2SDavid Xu static int
411655291f2SDavid Xu mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
412655291f2SDavid Xu {
413655291f2SDavid Xu 	KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
414655291f2SDavid Xu 	KASSERT(parent->mn_info != NULL,
415655291f2SDavid Xu 	    ("%s(): parent has no mn_info", __func__));
416655291f2SDavid Xu 	KASSERT(parent->mn_type == mqfstype_dir ||
417655291f2SDavid Xu 	    parent->mn_type == mqfstype_root,
418655291f2SDavid Xu 	    ("%s(): parent is not a directory", __func__));
419655291f2SDavid Xu 
420655291f2SDavid Xu 	node->mn_info = parent->mn_info;
421655291f2SDavid Xu 	node->mn_parent = parent;
422655291f2SDavid Xu 	LIST_INIT(&node->mn_children);
423655291f2SDavid Xu 	LIST_INIT(&node->mn_vnodes);
424655291f2SDavid Xu 	LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
425655291f2SDavid Xu 	mqnode_addref(parent);
426655291f2SDavid Xu 	return (0);
427655291f2SDavid Xu }
428655291f2SDavid Xu 
429ba0360b1SDavid Xu static struct mqfs_node *
430ba0360b1SDavid Xu mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
431ba0360b1SDavid Xu 	int nodetype)
432ba0360b1SDavid Xu {
433ba0360b1SDavid Xu 	struct mqfs_node *node;
434ba0360b1SDavid Xu 
435ba0360b1SDavid Xu 	node = mqnode_alloc();
436ba0360b1SDavid Xu 	strncpy(node->mn_name, name, namelen);
437ba0360b1SDavid Xu 	node->mn_type = nodetype;
438ba0360b1SDavid Xu 	node->mn_refcount = 1;
439fbc48e97SDavid Xu 	vfs_timestamp(&node->mn_birth);
440ba0360b1SDavid Xu 	node->mn_ctime = node->mn_atime = node->mn_mtime
441ba0360b1SDavid Xu 		= node->mn_birth;
442ba0360b1SDavid Xu 	node->mn_uid = cred->cr_uid;
443ba0360b1SDavid Xu 	node->mn_gid = cred->cr_gid;
444ba0360b1SDavid Xu 	node->mn_mode = mode;
445ba0360b1SDavid Xu 	return (node);
446ba0360b1SDavid Xu }
447ba0360b1SDavid Xu 
448ba0360b1SDavid Xu /*
449ba0360b1SDavid Xu  * Create a file
450ba0360b1SDavid Xu  */
451ba0360b1SDavid Xu static struct mqfs_node *
452ba0360b1SDavid Xu mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
453ba0360b1SDavid Xu 	struct ucred *cred, int mode)
454ba0360b1SDavid Xu {
455ba0360b1SDavid Xu 	struct mqfs_node *node;
456ba0360b1SDavid Xu 
457ba0360b1SDavid Xu 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
458ba0360b1SDavid Xu 	if (mqfs_add_node(parent, node) != 0) {
459ba0360b1SDavid Xu 		mqnode_free(node);
460ba0360b1SDavid Xu 		return (NULL);
461ba0360b1SDavid Xu 	}
462ba0360b1SDavid Xu 	return (node);
463ba0360b1SDavid Xu }
464ba0360b1SDavid Xu 
465655291f2SDavid Xu /*
466655291f2SDavid Xu  * Add . and .. to a directory
467655291f2SDavid Xu  */
468655291f2SDavid Xu static int
469655291f2SDavid Xu mqfs_fixup_dir(struct mqfs_node *parent)
470655291f2SDavid Xu {
471655291f2SDavid Xu 	struct mqfs_node *dir;
472655291f2SDavid Xu 
473655291f2SDavid Xu 	dir = mqnode_alloc();
474655291f2SDavid Xu 	dir->mn_name[0] = '.';
475655291f2SDavid Xu 	dir->mn_type = mqfstype_this;
476655291f2SDavid Xu 	dir->mn_refcount = 1;
477655291f2SDavid Xu 	if (mqfs_add_node(parent, dir) != 0) {
478655291f2SDavid Xu 		mqnode_free(dir);
479655291f2SDavid Xu 		return (-1);
480655291f2SDavid Xu 	}
481655291f2SDavid Xu 
482655291f2SDavid Xu 	dir = mqnode_alloc();
483655291f2SDavid Xu 	dir->mn_name[0] = dir->mn_name[1] = '.';
484655291f2SDavid Xu 	dir->mn_type = mqfstype_parent;
485655291f2SDavid Xu 	dir->mn_refcount = 1;
486655291f2SDavid Xu 
487655291f2SDavid Xu 	if (mqfs_add_node(parent, dir) != 0) {
488655291f2SDavid Xu 		mqnode_free(dir);
489655291f2SDavid Xu 		return (-1);
490655291f2SDavid Xu 	}
491655291f2SDavid Xu 
492655291f2SDavid Xu 	return (0);
493655291f2SDavid Xu }
494655291f2SDavid Xu 
495655291f2SDavid Xu #ifdef notyet
496655291f2SDavid Xu 
497655291f2SDavid Xu /*
498655291f2SDavid Xu  * Create a directory
499655291f2SDavid Xu  */
500b2f92ef9SDavid Xu static struct mqfs_node *
501ba0360b1SDavid Xu mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
502ba0360b1SDavid Xu 	struct ucred *cred, int mode)
503655291f2SDavid Xu {
504ba0360b1SDavid Xu 	struct mqfs_node *node;
505655291f2SDavid Xu 
506ba0360b1SDavid Xu 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
507ba0360b1SDavid Xu 	if (mqfs_add_node(parent, node) != 0) {
508ba0360b1SDavid Xu 		mqnode_free(node);
509655291f2SDavid Xu 		return (NULL);
510655291f2SDavid Xu 	}
511655291f2SDavid Xu 
512ba0360b1SDavid Xu 	if (mqfs_fixup_dir(node) != 0) {
513ba0360b1SDavid Xu 		mqfs_destroy(node);
514655291f2SDavid Xu 		return (NULL);
515655291f2SDavid Xu 	}
516ba0360b1SDavid Xu 	return (node);
517655291f2SDavid Xu }
518b2f92ef9SDavid Xu 
519b2f92ef9SDavid Xu /*
520b2f92ef9SDavid Xu  * Create a symlink
521b2f92ef9SDavid Xu  */
522b2f92ef9SDavid Xu static struct mqfs_node *
523ba0360b1SDavid Xu mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
524ba0360b1SDavid Xu 	struct ucred *cred, int mode)
525b2f92ef9SDavid Xu {
526b2f92ef9SDavid Xu 	struct mqfs_node *node;
527b2f92ef9SDavid Xu 
528ba0360b1SDavid Xu 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
529655291f2SDavid Xu 	if (mqfs_add_node(parent, node) != 0) {
530655291f2SDavid Xu 		mqnode_free(node);
531655291f2SDavid Xu 		return (NULL);
532655291f2SDavid Xu 	}
533655291f2SDavid Xu 	return (node);
534655291f2SDavid Xu }
535655291f2SDavid Xu 
536ba0360b1SDavid Xu #endif
537ba0360b1SDavid Xu 
538655291f2SDavid Xu /*
539655291f2SDavid Xu  * Destroy a node or a tree of nodes
540655291f2SDavid Xu  */
541b2f92ef9SDavid Xu static int
542655291f2SDavid Xu mqfs_destroy(struct mqfs_node *node)
543655291f2SDavid Xu {
544655291f2SDavid Xu 	struct mqfs_node *parent;
545655291f2SDavid Xu 
546655291f2SDavid Xu 	KASSERT(node != NULL,
547655291f2SDavid Xu 	    ("%s(): node is NULL", __func__));
548655291f2SDavid Xu 	KASSERT(node->mn_info != NULL,
549655291f2SDavid Xu 	    ("%s(): node has no mn_info", __func__));
550655291f2SDavid Xu 
551655291f2SDavid Xu 	/* destroy children */
552655291f2SDavid Xu 	if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
553655291f2SDavid Xu 		while (! LIST_EMPTY(&node->mn_children))
554655291f2SDavid Xu 			mqfs_destroy(LIST_FIRST(&node->mn_children));
555655291f2SDavid Xu 
556655291f2SDavid Xu 	/* unlink from parent */
557655291f2SDavid Xu 	if ((parent = node->mn_parent) != NULL) {
558655291f2SDavid Xu 		KASSERT(parent->mn_info == node->mn_info,
559655291f2SDavid Xu 		    ("%s(): parent has different mn_info", __func__));
560655291f2SDavid Xu 		LIST_REMOVE(node, mn_sibling);
561655291f2SDavid Xu 	}
562655291f2SDavid Xu 
563655291f2SDavid Xu 	if (node->mn_fileno != 0)
564655291f2SDavid Xu 		mqfs_fileno_free(node->mn_info, node);
565655291f2SDavid Xu 	if (node->mn_data != NULL)
566655291f2SDavid Xu 		mqueue_free(node->mn_data);
567655291f2SDavid Xu 	mqnode_free(node);
568655291f2SDavid Xu 	return (0);
569655291f2SDavid Xu }
570655291f2SDavid Xu 
571655291f2SDavid Xu /*
572655291f2SDavid Xu  * Mount a mqfs instance
573655291f2SDavid Xu  */
574655291f2SDavid Xu static int
575dfd233edSAttilio Rao mqfs_mount(struct mount *mp)
576655291f2SDavid Xu {
577655291f2SDavid Xu 	struct statfs *sbp;
578655291f2SDavid Xu 
579655291f2SDavid Xu 	if (mp->mnt_flag & MNT_UPDATE)
580655291f2SDavid Xu 		return (EOPNOTSUPP);
581655291f2SDavid Xu 
582655291f2SDavid Xu 	mp->mnt_data = &mqfs_data;
5835da56ddbSTor Egge 	MNT_ILOCK(mp);
584655291f2SDavid Xu 	mp->mnt_flag |= MNT_LOCAL;
5859da8a32aSDavid Xu 	mp->mnt_kern_flag |= MNTK_MPSAFE;
5865da56ddbSTor Egge 	MNT_IUNLOCK(mp);
587655291f2SDavid Xu 	vfs_getnewfsid(mp);
588655291f2SDavid Xu 
589655291f2SDavid Xu 	sbp = &mp->mnt_stat;
590655291f2SDavid Xu 	vfs_mountedfrom(mp, "mqueue");
591655291f2SDavid Xu 	sbp->f_bsize = PAGE_SIZE;
592655291f2SDavid Xu 	sbp->f_iosize = PAGE_SIZE;
593655291f2SDavid Xu 	sbp->f_blocks = 1;
594655291f2SDavid Xu 	sbp->f_bfree = 0;
595655291f2SDavid Xu 	sbp->f_bavail = 0;
596655291f2SDavid Xu 	sbp->f_files = 1;
597655291f2SDavid Xu 	sbp->f_ffree = 0;
598655291f2SDavid Xu 	return (0);
599655291f2SDavid Xu }
600655291f2SDavid Xu 
601655291f2SDavid Xu /*
602655291f2SDavid Xu  * Unmount a mqfs instance
603655291f2SDavid Xu  */
604655291f2SDavid Xu static int
605dfd233edSAttilio Rao mqfs_unmount(struct mount *mp, int mntflags)
606655291f2SDavid Xu {
607655291f2SDavid Xu 	int error;
608655291f2SDavid Xu 
609dfd233edSAttilio Rao 	error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0,
610dfd233edSAttilio Rao 	    curthread);
611655291f2SDavid Xu 	return (error);
612655291f2SDavid Xu }
613655291f2SDavid Xu 
614655291f2SDavid Xu /*
615655291f2SDavid Xu  * Return a root vnode
616655291f2SDavid Xu  */
617655291f2SDavid Xu static int
618dfd233edSAttilio Rao mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
619655291f2SDavid Xu {
620655291f2SDavid Xu 	struct mqfs_info *mqfs;
621655291f2SDavid Xu 	int ret;
622655291f2SDavid Xu 
623655291f2SDavid Xu 	mqfs = VFSTOMQFS(mp);
624655291f2SDavid Xu 	ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
625655291f2SDavid Xu 	return (ret);
626655291f2SDavid Xu }
627655291f2SDavid Xu 
628655291f2SDavid Xu /*
629655291f2SDavid Xu  * Return filesystem stats
630655291f2SDavid Xu  */
631655291f2SDavid Xu static int
632dfd233edSAttilio Rao mqfs_statfs(struct mount *mp, struct statfs *sbp)
633655291f2SDavid Xu {
634655291f2SDavid Xu 	/* XXX update statistics */
635655291f2SDavid Xu 	return (0);
636655291f2SDavid Xu }
637655291f2SDavid Xu 
638655291f2SDavid Xu /*
639655291f2SDavid Xu  * Initialize a mqfs instance
640655291f2SDavid Xu  */
641655291f2SDavid Xu static int
642655291f2SDavid Xu mqfs_init(struct vfsconf *vfc)
643655291f2SDavid Xu {
644655291f2SDavid Xu 	struct mqfs_node *root;
645655291f2SDavid Xu 	struct mqfs_info *mi;
646655291f2SDavid Xu 
647655291f2SDavid Xu 	mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
648655291f2SDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
649655291f2SDavid Xu 	mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
650655291f2SDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
651655291f2SDavid Xu 	mvdata_zone = uma_zcreate("mvdata",
652655291f2SDavid Xu 		sizeof(struct mqfs_vdata), NULL, NULL, NULL,
653655291f2SDavid Xu 		NULL, UMA_ALIGN_PTR, 0);
654b2f92ef9SDavid Xu 	mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
655655291f2SDavid Xu 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
656655291f2SDavid Xu 	mi = &mqfs_data;
657655291f2SDavid Xu 	sx_init(&mi->mi_lock, "mqfs lock");
658655291f2SDavid Xu 	/* set up the root diretory */
659ba0360b1SDavid Xu 	root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
660ba0360b1SDavid Xu 		mqfstype_root);
661655291f2SDavid Xu 	root->mn_info = mi;
662655291f2SDavid Xu 	LIST_INIT(&root->mn_children);
663655291f2SDavid Xu 	LIST_INIT(&root->mn_vnodes);
664655291f2SDavid Xu 	mi->mi_root = root;
665655291f2SDavid Xu 	mqfs_fileno_init(mi);
666655291f2SDavid Xu 	mqfs_fileno_alloc(mi, root);
667655291f2SDavid Xu 	mqfs_fixup_dir(root);
668b2f92ef9SDavid Xu 	exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
669b2f92ef9SDavid Xu 	    EVENTHANDLER_PRI_ANY);
670b2f92ef9SDavid Xu 	mq_fdclose = mqueue_fdclose;
6715ee2d4acSDavid Xu 	p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
672655291f2SDavid Xu 	return (0);
673655291f2SDavid Xu }
674655291f2SDavid Xu 
675655291f2SDavid Xu /*
676655291f2SDavid Xu  * Destroy a mqfs instance
677655291f2SDavid Xu  */
678655291f2SDavid Xu static int
679655291f2SDavid Xu mqfs_uninit(struct vfsconf *vfc)
680655291f2SDavid Xu {
681655291f2SDavid Xu 	struct mqfs_info *mi;
682655291f2SDavid Xu 
683655291f2SDavid Xu 	if (!unloadable)
684655291f2SDavid Xu 		return (EOPNOTSUPP);
685b2f92ef9SDavid Xu 	EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
686655291f2SDavid Xu 	mi = &mqfs_data;
687655291f2SDavid Xu 	mqfs_destroy(mi->mi_root);
688655291f2SDavid Xu 	mi->mi_root = NULL;
689655291f2SDavid Xu 	mqfs_fileno_uninit(mi);
690655291f2SDavid Xu 	sx_destroy(&mi->mi_lock);
691b2f92ef9SDavid Xu 	uma_zdestroy(mqnode_zone);
692b2f92ef9SDavid Xu 	uma_zdestroy(mqueue_zone);
693b2f92ef9SDavid Xu 	uma_zdestroy(mvdata_zone);
694b2f92ef9SDavid Xu 	uma_zdestroy(mqnoti_zone);
695655291f2SDavid Xu 	return (0);
696655291f2SDavid Xu }
697655291f2SDavid Xu 
698655291f2SDavid Xu /*
699655291f2SDavid Xu  * task routine
700655291f2SDavid Xu  */
701655291f2SDavid Xu static void
702655291f2SDavid Xu do_recycle(void *context, int pending __unused)
703655291f2SDavid Xu {
704655291f2SDavid Xu 	struct vnode *vp = (struct vnode *)context;
705655291f2SDavid Xu 
706655291f2SDavid Xu 	vrecycle(vp, curthread);
707655291f2SDavid Xu 	vdrop(vp);
708655291f2SDavid Xu }
709655291f2SDavid Xu 
710655291f2SDavid Xu /*
711655291f2SDavid Xu  * Allocate a vnode
712655291f2SDavid Xu  */
713b2f92ef9SDavid Xu static int
714655291f2SDavid Xu mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
715655291f2SDavid Xu {
716655291f2SDavid Xu 	struct mqfs_vdata *vd;
717fbc48e97SDavid Xu 	struct mqfs_info  *mqfs;
718fbc48e97SDavid Xu 	struct vnode *newvpp;
719655291f2SDavid Xu 	int error;
720655291f2SDavid Xu 
721fbc48e97SDavid Xu 	mqfs = pn->mn_info;
722fbc48e97SDavid Xu 	*vpp = NULL;
723fbc48e97SDavid Xu 	sx_xlock(&mqfs->mi_lock);
724655291f2SDavid Xu 	LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
725fbc48e97SDavid Xu 		if (vd->mv_vnode->v_mount == mp) {
726fbc48e97SDavid Xu 			vhold(vd->mv_vnode);
727655291f2SDavid Xu 			break;
728655291f2SDavid Xu 		}
729fbc48e97SDavid Xu 	}
730655291f2SDavid Xu 
731655291f2SDavid Xu 	if (vd != NULL) {
732fbc48e97SDavid Xu found:
733655291f2SDavid Xu 		*vpp = vd->mv_vnode;
734fbc48e97SDavid Xu 		sx_xunlock(&mqfs->mi_lock);
735fbc48e97SDavid Xu 		error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
736fbc48e97SDavid Xu 		vdrop(*vpp);
737fbc48e97SDavid Xu 		return (error);
738655291f2SDavid Xu 	}
739fbc48e97SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
740655291f2SDavid Xu 
741fbc48e97SDavid Xu 	error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
742655291f2SDavid Xu 	if (error)
743655291f2SDavid Xu 		return (error);
744fbc48e97SDavid Xu 	vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
745fbc48e97SDavid Xu 	error = insmntque(newvpp, mp);
746fbc48e97SDavid Xu 	if (error != 0)
74761b9d89fSTor Egge 		return (error);
748fbc48e97SDavid Xu 
749fbc48e97SDavid Xu 	sx_xlock(&mqfs->mi_lock);
750fbc48e97SDavid Xu 	/*
751fbc48e97SDavid Xu 	 * Check if it has already been allocated
752fbc48e97SDavid Xu 	 * while we were blocked.
753fbc48e97SDavid Xu 	 */
754fbc48e97SDavid Xu 	LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
755fbc48e97SDavid Xu 		if (vd->mv_vnode->v_mount == mp) {
756fbc48e97SDavid Xu 			vhold(vd->mv_vnode);
757fbc48e97SDavid Xu 			sx_xunlock(&mqfs->mi_lock);
758fbc48e97SDavid Xu 
759fbc48e97SDavid Xu 			vgone(newvpp);
760fbc48e97SDavid Xu 			vput(newvpp);
761fbc48e97SDavid Xu 			goto found;
76261b9d89fSTor Egge 		}
763fbc48e97SDavid Xu 	}
764fbc48e97SDavid Xu 
765fbc48e97SDavid Xu 	*vpp = newvpp;
766fbc48e97SDavid Xu 
767655291f2SDavid Xu 	vd = uma_zalloc(mvdata_zone, M_WAITOK);
768655291f2SDavid Xu 	(*vpp)->v_data = vd;
769655291f2SDavid Xu 	vd->mv_vnode = *vpp;
770655291f2SDavid Xu 	vd->mv_node = pn;
771655291f2SDavid Xu 	TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
772655291f2SDavid Xu 	LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
773655291f2SDavid Xu 	mqnode_addref(pn);
774655291f2SDavid Xu 	switch (pn->mn_type) {
775655291f2SDavid Xu 	case mqfstype_root:
776655291f2SDavid Xu 		(*vpp)->v_vflag = VV_ROOT;
777655291f2SDavid Xu 		/* fall through */
778655291f2SDavid Xu 	case mqfstype_dir:
779655291f2SDavid Xu 	case mqfstype_this:
780655291f2SDavid Xu 	case mqfstype_parent:
781655291f2SDavid Xu 		(*vpp)->v_type = VDIR;
782655291f2SDavid Xu 		break;
783655291f2SDavid Xu 	case mqfstype_file:
784655291f2SDavid Xu 		(*vpp)->v_type = VREG;
785655291f2SDavid Xu 		break;
786655291f2SDavid Xu 	case mqfstype_symlink:
787655291f2SDavid Xu 		(*vpp)->v_type = VLNK;
788655291f2SDavid Xu 		break;
789655291f2SDavid Xu 	case mqfstype_none:
790655291f2SDavid Xu 		KASSERT(0, ("mqfs_allocf called for null node\n"));
791655291f2SDavid Xu 	default:
792655291f2SDavid Xu 		panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
793655291f2SDavid Xu 	}
794fbc48e97SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
795655291f2SDavid Xu 	return (0);
796655291f2SDavid Xu }
797655291f2SDavid Xu 
798655291f2SDavid Xu /*
799655291f2SDavid Xu  * Search a directory entry
800655291f2SDavid Xu  */
801655291f2SDavid Xu static struct mqfs_node *
802655291f2SDavid Xu mqfs_search(struct mqfs_node *pd, const char *name, int len)
803655291f2SDavid Xu {
804655291f2SDavid Xu 	struct mqfs_node *pn;
805655291f2SDavid Xu 
806fbc48e97SDavid Xu 	sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
807655291f2SDavid Xu 	LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
8081cbae705SEd Schouten 		if (strncmp(pn->mn_name, name, len) == 0 &&
8091cbae705SEd Schouten 		    pn->mn_name[len] == '\0')
810655291f2SDavid Xu 			return (pn);
811655291f2SDavid Xu 	}
812655291f2SDavid Xu 	return (NULL);
813655291f2SDavid Xu }
814655291f2SDavid Xu 
815655291f2SDavid Xu /*
816873fbcd7SRobert Watson  * Look up a file or directory.
817655291f2SDavid Xu  */
818655291f2SDavid Xu static int
819655291f2SDavid Xu mqfs_lookupx(struct vop_cachedlookup_args *ap)
820655291f2SDavid Xu {
821655291f2SDavid Xu 	struct componentname *cnp;
822655291f2SDavid Xu 	struct vnode *dvp, **vpp;
823655291f2SDavid Xu 	struct mqfs_node *pd;
824655291f2SDavid Xu 	struct mqfs_node *pn;
825fbc48e97SDavid Xu 	struct mqfs_info *mqfs;
826655291f2SDavid Xu 	int nameiop, flags, error, namelen;
827655291f2SDavid Xu 	char *pname;
828655291f2SDavid Xu 	struct thread *td;
829655291f2SDavid Xu 
830655291f2SDavid Xu 	cnp = ap->a_cnp;
831655291f2SDavid Xu 	vpp = ap->a_vpp;
832655291f2SDavid Xu 	dvp = ap->a_dvp;
833655291f2SDavid Xu 	pname = cnp->cn_nameptr;
834655291f2SDavid Xu 	namelen = cnp->cn_namelen;
835655291f2SDavid Xu 	td = cnp->cn_thread;
836655291f2SDavid Xu 	flags = cnp->cn_flags;
837655291f2SDavid Xu 	nameiop = cnp->cn_nameiop;
838655291f2SDavid Xu 	pd = VTON(dvp);
839655291f2SDavid Xu 	pn = NULL;
840fbc48e97SDavid Xu 	mqfs = pd->mn_info;
841655291f2SDavid Xu 	*vpp = NULLVP;
842655291f2SDavid Xu 
843655291f2SDavid Xu 	if (dvp->v_type != VDIR)
844655291f2SDavid Xu 		return (ENOTDIR);
845655291f2SDavid Xu 
846655291f2SDavid Xu 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
847655291f2SDavid Xu 	if (error)
848655291f2SDavid Xu 		return (error);
849655291f2SDavid Xu 
850655291f2SDavid Xu 	/* shortcut: check if the name is too long */
851655291f2SDavid Xu 	if (cnp->cn_namelen >= MQFS_NAMELEN)
852655291f2SDavid Xu 		return (ENOENT);
853655291f2SDavid Xu 
854655291f2SDavid Xu 	/* self */
855655291f2SDavid Xu 	if (namelen == 1 && pname[0] == '.') {
856655291f2SDavid Xu 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
857655291f2SDavid Xu 			return (EINVAL);
858655291f2SDavid Xu 		pn = pd;
859655291f2SDavid Xu 		*vpp = dvp;
860655291f2SDavid Xu 		VREF(dvp);
861655291f2SDavid Xu 		return (0);
862655291f2SDavid Xu 	}
863655291f2SDavid Xu 
864655291f2SDavid Xu 	/* parent */
865655291f2SDavid Xu 	if (cnp->cn_flags & ISDOTDOT) {
866655291f2SDavid Xu 		if (dvp->v_vflag & VV_ROOT)
867655291f2SDavid Xu 			return (EIO);
868655291f2SDavid Xu 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
869655291f2SDavid Xu 			return (EINVAL);
87022db15c0SAttilio Rao 		VOP_UNLOCK(dvp, 0);
871655291f2SDavid Xu 		KASSERT(pd->mn_parent, ("non-root directory has no parent"));
872655291f2SDavid Xu 		pn = pd->mn_parent;
873655291f2SDavid Xu 		error = mqfs_allocv(dvp->v_mount, vpp, pn);
874cb05b60aSAttilio Rao 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
875655291f2SDavid Xu 		return (error);
876655291f2SDavid Xu 	}
877655291f2SDavid Xu 
878655291f2SDavid Xu 	/* named node */
879fbc48e97SDavid Xu 	sx_xlock(&mqfs->mi_lock);
880655291f2SDavid Xu 	pn = mqfs_search(pd, pname, namelen);
881fbc48e97SDavid Xu 	if (pn != NULL)
882fbc48e97SDavid Xu 		mqnode_addref(pn);
883fbc48e97SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
884655291f2SDavid Xu 
885655291f2SDavid Xu 	/* found */
886655291f2SDavid Xu 	if (pn != NULL) {
887655291f2SDavid Xu 		/* DELETE */
888655291f2SDavid Xu 		if (nameiop == DELETE && (flags & ISLASTCN)) {
889655291f2SDavid Xu 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
890fbc48e97SDavid Xu 			if (error) {
891fbc48e97SDavid Xu 				mqnode_release(pn);
892655291f2SDavid Xu 				return (error);
893fbc48e97SDavid Xu 			}
894655291f2SDavid Xu 			if (*vpp == dvp) {
895655291f2SDavid Xu 				VREF(dvp);
896655291f2SDavid Xu 				*vpp = dvp;
897fbc48e97SDavid Xu 				mqnode_release(pn);
898655291f2SDavid Xu 				return (0);
899655291f2SDavid Xu 			}
900655291f2SDavid Xu 		}
901655291f2SDavid Xu 
902655291f2SDavid Xu 		/* allocate vnode */
903655291f2SDavid Xu 		error = mqfs_allocv(dvp->v_mount, vpp, pn);
904fbc48e97SDavid Xu 		mqnode_release(pn);
905655291f2SDavid Xu 		if (error == 0 && cnp->cn_flags & MAKEENTRY)
906655291f2SDavid Xu 			cache_enter(dvp, *vpp, cnp);
907655291f2SDavid Xu 		return (error);
908655291f2SDavid Xu 	}
909655291f2SDavid Xu 
910655291f2SDavid Xu 	/* not found */
911655291f2SDavid Xu 
912655291f2SDavid Xu 	/* will create a new entry in the directory ? */
913655291f2SDavid Xu 	if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
914655291f2SDavid Xu 	    && (flags & ISLASTCN)) {
915655291f2SDavid Xu 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
916655291f2SDavid Xu 		if (error)
917655291f2SDavid Xu 			return (error);
918655291f2SDavid Xu 		cnp->cn_flags |= SAVENAME;
919655291f2SDavid Xu 		return (EJUSTRETURN);
920655291f2SDavid Xu 	}
921655291f2SDavid Xu 	return (ENOENT);
922655291f2SDavid Xu }
923655291f2SDavid Xu 
924655291f2SDavid Xu #if 0
925655291f2SDavid Xu struct vop_lookup_args {
926655291f2SDavid Xu 	struct vop_generic_args a_gen;
927655291f2SDavid Xu 	struct vnode *a_dvp;
928655291f2SDavid Xu 	struct vnode **a_vpp;
929655291f2SDavid Xu 	struct componentname *a_cnp;
930655291f2SDavid Xu };
931655291f2SDavid Xu #endif
932655291f2SDavid Xu 
933655291f2SDavid Xu /*
934655291f2SDavid Xu  * vnode lookup operation
935655291f2SDavid Xu  */
936655291f2SDavid Xu static int
937655291f2SDavid Xu mqfs_lookup(struct vop_cachedlookup_args *ap)
938655291f2SDavid Xu {
939655291f2SDavid Xu 	int rc;
940655291f2SDavid Xu 
941655291f2SDavid Xu 	rc = mqfs_lookupx(ap);
942655291f2SDavid Xu 	return (rc);
943655291f2SDavid Xu }
944655291f2SDavid Xu 
945655291f2SDavid Xu #if 0
946655291f2SDavid Xu struct vop_create_args {
947655291f2SDavid Xu 	struct vnode *a_dvp;
948655291f2SDavid Xu 	struct vnode **a_vpp;
949655291f2SDavid Xu 	struct componentname *a_cnp;
950655291f2SDavid Xu 	struct vattr *a_vap;
951655291f2SDavid Xu };
952655291f2SDavid Xu #endif
953655291f2SDavid Xu 
954655291f2SDavid Xu /*
955655291f2SDavid Xu  * vnode creation operation
956655291f2SDavid Xu  */
957655291f2SDavid Xu static int
958655291f2SDavid Xu mqfs_create(struct vop_create_args *ap)
959655291f2SDavid Xu {
960655291f2SDavid Xu 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
961655291f2SDavid Xu 	struct componentname *cnp = ap->a_cnp;
962655291f2SDavid Xu 	struct mqfs_node *pd;
963655291f2SDavid Xu 	struct mqfs_node *pn;
964655291f2SDavid Xu 	struct mqueue *mq;
965655291f2SDavid Xu 	int error;
966655291f2SDavid Xu 
967655291f2SDavid Xu 	pd = VTON(ap->a_dvp);
968655291f2SDavid Xu 	if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
969655291f2SDavid Xu 		return (ENOTDIR);
970655291f2SDavid Xu 	mq = mqueue_alloc(NULL);
971655291f2SDavid Xu 	if (mq == NULL)
972655291f2SDavid Xu 		return (EAGAIN);
973655291f2SDavid Xu 	sx_xlock(&mqfs->mi_lock);
974655291f2SDavid Xu 	if ((cnp->cn_flags & HASBUF) == 0)
975655291f2SDavid Xu 		panic("%s: no name", __func__);
976ba0360b1SDavid Xu 	pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
977ba0360b1SDavid Xu 		cnp->cn_cred, ap->a_vap->va_mode);
978fbc48e97SDavid Xu 	if (pn == NULL) {
979fbc48e97SDavid Xu 		sx_xunlock(&mqfs->mi_lock);
980ba0360b1SDavid Xu 		error = ENOSPC;
981fbc48e97SDavid Xu 	} else {
982fbc48e97SDavid Xu 		mqnode_addref(pn);
983fbc48e97SDavid Xu 		sx_xunlock(&mqfs->mi_lock);
984655291f2SDavid Xu 		error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
985fbc48e97SDavid Xu 		mqnode_release(pn);
986ba0360b1SDavid Xu 		if (error)
987ba0360b1SDavid Xu 			mqfs_destroy(pn);
988ba0360b1SDavid Xu 		else
989ba0360b1SDavid Xu 			pn->mn_data = mq;
990ba0360b1SDavid Xu 	}
991ba0360b1SDavid Xu 	if (error)
992ba0360b1SDavid Xu 		mqueue_free(mq);
993655291f2SDavid Xu 	return (error);
994655291f2SDavid Xu }
995655291f2SDavid Xu 
996655291f2SDavid Xu /*
997655291f2SDavid Xu  * Remove an entry
998655291f2SDavid Xu  */
999655291f2SDavid Xu static
1000655291f2SDavid Xu int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
1001655291f2SDavid Xu {
1002655291f2SDavid Xu 	struct mqfs_node *parent;
1003655291f2SDavid Xu 	struct mqfs_vdata *vd;
1004655291f2SDavid Xu 	int error = 0;
1005655291f2SDavid Xu 
1006655291f2SDavid Xu 	sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1007655291f2SDavid Xu 
1008655291f2SDavid Xu 	if (ucred->cr_uid != pn->mn_uid &&
100932f9753cSRobert Watson 	    (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1010655291f2SDavid Xu 		error = EACCES;
1011655291f2SDavid Xu 	else if (!pn->mn_deleted) {
1012655291f2SDavid Xu 		parent = pn->mn_parent;
1013655291f2SDavid Xu 		pn->mn_parent = NULL;
1014655291f2SDavid Xu 		pn->mn_deleted = 1;
1015655291f2SDavid Xu 		LIST_REMOVE(pn, mn_sibling);
1016655291f2SDavid Xu 		LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1017655291f2SDavid Xu 			cache_purge(vd->mv_vnode);
1018655291f2SDavid Xu 			vhold(vd->mv_vnode);
1019655291f2SDavid Xu 			taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1020655291f2SDavid Xu 		}
1021655291f2SDavid Xu 		mqnode_release(pn);
1022655291f2SDavid Xu 		mqnode_release(parent);
1023655291f2SDavid Xu 	} else
1024655291f2SDavid Xu 		error = ENOENT;
1025655291f2SDavid Xu 	return (error);
1026655291f2SDavid Xu }
1027655291f2SDavid Xu 
1028655291f2SDavid Xu #if 0
1029655291f2SDavid Xu struct vop_remove_args {
1030655291f2SDavid Xu 	struct vnode *a_dvp;
1031655291f2SDavid Xu 	struct vnode *a_vp;
1032655291f2SDavid Xu 	struct componentname *a_cnp;
1033655291f2SDavid Xu };
1034655291f2SDavid Xu #endif
1035655291f2SDavid Xu 
1036655291f2SDavid Xu /*
1037655291f2SDavid Xu  * vnode removal operation
1038655291f2SDavid Xu  */
1039655291f2SDavid Xu static int
1040655291f2SDavid Xu mqfs_remove(struct vop_remove_args *ap)
1041655291f2SDavid Xu {
1042655291f2SDavid Xu 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1043655291f2SDavid Xu 	struct mqfs_node *pn;
1044655291f2SDavid Xu 	int error;
1045655291f2SDavid Xu 
1046655291f2SDavid Xu 	if (ap->a_vp->v_type == VDIR)
1047655291f2SDavid Xu                 return (EPERM);
1048655291f2SDavid Xu 	pn = VTON(ap->a_vp);
1049655291f2SDavid Xu 	sx_xlock(&mqfs->mi_lock);
1050655291f2SDavid Xu 	error = do_unlink(pn, ap->a_cnp->cn_cred);
1051655291f2SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
1052655291f2SDavid Xu 	return (error);
1053655291f2SDavid Xu }
1054655291f2SDavid Xu 
1055655291f2SDavid Xu #if 0
1056655291f2SDavid Xu struct vop_inactive_args {
1057655291f2SDavid Xu 	struct vnode *a_vp;
1058655291f2SDavid Xu 	struct thread *a_td;
1059655291f2SDavid Xu };
1060655291f2SDavid Xu #endif
1061655291f2SDavid Xu 
1062655291f2SDavid Xu static int
1063655291f2SDavid Xu mqfs_inactive(struct vop_inactive_args *ap)
1064655291f2SDavid Xu {
1065655291f2SDavid Xu 	struct mqfs_node *pn = VTON(ap->a_vp);
1066655291f2SDavid Xu 
1067655291f2SDavid Xu 	if (pn->mn_deleted)
1068655291f2SDavid Xu 		vrecycle(ap->a_vp, ap->a_td);
1069655291f2SDavid Xu 	return (0);
1070655291f2SDavid Xu }
1071655291f2SDavid Xu 
1072655291f2SDavid Xu #if 0
1073655291f2SDavid Xu struct vop_reclaim_args {
1074655291f2SDavid Xu 	struct vop_generic_args a_gen;
1075655291f2SDavid Xu 	struct vnode *a_vp;
1076655291f2SDavid Xu 	struct thread *a_td;
1077655291f2SDavid Xu };
1078655291f2SDavid Xu #endif
1079655291f2SDavid Xu 
1080655291f2SDavid Xu static int
1081655291f2SDavid Xu mqfs_reclaim(struct vop_reclaim_args *ap)
1082655291f2SDavid Xu {
1083655291f2SDavid Xu 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1084655291f2SDavid Xu 	struct vnode *vp = ap->a_vp;
1085655291f2SDavid Xu 	struct mqfs_node *pn;
1086655291f2SDavid Xu 	struct mqfs_vdata *vd;
1087655291f2SDavid Xu 
1088655291f2SDavid Xu 	vd = vp->v_data;
1089655291f2SDavid Xu 	pn = vd->mv_node;
1090655291f2SDavid Xu 	sx_xlock(&mqfs->mi_lock);
1091655291f2SDavid Xu 	vp->v_data = NULL;
1092655291f2SDavid Xu 	LIST_REMOVE(vd, mv_link);
1093655291f2SDavid Xu 	uma_zfree(mvdata_zone, vd);
1094655291f2SDavid Xu 	mqnode_release(pn);
1095655291f2SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
1096655291f2SDavid Xu 	return (0);
1097655291f2SDavid Xu }
1098655291f2SDavid Xu 
1099655291f2SDavid Xu #if 0
1100655291f2SDavid Xu struct vop_open_args {
1101655291f2SDavid Xu 	struct vop_generic_args a_gen;
1102655291f2SDavid Xu 	struct vnode *a_vp;
1103655291f2SDavid Xu 	int a_mode;
1104655291f2SDavid Xu 	struct ucred *a_cred;
1105655291f2SDavid Xu 	struct thread *a_td;
1106d6da6408SKonstantin Belousov 	struct file *a_fp;
1107655291f2SDavid Xu };
1108655291f2SDavid Xu #endif
1109655291f2SDavid Xu 
1110655291f2SDavid Xu static int
1111655291f2SDavid Xu mqfs_open(struct vop_open_args *ap)
1112655291f2SDavid Xu {
1113655291f2SDavid Xu 	return (0);
1114655291f2SDavid Xu }
1115655291f2SDavid Xu 
1116655291f2SDavid Xu #if 0
1117655291f2SDavid Xu struct vop_close_args {
1118655291f2SDavid Xu 	struct vop_generic_args a_gen;
1119655291f2SDavid Xu 	struct vnode *a_vp;
1120655291f2SDavid Xu 	int a_fflag;
1121655291f2SDavid Xu 	struct ucred *a_cred;
1122655291f2SDavid Xu 	struct thread *a_td;
1123655291f2SDavid Xu };
1124655291f2SDavid Xu #endif
1125655291f2SDavid Xu 
1126655291f2SDavid Xu static int
1127655291f2SDavid Xu mqfs_close(struct vop_close_args *ap)
1128655291f2SDavid Xu {
1129655291f2SDavid Xu 	return (0);
1130655291f2SDavid Xu }
1131655291f2SDavid Xu 
1132655291f2SDavid Xu #if 0
1133655291f2SDavid Xu struct vop_access_args {
1134655291f2SDavid Xu 	struct vop_generic_args a_gen;
1135655291f2SDavid Xu 	struct vnode *a_vp;
113615bc6b2bSEdward Tomasz Napierala 	accmode_t a_accmode;
1137655291f2SDavid Xu 	struct ucred *a_cred;
1138655291f2SDavid Xu 	struct thread *a_td;
1139655291f2SDavid Xu };
1140655291f2SDavid Xu #endif
1141655291f2SDavid Xu 
1142655291f2SDavid Xu /*
1143655291f2SDavid Xu  * Verify permissions
1144655291f2SDavid Xu  */
1145655291f2SDavid Xu static int
1146655291f2SDavid Xu mqfs_access(struct vop_access_args *ap)
1147655291f2SDavid Xu {
1148655291f2SDavid Xu 	struct vnode *vp = ap->a_vp;
1149655291f2SDavid Xu 	struct vattr vattr;
1150655291f2SDavid Xu 	int error;
1151655291f2SDavid Xu 
11520359a12eSAttilio Rao 	error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1153655291f2SDavid Xu 	if (error)
1154655291f2SDavid Xu 		return (error);
1155655291f2SDavid Xu 	error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
115615bc6b2bSEdward Tomasz Napierala 	    vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1157655291f2SDavid Xu 	return (error);
1158655291f2SDavid Xu }
1159655291f2SDavid Xu 
1160655291f2SDavid Xu #if 0
1161655291f2SDavid Xu struct vop_getattr_args {
1162655291f2SDavid Xu 	struct vop_generic_args a_gen;
1163655291f2SDavid Xu 	struct vnode *a_vp;
1164655291f2SDavid Xu 	struct vattr *a_vap;
1165655291f2SDavid Xu 	struct ucred *a_cred;
1166655291f2SDavid Xu };
1167655291f2SDavid Xu #endif
1168655291f2SDavid Xu 
1169655291f2SDavid Xu /*
1170655291f2SDavid Xu  * Get file attributes
1171655291f2SDavid Xu  */
1172655291f2SDavid Xu static int
1173655291f2SDavid Xu mqfs_getattr(struct vop_getattr_args *ap)
1174655291f2SDavid Xu {
1175655291f2SDavid Xu 	struct vnode *vp = ap->a_vp;
1176655291f2SDavid Xu 	struct mqfs_node *pn = VTON(vp);
1177655291f2SDavid Xu 	struct vattr *vap = ap->a_vap;
1178655291f2SDavid Xu 	int error = 0;
1179655291f2SDavid Xu 
1180655291f2SDavid Xu 	vap->va_type = vp->v_type;
1181655291f2SDavid Xu 	vap->va_mode = pn->mn_mode;
1182655291f2SDavid Xu 	vap->va_nlink = 1;
1183655291f2SDavid Xu 	vap->va_uid = pn->mn_uid;
1184655291f2SDavid Xu 	vap->va_gid = pn->mn_gid;
1185655291f2SDavid Xu 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1186655291f2SDavid Xu 	vap->va_fileid = pn->mn_fileno;
1187655291f2SDavid Xu 	vap->va_size = 0;
1188655291f2SDavid Xu 	vap->va_blocksize = PAGE_SIZE;
1189655291f2SDavid Xu 	vap->va_bytes = vap->va_size = 0;
1190655291f2SDavid Xu 	vap->va_atime = pn->mn_atime;
1191655291f2SDavid Xu 	vap->va_mtime = pn->mn_mtime;
1192655291f2SDavid Xu 	vap->va_ctime = pn->mn_ctime;
1193655291f2SDavid Xu 	vap->va_birthtime = pn->mn_birth;
1194655291f2SDavid Xu 	vap->va_gen = 0;
1195655291f2SDavid Xu 	vap->va_flags = 0;
11964c5a20e3SKonstantin Belousov 	vap->va_rdev = NODEV;
1197655291f2SDavid Xu 	vap->va_bytes = 0;
1198655291f2SDavid Xu 	vap->va_filerev = 0;
1199655291f2SDavid Xu 	return (error);
1200655291f2SDavid Xu }
1201655291f2SDavid Xu 
1202655291f2SDavid Xu #if 0
1203655291f2SDavid Xu struct vop_setattr_args {
1204655291f2SDavid Xu 	struct vop_generic_args a_gen;
1205655291f2SDavid Xu 	struct vnode *a_vp;
1206655291f2SDavid Xu 	struct vattr *a_vap;
1207655291f2SDavid Xu 	struct ucred *a_cred;
1208655291f2SDavid Xu };
1209655291f2SDavid Xu #endif
1210655291f2SDavid Xu /*
1211655291f2SDavid Xu  * Set attributes
1212655291f2SDavid Xu  */
1213655291f2SDavid Xu static int
1214655291f2SDavid Xu mqfs_setattr(struct vop_setattr_args *ap)
1215655291f2SDavid Xu {
1216655291f2SDavid Xu 	struct mqfs_node *pn;
1217655291f2SDavid Xu 	struct vattr *vap;
1218655291f2SDavid Xu 	struct vnode *vp;
12190359a12eSAttilio Rao 	struct thread *td;
1220655291f2SDavid Xu 	int c, error;
1221655291f2SDavid Xu 	uid_t uid;
1222655291f2SDavid Xu 	gid_t gid;
1223655291f2SDavid Xu 
12240359a12eSAttilio Rao 	td = curthread;
1225655291f2SDavid Xu 	vap = ap->a_vap;
1226655291f2SDavid Xu 	vp = ap->a_vp;
1227655291f2SDavid Xu 	if ((vap->va_type != VNON) ||
1228655291f2SDavid Xu 	    (vap->va_nlink != VNOVAL) ||
1229655291f2SDavid Xu 	    (vap->va_fsid != VNOVAL) ||
1230655291f2SDavid Xu 	    (vap->va_fileid != VNOVAL) ||
1231655291f2SDavid Xu 	    (vap->va_blocksize != VNOVAL) ||
1232655291f2SDavid Xu 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1233655291f2SDavid Xu 	    (vap->va_rdev != VNOVAL) ||
1234655291f2SDavid Xu 	    ((int)vap->va_bytes != VNOVAL) ||
1235655291f2SDavid Xu 	    (vap->va_gen != VNOVAL)) {
1236655291f2SDavid Xu 		return (EINVAL);
1237655291f2SDavid Xu 	}
1238655291f2SDavid Xu 
1239655291f2SDavid Xu 	pn = VTON(vp);
1240655291f2SDavid Xu 
1241655291f2SDavid Xu 	error = c = 0;
1242655291f2SDavid Xu 	if (vap->va_uid == (uid_t)VNOVAL)
1243655291f2SDavid Xu 		uid = pn->mn_uid;
1244655291f2SDavid Xu 	else
1245655291f2SDavid Xu 		uid = vap->va_uid;
1246655291f2SDavid Xu 	if (vap->va_gid == (gid_t)VNOVAL)
1247655291f2SDavid Xu 		gid = pn->mn_gid;
1248655291f2SDavid Xu 	else
1249655291f2SDavid Xu 		gid = vap->va_gid;
1250655291f2SDavid Xu 
1251655291f2SDavid Xu 	if (uid != pn->mn_uid || gid != pn->mn_gid) {
1252655291f2SDavid Xu 		/*
1253655291f2SDavid Xu 		 * To modify the ownership of a file, must possess VADMIN
1254655291f2SDavid Xu 		 * for that file.
1255655291f2SDavid Xu 		 */
12560359a12eSAttilio Rao 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1257655291f2SDavid Xu 			return (error);
1258acd3428bSRobert Watson 
1259acd3428bSRobert Watson 		/*
1260acd3428bSRobert Watson 		 * XXXRW: Why is there a privilege check here: shouldn't the
1261acd3428bSRobert Watson 		 * check in VOP_ACCESS() be enough?  Also, are the group bits
1262acd3428bSRobert Watson 		 * below definitely right?
1263acd3428bSRobert Watson 		 */
1264655291f2SDavid Xu 		if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1265655291f2SDavid Xu 		    (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
12660359a12eSAttilio Rao 		    (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1267655291f2SDavid Xu 			return (error);
1268655291f2SDavid Xu 		pn->mn_uid = uid;
1269655291f2SDavid Xu 		pn->mn_gid = gid;
1270655291f2SDavid Xu 		c = 1;
1271655291f2SDavid Xu 	}
1272655291f2SDavid Xu 
1273655291f2SDavid Xu 	if (vap->va_mode != (mode_t)VNOVAL) {
1274655291f2SDavid Xu 		if ((ap->a_cred->cr_uid != pn->mn_uid) &&
12750359a12eSAttilio Rao 		    (error = priv_check(td, PRIV_MQ_ADMIN)))
1276655291f2SDavid Xu 			return (error);
1277655291f2SDavid Xu 		pn->mn_mode = vap->va_mode;
1278655291f2SDavid Xu 		c = 1;
1279655291f2SDavid Xu 	}
1280655291f2SDavid Xu 
1281655291f2SDavid Xu 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1282655291f2SDavid Xu 		/* See the comment in ufs_vnops::ufs_setattr(). */
12830359a12eSAttilio Rao 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1284655291f2SDavid Xu 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
12850359a12eSAttilio Rao 		    (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1286655291f2SDavid Xu 			return (error);
1287655291f2SDavid Xu 		if (vap->va_atime.tv_sec != VNOVAL) {
1288655291f2SDavid Xu 			pn->mn_atime = vap->va_atime;
1289655291f2SDavid Xu 		}
1290655291f2SDavid Xu 		if (vap->va_mtime.tv_sec != VNOVAL) {
1291655291f2SDavid Xu 			pn->mn_mtime = vap->va_mtime;
1292655291f2SDavid Xu 		}
1293655291f2SDavid Xu 		c = 1;
1294655291f2SDavid Xu 	}
1295655291f2SDavid Xu 	if (c) {
1296655291f2SDavid Xu 		vfs_timestamp(&pn->mn_ctime);
1297655291f2SDavid Xu 	}
1298655291f2SDavid Xu 	return (0);
1299655291f2SDavid Xu }
1300655291f2SDavid Xu 
1301655291f2SDavid Xu #if 0
1302655291f2SDavid Xu struct vop_read_args {
1303655291f2SDavid Xu 	struct vop_generic_args a_gen;
1304655291f2SDavid Xu 	struct vnode *a_vp;
1305655291f2SDavid Xu 	struct uio *a_uio;
1306655291f2SDavid Xu 	int a_ioflag;
1307655291f2SDavid Xu 	struct ucred *a_cred;
1308655291f2SDavid Xu };
1309655291f2SDavid Xu #endif
1310655291f2SDavid Xu 
1311655291f2SDavid Xu /*
1312655291f2SDavid Xu  * Read from a file
1313655291f2SDavid Xu  */
1314655291f2SDavid Xu static int
1315655291f2SDavid Xu mqfs_read(struct vop_read_args *ap)
1316655291f2SDavid Xu {
1317655291f2SDavid Xu 	char buf[80];
1318655291f2SDavid Xu 	struct vnode *vp = ap->a_vp;
1319655291f2SDavid Xu 	struct uio *uio = ap->a_uio;
1320655291f2SDavid Xu 	struct mqfs_node *pn;
1321655291f2SDavid Xu 	struct mqueue *mq;
1322655291f2SDavid Xu 	int len, error;
1323655291f2SDavid Xu 
1324655291f2SDavid Xu 	if (vp->v_type != VREG)
1325655291f2SDavid Xu 		return (EINVAL);
1326655291f2SDavid Xu 
1327655291f2SDavid Xu 	pn = VTON(vp);
1328655291f2SDavid Xu 	mq = VTOMQ(vp);
1329655291f2SDavid Xu 	snprintf(buf, sizeof(buf),
1330655291f2SDavid Xu 		"QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1331655291f2SDavid Xu 		mq->mq_totalbytes,
1332655291f2SDavid Xu 		mq->mq_maxmsg,
1333655291f2SDavid Xu 		mq->mq_curmsgs,
1334655291f2SDavid Xu 		mq->mq_msgsize);
1335655291f2SDavid Xu 	buf[sizeof(buf)-1] = '\0';
1336655291f2SDavid Xu 	len = strlen(buf);
1337655291f2SDavid Xu 	error = uiomove_frombuf(buf, len, uio);
1338655291f2SDavid Xu 	return (error);
1339655291f2SDavid Xu }
1340655291f2SDavid Xu 
1341655291f2SDavid Xu #if 0
1342655291f2SDavid Xu struct vop_readdir_args {
1343655291f2SDavid Xu 	struct vop_generic_args a_gen;
1344655291f2SDavid Xu 	struct vnode *a_vp;
1345655291f2SDavid Xu 	struct uio *a_uio;
1346655291f2SDavid Xu 	struct ucred *a_cred;
1347655291f2SDavid Xu 	int *a_eofflag;
1348655291f2SDavid Xu 	int *a_ncookies;
1349655291f2SDavid Xu 	u_long **a_cookies;
1350655291f2SDavid Xu };
1351655291f2SDavid Xu #endif
1352655291f2SDavid Xu 
1353655291f2SDavid Xu /*
1354655291f2SDavid Xu  * Return directory entries.
1355655291f2SDavid Xu  */
1356655291f2SDavid Xu static int
1357655291f2SDavid Xu mqfs_readdir(struct vop_readdir_args *ap)
1358655291f2SDavid Xu {
1359655291f2SDavid Xu 	struct vnode *vp;
1360655291f2SDavid Xu 	struct mqfs_info *mi;
1361655291f2SDavid Xu 	struct mqfs_node *pd;
1362655291f2SDavid Xu 	struct mqfs_node *pn;
1363655291f2SDavid Xu 	struct dirent entry;
1364655291f2SDavid Xu 	struct uio *uio;
1365655291f2SDavid Xu 	int *tmp_ncookies = NULL;
1366655291f2SDavid Xu 	off_t offset;
1367655291f2SDavid Xu 	int error, i;
1368655291f2SDavid Xu 
1369655291f2SDavid Xu 	vp = ap->a_vp;
1370655291f2SDavid Xu 	mi = VFSTOMQFS(vp->v_mount);
1371655291f2SDavid Xu 	pd = VTON(vp);
1372655291f2SDavid Xu 	uio = ap->a_uio;
1373655291f2SDavid Xu 
1374655291f2SDavid Xu 	if (vp->v_type != VDIR)
1375655291f2SDavid Xu 		return (ENOTDIR);
1376655291f2SDavid Xu 
1377655291f2SDavid Xu 	if (uio->uio_offset < 0)
1378655291f2SDavid Xu 		return (EINVAL);
1379655291f2SDavid Xu 
1380655291f2SDavid Xu 	if (ap->a_ncookies != NULL) {
1381655291f2SDavid Xu 		tmp_ncookies = ap->a_ncookies;
1382655291f2SDavid Xu 		*ap->a_ncookies = 0;
1383655291f2SDavid Xu 		ap->a_ncookies = NULL;
1384655291f2SDavid Xu         }
1385655291f2SDavid Xu 
1386655291f2SDavid Xu 	error = 0;
1387655291f2SDavid Xu 	offset = 0;
1388655291f2SDavid Xu 
1389655291f2SDavid Xu 	sx_xlock(&mi->mi_lock);
1390655291f2SDavid Xu 
1391655291f2SDavid Xu 	LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1392655291f2SDavid Xu 		entry.d_reclen = sizeof(entry);
1393655291f2SDavid Xu 		if (!pn->mn_fileno)
1394655291f2SDavid Xu 			mqfs_fileno_alloc(mi, pn);
1395655291f2SDavid Xu 		entry.d_fileno = pn->mn_fileno;
1396655291f2SDavid Xu 		for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1397655291f2SDavid Xu 			entry.d_name[i] = pn->mn_name[i];
1398655291f2SDavid Xu 		entry.d_name[i] = 0;
1399655291f2SDavid Xu 		entry.d_namlen = i;
1400655291f2SDavid Xu 		switch (pn->mn_type) {
1401655291f2SDavid Xu 		case mqfstype_root:
1402655291f2SDavid Xu 		case mqfstype_dir:
1403655291f2SDavid Xu 		case mqfstype_this:
1404655291f2SDavid Xu 		case mqfstype_parent:
1405655291f2SDavid Xu 			entry.d_type = DT_DIR;
1406655291f2SDavid Xu 			break;
1407655291f2SDavid Xu 		case mqfstype_file:
1408655291f2SDavid Xu 			entry.d_type = DT_REG;
1409655291f2SDavid Xu 			break;
1410655291f2SDavid Xu 		case mqfstype_symlink:
1411655291f2SDavid Xu 			entry.d_type = DT_LNK;
1412655291f2SDavid Xu 			break;
1413655291f2SDavid Xu 		default:
1414655291f2SDavid Xu 			panic("%s has unexpected node type: %d", pn->mn_name,
1415655291f2SDavid Xu 				pn->mn_type);
1416655291f2SDavid Xu 		}
1417655291f2SDavid Xu 		if (entry.d_reclen > uio->uio_resid)
1418655291f2SDavid Xu                         break;
1419655291f2SDavid Xu 		if (offset >= uio->uio_offset) {
1420655291f2SDavid Xu 			error = vfs_read_dirent(ap, &entry, offset);
1421655291f2SDavid Xu                         if (error)
1422655291f2SDavid Xu                                 break;
1423655291f2SDavid Xu                 }
1424655291f2SDavid Xu                 offset += entry.d_reclen;
1425655291f2SDavid Xu 	}
1426655291f2SDavid Xu 	sx_xunlock(&mi->mi_lock);
1427655291f2SDavid Xu 
1428655291f2SDavid Xu 	uio->uio_offset = offset;
1429655291f2SDavid Xu 
1430655291f2SDavid Xu 	if (tmp_ncookies != NULL)
1431655291f2SDavid Xu 		ap->a_ncookies = tmp_ncookies;
1432655291f2SDavid Xu 
1433655291f2SDavid Xu 	return (error);
1434655291f2SDavid Xu }
1435655291f2SDavid Xu 
1436655291f2SDavid Xu #ifdef notyet
1437655291f2SDavid Xu 
1438655291f2SDavid Xu #if 0
1439655291f2SDavid Xu struct vop_mkdir_args {
1440655291f2SDavid Xu 	struct vnode *a_dvp;
1441655291f2SDavid Xu 	struvt vnode **a_vpp;
1442655291f2SDavid Xu 	struvt componentname *a_cnp;
1443655291f2SDavid Xu 	struct vattr *a_vap;
1444655291f2SDavid Xu };
1445655291f2SDavid Xu #endif
1446655291f2SDavid Xu 
1447655291f2SDavid Xu /*
1448655291f2SDavid Xu  * Create a directory.
1449655291f2SDavid Xu  */
1450655291f2SDavid Xu static int
1451655291f2SDavid Xu mqfs_mkdir(struct vop_mkdir_args *ap)
1452655291f2SDavid Xu {
1453655291f2SDavid Xu 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1454655291f2SDavid Xu 	struct componentname *cnp = ap->a_cnp;
1455655291f2SDavid Xu 	struct mqfs_node *pd = VTON(ap->a_dvp);
1456655291f2SDavid Xu 	struct mqfs_node *pn;
1457655291f2SDavid Xu 	int error;
1458655291f2SDavid Xu 
1459655291f2SDavid Xu 	if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1460655291f2SDavid Xu 		return (ENOTDIR);
1461655291f2SDavid Xu 	sx_xlock(&mqfs->mi_lock);
1462655291f2SDavid Xu 	if ((cnp->cn_flags & HASBUF) == 0)
1463655291f2SDavid Xu 		panic("%s: no name", __func__);
1464ba0360b1SDavid Xu 	pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1465ba0360b1SDavid Xu 		ap->a_vap->cn_cred, ap->a_vap->va_mode);
1466fbc48e97SDavid Xu 	if (pn != NULL)
1467fbc48e97SDavid Xu 		mqnode_addref(pn);
1468655291f2SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
1469fbc48e97SDavid Xu 	if (pn == NULL) {
1470fbc48e97SDavid Xu 		error = ENOSPC;
1471fbc48e97SDavid Xu 	} else {
1472fbc48e97SDavid Xu 		error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1473fbc48e97SDavid Xu 		mqnode_release(pn);
1474fbc48e97SDavid Xu 	}
1475655291f2SDavid Xu 	return (error);
1476655291f2SDavid Xu }
1477655291f2SDavid Xu 
1478655291f2SDavid Xu #if 0
1479655291f2SDavid Xu struct vop_rmdir_args {
1480655291f2SDavid Xu 	struct vnode *a_dvp;
1481655291f2SDavid Xu 	struct vnode *a_vp;
1482655291f2SDavid Xu 	struct componentname *a_cnp;
1483655291f2SDavid Xu };
1484655291f2SDavid Xu #endif
1485655291f2SDavid Xu 
1486655291f2SDavid Xu /*
1487655291f2SDavid Xu  * Remove a directory.
1488655291f2SDavid Xu  */
1489655291f2SDavid Xu static int
1490655291f2SDavid Xu mqfs_rmdir(struct vop_rmdir_args *ap)
1491655291f2SDavid Xu {
1492655291f2SDavid Xu 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1493655291f2SDavid Xu 	struct mqfs_node *pn = VTON(ap->a_vp);
1494655291f2SDavid Xu 	struct mqfs_node *pt;
1495655291f2SDavid Xu 
1496655291f2SDavid Xu 	if (pn->mn_type != mqfstype_dir)
1497655291f2SDavid Xu 		return (ENOTDIR);
1498655291f2SDavid Xu 
1499655291f2SDavid Xu 	sx_xlock(&mqfs->mi_lock);
1500655291f2SDavid Xu 	if (pn->mn_deleted) {
1501655291f2SDavid Xu 		sx_xunlock(&mqfs->mi_lock);
1502655291f2SDavid Xu 		return (ENOENT);
1503655291f2SDavid Xu 	}
1504655291f2SDavid Xu 
1505655291f2SDavid Xu 	pt = LIST_FIRST(&pn->mn_children);
1506655291f2SDavid Xu 	pt = LIST_NEXT(pt, mn_sibling);
1507655291f2SDavid Xu 	pt = LIST_NEXT(pt, mn_sibling);
1508655291f2SDavid Xu 	if (pt != NULL) {
1509655291f2SDavid Xu 		sx_xunlock(&mqfs->mi_lock);
1510655291f2SDavid Xu 		return (ENOTEMPTY);
1511655291f2SDavid Xu 	}
1512655291f2SDavid Xu 	pt = pn->mn_parent;
1513655291f2SDavid Xu 	pn->mn_parent = NULL;
1514655291f2SDavid Xu 	pn->mn_deleted = 1;
1515655291f2SDavid Xu 	LIST_REMOVE(pn, mn_sibling);
1516655291f2SDavid Xu 	mqnode_release(pn);
1517655291f2SDavid Xu 	mqnode_release(pt);
1518655291f2SDavid Xu 	sx_xunlock(&mqfs->mi_lock);
1519655291f2SDavid Xu 	cache_purge(ap->a_vp);
1520655291f2SDavid Xu 	return (0);
1521655291f2SDavid Xu }
1522655291f2SDavid Xu 
1523655291f2SDavid Xu #endif /* notyet */
1524655291f2SDavid Xu 
1525655291f2SDavid Xu /*
1526655291f2SDavid Xu  * Allocate a message queue
1527655291f2SDavid Xu  */
1528655291f2SDavid Xu static struct mqueue *
1529655291f2SDavid Xu mqueue_alloc(const struct mq_attr *attr)
1530655291f2SDavid Xu {
1531655291f2SDavid Xu 	struct mqueue *mq;
1532655291f2SDavid Xu 
1533655291f2SDavid Xu 	if (curmq >= maxmq)
1534655291f2SDavid Xu 		return (NULL);
1535655291f2SDavid Xu 	mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1536655291f2SDavid Xu 	TAILQ_INIT(&mq->mq_msgq);
1537655291f2SDavid Xu 	if (attr != NULL) {
1538655291f2SDavid Xu 		mq->mq_maxmsg = attr->mq_maxmsg;
1539655291f2SDavid Xu 		mq->mq_msgsize = attr->mq_msgsize;
1540655291f2SDavid Xu 	} else {
1541655291f2SDavid Xu 		mq->mq_maxmsg = default_maxmsg;
1542655291f2SDavid Xu 		mq->mq_msgsize = default_msgsize;
1543655291f2SDavid Xu 	}
1544b042e976SDavid Xu 	mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1545d8b0556cSKonstantin Belousov 	knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1546d8b0556cSKonstantin Belousov 	knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1547655291f2SDavid Xu 	atomic_add_int(&curmq, 1);
1548655291f2SDavid Xu 	return (mq);
1549655291f2SDavid Xu }
1550655291f2SDavid Xu 
1551655291f2SDavid Xu /*
1552655291f2SDavid Xu  * Destroy a message queue
1553655291f2SDavid Xu  */
1554655291f2SDavid Xu static void
1555655291f2SDavid Xu mqueue_free(struct mqueue *mq)
1556655291f2SDavid Xu {
1557655291f2SDavid Xu 	struct mqueue_msg *msg;
1558655291f2SDavid Xu 
1559655291f2SDavid Xu 	while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1560655291f2SDavid Xu 		TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
15611ede983cSDag-Erling Smørgrav 		free(msg, M_MQUEUEDATA);
1562655291f2SDavid Xu 	}
1563655291f2SDavid Xu 
1564655291f2SDavid Xu 	mtx_destroy(&mq->mq_mutex);
15656aba400aSAttilio Rao 	seldrain(&mq->mq_rsel);
15666aba400aSAttilio Rao 	seldrain(&mq->mq_wsel);
1567655291f2SDavid Xu 	knlist_destroy(&mq->mq_rsel.si_note);
1568655291f2SDavid Xu 	knlist_destroy(&mq->mq_wsel.si_note);
1569655291f2SDavid Xu 	uma_zfree(mqueue_zone, mq);
1570655291f2SDavid Xu 	atomic_add_int(&curmq, -1);
1571655291f2SDavid Xu }
1572655291f2SDavid Xu 
1573655291f2SDavid Xu /*
1574655291f2SDavid Xu  * Load a message from user space
1575655291f2SDavid Xu  */
1576655291f2SDavid Xu static struct mqueue_msg *
1577655291f2SDavid Xu mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1578655291f2SDavid Xu {
1579655291f2SDavid Xu 	struct mqueue_msg *msg;
1580655291f2SDavid Xu 	size_t len;
1581655291f2SDavid Xu 	int error;
1582655291f2SDavid Xu 
1583655291f2SDavid Xu 	len = sizeof(struct mqueue_msg) + msg_size;
15841ede983cSDag-Erling Smørgrav 	msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1585655291f2SDavid Xu 	error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1586655291f2SDavid Xu 	    msg_size);
1587655291f2SDavid Xu 	if (error) {
15881ede983cSDag-Erling Smørgrav 		free(msg, M_MQUEUEDATA);
1589655291f2SDavid Xu 		msg = NULL;
1590655291f2SDavid Xu 	} else {
1591655291f2SDavid Xu 		msg->msg_size = msg_size;
1592655291f2SDavid Xu 		msg->msg_prio = msg_prio;
1593655291f2SDavid Xu 	}
1594655291f2SDavid Xu 	return (msg);
1595655291f2SDavid Xu }
1596655291f2SDavid Xu 
1597655291f2SDavid Xu /*
1598655291f2SDavid Xu  * Save a message to user space
1599655291f2SDavid Xu  */
1600655291f2SDavid Xu static int
1601655291f2SDavid Xu mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1602655291f2SDavid Xu {
1603655291f2SDavid Xu 	int error;
1604655291f2SDavid Xu 
1605655291f2SDavid Xu 	error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1606655291f2SDavid Xu 		msg->msg_size);
1607b2f92ef9SDavid Xu 	if (error == 0 && msg_prio != NULL)
1608655291f2SDavid Xu 		error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1609655291f2SDavid Xu 	return (error);
1610655291f2SDavid Xu }
1611655291f2SDavid Xu 
1612655291f2SDavid Xu /*
1613655291f2SDavid Xu  * Free a message's memory
1614655291f2SDavid Xu  */
1615655291f2SDavid Xu static __inline void
1616655291f2SDavid Xu mqueue_freemsg(struct mqueue_msg *msg)
1617655291f2SDavid Xu {
16181ede983cSDag-Erling Smørgrav 	free(msg, M_MQUEUEDATA);
1619655291f2SDavid Xu }
1620655291f2SDavid Xu 
1621655291f2SDavid Xu /*
1622655291f2SDavid Xu  * Send a message. if waitok is false, thread will not be
1623655291f2SDavid Xu  * blocked if there is no data in queue, otherwise, absolute
1624655291f2SDavid Xu  * time will be checked.
1625655291f2SDavid Xu  */
1626655291f2SDavid Xu int
1627655291f2SDavid Xu mqueue_send(struct mqueue *mq, const char *msg_ptr,
1628655291f2SDavid Xu 	size_t msg_len, unsigned msg_prio, int waitok,
1629655291f2SDavid Xu 	const struct timespec *abs_timeout)
1630655291f2SDavid Xu {
1631655291f2SDavid Xu 	struct mqueue_msg *msg;
1632afde2b65SKonstantin Belousov 	struct timespec ts, ts2;
1633655291f2SDavid Xu 	struct timeval tv;
1634655291f2SDavid Xu 	int error;
1635655291f2SDavid Xu 
1636a6de716dSDavid Xu 	if (msg_prio >= MQ_PRIO_MAX)
1637a6de716dSDavid Xu 		return (EINVAL);
1638655291f2SDavid Xu 	if (msg_len > mq->mq_msgsize)
1639655291f2SDavid Xu 		return (EMSGSIZE);
1640655291f2SDavid Xu 	msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1641655291f2SDavid Xu 	if (msg == NULL)
1642655291f2SDavid Xu 		return (EFAULT);
1643655291f2SDavid Xu 
1644655291f2SDavid Xu 	/* O_NONBLOCK case */
1645655291f2SDavid Xu 	if (!waitok) {
1646655291f2SDavid Xu 		error = _mqueue_send(mq, msg, -1);
1647655291f2SDavid Xu 		if (error)
1648655291f2SDavid Xu 			goto bad;
1649655291f2SDavid Xu 		return (0);
1650655291f2SDavid Xu 	}
1651655291f2SDavid Xu 
1652655291f2SDavid Xu 	/* we allow a null timeout (wait forever) */
1653655291f2SDavid Xu 	if (abs_timeout == NULL) {
1654655291f2SDavid Xu 		error = _mqueue_send(mq, msg, 0);
1655655291f2SDavid Xu 		if (error)
1656655291f2SDavid Xu 			goto bad;
1657655291f2SDavid Xu 		return (0);
1658655291f2SDavid Xu 	}
1659655291f2SDavid Xu 
1660655291f2SDavid Xu 	/* send it before checking time */
1661655291f2SDavid Xu 	error = _mqueue_send(mq, msg, -1);
1662655291f2SDavid Xu 	if (error == 0)
1663655291f2SDavid Xu 		return (0);
1664655291f2SDavid Xu 
1665655291f2SDavid Xu 	if (error != EAGAIN)
1666655291f2SDavid Xu 		goto bad;
1667655291f2SDavid Xu 
1668afde2b65SKonstantin Belousov 	if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1669655291f2SDavid Xu 		error = EINVAL;
1670655291f2SDavid Xu 		goto bad;
1671655291f2SDavid Xu 	}
1672655291f2SDavid Xu 	for (;;) {
1673afde2b65SKonstantin Belousov 		ts2 = *abs_timeout;
1674a6de716dSDavid Xu 		getnanotime(&ts);
1675655291f2SDavid Xu 		timespecsub(&ts2, &ts);
1676655291f2SDavid Xu 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1677655291f2SDavid Xu 			error = ETIMEDOUT;
1678655291f2SDavid Xu 			break;
1679655291f2SDavid Xu 		}
1680655291f2SDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1681655291f2SDavid Xu 		error = _mqueue_send(mq, msg, tvtohz(&tv));
1682655291f2SDavid Xu 		if (error != ETIMEDOUT)
1683655291f2SDavid Xu 			break;
1684655291f2SDavid Xu 	}
1685655291f2SDavid Xu 	if (error == 0)
1686655291f2SDavid Xu 		return (0);
1687655291f2SDavid Xu bad:
1688655291f2SDavid Xu 	mqueue_freemsg(msg);
1689655291f2SDavid Xu 	return (error);
1690655291f2SDavid Xu }
1691655291f2SDavid Xu 
1692655291f2SDavid Xu /*
1693655291f2SDavid Xu  * Common routine to send a message
1694655291f2SDavid Xu  */
1695655291f2SDavid Xu static int
1696655291f2SDavid Xu _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1697655291f2SDavid Xu {
1698655291f2SDavid Xu 	struct mqueue_msg *msg2;
1699655291f2SDavid Xu 	int error = 0;
1700655291f2SDavid Xu 
1701655291f2SDavid Xu 	mtx_lock(&mq->mq_mutex);
1702655291f2SDavid Xu 	while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1703655291f2SDavid Xu 		if (timo < 0) {
1704655291f2SDavid Xu 			mtx_unlock(&mq->mq_mutex);
1705655291f2SDavid Xu 			return (EAGAIN);
1706655291f2SDavid Xu 		}
1707655291f2SDavid Xu 		mq->mq_senders++;
1708655291f2SDavid Xu 		error = msleep(&mq->mq_senders, &mq->mq_mutex,
17090f180a7cSJohn Baldwin 			    PCATCH, "mqsend", timo);
1710655291f2SDavid Xu 		mq->mq_senders--;
1711655291f2SDavid Xu 		if (error == EAGAIN)
1712655291f2SDavid Xu 			error = ETIMEDOUT;
1713655291f2SDavid Xu 	}
1714655291f2SDavid Xu 	if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1715655291f2SDavid Xu 		mtx_unlock(&mq->mq_mutex);
1716655291f2SDavid Xu 		return (error);
1717655291f2SDavid Xu 	}
1718655291f2SDavid Xu 	error = 0;
1719655291f2SDavid Xu 	if (TAILQ_EMPTY(&mq->mq_msgq)) {
1720655291f2SDavid Xu 		TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1721655291f2SDavid Xu 	} else {
1722655291f2SDavid Xu 		if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1723655291f2SDavid Xu 			TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1724655291f2SDavid Xu 		} else {
1725655291f2SDavid Xu 			TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1726655291f2SDavid Xu 				if (msg2->msg_prio < msg->msg_prio)
1727655291f2SDavid Xu 					break;
1728655291f2SDavid Xu 			}
1729655291f2SDavid Xu 			TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1730655291f2SDavid Xu 		}
1731655291f2SDavid Xu 	}
1732655291f2SDavid Xu 	mq->mq_curmsgs++;
1733655291f2SDavid Xu 	mq->mq_totalbytes += msg->msg_size;
1734655291f2SDavid Xu 	if (mq->mq_receivers)
1735655291f2SDavid Xu 		wakeup_one(&mq->mq_receivers);
1736655291f2SDavid Xu 	else if (mq->mq_notifier != NULL)
1737655291f2SDavid Xu 		mqueue_send_notification(mq);
1738655291f2SDavid Xu 	if (mq->mq_flags & MQ_RSEL) {
1739655291f2SDavid Xu 		mq->mq_flags &= ~MQ_RSEL;
174003f70aecSDavid Xu 		selwakeup(&mq->mq_rsel);
1741655291f2SDavid Xu 	}
1742655291f2SDavid Xu 	KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1743655291f2SDavid Xu 	mtx_unlock(&mq->mq_mutex);
1744655291f2SDavid Xu 	return (0);
1745655291f2SDavid Xu }
1746655291f2SDavid Xu 
1747655291f2SDavid Xu /*
1748655291f2SDavid Xu  * Send realtime a signal to process which registered itself
1749655291f2SDavid Xu  * successfully by mq_notify.
1750655291f2SDavid Xu  */
1751655291f2SDavid Xu static void
1752655291f2SDavid Xu mqueue_send_notification(struct mqueue *mq)
1753655291f2SDavid Xu {
1754b2f92ef9SDavid Xu 	struct mqueue_notifier *nt;
1755cf7d9a8cSDavid Xu 	struct thread *td;
1756b2f92ef9SDavid Xu 	struct proc *p;
1757cf7d9a8cSDavid Xu 	int error;
1758655291f2SDavid Xu 
1759655291f2SDavid Xu 	mtx_assert(&mq->mq_mutex, MA_OWNED);
1760b2f92ef9SDavid Xu 	nt = mq->mq_notifier;
1761052ea11cSDavid Xu 	if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1762b2f92ef9SDavid Xu 		p = nt->nt_proc;
1763cf7d9a8cSDavid Xu 		error = sigev_findtd(p, &nt->nt_sigev, &td);
1764cf7d9a8cSDavid Xu 		if (error) {
1765cf7d9a8cSDavid Xu 			mq->mq_notifier = NULL;
1766cf7d9a8cSDavid Xu 			return;
1767cf7d9a8cSDavid Xu 		}
1768cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&nt->nt_ksi)) {
1769cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1770cf7d9a8cSDavid Xu 			tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1771cf7d9a8cSDavid Xu 		}
1772b2f92ef9SDavid Xu 		PROC_UNLOCK(p);
1773052ea11cSDavid Xu 	}
1774655291f2SDavid Xu 	mq->mq_notifier = NULL;
1775655291f2SDavid Xu }
1776655291f2SDavid Xu 
1777655291f2SDavid Xu /*
1778655291f2SDavid Xu  * Get a message. if waitok is false, thread will not be
1779655291f2SDavid Xu  * blocked if there is no data in queue, otherwise, absolute
1780655291f2SDavid Xu  * time will be checked.
1781655291f2SDavid Xu  */
1782655291f2SDavid Xu int
1783655291f2SDavid Xu mqueue_receive(struct mqueue *mq, char *msg_ptr,
1784655291f2SDavid Xu 	size_t msg_len, unsigned *msg_prio, int waitok,
1785655291f2SDavid Xu 	const struct timespec *abs_timeout)
1786655291f2SDavid Xu {
1787655291f2SDavid Xu 	struct mqueue_msg *msg;
1788afde2b65SKonstantin Belousov 	struct timespec ts, ts2;
1789655291f2SDavid Xu 	struct timeval tv;
1790655291f2SDavid Xu 	int error;
1791655291f2SDavid Xu 
1792655291f2SDavid Xu 	if (msg_len < mq->mq_msgsize)
1793655291f2SDavid Xu 		return (EMSGSIZE);
1794655291f2SDavid Xu 
1795655291f2SDavid Xu 	/* O_NONBLOCK case */
1796655291f2SDavid Xu 	if (!waitok) {
1797655291f2SDavid Xu 		error = _mqueue_recv(mq, &msg, -1);
1798655291f2SDavid Xu 		if (error)
1799655291f2SDavid Xu 			return (error);
1800655291f2SDavid Xu 		goto received;
1801655291f2SDavid Xu 	}
1802655291f2SDavid Xu 
1803655291f2SDavid Xu 	/* we allow a null timeout (wait forever). */
1804655291f2SDavid Xu 	if (abs_timeout == NULL) {
1805655291f2SDavid Xu 		error = _mqueue_recv(mq, &msg, 0);
1806655291f2SDavid Xu 		if (error)
1807655291f2SDavid Xu 			return (error);
1808655291f2SDavid Xu 		goto received;
1809655291f2SDavid Xu 	}
1810655291f2SDavid Xu 
1811655291f2SDavid Xu 	/* try to get a message before checking time */
1812655291f2SDavid Xu 	error = _mqueue_recv(mq, &msg, -1);
1813655291f2SDavid Xu 	if (error == 0)
1814655291f2SDavid Xu 		goto received;
1815655291f2SDavid Xu 
1816655291f2SDavid Xu 	if (error != EAGAIN)
1817655291f2SDavid Xu 		return (error);
1818655291f2SDavid Xu 
1819afde2b65SKonstantin Belousov 	if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1820655291f2SDavid Xu 		error = EINVAL;
1821655291f2SDavid Xu 		return (error);
1822655291f2SDavid Xu 	}
1823655291f2SDavid Xu 
1824655291f2SDavid Xu 	for (;;) {
1825afde2b65SKonstantin Belousov 		ts2 = *abs_timeout;
1826a6de716dSDavid Xu 		getnanotime(&ts);
1827655291f2SDavid Xu 		timespecsub(&ts2, &ts);
1828655291f2SDavid Xu 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1829655291f2SDavid Xu 			error = ETIMEDOUT;
1830655291f2SDavid Xu 			return (error);
1831655291f2SDavid Xu 		}
1832655291f2SDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1833655291f2SDavid Xu 		error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1834655291f2SDavid Xu 		if (error == 0)
1835655291f2SDavid Xu 			break;
1836655291f2SDavid Xu 		if (error != ETIMEDOUT)
1837655291f2SDavid Xu 			return (error);
1838655291f2SDavid Xu 	}
1839655291f2SDavid Xu 
1840655291f2SDavid Xu received:
1841655291f2SDavid Xu 	error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1842655291f2SDavid Xu 	if (error == 0) {
1843655291f2SDavid Xu 		curthread->td_retval[0] = msg->msg_size;
1844655291f2SDavid Xu 		curthread->td_retval[1] = 0;
1845655291f2SDavid Xu 	}
1846655291f2SDavid Xu 	mqueue_freemsg(msg);
1847655291f2SDavid Xu 	return (error);
1848655291f2SDavid Xu }
1849655291f2SDavid Xu 
1850655291f2SDavid Xu /*
1851655291f2SDavid Xu  * Common routine to receive a message
1852655291f2SDavid Xu  */
1853655291f2SDavid Xu static int
1854655291f2SDavid Xu _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1855655291f2SDavid Xu {
1856655291f2SDavid Xu 	int error = 0;
1857655291f2SDavid Xu 
1858655291f2SDavid Xu 	mtx_lock(&mq->mq_mutex);
1859655291f2SDavid Xu 	while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1860655291f2SDavid Xu 		if (timo < 0) {
1861655291f2SDavid Xu 			mtx_unlock(&mq->mq_mutex);
1862655291f2SDavid Xu 			return (EAGAIN);
1863655291f2SDavid Xu 		}
1864655291f2SDavid Xu 		mq->mq_receivers++;
1865655291f2SDavid Xu 		error = msleep(&mq->mq_receivers, &mq->mq_mutex,
18660f180a7cSJohn Baldwin 			    PCATCH, "mqrecv", timo);
1867655291f2SDavid Xu 		mq->mq_receivers--;
1868655291f2SDavid Xu 		if (error == EAGAIN)
1869655291f2SDavid Xu 			error = ETIMEDOUT;
1870655291f2SDavid Xu 	}
1871655291f2SDavid Xu 	if (*msg != NULL) {
1872655291f2SDavid Xu 		error = 0;
1873655291f2SDavid Xu 		TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1874655291f2SDavid Xu 		mq->mq_curmsgs--;
1875655291f2SDavid Xu 		mq->mq_totalbytes -= (*msg)->msg_size;
1876655291f2SDavid Xu 		if (mq->mq_senders)
1877655291f2SDavid Xu 			wakeup_one(&mq->mq_senders);
1878655291f2SDavid Xu 		if (mq->mq_flags & MQ_WSEL) {
1879655291f2SDavid Xu 			mq->mq_flags &= ~MQ_WSEL;
188003f70aecSDavid Xu 			selwakeup(&mq->mq_wsel);
1881655291f2SDavid Xu 		}
1882655291f2SDavid Xu 		KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1883655291f2SDavid Xu 	}
1884655291f2SDavid Xu 	if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1885655291f2SDavid Xu 	    !TAILQ_EMPTY(&mq->mq_msgq)) {
1886655291f2SDavid Xu 		mqueue_send_notification(mq);
1887655291f2SDavid Xu 	}
1888655291f2SDavid Xu 	mtx_unlock(&mq->mq_mutex);
1889655291f2SDavid Xu 	return (error);
1890655291f2SDavid Xu }
1891655291f2SDavid Xu 
1892b2f92ef9SDavid Xu static __inline struct mqueue_notifier *
1893b2f92ef9SDavid Xu notifier_alloc(void)
1894655291f2SDavid Xu {
1895b2f92ef9SDavid Xu 	return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1896655291f2SDavid Xu }
1897655291f2SDavid Xu 
1898655291f2SDavid Xu static __inline void
1899b2f92ef9SDavid Xu notifier_free(struct mqueue_notifier *p)
1900655291f2SDavid Xu {
1901b2f92ef9SDavid Xu 	uma_zfree(mqnoti_zone, p);
1902b2f92ef9SDavid Xu }
1903b2f92ef9SDavid Xu 
1904b2f92ef9SDavid Xu static struct mqueue_notifier *
1905b2f92ef9SDavid Xu notifier_search(struct proc *p, int fd)
1906b2f92ef9SDavid Xu {
1907b2f92ef9SDavid Xu 	struct mqueue_notifier *nt;
1908b2f92ef9SDavid Xu 
1909b2f92ef9SDavid Xu 	LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
19109da8a32aSDavid Xu 		if (nt->nt_ksi.ksi_mqd == fd)
1911b2f92ef9SDavid Xu 			break;
1912b2f92ef9SDavid Xu 	}
1913b2f92ef9SDavid Xu 	return (nt);
1914b2f92ef9SDavid Xu }
1915b2f92ef9SDavid Xu 
1916102178d0SDavid Xu static __inline void
1917b2f92ef9SDavid Xu notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1918b2f92ef9SDavid Xu {
1919b2f92ef9SDavid Xu 	LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1920b2f92ef9SDavid Xu }
1921b2f92ef9SDavid Xu 
1922102178d0SDavid Xu static __inline void
1923b2f92ef9SDavid Xu notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1924b2f92ef9SDavid Xu {
1925b2f92ef9SDavid Xu 	LIST_REMOVE(nt, nt_link);
1926b2f92ef9SDavid Xu 	notifier_free(nt);
1927b2f92ef9SDavid Xu }
1928b2f92ef9SDavid Xu 
1929b2f92ef9SDavid Xu static void
1930b2f92ef9SDavid Xu notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1931b2f92ef9SDavid Xu {
1932b2f92ef9SDavid Xu 	struct mqueue_notifier *nt;
1933b2f92ef9SDavid Xu 
1934b2f92ef9SDavid Xu 	mtx_assert(&mq->mq_mutex, MA_OWNED);
1935b2f92ef9SDavid Xu 	PROC_LOCK(p);
1936b2f92ef9SDavid Xu 	nt = notifier_search(p, fd);
1937b2f92ef9SDavid Xu 	if (nt != NULL) {
1938b2f92ef9SDavid Xu 		if (mq->mq_notifier == nt)
1939b2f92ef9SDavid Xu 			mq->mq_notifier = NULL;
1940b2f92ef9SDavid Xu 		sigqueue_take(&nt->nt_ksi);
1941b2f92ef9SDavid Xu 		notifier_delete(p, nt);
1942b2f92ef9SDavid Xu 	}
1943b2f92ef9SDavid Xu 	PROC_UNLOCK(p);
1944655291f2SDavid Xu }
1945655291f2SDavid Xu 
1946afde2b65SKonstantin Belousov static int
1947afde2b65SKonstantin Belousov kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
1948afde2b65SKonstantin Belousov     const struct mq_attr *attr)
1949655291f2SDavid Xu {
1950655291f2SDavid Xu 	char path[MQFS_NAMELEN + 1];
1951655291f2SDavid Xu 	struct mqfs_node *pn;
1952655291f2SDavid Xu 	struct filedesc *fdp;
1953655291f2SDavid Xu 	struct file *fp;
1954655291f2SDavid Xu 	struct mqueue *mq;
1955afde2b65SKonstantin Belousov 	int fd, error, len, cmode;
1956655291f2SDavid Xu 
1957655291f2SDavid Xu 	fdp = td->td_proc->p_fd;
1958afde2b65SKonstantin Belousov 	cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1959f72b11a4SDavid Xu 	mq = NULL;
1960afde2b65SKonstantin Belousov 	if ((flags & O_CREAT) != 0 && attr != NULL) {
1961afde2b65SKonstantin Belousov 		if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1962655291f2SDavid Xu 			return (EINVAL);
1963afde2b65SKonstantin Belousov 		if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1964655291f2SDavid Xu 			return (EINVAL);
1965afde2b65SKonstantin Belousov 	}
1966655291f2SDavid Xu 
1967afde2b65SKonstantin Belousov 	error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
1968655291f2SDavid Xu         if (error)
1969655291f2SDavid Xu 		return (error);
1970655291f2SDavid Xu 
1971655291f2SDavid Xu 	/*
1972655291f2SDavid Xu 	 * The first character of name must be a slash  (/) character
1973655291f2SDavid Xu 	 * and the remaining characters of name cannot include any slash
1974655291f2SDavid Xu 	 * characters.
1975655291f2SDavid Xu 	 */
1976655291f2SDavid Xu 	len = strlen(path);
1977*dc15eac0SEd Schouten 	if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
1978655291f2SDavid Xu 		return (EINVAL);
1979655291f2SDavid Xu 
19801fe80828SKonstantin Belousov 	error = falloc(td, &fp, &fd, 0);
1981655291f2SDavid Xu 	if (error)
1982655291f2SDavid Xu 		return (error);
1983655291f2SDavid Xu 
1984655291f2SDavid Xu 	sx_xlock(&mqfs_data.mi_lock);
1985655291f2SDavid Xu 	pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1986655291f2SDavid Xu 	if (pn == NULL) {
1987655291f2SDavid Xu 		if (!(flags & O_CREAT)) {
1988655291f2SDavid Xu 			error = ENOENT;
1989655291f2SDavid Xu 		} else {
1990afde2b65SKonstantin Belousov 			mq = mqueue_alloc(attr);
1991b2f92ef9SDavid Xu 			if (mq == NULL) {
1992b2f92ef9SDavid Xu 				error = ENFILE;
1993b2f92ef9SDavid Xu 			} else {
1994655291f2SDavid Xu 				pn = mqfs_create_file(mqfs_data.mi_root,
1995ba0360b1SDavid Xu 				         path + 1, len - 1, td->td_ucred,
1996ba0360b1SDavid Xu 					 cmode);
1997655291f2SDavid Xu 				if (pn == NULL) {
1998655291f2SDavid Xu 					error = ENOSPC;
1999b2f92ef9SDavid Xu 					mqueue_free(mq);
2000b2f92ef9SDavid Xu 				}
2001655291f2SDavid Xu 			}
2002655291f2SDavid Xu 		}
2003655291f2SDavid Xu 
2004655291f2SDavid Xu 		if (error == 0) {
2005655291f2SDavid Xu 			pn->mn_data = mq;
2006655291f2SDavid Xu 		}
2007655291f2SDavid Xu 	} else {
2008655291f2SDavid Xu 		if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2009655291f2SDavid Xu 			error = EEXIST;
2010655291f2SDavid Xu 		} else {
201115bc6b2bSEdward Tomasz Napierala 			accmode_t accmode = 0;
2012655291f2SDavid Xu 
2013655291f2SDavid Xu 			if (flags & FREAD)
201415bc6b2bSEdward Tomasz Napierala 				accmode |= VREAD;
2015655291f2SDavid Xu 			if (flags & FWRITE)
201615bc6b2bSEdward Tomasz Napierala 				accmode |= VWRITE;
2017655291f2SDavid Xu 			error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
201815bc6b2bSEdward Tomasz Napierala 				    pn->mn_gid, accmode, td->td_ucred, NULL);
2019655291f2SDavid Xu 		}
2020655291f2SDavid Xu 	}
2021655291f2SDavid Xu 
2022655291f2SDavid Xu 	if (error) {
2023655291f2SDavid Xu 		sx_xunlock(&mqfs_data.mi_lock);
2024655291f2SDavid Xu 		fdclose(fdp, fp, fd, td);
2025655291f2SDavid Xu 		fdrop(fp, td);
2026655291f2SDavid Xu 		return (error);
2027655291f2SDavid Xu 	}
2028655291f2SDavid Xu 
2029655291f2SDavid Xu 	mqnode_addref(pn);
2030655291f2SDavid Xu 	sx_xunlock(&mqfs_data.mi_lock);
2031655291f2SDavid Xu 
2032397c19d1SJeff Roberson 	finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2033397c19d1SJeff Roberson 	    &mqueueops);
2034655291f2SDavid Xu 
20355e3f7694SRobert Watson 	FILEDESC_XLOCK(fdp);
2036655291f2SDavid Xu 	if (fdp->fd_ofiles[fd] == fp)
2037655291f2SDavid Xu 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
20385e3f7694SRobert Watson 	FILEDESC_XUNLOCK(fdp);
2039655291f2SDavid Xu 	td->td_retval[0] = fd;
2040655291f2SDavid Xu 	fdrop(fp, td);
2041655291f2SDavid Xu 	return (0);
2042655291f2SDavid Xu }
2043655291f2SDavid Xu 
2044655291f2SDavid Xu /*
2045afde2b65SKonstantin Belousov  * Syscall to open a message queue.
2046afde2b65SKonstantin Belousov  */
2047afde2b65SKonstantin Belousov int
20488451d0ddSKip Macy sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
2049afde2b65SKonstantin Belousov {
2050afde2b65SKonstantin Belousov 	struct mq_attr attr;
2051afde2b65SKonstantin Belousov 	int flags, error;
2052afde2b65SKonstantin Belousov 
2053afde2b65SKonstantin Belousov 	if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2054afde2b65SKonstantin Belousov 		return (EINVAL);
2055afde2b65SKonstantin Belousov 	flags = FFLAGS(uap->flags);
2056afde2b65SKonstantin Belousov 	if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2057afde2b65SKonstantin Belousov 		error = copyin(uap->attr, &attr, sizeof(attr));
2058afde2b65SKonstantin Belousov 		if (error)
2059afde2b65SKonstantin Belousov 			return (error);
2060afde2b65SKonstantin Belousov 	}
2061afde2b65SKonstantin Belousov 	return (kern_kmq_open(td, uap->path, flags, uap->mode,
2062afde2b65SKonstantin Belousov 	    uap->attr != NULL ? &attr : NULL));
2063afde2b65SKonstantin Belousov }
2064afde2b65SKonstantin Belousov 
2065afde2b65SKonstantin Belousov /*
2066873fbcd7SRobert Watson  * Syscall to unlink a message queue.
2067655291f2SDavid Xu  */
2068655291f2SDavid Xu int
20698451d0ddSKip Macy sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2070655291f2SDavid Xu {
2071655291f2SDavid Xu 	char path[MQFS_NAMELEN+1];
2072655291f2SDavid Xu 	struct mqfs_node *pn;
2073655291f2SDavid Xu 	int error, len;
2074655291f2SDavid Xu 
2075655291f2SDavid Xu 	error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2076655291f2SDavid Xu         if (error)
2077655291f2SDavid Xu 		return (error);
2078655291f2SDavid Xu 
2079655291f2SDavid Xu 	len = strlen(path);
2080*dc15eac0SEd Schouten 	if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2081655291f2SDavid Xu 		return (EINVAL);
2082655291f2SDavid Xu 
2083655291f2SDavid Xu 	sx_xlock(&mqfs_data.mi_lock);
2084655291f2SDavid Xu 	pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2085655291f2SDavid Xu 	if (pn != NULL)
2086655291f2SDavid Xu 		error = do_unlink(pn, td->td_ucred);
2087655291f2SDavid Xu 	else
2088655291f2SDavid Xu 		error = ENOENT;
2089655291f2SDavid Xu 	sx_xunlock(&mqfs_data.mi_lock);
2090655291f2SDavid Xu 	return (error);
2091655291f2SDavid Xu }
2092655291f2SDavid Xu 
2093a9d2f8d8SRobert Watson typedef int (*_fgetf)(struct thread *, int, cap_rights_t, struct file **);
2094655291f2SDavid Xu 
2095655291f2SDavid Xu /*
2096655291f2SDavid Xu  * Get message queue by giving file slot
2097655291f2SDavid Xu  */
2098655291f2SDavid Xu static int
2099a9d2f8d8SRobert Watson _getmq(struct thread *td, int fd, cap_rights_t rights, _fgetf func,
2100655291f2SDavid Xu        struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2101655291f2SDavid Xu {
2102655291f2SDavid Xu 	struct mqfs_node *pn;
2103655291f2SDavid Xu 	int error;
2104655291f2SDavid Xu 
2105a9d2f8d8SRobert Watson 	error = func(td, fd, rights, fpp);
2106655291f2SDavid Xu 	if (error)
2107655291f2SDavid Xu 		return (error);
2108655291f2SDavid Xu 	if (&mqueueops != (*fpp)->f_ops) {
2109655291f2SDavid Xu 		fdrop(*fpp, td);
2110655291f2SDavid Xu 		return (EBADF);
2111655291f2SDavid Xu 	}
2112b2f92ef9SDavid Xu 	pn = (*fpp)->f_data;
2113655291f2SDavid Xu 	if (ppn)
2114655291f2SDavid Xu 		*ppn = pn;
2115655291f2SDavid Xu 	if (pmq)
2116655291f2SDavid Xu 		*pmq = pn->mn_data;
2117655291f2SDavid Xu 	return (0);
2118655291f2SDavid Xu }
2119655291f2SDavid Xu 
2120655291f2SDavid Xu static __inline int
2121655291f2SDavid Xu getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2122655291f2SDavid Xu 	struct mqueue **pmq)
2123655291f2SDavid Xu {
2124d1b6899eSJonathan Anderson 	return _getmq(td, fd, CAP_POLL_EVENT, fget, fpp, ppn, pmq);
2125655291f2SDavid Xu }
2126655291f2SDavid Xu 
2127655291f2SDavid Xu static __inline int
2128655291f2SDavid Xu getmq_read(struct thread *td, int fd, struct file **fpp,
2129655291f2SDavid Xu 	 struct mqfs_node **ppn, struct mqueue **pmq)
2130655291f2SDavid Xu {
2131a9d2f8d8SRobert Watson 	return _getmq(td, fd, CAP_READ, fget_read, fpp, ppn, pmq);
2132655291f2SDavid Xu }
2133655291f2SDavid Xu 
2134655291f2SDavid Xu static __inline int
2135655291f2SDavid Xu getmq_write(struct thread *td, int fd, struct file **fpp,
2136655291f2SDavid Xu 	struct mqfs_node **ppn, struct mqueue **pmq)
2137655291f2SDavid Xu {
2138a9d2f8d8SRobert Watson 	return _getmq(td, fd, CAP_WRITE, fget_write, fpp, ppn, pmq);
2139655291f2SDavid Xu }
2140655291f2SDavid Xu 
2141afde2b65SKonstantin Belousov static int
2142afde2b65SKonstantin Belousov kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2143afde2b65SKonstantin Belousov     struct mq_attr *oattr)
2144655291f2SDavid Xu {
2145655291f2SDavid Xu 	struct mqueue *mq;
2146655291f2SDavid Xu 	struct file *fp;
2147397c19d1SJeff Roberson 	u_int oflag, flag;
2148655291f2SDavid Xu 	int error;
2149655291f2SDavid Xu 
2150afde2b65SKonstantin Belousov 	if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2151655291f2SDavid Xu 		return (EINVAL);
2152afde2b65SKonstantin Belousov 	error = getmq(td, mqd, &fp, NULL, &mq);
2153655291f2SDavid Xu 	if (error)
2154655291f2SDavid Xu 		return (error);
2155afde2b65SKonstantin Belousov 	oattr->mq_maxmsg  = mq->mq_maxmsg;
2156afde2b65SKonstantin Belousov 	oattr->mq_msgsize = mq->mq_msgsize;
2157afde2b65SKonstantin Belousov 	oattr->mq_curmsgs = mq->mq_curmsgs;
2158afde2b65SKonstantin Belousov 	if (attr != NULL) {
2159397c19d1SJeff Roberson 		do {
2160397c19d1SJeff Roberson 			oflag = flag = fp->f_flag;
2161397c19d1SJeff Roberson 			flag &= ~O_NONBLOCK;
2162afde2b65SKonstantin Belousov 			flag |= (attr->mq_flags & O_NONBLOCK);
2163397c19d1SJeff Roberson 		} while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2164397c19d1SJeff Roberson 	} else
2165397c19d1SJeff Roberson 		oflag = fp->f_flag;
2166afde2b65SKonstantin Belousov 	oattr->mq_flags = (O_NONBLOCK & oflag);
2167655291f2SDavid Xu 	fdrop(fp, td);
2168afde2b65SKonstantin Belousov 	return (error);
2169afde2b65SKonstantin Belousov }
2170afde2b65SKonstantin Belousov 
2171afde2b65SKonstantin Belousov int
21728451d0ddSKip Macy sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2173afde2b65SKonstantin Belousov {
2174afde2b65SKonstantin Belousov 	struct mq_attr attr, oattr;
2175afde2b65SKonstantin Belousov 	int error;
2176afde2b65SKonstantin Belousov 
2177afde2b65SKonstantin Belousov 	if (uap->attr != NULL) {
2178afde2b65SKonstantin Belousov 		error = copyin(uap->attr, &attr, sizeof(attr));
2179afde2b65SKonstantin Belousov 		if (error != 0)
2180afde2b65SKonstantin Belousov 			return (error);
2181afde2b65SKonstantin Belousov 	}
2182afde2b65SKonstantin Belousov 	error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2183afde2b65SKonstantin Belousov 	    &oattr);
2184afde2b65SKonstantin Belousov 	if (error != 0)
2185afde2b65SKonstantin Belousov 		return (error);
2186afde2b65SKonstantin Belousov 	if (uap->oattr != NULL)
2187655291f2SDavid Xu 		error = copyout(&oattr, uap->oattr, sizeof(oattr));
2188655291f2SDavid Xu 	return (error);
2189655291f2SDavid Xu }
2190655291f2SDavid Xu 
2191655291f2SDavid Xu int
21928451d0ddSKip Macy sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2193655291f2SDavid Xu {
2194655291f2SDavid Xu 	struct mqueue *mq;
2195655291f2SDavid Xu 	struct file *fp;
2196afde2b65SKonstantin Belousov 	struct timespec *abs_timeout, ets;
2197655291f2SDavid Xu 	int error;
2198655291f2SDavid Xu 	int waitok;
2199655291f2SDavid Xu 
2200655291f2SDavid Xu 	error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2201655291f2SDavid Xu 	if (error)
2202655291f2SDavid Xu 		return (error);
2203afde2b65SKonstantin Belousov 	if (uap->abs_timeout != NULL) {
2204afde2b65SKonstantin Belousov 		error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2205afde2b65SKonstantin Belousov 		if (error != 0)
2206afde2b65SKonstantin Belousov 			return (error);
2207afde2b65SKonstantin Belousov 		abs_timeout = &ets;
2208afde2b65SKonstantin Belousov 	} else
2209afde2b65SKonstantin Belousov 		abs_timeout = NULL;
2210655291f2SDavid Xu 	waitok = !(fp->f_flag & O_NONBLOCK);
2211655291f2SDavid Xu 	error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2212afde2b65SKonstantin Belousov 		uap->msg_prio, waitok, abs_timeout);
2213655291f2SDavid Xu 	fdrop(fp, td);
2214655291f2SDavid Xu 	return (error);
2215655291f2SDavid Xu }
2216655291f2SDavid Xu 
2217655291f2SDavid Xu int
22188451d0ddSKip Macy sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2219655291f2SDavid Xu {
2220655291f2SDavid Xu 	struct mqueue *mq;
2221655291f2SDavid Xu 	struct file *fp;
2222afde2b65SKonstantin Belousov 	struct timespec *abs_timeout, ets;
2223655291f2SDavid Xu 	int error, waitok;
2224655291f2SDavid Xu 
2225655291f2SDavid Xu 	error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2226655291f2SDavid Xu 	if (error)
2227655291f2SDavid Xu 		return (error);
2228afde2b65SKonstantin Belousov 	if (uap->abs_timeout != NULL) {
2229afde2b65SKonstantin Belousov 		error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2230afde2b65SKonstantin Belousov 		if (error != 0)
2231afde2b65SKonstantin Belousov 			return (error);
2232afde2b65SKonstantin Belousov 		abs_timeout = &ets;
2233afde2b65SKonstantin Belousov 	} else
2234afde2b65SKonstantin Belousov 		abs_timeout = NULL;
2235655291f2SDavid Xu 	waitok = !(fp->f_flag & O_NONBLOCK);
2236655291f2SDavid Xu 	error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2237afde2b65SKonstantin Belousov 		uap->msg_prio, waitok, abs_timeout);
2238655291f2SDavid Xu 	fdrop(fp, td);
2239655291f2SDavid Xu 	return (error);
2240655291f2SDavid Xu }
2241655291f2SDavid Xu 
2242655291f2SDavid Xu int
22438451d0ddSKip Macy sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2244655291f2SDavid Xu {
2245655291f2SDavid Xu 	struct sigevent ev;
2246b2f92ef9SDavid Xu 	struct filedesc *fdp;
2247b2f92ef9SDavid Xu 	struct proc *p;
2248655291f2SDavid Xu 	struct mqueue *mq;
2249a9d2f8d8SRobert Watson 	struct file *fp, *fp2;
2250b2f92ef9SDavid Xu 	struct mqueue_notifier *nt, *newnt = NULL;
2251655291f2SDavid Xu 	int error;
2252655291f2SDavid Xu 
2253b2f92ef9SDavid Xu 	p = td->td_proc;
2254b2f92ef9SDavid Xu 	fdp = td->td_proc->p_fd;
2255655291f2SDavid Xu 	if (uap->sigev) {
2256655291f2SDavid Xu 		error = copyin(uap->sigev, &ev, sizeof(ev));
2257655291f2SDavid Xu 		if (error)
2258655291f2SDavid Xu 			return (error);
2259655291f2SDavid Xu 		if (ev.sigev_notify != SIGEV_SIGNAL &&
22609947b459SDavid Xu 		    ev.sigev_notify != SIGEV_THREAD_ID &&
22619947b459SDavid Xu 		    ev.sigev_notify != SIGEV_NONE)
2262655291f2SDavid Xu 			return (EINVAL);
2263655291f2SDavid Xu 		if ((ev.sigev_notify == SIGEV_SIGNAL ||
2264655291f2SDavid Xu 		     ev.sigev_notify == SIGEV_THREAD_ID) &&
2265655291f2SDavid Xu 			!_SIG_VALID(ev.sigev_signo))
2266655291f2SDavid Xu 			return (EINVAL);
2267655291f2SDavid Xu 	}
2268655291f2SDavid Xu 	error = getmq(td, uap->mqd, &fp, NULL, &mq);
2269655291f2SDavid Xu 	if (error)
2270655291f2SDavid Xu 		return (error);
2271b2f92ef9SDavid Xu again:
22725e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
2273a9d2f8d8SRobert Watson 	fp2 = fget_locked(fdp, uap->mqd);
2274a9d2f8d8SRobert Watson 	if (fp2 == NULL) {
2275a9d2f8d8SRobert Watson 		FILEDESC_SUNLOCK(fdp);
2276a9d2f8d8SRobert Watson 		error = EBADF;
2277a9d2f8d8SRobert Watson 		goto out;
2278a9d2f8d8SRobert Watson 	}
2279d1b6899eSJonathan Anderson 	error = cap_funwrap(fp2, CAP_POLL_EVENT, &fp2);
2280a9d2f8d8SRobert Watson 	if (error) {
2281a9d2f8d8SRobert Watson 		FILEDESC_SUNLOCK(fdp);
2282a9d2f8d8SRobert Watson 		goto out;
2283a9d2f8d8SRobert Watson 	}
2284a9d2f8d8SRobert Watson 	if (fp2 != fp) {
22855e3f7694SRobert Watson 		FILEDESC_SUNLOCK(fdp);
2286b2f92ef9SDavid Xu 		error = EBADF;
2287b2f92ef9SDavid Xu 		goto out;
2288b2f92ef9SDavid Xu 	}
2289655291f2SDavid Xu 	mtx_lock(&mq->mq_mutex);
22905e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
2291655291f2SDavid Xu 	if (uap->sigev != NULL) {
2292655291f2SDavid Xu 		if (mq->mq_notifier != NULL) {
2293655291f2SDavid Xu 			error = EBUSY;
2294052ea11cSDavid Xu 		} else {
2295b2f92ef9SDavid Xu 			PROC_LOCK(p);
2296b2f92ef9SDavid Xu 			nt = notifier_search(p, uap->mqd);
2297b2f92ef9SDavid Xu 			if (nt == NULL) {
2298b2f92ef9SDavid Xu 				if (newnt == NULL) {
2299b2f92ef9SDavid Xu 					PROC_UNLOCK(p);
2300b2f92ef9SDavid Xu 					mtx_unlock(&mq->mq_mutex);
2301b2f92ef9SDavid Xu 					newnt = notifier_alloc();
2302b2f92ef9SDavid Xu 					goto again;
2303b2f92ef9SDavid Xu 				}
2304b2f92ef9SDavid Xu 			}
2305b2f92ef9SDavid Xu 
2306b2f92ef9SDavid Xu 			if (nt != NULL) {
2307b2f92ef9SDavid Xu 				sigqueue_take(&nt->nt_ksi);
2308b2f92ef9SDavid Xu 				if (newnt != NULL) {
2309b2f92ef9SDavid Xu 					notifier_free(newnt);
2310b2f92ef9SDavid Xu 					newnt = NULL;
2311b2f92ef9SDavid Xu 				}
2312b2f92ef9SDavid Xu 			} else {
2313b2f92ef9SDavid Xu 				nt = newnt;
2314b2f92ef9SDavid Xu 				newnt = NULL;
2315b2f92ef9SDavid Xu 				ksiginfo_init(&nt->nt_ksi);
2316b2f92ef9SDavid Xu 				nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2317b2f92ef9SDavid Xu 				nt->nt_ksi.ksi_code = SI_MESGQ;
2318b2f92ef9SDavid Xu 				nt->nt_proc = p;
23199da8a32aSDavid Xu 				nt->nt_ksi.ksi_mqd = uap->mqd;
2320b2f92ef9SDavid Xu 				notifier_insert(p, nt);
2321b2f92ef9SDavid Xu 			}
2322b2f92ef9SDavid Xu 			nt->nt_sigev = ev;
2323b2f92ef9SDavid Xu 			mq->mq_notifier = nt;
2324b2f92ef9SDavid Xu 			PROC_UNLOCK(p);
2325655291f2SDavid Xu 			/*
2326b2f92ef9SDavid Xu 			 * if there is no receivers and message queue
2327b2f92ef9SDavid Xu 			 * is not empty, we should send notification
2328b2f92ef9SDavid Xu 			 * as soon as possible.
2329655291f2SDavid Xu 			 */
2330655291f2SDavid Xu 			if (mq->mq_receivers == 0 &&
2331655291f2SDavid Xu 			    !TAILQ_EMPTY(&mq->mq_msgq))
2332655291f2SDavid Xu 				mqueue_send_notification(mq);
2333655291f2SDavid Xu 		}
2334655291f2SDavid Xu 	} else {
2335b2f92ef9SDavid Xu 		notifier_remove(p, mq, uap->mqd);
2336655291f2SDavid Xu 	}
2337655291f2SDavid Xu 	mtx_unlock(&mq->mq_mutex);
2338b2f92ef9SDavid Xu 
2339b2f92ef9SDavid Xu out:
2340655291f2SDavid Xu 	fdrop(fp, td);
2341b2f92ef9SDavid Xu 	if (newnt != NULL)
2342b2f92ef9SDavid Xu 		notifier_free(newnt);
2343655291f2SDavid Xu 	return (error);
2344655291f2SDavid Xu }
2345655291f2SDavid Xu 
2346b2f92ef9SDavid Xu static void
2347b2f92ef9SDavid Xu mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2348b2f92ef9SDavid Xu {
2349b2f92ef9SDavid Xu 	struct filedesc *fdp;
2350b2f92ef9SDavid Xu 	struct mqueue *mq;
2351b2f92ef9SDavid Xu 
2352b2f92ef9SDavid Xu 	fdp = td->td_proc->p_fd;
23535e3f7694SRobert Watson 	FILEDESC_LOCK_ASSERT(fdp);
23545e3f7694SRobert Watson 
2355b2f92ef9SDavid Xu 	if (fp->f_ops == &mqueueops) {
2356b2f92ef9SDavid Xu 		mq = FPTOMQ(fp);
2357b2f92ef9SDavid Xu 		mtx_lock(&mq->mq_mutex);
2358b2f92ef9SDavid Xu 		notifier_remove(td->td_proc, mq, fd);
2359b2f92ef9SDavid Xu 
2360b2f92ef9SDavid Xu 		/* have to wakeup thread in same process */
2361b2f92ef9SDavid Xu 		if (mq->mq_flags & MQ_RSEL) {
2362b2f92ef9SDavid Xu 			mq->mq_flags &= ~MQ_RSEL;
236303f70aecSDavid Xu 			selwakeup(&mq->mq_rsel);
2364b2f92ef9SDavid Xu 		}
2365b2f92ef9SDavid Xu 		if (mq->mq_flags & MQ_WSEL) {
2366b2f92ef9SDavid Xu 			mq->mq_flags &= ~MQ_WSEL;
236703f70aecSDavid Xu 			selwakeup(&mq->mq_wsel);
2368b2f92ef9SDavid Xu 		}
2369b2f92ef9SDavid Xu 		mtx_unlock(&mq->mq_mutex);
2370b2f92ef9SDavid Xu 	}
2371b2f92ef9SDavid Xu }
2372b2f92ef9SDavid Xu 
2373b2f92ef9SDavid Xu static void
2374b2f92ef9SDavid Xu mq_proc_exit(void *arg __unused, struct proc *p)
2375b2f92ef9SDavid Xu {
2376b2f92ef9SDavid Xu 	struct filedesc *fdp;
2377b2f92ef9SDavid Xu 	struct file *fp;
2378b2f92ef9SDavid Xu 	struct mqueue *mq;
2379b2f92ef9SDavid Xu 	int i;
2380b2f92ef9SDavid Xu 
2381b2f92ef9SDavid Xu 	fdp = p->p_fd;
23825e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
2383b2f92ef9SDavid Xu 	for (i = 0; i < fdp->fd_nfiles; ++i) {
2384b2f92ef9SDavid Xu 		fp = fget_locked(fdp, i);
2385b2f92ef9SDavid Xu 		if (fp != NULL && fp->f_ops == &mqueueops) {
2386b2f92ef9SDavid Xu 			mq = FPTOMQ(fp);
2387b2f92ef9SDavid Xu 			mtx_lock(&mq->mq_mutex);
2388b2f92ef9SDavid Xu 			notifier_remove(p, FPTOMQ(fp), i);
2389b2f92ef9SDavid Xu 			mtx_unlock(&mq->mq_mutex);
2390b2f92ef9SDavid Xu 		}
2391b2f92ef9SDavid Xu 	}
23925e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
2393b2f92ef9SDavid Xu 	KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2394b2f92ef9SDavid Xu }
2395b2f92ef9SDavid Xu 
2396655291f2SDavid Xu static int
2397655291f2SDavid Xu mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2398655291f2SDavid Xu 	int flags, struct thread *td)
2399655291f2SDavid Xu {
2400655291f2SDavid Xu 	return (EOPNOTSUPP);
2401655291f2SDavid Xu }
2402655291f2SDavid Xu 
2403655291f2SDavid Xu static int
2404655291f2SDavid Xu mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
2405655291f2SDavid Xu 	int flags, struct thread *td)
2406655291f2SDavid Xu {
2407655291f2SDavid Xu 	return (EOPNOTSUPP);
2408655291f2SDavid Xu }
2409655291f2SDavid Xu 
2410655291f2SDavid Xu static int
2411e4650294SJohn Baldwin mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
2412e4650294SJohn Baldwin     struct thread *td)
2413e4650294SJohn Baldwin {
2414e4650294SJohn Baldwin 
2415e4650294SJohn Baldwin 	return (EINVAL);
2416e4650294SJohn Baldwin }
2417e4650294SJohn Baldwin 
2418e4650294SJohn Baldwin static int
2419655291f2SDavid Xu mqf_ioctl(struct file *fp, u_long cmd, void *data,
2420655291f2SDavid Xu 	struct ucred *active_cred, struct thread *td)
2421655291f2SDavid Xu {
2422655291f2SDavid Xu 	return (ENOTTY);
2423655291f2SDavid Xu }
2424655291f2SDavid Xu 
2425655291f2SDavid Xu static int
2426655291f2SDavid Xu mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2427655291f2SDavid Xu 	struct thread *td)
2428655291f2SDavid Xu {
2429655291f2SDavid Xu 	struct mqueue *mq = FPTOMQ(fp);
2430655291f2SDavid Xu 	int revents = 0;
2431655291f2SDavid Xu 
2432655291f2SDavid Xu 	mtx_lock(&mq->mq_mutex);
2433655291f2SDavid Xu 	if (events & (POLLIN | POLLRDNORM)) {
2434655291f2SDavid Xu 		if (mq->mq_curmsgs) {
2435655291f2SDavid Xu 			revents |= events & (POLLIN | POLLRDNORM);
2436655291f2SDavid Xu 		} else {
2437655291f2SDavid Xu 			mq->mq_flags |= MQ_RSEL;
2438655291f2SDavid Xu 			selrecord(td, &mq->mq_rsel);
2439655291f2SDavid Xu  		}
2440655291f2SDavid Xu 	}
2441655291f2SDavid Xu 	if (events & POLLOUT) {
2442655291f2SDavid Xu 		if (mq->mq_curmsgs < mq->mq_maxmsg)
2443655291f2SDavid Xu 			revents |= POLLOUT;
2444655291f2SDavid Xu 		else {
2445655291f2SDavid Xu 			mq->mq_flags |= MQ_WSEL;
2446655291f2SDavid Xu 			selrecord(td, &mq->mq_wsel);
2447655291f2SDavid Xu 		}
2448655291f2SDavid Xu 	}
2449655291f2SDavid Xu 	mtx_unlock(&mq->mq_mutex);
2450655291f2SDavid Xu 	return (revents);
2451655291f2SDavid Xu }
2452655291f2SDavid Xu 
2453655291f2SDavid Xu static int
2454655291f2SDavid Xu mqf_close(struct file *fp, struct thread *td)
2455655291f2SDavid Xu {
2456655291f2SDavid Xu 	struct mqfs_node *pn;
2457655291f2SDavid Xu 
2458655291f2SDavid Xu 	fp->f_ops = &badfileops;
2459b2f92ef9SDavid Xu 	pn = fp->f_data;
2460655291f2SDavid Xu 	fp->f_data = NULL;
2461655291f2SDavid Xu 	sx_xlock(&mqfs_data.mi_lock);
2462655291f2SDavid Xu 	mqnode_release(pn);
2463655291f2SDavid Xu 	sx_xunlock(&mqfs_data.mi_lock);
2464655291f2SDavid Xu 	return (0);
2465655291f2SDavid Xu }
2466655291f2SDavid Xu 
2467655291f2SDavid Xu static int
2468655291f2SDavid Xu mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2469655291f2SDavid Xu 	struct thread *td)
2470655291f2SDavid Xu {
2471b2f92ef9SDavid Xu 	struct mqfs_node *pn = fp->f_data;
2472655291f2SDavid Xu 
2473655291f2SDavid Xu 	bzero(st, sizeof *st);
24749c00bb91SKonstantin Belousov 	sx_xlock(&mqfs_data.mi_lock);
2475510ea843SEd Schouten 	st->st_atim = pn->mn_atime;
2476510ea843SEd Schouten 	st->st_mtim = pn->mn_mtime;
2477510ea843SEd Schouten 	st->st_ctim = pn->mn_ctime;
2478510ea843SEd Schouten 	st->st_birthtim = pn->mn_birth;
2479655291f2SDavid Xu 	st->st_uid = pn->mn_uid;
2480655291f2SDavid Xu 	st->st_gid = pn->mn_gid;
2481655291f2SDavid Xu 	st->st_mode = S_IFIFO | pn->mn_mode;
24829c00bb91SKonstantin Belousov 	sx_xunlock(&mqfs_data.mi_lock);
2483655291f2SDavid Xu 	return (0);
2484655291f2SDavid Xu }
2485655291f2SDavid Xu 
2486655291f2SDavid Xu static int
24879c00bb91SKonstantin Belousov mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
24889c00bb91SKonstantin Belousov     struct thread *td)
24899c00bb91SKonstantin Belousov {
24909c00bb91SKonstantin Belousov 	struct mqfs_node *pn;
24919c00bb91SKonstantin Belousov 	int error;
24929c00bb91SKonstantin Belousov 
24939c00bb91SKonstantin Belousov 	error = 0;
24949c00bb91SKonstantin Belousov 	pn = fp->f_data;
24959c00bb91SKonstantin Belousov 	sx_xlock(&mqfs_data.mi_lock);
24969c00bb91SKonstantin Belousov 	error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
24979c00bb91SKonstantin Belousov 	    active_cred, NULL);
24989c00bb91SKonstantin Belousov 	if (error != 0)
24999c00bb91SKonstantin Belousov 		goto out;
25009c00bb91SKonstantin Belousov 	pn->mn_mode = mode & ACCESSPERMS;
25019c00bb91SKonstantin Belousov out:
25029c00bb91SKonstantin Belousov 	sx_xunlock(&mqfs_data.mi_lock);
25039c00bb91SKonstantin Belousov 	return (error);
25049c00bb91SKonstantin Belousov }
25059c00bb91SKonstantin Belousov 
25069c00bb91SKonstantin Belousov static int
25079c00bb91SKonstantin Belousov mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
25089c00bb91SKonstantin Belousov     struct thread *td)
25099c00bb91SKonstantin Belousov {
25109c00bb91SKonstantin Belousov 	struct mqfs_node *pn;
25119c00bb91SKonstantin Belousov 	int error;
25129c00bb91SKonstantin Belousov 
25139c00bb91SKonstantin Belousov 	error = 0;
25149c00bb91SKonstantin Belousov 	pn = fp->f_data;
25159c00bb91SKonstantin Belousov 	sx_xlock(&mqfs_data.mi_lock);
25169c00bb91SKonstantin Belousov 	if (uid == (uid_t)-1)
25179c00bb91SKonstantin Belousov 		uid = pn->mn_uid;
25189c00bb91SKonstantin Belousov 	if (gid == (gid_t)-1)
25199c00bb91SKonstantin Belousov 		gid = pn->mn_gid;
25209c00bb91SKonstantin Belousov 	if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
25219c00bb91SKonstantin Belousov 	    (gid != pn->mn_gid && !groupmember(gid, active_cred))) &&
25229c00bb91SKonstantin Belousov 	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
25239c00bb91SKonstantin Belousov 		goto out;
25249c00bb91SKonstantin Belousov 	pn->mn_uid = uid;
25259c00bb91SKonstantin Belousov 	pn->mn_gid = gid;
25269c00bb91SKonstantin Belousov out:
25279c00bb91SKonstantin Belousov 	sx_xunlock(&mqfs_data.mi_lock);
25289c00bb91SKonstantin Belousov 	return (error);
25299c00bb91SKonstantin Belousov }
25309c00bb91SKonstantin Belousov 
25319c00bb91SKonstantin Belousov static int
2532655291f2SDavid Xu mqf_kqfilter(struct file *fp, struct knote *kn)
2533655291f2SDavid Xu {
2534655291f2SDavid Xu 	struct mqueue *mq = FPTOMQ(fp);
2535655291f2SDavid Xu 	int error = 0;
2536655291f2SDavid Xu 
2537655291f2SDavid Xu 	if (kn->kn_filter == EVFILT_READ) {
2538655291f2SDavid Xu 		kn->kn_fop = &mq_rfiltops;
2539655291f2SDavid Xu 		knlist_add(&mq->mq_rsel.si_note, kn, 0);
2540655291f2SDavid Xu 	} else if (kn->kn_filter == EVFILT_WRITE) {
2541655291f2SDavid Xu 		kn->kn_fop = &mq_wfiltops;
2542655291f2SDavid Xu 		knlist_add(&mq->mq_wsel.si_note, kn, 0);
2543655291f2SDavid Xu 	} else
2544655291f2SDavid Xu 		error = EINVAL;
2545655291f2SDavid Xu 	return (error);
2546655291f2SDavid Xu }
2547655291f2SDavid Xu 
2548655291f2SDavid Xu static void
2549655291f2SDavid Xu filt_mqdetach(struct knote *kn)
2550655291f2SDavid Xu {
2551655291f2SDavid Xu 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2552655291f2SDavid Xu 
2553655291f2SDavid Xu 	if (kn->kn_filter == EVFILT_READ)
2554655291f2SDavid Xu 		knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2555655291f2SDavid Xu 	else if (kn->kn_filter == EVFILT_WRITE)
2556655291f2SDavid Xu 		knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2557655291f2SDavid Xu 	else
2558655291f2SDavid Xu 		panic("filt_mqdetach");
2559655291f2SDavid Xu }
2560655291f2SDavid Xu 
2561655291f2SDavid Xu static int
2562655291f2SDavid Xu filt_mqread(struct knote *kn, long hint)
2563655291f2SDavid Xu {
2564655291f2SDavid Xu 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2565655291f2SDavid Xu 
2566655291f2SDavid Xu 	mtx_assert(&mq->mq_mutex, MA_OWNED);
2567655291f2SDavid Xu 	return (mq->mq_curmsgs != 0);
2568655291f2SDavid Xu }
2569655291f2SDavid Xu 
2570655291f2SDavid Xu static int
2571655291f2SDavid Xu filt_mqwrite(struct knote *kn, long hint)
2572655291f2SDavid Xu {
2573655291f2SDavid Xu 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2574655291f2SDavid Xu 
2575655291f2SDavid Xu 	mtx_assert(&mq->mq_mutex, MA_OWNED);
2576655291f2SDavid Xu 	return (mq->mq_curmsgs < mq->mq_maxmsg);
2577655291f2SDavid Xu }
2578655291f2SDavid Xu 
2579655291f2SDavid Xu static struct fileops mqueueops = {
2580655291f2SDavid Xu 	.fo_read		= mqf_read,
2581655291f2SDavid Xu 	.fo_write		= mqf_write,
2582e4650294SJohn Baldwin 	.fo_truncate		= mqf_truncate,
2583655291f2SDavid Xu 	.fo_ioctl		= mqf_ioctl,
2584655291f2SDavid Xu 	.fo_poll		= mqf_poll,
2585655291f2SDavid Xu 	.fo_kqfilter		= mqf_kqfilter,
2586655291f2SDavid Xu 	.fo_stat		= mqf_stat,
25879c00bb91SKonstantin Belousov 	.fo_chmod		= mqf_chmod,
25889c00bb91SKonstantin Belousov 	.fo_chown		= mqf_chown,
2589655291f2SDavid Xu 	.fo_close		= mqf_close
2590655291f2SDavid Xu };
2591655291f2SDavid Xu 
2592655291f2SDavid Xu static struct vop_vector mqfs_vnodeops = {
2593655291f2SDavid Xu 	.vop_default 		= &default_vnodeops,
2594655291f2SDavid Xu 	.vop_access		= mqfs_access,
2595655291f2SDavid Xu 	.vop_cachedlookup	= mqfs_lookup,
2596655291f2SDavid Xu 	.vop_lookup		= vfs_cache_lookup,
2597655291f2SDavid Xu 	.vop_reclaim		= mqfs_reclaim,
2598655291f2SDavid Xu 	.vop_create		= mqfs_create,
2599655291f2SDavid Xu 	.vop_remove		= mqfs_remove,
2600655291f2SDavid Xu 	.vop_inactive		= mqfs_inactive,
2601655291f2SDavid Xu 	.vop_open		= mqfs_open,
2602655291f2SDavid Xu 	.vop_close		= mqfs_close,
2603655291f2SDavid Xu 	.vop_getattr		= mqfs_getattr,
2604655291f2SDavid Xu 	.vop_setattr		= mqfs_setattr,
2605655291f2SDavid Xu 	.vop_read		= mqfs_read,
2606655291f2SDavid Xu 	.vop_write		= VOP_EOPNOTSUPP,
2607655291f2SDavid Xu 	.vop_readdir		= mqfs_readdir,
2608655291f2SDavid Xu 	.vop_mkdir		= VOP_EOPNOTSUPP,
2609655291f2SDavid Xu 	.vop_rmdir		= VOP_EOPNOTSUPP
2610655291f2SDavid Xu };
2611655291f2SDavid Xu 
2612655291f2SDavid Xu static struct vfsops mqfs_vfsops = {
2613655291f2SDavid Xu 	.vfs_init 		= mqfs_init,
2614655291f2SDavid Xu 	.vfs_uninit		= mqfs_uninit,
2615655291f2SDavid Xu 	.vfs_mount		= mqfs_mount,
2616655291f2SDavid Xu 	.vfs_unmount		= mqfs_unmount,
2617655291f2SDavid Xu 	.vfs_root		= mqfs_root,
2618655291f2SDavid Xu 	.vfs_statfs		= mqfs_statfs,
2619655291f2SDavid Xu };
2620655291f2SDavid Xu 
2621afde2b65SKonstantin Belousov static struct vfsconf mqueuefs_vfsconf = {
2622afde2b65SKonstantin Belousov 	.vfc_version = VFS_VERSION,
2623afde2b65SKonstantin Belousov 	.vfc_name = "mqueuefs",
2624afde2b65SKonstantin Belousov 	.vfc_vfsops = &mqfs_vfsops,
2625afde2b65SKonstantin Belousov 	.vfc_typenum = -1,
2626afde2b65SKonstantin Belousov 	.vfc_flags = VFCF_SYNTHETIC
2627afde2b65SKonstantin Belousov };
2628655291f2SDavid Xu 
2629afde2b65SKonstantin Belousov static struct syscall_helper_data mq_syscalls[] = {
2630afde2b65SKonstantin Belousov 	SYSCALL_INIT_HELPER(kmq_open),
2631afde2b65SKonstantin Belousov 	SYSCALL_INIT_HELPER(kmq_setattr),
2632afde2b65SKonstantin Belousov 	SYSCALL_INIT_HELPER(kmq_timedsend),
2633afde2b65SKonstantin Belousov 	SYSCALL_INIT_HELPER(kmq_timedreceive),
2634afde2b65SKonstantin Belousov 	SYSCALL_INIT_HELPER(kmq_notify),
2635afde2b65SKonstantin Belousov 	SYSCALL_INIT_HELPER(kmq_unlink),
2636afde2b65SKonstantin Belousov 	SYSCALL_INIT_LAST
2637afde2b65SKonstantin Belousov };
2638afde2b65SKonstantin Belousov 
2639afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2640afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32.h>
2641afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h>
2642afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h>
2643afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
2644afde2b65SKonstantin Belousov 
2645afde2b65SKonstantin Belousov static void
2646afde2b65SKonstantin Belousov mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2647afde2b65SKonstantin Belousov {
2648afde2b65SKonstantin Belousov 
2649afde2b65SKonstantin Belousov 	to->mq_flags = from->mq_flags;
2650afde2b65SKonstantin Belousov 	to->mq_maxmsg = from->mq_maxmsg;
2651afde2b65SKonstantin Belousov 	to->mq_msgsize = from->mq_msgsize;
2652afde2b65SKonstantin Belousov 	to->mq_curmsgs = from->mq_curmsgs;
2653afde2b65SKonstantin Belousov }
2654afde2b65SKonstantin Belousov 
2655afde2b65SKonstantin Belousov static void
2656afde2b65SKonstantin Belousov mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2657afde2b65SKonstantin Belousov {
2658afde2b65SKonstantin Belousov 
2659afde2b65SKonstantin Belousov 	to->mq_flags = from->mq_flags;
2660afde2b65SKonstantin Belousov 	to->mq_maxmsg = from->mq_maxmsg;
2661afde2b65SKonstantin Belousov 	to->mq_msgsize = from->mq_msgsize;
2662afde2b65SKonstantin Belousov 	to->mq_curmsgs = from->mq_curmsgs;
2663afde2b65SKonstantin Belousov }
2664afde2b65SKonstantin Belousov 
2665afde2b65SKonstantin Belousov int
2666afde2b65SKonstantin Belousov freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2667afde2b65SKonstantin Belousov {
2668afde2b65SKonstantin Belousov 	struct mq_attr attr;
2669afde2b65SKonstantin Belousov 	struct mq_attr32 attr32;
2670afde2b65SKonstantin Belousov 	int flags, error;
2671afde2b65SKonstantin Belousov 
2672afde2b65SKonstantin Belousov 	if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2673afde2b65SKonstantin Belousov 		return (EINVAL);
2674afde2b65SKonstantin Belousov 	flags = FFLAGS(uap->flags);
2675afde2b65SKonstantin Belousov 	if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2676afde2b65SKonstantin Belousov 		error = copyin(uap->attr, &attr32, sizeof(attr32));
2677afde2b65SKonstantin Belousov 		if (error)
2678afde2b65SKonstantin Belousov 			return (error);
2679afde2b65SKonstantin Belousov 		mq_attr_from32(&attr32, &attr);
2680afde2b65SKonstantin Belousov 	}
2681afde2b65SKonstantin Belousov 	return (kern_kmq_open(td, uap->path, flags, uap->mode,
2682afde2b65SKonstantin Belousov 	    uap->attr != NULL ? &attr : NULL));
2683afde2b65SKonstantin Belousov }
2684afde2b65SKonstantin Belousov 
2685afde2b65SKonstantin Belousov int
2686afde2b65SKonstantin Belousov freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2687afde2b65SKonstantin Belousov {
2688afde2b65SKonstantin Belousov 	struct mq_attr attr, oattr;
2689afde2b65SKonstantin Belousov 	struct mq_attr32 attr32, oattr32;
2690afde2b65SKonstantin Belousov 	int error;
2691afde2b65SKonstantin Belousov 
2692afde2b65SKonstantin Belousov 	if (uap->attr != NULL) {
2693afde2b65SKonstantin Belousov 		error = copyin(uap->attr, &attr32, sizeof(attr32));
2694afde2b65SKonstantin Belousov 		if (error != 0)
2695afde2b65SKonstantin Belousov 			return (error);
2696afde2b65SKonstantin Belousov 		mq_attr_from32(&attr32, &attr);
2697afde2b65SKonstantin Belousov 	}
2698afde2b65SKonstantin Belousov 	error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2699afde2b65SKonstantin Belousov 	    &oattr);
2700afde2b65SKonstantin Belousov 	if (error != 0)
2701afde2b65SKonstantin Belousov 		return (error);
2702afde2b65SKonstantin Belousov 	if (uap->oattr != NULL) {
2703afde2b65SKonstantin Belousov 		mq_attr_to32(&oattr, &oattr32);
2704afde2b65SKonstantin Belousov 		error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2705afde2b65SKonstantin Belousov 	}
2706afde2b65SKonstantin Belousov 	return (error);
2707afde2b65SKonstantin Belousov }
2708afde2b65SKonstantin Belousov 
2709afde2b65SKonstantin Belousov int
2710afde2b65SKonstantin Belousov freebsd32_kmq_timedsend(struct thread *td,
2711afde2b65SKonstantin Belousov     struct freebsd32_kmq_timedsend_args *uap)
2712afde2b65SKonstantin Belousov {
2713afde2b65SKonstantin Belousov 	struct mqueue *mq;
2714afde2b65SKonstantin Belousov 	struct file *fp;
2715afde2b65SKonstantin Belousov 	struct timespec32 ets32;
2716afde2b65SKonstantin Belousov 	struct timespec *abs_timeout, ets;
2717afde2b65SKonstantin Belousov 	int error;
2718afde2b65SKonstantin Belousov 	int waitok;
2719afde2b65SKonstantin Belousov 
2720afde2b65SKonstantin Belousov 	error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2721afde2b65SKonstantin Belousov 	if (error)
2722afde2b65SKonstantin Belousov 		return (error);
2723afde2b65SKonstantin Belousov 	if (uap->abs_timeout != NULL) {
2724afde2b65SKonstantin Belousov 		error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2725afde2b65SKonstantin Belousov 		if (error != 0)
2726afde2b65SKonstantin Belousov 			return (error);
2727afde2b65SKonstantin Belousov 		CP(ets32, ets, tv_sec);
2728afde2b65SKonstantin Belousov 		CP(ets32, ets, tv_nsec);
2729afde2b65SKonstantin Belousov 		abs_timeout = &ets;
2730afde2b65SKonstantin Belousov 	} else
2731afde2b65SKonstantin Belousov 		abs_timeout = NULL;
2732afde2b65SKonstantin Belousov 	waitok = !(fp->f_flag & O_NONBLOCK);
2733afde2b65SKonstantin Belousov 	error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2734afde2b65SKonstantin Belousov 		uap->msg_prio, waitok, abs_timeout);
2735afde2b65SKonstantin Belousov 	fdrop(fp, td);
2736afde2b65SKonstantin Belousov 	return (error);
2737afde2b65SKonstantin Belousov }
2738afde2b65SKonstantin Belousov 
2739afde2b65SKonstantin Belousov int
2740afde2b65SKonstantin Belousov freebsd32_kmq_timedreceive(struct thread *td,
2741afde2b65SKonstantin Belousov     struct freebsd32_kmq_timedreceive_args *uap)
2742afde2b65SKonstantin Belousov {
2743afde2b65SKonstantin Belousov 	struct mqueue *mq;
2744afde2b65SKonstantin Belousov 	struct file *fp;
2745afde2b65SKonstantin Belousov 	struct timespec32 ets32;
2746afde2b65SKonstantin Belousov 	struct timespec *abs_timeout, ets;
2747afde2b65SKonstantin Belousov 	int error, waitok;
2748afde2b65SKonstantin Belousov 
2749afde2b65SKonstantin Belousov 	error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2750afde2b65SKonstantin Belousov 	if (error)
2751afde2b65SKonstantin Belousov 		return (error);
2752afde2b65SKonstantin Belousov 	if (uap->abs_timeout != NULL) {
2753afde2b65SKonstantin Belousov 		error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2754afde2b65SKonstantin Belousov 		if (error != 0)
2755afde2b65SKonstantin Belousov 			return (error);
2756afde2b65SKonstantin Belousov 		CP(ets32, ets, tv_sec);
2757afde2b65SKonstantin Belousov 		CP(ets32, ets, tv_nsec);
2758afde2b65SKonstantin Belousov 		abs_timeout = &ets;
2759afde2b65SKonstantin Belousov 	} else
2760afde2b65SKonstantin Belousov 		abs_timeout = NULL;
2761afde2b65SKonstantin Belousov 	waitok = !(fp->f_flag & O_NONBLOCK);
2762afde2b65SKonstantin Belousov 	error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2763afde2b65SKonstantin Belousov 		uap->msg_prio, waitok, abs_timeout);
2764afde2b65SKonstantin Belousov 	fdrop(fp, td);
2765afde2b65SKonstantin Belousov 	return (error);
2766afde2b65SKonstantin Belousov }
2767afde2b65SKonstantin Belousov 
2768afde2b65SKonstantin Belousov static struct syscall_helper_data mq32_syscalls[] = {
2769afde2b65SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2770afde2b65SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
2771afde2b65SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
2772afde2b65SKonstantin Belousov 	SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
27738451d0ddSKip Macy 	SYSCALL32_INIT_HELPER_COMPAT(kmq_notify),
27748451d0ddSKip Macy 	SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2775afde2b65SKonstantin Belousov 	SYSCALL_INIT_LAST
2776afde2b65SKonstantin Belousov };
2777afde2b65SKonstantin Belousov #endif
2778afde2b65SKonstantin Belousov 
2779afde2b65SKonstantin Belousov static int
2780afde2b65SKonstantin Belousov mqinit(void)
2781afde2b65SKonstantin Belousov {
2782afde2b65SKonstantin Belousov 	int error;
2783afde2b65SKonstantin Belousov 
2784afde2b65SKonstantin Belousov 	error = syscall_helper_register(mq_syscalls);
2785afde2b65SKonstantin Belousov 	if (error != 0)
2786afde2b65SKonstantin Belousov 		return (error);
2787afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2788afde2b65SKonstantin Belousov 	error = syscall32_helper_register(mq32_syscalls);
2789afde2b65SKonstantin Belousov 	if (error != 0)
2790afde2b65SKonstantin Belousov 		return (error);
2791afde2b65SKonstantin Belousov #endif
2792afde2b65SKonstantin Belousov 	return (0);
2793afde2b65SKonstantin Belousov }
2794afde2b65SKonstantin Belousov 
2795afde2b65SKonstantin Belousov static int
2796afde2b65SKonstantin Belousov mqunload(void)
2797afde2b65SKonstantin Belousov {
2798afde2b65SKonstantin Belousov 
2799afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2800afde2b65SKonstantin Belousov 	syscall32_helper_unregister(mq32_syscalls);
2801afde2b65SKonstantin Belousov #endif
2802afde2b65SKonstantin Belousov 	syscall_helper_unregister(mq_syscalls);
2803afde2b65SKonstantin Belousov 	return (0);
2804afde2b65SKonstantin Belousov }
2805afde2b65SKonstantin Belousov 
2806afde2b65SKonstantin Belousov static int
2807afde2b65SKonstantin Belousov mq_modload(struct module *module, int cmd, void *arg)
2808afde2b65SKonstantin Belousov {
2809afde2b65SKonstantin Belousov 	int error = 0;
2810afde2b65SKonstantin Belousov 
2811afde2b65SKonstantin Belousov 	error = vfs_modevent(module, cmd, arg);
2812afde2b65SKonstantin Belousov 	if (error != 0)
2813afde2b65SKonstantin Belousov 		return (error);
2814afde2b65SKonstantin Belousov 
2815afde2b65SKonstantin Belousov 	switch (cmd) {
2816afde2b65SKonstantin Belousov 	case MOD_LOAD:
2817afde2b65SKonstantin Belousov 		error = mqinit();
2818afde2b65SKonstantin Belousov 		if (error != 0)
2819afde2b65SKonstantin Belousov 			mqunload();
2820afde2b65SKonstantin Belousov 		break;
2821afde2b65SKonstantin Belousov 	case MOD_UNLOAD:
2822afde2b65SKonstantin Belousov 		error = mqunload();
2823afde2b65SKonstantin Belousov 		break;
2824afde2b65SKonstantin Belousov 	default:
2825afde2b65SKonstantin Belousov 		break;
2826afde2b65SKonstantin Belousov 	}
2827afde2b65SKonstantin Belousov 	return (error);
2828afde2b65SKonstantin Belousov }
2829afde2b65SKonstantin Belousov 
2830afde2b65SKonstantin Belousov static moduledata_t mqueuefs_mod = {
2831afde2b65SKonstantin Belousov 	"mqueuefs",
2832afde2b65SKonstantin Belousov 	mq_modload,
2833afde2b65SKonstantin Belousov 	&mqueuefs_vfsconf
2834afde2b65SKonstantin Belousov };
2835afde2b65SKonstantin Belousov DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
283647bf2cf9SDavid Xu MODULE_VERSION(mqueuefs, 1);
2837