1655291f2SDavid Xu /*- 2655291f2SDavid Xu * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 3655291f2SDavid Xu * All rights reserved. 4655291f2SDavid Xu * 5655291f2SDavid Xu * Redistribution and use in source and binary forms, with or without 6655291f2SDavid Xu * modification, are permitted provided that the following conditions 7655291f2SDavid Xu * are met: 8655291f2SDavid Xu * 1. Redistributions of source code must retain the above copyright 9655291f2SDavid Xu * notice, this list of conditions and the following disclaimer. 10655291f2SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright 11655291f2SDavid Xu * notice, this list of conditions and the following disclaimer in the 12655291f2SDavid Xu * documentation and/or other materials provided with the distribution. 13655291f2SDavid Xu * 14655291f2SDavid Xu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15655291f2SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16655291f2SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17655291f2SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18655291f2SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19655291f2SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20655291f2SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21655291f2SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22655291f2SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23655291f2SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24655291f2SDavid Xu * SUCH DAMAGE. 25655291f2SDavid Xu * 26655291f2SDavid Xu */ 27655291f2SDavid Xu 28655291f2SDavid Xu /* 29655291f2SDavid Xu * POSIX message queue implementation. 30655291f2SDavid Xu * 31655291f2SDavid Xu * 1) A mqueue filesystem can be mounted, each message queue appears 32655291f2SDavid Xu * in mounted directory, user can change queue's permission and 33655291f2SDavid Xu * ownership, or remove a queue. Manually creating a file in the 34655291f2SDavid Xu * directory causes a message queue to be created in the kernel with 35655291f2SDavid Xu * default message queue attributes applied and same name used, this 36655291f2SDavid Xu * method is not advocated since mq_open syscall allows user to specify 37655291f2SDavid Xu * different attributes. Also the file system can be mounted multiple 38655291f2SDavid Xu * times at different mount points but shows same contents. 39655291f2SDavid Xu * 40655291f2SDavid Xu * 2) Standard POSIX message queue API. The syscalls do not use vfs layer, 41655291f2SDavid Xu * but directly operate on internal data structure, this allows user to 42655291f2SDavid Xu * use the IPC facility without having to mount mqueue file system. 43655291f2SDavid Xu */ 44655291f2SDavid Xu 45655291f2SDavid Xu #include <sys/cdefs.h> 46655291f2SDavid Xu __FBSDID("$FreeBSD$"); 47655291f2SDavid Xu 48655291f2SDavid Xu #include <sys/param.h> 49655291f2SDavid Xu #include <sys/kernel.h> 50655291f2SDavid Xu #include <sys/systm.h> 51655291f2SDavid Xu #include <sys/limits.h> 52655291f2SDavid Xu #include <sys/buf.h> 53655291f2SDavid Xu #include <sys/dirent.h> 54655291f2SDavid Xu #include <sys/event.h> 55b2f92ef9SDavid Xu #include <sys/eventhandler.h> 56655291f2SDavid Xu #include <sys/fcntl.h> 57655291f2SDavid Xu #include <sys/file.h> 585ee2d4acSDavid Xu #include <sys/filedesc.h> 59655291f2SDavid Xu #include <sys/limits.h> 60655291f2SDavid Xu #include <sys/lock.h> 61655291f2SDavid Xu #include <sys/malloc.h> 62655291f2SDavid Xu #include <sys/module.h> 63655291f2SDavid Xu #include <sys/mount.h> 64655291f2SDavid Xu #include <sys/mqueue.h> 65655291f2SDavid Xu #include <sys/mutex.h> 66655291f2SDavid Xu #include <sys/namei.h> 67655291f2SDavid Xu #include <sys/poll.h> 68655291f2SDavid Xu #include <sys/proc.h> 69655291f2SDavid Xu #include <sys/queue.h> 70655291f2SDavid Xu #include <sys/sysproto.h> 71655291f2SDavid Xu #include <sys/stat.h> 72655291f2SDavid Xu #include <sys/syscall.h> 73655291f2SDavid Xu #include <sys/syscallsubr.h> 745ee2d4acSDavid Xu #include <sys/sysent.h> 75655291f2SDavid Xu #include <sys/sx.h> 76655291f2SDavid Xu #include <sys/sysctl.h> 77655291f2SDavid Xu #include <sys/taskqueue.h> 78655291f2SDavid Xu #include <sys/unistd.h> 79655291f2SDavid Xu #include <sys/vnode.h> 80655291f2SDavid Xu #include <machine/atomic.h> 815ee2d4acSDavid Xu #include <posix4/posix4.h> 82655291f2SDavid Xu 83655291f2SDavid Xu /* 84655291f2SDavid Xu * Limits and constants 85655291f2SDavid Xu */ 86655291f2SDavid Xu #define MQFS_NAMELEN NAME_MAX 87655291f2SDavid Xu #define MQFS_DELEN (8 + MQFS_NAMELEN) 88655291f2SDavid Xu 89655291f2SDavid Xu /* node types */ 90655291f2SDavid Xu typedef enum { 91655291f2SDavid Xu mqfstype_none = 0, 92655291f2SDavid Xu mqfstype_root, 93655291f2SDavid Xu mqfstype_dir, 94655291f2SDavid Xu mqfstype_this, 95655291f2SDavid Xu mqfstype_parent, 96655291f2SDavid Xu mqfstype_file, 97655291f2SDavid Xu mqfstype_symlink, 98655291f2SDavid Xu } mqfs_type_t; 99655291f2SDavid Xu 100655291f2SDavid Xu struct mqfs_node; 101655291f2SDavid Xu 102655291f2SDavid Xu /* 103655291f2SDavid Xu * mqfs_info: describes a mqfs instance 104655291f2SDavid Xu */ 105655291f2SDavid Xu struct mqfs_info { 106655291f2SDavid Xu struct sx mi_lock; 107655291f2SDavid Xu struct mqfs_node *mi_root; 108655291f2SDavid Xu struct unrhdr *mi_unrhdr; 109655291f2SDavid Xu }; 110655291f2SDavid Xu 111655291f2SDavid Xu struct mqfs_vdata { 112655291f2SDavid Xu LIST_ENTRY(mqfs_vdata) mv_link; 113655291f2SDavid Xu struct mqfs_node *mv_node; 114655291f2SDavid Xu struct vnode *mv_vnode; 115655291f2SDavid Xu struct task mv_task; 116655291f2SDavid Xu }; 117655291f2SDavid Xu 118655291f2SDavid Xu /* 119655291f2SDavid Xu * mqfs_node: describes a node (file or directory) within a mqfs 120655291f2SDavid Xu */ 121655291f2SDavid Xu struct mqfs_node { 122655291f2SDavid Xu char mn_name[MQFS_NAMELEN+1]; 123655291f2SDavid Xu struct mqfs_info *mn_info; 124655291f2SDavid Xu struct mqfs_node *mn_parent; 125655291f2SDavid Xu LIST_HEAD(,mqfs_node) mn_children; 126655291f2SDavid Xu LIST_ENTRY(mqfs_node) mn_sibling; 127655291f2SDavid Xu LIST_HEAD(,mqfs_vdata) mn_vnodes; 128655291f2SDavid Xu int mn_refcount; 129655291f2SDavid Xu mqfs_type_t mn_type; 130655291f2SDavid Xu int mn_deleted; 131655291f2SDavid Xu u_int32_t mn_fileno; 132655291f2SDavid Xu void *mn_data; 133655291f2SDavid Xu struct timespec mn_birth; 134655291f2SDavid Xu struct timespec mn_ctime; 135655291f2SDavid Xu struct timespec mn_atime; 136655291f2SDavid Xu struct timespec mn_mtime; 137655291f2SDavid Xu uid_t mn_uid; 138655291f2SDavid Xu gid_t mn_gid; 139655291f2SDavid Xu int mn_mode; 140655291f2SDavid Xu }; 141655291f2SDavid Xu 142655291f2SDavid Xu #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node) 143655291f2SDavid Xu #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data)) 144655291f2SDavid Xu #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data)) 145b2f92ef9SDavid Xu #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \ 146b2f92ef9SDavid Xu (fp)->f_data)->mn_data)) 147b2f92ef9SDavid Xu 148655291f2SDavid Xu TAILQ_HEAD(msgq, mqueue_msg); 149655291f2SDavid Xu 150655291f2SDavid Xu struct mqueue; 151b2f92ef9SDavid Xu 152b2f92ef9SDavid Xu struct mqueue_notifier { 153b2f92ef9SDavid Xu LIST_ENTRY(mqueue_notifier) nt_link; 154b2f92ef9SDavid Xu struct sigevent nt_sigev; 155b2f92ef9SDavid Xu ksiginfo_t nt_ksi; 156b2f92ef9SDavid Xu struct proc *nt_proc; 157655291f2SDavid Xu }; 158655291f2SDavid Xu 159655291f2SDavid Xu struct mqueue { 160655291f2SDavid Xu struct mtx mq_mutex; 161655291f2SDavid Xu int mq_flags; 162655291f2SDavid Xu long mq_maxmsg; 163655291f2SDavid Xu long mq_msgsize; 164655291f2SDavid Xu long mq_curmsgs; 165655291f2SDavid Xu long mq_totalbytes; 166655291f2SDavid Xu struct msgq mq_msgq; 167655291f2SDavid Xu int mq_receivers; 168655291f2SDavid Xu int mq_senders; 169655291f2SDavid Xu struct selinfo mq_rsel; 170655291f2SDavid Xu struct selinfo mq_wsel; 171b2f92ef9SDavid Xu struct mqueue_notifier *mq_notifier; 172655291f2SDavid Xu }; 173655291f2SDavid Xu 174655291f2SDavid Xu #define MQ_RSEL 0x01 175655291f2SDavid Xu #define MQ_WSEL 0x02 176655291f2SDavid Xu 177655291f2SDavid Xu struct mqueue_msg { 178655291f2SDavid Xu TAILQ_ENTRY(mqueue_msg) msg_link; 179655291f2SDavid Xu unsigned int msg_prio; 180655291f2SDavid Xu unsigned int msg_size; 181655291f2SDavid Xu /* following real data... */ 182655291f2SDavid Xu }; 183655291f2SDavid Xu 184655291f2SDavid Xu SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0, 185655291f2SDavid Xu "POSIX real time message queue"); 186655291f2SDavid Xu 187655291f2SDavid Xu static int default_maxmsg = 10; 188655291f2SDavid Xu static int default_msgsize = 1024; 189655291f2SDavid Xu 1909947b459SDavid Xu static int maxmsg = 100; 191655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW, 192655291f2SDavid Xu &maxmsg, 0, "Default maximum messages in queue"); 193655291f2SDavid Xu static int maxmsgsize = 16384; 194655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW, 195655291f2SDavid Xu &maxmsgsize, 0, "Default maximum message size"); 196655291f2SDavid Xu static int maxmq = 100; 197655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW, 198655291f2SDavid Xu &maxmq, 0, "maximum message queues"); 199655291f2SDavid Xu static int curmq = 0; 200655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW, 201655291f2SDavid Xu &curmq, 0, "current message queue number"); 202655291f2SDavid Xu static int unloadable = 0; 203655291f2SDavid Xu static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data"); 204655291f2SDavid Xu 205b2f92ef9SDavid Xu static eventhandler_tag exit_tag; 206b2f92ef9SDavid Xu 207655291f2SDavid Xu /* Only one instance per-system */ 208655291f2SDavid Xu static struct mqfs_info mqfs_data; 209655291f2SDavid Xu static uma_zone_t mqnode_zone; 210655291f2SDavid Xu static uma_zone_t mqueue_zone; 211655291f2SDavid Xu static uma_zone_t mvdata_zone; 212b2f92ef9SDavid Xu static uma_zone_t mqnoti_zone; 213655291f2SDavid Xu static struct vop_vector mqfs_vnodeops; 214655291f2SDavid Xu static struct fileops mqueueops; 215655291f2SDavid Xu 216655291f2SDavid Xu /* 217655291f2SDavid Xu * Directory structure construction and manipulation 218655291f2SDavid Xu */ 219655291f2SDavid Xu #ifdef notyet 220655291f2SDavid Xu static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent, 221655291f2SDavid Xu const char *name, int namelen); 222102178d0SDavid Xu static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent, 223102178d0SDavid Xu const char *name, int namelen); 224655291f2SDavid Xu #endif 225655291f2SDavid Xu 226655291f2SDavid Xu static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent, 227655291f2SDavid Xu const char *name, int namelen); 228655291f2SDavid Xu static int mqfs_destroy(struct mqfs_node *mn); 229655291f2SDavid Xu static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn); 230655291f2SDavid Xu static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn); 231655291f2SDavid Xu static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn); 232655291f2SDavid Xu 233655291f2SDavid Xu /* 234655291f2SDavid Xu * Message queue construction and maniplation 235655291f2SDavid Xu */ 236655291f2SDavid Xu static struct mqueue *mqueue_alloc(const struct mq_attr *attr); 237655291f2SDavid Xu static void mqueue_free(struct mqueue *mq); 238655291f2SDavid Xu static int mqueue_send(struct mqueue *mq, const char *msg_ptr, 239655291f2SDavid Xu size_t msg_len, unsigned msg_prio, int waitok, 240655291f2SDavid Xu const struct timespec *abs_timeout); 241655291f2SDavid Xu static int mqueue_receive(struct mqueue *mq, char *msg_ptr, 242655291f2SDavid Xu size_t msg_len, unsigned *msg_prio, int waitok, 243655291f2SDavid Xu const struct timespec *abs_timeout); 244655291f2SDavid Xu static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, 245655291f2SDavid Xu int timo); 246655291f2SDavid Xu static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, 247655291f2SDavid Xu int timo); 248655291f2SDavid Xu static void mqueue_send_notification(struct mqueue *mq); 249b2f92ef9SDavid Xu static void mqueue_fdclose(struct thread *td, int fd, struct file *fp); 250b2f92ef9SDavid Xu static void mq_proc_exit(void *arg, struct proc *p); 251655291f2SDavid Xu 252b2f92ef9SDavid Xu /* 253b2f92ef9SDavid Xu * kqueue filters 254b2f92ef9SDavid Xu */ 255655291f2SDavid Xu static void filt_mqdetach(struct knote *kn); 256655291f2SDavid Xu static int filt_mqread(struct knote *kn, long hint); 257655291f2SDavid Xu static int filt_mqwrite(struct knote *kn, long hint); 258655291f2SDavid Xu 259655291f2SDavid Xu struct filterops mq_rfiltops = 260655291f2SDavid Xu { 1, NULL, filt_mqdetach, filt_mqread }; 261655291f2SDavid Xu struct filterops mq_wfiltops = 262655291f2SDavid Xu { 1, NULL, filt_mqdetach, filt_mqwrite }; 263655291f2SDavid Xu 264655291f2SDavid Xu /* 265655291f2SDavid Xu * Initialize fileno bitmap 266655291f2SDavid Xu */ 267655291f2SDavid Xu static void 268655291f2SDavid Xu mqfs_fileno_init(struct mqfs_info *mi) 269655291f2SDavid Xu { 270655291f2SDavid Xu struct unrhdr *up; 271655291f2SDavid Xu 272655291f2SDavid Xu up = new_unrhdr(1, INT_MAX, NULL); 273655291f2SDavid Xu mi->mi_unrhdr = up; 274655291f2SDavid Xu } 275655291f2SDavid Xu 276655291f2SDavid Xu /* 277655291f2SDavid Xu * Tear down fileno bitmap 278655291f2SDavid Xu */ 279655291f2SDavid Xu static void 280655291f2SDavid Xu mqfs_fileno_uninit(struct mqfs_info *mi) 281655291f2SDavid Xu { 282655291f2SDavid Xu struct unrhdr *up; 283655291f2SDavid Xu 284655291f2SDavid Xu up = mi->mi_unrhdr; 285655291f2SDavid Xu mi->mi_unrhdr = NULL; 286655291f2SDavid Xu delete_unrhdr(up); 287655291f2SDavid Xu } 288655291f2SDavid Xu 289655291f2SDavid Xu /* 290655291f2SDavid Xu * Allocate a file number 291655291f2SDavid Xu */ 292b2f92ef9SDavid Xu static void 293655291f2SDavid Xu mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn) 294655291f2SDavid Xu { 295655291f2SDavid Xu /* make sure our parent has a file number */ 296655291f2SDavid Xu if (mn->mn_parent && !mn->mn_parent->mn_fileno) 297655291f2SDavid Xu mqfs_fileno_alloc(mi, mn->mn_parent); 298655291f2SDavid Xu 299655291f2SDavid Xu switch (mn->mn_type) { 300655291f2SDavid Xu case mqfstype_root: 301655291f2SDavid Xu case mqfstype_dir: 302655291f2SDavid Xu case mqfstype_file: 303655291f2SDavid Xu case mqfstype_symlink: 304655291f2SDavid Xu mn->mn_fileno = alloc_unr(mi->mi_unrhdr); 305655291f2SDavid Xu break; 306655291f2SDavid Xu case mqfstype_this: 307655291f2SDavid Xu KASSERT(mn->mn_parent != NULL, 308655291f2SDavid Xu ("mqfstype_this node has no parent")); 309655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_fileno; 310655291f2SDavid Xu break; 311655291f2SDavid Xu case mqfstype_parent: 312655291f2SDavid Xu KASSERT(mn->mn_parent != NULL, 313655291f2SDavid Xu ("mqfstype_parent node has no parent")); 314655291f2SDavid Xu if (mn->mn_parent == mi->mi_root) { 315655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_fileno; 316655291f2SDavid Xu break; 317655291f2SDavid Xu } 318655291f2SDavid Xu KASSERT(mn->mn_parent->mn_parent != NULL, 319655291f2SDavid Xu ("mqfstype_parent node has no grandparent")); 320655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno; 321655291f2SDavid Xu break; 322655291f2SDavid Xu default: 323655291f2SDavid Xu KASSERT(0, 324655291f2SDavid Xu ("mqfs_fileno_alloc() called for unknown type node: %d", 325655291f2SDavid Xu mn->mn_type)); 326655291f2SDavid Xu break; 327655291f2SDavid Xu } 328655291f2SDavid Xu } 329655291f2SDavid Xu 330655291f2SDavid Xu /* 331655291f2SDavid Xu * Release a file number 332655291f2SDavid Xu */ 333b2f92ef9SDavid Xu static void 334655291f2SDavid Xu mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn) 335655291f2SDavid Xu { 336655291f2SDavid Xu switch (mn->mn_type) { 337655291f2SDavid Xu case mqfstype_root: 338655291f2SDavid Xu case mqfstype_dir: 339655291f2SDavid Xu case mqfstype_file: 340655291f2SDavid Xu case mqfstype_symlink: 341655291f2SDavid Xu free_unr(mi->mi_unrhdr, mn->mn_fileno); 342655291f2SDavid Xu break; 343655291f2SDavid Xu case mqfstype_this: 344655291f2SDavid Xu case mqfstype_parent: 345655291f2SDavid Xu /* ignore these, as they don't "own" their file number */ 346655291f2SDavid Xu break; 347655291f2SDavid Xu default: 348655291f2SDavid Xu KASSERT(0, 349655291f2SDavid Xu ("mqfs_fileno_free() called for unknown type node: %d", 350655291f2SDavid Xu mn->mn_type)); 351655291f2SDavid Xu break; 352655291f2SDavid Xu } 353655291f2SDavid Xu } 354655291f2SDavid Xu 355655291f2SDavid Xu static __inline struct mqfs_node * 356655291f2SDavid Xu mqnode_alloc(void) 357655291f2SDavid Xu { 358655291f2SDavid Xu return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO); 359655291f2SDavid Xu } 360655291f2SDavid Xu 361655291f2SDavid Xu static __inline void 362655291f2SDavid Xu mqnode_free(struct mqfs_node *node) 363655291f2SDavid Xu { 364655291f2SDavid Xu uma_zfree(mqnode_zone, node); 365655291f2SDavid Xu } 366655291f2SDavid Xu 367655291f2SDavid Xu static __inline void 368655291f2SDavid Xu mqnode_addref(struct mqfs_node *node) 369655291f2SDavid Xu { 370655291f2SDavid Xu atomic_fetchadd_int(&node->mn_refcount, 1); 371655291f2SDavid Xu } 372655291f2SDavid Xu 373655291f2SDavid Xu static __inline void 374655291f2SDavid Xu mqnode_release(struct mqfs_node *node) 375655291f2SDavid Xu { 376655291f2SDavid Xu int old, exp; 377655291f2SDavid Xu 378655291f2SDavid Xu old = atomic_fetchadd_int(&node->mn_refcount, -1); 379655291f2SDavid Xu if (node->mn_type == mqfstype_dir || 380655291f2SDavid Xu node->mn_type == mqfstype_root) 381655291f2SDavid Xu exp = 3; /* include . and .. */ 382655291f2SDavid Xu else 383655291f2SDavid Xu exp = 1; 384655291f2SDavid Xu if (old == exp) 385655291f2SDavid Xu mqfs_destroy(node); 386655291f2SDavid Xu } 387655291f2SDavid Xu 388655291f2SDavid Xu /* 389655291f2SDavid Xu * Add a node to a directory 390655291f2SDavid Xu */ 391655291f2SDavid Xu static int 392655291f2SDavid Xu mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node) 393655291f2SDavid Xu { 394655291f2SDavid Xu KASSERT(parent != NULL, ("%s(): parent is NULL", __func__)); 395655291f2SDavid Xu KASSERT(parent->mn_info != NULL, 396655291f2SDavid Xu ("%s(): parent has no mn_info", __func__)); 397655291f2SDavid Xu KASSERT(parent->mn_type == mqfstype_dir || 398655291f2SDavid Xu parent->mn_type == mqfstype_root, 399655291f2SDavid Xu ("%s(): parent is not a directory", __func__)); 400655291f2SDavid Xu 401655291f2SDavid Xu node->mn_info = parent->mn_info; 402655291f2SDavid Xu node->mn_parent = parent; 403655291f2SDavid Xu LIST_INIT(&node->mn_children); 404655291f2SDavid Xu LIST_INIT(&node->mn_vnodes); 405655291f2SDavid Xu LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling); 406655291f2SDavid Xu mqnode_addref(parent); 407655291f2SDavid Xu return (0); 408655291f2SDavid Xu } 409655291f2SDavid Xu 410655291f2SDavid Xu /* 411655291f2SDavid Xu * Add . and .. to a directory 412655291f2SDavid Xu */ 413655291f2SDavid Xu static int 414655291f2SDavid Xu mqfs_fixup_dir(struct mqfs_node *parent) 415655291f2SDavid Xu { 416655291f2SDavid Xu struct mqfs_node *dir; 417655291f2SDavid Xu 418655291f2SDavid Xu dir = mqnode_alloc(); 419655291f2SDavid Xu dir->mn_name[0] = '.'; 420655291f2SDavid Xu dir->mn_type = mqfstype_this; 421655291f2SDavid Xu dir->mn_refcount = 1; 422655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 423655291f2SDavid Xu mqnode_free(dir); 424655291f2SDavid Xu return (-1); 425655291f2SDavid Xu } 426655291f2SDavid Xu 427655291f2SDavid Xu dir = mqnode_alloc(); 428655291f2SDavid Xu dir->mn_name[0] = dir->mn_name[1] = '.'; 429655291f2SDavid Xu dir->mn_type = mqfstype_parent; 430655291f2SDavid Xu dir->mn_refcount = 1; 431655291f2SDavid Xu 432655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 433655291f2SDavid Xu mqnode_free(dir); 434655291f2SDavid Xu return (-1); 435655291f2SDavid Xu } 436655291f2SDavid Xu 437655291f2SDavid Xu return (0); 438655291f2SDavid Xu } 439655291f2SDavid Xu 440655291f2SDavid Xu #ifdef notyet 441655291f2SDavid Xu 442655291f2SDavid Xu /* 443655291f2SDavid Xu * Create a directory 444655291f2SDavid Xu */ 445b2f92ef9SDavid Xu static struct mqfs_node * 446655291f2SDavid Xu mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen) 447655291f2SDavid Xu { 448655291f2SDavid Xu struct mqfs_node *dir; 449655291f2SDavid Xu 450655291f2SDavid Xu dir = mqnode_alloc(); 451655291f2SDavid Xu strncpy(dir->mn_name, name, namelen); 452655291f2SDavid Xu dir->mn_type = mqfstype_dir; 453655291f2SDavid Xu dir->mn_refcount = 1; 454655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 455655291f2SDavid Xu mqnode_free(dir); 456655291f2SDavid Xu return (NULL); 457655291f2SDavid Xu } 458655291f2SDavid Xu 459655291f2SDavid Xu if (mqfs_fixup_dir(dir) != 0) { 460655291f2SDavid Xu mqfs_destroy(dir); 461655291f2SDavid Xu return (NULL); 462655291f2SDavid Xu } 463655291f2SDavid Xu 464655291f2SDavid Xu return (dir); 465655291f2SDavid Xu } 466b2f92ef9SDavid Xu 467b2f92ef9SDavid Xu /* 468b2f92ef9SDavid Xu * Create a symlink 469b2f92ef9SDavid Xu */ 470b2f92ef9SDavid Xu static struct mqfs_node * 471b2f92ef9SDavid Xu mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen) 472b2f92ef9SDavid Xu { 473b2f92ef9SDavid Xu struct mqfs_node *node; 474b2f92ef9SDavid Xu 475b2f92ef9SDavid Xu node = mqfs_create_file(parent, name, namelen); 476b2f92ef9SDavid Xu if (node == NULL) 477b2f92ef9SDavid Xu return (NULL); 478b2f92ef9SDavid Xu node->mn_type = mqfstype_symlink; 479b2f92ef9SDavid Xu return (node); 480b2f92ef9SDavid Xu } 481b2f92ef9SDavid Xu 482655291f2SDavid Xu #endif 483655291f2SDavid Xu 484655291f2SDavid Xu /* 485655291f2SDavid Xu * Create a file 486655291f2SDavid Xu */ 487b2f92ef9SDavid Xu static struct mqfs_node * 488655291f2SDavid Xu mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen) 489655291f2SDavid Xu { 490655291f2SDavid Xu struct mqfs_node *node; 491655291f2SDavid Xu 492655291f2SDavid Xu node = mqnode_alloc(); 493655291f2SDavid Xu strncpy(node->mn_name, name, namelen); 494655291f2SDavid Xu node->mn_type = mqfstype_file; 495655291f2SDavid Xu node->mn_refcount = 1; 496655291f2SDavid Xu 497655291f2SDavid Xu if (mqfs_add_node(parent, node) != 0) { 498655291f2SDavid Xu mqnode_free(node); 499655291f2SDavid Xu return (NULL); 500655291f2SDavid Xu } 501655291f2SDavid Xu return (node); 502655291f2SDavid Xu } 503655291f2SDavid Xu 504655291f2SDavid Xu /* 505655291f2SDavid Xu * Destroy a node or a tree of nodes 506655291f2SDavid Xu */ 507b2f92ef9SDavid Xu static int 508655291f2SDavid Xu mqfs_destroy(struct mqfs_node *node) 509655291f2SDavid Xu { 510655291f2SDavid Xu struct mqfs_node *parent; 511655291f2SDavid Xu 512655291f2SDavid Xu KASSERT(node != NULL, 513655291f2SDavid Xu ("%s(): node is NULL", __func__)); 514655291f2SDavid Xu KASSERT(node->mn_info != NULL, 515655291f2SDavid Xu ("%s(): node has no mn_info", __func__)); 516655291f2SDavid Xu 517655291f2SDavid Xu /* destroy children */ 518655291f2SDavid Xu if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root) 519655291f2SDavid Xu while (! LIST_EMPTY(&node->mn_children)) 520655291f2SDavid Xu mqfs_destroy(LIST_FIRST(&node->mn_children)); 521655291f2SDavid Xu 522655291f2SDavid Xu /* unlink from parent */ 523655291f2SDavid Xu if ((parent = node->mn_parent) != NULL) { 524655291f2SDavid Xu KASSERT(parent->mn_info == node->mn_info, 525655291f2SDavid Xu ("%s(): parent has different mn_info", __func__)); 526655291f2SDavid Xu LIST_REMOVE(node, mn_sibling); 527655291f2SDavid Xu } 528655291f2SDavid Xu 529655291f2SDavid Xu if (node->mn_fileno != 0) 530655291f2SDavid Xu mqfs_fileno_free(node->mn_info, node); 531655291f2SDavid Xu if (node->mn_data != NULL) 532655291f2SDavid Xu mqueue_free(node->mn_data); 533655291f2SDavid Xu mqnode_free(node); 534655291f2SDavid Xu return (0); 535655291f2SDavid Xu } 536655291f2SDavid Xu 537655291f2SDavid Xu /* 538655291f2SDavid Xu * Mount a mqfs instance 539655291f2SDavid Xu */ 540655291f2SDavid Xu static int 541655291f2SDavid Xu mqfs_mount(struct mount *mp, struct thread *td) 542655291f2SDavid Xu { 543655291f2SDavid Xu struct statfs *sbp; 544655291f2SDavid Xu 545655291f2SDavid Xu if (mp->mnt_flag & MNT_UPDATE) 546655291f2SDavid Xu return (EOPNOTSUPP); 547655291f2SDavid Xu 548655291f2SDavid Xu mp->mnt_data = &mqfs_data; 549655291f2SDavid Xu mp->mnt_flag |= MNT_LOCAL; 5509da8a32aSDavid Xu mp->mnt_kern_flag |= MNTK_MPSAFE; 551655291f2SDavid Xu vfs_getnewfsid(mp); 552655291f2SDavid Xu 553655291f2SDavid Xu sbp = &mp->mnt_stat; 554655291f2SDavid Xu vfs_mountedfrom(mp, "mqueue"); 555655291f2SDavid Xu sbp->f_bsize = PAGE_SIZE; 556655291f2SDavid Xu sbp->f_iosize = PAGE_SIZE; 557655291f2SDavid Xu sbp->f_blocks = 1; 558655291f2SDavid Xu sbp->f_bfree = 0; 559655291f2SDavid Xu sbp->f_bavail = 0; 560655291f2SDavid Xu sbp->f_files = 1; 561655291f2SDavid Xu sbp->f_ffree = 0; 562655291f2SDavid Xu return (0); 563655291f2SDavid Xu } 564655291f2SDavid Xu 565655291f2SDavid Xu /* 566655291f2SDavid Xu * Unmount a mqfs instance 567655291f2SDavid Xu */ 568655291f2SDavid Xu static int 569655291f2SDavid Xu mqfs_unmount(struct mount *mp, int mntflags, struct thread *td) 570655291f2SDavid Xu { 571655291f2SDavid Xu int error; 572655291f2SDavid Xu 573655291f2SDavid Xu error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, td); 574655291f2SDavid Xu return (error); 575655291f2SDavid Xu } 576655291f2SDavid Xu 577655291f2SDavid Xu /* 578655291f2SDavid Xu * Return a root vnode 579655291f2SDavid Xu */ 580655291f2SDavid Xu static int 581655291f2SDavid Xu mqfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 582655291f2SDavid Xu { 583655291f2SDavid Xu struct mqfs_info *mqfs; 584655291f2SDavid Xu int ret; 585655291f2SDavid Xu 586655291f2SDavid Xu mqfs = VFSTOMQFS(mp); 587655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 588655291f2SDavid Xu ret = mqfs_allocv(mp, vpp, mqfs->mi_root); 589655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 590655291f2SDavid Xu return (ret); 591655291f2SDavid Xu } 592655291f2SDavid Xu 593655291f2SDavid Xu /* 594655291f2SDavid Xu * Return filesystem stats 595655291f2SDavid Xu */ 596655291f2SDavid Xu static int 597655291f2SDavid Xu mqfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 598655291f2SDavid Xu { 599655291f2SDavid Xu /* XXX update statistics */ 600655291f2SDavid Xu return (0); 601655291f2SDavid Xu } 602655291f2SDavid Xu 603655291f2SDavid Xu /* 604655291f2SDavid Xu * Initialize a mqfs instance 605655291f2SDavid Xu */ 606655291f2SDavid Xu static int 607655291f2SDavid Xu mqfs_init(struct vfsconf *vfc) 608655291f2SDavid Xu { 609655291f2SDavid Xu struct mqfs_node *root; 610655291f2SDavid Xu struct mqfs_info *mi; 611655291f2SDavid Xu 612655291f2SDavid Xu mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node), 613655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 614655291f2SDavid Xu mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue), 615655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 616655291f2SDavid Xu mvdata_zone = uma_zcreate("mvdata", 617655291f2SDavid Xu sizeof(struct mqfs_vdata), NULL, NULL, NULL, 618655291f2SDavid Xu NULL, UMA_ALIGN_PTR, 0); 619b2f92ef9SDavid Xu mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier), 620655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 621655291f2SDavid Xu mi = &mqfs_data; 622655291f2SDavid Xu sx_init(&mi->mi_lock, "mqfs lock"); 623655291f2SDavid Xu /* set up the root diretory */ 624655291f2SDavid Xu root = mqnode_alloc(); 625655291f2SDavid Xu root->mn_type = mqfstype_root; 626655291f2SDavid Xu root->mn_refcount = 1; 627655291f2SDavid Xu root->mn_name[0] = '/'; 628655291f2SDavid Xu root->mn_info = mi; 629655291f2SDavid Xu LIST_INIT(&root->mn_children); 630655291f2SDavid Xu LIST_INIT(&root->mn_vnodes); 631655291f2SDavid Xu root->mn_mode = 01777; 632655291f2SDavid Xu mi->mi_root = root; 633655291f2SDavid Xu mqfs_fileno_init(mi); 634655291f2SDavid Xu mqfs_fileno_alloc(mi, root); 635655291f2SDavid Xu mqfs_fixup_dir(root); 636b2f92ef9SDavid Xu exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL, 637b2f92ef9SDavid Xu EVENTHANDLER_PRI_ANY); 638b2f92ef9SDavid Xu mq_fdclose = mqueue_fdclose; 6395ee2d4acSDavid Xu p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING); 640655291f2SDavid Xu return (0); 641655291f2SDavid Xu } 642655291f2SDavid Xu 643655291f2SDavid Xu /* 644655291f2SDavid Xu * Destroy a mqfs instance 645655291f2SDavid Xu */ 646655291f2SDavid Xu static int 647655291f2SDavid Xu mqfs_uninit(struct vfsconf *vfc) 648655291f2SDavid Xu { 649655291f2SDavid Xu struct mqfs_info *mi; 650655291f2SDavid Xu 651655291f2SDavid Xu if (!unloadable) 652655291f2SDavid Xu return (EOPNOTSUPP); 653b2f92ef9SDavid Xu EVENTHANDLER_DEREGISTER(process_exit, exit_tag); 654655291f2SDavid Xu mi = &mqfs_data; 655655291f2SDavid Xu mqfs_destroy(mi->mi_root); 656655291f2SDavid Xu mi->mi_root = NULL; 657655291f2SDavid Xu mqfs_fileno_uninit(mi); 658655291f2SDavid Xu sx_destroy(&mi->mi_lock); 659b2f92ef9SDavid Xu uma_zdestroy(mqnode_zone); 660b2f92ef9SDavid Xu uma_zdestroy(mqueue_zone); 661b2f92ef9SDavid Xu uma_zdestroy(mvdata_zone); 662b2f92ef9SDavid Xu uma_zdestroy(mqnoti_zone); 663655291f2SDavid Xu return (0); 664655291f2SDavid Xu } 665655291f2SDavid Xu 666655291f2SDavid Xu /* 667655291f2SDavid Xu * task routine 668655291f2SDavid Xu */ 669655291f2SDavid Xu static void 670655291f2SDavid Xu do_recycle(void *context, int pending __unused) 671655291f2SDavid Xu { 672655291f2SDavid Xu struct vnode *vp = (struct vnode *)context; 673655291f2SDavid Xu 674655291f2SDavid Xu vrecycle(vp, curthread); 675655291f2SDavid Xu vdrop(vp); 676655291f2SDavid Xu } 677655291f2SDavid Xu 678655291f2SDavid Xu /* 679655291f2SDavid Xu * Allocate a vnode 680655291f2SDavid Xu */ 681b2f92ef9SDavid Xu static int 682655291f2SDavid Xu mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn) 683655291f2SDavid Xu { 684655291f2SDavid Xu struct mqfs_vdata *vd; 685655291f2SDavid Xu int error; 686655291f2SDavid Xu 687655291f2SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 688655291f2SDavid Xu if (vd->mv_vnode->v_mount == mp) 689655291f2SDavid Xu break; 690655291f2SDavid Xu } 691655291f2SDavid Xu 692655291f2SDavid Xu if (vd != NULL) { 693655291f2SDavid Xu if (vget(vd->mv_vnode, 0, curthread) == 0) { 694655291f2SDavid Xu *vpp = vd->mv_vnode; 695655291f2SDavid Xu vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE, 696655291f2SDavid Xu curthread); 697655291f2SDavid Xu return (0); 698655291f2SDavid Xu } 699655291f2SDavid Xu /* XXX if this can happen, we're in trouble */ 700655291f2SDavid Xu } 701655291f2SDavid Xu 702655291f2SDavid Xu error = getnewvnode("mqueue", mp, &mqfs_vnodeops, vpp); 703655291f2SDavid Xu if (error) 704655291f2SDavid Xu return (error); 705655291f2SDavid Xu vd = uma_zalloc(mvdata_zone, M_WAITOK); 706655291f2SDavid Xu (*vpp)->v_data = vd; 707655291f2SDavid Xu vd->mv_vnode = *vpp; 708655291f2SDavid Xu vd->mv_node = pn; 709655291f2SDavid Xu TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp); 710655291f2SDavid Xu LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link); 711655291f2SDavid Xu mqnode_addref(pn); 712655291f2SDavid Xu switch (pn->mn_type) { 713655291f2SDavid Xu case mqfstype_root: 714655291f2SDavid Xu (*vpp)->v_vflag = VV_ROOT; 715655291f2SDavid Xu /* fall through */ 716655291f2SDavid Xu case mqfstype_dir: 717655291f2SDavid Xu case mqfstype_this: 718655291f2SDavid Xu case mqfstype_parent: 719655291f2SDavid Xu (*vpp)->v_type = VDIR; 720655291f2SDavid Xu break; 721655291f2SDavid Xu case mqfstype_file: 722655291f2SDavid Xu (*vpp)->v_type = VREG; 723655291f2SDavid Xu break; 724655291f2SDavid Xu case mqfstype_symlink: 725655291f2SDavid Xu (*vpp)->v_type = VLNK; 726655291f2SDavid Xu break; 727655291f2SDavid Xu case mqfstype_none: 728655291f2SDavid Xu KASSERT(0, ("mqfs_allocf called for null node\n")); 729655291f2SDavid Xu default: 730655291f2SDavid Xu panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type); 731655291f2SDavid Xu } 732655291f2SDavid Xu vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread); 733655291f2SDavid Xu return (0); 734655291f2SDavid Xu } 735655291f2SDavid Xu 736655291f2SDavid Xu /* 737655291f2SDavid Xu * Search a directory entry 738655291f2SDavid Xu */ 739655291f2SDavid Xu static struct mqfs_node * 740655291f2SDavid Xu mqfs_search(struct mqfs_node *pd, const char *name, int len) 741655291f2SDavid Xu { 742655291f2SDavid Xu struct mqfs_node *pn; 743655291f2SDavid Xu 744655291f2SDavid Xu LIST_FOREACH(pn, &pd->mn_children, mn_sibling) { 745655291f2SDavid Xu if (strncmp(pn->mn_name, name, len) == 0) 746655291f2SDavid Xu return (pn); 747655291f2SDavid Xu } 748655291f2SDavid Xu return (NULL); 749655291f2SDavid Xu } 750655291f2SDavid Xu 751655291f2SDavid Xu /* 752655291f2SDavid Xu * Look up a file or directory 753655291f2SDavid Xu */ 754655291f2SDavid Xu static int 755655291f2SDavid Xu mqfs_lookupx(struct vop_cachedlookup_args *ap) 756655291f2SDavid Xu { 757655291f2SDavid Xu struct componentname *cnp; 758655291f2SDavid Xu struct vnode *dvp, **vpp; 759655291f2SDavid Xu struct mqfs_node *pd; 760655291f2SDavid Xu struct mqfs_node *pn; 761655291f2SDavid Xu int nameiop, flags, error, namelen; 762655291f2SDavid Xu char *pname; 763655291f2SDavid Xu struct thread *td; 764655291f2SDavid Xu 765655291f2SDavid Xu cnp = ap->a_cnp; 766655291f2SDavid Xu vpp = ap->a_vpp; 767655291f2SDavid Xu dvp = ap->a_dvp; 768655291f2SDavid Xu pname = cnp->cn_nameptr; 769655291f2SDavid Xu namelen = cnp->cn_namelen; 770655291f2SDavid Xu td = cnp->cn_thread; 771655291f2SDavid Xu flags = cnp->cn_flags; 772655291f2SDavid Xu nameiop = cnp->cn_nameiop; 773655291f2SDavid Xu pd = VTON(dvp); 774655291f2SDavid Xu pn = NULL; 775655291f2SDavid Xu *vpp = NULLVP; 776655291f2SDavid Xu 777655291f2SDavid Xu if (dvp->v_type != VDIR) 778655291f2SDavid Xu return (ENOTDIR); 779655291f2SDavid Xu 780655291f2SDavid Xu error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread); 781655291f2SDavid Xu if (error) 782655291f2SDavid Xu return (error); 783655291f2SDavid Xu 784655291f2SDavid Xu /* shortcut: check if the name is too long */ 785655291f2SDavid Xu if (cnp->cn_namelen >= MQFS_NAMELEN) 786655291f2SDavid Xu return (ENOENT); 787655291f2SDavid Xu 788655291f2SDavid Xu /* self */ 789655291f2SDavid Xu if (namelen == 1 && pname[0] == '.') { 790655291f2SDavid Xu if ((flags & ISLASTCN) && nameiop != LOOKUP) 791655291f2SDavid Xu return (EINVAL); 792655291f2SDavid Xu pn = pd; 793655291f2SDavid Xu *vpp = dvp; 794655291f2SDavid Xu VREF(dvp); 795655291f2SDavid Xu return (0); 796655291f2SDavid Xu } 797655291f2SDavid Xu 798655291f2SDavid Xu /* parent */ 799655291f2SDavid Xu if (cnp->cn_flags & ISDOTDOT) { 800655291f2SDavid Xu if (dvp->v_vflag & VV_ROOT) 801655291f2SDavid Xu return (EIO); 802655291f2SDavid Xu if ((flags & ISLASTCN) && nameiop != LOOKUP) 803655291f2SDavid Xu return (EINVAL); 804655291f2SDavid Xu VOP_UNLOCK(dvp, 0, cnp->cn_thread); 805655291f2SDavid Xu KASSERT(pd->mn_parent, ("non-root directory has no parent")); 806655291f2SDavid Xu pn = pd->mn_parent; 807655291f2SDavid Xu error = mqfs_allocv(dvp->v_mount, vpp, pn); 808655291f2SDavid Xu vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 809655291f2SDavid Xu return (error); 810655291f2SDavid Xu } 811655291f2SDavid Xu 812655291f2SDavid Xu /* named node */ 813655291f2SDavid Xu pn = mqfs_search(pd, pname, namelen); 814655291f2SDavid Xu 815655291f2SDavid Xu /* found */ 816655291f2SDavid Xu if (pn != NULL) { 817655291f2SDavid Xu /* DELETE */ 818655291f2SDavid Xu if (nameiop == DELETE && (flags & ISLASTCN)) { 819655291f2SDavid Xu error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 820655291f2SDavid Xu if (error) 821655291f2SDavid Xu return (error); 822655291f2SDavid Xu if (*vpp == dvp) { 823655291f2SDavid Xu VREF(dvp); 824655291f2SDavid Xu *vpp = dvp; 825655291f2SDavid Xu return (0); 826655291f2SDavid Xu } 827655291f2SDavid Xu } 828655291f2SDavid Xu 829655291f2SDavid Xu /* allocate vnode */ 830655291f2SDavid Xu error = mqfs_allocv(dvp->v_mount, vpp, pn); 831655291f2SDavid Xu if (error == 0 && cnp->cn_flags & MAKEENTRY) 832655291f2SDavid Xu cache_enter(dvp, *vpp, cnp); 833655291f2SDavid Xu return (error); 834655291f2SDavid Xu } 835655291f2SDavid Xu 836655291f2SDavid Xu /* not found */ 837655291f2SDavid Xu 838655291f2SDavid Xu /* will create a new entry in the directory ? */ 839655291f2SDavid Xu if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT) 840655291f2SDavid Xu && (flags & ISLASTCN)) { 841655291f2SDavid Xu error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 842655291f2SDavid Xu if (error) 843655291f2SDavid Xu return (error); 844655291f2SDavid Xu cnp->cn_flags |= SAVENAME; 845655291f2SDavid Xu return (EJUSTRETURN); 846655291f2SDavid Xu } 847655291f2SDavid Xu return (ENOENT); 848655291f2SDavid Xu } 849655291f2SDavid Xu 850655291f2SDavid Xu #if 0 851655291f2SDavid Xu struct vop_lookup_args { 852655291f2SDavid Xu struct vop_generic_args a_gen; 853655291f2SDavid Xu struct vnode *a_dvp; 854655291f2SDavid Xu struct vnode **a_vpp; 855655291f2SDavid Xu struct componentname *a_cnp; 856655291f2SDavid Xu }; 857655291f2SDavid Xu #endif 858655291f2SDavid Xu 859655291f2SDavid Xu /* 860655291f2SDavid Xu * vnode lookup operation 861655291f2SDavid Xu */ 862655291f2SDavid Xu static int 863655291f2SDavid Xu mqfs_lookup(struct vop_cachedlookup_args *ap) 864655291f2SDavid Xu { 865655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 866655291f2SDavid Xu int rc; 867655291f2SDavid Xu 868655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 869655291f2SDavid Xu rc = mqfs_lookupx(ap); 870655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 871655291f2SDavid Xu return (rc); 872655291f2SDavid Xu } 873655291f2SDavid Xu 874655291f2SDavid Xu #if 0 875655291f2SDavid Xu struct vop_create_args { 876655291f2SDavid Xu struct vnode *a_dvp; 877655291f2SDavid Xu struct vnode **a_vpp; 878655291f2SDavid Xu struct componentname *a_cnp; 879655291f2SDavid Xu struct vattr *a_vap; 880655291f2SDavid Xu }; 881655291f2SDavid Xu #endif 882655291f2SDavid Xu 883655291f2SDavid Xu /* 884655291f2SDavid Xu * vnode creation operation 885655291f2SDavid Xu */ 886655291f2SDavid Xu static int 887655291f2SDavid Xu mqfs_create(struct vop_create_args *ap) 888655291f2SDavid Xu { 889655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 890655291f2SDavid Xu struct componentname *cnp = ap->a_cnp; 891655291f2SDavid Xu struct mqfs_node *pd; 892655291f2SDavid Xu struct mqfs_node *pn; 893655291f2SDavid Xu struct mqueue *mq; 894655291f2SDavid Xu int error; 895655291f2SDavid Xu 896655291f2SDavid Xu pd = VTON(ap->a_dvp); 897655291f2SDavid Xu if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir) 898655291f2SDavid Xu return (ENOTDIR); 899655291f2SDavid Xu mq = mqueue_alloc(NULL); 900655291f2SDavid Xu if (mq == NULL) 901655291f2SDavid Xu return (EAGAIN); 902655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 903655291f2SDavid Xu #if 0 904655291f2SDavid Xu /* named node */ 905655291f2SDavid Xu pn = mqfs_search(pd, cnp->cn_nameptr, cnp->cn_namelen); 906655291f2SDavid Xu if (pn != NULL) { 907655291f2SDavid Xu mqueue_free(mq); 908655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 909655291f2SDavid Xu return (EEXIST); 910655291f2SDavid Xu } 911655291f2SDavid Xu #else 912655291f2SDavid Xu if ((cnp->cn_flags & HASBUF) == 0) 913655291f2SDavid Xu panic("%s: no name", __func__); 914655291f2SDavid Xu #endif 915655291f2SDavid Xu pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen); 916655291f2SDavid Xu pn->mn_mode = ap->a_vap->va_mode; 917655291f2SDavid Xu pn->mn_uid = cnp->cn_cred->cr_uid; 918655291f2SDavid Xu pn->mn_gid = cnp->cn_cred->cr_gid; 919655291f2SDavid Xu pn->mn_data = mq; 920655291f2SDavid Xu getnanotime(&pn->mn_birth); 921655291f2SDavid Xu pn->mn_ctime = pn->mn_atime = pn->mn_mtime = pn->mn_birth; 922655291f2SDavid Xu /* node attribute */ 923655291f2SDavid Xu error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn); 924655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 925655291f2SDavid Xu return (error); 926655291f2SDavid Xu } 927655291f2SDavid Xu 928655291f2SDavid Xu /* 929655291f2SDavid Xu * Remove an entry 930655291f2SDavid Xu */ 931655291f2SDavid Xu static 932655291f2SDavid Xu int do_unlink(struct mqfs_node *pn, struct ucred *ucred) 933655291f2SDavid Xu { 934655291f2SDavid Xu struct mqfs_node *parent; 935655291f2SDavid Xu struct mqfs_vdata *vd; 936655291f2SDavid Xu int error = 0; 937655291f2SDavid Xu 938655291f2SDavid Xu sx_assert(&pn->mn_info->mi_lock, SX_LOCKED); 939655291f2SDavid Xu 940655291f2SDavid Xu if (ucred->cr_uid != pn->mn_uid && 941655291f2SDavid Xu (error = suser_cred(ucred, 0)) != 0) 942655291f2SDavid Xu error = EACCES; 943655291f2SDavid Xu else if (!pn->mn_deleted) { 944655291f2SDavid Xu parent = pn->mn_parent; 945655291f2SDavid Xu pn->mn_parent = NULL; 946655291f2SDavid Xu pn->mn_deleted = 1; 947655291f2SDavid Xu LIST_REMOVE(pn, mn_sibling); 948655291f2SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 949655291f2SDavid Xu cache_purge(vd->mv_vnode); 950655291f2SDavid Xu vhold(vd->mv_vnode); 951655291f2SDavid Xu taskqueue_enqueue(taskqueue_thread, &vd->mv_task); 952655291f2SDavid Xu } 953655291f2SDavid Xu mqnode_release(pn); 954655291f2SDavid Xu mqnode_release(parent); 955655291f2SDavid Xu } else 956655291f2SDavid Xu error = ENOENT; 957655291f2SDavid Xu return (error); 958655291f2SDavid Xu } 959655291f2SDavid Xu 960655291f2SDavid Xu #if 0 961655291f2SDavid Xu struct vop_remove_args { 962655291f2SDavid Xu struct vnode *a_dvp; 963655291f2SDavid Xu struct vnode *a_vp; 964655291f2SDavid Xu struct componentname *a_cnp; 965655291f2SDavid Xu }; 966655291f2SDavid Xu #endif 967655291f2SDavid Xu 968655291f2SDavid Xu /* 969655291f2SDavid Xu * vnode removal operation 970655291f2SDavid Xu */ 971655291f2SDavid Xu static int 972655291f2SDavid Xu mqfs_remove(struct vop_remove_args *ap) 973655291f2SDavid Xu { 974655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 975655291f2SDavid Xu struct mqfs_node *pn; 976655291f2SDavid Xu int error; 977655291f2SDavid Xu 978655291f2SDavid Xu if (ap->a_vp->v_type == VDIR) 979655291f2SDavid Xu return (EPERM); 980655291f2SDavid Xu pn = VTON(ap->a_vp); 981655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 982655291f2SDavid Xu error = do_unlink(pn, ap->a_cnp->cn_cred); 983655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 984655291f2SDavid Xu return (error); 985655291f2SDavid Xu } 986655291f2SDavid Xu 987655291f2SDavid Xu #if 0 988655291f2SDavid Xu struct vop_inactive_args { 989655291f2SDavid Xu struct vnode *a_vp; 990655291f2SDavid Xu struct thread *a_td; 991655291f2SDavid Xu }; 992655291f2SDavid Xu #endif 993655291f2SDavid Xu 994655291f2SDavid Xu static int 995655291f2SDavid Xu mqfs_inactive(struct vop_inactive_args *ap) 996655291f2SDavid Xu { 997655291f2SDavid Xu struct mqfs_node *pn = VTON(ap->a_vp); 998655291f2SDavid Xu 999655291f2SDavid Xu if (pn->mn_deleted) 1000655291f2SDavid Xu vrecycle(ap->a_vp, ap->a_td); 1001655291f2SDavid Xu return (0); 1002655291f2SDavid Xu } 1003655291f2SDavid Xu 1004655291f2SDavid Xu #if 0 1005655291f2SDavid Xu struct vop_reclaim_args { 1006655291f2SDavid Xu struct vop_generic_args a_gen; 1007655291f2SDavid Xu struct vnode *a_vp; 1008655291f2SDavid Xu struct thread *a_td; 1009655291f2SDavid Xu }; 1010655291f2SDavid Xu #endif 1011655291f2SDavid Xu 1012655291f2SDavid Xu static int 1013655291f2SDavid Xu mqfs_reclaim(struct vop_reclaim_args *ap) 1014655291f2SDavid Xu { 1015655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount); 1016655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1017655291f2SDavid Xu struct mqfs_node *pn; 1018655291f2SDavid Xu struct mqfs_vdata *vd; 1019655291f2SDavid Xu 1020655291f2SDavid Xu vd = vp->v_data; 1021655291f2SDavid Xu pn = vd->mv_node; 1022655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1023655291f2SDavid Xu vp->v_data = NULL; 1024655291f2SDavid Xu LIST_REMOVE(vd, mv_link); 1025655291f2SDavid Xu uma_zfree(mvdata_zone, vd); 1026655291f2SDavid Xu mqnode_release(pn); 1027655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1028655291f2SDavid Xu return (0); 1029655291f2SDavid Xu } 1030655291f2SDavid Xu 1031655291f2SDavid Xu #if 0 1032655291f2SDavid Xu struct vop_open_args { 1033655291f2SDavid Xu struct vop_generic_args a_gen; 1034655291f2SDavid Xu struct vnode *a_vp; 1035655291f2SDavid Xu int a_mode; 1036655291f2SDavid Xu struct ucred *a_cred; 1037655291f2SDavid Xu struct thread *a_td; 1038655291f2SDavid Xu int a_fdidx; 1039655291f2SDavid Xu }; 1040655291f2SDavid Xu #endif 1041655291f2SDavid Xu 1042655291f2SDavid Xu static int 1043655291f2SDavid Xu mqfs_open(struct vop_open_args *ap) 1044655291f2SDavid Xu { 1045655291f2SDavid Xu return (0); 1046655291f2SDavid Xu } 1047655291f2SDavid Xu 1048655291f2SDavid Xu #if 0 1049655291f2SDavid Xu struct vop_close_args { 1050655291f2SDavid Xu struct vop_generic_args a_gen; 1051655291f2SDavid Xu struct vnode *a_vp; 1052655291f2SDavid Xu int a_fflag; 1053655291f2SDavid Xu struct ucred *a_cred; 1054655291f2SDavid Xu struct thread *a_td; 1055655291f2SDavid Xu }; 1056655291f2SDavid Xu #endif 1057655291f2SDavid Xu 1058655291f2SDavid Xu static int 1059655291f2SDavid Xu mqfs_close(struct vop_close_args *ap) 1060655291f2SDavid Xu { 1061655291f2SDavid Xu return (0); 1062655291f2SDavid Xu } 1063655291f2SDavid Xu 1064655291f2SDavid Xu #if 0 1065655291f2SDavid Xu struct vop_access_args { 1066655291f2SDavid Xu struct vop_generic_args a_gen; 1067655291f2SDavid Xu struct vnode *a_vp; 1068655291f2SDavid Xu int a_mode; 1069655291f2SDavid Xu struct ucred *a_cred; 1070655291f2SDavid Xu struct thread *a_td; 1071655291f2SDavid Xu }; 1072655291f2SDavid Xu #endif 1073655291f2SDavid Xu 1074655291f2SDavid Xu /* 1075655291f2SDavid Xu * Verify permissions 1076655291f2SDavid Xu */ 1077655291f2SDavid Xu static int 1078655291f2SDavid Xu mqfs_access(struct vop_access_args *ap) 1079655291f2SDavid Xu { 1080655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1081655291f2SDavid Xu struct vattr vattr; 1082655291f2SDavid Xu int error; 1083655291f2SDavid Xu 1084655291f2SDavid Xu error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td); 1085655291f2SDavid Xu if (error) 1086655291f2SDavid Xu return (error); 1087655291f2SDavid Xu error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid, 1088655291f2SDavid Xu vattr.va_gid, ap->a_mode, ap->a_cred, NULL); 1089655291f2SDavid Xu return (error); 1090655291f2SDavid Xu } 1091655291f2SDavid Xu 1092655291f2SDavid Xu #if 0 1093655291f2SDavid Xu struct vop_getattr_args { 1094655291f2SDavid Xu struct vop_generic_args a_gen; 1095655291f2SDavid Xu struct vnode *a_vp; 1096655291f2SDavid Xu struct vattr *a_vap; 1097655291f2SDavid Xu struct ucred *a_cred; 1098655291f2SDavid Xu struct thread *a_td; 1099655291f2SDavid Xu }; 1100655291f2SDavid Xu #endif 1101655291f2SDavid Xu 1102655291f2SDavid Xu /* 1103655291f2SDavid Xu * Get file attributes 1104655291f2SDavid Xu */ 1105655291f2SDavid Xu static int 1106655291f2SDavid Xu mqfs_getattr(struct vop_getattr_args *ap) 1107655291f2SDavid Xu { 1108655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1109655291f2SDavid Xu struct mqfs_node *pn = VTON(vp); 1110655291f2SDavid Xu struct vattr *vap = ap->a_vap; 1111655291f2SDavid Xu int error = 0; 1112655291f2SDavid Xu 1113655291f2SDavid Xu VATTR_NULL(vap); 1114655291f2SDavid Xu vap->va_type = vp->v_type; 1115655291f2SDavid Xu vap->va_mode = pn->mn_mode; 1116655291f2SDavid Xu vap->va_nlink = 1; 1117655291f2SDavid Xu vap->va_uid = pn->mn_uid; 1118655291f2SDavid Xu vap->va_gid = pn->mn_gid; 1119655291f2SDavid Xu vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1120655291f2SDavid Xu vap->va_fileid = pn->mn_fileno; 1121655291f2SDavid Xu vap->va_size = 0; 1122655291f2SDavid Xu vap->va_blocksize = PAGE_SIZE; 1123655291f2SDavid Xu vap->va_bytes = vap->va_size = 0; 1124655291f2SDavid Xu vap->va_atime = pn->mn_atime; 1125655291f2SDavid Xu vap->va_mtime = pn->mn_mtime; 1126655291f2SDavid Xu vap->va_ctime = pn->mn_ctime; 1127655291f2SDavid Xu vap->va_birthtime = pn->mn_birth; 1128655291f2SDavid Xu vap->va_gen = 0; 1129655291f2SDavid Xu vap->va_flags = 0; 1130655291f2SDavid Xu vap->va_rdev = 0; 1131655291f2SDavid Xu vap->va_bytes = 0; 1132655291f2SDavid Xu vap->va_filerev = 0; 1133655291f2SDavid Xu vap->va_vaflags = 0; 1134655291f2SDavid Xu return (error); 1135655291f2SDavid Xu } 1136655291f2SDavid Xu 1137655291f2SDavid Xu #if 0 1138655291f2SDavid Xu struct vop_setattr_args { 1139655291f2SDavid Xu struct vop_generic_args a_gen; 1140655291f2SDavid Xu struct vnode *a_vp; 1141655291f2SDavid Xu struct vattr *a_vap; 1142655291f2SDavid Xu struct ucred *a_cred; 1143655291f2SDavid Xu struct thread *a_td; 1144655291f2SDavid Xu }; 1145655291f2SDavid Xu #endif 1146655291f2SDavid Xu /* 1147655291f2SDavid Xu * Set attributes 1148655291f2SDavid Xu */ 1149655291f2SDavid Xu static int 1150655291f2SDavid Xu mqfs_setattr(struct vop_setattr_args *ap) 1151655291f2SDavid Xu { 1152655291f2SDavid Xu struct mqfs_node *pn; 1153655291f2SDavid Xu struct vattr *vap; 1154655291f2SDavid Xu struct vnode *vp; 1155655291f2SDavid Xu int c, error; 1156655291f2SDavid Xu uid_t uid; 1157655291f2SDavid Xu gid_t gid; 1158655291f2SDavid Xu 1159655291f2SDavid Xu vap = ap->a_vap; 1160655291f2SDavid Xu vp = ap->a_vp; 1161655291f2SDavid Xu if ((vap->va_type != VNON) || 1162655291f2SDavid Xu (vap->va_nlink != VNOVAL) || 1163655291f2SDavid Xu (vap->va_fsid != VNOVAL) || 1164655291f2SDavid Xu (vap->va_fileid != VNOVAL) || 1165655291f2SDavid Xu (vap->va_blocksize != VNOVAL) || 1166655291f2SDavid Xu (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1167655291f2SDavid Xu (vap->va_rdev != VNOVAL) || 1168655291f2SDavid Xu ((int)vap->va_bytes != VNOVAL) || 1169655291f2SDavid Xu (vap->va_gen != VNOVAL)) { 1170655291f2SDavid Xu return (EINVAL); 1171655291f2SDavid Xu } 1172655291f2SDavid Xu 1173655291f2SDavid Xu pn = VTON(vp); 1174655291f2SDavid Xu 1175655291f2SDavid Xu error = c = 0; 1176655291f2SDavid Xu if (vap->va_uid == (uid_t)VNOVAL) 1177655291f2SDavid Xu uid = pn->mn_uid; 1178655291f2SDavid Xu else 1179655291f2SDavid Xu uid = vap->va_uid; 1180655291f2SDavid Xu if (vap->va_gid == (gid_t)VNOVAL) 1181655291f2SDavid Xu gid = pn->mn_gid; 1182655291f2SDavid Xu else 1183655291f2SDavid Xu gid = vap->va_gid; 1184655291f2SDavid Xu 1185655291f2SDavid Xu if (uid != pn->mn_uid || gid != pn->mn_gid) { 1186655291f2SDavid Xu /* 1187655291f2SDavid Xu * To modify the ownership of a file, must possess VADMIN 1188655291f2SDavid Xu * for that file. 1189655291f2SDavid Xu */ 1190655291f2SDavid Xu if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td))) 1191655291f2SDavid Xu return (error); 1192655291f2SDavid Xu if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid || 1193655291f2SDavid Xu (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) && 1194655291f2SDavid Xu (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)) 1195655291f2SDavid Xu != 0) 1196655291f2SDavid Xu return (error); 1197655291f2SDavid Xu pn->mn_uid = uid; 1198655291f2SDavid Xu pn->mn_gid = gid; 1199655291f2SDavid Xu c = 1; 1200655291f2SDavid Xu } 1201655291f2SDavid Xu 1202655291f2SDavid Xu if (vap->va_mode != (mode_t)VNOVAL) { 1203655291f2SDavid Xu if ((ap->a_cred->cr_uid != pn->mn_uid) && 1204655291f2SDavid Xu (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL))) 1205655291f2SDavid Xu return (error); 1206655291f2SDavid Xu pn->mn_mode = vap->va_mode; 1207655291f2SDavid Xu c = 1; 1208655291f2SDavid Xu } 1209655291f2SDavid Xu 1210655291f2SDavid Xu if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1211655291f2SDavid Xu /* See the comment in ufs_vnops::ufs_setattr(). */ 1212655291f2SDavid Xu if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) && 1213655291f2SDavid Xu ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 1214655291f2SDavid Xu (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td)))) 1215655291f2SDavid Xu return (error); 1216655291f2SDavid Xu if (vap->va_atime.tv_sec != VNOVAL) { 1217655291f2SDavid Xu pn->mn_atime = vap->va_atime; 1218655291f2SDavid Xu } 1219655291f2SDavid Xu if (vap->va_mtime.tv_sec != VNOVAL) { 1220655291f2SDavid Xu pn->mn_mtime = vap->va_mtime; 1221655291f2SDavid Xu } 1222655291f2SDavid Xu c = 1; 1223655291f2SDavid Xu } 1224655291f2SDavid Xu if (c) { 1225655291f2SDavid Xu vfs_timestamp(&pn->mn_ctime); 1226655291f2SDavid Xu } 1227655291f2SDavid Xu return (0); 1228655291f2SDavid Xu } 1229655291f2SDavid Xu 1230655291f2SDavid Xu #if 0 1231655291f2SDavid Xu struct vop_read_args { 1232655291f2SDavid Xu struct vop_generic_args a_gen; 1233655291f2SDavid Xu struct vnode *a_vp; 1234655291f2SDavid Xu struct uio *a_uio; 1235655291f2SDavid Xu int a_ioflag; 1236655291f2SDavid Xu struct ucred *a_cred; 1237655291f2SDavid Xu }; 1238655291f2SDavid Xu #endif 1239655291f2SDavid Xu 1240655291f2SDavid Xu /* 1241655291f2SDavid Xu * Read from a file 1242655291f2SDavid Xu */ 1243655291f2SDavid Xu static int 1244655291f2SDavid Xu mqfs_read(struct vop_read_args *ap) 1245655291f2SDavid Xu { 1246655291f2SDavid Xu char buf[80]; 1247655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1248655291f2SDavid Xu struct uio *uio = ap->a_uio; 1249655291f2SDavid Xu struct mqfs_node *pn; 1250655291f2SDavid Xu struct mqueue *mq; 1251655291f2SDavid Xu int len, error; 1252655291f2SDavid Xu 1253655291f2SDavid Xu if (vp->v_type != VREG) 1254655291f2SDavid Xu return (EINVAL); 1255655291f2SDavid Xu 1256655291f2SDavid Xu pn = VTON(vp); 1257655291f2SDavid Xu mq = VTOMQ(vp); 1258655291f2SDavid Xu snprintf(buf, sizeof(buf), 1259655291f2SDavid Xu "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n", 1260655291f2SDavid Xu mq->mq_totalbytes, 1261655291f2SDavid Xu mq->mq_maxmsg, 1262655291f2SDavid Xu mq->mq_curmsgs, 1263655291f2SDavid Xu mq->mq_msgsize); 1264655291f2SDavid Xu buf[sizeof(buf)-1] = '\0'; 1265655291f2SDavid Xu len = strlen(buf); 1266655291f2SDavid Xu error = uiomove_frombuf(buf, len, uio); 1267655291f2SDavid Xu return (error); 1268655291f2SDavid Xu } 1269655291f2SDavid Xu 1270655291f2SDavid Xu #if 0 1271655291f2SDavid Xu struct vop_readdir_args { 1272655291f2SDavid Xu struct vop_generic_args a_gen; 1273655291f2SDavid Xu struct vnode *a_vp; 1274655291f2SDavid Xu struct uio *a_uio; 1275655291f2SDavid Xu struct ucred *a_cred; 1276655291f2SDavid Xu int *a_eofflag; 1277655291f2SDavid Xu int *a_ncookies; 1278655291f2SDavid Xu u_long **a_cookies; 1279655291f2SDavid Xu }; 1280655291f2SDavid Xu #endif 1281655291f2SDavid Xu 1282655291f2SDavid Xu /* 1283655291f2SDavid Xu * Return directory entries. 1284655291f2SDavid Xu */ 1285655291f2SDavid Xu static int 1286655291f2SDavid Xu mqfs_readdir(struct vop_readdir_args *ap) 1287655291f2SDavid Xu { 1288655291f2SDavid Xu struct vnode *vp; 1289655291f2SDavid Xu struct mqfs_info *mi; 1290655291f2SDavid Xu struct mqfs_node *pd; 1291655291f2SDavid Xu struct mqfs_node *pn; 1292655291f2SDavid Xu struct dirent entry; 1293655291f2SDavid Xu struct uio *uio; 1294655291f2SDavid Xu int *tmp_ncookies = NULL; 1295655291f2SDavid Xu off_t offset; 1296655291f2SDavid Xu int error, i; 1297655291f2SDavid Xu 1298655291f2SDavid Xu vp = ap->a_vp; 1299655291f2SDavid Xu mi = VFSTOMQFS(vp->v_mount); 1300655291f2SDavid Xu pd = VTON(vp); 1301655291f2SDavid Xu uio = ap->a_uio; 1302655291f2SDavid Xu 1303655291f2SDavid Xu if (vp->v_type != VDIR) 1304655291f2SDavid Xu return (ENOTDIR); 1305655291f2SDavid Xu 1306655291f2SDavid Xu if (uio->uio_offset < 0) 1307655291f2SDavid Xu return (EINVAL); 1308655291f2SDavid Xu 1309655291f2SDavid Xu if (ap->a_ncookies != NULL) { 1310655291f2SDavid Xu tmp_ncookies = ap->a_ncookies; 1311655291f2SDavid Xu *ap->a_ncookies = 0; 1312655291f2SDavid Xu ap->a_ncookies = NULL; 1313655291f2SDavid Xu } 1314655291f2SDavid Xu 1315655291f2SDavid Xu error = 0; 1316655291f2SDavid Xu offset = 0; 1317655291f2SDavid Xu 1318655291f2SDavid Xu sx_xlock(&mi->mi_lock); 1319655291f2SDavid Xu 1320655291f2SDavid Xu LIST_FOREACH(pn, &pd->mn_children, mn_sibling) { 1321655291f2SDavid Xu entry.d_reclen = sizeof(entry); 1322655291f2SDavid Xu if (!pn->mn_fileno) 1323655291f2SDavid Xu mqfs_fileno_alloc(mi, pn); 1324655291f2SDavid Xu entry.d_fileno = pn->mn_fileno; 1325655291f2SDavid Xu for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i) 1326655291f2SDavid Xu entry.d_name[i] = pn->mn_name[i]; 1327655291f2SDavid Xu entry.d_name[i] = 0; 1328655291f2SDavid Xu entry.d_namlen = i; 1329655291f2SDavid Xu switch (pn->mn_type) { 1330655291f2SDavid Xu case mqfstype_root: 1331655291f2SDavid Xu case mqfstype_dir: 1332655291f2SDavid Xu case mqfstype_this: 1333655291f2SDavid Xu case mqfstype_parent: 1334655291f2SDavid Xu entry.d_type = DT_DIR; 1335655291f2SDavid Xu break; 1336655291f2SDavid Xu case mqfstype_file: 1337655291f2SDavid Xu entry.d_type = DT_REG; 1338655291f2SDavid Xu break; 1339655291f2SDavid Xu case mqfstype_symlink: 1340655291f2SDavid Xu entry.d_type = DT_LNK; 1341655291f2SDavid Xu break; 1342655291f2SDavid Xu default: 1343655291f2SDavid Xu panic("%s has unexpected node type: %d", pn->mn_name, 1344655291f2SDavid Xu pn->mn_type); 1345655291f2SDavid Xu } 1346655291f2SDavid Xu if (entry.d_reclen > uio->uio_resid) 1347655291f2SDavid Xu break; 1348655291f2SDavid Xu if (offset >= uio->uio_offset) { 1349655291f2SDavid Xu error = vfs_read_dirent(ap, &entry, offset); 1350655291f2SDavid Xu if (error) 1351655291f2SDavid Xu break; 1352655291f2SDavid Xu } 1353655291f2SDavid Xu offset += entry.d_reclen; 1354655291f2SDavid Xu } 1355655291f2SDavid Xu sx_xunlock(&mi->mi_lock); 1356655291f2SDavid Xu 1357655291f2SDavid Xu uio->uio_offset = offset; 1358655291f2SDavid Xu 1359655291f2SDavid Xu if (tmp_ncookies != NULL) 1360655291f2SDavid Xu ap->a_ncookies = tmp_ncookies; 1361655291f2SDavid Xu 1362655291f2SDavid Xu return (error); 1363655291f2SDavid Xu } 1364655291f2SDavid Xu 1365655291f2SDavid Xu #ifdef notyet 1366655291f2SDavid Xu 1367655291f2SDavid Xu #if 0 1368655291f2SDavid Xu struct vop_mkdir_args { 1369655291f2SDavid Xu struct vnode *a_dvp; 1370655291f2SDavid Xu struvt vnode **a_vpp; 1371655291f2SDavid Xu struvt componentname *a_cnp; 1372655291f2SDavid Xu struct vattr *a_vap; 1373655291f2SDavid Xu }; 1374655291f2SDavid Xu #endif 1375655291f2SDavid Xu 1376655291f2SDavid Xu /* 1377655291f2SDavid Xu * Create a directory. 1378655291f2SDavid Xu */ 1379655291f2SDavid Xu static int 1380655291f2SDavid Xu mqfs_mkdir(struct vop_mkdir_args *ap) 1381655291f2SDavid Xu { 1382655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1383655291f2SDavid Xu struct componentname *cnp = ap->a_cnp; 1384655291f2SDavid Xu struct mqfs_node *pd = VTON(ap->a_dvp); 1385655291f2SDavid Xu struct mqfs_node *pn; 1386655291f2SDavid Xu int error; 1387655291f2SDavid Xu 1388655291f2SDavid Xu if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir) 1389655291f2SDavid Xu return (ENOTDIR); 1390655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1391655291f2SDavid Xu #if 0 1392655291f2SDavid Xu /* named node */ 1393655291f2SDavid Xu pn = mqfs_search(pd, cnp->cn_nameptr, cnp->cn_namelen); 1394655291f2SDavid Xu if (pn != NULL) { 1395655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1396655291f2SDavid Xu return (EEXIST); 1397655291f2SDavid Xu } 1398655291f2SDavid Xu #else 1399655291f2SDavid Xu if ((cnp->cn_flags & HASBUF) == 0) 1400655291f2SDavid Xu panic("%s: no name", __func__); 1401655291f2SDavid Xu #endif 1402655291f2SDavid Xu pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen); 1403655291f2SDavid Xu pn->mn_mode = ap->a_vap->va_mode; 1404655291f2SDavid Xu pn->mn_uid = cnp->cn_cred->cr_uid; 1405655291f2SDavid Xu pn->mn_gid = cnp->cn_cred->cr_gid; 1406655291f2SDavid Xu getnanotime(&pn->mn_birth); 1407655291f2SDavid Xu pn->mn_ctime = pn->mn_atime = pn->mn_mtime = pn->mn_birth; 1408655291f2SDavid Xu /* node attribute */ 1409655291f2SDavid Xu error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn); 1410655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1411655291f2SDavid Xu return (error); 1412655291f2SDavid Xu } 1413655291f2SDavid Xu 1414655291f2SDavid Xu #if 0 1415655291f2SDavid Xu struct vop_rmdir_args { 1416655291f2SDavid Xu struct vnode *a_dvp; 1417655291f2SDavid Xu struct vnode *a_vp; 1418655291f2SDavid Xu struct componentname *a_cnp; 1419655291f2SDavid Xu }; 1420655291f2SDavid Xu #endif 1421655291f2SDavid Xu 1422655291f2SDavid Xu /* 1423655291f2SDavid Xu * Remove a directory. 1424655291f2SDavid Xu */ 1425655291f2SDavid Xu static int 1426655291f2SDavid Xu mqfs_rmdir(struct vop_rmdir_args *ap) 1427655291f2SDavid Xu { 1428655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1429655291f2SDavid Xu struct mqfs_node *pn = VTON(ap->a_vp); 1430655291f2SDavid Xu struct mqfs_node *pt; 1431655291f2SDavid Xu 1432655291f2SDavid Xu if (pn->mn_type != mqfstype_dir) 1433655291f2SDavid Xu return (ENOTDIR); 1434655291f2SDavid Xu 1435655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1436655291f2SDavid Xu if (pn->mn_deleted) { 1437655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1438655291f2SDavid Xu return (ENOENT); 1439655291f2SDavid Xu } 1440655291f2SDavid Xu 1441655291f2SDavid Xu pt = LIST_FIRST(&pn->mn_children); 1442655291f2SDavid Xu pt = LIST_NEXT(pt, mn_sibling); 1443655291f2SDavid Xu pt = LIST_NEXT(pt, mn_sibling); 1444655291f2SDavid Xu if (pt != NULL) { 1445655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1446655291f2SDavid Xu return (ENOTEMPTY); 1447655291f2SDavid Xu } 1448655291f2SDavid Xu pt = pn->mn_parent; 1449655291f2SDavid Xu pn->mn_parent = NULL; 1450655291f2SDavid Xu pn->mn_deleted = 1; 1451655291f2SDavid Xu LIST_REMOVE(pn, mn_sibling); 1452655291f2SDavid Xu mqnode_release(pn); 1453655291f2SDavid Xu mqnode_release(pt); 1454655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1455655291f2SDavid Xu cache_purge(ap->a_vp); 1456655291f2SDavid Xu return (0); 1457655291f2SDavid Xu } 1458655291f2SDavid Xu 1459655291f2SDavid Xu #endif /* notyet */ 1460655291f2SDavid Xu 1461655291f2SDavid Xu /* 1462655291f2SDavid Xu * Allocate a message queue 1463655291f2SDavid Xu */ 1464655291f2SDavid Xu static struct mqueue * 1465655291f2SDavid Xu mqueue_alloc(const struct mq_attr *attr) 1466655291f2SDavid Xu { 1467655291f2SDavid Xu struct mqueue *mq; 1468655291f2SDavid Xu 1469655291f2SDavid Xu if (curmq >= maxmq) 1470655291f2SDavid Xu return (NULL); 1471655291f2SDavid Xu mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO); 1472655291f2SDavid Xu TAILQ_INIT(&mq->mq_msgq); 1473655291f2SDavid Xu if (attr != NULL) { 1474655291f2SDavid Xu mq->mq_maxmsg = attr->mq_maxmsg; 1475655291f2SDavid Xu mq->mq_msgsize = attr->mq_msgsize; 1476655291f2SDavid Xu } else { 1477655291f2SDavid Xu mq->mq_maxmsg = default_maxmsg; 1478655291f2SDavid Xu mq->mq_msgsize = default_msgsize; 1479655291f2SDavid Xu } 1480655291f2SDavid Xu mtx_init(&mq->mq_mutex, "mqueue", NULL, MTX_DEF); 1481655291f2SDavid Xu knlist_init(&mq->mq_rsel.si_note, &mq->mq_mutex, NULL, NULL, NULL); 1482655291f2SDavid Xu knlist_init(&mq->mq_wsel.si_note, &mq->mq_mutex, NULL, NULL, NULL); 1483655291f2SDavid Xu atomic_add_int(&curmq, 1); 1484655291f2SDavid Xu return (mq); 1485655291f2SDavid Xu } 1486655291f2SDavid Xu 1487655291f2SDavid Xu /* 1488655291f2SDavid Xu * Destroy a message queue 1489655291f2SDavid Xu */ 1490655291f2SDavid Xu static void 1491655291f2SDavid Xu mqueue_free(struct mqueue *mq) 1492655291f2SDavid Xu { 1493655291f2SDavid Xu struct mqueue_msg *msg; 1494655291f2SDavid Xu 1495655291f2SDavid Xu while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) { 1496655291f2SDavid Xu TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link); 1497655291f2SDavid Xu FREE(msg, M_MQUEUEDATA); 1498655291f2SDavid Xu } 1499655291f2SDavid Xu 1500655291f2SDavid Xu mtx_destroy(&mq->mq_mutex); 1501655291f2SDavid Xu knlist_destroy(&mq->mq_rsel.si_note); 1502655291f2SDavid Xu knlist_destroy(&mq->mq_wsel.si_note); 1503655291f2SDavid Xu uma_zfree(mqueue_zone, mq); 1504655291f2SDavid Xu atomic_add_int(&curmq, -1); 1505655291f2SDavid Xu } 1506655291f2SDavid Xu 1507655291f2SDavid Xu /* 1508655291f2SDavid Xu * Load a message from user space 1509655291f2SDavid Xu */ 1510655291f2SDavid Xu static struct mqueue_msg * 1511655291f2SDavid Xu mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio) 1512655291f2SDavid Xu { 1513655291f2SDavid Xu struct mqueue_msg *msg; 1514655291f2SDavid Xu size_t len; 1515655291f2SDavid Xu int error; 1516655291f2SDavid Xu 1517655291f2SDavid Xu len = sizeof(struct mqueue_msg) + msg_size; 1518655291f2SDavid Xu MALLOC(msg, struct mqueue_msg *, len, M_MQUEUEDATA, M_WAITOK); 1519655291f2SDavid Xu error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg), 1520655291f2SDavid Xu msg_size); 1521655291f2SDavid Xu if (error) { 1522655291f2SDavid Xu FREE(msg, M_MQUEUEDATA); 1523655291f2SDavid Xu msg = NULL; 1524655291f2SDavid Xu } else { 1525655291f2SDavid Xu msg->msg_size = msg_size; 1526655291f2SDavid Xu msg->msg_prio = msg_prio; 1527655291f2SDavid Xu } 1528655291f2SDavid Xu return (msg); 1529655291f2SDavid Xu } 1530655291f2SDavid Xu 1531655291f2SDavid Xu /* 1532655291f2SDavid Xu * Save a message to user space 1533655291f2SDavid Xu */ 1534655291f2SDavid Xu static int 1535655291f2SDavid Xu mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio) 1536655291f2SDavid Xu { 1537655291f2SDavid Xu int error; 1538655291f2SDavid Xu 1539655291f2SDavid Xu error = copyout(((char *)msg) + sizeof(*msg), msg_ptr, 1540655291f2SDavid Xu msg->msg_size); 1541b2f92ef9SDavid Xu if (error == 0 && msg_prio != NULL) 1542655291f2SDavid Xu error = copyout(&msg->msg_prio, msg_prio, sizeof(int)); 1543655291f2SDavid Xu return (error); 1544655291f2SDavid Xu } 1545655291f2SDavid Xu 1546655291f2SDavid Xu /* 1547655291f2SDavid Xu * Free a message's memory 1548655291f2SDavid Xu */ 1549655291f2SDavid Xu static __inline void 1550655291f2SDavid Xu mqueue_freemsg(struct mqueue_msg *msg) 1551655291f2SDavid Xu { 1552655291f2SDavid Xu FREE(msg, M_MQUEUEDATA); 1553655291f2SDavid Xu } 1554655291f2SDavid Xu 1555655291f2SDavid Xu /* 1556655291f2SDavid Xu * Send a message. if waitok is false, thread will not be 1557655291f2SDavid Xu * blocked if there is no data in queue, otherwise, absolute 1558655291f2SDavid Xu * time will be checked. 1559655291f2SDavid Xu */ 1560655291f2SDavid Xu int 1561655291f2SDavid Xu mqueue_send(struct mqueue *mq, const char *msg_ptr, 1562655291f2SDavid Xu size_t msg_len, unsigned msg_prio, int waitok, 1563655291f2SDavid Xu const struct timespec *abs_timeout) 1564655291f2SDavid Xu { 1565655291f2SDavid Xu struct mqueue_msg *msg; 1566655291f2SDavid Xu struct timespec ets, ts, ts2; 1567655291f2SDavid Xu struct timeval tv; 1568655291f2SDavid Xu int error; 1569655291f2SDavid Xu 1570a6de716dSDavid Xu if (msg_prio >= MQ_PRIO_MAX) 1571a6de716dSDavid Xu return (EINVAL); 1572655291f2SDavid Xu if (msg_len > mq->mq_msgsize) 1573655291f2SDavid Xu return (EMSGSIZE); 1574655291f2SDavid Xu msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio); 1575655291f2SDavid Xu if (msg == NULL) 1576655291f2SDavid Xu return (EFAULT); 1577655291f2SDavid Xu 1578655291f2SDavid Xu /* O_NONBLOCK case */ 1579655291f2SDavid Xu if (!waitok) { 1580655291f2SDavid Xu error = _mqueue_send(mq, msg, -1); 1581655291f2SDavid Xu if (error) 1582655291f2SDavid Xu goto bad; 1583655291f2SDavid Xu return (0); 1584655291f2SDavid Xu } 1585655291f2SDavid Xu 1586655291f2SDavid Xu /* we allow a null timeout (wait forever) */ 1587655291f2SDavid Xu if (abs_timeout == NULL) { 1588655291f2SDavid Xu error = _mqueue_send(mq, msg, 0); 1589655291f2SDavid Xu if (error) 1590655291f2SDavid Xu goto bad; 1591655291f2SDavid Xu return (0); 1592655291f2SDavid Xu } 1593655291f2SDavid Xu 1594655291f2SDavid Xu /* send it before checking time */ 1595655291f2SDavid Xu error = _mqueue_send(mq, msg, -1); 1596655291f2SDavid Xu if (error == 0) 1597655291f2SDavid Xu return (0); 1598655291f2SDavid Xu 1599655291f2SDavid Xu if (error != EAGAIN) 1600655291f2SDavid Xu goto bad; 1601655291f2SDavid Xu 1602655291f2SDavid Xu error = copyin(abs_timeout, &ets, sizeof(ets)); 1603655291f2SDavid Xu if (error != 0) 1604655291f2SDavid Xu goto bad; 1605655291f2SDavid Xu if (ets.tv_nsec >= 1000000000 || ets.tv_nsec < 0) { 1606655291f2SDavid Xu error = EINVAL; 1607655291f2SDavid Xu goto bad; 1608655291f2SDavid Xu } 1609655291f2SDavid Xu for (;;) { 1610655291f2SDavid Xu ts2 = ets; 1611a6de716dSDavid Xu getnanotime(&ts); 1612655291f2SDavid Xu timespecsub(&ts2, &ts); 1613655291f2SDavid Xu if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { 1614655291f2SDavid Xu error = ETIMEDOUT; 1615655291f2SDavid Xu break; 1616655291f2SDavid Xu } 1617655291f2SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts2); 1618655291f2SDavid Xu error = _mqueue_send(mq, msg, tvtohz(&tv)); 1619655291f2SDavid Xu if (error != ETIMEDOUT) 1620655291f2SDavid Xu break; 1621655291f2SDavid Xu } 1622655291f2SDavid Xu if (error == 0) 1623655291f2SDavid Xu return (0); 1624655291f2SDavid Xu bad: 1625655291f2SDavid Xu mqueue_freemsg(msg); 1626655291f2SDavid Xu return (error); 1627655291f2SDavid Xu } 1628655291f2SDavid Xu 1629655291f2SDavid Xu /* 1630655291f2SDavid Xu * Common routine to send a message 1631655291f2SDavid Xu */ 1632655291f2SDavid Xu static int 1633655291f2SDavid Xu _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo) 1634655291f2SDavid Xu { 1635655291f2SDavid Xu struct mqueue_msg *msg2; 1636655291f2SDavid Xu int error = 0; 1637655291f2SDavid Xu 1638655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 1639655291f2SDavid Xu while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) { 1640655291f2SDavid Xu if (timo < 0) { 1641655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1642655291f2SDavid Xu return (EAGAIN); 1643655291f2SDavid Xu } 1644655291f2SDavid Xu mq->mq_senders++; 1645655291f2SDavid Xu error = msleep(&mq->mq_senders, &mq->mq_mutex, 1646655291f2SDavid Xu PSOCK | PCATCH, "mqsend", timo); 1647655291f2SDavid Xu mq->mq_senders--; 1648655291f2SDavid Xu if (error == EAGAIN) 1649655291f2SDavid Xu error = ETIMEDOUT; 1650655291f2SDavid Xu } 1651655291f2SDavid Xu if (mq->mq_curmsgs >= mq->mq_maxmsg) { 1652655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1653655291f2SDavid Xu return (error); 1654655291f2SDavid Xu } 1655655291f2SDavid Xu error = 0; 1656655291f2SDavid Xu if (TAILQ_EMPTY(&mq->mq_msgq)) { 1657655291f2SDavid Xu TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link); 1658655291f2SDavid Xu } else { 1659655291f2SDavid Xu if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) { 1660655291f2SDavid Xu TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link); 1661655291f2SDavid Xu } else { 1662655291f2SDavid Xu TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) { 1663655291f2SDavid Xu if (msg2->msg_prio < msg->msg_prio) 1664655291f2SDavid Xu break; 1665655291f2SDavid Xu } 1666655291f2SDavid Xu TAILQ_INSERT_BEFORE(msg2, msg, msg_link); 1667655291f2SDavid Xu } 1668655291f2SDavid Xu } 1669655291f2SDavid Xu mq->mq_curmsgs++; 1670655291f2SDavid Xu mq->mq_totalbytes += msg->msg_size; 1671655291f2SDavid Xu if (mq->mq_receivers) 1672655291f2SDavid Xu wakeup_one(&mq->mq_receivers); 1673655291f2SDavid Xu else if (mq->mq_notifier != NULL) 1674655291f2SDavid Xu mqueue_send_notification(mq); 1675655291f2SDavid Xu if (mq->mq_flags & MQ_RSEL) { 1676655291f2SDavid Xu mq->mq_flags &= ~MQ_RSEL; 1677655291f2SDavid Xu selwakeuppri(&mq->mq_rsel, PSOCK); 1678655291f2SDavid Xu } 1679655291f2SDavid Xu KNOTE_LOCKED(&mq->mq_rsel.si_note, 0); 1680655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1681655291f2SDavid Xu return (0); 1682655291f2SDavid Xu } 1683655291f2SDavid Xu 1684655291f2SDavid Xu /* 1685655291f2SDavid Xu * Send realtime a signal to process which registered itself 1686655291f2SDavid Xu * successfully by mq_notify. 1687655291f2SDavid Xu */ 1688655291f2SDavid Xu static void 1689655291f2SDavid Xu mqueue_send_notification(struct mqueue *mq) 1690655291f2SDavid Xu { 1691b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1692b2f92ef9SDavid Xu struct proc *p; 1693655291f2SDavid Xu 1694655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 1695b2f92ef9SDavid Xu nt = mq->mq_notifier; 1696052ea11cSDavid Xu if (nt->nt_sigev.sigev_notify != SIGEV_NONE) { 1697b2f92ef9SDavid Xu p = nt->nt_proc; 1698b2f92ef9SDavid Xu PROC_LOCK(p); 1699b2f92ef9SDavid Xu if (!KSI_ONQ(&nt->nt_ksi)) 1700b2f92ef9SDavid Xu psignal_event(p, &nt->nt_sigev, &nt->nt_ksi); 1701b2f92ef9SDavid Xu PROC_UNLOCK(p); 1702052ea11cSDavid Xu } 1703655291f2SDavid Xu mq->mq_notifier = NULL; 1704655291f2SDavid Xu } 1705655291f2SDavid Xu 1706655291f2SDavid Xu /* 1707655291f2SDavid Xu * Get a message. if waitok is false, thread will not be 1708655291f2SDavid Xu * blocked if there is no data in queue, otherwise, absolute 1709655291f2SDavid Xu * time will be checked. 1710655291f2SDavid Xu */ 1711655291f2SDavid Xu int 1712655291f2SDavid Xu mqueue_receive(struct mqueue *mq, char *msg_ptr, 1713655291f2SDavid Xu size_t msg_len, unsigned *msg_prio, int waitok, 1714655291f2SDavid Xu const struct timespec *abs_timeout) 1715655291f2SDavid Xu { 1716655291f2SDavid Xu struct mqueue_msg *msg; 1717655291f2SDavid Xu struct timespec ets, ts, ts2; 1718655291f2SDavid Xu struct timeval tv; 1719655291f2SDavid Xu int error; 1720655291f2SDavid Xu 1721655291f2SDavid Xu if (msg_len < mq->mq_msgsize) 1722655291f2SDavid Xu return (EMSGSIZE); 1723655291f2SDavid Xu 1724655291f2SDavid Xu /* O_NONBLOCK case */ 1725655291f2SDavid Xu if (!waitok) { 1726655291f2SDavid Xu error = _mqueue_recv(mq, &msg, -1); 1727655291f2SDavid Xu if (error) 1728655291f2SDavid Xu return (error); 1729655291f2SDavid Xu goto received; 1730655291f2SDavid Xu } 1731655291f2SDavid Xu 1732655291f2SDavid Xu /* we allow a null timeout (wait forever). */ 1733655291f2SDavid Xu if (abs_timeout == NULL) { 1734655291f2SDavid Xu error = _mqueue_recv(mq, &msg, 0); 1735655291f2SDavid Xu if (error) 1736655291f2SDavid Xu return (error); 1737655291f2SDavid Xu goto received; 1738655291f2SDavid Xu } 1739655291f2SDavid Xu 1740655291f2SDavid Xu /* try to get a message before checking time */ 1741655291f2SDavid Xu error = _mqueue_recv(mq, &msg, -1); 1742655291f2SDavid Xu if (error == 0) 1743655291f2SDavid Xu goto received; 1744655291f2SDavid Xu 1745655291f2SDavid Xu if (error != EAGAIN) 1746655291f2SDavid Xu return (error); 1747655291f2SDavid Xu 1748655291f2SDavid Xu error = copyin(abs_timeout, &ets, sizeof(ets)); 1749655291f2SDavid Xu if (error != 0) 1750655291f2SDavid Xu return (error); 1751655291f2SDavid Xu if (ets.tv_nsec >= 1000000000 || ets.tv_nsec < 0) { 1752655291f2SDavid Xu error = EINVAL; 1753655291f2SDavid Xu return (error); 1754655291f2SDavid Xu } 1755655291f2SDavid Xu 1756655291f2SDavid Xu for (;;) { 1757655291f2SDavid Xu ts2 = ets; 1758a6de716dSDavid Xu getnanotime(&ts); 1759655291f2SDavid Xu timespecsub(&ts2, &ts); 1760655291f2SDavid Xu if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { 1761655291f2SDavid Xu error = ETIMEDOUT; 1762655291f2SDavid Xu return (error); 1763655291f2SDavid Xu } 1764655291f2SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts2); 1765655291f2SDavid Xu error = _mqueue_recv(mq, &msg, tvtohz(&tv)); 1766655291f2SDavid Xu if (error == 0) 1767655291f2SDavid Xu break; 1768655291f2SDavid Xu if (error != ETIMEDOUT) 1769655291f2SDavid Xu return (error); 1770655291f2SDavid Xu } 1771655291f2SDavid Xu 1772655291f2SDavid Xu received: 1773655291f2SDavid Xu error = mqueue_savemsg(msg, msg_ptr, msg_prio); 1774655291f2SDavid Xu if (error == 0) { 1775655291f2SDavid Xu curthread->td_retval[0] = msg->msg_size; 1776655291f2SDavid Xu curthread->td_retval[1] = 0; 1777655291f2SDavid Xu } 1778655291f2SDavid Xu mqueue_freemsg(msg); 1779655291f2SDavid Xu return (error); 1780655291f2SDavid Xu } 1781655291f2SDavid Xu 1782655291f2SDavid Xu /* 1783655291f2SDavid Xu * Common routine to receive a message 1784655291f2SDavid Xu */ 1785655291f2SDavid Xu static int 1786655291f2SDavid Xu _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo) 1787655291f2SDavid Xu { 1788655291f2SDavid Xu int error = 0; 1789655291f2SDavid Xu 1790655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 1791655291f2SDavid Xu while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) { 1792655291f2SDavid Xu if (timo < 0) { 1793655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1794655291f2SDavid Xu return (EAGAIN); 1795655291f2SDavid Xu } 1796655291f2SDavid Xu mq->mq_receivers++; 1797655291f2SDavid Xu error = msleep(&mq->mq_receivers, &mq->mq_mutex, 1798655291f2SDavid Xu PSOCK | PCATCH, "mqrecv", timo); 1799655291f2SDavid Xu mq->mq_receivers--; 1800655291f2SDavid Xu if (error == EAGAIN) 1801655291f2SDavid Xu error = ETIMEDOUT; 1802655291f2SDavid Xu } 1803655291f2SDavid Xu if (*msg != NULL) { 1804655291f2SDavid Xu error = 0; 1805655291f2SDavid Xu TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link); 1806655291f2SDavid Xu mq->mq_curmsgs--; 1807655291f2SDavid Xu mq->mq_totalbytes -= (*msg)->msg_size; 1808655291f2SDavid Xu if (mq->mq_senders) 1809655291f2SDavid Xu wakeup_one(&mq->mq_senders); 1810655291f2SDavid Xu if (mq->mq_flags & MQ_WSEL) { 1811655291f2SDavid Xu mq->mq_flags &= ~MQ_WSEL; 1812655291f2SDavid Xu selwakeuppri(&mq->mq_wsel, PSOCK); 1813655291f2SDavid Xu } 1814655291f2SDavid Xu KNOTE_LOCKED(&mq->mq_wsel.si_note, 0); 1815655291f2SDavid Xu } 1816655291f2SDavid Xu if (mq->mq_notifier != NULL && mq->mq_receivers == 0 && 1817655291f2SDavid Xu !TAILQ_EMPTY(&mq->mq_msgq)) { 1818655291f2SDavid Xu mqueue_send_notification(mq); 1819655291f2SDavid Xu } 1820655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1821655291f2SDavid Xu return (error); 1822655291f2SDavid Xu } 1823655291f2SDavid Xu 1824b2f92ef9SDavid Xu static __inline struct mqueue_notifier * 1825b2f92ef9SDavid Xu notifier_alloc(void) 1826655291f2SDavid Xu { 1827b2f92ef9SDavid Xu return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO)); 1828655291f2SDavid Xu } 1829655291f2SDavid Xu 1830655291f2SDavid Xu static __inline void 1831b2f92ef9SDavid Xu notifier_free(struct mqueue_notifier *p) 1832655291f2SDavid Xu { 1833b2f92ef9SDavid Xu uma_zfree(mqnoti_zone, p); 1834b2f92ef9SDavid Xu } 1835b2f92ef9SDavid Xu 1836b2f92ef9SDavid Xu static struct mqueue_notifier * 1837b2f92ef9SDavid Xu notifier_search(struct proc *p, int fd) 1838b2f92ef9SDavid Xu { 1839b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1840b2f92ef9SDavid Xu 1841b2f92ef9SDavid Xu LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) { 18429da8a32aSDavid Xu if (nt->nt_ksi.ksi_mqd == fd) 1843b2f92ef9SDavid Xu break; 1844b2f92ef9SDavid Xu } 1845b2f92ef9SDavid Xu return (nt); 1846b2f92ef9SDavid Xu } 1847b2f92ef9SDavid Xu 1848102178d0SDavid Xu static __inline void 1849b2f92ef9SDavid Xu notifier_insert(struct proc *p, struct mqueue_notifier *nt) 1850b2f92ef9SDavid Xu { 1851b2f92ef9SDavid Xu LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link); 1852b2f92ef9SDavid Xu } 1853b2f92ef9SDavid Xu 1854102178d0SDavid Xu static __inline void 1855b2f92ef9SDavid Xu notifier_delete(struct proc *p, struct mqueue_notifier *nt) 1856b2f92ef9SDavid Xu { 1857b2f92ef9SDavid Xu LIST_REMOVE(nt, nt_link); 1858b2f92ef9SDavid Xu notifier_free(nt); 1859b2f92ef9SDavid Xu } 1860b2f92ef9SDavid Xu 1861b2f92ef9SDavid Xu static void 1862b2f92ef9SDavid Xu notifier_remove(struct proc *p, struct mqueue *mq, int fd) 1863b2f92ef9SDavid Xu { 1864b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1865b2f92ef9SDavid Xu 1866b2f92ef9SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 1867b2f92ef9SDavid Xu PROC_LOCK(p); 1868b2f92ef9SDavid Xu nt = notifier_search(p, fd); 1869b2f92ef9SDavid Xu if (nt != NULL) { 1870b2f92ef9SDavid Xu if (mq->mq_notifier == nt) 1871b2f92ef9SDavid Xu mq->mq_notifier = NULL; 1872b2f92ef9SDavid Xu sigqueue_take(&nt->nt_ksi); 1873b2f92ef9SDavid Xu notifier_delete(p, nt); 1874b2f92ef9SDavid Xu } 1875b2f92ef9SDavid Xu PROC_UNLOCK(p); 1876655291f2SDavid Xu } 1877655291f2SDavid Xu 1878655291f2SDavid Xu /* 1879655291f2SDavid Xu * Syscall to open a message queue 1880655291f2SDavid Xu */ 1881655291f2SDavid Xu int 1882655291f2SDavid Xu mq_open(struct thread *td, struct mq_open_args *uap) 1883655291f2SDavid Xu { 1884655291f2SDavid Xu char path[MQFS_NAMELEN + 1]; 1885655291f2SDavid Xu struct mq_attr attr, *pattr; 1886655291f2SDavid Xu struct mqfs_node *pn; 1887655291f2SDavid Xu struct filedesc *fdp; 1888655291f2SDavid Xu struct file *fp; 1889655291f2SDavid Xu struct mqueue *mq; 1890655291f2SDavid Xu int fd, error, len, flags, cmode; 1891655291f2SDavid Xu 1892655291f2SDavid Xu if ((uap->flags & O_ACCMODE) == O_ACCMODE) 1893655291f2SDavid Xu return (EINVAL); 1894655291f2SDavid Xu 1895655291f2SDavid Xu fdp = td->td_proc->p_fd; 1896655291f2SDavid Xu flags = FFLAGS(uap->flags); 1897655291f2SDavid Xu cmode = (((uap->mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT); 1898f72b11a4SDavid Xu mq = NULL; 1899655291f2SDavid Xu if ((flags & O_CREAT) && (uap->attr != NULL)) { 1900655291f2SDavid Xu error = copyin(uap->attr, &attr, sizeof(attr)); 1901655291f2SDavid Xu if (error) 1902655291f2SDavid Xu return (error); 1903655291f2SDavid Xu if (attr.mq_maxmsg <= 0 || attr.mq_maxmsg > maxmsg) 1904655291f2SDavid Xu return (EINVAL); 1905655291f2SDavid Xu if (attr.mq_msgsize <= 0 || attr.mq_msgsize > maxmsgsize) 1906655291f2SDavid Xu return (EINVAL); 1907655291f2SDavid Xu pattr = &attr; 1908655291f2SDavid Xu } else 1909655291f2SDavid Xu pattr = NULL; 1910655291f2SDavid Xu 1911655291f2SDavid Xu error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL); 1912655291f2SDavid Xu if (error) 1913655291f2SDavid Xu return (error); 1914655291f2SDavid Xu 1915655291f2SDavid Xu /* 1916655291f2SDavid Xu * The first character of name must be a slash (/) character 1917655291f2SDavid Xu * and the remaining characters of name cannot include any slash 1918655291f2SDavid Xu * characters. 1919655291f2SDavid Xu */ 1920655291f2SDavid Xu len = strlen(path); 1921655291f2SDavid Xu if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL) 1922655291f2SDavid Xu return (EINVAL); 1923655291f2SDavid Xu 1924655291f2SDavid Xu error = falloc(td, &fp, &fd); 1925655291f2SDavid Xu if (error) 1926655291f2SDavid Xu return (error); 1927655291f2SDavid Xu 1928655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 1929655291f2SDavid Xu pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1); 1930655291f2SDavid Xu if (pn == NULL) { 1931655291f2SDavid Xu if (!(flags & O_CREAT)) { 1932655291f2SDavid Xu error = ENOENT; 1933655291f2SDavid Xu } else { 1934655291f2SDavid Xu mq = mqueue_alloc(pattr); 1935b2f92ef9SDavid Xu if (mq == NULL) { 1936b2f92ef9SDavid Xu error = ENFILE; 1937b2f92ef9SDavid Xu } else { 1938655291f2SDavid Xu pn = mqfs_create_file(mqfs_data.mi_root, 1939655291f2SDavid Xu path + 1, len - 1); 1940655291f2SDavid Xu if (pn == NULL) { 1941655291f2SDavid Xu error = ENOSPC; 1942b2f92ef9SDavid Xu mqueue_free(mq); 1943b2f92ef9SDavid Xu } 1944655291f2SDavid Xu } 1945655291f2SDavid Xu } 1946655291f2SDavid Xu 1947655291f2SDavid Xu if (error == 0) { 1948655291f2SDavid Xu pn->mn_data = mq; 1949655291f2SDavid Xu getnanotime(&pn->mn_birth); 1950655291f2SDavid Xu pn->mn_ctime = pn->mn_atime = pn->mn_mtime 1951655291f2SDavid Xu = pn->mn_birth; 1952655291f2SDavid Xu pn->mn_uid = td->td_ucred->cr_uid; 1953655291f2SDavid Xu pn->mn_gid = td->td_ucred->cr_gid; 1954655291f2SDavid Xu pn->mn_mode = cmode; 1955655291f2SDavid Xu } 1956655291f2SDavid Xu } else { 1957655291f2SDavid Xu if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) { 1958655291f2SDavid Xu error = EEXIST; 1959655291f2SDavid Xu } else { 1960655291f2SDavid Xu int acc_mode = 0; 1961655291f2SDavid Xu 1962655291f2SDavid Xu if (flags & FREAD) 1963655291f2SDavid Xu acc_mode |= VREAD; 1964655291f2SDavid Xu if (flags & FWRITE) 1965655291f2SDavid Xu acc_mode |= VWRITE; 1966655291f2SDavid Xu error = vaccess(VREG, pn->mn_mode, pn->mn_uid, 1967655291f2SDavid Xu pn->mn_gid, acc_mode, td->td_ucred, NULL); 1968655291f2SDavid Xu } 1969655291f2SDavid Xu } 1970655291f2SDavid Xu 1971655291f2SDavid Xu if (error) { 1972655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 1973655291f2SDavid Xu fdclose(fdp, fp, fd, td); 1974655291f2SDavid Xu fdrop(fp, td); 1975655291f2SDavid Xu return (error); 1976655291f2SDavid Xu } 1977655291f2SDavid Xu 1978655291f2SDavid Xu mqnode_addref(pn); 1979655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 1980655291f2SDavid Xu 1981655291f2SDavid Xu FILE_LOCK(fp); 1982655291f2SDavid Xu fp->f_flag = (flags & (FREAD | FWRITE | O_NONBLOCK)); 1983655291f2SDavid Xu fp->f_type = DTYPE_MQUEUE; 1984655291f2SDavid Xu fp->f_ops = &mqueueops; 1985b2f92ef9SDavid Xu fp->f_data = pn; 1986655291f2SDavid Xu FILE_UNLOCK(fp); 1987655291f2SDavid Xu 1988655291f2SDavid Xu FILEDESC_LOCK_FAST(fdp); 1989655291f2SDavid Xu if (fdp->fd_ofiles[fd] == fp) 1990655291f2SDavid Xu fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 1991655291f2SDavid Xu FILEDESC_UNLOCK_FAST(fdp); 1992655291f2SDavid Xu td->td_retval[0] = fd; 1993655291f2SDavid Xu fdrop(fp, td); 1994655291f2SDavid Xu return (0); 1995655291f2SDavid Xu } 1996655291f2SDavid Xu 1997655291f2SDavid Xu /* 1998655291f2SDavid Xu * Syscall to unlink a message queue 1999655291f2SDavid Xu */ 2000655291f2SDavid Xu int 2001655291f2SDavid Xu mq_unlink(struct thread *td, struct mq_unlink_args *uap) 2002655291f2SDavid Xu { 2003655291f2SDavid Xu char path[MQFS_NAMELEN+1]; 2004655291f2SDavid Xu struct mqfs_node *pn; 2005655291f2SDavid Xu int error, len; 2006655291f2SDavid Xu 2007655291f2SDavid Xu error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL); 2008655291f2SDavid Xu if (error) 2009655291f2SDavid Xu return (error); 2010655291f2SDavid Xu 2011655291f2SDavid Xu len = strlen(path); 2012655291f2SDavid Xu if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL) 2013655291f2SDavid Xu return (EINVAL); 2014655291f2SDavid Xu 2015655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2016655291f2SDavid Xu pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1); 2017655291f2SDavid Xu if (pn != NULL) 2018655291f2SDavid Xu error = do_unlink(pn, td->td_ucred); 2019655291f2SDavid Xu else 2020655291f2SDavid Xu error = ENOENT; 2021655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2022655291f2SDavid Xu return (error); 2023655291f2SDavid Xu } 2024655291f2SDavid Xu 2025655291f2SDavid Xu typedef int (*_fgetf)(struct thread *, int, struct file **); 2026655291f2SDavid Xu 2027655291f2SDavid Xu /* 2028655291f2SDavid Xu * Get message queue by giving file slot 2029655291f2SDavid Xu */ 2030655291f2SDavid Xu static int 2031655291f2SDavid Xu _getmq(struct thread *td, int fd, _fgetf func, 2032655291f2SDavid Xu struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq) 2033655291f2SDavid Xu { 2034655291f2SDavid Xu struct mqfs_node *pn; 2035655291f2SDavid Xu int error; 2036655291f2SDavid Xu 2037655291f2SDavid Xu error = func(td, fd, fpp); 2038655291f2SDavid Xu if (error) 2039655291f2SDavid Xu return (error); 2040655291f2SDavid Xu if (&mqueueops != (*fpp)->f_ops) { 2041655291f2SDavid Xu fdrop(*fpp, td); 2042655291f2SDavid Xu return (EBADF); 2043655291f2SDavid Xu } 2044b2f92ef9SDavid Xu pn = (*fpp)->f_data; 2045655291f2SDavid Xu if (ppn) 2046655291f2SDavid Xu *ppn = pn; 2047655291f2SDavid Xu if (pmq) 2048655291f2SDavid Xu *pmq = pn->mn_data; 2049655291f2SDavid Xu return (0); 2050655291f2SDavid Xu } 2051655291f2SDavid Xu 2052655291f2SDavid Xu static __inline int 2053655291f2SDavid Xu getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, 2054655291f2SDavid Xu struct mqueue **pmq) 2055655291f2SDavid Xu { 2056655291f2SDavid Xu return _getmq(td, fd, fget, fpp, ppn, pmq); 2057655291f2SDavid Xu } 2058655291f2SDavid Xu 2059655291f2SDavid Xu static __inline int 2060655291f2SDavid Xu getmq_read(struct thread *td, int fd, struct file **fpp, 2061655291f2SDavid Xu struct mqfs_node **ppn, struct mqueue **pmq) 2062655291f2SDavid Xu { 2063655291f2SDavid Xu return _getmq(td, fd, fget_read, fpp, ppn, pmq); 2064655291f2SDavid Xu } 2065655291f2SDavid Xu 2066655291f2SDavid Xu static __inline int 2067655291f2SDavid Xu getmq_write(struct thread *td, int fd, struct file **fpp, 2068655291f2SDavid Xu struct mqfs_node **ppn, struct mqueue **pmq) 2069655291f2SDavid Xu { 2070655291f2SDavid Xu return _getmq(td, fd, fget_write, fpp, ppn, pmq); 2071655291f2SDavid Xu } 2072655291f2SDavid Xu 2073655291f2SDavid Xu /* 2074655291f2SDavid Xu * Syscall 2075655291f2SDavid Xu */ 2076655291f2SDavid Xu int 2077655291f2SDavid Xu mq_setattr(struct thread *td, struct mq_setattr_args *uap) 2078655291f2SDavid Xu { 2079655291f2SDavid Xu struct mqueue *mq; 2080655291f2SDavid Xu struct file *fp; 2081655291f2SDavid Xu struct mq_attr attr, oattr; 2082655291f2SDavid Xu int error; 2083655291f2SDavid Xu 2084655291f2SDavid Xu if (uap->attr) { 2085655291f2SDavid Xu error = copyin(uap->attr, &attr, sizeof(attr)); 2086655291f2SDavid Xu if (error) 2087655291f2SDavid Xu return (error); 2088655291f2SDavid Xu if (attr.mq_flags & ~O_NONBLOCK) 2089655291f2SDavid Xu return (EINVAL); 2090655291f2SDavid Xu } 2091655291f2SDavid Xu error = getmq(td, uap->mqd, &fp, NULL, &mq); 2092655291f2SDavid Xu if (error) 2093655291f2SDavid Xu return (error); 2094655291f2SDavid Xu oattr.mq_maxmsg = mq->mq_maxmsg; 2095655291f2SDavid Xu oattr.mq_msgsize = mq->mq_msgsize; 2096655291f2SDavid Xu oattr.mq_curmsgs = mq->mq_curmsgs; 2097655291f2SDavid Xu FILE_LOCK(fp); 2098655291f2SDavid Xu oattr.mq_flags = (O_NONBLOCK & fp->f_flag); 2099655291f2SDavid Xu if (uap->attr) { 2100655291f2SDavid Xu fp->f_flag &= ~O_NONBLOCK; 2101655291f2SDavid Xu fp->f_flag |= (attr.mq_flags & O_NONBLOCK); 2102655291f2SDavid Xu } 2103655291f2SDavid Xu FILE_UNLOCK(fp); 2104655291f2SDavid Xu fdrop(fp, td); 2105655291f2SDavid Xu if (uap->oattr) 2106655291f2SDavid Xu error = copyout(&oattr, uap->oattr, sizeof(oattr)); 2107655291f2SDavid Xu return (error); 2108655291f2SDavid Xu } 2109655291f2SDavid Xu 2110655291f2SDavid Xu /* 2111655291f2SDavid Xu * Syscall 2112655291f2SDavid Xu */ 2113655291f2SDavid Xu int 2114655291f2SDavid Xu mq_timedreceive(struct thread *td, struct mq_timedreceive_args *uap) 2115655291f2SDavid Xu { 2116655291f2SDavid Xu struct mqueue *mq; 2117655291f2SDavid Xu struct file *fp; 2118655291f2SDavid Xu int error; 2119655291f2SDavid Xu int waitok; 2120655291f2SDavid Xu 2121655291f2SDavid Xu error = getmq_read(td, uap->mqd, &fp, NULL, &mq); 2122655291f2SDavid Xu if (error) 2123655291f2SDavid Xu return (error); 2124655291f2SDavid Xu waitok = !(fp->f_flag & O_NONBLOCK); 2125655291f2SDavid Xu error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len, 2126655291f2SDavid Xu uap->msg_prio, waitok, uap->abs_timeout); 2127655291f2SDavid Xu fdrop(fp, td); 2128655291f2SDavid Xu return (error); 2129655291f2SDavid Xu } 2130655291f2SDavid Xu 2131655291f2SDavid Xu /* 2132655291f2SDavid Xu * Syscall 2133655291f2SDavid Xu */ 2134655291f2SDavid Xu int 2135655291f2SDavid Xu mq_timedsend(struct thread *td, struct mq_timedsend_args *uap) 2136655291f2SDavid Xu { 2137655291f2SDavid Xu struct mqueue *mq; 2138655291f2SDavid Xu struct file *fp; 2139655291f2SDavid Xu int error, waitok; 2140655291f2SDavid Xu 2141655291f2SDavid Xu error = getmq_write(td, uap->mqd, &fp, NULL, &mq); 2142655291f2SDavid Xu if (error) 2143655291f2SDavid Xu return (error); 2144655291f2SDavid Xu waitok = !(fp->f_flag & O_NONBLOCK); 2145655291f2SDavid Xu error = mqueue_send(mq, uap->msg_ptr, uap->msg_len, 2146655291f2SDavid Xu uap->msg_prio, waitok, uap->abs_timeout); 2147655291f2SDavid Xu fdrop(fp, td); 2148655291f2SDavid Xu return (error); 2149655291f2SDavid Xu } 2150655291f2SDavid Xu 2151655291f2SDavid Xu /* 2152655291f2SDavid Xu * Syscall 2153655291f2SDavid Xu */ 2154655291f2SDavid Xu int 2155655291f2SDavid Xu mq_notify(struct thread *td, struct mq_notify_args *uap) 2156655291f2SDavid Xu { 2157655291f2SDavid Xu struct sigevent ev; 2158b2f92ef9SDavid Xu struct filedesc *fdp; 2159b2f92ef9SDavid Xu struct proc *p; 2160655291f2SDavid Xu struct mqueue *mq; 2161655291f2SDavid Xu struct file *fp; 2162b2f92ef9SDavid Xu struct mqueue_notifier *nt, *newnt = NULL; 2163655291f2SDavid Xu int error; 2164655291f2SDavid Xu 2165b2f92ef9SDavid Xu p = td->td_proc; 2166b2f92ef9SDavid Xu fdp = td->td_proc->p_fd; 2167655291f2SDavid Xu if (uap->sigev) { 2168655291f2SDavid Xu error = copyin(uap->sigev, &ev, sizeof(ev)); 2169655291f2SDavid Xu if (error) 2170655291f2SDavid Xu return (error); 2171655291f2SDavid Xu if (ev.sigev_notify != SIGEV_SIGNAL && 21729947b459SDavid Xu ev.sigev_notify != SIGEV_THREAD_ID && 21739947b459SDavid Xu ev.sigev_notify != SIGEV_NONE) 2174655291f2SDavid Xu return (EINVAL); 2175655291f2SDavid Xu if ((ev.sigev_notify == SIGEV_SIGNAL || 2176655291f2SDavid Xu ev.sigev_notify == SIGEV_THREAD_ID) && 2177655291f2SDavid Xu !_SIG_VALID(ev.sigev_signo)) 2178655291f2SDavid Xu return (EINVAL); 2179655291f2SDavid Xu } 2180655291f2SDavid Xu error = getmq(td, uap->mqd, &fp, NULL, &mq); 2181655291f2SDavid Xu if (error) 2182655291f2SDavid Xu return (error); 2183b2f92ef9SDavid Xu again: 2184b2f92ef9SDavid Xu FILEDESC_LOCK_FAST(fdp); 2185b2f92ef9SDavid Xu if (fget_locked(fdp, uap->mqd) != fp) { 2186b2f92ef9SDavid Xu FILEDESC_UNLOCK_FAST(fdp); 2187b2f92ef9SDavid Xu error = EBADF; 2188b2f92ef9SDavid Xu goto out; 2189b2f92ef9SDavid Xu } 2190655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 2191b2f92ef9SDavid Xu FILEDESC_UNLOCK_FAST(fdp); 2192655291f2SDavid Xu if (uap->sigev != NULL) { 2193655291f2SDavid Xu if (mq->mq_notifier != NULL) { 2194655291f2SDavid Xu error = EBUSY; 2195052ea11cSDavid Xu } else { 2196b2f92ef9SDavid Xu PROC_LOCK(p); 2197b2f92ef9SDavid Xu nt = notifier_search(p, uap->mqd); 2198b2f92ef9SDavid Xu if (nt == NULL) { 2199b2f92ef9SDavid Xu if (newnt == NULL) { 2200b2f92ef9SDavid Xu PROC_UNLOCK(p); 2201b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2202b2f92ef9SDavid Xu newnt = notifier_alloc(); 2203b2f92ef9SDavid Xu goto again; 2204b2f92ef9SDavid Xu } 2205b2f92ef9SDavid Xu } 2206b2f92ef9SDavid Xu 2207b2f92ef9SDavid Xu if (nt != NULL) { 2208b2f92ef9SDavid Xu sigqueue_take(&nt->nt_ksi); 2209b2f92ef9SDavid Xu if (newnt != NULL) { 2210b2f92ef9SDavid Xu notifier_free(newnt); 2211b2f92ef9SDavid Xu newnt = NULL; 2212b2f92ef9SDavid Xu } 2213b2f92ef9SDavid Xu } else { 2214b2f92ef9SDavid Xu nt = newnt; 2215b2f92ef9SDavid Xu newnt = NULL; 2216b2f92ef9SDavid Xu ksiginfo_init(&nt->nt_ksi); 2217b2f92ef9SDavid Xu nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT; 2218b2f92ef9SDavid Xu nt->nt_ksi.ksi_code = SI_MESGQ; 2219b2f92ef9SDavid Xu nt->nt_proc = p; 22209da8a32aSDavid Xu nt->nt_ksi.ksi_mqd = uap->mqd; 2221b2f92ef9SDavid Xu notifier_insert(p, nt); 2222b2f92ef9SDavid Xu } 2223b2f92ef9SDavid Xu nt->nt_sigev = ev; 2224b2f92ef9SDavid Xu mq->mq_notifier = nt; 2225b2f92ef9SDavid Xu PROC_UNLOCK(p); 2226655291f2SDavid Xu /* 2227b2f92ef9SDavid Xu * if there is no receivers and message queue 2228b2f92ef9SDavid Xu * is not empty, we should send notification 2229b2f92ef9SDavid Xu * as soon as possible. 2230655291f2SDavid Xu */ 2231655291f2SDavid Xu if (mq->mq_receivers == 0 && 2232655291f2SDavid Xu !TAILQ_EMPTY(&mq->mq_msgq)) 2233655291f2SDavid Xu mqueue_send_notification(mq); 2234655291f2SDavid Xu } 2235655291f2SDavid Xu } else { 2236b2f92ef9SDavid Xu notifier_remove(p, mq, uap->mqd); 2237655291f2SDavid Xu } 2238655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2239b2f92ef9SDavid Xu 2240b2f92ef9SDavid Xu out: 2241655291f2SDavid Xu fdrop(fp, td); 2242b2f92ef9SDavid Xu if (newnt != NULL) 2243b2f92ef9SDavid Xu notifier_free(newnt); 2244655291f2SDavid Xu return (error); 2245655291f2SDavid Xu } 2246655291f2SDavid Xu 2247b2f92ef9SDavid Xu static void 2248b2f92ef9SDavid Xu mqueue_fdclose(struct thread *td, int fd, struct file *fp) 2249b2f92ef9SDavid Xu { 2250b2f92ef9SDavid Xu struct filedesc *fdp; 2251b2f92ef9SDavid Xu struct mqueue *mq; 2252b2f92ef9SDavid Xu 2253b2f92ef9SDavid Xu fdp = td->td_proc->p_fd; 2254b2f92ef9SDavid Xu FILEDESC_LOCK_ASSERT(fdp, MA_OWNED); 2255b2f92ef9SDavid Xu if (fp->f_ops == &mqueueops) { 2256b2f92ef9SDavid Xu mq = FPTOMQ(fp); 2257b2f92ef9SDavid Xu mtx_lock(&mq->mq_mutex); 2258b2f92ef9SDavid Xu notifier_remove(td->td_proc, mq, fd); 2259b2f92ef9SDavid Xu 2260b2f92ef9SDavid Xu /* have to wakeup thread in same process */ 2261b2f92ef9SDavid Xu if (mq->mq_flags & MQ_RSEL) { 2262b2f92ef9SDavid Xu mq->mq_flags &= ~MQ_RSEL; 2263b2f92ef9SDavid Xu selwakeuppri(&mq->mq_rsel, PSOCK); 2264b2f92ef9SDavid Xu } 2265b2f92ef9SDavid Xu if (mq->mq_flags & MQ_WSEL) { 2266b2f92ef9SDavid Xu mq->mq_flags &= ~MQ_WSEL; 2267b2f92ef9SDavid Xu selwakeuppri(&mq->mq_wsel, PSOCK); 2268b2f92ef9SDavid Xu } 2269b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2270b2f92ef9SDavid Xu } 2271b2f92ef9SDavid Xu } 2272b2f92ef9SDavid Xu 2273b2f92ef9SDavid Xu static void 2274b2f92ef9SDavid Xu mq_proc_exit(void *arg __unused, struct proc *p) 2275b2f92ef9SDavid Xu { 2276b2f92ef9SDavid Xu struct filedesc *fdp; 2277b2f92ef9SDavid Xu struct file *fp; 2278b2f92ef9SDavid Xu struct mqueue *mq; 2279b2f92ef9SDavid Xu int i; 2280b2f92ef9SDavid Xu 2281b2f92ef9SDavid Xu fdp = p->p_fd; 2282b2f92ef9SDavid Xu FILEDESC_LOCK_FAST(fdp); 2283b2f92ef9SDavid Xu for (i = 0; i < fdp->fd_nfiles; ++i) { 2284b2f92ef9SDavid Xu fp = fget_locked(fdp, i); 2285b2f92ef9SDavid Xu if (fp != NULL && fp->f_ops == &mqueueops) { 2286b2f92ef9SDavid Xu mq = FPTOMQ(fp); 2287b2f92ef9SDavid Xu mtx_lock(&mq->mq_mutex); 2288b2f92ef9SDavid Xu notifier_remove(p, FPTOMQ(fp), i); 2289b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2290b2f92ef9SDavid Xu } 2291b2f92ef9SDavid Xu } 2292b2f92ef9SDavid Xu FILEDESC_UNLOCK_FAST(fdp); 2293b2f92ef9SDavid Xu KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left")); 2294b2f92ef9SDavid Xu } 2295b2f92ef9SDavid Xu 2296655291f2SDavid Xu static int 2297655291f2SDavid Xu mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 2298655291f2SDavid Xu int flags, struct thread *td) 2299655291f2SDavid Xu { 2300655291f2SDavid Xu return (EOPNOTSUPP); 2301655291f2SDavid Xu } 2302655291f2SDavid Xu 2303655291f2SDavid Xu static int 2304655291f2SDavid Xu mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 2305655291f2SDavid Xu int flags, struct thread *td) 2306655291f2SDavid Xu { 2307655291f2SDavid Xu return (EOPNOTSUPP); 2308655291f2SDavid Xu } 2309655291f2SDavid Xu 2310655291f2SDavid Xu static int 2311655291f2SDavid Xu mqf_ioctl(struct file *fp, u_long cmd, void *data, 2312655291f2SDavid Xu struct ucred *active_cred, struct thread *td) 2313655291f2SDavid Xu { 2314655291f2SDavid Xu return (ENOTTY); 2315655291f2SDavid Xu } 2316655291f2SDavid Xu 2317655291f2SDavid Xu static int 2318655291f2SDavid Xu mqf_poll(struct file *fp, int events, struct ucred *active_cred, 2319655291f2SDavid Xu struct thread *td) 2320655291f2SDavid Xu { 2321655291f2SDavid Xu struct mqueue *mq = FPTOMQ(fp); 2322655291f2SDavid Xu int revents = 0; 2323655291f2SDavid Xu 2324655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 2325655291f2SDavid Xu if (events & (POLLIN | POLLRDNORM)) { 2326655291f2SDavid Xu if (mq->mq_curmsgs) { 2327655291f2SDavid Xu revents |= events & (POLLIN | POLLRDNORM); 2328655291f2SDavid Xu } else { 2329655291f2SDavid Xu mq->mq_flags |= MQ_RSEL; 2330655291f2SDavid Xu selrecord(td, &mq->mq_rsel); 2331655291f2SDavid Xu } 2332655291f2SDavid Xu } 2333655291f2SDavid Xu if (events & POLLOUT) { 2334655291f2SDavid Xu if (mq->mq_curmsgs < mq->mq_maxmsg) 2335655291f2SDavid Xu revents |= POLLOUT; 2336655291f2SDavid Xu else { 2337655291f2SDavid Xu mq->mq_flags |= MQ_WSEL; 2338655291f2SDavid Xu selrecord(td, &mq->mq_wsel); 2339655291f2SDavid Xu } 2340655291f2SDavid Xu } 2341655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2342655291f2SDavid Xu return (revents); 2343655291f2SDavid Xu } 2344655291f2SDavid Xu 2345655291f2SDavid Xu static int 2346655291f2SDavid Xu mqf_close(struct file *fp, struct thread *td) 2347655291f2SDavid Xu { 2348655291f2SDavid Xu struct mqfs_node *pn; 2349655291f2SDavid Xu 2350655291f2SDavid Xu FILE_LOCK(fp); 2351655291f2SDavid Xu fp->f_ops = &badfileops; 2352655291f2SDavid Xu FILE_UNLOCK(fp); 2353b2f92ef9SDavid Xu pn = fp->f_data; 2354655291f2SDavid Xu fp->f_data = NULL; 2355655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2356655291f2SDavid Xu mqnode_release(pn); 2357655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2358655291f2SDavid Xu return (0); 2359655291f2SDavid Xu } 2360655291f2SDavid Xu 2361655291f2SDavid Xu static int 2362655291f2SDavid Xu mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred, 2363655291f2SDavid Xu struct thread *td) 2364655291f2SDavid Xu { 2365b2f92ef9SDavid Xu struct mqfs_node *pn = fp->f_data; 2366655291f2SDavid Xu 2367655291f2SDavid Xu bzero(st, sizeof *st); 2368655291f2SDavid Xu st->st_atimespec = pn->mn_atime; 2369655291f2SDavid Xu st->st_mtimespec = pn->mn_mtime; 2370655291f2SDavid Xu st->st_ctimespec = pn->mn_ctime; 2371655291f2SDavid Xu st->st_birthtimespec = pn->mn_birth; 2372655291f2SDavid Xu st->st_uid = pn->mn_uid; 2373655291f2SDavid Xu st->st_gid = pn->mn_gid; 2374655291f2SDavid Xu st->st_mode = S_IFIFO | pn->mn_mode; 2375655291f2SDavid Xu return (0); 2376655291f2SDavid Xu } 2377655291f2SDavid Xu 2378655291f2SDavid Xu static int 2379655291f2SDavid Xu mqf_kqfilter(struct file *fp, struct knote *kn) 2380655291f2SDavid Xu { 2381655291f2SDavid Xu struct mqueue *mq = FPTOMQ(fp); 2382655291f2SDavid Xu int error = 0; 2383655291f2SDavid Xu 2384655291f2SDavid Xu if (kn->kn_filter == EVFILT_READ) { 2385655291f2SDavid Xu kn->kn_fop = &mq_rfiltops; 2386655291f2SDavid Xu knlist_add(&mq->mq_rsel.si_note, kn, 0); 2387655291f2SDavid Xu } else if (kn->kn_filter == EVFILT_WRITE) { 2388655291f2SDavid Xu kn->kn_fop = &mq_wfiltops; 2389655291f2SDavid Xu knlist_add(&mq->mq_wsel.si_note, kn, 0); 2390655291f2SDavid Xu } else 2391655291f2SDavid Xu error = EINVAL; 2392655291f2SDavid Xu return (error); 2393655291f2SDavid Xu } 2394655291f2SDavid Xu 2395655291f2SDavid Xu static void 2396655291f2SDavid Xu filt_mqdetach(struct knote *kn) 2397655291f2SDavid Xu { 2398655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2399655291f2SDavid Xu 2400655291f2SDavid Xu if (kn->kn_filter == EVFILT_READ) 2401655291f2SDavid Xu knlist_remove(&mq->mq_rsel.si_note, kn, 0); 2402655291f2SDavid Xu else if (kn->kn_filter == EVFILT_WRITE) 2403655291f2SDavid Xu knlist_remove(&mq->mq_wsel.si_note, kn, 0); 2404655291f2SDavid Xu else 2405655291f2SDavid Xu panic("filt_mqdetach"); 2406655291f2SDavid Xu } 2407655291f2SDavid Xu 2408655291f2SDavid Xu static int 2409655291f2SDavid Xu filt_mqread(struct knote *kn, long hint) 2410655291f2SDavid Xu { 2411655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2412655291f2SDavid Xu 2413655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2414655291f2SDavid Xu return (mq->mq_curmsgs != 0); 2415655291f2SDavid Xu } 2416655291f2SDavid Xu 2417655291f2SDavid Xu static int 2418655291f2SDavid Xu filt_mqwrite(struct knote *kn, long hint) 2419655291f2SDavid Xu { 2420655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2421655291f2SDavid Xu 2422655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2423655291f2SDavid Xu return (mq->mq_curmsgs < mq->mq_maxmsg); 2424655291f2SDavid Xu } 2425655291f2SDavid Xu 2426655291f2SDavid Xu static struct fileops mqueueops = { 2427655291f2SDavid Xu .fo_read = mqf_read, 2428655291f2SDavid Xu .fo_write = mqf_write, 2429655291f2SDavid Xu .fo_ioctl = mqf_ioctl, 2430655291f2SDavid Xu .fo_poll = mqf_poll, 2431655291f2SDavid Xu .fo_kqfilter = mqf_kqfilter, 2432655291f2SDavid Xu .fo_stat = mqf_stat, 2433655291f2SDavid Xu .fo_close = mqf_close 2434655291f2SDavid Xu }; 2435655291f2SDavid Xu 2436655291f2SDavid Xu static struct vop_vector mqfs_vnodeops = { 2437655291f2SDavid Xu .vop_default = &default_vnodeops, 2438655291f2SDavid Xu .vop_access = mqfs_access, 2439655291f2SDavid Xu .vop_cachedlookup = mqfs_lookup, 2440655291f2SDavid Xu .vop_lookup = vfs_cache_lookup, 2441655291f2SDavid Xu .vop_reclaim = mqfs_reclaim, 2442655291f2SDavid Xu .vop_create = mqfs_create, 2443655291f2SDavid Xu .vop_remove = mqfs_remove, 2444655291f2SDavid Xu .vop_inactive = mqfs_inactive, 2445655291f2SDavid Xu .vop_open = mqfs_open, 2446655291f2SDavid Xu .vop_close = mqfs_close, 2447655291f2SDavid Xu .vop_getattr = mqfs_getattr, 2448655291f2SDavid Xu .vop_setattr = mqfs_setattr, 2449655291f2SDavid Xu .vop_read = mqfs_read, 2450655291f2SDavid Xu .vop_write = VOP_EOPNOTSUPP, 2451655291f2SDavid Xu .vop_readdir = mqfs_readdir, 2452655291f2SDavid Xu .vop_mkdir = VOP_EOPNOTSUPP, 2453655291f2SDavid Xu .vop_rmdir = VOP_EOPNOTSUPP 2454655291f2SDavid Xu }; 2455655291f2SDavid Xu 2456655291f2SDavid Xu static struct vfsops mqfs_vfsops = { 2457655291f2SDavid Xu .vfs_init = mqfs_init, 2458655291f2SDavid Xu .vfs_uninit = mqfs_uninit, 2459655291f2SDavid Xu .vfs_mount = mqfs_mount, 2460655291f2SDavid Xu .vfs_unmount = mqfs_unmount, 2461655291f2SDavid Xu .vfs_root = mqfs_root, 2462655291f2SDavid Xu .vfs_statfs = mqfs_statfs, 2463655291f2SDavid Xu }; 2464655291f2SDavid Xu 2465655291f2SDavid Xu SYSCALL_MODULE_HELPER(mq_open); 2466655291f2SDavid Xu SYSCALL_MODULE_HELPER(mq_setattr); 2467655291f2SDavid Xu SYSCALL_MODULE_HELPER(mq_timedsend); 2468655291f2SDavid Xu SYSCALL_MODULE_HELPER(mq_timedreceive); 2469655291f2SDavid Xu SYSCALL_MODULE_HELPER(mq_notify); 2470655291f2SDavid Xu SYSCALL_MODULE_HELPER(mq_unlink); 2471655291f2SDavid Xu 247247bf2cf9SDavid Xu VFS_SET(mqfs_vfsops, mqueuefs, VFCF_SYNTHETIC); 247347bf2cf9SDavid Xu MODULE_VERSION(mqueuefs, 1); 2474