]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_mqueue.c
Schedule fast taskqueue callouts on right CPU.
[FreeBSD/FreeBSD.git] / sys / kern / uipc_mqueue.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5  * Copyright (c) 2016-2017 Robert N. M. Watson
6  * All rights reserved.
7  *
8  * Portions of this software were developed by BAE Systems, the University of
9  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
10  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
11  * Computing (TC) research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35
36 /*
37  * POSIX message queue implementation.
38  *
39  * 1) A mqueue filesystem can be mounted, each message queue appears
40  *    in mounted directory, user can change queue's permission and
41  *    ownership, or remove a queue. Manually creating a file in the
42  *    directory causes a message queue to be created in the kernel with
43  *    default message queue attributes applied and same name used, this
44  *    method is not advocated since mq_open syscall allows user to specify
45  *    different attributes. Also the file system can be mounted multiple
46  *    times at different mount points but shows same contents.
47  *
48  * 2) Standard POSIX message queue API. The syscalls do not use vfs layer,
49  *    but directly operate on internal data structure, this allows user to
50  *    use the IPC facility without having to mount mqueue file system.
51  */
52
53 #include <sys/cdefs.h>
54 #include "opt_capsicum.h"
55
56 #include <sys/param.h>
57 #include <sys/kernel.h>
58 #include <sys/systm.h>
59 #include <sys/limits.h>
60 #include <sys/malloc.h>
61 #include <sys/buf.h>
62 #include <sys/capsicum.h>
63 #include <sys/dirent.h>
64 #include <sys/event.h>
65 #include <sys/eventhandler.h>
66 #include <sys/fcntl.h>
67 #include <sys/file.h>
68 #include <sys/filedesc.h>
69 #include <sys/jail.h>
70 #include <sys/lock.h>
71 #include <sys/module.h>
72 #include <sys/mount.h>
73 #include <sys/mqueue.h>
74 #include <sys/mutex.h>
75 #include <sys/namei.h>
76 #include <sys/posix4.h>
77 #include <sys/poll.h>
78 #include <sys/priv.h>
79 #include <sys/proc.h>
80 #include <sys/queue.h>
81 #include <sys/sysproto.h>
82 #include <sys/stat.h>
83 #include <sys/syscall.h>
84 #include <sys/syscallsubr.h>
85 #include <sys/sysent.h>
86 #include <sys/sx.h>
87 #include <sys/sysctl.h>
88 #include <sys/taskqueue.h>
89 #include <sys/unistd.h>
90 #include <sys/user.h>
91 #include <sys/vnode.h>
92 #include <machine/atomic.h>
93
94 #include <security/audit/audit.h>
95
96 FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
97
98 /*
99  * Limits and constants
100  */
101 #define MQFS_NAMELEN            NAME_MAX
102 #define MQFS_DELEN              (8 + MQFS_NAMELEN)
103
104 /* node types */
105 typedef enum {
106         mqfstype_none = 0,
107         mqfstype_root,
108         mqfstype_dir,
109         mqfstype_this,
110         mqfstype_parent,
111         mqfstype_file,
112         mqfstype_symlink,
113 } mqfs_type_t;
114
115 struct mqfs_node;
116
117 /*
118  * mqfs_info: describes a mqfs instance
119  */
120 struct mqfs_info {
121         struct sx               mi_lock;
122         struct mqfs_node        *mi_root;
123         struct unrhdr           *mi_unrhdr;
124 };
125
126 struct mqfs_vdata {
127         LIST_ENTRY(mqfs_vdata)  mv_link;
128         struct mqfs_node        *mv_node;
129         struct vnode            *mv_vnode;
130         struct task             mv_task;
131 };
132
133 /*
134  * mqfs_node: describes a node (file or directory) within a mqfs
135  */
136 struct mqfs_node {
137         char                    mn_name[MQFS_NAMELEN+1];
138         struct mqfs_info        *mn_info;
139         struct mqfs_node        *mn_parent;
140         LIST_HEAD(,mqfs_node)   mn_children;
141         LIST_ENTRY(mqfs_node)   mn_sibling;
142         LIST_HEAD(,mqfs_vdata)  mn_vnodes;
143         const void              *mn_pr_root;
144         int                     mn_refcount;
145         mqfs_type_t             mn_type;
146         int                     mn_deleted;
147         uint32_t                mn_fileno;
148         void                    *mn_data;
149         struct timespec         mn_birth;
150         struct timespec         mn_ctime;
151         struct timespec         mn_atime;
152         struct timespec         mn_mtime;
153         uid_t                   mn_uid;
154         gid_t                   mn_gid;
155         int                     mn_mode;
156 };
157
158 #define VTON(vp)        (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
159 #define VTOMQ(vp)       ((struct mqueue *)(VTON(vp)->mn_data))
160 #define VFSTOMQFS(m)    ((struct mqfs_info *)((m)->mnt_data))
161 #define FPTOMQ(fp)      ((struct mqueue *)(((struct mqfs_node *) \
162                                 (fp)->f_data)->mn_data))
163
164 TAILQ_HEAD(msgq, mqueue_msg);
165
166 struct mqueue;
167
168 struct mqueue_notifier {
169         LIST_ENTRY(mqueue_notifier)     nt_link;
170         struct sigevent                 nt_sigev;
171         ksiginfo_t                      nt_ksi;
172         struct proc                     *nt_proc;
173 };
174
175 struct mqueue {
176         struct mtx      mq_mutex;
177         int             mq_flags;
178         long            mq_maxmsg;
179         long            mq_msgsize;
180         long            mq_curmsgs;
181         long            mq_totalbytes;
182         struct msgq     mq_msgq;
183         int             mq_receivers;
184         int             mq_senders;
185         struct selinfo  mq_rsel;
186         struct selinfo  mq_wsel;
187         struct mqueue_notifier  *mq_notifier;
188 };
189
190 #define MQ_RSEL         0x01
191 #define MQ_WSEL         0x02
192
193 struct mqueue_msg {
194         TAILQ_ENTRY(mqueue_msg) msg_link;
195         unsigned int    msg_prio;
196         unsigned int    msg_size;
197         /* following real data... */
198 };
199
200 static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
201         "POSIX real time message queue");
202
203 static int      default_maxmsg  = 10;
204 static int      default_msgsize = 1024;
205
206 static int      maxmsg = 100;
207 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
208     &maxmsg, 0, "Default maximum messages in queue");
209 static int      maxmsgsize = 16384;
210 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
211     &maxmsgsize, 0, "Default maximum message size");
212 static int      maxmq = 100;
213 SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
214     &maxmq, 0, "maximum message queues");
215 static int      curmq = 0;
216 SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
217     &curmq, 0, "current message queue number");
218 static int      unloadable = 0;
219 static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
220
221 static eventhandler_tag exit_tag;
222
223 /* Only one instance per-system */
224 static struct mqfs_info         mqfs_data;
225 static uma_zone_t               mqnode_zone;
226 static uma_zone_t               mqueue_zone;
227 static uma_zone_t               mvdata_zone;
228 static uma_zone_t               mqnoti_zone;
229 static struct vop_vector        mqfs_vnodeops;
230 static struct fileops           mqueueops;
231 static unsigned                 mqfs_osd_jail_slot;
232
233 /*
234  * Directory structure construction and manipulation
235  */
236 #ifdef notyet
237 static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
238         const char *name, int namelen, struct ucred *cred, int mode);
239 static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
240         const char *name, int namelen, struct ucred *cred, int mode);
241 #endif
242
243 static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
244         const char *name, int namelen, struct ucred *cred, int mode);
245 static int      mqfs_destroy(struct mqfs_node *mn);
246 static void     mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
247 static void     mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
248 static int      mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
249 static int      mqfs_prison_remove(void *obj, void *data);
250
251 /*
252  * Message queue construction and maniplation
253  */
254 static struct mqueue    *mqueue_alloc(const struct mq_attr *attr);
255 static void     mqueue_free(struct mqueue *mq);
256 static int      mqueue_send(struct mqueue *mq, const char *msg_ptr,
257                         size_t msg_len, unsigned msg_prio, int waitok,
258                         const struct timespec *abs_timeout);
259 static int      mqueue_receive(struct mqueue *mq, char *msg_ptr,
260                         size_t msg_len, unsigned *msg_prio, int waitok,
261                         const struct timespec *abs_timeout);
262 static int      _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
263                         int timo);
264 static int      _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
265                         int timo);
266 static void     mqueue_send_notification(struct mqueue *mq);
267 static void     mqueue_fdclose(struct thread *td, int fd, struct file *fp);
268 static void     mq_proc_exit(void *arg, struct proc *p);
269
270 /*
271  * kqueue filters
272  */
273 static void     filt_mqdetach(struct knote *kn);
274 static int      filt_mqread(struct knote *kn, long hint);
275 static int      filt_mqwrite(struct knote *kn, long hint);
276
277 struct filterops mq_rfiltops = {
278         .f_isfd = 1,
279         .f_detach = filt_mqdetach,
280         .f_event = filt_mqread,
281 };
282 struct filterops mq_wfiltops = {
283         .f_isfd = 1,
284         .f_detach = filt_mqdetach,
285         .f_event = filt_mqwrite,
286 };
287
288 /*
289  * Initialize fileno bitmap
290  */
291 static void
292 mqfs_fileno_init(struct mqfs_info *mi)
293 {
294         struct unrhdr *up;
295
296         up = new_unrhdr(1, INT_MAX, NULL);
297         mi->mi_unrhdr = up;
298 }
299
300 /*
301  * Tear down fileno bitmap
302  */
303 static void
304 mqfs_fileno_uninit(struct mqfs_info *mi)
305 {
306         struct unrhdr *up;
307
308         up = mi->mi_unrhdr;
309         mi->mi_unrhdr = NULL;
310         delete_unrhdr(up);
311 }
312
313 /*
314  * Allocate a file number
315  */
316 static void
317 mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
318 {
319         /* make sure our parent has a file number */
320         if (mn->mn_parent && !mn->mn_parent->mn_fileno)
321                 mqfs_fileno_alloc(mi, mn->mn_parent);
322
323         switch (mn->mn_type) {
324         case mqfstype_root:
325         case mqfstype_dir:
326         case mqfstype_file:
327         case mqfstype_symlink:
328                 mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
329                 break;
330         case mqfstype_this:
331                 KASSERT(mn->mn_parent != NULL,
332                     ("mqfstype_this node has no parent"));
333                 mn->mn_fileno = mn->mn_parent->mn_fileno;
334                 break;
335         case mqfstype_parent:
336                 KASSERT(mn->mn_parent != NULL,
337                     ("mqfstype_parent node has no parent"));
338                 if (mn->mn_parent == mi->mi_root) {
339                         mn->mn_fileno = mn->mn_parent->mn_fileno;
340                         break;
341                 }
342                 KASSERT(mn->mn_parent->mn_parent != NULL,
343                     ("mqfstype_parent node has no grandparent"));
344                 mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
345                 break;
346         default:
347                 KASSERT(0,
348                     ("mqfs_fileno_alloc() called for unknown type node: %d",
349                         mn->mn_type));
350                 break;
351         }
352 }
353
354 /*
355  * Release a file number
356  */
357 static void
358 mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
359 {
360         switch (mn->mn_type) {
361         case mqfstype_root:
362         case mqfstype_dir:
363         case mqfstype_file:
364         case mqfstype_symlink:
365                 free_unr(mi->mi_unrhdr, mn->mn_fileno);
366                 break;
367         case mqfstype_this:
368         case mqfstype_parent:
369                 /* ignore these, as they don't "own" their file number */
370                 break;
371         default:
372                 KASSERT(0,
373                     ("mqfs_fileno_free() called for unknown type node: %d", 
374                         mn->mn_type));
375                 break;
376         }
377 }
378
379 static __inline struct mqfs_node *
380 mqnode_alloc(void)
381 {
382         return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
383 }
384
385 static __inline void
386 mqnode_free(struct mqfs_node *node)
387 {
388         uma_zfree(mqnode_zone, node);
389 }
390
391 static __inline void
392 mqnode_addref(struct mqfs_node *node)
393 {
394         atomic_add_int(&node->mn_refcount, 1);
395 }
396
397 static __inline void
398 mqnode_release(struct mqfs_node *node)
399 {
400         struct mqfs_info *mqfs;
401         int old, exp;
402
403         mqfs = node->mn_info;
404         old = atomic_fetchadd_int(&node->mn_refcount, -1);
405         if (node->mn_type == mqfstype_dir ||
406             node->mn_type == mqfstype_root)
407                 exp = 3; /* include . and .. */
408         else
409                 exp = 1;
410         if (old == exp) {
411                 int locked = sx_xlocked(&mqfs->mi_lock);
412                 if (!locked)
413                         sx_xlock(&mqfs->mi_lock);
414                 mqfs_destroy(node);
415                 if (!locked)
416                         sx_xunlock(&mqfs->mi_lock);
417         }
418 }
419
420 /*
421  * Add a node to a directory
422  */
423 static int
424 mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
425 {
426         KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
427         KASSERT(parent->mn_info != NULL,
428             ("%s(): parent has no mn_info", __func__));
429         KASSERT(parent->mn_type == mqfstype_dir ||
430             parent->mn_type == mqfstype_root,
431             ("%s(): parent is not a directory", __func__));
432
433         node->mn_info = parent->mn_info;
434         node->mn_parent = parent;
435         LIST_INIT(&node->mn_children);
436         LIST_INIT(&node->mn_vnodes);
437         LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
438         mqnode_addref(parent);
439         return (0);
440 }
441
442 static struct mqfs_node *
443 mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
444         int nodetype)
445 {
446         struct mqfs_node *node;
447
448         node = mqnode_alloc();
449         strncpy(node->mn_name, name, namelen);
450         node->mn_pr_root = cred->cr_prison->pr_root;
451         node->mn_type = nodetype;
452         node->mn_refcount = 1;
453         vfs_timestamp(&node->mn_birth);
454         node->mn_ctime = node->mn_atime = node->mn_mtime
455                 = node->mn_birth;
456         node->mn_uid = cred->cr_uid;
457         node->mn_gid = cred->cr_gid;
458         node->mn_mode = mode;
459         return (node);
460 }
461
462 /*
463  * Create a file
464  */
465 static struct mqfs_node *
466 mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
467         struct ucred *cred, int mode)
468 {
469         struct mqfs_node *node;
470
471         node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
472         if (mqfs_add_node(parent, node) != 0) {
473                 mqnode_free(node);
474                 return (NULL);
475         }
476         return (node);
477 }
478
479 /*
480  * Add . and .. to a directory
481  */
482 static int
483 mqfs_fixup_dir(struct mqfs_node *parent)
484 {
485         struct mqfs_node *dir;
486
487         dir = mqnode_alloc();
488         dir->mn_name[0] = '.';
489         dir->mn_type = mqfstype_this;
490         dir->mn_refcount = 1;
491         if (mqfs_add_node(parent, dir) != 0) {
492                 mqnode_free(dir);
493                 return (-1);
494         }
495
496         dir = mqnode_alloc();
497         dir->mn_name[0] = dir->mn_name[1] = '.';
498         dir->mn_type = mqfstype_parent;
499         dir->mn_refcount = 1;
500
501         if (mqfs_add_node(parent, dir) != 0) {
502                 mqnode_free(dir);
503                 return (-1);
504         }
505
506         return (0);
507 }
508
509 #ifdef notyet
510
511 /*
512  * Create a directory
513  */
514 static struct mqfs_node *
515 mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
516         struct ucred *cred, int mode)
517 {
518         struct mqfs_node *node;
519
520         node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
521         if (mqfs_add_node(parent, node) != 0) {
522                 mqnode_free(node);
523                 return (NULL);
524         }
525
526         if (mqfs_fixup_dir(node) != 0) {
527                 mqfs_destroy(node);
528                 return (NULL);
529         }
530         return (node);
531 }
532
533 /*
534  * Create a symlink
535  */
536 static struct mqfs_node *
537 mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
538         struct ucred *cred, int mode)
539 {
540         struct mqfs_node *node;
541
542         node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
543         if (mqfs_add_node(parent, node) != 0) {
544                 mqnode_free(node);
545                 return (NULL);
546         }
547         return (node);
548 }
549
550 #endif
551
552 /*
553  * Destroy a node or a tree of nodes
554  */
555 static int
556 mqfs_destroy(struct mqfs_node *node)
557 {
558         struct mqfs_node *parent;
559
560         KASSERT(node != NULL,
561             ("%s(): node is NULL", __func__));
562         KASSERT(node->mn_info != NULL,
563             ("%s(): node has no mn_info", __func__));
564
565         /* destroy children */
566         if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
567                 while (! LIST_EMPTY(&node->mn_children))
568                         mqfs_destroy(LIST_FIRST(&node->mn_children));
569
570         /* unlink from parent */
571         if ((parent = node->mn_parent) != NULL) {
572                 KASSERT(parent->mn_info == node->mn_info,
573                     ("%s(): parent has different mn_info", __func__));
574                 LIST_REMOVE(node, mn_sibling);
575         }
576
577         if (node->mn_fileno != 0)
578                 mqfs_fileno_free(node->mn_info, node);
579         if (node->mn_data != NULL)
580                 mqueue_free(node->mn_data);
581         mqnode_free(node);
582         return (0);
583 }
584
585 /*
586  * Mount a mqfs instance
587  */
588 static int
589 mqfs_mount(struct mount *mp)
590 {
591         struct statfs *sbp;
592
593         if (mp->mnt_flag & MNT_UPDATE)
594                 return (EOPNOTSUPP);
595
596         mp->mnt_data = &mqfs_data;
597         MNT_ILOCK(mp);
598         mp->mnt_flag |= MNT_LOCAL;
599         MNT_IUNLOCK(mp);
600         vfs_getnewfsid(mp);
601
602         sbp = &mp->mnt_stat;
603         vfs_mountedfrom(mp, "mqueue");
604         sbp->f_bsize = PAGE_SIZE;
605         sbp->f_iosize = PAGE_SIZE;
606         sbp->f_blocks = 1;
607         sbp->f_bfree = 0;
608         sbp->f_bavail = 0;
609         sbp->f_files = 1;
610         sbp->f_ffree = 0;
611         return (0);
612 }
613
614 /*
615  * Unmount a mqfs instance
616  */
617 static int
618 mqfs_unmount(struct mount *mp, int mntflags)
619 {
620         int error;
621
622         error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0,
623             curthread);
624         return (error);
625 }
626
627 /*
628  * Return a root vnode
629  */
630 static int
631 mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
632 {
633         struct mqfs_info *mqfs;
634         int ret;
635
636         mqfs = VFSTOMQFS(mp);
637         ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
638         return (ret);
639 }
640
641 /*
642  * Return filesystem stats
643  */
644 static int
645 mqfs_statfs(struct mount *mp, struct statfs *sbp)
646 {
647         /* XXX update statistics */
648         return (0);
649 }
650
651 /*
652  * Initialize a mqfs instance
653  */
654 static int
655 mqfs_init(struct vfsconf *vfc)
656 {
657         struct mqfs_node *root;
658         struct mqfs_info *mi;
659         osd_method_t methods[PR_MAXMETHOD] = {
660             [PR_METHOD_REMOVE] = mqfs_prison_remove,
661         };
662
663         mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
664                 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
665         mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
666                 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
667         mvdata_zone = uma_zcreate("mvdata",
668                 sizeof(struct mqfs_vdata), NULL, NULL, NULL,
669                 NULL, UMA_ALIGN_PTR, 0);
670         mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
671                 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
672         mi = &mqfs_data;
673         sx_init(&mi->mi_lock, "mqfs lock");
674         /* set up the root diretory */
675         root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
676                 mqfstype_root);
677         root->mn_info = mi;
678         LIST_INIT(&root->mn_children);
679         LIST_INIT(&root->mn_vnodes);
680         mi->mi_root = root;
681         mqfs_fileno_init(mi);
682         mqfs_fileno_alloc(mi, root);
683         mqfs_fixup_dir(root);
684         exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
685             EVENTHANDLER_PRI_ANY);
686         mq_fdclose = mqueue_fdclose;
687         p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
688         mqfs_osd_jail_slot = osd_jail_register(NULL, methods);
689         return (0);
690 }
691
692 /*
693  * Destroy a mqfs instance
694  */
695 static int
696 mqfs_uninit(struct vfsconf *vfc)
697 {
698         struct mqfs_info *mi;
699
700         if (!unloadable)
701                 return (EOPNOTSUPP);
702         osd_jail_deregister(mqfs_osd_jail_slot);
703         EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
704         mi = &mqfs_data;
705         mqfs_destroy(mi->mi_root);
706         mi->mi_root = NULL;
707         mqfs_fileno_uninit(mi);
708         sx_destroy(&mi->mi_lock);
709         uma_zdestroy(mqnode_zone);
710         uma_zdestroy(mqueue_zone);
711         uma_zdestroy(mvdata_zone);
712         uma_zdestroy(mqnoti_zone);
713         return (0);
714 }
715
716 /*
717  * task routine
718  */
719 static void
720 do_recycle(void *context, int pending __unused)
721 {
722         struct vnode *vp = (struct vnode *)context;
723
724         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
725         vrecycle(vp);
726         VOP_UNLOCK(vp);
727         vdrop(vp);
728 }
729
730 /*
731  * Allocate a vnode
732  */
733 static int
734 mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
735 {
736         struct mqfs_vdata *vd;
737         struct mqfs_info  *mqfs;
738         struct vnode *newvpp;
739         int error;
740
741         mqfs = pn->mn_info;
742         *vpp = NULL;
743         sx_xlock(&mqfs->mi_lock);
744         LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
745                 if (vd->mv_vnode->v_mount == mp) {
746                         vhold(vd->mv_vnode);
747                         break;
748                 }
749         }
750
751         if (vd != NULL) {
752 found:
753                 *vpp = vd->mv_vnode;
754                 sx_xunlock(&mqfs->mi_lock);
755                 error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE);
756                 vdrop(*vpp);
757                 return (error);
758         }
759         sx_xunlock(&mqfs->mi_lock);
760
761         error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
762         if (error)
763                 return (error);
764         vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
765         error = insmntque(newvpp, mp);
766         if (error != 0)
767                 return (error);
768
769         sx_xlock(&mqfs->mi_lock);
770         /*
771          * Check if it has already been allocated
772          * while we were blocked.
773          */
774         LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
775                 if (vd->mv_vnode->v_mount == mp) {
776                         vhold(vd->mv_vnode);
777                         sx_xunlock(&mqfs->mi_lock);
778
779                         vgone(newvpp);
780                         vput(newvpp);
781                         goto found;
782                 }
783         }
784
785         *vpp = newvpp;
786
787         vd = uma_zalloc(mvdata_zone, M_WAITOK);
788         (*vpp)->v_data = vd;
789         vd->mv_vnode = *vpp;
790         vd->mv_node = pn;
791         TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
792         LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
793         mqnode_addref(pn);
794         switch (pn->mn_type) {
795         case mqfstype_root:
796                 (*vpp)->v_vflag = VV_ROOT;
797                 /* fall through */
798         case mqfstype_dir:
799         case mqfstype_this:
800         case mqfstype_parent:
801                 (*vpp)->v_type = VDIR;
802                 break;
803         case mqfstype_file:
804                 (*vpp)->v_type = VREG;
805                 break;
806         case mqfstype_symlink:
807                 (*vpp)->v_type = VLNK;
808                 break;
809         case mqfstype_none:
810                 KASSERT(0, ("mqfs_allocf called for null node\n"));
811         default:
812                 panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
813         }
814         sx_xunlock(&mqfs->mi_lock);
815         return (0);
816 }
817
818 /* 
819  * Search a directory entry
820  */
821 static struct mqfs_node *
822 mqfs_search(struct mqfs_node *pd, const char *name, int len, struct ucred *cred)
823 {
824         struct mqfs_node *pn;
825         const void *pr_root;
826
827         sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
828         pr_root = cred->cr_prison->pr_root;
829         LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
830                 /* Only match names within the same prison root directory */
831                 if ((pn->mn_pr_root == NULL || pn->mn_pr_root == pr_root) &&
832                     strncmp(pn->mn_name, name, len) == 0 &&
833                     pn->mn_name[len] == '\0')
834                         return (pn);
835         }
836         return (NULL);
837 }
838
839 /*
840  * Look up a file or directory.
841  */
842 static int
843 mqfs_lookupx(struct vop_cachedlookup_args *ap)
844 {
845         struct componentname *cnp;
846         struct vnode *dvp, **vpp;
847         struct mqfs_node *pd;
848         struct mqfs_node *pn;
849         struct mqfs_info *mqfs;
850         int nameiop, flags, error, namelen;
851         char *pname;
852         struct thread *td;
853
854         cnp = ap->a_cnp;
855         vpp = ap->a_vpp;
856         dvp = ap->a_dvp;
857         pname = cnp->cn_nameptr;
858         namelen = cnp->cn_namelen;
859         td = cnp->cn_thread;
860         flags = cnp->cn_flags;
861         nameiop = cnp->cn_nameiop;
862         pd = VTON(dvp);
863         pn = NULL;
864         mqfs = pd->mn_info;
865         *vpp = NULLVP;
866
867         if (dvp->v_type != VDIR)
868                 return (ENOTDIR);
869
870         error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
871         if (error)
872                 return (error);
873
874         /* shortcut: check if the name is too long */
875         if (cnp->cn_namelen >= MQFS_NAMELEN)
876                 return (ENOENT);
877
878         /* self */
879         if (namelen == 1 && pname[0] == '.') {
880                 if ((flags & ISLASTCN) && nameiop != LOOKUP)
881                         return (EINVAL);
882                 pn = pd;
883                 *vpp = dvp;
884                 VREF(dvp);
885                 return (0);
886         }
887
888         /* parent */
889         if (cnp->cn_flags & ISDOTDOT) {
890                 if (dvp->v_vflag & VV_ROOT)
891                         return (EIO);
892                 if ((flags & ISLASTCN) && nameiop != LOOKUP)
893                         return (EINVAL);
894                 VOP_UNLOCK(dvp);
895                 KASSERT(pd->mn_parent, ("non-root directory has no parent"));
896                 pn = pd->mn_parent;
897                 error = mqfs_allocv(dvp->v_mount, vpp, pn);
898                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
899                 return (error);
900         }
901
902         /* named node */
903         sx_xlock(&mqfs->mi_lock);
904         pn = mqfs_search(pd, pname, namelen, cnp->cn_cred);
905         if (pn != NULL)
906                 mqnode_addref(pn);
907         sx_xunlock(&mqfs->mi_lock);
908
909         /* found */
910         if (pn != NULL) {
911                 /* DELETE */
912                 if (nameiop == DELETE && (flags & ISLASTCN)) {
913                         error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
914                         if (error) {
915                                 mqnode_release(pn);
916                                 return (error);
917                         }
918                         if (*vpp == dvp) {
919                                 VREF(dvp);
920                                 *vpp = dvp;
921                                 mqnode_release(pn);
922                                 return (0);
923                         }
924                 }
925
926                 /* allocate vnode */
927                 error = mqfs_allocv(dvp->v_mount, vpp, pn);
928                 mqnode_release(pn);
929                 if (error == 0 && cnp->cn_flags & MAKEENTRY)
930                         cache_enter(dvp, *vpp, cnp);
931                 return (error);
932         }
933
934         /* not found */
935
936         /* will create a new entry in the directory ? */
937         if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
938             && (flags & ISLASTCN)) {
939                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
940                 if (error)
941                         return (error);
942                 cnp->cn_flags |= SAVENAME;
943                 return (EJUSTRETURN);
944         }
945         return (ENOENT);
946 }
947
948 #if 0
949 struct vop_lookup_args {
950         struct vop_generic_args a_gen;
951         struct vnode *a_dvp;
952         struct vnode **a_vpp;
953         struct componentname *a_cnp;
954 };
955 #endif
956
957 /*
958  * vnode lookup operation
959  */
960 static int
961 mqfs_lookup(struct vop_cachedlookup_args *ap)
962 {
963         int rc;
964
965         rc = mqfs_lookupx(ap);
966         return (rc);
967 }
968
969 #if 0
970 struct vop_create_args {
971         struct vnode *a_dvp;
972         struct vnode **a_vpp;
973         struct componentname *a_cnp;
974         struct vattr *a_vap;
975 };
976 #endif
977
978 /*
979  * vnode creation operation
980  */
981 static int
982 mqfs_create(struct vop_create_args *ap)
983 {
984         struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
985         struct componentname *cnp = ap->a_cnp;
986         struct mqfs_node *pd;
987         struct mqfs_node *pn;
988         struct mqueue *mq;
989         int error;
990
991         pd = VTON(ap->a_dvp);
992         if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
993                 return (ENOTDIR);
994         mq = mqueue_alloc(NULL);
995         if (mq == NULL)
996                 return (EAGAIN);
997         sx_xlock(&mqfs->mi_lock);
998         if ((cnp->cn_flags & HASBUF) == 0)
999                 panic("%s: no name", __func__);
1000         pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
1001                 cnp->cn_cred, ap->a_vap->va_mode);
1002         if (pn == NULL) {
1003                 sx_xunlock(&mqfs->mi_lock);
1004                 error = ENOSPC;
1005         } else {
1006                 mqnode_addref(pn);
1007                 sx_xunlock(&mqfs->mi_lock);
1008                 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1009                 mqnode_release(pn);
1010                 if (error)
1011                         mqfs_destroy(pn);
1012                 else
1013                         pn->mn_data = mq;
1014         }
1015         if (error)
1016                 mqueue_free(mq);
1017         return (error);
1018 }
1019
1020 /*
1021  * Remove an entry
1022  */
1023 static
1024 int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
1025 {
1026         struct mqfs_node *parent;
1027         struct mqfs_vdata *vd;
1028         int error = 0;
1029
1030         sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
1031
1032         if (ucred->cr_uid != pn->mn_uid &&
1033             (error = priv_check_cred(ucred, PRIV_MQ_ADMIN)) != 0)
1034                 error = EACCES;
1035         else if (!pn->mn_deleted) {
1036                 parent = pn->mn_parent;
1037                 pn->mn_parent = NULL;
1038                 pn->mn_deleted = 1;
1039                 LIST_REMOVE(pn, mn_sibling);
1040                 LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
1041                         cache_purge(vd->mv_vnode);
1042                         vhold(vd->mv_vnode);
1043                         taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
1044                 }
1045                 mqnode_release(pn);
1046                 mqnode_release(parent);
1047         } else
1048                 error = ENOENT;
1049         return (error);
1050 }
1051
1052 #if 0
1053 struct vop_remove_args {
1054         struct vnode *a_dvp;
1055         struct vnode *a_vp;
1056         struct componentname *a_cnp;
1057 };
1058 #endif
1059
1060 /*
1061  * vnode removal operation
1062  */
1063 static int
1064 mqfs_remove(struct vop_remove_args *ap)
1065 {
1066         struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1067         struct mqfs_node *pn;
1068         int error;
1069
1070         if (ap->a_vp->v_type == VDIR)
1071                 return (EPERM);
1072         pn = VTON(ap->a_vp);
1073         sx_xlock(&mqfs->mi_lock);
1074         error = do_unlink(pn, ap->a_cnp->cn_cred);
1075         sx_xunlock(&mqfs->mi_lock);
1076         return (error);
1077 }
1078
1079 #if 0
1080 struct vop_inactive_args {
1081         struct vnode *a_vp;
1082         struct thread *a_td;
1083 };
1084 #endif
1085
1086 static int
1087 mqfs_inactive(struct vop_inactive_args *ap)
1088 {
1089         struct mqfs_node *pn = VTON(ap->a_vp);
1090
1091         if (pn->mn_deleted)
1092                 vrecycle(ap->a_vp);
1093         return (0);
1094 }
1095
1096 #if 0
1097 struct vop_reclaim_args {
1098         struct vop_generic_args a_gen;
1099         struct vnode *a_vp;
1100 };
1101 #endif
1102
1103 static int
1104 mqfs_reclaim(struct vop_reclaim_args *ap)
1105 {
1106         struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
1107         struct vnode *vp = ap->a_vp;
1108         struct mqfs_node *pn;
1109         struct mqfs_vdata *vd;
1110
1111         vd = vp->v_data;
1112         pn = vd->mv_node;
1113         sx_xlock(&mqfs->mi_lock);
1114         vp->v_data = NULL;
1115         LIST_REMOVE(vd, mv_link);
1116         uma_zfree(mvdata_zone, vd);
1117         mqnode_release(pn);
1118         sx_xunlock(&mqfs->mi_lock);
1119         return (0);
1120 }
1121
1122 #if 0
1123 struct vop_open_args {
1124         struct vop_generic_args a_gen;
1125         struct vnode *a_vp;
1126         int a_mode;
1127         struct ucred *a_cred;
1128         struct thread *a_td;
1129         struct file *a_fp;
1130 };
1131 #endif
1132
1133 static int
1134 mqfs_open(struct vop_open_args *ap)
1135 {
1136         return (0);
1137 }
1138
1139 #if 0
1140 struct vop_close_args {
1141         struct vop_generic_args a_gen;
1142         struct vnode *a_vp;
1143         int a_fflag;
1144         struct ucred *a_cred;
1145         struct thread *a_td;
1146 };
1147 #endif
1148
1149 static int
1150 mqfs_close(struct vop_close_args *ap)
1151 {
1152         return (0);
1153 }
1154
1155 #if 0
1156 struct vop_access_args {
1157         struct vop_generic_args a_gen;
1158         struct vnode *a_vp;
1159         accmode_t a_accmode;
1160         struct ucred *a_cred;
1161         struct thread *a_td;
1162 };
1163 #endif
1164
1165 /*
1166  * Verify permissions
1167  */
1168 static int
1169 mqfs_access(struct vop_access_args *ap)
1170 {
1171         struct vnode *vp = ap->a_vp;
1172         struct vattr vattr;
1173         int error;
1174
1175         error = VOP_GETATTR(vp, &vattr, ap->a_cred);
1176         if (error)
1177                 return (error);
1178         error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid, vattr.va_gid,
1179             ap->a_accmode, ap->a_cred);
1180         return (error);
1181 }
1182
1183 #if 0
1184 struct vop_getattr_args {
1185         struct vop_generic_args a_gen;
1186         struct vnode *a_vp;
1187         struct vattr *a_vap;
1188         struct ucred *a_cred;
1189 };
1190 #endif
1191
1192 /*
1193  * Get file attributes
1194  */
1195 static int
1196 mqfs_getattr(struct vop_getattr_args *ap)
1197 {
1198         struct vnode *vp = ap->a_vp;
1199         struct mqfs_node *pn = VTON(vp);
1200         struct vattr *vap = ap->a_vap;
1201         int error = 0;
1202
1203         vap->va_type = vp->v_type;
1204         vap->va_mode = pn->mn_mode;
1205         vap->va_nlink = 1;
1206         vap->va_uid = pn->mn_uid;
1207         vap->va_gid = pn->mn_gid;
1208         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1209         vap->va_fileid = pn->mn_fileno;
1210         vap->va_size = 0;
1211         vap->va_blocksize = PAGE_SIZE;
1212         vap->va_bytes = vap->va_size = 0;
1213         vap->va_atime = pn->mn_atime;
1214         vap->va_mtime = pn->mn_mtime;
1215         vap->va_ctime = pn->mn_ctime;
1216         vap->va_birthtime = pn->mn_birth;
1217         vap->va_gen = 0;
1218         vap->va_flags = 0;
1219         vap->va_rdev = NODEV;
1220         vap->va_bytes = 0;
1221         vap->va_filerev = 0;
1222         return (error);
1223 }
1224
1225 #if 0
1226 struct vop_setattr_args {
1227         struct vop_generic_args a_gen;
1228         struct vnode *a_vp;
1229         struct vattr *a_vap;
1230         struct ucred *a_cred;
1231 };
1232 #endif
1233 /*
1234  * Set attributes
1235  */
1236 static int
1237 mqfs_setattr(struct vop_setattr_args *ap)
1238 {
1239         struct mqfs_node *pn;
1240         struct vattr *vap;
1241         struct vnode *vp;
1242         struct thread *td;
1243         int c, error;
1244         uid_t uid;
1245         gid_t gid;
1246
1247         td = curthread;
1248         vap = ap->a_vap;
1249         vp = ap->a_vp;
1250         if ((vap->va_type != VNON) ||
1251             (vap->va_nlink != VNOVAL) ||
1252             (vap->va_fsid != VNOVAL) ||
1253             (vap->va_fileid != VNOVAL) ||
1254             (vap->va_blocksize != VNOVAL) ||
1255             (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1256             (vap->va_rdev != VNOVAL) ||
1257             ((int)vap->va_bytes != VNOVAL) ||
1258             (vap->va_gen != VNOVAL)) {
1259                 return (EINVAL);
1260         }
1261
1262         pn = VTON(vp);
1263
1264         error = c = 0;
1265         if (vap->va_uid == (uid_t)VNOVAL)
1266                 uid = pn->mn_uid;
1267         else
1268                 uid = vap->va_uid;
1269         if (vap->va_gid == (gid_t)VNOVAL)
1270                 gid = pn->mn_gid;
1271         else
1272                 gid = vap->va_gid;
1273
1274         if (uid != pn->mn_uid || gid != pn->mn_gid) {
1275                 /*
1276                  * To modify the ownership of a file, must possess VADMIN
1277                  * for that file.
1278                  */
1279                 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
1280                         return (error);
1281
1282                 /*
1283                  * XXXRW: Why is there a privilege check here: shouldn't the
1284                  * check in VOP_ACCESS() be enough?  Also, are the group bits
1285                  * below definitely right?
1286                  */
1287                 if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
1288                     (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
1289                     (error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
1290                         return (error);
1291                 pn->mn_uid = uid;
1292                 pn->mn_gid = gid;
1293                 c = 1;
1294         }
1295
1296         if (vap->va_mode != (mode_t)VNOVAL) {
1297                 if ((ap->a_cred->cr_uid != pn->mn_uid) &&
1298                     (error = priv_check(td, PRIV_MQ_ADMIN)))
1299                         return (error);
1300                 pn->mn_mode = vap->va_mode;
1301                 c = 1;
1302         }
1303
1304         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1305                 /* See the comment in ufs_vnops::ufs_setattr(). */
1306                 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1307                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1308                     (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1309                         return (error);
1310                 if (vap->va_atime.tv_sec != VNOVAL) {
1311                         pn->mn_atime = vap->va_atime;
1312                 }
1313                 if (vap->va_mtime.tv_sec != VNOVAL) {
1314                         pn->mn_mtime = vap->va_mtime;
1315                 }
1316                 c = 1;
1317         }
1318         if (c) {
1319                 vfs_timestamp(&pn->mn_ctime);
1320         }
1321         return (0);
1322 }
1323
1324 #if 0
1325 struct vop_read_args {
1326         struct vop_generic_args a_gen;
1327         struct vnode *a_vp;
1328         struct uio *a_uio;
1329         int a_ioflag;
1330         struct ucred *a_cred;
1331 };
1332 #endif
1333
1334 /*
1335  * Read from a file
1336  */
1337 static int
1338 mqfs_read(struct vop_read_args *ap)
1339 {
1340         char buf[80];
1341         struct vnode *vp = ap->a_vp;
1342         struct uio *uio = ap->a_uio;
1343         struct mqueue *mq;
1344         int len, error;
1345
1346         if (vp->v_type != VREG)
1347                 return (EINVAL);
1348
1349         mq = VTOMQ(vp);
1350         snprintf(buf, sizeof(buf),
1351                 "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
1352                 mq->mq_totalbytes,
1353                 mq->mq_maxmsg,
1354                 mq->mq_curmsgs,
1355                 mq->mq_msgsize);
1356         buf[sizeof(buf)-1] = '\0';
1357         len = strlen(buf);
1358         error = uiomove_frombuf(buf, len, uio);
1359         return (error);
1360 }
1361
1362 #if 0
1363 struct vop_readdir_args {
1364         struct vop_generic_args a_gen;
1365         struct vnode *a_vp;
1366         struct uio *a_uio;
1367         struct ucred *a_cred;
1368         int *a_eofflag;
1369         int *a_ncookies;
1370         u_long **a_cookies;
1371 };
1372 #endif
1373
1374 /*
1375  * Return directory entries.
1376  */
1377 static int
1378 mqfs_readdir(struct vop_readdir_args *ap)
1379 {
1380         struct vnode *vp;
1381         struct mqfs_info *mi;
1382         struct mqfs_node *pd;
1383         struct mqfs_node *pn;
1384         struct dirent entry;
1385         struct uio *uio;
1386         const void *pr_root;
1387         int *tmp_ncookies = NULL;
1388         off_t offset;
1389         int error, i;
1390
1391         vp = ap->a_vp;
1392         mi = VFSTOMQFS(vp->v_mount);
1393         pd = VTON(vp);
1394         uio = ap->a_uio;
1395
1396         if (vp->v_type != VDIR)
1397                 return (ENOTDIR);
1398
1399         if (uio->uio_offset < 0)
1400                 return (EINVAL);
1401
1402         if (ap->a_ncookies != NULL) {
1403                 tmp_ncookies = ap->a_ncookies;
1404                 *ap->a_ncookies = 0;
1405                 ap->a_ncookies = NULL;
1406         }
1407
1408         error = 0;
1409         offset = 0;
1410
1411         pr_root = ap->a_cred->cr_prison->pr_root;
1412         sx_xlock(&mi->mi_lock);
1413
1414         LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
1415                 entry.d_reclen = sizeof(entry);
1416
1417                 /*
1418                  * Only show names within the same prison root directory
1419                  * (or not associated with a prison, e.g. "." and "..").
1420                  */
1421                 if (pn->mn_pr_root != NULL && pn->mn_pr_root != pr_root)
1422                         continue;
1423                 if (!pn->mn_fileno)
1424                         mqfs_fileno_alloc(mi, pn);
1425                 entry.d_fileno = pn->mn_fileno;
1426                 entry.d_off = offset + entry.d_reclen;
1427                 for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
1428                         entry.d_name[i] = pn->mn_name[i];
1429                 entry.d_namlen = i;
1430                 switch (pn->mn_type) {
1431                 case mqfstype_root:
1432                 case mqfstype_dir:
1433                 case mqfstype_this:
1434                 case mqfstype_parent:
1435                         entry.d_type = DT_DIR;
1436                         break;
1437                 case mqfstype_file:
1438                         entry.d_type = DT_REG;
1439                         break;
1440                 case mqfstype_symlink:
1441                         entry.d_type = DT_LNK;
1442                         break;
1443                 default:
1444                         panic("%s has unexpected node type: %d", pn->mn_name,
1445                                 pn->mn_type);
1446                 }
1447                 dirent_terminate(&entry);
1448                 if (entry.d_reclen > uio->uio_resid)
1449                         break;
1450                 if (offset >= uio->uio_offset) {
1451                         error = vfs_read_dirent(ap, &entry, offset);
1452                         if (error)
1453                                 break;
1454                 }
1455                 offset += entry.d_reclen;
1456         }
1457         sx_xunlock(&mi->mi_lock);
1458
1459         uio->uio_offset = offset;
1460
1461         if (tmp_ncookies != NULL)
1462                 ap->a_ncookies = tmp_ncookies;
1463
1464         return (error);
1465 }
1466
1467 #ifdef notyet
1468
1469 #if 0
1470 struct vop_mkdir_args {
1471         struct vnode *a_dvp;
1472         struvt vnode **a_vpp;
1473         struvt componentname *a_cnp;
1474         struct vattr *a_vap;
1475 };
1476 #endif
1477
1478 /*
1479  * Create a directory.
1480  */
1481 static int
1482 mqfs_mkdir(struct vop_mkdir_args *ap)
1483 {
1484         struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1485         struct componentname *cnp = ap->a_cnp;
1486         struct mqfs_node *pd = VTON(ap->a_dvp);
1487         struct mqfs_node *pn;
1488         int error;
1489
1490         if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
1491                 return (ENOTDIR);
1492         sx_xlock(&mqfs->mi_lock);
1493         if ((cnp->cn_flags & HASBUF) == 0)
1494                 panic("%s: no name", __func__);
1495         pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
1496                 ap->a_vap->cn_cred, ap->a_vap->va_mode);
1497         if (pn != NULL)
1498                 mqnode_addref(pn);
1499         sx_xunlock(&mqfs->mi_lock);
1500         if (pn == NULL) {
1501                 error = ENOSPC;
1502         } else {
1503                 error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
1504                 mqnode_release(pn);
1505         }
1506         return (error);
1507 }
1508
1509 #if 0
1510 struct vop_rmdir_args {
1511         struct vnode *a_dvp;
1512         struct vnode *a_vp;
1513         struct componentname *a_cnp;
1514 };
1515 #endif
1516
1517 /*
1518  * Remove a directory.
1519  */
1520 static int
1521 mqfs_rmdir(struct vop_rmdir_args *ap)
1522 {
1523         struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
1524         struct mqfs_node *pn = VTON(ap->a_vp);
1525         struct mqfs_node *pt;
1526
1527         if (pn->mn_type != mqfstype_dir)
1528                 return (ENOTDIR);
1529
1530         sx_xlock(&mqfs->mi_lock);
1531         if (pn->mn_deleted) {
1532                 sx_xunlock(&mqfs->mi_lock);
1533                 return (ENOENT);
1534         }
1535
1536         pt = LIST_FIRST(&pn->mn_children);
1537         pt = LIST_NEXT(pt, mn_sibling);
1538         pt = LIST_NEXT(pt, mn_sibling);
1539         if (pt != NULL) {
1540                 sx_xunlock(&mqfs->mi_lock);
1541                 return (ENOTEMPTY);
1542         }
1543         pt = pn->mn_parent;
1544         pn->mn_parent = NULL;
1545         pn->mn_deleted = 1;
1546         LIST_REMOVE(pn, mn_sibling);
1547         mqnode_release(pn);
1548         mqnode_release(pt);
1549         sx_xunlock(&mqfs->mi_lock);
1550         cache_purge(ap->a_vp);
1551         return (0);
1552 }
1553
1554 #endif /* notyet */
1555
1556 /*
1557  * See if this prison root is obsolete, and clean up associated queues if it is.
1558  */
1559 static int
1560 mqfs_prison_remove(void *obj, void *data __unused)
1561 {
1562         const struct prison *pr = obj;
1563         struct prison *tpr;
1564         struct mqfs_node *pn, *tpn;
1565         struct vnode *pr_root;
1566
1567         pr_root = pr->pr_root;
1568         if (pr->pr_parent->pr_root == pr_root)
1569                 return (0);
1570         TAILQ_FOREACH(tpr, &allprison, pr_list) {
1571                 if (tpr != pr && tpr->pr_root == pr_root)
1572                         return (0);
1573         }
1574         /*
1575          * No jails are rooted in this directory anymore,
1576          * so no queues should be either.
1577          */
1578         sx_xlock(&mqfs_data.mi_lock);
1579         LIST_FOREACH_SAFE(pn, &mqfs_data.mi_root->mn_children,
1580             mn_sibling, tpn) {
1581                 if (pn->mn_pr_root == pr_root)
1582                         (void)do_unlink(pn, curthread->td_ucred);
1583         }
1584         sx_xunlock(&mqfs_data.mi_lock);
1585         return (0);
1586 }
1587
1588 /*
1589  * Allocate a message queue
1590  */
1591 static struct mqueue *
1592 mqueue_alloc(const struct mq_attr *attr)
1593 {
1594         struct mqueue *mq;
1595
1596         if (curmq >= maxmq)
1597                 return (NULL);
1598         mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
1599         TAILQ_INIT(&mq->mq_msgq);
1600         if (attr != NULL) {
1601                 mq->mq_maxmsg = attr->mq_maxmsg;
1602                 mq->mq_msgsize = attr->mq_msgsize;
1603         } else {
1604                 mq->mq_maxmsg = default_maxmsg;
1605                 mq->mq_msgsize = default_msgsize;
1606         }
1607         mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
1608         knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
1609         knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
1610         atomic_add_int(&curmq, 1);
1611         return (mq);
1612 }
1613
1614 /*
1615  * Destroy a message queue
1616  */
1617 static void
1618 mqueue_free(struct mqueue *mq)
1619 {
1620         struct mqueue_msg *msg;
1621
1622         while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
1623                 TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
1624                 free(msg, M_MQUEUEDATA);
1625         }
1626
1627         mtx_destroy(&mq->mq_mutex);
1628         seldrain(&mq->mq_rsel);
1629         seldrain(&mq->mq_wsel);
1630         knlist_destroy(&mq->mq_rsel.si_note);
1631         knlist_destroy(&mq->mq_wsel.si_note);
1632         uma_zfree(mqueue_zone, mq);
1633         atomic_add_int(&curmq, -1);
1634 }
1635
1636 /*
1637  * Load a message from user space
1638  */
1639 static struct mqueue_msg *
1640 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
1641 {
1642         struct mqueue_msg *msg;
1643         size_t len;
1644         int error;
1645
1646         len = sizeof(struct mqueue_msg) + msg_size;
1647         msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
1648         error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
1649             msg_size);
1650         if (error) {
1651                 free(msg, M_MQUEUEDATA);
1652                 msg = NULL;
1653         } else {
1654                 msg->msg_size = msg_size;
1655                 msg->msg_prio = msg_prio;
1656         }
1657         return (msg);
1658 }
1659
1660 /*
1661  * Save a message to user space
1662  */
1663 static int
1664 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
1665 {
1666         int error;
1667
1668         error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
1669                 msg->msg_size);
1670         if (error == 0 && msg_prio != NULL)
1671                 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
1672         return (error);
1673 }
1674
1675 /*
1676  * Free a message's memory
1677  */
1678 static __inline void
1679 mqueue_freemsg(struct mqueue_msg *msg)
1680 {
1681         free(msg, M_MQUEUEDATA);
1682 }
1683
1684 /*
1685  * Send a message. if waitok is false, thread will not be
1686  * blocked if there is no data in queue, otherwise, absolute
1687  * time will be checked.
1688  */
1689 int
1690 mqueue_send(struct mqueue *mq, const char *msg_ptr,
1691         size_t msg_len, unsigned msg_prio, int waitok,
1692         const struct timespec *abs_timeout)
1693 {
1694         struct mqueue_msg *msg;
1695         struct timespec ts, ts2;
1696         struct timeval tv;
1697         int error;
1698
1699         if (msg_prio >= MQ_PRIO_MAX)
1700                 return (EINVAL);
1701         if (msg_len > mq->mq_msgsize)
1702                 return (EMSGSIZE);
1703         msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
1704         if (msg == NULL)
1705                 return (EFAULT);
1706
1707         /* O_NONBLOCK case */
1708         if (!waitok) {
1709                 error = _mqueue_send(mq, msg, -1);
1710                 if (error)
1711                         goto bad;
1712                 return (0);
1713         }
1714
1715         /* we allow a null timeout (wait forever) */
1716         if (abs_timeout == NULL) {
1717                 error = _mqueue_send(mq, msg, 0);
1718                 if (error)
1719                         goto bad;
1720                 return (0);
1721         }
1722
1723         /* send it before checking time */
1724         error = _mqueue_send(mq, msg, -1);
1725         if (error == 0)
1726                 return (0);
1727
1728         if (error != EAGAIN)
1729                 goto bad;
1730
1731         if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1732                 error = EINVAL;
1733                 goto bad;
1734         }
1735         for (;;) {
1736                 getnanotime(&ts);
1737                 timespecsub(abs_timeout, &ts, &ts2);
1738                 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1739                         error = ETIMEDOUT;
1740                         break;
1741                 }
1742                 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1743                 error = _mqueue_send(mq, msg, tvtohz(&tv));
1744                 if (error != ETIMEDOUT)
1745                         break;
1746         }
1747         if (error == 0)
1748                 return (0);
1749 bad:
1750         mqueue_freemsg(msg);
1751         return (error);
1752 }
1753
1754 /*
1755  * Common routine to send a message
1756  */
1757 static int
1758 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
1759 {       
1760         struct mqueue_msg *msg2;
1761         int error = 0;
1762
1763         mtx_lock(&mq->mq_mutex);
1764         while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
1765                 if (timo < 0) {
1766                         mtx_unlock(&mq->mq_mutex);
1767                         return (EAGAIN);
1768                 }
1769                 mq->mq_senders++;
1770                 error = msleep(&mq->mq_senders, &mq->mq_mutex,
1771                             PCATCH, "mqsend", timo);
1772                 mq->mq_senders--;
1773                 if (error == EAGAIN)
1774                         error = ETIMEDOUT;
1775         }
1776         if (mq->mq_curmsgs >= mq->mq_maxmsg) {
1777                 mtx_unlock(&mq->mq_mutex);
1778                 return (error);
1779         }
1780         error = 0;
1781         if (TAILQ_EMPTY(&mq->mq_msgq)) {
1782                 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
1783         } else {
1784                 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
1785                         TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
1786                 } else {
1787                         TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
1788                                 if (msg2->msg_prio < msg->msg_prio)
1789                                         break;
1790                         }
1791                         TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
1792                 }
1793         }
1794         mq->mq_curmsgs++;
1795         mq->mq_totalbytes += msg->msg_size;
1796         if (mq->mq_receivers)
1797                 wakeup_one(&mq->mq_receivers);
1798         else if (mq->mq_notifier != NULL)
1799                 mqueue_send_notification(mq);
1800         if (mq->mq_flags & MQ_RSEL) {
1801                 mq->mq_flags &= ~MQ_RSEL;
1802                 selwakeup(&mq->mq_rsel);
1803         }
1804         KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
1805         mtx_unlock(&mq->mq_mutex);
1806         return (0);
1807 }
1808
1809 /*
1810  * Send realtime a signal to process which registered itself
1811  * successfully by mq_notify.
1812  */
1813 static void
1814 mqueue_send_notification(struct mqueue *mq)
1815 {
1816         struct mqueue_notifier *nt;
1817         struct thread *td;
1818         struct proc *p;
1819         int error;
1820
1821         mtx_assert(&mq->mq_mutex, MA_OWNED);
1822         nt = mq->mq_notifier;
1823         if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
1824                 p = nt->nt_proc;
1825                 error = sigev_findtd(p, &nt->nt_sigev, &td);
1826                 if (error) {
1827                         mq->mq_notifier = NULL;
1828                         return;
1829                 }
1830                 if (!KSI_ONQ(&nt->nt_ksi)) {
1831                         ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
1832                         tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
1833                 }
1834                 PROC_UNLOCK(p);
1835         }
1836         mq->mq_notifier = NULL;
1837 }
1838
1839 /*
1840  * Get a message. if waitok is false, thread will not be
1841  * blocked if there is no data in queue, otherwise, absolute
1842  * time will be checked.
1843  */
1844 int
1845 mqueue_receive(struct mqueue *mq, char *msg_ptr,
1846         size_t msg_len, unsigned *msg_prio, int waitok,
1847         const struct timespec *abs_timeout)
1848 {
1849         struct mqueue_msg *msg;
1850         struct timespec ts, ts2;
1851         struct timeval tv;
1852         int error;
1853
1854         if (msg_len < mq->mq_msgsize)
1855                 return (EMSGSIZE);
1856
1857         /* O_NONBLOCK case */
1858         if (!waitok) {
1859                 error = _mqueue_recv(mq, &msg, -1);
1860                 if (error)
1861                         return (error);
1862                 goto received;
1863         }
1864
1865         /* we allow a null timeout (wait forever). */
1866         if (abs_timeout == NULL) {
1867                 error = _mqueue_recv(mq, &msg, 0);
1868                 if (error)
1869                         return (error);
1870                 goto received;
1871         }
1872
1873         /* try to get a message before checking time */
1874         error = _mqueue_recv(mq, &msg, -1);
1875         if (error == 0)
1876                 goto received;
1877
1878         if (error != EAGAIN)
1879                 return (error);
1880
1881         if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
1882                 error = EINVAL;
1883                 return (error);
1884         }
1885
1886         for (;;) {
1887                 getnanotime(&ts);
1888                 timespecsub(abs_timeout, &ts, &ts2);
1889                 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
1890                         error = ETIMEDOUT;
1891                         return (error);
1892                 }
1893                 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
1894                 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
1895                 if (error == 0)
1896                         break;
1897                 if (error != ETIMEDOUT)
1898                         return (error);
1899         }
1900
1901 received:
1902         error = mqueue_savemsg(msg, msg_ptr, msg_prio);
1903         if (error == 0) {
1904                 curthread->td_retval[0] = msg->msg_size;
1905                 curthread->td_retval[1] = 0;
1906         }
1907         mqueue_freemsg(msg);
1908         return (error);
1909 }
1910
1911 /*
1912  * Common routine to receive a message
1913  */
1914 static int
1915 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
1916 {       
1917         int error = 0;
1918
1919         mtx_lock(&mq->mq_mutex);
1920         while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
1921                 if (timo < 0) {
1922                         mtx_unlock(&mq->mq_mutex);
1923                         return (EAGAIN);
1924                 }
1925                 mq->mq_receivers++;
1926                 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
1927                             PCATCH, "mqrecv", timo);
1928                 mq->mq_receivers--;
1929                 if (error == EAGAIN)
1930                         error = ETIMEDOUT;
1931         }
1932         if (*msg != NULL) {
1933                 error = 0;
1934                 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
1935                 mq->mq_curmsgs--;
1936                 mq->mq_totalbytes -= (*msg)->msg_size;
1937                 if (mq->mq_senders)
1938                         wakeup_one(&mq->mq_senders);
1939                 if (mq->mq_flags & MQ_WSEL) {
1940                         mq->mq_flags &= ~MQ_WSEL;
1941                         selwakeup(&mq->mq_wsel);
1942                 }
1943                 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
1944         }
1945         if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
1946             !TAILQ_EMPTY(&mq->mq_msgq)) {
1947                 mqueue_send_notification(mq);
1948         }
1949         mtx_unlock(&mq->mq_mutex);
1950         return (error);
1951 }
1952
1953 static __inline struct mqueue_notifier *
1954 notifier_alloc(void)
1955 {
1956         return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
1957 }
1958
1959 static __inline void
1960 notifier_free(struct mqueue_notifier *p)
1961 {
1962         uma_zfree(mqnoti_zone, p);
1963 }
1964
1965 static struct mqueue_notifier *
1966 notifier_search(struct proc *p, int fd)
1967 {
1968         struct mqueue_notifier *nt;
1969
1970         LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
1971                 if (nt->nt_ksi.ksi_mqd == fd)
1972                         break;
1973         }
1974         return (nt);
1975 }
1976
1977 static __inline void
1978 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
1979 {
1980         LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
1981 }
1982
1983 static __inline void
1984 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
1985 {
1986         LIST_REMOVE(nt, nt_link);
1987         notifier_free(nt);
1988 }
1989
1990 static void
1991 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
1992 {
1993         struct mqueue_notifier *nt;
1994
1995         mtx_assert(&mq->mq_mutex, MA_OWNED);
1996         PROC_LOCK(p);
1997         nt = notifier_search(p, fd);
1998         if (nt != NULL) {
1999                 if (mq->mq_notifier == nt)
2000                         mq->mq_notifier = NULL;
2001                 sigqueue_take(&nt->nt_ksi);
2002                 notifier_delete(p, nt);
2003         }
2004         PROC_UNLOCK(p);
2005 }
2006
2007 static int
2008 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
2009     const struct mq_attr *attr)
2010 {
2011         char path[MQFS_NAMELEN + 1];
2012         struct mqfs_node *pn;
2013         struct pwddesc *pdp;
2014         struct file *fp;
2015         struct mqueue *mq;
2016         int fd, error, len, cmode;
2017
2018         AUDIT_ARG_FFLAGS(flags);
2019         AUDIT_ARG_MODE(mode);
2020
2021         pdp = td->td_proc->p_pd;
2022         cmode = (((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT);
2023         mq = NULL;
2024         if ((flags & O_CREAT) != 0 && attr != NULL) {
2025                 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
2026                         return (EINVAL);
2027                 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
2028                         return (EINVAL);
2029         }
2030
2031         error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
2032         if (error)
2033                 return (error);
2034
2035         /*
2036          * The first character of name must be a slash  (/) character
2037          * and the remaining characters of name cannot include any slash
2038          * characters. 
2039          */
2040         len = strlen(path);
2041         if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2042                 return (EINVAL);
2043         /*
2044          * "." and ".." are magic directories, populated on the fly, and cannot
2045          * be opened as queues.
2046          */
2047         if (strcmp(path, "/.") == 0 || strcmp(path, "/..") == 0)
2048                 return (EINVAL);
2049         AUDIT_ARG_UPATH1_CANON(path);
2050
2051         error = falloc(td, &fp, &fd, O_CLOEXEC);
2052         if (error)
2053                 return (error);
2054
2055         sx_xlock(&mqfs_data.mi_lock);
2056         pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred);
2057         if (pn == NULL) {
2058                 if (!(flags & O_CREAT)) {
2059                         error = ENOENT;
2060                 } else {
2061                         mq = mqueue_alloc(attr);
2062                         if (mq == NULL) {
2063                                 error = ENFILE;
2064                         } else {
2065                                 pn = mqfs_create_file(mqfs_data.mi_root,
2066                                          path + 1, len - 1, td->td_ucred,
2067                                          cmode);
2068                                 if (pn == NULL) {
2069                                         error = ENOSPC;
2070                                         mqueue_free(mq);
2071                                 }
2072                         }
2073                 }
2074
2075                 if (error == 0) {
2076                         pn->mn_data = mq;
2077                 }
2078         } else {
2079                 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
2080                         error = EEXIST;
2081                 } else {
2082                         accmode_t accmode = 0;
2083
2084                         if (flags & FREAD)
2085                                 accmode |= VREAD;
2086                         if (flags & FWRITE)
2087                                 accmode |= VWRITE;
2088                         error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
2089                             pn->mn_gid, accmode, td->td_ucred);
2090                 }
2091         }
2092
2093         if (error) {
2094                 sx_xunlock(&mqfs_data.mi_lock);
2095                 fdclose(td, fp, fd);
2096                 fdrop(fp, td);
2097                 return (error);
2098         }
2099
2100         mqnode_addref(pn);
2101         sx_xunlock(&mqfs_data.mi_lock);
2102
2103         finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
2104             &mqueueops);
2105
2106         td->td_retval[0] = fd;
2107         fdrop(fp, td);
2108         return (0);
2109 }
2110
2111 /*
2112  * Syscall to open a message queue.
2113  */
2114 int
2115 sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
2116 {
2117         struct mq_attr attr;
2118         int flags, error;
2119
2120         if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2121                 return (EINVAL);
2122         flags = FFLAGS(uap->flags);
2123         if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2124                 error = copyin(uap->attr, &attr, sizeof(attr));
2125                 if (error)
2126                         return (error);
2127         }
2128         return (kern_kmq_open(td, uap->path, flags, uap->mode,
2129             uap->attr != NULL ? &attr : NULL));
2130 }
2131
2132 /*
2133  * Syscall to unlink a message queue.
2134  */
2135 int
2136 sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
2137 {
2138         char path[MQFS_NAMELEN+1];
2139         struct mqfs_node *pn;
2140         int error, len;
2141
2142         error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
2143         if (error)
2144                 return (error);
2145
2146         len = strlen(path);
2147         if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
2148                 return (EINVAL);
2149         if (strcmp(path, "/.") == 0 || strcmp(path, "/..") == 0)
2150                 return (EINVAL);
2151         AUDIT_ARG_UPATH1_CANON(path);
2152
2153         sx_xlock(&mqfs_data.mi_lock);
2154         pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred);
2155         if (pn != NULL)
2156                 error = do_unlink(pn, td->td_ucred);
2157         else
2158                 error = ENOENT;
2159         sx_xunlock(&mqfs_data.mi_lock);
2160         return (error);
2161 }
2162
2163 typedef int (*_fgetf)(struct thread *, int, cap_rights_t *, struct file **);
2164
2165 /*
2166  * Get message queue by giving file slot
2167  */
2168 static int
2169 _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func,
2170        struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
2171 {
2172         struct mqfs_node *pn;
2173         int error;
2174
2175         error = func(td, fd, rightsp, fpp);
2176         if (error)
2177                 return (error);
2178         if (&mqueueops != (*fpp)->f_ops) {
2179                 fdrop(*fpp, td);
2180                 return (EBADF);
2181         }
2182         pn = (*fpp)->f_data;
2183         if (ppn)
2184                 *ppn = pn;
2185         if (pmq)
2186                 *pmq = pn->mn_data;
2187         return (0);
2188 }
2189
2190 static __inline int
2191 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
2192         struct mqueue **pmq)
2193 {
2194
2195         return _getmq(td, fd, &cap_event_rights, fget,
2196             fpp, ppn, pmq);
2197 }
2198
2199 static __inline int
2200 getmq_read(struct thread *td, int fd, struct file **fpp,
2201          struct mqfs_node **ppn, struct mqueue **pmq)
2202 {
2203
2204         return _getmq(td, fd, &cap_read_rights, fget_read,
2205             fpp, ppn, pmq);
2206 }
2207
2208 static __inline int
2209 getmq_write(struct thread *td, int fd, struct file **fpp,
2210         struct mqfs_node **ppn, struct mqueue **pmq)
2211 {
2212
2213         return _getmq(td, fd, &cap_write_rights, fget_write,
2214             fpp, ppn, pmq);
2215 }
2216
2217 static int
2218 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
2219     struct mq_attr *oattr)
2220 {
2221         struct mqueue *mq;
2222         struct file *fp;
2223         u_int oflag, flag;
2224         int error;
2225
2226         AUDIT_ARG_FD(mqd);
2227         if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
2228                 return (EINVAL);
2229         error = getmq(td, mqd, &fp, NULL, &mq);
2230         if (error)
2231                 return (error);
2232         oattr->mq_maxmsg  = mq->mq_maxmsg;
2233         oattr->mq_msgsize = mq->mq_msgsize;
2234         oattr->mq_curmsgs = mq->mq_curmsgs;
2235         if (attr != NULL) {
2236                 do {
2237                         oflag = flag = fp->f_flag;
2238                         flag &= ~O_NONBLOCK;
2239                         flag |= (attr->mq_flags & O_NONBLOCK);
2240                 } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
2241         } else
2242                 oflag = fp->f_flag;
2243         oattr->mq_flags = (O_NONBLOCK & oflag);
2244         fdrop(fp, td);
2245         return (error);
2246 }
2247
2248 int
2249 sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
2250 {
2251         struct mq_attr attr, oattr;
2252         int error;
2253
2254         if (uap->attr != NULL) {
2255                 error = copyin(uap->attr, &attr, sizeof(attr));
2256                 if (error != 0)
2257                         return (error);
2258         }
2259         error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2260             &oattr);
2261         if (error == 0 && uap->oattr != NULL) {
2262                 bzero(oattr.__reserved, sizeof(oattr.__reserved));
2263                 error = copyout(&oattr, uap->oattr, sizeof(oattr));
2264         }
2265         return (error);
2266 }
2267
2268 int
2269 sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
2270 {
2271         struct mqueue *mq;
2272         struct file *fp;
2273         struct timespec *abs_timeout, ets;
2274         int error;
2275         int waitok;
2276
2277         AUDIT_ARG_FD(uap->mqd);
2278         error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2279         if (error)
2280                 return (error);
2281         if (uap->abs_timeout != NULL) {
2282                 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2283                 if (error != 0)
2284                         goto out;
2285                 abs_timeout = &ets;
2286         } else
2287                 abs_timeout = NULL;
2288         waitok = !(fp->f_flag & O_NONBLOCK);
2289         error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2290                 uap->msg_prio, waitok, abs_timeout);
2291 out:
2292         fdrop(fp, td);
2293         return (error);
2294 }
2295
2296 int
2297 sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
2298 {
2299         struct mqueue *mq;
2300         struct file *fp;
2301         struct timespec *abs_timeout, ets;
2302         int error, waitok;
2303
2304         AUDIT_ARG_FD(uap->mqd);
2305         error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2306         if (error)
2307                 return (error);
2308         if (uap->abs_timeout != NULL) {
2309                 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
2310                 if (error != 0)
2311                         goto out;
2312                 abs_timeout = &ets;
2313         } else
2314                 abs_timeout = NULL;
2315         waitok = !(fp->f_flag & O_NONBLOCK);
2316         error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2317                 uap->msg_prio, waitok, abs_timeout);
2318 out:
2319         fdrop(fp, td);
2320         return (error);
2321 }
2322
2323 static int
2324 kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev)
2325 {
2326         struct filedesc *fdp;
2327         struct proc *p;
2328         struct mqueue *mq;
2329         struct file *fp, *fp2;
2330         struct mqueue_notifier *nt, *newnt = NULL;
2331         int error;
2332
2333         AUDIT_ARG_FD(mqd);
2334         if (sigev != NULL) {
2335                 if (sigev->sigev_notify != SIGEV_SIGNAL &&
2336                     sigev->sigev_notify != SIGEV_THREAD_ID &&
2337                     sigev->sigev_notify != SIGEV_NONE)
2338                         return (EINVAL);
2339                 if ((sigev->sigev_notify == SIGEV_SIGNAL ||
2340                     sigev->sigev_notify == SIGEV_THREAD_ID) &&
2341                     !_SIG_VALID(sigev->sigev_signo))
2342                         return (EINVAL);
2343         }
2344         p = td->td_proc;
2345         fdp = td->td_proc->p_fd;
2346         error = getmq(td, mqd, &fp, NULL, &mq);
2347         if (error)
2348                 return (error);
2349 again:
2350         FILEDESC_SLOCK(fdp);
2351         fp2 = fget_locked(fdp, mqd);
2352         if (fp2 == NULL) {
2353                 FILEDESC_SUNLOCK(fdp);
2354                 error = EBADF;
2355                 goto out;
2356         }
2357 #ifdef CAPABILITIES
2358         error = cap_check(cap_rights(fdp, mqd), &cap_event_rights);
2359         if (error) {
2360                 FILEDESC_SUNLOCK(fdp);
2361                 goto out;
2362         }
2363 #endif
2364         if (fp2 != fp) {
2365                 FILEDESC_SUNLOCK(fdp);
2366                 error = EBADF;
2367                 goto out;
2368         }
2369         mtx_lock(&mq->mq_mutex);
2370         FILEDESC_SUNLOCK(fdp);
2371         if (sigev != NULL) {
2372                 if (mq->mq_notifier != NULL) {
2373                         error = EBUSY;
2374                 } else {
2375                         PROC_LOCK(p);
2376                         nt = notifier_search(p, mqd);
2377                         if (nt == NULL) {
2378                                 if (newnt == NULL) {
2379                                         PROC_UNLOCK(p);
2380                                         mtx_unlock(&mq->mq_mutex);
2381                                         newnt = notifier_alloc();
2382                                         goto again;
2383                                 }
2384                         }
2385
2386                         if (nt != NULL) {
2387                                 sigqueue_take(&nt->nt_ksi);
2388                                 if (newnt != NULL) {
2389                                         notifier_free(newnt);
2390                                         newnt = NULL;
2391                                 }
2392                         } else {
2393                                 nt = newnt;
2394                                 newnt = NULL;
2395                                 ksiginfo_init(&nt->nt_ksi);
2396                                 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
2397                                 nt->nt_ksi.ksi_code = SI_MESGQ;
2398                                 nt->nt_proc = p;
2399                                 nt->nt_ksi.ksi_mqd = mqd;
2400                                 notifier_insert(p, nt);
2401                         }
2402                         nt->nt_sigev = *sigev;
2403                         mq->mq_notifier = nt;
2404                         PROC_UNLOCK(p);
2405                         /*
2406                          * if there is no receivers and message queue
2407                          * is not empty, we should send notification
2408                          * as soon as possible.
2409                          */
2410                         if (mq->mq_receivers == 0 &&
2411                             !TAILQ_EMPTY(&mq->mq_msgq))
2412                                 mqueue_send_notification(mq);
2413                 }
2414         } else {
2415                 notifier_remove(p, mq, mqd);
2416         }
2417         mtx_unlock(&mq->mq_mutex);
2418
2419 out:
2420         fdrop(fp, td);
2421         if (newnt != NULL)
2422                 notifier_free(newnt);
2423         return (error);
2424 }
2425
2426 int
2427 sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
2428 {
2429         struct sigevent ev, *evp;
2430         int error;
2431
2432         if (uap->sigev == NULL) {
2433                 evp = NULL;
2434         } else {
2435                 error = copyin(uap->sigev, &ev, sizeof(ev));
2436                 if (error != 0)
2437                         return (error);
2438                 evp = &ev;
2439         }
2440         return (kern_kmq_notify(td, uap->mqd, evp));
2441 }
2442
2443 static void
2444 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
2445 {
2446         struct mqueue *mq;
2447 #ifdef INVARIANTS
2448         struct filedesc *fdp;
2449
2450         fdp = td->td_proc->p_fd;
2451         FILEDESC_LOCK_ASSERT(fdp);
2452 #endif
2453
2454         if (fp->f_ops == &mqueueops) {
2455                 mq = FPTOMQ(fp);
2456                 mtx_lock(&mq->mq_mutex);
2457                 notifier_remove(td->td_proc, mq, fd);
2458
2459                 /* have to wakeup thread in same process */
2460                 if (mq->mq_flags & MQ_RSEL) {
2461                         mq->mq_flags &= ~MQ_RSEL;
2462                         selwakeup(&mq->mq_rsel);
2463                 }
2464                 if (mq->mq_flags & MQ_WSEL) {
2465                         mq->mq_flags &= ~MQ_WSEL;
2466                         selwakeup(&mq->mq_wsel);
2467                 }
2468                 mtx_unlock(&mq->mq_mutex);
2469         }
2470 }
2471
2472 static void
2473 mq_proc_exit(void *arg __unused, struct proc *p)
2474 {
2475         struct filedesc *fdp;
2476         struct file *fp;
2477         struct mqueue *mq;
2478         int i;
2479
2480         fdp = p->p_fd;
2481         FILEDESC_SLOCK(fdp);
2482         for (i = 0; i < fdp->fd_nfiles; ++i) {
2483                 fp = fget_locked(fdp, i);
2484                 if (fp != NULL && fp->f_ops == &mqueueops) {
2485                         mq = FPTOMQ(fp);
2486                         mtx_lock(&mq->mq_mutex);
2487                         notifier_remove(p, FPTOMQ(fp), i);
2488                         mtx_unlock(&mq->mq_mutex);
2489                 }
2490         }
2491         FILEDESC_SUNLOCK(fdp);
2492         KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
2493 }
2494
2495 static int
2496 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
2497         struct thread *td)
2498 {
2499         struct mqueue *mq = FPTOMQ(fp);
2500         int revents = 0;
2501
2502         mtx_lock(&mq->mq_mutex);
2503         if (events & (POLLIN | POLLRDNORM)) {
2504                 if (mq->mq_curmsgs) {
2505                         revents |= events & (POLLIN | POLLRDNORM);
2506                 } else {
2507                         mq->mq_flags |= MQ_RSEL;
2508                         selrecord(td, &mq->mq_rsel);
2509                 }
2510         }
2511         if (events & POLLOUT) {
2512                 if (mq->mq_curmsgs < mq->mq_maxmsg)
2513                         revents |= POLLOUT;
2514                 else {
2515                         mq->mq_flags |= MQ_WSEL;
2516                         selrecord(td, &mq->mq_wsel);
2517                 }
2518         }
2519         mtx_unlock(&mq->mq_mutex);
2520         return (revents);
2521 }
2522
2523 static int
2524 mqf_close(struct file *fp, struct thread *td)
2525 {
2526         struct mqfs_node *pn;
2527
2528         fp->f_ops = &badfileops;
2529         pn = fp->f_data;
2530         fp->f_data = NULL;
2531         sx_xlock(&mqfs_data.mi_lock);
2532         mqnode_release(pn);
2533         sx_xunlock(&mqfs_data.mi_lock);
2534         return (0);
2535 }
2536
2537 static int
2538 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2539         struct thread *td)
2540 {
2541         struct mqfs_node *pn = fp->f_data;
2542
2543         bzero(st, sizeof *st);
2544         sx_xlock(&mqfs_data.mi_lock);
2545         st->st_atim = pn->mn_atime;
2546         st->st_mtim = pn->mn_mtime;
2547         st->st_ctim = pn->mn_ctime;
2548         st->st_birthtim = pn->mn_birth;
2549         st->st_uid = pn->mn_uid;
2550         st->st_gid = pn->mn_gid;
2551         st->st_mode = S_IFIFO | pn->mn_mode;
2552         sx_xunlock(&mqfs_data.mi_lock);
2553         return (0);
2554 }
2555
2556 static int
2557 mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
2558     struct thread *td)
2559 {
2560         struct mqfs_node *pn;
2561         int error;
2562
2563         error = 0;
2564         pn = fp->f_data;
2565         sx_xlock(&mqfs_data.mi_lock);
2566         error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
2567             active_cred);
2568         if (error != 0)
2569                 goto out;
2570         pn->mn_mode = mode & ACCESSPERMS;
2571 out:
2572         sx_xunlock(&mqfs_data.mi_lock);
2573         return (error);
2574 }
2575
2576 static int
2577 mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
2578     struct thread *td)
2579 {
2580         struct mqfs_node *pn;
2581         int error;
2582
2583         error = 0;
2584         pn = fp->f_data;
2585         sx_xlock(&mqfs_data.mi_lock);
2586         if (uid == (uid_t)-1)
2587                 uid = pn->mn_uid;
2588         if (gid == (gid_t)-1)
2589                 gid = pn->mn_gid;
2590         if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
2591             (gid != pn->mn_gid && !groupmember(gid, active_cred))) &&
2592             (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN)))
2593                 goto out;
2594         pn->mn_uid = uid;
2595         pn->mn_gid = gid;
2596 out:
2597         sx_xunlock(&mqfs_data.mi_lock);
2598         return (error);
2599 }
2600
2601 static int
2602 mqf_kqfilter(struct file *fp, struct knote *kn)
2603 {
2604         struct mqueue *mq = FPTOMQ(fp);
2605         int error = 0;
2606
2607         if (kn->kn_filter == EVFILT_READ) {
2608                 kn->kn_fop = &mq_rfiltops;
2609                 knlist_add(&mq->mq_rsel.si_note, kn, 0);
2610         } else if (kn->kn_filter == EVFILT_WRITE) {
2611                 kn->kn_fop = &mq_wfiltops;
2612                 knlist_add(&mq->mq_wsel.si_note, kn, 0);
2613         } else
2614                 error = EINVAL;
2615         return (error);
2616 }
2617
2618 static void
2619 filt_mqdetach(struct knote *kn)
2620 {
2621         struct mqueue *mq = FPTOMQ(kn->kn_fp);
2622
2623         if (kn->kn_filter == EVFILT_READ)
2624                 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
2625         else if (kn->kn_filter == EVFILT_WRITE)
2626                 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
2627         else
2628                 panic("filt_mqdetach");
2629 }
2630
2631 static int
2632 filt_mqread(struct knote *kn, long hint)
2633 {
2634         struct mqueue *mq = FPTOMQ(kn->kn_fp);
2635
2636         mtx_assert(&mq->mq_mutex, MA_OWNED);
2637         return (mq->mq_curmsgs != 0);
2638 }
2639
2640 static int
2641 filt_mqwrite(struct knote *kn, long hint)
2642 {
2643         struct mqueue *mq = FPTOMQ(kn->kn_fp);
2644
2645         mtx_assert(&mq->mq_mutex, MA_OWNED);
2646         return (mq->mq_curmsgs < mq->mq_maxmsg);
2647 }
2648
2649 static int
2650 mqf_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2651 {
2652
2653         kif->kf_type = KF_TYPE_MQUEUE;
2654         return (0);
2655 }
2656
2657 static struct fileops mqueueops = {
2658         .fo_read                = invfo_rdwr,
2659         .fo_write               = invfo_rdwr,
2660         .fo_truncate            = invfo_truncate,
2661         .fo_ioctl               = invfo_ioctl,
2662         .fo_poll                = mqf_poll,
2663         .fo_kqfilter            = mqf_kqfilter,
2664         .fo_stat                = mqf_stat,
2665         .fo_close               = mqf_close,
2666         .fo_chmod               = mqf_chmod,
2667         .fo_chown               = mqf_chown,
2668         .fo_sendfile            = invfo_sendfile,
2669         .fo_fill_kinfo          = mqf_fill_kinfo,
2670         .fo_flags               = DFLAG_PASSABLE,
2671 };
2672
2673 static struct vop_vector mqfs_vnodeops = {
2674         .vop_default            = &default_vnodeops,
2675         .vop_access             = mqfs_access,
2676         .vop_cachedlookup       = mqfs_lookup,
2677         .vop_lookup             = vfs_cache_lookup,
2678         .vop_reclaim            = mqfs_reclaim,
2679         .vop_create             = mqfs_create,
2680         .vop_remove             = mqfs_remove,
2681         .vop_inactive           = mqfs_inactive,
2682         .vop_open               = mqfs_open,
2683         .vop_close              = mqfs_close,
2684         .vop_getattr            = mqfs_getattr,
2685         .vop_setattr            = mqfs_setattr,
2686         .vop_read               = mqfs_read,
2687         .vop_write              = VOP_EOPNOTSUPP,
2688         .vop_readdir            = mqfs_readdir,
2689         .vop_mkdir              = VOP_EOPNOTSUPP,
2690         .vop_rmdir              = VOP_EOPNOTSUPP
2691 };
2692 VFS_VOP_VECTOR_REGISTER(mqfs_vnodeops);
2693
2694 static struct vfsops mqfs_vfsops = {
2695         .vfs_init               = mqfs_init,
2696         .vfs_uninit             = mqfs_uninit,
2697         .vfs_mount              = mqfs_mount,
2698         .vfs_unmount            = mqfs_unmount,
2699         .vfs_root               = mqfs_root,
2700         .vfs_statfs             = mqfs_statfs,
2701 };
2702
2703 static struct vfsconf mqueuefs_vfsconf = {
2704         .vfc_version = VFS_VERSION,
2705         .vfc_name = "mqueuefs",
2706         .vfc_vfsops = &mqfs_vfsops,
2707         .vfc_typenum = -1,
2708         .vfc_flags = VFCF_SYNTHETIC
2709 };
2710
2711 static struct syscall_helper_data mq_syscalls[] = {
2712         SYSCALL_INIT_HELPER(kmq_open),
2713         SYSCALL_INIT_HELPER_F(kmq_setattr, SYF_CAPENABLED),
2714         SYSCALL_INIT_HELPER_F(kmq_timedsend, SYF_CAPENABLED),
2715         SYSCALL_INIT_HELPER_F(kmq_timedreceive, SYF_CAPENABLED),
2716         SYSCALL_INIT_HELPER_F(kmq_notify, SYF_CAPENABLED),
2717         SYSCALL_INIT_HELPER(kmq_unlink),
2718         SYSCALL_INIT_LAST
2719 };
2720
2721 #ifdef COMPAT_FREEBSD32
2722 #include <compat/freebsd32/freebsd32.h>
2723 #include <compat/freebsd32/freebsd32_proto.h>
2724 #include <compat/freebsd32/freebsd32_signal.h>
2725 #include <compat/freebsd32/freebsd32_syscall.h>
2726 #include <compat/freebsd32/freebsd32_util.h>
2727
2728 static void
2729 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
2730 {
2731
2732         to->mq_flags = from->mq_flags;
2733         to->mq_maxmsg = from->mq_maxmsg;
2734         to->mq_msgsize = from->mq_msgsize;
2735         to->mq_curmsgs = from->mq_curmsgs;
2736 }
2737
2738 static void
2739 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
2740 {
2741
2742         to->mq_flags = from->mq_flags;
2743         to->mq_maxmsg = from->mq_maxmsg;
2744         to->mq_msgsize = from->mq_msgsize;
2745         to->mq_curmsgs = from->mq_curmsgs;
2746 }
2747
2748 int
2749 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
2750 {
2751         struct mq_attr attr;
2752         struct mq_attr32 attr32;
2753         int flags, error;
2754
2755         if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC)
2756                 return (EINVAL);
2757         flags = FFLAGS(uap->flags);
2758         if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
2759                 error = copyin(uap->attr, &attr32, sizeof(attr32));
2760                 if (error)
2761                         return (error);
2762                 mq_attr_from32(&attr32, &attr);
2763         }
2764         return (kern_kmq_open(td, uap->path, flags, uap->mode,
2765             uap->attr != NULL ? &attr : NULL));
2766 }
2767
2768 int
2769 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
2770 {
2771         struct mq_attr attr, oattr;
2772         struct mq_attr32 attr32, oattr32;
2773         int error;
2774
2775         if (uap->attr != NULL) {
2776                 error = copyin(uap->attr, &attr32, sizeof(attr32));
2777                 if (error != 0)
2778                         return (error);
2779                 mq_attr_from32(&attr32, &attr);
2780         }
2781         error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
2782             &oattr);
2783         if (error == 0 && uap->oattr != NULL) {
2784                 mq_attr_to32(&oattr, &oattr32);
2785                 bzero(oattr32.__reserved, sizeof(oattr32.__reserved));
2786                 error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
2787         }
2788         return (error);
2789 }
2790
2791 int
2792 freebsd32_kmq_timedsend(struct thread *td,
2793     struct freebsd32_kmq_timedsend_args *uap)
2794 {
2795         struct mqueue *mq;
2796         struct file *fp;
2797         struct timespec32 ets32;
2798         struct timespec *abs_timeout, ets;
2799         int error;
2800         int waitok;
2801
2802         AUDIT_ARG_FD(uap->mqd);
2803         error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
2804         if (error)
2805                 return (error);
2806         if (uap->abs_timeout != NULL) {
2807                 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2808                 if (error != 0)
2809                         goto out;
2810                 CP(ets32, ets, tv_sec);
2811                 CP(ets32, ets, tv_nsec);
2812                 abs_timeout = &ets;
2813         } else
2814                 abs_timeout = NULL;
2815         waitok = !(fp->f_flag & O_NONBLOCK);
2816         error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
2817                 uap->msg_prio, waitok, abs_timeout);
2818 out:
2819         fdrop(fp, td);
2820         return (error);
2821 }
2822
2823 int
2824 freebsd32_kmq_timedreceive(struct thread *td,
2825     struct freebsd32_kmq_timedreceive_args *uap)
2826 {
2827         struct mqueue *mq;
2828         struct file *fp;
2829         struct timespec32 ets32;
2830         struct timespec *abs_timeout, ets;
2831         int error, waitok;
2832
2833         AUDIT_ARG_FD(uap->mqd);
2834         error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
2835         if (error)
2836                 return (error);
2837         if (uap->abs_timeout != NULL) {
2838                 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
2839                 if (error != 0)
2840                         goto out;
2841                 CP(ets32, ets, tv_sec);
2842                 CP(ets32, ets, tv_nsec);
2843                 abs_timeout = &ets;
2844         } else
2845                 abs_timeout = NULL;
2846         waitok = !(fp->f_flag & O_NONBLOCK);
2847         error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
2848                 uap->msg_prio, waitok, abs_timeout);
2849 out:
2850         fdrop(fp, td);
2851         return (error);
2852 }
2853
2854 int
2855 freebsd32_kmq_notify(struct thread *td, struct freebsd32_kmq_notify_args *uap)
2856 {
2857         struct sigevent ev, *evp;
2858         struct sigevent32 ev32;
2859         int error;
2860
2861         if (uap->sigev == NULL) {
2862                 evp = NULL;
2863         } else {
2864                 error = copyin(uap->sigev, &ev32, sizeof(ev32));
2865                 if (error != 0)
2866                         return (error);
2867                 error = convert_sigevent32(&ev32, &ev);
2868                 if (error != 0)
2869                         return (error);
2870                 evp = &ev;
2871         }
2872         return (kern_kmq_notify(td, uap->mqd, evp));
2873 }
2874
2875 static struct syscall_helper_data mq32_syscalls[] = {
2876         SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
2877         SYSCALL32_INIT_HELPER_F(freebsd32_kmq_setattr, SYF_CAPENABLED),
2878         SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedsend, SYF_CAPENABLED),
2879         SYSCALL32_INIT_HELPER_F(freebsd32_kmq_timedreceive, SYF_CAPENABLED),
2880         SYSCALL32_INIT_HELPER_F(freebsd32_kmq_notify, SYF_CAPENABLED),
2881         SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
2882         SYSCALL_INIT_LAST
2883 };
2884 #endif
2885
2886 static int
2887 mqinit(void)
2888 {
2889         int error;
2890
2891         error = syscall_helper_register(mq_syscalls, SY_THR_STATIC_KLD);
2892         if (error != 0)
2893                 return (error);
2894 #ifdef COMPAT_FREEBSD32
2895         error = syscall32_helper_register(mq32_syscalls, SY_THR_STATIC_KLD);
2896         if (error != 0)
2897                 return (error);
2898 #endif
2899         return (0);
2900 }
2901
2902 static int
2903 mqunload(void)
2904 {
2905
2906 #ifdef COMPAT_FREEBSD32
2907         syscall32_helper_unregister(mq32_syscalls);
2908 #endif
2909         syscall_helper_unregister(mq_syscalls);
2910         return (0);
2911 }
2912
2913 static int
2914 mq_modload(struct module *module, int cmd, void *arg)
2915 {
2916         int error = 0;
2917
2918         error = vfs_modevent(module, cmd, arg);
2919         if (error != 0)
2920                 return (error);
2921
2922         switch (cmd) {
2923         case MOD_LOAD:
2924                 error = mqinit();
2925                 if (error != 0)
2926                         mqunload();
2927                 break;
2928         case MOD_UNLOAD:
2929                 error = mqunload();
2930                 break;
2931         default:
2932                 break;
2933         }
2934         return (error);
2935 }
2936
2937 static moduledata_t mqueuefs_mod = {
2938         "mqueuefs",
2939         mq_modload,
2940         &mqueuefs_vfsconf
2941 };
2942 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
2943 MODULE_VERSION(mqueuefs, 1);