The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mqueue.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d30407ee4247298c5900ae9367e33097


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.