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.2/sys/kern/uipc_mqueue.c 206351 2010-04-07 14:49:35Z kib $");
   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         knlist_destroy(&mq->mq_rsel.si_note);
 1557         knlist_destroy(&mq->mq_wsel.si_note);
 1558         uma_zfree(mqueue_zone, mq);
 1559         atomic_add_int(&curmq, -1);
 1560 }
 1561 
 1562 /*
 1563  * Load a message from user space
 1564  */
 1565 static struct mqueue_msg *
 1566 mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
 1567 {
 1568         struct mqueue_msg *msg;
 1569         size_t len;
 1570         int error;
 1571 
 1572         len = sizeof(struct mqueue_msg) + msg_size;
 1573         msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
 1574         error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
 1575             msg_size);
 1576         if (error) {
 1577                 free(msg, M_MQUEUEDATA);
 1578                 msg = NULL;
 1579         } else {
 1580                 msg->msg_size = msg_size;
 1581                 msg->msg_prio = msg_prio;
 1582         }
 1583         return (msg);
 1584 }
 1585 
 1586 /*
 1587  * Save a message to user space
 1588  */
 1589 static int
 1590 mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
 1591 {
 1592         int error;
 1593 
 1594         error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
 1595                 msg->msg_size);
 1596         if (error == 0 && msg_prio != NULL)
 1597                 error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
 1598         return (error);
 1599 }
 1600 
 1601 /*
 1602  * Free a message's memory
 1603  */
 1604 static __inline void
 1605 mqueue_freemsg(struct mqueue_msg *msg)
 1606 {
 1607         free(msg, M_MQUEUEDATA);
 1608 }
 1609 
 1610 /*
 1611  * Send a message. if waitok is false, thread will not be
 1612  * blocked if there is no data in queue, otherwise, absolute
 1613  * time will be checked.
 1614  */
 1615 int
 1616 mqueue_send(struct mqueue *mq, const char *msg_ptr,
 1617         size_t msg_len, unsigned msg_prio, int waitok,
 1618         const struct timespec *abs_timeout)
 1619 {
 1620         struct mqueue_msg *msg;
 1621         struct timespec ts, ts2;
 1622         struct timeval tv;
 1623         int error;
 1624 
 1625         if (msg_prio >= MQ_PRIO_MAX)
 1626                 return (EINVAL);
 1627         if (msg_len > mq->mq_msgsize)
 1628                 return (EMSGSIZE);
 1629         msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
 1630         if (msg == NULL)
 1631                 return (EFAULT);
 1632 
 1633         /* O_NONBLOCK case */
 1634         if (!waitok) {
 1635                 error = _mqueue_send(mq, msg, -1);
 1636                 if (error)
 1637                         goto bad;
 1638                 return (0);
 1639         }
 1640 
 1641         /* we allow a null timeout (wait forever) */
 1642         if (abs_timeout == NULL) {
 1643                 error = _mqueue_send(mq, msg, 0);
 1644                 if (error)
 1645                         goto bad;
 1646                 return (0);
 1647         }
 1648 
 1649         /* send it before checking time */
 1650         error = _mqueue_send(mq, msg, -1);
 1651         if (error == 0)
 1652                 return (0);
 1653 
 1654         if (error != EAGAIN)
 1655                 goto bad;
 1656 
 1657         if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
 1658                 error = EINVAL;
 1659                 goto bad;
 1660         }
 1661         for (;;) {
 1662                 ts2 = *abs_timeout;
 1663                 getnanotime(&ts);
 1664                 timespecsub(&ts2, &ts);
 1665                 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
 1666                         error = ETIMEDOUT;
 1667                         break;
 1668                 }
 1669                 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
 1670                 error = _mqueue_send(mq, msg, tvtohz(&tv));
 1671                 if (error != ETIMEDOUT)
 1672                         break;
 1673         }
 1674         if (error == 0)
 1675                 return (0);
 1676 bad:
 1677         mqueue_freemsg(msg);
 1678         return (error);
 1679 }
 1680 
 1681 /*
 1682  * Common routine to send a message
 1683  */
 1684 static int
 1685 _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
 1686 {       
 1687         struct mqueue_msg *msg2;
 1688         int error = 0;
 1689 
 1690         mtx_lock(&mq->mq_mutex);
 1691         while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
 1692                 if (timo < 0) {
 1693                         mtx_unlock(&mq->mq_mutex);
 1694                         return (EAGAIN);
 1695                 }
 1696                 mq->mq_senders++;
 1697                 error = msleep(&mq->mq_senders, &mq->mq_mutex,
 1698                             PCATCH, "mqsend", timo);
 1699                 mq->mq_senders--;
 1700                 if (error == EAGAIN)
 1701                         error = ETIMEDOUT;
 1702         }
 1703         if (mq->mq_curmsgs >= mq->mq_maxmsg) {
 1704                 mtx_unlock(&mq->mq_mutex);
 1705                 return (error);
 1706         }
 1707         error = 0;
 1708         if (TAILQ_EMPTY(&mq->mq_msgq)) {
 1709                 TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
 1710         } else {
 1711                 if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
 1712                         TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
 1713                 } else {
 1714                         TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
 1715                                 if (msg2->msg_prio < msg->msg_prio)
 1716                                         break;
 1717                         }
 1718                         TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
 1719                 }
 1720         }
 1721         mq->mq_curmsgs++;
 1722         mq->mq_totalbytes += msg->msg_size;
 1723         if (mq->mq_receivers)
 1724                 wakeup_one(&mq->mq_receivers);
 1725         else if (mq->mq_notifier != NULL)
 1726                 mqueue_send_notification(mq);
 1727         if (mq->mq_flags & MQ_RSEL) {
 1728                 mq->mq_flags &= ~MQ_RSEL;
 1729                 selwakeup(&mq->mq_rsel);
 1730         }
 1731         KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
 1732         mtx_unlock(&mq->mq_mutex);
 1733         return (0);
 1734 }
 1735 
 1736 /*
 1737  * Send realtime a signal to process which registered itself
 1738  * successfully by mq_notify.
 1739  */
 1740 static void
 1741 mqueue_send_notification(struct mqueue *mq)
 1742 {
 1743         struct mqueue_notifier *nt;
 1744         struct proc *p;
 1745 
 1746         mtx_assert(&mq->mq_mutex, MA_OWNED);
 1747         nt = mq->mq_notifier;
 1748         if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
 1749                 p = nt->nt_proc;
 1750                 PROC_LOCK(p);
 1751                 if (!KSI_ONQ(&nt->nt_ksi))
 1752                         psignal_event(p, &nt->nt_sigev, &nt->nt_ksi);
 1753                 PROC_UNLOCK(p);
 1754         }
 1755         mq->mq_notifier = NULL;
 1756 }
 1757 
 1758 /*
 1759  * Get a message. if waitok is false, thread will not be
 1760  * blocked if there is no data in queue, otherwise, absolute
 1761  * time will be checked.
 1762  */
 1763 int
 1764 mqueue_receive(struct mqueue *mq, char *msg_ptr,
 1765         size_t msg_len, unsigned *msg_prio, int waitok,
 1766         const struct timespec *abs_timeout)
 1767 {
 1768         struct mqueue_msg *msg;
 1769         struct timespec ts, ts2;
 1770         struct timeval tv;
 1771         int error;
 1772 
 1773         if (msg_len < mq->mq_msgsize)
 1774                 return (EMSGSIZE);
 1775 
 1776         /* O_NONBLOCK case */
 1777         if (!waitok) {
 1778                 error = _mqueue_recv(mq, &msg, -1);
 1779                 if (error)
 1780                         return (error);
 1781                 goto received;
 1782         }
 1783 
 1784         /* we allow a null timeout (wait forever). */
 1785         if (abs_timeout == NULL) {
 1786                 error = _mqueue_recv(mq, &msg, 0);
 1787                 if (error)
 1788                         return (error);
 1789                 goto received;
 1790         }
 1791 
 1792         /* try to get a message before checking time */
 1793         error = _mqueue_recv(mq, &msg, -1);
 1794         if (error == 0)
 1795                 goto received;
 1796 
 1797         if (error != EAGAIN)
 1798                 return (error);
 1799 
 1800         if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
 1801                 error = EINVAL;
 1802                 return (error);
 1803         }
 1804 
 1805         for (;;) {
 1806                 ts2 = *abs_timeout;
 1807                 getnanotime(&ts);
 1808                 timespecsub(&ts2, &ts);
 1809                 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
 1810                         error = ETIMEDOUT;
 1811                         return (error);
 1812                 }
 1813                 TIMESPEC_TO_TIMEVAL(&tv, &ts2);
 1814                 error = _mqueue_recv(mq, &msg, tvtohz(&tv));
 1815                 if (error == 0)
 1816                         break;
 1817                 if (error != ETIMEDOUT)
 1818                         return (error);
 1819         }
 1820 
 1821 received:
 1822         error = mqueue_savemsg(msg, msg_ptr, msg_prio);
 1823         if (error == 0) {
 1824                 curthread->td_retval[0] = msg->msg_size;
 1825                 curthread->td_retval[1] = 0;
 1826         }
 1827         mqueue_freemsg(msg);
 1828         return (error);
 1829 }
 1830 
 1831 /*
 1832  * Common routine to receive a message
 1833  */
 1834 static int
 1835 _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
 1836 {       
 1837         int error = 0;
 1838         
 1839         mtx_lock(&mq->mq_mutex);
 1840         while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
 1841                 if (timo < 0) {
 1842                         mtx_unlock(&mq->mq_mutex);
 1843                         return (EAGAIN);
 1844                 }
 1845                 mq->mq_receivers++;
 1846                 error = msleep(&mq->mq_receivers, &mq->mq_mutex,
 1847                             PCATCH, "mqrecv", timo);
 1848                 mq->mq_receivers--;
 1849                 if (error == EAGAIN)
 1850                         error = ETIMEDOUT;
 1851         }
 1852         if (*msg != NULL) {
 1853                 error = 0;
 1854                 TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
 1855                 mq->mq_curmsgs--;
 1856                 mq->mq_totalbytes -= (*msg)->msg_size;
 1857                 if (mq->mq_senders)
 1858                         wakeup_one(&mq->mq_senders);
 1859                 if (mq->mq_flags & MQ_WSEL) {
 1860                         mq->mq_flags &= ~MQ_WSEL;
 1861                         selwakeup(&mq->mq_wsel);
 1862                 }
 1863                 KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
 1864         }
 1865         if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
 1866             !TAILQ_EMPTY(&mq->mq_msgq)) {
 1867                 mqueue_send_notification(mq);
 1868         }
 1869         mtx_unlock(&mq->mq_mutex);
 1870         return (error);
 1871 }
 1872 
 1873 static __inline struct mqueue_notifier *
 1874 notifier_alloc(void)
 1875 {
 1876         return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
 1877 }
 1878 
 1879 static __inline void
 1880 notifier_free(struct mqueue_notifier *p)
 1881 {
 1882         uma_zfree(mqnoti_zone, p);
 1883 }
 1884 
 1885 static struct mqueue_notifier *
 1886 notifier_search(struct proc *p, int fd)
 1887 {
 1888         struct mqueue_notifier *nt;
 1889 
 1890         LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
 1891                 if (nt->nt_ksi.ksi_mqd == fd)
 1892                         break;
 1893         }
 1894         return (nt);
 1895 }
 1896 
 1897 static __inline void
 1898 notifier_insert(struct proc *p, struct mqueue_notifier *nt)
 1899 {
 1900         LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
 1901 }
 1902 
 1903 static __inline void
 1904 notifier_delete(struct proc *p, struct mqueue_notifier *nt)
 1905 {
 1906         LIST_REMOVE(nt, nt_link);
 1907         notifier_free(nt);
 1908 }
 1909 
 1910 static void
 1911 notifier_remove(struct proc *p, struct mqueue *mq, int fd)
 1912 {
 1913         struct mqueue_notifier *nt;
 1914 
 1915         mtx_assert(&mq->mq_mutex, MA_OWNED);
 1916         PROC_LOCK(p);
 1917         nt = notifier_search(p, fd);
 1918         if (nt != NULL) {
 1919                 if (mq->mq_notifier == nt)
 1920                         mq->mq_notifier = NULL;
 1921                 sigqueue_take(&nt->nt_ksi);
 1922                 notifier_delete(p, nt);
 1923         }
 1924         PROC_UNLOCK(p);
 1925 }
 1926 
 1927 static int
 1928 kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
 1929     const struct mq_attr *attr)
 1930 {
 1931         char path[MQFS_NAMELEN + 1];
 1932         struct mqfs_node *pn;
 1933         struct filedesc *fdp;
 1934         struct file *fp;
 1935         struct mqueue *mq;
 1936         int fd, error, len, cmode;
 1937 
 1938         fdp = td->td_proc->p_fd;
 1939         cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
 1940         mq = NULL;
 1941         if ((flags & O_CREAT) != 0 && attr != NULL) {
 1942                 if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
 1943                         return (EINVAL);
 1944                 if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
 1945                         return (EINVAL);
 1946         }
 1947 
 1948         error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
 1949         if (error)
 1950                 return (error);
 1951 
 1952         /*
 1953          * The first character of name must be a slash  (/) character
 1954          * and the remaining characters of name cannot include any slash
 1955          * characters. 
 1956          */
 1957         len = strlen(path);
 1958         if (len < 2  || path[0] != '/' || index(path + 1, '/') != NULL)
 1959                 return (EINVAL);
 1960 
 1961         error = falloc(td, &fp, &fd);
 1962         if (error)
 1963                 return (error);
 1964 
 1965         sx_xlock(&mqfs_data.mi_lock);
 1966         pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
 1967         if (pn == NULL) {
 1968                 if (!(flags & O_CREAT)) {
 1969                         error = ENOENT;
 1970                 } else {
 1971                         mq = mqueue_alloc(attr);
 1972                         if (mq == NULL) {
 1973                                 error = ENFILE;
 1974                         } else {
 1975                                 pn = mqfs_create_file(mqfs_data.mi_root,
 1976                                          path + 1, len - 1, td->td_ucred,
 1977                                          cmode);
 1978                                 if (pn == NULL) {
 1979                                         error = ENOSPC;
 1980                                         mqueue_free(mq);
 1981                                 }
 1982                         }
 1983                 }
 1984 
 1985                 if (error == 0) {
 1986                         pn->mn_data = mq;
 1987                 }
 1988         } else {
 1989                 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
 1990                         error = EEXIST;
 1991                 } else {
 1992                         accmode_t accmode = 0;
 1993 
 1994                         if (flags & FREAD)
 1995                                 accmode |= VREAD;
 1996                         if (flags & FWRITE)
 1997                                 accmode |= VWRITE;
 1998                         error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
 1999                                     pn->mn_gid, accmode, td->td_ucred, NULL);
 2000                 }
 2001         }
 2002 
 2003         if (error) {
 2004                 sx_xunlock(&mqfs_data.mi_lock);
 2005                 fdclose(fdp, fp, fd, td);
 2006                 fdrop(fp, td);
 2007                 return (error);
 2008         }
 2009 
 2010         mqnode_addref(pn);
 2011         sx_xunlock(&mqfs_data.mi_lock);
 2012 
 2013         finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
 2014             &mqueueops);
 2015 
 2016         FILEDESC_XLOCK(fdp);
 2017         if (fdp->fd_ofiles[fd] == fp)
 2018                 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
 2019         FILEDESC_XUNLOCK(fdp);
 2020         td->td_retval[0] = fd;
 2021         fdrop(fp, td);
 2022         return (0);
 2023 }
 2024 
 2025 /*
 2026  * Syscall to open a message queue.
 2027  */
 2028 int
 2029 kmq_open(struct thread *td, struct kmq_open_args *uap)
 2030 {
 2031         struct mq_attr attr;
 2032         int flags, error;
 2033 
 2034         if ((uap->flags & O_ACCMODE) == O_ACCMODE)
 2035                 return (EINVAL);
 2036         flags = FFLAGS(uap->flags);
 2037         if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
 2038                 error = copyin(uap->attr, &attr, sizeof(attr));
 2039                 if (error)
 2040                         return (error);
 2041         }
 2042         return (kern_kmq_open(td, uap->path, flags, uap->mode,
 2043             uap->attr != NULL ? &attr : NULL));
 2044 }
 2045 
 2046 /*
 2047  * Syscall to unlink a message queue.
 2048  */
 2049 int
 2050 kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
 2051 {
 2052         char path[MQFS_NAMELEN+1];
 2053         struct mqfs_node *pn;
 2054         int error, len;
 2055 
 2056         error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
 2057         if (error)
 2058                 return (error);
 2059 
 2060         len = strlen(path);
 2061         if (len < 2  || path[0] != '/' || index(path + 1, '/') != NULL)
 2062                 return (EINVAL);
 2063 
 2064         sx_xlock(&mqfs_data.mi_lock);
 2065         pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
 2066         if (pn != NULL)
 2067                 error = do_unlink(pn, td->td_ucred);
 2068         else
 2069                 error = ENOENT;
 2070         sx_xunlock(&mqfs_data.mi_lock);
 2071         return (error);
 2072 }
 2073 
 2074 typedef int (*_fgetf)(struct thread *, int, struct file **);
 2075 
 2076 /*
 2077  * Get message queue by giving file slot
 2078  */
 2079 static int
 2080 _getmq(struct thread *td, int fd, _fgetf func,
 2081        struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
 2082 {
 2083         struct mqfs_node *pn;
 2084         int error;
 2085 
 2086         error = func(td, fd, fpp);
 2087         if (error)
 2088                 return (error);
 2089         if (&mqueueops != (*fpp)->f_ops) {
 2090                 fdrop(*fpp, td);
 2091                 return (EBADF);
 2092         }
 2093         pn = (*fpp)->f_data;
 2094         if (ppn)
 2095                 *ppn = pn;
 2096         if (pmq)
 2097                 *pmq = pn->mn_data;
 2098         return (0);
 2099 }
 2100 
 2101 static __inline int
 2102 getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
 2103         struct mqueue **pmq)
 2104 {
 2105         return _getmq(td, fd, fget, fpp, ppn, pmq);
 2106 }
 2107 
 2108 static __inline int
 2109 getmq_read(struct thread *td, int fd, struct file **fpp,
 2110          struct mqfs_node **ppn, struct mqueue **pmq)
 2111 {
 2112         return _getmq(td, fd, fget_read, fpp, ppn, pmq);
 2113 }
 2114 
 2115 static __inline int
 2116 getmq_write(struct thread *td, int fd, struct file **fpp,
 2117         struct mqfs_node **ppn, struct mqueue **pmq)
 2118 {
 2119         return _getmq(td, fd, fget_write, fpp, ppn, pmq);
 2120 }
 2121 
 2122 static int
 2123 kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
 2124     struct mq_attr *oattr)
 2125 {
 2126         struct mqueue *mq;
 2127         struct file *fp;
 2128         u_int oflag, flag;
 2129         int error;
 2130 
 2131         if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
 2132                 return (EINVAL);
 2133         error = getmq(td, mqd, &fp, NULL, &mq);
 2134         if (error)
 2135                 return (error);
 2136         oattr->mq_maxmsg  = mq->mq_maxmsg;
 2137         oattr->mq_msgsize = mq->mq_msgsize;
 2138         oattr->mq_curmsgs = mq->mq_curmsgs;
 2139         if (attr != NULL) {
 2140                 do {
 2141                         oflag = flag = fp->f_flag;
 2142                         flag &= ~O_NONBLOCK;
 2143                         flag |= (attr->mq_flags & O_NONBLOCK);
 2144                 } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
 2145         } else
 2146                 oflag = fp->f_flag;
 2147         oattr->mq_flags = (O_NONBLOCK & oflag);
 2148         fdrop(fp, td);
 2149         return (error);
 2150 }
 2151 
 2152 int
 2153 kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
 2154 {
 2155         struct mq_attr attr, oattr;
 2156         int error;
 2157 
 2158         if (uap->attr != NULL) {
 2159                 error = copyin(uap->attr, &attr, sizeof(attr));
 2160                 if (error != 0)
 2161                         return (error);
 2162         }
 2163         error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
 2164             &oattr);
 2165         if (error != 0)
 2166                 return (error);
 2167         if (uap->oattr != NULL)
 2168                 error = copyout(&oattr, uap->oattr, sizeof(oattr));
 2169         return (error);
 2170 }
 2171 
 2172 int
 2173 kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
 2174 {
 2175         struct mqueue *mq;
 2176         struct file *fp;
 2177         struct timespec *abs_timeout, ets;
 2178         int error;
 2179         int waitok;
 2180 
 2181         error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
 2182         if (error)
 2183                 return (error);
 2184         if (uap->abs_timeout != NULL) {
 2185                 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
 2186                 if (error != 0)
 2187                         return (error);
 2188                 abs_timeout = &ets;
 2189         } else
 2190                 abs_timeout = NULL;
 2191         waitok = !(fp->f_flag & O_NONBLOCK);
 2192         error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
 2193                 uap->msg_prio, waitok, abs_timeout);
 2194         fdrop(fp, td);
 2195         return (error);
 2196 }
 2197 
 2198 int
 2199 kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
 2200 {
 2201         struct mqueue *mq;
 2202         struct file *fp;
 2203         struct timespec *abs_timeout, ets;
 2204         int error, waitok;
 2205 
 2206         error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
 2207         if (error)
 2208                 return (error);
 2209         if (uap->abs_timeout != NULL) {
 2210                 error = copyin(uap->abs_timeout, &ets, sizeof(ets));
 2211                 if (error != 0)
 2212                         return (error);
 2213                 abs_timeout = &ets;
 2214         } else
 2215                 abs_timeout = NULL;
 2216         waitok = !(fp->f_flag & O_NONBLOCK);
 2217         error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
 2218                 uap->msg_prio, waitok, abs_timeout);
 2219         fdrop(fp, td);
 2220         return (error);
 2221 }
 2222 
 2223 int
 2224 kmq_notify(struct thread *td, struct kmq_notify_args *uap)
 2225 {
 2226         struct sigevent ev;
 2227         struct filedesc *fdp;
 2228         struct proc *p;
 2229         struct mqueue *mq;
 2230         struct file *fp;
 2231         struct mqueue_notifier *nt, *newnt = NULL;
 2232         int error;
 2233 
 2234         p = td->td_proc;
 2235         fdp = td->td_proc->p_fd;
 2236         if (uap->sigev) {
 2237                 error = copyin(uap->sigev, &ev, sizeof(ev));
 2238                 if (error)
 2239                         return (error);
 2240                 if (ev.sigev_notify != SIGEV_SIGNAL &&
 2241                     ev.sigev_notify != SIGEV_THREAD_ID &&
 2242                     ev.sigev_notify != SIGEV_NONE)
 2243                         return (EINVAL);
 2244                 if ((ev.sigev_notify == SIGEV_SIGNAL ||
 2245                      ev.sigev_notify == SIGEV_THREAD_ID) &&
 2246                         !_SIG_VALID(ev.sigev_signo))
 2247                         return (EINVAL);
 2248         }
 2249         error = getmq(td, uap->mqd, &fp, NULL, &mq);
 2250         if (error)
 2251                 return (error);
 2252 again:
 2253         FILEDESC_SLOCK(fdp);
 2254         if (fget_locked(fdp, uap->mqd) != fp) {
 2255                 FILEDESC_SUNLOCK(fdp);
 2256                 error = EBADF;
 2257                 goto out;
 2258         }
 2259         mtx_lock(&mq->mq_mutex);
 2260         FILEDESC_SUNLOCK(fdp);
 2261         if (uap->sigev != NULL) {
 2262                 if (mq->mq_notifier != NULL) {
 2263                         error = EBUSY;
 2264                 } else {
 2265                         PROC_LOCK(p);
 2266                         nt = notifier_search(p, uap->mqd);
 2267                         if (nt == NULL) {
 2268                                 if (newnt == NULL) {
 2269                                         PROC_UNLOCK(p);
 2270                                         mtx_unlock(&mq->mq_mutex);
 2271                                         newnt = notifier_alloc();
 2272                                         goto again;
 2273                                 }
 2274                         }
 2275 
 2276                         if (nt != NULL) {
 2277                                 sigqueue_take(&nt->nt_ksi);
 2278                                 if (newnt != NULL) {
 2279                                         notifier_free(newnt);
 2280                                         newnt = NULL;
 2281                                 }
 2282                         } else {
 2283                                 nt = newnt;
 2284                                 newnt = NULL;
 2285                                 ksiginfo_init(&nt->nt_ksi);
 2286                                 nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
 2287                                 nt->nt_ksi.ksi_code = SI_MESGQ;
 2288                                 nt->nt_proc = p;
 2289                                 nt->nt_ksi.ksi_mqd = uap->mqd;
 2290                                 notifier_insert(p, nt);
 2291                         }
 2292                         nt->nt_sigev = ev;
 2293                         mq->mq_notifier = nt;
 2294                         PROC_UNLOCK(p);
 2295                         /*
 2296                          * if there is no receivers and message queue
 2297                          * is not empty, we should send notification
 2298                          * as soon as possible.
 2299                          */
 2300                         if (mq->mq_receivers == 0 &&
 2301                             !TAILQ_EMPTY(&mq->mq_msgq))
 2302                                 mqueue_send_notification(mq);
 2303                 }
 2304         } else {
 2305                 notifier_remove(p, mq, uap->mqd);
 2306         }
 2307         mtx_unlock(&mq->mq_mutex);
 2308 
 2309 out:
 2310         fdrop(fp, td);
 2311         if (newnt != NULL)
 2312                 notifier_free(newnt);
 2313         return (error);
 2314 }
 2315 
 2316 static void
 2317 mqueue_fdclose(struct thread *td, int fd, struct file *fp)
 2318 {
 2319         struct filedesc *fdp;
 2320         struct mqueue *mq;
 2321  
 2322         fdp = td->td_proc->p_fd;
 2323         FILEDESC_LOCK_ASSERT(fdp);
 2324 
 2325         if (fp->f_ops == &mqueueops) {
 2326                 mq = FPTOMQ(fp);
 2327                 mtx_lock(&mq->mq_mutex);
 2328                 notifier_remove(td->td_proc, mq, fd);
 2329 
 2330                 /* have to wakeup thread in same process */
 2331                 if (mq->mq_flags & MQ_RSEL) {
 2332                         mq->mq_flags &= ~MQ_RSEL;
 2333                         selwakeup(&mq->mq_rsel);
 2334                 }
 2335                 if (mq->mq_flags & MQ_WSEL) {
 2336                         mq->mq_flags &= ~MQ_WSEL;
 2337                         selwakeup(&mq->mq_wsel);
 2338                 }
 2339                 mtx_unlock(&mq->mq_mutex);
 2340         }
 2341 }
 2342 
 2343 static void
 2344 mq_proc_exit(void *arg __unused, struct proc *p)
 2345 {
 2346         struct filedesc *fdp;
 2347         struct file *fp;
 2348         struct mqueue *mq;
 2349         int i;
 2350 
 2351         fdp = p->p_fd;
 2352         FILEDESC_SLOCK(fdp);
 2353         for (i = 0; i < fdp->fd_nfiles; ++i) {
 2354                 fp = fget_locked(fdp, i);
 2355                 if (fp != NULL && fp->f_ops == &mqueueops) {
 2356                         mq = FPTOMQ(fp);
 2357                         mtx_lock(&mq->mq_mutex);
 2358                         notifier_remove(p, FPTOMQ(fp), i);
 2359                         mtx_unlock(&mq->mq_mutex);
 2360                 }
 2361         }
 2362         FILEDESC_SUNLOCK(fdp);
 2363         KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
 2364 }
 2365 
 2366 static int
 2367 mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
 2368         int flags, struct thread *td)
 2369 {
 2370         return (EOPNOTSUPP);
 2371 }
 2372 
 2373 static int
 2374 mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
 2375         int flags, struct thread *td)
 2376 {
 2377         return (EOPNOTSUPP);
 2378 }
 2379 
 2380 static int
 2381 mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
 2382     struct thread *td)
 2383 {
 2384 
 2385         return (EINVAL);
 2386 }
 2387 
 2388 static int
 2389 mqf_ioctl(struct file *fp, u_long cmd, void *data,
 2390         struct ucred *active_cred, struct thread *td)
 2391 {
 2392         return (ENOTTY);
 2393 }
 2394 
 2395 static int
 2396 mqf_poll(struct file *fp, int events, struct ucred *active_cred,
 2397         struct thread *td)
 2398 {
 2399         struct mqueue *mq = FPTOMQ(fp);
 2400         int revents = 0;
 2401 
 2402         mtx_lock(&mq->mq_mutex);
 2403         if (events & (POLLIN | POLLRDNORM)) {
 2404                 if (mq->mq_curmsgs) {
 2405                         revents |= events & (POLLIN | POLLRDNORM);
 2406                 } else {
 2407                         mq->mq_flags |= MQ_RSEL;
 2408                         selrecord(td, &mq->mq_rsel);
 2409                 }
 2410         }
 2411         if (events & POLLOUT) {
 2412                 if (mq->mq_curmsgs < mq->mq_maxmsg)
 2413                         revents |= POLLOUT;
 2414                 else {
 2415                         mq->mq_flags |= MQ_WSEL;
 2416                         selrecord(td, &mq->mq_wsel);
 2417                 }
 2418         }
 2419         mtx_unlock(&mq->mq_mutex);
 2420         return (revents);
 2421 }
 2422 
 2423 static int
 2424 mqf_close(struct file *fp, struct thread *td)
 2425 {
 2426         struct mqfs_node *pn;
 2427 
 2428         fp->f_ops = &badfileops;
 2429         pn = fp->f_data;
 2430         fp->f_data = NULL;
 2431         sx_xlock(&mqfs_data.mi_lock);
 2432         mqnode_release(pn);
 2433         sx_xunlock(&mqfs_data.mi_lock);
 2434         return (0);
 2435 }
 2436 
 2437 static int
 2438 mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
 2439         struct thread *td)
 2440 {
 2441         struct mqfs_node *pn = fp->f_data;
 2442 
 2443         bzero(st, sizeof *st);
 2444         st->st_atimespec = pn->mn_atime;
 2445         st->st_mtimespec = pn->mn_mtime;
 2446         st->st_ctimespec = pn->mn_ctime;
 2447         st->st_birthtimespec = pn->mn_birth;
 2448         st->st_uid = pn->mn_uid;
 2449         st->st_gid = pn->mn_gid;
 2450         st->st_mode = S_IFIFO | pn->mn_mode;
 2451         return (0);
 2452 }
 2453 
 2454 static int
 2455 mqf_kqfilter(struct file *fp, struct knote *kn)
 2456 {
 2457         struct mqueue *mq = FPTOMQ(fp);
 2458         int error = 0;
 2459 
 2460         if (kn->kn_filter == EVFILT_READ) {
 2461                 kn->kn_fop = &mq_rfiltops;
 2462                 knlist_add(&mq->mq_rsel.si_note, kn, 0);
 2463         } else if (kn->kn_filter == EVFILT_WRITE) {
 2464                 kn->kn_fop = &mq_wfiltops;
 2465                 knlist_add(&mq->mq_wsel.si_note, kn, 0);
 2466         } else
 2467                 error = EINVAL;
 2468         return (error);
 2469 }
 2470 
 2471 static void
 2472 filt_mqdetach(struct knote *kn)
 2473 {
 2474         struct mqueue *mq = FPTOMQ(kn->kn_fp);
 2475 
 2476         if (kn->kn_filter == EVFILT_READ)
 2477                 knlist_remove(&mq->mq_rsel.si_note, kn, 0);
 2478         else if (kn->kn_filter == EVFILT_WRITE)
 2479                 knlist_remove(&mq->mq_wsel.si_note, kn, 0);
 2480         else
 2481                 panic("filt_mqdetach");
 2482 }
 2483 
 2484 static int
 2485 filt_mqread(struct knote *kn, long hint)
 2486 {
 2487         struct mqueue *mq = FPTOMQ(kn->kn_fp);
 2488 
 2489         mtx_assert(&mq->mq_mutex, MA_OWNED);
 2490         return (mq->mq_curmsgs != 0);
 2491 }
 2492 
 2493 static int
 2494 filt_mqwrite(struct knote *kn, long hint)
 2495 {
 2496         struct mqueue *mq = FPTOMQ(kn->kn_fp);
 2497 
 2498         mtx_assert(&mq->mq_mutex, MA_OWNED);
 2499         return (mq->mq_curmsgs < mq->mq_maxmsg);
 2500 }
 2501 
 2502 static struct fileops mqueueops = {
 2503         .fo_read                = mqf_read,
 2504         .fo_write               = mqf_write,
 2505         .fo_truncate            = mqf_truncate,
 2506         .fo_ioctl               = mqf_ioctl,
 2507         .fo_poll                = mqf_poll,
 2508         .fo_kqfilter            = mqf_kqfilter,
 2509         .fo_stat                = mqf_stat,
 2510         .fo_close               = mqf_close
 2511 };
 2512 
 2513 static struct vop_vector mqfs_vnodeops = {
 2514         .vop_default            = &default_vnodeops,
 2515         .vop_access             = mqfs_access,
 2516         .vop_cachedlookup       = mqfs_lookup,
 2517         .vop_lookup             = vfs_cache_lookup,
 2518         .vop_reclaim            = mqfs_reclaim,
 2519         .vop_create             = mqfs_create,
 2520         .vop_remove             = mqfs_remove,
 2521         .vop_inactive           = mqfs_inactive,
 2522         .vop_open               = mqfs_open,
 2523         .vop_close              = mqfs_close,
 2524         .vop_getattr            = mqfs_getattr,
 2525         .vop_setattr            = mqfs_setattr,
 2526         .vop_read               = mqfs_read,
 2527         .vop_write              = VOP_EOPNOTSUPP,
 2528         .vop_readdir            = mqfs_readdir,
 2529         .vop_mkdir              = VOP_EOPNOTSUPP,
 2530         .vop_rmdir              = VOP_EOPNOTSUPP
 2531 };
 2532 
 2533 static struct vfsops mqfs_vfsops = {
 2534         .vfs_init               = mqfs_init,
 2535         .vfs_uninit             = mqfs_uninit,
 2536         .vfs_mount              = mqfs_mount,
 2537         .vfs_unmount            = mqfs_unmount,
 2538         .vfs_root               = mqfs_root,
 2539         .vfs_statfs             = mqfs_statfs,
 2540 };
 2541 
 2542 static struct vfsconf mqueuefs_vfsconf = {
 2543         .vfc_version = VFS_VERSION,
 2544         .vfc_name = "mqueuefs",
 2545         .vfc_vfsops = &mqfs_vfsops,
 2546         .vfc_typenum = -1,
 2547         .vfc_flags = VFCF_SYNTHETIC
 2548 };
 2549 
 2550 static struct syscall_helper_data mq_syscalls[] = {
 2551         SYSCALL_INIT_HELPER(kmq_open),
 2552         SYSCALL_INIT_HELPER(kmq_setattr),
 2553         SYSCALL_INIT_HELPER(kmq_timedsend),
 2554         SYSCALL_INIT_HELPER(kmq_timedreceive),
 2555         SYSCALL_INIT_HELPER(kmq_notify),
 2556         SYSCALL_INIT_HELPER(kmq_unlink),
 2557         SYSCALL_INIT_LAST
 2558 };
 2559 
 2560 #ifdef COMPAT_FREEBSD32
 2561 #include <compat/freebsd32/freebsd32.h>
 2562 #include <compat/freebsd32/freebsd32_proto.h>
 2563 #include <compat/freebsd32/freebsd32_syscall.h>
 2564 #include <compat/freebsd32/freebsd32_util.h>
 2565 
 2566 static void
 2567 mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
 2568 {
 2569 
 2570         to->mq_flags = from->mq_flags;
 2571         to->mq_maxmsg = from->mq_maxmsg;
 2572         to->mq_msgsize = from->mq_msgsize;
 2573         to->mq_curmsgs = from->mq_curmsgs;
 2574 }
 2575 
 2576 static void
 2577 mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
 2578 {
 2579 
 2580         to->mq_flags = from->mq_flags;
 2581         to->mq_maxmsg = from->mq_maxmsg;
 2582         to->mq_msgsize = from->mq_msgsize;
 2583         to->mq_curmsgs = from->mq_curmsgs;
 2584 }
 2585 
 2586 int
 2587 freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
 2588 {
 2589         struct mq_attr attr;
 2590         struct mq_attr32 attr32;
 2591         int flags, error;
 2592 
 2593         if ((uap->flags & O_ACCMODE) == O_ACCMODE)
 2594                 return (EINVAL);
 2595         flags = FFLAGS(uap->flags);
 2596         if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
 2597                 error = copyin(uap->attr, &attr32, sizeof(attr32));
 2598                 if (error)
 2599                         return (error);
 2600                 mq_attr_from32(&attr32, &attr);
 2601         }
 2602         return (kern_kmq_open(td, uap->path, flags, uap->mode,
 2603             uap->attr != NULL ? &attr : NULL));
 2604 }
 2605 
 2606 int
 2607 freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
 2608 {
 2609         struct mq_attr attr, oattr;
 2610         struct mq_attr32 attr32, oattr32;
 2611         int error;
 2612 
 2613         if (uap->attr != NULL) {
 2614                 error = copyin(uap->attr, &attr32, sizeof(attr32));
 2615                 if (error != 0)
 2616                         return (error);
 2617                 mq_attr_from32(&attr32, &attr);
 2618         }
 2619         error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
 2620             &oattr);
 2621         if (error != 0)
 2622                 return (error);
 2623         if (uap->oattr != NULL) {
 2624                 mq_attr_to32(&oattr, &oattr32);
 2625                 error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
 2626         }
 2627         return (error);
 2628 }
 2629 
 2630 int
 2631 freebsd32_kmq_timedsend(struct thread *td,
 2632     struct freebsd32_kmq_timedsend_args *uap)
 2633 {
 2634         struct mqueue *mq;
 2635         struct file *fp;
 2636         struct timespec32 ets32;
 2637         struct timespec *abs_timeout, ets;
 2638         int error;
 2639         int waitok;
 2640 
 2641         error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
 2642         if (error)
 2643                 return (error);
 2644         if (uap->abs_timeout != NULL) {
 2645                 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
 2646                 if (error != 0)
 2647                         return (error);
 2648                 CP(ets32, ets, tv_sec);
 2649                 CP(ets32, ets, tv_nsec);
 2650                 abs_timeout = &ets;
 2651         } else
 2652                 abs_timeout = NULL;
 2653         waitok = !(fp->f_flag & O_NONBLOCK);
 2654         error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
 2655                 uap->msg_prio, waitok, abs_timeout);
 2656         fdrop(fp, td);
 2657         return (error);
 2658 }
 2659 
 2660 int
 2661 freebsd32_kmq_timedreceive(struct thread *td,
 2662     struct freebsd32_kmq_timedreceive_args *uap)
 2663 {
 2664         struct mqueue *mq;
 2665         struct file *fp;
 2666         struct timespec32 ets32;
 2667         struct timespec *abs_timeout, ets;
 2668         int error, waitok;
 2669 
 2670         error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
 2671         if (error)
 2672                 return (error);
 2673         if (uap->abs_timeout != NULL) {
 2674                 error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
 2675                 if (error != 0)
 2676                         return (error);
 2677                 CP(ets32, ets, tv_sec);
 2678                 CP(ets32, ets, tv_nsec);
 2679                 abs_timeout = &ets;
 2680         } else
 2681                 abs_timeout = NULL;
 2682         waitok = !(fp->f_flag & O_NONBLOCK);
 2683         error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
 2684                 uap->msg_prio, waitok, abs_timeout);
 2685         fdrop(fp, td);
 2686         return (error);
 2687 }
 2688 
 2689 static struct syscall_helper_data mq32_syscalls[] = {
 2690         SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
 2691         SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
 2692         SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
 2693         SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
 2694         SYSCALL32_INIT_HELPER(kmq_notify),
 2695         SYSCALL32_INIT_HELPER(kmq_unlink),
 2696         SYSCALL_INIT_LAST
 2697 };
 2698 #endif
 2699 
 2700 static int
 2701 mqinit(void)
 2702 {
 2703         int error;
 2704 
 2705         error = syscall_helper_register(mq_syscalls);
 2706         if (error != 0)
 2707                 return (error);
 2708 #ifdef COMPAT_FREEBSD32
 2709         error = syscall32_helper_register(mq32_syscalls);
 2710         if (error != 0)
 2711                 return (error);
 2712 #endif
 2713         return (0);
 2714 }
 2715 
 2716 static int
 2717 mqunload(void)
 2718 {
 2719 
 2720 #ifdef COMPAT_FREEBSD32
 2721         syscall32_helper_unregister(mq32_syscalls);
 2722 #endif
 2723         syscall_helper_unregister(mq_syscalls);
 2724         return (0);
 2725 }
 2726 
 2727 static int
 2728 mq_modload(struct module *module, int cmd, void *arg)
 2729 {
 2730         int error = 0;
 2731 
 2732         error = vfs_modevent(module, cmd, arg);
 2733         if (error != 0)
 2734                 return (error);
 2735 
 2736         switch (cmd) {
 2737         case MOD_LOAD:
 2738                 error = mqinit();
 2739                 if (error != 0)
 2740                         mqunload();
 2741                 break;
 2742         case MOD_UNLOAD:
 2743                 error = mqunload();
 2744                 break;
 2745         default:
 2746                 break;
 2747         }
 2748         return (error);
 2749 }
 2750 
 2751 static moduledata_t mqueuefs_mod = {
 2752         "mqueuefs",
 2753         mq_modload,
 2754         &mqueuefs_vfsconf
 2755 };
 2756 DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
 2757 MODULE_VERSION(mqueuefs, 1);

Cache object: 0be3e6b55863ee6ee8485244f005ad0f


[ 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.