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