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