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/capability.h>
56 #include <sys/dirent.h>
57 #include <sys/event.h>
58 #include <sys/eventhandler.h>
59 #include <sys/fcntl.h>
61 #include <sys/filedesc.h>
63 #include <sys/malloc.h>
64 #include <sys/module.h>
65 #include <sys/mount.h>
66 #include <sys/mqueue.h>
67 #include <sys/mutex.h>
68 #include <sys/namei.h>
69 #include <sys/posix4.h>
73 #include <sys/queue.h>
74 #include <sys/sysproto.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysent.h>
80 #include <sys/sysctl.h>
81 #include <sys/taskqueue.h>
82 #include <sys/unistd.h>
83 #include <sys/vnode.h>
84 #include <machine/atomic.h>
86 FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
89 * Limits and constants
91 #define MQFS_NAMELEN NAME_MAX
92 #define MQFS_DELEN (8 + MQFS_NAMELEN)
108 * mqfs_info: describes a mqfs instance
112 struct mqfs_node *mi_root;
113 struct unrhdr *mi_unrhdr;
117 LIST_ENTRY(mqfs_vdata) mv_link;
118 struct mqfs_node *mv_node;
119 struct vnode *mv_vnode;
124 * mqfs_node: describes a node (file or directory) within a mqfs
127 char mn_name[MQFS_NAMELEN+1];
128 struct mqfs_info *mn_info;
129 struct mqfs_node *mn_parent;
130 LIST_HEAD(,mqfs_node) mn_children;
131 LIST_ENTRY(mqfs_node) mn_sibling;
132 LIST_HEAD(,mqfs_vdata) mn_vnodes;
138 struct timespec mn_birth;
139 struct timespec mn_ctime;
140 struct timespec mn_atime;
141 struct timespec mn_mtime;
147 #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
148 #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
149 #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
150 #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
151 (fp)->f_data)->mn_data))
153 TAILQ_HEAD(msgq, mqueue_msg);
157 struct mqueue_notifier {
158 LIST_ENTRY(mqueue_notifier) nt_link;
159 struct sigevent nt_sigev;
161 struct proc *nt_proc;
174 struct selinfo mq_rsel;
175 struct selinfo mq_wsel;
176 struct mqueue_notifier *mq_notifier;
183 TAILQ_ENTRY(mqueue_msg) msg_link;
184 unsigned int msg_prio;
185 unsigned int msg_size;
186 /* following real data... */
189 SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
190 "POSIX real time message queue");
192 static int default_maxmsg = 10;
193 static int default_msgsize = 1024;
195 static int maxmsg = 100;
196 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
197 &maxmsg, 0, "Default maximum messages in queue");
198 static int maxmsgsize = 16384;
199 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
200 &maxmsgsize, 0, "Default maximum message size");
201 static int maxmq = 100;
202 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
203 &maxmq, 0, "maximum message queues");
204 static int curmq = 0;
205 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
206 &curmq, 0, "current message queue number");
207 static int unloadable = 0;
208 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
210 static eventhandler_tag exit_tag;
212 /* Only one instance per-system */
213 static struct mqfs_info mqfs_data;
214 static uma_zone_t mqnode_zone;
215 static uma_zone_t mqueue_zone;
216 static uma_zone_t mvdata_zone;
217 static uma_zone_t mqnoti_zone;
218 static struct vop_vector mqfs_vnodeops;
219 static struct fileops mqueueops;
222 * Directory structure construction and manipulation
225 static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
226 const char *name, int namelen, struct ucred *cred, int mode);
227 static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
228 const char *name, int namelen, struct ucred *cred, int mode);
231 static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
232 const char *name, int namelen, struct ucred *cred, int mode);
233 static int mqfs_destroy(struct mqfs_node *mn);
234 static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
235 static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
236 static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
239 * Message queue construction and maniplation
241 static struct mqueue *mqueue_alloc(const struct mq_attr *attr);
242 static void mqueue_free(struct mqueue *mq);
243 static int mqueue_send(struct mqueue *mq, const char *msg_ptr,
244 size_t msg_len, unsigned msg_prio, int waitok,
245 const struct timespec *abs_timeout);
246 static int mqueue_receive(struct mqueue *mq, char *msg_ptr,
247 size_t msg_len, unsigned *msg_prio, int waitok,
248 const struct timespec *abs_timeout);
249 static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
251 static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
253 static void mqueue_send_notification(struct mqueue *mq);
254 static void mqueue_fdclose(struct thread *td, int fd, struct file *fp);
255 static void mq_proc_exit(void *arg, struct proc *p);
260 static void filt_mqdetach(struct knote *kn);
261 static int filt_mqread(struct knote *kn, long hint);
262 static int filt_mqwrite(struct knote *kn, long hint);
264 struct filterops mq_rfiltops = {
266 .f_detach = filt_mqdetach,
267 .f_event = filt_mqread,
269 struct filterops mq_wfiltops = {
271 .f_detach = filt_mqdetach,
272 .f_event = filt_mqwrite,
276 * Initialize fileno bitmap
279 mqfs_fileno_init(struct mqfs_info *mi)
283 up = new_unrhdr(1, INT_MAX, NULL);
288 * Tear down fileno bitmap
291 mqfs_fileno_uninit(struct mqfs_info *mi)
296 mi->mi_unrhdr = NULL;
301 * Allocate a file number
304 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
306 /* make sure our parent has a file number */
307 if (mn->mn_parent && !mn->mn_parent->mn_fileno)
308 mqfs_fileno_alloc(mi, mn->mn_parent);
310 switch (mn->mn_type) {
314 case mqfstype_symlink:
315 mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
318 KASSERT(mn->mn_parent != NULL,
319 ("mqfstype_this node has no parent"));
320 mn->mn_fileno = mn->mn_parent->mn_fileno;
322 case mqfstype_parent:
323 KASSERT(mn->mn_parent != NULL,
324 ("mqfstype_parent node has no parent"));
325 if (mn->mn_parent == mi->mi_root) {
326 mn->mn_fileno = mn->mn_parent->mn_fileno;
329 KASSERT(mn->mn_parent->mn_parent != NULL,
330 ("mqfstype_parent node has no grandparent"));
331 mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
335 ("mqfs_fileno_alloc() called for unknown type node: %d",
342 * Release a file number
345 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
347 switch (mn->mn_type) {
351 case mqfstype_symlink:
352 free_unr(mi->mi_unrhdr, mn->mn_fileno);
355 case mqfstype_parent:
356 /* ignore these, as they don't "own" their file number */
360 ("mqfs_fileno_free() called for unknown type node: %d",
366 static __inline struct mqfs_node *
369 return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
373 mqnode_free(struct mqfs_node *node)
375 uma_zfree(mqnode_zone, node);
379 mqnode_addref(struct mqfs_node *node)
381 atomic_fetchadd_int(&node->mn_refcount, 1);
385 mqnode_release(struct mqfs_node *node)
387 struct mqfs_info *mqfs;
390 mqfs = node->mn_info;
391 old = atomic_fetchadd_int(&node->mn_refcount, -1);
392 if (node->mn_type == mqfstype_dir ||
393 node->mn_type == mqfstype_root)
394 exp = 3; /* include . and .. */
398 int locked = sx_xlocked(&mqfs->mi_lock);
400 sx_xlock(&mqfs->mi_lock);
403 sx_xunlock(&mqfs->mi_lock);
408 * Add a node to a directory
411 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
413 KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
414 KASSERT(parent->mn_info != NULL,
415 ("%s(): parent has no mn_info", __func__));
416 KASSERT(parent->mn_type == mqfstype_dir ||
417 parent->mn_type == mqfstype_root,
418 ("%s(): parent is not a directory", __func__));
420 node->mn_info = parent->mn_info;
421 node->mn_parent = parent;
422 LIST_INIT(&node->mn_children);
423 LIST_INIT(&node->mn_vnodes);
424 LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
425 mqnode_addref(parent);
429 static struct mqfs_node *
430 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
433 struct mqfs_node *node;
435 node = mqnode_alloc();
436 strncpy(node->mn_name, name, namelen);
437 node->mn_type = nodetype;
438 node->mn_refcount = 1;
439 vfs_timestamp(&node->mn_birth);
440 node->mn_ctime = node->mn_atime = node->mn_mtime
442 node->mn_uid = cred->cr_uid;
443 node->mn_gid = cred->cr_gid;
444 node->mn_mode = mode;
451 static struct mqfs_node *
452 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
453 struct ucred *cred, int mode)
455 struct mqfs_node *node;
457 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
458 if (mqfs_add_node(parent, node) != 0) {
466 * Add . and .. to a directory
469 mqfs_fixup_dir(struct mqfs_node *parent)
471 struct mqfs_node *dir;
473 dir = mqnode_alloc();
474 dir->mn_name[0] = '.';
475 dir->mn_type = mqfstype_this;
476 dir->mn_refcount = 1;
477 if (mqfs_add_node(parent, dir) != 0) {
482 dir = mqnode_alloc();
483 dir->mn_name[0] = dir->mn_name[1] = '.';
484 dir->mn_type = mqfstype_parent;
485 dir->mn_refcount = 1;
487 if (mqfs_add_node(parent, dir) != 0) {
500 static struct mqfs_node *
501 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
502 struct ucred *cred, int mode)
504 struct mqfs_node *node;
506 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
507 if (mqfs_add_node(parent, node) != 0) {
512 if (mqfs_fixup_dir(node) != 0) {
522 static struct mqfs_node *
523 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
524 struct ucred *cred, int mode)
526 struct mqfs_node *node;
528 node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
529 if (mqfs_add_node(parent, node) != 0) {
539 * Destroy a node or a tree of nodes
542 mqfs_destroy(struct mqfs_node *node)
544 struct mqfs_node *parent;
546 KASSERT(node != NULL,
547 ("%s(): node is NULL", __func__));
548 KASSERT(node->mn_info != NULL,
549 ("%s(): node has no mn_info", __func__));
551 /* destroy children */
552 if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
553 while (! LIST_EMPTY(&node->mn_children))
554 mqfs_destroy(LIST_FIRST(&node->mn_children));
556 /* unlink from parent */
557 if ((parent = node->mn_parent) != NULL) {
558 KASSERT(parent->mn_info == node->mn_info,
559 ("%s(): parent has different mn_info", __func__));
560 LIST_REMOVE(node, mn_sibling);
563 if (node->mn_fileno != 0)
564 mqfs_fileno_free(node->mn_info, node);
565 if (node->mn_data != NULL)
566 mqueue_free(node->mn_data);
572 * Mount a mqfs instance
575 mqfs_mount(struct mount *mp)
579 if (mp->mnt_flag & MNT_UPDATE)
582 mp->mnt_data = &mqfs_data;
584 mp->mnt_flag |= MNT_LOCAL;
585 mp->mnt_kern_flag |= MNTK_MPSAFE;
590 vfs_mountedfrom(mp, "mqueue");
591 sbp->f_bsize = PAGE_SIZE;
592 sbp->f_iosize = PAGE_SIZE;
602 * Unmount a mqfs instance
605 mqfs_unmount(struct mount *mp, int mntflags)
609 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
615 * Return a root vnode
618 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
620 struct mqfs_info *mqfs;
623 mqfs = VFSTOMQFS(mp);
624 ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
629 * Return filesystem stats
632 mqfs_statfs(struct mount *mp, struct statfs *sbp)
634 /* XXX update statistics */
639 * Initialize a mqfs instance
642 mqfs_init(struct vfsconf *vfc)
644 struct mqfs_node *root;
645 struct mqfs_info *mi;
647 mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
648 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
649 mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
650 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
651 mvdata_zone = uma_zcreate("mvdata",
652 sizeof(struct mqfs_vdata), NULL, NULL, NULL,
653 NULL, UMA_ALIGN_PTR, 0);
654 mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
655 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
657 sx_init(&mi->mi_lock, "mqfs lock");
658 /* set up the root diretory */
659 root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
662 LIST_INIT(&root->mn_children);
663 LIST_INIT(&root->mn_vnodes);
665 mqfs_fileno_init(mi);
666 mqfs_fileno_alloc(mi, root);
667 mqfs_fixup_dir(root);
668 exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
669 EVENTHANDLER_PRI_ANY);
670 mq_fdclose = mqueue_fdclose;
671 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
676 * Destroy a mqfs instance
679 mqfs_uninit(struct vfsconf *vfc)
681 struct mqfs_info *mi;
685 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
687 mqfs_destroy(mi->mi_root);
689 mqfs_fileno_uninit(mi);
690 sx_destroy(&mi->mi_lock);
691 uma_zdestroy(mqnode_zone);
692 uma_zdestroy(mqueue_zone);
693 uma_zdestroy(mvdata_zone);
694 uma_zdestroy(mqnoti_zone);
702 do_recycle(void *context, int pending __unused)
704 struct vnode *vp = (struct vnode *)context;
706 vrecycle(vp, curthread);
714 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
716 struct mqfs_vdata *vd;
717 struct mqfs_info *mqfs;
718 struct vnode *newvpp;
723 sx_xlock(&mqfs->mi_lock);
724 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
725 if (vd->mv_vnode->v_mount == mp) {
734 sx_xunlock(&mqfs->mi_lock);
735 error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
739 sx_xunlock(&mqfs->mi_lock);
741 error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
744 vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
745 error = insmntque(newvpp, mp);
749 sx_xlock(&mqfs->mi_lock);
751 * Check if it has already been allocated
752 * while we were blocked.
754 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
755 if (vd->mv_vnode->v_mount == mp) {
757 sx_xunlock(&mqfs->mi_lock);
767 vd = uma_zalloc(mvdata_zone, M_WAITOK);
771 TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
772 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
774 switch (pn->mn_type) {
776 (*vpp)->v_vflag = VV_ROOT;
780 case mqfstype_parent:
781 (*vpp)->v_type = VDIR;
784 (*vpp)->v_type = VREG;
786 case mqfstype_symlink:
787 (*vpp)->v_type = VLNK;
790 KASSERT(0, ("mqfs_allocf called for null node\n"));
792 panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
794 sx_xunlock(&mqfs->mi_lock);
799 * Search a directory entry
801 static struct mqfs_node *
802 mqfs_search(struct mqfs_node *pd, const char *name, int len)
804 struct mqfs_node *pn;
806 sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
807 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
808 if (strncmp(pn->mn_name, name, len) == 0 &&
809 pn->mn_name[len] == '\0')
816 * Look up a file or directory.
819 mqfs_lookupx(struct vop_cachedlookup_args *ap)
821 struct componentname *cnp;
822 struct vnode *dvp, **vpp;
823 struct mqfs_node *pd;
824 struct mqfs_node *pn;
825 struct mqfs_info *mqfs;
826 int nameiop, flags, error, namelen;
833 pname = cnp->cn_nameptr;
834 namelen = cnp->cn_namelen;
836 flags = cnp->cn_flags;
837 nameiop = cnp->cn_nameiop;
843 if (dvp->v_type != VDIR)
846 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
850 /* shortcut: check if the name is too long */
851 if (cnp->cn_namelen >= MQFS_NAMELEN)
855 if (namelen == 1 && pname[0] == '.') {
856 if ((flags & ISLASTCN) && nameiop != LOOKUP)
865 if (cnp->cn_flags & ISDOTDOT) {
866 if (dvp->v_vflag & VV_ROOT)
868 if ((flags & ISLASTCN) && nameiop != LOOKUP)
871 KASSERT(pd->mn_parent, ("non-root directory has no parent"));
873 error = mqfs_allocv(dvp->v_mount, vpp, pn);
874 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
879 sx_xlock(&mqfs->mi_lock);
880 pn = mqfs_search(pd, pname, namelen);
883 sx_xunlock(&mqfs->mi_lock);
888 if (nameiop == DELETE && (flags & ISLASTCN)) {
889 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
903 error = mqfs_allocv(dvp->v_mount, vpp, pn);
905 if (error == 0 && cnp->cn_flags & MAKEENTRY)
906 cache_enter(dvp, *vpp, cnp);
912 /* will create a new entry in the directory ? */
913 if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
914 && (flags & ISLASTCN)) {
915 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
918 cnp->cn_flags |= SAVENAME;
919 return (EJUSTRETURN);
925 struct vop_lookup_args {
926 struct vop_generic_args a_gen;
928 struct vnode **a_vpp;
929 struct componentname *a_cnp;
934 * vnode lookup operation
937 mqfs_lookup(struct vop_cachedlookup_args *ap)
941 rc = mqfs_lookupx(ap);
946 struct vop_create_args {
948 struct vnode **a_vpp;
949 struct componentname *a_cnp;
955 * vnode creation operation
958 mqfs_create(struct vop_create_args *ap)
960 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
961 struct componentname *cnp = ap->a_cnp;
962 struct mqfs_node *pd;
963 struct mqfs_node *pn;
967 pd = VTON(ap->a_dvp);
968 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
970 mq = mqueue_alloc(NULL);
973 sx_xlock(&mqfs->mi_lock);
974 if ((cnp->cn_flags & HASBUF) == 0)
975 panic("%s: no name", __func__);
976 pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
977 cnp->cn_cred, ap->a_vap->va_mode);
979 sx_xunlock(&mqfs->mi_lock);
983 sx_xunlock(&mqfs->mi_lock);
984 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1000 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
1002 struct mqfs_node *parent;
1003 struct mqfs_vdata *vd;
1006 sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1008 if (ucred->cr_uid != pn->mn_uid &&
1009 (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
1011 else if (!pn->mn_deleted) {
1012 parent = pn->mn_parent;
1013 pn->mn_parent = NULL;
1015 LIST_REMOVE(pn, mn_sibling);
1016 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1017 cache_purge(vd->mv_vnode);
1018 vhold(vd->mv_vnode);
1019 taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1022 mqnode_release(parent);
1029 struct vop_remove_args {
1030 struct vnode *a_dvp;
1032 struct componentname *a_cnp;
1037 * vnode removal operation
1040 mqfs_remove(struct vop_remove_args *ap)
1042 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1043 struct mqfs_node *pn;
1046 if (ap->a_vp->v_type == VDIR)
1048 pn = VTON(ap->a_vp);
1049 sx_xlock(&mqfs->mi_lock);
1050 error = do_unlink(pn, ap->a_cnp->cn_cred);
1051 sx_xunlock(&mqfs->mi_lock);
1056 struct vop_inactive_args {
1058 struct thread *a_td;
1063 mqfs_inactive(struct vop_inactive_args *ap)
1065 struct mqfs_node *pn = VTON(ap->a_vp);
1068 vrecycle(ap->a_vp, ap->a_td);
1073 struct vop_reclaim_args {
1074 struct vop_generic_args a_gen;
1076 struct thread *a_td;
1081 mqfs_reclaim(struct vop_reclaim_args *ap)
1083 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1084 struct vnode *vp = ap->a_vp;
1085 struct mqfs_node *pn;
1086 struct mqfs_vdata *vd;
1090 sx_xlock(&mqfs->mi_lock);
1092 LIST_REMOVE(vd, mv_link);
1093 uma_zfree(mvdata_zone, vd);
1095 sx_xunlock(&mqfs->mi_lock);
1100 struct vop_open_args {
1101 struct vop_generic_args a_gen;
1104 struct ucred *a_cred;
1105 struct thread *a_td;
1111 mqfs_open(struct vop_open_args *ap)
1117 struct vop_close_args {
1118 struct vop_generic_args a_gen;
1121 struct ucred *a_cred;
1122 struct thread *a_td;
1127 mqfs_close(struct vop_close_args *ap)
1133 struct vop_access_args {
1134 struct vop_generic_args a_gen;
1136 accmode_t a_accmode;
1137 struct ucred *a_cred;
1138 struct thread *a_td;
1143 * Verify permissions
1146 mqfs_access(struct vop_access_args *ap)
1148 struct vnode *vp = ap->a_vp;
1152 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1155 error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1156 vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
1161 struct vop_getattr_args {
1162 struct vop_generic_args a_gen;
1164 struct vattr *a_vap;
1165 struct ucred *a_cred;
1170 * Get file attributes
1173 mqfs_getattr(struct vop_getattr_args *ap)
1175 struct vnode *vp = ap->a_vp;
1176 struct mqfs_node *pn = VTON(vp);
1177 struct vattr *vap = ap->a_vap;
1180 vap->va_type = vp->v_type;
1181 vap->va_mode = pn->mn_mode;
1183 vap->va_uid = pn->mn_uid;
1184 vap->va_gid = pn->mn_gid;
1185 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1186 vap->va_fileid = pn->mn_fileno;
1188 vap->va_blocksize = PAGE_SIZE;
1189 vap->va_bytes = vap->va_size = 0;
1190 vap->va_atime = pn->mn_atime;
1191 vap->va_mtime = pn->mn_mtime;
1192 vap->va_ctime = pn->mn_ctime;
1193 vap->va_birthtime = pn->mn_birth;
1196 vap->va_rdev = NODEV;
1198 vap->va_filerev = 0;
1203 struct vop_setattr_args {
1204 struct vop_generic_args a_gen;
1206 struct vattr *a_vap;
1207 struct ucred *a_cred;
1214 mqfs_setattr(struct vop_setattr_args *ap)
1216 struct mqfs_node *pn;
1227 if ((vap->va_type != VNON) ||
1228 (vap->va_nlink != VNOVAL) ||
1229 (vap->va_fsid != VNOVAL) ||
1230 (vap->va_fileid != VNOVAL) ||
1231 (vap->va_blocksize != VNOVAL) ||
1232 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1233 (vap->va_rdev != VNOVAL) ||
1234 ((int)vap->va_bytes != VNOVAL) ||
1235 (vap->va_gen != VNOVAL)) {
1242 if (vap->va_uid == (uid_t)VNOVAL)
1246 if (vap->va_gid == (gid_t)VNOVAL)
1251 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1253 * To modify the ownership of a file, must possess VADMIN
1256 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1260 * XXXRW: Why is there a privilege check here: shouldn't the
1261 * check in VOP_ACCESS() be enough? Also, are the group bits
1262 * below definitely right?
1264 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1265 (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1266 (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1273 if (vap->va_mode != (mode_t)VNOVAL) {
1274 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1275 (error = priv_check(td, PRIV_MQ_ADMIN)))
1277 pn->mn_mode = vap->va_mode;
1281 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1282 /* See the comment in ufs_vnops::ufs_setattr(). */
1283 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1284 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1285 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1287 if (vap->va_atime.tv_sec != VNOVAL) {
1288 pn->mn_atime = vap->va_atime;
1290 if (vap->va_mtime.tv_sec != VNOVAL) {
1291 pn->mn_mtime = vap->va_mtime;
1296 vfs_timestamp(&pn->mn_ctime);
1302 struct vop_read_args {
1303 struct vop_generic_args a_gen;
1307 struct ucred *a_cred;
1315 mqfs_read(struct vop_read_args *ap)
1318 struct vnode *vp = ap->a_vp;
1319 struct uio *uio = ap->a_uio;
1320 struct mqfs_node *pn;
1324 if (vp->v_type != VREG)
1329 snprintf(buf, sizeof(buf),
1330 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1335 buf[sizeof(buf)-1] = '\0';
1337 error = uiomove_frombuf(buf, len, uio);
1342 struct vop_readdir_args {
1343 struct vop_generic_args a_gen;
1346 struct ucred *a_cred;
1354 * Return directory entries.
1357 mqfs_readdir(struct vop_readdir_args *ap)
1360 struct mqfs_info *mi;
1361 struct mqfs_node *pd;
1362 struct mqfs_node *pn;
1363 struct dirent entry;
1365 int *tmp_ncookies = NULL;
1370 mi = VFSTOMQFS(vp->v_mount);
1374 if (vp->v_type != VDIR)
1377 if (uio->uio_offset < 0)
1380 if (ap->a_ncookies != NULL) {
1381 tmp_ncookies = ap->a_ncookies;
1382 *ap->a_ncookies = 0;
1383 ap->a_ncookies = NULL;
1389 sx_xlock(&mi->mi_lock);
1391 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1392 entry.d_reclen = sizeof(entry);
1394 mqfs_fileno_alloc(mi, pn);
1395 entry.d_fileno = pn->mn_fileno;
1396 for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1397 entry.d_name[i] = pn->mn_name[i];
1398 entry.d_name[i] = 0;
1400 switch (pn->mn_type) {
1404 case mqfstype_parent:
1405 entry.d_type = DT_DIR;
1408 entry.d_type = DT_REG;
1410 case mqfstype_symlink:
1411 entry.d_type = DT_LNK;
1414 panic("%s has unexpected node type: %d", pn->mn_name,
1417 if (entry.d_reclen > uio->uio_resid)
1419 if (offset >= uio->uio_offset) {
1420 error = vfs_read_dirent(ap, &entry, offset);
1424 offset += entry.d_reclen;
1426 sx_xunlock(&mi->mi_lock);
1428 uio->uio_offset = offset;
1430 if (tmp_ncookies != NULL)
1431 ap->a_ncookies = tmp_ncookies;
1439 struct vop_mkdir_args {
1440 struct vnode *a_dvp;
1441 struvt vnode **a_vpp;
1442 struvt componentname *a_cnp;
1443 struct vattr *a_vap;
1448 * Create a directory.
1451 mqfs_mkdir(struct vop_mkdir_args *ap)
1453 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1454 struct componentname *cnp = ap->a_cnp;
1455 struct mqfs_node *pd = VTON(ap->a_dvp);
1456 struct mqfs_node *pn;
1459 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1461 sx_xlock(&mqfs->mi_lock);
1462 if ((cnp->cn_flags & HASBUF) == 0)
1463 panic("%s: no name", __func__);
1464 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1465 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1468 sx_xunlock(&mqfs->mi_lock);
1472 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1479 struct vop_rmdir_args {
1480 struct vnode *a_dvp;
1482 struct componentname *a_cnp;
1487 * Remove a directory.
1490 mqfs_rmdir(struct vop_rmdir_args *ap)
1492 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1493 struct mqfs_node *pn = VTON(ap->a_vp);
1494 struct mqfs_node *pt;
1496 if (pn->mn_type != mqfstype_dir)
1499 sx_xlock(&mqfs->mi_lock);
1500 if (pn->mn_deleted) {
1501 sx_xunlock(&mqfs->mi_lock);
1505 pt = LIST_FIRST(&pn->mn_children);
1506 pt = LIST_NEXT(pt, mn_sibling);
1507 pt = LIST_NEXT(pt, mn_sibling);
1509 sx_xunlock(&mqfs->mi_lock);
1513 pn->mn_parent = NULL;
1515 LIST_REMOVE(pn, mn_sibling);
1518 sx_xunlock(&mqfs->mi_lock);
1519 cache_purge(ap->a_vp);
1526 * Allocate a message queue
1528 static struct mqueue *
1529 mqueue_alloc(const struct mq_attr *attr)
1535 mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1536 TAILQ_INIT(&mq->mq_msgq);
1538 mq->mq_maxmsg = attr->mq_maxmsg;
1539 mq->mq_msgsize = attr->mq_msgsize;
1541 mq->mq_maxmsg = default_maxmsg;
1542 mq->mq_msgsize = default_msgsize;
1544 mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1545 knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1546 knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1547 atomic_add_int(&curmq, 1);
1552 * Destroy a message queue
1555 mqueue_free(struct mqueue *mq)
1557 struct mqueue_msg *msg;
1559 while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1560 TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1561 free(msg, M_MQUEUEDATA);
1564 mtx_destroy(&mq->mq_mutex);
1565 knlist_destroy(&mq->mq_rsel.si_note);
1566 knlist_destroy(&mq->mq_wsel.si_note);
1567 uma_zfree(mqueue_zone, mq);
1568 atomic_add_int(&curmq, -1);
1572 * Load a message from user space
1574 static struct mqueue_msg *
1575 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1577 struct mqueue_msg *msg;
1581 len = sizeof(struct mqueue_msg) + msg_size;
1582 msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1583 error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1586 free(msg, M_MQUEUEDATA);
1589 msg->msg_size = msg_size;
1590 msg->msg_prio = msg_prio;
1596 * Save a message to user space
1599 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1603 error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1605 if (error == 0 && msg_prio != NULL)
1606 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1611 * Free a message's memory
1613 static __inline void
1614 mqueue_freemsg(struct mqueue_msg *msg)
1616 free(msg, M_MQUEUEDATA);
1620 * Send a message. if waitok is false, thread will not be
1621 * blocked if there is no data in queue, otherwise, absolute
1622 * time will be checked.
1625 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1626 size_t msg_len, unsigned msg_prio, int waitok,
1627 const struct timespec *abs_timeout)
1629 struct mqueue_msg *msg;
1630 struct timespec ts, ts2;
1634 if (msg_prio >= MQ_PRIO_MAX)
1636 if (msg_len > mq->mq_msgsize)
1638 msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1642 /* O_NONBLOCK case */
1644 error = _mqueue_send(mq, msg, -1);
1650 /* we allow a null timeout (wait forever) */
1651 if (abs_timeout == NULL) {
1652 error = _mqueue_send(mq, msg, 0);
1658 /* send it before checking time */
1659 error = _mqueue_send(mq, msg, -1);
1663 if (error != EAGAIN)
1666 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1673 timespecsub(&ts2, &ts);
1674 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1678 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1679 error = _mqueue_send(mq, msg, tvtohz(&tv));
1680 if (error != ETIMEDOUT)
1686 mqueue_freemsg(msg);
1691 * Common routine to send a message
1694 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1696 struct mqueue_msg *msg2;
1699 mtx_lock(&mq->mq_mutex);
1700 while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1702 mtx_unlock(&mq->mq_mutex);
1706 error = msleep(&mq->mq_senders, &mq->mq_mutex,
1707 PCATCH, "mqsend", timo);
1709 if (error == EAGAIN)
1712 if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1713 mtx_unlock(&mq->mq_mutex);
1717 if (TAILQ_EMPTY(&mq->mq_msgq)) {
1718 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1720 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1721 TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1723 TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1724 if (msg2->msg_prio < msg->msg_prio)
1727 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1731 mq->mq_totalbytes += msg->msg_size;
1732 if (mq->mq_receivers)
1733 wakeup_one(&mq->mq_receivers);
1734 else if (mq->mq_notifier != NULL)
1735 mqueue_send_notification(mq);
1736 if (mq->mq_flags & MQ_RSEL) {
1737 mq->mq_flags &= ~MQ_RSEL;
1738 selwakeup(&mq->mq_rsel);
1740 KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1741 mtx_unlock(&mq->mq_mutex);
1746 * Send realtime a signal to process which registered itself
1747 * successfully by mq_notify.
1750 mqueue_send_notification(struct mqueue *mq)
1752 struct mqueue_notifier *nt;
1757 mtx_assert(&mq->mq_mutex, MA_OWNED);
1758 nt = mq->mq_notifier;
1759 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1761 error = sigev_findtd(p, &nt->nt_sigev, &td);
1763 mq->mq_notifier = NULL;
1766 if (!KSI_ONQ(&nt->nt_ksi)) {
1767 ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1768 tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1772 mq->mq_notifier = NULL;
1776 * Get a message. if waitok is false, thread will not be
1777 * blocked if there is no data in queue, otherwise, absolute
1778 * time will be checked.
1781 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1782 size_t msg_len, unsigned *msg_prio, int waitok,
1783 const struct timespec *abs_timeout)
1785 struct mqueue_msg *msg;
1786 struct timespec ts, ts2;
1790 if (msg_len < mq->mq_msgsize)
1793 /* O_NONBLOCK case */
1795 error = _mqueue_recv(mq, &msg, -1);
1801 /* we allow a null timeout (wait forever). */
1802 if (abs_timeout == NULL) {
1803 error = _mqueue_recv(mq, &msg, 0);
1809 /* try to get a message before checking time */
1810 error = _mqueue_recv(mq, &msg, -1);
1814 if (error != EAGAIN)
1817 if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1825 timespecsub(&ts2, &ts);
1826 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1830 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1831 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1834 if (error != ETIMEDOUT)
1839 error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1841 curthread->td_retval[0] = msg->msg_size;
1842 curthread->td_retval[1] = 0;
1844 mqueue_freemsg(msg);
1849 * Common routine to receive a message
1852 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1856 mtx_lock(&mq->mq_mutex);
1857 while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1859 mtx_unlock(&mq->mq_mutex);
1863 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1864 PCATCH, "mqrecv", timo);
1866 if (error == EAGAIN)
1871 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1873 mq->mq_totalbytes -= (*msg)->msg_size;
1875 wakeup_one(&mq->mq_senders);
1876 if (mq->mq_flags & MQ_WSEL) {
1877 mq->mq_flags &= ~MQ_WSEL;
1878 selwakeup(&mq->mq_wsel);
1880 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1882 if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1883 !TAILQ_EMPTY(&mq->mq_msgq)) {
1884 mqueue_send_notification(mq);
1886 mtx_unlock(&mq->mq_mutex);
1890 static __inline struct mqueue_notifier *
1891 notifier_alloc(void)
1893 return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1896 static __inline void
1897 notifier_free(struct mqueue_notifier *p)
1899 uma_zfree(mqnoti_zone, p);
1902 static struct mqueue_notifier *
1903 notifier_search(struct proc *p, int fd)
1905 struct mqueue_notifier *nt;
1907 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1908 if (nt->nt_ksi.ksi_mqd == fd)
1914 static __inline void
1915 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1917 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1920 static __inline void
1921 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1923 LIST_REMOVE(nt, nt_link);
1928 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1930 struct mqueue_notifier *nt;
1932 mtx_assert(&mq->mq_mutex, MA_OWNED);
1934 nt = notifier_search(p, fd);
1936 if (mq->mq_notifier == nt)
1937 mq->mq_notifier = NULL;
1938 sigqueue_take(&nt->nt_ksi);
1939 notifier_delete(p, nt);
1945 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
1946 const struct mq_attr *attr)
1948 char path[MQFS_NAMELEN + 1];
1949 struct mqfs_node *pn;
1950 struct filedesc *fdp;
1953 int fd, error, len, cmode;
1955 fdp = td->td_proc->p_fd;
1956 cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1958 if ((flags & O_CREAT) != 0 && attr != NULL) {
1959 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
1961 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
1965 error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
1970 * The first character of name must be a slash (/) character
1971 * and the remaining characters of name cannot include any slash
1975 if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL)
1978 error = falloc(td, &fp, &fd, 0);
1982 sx_xlock(&mqfs_data.mi_lock);
1983 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1985 if (!(flags & O_CREAT)) {
1988 mq = mqueue_alloc(attr);
1992 pn = mqfs_create_file(mqfs_data.mi_root,
1993 path + 1, len - 1, td->td_ucred,
2006 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2009 accmode_t accmode = 0;
2015 error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
2016 pn->mn_gid, accmode, td->td_ucred, NULL);
2021 sx_xunlock(&mqfs_data.mi_lock);
2022 fdclose(fdp, fp, fd, td);
2028 sx_xunlock(&mqfs_data.mi_lock);
2030 finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2033 FILEDESC_XLOCK(fdp);
2034 if (fdp->fd_ofiles[fd] == fp)
2035 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
2036 FILEDESC_XUNLOCK(fdp);
2037 td->td_retval[0] = fd;
2043 * Syscall to open a message queue.
2046 kmq_open(struct thread *td, struct kmq_open_args *uap)
2048 struct mq_attr attr;
2051 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2053 flags = FFLAGS(uap->flags);
2054 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2055 error = copyin(uap->attr, &attr, sizeof(attr));
2059 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2060 uap->attr != NULL ? &attr : NULL));
2064 * Syscall to unlink a message queue.
2067 kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2069 char path[MQFS_NAMELEN+1];
2070 struct mqfs_node *pn;
2073 error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2078 if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL)
2081 sx_xlock(&mqfs_data.mi_lock);
2082 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2084 error = do_unlink(pn, td->td_ucred);
2087 sx_xunlock(&mqfs_data.mi_lock);
2091 typedef int (*_fgetf)(struct thread *, int, cap_rights_t, struct file **);
2094 * Get message queue by giving file slot
2097 _getmq(struct thread *td, int fd, cap_rights_t rights, _fgetf func,
2098 struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2100 struct mqfs_node *pn;
2103 error = func(td, fd, rights, fpp);
2106 if (&mqueueops != (*fpp)->f_ops) {
2110 pn = (*fpp)->f_data;
2119 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2120 struct mqueue **pmq)
2122 return _getmq(td, fd, CAP_POLL_EVENT, fget, fpp, ppn, pmq);
2126 getmq_read(struct thread *td, int fd, struct file **fpp,
2127 struct mqfs_node **ppn, struct mqueue **pmq)
2129 return _getmq(td, fd, CAP_READ, fget_read, fpp, ppn, pmq);
2133 getmq_write(struct thread *td, int fd, struct file **fpp,
2134 struct mqfs_node **ppn, struct mqueue **pmq)
2136 return _getmq(td, fd, CAP_WRITE, fget_write, fpp, ppn, pmq);
2140 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2141 struct mq_attr *oattr)
2148 if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2150 error = getmq(td, mqd, &fp, NULL, &mq);
2153 oattr->mq_maxmsg = mq->mq_maxmsg;
2154 oattr->mq_msgsize = mq->mq_msgsize;
2155 oattr->mq_curmsgs = mq->mq_curmsgs;
2158 oflag = flag = fp->f_flag;
2159 flag &= ~O_NONBLOCK;
2160 flag |= (attr->mq_flags & O_NONBLOCK);
2161 } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2164 oattr->mq_flags = (O_NONBLOCK & oflag);
2170 kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2172 struct mq_attr attr, oattr;
2175 if (uap->attr != NULL) {
2176 error = copyin(uap->attr, &attr, sizeof(attr));
2180 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2184 if (uap->oattr != NULL)
2185 error = copyout(&oattr, uap->oattr, sizeof(oattr));
2190 kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2194 struct timespec *abs_timeout, ets;
2198 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2201 if (uap->abs_timeout != NULL) {
2202 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2208 waitok = !(fp->f_flag & O_NONBLOCK);
2209 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2210 uap->msg_prio, waitok, abs_timeout);
2216 kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2220 struct timespec *abs_timeout, ets;
2223 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2226 if (uap->abs_timeout != NULL) {
2227 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2233 waitok = !(fp->f_flag & O_NONBLOCK);
2234 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2235 uap->msg_prio, waitok, abs_timeout);
2241 kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2244 struct filedesc *fdp;
2247 struct file *fp, *fp2;
2248 struct mqueue_notifier *nt, *newnt = NULL;
2252 fdp = td->td_proc->p_fd;
2254 error = copyin(uap->sigev, &ev, sizeof(ev));
2257 if (ev.sigev_notify != SIGEV_SIGNAL &&
2258 ev.sigev_notify != SIGEV_THREAD_ID &&
2259 ev.sigev_notify != SIGEV_NONE)
2261 if ((ev.sigev_notify == SIGEV_SIGNAL ||
2262 ev.sigev_notify == SIGEV_THREAD_ID) &&
2263 !_SIG_VALID(ev.sigev_signo))
2266 error = getmq(td, uap->mqd, &fp, NULL, &mq);
2270 FILEDESC_SLOCK(fdp);
2271 fp2 = fget_locked(fdp, uap->mqd);
2273 FILEDESC_SUNLOCK(fdp);
2277 error = cap_funwrap(fp2, CAP_POLL_EVENT, &fp2);
2279 FILEDESC_SUNLOCK(fdp);
2283 FILEDESC_SUNLOCK(fdp);
2287 mtx_lock(&mq->mq_mutex);
2288 FILEDESC_SUNLOCK(fdp);
2289 if (uap->sigev != NULL) {
2290 if (mq->mq_notifier != NULL) {
2294 nt = notifier_search(p, uap->mqd);
2296 if (newnt == NULL) {
2298 mtx_unlock(&mq->mq_mutex);
2299 newnt = notifier_alloc();
2305 sigqueue_take(&nt->nt_ksi);
2306 if (newnt != NULL) {
2307 notifier_free(newnt);
2313 ksiginfo_init(&nt->nt_ksi);
2314 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2315 nt->nt_ksi.ksi_code = SI_MESGQ;
2317 nt->nt_ksi.ksi_mqd = uap->mqd;
2318 notifier_insert(p, nt);
2321 mq->mq_notifier = nt;
2324 * if there is no receivers and message queue
2325 * is not empty, we should send notification
2326 * as soon as possible.
2328 if (mq->mq_receivers == 0 &&
2329 !TAILQ_EMPTY(&mq->mq_msgq))
2330 mqueue_send_notification(mq);
2333 notifier_remove(p, mq, uap->mqd);
2335 mtx_unlock(&mq->mq_mutex);
2340 notifier_free(newnt);
2345 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2347 struct filedesc *fdp;
2350 fdp = td->td_proc->p_fd;
2351 FILEDESC_LOCK_ASSERT(fdp);
2353 if (fp->f_ops == &mqueueops) {
2355 mtx_lock(&mq->mq_mutex);
2356 notifier_remove(td->td_proc, mq, fd);
2358 /* have to wakeup thread in same process */
2359 if (mq->mq_flags & MQ_RSEL) {
2360 mq->mq_flags &= ~MQ_RSEL;
2361 selwakeup(&mq->mq_rsel);
2363 if (mq->mq_flags & MQ_WSEL) {
2364 mq->mq_flags &= ~MQ_WSEL;
2365 selwakeup(&mq->mq_wsel);
2367 mtx_unlock(&mq->mq_mutex);
2372 mq_proc_exit(void *arg __unused, struct proc *p)
2374 struct filedesc *fdp;
2380 FILEDESC_SLOCK(fdp);
2381 for (i = 0; i < fdp->fd_nfiles; ++i) {
2382 fp = fget_locked(fdp, i);
2383 if (fp != NULL && fp->f_ops == &mqueueops) {
2385 mtx_lock(&mq->mq_mutex);
2386 notifier_remove(p, FPTOMQ(fp), i);
2387 mtx_unlock(&mq->mq_mutex);
2390 FILEDESC_SUNLOCK(fdp);
2391 KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2395 mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2396 int flags, struct thread *td)
2398 return (EOPNOTSUPP);
2402 mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
2403 int flags, struct thread *td)
2405 return (EOPNOTSUPP);
2409 mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
2417 mqf_ioctl(struct file *fp, u_long cmd, void *data,
2418 struct ucred *active_cred, struct thread *td)
2424 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2427 struct mqueue *mq = FPTOMQ(fp);
2430 mtx_lock(&mq->mq_mutex);
2431 if (events & (POLLIN | POLLRDNORM)) {
2432 if (mq->mq_curmsgs) {
2433 revents |= events & (POLLIN | POLLRDNORM);
2435 mq->mq_flags |= MQ_RSEL;
2436 selrecord(td, &mq->mq_rsel);
2439 if (events & POLLOUT) {
2440 if (mq->mq_curmsgs < mq->mq_maxmsg)
2443 mq->mq_flags |= MQ_WSEL;
2444 selrecord(td, &mq->mq_wsel);
2447 mtx_unlock(&mq->mq_mutex);
2452 mqf_close(struct file *fp, struct thread *td)
2454 struct mqfs_node *pn;
2456 fp->f_ops = &badfileops;
2459 sx_xlock(&mqfs_data.mi_lock);
2461 sx_xunlock(&mqfs_data.mi_lock);
2466 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2469 struct mqfs_node *pn = fp->f_data;
2471 bzero(st, sizeof *st);
2472 st->st_atim = pn->mn_atime;
2473 st->st_mtim = pn->mn_mtime;
2474 st->st_ctim = pn->mn_ctime;
2475 st->st_birthtim = pn->mn_birth;
2476 st->st_uid = pn->mn_uid;
2477 st->st_gid = pn->mn_gid;
2478 st->st_mode = S_IFIFO | pn->mn_mode;
2483 mqf_kqfilter(struct file *fp, struct knote *kn)
2485 struct mqueue *mq = FPTOMQ(fp);
2488 if (kn->kn_filter == EVFILT_READ) {
2489 kn->kn_fop = &mq_rfiltops;
2490 knlist_add(&mq->mq_rsel.si_note, kn, 0);
2491 } else if (kn->kn_filter == EVFILT_WRITE) {
2492 kn->kn_fop = &mq_wfiltops;
2493 knlist_add(&mq->mq_wsel.si_note, kn, 0);
2500 filt_mqdetach(struct knote *kn)
2502 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2504 if (kn->kn_filter == EVFILT_READ)
2505 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2506 else if (kn->kn_filter == EVFILT_WRITE)
2507 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2509 panic("filt_mqdetach");
2513 filt_mqread(struct knote *kn, long hint)
2515 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2517 mtx_assert(&mq->mq_mutex, MA_OWNED);
2518 return (mq->mq_curmsgs != 0);
2522 filt_mqwrite(struct knote *kn, long hint)
2524 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2526 mtx_assert(&mq->mq_mutex, MA_OWNED);
2527 return (mq->mq_curmsgs < mq->mq_maxmsg);
2530 static struct fileops mqueueops = {
2531 .fo_read = mqf_read,
2532 .fo_write = mqf_write,
2533 .fo_truncate = mqf_truncate,
2534 .fo_ioctl = mqf_ioctl,
2535 .fo_poll = mqf_poll,
2536 .fo_kqfilter = mqf_kqfilter,
2537 .fo_stat = mqf_stat,
2538 .fo_close = mqf_close
2541 static struct vop_vector mqfs_vnodeops = {
2542 .vop_default = &default_vnodeops,
2543 .vop_access = mqfs_access,
2544 .vop_cachedlookup = mqfs_lookup,
2545 .vop_lookup = vfs_cache_lookup,
2546 .vop_reclaim = mqfs_reclaim,
2547 .vop_create = mqfs_create,
2548 .vop_remove = mqfs_remove,
2549 .vop_inactive = mqfs_inactive,
2550 .vop_open = mqfs_open,
2551 .vop_close = mqfs_close,
2552 .vop_getattr = mqfs_getattr,
2553 .vop_setattr = mqfs_setattr,
2554 .vop_read = mqfs_read,
2555 .vop_write = VOP_EOPNOTSUPP,
2556 .vop_readdir = mqfs_readdir,
2557 .vop_mkdir = VOP_EOPNOTSUPP,
2558 .vop_rmdir = VOP_EOPNOTSUPP
2561 static struct vfsops mqfs_vfsops = {
2562 .vfs_init = mqfs_init,
2563 .vfs_uninit = mqfs_uninit,
2564 .vfs_mount = mqfs_mount,
2565 .vfs_unmount = mqfs_unmount,
2566 .vfs_root = mqfs_root,
2567 .vfs_statfs = mqfs_statfs,
2570 static struct vfsconf mqueuefs_vfsconf = {
2571 .vfc_version = VFS_VERSION,
2572 .vfc_name = "mqueuefs",
2573 .vfc_vfsops = &mqfs_vfsops,
2575 .vfc_flags = VFCF_SYNTHETIC
2578 static struct syscall_helper_data mq_syscalls[] = {
2579 SYSCALL_INIT_HELPER(kmq_open),
2580 SYSCALL_INIT_HELPER(kmq_setattr),
2581 SYSCALL_INIT_HELPER(kmq_timedsend),
2582 SYSCALL_INIT_HELPER(kmq_timedreceive),
2583 SYSCALL_INIT_HELPER(kmq_notify),
2584 SYSCALL_INIT_HELPER(kmq_unlink),
2588 #ifdef COMPAT_FREEBSD32
2589 #include <compat/freebsd32/freebsd32.h>
2590 #include <compat/freebsd32/freebsd32_proto.h>
2591 #include <compat/freebsd32/freebsd32_syscall.h>
2592 #include <compat/freebsd32/freebsd32_util.h>
2595 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2598 to->mq_flags = from->mq_flags;
2599 to->mq_maxmsg = from->mq_maxmsg;
2600 to->mq_msgsize = from->mq_msgsize;
2601 to->mq_curmsgs = from->mq_curmsgs;
2605 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2608 to->mq_flags = from->mq_flags;
2609 to->mq_maxmsg = from->mq_maxmsg;
2610 to->mq_msgsize = from->mq_msgsize;
2611 to->mq_curmsgs = from->mq_curmsgs;
2615 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2617 struct mq_attr attr;
2618 struct mq_attr32 attr32;
2621 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
2623 flags = FFLAGS(uap->flags);
2624 if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2625 error = copyin(uap->attr, &attr32, sizeof(attr32));
2628 mq_attr_from32(&attr32, &attr);
2630 return (kern_kmq_open(td, uap->path, flags, uap->mode,
2631 uap->attr != NULL ? &attr : NULL));
2635 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2637 struct mq_attr attr, oattr;
2638 struct mq_attr32 attr32, oattr32;
2641 if (uap->attr != NULL) {
2642 error = copyin(uap->attr, &attr32, sizeof(attr32));
2645 mq_attr_from32(&attr32, &attr);
2647 error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2651 if (uap->oattr != NULL) {
2652 mq_attr_to32(&oattr, &oattr32);
2653 error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2659 freebsd32_kmq_timedsend(struct thread *td,
2660 struct freebsd32_kmq_timedsend_args *uap)
2664 struct timespec32 ets32;
2665 struct timespec *abs_timeout, ets;
2669 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2672 if (uap->abs_timeout != NULL) {
2673 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2676 CP(ets32, ets, tv_sec);
2677 CP(ets32, ets, tv_nsec);
2681 waitok = !(fp->f_flag & O_NONBLOCK);
2682 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2683 uap->msg_prio, waitok, abs_timeout);
2689 freebsd32_kmq_timedreceive(struct thread *td,
2690 struct freebsd32_kmq_timedreceive_args *uap)
2694 struct timespec32 ets32;
2695 struct timespec *abs_timeout, ets;
2698 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2701 if (uap->abs_timeout != NULL) {
2702 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2705 CP(ets32, ets, tv_sec);
2706 CP(ets32, ets, tv_nsec);
2710 waitok = !(fp->f_flag & O_NONBLOCK);
2711 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2712 uap->msg_prio, waitok, abs_timeout);
2717 static struct syscall_helper_data mq32_syscalls[] = {
2718 SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2719 SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
2720 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
2721 SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
2722 SYSCALL32_INIT_HELPER(kmq_notify),
2723 SYSCALL32_INIT_HELPER(kmq_unlink),
2733 error = syscall_helper_register(mq_syscalls);
2736 #ifdef COMPAT_FREEBSD32
2737 error = syscall32_helper_register(mq32_syscalls);
2748 #ifdef COMPAT_FREEBSD32
2749 syscall32_helper_unregister(mq32_syscalls);
2751 syscall_helper_unregister(mq_syscalls);
2756 mq_modload(struct module *module, int cmd, void *arg)
2760 error = vfs_modevent(module, cmd, arg);
2779 static moduledata_t mqueuefs_mod = {
2784 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2785 MODULE_VERSION(mqueuefs, 1);