xref: /freebsd/sys/kern/uipc_mqueue.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
1 /*-
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 /*
29  * POSIX message queue implementation.
30  *
31  * 1) A mqueue filesystem can be mounted, each message queue appears
32  *    in mounted directory, user can change queue's permission and
33  *    ownership, or remove a queue. Manually creating a file in the
34  *    directory causes a message queue to be created in the kernel with
35  *    default message queue attributes applied and same name used, this
36  *    method is not advocated since mq_open syscall allows user to specify
37  *    different attributes. Also the file system can be mounted multiple
38  *    times at different mount points but shows same contents.
39  *
40  * 2) Standard POSIX message queue API. The syscalls do not use vfs layer,
41  *    but directly operate on internal data structure, this allows user to
42  *    use the IPC facility without having to mount mqueue file system.
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include "opt_compat.h"
49 
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/limits.h>
54 #include <sys/buf.h>
55 #include <sys/dirent.h>
56 #include <sys/event.h>
57 #include <sys/eventhandler.h>
58 #include <sys/fcntl.h>
59 #include <sys/file.h>
60 #include <sys/filedesc.h>
61 #include <sys/lock.h>
62 #include <sys/malloc.h>
63 #include <sys/module.h>
64 #include <sys/mount.h>
65 #include <sys/mqueue.h>
66 #include <sys/mutex.h>
67 #include <sys/namei.h>
68 #include <sys/posix4.h>
69 #include <sys/poll.h>
70 #include <sys/priv.h>
71 #include <sys/proc.h>
72 #include <sys/queue.h>
73 #include <sys/sysproto.h>
74 #include <sys/stat.h>
75 #include <sys/syscall.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysent.h>
78 #include <sys/sx.h>
79 #include <sys/sysctl.h>
80 #include <sys/taskqueue.h>
81 #include <sys/unistd.h>
82 #include <sys/vnode.h>
83 #include <machine/atomic.h>
84 
85 /*
86  * Limits and constants
87  */
88 #define	MQFS_NAMELEN		NAME_MAX
89 #define MQFS_DELEN		(8 + MQFS_NAMELEN)
90 
91 /* node types */
92 typedef enum {
93 	mqfstype_none = 0,
94 	mqfstype_root,
95 	mqfstype_dir,
96 	mqfstype_this,
97 	mqfstype_parent,
98 	mqfstype_file,
99 	mqfstype_symlink,
100 } mqfs_type_t;
101 
102 struct mqfs_node;
103 
104 /*
105  * mqfs_info: describes a mqfs instance
106  */
107 struct mqfs_info {
108 	struct sx		mi_lock;
109 	struct mqfs_node	*mi_root;
110 	struct unrhdr		*mi_unrhdr;
111 };
112 
113 struct mqfs_vdata {
114 	LIST_ENTRY(mqfs_vdata)	mv_link;
115 	struct mqfs_node	*mv_node;
116 	struct vnode		*mv_vnode;
117 	struct task		mv_task;
118 };
119 
120 /*
121  * mqfs_node: describes a node (file or directory) within a mqfs
122  */
123 struct mqfs_node {
124 	char			mn_name[MQFS_NAMELEN+1];
125 	struct mqfs_info	*mn_info;
126 	struct mqfs_node	*mn_parent;
127 	LIST_HEAD(,mqfs_node)	mn_children;
128 	LIST_ENTRY(mqfs_node)	mn_sibling;
129 	LIST_HEAD(,mqfs_vdata)	mn_vnodes;
130 	int			mn_refcount;
131 	mqfs_type_t		mn_type;
132 	int			mn_deleted;
133 	u_int32_t		mn_fileno;
134 	void			*mn_data;
135 	struct timespec		mn_birth;
136 	struct timespec		mn_ctime;
137 	struct timespec		mn_atime;
138 	struct timespec		mn_mtime;
139 	uid_t			mn_uid;
140 	gid_t			mn_gid;
141 	int			mn_mode;
142 };
143 
144 #define	VTON(vp)	(((struct mqfs_vdata *)((vp)->v_data))->mv_node)
145 #define VTOMQ(vp) 	((struct mqueue *)(VTON(vp)->mn_data))
146 #define	VFSTOMQFS(m)	((struct mqfs_info *)((m)->mnt_data))
147 #define	FPTOMQ(fp)	((struct mqueue *)(((struct mqfs_node *) \
148 				(fp)->f_data)->mn_data))
149 
150 TAILQ_HEAD(msgq, mqueue_msg);
151 
152 struct mqueue;
153 
154 struct mqueue_notifier {
155 	LIST_ENTRY(mqueue_notifier)	nt_link;
156 	struct sigevent			nt_sigev;
157 	ksiginfo_t			nt_ksi;
158 	struct proc			*nt_proc;
159 };
160 
161 struct mqueue {
162 	struct mtx	mq_mutex;
163 	int		mq_flags;
164 	long		mq_maxmsg;
165 	long		mq_msgsize;
166 	long		mq_curmsgs;
167 	long		mq_totalbytes;
168 	struct msgq	mq_msgq;
169 	int		mq_receivers;
170 	int		mq_senders;
171 	struct selinfo	mq_rsel;
172 	struct selinfo	mq_wsel;
173 	struct mqueue_notifier	*mq_notifier;
174 };
175 
176 #define	MQ_RSEL		0x01
177 #define	MQ_WSEL		0x02
178 
179 struct mqueue_msg {
180 	TAILQ_ENTRY(mqueue_msg)	msg_link;
181 	unsigned int	msg_prio;
182 	unsigned int	msg_size;
183 	/* following real data... */
184 };
185 
186 SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
187 	"POSIX real time message queue");
188 
189 static int	default_maxmsg  = 10;
190 static int	default_msgsize = 1024;
191 
192 static int	maxmsg = 100;
193 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
194     &maxmsg, 0, "Default maximum messages in queue");
195 static int	maxmsgsize = 16384;
196 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
197     &maxmsgsize, 0, "Default maximum message size");
198 static int	maxmq = 100;
199 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
200     &maxmq, 0, "maximum message queues");
201 static int	curmq = 0;
202 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
203     &curmq, 0, "current message queue number");
204 static int	unloadable = 0;
205 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
206 
207 static eventhandler_tag exit_tag;
208 
209 /* Only one instance per-system */
210 static struct mqfs_info		mqfs_data;
211 static uma_zone_t		mqnode_zone;
212 static uma_zone_t		mqueue_zone;
213 static uma_zone_t		mvdata_zone;
214 static uma_zone_t		mqnoti_zone;
215 static struct vop_vector	mqfs_vnodeops;
216 static struct fileops		mqueueops;
217 
218 /*
219  * Directory structure construction and manipulation
220  */
221 #ifdef notyet
222 static struct mqfs_node	*mqfs_create_dir(struct mqfs_node *parent,
223 	const char *name, int namelen, struct ucred *cred, int mode);
224 static struct mqfs_node	*mqfs_create_link(struct mqfs_node *parent,
225 	const char *name, int namelen, struct ucred *cred, int mode);
226 #endif
227 
228 static struct mqfs_node	*mqfs_create_file(struct mqfs_node *parent,
229 	const char *name, int namelen, struct ucred *cred, int mode);
230 static int	mqfs_destroy(struct mqfs_node *mn);
231 static void	mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
232 static void	mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
233 static int	mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
234 
235 /*
236  * Message queue construction and maniplation
237  */
238 static struct mqueue	*mqueue_alloc(const struct mq_attr *attr);
239 static void	mqueue_free(struct mqueue *mq);
240 static int	mqueue_send(struct mqueue *mq, const char *msg_ptr,
241 			size_t msg_len, unsigned msg_prio, int waitok,
242 			const struct timespec *abs_timeout);
243 static int	mqueue_receive(struct mqueue *mq, char *msg_ptr,
244 			size_t msg_len, unsigned *msg_prio, int waitok,
245 			const struct timespec *abs_timeout);
246 static int	_mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
247 			int timo);
248 static int	_mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
249 			int timo);
250 static void	mqueue_send_notification(struct mqueue *mq);
251 static void	mqueue_fdclose(struct thread *td, int fd, struct file *fp);
252 static void	mq_proc_exit(void *arg, struct proc *p);
253 
254 /*
255  * kqueue filters
256  */
257 static void	filt_mqdetach(struct knote *kn);
258 static int	filt_mqread(struct knote *kn, long hint);
259 static int	filt_mqwrite(struct knote *kn, long hint);
260 
261 struct filterops mq_rfiltops = {
262 	.f_isfd = 1,
263 	.f_detach = filt_mqdetach,
264 	.f_event = filt_mqread,
265 };
266 struct filterops mq_wfiltops = {
267 	.f_isfd = 1,
268 	.f_detach = filt_mqdetach,
269 	.f_event = filt_mqwrite,
270 };
271 
272 /*
273  * Initialize fileno bitmap
274  */
275 static void
276 mqfs_fileno_init(struct mqfs_info *mi)
277 {
278 	struct unrhdr *up;
279 
280 	up = new_unrhdr(1, INT_MAX, NULL);
281 	mi->mi_unrhdr = up;
282 }
283 
284 /*
285  * Tear down fileno bitmap
286  */
287 static void
288 mqfs_fileno_uninit(struct mqfs_info *mi)
289 {
290 	struct unrhdr *up;
291 
292 	up = mi->mi_unrhdr;
293 	mi->mi_unrhdr = NULL;
294 	delete_unrhdr(up);
295 }
296 
297 /*
298  * Allocate a file number
299  */
300 static void
301 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
302 {
303 	/* make sure our parent has a file number */
304 	if (mn->mn_parent && !mn->mn_parent->mn_fileno)
305 		mqfs_fileno_alloc(mi, mn->mn_parent);
306 
307 	switch (mn->mn_type) {
308 	case mqfstype_root:
309 	case mqfstype_dir:
310 	case mqfstype_file:
311 	case mqfstype_symlink:
312 		mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
313 		break;
314 	case mqfstype_this:
315 		KASSERT(mn->mn_parent != NULL,
316 		    ("mqfstype_this node has no parent"));
317 		mn->mn_fileno = mn->mn_parent->mn_fileno;
318 		break;
319 	case mqfstype_parent:
320 		KASSERT(mn->mn_parent != NULL,
321 		    ("mqfstype_parent node has no parent"));
322 		if (mn->mn_parent == mi->mi_root) {
323 			mn->mn_fileno = mn->mn_parent->mn_fileno;
324 			break;
325 		}
326 		KASSERT(mn->mn_parent->mn_parent != NULL,
327 		    ("mqfstype_parent node has no grandparent"));
328 		mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
329 		break;
330 	default:
331 		KASSERT(0,
332 		    ("mqfs_fileno_alloc() called for unknown type node: %d",
333 			mn->mn_type));
334 		break;
335 	}
336 }
337 
338 /*
339  * Release a file number
340  */
341 static void
342 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
343 {
344 	switch (mn->mn_type) {
345 	case mqfstype_root:
346 	case mqfstype_dir:
347 	case mqfstype_file:
348 	case mqfstype_symlink:
349 		free_unr(mi->mi_unrhdr, mn->mn_fileno);
350 		break;
351 	case mqfstype_this:
352 	case mqfstype_parent:
353 		/* ignore these, as they don't "own" their file number */
354 		break;
355 	default:
356 		KASSERT(0,
357 		    ("mqfs_fileno_free() called for unknown type node: %d",
358 			mn->mn_type));
359 		break;
360 	}
361 }
362 
363 static __inline struct mqfs_node *
364 mqnode_alloc(void)
365 {
366 	return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
367 }
368 
369 static __inline void
370 mqnode_free(struct mqfs_node *node)
371 {
372 	uma_zfree(mqnode_zone, node);
373 }
374 
375 static __inline void
376 mqnode_addref(struct mqfs_node *node)
377 {
378 	atomic_fetchadd_int(&node->mn_refcount, 1);
379 }
380 
381 static __inline void
382 mqnode_release(struct mqfs_node *node)
383 {
384 	struct mqfs_info *mqfs;
385 	int old, exp;
386 
387 	mqfs = node->mn_info;
388 	old = atomic_fetchadd_int(&node->mn_refcount, -1);
389 	if (node->mn_type == mqfstype_dir ||
390 	    node->mn_type == mqfstype_root)
391 		exp = 3; /* include . and .. */
392 	else
393 		exp = 1;
394 	if (old == exp) {
395 		int locked = sx_xlocked(&mqfs->mi_lock);
396 		if (!locked)
397 			sx_xlock(&mqfs->mi_lock);
398 		mqfs_destroy(node);
399 		if (!locked)
400 			sx_xunlock(&mqfs->mi_lock);
401 	}
402 }
403 
404 /*
405  * Add a node to a directory
406  */
407 static int
408 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
409 {
410 	KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
411 	KASSERT(parent->mn_info != NULL,
412 	    ("%s(): parent has no mn_info", __func__));
413 	KASSERT(parent->mn_type == mqfstype_dir ||
414 	    parent->mn_type == mqfstype_root,
415 	    ("%s(): parent is not a directory", __func__));
416 
417 	node->mn_info = parent->mn_info;
418 	node->mn_parent = parent;
419 	LIST_INIT(&node->mn_children);
420 	LIST_INIT(&node->mn_vnodes);
421 	LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
422 	mqnode_addref(parent);
423 	return (0);
424 }
425 
426 static struct mqfs_node *
427 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
428 	int nodetype)
429 {
430 	struct mqfs_node *node;
431 
432 	node = mqnode_alloc();
433 	strncpy(node->mn_name, name, namelen);
434 	node->mn_type = nodetype;
435 	node->mn_refcount = 1;
436 	vfs_timestamp(&node->mn_birth);
437 	node->mn_ctime = node->mn_atime = node->mn_mtime
438 		= node->mn_birth;
439 	node->mn_uid = cred->cr_uid;
440 	node->mn_gid = cred->cr_gid;
441 	node->mn_mode = mode;
442 	return (node);
443 }
444 
445 /*
446  * Create a file
447  */
448 static struct mqfs_node *
449 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
450 	struct ucred *cred, int mode)
451 {
452 	struct mqfs_node *node;
453 
454 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
455 	if (mqfs_add_node(parent, node) != 0) {
456 		mqnode_free(node);
457 		return (NULL);
458 	}
459 	return (node);
460 }
461 
462 /*
463  * Add . and .. to a directory
464  */
465 static int
466 mqfs_fixup_dir(struct mqfs_node *parent)
467 {
468 	struct mqfs_node *dir;
469 
470 	dir = mqnode_alloc();
471 	dir->mn_name[0] = '.';
472 	dir->mn_type = mqfstype_this;
473 	dir->mn_refcount = 1;
474 	if (mqfs_add_node(parent, dir) != 0) {
475 		mqnode_free(dir);
476 		return (-1);
477 	}
478 
479 	dir = mqnode_alloc();
480 	dir->mn_name[0] = dir->mn_name[1] = '.';
481 	dir->mn_type = mqfstype_parent;
482 	dir->mn_refcount = 1;
483 
484 	if (mqfs_add_node(parent, dir) != 0) {
485 		mqnode_free(dir);
486 		return (-1);
487 	}
488 
489 	return (0);
490 }
491 
492 #ifdef notyet
493 
494 /*
495  * Create a directory
496  */
497 static struct mqfs_node *
498 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
499 	struct ucred *cred, int mode)
500 {
501 	struct mqfs_node *node;
502 
503 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
504 	if (mqfs_add_node(parent, node) != 0) {
505 		mqnode_free(node);
506 		return (NULL);
507 	}
508 
509 	if (mqfs_fixup_dir(node) != 0) {
510 		mqfs_destroy(node);
511 		return (NULL);
512 	}
513 	return (node);
514 }
515 
516 /*
517  * Create a symlink
518  */
519 static struct mqfs_node *
520 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
521 	struct ucred *cred, int mode)
522 {
523 	struct mqfs_node *node;
524 
525 	node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
526 	if (mqfs_add_node(parent, node) != 0) {
527 		mqnode_free(node);
528 		return (NULL);
529 	}
530 	return (node);
531 }
532 
533 #endif
534 
535 /*
536  * Destroy a node or a tree of nodes
537  */
538 static int
539 mqfs_destroy(struct mqfs_node *node)
540 {
541 	struct mqfs_node *parent;
542 
543 	KASSERT(node != NULL,
544 	    ("%s(): node is NULL", __func__));
545 	KASSERT(node->mn_info != NULL,
546 	    ("%s(): node has no mn_info", __func__));
547 
548 	/* destroy children */
549 	if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
550 		while (! LIST_EMPTY(&node->mn_children))
551 			mqfs_destroy(LIST_FIRST(&node->mn_children));
552 
553 	/* unlink from parent */
554 	if ((parent = node->mn_parent) != NULL) {
555 		KASSERT(parent->mn_info == node->mn_info,
556 		    ("%s(): parent has different mn_info", __func__));
557 		LIST_REMOVE(node, mn_sibling);
558 	}
559 
560 	if (node->mn_fileno != 0)
561 		mqfs_fileno_free(node->mn_info, node);
562 	if (node->mn_data != NULL)
563 		mqueue_free(node->mn_data);
564 	mqnode_free(node);
565 	return (0);
566 }
567 
568 /*
569  * Mount a mqfs instance
570  */
571 static int
572 mqfs_mount(struct mount *mp)
573 {
574 	struct statfs *sbp;
575 
576 	if (mp->mnt_flag & MNT_UPDATE)
577 		return (EOPNOTSUPP);
578 
579 	mp->mnt_data = &mqfs_data;
580 	MNT_ILOCK(mp);
581 	mp->mnt_flag |= MNT_LOCAL;
582 	mp->mnt_kern_flag |= MNTK_MPSAFE;
583 	MNT_IUNLOCK(mp);
584 	vfs_getnewfsid(mp);
585 
586 	sbp = &mp->mnt_stat;
587 	vfs_mountedfrom(mp, "mqueue");
588 	sbp->f_bsize = PAGE_SIZE;
589 	sbp->f_iosize = PAGE_SIZE;
590 	sbp->f_blocks = 1;
591 	sbp->f_bfree = 0;
592 	sbp->f_bavail = 0;
593 	sbp->f_files = 1;
594 	sbp->f_ffree = 0;
595 	return (0);
596 }
597 
598 /*
599  * Unmount a mqfs instance
600  */
601 static int
602 mqfs_unmount(struct mount *mp, int mntflags)
603 {
604 	int error;
605 
606 	error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0,
607 	    curthread);
608 	return (error);
609 }
610 
611 /*
612  * Return a root vnode
613  */
614 static int
615 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
616 {
617 	struct mqfs_info *mqfs;
618 	int ret;
619 
620 	mqfs = VFSTOMQFS(mp);
621 	ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
622 	return (ret);
623 }
624 
625 /*
626  * Return filesystem stats
627  */
628 static int
629 mqfs_statfs(struct mount *mp, struct statfs *sbp)
630 {
631 	/* XXX update statistics */
632 	return (0);
633 }
634 
635 /*
636  * Initialize a mqfs instance
637  */
638 static int
639 mqfs_init(struct vfsconf *vfc)
640 {
641 	struct mqfs_node *root;
642 	struct mqfs_info *mi;
643 
644 	mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
645 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
646 	mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
647 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
648 	mvdata_zone = uma_zcreate("mvdata",
649 		sizeof(struct mqfs_vdata), NULL, NULL, NULL,
650 		NULL, UMA_ALIGN_PTR, 0);
651 	mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
652 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
653 	mi = &mqfs_data;
654 	sx_init(&mi->mi_lock, "mqfs lock");
655 	/* set up the root diretory */
656 	root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
657 		mqfstype_root);
658 	root->mn_info = mi;
659 	LIST_INIT(&root->mn_children);
660 	LIST_INIT(&root->mn_vnodes);
661 	mi->mi_root = root;
662 	mqfs_fileno_init(mi);
663 	mqfs_fileno_alloc(mi, root);
664 	mqfs_fixup_dir(root);
665 	exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
666 	    EVENTHANDLER_PRI_ANY);
667 	mq_fdclose = mqueue_fdclose;
668 	p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
669 	return (0);
670 }
671 
672 /*
673  * Destroy a mqfs instance
674  */
675 static int
676 mqfs_uninit(struct vfsconf *vfc)
677 {
678 	struct mqfs_info *mi;
679 
680 	if (!unloadable)
681 		return (EOPNOTSUPP);
682 	EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
683 	mi = &mqfs_data;
684 	mqfs_destroy(mi->mi_root);
685 	mi->mi_root = NULL;
686 	mqfs_fileno_uninit(mi);
687 	sx_destroy(&mi->mi_lock);
688 	uma_zdestroy(mqnode_zone);
689 	uma_zdestroy(mqueue_zone);
690 	uma_zdestroy(mvdata_zone);
691 	uma_zdestroy(mqnoti_zone);
692 	return (0);
693 }
694 
695 /*
696  * task routine
697  */
698 static void
699 do_recycle(void *context, int pending __unused)
700 {
701 	struct vnode *vp = (struct vnode *)context;
702 
703 	vrecycle(vp, curthread);
704 	vdrop(vp);
705 }
706 
707 /*
708  * Allocate a vnode
709  */
710 static int
711 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
712 {
713 	struct mqfs_vdata *vd;
714 	struct mqfs_info  *mqfs;
715 	struct vnode *newvpp;
716 	int error;
717 
718 	mqfs = pn->mn_info;
719 	*vpp = NULL;
720 	sx_xlock(&mqfs->mi_lock);
721 	LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
722 		if (vd->mv_vnode->v_mount == mp) {
723 			vhold(vd->mv_vnode);
724 			break;
725 		}
726 	}
727 
728 	if (vd != NULL) {
729 found:
730 		*vpp = vd->mv_vnode;
731 		sx_xunlock(&mqfs->mi_lock);
732 		error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
733 		vdrop(*vpp);
734 		return (error);
735 	}
736 	sx_xunlock(&mqfs->mi_lock);
737 
738 	error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
739 	if (error)
740 		return (error);
741 	vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
742 	error = insmntque(newvpp, mp);
743 	if (error != 0)
744 		return (error);
745 
746 	sx_xlock(&mqfs->mi_lock);
747 	/*
748 	 * Check if it has already been allocated
749 	 * while we were blocked.
750 	 */
751 	LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
752 		if (vd->mv_vnode->v_mount == mp) {
753 			vhold(vd->mv_vnode);
754 			sx_xunlock(&mqfs->mi_lock);
755 
756 			vgone(newvpp);
757 			vput(newvpp);
758 			goto found;
759 		}
760 	}
761 
762 	*vpp = newvpp;
763 
764 	vd = uma_zalloc(mvdata_zone, M_WAITOK);
765 	(*vpp)->v_data = vd;
766 	vd->mv_vnode = *vpp;
767 	vd->mv_node = pn;
768 	TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
769 	LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
770 	mqnode_addref(pn);
771 	switch (pn->mn_type) {
772 	case mqfstype_root:
773 		(*vpp)->v_vflag = VV_ROOT;
774 		/* fall through */
775 	case mqfstype_dir:
776 	case mqfstype_this:
777 	case mqfstype_parent:
778 		(*vpp)->v_type = VDIR;
779 		break;
780 	case mqfstype_file:
781 		(*vpp)->v_type = VREG;
782 		break;
783 	case mqfstype_symlink:
784 		(*vpp)->v_type = VLNK;
785 		break;
786 	case mqfstype_none:
787 		KASSERT(0, ("mqfs_allocf called for null node\n"));
788 	default:
789 		panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
790 	}
791 	sx_xunlock(&mqfs->mi_lock);
792 	return (0);
793 }
794 
795 /*
796  * Search a directory entry
797  */
798 static struct mqfs_node *
799 mqfs_search(struct mqfs_node *pd, const char *name, int len)
800 {
801 	struct mqfs_node *pn;
802 
803 	sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
804 	LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
805 		if (strncmp(pn->mn_name, name, len) == 0 &&
806 		    pn->mn_name[len] == '\0')
807 			return (pn);
808 	}
809 	return (NULL);
810 }
811 
812 /*
813  * Look up a file or directory.
814  */
815 static int
816 mqfs_lookupx(struct vop_cachedlookup_args *ap)
817 {
818 	struct componentname *cnp;
819 	struct vnode *dvp, **vpp;
820 	struct mqfs_node *pd;
821 	struct mqfs_node *pn;
822 	struct mqfs_info *mqfs;
823 	int nameiop, flags, error, namelen;
824 	char *pname;
825 	struct thread *td;
826 
827 	cnp = ap->a_cnp;
828 	vpp = ap->a_vpp;
829 	dvp = ap->a_dvp;
830 	pname = cnp->cn_nameptr;
831 	namelen = cnp->cn_namelen;
832 	td = cnp->cn_thread;
833 	flags = cnp->cn_flags;
834 	nameiop = cnp->cn_nameiop;
835 	pd = VTON(dvp);
836 	pn = NULL;
837 	mqfs = pd->mn_info;
838 	*vpp = NULLVP;
839 
840 	if (dvp->v_type != VDIR)
841 		return (ENOTDIR);
842 
843 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
844 	if (error)
845 		return (error);
846 
847 	/* shortcut: check if the name is too long */
848 	if (cnp->cn_namelen >= MQFS_NAMELEN)
849 		return (ENOENT);
850 
851 	/* self */
852 	if (namelen == 1 && pname[0] == '.') {
853 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
854 			return (EINVAL);
855 		pn = pd;
856 		*vpp = dvp;
857 		VREF(dvp);
858 		return (0);
859 	}
860 
861 	/* parent */
862 	if (cnp->cn_flags & ISDOTDOT) {
863 		if (dvp->v_vflag & VV_ROOT)
864 			return (EIO);
865 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
866 			return (EINVAL);
867 		VOP_UNLOCK(dvp, 0);
868 		KASSERT(pd->mn_parent, ("non-root directory has no parent"));
869 		pn = pd->mn_parent;
870 		error = mqfs_allocv(dvp->v_mount, vpp, pn);
871 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
872 		return (error);
873 	}
874 
875 	/* named node */
876 	sx_xlock(&mqfs->mi_lock);
877 	pn = mqfs_search(pd, pname, namelen);
878 	if (pn != NULL)
879 		mqnode_addref(pn);
880 	sx_xunlock(&mqfs->mi_lock);
881 
882 	/* found */
883 	if (pn != NULL) {
884 		/* DELETE */
885 		if (nameiop == DELETE && (flags & ISLASTCN)) {
886 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
887 			if (error) {
888 				mqnode_release(pn);
889 				return (error);
890 			}
891 			if (*vpp == dvp) {
892 				VREF(dvp);
893 				*vpp = dvp;
894 				mqnode_release(pn);
895 				return (0);
896 			}
897 		}
898 
899 		/* allocate vnode */
900 		error = mqfs_allocv(dvp->v_mount, vpp, pn);
901 		mqnode_release(pn);
902 		if (error == 0 && cnp->cn_flags & MAKEENTRY)
903 			cache_enter(dvp, *vpp, cnp);
904 		return (error);
905 	}
906 
907 	/* not found */
908 
909 	/* will create a new entry in the directory ? */
910 	if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
911 	    && (flags & ISLASTCN)) {
912 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
913 		if (error)
914 			return (error);
915 		cnp->cn_flags |= SAVENAME;
916 		return (EJUSTRETURN);
917 	}
918 	return (ENOENT);
919 }
920 
921 #if 0
922 struct vop_lookup_args {
923 	struct vop_generic_args a_gen;
924 	struct vnode *a_dvp;
925 	struct vnode **a_vpp;
926 	struct componentname *a_cnp;
927 };
928 #endif
929 
930 /*
931  * vnode lookup operation
932  */
933 static int
934 mqfs_lookup(struct vop_cachedlookup_args *ap)
935 {
936 	int rc;
937 
938 	rc = mqfs_lookupx(ap);
939 	return (rc);
940 }
941 
942 #if 0
943 struct vop_create_args {
944 	struct vnode *a_dvp;
945 	struct vnode **a_vpp;
946 	struct componentname *a_cnp;
947 	struct vattr *a_vap;
948 };
949 #endif
950 
951 /*
952  * vnode creation operation
953  */
954 static int
955 mqfs_create(struct vop_create_args *ap)
956 {
957 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
958 	struct componentname *cnp = ap->a_cnp;
959 	struct mqfs_node *pd;
960 	struct mqfs_node *pn;
961 	struct mqueue *mq;
962 	int error;
963 
964 	pd = VTON(ap->a_dvp);
965 	if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
966 		return (ENOTDIR);
967 	mq = mqueue_alloc(NULL);
968 	if (mq == NULL)
969 		return (EAGAIN);
970 	sx_xlock(&mqfs->mi_lock);
971 	if ((cnp->cn_flags & HASBUF) == 0)
972 		panic("%s: no name", __func__);
973 	pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
974 		cnp->cn_cred, ap->a_vap->va_mode);
975 	if (pn == NULL) {
976 		sx_xunlock(&mqfs->mi_lock);
977 		error = ENOSPC;
978 	} else {
979 		mqnode_addref(pn);
980 		sx_xunlock(&mqfs->mi_lock);
981 		error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
982 		mqnode_release(pn);
983 		if (error)
984 			mqfs_destroy(pn);
985 		else
986 			pn->mn_data = mq;
987 	}
988 	if (error)
989 		mqueue_free(mq);
990 	return (error);
991 }
992 
993 /*
994  * Remove an entry
995  */
996 static
997 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
998 {
999 	struct mqfs_node *parent;
1000 	struct mqfs_vdata *vd;
1001 	int error = 0;
1002 
1003 	sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1004 
1005 	if (ucred->cr_uid != pn->mn_uid &&
1006 	    (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1007 		error = EACCES;
1008 	else if (!pn->mn_deleted) {
1009 		parent = pn->mn_parent;
1010 		pn->mn_parent = NULL;
1011 		pn->mn_deleted = 1;
1012 		LIST_REMOVE(pn, mn_sibling);
1013 		LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1014 			cache_purge(vd->mv_vnode);
1015 			vhold(vd->mv_vnode);
1016 			taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1017 		}
1018 		mqnode_release(pn);
1019 		mqnode_release(parent);
1020 	} else
1021 		error = ENOENT;
1022 	return (error);
1023 }
1024 
1025 #if 0
1026 struct vop_remove_args {
1027 	struct vnode *a_dvp;
1028 	struct vnode *a_vp;
1029 	struct componentname *a_cnp;
1030 };
1031 #endif
1032 
1033 /*
1034  * vnode removal operation
1035  */
1036 static int
1037 mqfs_remove(struct vop_remove_args *ap)
1038 {
1039 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1040 	struct mqfs_node *pn;
1041 	int error;
1042 
1043 	if (ap->a_vp->v_type == VDIR)
1044                 return (EPERM);
1045 	pn = VTON(ap->a_vp);
1046 	sx_xlock(&mqfs->mi_lock);
1047 	error = do_unlink(pn, ap->a_cnp->cn_cred);
1048 	sx_xunlock(&mqfs->mi_lock);
1049 	return (error);
1050 }
1051 
1052 #if 0
1053 struct vop_inactive_args {
1054 	struct vnode *a_vp;
1055 	struct thread *a_td;
1056 };
1057 #endif
1058 
1059 static int
1060 mqfs_inactive(struct vop_inactive_args *ap)
1061 {
1062 	struct mqfs_node *pn = VTON(ap->a_vp);
1063 
1064 	if (pn->mn_deleted)
1065 		vrecycle(ap->a_vp, ap->a_td);
1066 	return (0);
1067 }
1068 
1069 #if 0
1070 struct vop_reclaim_args {
1071 	struct vop_generic_args a_gen;
1072 	struct vnode *a_vp;
1073 	struct thread *a_td;
1074 };
1075 #endif
1076 
1077 static int
1078 mqfs_reclaim(struct vop_reclaim_args *ap)
1079 {
1080 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1081 	struct vnode *vp = ap->a_vp;
1082 	struct mqfs_node *pn;
1083 	struct mqfs_vdata *vd;
1084 
1085 	vd = vp->v_data;
1086 	pn = vd->mv_node;
1087 	sx_xlock(&mqfs->mi_lock);
1088 	vp->v_data = NULL;
1089 	LIST_REMOVE(vd, mv_link);
1090 	uma_zfree(mvdata_zone, vd);
1091 	mqnode_release(pn);
1092 	sx_xunlock(&mqfs->mi_lock);
1093 	return (0);
1094 }
1095 
1096 #if 0
1097 struct vop_open_args {
1098 	struct vop_generic_args a_gen;
1099 	struct vnode *a_vp;
1100 	int a_mode;
1101 	struct ucred *a_cred;
1102 	struct thread *a_td;
1103 	struct file *a_fp;
1104 };
1105 #endif
1106 
1107 static int
1108 mqfs_open(struct vop_open_args *ap)
1109 {
1110 	return (0);
1111 }
1112 
1113 #if 0
1114 struct vop_close_args {
1115 	struct vop_generic_args a_gen;
1116 	struct vnode *a_vp;
1117 	int a_fflag;
1118 	struct ucred *a_cred;
1119 	struct thread *a_td;
1120 };
1121 #endif
1122 
1123 static int
1124 mqfs_close(struct vop_close_args *ap)
1125 {
1126 	return (0);
1127 }
1128 
1129 #if 0
1130 struct vop_access_args {
1131 	struct vop_generic_args a_gen;
1132 	struct vnode *a_vp;
1133 	accmode_t a_accmode;
1134 	struct ucred *a_cred;
1135 	struct thread *a_td;
1136 };
1137 #endif
1138 
1139 /*
1140  * Verify permissions
1141  */
1142 static int
1143 mqfs_access(struct vop_access_args *ap)
1144 {
1145 	struct vnode *vp = ap->a_vp;
1146 	struct vattr vattr;
1147 	int error;
1148 
1149 	error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1150 	if (error)
1151 		return (error);
1152 	error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1153 	    vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1154 	return (error);
1155 }
1156 
1157 #if 0
1158 struct vop_getattr_args {
1159 	struct vop_generic_args a_gen;
1160 	struct vnode *a_vp;
1161 	struct vattr *a_vap;
1162 	struct ucred *a_cred;
1163 };
1164 #endif
1165 
1166 /*
1167  * Get file attributes
1168  */
1169 static int
1170 mqfs_getattr(struct vop_getattr_args *ap)
1171 {
1172 	struct vnode *vp = ap->a_vp;
1173 	struct mqfs_node *pn = VTON(vp);
1174 	struct vattr *vap = ap->a_vap;
1175 	int error = 0;
1176 
1177 	vap->va_type = vp->v_type;
1178 	vap->va_mode = pn->mn_mode;
1179 	vap->va_nlink = 1;
1180 	vap->va_uid = pn->mn_uid;
1181 	vap->va_gid = pn->mn_gid;
1182 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1183 	vap->va_fileid = pn->mn_fileno;
1184 	vap->va_size = 0;
1185 	vap->va_blocksize = PAGE_SIZE;
1186 	vap->va_bytes = vap->va_size = 0;
1187 	vap->va_atime = pn->mn_atime;
1188 	vap->va_mtime = pn->mn_mtime;
1189 	vap->va_ctime = pn->mn_ctime;
1190 	vap->va_birthtime = pn->mn_birth;
1191 	vap->va_gen = 0;
1192 	vap->va_flags = 0;
1193 	vap->va_rdev = NODEV;
1194 	vap->va_bytes = 0;
1195 	vap->va_filerev = 0;
1196 	return (error);
1197 }
1198 
1199 #if 0
1200 struct vop_setattr_args {
1201 	struct vop_generic_args a_gen;
1202 	struct vnode *a_vp;
1203 	struct vattr *a_vap;
1204 	struct ucred *a_cred;
1205 };
1206 #endif
1207 /*
1208  * Set attributes
1209  */
1210 static int
1211 mqfs_setattr(struct vop_setattr_args *ap)
1212 {
1213 	struct mqfs_node *pn;
1214 	struct vattr *vap;
1215 	struct vnode *vp;
1216 	struct thread *td;
1217 	int c, error;
1218 	uid_t uid;
1219 	gid_t gid;
1220 
1221 	td = curthread;
1222 	vap = ap->a_vap;
1223 	vp = ap->a_vp;
1224 	if ((vap->va_type != VNON) ||
1225 	    (vap->va_nlink != VNOVAL) ||
1226 	    (vap->va_fsid != VNOVAL) ||
1227 	    (vap->va_fileid != VNOVAL) ||
1228 	    (vap->va_blocksize != VNOVAL) ||
1229 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1230 	    (vap->va_rdev != VNOVAL) ||
1231 	    ((int)vap->va_bytes != VNOVAL) ||
1232 	    (vap->va_gen != VNOVAL)) {
1233 		return (EINVAL);
1234 	}
1235 
1236 	pn = VTON(vp);
1237 
1238 	error = c = 0;
1239 	if (vap->va_uid == (uid_t)VNOVAL)
1240 		uid = pn->mn_uid;
1241 	else
1242 		uid = vap->va_uid;
1243 	if (vap->va_gid == (gid_t)VNOVAL)
1244 		gid = pn->mn_gid;
1245 	else
1246 		gid = vap->va_gid;
1247 
1248 	if (uid != pn->mn_uid || gid != pn->mn_gid) {
1249 		/*
1250 		 * To modify the ownership of a file, must possess VADMIN
1251 		 * for that file.
1252 		 */
1253 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1254 			return (error);
1255 
1256 		/*
1257 		 * XXXRW: Why is there a privilege check here: shouldn't the
1258 		 * check in VOP_ACCESS() be enough?  Also, are the group bits
1259 		 * below definitely right?
1260 		 */
1261 		if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1262 		    (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1263 		    (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1264 			return (error);
1265 		pn->mn_uid = uid;
1266 		pn->mn_gid = gid;
1267 		c = 1;
1268 	}
1269 
1270 	if (vap->va_mode != (mode_t)VNOVAL) {
1271 		if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1272 		    (error = priv_check(td, PRIV_MQ_ADMIN)))
1273 			return (error);
1274 		pn->mn_mode = vap->va_mode;
1275 		c = 1;
1276 	}
1277 
1278 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1279 		/* See the comment in ufs_vnops::ufs_setattr(). */
1280 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1281 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1282 		    (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1283 			return (error);
1284 		if (vap->va_atime.tv_sec != VNOVAL) {
1285 			pn->mn_atime = vap->va_atime;
1286 		}
1287 		if (vap->va_mtime.tv_sec != VNOVAL) {
1288 			pn->mn_mtime = vap->va_mtime;
1289 		}
1290 		c = 1;
1291 	}
1292 	if (c) {
1293 		vfs_timestamp(&pn->mn_ctime);
1294 	}
1295 	return (0);
1296 }
1297 
1298 #if 0
1299 struct vop_read_args {
1300 	struct vop_generic_args a_gen;
1301 	struct vnode *a_vp;
1302 	struct uio *a_uio;
1303 	int a_ioflag;
1304 	struct ucred *a_cred;
1305 };
1306 #endif
1307 
1308 /*
1309  * Read from a file
1310  */
1311 static int
1312 mqfs_read(struct vop_read_args *ap)
1313 {
1314 	char buf[80];
1315 	struct vnode *vp = ap->a_vp;
1316 	struct uio *uio = ap->a_uio;
1317 	struct mqfs_node *pn;
1318 	struct mqueue *mq;
1319 	int len, error;
1320 
1321 	if (vp->v_type != VREG)
1322 		return (EINVAL);
1323 
1324 	pn = VTON(vp);
1325 	mq = VTOMQ(vp);
1326 	snprintf(buf, sizeof(buf),
1327 		"QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1328 		mq->mq_totalbytes,
1329 		mq->mq_maxmsg,
1330 		mq->mq_curmsgs,
1331 		mq->mq_msgsize);
1332 	buf[sizeof(buf)-1] = '\0';
1333 	len = strlen(buf);
1334 	error = uiomove_frombuf(buf, len, uio);
1335 	return (error);
1336 }
1337 
1338 #if 0
1339 struct vop_readdir_args {
1340 	struct vop_generic_args a_gen;
1341 	struct vnode *a_vp;
1342 	struct uio *a_uio;
1343 	struct ucred *a_cred;
1344 	int *a_eofflag;
1345 	int *a_ncookies;
1346 	u_long **a_cookies;
1347 };
1348 #endif
1349 
1350 /*
1351  * Return directory entries.
1352  */
1353 static int
1354 mqfs_readdir(struct vop_readdir_args *ap)
1355 {
1356 	struct vnode *vp;
1357 	struct mqfs_info *mi;
1358 	struct mqfs_node *pd;
1359 	struct mqfs_node *pn;
1360 	struct dirent entry;
1361 	struct uio *uio;
1362 	int *tmp_ncookies = NULL;
1363 	off_t offset;
1364 	int error, i;
1365 
1366 	vp = ap->a_vp;
1367 	mi = VFSTOMQFS(vp->v_mount);
1368 	pd = VTON(vp);
1369 	uio = ap->a_uio;
1370 
1371 	if (vp->v_type != VDIR)
1372 		return (ENOTDIR);
1373 
1374 	if (uio->uio_offset < 0)
1375 		return (EINVAL);
1376 
1377 	if (ap->a_ncookies != NULL) {
1378 		tmp_ncookies = ap->a_ncookies;
1379 		*ap->a_ncookies = 0;
1380 		ap->a_ncookies = NULL;
1381         }
1382 
1383 	error = 0;
1384 	offset = 0;
1385 
1386 	sx_xlock(&mi->mi_lock);
1387 
1388 	LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1389 		entry.d_reclen = sizeof(entry);
1390 		if (!pn->mn_fileno)
1391 			mqfs_fileno_alloc(mi, pn);
1392 		entry.d_fileno = pn->mn_fileno;
1393 		for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1394 			entry.d_name[i] = pn->mn_name[i];
1395 		entry.d_name[i] = 0;
1396 		entry.d_namlen = i;
1397 		switch (pn->mn_type) {
1398 		case mqfstype_root:
1399 		case mqfstype_dir:
1400 		case mqfstype_this:
1401 		case mqfstype_parent:
1402 			entry.d_type = DT_DIR;
1403 			break;
1404 		case mqfstype_file:
1405 			entry.d_type = DT_REG;
1406 			break;
1407 		case mqfstype_symlink:
1408 			entry.d_type = DT_LNK;
1409 			break;
1410 		default:
1411 			panic("%s has unexpected node type: %d", pn->mn_name,
1412 				pn->mn_type);
1413 		}
1414 		if (entry.d_reclen > uio->uio_resid)
1415                         break;
1416 		if (offset >= uio->uio_offset) {
1417 			error = vfs_read_dirent(ap, &entry, offset);
1418                         if (error)
1419                                 break;
1420                 }
1421                 offset += entry.d_reclen;
1422 	}
1423 	sx_xunlock(&mi->mi_lock);
1424 
1425 	uio->uio_offset = offset;
1426 
1427 	if (tmp_ncookies != NULL)
1428 		ap->a_ncookies = tmp_ncookies;
1429 
1430 	return (error);
1431 }
1432 
1433 #ifdef notyet
1434 
1435 #if 0
1436 struct vop_mkdir_args {
1437 	struct vnode *a_dvp;
1438 	struvt vnode **a_vpp;
1439 	struvt componentname *a_cnp;
1440 	struct vattr *a_vap;
1441 };
1442 #endif
1443 
1444 /*
1445  * Create a directory.
1446  */
1447 static int
1448 mqfs_mkdir(struct vop_mkdir_args *ap)
1449 {
1450 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1451 	struct componentname *cnp = ap->a_cnp;
1452 	struct mqfs_node *pd = VTON(ap->a_dvp);
1453 	struct mqfs_node *pn;
1454 	int error;
1455 
1456 	if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1457 		return (ENOTDIR);
1458 	sx_xlock(&mqfs->mi_lock);
1459 	if ((cnp->cn_flags & HASBUF) == 0)
1460 		panic("%s: no name", __func__);
1461 	pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1462 		ap->a_vap->cn_cred, ap->a_vap->va_mode);
1463 	if (pn != NULL)
1464 		mqnode_addref(pn);
1465 	sx_xunlock(&mqfs->mi_lock);
1466 	if (pn == NULL) {
1467 		error = ENOSPC;
1468 	} else {
1469 		error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1470 		mqnode_release(pn);
1471 	}
1472 	return (error);
1473 }
1474 
1475 #if 0
1476 struct vop_rmdir_args {
1477 	struct vnode *a_dvp;
1478 	struct vnode *a_vp;
1479 	struct componentname *a_cnp;
1480 };
1481 #endif
1482 
1483 /*
1484  * Remove a directory.
1485  */
1486 static int
1487 mqfs_rmdir(struct vop_rmdir_args *ap)
1488 {
1489 	struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1490 	struct mqfs_node *pn = VTON(ap->a_vp);
1491 	struct mqfs_node *pt;
1492 
1493 	if (pn->mn_type != mqfstype_dir)
1494 		return (ENOTDIR);
1495 
1496 	sx_xlock(&mqfs->mi_lock);
1497 	if (pn->mn_deleted) {
1498 		sx_xunlock(&mqfs->mi_lock);
1499 		return (ENOENT);
1500 	}
1501 
1502 	pt = LIST_FIRST(&pn->mn_children);
1503 	pt = LIST_NEXT(pt, mn_sibling);
1504 	pt = LIST_NEXT(pt, mn_sibling);
1505 	if (pt != NULL) {
1506 		sx_xunlock(&mqfs->mi_lock);
1507 		return (ENOTEMPTY);
1508 	}
1509 	pt = pn->mn_parent;
1510 	pn->mn_parent = NULL;
1511 	pn->mn_deleted = 1;
1512 	LIST_REMOVE(pn, mn_sibling);
1513 	mqnode_release(pn);
1514 	mqnode_release(pt);
1515 	sx_xunlock(&mqfs->mi_lock);
1516 	cache_purge(ap->a_vp);
1517 	return (0);
1518 }
1519 
1520 #endif /* notyet */
1521 
1522 /*
1523  * Allocate a message queue
1524  */
1525 static struct mqueue *
1526 mqueue_alloc(const struct mq_attr *attr)
1527 {
1528 	struct mqueue *mq;
1529 
1530 	if (curmq >= maxmq)
1531 		return (NULL);
1532 	mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1533 	TAILQ_INIT(&mq->mq_msgq);
1534 	if (attr != NULL) {
1535 		mq->mq_maxmsg = attr->mq_maxmsg;
1536 		mq->mq_msgsize = attr->mq_msgsize;
1537 	} else {
1538 		mq->mq_maxmsg = default_maxmsg;
1539 		mq->mq_msgsize = default_msgsize;
1540 	}
1541 	mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1542 	knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1543 	knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1544 	atomic_add_int(&curmq, 1);
1545 	return (mq);
1546 }
1547 
1548 /*
1549  * Destroy a message queue
1550  */
1551 static void
1552 mqueue_free(struct mqueue *mq)
1553 {
1554 	struct mqueue_msg *msg;
1555 
1556 	while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1557 		TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1558 		free(msg, M_MQUEUEDATA);
1559 	}
1560 
1561 	mtx_destroy(&mq->mq_mutex);
1562 	knlist_destroy(&mq->mq_rsel.si_note);
1563 	knlist_destroy(&mq->mq_wsel.si_note);
1564 	uma_zfree(mqueue_zone, mq);
1565 	atomic_add_int(&curmq, -1);
1566 }
1567 
1568 /*
1569  * Load a message from user space
1570  */
1571 static struct mqueue_msg *
1572 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1573 {
1574 	struct mqueue_msg *msg;
1575 	size_t len;
1576 	int error;
1577 
1578 	len = sizeof(struct mqueue_msg) + msg_size;
1579 	msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1580 	error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1581 	    msg_size);
1582 	if (error) {
1583 		free(msg, M_MQUEUEDATA);
1584 		msg = NULL;
1585 	} else {
1586 		msg->msg_size = msg_size;
1587 		msg->msg_prio = msg_prio;
1588 	}
1589 	return (msg);
1590 }
1591 
1592 /*
1593  * Save a message to user space
1594  */
1595 static int
1596 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1597 {
1598 	int error;
1599 
1600 	error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1601 		msg->msg_size);
1602 	if (error == 0 && msg_prio != NULL)
1603 		error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1604 	return (error);
1605 }
1606 
1607 /*
1608  * Free a message's memory
1609  */
1610 static __inline void
1611 mqueue_freemsg(struct mqueue_msg *msg)
1612 {
1613 	free(msg, M_MQUEUEDATA);
1614 }
1615 
1616 /*
1617  * Send a message. if waitok is false, thread will not be
1618  * blocked if there is no data in queue, otherwise, absolute
1619  * time will be checked.
1620  */
1621 int
1622 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1623 	size_t msg_len, unsigned msg_prio, int waitok,
1624 	const struct timespec *abs_timeout)
1625 {
1626 	struct mqueue_msg *msg;
1627 	struct timespec ts, ts2;
1628 	struct timeval tv;
1629 	int error;
1630 
1631 	if (msg_prio >= MQ_PRIO_MAX)
1632 		return (EINVAL);
1633 	if (msg_len > mq->mq_msgsize)
1634 		return (EMSGSIZE);
1635 	msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1636 	if (msg == NULL)
1637 		return (EFAULT);
1638 
1639 	/* O_NONBLOCK case */
1640 	if (!waitok) {
1641 		error = _mqueue_send(mq, msg, -1);
1642 		if (error)
1643 			goto bad;
1644 		return (0);
1645 	}
1646 
1647 	/* we allow a null timeout (wait forever) */
1648 	if (abs_timeout == NULL) {
1649 		error = _mqueue_send(mq, msg, 0);
1650 		if (error)
1651 			goto bad;
1652 		return (0);
1653 	}
1654 
1655 	/* send it before checking time */
1656 	error = _mqueue_send(mq, msg, -1);
1657 	if (error == 0)
1658 		return (0);
1659 
1660 	if (error != EAGAIN)
1661 		goto bad;
1662 
1663 	if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1664 		error = EINVAL;
1665 		goto bad;
1666 	}
1667 	for (;;) {
1668 		ts2 = *abs_timeout;
1669 		getnanotime(&ts);
1670 		timespecsub(&ts2, &ts);
1671 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1672 			error = ETIMEDOUT;
1673 			break;
1674 		}
1675 		TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1676 		error = _mqueue_send(mq, msg, tvtohz(&tv));
1677 		if (error != ETIMEDOUT)
1678 			break;
1679 	}
1680 	if (error == 0)
1681 		return (0);
1682 bad:
1683 	mqueue_freemsg(msg);
1684 	return (error);
1685 }
1686 
1687 /*
1688  * Common routine to send a message
1689  */
1690 static int
1691 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1692 {
1693 	struct mqueue_msg *msg2;
1694 	int error = 0;
1695 
1696 	mtx_lock(&mq->mq_mutex);
1697 	while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1698 		if (timo < 0) {
1699 			mtx_unlock(&mq->mq_mutex);
1700 			return (EAGAIN);
1701 		}
1702 		mq->mq_senders++;
1703 		error = msleep(&mq->mq_senders, &mq->mq_mutex,
1704 			    PCATCH, "mqsend", timo);
1705 		mq->mq_senders--;
1706 		if (error == EAGAIN)
1707 			error = ETIMEDOUT;
1708 	}
1709 	if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1710 		mtx_unlock(&mq->mq_mutex);
1711 		return (error);
1712 	}
1713 	error = 0;
1714 	if (TAILQ_EMPTY(&mq->mq_msgq)) {
1715 		TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1716 	} else {
1717 		if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1718 			TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1719 		} else {
1720 			TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1721 				if (msg2->msg_prio < msg->msg_prio)
1722 					break;
1723 			}
1724 			TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1725 		}
1726 	}
1727 	mq->mq_curmsgs++;
1728 	mq->mq_totalbytes += msg->msg_size;
1729 	if (mq->mq_receivers)
1730 		wakeup_one(&mq->mq_receivers);
1731 	else if (mq->mq_notifier != NULL)
1732 		mqueue_send_notification(mq);
1733 	if (mq->mq_flags & MQ_RSEL) {
1734 		mq->mq_flags &= ~MQ_RSEL;
1735 		selwakeup(&mq->mq_rsel);
1736 	}
1737 	KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1738 	mtx_unlock(&mq->mq_mutex);
1739 	return (0);
1740 }
1741 
1742 /*
1743  * Send realtime a signal to process which registered itself
1744  * successfully by mq_notify.
1745  */
1746 static void
1747 mqueue_send_notification(struct mqueue *mq)
1748 {
1749 	struct mqueue_notifier *nt;
1750 	struct proc *p;
1751 
1752 	mtx_assert(&mq->mq_mutex, MA_OWNED);
1753 	nt = mq->mq_notifier;
1754 	if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1755 		p = nt->nt_proc;
1756 		PROC_LOCK(p);
1757 		if (!KSI_ONQ(&nt->nt_ksi))
1758 			psignal_event(p, &nt->nt_sigev, &nt->nt_ksi);
1759 		PROC_UNLOCK(p);
1760 	}
1761 	mq->mq_notifier = NULL;
1762 }
1763 
1764 /*
1765  * Get a message. if waitok is false, thread will not be
1766  * blocked if there is no data in queue, otherwise, absolute
1767  * time will be checked.
1768  */
1769 int
1770 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1771 	size_t msg_len, unsigned *msg_prio, int waitok,
1772 	const struct timespec *abs_timeout)
1773 {
1774 	struct mqueue_msg *msg;
1775 	struct timespec ts, ts2;
1776 	struct timeval tv;
1777 	int error;
1778 
1779 	if (msg_len < mq->mq_msgsize)
1780 		return (EMSGSIZE);
1781 
1782 	/* O_NONBLOCK case */
1783 	if (!waitok) {
1784 		error = _mqueue_recv(mq, &msg, -1);
1785 		if (error)
1786 			return (error);
1787 		goto received;
1788 	}
1789 
1790 	/* we allow a null timeout (wait forever). */
1791 	if (abs_timeout == NULL) {
1792 		error = _mqueue_recv(mq, &msg, 0);
1793 		if (error)
1794 			return (error);
1795 		goto received;
1796 	}
1797 
1798 	/* try to get a message before checking time */
1799 	error = _mqueue_recv(mq, &msg, -1);
1800 	if (error == 0)
1801 		goto received;
1802 
1803 	if (error != EAGAIN)
1804 		return (error);
1805 
1806 	if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1807 		error = EINVAL;
1808 		return (error);
1809 	}
1810 
1811 	for (;;) {
1812 		ts2 = *abs_timeout;
1813 		getnanotime(&ts);
1814 		timespecsub(&ts2, &ts);
1815 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1816 			error = ETIMEDOUT;
1817 			return (error);
1818 		}
1819 		TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1820 		error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1821 		if (error == 0)
1822 			break;
1823 		if (error != ETIMEDOUT)
1824 			return (error);
1825 	}
1826 
1827 received:
1828 	error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1829 	if (error == 0) {
1830 		curthread->td_retval[0] = msg->msg_size;
1831 		curthread->td_retval[1] = 0;
1832 	}
1833 	mqueue_freemsg(msg);
1834 	return (error);
1835 }
1836 
1837 /*
1838  * Common routine to receive a message
1839  */
1840 static int
1841 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1842 {
1843 	int error = 0;
1844 
1845 	mtx_lock(&mq->mq_mutex);
1846 	while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1847 		if (timo < 0) {
1848 			mtx_unlock(&mq->mq_mutex);
1849 			return (EAGAIN);
1850 		}
1851 		mq->mq_receivers++;
1852 		error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1853 			    PCATCH, "mqrecv", timo);
1854 		mq->mq_receivers--;
1855 		if (error == EAGAIN)
1856 			error = ETIMEDOUT;
1857 	}
1858 	if (*msg != NULL) {
1859 		error = 0;
1860 		TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1861 		mq->mq_curmsgs--;
1862 		mq->mq_totalbytes -= (*msg)->msg_size;
1863 		if (mq->mq_senders)
1864 			wakeup_one(&mq->mq_senders);
1865 		if (mq->mq_flags & MQ_WSEL) {
1866 			mq->mq_flags &= ~MQ_WSEL;
1867 			selwakeup(&mq->mq_wsel);
1868 		}
1869 		KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1870 	}
1871 	if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1872 	    !TAILQ_EMPTY(&mq->mq_msgq)) {
1873 		mqueue_send_notification(mq);
1874 	}
1875 	mtx_unlock(&mq->mq_mutex);
1876 	return (error);
1877 }
1878 
1879 static __inline struct mqueue_notifier *
1880 notifier_alloc(void)
1881 {
1882 	return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1883 }
1884 
1885 static __inline void
1886 notifier_free(struct mqueue_notifier *p)
1887 {
1888 	uma_zfree(mqnoti_zone, p);
1889 }
1890 
1891 static struct mqueue_notifier *
1892 notifier_search(struct proc *p, int fd)
1893 {
1894 	struct mqueue_notifier *nt;
1895 
1896 	LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1897 		if (nt->nt_ksi.ksi_mqd == fd)
1898 			break;
1899 	}
1900 	return (nt);
1901 }
1902 
1903 static __inline void
1904 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1905 {
1906 	LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1907 }
1908 
1909 static __inline void
1910 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1911 {
1912 	LIST_REMOVE(nt, nt_link);
1913 	notifier_free(nt);
1914 }
1915 
1916 static void
1917 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1918 {
1919 	struct mqueue_notifier *nt;
1920 
1921 	mtx_assert(&mq->mq_mutex, MA_OWNED);
1922 	PROC_LOCK(p);
1923 	nt = notifier_search(p, fd);
1924 	if (nt != NULL) {
1925 		if (mq->mq_notifier == nt)
1926 			mq->mq_notifier = NULL;
1927 		sigqueue_take(&nt->nt_ksi);
1928 		notifier_delete(p, nt);
1929 	}
1930 	PROC_UNLOCK(p);
1931 }
1932 
1933 static int
1934 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
1935     const struct mq_attr *attr)
1936 {
1937 	char path[MQFS_NAMELEN + 1];
1938 	struct mqfs_node *pn;
1939 	struct filedesc *fdp;
1940 	struct file *fp;
1941 	struct mqueue *mq;
1942 	int fd, error, len, cmode;
1943 
1944 	fdp = td->td_proc->p_fd;
1945 	cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1946 	mq = NULL;
1947 	if ((flags & O_CREAT) != 0 && attr != NULL) {
1948 		if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1949 			return (EINVAL);
1950 		if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1951 			return (EINVAL);
1952 	}
1953 
1954 	error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
1955         if (error)
1956 		return (error);
1957 
1958 	/*
1959 	 * The first character of name must be a slash  (/) character
1960 	 * and the remaining characters of name cannot include any slash
1961 	 * characters.
1962 	 */
1963 	len = strlen(path);
1964 	if (len < 2  || path[0] != '/' || index(path + 1, '/') != NULL)
1965 		return (EINVAL);
1966 
1967 	error = falloc(td, &fp, &fd);
1968 	if (error)
1969 		return (error);
1970 
1971 	sx_xlock(&mqfs_data.mi_lock);
1972 	pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1973 	if (pn == NULL) {
1974 		if (!(flags & O_CREAT)) {
1975 			error = ENOENT;
1976 		} else {
1977 			mq = mqueue_alloc(attr);
1978 			if (mq == NULL) {
1979 				error = ENFILE;
1980 			} else {
1981 				pn = mqfs_create_file(mqfs_data.mi_root,
1982 				         path + 1, len - 1, td->td_ucred,
1983 					 cmode);
1984 				if (pn == NULL) {
1985 					error = ENOSPC;
1986 					mqueue_free(mq);
1987 				}
1988 			}
1989 		}
1990 
1991 		if (error == 0) {
1992 			pn->mn_data = mq;
1993 		}
1994 	} else {
1995 		if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
1996 			error = EEXIST;
1997 		} else {
1998 			accmode_t accmode = 0;
1999 
2000 			if (flags & FREAD)
2001 				accmode |= VREAD;
2002 			if (flags & FWRITE)
2003 				accmode |= VWRITE;
2004 			error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
2005 				    pn->mn_gid, accmode, td->td_ucred, NULL);
2006 		}
2007 	}
2008 
2009 	if (error) {
2010 		sx_xunlock(&mqfs_data.mi_lock);
2011 		fdclose(fdp, fp, fd, td);
2012 		fdrop(fp, td);
2013 		return (error);
2014 	}
2015 
2016 	mqnode_addref(pn);
2017 	sx_xunlock(&mqfs_data.mi_lock);
2018 
2019 	finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2020 	    &mqueueops);
2021 
2022 	FILEDESC_XLOCK(fdp);
2023 	if (fdp->fd_ofiles[fd] == fp)
2024 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
2025 	FILEDESC_XUNLOCK(fdp);
2026 	td->td_retval[0] = fd;
2027 	fdrop(fp, td);
2028 	return (0);
2029 }
2030 
2031 /*
2032  * Syscall to open a message queue.
2033  */
2034 int
2035 kmq_open(struct thread *td, struct kmq_open_args *uap)
2036 {
2037 	struct mq_attr attr;
2038 	int flags, error;
2039 
2040 	if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2041 		return (EINVAL);
2042 	flags = FFLAGS(uap->flags);
2043 	if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2044 		error = copyin(uap->attr, &attr, sizeof(attr));
2045 		if (error)
2046 			return (error);
2047 	}
2048 	return (kern_kmq_open(td, uap->path, flags, uap->mode,
2049 	    uap->attr != NULL ? &attr : NULL));
2050 }
2051 
2052 /*
2053  * Syscall to unlink a message queue.
2054  */
2055 int
2056 kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2057 {
2058 	char path[MQFS_NAMELEN+1];
2059 	struct mqfs_node *pn;
2060 	int error, len;
2061 
2062 	error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2063         if (error)
2064 		return (error);
2065 
2066 	len = strlen(path);
2067 	if (len < 2  || path[0] != '/' || index(path + 1, '/') != NULL)
2068 		return (EINVAL);
2069 
2070 	sx_xlock(&mqfs_data.mi_lock);
2071 	pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2072 	if (pn != NULL)
2073 		error = do_unlink(pn, td->td_ucred);
2074 	else
2075 		error = ENOENT;
2076 	sx_xunlock(&mqfs_data.mi_lock);
2077 	return (error);
2078 }
2079 
2080 typedef int (*_fgetf)(struct thread *, int, struct file **);
2081 
2082 /*
2083  * Get message queue by giving file slot
2084  */
2085 static int
2086 _getmq(struct thread *td, int fd, _fgetf func,
2087        struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2088 {
2089 	struct mqfs_node *pn;
2090 	int error;
2091 
2092 	error = func(td, fd, fpp);
2093 	if (error)
2094 		return (error);
2095 	if (&mqueueops != (*fpp)->f_ops) {
2096 		fdrop(*fpp, td);
2097 		return (EBADF);
2098 	}
2099 	pn = (*fpp)->f_data;
2100 	if (ppn)
2101 		*ppn = pn;
2102 	if (pmq)
2103 		*pmq = pn->mn_data;
2104 	return (0);
2105 }
2106 
2107 static __inline int
2108 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2109 	struct mqueue **pmq)
2110 {
2111 	return _getmq(td, fd, fget, fpp, ppn, pmq);
2112 }
2113 
2114 static __inline int
2115 getmq_read(struct thread *td, int fd, struct file **fpp,
2116 	 struct mqfs_node **ppn, struct mqueue **pmq)
2117 {
2118 	return _getmq(td, fd, fget_read, fpp, ppn, pmq);
2119 }
2120 
2121 static __inline int
2122 getmq_write(struct thread *td, int fd, struct file **fpp,
2123 	struct mqfs_node **ppn, struct mqueue **pmq)
2124 {
2125 	return _getmq(td, fd, fget_write, fpp, ppn, pmq);
2126 }
2127 
2128 static int
2129 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2130     struct mq_attr *oattr)
2131 {
2132 	struct mqueue *mq;
2133 	struct file *fp;
2134 	u_int oflag, flag;
2135 	int error;
2136 
2137 	if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2138 		return (EINVAL);
2139 	error = getmq(td, mqd, &fp, NULL, &mq);
2140 	if (error)
2141 		return (error);
2142 	oattr->mq_maxmsg  = mq->mq_maxmsg;
2143 	oattr->mq_msgsize = mq->mq_msgsize;
2144 	oattr->mq_curmsgs = mq->mq_curmsgs;
2145 	if (attr != NULL) {
2146 		do {
2147 			oflag = flag = fp->f_flag;
2148 			flag &= ~O_NONBLOCK;
2149 			flag |= (attr->mq_flags & O_NONBLOCK);
2150 		} while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2151 	} else
2152 		oflag = fp->f_flag;
2153 	oattr->mq_flags = (O_NONBLOCK & oflag);
2154 	fdrop(fp, td);
2155 	return (error);
2156 }
2157 
2158 int
2159 kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2160 {
2161 	struct mq_attr attr, oattr;
2162 	int error;
2163 
2164 	if (uap->attr != NULL) {
2165 		error = copyin(uap->attr, &attr, sizeof(attr));
2166 		if (error != 0)
2167 			return (error);
2168 	}
2169 	error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2170 	    &oattr);
2171 	if (error != 0)
2172 		return (error);
2173 	if (uap->oattr != NULL)
2174 		error = copyout(&oattr, uap->oattr, sizeof(oattr));
2175 	return (error);
2176 }
2177 
2178 int
2179 kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2180 {
2181 	struct mqueue *mq;
2182 	struct file *fp;
2183 	struct timespec *abs_timeout, ets;
2184 	int error;
2185 	int waitok;
2186 
2187 	error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2188 	if (error)
2189 		return (error);
2190 	if (uap->abs_timeout != NULL) {
2191 		error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2192 		if (error != 0)
2193 			return (error);
2194 		abs_timeout = &ets;
2195 	} else
2196 		abs_timeout = NULL;
2197 	waitok = !(fp->f_flag & O_NONBLOCK);
2198 	error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2199 		uap->msg_prio, waitok, abs_timeout);
2200 	fdrop(fp, td);
2201 	return (error);
2202 }
2203 
2204 int
2205 kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2206 {
2207 	struct mqueue *mq;
2208 	struct file *fp;
2209 	struct timespec *abs_timeout, ets;
2210 	int error, waitok;
2211 
2212 	error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2213 	if (error)
2214 		return (error);
2215 	if (uap->abs_timeout != NULL) {
2216 		error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2217 		if (error != 0)
2218 			return (error);
2219 		abs_timeout = &ets;
2220 	} else
2221 		abs_timeout = NULL;
2222 	waitok = !(fp->f_flag & O_NONBLOCK);
2223 	error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2224 		uap->msg_prio, waitok, abs_timeout);
2225 	fdrop(fp, td);
2226 	return (error);
2227 }
2228 
2229 int
2230 kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2231 {
2232 	struct sigevent ev;
2233 	struct filedesc *fdp;
2234 	struct proc *p;
2235 	struct mqueue *mq;
2236 	struct file *fp;
2237 	struct mqueue_notifier *nt, *newnt = NULL;
2238 	int error;
2239 
2240 	p = td->td_proc;
2241 	fdp = td->td_proc->p_fd;
2242 	if (uap->sigev) {
2243 		error = copyin(uap->sigev, &ev, sizeof(ev));
2244 		if (error)
2245 			return (error);
2246 		if (ev.sigev_notify != SIGEV_SIGNAL &&
2247 		    ev.sigev_notify != SIGEV_THREAD_ID &&
2248 		    ev.sigev_notify != SIGEV_NONE)
2249 			return (EINVAL);
2250 		if ((ev.sigev_notify == SIGEV_SIGNAL ||
2251 		     ev.sigev_notify == SIGEV_THREAD_ID) &&
2252 			!_SIG_VALID(ev.sigev_signo))
2253 			return (EINVAL);
2254 	}
2255 	error = getmq(td, uap->mqd, &fp, NULL, &mq);
2256 	if (error)
2257 		return (error);
2258 again:
2259 	FILEDESC_SLOCK(fdp);
2260 	if (fget_locked(fdp, uap->mqd) != fp) {
2261 		FILEDESC_SUNLOCK(fdp);
2262 		error = EBADF;
2263 		goto out;
2264 	}
2265 	mtx_lock(&mq->mq_mutex);
2266 	FILEDESC_SUNLOCK(fdp);
2267 	if (uap->sigev != NULL) {
2268 		if (mq->mq_notifier != NULL) {
2269 			error = EBUSY;
2270 		} else {
2271 			PROC_LOCK(p);
2272 			nt = notifier_search(p, uap->mqd);
2273 			if (nt == NULL) {
2274 				if (newnt == NULL) {
2275 					PROC_UNLOCK(p);
2276 					mtx_unlock(&mq->mq_mutex);
2277 					newnt = notifier_alloc();
2278 					goto again;
2279 				}
2280 			}
2281 
2282 			if (nt != NULL) {
2283 				sigqueue_take(&nt->nt_ksi);
2284 				if (newnt != NULL) {
2285 					notifier_free(newnt);
2286 					newnt = NULL;
2287 				}
2288 			} else {
2289 				nt = newnt;
2290 				newnt = NULL;
2291 				ksiginfo_init(&nt->nt_ksi);
2292 				nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2293 				nt->nt_ksi.ksi_code = SI_MESGQ;
2294 				nt->nt_proc = p;
2295 				nt->nt_ksi.ksi_mqd = uap->mqd;
2296 				notifier_insert(p, nt);
2297 			}
2298 			nt->nt_sigev = ev;
2299 			mq->mq_notifier = nt;
2300 			PROC_UNLOCK(p);
2301 			/*
2302 			 * if there is no receivers and message queue
2303 			 * is not empty, we should send notification
2304 			 * as soon as possible.
2305 			 */
2306 			if (mq->mq_receivers == 0 &&
2307 			    !TAILQ_EMPTY(&mq->mq_msgq))
2308 				mqueue_send_notification(mq);
2309 		}
2310 	} else {
2311 		notifier_remove(p, mq, uap->mqd);
2312 	}
2313 	mtx_unlock(&mq->mq_mutex);
2314 
2315 out:
2316 	fdrop(fp, td);
2317 	if (newnt != NULL)
2318 		notifier_free(newnt);
2319 	return (error);
2320 }
2321 
2322 static void
2323 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2324 {
2325 	struct filedesc *fdp;
2326 	struct mqueue *mq;
2327 
2328 	fdp = td->td_proc->p_fd;
2329 	FILEDESC_LOCK_ASSERT(fdp);
2330 
2331 	if (fp->f_ops == &mqueueops) {
2332 		mq = FPTOMQ(fp);
2333 		mtx_lock(&mq->mq_mutex);
2334 		notifier_remove(td->td_proc, mq, fd);
2335 
2336 		/* have to wakeup thread in same process */
2337 		if (mq->mq_flags & MQ_RSEL) {
2338 			mq->mq_flags &= ~MQ_RSEL;
2339 			selwakeup(&mq->mq_rsel);
2340 		}
2341 		if (mq->mq_flags & MQ_WSEL) {
2342 			mq->mq_flags &= ~MQ_WSEL;
2343 			selwakeup(&mq->mq_wsel);
2344 		}
2345 		mtx_unlock(&mq->mq_mutex);
2346 	}
2347 }
2348 
2349 static void
2350 mq_proc_exit(void *arg __unused, struct proc *p)
2351 {
2352 	struct filedesc *fdp;
2353 	struct file *fp;
2354 	struct mqueue *mq;
2355 	int i;
2356 
2357 	fdp = p->p_fd;
2358 	FILEDESC_SLOCK(fdp);
2359 	for (i = 0; i < fdp->fd_nfiles; ++i) {
2360 		fp = fget_locked(fdp, i);
2361 		if (fp != NULL && fp->f_ops == &mqueueops) {
2362 			mq = FPTOMQ(fp);
2363 			mtx_lock(&mq->mq_mutex);
2364 			notifier_remove(p, FPTOMQ(fp), i);
2365 			mtx_unlock(&mq->mq_mutex);
2366 		}
2367 	}
2368 	FILEDESC_SUNLOCK(fdp);
2369 	KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2370 }
2371 
2372 static int
2373 mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2374 	int flags, struct thread *td)
2375 {
2376 	return (EOPNOTSUPP);
2377 }
2378 
2379 static int
2380 mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
2381 	int flags, struct thread *td)
2382 {
2383 	return (EOPNOTSUPP);
2384 }
2385 
2386 static int
2387 mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
2388     struct thread *td)
2389 {
2390 
2391 	return (EINVAL);
2392 }
2393 
2394 static int
2395 mqf_ioctl(struct file *fp, u_long cmd, void *data,
2396 	struct ucred *active_cred, struct thread *td)
2397 {
2398 	return (ENOTTY);
2399 }
2400 
2401 static int
2402 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2403 	struct thread *td)
2404 {
2405 	struct mqueue *mq = FPTOMQ(fp);
2406 	int revents = 0;
2407 
2408 	mtx_lock(&mq->mq_mutex);
2409 	if (events & (POLLIN | POLLRDNORM)) {
2410 		if (mq->mq_curmsgs) {
2411 			revents |= events & (POLLIN | POLLRDNORM);
2412 		} else {
2413 			mq->mq_flags |= MQ_RSEL;
2414 			selrecord(td, &mq->mq_rsel);
2415  		}
2416 	}
2417 	if (events & POLLOUT) {
2418 		if (mq->mq_curmsgs < mq->mq_maxmsg)
2419 			revents |= POLLOUT;
2420 		else {
2421 			mq->mq_flags |= MQ_WSEL;
2422 			selrecord(td, &mq->mq_wsel);
2423 		}
2424 	}
2425 	mtx_unlock(&mq->mq_mutex);
2426 	return (revents);
2427 }
2428 
2429 static int
2430 mqf_close(struct file *fp, struct thread *td)
2431 {
2432 	struct mqfs_node *pn;
2433 
2434 	fp->f_ops = &badfileops;
2435 	pn = fp->f_data;
2436 	fp->f_data = NULL;
2437 	sx_xlock(&mqfs_data.mi_lock);
2438 	mqnode_release(pn);
2439 	sx_xunlock(&mqfs_data.mi_lock);
2440 	return (0);
2441 }
2442 
2443 static int
2444 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2445 	struct thread *td)
2446 {
2447 	struct mqfs_node *pn = fp->f_data;
2448 
2449 	bzero(st, sizeof *st);
2450 	st->st_atim = pn->mn_atime;
2451 	st->st_mtim = pn->mn_mtime;
2452 	st->st_ctim = pn->mn_ctime;
2453 	st->st_birthtim = pn->mn_birth;
2454 	st->st_uid = pn->mn_uid;
2455 	st->st_gid = pn->mn_gid;
2456 	st->st_mode = S_IFIFO | pn->mn_mode;
2457 	return (0);
2458 }
2459 
2460 static int
2461 mqf_kqfilter(struct file *fp, struct knote *kn)
2462 {
2463 	struct mqueue *mq = FPTOMQ(fp);
2464 	int error = 0;
2465 
2466 	if (kn->kn_filter == EVFILT_READ) {
2467 		kn->kn_fop = &mq_rfiltops;
2468 		knlist_add(&mq->mq_rsel.si_note, kn, 0);
2469 	} else if (kn->kn_filter == EVFILT_WRITE) {
2470 		kn->kn_fop = &mq_wfiltops;
2471 		knlist_add(&mq->mq_wsel.si_note, kn, 0);
2472 	} else
2473 		error = EINVAL;
2474 	return (error);
2475 }
2476 
2477 static void
2478 filt_mqdetach(struct knote *kn)
2479 {
2480 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2481 
2482 	if (kn->kn_filter == EVFILT_READ)
2483 		knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2484 	else if (kn->kn_filter == EVFILT_WRITE)
2485 		knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2486 	else
2487 		panic("filt_mqdetach");
2488 }
2489 
2490 static int
2491 filt_mqread(struct knote *kn, long hint)
2492 {
2493 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2494 
2495 	mtx_assert(&mq->mq_mutex, MA_OWNED);
2496 	return (mq->mq_curmsgs != 0);
2497 }
2498 
2499 static int
2500 filt_mqwrite(struct knote *kn, long hint)
2501 {
2502 	struct mqueue *mq = FPTOMQ(kn->kn_fp);
2503 
2504 	mtx_assert(&mq->mq_mutex, MA_OWNED);
2505 	return (mq->mq_curmsgs < mq->mq_maxmsg);
2506 }
2507 
2508 static struct fileops mqueueops = {
2509 	.fo_read		= mqf_read,
2510 	.fo_write		= mqf_write,
2511 	.fo_truncate		= mqf_truncate,
2512 	.fo_ioctl		= mqf_ioctl,
2513 	.fo_poll		= mqf_poll,
2514 	.fo_kqfilter		= mqf_kqfilter,
2515 	.fo_stat		= mqf_stat,
2516 	.fo_close		= mqf_close
2517 };
2518 
2519 static struct vop_vector mqfs_vnodeops = {
2520 	.vop_default 		= &default_vnodeops,
2521 	.vop_access		= mqfs_access,
2522 	.vop_cachedlookup	= mqfs_lookup,
2523 	.vop_lookup		= vfs_cache_lookup,
2524 	.vop_reclaim		= mqfs_reclaim,
2525 	.vop_create		= mqfs_create,
2526 	.vop_remove		= mqfs_remove,
2527 	.vop_inactive		= mqfs_inactive,
2528 	.vop_open		= mqfs_open,
2529 	.vop_close		= mqfs_close,
2530 	.vop_getattr		= mqfs_getattr,
2531 	.vop_setattr		= mqfs_setattr,
2532 	.vop_read		= mqfs_read,
2533 	.vop_write		= VOP_EOPNOTSUPP,
2534 	.vop_readdir		= mqfs_readdir,
2535 	.vop_mkdir		= VOP_EOPNOTSUPP,
2536 	.vop_rmdir		= VOP_EOPNOTSUPP
2537 };
2538 
2539 static struct vfsops mqfs_vfsops = {
2540 	.vfs_init 		= mqfs_init,
2541 	.vfs_uninit		= mqfs_uninit,
2542 	.vfs_mount		= mqfs_mount,
2543 	.vfs_unmount		= mqfs_unmount,
2544 	.vfs_root		= mqfs_root,
2545 	.vfs_statfs		= mqfs_statfs,
2546 };
2547 
2548 static struct vfsconf mqueuefs_vfsconf = {
2549 	.vfc_version = VFS_VERSION,
2550 	.vfc_name = "mqueuefs",
2551 	.vfc_vfsops = &mqfs_vfsops,
2552 	.vfc_typenum = -1,
2553 	.vfc_flags = VFCF_SYNTHETIC
2554 };
2555 
2556 static struct syscall_helper_data mq_syscalls[] = {
2557 	SYSCALL_INIT_HELPER(kmq_open),
2558 	SYSCALL_INIT_HELPER(kmq_setattr),
2559 	SYSCALL_INIT_HELPER(kmq_timedsend),
2560 	SYSCALL_INIT_HELPER(kmq_timedreceive),
2561 	SYSCALL_INIT_HELPER(kmq_notify),
2562 	SYSCALL_INIT_HELPER(kmq_unlink),
2563 	SYSCALL_INIT_LAST
2564 };
2565 
2566 #ifdef COMPAT_FREEBSD32
2567 #include <compat/freebsd32/freebsd32.h>
2568 #include <compat/freebsd32/freebsd32_proto.h>
2569 #include <compat/freebsd32/freebsd32_syscall.h>
2570 #include <compat/freebsd32/freebsd32_util.h>
2571 
2572 static void
2573 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2574 {
2575 
2576 	to->mq_flags = from->mq_flags;
2577 	to->mq_maxmsg = from->mq_maxmsg;
2578 	to->mq_msgsize = from->mq_msgsize;
2579 	to->mq_curmsgs = from->mq_curmsgs;
2580 }
2581 
2582 static void
2583 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2584 {
2585 
2586 	to->mq_flags = from->mq_flags;
2587 	to->mq_maxmsg = from->mq_maxmsg;
2588 	to->mq_msgsize = from->mq_msgsize;
2589 	to->mq_curmsgs = from->mq_curmsgs;
2590 }
2591 
2592 int
2593 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2594 {
2595 	struct mq_attr attr;
2596 	struct mq_attr32 attr32;
2597 	int flags, error;
2598 
2599 	if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2600 		return (EINVAL);
2601 	flags = FFLAGS(uap->flags);
2602 	if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2603 		error = copyin(uap->attr, &attr32, sizeof(attr32));
2604 		if (error)
2605 			return (error);
2606 		mq_attr_from32(&attr32, &attr);
2607 	}
2608 	return (kern_kmq_open(td, uap->path, flags, uap->mode,
2609 	    uap->attr != NULL ? &attr : NULL));
2610 }
2611 
2612 int
2613 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2614 {
2615 	struct mq_attr attr, oattr;
2616 	struct mq_attr32 attr32, oattr32;
2617 	int error;
2618 
2619 	if (uap->attr != NULL) {
2620 		error = copyin(uap->attr, &attr32, sizeof(attr32));
2621 		if (error != 0)
2622 			return (error);
2623 		mq_attr_from32(&attr32, &attr);
2624 	}
2625 	error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2626 	    &oattr);
2627 	if (error != 0)
2628 		return (error);
2629 	if (uap->oattr != NULL) {
2630 		mq_attr_to32(&oattr, &oattr32);
2631 		error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2632 	}
2633 	return (error);
2634 }
2635 
2636 int
2637 freebsd32_kmq_timedsend(struct thread *td,
2638     struct freebsd32_kmq_timedsend_args *uap)
2639 {
2640 	struct mqueue *mq;
2641 	struct file *fp;
2642 	struct timespec32 ets32;
2643 	struct timespec *abs_timeout, ets;
2644 	int error;
2645 	int waitok;
2646 
2647 	error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2648 	if (error)
2649 		return (error);
2650 	if (uap->abs_timeout != NULL) {
2651 		error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2652 		if (error != 0)
2653 			return (error);
2654 		CP(ets32, ets, tv_sec);
2655 		CP(ets32, ets, tv_nsec);
2656 		abs_timeout = &ets;
2657 	} else
2658 		abs_timeout = NULL;
2659 	waitok = !(fp->f_flag & O_NONBLOCK);
2660 	error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2661 		uap->msg_prio, waitok, abs_timeout);
2662 	fdrop(fp, td);
2663 	return (error);
2664 }
2665 
2666 int
2667 freebsd32_kmq_timedreceive(struct thread *td,
2668     struct freebsd32_kmq_timedreceive_args *uap)
2669 {
2670 	struct mqueue *mq;
2671 	struct file *fp;
2672 	struct timespec32 ets32;
2673 	struct timespec *abs_timeout, ets;
2674 	int error, waitok;
2675 
2676 	error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2677 	if (error)
2678 		return (error);
2679 	if (uap->abs_timeout != NULL) {
2680 		error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2681 		if (error != 0)
2682 			return (error);
2683 		CP(ets32, ets, tv_sec);
2684 		CP(ets32, ets, tv_nsec);
2685 		abs_timeout = &ets;
2686 	} else
2687 		abs_timeout = NULL;
2688 	waitok = !(fp->f_flag & O_NONBLOCK);
2689 	error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2690 		uap->msg_prio, waitok, abs_timeout);
2691 	fdrop(fp, td);
2692 	return (error);
2693 }
2694 
2695 static struct syscall_helper_data mq32_syscalls[] = {
2696 	SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2697 	SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
2698 	SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
2699 	SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
2700 	SYSCALL32_INIT_HELPER(kmq_notify),
2701 	SYSCALL32_INIT_HELPER(kmq_unlink),
2702 	SYSCALL_INIT_LAST
2703 };
2704 #endif
2705 
2706 static int
2707 mqinit(void)
2708 {
2709 	int error;
2710 
2711 	error = syscall_helper_register(mq_syscalls);
2712 	if (error != 0)
2713 		return (error);
2714 #ifdef COMPAT_FREEBSD32
2715 	error = syscall32_helper_register(mq32_syscalls);
2716 	if (error != 0)
2717 		return (error);
2718 #endif
2719 	return (0);
2720 }
2721 
2722 static int
2723 mqunload(void)
2724 {
2725 
2726 #ifdef COMPAT_FREEBSD32
2727 	syscall32_helper_unregister(mq32_syscalls);
2728 #endif
2729 	syscall_helper_unregister(mq_syscalls);
2730 	return (0);
2731 }
2732 
2733 static int
2734 mq_modload(struct module *module, int cmd, void *arg)
2735 {
2736 	int error = 0;
2737 
2738 	error = vfs_modevent(module, cmd, arg);
2739 	if (error != 0)
2740 		return (error);
2741 
2742 	switch (cmd) {
2743 	case MOD_LOAD:
2744 		error = mqinit();
2745 		if (error != 0)
2746 			mqunload();
2747 		break;
2748 	case MOD_UNLOAD:
2749 		error = mqunload();
2750 		break;
2751 	default:
2752 		break;
2753 	}
2754 	return (error);
2755 }
2756 
2757 static moduledata_t mqueuefs_mod = {
2758 	"mqueuefs",
2759 	mq_modload,
2760 	&mqueuefs_vfsconf
2761 };
2762 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2763 MODULE_VERSION(mqueuefs, 1);
2764