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 <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/limits.h>
53 #include <sys/dirent.h>
54 #include <sys/event.h>
55 #include <sys/eventhandler.h>
56 #include <sys/fcntl.h>
58 #include <sys/filedesc.h>
59 #include <sys/limits.h>
61 #include <sys/malloc.h>
62 #include <sys/module.h>
63 #include <sys/mount.h>
64 #include <sys/mqueue.h>
65 #include <sys/mutex.h>
66 #include <sys/namei.h>
69 #include <sys/queue.h>
70 #include <sys/sysproto.h>
72 #include <sys/syscall.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysent.h>
76 #include <sys/sysctl.h>
77 #include <sys/taskqueue.h>
78 #include <sys/unistd.h>
79 #include <sys/vnode.h>
80 #include <machine/atomic.h>
81 #include <posix4/posix4.h>
84 * Limits and constants
86 #define MQFS_NAMELEN NAME_MAX
87 #define MQFS_DELEN (8 + MQFS_NAMELEN)
103 * mqfs_info: describes a mqfs instance
107 struct mqfs_node *mi_root;
108 struct unrhdr *mi_unrhdr;
112 LIST_ENTRY(mqfs_vdata) mv_link;
113 struct mqfs_node *mv_node;
114 struct vnode *mv_vnode;
119 * mqfs_node: describes a node (file or directory) within a mqfs
122 char mn_name[MQFS_NAMELEN+1];
123 struct mqfs_info *mn_info;
124 struct mqfs_node *mn_parent;
125 LIST_HEAD(,mqfs_node) mn_children;
126 LIST_ENTRY(mqfs_node) mn_sibling;
127 LIST_HEAD(,mqfs_vdata) mn_vnodes;
133 struct timespec mn_birth;
134 struct timespec mn_ctime;
135 struct timespec mn_atime;
136 struct timespec mn_mtime;
142 #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
143 #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
144 #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
145 #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
146 (fp)->f_data)->mn_data))
148 TAILQ_HEAD(msgq, mqueue_msg);
152 struct mqueue_notifier {
153 LIST_ENTRY(mqueue_notifier) nt_link;
154 struct sigevent nt_sigev;
156 struct proc *nt_proc;
169 struct selinfo mq_rsel;
170 struct selinfo mq_wsel;
171 struct mqueue_notifier *mq_notifier;
178 TAILQ_ENTRY(mqueue_msg) msg_link;
179 unsigned int msg_prio;
180 unsigned int msg_size;
181 /* following real data... */
184 SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
185 "POSIX real time message queue");
187 static int default_maxmsg = 10;
188 static int default_msgsize = 1024;
190 static int maxmsg = 100;
191 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
192 &maxmsg, 0, "Default maximum messages in queue");
193 static int maxmsgsize = 16384;
194 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
195 &maxmsgsize, 0, "Default maximum message size");
196 static int maxmq = 100;
197 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
198 &maxmq, 0, "maximum message queues");
199 static int curmq = 0;
200 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
201 &curmq, 0, "current message queue number");
202 static int unloadable = 0;
203 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
205 static eventhandler_tag exit_tag;
207 /* Only one instance per-system */
208 static struct mqfs_info mqfs_data;
209 static uma_zone_t mqnode_zone;
210 static uma_zone_t mqueue_zone;
211 static uma_zone_t mvdata_zone;
212 static uma_zone_t mqnoti_zone;
213 static struct vop_vector mqfs_vnodeops;
214 static struct fileops mqueueops;
217 * Directory structure construction and manipulation
220 static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
221 const char *name, int namelen);
222 static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
223 const char *name, int namelen);
226 static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
227 const char *name, int namelen);
228 static int mqfs_destroy(struct mqfs_node *mn);
229 static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
230 static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
231 static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
234 * Message queue construction and maniplation
236 static struct mqueue *mqueue_alloc(const struct mq_attr *attr);
237 static void mqueue_free(struct mqueue *mq);
238 static int mqueue_send(struct mqueue *mq, const char *msg_ptr,
239 size_t msg_len, unsigned msg_prio, int waitok,
240 const struct timespec *abs_timeout);
241 static int mqueue_receive(struct mqueue *mq, char *msg_ptr,
242 size_t msg_len, unsigned *msg_prio, int waitok,
243 const struct timespec *abs_timeout);
244 static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
246 static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
248 static void mqueue_send_notification(struct mqueue *mq);
249 static void mqueue_fdclose(struct thread *td, int fd, struct file *fp);
250 static void mq_proc_exit(void *arg, struct proc *p);
255 static void filt_mqdetach(struct knote *kn);
256 static int filt_mqread(struct knote *kn, long hint);
257 static int filt_mqwrite(struct knote *kn, long hint);
259 struct filterops mq_rfiltops =
260 { 1, NULL, filt_mqdetach, filt_mqread };
261 struct filterops mq_wfiltops =
262 { 1, NULL, filt_mqdetach, filt_mqwrite };
265 * Initialize fileno bitmap
268 mqfs_fileno_init(struct mqfs_info *mi)
272 up = new_unrhdr(1, INT_MAX, NULL);
277 * Tear down fileno bitmap
280 mqfs_fileno_uninit(struct mqfs_info *mi)
285 mi->mi_unrhdr = NULL;
290 * Allocate a file number
293 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
295 /* make sure our parent has a file number */
296 if (mn->mn_parent && !mn->mn_parent->mn_fileno)
297 mqfs_fileno_alloc(mi, mn->mn_parent);
299 switch (mn->mn_type) {
303 case mqfstype_symlink:
304 mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
307 KASSERT(mn->mn_parent != NULL,
308 ("mqfstype_this node has no parent"));
309 mn->mn_fileno = mn->mn_parent->mn_fileno;
311 case mqfstype_parent:
312 KASSERT(mn->mn_parent != NULL,
313 ("mqfstype_parent node has no parent"));
314 if (mn->mn_parent == mi->mi_root) {
315 mn->mn_fileno = mn->mn_parent->mn_fileno;
318 KASSERT(mn->mn_parent->mn_parent != NULL,
319 ("mqfstype_parent node has no grandparent"));
320 mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
324 ("mqfs_fileno_alloc() called for unknown type node: %d",
331 * Release a file number
334 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
336 switch (mn->mn_type) {
340 case mqfstype_symlink:
341 free_unr(mi->mi_unrhdr, mn->mn_fileno);
344 case mqfstype_parent:
345 /* ignore these, as they don't "own" their file number */
349 ("mqfs_fileno_free() called for unknown type node: %d",
355 static __inline struct mqfs_node *
358 return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
362 mqnode_free(struct mqfs_node *node)
364 uma_zfree(mqnode_zone, node);
368 mqnode_addref(struct mqfs_node *node)
370 atomic_fetchadd_int(&node->mn_refcount, 1);
374 mqnode_release(struct mqfs_node *node)
378 old = atomic_fetchadd_int(&node->mn_refcount, -1);
379 if (node->mn_type == mqfstype_dir ||
380 node->mn_type == mqfstype_root)
381 exp = 3; /* include . and .. */
389 * Add a node to a directory
392 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
394 KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
395 KASSERT(parent->mn_info != NULL,
396 ("%s(): parent has no mn_info", __func__));
397 KASSERT(parent->mn_type == mqfstype_dir ||
398 parent->mn_type == mqfstype_root,
399 ("%s(): parent is not a directory", __func__));
401 node->mn_info = parent->mn_info;
402 node->mn_parent = parent;
403 LIST_INIT(&node->mn_children);
404 LIST_INIT(&node->mn_vnodes);
405 LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
406 mqnode_addref(parent);
411 * Add . and .. to a directory
414 mqfs_fixup_dir(struct mqfs_node *parent)
416 struct mqfs_node *dir;
418 dir = mqnode_alloc();
419 dir->mn_name[0] = '.';
420 dir->mn_type = mqfstype_this;
421 dir->mn_refcount = 1;
422 if (mqfs_add_node(parent, dir) != 0) {
427 dir = mqnode_alloc();
428 dir->mn_name[0] = dir->mn_name[1] = '.';
429 dir->mn_type = mqfstype_parent;
430 dir->mn_refcount = 1;
432 if (mqfs_add_node(parent, dir) != 0) {
445 static struct mqfs_node *
446 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen)
448 struct mqfs_node *dir;
450 dir = mqnode_alloc();
451 strncpy(dir->mn_name, name, namelen);
452 dir->mn_type = mqfstype_dir;
453 dir->mn_refcount = 1;
454 if (mqfs_add_node(parent, dir) != 0) {
459 if (mqfs_fixup_dir(dir) != 0) {
470 static struct mqfs_node *
471 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen)
473 struct mqfs_node *node;
475 node = mqfs_create_file(parent, name, namelen);
478 node->mn_type = mqfstype_symlink;
487 static struct mqfs_node *
488 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen)
490 struct mqfs_node *node;
492 node = mqnode_alloc();
493 strncpy(node->mn_name, name, namelen);
494 node->mn_type = mqfstype_file;
495 node->mn_refcount = 1;
497 if (mqfs_add_node(parent, node) != 0) {
505 * Destroy a node or a tree of nodes
508 mqfs_destroy(struct mqfs_node *node)
510 struct mqfs_node *parent;
512 KASSERT(node != NULL,
513 ("%s(): node is NULL", __func__));
514 KASSERT(node->mn_info != NULL,
515 ("%s(): node has no mn_info", __func__));
517 /* destroy children */
518 if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
519 while (! LIST_EMPTY(&node->mn_children))
520 mqfs_destroy(LIST_FIRST(&node->mn_children));
522 /* unlink from parent */
523 if ((parent = node->mn_parent) != NULL) {
524 KASSERT(parent->mn_info == node->mn_info,
525 ("%s(): parent has different mn_info", __func__));
526 LIST_REMOVE(node, mn_sibling);
529 if (node->mn_fileno != 0)
530 mqfs_fileno_free(node->mn_info, node);
531 if (node->mn_data != NULL)
532 mqueue_free(node->mn_data);
538 * Mount a mqfs instance
541 mqfs_mount(struct mount *mp, struct thread *td)
545 if (mp->mnt_flag & MNT_UPDATE)
548 mp->mnt_data = &mqfs_data;
549 mp->mnt_flag |= MNT_LOCAL;
550 mp->mnt_kern_flag |= MNTK_MPSAFE;
554 vfs_mountedfrom(mp, "mqueue");
555 sbp->f_bsize = PAGE_SIZE;
556 sbp->f_iosize = PAGE_SIZE;
566 * Unmount a mqfs instance
569 mqfs_unmount(struct mount *mp, int mntflags, struct thread *td)
573 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, td);
578 * Return a root vnode
581 mqfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
583 struct mqfs_info *mqfs;
586 mqfs = VFSTOMQFS(mp);
587 sx_xlock(&mqfs->mi_lock);
588 ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
589 sx_xunlock(&mqfs->mi_lock);
594 * Return filesystem stats
597 mqfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
599 /* XXX update statistics */
604 * Initialize a mqfs instance
607 mqfs_init(struct vfsconf *vfc)
609 struct mqfs_node *root;
610 struct mqfs_info *mi;
612 mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
613 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
614 mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
615 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
616 mvdata_zone = uma_zcreate("mvdata",
617 sizeof(struct mqfs_vdata), NULL, NULL, NULL,
618 NULL, UMA_ALIGN_PTR, 0);
619 mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
620 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
622 sx_init(&mi->mi_lock, "mqfs lock");
623 /* set up the root diretory */
624 root = mqnode_alloc();
625 root->mn_type = mqfstype_root;
626 root->mn_refcount = 1;
627 root->mn_name[0] = '/';
629 LIST_INIT(&root->mn_children);
630 LIST_INIT(&root->mn_vnodes);
631 root->mn_mode = 01777;
633 mqfs_fileno_init(mi);
634 mqfs_fileno_alloc(mi, root);
635 mqfs_fixup_dir(root);
636 exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
637 EVENTHANDLER_PRI_ANY);
638 mq_fdclose = mqueue_fdclose;
639 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
644 * Destroy a mqfs instance
647 mqfs_uninit(struct vfsconf *vfc)
649 struct mqfs_info *mi;
653 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
655 mqfs_destroy(mi->mi_root);
657 mqfs_fileno_uninit(mi);
658 sx_destroy(&mi->mi_lock);
659 uma_zdestroy(mqnode_zone);
660 uma_zdestroy(mqueue_zone);
661 uma_zdestroy(mvdata_zone);
662 uma_zdestroy(mqnoti_zone);
670 do_recycle(void *context, int pending __unused)
672 struct vnode *vp = (struct vnode *)context;
674 vrecycle(vp, curthread);
682 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
684 struct mqfs_vdata *vd;
687 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
688 if (vd->mv_vnode->v_mount == mp)
693 if (vget(vd->mv_vnode, 0, curthread) == 0) {
695 vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE,
699 /* XXX if this can happen, we're in trouble */
702 error = getnewvnode("mqueue", mp, &mqfs_vnodeops, vpp);
705 vd = uma_zalloc(mvdata_zone, M_WAITOK);
709 TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
710 LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
712 switch (pn->mn_type) {
714 (*vpp)->v_vflag = VV_ROOT;
718 case mqfstype_parent:
719 (*vpp)->v_type = VDIR;
722 (*vpp)->v_type = VREG;
724 case mqfstype_symlink:
725 (*vpp)->v_type = VLNK;
728 KASSERT(0, ("mqfs_allocf called for null node\n"));
730 panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
732 vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
737 * Search a directory entry
739 static struct mqfs_node *
740 mqfs_search(struct mqfs_node *pd, const char *name, int len)
742 struct mqfs_node *pn;
744 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
745 if (strncmp(pn->mn_name, name, len) == 0)
752 * Look up a file or directory
755 mqfs_lookupx(struct vop_cachedlookup_args *ap)
757 struct componentname *cnp;
758 struct vnode *dvp, **vpp;
759 struct mqfs_node *pd;
760 struct mqfs_node *pn;
761 int nameiop, flags, error, namelen;
768 pname = cnp->cn_nameptr;
769 namelen = cnp->cn_namelen;
771 flags = cnp->cn_flags;
772 nameiop = cnp->cn_nameiop;
777 if (dvp->v_type != VDIR)
780 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
784 /* shortcut: check if the name is too long */
785 if (cnp->cn_namelen >= MQFS_NAMELEN)
789 if (namelen == 1 && pname[0] == '.') {
790 if ((flags & ISLASTCN) && nameiop != LOOKUP)
799 if (cnp->cn_flags & ISDOTDOT) {
800 if (dvp->v_vflag & VV_ROOT)
802 if ((flags & ISLASTCN) && nameiop != LOOKUP)
804 VOP_UNLOCK(dvp, 0, cnp->cn_thread);
805 KASSERT(pd->mn_parent, ("non-root directory has no parent"));
807 error = mqfs_allocv(dvp->v_mount, vpp, pn);
808 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
813 pn = mqfs_search(pd, pname, namelen);
818 if (nameiop == DELETE && (flags & ISLASTCN)) {
819 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
830 error = mqfs_allocv(dvp->v_mount, vpp, pn);
831 if (error == 0 && cnp->cn_flags & MAKEENTRY)
832 cache_enter(dvp, *vpp, cnp);
838 /* will create a new entry in the directory ? */
839 if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
840 && (flags & ISLASTCN)) {
841 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
844 cnp->cn_flags |= SAVENAME;
845 return (EJUSTRETURN);
851 struct vop_lookup_args {
852 struct vop_generic_args a_gen;
854 struct vnode **a_vpp;
855 struct componentname *a_cnp;
860 * vnode lookup operation
863 mqfs_lookup(struct vop_cachedlookup_args *ap)
865 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
868 sx_xlock(&mqfs->mi_lock);
869 rc = mqfs_lookupx(ap);
870 sx_xunlock(&mqfs->mi_lock);
875 struct vop_create_args {
877 struct vnode **a_vpp;
878 struct componentname *a_cnp;
884 * vnode creation operation
887 mqfs_create(struct vop_create_args *ap)
889 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
890 struct componentname *cnp = ap->a_cnp;
891 struct mqfs_node *pd;
892 struct mqfs_node *pn;
896 pd = VTON(ap->a_dvp);
897 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
899 mq = mqueue_alloc(NULL);
902 sx_xlock(&mqfs->mi_lock);
905 pn = mqfs_search(pd, cnp->cn_nameptr, cnp->cn_namelen);
908 sx_xunlock(&mqfs->mi_lock);
912 if ((cnp->cn_flags & HASBUF) == 0)
913 panic("%s: no name", __func__);
915 pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen);
916 pn->mn_mode = ap->a_vap->va_mode;
917 pn->mn_uid = cnp->cn_cred->cr_uid;
918 pn->mn_gid = cnp->cn_cred->cr_gid;
920 getnanotime(&pn->mn_birth);
921 pn->mn_ctime = pn->mn_atime = pn->mn_mtime = pn->mn_birth;
923 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
924 sx_xunlock(&mqfs->mi_lock);
932 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
934 struct mqfs_node *parent;
935 struct mqfs_vdata *vd;
938 sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
940 if (ucred->cr_uid != pn->mn_uid &&
941 (error = suser_cred(ucred, 0)) != 0)
943 else if (!pn->mn_deleted) {
944 parent = pn->mn_parent;
945 pn->mn_parent = NULL;
947 LIST_REMOVE(pn, mn_sibling);
948 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
949 cache_purge(vd->mv_vnode);
951 taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
954 mqnode_release(parent);
961 struct vop_remove_args {
964 struct componentname *a_cnp;
969 * vnode removal operation
972 mqfs_remove(struct vop_remove_args *ap)
974 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
975 struct mqfs_node *pn;
978 if (ap->a_vp->v_type == VDIR)
981 sx_xlock(&mqfs->mi_lock);
982 error = do_unlink(pn, ap->a_cnp->cn_cred);
983 sx_xunlock(&mqfs->mi_lock);
988 struct vop_inactive_args {
995 mqfs_inactive(struct vop_inactive_args *ap)
997 struct mqfs_node *pn = VTON(ap->a_vp);
1000 vrecycle(ap->a_vp, ap->a_td);
1005 struct vop_reclaim_args {
1006 struct vop_generic_args a_gen;
1008 struct thread *a_td;
1013 mqfs_reclaim(struct vop_reclaim_args *ap)
1015 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1016 struct vnode *vp = ap->a_vp;
1017 struct mqfs_node *pn;
1018 struct mqfs_vdata *vd;
1022 sx_xlock(&mqfs->mi_lock);
1024 LIST_REMOVE(vd, mv_link);
1025 uma_zfree(mvdata_zone, vd);
1027 sx_xunlock(&mqfs->mi_lock);
1032 struct vop_open_args {
1033 struct vop_generic_args a_gen;
1036 struct ucred *a_cred;
1037 struct thread *a_td;
1043 mqfs_open(struct vop_open_args *ap)
1049 struct vop_close_args {
1050 struct vop_generic_args a_gen;
1053 struct ucred *a_cred;
1054 struct thread *a_td;
1059 mqfs_close(struct vop_close_args *ap)
1065 struct vop_access_args {
1066 struct vop_generic_args a_gen;
1069 struct ucred *a_cred;
1070 struct thread *a_td;
1075 * Verify permissions
1078 mqfs_access(struct vop_access_args *ap)
1080 struct vnode *vp = ap->a_vp;
1084 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
1087 error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
1088 vattr.va_gid, ap->a_mode, ap->a_cred, NULL);
1093 struct vop_getattr_args {
1094 struct vop_generic_args a_gen;
1096 struct vattr *a_vap;
1097 struct ucred *a_cred;
1098 struct thread *a_td;
1103 * Get file attributes
1106 mqfs_getattr(struct vop_getattr_args *ap)
1108 struct vnode *vp = ap->a_vp;
1109 struct mqfs_node *pn = VTON(vp);
1110 struct vattr *vap = ap->a_vap;
1114 vap->va_type = vp->v_type;
1115 vap->va_mode = pn->mn_mode;
1117 vap->va_uid = pn->mn_uid;
1118 vap->va_gid = pn->mn_gid;
1119 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1120 vap->va_fileid = pn->mn_fileno;
1122 vap->va_blocksize = PAGE_SIZE;
1123 vap->va_bytes = vap->va_size = 0;
1124 vap->va_atime = pn->mn_atime;
1125 vap->va_mtime = pn->mn_mtime;
1126 vap->va_ctime = pn->mn_ctime;
1127 vap->va_birthtime = pn->mn_birth;
1132 vap->va_filerev = 0;
1133 vap->va_vaflags = 0;
1138 struct vop_setattr_args {
1139 struct vop_generic_args a_gen;
1141 struct vattr *a_vap;
1142 struct ucred *a_cred;
1143 struct thread *a_td;
1150 mqfs_setattr(struct vop_setattr_args *ap)
1152 struct mqfs_node *pn;
1161 if ((vap->va_type != VNON) ||
1162 (vap->va_nlink != VNOVAL) ||
1163 (vap->va_fsid != VNOVAL) ||
1164 (vap->va_fileid != VNOVAL) ||
1165 (vap->va_blocksize != VNOVAL) ||
1166 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1167 (vap->va_rdev != VNOVAL) ||
1168 ((int)vap->va_bytes != VNOVAL) ||
1169 (vap->va_gen != VNOVAL)) {
1176 if (vap->va_uid == (uid_t)VNOVAL)
1180 if (vap->va_gid == (gid_t)VNOVAL)
1185 if (uid != pn->mn_uid || gid != pn->mn_gid) {
1187 * To modify the ownership of a file, must possess VADMIN
1190 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)))
1192 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1193 (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1194 (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL))
1202 if (vap->va_mode != (mode_t)VNOVAL) {
1203 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1204 (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)))
1206 pn->mn_mode = vap->va_mode;
1210 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1211 /* See the comment in ufs_vnops::ufs_setattr(). */
1212 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) &&
1213 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1214 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td))))
1216 if (vap->va_atime.tv_sec != VNOVAL) {
1217 pn->mn_atime = vap->va_atime;
1219 if (vap->va_mtime.tv_sec != VNOVAL) {
1220 pn->mn_mtime = vap->va_mtime;
1225 vfs_timestamp(&pn->mn_ctime);
1231 struct vop_read_args {
1232 struct vop_generic_args a_gen;
1236 struct ucred *a_cred;
1244 mqfs_read(struct vop_read_args *ap)
1247 struct vnode *vp = ap->a_vp;
1248 struct uio *uio = ap->a_uio;
1249 struct mqfs_node *pn;
1253 if (vp->v_type != VREG)
1258 snprintf(buf, sizeof(buf),
1259 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1264 buf[sizeof(buf)-1] = '\0';
1266 error = uiomove_frombuf(buf, len, uio);
1271 struct vop_readdir_args {
1272 struct vop_generic_args a_gen;
1275 struct ucred *a_cred;
1283 * Return directory entries.
1286 mqfs_readdir(struct vop_readdir_args *ap)
1289 struct mqfs_info *mi;
1290 struct mqfs_node *pd;
1291 struct mqfs_node *pn;
1292 struct dirent entry;
1294 int *tmp_ncookies = NULL;
1299 mi = VFSTOMQFS(vp->v_mount);
1303 if (vp->v_type != VDIR)
1306 if (uio->uio_offset < 0)
1309 if (ap->a_ncookies != NULL) {
1310 tmp_ncookies = ap->a_ncookies;
1311 *ap->a_ncookies = 0;
1312 ap->a_ncookies = NULL;
1318 sx_xlock(&mi->mi_lock);
1320 LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1321 entry.d_reclen = sizeof(entry);
1323 mqfs_fileno_alloc(mi, pn);
1324 entry.d_fileno = pn->mn_fileno;
1325 for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1326 entry.d_name[i] = pn->mn_name[i];
1327 entry.d_name[i] = 0;
1329 switch (pn->mn_type) {
1333 case mqfstype_parent:
1334 entry.d_type = DT_DIR;
1337 entry.d_type = DT_REG;
1339 case mqfstype_symlink:
1340 entry.d_type = DT_LNK;
1343 panic("%s has unexpected node type: %d", pn->mn_name,
1346 if (entry.d_reclen > uio->uio_resid)
1348 if (offset >= uio->uio_offset) {
1349 error = vfs_read_dirent(ap, &entry, offset);
1353 offset += entry.d_reclen;
1355 sx_xunlock(&mi->mi_lock);
1357 uio->uio_offset = offset;
1359 if (tmp_ncookies != NULL)
1360 ap->a_ncookies = tmp_ncookies;
1368 struct vop_mkdir_args {
1369 struct vnode *a_dvp;
1370 struvt vnode **a_vpp;
1371 struvt componentname *a_cnp;
1372 struct vattr *a_vap;
1377 * Create a directory.
1380 mqfs_mkdir(struct vop_mkdir_args *ap)
1382 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1383 struct componentname *cnp = ap->a_cnp;
1384 struct mqfs_node *pd = VTON(ap->a_dvp);
1385 struct mqfs_node *pn;
1388 if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1390 sx_xlock(&mqfs->mi_lock);
1393 pn = mqfs_search(pd, cnp->cn_nameptr, cnp->cn_namelen);
1395 sx_xunlock(&mqfs->mi_lock);
1399 if ((cnp->cn_flags & HASBUF) == 0)
1400 panic("%s: no name", __func__);
1402 pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen);
1403 pn->mn_mode = ap->a_vap->va_mode;
1404 pn->mn_uid = cnp->cn_cred->cr_uid;
1405 pn->mn_gid = cnp->cn_cred->cr_gid;
1406 getnanotime(&pn->mn_birth);
1407 pn->mn_ctime = pn->mn_atime = pn->mn_mtime = pn->mn_birth;
1408 /* node attribute */
1409 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1410 sx_xunlock(&mqfs->mi_lock);
1415 struct vop_rmdir_args {
1416 struct vnode *a_dvp;
1418 struct componentname *a_cnp;
1423 * Remove a directory.
1426 mqfs_rmdir(struct vop_rmdir_args *ap)
1428 struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1429 struct mqfs_node *pn = VTON(ap->a_vp);
1430 struct mqfs_node *pt;
1432 if (pn->mn_type != mqfstype_dir)
1435 sx_xlock(&mqfs->mi_lock);
1436 if (pn->mn_deleted) {
1437 sx_xunlock(&mqfs->mi_lock);
1441 pt = LIST_FIRST(&pn->mn_children);
1442 pt = LIST_NEXT(pt, mn_sibling);
1443 pt = LIST_NEXT(pt, mn_sibling);
1445 sx_xunlock(&mqfs->mi_lock);
1449 pn->mn_parent = NULL;
1451 LIST_REMOVE(pn, mn_sibling);
1454 sx_xunlock(&mqfs->mi_lock);
1455 cache_purge(ap->a_vp);
1462 * Allocate a message queue
1464 static struct mqueue *
1465 mqueue_alloc(const struct mq_attr *attr)
1471 mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1472 TAILQ_INIT(&mq->mq_msgq);
1474 mq->mq_maxmsg = attr->mq_maxmsg;
1475 mq->mq_msgsize = attr->mq_msgsize;
1477 mq->mq_maxmsg = default_maxmsg;
1478 mq->mq_msgsize = default_msgsize;
1480 mtx_init(&mq->mq_mutex, "mqueue", NULL, MTX_DEF);
1481 knlist_init(&mq->mq_rsel.si_note, &mq->mq_mutex, NULL, NULL, NULL);
1482 knlist_init(&mq->mq_wsel.si_note, &mq->mq_mutex, NULL, NULL, NULL);
1483 atomic_add_int(&curmq, 1);
1488 * Destroy a message queue
1491 mqueue_free(struct mqueue *mq)
1493 struct mqueue_msg *msg;
1495 while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1496 TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1497 FREE(msg, M_MQUEUEDATA);
1500 mtx_destroy(&mq->mq_mutex);
1501 knlist_destroy(&mq->mq_rsel.si_note);
1502 knlist_destroy(&mq->mq_wsel.si_note);
1503 uma_zfree(mqueue_zone, mq);
1504 atomic_add_int(&curmq, -1);
1508 * Load a message from user space
1510 static struct mqueue_msg *
1511 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1513 struct mqueue_msg *msg;
1517 len = sizeof(struct mqueue_msg) + msg_size;
1518 MALLOC(msg, struct mqueue_msg *, len, M_MQUEUEDATA, M_WAITOK);
1519 error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1522 FREE(msg, M_MQUEUEDATA);
1525 msg->msg_size = msg_size;
1526 msg->msg_prio = msg_prio;
1532 * Save a message to user space
1535 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1539 error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1541 if (error == 0 && msg_prio != NULL)
1542 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1547 * Free a message's memory
1549 static __inline void
1550 mqueue_freemsg(struct mqueue_msg *msg)
1552 FREE(msg, M_MQUEUEDATA);
1556 * Send a message. if waitok is false, thread will not be
1557 * blocked if there is no data in queue, otherwise, absolute
1558 * time will be checked.
1561 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1562 size_t msg_len, unsigned msg_prio, int waitok,
1563 const struct timespec *abs_timeout)
1565 struct mqueue_msg *msg;
1566 struct timespec ets, ts, ts2;
1570 if (msg_prio >= MQ_PRIO_MAX)
1572 if (msg_len > mq->mq_msgsize)
1574 msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1578 /* O_NONBLOCK case */
1580 error = _mqueue_send(mq, msg, -1);
1586 /* we allow a null timeout (wait forever) */
1587 if (abs_timeout == NULL) {
1588 error = _mqueue_send(mq, msg, 0);
1594 /* send it before checking time */
1595 error = _mqueue_send(mq, msg, -1);
1599 if (error != EAGAIN)
1602 error = copyin(abs_timeout, &ets, sizeof(ets));
1605 if (ets.tv_nsec >= 1000000000 || ets.tv_nsec < 0) {
1612 timespecsub(&ts2, &ts);
1613 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1617 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1618 error = _mqueue_send(mq, msg, tvtohz(&tv));
1619 if (error != ETIMEDOUT)
1625 mqueue_freemsg(msg);
1630 * Common routine to send a message
1633 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1635 struct mqueue_msg *msg2;
1638 mtx_lock(&mq->mq_mutex);
1639 while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1641 mtx_unlock(&mq->mq_mutex);
1645 error = msleep(&mq->mq_senders, &mq->mq_mutex,
1646 curthread->td_priority | PCATCH, "mqsend", timo);
1648 if (error == EAGAIN)
1651 if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1652 mtx_unlock(&mq->mq_mutex);
1656 if (TAILQ_EMPTY(&mq->mq_msgq)) {
1657 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1659 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1660 TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1662 TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1663 if (msg2->msg_prio < msg->msg_prio)
1666 TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1670 mq->mq_totalbytes += msg->msg_size;
1671 if (mq->mq_receivers)
1672 wakeup_one(&mq->mq_receivers);
1673 else if (mq->mq_notifier != NULL)
1674 mqueue_send_notification(mq);
1675 if (mq->mq_flags & MQ_RSEL) {
1676 mq->mq_flags &= ~MQ_RSEL;
1677 selwakeup(&mq->mq_rsel);
1679 KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1680 mtx_unlock(&mq->mq_mutex);
1685 * Send realtime a signal to process which registered itself
1686 * successfully by mq_notify.
1689 mqueue_send_notification(struct mqueue *mq)
1691 struct mqueue_notifier *nt;
1694 mtx_assert(&mq->mq_mutex, MA_OWNED);
1695 nt = mq->mq_notifier;
1696 if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1699 if (!KSI_ONQ(&nt->nt_ksi))
1700 psignal_event(p, &nt->nt_sigev, &nt->nt_ksi);
1703 mq->mq_notifier = NULL;
1707 * Get a message. if waitok is false, thread will not be
1708 * blocked if there is no data in queue, otherwise, absolute
1709 * time will be checked.
1712 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1713 size_t msg_len, unsigned *msg_prio, int waitok,
1714 const struct timespec *abs_timeout)
1716 struct mqueue_msg *msg;
1717 struct timespec ets, ts, ts2;
1721 if (msg_len < mq->mq_msgsize)
1724 /* O_NONBLOCK case */
1726 error = _mqueue_recv(mq, &msg, -1);
1732 /* we allow a null timeout (wait forever). */
1733 if (abs_timeout == NULL) {
1734 error = _mqueue_recv(mq, &msg, 0);
1740 /* try to get a message before checking time */
1741 error = _mqueue_recv(mq, &msg, -1);
1745 if (error != EAGAIN)
1748 error = copyin(abs_timeout, &ets, sizeof(ets));
1751 if (ets.tv_nsec >= 1000000000 || ets.tv_nsec < 0) {
1759 timespecsub(&ts2, &ts);
1760 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1764 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1765 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1768 if (error != ETIMEDOUT)
1773 error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1775 curthread->td_retval[0] = msg->msg_size;
1776 curthread->td_retval[1] = 0;
1778 mqueue_freemsg(msg);
1783 * Common routine to receive a message
1786 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1790 mtx_lock(&mq->mq_mutex);
1791 while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1793 mtx_unlock(&mq->mq_mutex);
1797 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1798 curthread->td_priority | PCATCH, "mqrecv", timo);
1800 if (error == EAGAIN)
1805 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1807 mq->mq_totalbytes -= (*msg)->msg_size;
1809 wakeup_one(&mq->mq_senders);
1810 if (mq->mq_flags & MQ_WSEL) {
1811 mq->mq_flags &= ~MQ_WSEL;
1812 selwakeup(&mq->mq_wsel);
1814 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1816 if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1817 !TAILQ_EMPTY(&mq->mq_msgq)) {
1818 mqueue_send_notification(mq);
1820 mtx_unlock(&mq->mq_mutex);
1824 static __inline struct mqueue_notifier *
1825 notifier_alloc(void)
1827 return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1830 static __inline void
1831 notifier_free(struct mqueue_notifier *p)
1833 uma_zfree(mqnoti_zone, p);
1836 static struct mqueue_notifier *
1837 notifier_search(struct proc *p, int fd)
1839 struct mqueue_notifier *nt;
1841 LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1842 if (nt->nt_ksi.ksi_mqd == fd)
1848 static __inline void
1849 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1851 LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1854 static __inline void
1855 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1857 LIST_REMOVE(nt, nt_link);
1862 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1864 struct mqueue_notifier *nt;
1866 mtx_assert(&mq->mq_mutex, MA_OWNED);
1868 nt = notifier_search(p, fd);
1870 if (mq->mq_notifier == nt)
1871 mq->mq_notifier = NULL;
1872 sigqueue_take(&nt->nt_ksi);
1873 notifier_delete(p, nt);
1879 * Syscall to open a message queue
1882 mq_open(struct thread *td, struct mq_open_args *uap)
1884 char path[MQFS_NAMELEN + 1];
1885 struct mq_attr attr, *pattr;
1886 struct mqfs_node *pn;
1887 struct filedesc *fdp;
1890 int fd, error, len, flags, cmode;
1892 if ((uap->flags & O_ACCMODE) == O_ACCMODE)
1895 fdp = td->td_proc->p_fd;
1896 flags = FFLAGS(uap->flags);
1897 cmode = (((uap->mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
1899 if ((flags & O_CREAT) && (uap->attr != NULL)) {
1900 error = copyin(uap->attr, &attr, sizeof(attr));
1903 if (attr.mq_maxmsg <= 0 || attr.mq_maxmsg > maxmsg)
1905 if (attr.mq_msgsize <= 0 || attr.mq_msgsize > maxmsgsize)
1911 error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
1916 * The first character of name must be a slash (/) character
1917 * and the remaining characters of name cannot include any slash
1921 if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL)
1924 error = falloc(td, &fp, &fd);
1928 sx_xlock(&mqfs_data.mi_lock);
1929 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
1931 if (!(flags & O_CREAT)) {
1934 mq = mqueue_alloc(pattr);
1938 pn = mqfs_create_file(mqfs_data.mi_root,
1949 getnanotime(&pn->mn_birth);
1950 pn->mn_ctime = pn->mn_atime = pn->mn_mtime
1952 pn->mn_uid = td->td_ucred->cr_uid;
1953 pn->mn_gid = td->td_ucred->cr_gid;
1954 pn->mn_mode = cmode;
1957 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
1966 error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
1967 pn->mn_gid, acc_mode, td->td_ucred, NULL);
1972 sx_xunlock(&mqfs_data.mi_lock);
1973 fdclose(fdp, fp, fd, td);
1979 sx_xunlock(&mqfs_data.mi_lock);
1982 fp->f_flag = (flags & (FREAD | FWRITE | O_NONBLOCK));
1983 fp->f_type = DTYPE_MQUEUE;
1984 fp->f_ops = &mqueueops;
1988 FILEDESC_LOCK_FAST(fdp);
1989 if (fdp->fd_ofiles[fd] == fp)
1990 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
1991 FILEDESC_UNLOCK_FAST(fdp);
1992 td->td_retval[0] = fd;
1998 * Syscall to unlink a message queue
2001 mq_unlink(struct thread *td, struct mq_unlink_args *uap)
2003 char path[MQFS_NAMELEN+1];
2004 struct mqfs_node *pn;
2007 error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2012 if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL)
2015 sx_xlock(&mqfs_data.mi_lock);
2016 pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
2018 error = do_unlink(pn, td->td_ucred);
2021 sx_xunlock(&mqfs_data.mi_lock);
2025 typedef int (*_fgetf)(struct thread *, int, struct file **);
2028 * Get message queue by giving file slot
2031 _getmq(struct thread *td, int fd, _fgetf func,
2032 struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2034 struct mqfs_node *pn;
2037 error = func(td, fd, fpp);
2040 if (&mqueueops != (*fpp)->f_ops) {
2044 pn = (*fpp)->f_data;
2053 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2054 struct mqueue **pmq)
2056 return _getmq(td, fd, fget, fpp, ppn, pmq);
2060 getmq_read(struct thread *td, int fd, struct file **fpp,
2061 struct mqfs_node **ppn, struct mqueue **pmq)
2063 return _getmq(td, fd, fget_read, fpp, ppn, pmq);
2067 getmq_write(struct thread *td, int fd, struct file **fpp,
2068 struct mqfs_node **ppn, struct mqueue **pmq)
2070 return _getmq(td, fd, fget_write, fpp, ppn, pmq);
2077 mq_setattr(struct thread *td, struct mq_setattr_args *uap)
2081 struct mq_attr attr, oattr;
2085 error = copyin(uap->attr, &attr, sizeof(attr));
2088 if (attr.mq_flags & ~O_NONBLOCK)
2091 error = getmq(td, uap->mqd, &fp, NULL, &mq);
2094 oattr.mq_maxmsg = mq->mq_maxmsg;
2095 oattr.mq_msgsize = mq->mq_msgsize;
2096 oattr.mq_curmsgs = mq->mq_curmsgs;
2098 oattr.mq_flags = (O_NONBLOCK & fp->f_flag);
2100 fp->f_flag &= ~O_NONBLOCK;
2101 fp->f_flag |= (attr.mq_flags & O_NONBLOCK);
2106 error = copyout(&oattr, uap->oattr, sizeof(oattr));
2114 mq_timedreceive(struct thread *td, struct mq_timedreceive_args *uap)
2121 error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2124 waitok = !(fp->f_flag & O_NONBLOCK);
2125 error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2126 uap->msg_prio, waitok, uap->abs_timeout);
2135 mq_timedsend(struct thread *td, struct mq_timedsend_args *uap)
2141 error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2144 waitok = !(fp->f_flag & O_NONBLOCK);
2145 error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2146 uap->msg_prio, waitok, uap->abs_timeout);
2155 mq_notify(struct thread *td, struct mq_notify_args *uap)
2158 struct filedesc *fdp;
2162 struct mqueue_notifier *nt, *newnt = NULL;
2166 fdp = td->td_proc->p_fd;
2168 error = copyin(uap->sigev, &ev, sizeof(ev));
2171 if (ev.sigev_notify != SIGEV_SIGNAL &&
2172 ev.sigev_notify != SIGEV_THREAD_ID &&
2173 ev.sigev_notify != SIGEV_NONE)
2175 if ((ev.sigev_notify == SIGEV_SIGNAL ||
2176 ev.sigev_notify == SIGEV_THREAD_ID) &&
2177 !_SIG_VALID(ev.sigev_signo))
2180 error = getmq(td, uap->mqd, &fp, NULL, &mq);
2184 FILEDESC_LOCK_FAST(fdp);
2185 if (fget_locked(fdp, uap->mqd) != fp) {
2186 FILEDESC_UNLOCK_FAST(fdp);
2190 mtx_lock(&mq->mq_mutex);
2191 FILEDESC_UNLOCK_FAST(fdp);
2192 if (uap->sigev != NULL) {
2193 if (mq->mq_notifier != NULL) {
2197 nt = notifier_search(p, uap->mqd);
2199 if (newnt == NULL) {
2201 mtx_unlock(&mq->mq_mutex);
2202 newnt = notifier_alloc();
2208 sigqueue_take(&nt->nt_ksi);
2209 if (newnt != NULL) {
2210 notifier_free(newnt);
2216 ksiginfo_init(&nt->nt_ksi);
2217 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2218 nt->nt_ksi.ksi_code = SI_MESGQ;
2220 nt->nt_ksi.ksi_mqd = uap->mqd;
2221 notifier_insert(p, nt);
2224 mq->mq_notifier = nt;
2227 * if there is no receivers and message queue
2228 * is not empty, we should send notification
2229 * as soon as possible.
2231 if (mq->mq_receivers == 0 &&
2232 !TAILQ_EMPTY(&mq->mq_msgq))
2233 mqueue_send_notification(mq);
2236 notifier_remove(p, mq, uap->mqd);
2238 mtx_unlock(&mq->mq_mutex);
2243 notifier_free(newnt);
2248 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2250 struct filedesc *fdp;
2253 fdp = td->td_proc->p_fd;
2254 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
2255 if (fp->f_ops == &mqueueops) {
2257 mtx_lock(&mq->mq_mutex);
2258 notifier_remove(td->td_proc, mq, fd);
2260 /* have to wakeup thread in same process */
2261 if (mq->mq_flags & MQ_RSEL) {
2262 mq->mq_flags &= ~MQ_RSEL;
2263 selwakeup(&mq->mq_rsel);
2265 if (mq->mq_flags & MQ_WSEL) {
2266 mq->mq_flags &= ~MQ_WSEL;
2267 selwakeup(&mq->mq_wsel);
2269 mtx_unlock(&mq->mq_mutex);
2274 mq_proc_exit(void *arg __unused, struct proc *p)
2276 struct filedesc *fdp;
2282 FILEDESC_LOCK_FAST(fdp);
2283 for (i = 0; i < fdp->fd_nfiles; ++i) {
2284 fp = fget_locked(fdp, i);
2285 if (fp != NULL && fp->f_ops == &mqueueops) {
2287 mtx_lock(&mq->mq_mutex);
2288 notifier_remove(p, FPTOMQ(fp), i);
2289 mtx_unlock(&mq->mq_mutex);
2292 FILEDESC_UNLOCK_FAST(fdp);
2293 KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2297 mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2298 int flags, struct thread *td)
2300 return (EOPNOTSUPP);
2304 mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
2305 int flags, struct thread *td)
2307 return (EOPNOTSUPP);
2311 mqf_ioctl(struct file *fp, u_long cmd, void *data,
2312 struct ucred *active_cred, struct thread *td)
2318 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2321 struct mqueue *mq = FPTOMQ(fp);
2324 mtx_lock(&mq->mq_mutex);
2325 if (events & (POLLIN | POLLRDNORM)) {
2326 if (mq->mq_curmsgs) {
2327 revents |= events & (POLLIN | POLLRDNORM);
2329 mq->mq_flags |= MQ_RSEL;
2330 selrecord(td, &mq->mq_rsel);
2333 if (events & POLLOUT) {
2334 if (mq->mq_curmsgs < mq->mq_maxmsg)
2337 mq->mq_flags |= MQ_WSEL;
2338 selrecord(td, &mq->mq_wsel);
2341 mtx_unlock(&mq->mq_mutex);
2346 mqf_close(struct file *fp, struct thread *td)
2348 struct mqfs_node *pn;
2351 fp->f_ops = &badfileops;
2355 sx_xlock(&mqfs_data.mi_lock);
2357 sx_xunlock(&mqfs_data.mi_lock);
2362 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2365 struct mqfs_node *pn = fp->f_data;
2367 bzero(st, sizeof *st);
2368 st->st_atimespec = pn->mn_atime;
2369 st->st_mtimespec = pn->mn_mtime;
2370 st->st_ctimespec = pn->mn_ctime;
2371 st->st_birthtimespec = pn->mn_birth;
2372 st->st_uid = pn->mn_uid;
2373 st->st_gid = pn->mn_gid;
2374 st->st_mode = S_IFIFO | pn->mn_mode;
2379 mqf_kqfilter(struct file *fp, struct knote *kn)
2381 struct mqueue *mq = FPTOMQ(fp);
2384 if (kn->kn_filter == EVFILT_READ) {
2385 kn->kn_fop = &mq_rfiltops;
2386 knlist_add(&mq->mq_rsel.si_note, kn, 0);
2387 } else if (kn->kn_filter == EVFILT_WRITE) {
2388 kn->kn_fop = &mq_wfiltops;
2389 knlist_add(&mq->mq_wsel.si_note, kn, 0);
2396 filt_mqdetach(struct knote *kn)
2398 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2400 if (kn->kn_filter == EVFILT_READ)
2401 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2402 else if (kn->kn_filter == EVFILT_WRITE)
2403 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2405 panic("filt_mqdetach");
2409 filt_mqread(struct knote *kn, long hint)
2411 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2413 mtx_assert(&mq->mq_mutex, MA_OWNED);
2414 return (mq->mq_curmsgs != 0);
2418 filt_mqwrite(struct knote *kn, long hint)
2420 struct mqueue *mq = FPTOMQ(kn->kn_fp);
2422 mtx_assert(&mq->mq_mutex, MA_OWNED);
2423 return (mq->mq_curmsgs < mq->mq_maxmsg);
2426 static struct fileops mqueueops = {
2427 .fo_read = mqf_read,
2428 .fo_write = mqf_write,
2429 .fo_ioctl = mqf_ioctl,
2430 .fo_poll = mqf_poll,
2431 .fo_kqfilter = mqf_kqfilter,
2432 .fo_stat = mqf_stat,
2433 .fo_close = mqf_close
2436 static struct vop_vector mqfs_vnodeops = {
2437 .vop_default = &default_vnodeops,
2438 .vop_access = mqfs_access,
2439 .vop_cachedlookup = mqfs_lookup,
2440 .vop_lookup = vfs_cache_lookup,
2441 .vop_reclaim = mqfs_reclaim,
2442 .vop_create = mqfs_create,
2443 .vop_remove = mqfs_remove,
2444 .vop_inactive = mqfs_inactive,
2445 .vop_open = mqfs_open,
2446 .vop_close = mqfs_close,
2447 .vop_getattr = mqfs_getattr,
2448 .vop_setattr = mqfs_setattr,
2449 .vop_read = mqfs_read,
2450 .vop_write = VOP_EOPNOTSUPP,
2451 .vop_readdir = mqfs_readdir,
2452 .vop_mkdir = VOP_EOPNOTSUPP,
2453 .vop_rmdir = VOP_EOPNOTSUPP
2456 static struct vfsops mqfs_vfsops = {
2457 .vfs_init = mqfs_init,
2458 .vfs_uninit = mqfs_uninit,
2459 .vfs_mount = mqfs_mount,
2460 .vfs_unmount = mqfs_unmount,
2461 .vfs_root = mqfs_root,
2462 .vfs_statfs = mqfs_statfs,
2465 SYSCALL_MODULE_HELPER(mq_open);
2466 SYSCALL_MODULE_HELPER(mq_setattr);
2467 SYSCALL_MODULE_HELPER(mq_timedsend);
2468 SYSCALL_MODULE_HELPER(mq_timedreceive);
2469 SYSCALL_MODULE_HELPER(mq_notify);
2470 SYSCALL_MODULE_HELPER(mq_unlink);
2472 VFS_SET(mqfs_vfsops, mqueuefs, VFCF_SYNTHETIC);
2473 MODULE_VERSION(mqueuefs, 1);