2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * POSIX message queue implementation.
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.
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.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_compat.h"
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/limits.h>
55 #include <sys/dirent.h>
56 #include <sys/event.h>
57 #include <sys/eventhandler.h>
58 #include <sys/fcntl.h>
60 #include <sys/filedesc.h>
62 #include <sys/malloc.h>
63 #include <sys/module.h>
64 #include <sys/mount.h>
65 #include <sys/mqueue.h>
66 #include <sys/mutex.h>
67 #include <sys/namei.h>
68 #include <sys/posix4.h>
72 #include <sys/queue.h>
73 #include <sys/sysproto.h>
75 #include <sys/syscall.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysent.h>
79 #include <sys/sysctl.h>
80 #include <sys/taskqueue.h>
81 #include <sys/unistd.h>
82 #include <sys/vnode.h>
83 #include <machine/atomic.h>
86 * Limits and constants
88 #define MQFS_NAMELEN NAME_MAX
89 #define MQFS_DELEN (8 + MQFS_NAMELEN)
105 * mqfs_info: describes a mqfs instance
109 struct mqfs_node *mi_root;
110 struct unrhdr *mi_unrhdr;
114 LIST_ENTRY(mqfs_vdata) mv_link;
115 struct mqfs_node *mv_node;
116 struct vnode *mv_vnode;
121 * mqfs_node: describes a node (file or directory) within a mqfs
124 char mn_name[MQFS_NAMELEN+1];
125 struct mqfs_info *mn_info;
126 struct mqfs_node *mn_parent;
127 LIST_HEAD(,mqfs_node) mn_children;
128 LIST_ENTRY(mqfs_node) mn_sibling;
129 LIST_HEAD(,mqfs_vdata) mn_vnodes;
135 struct timespec mn_birth;
136 struct timespec mn_ctime;
137 struct timespec mn_atime;
138 struct timespec mn_mtime;
144 #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
145 #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
146 #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
147 #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
148 (fp)->f_data)->mn_data))
150 TAILQ_HEAD(msgq, mqueue_msg);
154 struct mqueue_notifier {
155 LIST_ENTRY(mqueue_notifier) nt_link;
156 struct sigevent nt_sigev;
158 struct proc *nt_proc;
171 struct selinfo mq_rsel;
172 struct selinfo mq_wsel;
173 struct mqueue_notifier *mq_notifier;
180 TAILQ_ENTRY(mqueue_msg) msg_link;
181 unsigned int msg_prio;
182 unsigned int msg_size;
183 /* following real data... */
186 SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
187 "POSIX real time message queue");
189 static int default_maxmsg = 10;
190 static int default_msgsize = 1024;
192 static int maxmsg = 100;
193 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
194 &maxmsg, 0, "Default maximum messages in queue");
195 static int maxmsgsize = 16384;
196 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
197 &maxmsgsize, 0, "Default maximum message size");
198 static int maxmq = 100;
199 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
200 &maxmq, 0, "maximum message queues");
201 static int curmq = 0;
202 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
203 &curmq, 0, "current message queue number");
204 static int unloadable = 0;
205 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
207 static eventhandler_tag exit_tag;
209 /* Only one instance per-system */
210 static struct mqfs_info mqfs_data;
211 static uma_zone_t mqnode_zone;
212 static uma_zone_t mqueue_zone;
213 static uma_zone_t mvdata_zone;
214 static uma_zone_t mqnoti_zone;
215 static struct vop_vector mqfs_vnodeops;
216 static struct fileops mqueueops;
219 * Directory structure construction and manipulation
222 static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
223 const char *name, int namelen, struct ucred *cred, int mode);
224 static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
225 const char *name, int namelen, struct ucred *cred, int mode);
228 static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
229 const char *name, int namelen, struct ucred *cred, int mode);
230 static int mqfs_destroy(struct mqfs_node *mn);
231 static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
232 static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
233 static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
236 * Message queue construction and maniplation
238 static struct mqueue *mqueue_alloc(const struct mq_attr *attr);
239 static void mqueue_free(struct mqueue *mq);
240 static int mqueue_send(struct mqueue *mq, const char *msg_ptr,
241 size_t msg_len, unsigned msg_prio, int waitok,
242 const struct timespec *abs_timeout);
243 static int mqueue_receive(struct mqueue *mq, char *msg_ptr,
244 size_t msg_len, unsigned *msg_prio, int waitok,
245 const struct timespec *abs_timeout);
246 static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
248 static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
250 static void mqueue_send_notification(struct mqueue *mq);
251 static void mqueue_fdclose(struct thread *td, int fd, struct file *fp);
252 static void mq_proc_exit(void *arg, struct proc *p);
257 static void filt_mqdetach(struct knote *kn);
258 static int filt_mqread(struct knote *kn, long hint);
259 static int filt_mqwrite(struct knote *kn, long hint);
261 struct filterops mq_rfiltops =
262 { 1, NULL, filt_mqdetach, filt_mqread };
263 struct filterops mq_wfiltops =
264 { 1, NULL, filt_mqdetach, filt_mqwrite };
267 * Initialize fileno bitmap
270 mqfs_fileno_init(struct mqfs_info *mi)
274 up = new_unrhdr(1, INT_MAX, NULL);
279 * Tear down fileno bitmap
282 mqfs_fileno_uninit(struct mqfs_info *mi)
287 mi->mi_unrhdr = NULL;
292 * Allocate a file number
295 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
297 /* make sure our parent has a file number */
298 if (mn->mn_parent && !mn->mn_parent->mn_fileno)
299 mqfs_fileno_alloc(mi, mn->mn_parent);
301 switch (mn->mn_type) {
305 case mqfstype_symlink:
306 mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
309 KASSERT(mn->mn_parent != NULL,
310 ("mqfstype_this node has no parent"));
311 mn->mn_fileno = mn->mn_parent->mn_fileno;
313 case mqfstype_parent:
314 KASSERT(mn->mn_parent != NULL,
315 ("mqfstype_parent node has no parent"));
316 if (mn->mn_parent == mi->mi_root) {
317 mn->mn_fileno = mn->mn_parent->mn_fileno;
320 KASSERT(mn->mn_parent->mn_parent != NULL,
321 ("mqfstype_parent node has no grandparent"));
322 mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
326 ("mqfs_fileno_alloc() called for unknown type node: %d",
333 * Release a file number
336 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
338 switch (mn->mn_type) {
342 case mqfstype_symlink:
343 free_unr(mi->mi_unrhdr, mn->mn_fileno);
346 case mqfstype_parent:
347 /* ignore these, as they don't "own" their file number */
351 ("mqfs_fileno_free() called for unknown type node: %d",
357 static __inline struct mqfs_node *
360 return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
364 mqnode_free(struct mqfs_node *node)
366 uma_zfree(mqnode_zone, node);
370 mqnode_addref(struct mqfs_node *node)
372 atomic_fetchadd_int(&node->mn_refcount, 1);
376 mqnode_release(struct mqfs_node *node)
378 struct mqfs_info *mqfs;
381 mqfs = node->mn_info;
382 old = atomic_fetchadd_int(&node->mn_refcount, -1);
383 if (node->mn_type == mqfstype_dir ||
384 node->mn_type == mqfstype_root)
385 exp = 3; /* include . and .. */
389 int locked = sx_xlocked(&mqfs->mi_lock);
391 sx_xlock(&mqfs->mi_lock);
394 sx_xunlock(&mqfs->mi_lock);
399 * Add a node to a directory
402 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
404 KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
405 KASSERT(parent->mn_info != NULL,
406 ("%s(): parent has no mn_info", __func__));
407 KASSERT(parent->mn_type == mqfstype_dir ||
408 parent->mn_type == mqfstype_root,
409 ("%s(): parent is not a directory", __func__));
411 node->mn_info = parent->mn_info;
412 node->mn_parent = parent;
413 LIST_INIT(&node->mn_children);
414 LIST_INIT(&node->mn_vnodes);
415 LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
416 mqnode_addref(parent);
420 static struct mqfs_node *
421 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
424 struct mqfs_node *node;
426 node = mqnode_alloc();
427 strncpy(node->mn_name, name, namelen);
428 node->mn_type = nodetype;
429 node->mn_refcount = 1;
430 vfs_timestamp(&node->mn_birth);
431 node->mn_ctime = node->mn_atime = node->mn_mtime
433 node->mn_uid = cred->cr_uid;
434 node->mn_gid = cred->cr_gid;
435 node->mn_mode = mode;
442 static struct mqfs_node *
443 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
444 struct ucred *cred, int mode)
446 struct mqfs_node *node;
448 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
449 if (mqfs_add_node(parent, node) != 0) {
457 * Add . and .. to a directory
460 mqfs_fixup_dir(struct mqfs_node *parent)
462 struct mqfs_node *dir;
464 dir = mqnode_alloc();
465 dir->mn_name[0] = '.';
466 dir->mn_type = mqfstype_this;
467 dir->mn_refcount = 1;
468 if (mqfs_add_node(parent, dir) != 0) {
473 dir = mqnode_alloc();
474 dir->mn_name[0] = dir->mn_name[1] = '.';
475 dir->mn_type = mqfstype_parent;
476 dir->mn_refcount = 1;
478 if (mqfs_add_node(parent, dir) != 0) {
491 static struct mqfs_node *
492 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
493 struct ucred *cred, int mode)
495 struct mqfs_node *node;
497 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
498 if (mqfs_add_node(parent, node) != 0) {
503 if (mqfs_fixup_dir(node) != 0) {
513 static struct mqfs_node *
514 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
515 struct ucred *cred, int mode)
517 struct mqfs_node *node;
519 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
520 if (mqfs_add_node(parent, node) != 0) {
530 * Destroy a node or a tree of nodes
533 mqfs_destroy(struct mqfs_node *node)
535 struct mqfs_node *parent;
537 KASSERT(node != NULL,
538 ("%s(): node is NULL", __func__));
539 KASSERT(node->mn_info != NULL,
540 ("%s(): node has no mn_info", __func__));
542 /* destroy children */
543 if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
544 while (! LIST_EMPTY(&node->mn_children))
545 mqfs_destroy(LIST_FIRST(&node->mn_children));
547 /* unlink from parent */
548 if ((parent = node->mn_parent) != NULL) {
549 KASSERT(parent->mn_info == node->mn_info,
550 ("%s(): parent has different mn_info", __func__));
551 LIST_REMOVE(node, mn_sibling);
554 if (node->mn_fileno != 0)
555 mqfs_fileno_free(node->mn_info, node);
556 if (node->mn_data != NULL)
557 mqueue_free(node->mn_data);
563 * Mount a mqfs instance
566 mqfs_mount(struct mount *mp)
570 if (mp->mnt_flag & MNT_UPDATE)
573 mp->mnt_data = &mqfs_data;
575 mp->mnt_flag |= MNT_LOCAL;
576 mp->mnt_kern_flag |= MNTK_MPSAFE;
581 vfs_mountedfrom(mp, "mqueue");
582 sbp->f_bsize = PAGE_SIZE;
583 sbp->f_iosize = PAGE_SIZE;
593 * Unmount a mqfs instance
596 mqfs_unmount(struct mount *mp, int mntflags)
600 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
606 * Return a root vnode
609 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
611 struct mqfs_info *mqfs;
614 mqfs = VFSTOMQFS(mp);
615 ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
620 * Return filesystem stats
623 mqfs_statfs(struct mount *mp, struct statfs *sbp)
625 /* XXX update statistics */
630 * Initialize a mqfs instance
633 mqfs_init(struct vfsconf *vfc)
635 struct mqfs_node *root;
636 struct mqfs_info *mi;
638 mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
639 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
640 mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
641 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
642 mvdata_zone = uma_zcreate("mvdata",
643 sizeof(struct mqfs_vdata), NULL, NULL, NULL,
644 NULL, UMA_ALIGN_PTR, 0);
645 mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
646 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
648 sx_init(&mi->mi_lock, "mqfs lock");
649 /* set up the root diretory */
650 root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
653 LIST_INIT(&root->mn_children);
654 LIST_INIT(&root->mn_vnodes);
656 mqfs_fileno_init(mi);
657 mqfs_fileno_alloc(mi, root);
658 mqfs_fixup_dir(root);
659 exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
660 EVENTHANDLER_PRI_ANY);
661 mq_fdclose = mqueue_fdclose;
662 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
667 * Destroy a mqfs instance
670 mqfs_uninit(struct vfsconf *vfc)
672 struct mqfs_info *mi;
676 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
678 mqfs_destroy(mi->mi_root);
680 mqfs_fileno_uninit(mi);
681 sx_destroy(&mi->mi_lock);
682 uma_zdestroy(mqnode_zone);
683 uma_zdestroy(mqueue_zone);
684 uma_zdestroy(mvdata_zone);
685 uma_zdestroy(mqnoti_zone);
693 do_recycle(void *context, int pending __unused)
695 struct vnode *vp = (struct vnode *)context;
697 vrecycle(vp, curthread);
705 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
707 struct mqfs_vdata *vd;
708 struct mqfs_info *mqfs;
709 struct vnode *newvpp;
714 sx_xlock(&mqfs->mi_lock);
715 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
716 if (vd->mv_vnode->v_mount == mp) {
725 sx_xunlock(&mqfs->mi_lock);
726 error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
730 sx_xunlock(&mqfs->mi_lock);
732 error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
735 vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
736 error = insmntque(newvpp, mp);
740 sx_xlock(&mqfs->mi_lock);
742 * Check if it has already been allocated
743 * while we were blocked.
745 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
746 if (vd->mv_vnode->v_mount == mp) {
748 sx_xunlock(&mqfs->mi_lock);
758 vd = uma_zalloc(mvdata_zone, M_WAITOK);
762 TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
763 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
765 switch (pn->mn_type) {
767 (*vpp)->v_vflag = VV_ROOT;
771 case mqfstype_parent:
772 (*vpp)->v_type = VDIR;
775 (*vpp)->v_type = VREG;
777 case mqfstype_symlink:
778 (*vpp)->v_type = VLNK;
781 KASSERT(0, ("mqfs_allocf called for null node\n"));
783 panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
785 sx_xunlock(&mqfs->mi_lock);
790 * Search a directory entry
792 static struct mqfs_node *
793 mqfs_search(struct mqfs_node *pd, const char *name, int len)
795 struct mqfs_node *pn;
797 sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
798 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
799 if (strncmp(pn->mn_name, name, len) == 0 &&
800 pn->mn_name[len] == '\0')
807 * Look up a file or directory.
810 mqfs_lookupx(struct vop_cachedlookup_args *ap)
812 struct componentname *cnp;
813 struct vnode *dvp, **vpp;
814 struct mqfs_node *pd;
815 struct mqfs_node *pn;
816 struct mqfs_info *mqfs;
817 int nameiop, flags, error, namelen;
824 pname = cnp->cn_nameptr;
825 namelen = cnp->cn_namelen;
827 flags = cnp->cn_flags;
828 nameiop = cnp->cn_nameiop;
834 if (dvp->v_type != VDIR)
837 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
841 /* shortcut: check if the name is too long */
842 if (cnp->cn_namelen >= MQFS_NAMELEN)
846 if (namelen == 1 && pname[0] == '.') {
847 if ((flags & ISLASTCN) && nameiop != LOOKUP)
856 if (cnp->cn_flags & ISDOTDOT) {
857 if (dvp->v_vflag & VV_ROOT)
859 if ((flags & ISLASTCN) && nameiop != LOOKUP)
862 KASSERT(pd->mn_parent, ("non-root directory has no parent"));
864 error = mqfs_allocv(dvp->v_mount, vpp, pn);
865 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
870 sx_xlock(&mqfs->mi_lock);
871 pn = mqfs_search(pd, pname, namelen);
874 sx_xunlock(&mqfs->mi_lock);
879 if (nameiop == DELETE && (flags & ISLASTCN)) {
880 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
894 error = mqfs_allocv(dvp->v_mount, vpp, pn);
896 if (error == 0 && cnp->cn_flags & MAKEENTRY)
897 cache_enter(dvp, *vpp, cnp);
903 /* will create a new entry in the directory ? */
904 if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
905 && (flags & ISLASTCN)) {
906 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
909 cnp->cn_flags |= SAVENAME;
910 return (EJUSTRETURN);
916 struct vop_lookup_args {
917 struct vop_generic_args a_gen;
919 struct vnode **a_vpp;
920 struct componentname *a_cnp;
925 * vnode lookup operation
928 mqfs_lookup(struct vop_cachedlookup_args *ap)
932 rc = mqfs_lookupx(ap);
937 struct vop_create_args {
939 struct vnode **a_vpp;
940 struct componentname *a_cnp;
946 * vnode creation operation
949 mqfs_create(struct vop_create_args *ap)
951 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
952 struct componentname *cnp = ap->a_cnp;
953 struct mqfs_node *pd;
954 struct mqfs_node *pn;
958 pd = VTON(ap->a_dvp);
959 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
961 mq = mqueue_alloc(NULL);
964 sx_xlock(&mqfs->mi_lock);
965 if ((cnp->cn_flags & HASBUF) == 0)
966 panic("%s: no name", __func__);
967 pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
968 cnp->cn_cred, ap->a_vap->va_mode);
970 sx_xunlock(&mqfs->mi_lock);
974 sx_xunlock(&mqfs->mi_lock);
975 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
991 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
993 struct mqfs_node *parent;
994 struct mqfs_vdata *vd;
997 sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
999 if (ucred->cr_uid != pn->mn_uid &&
1000 (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1002 else if (!pn->mn_deleted) {
1003 parent = pn->mn_parent;
1004 pn->mn_parent = NULL;
1006 LIST_REMOVE(pn, mn_sibling);
1007 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1008 cache_purge(vd->mv_vnode);
1009 vhold(vd->mv_vnode);
1010 taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1013 mqnode_release(parent);
1020 struct vop_remove_args {
1021 struct vnode *a_dvp;
1023 struct componentname *a_cnp;
1028 * vnode removal operation
1031 mqfs_remove(struct vop_remove_args *ap)
1033 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1034 struct mqfs_node *pn;
1037 if (ap->a_vp->v_type == VDIR)
1039 pn = VTON(ap->a_vp);
1040 sx_xlock(&mqfs->mi_lock);
1041 error = do_unlink(pn, ap->a_cnp->cn_cred);
1042 sx_xunlock(&mqfs->mi_lock);
1047 struct vop_inactive_args {
1049 struct thread *a_td;
1054 mqfs_inactive(struct vop_inactive_args *ap)
1056 struct mqfs_node *pn = VTON(ap->a_vp);
1059 vrecycle(ap->a_vp, ap->a_td);
1064 struct vop_reclaim_args {
1065 struct vop_generic_args a_gen;
1067 struct thread *a_td;
1072 mqfs_reclaim(struct vop_reclaim_args *ap)
1074 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1075 struct vnode *vp = ap->a_vp;
1076 struct mqfs_node *pn;
1077 struct mqfs_vdata *vd;
1081 sx_xlock(&mqfs->mi_lock);
1083 LIST_REMOVE(vd, mv_link);
1084 uma_zfree(mvdata_zone, vd);
1086 sx_xunlock(&mqfs->mi_lock);
1091 struct vop_open_args {
1092 struct vop_generic_args a_gen;
1095 struct ucred *a_cred;
1096 struct thread *a_td;
1102 mqfs_open(struct vop_open_args *ap)
1108 struct vop_close_args {
1109 struct vop_generic_args a_gen;
1112 struct ucred *a_cred;
1113 struct thread *a_td;
1118 mqfs_close(struct vop_close_args *ap)
1124 struct vop_access_args {
1125 struct vop_generic_args a_gen;
1127 accmode_t a_accmode;
1128 struct ucred *a_cred;
1129 struct thread *a_td;
1134 * Verify permissions
1137 mqfs_access(struct vop_access_args *ap)
1139 struct vnode *vp = ap->a_vp;
1143 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1146 error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1147 vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1152 struct vop_getattr_args {
1153 struct vop_generic_args a_gen;
1155 struct vattr *a_vap;
1156 struct ucred *a_cred;
1161 * Get file attributes
1164 mqfs_getattr(struct vop_getattr_args *ap)
1166 struct vnode *vp = ap->a_vp;
1167 struct mqfs_node *pn = VTON(vp);
1168 struct vattr *vap = ap->a_vap;
1171 vap->va_type = vp->v_type;
1172 vap->va_mode = pn->mn_mode;
1174 vap->va_uid = pn->mn_uid;
1175 vap->va_gid = pn->mn_gid;
1176 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1177 vap->va_fileid = pn->mn_fileno;
1179 vap->va_blocksize = PAGE_SIZE;
1180 vap->va_bytes = vap->va_size = 0;
1181 vap->va_atime = pn->mn_atime;
1182 vap->va_mtime = pn->mn_mtime;
1183 vap->va_ctime = pn->mn_ctime;
1184 vap->va_birthtime = pn->mn_birth;
1187 vap->va_rdev = NODEV;
1189 vap->va_filerev = 0;
1194 struct vop_setattr_args {
1195 struct vop_generic_args a_gen;
1197 struct vattr *a_vap;
1198 struct ucred *a_cred;
1205 mqfs_setattr(struct vop_setattr_args *ap)
1207 struct mqfs_node *pn;
1218 if ((vap->va_type != VNON) ||
1219 (vap->va_nlink != VNOVAL) ||
1220 (vap->va_fsid != VNOVAL) ||
1221 (vap->va_fileid != VNOVAL) ||
1222 (vap->va_blocksize != VNOVAL) ||
1223 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1224 (vap->va_rdev != VNOVAL) ||
1225 ((int)vap->va_bytes != VNOVAL) ||
1226 (vap->va_gen != VNOVAL)) {
1233 if (vap->va_uid == (uid_t)VNOVAL)
1237 if (vap->va_gid == (gid_t)VNOVAL)
1242 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1244 * To modify the ownership of a file, must possess VADMIN
1247 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1251 * XXXRW: Why is there a privilege check here: shouldn't the
1252 * check in VOP_ACCESS() be enough? Also, are the group bits
1253 * below definitely right?
1255 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1256 (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1257 (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1264 if (vap->va_mode != (mode_t)VNOVAL) {
1265 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1266 (error = priv_check(td, PRIV_MQ_ADMIN)))
1268 pn->mn_mode = vap->va_mode;
1272 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1273 /* See the comment in ufs_vnops::ufs_setattr(). */
1274 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1275 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1276 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1278 if (vap->va_atime.tv_sec != VNOVAL) {
1279 pn->mn_atime = vap->va_atime;
1281 if (vap->va_mtime.tv_sec != VNOVAL) {
1282 pn->mn_mtime = vap->va_mtime;
1287 vfs_timestamp(&pn->mn_ctime);
1293 struct vop_read_args {
1294 struct vop_generic_args a_gen;
1298 struct ucred *a_cred;
1306 mqfs_read(struct vop_read_args *ap)
1309 struct vnode *vp = ap->a_vp;
1310 struct uio *uio = ap->a_uio;
1311 struct mqfs_node *pn;
1315 if (vp->v_type != VREG)
1320 snprintf(buf, sizeof(buf),
1321 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1326 buf[sizeof(buf)-1] = '\0';
1328 error = uiomove_frombuf(buf, len, uio);
1333 struct vop_readdir_args {
1334 struct vop_generic_args a_gen;
1337 struct ucred *a_cred;
1345 * Return directory entries.
1348 mqfs_readdir(struct vop_readdir_args *ap)
1351 struct mqfs_info *mi;
1352 struct mqfs_node *pd;
1353 struct mqfs_node *pn;
1354 struct dirent entry;
1356 int *tmp_ncookies = NULL;
1361 mi = VFSTOMQFS(vp->v_mount);
1365 if (vp->v_type != VDIR)
1368 if (uio->uio_offset < 0)
1371 if (ap->a_ncookies != NULL) {
1372 tmp_ncookies = ap->a_ncookies;
1373 *ap->a_ncookies = 0;
1374 ap->a_ncookies = NULL;
1380 sx_xlock(&mi->mi_lock);
1382 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1383 entry.d_reclen = sizeof(entry);
1385 mqfs_fileno_alloc(mi, pn);
1386 entry.d_fileno = pn->mn_fileno;
1387 for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1388 entry.d_name[i] = pn->mn_name[i];
1389 entry.d_name[i] = 0;
1391 switch (pn->mn_type) {
1395 case mqfstype_parent:
1396 entry.d_type = DT_DIR;
1399 entry.d_type = DT_REG;
1401 case mqfstype_symlink:
1402 entry.d_type = DT_LNK;
1405 panic("%s has unexpected node type: %d", pn->mn_name,
1408 if (entry.d_reclen > uio->uio_resid)
1410 if (offset >= uio->uio_offset) {
1411 error = vfs_read_dirent(ap, &entry, offset);
1415 offset += entry.d_reclen;
1417 sx_xunlock(&mi->mi_lock);
1419 uio->uio_offset = offset;
1421 if (tmp_ncookies != NULL)
1422 ap->a_ncookies = tmp_ncookies;
1430 struct vop_mkdir_args {
1431 struct vnode *a_dvp;
1432 struvt vnode **a_vpp;
1433 struvt componentname *a_cnp;
1434 struct vattr *a_vap;
1439 * Create a directory.
1442 mqfs_mkdir(struct vop_mkdir_args *ap)
1444 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1445 struct componentname *cnp = ap->a_cnp;
1446 struct mqfs_node *pd = VTON(ap->a_dvp);
1447 struct mqfs_node *pn;
1450 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1452 sx_xlock(&mqfs->mi_lock);
1453 if ((cnp->cn_flags & HASBUF) == 0)
1454 panic("%s: no name", __func__);
1455 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1456 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1459 sx_xunlock(&mqfs->mi_lock);
1463 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1470 struct vop_rmdir_args {
1471 struct vnode *a_dvp;
1473 struct componentname *a_cnp;
1478 * Remove a directory.
1481 mqfs_rmdir(struct vop_rmdir_args *ap)
1483 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1484 struct mqfs_node *pn = VTON(ap->a_vp);
1485 struct mqfs_node *pt;
1487 if (pn->mn_type != mqfstype_dir)
1490 sx_xlock(&mqfs->mi_lock);
1491 if (pn->mn_deleted) {
1492 sx_xunlock(&mqfs->mi_lock);
1496 pt = LIST_FIRST(&pn->mn_children);
1497 pt = LIST_NEXT(pt, mn_sibling);
1498 pt = LIST_NEXT(pt, mn_sibling);
1500 sx_xunlock(&mqfs->mi_lock);
1504 pn->mn_parent = NULL;
1506 LIST_REMOVE(pn, mn_sibling);
1509 sx_xunlock(&mqfs->mi_lock);
1510 cache_purge(ap->a_vp);
1517 * Allocate a message queue
1519 static struct mqueue *
1520 mqueue_alloc(const struct mq_attr *attr)
1526 mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1527 TAILQ_INIT(&mq->mq_msgq);
1529 mq->mq_maxmsg = attr->mq_maxmsg;
1530 mq->mq_msgsize = attr->mq_msgsize;
1532 mq->mq_maxmsg = default_maxmsg;
1533 mq->mq_msgsize = default_msgsize;
1535 mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1536 knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1537 knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1538 atomic_add_int(&curmq, 1);
1543 * Destroy a message queue
1546 mqueue_free(struct mqueue *mq)
1548 struct mqueue_msg *msg;
1550 while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1551 TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1552 free(msg, M_MQUEUEDATA);
1555 mtx_destroy(&mq->mq_mutex);
1556 knlist_destroy(&mq->mq_rsel.si_note);
1557 knlist_destroy(&mq->mq_wsel.si_note);
1558 uma_zfree(mqueue_zone, mq);
1559 atomic_add_int(&curmq, -1);
1563 * Load a message from user space
1565 static struct mqueue_msg *
1566 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1568 struct mqueue_msg *msg;
1572 len = sizeof(struct mqueue_msg) + msg_size;
1573 msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1574 error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1577 free(msg, M_MQUEUEDATA);
1580 msg->msg_size = msg_size;
1581 msg->msg_prio = msg_prio;
1587 * Save a message to user space
1590 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1594 error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1596 if (error == 0 && msg_prio != NULL)
1597 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1602 * Free a message's memory
1604 static __inline void
1605 mqueue_freemsg(struct mqueue_msg *msg)
1607 free(msg, M_MQUEUEDATA);
1611 * Send a message. if waitok is false, thread will not be
1612 * blocked if there is no data in queue, otherwise, absolute
1613 * time will be checked.
1616 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1617 size_t msg_len, unsigned msg_prio, int waitok,
1618 const struct timespec *abs_timeout)
1620 struct mqueue_msg *msg;
1621 struct timespec ts, ts2;
1625 if (msg_prio >= MQ_PRIO_MAX)
1627 if (msg_len > mq->mq_msgsize)
1629 msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1633 /* O_NONBLOCK case */
1635 error = _mqueue_send(mq, msg, -1);
1641 /* we allow a null timeout (wait forever) */
1642 if (abs_timeout == NULL) {
1643 error = _mqueue_send(mq, msg, 0);
1649 /* send it before checking time */
1650 error = _mqueue_send(mq, msg, -1);
1654 if (error != EAGAIN)
1657 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1664 timespecsub(&ts2, &ts);
1665 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1669 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1670 error = _mqueue_send(mq, msg, tvtohz(&tv));
1671 if (error != ETIMEDOUT)
1677 mqueue_freemsg(msg);
1682 * Common routine to send a message
1685 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1687 struct mqueue_msg *msg2;
1690 mtx_lock(&mq->mq_mutex);
1691 while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1693 mtx_unlock(&mq->mq_mutex);
1697 error = msleep(&mq->mq_senders, &mq->mq_mutex,
1698 PCATCH, "mqsend", timo);
1700 if (error == EAGAIN)
1703 if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1704 mtx_unlock(&mq->mq_mutex);
1708 if (TAILQ_EMPTY(&mq->mq_msgq)) {
1709 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1711 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1712 TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1714 TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1715 if (msg2->msg_prio < msg->msg_prio)
1718 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1722 mq->mq_totalbytes += msg->msg_size;
1723 if (mq->mq_receivers)
1724 wakeup_one(&mq->mq_receivers);
1725 else if (mq->mq_notifier != NULL)
1726 mqueue_send_notification(mq);
1727 if (mq->mq_flags & MQ_RSEL) {
1728 mq->mq_flags &= ~MQ_RSEL;
1729 selwakeup(&mq->mq_rsel);
1731 KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1732 mtx_unlock(&mq->mq_mutex);
1737 * Send realtime a signal to process which registered itself
1738 * successfully by mq_notify.
1741 mqueue_send_notification(struct mqueue *mq)
1743 struct mqueue_notifier *nt;
1746 mtx_assert(&mq->mq_mutex, MA_OWNED);
1747 nt = mq->mq_notifier;
1748 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1751 if (!KSI_ONQ(&nt->nt_ksi))
1752 psignal_event(p, &nt->nt_sigev, &nt->nt_ksi);
1755 mq->mq_notifier = NULL;
1759 * Get a message. if waitok is false, thread will not be
1760 * blocked if there is no data in queue, otherwise, absolute
1761 * time will be checked.
1764 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1765 size_t msg_len, unsigned *msg_prio, int waitok,
1766 const struct timespec *abs_timeout)
1768 struct mqueue_msg *msg;
1769 struct timespec ts, ts2;
1773 if (msg_len < mq->mq_msgsize)
1776 /* O_NONBLOCK case */
1778 error = _mqueue_recv(mq, &msg, -1);
1784 /* we allow a null timeout (wait forever). */
1785 if (abs_timeout == NULL) {
1786 error = _mqueue_recv(mq, &msg, 0);
1792 /* try to get a message before checking time */
1793 error = _mqueue_recv(mq, &msg, -1);
1797 if (error != EAGAIN)
1800 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1808 timespecsub(&ts2, &ts);
1809 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1813 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1814 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1817 if (error != ETIMEDOUT)
1822 error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1824 curthread->td_retval[0] = msg->msg_size;
1825 curthread->td_retval[1] = 0;
1827 mqueue_freemsg(msg);
1832 * Common routine to receive a message
1835 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1839 mtx_lock(&mq->mq_mutex);
1840 while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1842 mtx_unlock(&mq->mq_mutex);
1846 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1847 PCATCH, "mqrecv", timo);
1849 if (error == EAGAIN)
1854 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1856 mq->mq_totalbytes -= (*msg)->msg_size;
1858 wakeup_one(&mq->mq_senders);
1859 if (mq->mq_flags & MQ_WSEL) {
1860 mq->mq_flags &= ~MQ_WSEL;
1861 selwakeup(&mq->mq_wsel);
1863 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1865 if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1866 !TAILQ_EMPTY(&mq->mq_msgq)) {
1867 mqueue_send_notification(mq);
1869 mtx_unlock(&mq->mq_mutex);
1873 static __inline struct mqueue_notifier *
1874 notifier_alloc(void)
1876 return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1879 static __inline void
1880 notifier_free(struct mqueue_notifier *p)
1882 uma_zfree(mqnoti_zone, p);
1885 static struct mqueue_notifier *
1886 notifier_search(struct proc *p, int fd)
1888 struct mqueue_notifier *nt;
1890 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1891 if (nt->nt_ksi.ksi_mqd == fd)
1897 static __inline void
1898 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1900 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1903 static __inline void
1904 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1906 LIST_REMOVE(nt, nt_link);
1911 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1913 struct mqueue_notifier *nt;
1915 mtx_assert(&mq->mq_mutex, MA_OWNED);
1917 nt = notifier_search(p, fd);
1919 if (mq->mq_notifier == nt)
1920 mq->mq_notifier = NULL;
1921 sigqueue_take(&nt->nt_ksi);
1922 notifier_delete(p, nt);
1928 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
1929 const struct mq_attr *attr)
1931 char path[MQFS_NAMELEN + 1];
1932 struct mqfs_node *pn;
1933 struct filedesc *fdp;
1936 int fd, error, len, cmode;
1938 fdp = td->td_proc->p_fd;
1939 cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1941 if ((flags & O_CREAT) != 0 && attr != NULL) {
1942 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1944 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1948 error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
1953 * The first character of name must be a slash (/) character
1954 * and the remaining characters of name cannot include any slash
1958 if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL)
1961 error = falloc(td, &fp, &fd);
1965 sx_xlock(&mqfs_data.mi_lock);
1966 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1968 if (!(flags & O_CREAT)) {
1971 mq = mqueue_alloc(attr);
1975 pn = mqfs_create_file(mqfs_data.mi_root,
1976 path + 1, len - 1, td->td_ucred,
1989 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
1992 accmode_t accmode = 0;
1998 error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
1999 pn->mn_gid, accmode, td->td_ucred, NULL);
2004 sx_xunlock(&mqfs_data.mi_lock);
2005 fdclose(fdp, fp, fd, td);
2011 sx_xunlock(&mqfs_data.mi_lock);
2013 finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2016 FILEDESC_XLOCK(fdp);
2017 if (fdp->fd_ofiles[fd] == fp)
2018 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
2019 FILEDESC_XUNLOCK(fdp);
2020 td->td_retval[0] = fd;
2026 * Syscall to open a message queue.
2029 kmq_open(struct thread *td, struct kmq_open_args *uap)
2031 struct mq_attr attr;
2034 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2036 flags = FFLAGS(uap->flags);
2037 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2038 error = copyin(uap->attr, &attr, sizeof(attr));
2042 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2043 uap->attr != NULL ? &attr : NULL));
2047 * Syscall to unlink a message queue.
2050 kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2052 char path[MQFS_NAMELEN+1];
2053 struct mqfs_node *pn;
2056 error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2061 if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL)
2064 sx_xlock(&mqfs_data.mi_lock);
2065 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2067 error = do_unlink(pn, td->td_ucred);
2070 sx_xunlock(&mqfs_data.mi_lock);
2074 typedef int (*_fgetf)(struct thread *, int, struct file **);
2077 * Get message queue by giving file slot
2080 _getmq(struct thread *td, int fd, _fgetf func,
2081 struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2083 struct mqfs_node *pn;
2086 error = func(td, fd, fpp);
2089 if (&mqueueops != (*fpp)->f_ops) {
2093 pn = (*fpp)->f_data;
2102 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2103 struct mqueue **pmq)
2105 return _getmq(td, fd, fget, fpp, ppn, pmq);
2109 getmq_read(struct thread *td, int fd, struct file **fpp,
2110 struct mqfs_node **ppn, struct mqueue **pmq)
2112 return _getmq(td, fd, fget_read, fpp, ppn, pmq);
2116 getmq_write(struct thread *td, int fd, struct file **fpp,
2117 struct mqfs_node **ppn, struct mqueue **pmq)
2119 return _getmq(td, fd, fget_write, fpp, ppn, pmq);
2123 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2124 struct mq_attr *oattr)
2131 if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2133 error = getmq(td, mqd, &fp, NULL, &mq);
2136 oattr->mq_maxmsg = mq->mq_maxmsg;
2137 oattr->mq_msgsize = mq->mq_msgsize;
2138 oattr->mq_curmsgs = mq->mq_curmsgs;
2141 oflag = flag = fp->f_flag;
2142 flag &= ~O_NONBLOCK;
2143 flag |= (attr->mq_flags & O_NONBLOCK);
2144 } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2147 oattr->mq_flags = (O_NONBLOCK & oflag);
2153 kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2155 struct mq_attr attr, oattr;
2158 if (uap->attr != NULL) {
2159 error = copyin(uap->attr, &attr, sizeof(attr));
2163 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2167 if (uap->oattr != NULL)
2168 error = copyout(&oattr, uap->oattr, sizeof(oattr));
2173 kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2177 struct timespec *abs_timeout, ets;
2181 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2184 if (uap->abs_timeout != NULL) {
2185 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2191 waitok = !(fp->f_flag & O_NONBLOCK);
2192 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2193 uap->msg_prio, waitok, abs_timeout);
2199 kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2203 struct timespec *abs_timeout, ets;
2206 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2209 if (uap->abs_timeout != NULL) {
2210 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2216 waitok = !(fp->f_flag & O_NONBLOCK);
2217 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2218 uap->msg_prio, waitok, abs_timeout);
2224 kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2227 struct filedesc *fdp;
2231 struct mqueue_notifier *nt, *newnt = NULL;
2235 fdp = td->td_proc->p_fd;
2237 error = copyin(uap->sigev, &ev, sizeof(ev));
2240 if (ev.sigev_notify != SIGEV_SIGNAL &&
2241 ev.sigev_notify != SIGEV_THREAD_ID &&
2242 ev.sigev_notify != SIGEV_NONE)
2244 if ((ev.sigev_notify == SIGEV_SIGNAL ||
2245 ev.sigev_notify == SIGEV_THREAD_ID) &&
2246 !_SIG_VALID(ev.sigev_signo))
2249 error = getmq(td, uap->mqd, &fp, NULL, &mq);
2253 FILEDESC_SLOCK(fdp);
2254 if (fget_locked(fdp, uap->mqd) != fp) {
2255 FILEDESC_SUNLOCK(fdp);
2259 mtx_lock(&mq->mq_mutex);
2260 FILEDESC_SUNLOCK(fdp);
2261 if (uap->sigev != NULL) {
2262 if (mq->mq_notifier != NULL) {
2266 nt = notifier_search(p, uap->mqd);
2268 if (newnt == NULL) {
2270 mtx_unlock(&mq->mq_mutex);
2271 newnt = notifier_alloc();
2277 sigqueue_take(&nt->nt_ksi);
2278 if (newnt != NULL) {
2279 notifier_free(newnt);
2285 ksiginfo_init(&nt->nt_ksi);
2286 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2287 nt->nt_ksi.ksi_code = SI_MESGQ;
2289 nt->nt_ksi.ksi_mqd = uap->mqd;
2290 notifier_insert(p, nt);
2293 mq->mq_notifier = nt;
2296 * if there is no receivers and message queue
2297 * is not empty, we should send notification
2298 * as soon as possible.
2300 if (mq->mq_receivers == 0 &&
2301 !TAILQ_EMPTY(&mq->mq_msgq))
2302 mqueue_send_notification(mq);
2305 notifier_remove(p, mq, uap->mqd);
2307 mtx_unlock(&mq->mq_mutex);
2312 notifier_free(newnt);
2317 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2319 struct filedesc *fdp;
2322 fdp = td->td_proc->p_fd;
2323 FILEDESC_LOCK_ASSERT(fdp);
2325 if (fp->f_ops == &mqueueops) {
2327 mtx_lock(&mq->mq_mutex);
2328 notifier_remove(td->td_proc, mq, fd);
2330 /* have to wakeup thread in same process */
2331 if (mq->mq_flags & MQ_RSEL) {
2332 mq->mq_flags &= ~MQ_RSEL;
2333 selwakeup(&mq->mq_rsel);
2335 if (mq->mq_flags & MQ_WSEL) {
2336 mq->mq_flags &= ~MQ_WSEL;
2337 selwakeup(&mq->mq_wsel);
2339 mtx_unlock(&mq->mq_mutex);
2344 mq_proc_exit(void *arg __unused, struct proc *p)
2346 struct filedesc *fdp;
2352 FILEDESC_SLOCK(fdp);
2353 for (i = 0; i < fdp->fd_nfiles; ++i) {
2354 fp = fget_locked(fdp, i);
2355 if (fp != NULL && fp->f_ops == &mqueueops) {
2357 mtx_lock(&mq->mq_mutex);
2358 notifier_remove(p, FPTOMQ(fp), i);
2359 mtx_unlock(&mq->mq_mutex);
2362 FILEDESC_SUNLOCK(fdp);
2363 KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2367 mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2368 int flags, struct thread *td)
2370 return (EOPNOTSUPP);
2374 mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
2375 int flags, struct thread *td)
2377 return (EOPNOTSUPP);
2381 mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
2389 mqf_ioctl(struct file *fp, u_long cmd, void *data,
2390 struct ucred *active_cred, struct thread *td)
2396 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2399 struct mqueue *mq = FPTOMQ(fp);
2402 mtx_lock(&mq->mq_mutex);
2403 if (events & (POLLIN | POLLRDNORM)) {
2404 if (mq->mq_curmsgs) {
2405 revents |= events & (POLLIN | POLLRDNORM);
2407 mq->mq_flags |= MQ_RSEL;
2408 selrecord(td, &mq->mq_rsel);
2411 if (events & POLLOUT) {
2412 if (mq->mq_curmsgs < mq->mq_maxmsg)
2415 mq->mq_flags |= MQ_WSEL;
2416 selrecord(td, &mq->mq_wsel);
2419 mtx_unlock(&mq->mq_mutex);
2424 mqf_close(struct file *fp, struct thread *td)
2426 struct mqfs_node *pn;
2428 fp->f_ops = &badfileops;
2431 sx_xlock(&mqfs_data.mi_lock);
2433 sx_xunlock(&mqfs_data.mi_lock);
2438 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2441 struct mqfs_node *pn = fp->f_data;
2443 bzero(st, sizeof *st);
2444 st->st_atimespec = pn->mn_atime;
2445 st->st_mtimespec = pn->mn_mtime;
2446 st->st_ctimespec = pn->mn_ctime;
2447 st->st_birthtimespec = pn->mn_birth;
2448 st->st_uid = pn->mn_uid;
2449 st->st_gid = pn->mn_gid;
2450 st->st_mode = S_IFIFO | pn->mn_mode;
2455 mqf_kqfilter(struct file *fp, struct knote *kn)
2457 struct mqueue *mq = FPTOMQ(fp);
2460 if (kn->kn_filter == EVFILT_READ) {
2461 kn->kn_fop = &mq_rfiltops;
2462 knlist_add(&mq->mq_rsel.si_note, kn, 0);
2463 } else if (kn->kn_filter == EVFILT_WRITE) {
2464 kn->kn_fop = &mq_wfiltops;
2465 knlist_add(&mq->mq_wsel.si_note, kn, 0);
2472 filt_mqdetach(struct knote *kn)
2474 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2476 if (kn->kn_filter == EVFILT_READ)
2477 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2478 else if (kn->kn_filter == EVFILT_WRITE)
2479 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2481 panic("filt_mqdetach");
2485 filt_mqread(struct knote *kn, long hint)
2487 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2489 mtx_assert(&mq->mq_mutex, MA_OWNED);
2490 return (mq->mq_curmsgs != 0);
2494 filt_mqwrite(struct knote *kn, long hint)
2496 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2498 mtx_assert(&mq->mq_mutex, MA_OWNED);
2499 return (mq->mq_curmsgs < mq->mq_maxmsg);
2502 static struct fileops mqueueops = {
2503 .fo_read = mqf_read,
2504 .fo_write = mqf_write,
2505 .fo_truncate = mqf_truncate,
2506 .fo_ioctl = mqf_ioctl,
2507 .fo_poll = mqf_poll,
2508 .fo_kqfilter = mqf_kqfilter,
2509 .fo_stat = mqf_stat,
2510 .fo_close = mqf_close
2513 static struct vop_vector mqfs_vnodeops = {
2514 .vop_default = &default_vnodeops,
2515 .vop_access = mqfs_access,
2516 .vop_cachedlookup = mqfs_lookup,
2517 .vop_lookup = vfs_cache_lookup,
2518 .vop_reclaim = mqfs_reclaim,
2519 .vop_create = mqfs_create,
2520 .vop_remove = mqfs_remove,
2521 .vop_inactive = mqfs_inactive,
2522 .vop_open = mqfs_open,
2523 .vop_close = mqfs_close,
2524 .vop_getattr = mqfs_getattr,
2525 .vop_setattr = mqfs_setattr,
2526 .vop_read = mqfs_read,
2527 .vop_write = VOP_EOPNOTSUPP,
2528 .vop_readdir = mqfs_readdir,
2529 .vop_mkdir = VOP_EOPNOTSUPP,
2530 .vop_rmdir = VOP_EOPNOTSUPP
2533 static struct vfsops mqfs_vfsops = {
2534 .vfs_init = mqfs_init,
2535 .vfs_uninit = mqfs_uninit,
2536 .vfs_mount = mqfs_mount,
2537 .vfs_unmount = mqfs_unmount,
2538 .vfs_root = mqfs_root,
2539 .vfs_statfs = mqfs_statfs,
2542 static struct vfsconf mqueuefs_vfsconf = {
2543 .vfc_version = VFS_VERSION,
2544 .vfc_name = "mqueuefs",
2545 .vfc_vfsops = &mqfs_vfsops,
2547 .vfc_flags = VFCF_SYNTHETIC
2550 static struct syscall_helper_data mq_syscalls[] = {
2551 SYSCALL_INIT_HELPER(kmq_open),
2552 SYSCALL_INIT_HELPER(kmq_setattr),
2553 SYSCALL_INIT_HELPER(kmq_timedsend),
2554 SYSCALL_INIT_HELPER(kmq_timedreceive),
2555 SYSCALL_INIT_HELPER(kmq_notify),
2556 SYSCALL_INIT_HELPER(kmq_unlink),
2560 #ifdef COMPAT_FREEBSD32
2561 #include <compat/freebsd32/freebsd32.h>
2562 #include <compat/freebsd32/freebsd32_proto.h>
2563 #include <compat/freebsd32/freebsd32_syscall.h>
2564 #include <compat/freebsd32/freebsd32_util.h>
2567 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2570 to->mq_flags = from->mq_flags;
2571 to->mq_maxmsg = from->mq_maxmsg;
2572 to->mq_msgsize = from->mq_msgsize;
2573 to->mq_curmsgs = from->mq_curmsgs;
2577 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2580 to->mq_flags = from->mq_flags;
2581 to->mq_maxmsg = from->mq_maxmsg;
2582 to->mq_msgsize = from->mq_msgsize;
2583 to->mq_curmsgs = from->mq_curmsgs;
2587 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2589 struct mq_attr attr;
2590 struct mq_attr32 attr32;
2593 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2595 flags = FFLAGS(uap->flags);
2596 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2597 error = copyin(uap->attr, &attr32, sizeof(attr32));
2600 mq_attr_from32(&attr32, &attr);
2602 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2603 uap->attr != NULL ? &attr : NULL));
2607 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2609 struct mq_attr attr, oattr;
2610 struct mq_attr32 attr32, oattr32;
2613 if (uap->attr != NULL) {
2614 error = copyin(uap->attr, &attr32, sizeof(attr32));
2617 mq_attr_from32(&attr32, &attr);
2619 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2623 if (uap->oattr != NULL) {
2624 mq_attr_to32(&oattr, &oattr32);
2625 error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2631 freebsd32_kmq_timedsend(struct thread *td,
2632 struct freebsd32_kmq_timedsend_args *uap)
2636 struct timespec32 ets32;
2637 struct timespec *abs_timeout, ets;
2641 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2644 if (uap->abs_timeout != NULL) {
2645 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2648 CP(ets32, ets, tv_sec);
2649 CP(ets32, ets, tv_nsec);
2653 waitok = !(fp->f_flag & O_NONBLOCK);
2654 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2655 uap->msg_prio, waitok, abs_timeout);
2661 freebsd32_kmq_timedreceive(struct thread *td,
2662 struct freebsd32_kmq_timedreceive_args *uap)
2666 struct timespec32 ets32;
2667 struct timespec *abs_timeout, ets;
2670 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2673 if (uap->abs_timeout != NULL) {
2674 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2677 CP(ets32, ets, tv_sec);
2678 CP(ets32, ets, tv_nsec);
2682 waitok = !(fp->f_flag & O_NONBLOCK);
2683 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2684 uap->msg_prio, waitok, abs_timeout);
2689 static struct syscall_helper_data mq32_syscalls[] = {
2690 SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2691 SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
2692 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
2693 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
2694 SYSCALL32_INIT_HELPER(kmq_notify),
2695 SYSCALL32_INIT_HELPER(kmq_unlink),
2705 error = syscall_helper_register(mq_syscalls);
2708 #ifdef COMPAT_FREEBSD32
2709 error = syscall32_helper_register(mq32_syscalls);
2720 #ifdef COMPAT_FREEBSD32
2721 syscall32_helper_unregister(mq32_syscalls);
2723 syscall_helper_unregister(mq_syscalls);
2728 mq_modload(struct module *module, int cmd, void *arg)
2732 error = vfs_modevent(module, cmd, arg);
2751 static moduledata_t mqueuefs_mod = {
2756 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2757 MODULE_VERSION(mqueuefs, 1);