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 482609222aSPawel Jakub Dawidek #include "opt_capsicum.h" 49afde2b65SKonstantin Belousov #include "opt_compat.h" 50afde2b65SKonstantin Belousov 51655291f2SDavid Xu #include <sys/param.h> 52655291f2SDavid Xu #include <sys/kernel.h> 53655291f2SDavid Xu #include <sys/systm.h> 54655291f2SDavid Xu #include <sys/limits.h> 55*adb023aeSJamie Gritton #include <sys/malloc.h> 56655291f2SDavid Xu #include <sys/buf.h> 574a144410SRobert Watson #include <sys/capsicum.h> 58655291f2SDavid Xu #include <sys/dirent.h> 59655291f2SDavid Xu #include <sys/event.h> 60b2f92ef9SDavid Xu #include <sys/eventhandler.h> 61655291f2SDavid Xu #include <sys/fcntl.h> 62655291f2SDavid Xu #include <sys/file.h> 635ee2d4acSDavid Xu #include <sys/filedesc.h> 64*adb023aeSJamie Gritton #include <sys/jail.h> 65655291f2SDavid Xu #include <sys/lock.h> 66655291f2SDavid Xu #include <sys/module.h> 67655291f2SDavid Xu #include <sys/mount.h> 68655291f2SDavid Xu #include <sys/mqueue.h> 69655291f2SDavid Xu #include <sys/mutex.h> 70655291f2SDavid Xu #include <sys/namei.h> 716aeb05d7STom Rhodes #include <sys/posix4.h> 72655291f2SDavid Xu #include <sys/poll.h> 73acd3428bSRobert Watson #include <sys/priv.h> 74655291f2SDavid Xu #include <sys/proc.h> 75655291f2SDavid Xu #include <sys/queue.h> 76655291f2SDavid Xu #include <sys/sysproto.h> 77655291f2SDavid Xu #include <sys/stat.h> 78655291f2SDavid Xu #include <sys/syscall.h> 79655291f2SDavid Xu #include <sys/syscallsubr.h> 805ee2d4acSDavid Xu #include <sys/sysent.h> 81655291f2SDavid Xu #include <sys/sx.h> 82655291f2SDavid Xu #include <sys/sysctl.h> 83655291f2SDavid Xu #include <sys/taskqueue.h> 84655291f2SDavid Xu #include <sys/unistd.h> 859696feebSJohn Baldwin #include <sys/user.h> 86655291f2SDavid Xu #include <sys/vnode.h> 87655291f2SDavid Xu #include <machine/atomic.h> 88655291f2SDavid Xu 89de5b1952SAlexander Leidinger FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support"); 90de5b1952SAlexander Leidinger 91655291f2SDavid Xu /* 92655291f2SDavid Xu * Limits and constants 93655291f2SDavid Xu */ 94655291f2SDavid Xu #define MQFS_NAMELEN NAME_MAX 95655291f2SDavid Xu #define MQFS_DELEN (8 + MQFS_NAMELEN) 96655291f2SDavid Xu 97655291f2SDavid Xu /* node types */ 98655291f2SDavid Xu typedef enum { 99655291f2SDavid Xu mqfstype_none = 0, 100655291f2SDavid Xu mqfstype_root, 101655291f2SDavid Xu mqfstype_dir, 102655291f2SDavid Xu mqfstype_this, 103655291f2SDavid Xu mqfstype_parent, 104655291f2SDavid Xu mqfstype_file, 105655291f2SDavid Xu mqfstype_symlink, 106655291f2SDavid Xu } mqfs_type_t; 107655291f2SDavid Xu 108655291f2SDavid Xu struct mqfs_node; 109655291f2SDavid Xu 110655291f2SDavid Xu /* 111655291f2SDavid Xu * mqfs_info: describes a mqfs instance 112655291f2SDavid Xu */ 113655291f2SDavid Xu struct mqfs_info { 114655291f2SDavid Xu struct sx mi_lock; 115655291f2SDavid Xu struct mqfs_node *mi_root; 116655291f2SDavid Xu struct unrhdr *mi_unrhdr; 117655291f2SDavid Xu }; 118655291f2SDavid Xu 119655291f2SDavid Xu struct mqfs_vdata { 120655291f2SDavid Xu LIST_ENTRY(mqfs_vdata) mv_link; 121655291f2SDavid Xu struct mqfs_node *mv_node; 122655291f2SDavid Xu struct vnode *mv_vnode; 123655291f2SDavid Xu struct task mv_task; 124655291f2SDavid Xu }; 125655291f2SDavid Xu 126655291f2SDavid Xu /* 127655291f2SDavid Xu * mqfs_node: describes a node (file or directory) within a mqfs 128655291f2SDavid Xu */ 129655291f2SDavid Xu struct mqfs_node { 130655291f2SDavid Xu char mn_name[MQFS_NAMELEN+1]; 131655291f2SDavid Xu struct mqfs_info *mn_info; 132655291f2SDavid Xu struct mqfs_node *mn_parent; 133655291f2SDavid Xu LIST_HEAD(,mqfs_node) mn_children; 134655291f2SDavid Xu LIST_ENTRY(mqfs_node) mn_sibling; 135655291f2SDavid Xu LIST_HEAD(,mqfs_vdata) mn_vnodes; 136*adb023aeSJamie Gritton const void *mn_pr_root; 137655291f2SDavid Xu int mn_refcount; 138655291f2SDavid Xu mqfs_type_t mn_type; 139655291f2SDavid Xu int mn_deleted; 14060ae52f7SEd Schouten uint32_t mn_fileno; 141655291f2SDavid Xu void *mn_data; 142655291f2SDavid Xu struct timespec mn_birth; 143655291f2SDavid Xu struct timespec mn_ctime; 144655291f2SDavid Xu struct timespec mn_atime; 145655291f2SDavid Xu struct timespec mn_mtime; 146655291f2SDavid Xu uid_t mn_uid; 147655291f2SDavid Xu gid_t mn_gid; 148655291f2SDavid Xu int mn_mode; 149655291f2SDavid Xu }; 150655291f2SDavid Xu 151655291f2SDavid Xu #define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node) 152655291f2SDavid Xu #define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data)) 153655291f2SDavid Xu #define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data)) 154b2f92ef9SDavid Xu #define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \ 155b2f92ef9SDavid Xu (fp)->f_data)->mn_data)) 156b2f92ef9SDavid Xu 157*adb023aeSJamie Gritton struct mqfs_osd { 158*adb023aeSJamie Gritton struct task mo_task; 159*adb023aeSJamie Gritton const void *mo_pr_root; 160*adb023aeSJamie Gritton }; 161*adb023aeSJamie Gritton 162655291f2SDavid Xu TAILQ_HEAD(msgq, mqueue_msg); 163655291f2SDavid Xu 164655291f2SDavid Xu struct mqueue; 165b2f92ef9SDavid Xu 166b2f92ef9SDavid Xu struct mqueue_notifier { 167b2f92ef9SDavid Xu LIST_ENTRY(mqueue_notifier) nt_link; 168b2f92ef9SDavid Xu struct sigevent nt_sigev; 169b2f92ef9SDavid Xu ksiginfo_t nt_ksi; 170b2f92ef9SDavid Xu struct proc *nt_proc; 171655291f2SDavid Xu }; 172655291f2SDavid Xu 173655291f2SDavid Xu struct mqueue { 174655291f2SDavid Xu struct mtx mq_mutex; 175655291f2SDavid Xu int mq_flags; 176655291f2SDavid Xu long mq_maxmsg; 177655291f2SDavid Xu long mq_msgsize; 178655291f2SDavid Xu long mq_curmsgs; 179655291f2SDavid Xu long mq_totalbytes; 180655291f2SDavid Xu struct msgq mq_msgq; 181655291f2SDavid Xu int mq_receivers; 182655291f2SDavid Xu int mq_senders; 183655291f2SDavid Xu struct selinfo mq_rsel; 184655291f2SDavid Xu struct selinfo mq_wsel; 185b2f92ef9SDavid Xu struct mqueue_notifier *mq_notifier; 186655291f2SDavid Xu }; 187655291f2SDavid Xu 188655291f2SDavid Xu #define MQ_RSEL 0x01 189655291f2SDavid Xu #define MQ_WSEL 0x02 190655291f2SDavid Xu 191655291f2SDavid Xu struct mqueue_msg { 192655291f2SDavid Xu TAILQ_ENTRY(mqueue_msg) msg_link; 193655291f2SDavid Xu unsigned int msg_prio; 194655291f2SDavid Xu unsigned int msg_size; 195655291f2SDavid Xu /* following real data... */ 196655291f2SDavid Xu }; 197655291f2SDavid Xu 1986472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0, 199655291f2SDavid Xu "POSIX real time message queue"); 200655291f2SDavid Xu 201655291f2SDavid Xu static int default_maxmsg = 10; 202655291f2SDavid Xu static int default_msgsize = 1024; 203655291f2SDavid Xu 2049947b459SDavid Xu static int maxmsg = 100; 205655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW, 206655291f2SDavid Xu &maxmsg, 0, "Default maximum messages in queue"); 207655291f2SDavid Xu static int maxmsgsize = 16384; 208655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW, 209655291f2SDavid Xu &maxmsgsize, 0, "Default maximum message size"); 210655291f2SDavid Xu static int maxmq = 100; 211655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW, 212655291f2SDavid Xu &maxmq, 0, "maximum message queues"); 213655291f2SDavid Xu static int curmq = 0; 214655291f2SDavid Xu SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW, 215655291f2SDavid Xu &curmq, 0, "current message queue number"); 216655291f2SDavid Xu static int unloadable = 0; 217655291f2SDavid Xu static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data"); 218655291f2SDavid Xu 219b2f92ef9SDavid Xu static eventhandler_tag exit_tag; 220b2f92ef9SDavid Xu 221655291f2SDavid Xu /* Only one instance per-system */ 222655291f2SDavid Xu static struct mqfs_info mqfs_data; 223655291f2SDavid Xu static uma_zone_t mqnode_zone; 224655291f2SDavid Xu static uma_zone_t mqueue_zone; 225655291f2SDavid Xu static uma_zone_t mvdata_zone; 226b2f92ef9SDavid Xu static uma_zone_t mqnoti_zone; 227655291f2SDavid Xu static struct vop_vector mqfs_vnodeops; 228655291f2SDavid Xu static struct fileops mqueueops; 229*adb023aeSJamie Gritton static unsigned mqfs_osd_jail_slot; 230655291f2SDavid Xu 231655291f2SDavid Xu /* 232655291f2SDavid Xu * Directory structure construction and manipulation 233655291f2SDavid Xu */ 234655291f2SDavid Xu #ifdef notyet 235655291f2SDavid Xu static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent, 236ba0360b1SDavid Xu const char *name, int namelen, struct ucred *cred, int mode); 237102178d0SDavid Xu static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent, 238ba0360b1SDavid Xu const char *name, int namelen, struct ucred *cred, int mode); 239655291f2SDavid Xu #endif 240655291f2SDavid Xu 241655291f2SDavid Xu static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent, 242ba0360b1SDavid Xu const char *name, int namelen, struct ucred *cred, int mode); 243655291f2SDavid Xu static int mqfs_destroy(struct mqfs_node *mn); 244655291f2SDavid Xu static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn); 245655291f2SDavid Xu static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn); 246655291f2SDavid Xu static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn); 247*adb023aeSJamie Gritton static int mqfs_prison_create(void *obj, void *data); 248*adb023aeSJamie Gritton static void mqfs_prison_destructor(void *data); 249*adb023aeSJamie Gritton static void mqfs_prison_remove_task(void *context, int pending); 250655291f2SDavid Xu 251655291f2SDavid Xu /* 252655291f2SDavid Xu * Message queue construction and maniplation 253655291f2SDavid Xu */ 254655291f2SDavid Xu static struct mqueue *mqueue_alloc(const struct mq_attr *attr); 255655291f2SDavid Xu static void mqueue_free(struct mqueue *mq); 256655291f2SDavid Xu static int mqueue_send(struct mqueue *mq, const char *msg_ptr, 257655291f2SDavid Xu size_t msg_len, unsigned msg_prio, int waitok, 258655291f2SDavid Xu const struct timespec *abs_timeout); 259655291f2SDavid Xu static int mqueue_receive(struct mqueue *mq, char *msg_ptr, 260655291f2SDavid Xu size_t msg_len, unsigned *msg_prio, int waitok, 261655291f2SDavid Xu const struct timespec *abs_timeout); 262655291f2SDavid Xu static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, 263655291f2SDavid Xu int timo); 264655291f2SDavid Xu static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, 265655291f2SDavid Xu int timo); 266655291f2SDavid Xu static void mqueue_send_notification(struct mqueue *mq); 267b2f92ef9SDavid Xu static void mqueue_fdclose(struct thread *td, int fd, struct file *fp); 268b2f92ef9SDavid Xu static void mq_proc_exit(void *arg, struct proc *p); 269655291f2SDavid Xu 270b2f92ef9SDavid Xu /* 271b2f92ef9SDavid Xu * kqueue filters 272b2f92ef9SDavid Xu */ 273655291f2SDavid Xu static void filt_mqdetach(struct knote *kn); 274655291f2SDavid Xu static int filt_mqread(struct knote *kn, long hint); 275655291f2SDavid Xu static int filt_mqwrite(struct knote *kn, long hint); 276655291f2SDavid Xu 277e76d823bSRobert Watson struct filterops mq_rfiltops = { 278e76d823bSRobert Watson .f_isfd = 1, 279e76d823bSRobert Watson .f_detach = filt_mqdetach, 280e76d823bSRobert Watson .f_event = filt_mqread, 281e76d823bSRobert Watson }; 282e76d823bSRobert Watson struct filterops mq_wfiltops = { 283e76d823bSRobert Watson .f_isfd = 1, 284e76d823bSRobert Watson .f_detach = filt_mqdetach, 285e76d823bSRobert Watson .f_event = filt_mqwrite, 286e76d823bSRobert Watson }; 287655291f2SDavid Xu 288655291f2SDavid Xu /* 289655291f2SDavid Xu * Initialize fileno bitmap 290655291f2SDavid Xu */ 291655291f2SDavid Xu static void 292655291f2SDavid Xu mqfs_fileno_init(struct mqfs_info *mi) 293655291f2SDavid Xu { 294655291f2SDavid Xu struct unrhdr *up; 295655291f2SDavid Xu 296655291f2SDavid Xu up = new_unrhdr(1, INT_MAX, NULL); 297655291f2SDavid Xu mi->mi_unrhdr = up; 298655291f2SDavid Xu } 299655291f2SDavid Xu 300655291f2SDavid Xu /* 301655291f2SDavid Xu * Tear down fileno bitmap 302655291f2SDavid Xu */ 303655291f2SDavid Xu static void 304655291f2SDavid Xu mqfs_fileno_uninit(struct mqfs_info *mi) 305655291f2SDavid Xu { 306655291f2SDavid Xu struct unrhdr *up; 307655291f2SDavid Xu 308655291f2SDavid Xu up = mi->mi_unrhdr; 309655291f2SDavid Xu mi->mi_unrhdr = NULL; 310655291f2SDavid Xu delete_unrhdr(up); 311655291f2SDavid Xu } 312655291f2SDavid Xu 313655291f2SDavid Xu /* 314655291f2SDavid Xu * Allocate a file number 315655291f2SDavid Xu */ 316b2f92ef9SDavid Xu static void 317655291f2SDavid Xu mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn) 318655291f2SDavid Xu { 319655291f2SDavid Xu /* make sure our parent has a file number */ 320655291f2SDavid Xu if (mn->mn_parent && !mn->mn_parent->mn_fileno) 321655291f2SDavid Xu mqfs_fileno_alloc(mi, mn->mn_parent); 322655291f2SDavid Xu 323655291f2SDavid Xu switch (mn->mn_type) { 324655291f2SDavid Xu case mqfstype_root: 325655291f2SDavid Xu case mqfstype_dir: 326655291f2SDavid Xu case mqfstype_file: 327655291f2SDavid Xu case mqfstype_symlink: 328655291f2SDavid Xu mn->mn_fileno = alloc_unr(mi->mi_unrhdr); 329655291f2SDavid Xu break; 330655291f2SDavid Xu case mqfstype_this: 331655291f2SDavid Xu KASSERT(mn->mn_parent != NULL, 332655291f2SDavid Xu ("mqfstype_this node has no parent")); 333655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_fileno; 334655291f2SDavid Xu break; 335655291f2SDavid Xu case mqfstype_parent: 336655291f2SDavid Xu KASSERT(mn->mn_parent != NULL, 337655291f2SDavid Xu ("mqfstype_parent node has no parent")); 338655291f2SDavid Xu if (mn->mn_parent == mi->mi_root) { 339655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_fileno; 340655291f2SDavid Xu break; 341655291f2SDavid Xu } 342655291f2SDavid Xu KASSERT(mn->mn_parent->mn_parent != NULL, 343655291f2SDavid Xu ("mqfstype_parent node has no grandparent")); 344655291f2SDavid Xu mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno; 345655291f2SDavid Xu break; 346655291f2SDavid Xu default: 347655291f2SDavid Xu KASSERT(0, 348655291f2SDavid Xu ("mqfs_fileno_alloc() called for unknown type node: %d", 349655291f2SDavid Xu mn->mn_type)); 350655291f2SDavid Xu break; 351655291f2SDavid Xu } 352655291f2SDavid Xu } 353655291f2SDavid Xu 354655291f2SDavid Xu /* 355655291f2SDavid Xu * Release a file number 356655291f2SDavid Xu */ 357b2f92ef9SDavid Xu static void 358655291f2SDavid Xu mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn) 359655291f2SDavid Xu { 360655291f2SDavid Xu switch (mn->mn_type) { 361655291f2SDavid Xu case mqfstype_root: 362655291f2SDavid Xu case mqfstype_dir: 363655291f2SDavid Xu case mqfstype_file: 364655291f2SDavid Xu case mqfstype_symlink: 365655291f2SDavid Xu free_unr(mi->mi_unrhdr, mn->mn_fileno); 366655291f2SDavid Xu break; 367655291f2SDavid Xu case mqfstype_this: 368655291f2SDavid Xu case mqfstype_parent: 369655291f2SDavid Xu /* ignore these, as they don't "own" their file number */ 370655291f2SDavid Xu break; 371655291f2SDavid Xu default: 372655291f2SDavid Xu KASSERT(0, 373655291f2SDavid Xu ("mqfs_fileno_free() called for unknown type node: %d", 374655291f2SDavid Xu mn->mn_type)); 375655291f2SDavid Xu break; 376655291f2SDavid Xu } 377655291f2SDavid Xu } 378655291f2SDavid Xu 379655291f2SDavid Xu static __inline struct mqfs_node * 380655291f2SDavid Xu mqnode_alloc(void) 381655291f2SDavid Xu { 382655291f2SDavid Xu return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO); 383655291f2SDavid Xu } 384655291f2SDavid Xu 385655291f2SDavid Xu static __inline void 386655291f2SDavid Xu mqnode_free(struct mqfs_node *node) 387655291f2SDavid Xu { 388655291f2SDavid Xu uma_zfree(mqnode_zone, node); 389655291f2SDavid Xu } 390655291f2SDavid Xu 391655291f2SDavid Xu static __inline void 392655291f2SDavid Xu mqnode_addref(struct mqfs_node *node) 393655291f2SDavid Xu { 394655291f2SDavid Xu atomic_fetchadd_int(&node->mn_refcount, 1); 395655291f2SDavid Xu } 396655291f2SDavid Xu 397655291f2SDavid Xu static __inline void 398655291f2SDavid Xu mqnode_release(struct mqfs_node *node) 399655291f2SDavid Xu { 400fbc48e97SDavid Xu struct mqfs_info *mqfs; 401655291f2SDavid Xu int old, exp; 402655291f2SDavid Xu 403fbc48e97SDavid Xu mqfs = node->mn_info; 404655291f2SDavid Xu old = atomic_fetchadd_int(&node->mn_refcount, -1); 405655291f2SDavid Xu if (node->mn_type == mqfstype_dir || 406655291f2SDavid Xu node->mn_type == mqfstype_root) 407655291f2SDavid Xu exp = 3; /* include . and .. */ 408655291f2SDavid Xu else 409655291f2SDavid Xu exp = 1; 410fbc48e97SDavid Xu if (old == exp) { 411fbc48e97SDavid Xu int locked = sx_xlocked(&mqfs->mi_lock); 412fbc48e97SDavid Xu if (!locked) 413fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 414655291f2SDavid Xu mqfs_destroy(node); 415fbc48e97SDavid Xu if (!locked) 416fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 417fbc48e97SDavid Xu } 418655291f2SDavid Xu } 419655291f2SDavid Xu 420655291f2SDavid Xu /* 421655291f2SDavid Xu * Add a node to a directory 422655291f2SDavid Xu */ 423655291f2SDavid Xu static int 424655291f2SDavid Xu mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node) 425655291f2SDavid Xu { 426655291f2SDavid Xu KASSERT(parent != NULL, ("%s(): parent is NULL", __func__)); 427655291f2SDavid Xu KASSERT(parent->mn_info != NULL, 428655291f2SDavid Xu ("%s(): parent has no mn_info", __func__)); 429655291f2SDavid Xu KASSERT(parent->mn_type == mqfstype_dir || 430655291f2SDavid Xu parent->mn_type == mqfstype_root, 431655291f2SDavid Xu ("%s(): parent is not a directory", __func__)); 432655291f2SDavid Xu 433655291f2SDavid Xu node->mn_info = parent->mn_info; 434655291f2SDavid Xu node->mn_parent = parent; 435655291f2SDavid Xu LIST_INIT(&node->mn_children); 436655291f2SDavid Xu LIST_INIT(&node->mn_vnodes); 437655291f2SDavid Xu LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling); 438655291f2SDavid Xu mqnode_addref(parent); 439655291f2SDavid Xu return (0); 440655291f2SDavid Xu } 441655291f2SDavid Xu 442ba0360b1SDavid Xu static struct mqfs_node * 443ba0360b1SDavid Xu mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode, 444ba0360b1SDavid Xu int nodetype) 445ba0360b1SDavid Xu { 446ba0360b1SDavid Xu struct mqfs_node *node; 447ba0360b1SDavid Xu 448ba0360b1SDavid Xu node = mqnode_alloc(); 449ba0360b1SDavid Xu strncpy(node->mn_name, name, namelen); 450*adb023aeSJamie Gritton node->mn_pr_root = cred->cr_prison->pr_root; 451ba0360b1SDavid Xu node->mn_type = nodetype; 452ba0360b1SDavid Xu node->mn_refcount = 1; 453fbc48e97SDavid Xu vfs_timestamp(&node->mn_birth); 454ba0360b1SDavid Xu node->mn_ctime = node->mn_atime = node->mn_mtime 455ba0360b1SDavid Xu = node->mn_birth; 456ba0360b1SDavid Xu node->mn_uid = cred->cr_uid; 457ba0360b1SDavid Xu node->mn_gid = cred->cr_gid; 458ba0360b1SDavid Xu node->mn_mode = mode; 459ba0360b1SDavid Xu return (node); 460ba0360b1SDavid Xu } 461ba0360b1SDavid Xu 462ba0360b1SDavid Xu /* 463ba0360b1SDavid Xu * Create a file 464ba0360b1SDavid Xu */ 465ba0360b1SDavid Xu static struct mqfs_node * 466ba0360b1SDavid Xu mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen, 467ba0360b1SDavid Xu struct ucred *cred, int mode) 468ba0360b1SDavid Xu { 469ba0360b1SDavid Xu struct mqfs_node *node; 470ba0360b1SDavid Xu 471ba0360b1SDavid Xu node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file); 472ba0360b1SDavid Xu if (mqfs_add_node(parent, node) != 0) { 473ba0360b1SDavid Xu mqnode_free(node); 474ba0360b1SDavid Xu return (NULL); 475ba0360b1SDavid Xu } 476ba0360b1SDavid Xu return (node); 477ba0360b1SDavid Xu } 478ba0360b1SDavid Xu 479655291f2SDavid Xu /* 480655291f2SDavid Xu * Add . and .. to a directory 481655291f2SDavid Xu */ 482655291f2SDavid Xu static int 483655291f2SDavid Xu mqfs_fixup_dir(struct mqfs_node *parent) 484655291f2SDavid Xu { 485655291f2SDavid Xu struct mqfs_node *dir; 486655291f2SDavid Xu 487655291f2SDavid Xu dir = mqnode_alloc(); 488655291f2SDavid Xu dir->mn_name[0] = '.'; 489655291f2SDavid Xu dir->mn_type = mqfstype_this; 490655291f2SDavid Xu dir->mn_refcount = 1; 491655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 492655291f2SDavid Xu mqnode_free(dir); 493655291f2SDavid Xu return (-1); 494655291f2SDavid Xu } 495655291f2SDavid Xu 496655291f2SDavid Xu dir = mqnode_alloc(); 497655291f2SDavid Xu dir->mn_name[0] = dir->mn_name[1] = '.'; 498655291f2SDavid Xu dir->mn_type = mqfstype_parent; 499655291f2SDavid Xu dir->mn_refcount = 1; 500655291f2SDavid Xu 501655291f2SDavid Xu if (mqfs_add_node(parent, dir) != 0) { 502655291f2SDavid Xu mqnode_free(dir); 503655291f2SDavid Xu return (-1); 504655291f2SDavid Xu } 505655291f2SDavid Xu 506655291f2SDavid Xu return (0); 507655291f2SDavid Xu } 508655291f2SDavid Xu 509655291f2SDavid Xu #ifdef notyet 510655291f2SDavid Xu 511655291f2SDavid Xu /* 512655291f2SDavid Xu * Create a directory 513655291f2SDavid Xu */ 514b2f92ef9SDavid Xu static struct mqfs_node * 515ba0360b1SDavid Xu mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen, 516ba0360b1SDavid Xu struct ucred *cred, int mode) 517655291f2SDavid Xu { 518ba0360b1SDavid Xu struct mqfs_node *node; 519655291f2SDavid Xu 520ba0360b1SDavid Xu node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir); 521ba0360b1SDavid Xu if (mqfs_add_node(parent, node) != 0) { 522ba0360b1SDavid Xu mqnode_free(node); 523655291f2SDavid Xu return (NULL); 524655291f2SDavid Xu } 525655291f2SDavid Xu 526ba0360b1SDavid Xu if (mqfs_fixup_dir(node) != 0) { 527ba0360b1SDavid Xu mqfs_destroy(node); 528655291f2SDavid Xu return (NULL); 529655291f2SDavid Xu } 530ba0360b1SDavid Xu return (node); 531655291f2SDavid Xu } 532b2f92ef9SDavid Xu 533b2f92ef9SDavid Xu /* 534b2f92ef9SDavid Xu * Create a symlink 535b2f92ef9SDavid Xu */ 536b2f92ef9SDavid Xu static struct mqfs_node * 537ba0360b1SDavid Xu mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen, 538ba0360b1SDavid Xu struct ucred *cred, int mode) 539b2f92ef9SDavid Xu { 540b2f92ef9SDavid Xu struct mqfs_node *node; 541b2f92ef9SDavid Xu 542ba0360b1SDavid Xu node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink); 543655291f2SDavid Xu if (mqfs_add_node(parent, node) != 0) { 544655291f2SDavid Xu mqnode_free(node); 545655291f2SDavid Xu return (NULL); 546655291f2SDavid Xu } 547655291f2SDavid Xu return (node); 548655291f2SDavid Xu } 549655291f2SDavid Xu 550ba0360b1SDavid Xu #endif 551ba0360b1SDavid Xu 552655291f2SDavid Xu /* 553655291f2SDavid Xu * Destroy a node or a tree of nodes 554655291f2SDavid Xu */ 555b2f92ef9SDavid Xu static int 556655291f2SDavid Xu mqfs_destroy(struct mqfs_node *node) 557655291f2SDavid Xu { 558655291f2SDavid Xu struct mqfs_node *parent; 559655291f2SDavid Xu 560655291f2SDavid Xu KASSERT(node != NULL, 561655291f2SDavid Xu ("%s(): node is NULL", __func__)); 562655291f2SDavid Xu KASSERT(node->mn_info != NULL, 563655291f2SDavid Xu ("%s(): node has no mn_info", __func__)); 564655291f2SDavid Xu 565655291f2SDavid Xu /* destroy children */ 566655291f2SDavid Xu if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root) 567655291f2SDavid Xu while (! LIST_EMPTY(&node->mn_children)) 568655291f2SDavid Xu mqfs_destroy(LIST_FIRST(&node->mn_children)); 569655291f2SDavid Xu 570655291f2SDavid Xu /* unlink from parent */ 571655291f2SDavid Xu if ((parent = node->mn_parent) != NULL) { 572655291f2SDavid Xu KASSERT(parent->mn_info == node->mn_info, 573655291f2SDavid Xu ("%s(): parent has different mn_info", __func__)); 574655291f2SDavid Xu LIST_REMOVE(node, mn_sibling); 575655291f2SDavid Xu } 576655291f2SDavid Xu 577655291f2SDavid Xu if (node->mn_fileno != 0) 578655291f2SDavid Xu mqfs_fileno_free(node->mn_info, node); 579655291f2SDavid Xu if (node->mn_data != NULL) 580655291f2SDavid Xu mqueue_free(node->mn_data); 581655291f2SDavid Xu mqnode_free(node); 582655291f2SDavid Xu return (0); 583655291f2SDavid Xu } 584655291f2SDavid Xu 585655291f2SDavid Xu /* 586655291f2SDavid Xu * Mount a mqfs instance 587655291f2SDavid Xu */ 588655291f2SDavid Xu static int 589dfd233edSAttilio Rao mqfs_mount(struct mount *mp) 590655291f2SDavid Xu { 591655291f2SDavid Xu struct statfs *sbp; 592655291f2SDavid Xu 593655291f2SDavid Xu if (mp->mnt_flag & MNT_UPDATE) 594655291f2SDavid Xu return (EOPNOTSUPP); 595655291f2SDavid Xu 596655291f2SDavid Xu mp->mnt_data = &mqfs_data; 5975da56ddbSTor Egge MNT_ILOCK(mp); 598655291f2SDavid Xu mp->mnt_flag |= MNT_LOCAL; 5995da56ddbSTor Egge MNT_IUNLOCK(mp); 600655291f2SDavid Xu vfs_getnewfsid(mp); 601655291f2SDavid Xu 602655291f2SDavid Xu sbp = &mp->mnt_stat; 603655291f2SDavid Xu vfs_mountedfrom(mp, "mqueue"); 604655291f2SDavid Xu sbp->f_bsize = PAGE_SIZE; 605655291f2SDavid Xu sbp->f_iosize = PAGE_SIZE; 606655291f2SDavid Xu sbp->f_blocks = 1; 607655291f2SDavid Xu sbp->f_bfree = 0; 608655291f2SDavid Xu sbp->f_bavail = 0; 609655291f2SDavid Xu sbp->f_files = 1; 610655291f2SDavid Xu sbp->f_ffree = 0; 611655291f2SDavid Xu return (0); 612655291f2SDavid Xu } 613655291f2SDavid Xu 614655291f2SDavid Xu /* 615655291f2SDavid Xu * Unmount a mqfs instance 616655291f2SDavid Xu */ 617655291f2SDavid Xu static int 618dfd233edSAttilio Rao mqfs_unmount(struct mount *mp, int mntflags) 619655291f2SDavid Xu { 620655291f2SDavid Xu int error; 621655291f2SDavid Xu 622dfd233edSAttilio Rao error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, 623dfd233edSAttilio Rao curthread); 624655291f2SDavid Xu return (error); 625655291f2SDavid Xu } 626655291f2SDavid Xu 627655291f2SDavid Xu /* 628655291f2SDavid Xu * Return a root vnode 629655291f2SDavid Xu */ 630655291f2SDavid Xu static int 631dfd233edSAttilio Rao mqfs_root(struct mount *mp, int flags, struct vnode **vpp) 632655291f2SDavid Xu { 633655291f2SDavid Xu struct mqfs_info *mqfs; 634655291f2SDavid Xu int ret; 635655291f2SDavid Xu 636655291f2SDavid Xu mqfs = VFSTOMQFS(mp); 637655291f2SDavid Xu ret = mqfs_allocv(mp, vpp, mqfs->mi_root); 638655291f2SDavid Xu return (ret); 639655291f2SDavid Xu } 640655291f2SDavid Xu 641655291f2SDavid Xu /* 642655291f2SDavid Xu * Return filesystem stats 643655291f2SDavid Xu */ 644655291f2SDavid Xu static int 645dfd233edSAttilio Rao mqfs_statfs(struct mount *mp, struct statfs *sbp) 646655291f2SDavid Xu { 647655291f2SDavid Xu /* XXX update statistics */ 648655291f2SDavid Xu return (0); 649655291f2SDavid Xu } 650655291f2SDavid Xu 651655291f2SDavid Xu /* 652655291f2SDavid Xu * Initialize a mqfs instance 653655291f2SDavid Xu */ 654655291f2SDavid Xu static int 655655291f2SDavid Xu mqfs_init(struct vfsconf *vfc) 656655291f2SDavid Xu { 657655291f2SDavid Xu struct mqfs_node *root; 658655291f2SDavid Xu struct mqfs_info *mi; 659*adb023aeSJamie Gritton struct prison *pr; 660*adb023aeSJamie Gritton osd_method_t methods[PR_MAXMETHOD] = { 661*adb023aeSJamie Gritton [PR_METHOD_CREATE] = mqfs_prison_create, 662*adb023aeSJamie Gritton }; 663655291f2SDavid Xu 664655291f2SDavid Xu mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node), 665655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 666655291f2SDavid Xu mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue), 667655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 668655291f2SDavid Xu mvdata_zone = uma_zcreate("mvdata", 669655291f2SDavid Xu sizeof(struct mqfs_vdata), NULL, NULL, NULL, 670655291f2SDavid Xu NULL, UMA_ALIGN_PTR, 0); 671b2f92ef9SDavid Xu mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier), 672655291f2SDavid Xu NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 673655291f2SDavid Xu mi = &mqfs_data; 674655291f2SDavid Xu sx_init(&mi->mi_lock, "mqfs lock"); 675655291f2SDavid Xu /* set up the root diretory */ 676ba0360b1SDavid Xu root = mqfs_create_node("/", 1, curthread->td_ucred, 01777, 677ba0360b1SDavid Xu mqfstype_root); 678655291f2SDavid Xu root->mn_info = mi; 679655291f2SDavid Xu LIST_INIT(&root->mn_children); 680655291f2SDavid Xu LIST_INIT(&root->mn_vnodes); 681655291f2SDavid Xu mi->mi_root = root; 682655291f2SDavid Xu mqfs_fileno_init(mi); 683655291f2SDavid Xu mqfs_fileno_alloc(mi, root); 684655291f2SDavid Xu mqfs_fixup_dir(root); 685b2f92ef9SDavid Xu exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL, 686b2f92ef9SDavid Xu EVENTHANDLER_PRI_ANY); 687b2f92ef9SDavid Xu mq_fdclose = mqueue_fdclose; 6885ee2d4acSDavid Xu p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING); 689*adb023aeSJamie Gritton /* Note current jails */ 690*adb023aeSJamie Gritton mqfs_osd_jail_slot = osd_jail_register(mqfs_prison_destructor, methods); 691*adb023aeSJamie Gritton sx_slock(&allprison_lock); 692*adb023aeSJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 693*adb023aeSJamie Gritton (void)mqfs_prison_create(pr, NULL); 694*adb023aeSJamie Gritton sx_sunlock(&allprison_lock); 695655291f2SDavid Xu return (0); 696655291f2SDavid Xu } 697655291f2SDavid Xu 698655291f2SDavid Xu /* 699655291f2SDavid Xu * Destroy a mqfs instance 700655291f2SDavid Xu */ 701655291f2SDavid Xu static int 702655291f2SDavid Xu mqfs_uninit(struct vfsconf *vfc) 703655291f2SDavid Xu { 704*adb023aeSJamie Gritton unsigned slot; 705655291f2SDavid Xu struct mqfs_info *mi; 706655291f2SDavid Xu 707655291f2SDavid Xu if (!unloadable) 708655291f2SDavid Xu return (EOPNOTSUPP); 709*adb023aeSJamie Gritton slot = mqfs_osd_jail_slot; 710*adb023aeSJamie Gritton mqfs_osd_jail_slot = 0; 711*adb023aeSJamie Gritton osd_jail_deregister(slot); 712b2f92ef9SDavid Xu EVENTHANDLER_DEREGISTER(process_exit, exit_tag); 713655291f2SDavid Xu mi = &mqfs_data; 714655291f2SDavid Xu mqfs_destroy(mi->mi_root); 715655291f2SDavid Xu mi->mi_root = NULL; 716655291f2SDavid Xu mqfs_fileno_uninit(mi); 717655291f2SDavid Xu sx_destroy(&mi->mi_lock); 718b2f92ef9SDavid Xu uma_zdestroy(mqnode_zone); 719b2f92ef9SDavid Xu uma_zdestroy(mqueue_zone); 720b2f92ef9SDavid Xu uma_zdestroy(mvdata_zone); 721b2f92ef9SDavid Xu uma_zdestroy(mqnoti_zone); 722655291f2SDavid Xu return (0); 723655291f2SDavid Xu } 724655291f2SDavid Xu 725655291f2SDavid Xu /* 726655291f2SDavid Xu * task routine 727655291f2SDavid Xu */ 728655291f2SDavid Xu static void 729655291f2SDavid Xu do_recycle(void *context, int pending __unused) 730655291f2SDavid Xu { 731655291f2SDavid Xu struct vnode *vp = (struct vnode *)context; 732655291f2SDavid Xu 733af6e6b87SEdward Tomasz Napierala vrecycle(vp); 734655291f2SDavid Xu vdrop(vp); 735655291f2SDavid Xu } 736655291f2SDavid Xu 737655291f2SDavid Xu /* 738655291f2SDavid Xu * Allocate a vnode 739655291f2SDavid Xu */ 740b2f92ef9SDavid Xu static int 741655291f2SDavid Xu mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn) 742655291f2SDavid Xu { 743655291f2SDavid Xu struct mqfs_vdata *vd; 744fbc48e97SDavid Xu struct mqfs_info *mqfs; 745fbc48e97SDavid Xu struct vnode *newvpp; 746655291f2SDavid Xu int error; 747655291f2SDavid Xu 748fbc48e97SDavid Xu mqfs = pn->mn_info; 749fbc48e97SDavid Xu *vpp = NULL; 750fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 751655291f2SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 752fbc48e97SDavid Xu if (vd->mv_vnode->v_mount == mp) { 753fbc48e97SDavid Xu vhold(vd->mv_vnode); 754655291f2SDavid Xu break; 755655291f2SDavid Xu } 756fbc48e97SDavid Xu } 757655291f2SDavid Xu 758655291f2SDavid Xu if (vd != NULL) { 759fbc48e97SDavid Xu found: 760655291f2SDavid Xu *vpp = vd->mv_vnode; 761fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 762fbc48e97SDavid Xu error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread); 763fbc48e97SDavid Xu vdrop(*vpp); 764fbc48e97SDavid Xu return (error); 765655291f2SDavid Xu } 766fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 767655291f2SDavid Xu 768fbc48e97SDavid Xu error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp); 769655291f2SDavid Xu if (error) 770655291f2SDavid Xu return (error); 771fbc48e97SDavid Xu vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY); 772fbc48e97SDavid Xu error = insmntque(newvpp, mp); 773fbc48e97SDavid Xu if (error != 0) 77461b9d89fSTor Egge return (error); 775fbc48e97SDavid Xu 776fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 777fbc48e97SDavid Xu /* 778fbc48e97SDavid Xu * Check if it has already been allocated 779fbc48e97SDavid Xu * while we were blocked. 780fbc48e97SDavid Xu */ 781fbc48e97SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 782fbc48e97SDavid Xu if (vd->mv_vnode->v_mount == mp) { 783fbc48e97SDavid Xu vhold(vd->mv_vnode); 784fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 785fbc48e97SDavid Xu 786fbc48e97SDavid Xu vgone(newvpp); 787fbc48e97SDavid Xu vput(newvpp); 788fbc48e97SDavid Xu goto found; 78961b9d89fSTor Egge } 790fbc48e97SDavid Xu } 791fbc48e97SDavid Xu 792fbc48e97SDavid Xu *vpp = newvpp; 793fbc48e97SDavid Xu 794655291f2SDavid Xu vd = uma_zalloc(mvdata_zone, M_WAITOK); 795655291f2SDavid Xu (*vpp)->v_data = vd; 796655291f2SDavid Xu vd->mv_vnode = *vpp; 797655291f2SDavid Xu vd->mv_node = pn; 798655291f2SDavid Xu TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp); 799655291f2SDavid Xu LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link); 800655291f2SDavid Xu mqnode_addref(pn); 801655291f2SDavid Xu switch (pn->mn_type) { 802655291f2SDavid Xu case mqfstype_root: 803655291f2SDavid Xu (*vpp)->v_vflag = VV_ROOT; 804655291f2SDavid Xu /* fall through */ 805655291f2SDavid Xu case mqfstype_dir: 806655291f2SDavid Xu case mqfstype_this: 807655291f2SDavid Xu case mqfstype_parent: 808655291f2SDavid Xu (*vpp)->v_type = VDIR; 809655291f2SDavid Xu break; 810655291f2SDavid Xu case mqfstype_file: 811655291f2SDavid Xu (*vpp)->v_type = VREG; 812655291f2SDavid Xu break; 813655291f2SDavid Xu case mqfstype_symlink: 814655291f2SDavid Xu (*vpp)->v_type = VLNK; 815655291f2SDavid Xu break; 816655291f2SDavid Xu case mqfstype_none: 817655291f2SDavid Xu KASSERT(0, ("mqfs_allocf called for null node\n")); 818655291f2SDavid Xu default: 819655291f2SDavid Xu panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type); 820655291f2SDavid Xu } 821fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 822655291f2SDavid Xu return (0); 823655291f2SDavid Xu } 824655291f2SDavid Xu 825655291f2SDavid Xu /* 826655291f2SDavid Xu * Search a directory entry 827655291f2SDavid Xu */ 828655291f2SDavid Xu static struct mqfs_node * 829*adb023aeSJamie Gritton mqfs_search(struct mqfs_node *pd, const char *name, int len, struct ucred *cred) 830655291f2SDavid Xu { 831655291f2SDavid Xu struct mqfs_node *pn; 832*adb023aeSJamie Gritton const void *pr_root; 833655291f2SDavid Xu 834fbc48e97SDavid Xu sx_assert(&pd->mn_info->mi_lock, SX_LOCKED); 835*adb023aeSJamie Gritton pr_root = cred->cr_prison->pr_root; 836655291f2SDavid Xu LIST_FOREACH(pn, &pd->mn_children, mn_sibling) { 837*adb023aeSJamie Gritton /* Only match names within the same prison root directory */ 838*adb023aeSJamie Gritton if ((pn->mn_pr_root == NULL || pn->mn_pr_root == pr_root) && 839*adb023aeSJamie Gritton strncmp(pn->mn_name, name, len) == 0 && 8401cbae705SEd Schouten pn->mn_name[len] == '\0') 841655291f2SDavid Xu return (pn); 842655291f2SDavid Xu } 843655291f2SDavid Xu return (NULL); 844655291f2SDavid Xu } 845655291f2SDavid Xu 846655291f2SDavid Xu /* 847873fbcd7SRobert Watson * Look up a file or directory. 848655291f2SDavid Xu */ 849655291f2SDavid Xu static int 850655291f2SDavid Xu mqfs_lookupx(struct vop_cachedlookup_args *ap) 851655291f2SDavid Xu { 852655291f2SDavid Xu struct componentname *cnp; 853655291f2SDavid Xu struct vnode *dvp, **vpp; 854655291f2SDavid Xu struct mqfs_node *pd; 855655291f2SDavid Xu struct mqfs_node *pn; 856fbc48e97SDavid Xu struct mqfs_info *mqfs; 857655291f2SDavid Xu int nameiop, flags, error, namelen; 858655291f2SDavid Xu char *pname; 859655291f2SDavid Xu struct thread *td; 860655291f2SDavid Xu 861655291f2SDavid Xu cnp = ap->a_cnp; 862655291f2SDavid Xu vpp = ap->a_vpp; 863655291f2SDavid Xu dvp = ap->a_dvp; 864655291f2SDavid Xu pname = cnp->cn_nameptr; 865655291f2SDavid Xu namelen = cnp->cn_namelen; 866655291f2SDavid Xu td = cnp->cn_thread; 867655291f2SDavid Xu flags = cnp->cn_flags; 868655291f2SDavid Xu nameiop = cnp->cn_nameiop; 869655291f2SDavid Xu pd = VTON(dvp); 870655291f2SDavid Xu pn = NULL; 871fbc48e97SDavid Xu mqfs = pd->mn_info; 872655291f2SDavid Xu *vpp = NULLVP; 873655291f2SDavid Xu 874655291f2SDavid Xu if (dvp->v_type != VDIR) 875655291f2SDavid Xu return (ENOTDIR); 876655291f2SDavid Xu 877655291f2SDavid Xu error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread); 878655291f2SDavid Xu if (error) 879655291f2SDavid Xu return (error); 880655291f2SDavid Xu 881655291f2SDavid Xu /* shortcut: check if the name is too long */ 882655291f2SDavid Xu if (cnp->cn_namelen >= MQFS_NAMELEN) 883655291f2SDavid Xu return (ENOENT); 884655291f2SDavid Xu 885655291f2SDavid Xu /* self */ 886655291f2SDavid Xu if (namelen == 1 && pname[0] == '.') { 887655291f2SDavid Xu if ((flags & ISLASTCN) && nameiop != LOOKUP) 888655291f2SDavid Xu return (EINVAL); 889655291f2SDavid Xu pn = pd; 890655291f2SDavid Xu *vpp = dvp; 891655291f2SDavid Xu VREF(dvp); 892655291f2SDavid Xu return (0); 893655291f2SDavid Xu } 894655291f2SDavid Xu 895655291f2SDavid Xu /* parent */ 896655291f2SDavid Xu if (cnp->cn_flags & ISDOTDOT) { 897655291f2SDavid Xu if (dvp->v_vflag & VV_ROOT) 898655291f2SDavid Xu return (EIO); 899655291f2SDavid Xu if ((flags & ISLASTCN) && nameiop != LOOKUP) 900655291f2SDavid Xu return (EINVAL); 90122db15c0SAttilio Rao VOP_UNLOCK(dvp, 0); 902655291f2SDavid Xu KASSERT(pd->mn_parent, ("non-root directory has no parent")); 903655291f2SDavid Xu pn = pd->mn_parent; 904655291f2SDavid Xu error = mqfs_allocv(dvp->v_mount, vpp, pn); 905cb05b60aSAttilio Rao vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 906655291f2SDavid Xu return (error); 907655291f2SDavid Xu } 908655291f2SDavid Xu 909655291f2SDavid Xu /* named node */ 910fbc48e97SDavid Xu sx_xlock(&mqfs->mi_lock); 911*adb023aeSJamie Gritton pn = mqfs_search(pd, pname, namelen, cnp->cn_cred); 912fbc48e97SDavid Xu if (pn != NULL) 913fbc48e97SDavid Xu mqnode_addref(pn); 914fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 915655291f2SDavid Xu 916655291f2SDavid Xu /* found */ 917655291f2SDavid Xu if (pn != NULL) { 918655291f2SDavid Xu /* DELETE */ 919655291f2SDavid Xu if (nameiop == DELETE && (flags & ISLASTCN)) { 920655291f2SDavid Xu error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 921fbc48e97SDavid Xu if (error) { 922fbc48e97SDavid Xu mqnode_release(pn); 923655291f2SDavid Xu return (error); 924fbc48e97SDavid Xu } 925655291f2SDavid Xu if (*vpp == dvp) { 926655291f2SDavid Xu VREF(dvp); 927655291f2SDavid Xu *vpp = dvp; 928fbc48e97SDavid Xu mqnode_release(pn); 929655291f2SDavid Xu return (0); 930655291f2SDavid Xu } 931655291f2SDavid Xu } 932655291f2SDavid Xu 933655291f2SDavid Xu /* allocate vnode */ 934655291f2SDavid Xu error = mqfs_allocv(dvp->v_mount, vpp, pn); 935fbc48e97SDavid Xu mqnode_release(pn); 936655291f2SDavid Xu if (error == 0 && cnp->cn_flags & MAKEENTRY) 937655291f2SDavid Xu cache_enter(dvp, *vpp, cnp); 938655291f2SDavid Xu return (error); 939655291f2SDavid Xu } 940655291f2SDavid Xu 941655291f2SDavid Xu /* not found */ 942655291f2SDavid Xu 943655291f2SDavid Xu /* will create a new entry in the directory ? */ 944655291f2SDavid Xu if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT) 945655291f2SDavid Xu && (flags & ISLASTCN)) { 946655291f2SDavid Xu error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 947655291f2SDavid Xu if (error) 948655291f2SDavid Xu return (error); 949655291f2SDavid Xu cnp->cn_flags |= SAVENAME; 950655291f2SDavid Xu return (EJUSTRETURN); 951655291f2SDavid Xu } 952655291f2SDavid Xu return (ENOENT); 953655291f2SDavid Xu } 954655291f2SDavid Xu 955655291f2SDavid Xu #if 0 956655291f2SDavid Xu struct vop_lookup_args { 957655291f2SDavid Xu struct vop_generic_args a_gen; 958655291f2SDavid Xu struct vnode *a_dvp; 959655291f2SDavid Xu struct vnode **a_vpp; 960655291f2SDavid Xu struct componentname *a_cnp; 961655291f2SDavid Xu }; 962655291f2SDavid Xu #endif 963655291f2SDavid Xu 964655291f2SDavid Xu /* 965655291f2SDavid Xu * vnode lookup operation 966655291f2SDavid Xu */ 967655291f2SDavid Xu static int 968655291f2SDavid Xu mqfs_lookup(struct vop_cachedlookup_args *ap) 969655291f2SDavid Xu { 970655291f2SDavid Xu int rc; 971655291f2SDavid Xu 972655291f2SDavid Xu rc = mqfs_lookupx(ap); 973655291f2SDavid Xu return (rc); 974655291f2SDavid Xu } 975655291f2SDavid Xu 976655291f2SDavid Xu #if 0 977655291f2SDavid Xu struct vop_create_args { 978655291f2SDavid Xu struct vnode *a_dvp; 979655291f2SDavid Xu struct vnode **a_vpp; 980655291f2SDavid Xu struct componentname *a_cnp; 981655291f2SDavid Xu struct vattr *a_vap; 982655291f2SDavid Xu }; 983655291f2SDavid Xu #endif 984655291f2SDavid Xu 985655291f2SDavid Xu /* 986655291f2SDavid Xu * vnode creation operation 987655291f2SDavid Xu */ 988655291f2SDavid Xu static int 989655291f2SDavid Xu mqfs_create(struct vop_create_args *ap) 990655291f2SDavid Xu { 991655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 992655291f2SDavid Xu struct componentname *cnp = ap->a_cnp; 993655291f2SDavid Xu struct mqfs_node *pd; 994655291f2SDavid Xu struct mqfs_node *pn; 995655291f2SDavid Xu struct mqueue *mq; 996655291f2SDavid Xu int error; 997655291f2SDavid Xu 998655291f2SDavid Xu pd = VTON(ap->a_dvp); 999655291f2SDavid Xu if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir) 1000655291f2SDavid Xu return (ENOTDIR); 1001655291f2SDavid Xu mq = mqueue_alloc(NULL); 1002655291f2SDavid Xu if (mq == NULL) 1003655291f2SDavid Xu return (EAGAIN); 1004655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1005655291f2SDavid Xu if ((cnp->cn_flags & HASBUF) == 0) 1006655291f2SDavid Xu panic("%s: no name", __func__); 1007ba0360b1SDavid Xu pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen, 1008ba0360b1SDavid Xu cnp->cn_cred, ap->a_vap->va_mode); 1009fbc48e97SDavid Xu if (pn == NULL) { 1010fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 1011ba0360b1SDavid Xu error = ENOSPC; 1012fbc48e97SDavid Xu } else { 1013fbc48e97SDavid Xu mqnode_addref(pn); 1014fbc48e97SDavid Xu sx_xunlock(&mqfs->mi_lock); 1015655291f2SDavid Xu error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn); 1016fbc48e97SDavid Xu mqnode_release(pn); 1017ba0360b1SDavid Xu if (error) 1018ba0360b1SDavid Xu mqfs_destroy(pn); 1019ba0360b1SDavid Xu else 1020ba0360b1SDavid Xu pn->mn_data = mq; 1021ba0360b1SDavid Xu } 1022ba0360b1SDavid Xu if (error) 1023ba0360b1SDavid Xu mqueue_free(mq); 1024655291f2SDavid Xu return (error); 1025655291f2SDavid Xu } 1026655291f2SDavid Xu 1027655291f2SDavid Xu /* 1028655291f2SDavid Xu * Remove an entry 1029655291f2SDavid Xu */ 1030655291f2SDavid Xu static 1031655291f2SDavid Xu int do_unlink(struct mqfs_node *pn, struct ucred *ucred) 1032655291f2SDavid Xu { 1033655291f2SDavid Xu struct mqfs_node *parent; 1034655291f2SDavid Xu struct mqfs_vdata *vd; 1035655291f2SDavid Xu int error = 0; 1036655291f2SDavid Xu 1037655291f2SDavid Xu sx_assert(&pn->mn_info->mi_lock, SX_LOCKED); 1038655291f2SDavid Xu 1039655291f2SDavid Xu if (ucred->cr_uid != pn->mn_uid && 104032f9753cSRobert Watson (error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0) 1041655291f2SDavid Xu error = EACCES; 1042655291f2SDavid Xu else if (!pn->mn_deleted) { 1043655291f2SDavid Xu parent = pn->mn_parent; 1044655291f2SDavid Xu pn->mn_parent = NULL; 1045655291f2SDavid Xu pn->mn_deleted = 1; 1046655291f2SDavid Xu LIST_REMOVE(pn, mn_sibling); 1047655291f2SDavid Xu LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) { 1048655291f2SDavid Xu cache_purge(vd->mv_vnode); 1049655291f2SDavid Xu vhold(vd->mv_vnode); 1050655291f2SDavid Xu taskqueue_enqueue(taskqueue_thread, &vd->mv_task); 1051655291f2SDavid Xu } 1052655291f2SDavid Xu mqnode_release(pn); 1053655291f2SDavid Xu mqnode_release(parent); 1054655291f2SDavid Xu } else 1055655291f2SDavid Xu error = ENOENT; 1056655291f2SDavid Xu return (error); 1057655291f2SDavid Xu } 1058655291f2SDavid Xu 1059655291f2SDavid Xu #if 0 1060655291f2SDavid Xu struct vop_remove_args { 1061655291f2SDavid Xu struct vnode *a_dvp; 1062655291f2SDavid Xu struct vnode *a_vp; 1063655291f2SDavid Xu struct componentname *a_cnp; 1064655291f2SDavid Xu }; 1065655291f2SDavid Xu #endif 1066655291f2SDavid Xu 1067655291f2SDavid Xu /* 1068655291f2SDavid Xu * vnode removal operation 1069655291f2SDavid Xu */ 1070655291f2SDavid Xu static int 1071655291f2SDavid Xu mqfs_remove(struct vop_remove_args *ap) 1072655291f2SDavid Xu { 1073655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1074655291f2SDavid Xu struct mqfs_node *pn; 1075655291f2SDavid Xu int error; 1076655291f2SDavid Xu 1077655291f2SDavid Xu if (ap->a_vp->v_type == VDIR) 1078655291f2SDavid Xu return (EPERM); 1079655291f2SDavid Xu pn = VTON(ap->a_vp); 1080655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1081655291f2SDavid Xu error = do_unlink(pn, ap->a_cnp->cn_cred); 1082655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1083655291f2SDavid Xu return (error); 1084655291f2SDavid Xu } 1085655291f2SDavid Xu 1086655291f2SDavid Xu #if 0 1087655291f2SDavid Xu struct vop_inactive_args { 1088655291f2SDavid Xu struct vnode *a_vp; 1089655291f2SDavid Xu struct thread *a_td; 1090655291f2SDavid Xu }; 1091655291f2SDavid Xu #endif 1092655291f2SDavid Xu 1093655291f2SDavid Xu static int 1094655291f2SDavid Xu mqfs_inactive(struct vop_inactive_args *ap) 1095655291f2SDavid Xu { 1096655291f2SDavid Xu struct mqfs_node *pn = VTON(ap->a_vp); 1097655291f2SDavid Xu 1098655291f2SDavid Xu if (pn->mn_deleted) 1099af6e6b87SEdward Tomasz Napierala vrecycle(ap->a_vp); 1100655291f2SDavid Xu return (0); 1101655291f2SDavid Xu } 1102655291f2SDavid Xu 1103655291f2SDavid Xu #if 0 1104655291f2SDavid Xu struct vop_reclaim_args { 1105655291f2SDavid Xu struct vop_generic_args a_gen; 1106655291f2SDavid Xu struct vnode *a_vp; 1107655291f2SDavid Xu struct thread *a_td; 1108655291f2SDavid Xu }; 1109655291f2SDavid Xu #endif 1110655291f2SDavid Xu 1111655291f2SDavid Xu static int 1112655291f2SDavid Xu mqfs_reclaim(struct vop_reclaim_args *ap) 1113655291f2SDavid Xu { 1114655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount); 1115655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1116655291f2SDavid Xu struct mqfs_node *pn; 1117655291f2SDavid Xu struct mqfs_vdata *vd; 1118655291f2SDavid Xu 1119655291f2SDavid Xu vd = vp->v_data; 1120655291f2SDavid Xu pn = vd->mv_node; 1121655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1122655291f2SDavid Xu vp->v_data = NULL; 1123655291f2SDavid Xu LIST_REMOVE(vd, mv_link); 1124655291f2SDavid Xu uma_zfree(mvdata_zone, vd); 1125655291f2SDavid Xu mqnode_release(pn); 1126655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1127655291f2SDavid Xu return (0); 1128655291f2SDavid Xu } 1129655291f2SDavid Xu 1130655291f2SDavid Xu #if 0 1131655291f2SDavid Xu struct vop_open_args { 1132655291f2SDavid Xu struct vop_generic_args a_gen; 1133655291f2SDavid Xu struct vnode *a_vp; 1134655291f2SDavid Xu int a_mode; 1135655291f2SDavid Xu struct ucred *a_cred; 1136655291f2SDavid Xu struct thread *a_td; 1137d6da6408SKonstantin Belousov struct file *a_fp; 1138655291f2SDavid Xu }; 1139655291f2SDavid Xu #endif 1140655291f2SDavid Xu 1141655291f2SDavid Xu static int 1142655291f2SDavid Xu mqfs_open(struct vop_open_args *ap) 1143655291f2SDavid Xu { 1144655291f2SDavid Xu return (0); 1145655291f2SDavid Xu } 1146655291f2SDavid Xu 1147655291f2SDavid Xu #if 0 1148655291f2SDavid Xu struct vop_close_args { 1149655291f2SDavid Xu struct vop_generic_args a_gen; 1150655291f2SDavid Xu struct vnode *a_vp; 1151655291f2SDavid Xu int a_fflag; 1152655291f2SDavid Xu struct ucred *a_cred; 1153655291f2SDavid Xu struct thread *a_td; 1154655291f2SDavid Xu }; 1155655291f2SDavid Xu #endif 1156655291f2SDavid Xu 1157655291f2SDavid Xu static int 1158655291f2SDavid Xu mqfs_close(struct vop_close_args *ap) 1159655291f2SDavid Xu { 1160655291f2SDavid Xu return (0); 1161655291f2SDavid Xu } 1162655291f2SDavid Xu 1163655291f2SDavid Xu #if 0 1164655291f2SDavid Xu struct vop_access_args { 1165655291f2SDavid Xu struct vop_generic_args a_gen; 1166655291f2SDavid Xu struct vnode *a_vp; 116715bc6b2bSEdward Tomasz Napierala accmode_t a_accmode; 1168655291f2SDavid Xu struct ucred *a_cred; 1169655291f2SDavid Xu struct thread *a_td; 1170655291f2SDavid Xu }; 1171655291f2SDavid Xu #endif 1172655291f2SDavid Xu 1173655291f2SDavid Xu /* 1174655291f2SDavid Xu * Verify permissions 1175655291f2SDavid Xu */ 1176655291f2SDavid Xu static int 1177655291f2SDavid Xu mqfs_access(struct vop_access_args *ap) 1178655291f2SDavid Xu { 1179655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1180655291f2SDavid Xu struct vattr vattr; 1181655291f2SDavid Xu int error; 1182655291f2SDavid Xu 11830359a12eSAttilio Rao error = VOP_GETATTR(vp, &vattr, ap->a_cred); 1184655291f2SDavid Xu if (error) 1185655291f2SDavid Xu return (error); 1186655291f2SDavid Xu error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid, 118715bc6b2bSEdward Tomasz Napierala vattr.va_gid, ap->a_accmode, ap->a_cred, NULL); 1188655291f2SDavid Xu return (error); 1189655291f2SDavid Xu } 1190655291f2SDavid Xu 1191655291f2SDavid Xu #if 0 1192655291f2SDavid Xu struct vop_getattr_args { 1193655291f2SDavid Xu struct vop_generic_args a_gen; 1194655291f2SDavid Xu struct vnode *a_vp; 1195655291f2SDavid Xu struct vattr *a_vap; 1196655291f2SDavid Xu struct ucred *a_cred; 1197655291f2SDavid Xu }; 1198655291f2SDavid Xu #endif 1199655291f2SDavid Xu 1200655291f2SDavid Xu /* 1201655291f2SDavid Xu * Get file attributes 1202655291f2SDavid Xu */ 1203655291f2SDavid Xu static int 1204655291f2SDavid Xu mqfs_getattr(struct vop_getattr_args *ap) 1205655291f2SDavid Xu { 1206655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1207655291f2SDavid Xu struct mqfs_node *pn = VTON(vp); 1208655291f2SDavid Xu struct vattr *vap = ap->a_vap; 1209655291f2SDavid Xu int error = 0; 1210655291f2SDavid Xu 1211655291f2SDavid Xu vap->va_type = vp->v_type; 1212655291f2SDavid Xu vap->va_mode = pn->mn_mode; 1213655291f2SDavid Xu vap->va_nlink = 1; 1214655291f2SDavid Xu vap->va_uid = pn->mn_uid; 1215655291f2SDavid Xu vap->va_gid = pn->mn_gid; 1216655291f2SDavid Xu vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1217655291f2SDavid Xu vap->va_fileid = pn->mn_fileno; 1218655291f2SDavid Xu vap->va_size = 0; 1219655291f2SDavid Xu vap->va_blocksize = PAGE_SIZE; 1220655291f2SDavid Xu vap->va_bytes = vap->va_size = 0; 1221655291f2SDavid Xu vap->va_atime = pn->mn_atime; 1222655291f2SDavid Xu vap->va_mtime = pn->mn_mtime; 1223655291f2SDavid Xu vap->va_ctime = pn->mn_ctime; 1224655291f2SDavid Xu vap->va_birthtime = pn->mn_birth; 1225655291f2SDavid Xu vap->va_gen = 0; 1226655291f2SDavid Xu vap->va_flags = 0; 12274c5a20e3SKonstantin Belousov vap->va_rdev = NODEV; 1228655291f2SDavid Xu vap->va_bytes = 0; 1229655291f2SDavid Xu vap->va_filerev = 0; 1230655291f2SDavid Xu return (error); 1231655291f2SDavid Xu } 1232655291f2SDavid Xu 1233655291f2SDavid Xu #if 0 1234655291f2SDavid Xu struct vop_setattr_args { 1235655291f2SDavid Xu struct vop_generic_args a_gen; 1236655291f2SDavid Xu struct vnode *a_vp; 1237655291f2SDavid Xu struct vattr *a_vap; 1238655291f2SDavid Xu struct ucred *a_cred; 1239655291f2SDavid Xu }; 1240655291f2SDavid Xu #endif 1241655291f2SDavid Xu /* 1242655291f2SDavid Xu * Set attributes 1243655291f2SDavid Xu */ 1244655291f2SDavid Xu static int 1245655291f2SDavid Xu mqfs_setattr(struct vop_setattr_args *ap) 1246655291f2SDavid Xu { 1247655291f2SDavid Xu struct mqfs_node *pn; 1248655291f2SDavid Xu struct vattr *vap; 1249655291f2SDavid Xu struct vnode *vp; 12500359a12eSAttilio Rao struct thread *td; 1251655291f2SDavid Xu int c, error; 1252655291f2SDavid Xu uid_t uid; 1253655291f2SDavid Xu gid_t gid; 1254655291f2SDavid Xu 12550359a12eSAttilio Rao td = curthread; 1256655291f2SDavid Xu vap = ap->a_vap; 1257655291f2SDavid Xu vp = ap->a_vp; 1258655291f2SDavid Xu if ((vap->va_type != VNON) || 1259655291f2SDavid Xu (vap->va_nlink != VNOVAL) || 1260655291f2SDavid Xu (vap->va_fsid != VNOVAL) || 1261655291f2SDavid Xu (vap->va_fileid != VNOVAL) || 1262655291f2SDavid Xu (vap->va_blocksize != VNOVAL) || 1263655291f2SDavid Xu (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1264655291f2SDavid Xu (vap->va_rdev != VNOVAL) || 1265655291f2SDavid Xu ((int)vap->va_bytes != VNOVAL) || 1266655291f2SDavid Xu (vap->va_gen != VNOVAL)) { 1267655291f2SDavid Xu return (EINVAL); 1268655291f2SDavid Xu } 1269655291f2SDavid Xu 1270655291f2SDavid Xu pn = VTON(vp); 1271655291f2SDavid Xu 1272655291f2SDavid Xu error = c = 0; 1273655291f2SDavid Xu if (vap->va_uid == (uid_t)VNOVAL) 1274655291f2SDavid Xu uid = pn->mn_uid; 1275655291f2SDavid Xu else 1276655291f2SDavid Xu uid = vap->va_uid; 1277655291f2SDavid Xu if (vap->va_gid == (gid_t)VNOVAL) 1278655291f2SDavid Xu gid = pn->mn_gid; 1279655291f2SDavid Xu else 1280655291f2SDavid Xu gid = vap->va_gid; 1281655291f2SDavid Xu 1282655291f2SDavid Xu if (uid != pn->mn_uid || gid != pn->mn_gid) { 1283655291f2SDavid Xu /* 1284655291f2SDavid Xu * To modify the ownership of a file, must possess VADMIN 1285655291f2SDavid Xu * for that file. 1286655291f2SDavid Xu */ 12870359a12eSAttilio Rao if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td))) 1288655291f2SDavid Xu return (error); 1289acd3428bSRobert Watson 1290acd3428bSRobert Watson /* 1291acd3428bSRobert Watson * XXXRW: Why is there a privilege check here: shouldn't the 1292acd3428bSRobert Watson * check in VOP_ACCESS() be enough? Also, are the group bits 1293acd3428bSRobert Watson * below definitely right? 1294acd3428bSRobert Watson */ 1295655291f2SDavid Xu if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid || 1296655291f2SDavid Xu (gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) && 12970359a12eSAttilio Rao (error = priv_check(td, PRIV_MQ_ADMIN)) != 0) 1298655291f2SDavid Xu return (error); 1299655291f2SDavid Xu pn->mn_uid = uid; 1300655291f2SDavid Xu pn->mn_gid = gid; 1301655291f2SDavid Xu c = 1; 1302655291f2SDavid Xu } 1303655291f2SDavid Xu 1304655291f2SDavid Xu if (vap->va_mode != (mode_t)VNOVAL) { 1305655291f2SDavid Xu if ((ap->a_cred->cr_uid != pn->mn_uid) && 13060359a12eSAttilio Rao (error = priv_check(td, PRIV_MQ_ADMIN))) 1307655291f2SDavid Xu return (error); 1308655291f2SDavid Xu pn->mn_mode = vap->va_mode; 1309655291f2SDavid Xu c = 1; 1310655291f2SDavid Xu } 1311655291f2SDavid Xu 1312655291f2SDavid Xu if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1313655291f2SDavid Xu /* See the comment in ufs_vnops::ufs_setattr(). */ 13140359a12eSAttilio Rao if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) && 1315655291f2SDavid Xu ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 13160359a12eSAttilio Rao (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td)))) 1317655291f2SDavid Xu return (error); 1318655291f2SDavid Xu if (vap->va_atime.tv_sec != VNOVAL) { 1319655291f2SDavid Xu pn->mn_atime = vap->va_atime; 1320655291f2SDavid Xu } 1321655291f2SDavid Xu if (vap->va_mtime.tv_sec != VNOVAL) { 1322655291f2SDavid Xu pn->mn_mtime = vap->va_mtime; 1323655291f2SDavid Xu } 1324655291f2SDavid Xu c = 1; 1325655291f2SDavid Xu } 1326655291f2SDavid Xu if (c) { 1327655291f2SDavid Xu vfs_timestamp(&pn->mn_ctime); 1328655291f2SDavid Xu } 1329655291f2SDavid Xu return (0); 1330655291f2SDavid Xu } 1331655291f2SDavid Xu 1332655291f2SDavid Xu #if 0 1333655291f2SDavid Xu struct vop_read_args { 1334655291f2SDavid Xu struct vop_generic_args a_gen; 1335655291f2SDavid Xu struct vnode *a_vp; 1336655291f2SDavid Xu struct uio *a_uio; 1337655291f2SDavid Xu int a_ioflag; 1338655291f2SDavid Xu struct ucred *a_cred; 1339655291f2SDavid Xu }; 1340655291f2SDavid Xu #endif 1341655291f2SDavid Xu 1342655291f2SDavid Xu /* 1343655291f2SDavid Xu * Read from a file 1344655291f2SDavid Xu */ 1345655291f2SDavid Xu static int 1346655291f2SDavid Xu mqfs_read(struct vop_read_args *ap) 1347655291f2SDavid Xu { 1348655291f2SDavid Xu char buf[80]; 1349655291f2SDavid Xu struct vnode *vp = ap->a_vp; 1350655291f2SDavid Xu struct uio *uio = ap->a_uio; 1351655291f2SDavid Xu struct mqfs_node *pn; 1352655291f2SDavid Xu struct mqueue *mq; 1353655291f2SDavid Xu int len, error; 1354655291f2SDavid Xu 1355655291f2SDavid Xu if (vp->v_type != VREG) 1356655291f2SDavid Xu return (EINVAL); 1357655291f2SDavid Xu 1358655291f2SDavid Xu pn = VTON(vp); 1359655291f2SDavid Xu mq = VTOMQ(vp); 1360655291f2SDavid Xu snprintf(buf, sizeof(buf), 1361655291f2SDavid Xu "QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n", 1362655291f2SDavid Xu mq->mq_totalbytes, 1363655291f2SDavid Xu mq->mq_maxmsg, 1364655291f2SDavid Xu mq->mq_curmsgs, 1365655291f2SDavid Xu mq->mq_msgsize); 1366655291f2SDavid Xu buf[sizeof(buf)-1] = '\0'; 1367655291f2SDavid Xu len = strlen(buf); 1368655291f2SDavid Xu error = uiomove_frombuf(buf, len, uio); 1369655291f2SDavid Xu return (error); 1370655291f2SDavid Xu } 1371655291f2SDavid Xu 1372655291f2SDavid Xu #if 0 1373655291f2SDavid Xu struct vop_readdir_args { 1374655291f2SDavid Xu struct vop_generic_args a_gen; 1375655291f2SDavid Xu struct vnode *a_vp; 1376655291f2SDavid Xu struct uio *a_uio; 1377655291f2SDavid Xu struct ucred *a_cred; 1378655291f2SDavid Xu int *a_eofflag; 1379655291f2SDavid Xu int *a_ncookies; 1380655291f2SDavid Xu u_long **a_cookies; 1381655291f2SDavid Xu }; 1382655291f2SDavid Xu #endif 1383655291f2SDavid Xu 1384655291f2SDavid Xu /* 1385655291f2SDavid Xu * Return directory entries. 1386655291f2SDavid Xu */ 1387655291f2SDavid Xu static int 1388655291f2SDavid Xu mqfs_readdir(struct vop_readdir_args *ap) 1389655291f2SDavid Xu { 1390655291f2SDavid Xu struct vnode *vp; 1391655291f2SDavid Xu struct mqfs_info *mi; 1392655291f2SDavid Xu struct mqfs_node *pd; 1393655291f2SDavid Xu struct mqfs_node *pn; 1394655291f2SDavid Xu struct dirent entry; 1395655291f2SDavid Xu struct uio *uio; 1396*adb023aeSJamie Gritton const void *pr_root; 1397655291f2SDavid Xu int *tmp_ncookies = NULL; 1398655291f2SDavid Xu off_t offset; 1399655291f2SDavid Xu int error, i; 1400655291f2SDavid Xu 1401655291f2SDavid Xu vp = ap->a_vp; 1402655291f2SDavid Xu mi = VFSTOMQFS(vp->v_mount); 1403655291f2SDavid Xu pd = VTON(vp); 1404655291f2SDavid Xu uio = ap->a_uio; 1405655291f2SDavid Xu 1406655291f2SDavid Xu if (vp->v_type != VDIR) 1407655291f2SDavid Xu return (ENOTDIR); 1408655291f2SDavid Xu 1409655291f2SDavid Xu if (uio->uio_offset < 0) 1410655291f2SDavid Xu return (EINVAL); 1411655291f2SDavid Xu 1412655291f2SDavid Xu if (ap->a_ncookies != NULL) { 1413655291f2SDavid Xu tmp_ncookies = ap->a_ncookies; 1414655291f2SDavid Xu *ap->a_ncookies = 0; 1415655291f2SDavid Xu ap->a_ncookies = NULL; 1416655291f2SDavid Xu } 1417655291f2SDavid Xu 1418655291f2SDavid Xu error = 0; 1419655291f2SDavid Xu offset = 0; 1420655291f2SDavid Xu 1421*adb023aeSJamie Gritton pr_root = ap->a_cred->cr_prison->pr_root; 1422655291f2SDavid Xu sx_xlock(&mi->mi_lock); 1423655291f2SDavid Xu 1424655291f2SDavid Xu LIST_FOREACH(pn, &pd->mn_children, mn_sibling) { 1425655291f2SDavid Xu entry.d_reclen = sizeof(entry); 1426*adb023aeSJamie Gritton /* 1427*adb023aeSJamie Gritton * Only show names within the same prison root directory 1428*adb023aeSJamie Gritton * (or not associated with a prison, e.g. "." and ".."). 1429*adb023aeSJamie Gritton */ 1430*adb023aeSJamie Gritton if (pn->mn_pr_root != NULL && pn->mn_pr_root != pr_root) 1431*adb023aeSJamie Gritton continue; 1432655291f2SDavid Xu if (!pn->mn_fileno) 1433655291f2SDavid Xu mqfs_fileno_alloc(mi, pn); 1434655291f2SDavid Xu entry.d_fileno = pn->mn_fileno; 1435655291f2SDavid Xu for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i) 1436655291f2SDavid Xu entry.d_name[i] = pn->mn_name[i]; 1437655291f2SDavid Xu entry.d_name[i] = 0; 1438655291f2SDavid Xu entry.d_namlen = i; 1439655291f2SDavid Xu switch (pn->mn_type) { 1440655291f2SDavid Xu case mqfstype_root: 1441655291f2SDavid Xu case mqfstype_dir: 1442655291f2SDavid Xu case mqfstype_this: 1443655291f2SDavid Xu case mqfstype_parent: 1444655291f2SDavid Xu entry.d_type = DT_DIR; 1445655291f2SDavid Xu break; 1446655291f2SDavid Xu case mqfstype_file: 1447655291f2SDavid Xu entry.d_type = DT_REG; 1448655291f2SDavid Xu break; 1449655291f2SDavid Xu case mqfstype_symlink: 1450655291f2SDavid Xu entry.d_type = DT_LNK; 1451655291f2SDavid Xu break; 1452655291f2SDavid Xu default: 1453655291f2SDavid Xu panic("%s has unexpected node type: %d", pn->mn_name, 1454655291f2SDavid Xu pn->mn_type); 1455655291f2SDavid Xu } 1456655291f2SDavid Xu if (entry.d_reclen > uio->uio_resid) 1457655291f2SDavid Xu break; 1458655291f2SDavid Xu if (offset >= uio->uio_offset) { 1459655291f2SDavid Xu error = vfs_read_dirent(ap, &entry, offset); 1460655291f2SDavid Xu if (error) 1461655291f2SDavid Xu break; 1462655291f2SDavid Xu } 1463655291f2SDavid Xu offset += entry.d_reclen; 1464655291f2SDavid Xu } 1465655291f2SDavid Xu sx_xunlock(&mi->mi_lock); 1466655291f2SDavid Xu 1467655291f2SDavid Xu uio->uio_offset = offset; 1468655291f2SDavid Xu 1469655291f2SDavid Xu if (tmp_ncookies != NULL) 1470655291f2SDavid Xu ap->a_ncookies = tmp_ncookies; 1471655291f2SDavid Xu 1472655291f2SDavid Xu return (error); 1473655291f2SDavid Xu } 1474655291f2SDavid Xu 1475655291f2SDavid Xu #ifdef notyet 1476655291f2SDavid Xu 1477655291f2SDavid Xu #if 0 1478655291f2SDavid Xu struct vop_mkdir_args { 1479655291f2SDavid Xu struct vnode *a_dvp; 1480655291f2SDavid Xu struvt vnode **a_vpp; 1481655291f2SDavid Xu struvt componentname *a_cnp; 1482655291f2SDavid Xu struct vattr *a_vap; 1483655291f2SDavid Xu }; 1484655291f2SDavid Xu #endif 1485655291f2SDavid Xu 1486655291f2SDavid Xu /* 1487655291f2SDavid Xu * Create a directory. 1488655291f2SDavid Xu */ 1489655291f2SDavid Xu static int 1490655291f2SDavid Xu mqfs_mkdir(struct vop_mkdir_args *ap) 1491655291f2SDavid Xu { 1492655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1493655291f2SDavid Xu struct componentname *cnp = ap->a_cnp; 1494655291f2SDavid Xu struct mqfs_node *pd = VTON(ap->a_dvp); 1495655291f2SDavid Xu struct mqfs_node *pn; 1496655291f2SDavid Xu int error; 1497655291f2SDavid Xu 1498655291f2SDavid Xu if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir) 1499655291f2SDavid Xu return (ENOTDIR); 1500655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1501655291f2SDavid Xu if ((cnp->cn_flags & HASBUF) == 0) 1502655291f2SDavid Xu panic("%s: no name", __func__); 1503ba0360b1SDavid Xu pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen, 1504ba0360b1SDavid Xu ap->a_vap->cn_cred, ap->a_vap->va_mode); 1505fbc48e97SDavid Xu if (pn != NULL) 1506fbc48e97SDavid Xu mqnode_addref(pn); 1507655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1508fbc48e97SDavid Xu if (pn == NULL) { 1509fbc48e97SDavid Xu error = ENOSPC; 1510fbc48e97SDavid Xu } else { 1511fbc48e97SDavid Xu error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn); 1512fbc48e97SDavid Xu mqnode_release(pn); 1513fbc48e97SDavid Xu } 1514655291f2SDavid Xu return (error); 1515655291f2SDavid Xu } 1516655291f2SDavid Xu 1517655291f2SDavid Xu #if 0 1518655291f2SDavid Xu struct vop_rmdir_args { 1519655291f2SDavid Xu struct vnode *a_dvp; 1520655291f2SDavid Xu struct vnode *a_vp; 1521655291f2SDavid Xu struct componentname *a_cnp; 1522655291f2SDavid Xu }; 1523655291f2SDavid Xu #endif 1524655291f2SDavid Xu 1525655291f2SDavid Xu /* 1526655291f2SDavid Xu * Remove a directory. 1527655291f2SDavid Xu */ 1528655291f2SDavid Xu static int 1529655291f2SDavid Xu mqfs_rmdir(struct vop_rmdir_args *ap) 1530655291f2SDavid Xu { 1531655291f2SDavid Xu struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount); 1532655291f2SDavid Xu struct mqfs_node *pn = VTON(ap->a_vp); 1533655291f2SDavid Xu struct mqfs_node *pt; 1534655291f2SDavid Xu 1535655291f2SDavid Xu if (pn->mn_type != mqfstype_dir) 1536655291f2SDavid Xu return (ENOTDIR); 1537655291f2SDavid Xu 1538655291f2SDavid Xu sx_xlock(&mqfs->mi_lock); 1539655291f2SDavid Xu if (pn->mn_deleted) { 1540655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1541655291f2SDavid Xu return (ENOENT); 1542655291f2SDavid Xu } 1543655291f2SDavid Xu 1544655291f2SDavid Xu pt = LIST_FIRST(&pn->mn_children); 1545655291f2SDavid Xu pt = LIST_NEXT(pt, mn_sibling); 1546655291f2SDavid Xu pt = LIST_NEXT(pt, mn_sibling); 1547655291f2SDavid Xu if (pt != NULL) { 1548655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1549655291f2SDavid Xu return (ENOTEMPTY); 1550655291f2SDavid Xu } 1551655291f2SDavid Xu pt = pn->mn_parent; 1552655291f2SDavid Xu pn->mn_parent = NULL; 1553655291f2SDavid Xu pn->mn_deleted = 1; 1554655291f2SDavid Xu LIST_REMOVE(pn, mn_sibling); 1555655291f2SDavid Xu mqnode_release(pn); 1556655291f2SDavid Xu mqnode_release(pt); 1557655291f2SDavid Xu sx_xunlock(&mqfs->mi_lock); 1558655291f2SDavid Xu cache_purge(ap->a_vp); 1559655291f2SDavid Xu return (0); 1560655291f2SDavid Xu } 1561655291f2SDavid Xu 1562655291f2SDavid Xu #endif /* notyet */ 1563655291f2SDavid Xu 1564*adb023aeSJamie Gritton 1565*adb023aeSJamie Gritton /* 1566*adb023aeSJamie Gritton * Set a destructor task with the prison's root 1567*adb023aeSJamie Gritton */ 1568*adb023aeSJamie Gritton static int 1569*adb023aeSJamie Gritton mqfs_prison_create(void *obj, void *data __unused) 1570*adb023aeSJamie Gritton { 1571*adb023aeSJamie Gritton struct prison *pr = obj; 1572*adb023aeSJamie Gritton struct mqfs_osd *mo; 1573*adb023aeSJamie Gritton void *rsv; 1574*adb023aeSJamie Gritton 1575*adb023aeSJamie Gritton if (pr->pr_root == pr->pr_parent->pr_root) 1576*adb023aeSJamie Gritton return(0); 1577*adb023aeSJamie Gritton 1578*adb023aeSJamie Gritton mo = malloc(sizeof(struct mqfs_osd), M_PRISON, M_WAITOK); 1579*adb023aeSJamie Gritton rsv = osd_reserve(mqfs_osd_jail_slot); 1580*adb023aeSJamie Gritton TASK_INIT(&mo->mo_task, 0, mqfs_prison_remove_task, mo); 1581*adb023aeSJamie Gritton mtx_lock(&pr->pr_mtx); 1582*adb023aeSJamie Gritton mo->mo_pr_root = pr->pr_root; 1583*adb023aeSJamie Gritton (void)osd_jail_set_reserved(pr, mqfs_osd_jail_slot, rsv, mo); 1584*adb023aeSJamie Gritton mtx_unlock(&pr->pr_mtx); 1585*adb023aeSJamie Gritton return (0); 1586*adb023aeSJamie Gritton } 1587*adb023aeSJamie Gritton 1588*adb023aeSJamie Gritton /* 1589*adb023aeSJamie Gritton * Queue the task for after jail/OSD locks are released 1590*adb023aeSJamie Gritton */ 1591*adb023aeSJamie Gritton static void 1592*adb023aeSJamie Gritton mqfs_prison_destructor(void *data) 1593*adb023aeSJamie Gritton { 1594*adb023aeSJamie Gritton struct mqfs_osd *mo = data; 1595*adb023aeSJamie Gritton 1596*adb023aeSJamie Gritton if (mqfs_osd_jail_slot != 0) 1597*adb023aeSJamie Gritton taskqueue_enqueue(taskqueue_thread, &mo->mo_task); 1598*adb023aeSJamie Gritton else 1599*adb023aeSJamie Gritton free(mo, M_PRISON); 1600*adb023aeSJamie Gritton } 1601*adb023aeSJamie Gritton 1602*adb023aeSJamie Gritton /* 1603*adb023aeSJamie Gritton * See if this prison root is obsolete, and clean up associated queues if it is 1604*adb023aeSJamie Gritton */ 1605*adb023aeSJamie Gritton static void 1606*adb023aeSJamie Gritton mqfs_prison_remove_task(void *context, int pending) 1607*adb023aeSJamie Gritton { 1608*adb023aeSJamie Gritton struct mqfs_osd *mo = context; 1609*adb023aeSJamie Gritton struct mqfs_node *pn, *tpn; 1610*adb023aeSJamie Gritton const struct prison *pr; 1611*adb023aeSJamie Gritton const void *pr_root; 1612*adb023aeSJamie Gritton int found; 1613*adb023aeSJamie Gritton 1614*adb023aeSJamie Gritton pr_root = mo->mo_pr_root; 1615*adb023aeSJamie Gritton found = 0; 1616*adb023aeSJamie Gritton sx_slock(&allprison_lock); 1617*adb023aeSJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 1618*adb023aeSJamie Gritton if (pr->pr_root == pr_root) 1619*adb023aeSJamie Gritton found = 1; 1620*adb023aeSJamie Gritton } 1621*adb023aeSJamie Gritton sx_sunlock(&allprison_lock); 1622*adb023aeSJamie Gritton if (!found) { 1623*adb023aeSJamie Gritton /* 1624*adb023aeSJamie Gritton * No jails are rooted in this directory anymore, 1625*adb023aeSJamie Gritton * so no queues should be either. 1626*adb023aeSJamie Gritton */ 1627*adb023aeSJamie Gritton sx_xlock(&mqfs_data.mi_lock); 1628*adb023aeSJamie Gritton LIST_FOREACH_SAFE(pn, &mqfs_data.mi_root->mn_children, 1629*adb023aeSJamie Gritton mn_sibling, tpn) { 1630*adb023aeSJamie Gritton if (pn->mn_pr_root == pr_root) 1631*adb023aeSJamie Gritton (void)do_unlink(pn, curthread->td_ucred); 1632*adb023aeSJamie Gritton } 1633*adb023aeSJamie Gritton sx_xunlock(&mqfs_data.mi_lock); 1634*adb023aeSJamie Gritton } 1635*adb023aeSJamie Gritton free(mo, M_PRISON); 1636*adb023aeSJamie Gritton } 1637*adb023aeSJamie Gritton 1638*adb023aeSJamie Gritton 1639655291f2SDavid Xu /* 1640655291f2SDavid Xu * Allocate a message queue 1641655291f2SDavid Xu */ 1642655291f2SDavid Xu static struct mqueue * 1643655291f2SDavid Xu mqueue_alloc(const struct mq_attr *attr) 1644655291f2SDavid Xu { 1645655291f2SDavid Xu struct mqueue *mq; 1646655291f2SDavid Xu 1647655291f2SDavid Xu if (curmq >= maxmq) 1648655291f2SDavid Xu return (NULL); 1649655291f2SDavid Xu mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO); 1650655291f2SDavid Xu TAILQ_INIT(&mq->mq_msgq); 1651655291f2SDavid Xu if (attr != NULL) { 1652655291f2SDavid Xu mq->mq_maxmsg = attr->mq_maxmsg; 1653655291f2SDavid Xu mq->mq_msgsize = attr->mq_msgsize; 1654655291f2SDavid Xu } else { 1655655291f2SDavid Xu mq->mq_maxmsg = default_maxmsg; 1656655291f2SDavid Xu mq->mq_msgsize = default_msgsize; 1657655291f2SDavid Xu } 1658b042e976SDavid Xu mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF); 1659d8b0556cSKonstantin Belousov knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex); 1660d8b0556cSKonstantin Belousov knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex); 1661655291f2SDavid Xu atomic_add_int(&curmq, 1); 1662655291f2SDavid Xu return (mq); 1663655291f2SDavid Xu } 1664655291f2SDavid Xu 1665655291f2SDavid Xu /* 1666655291f2SDavid Xu * Destroy a message queue 1667655291f2SDavid Xu */ 1668655291f2SDavid Xu static void 1669655291f2SDavid Xu mqueue_free(struct mqueue *mq) 1670655291f2SDavid Xu { 1671655291f2SDavid Xu struct mqueue_msg *msg; 1672655291f2SDavid Xu 1673655291f2SDavid Xu while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) { 1674655291f2SDavid Xu TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link); 16751ede983cSDag-Erling Smørgrav free(msg, M_MQUEUEDATA); 1676655291f2SDavid Xu } 1677655291f2SDavid Xu 1678655291f2SDavid Xu mtx_destroy(&mq->mq_mutex); 16796aba400aSAttilio Rao seldrain(&mq->mq_rsel); 16806aba400aSAttilio Rao seldrain(&mq->mq_wsel); 1681655291f2SDavid Xu knlist_destroy(&mq->mq_rsel.si_note); 1682655291f2SDavid Xu knlist_destroy(&mq->mq_wsel.si_note); 1683655291f2SDavid Xu uma_zfree(mqueue_zone, mq); 1684655291f2SDavid Xu atomic_add_int(&curmq, -1); 1685655291f2SDavid Xu } 1686655291f2SDavid Xu 1687655291f2SDavid Xu /* 1688655291f2SDavid Xu * Load a message from user space 1689655291f2SDavid Xu */ 1690655291f2SDavid Xu static struct mqueue_msg * 1691655291f2SDavid Xu mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio) 1692655291f2SDavid Xu { 1693655291f2SDavid Xu struct mqueue_msg *msg; 1694655291f2SDavid Xu size_t len; 1695655291f2SDavid Xu int error; 1696655291f2SDavid Xu 1697655291f2SDavid Xu len = sizeof(struct mqueue_msg) + msg_size; 16981ede983cSDag-Erling Smørgrav msg = malloc(len, M_MQUEUEDATA, M_WAITOK); 1699655291f2SDavid Xu error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg), 1700655291f2SDavid Xu msg_size); 1701655291f2SDavid Xu if (error) { 17021ede983cSDag-Erling Smørgrav free(msg, M_MQUEUEDATA); 1703655291f2SDavid Xu msg = NULL; 1704655291f2SDavid Xu } else { 1705655291f2SDavid Xu msg->msg_size = msg_size; 1706655291f2SDavid Xu msg->msg_prio = msg_prio; 1707655291f2SDavid Xu } 1708655291f2SDavid Xu return (msg); 1709655291f2SDavid Xu } 1710655291f2SDavid Xu 1711655291f2SDavid Xu /* 1712655291f2SDavid Xu * Save a message to user space 1713655291f2SDavid Xu */ 1714655291f2SDavid Xu static int 1715655291f2SDavid Xu mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio) 1716655291f2SDavid Xu { 1717655291f2SDavid Xu int error; 1718655291f2SDavid Xu 1719655291f2SDavid Xu error = copyout(((char *)msg) + sizeof(*msg), msg_ptr, 1720655291f2SDavid Xu msg->msg_size); 1721b2f92ef9SDavid Xu if (error == 0 && msg_prio != NULL) 1722655291f2SDavid Xu error = copyout(&msg->msg_prio, msg_prio, sizeof(int)); 1723655291f2SDavid Xu return (error); 1724655291f2SDavid Xu } 1725655291f2SDavid Xu 1726655291f2SDavid Xu /* 1727655291f2SDavid Xu * Free a message's memory 1728655291f2SDavid Xu */ 1729655291f2SDavid Xu static __inline void 1730655291f2SDavid Xu mqueue_freemsg(struct mqueue_msg *msg) 1731655291f2SDavid Xu { 17321ede983cSDag-Erling Smørgrav free(msg, M_MQUEUEDATA); 1733655291f2SDavid Xu } 1734655291f2SDavid Xu 1735655291f2SDavid Xu /* 1736655291f2SDavid Xu * Send a message. if waitok is false, thread will not be 1737655291f2SDavid Xu * blocked if there is no data in queue, otherwise, absolute 1738655291f2SDavid Xu * time will be checked. 1739655291f2SDavid Xu */ 1740655291f2SDavid Xu int 1741655291f2SDavid Xu mqueue_send(struct mqueue *mq, const char *msg_ptr, 1742655291f2SDavid Xu size_t msg_len, unsigned msg_prio, int waitok, 1743655291f2SDavid Xu const struct timespec *abs_timeout) 1744655291f2SDavid Xu { 1745655291f2SDavid Xu struct mqueue_msg *msg; 1746afde2b65SKonstantin Belousov struct timespec ts, ts2; 1747655291f2SDavid Xu struct timeval tv; 1748655291f2SDavid Xu int error; 1749655291f2SDavid Xu 1750a6de716dSDavid Xu if (msg_prio >= MQ_PRIO_MAX) 1751a6de716dSDavid Xu return (EINVAL); 1752655291f2SDavid Xu if (msg_len > mq->mq_msgsize) 1753655291f2SDavid Xu return (EMSGSIZE); 1754655291f2SDavid Xu msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio); 1755655291f2SDavid Xu if (msg == NULL) 1756655291f2SDavid Xu return (EFAULT); 1757655291f2SDavid Xu 1758655291f2SDavid Xu /* O_NONBLOCK case */ 1759655291f2SDavid Xu if (!waitok) { 1760655291f2SDavid Xu error = _mqueue_send(mq, msg, -1); 1761655291f2SDavid Xu if (error) 1762655291f2SDavid Xu goto bad; 1763655291f2SDavid Xu return (0); 1764655291f2SDavid Xu } 1765655291f2SDavid Xu 1766655291f2SDavid Xu /* we allow a null timeout (wait forever) */ 1767655291f2SDavid Xu if (abs_timeout == NULL) { 1768655291f2SDavid Xu error = _mqueue_send(mq, msg, 0); 1769655291f2SDavid Xu if (error) 1770655291f2SDavid Xu goto bad; 1771655291f2SDavid Xu return (0); 1772655291f2SDavid Xu } 1773655291f2SDavid Xu 1774655291f2SDavid Xu /* send it before checking time */ 1775655291f2SDavid Xu error = _mqueue_send(mq, msg, -1); 1776655291f2SDavid Xu if (error == 0) 1777655291f2SDavid Xu return (0); 1778655291f2SDavid Xu 1779655291f2SDavid Xu if (error != EAGAIN) 1780655291f2SDavid Xu goto bad; 1781655291f2SDavid Xu 1782afde2b65SKonstantin Belousov if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) { 1783655291f2SDavid Xu error = EINVAL; 1784655291f2SDavid Xu goto bad; 1785655291f2SDavid Xu } 1786655291f2SDavid Xu for (;;) { 1787afde2b65SKonstantin Belousov ts2 = *abs_timeout; 1788a6de716dSDavid Xu getnanotime(&ts); 1789655291f2SDavid Xu timespecsub(&ts2, &ts); 1790655291f2SDavid Xu if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { 1791655291f2SDavid Xu error = ETIMEDOUT; 1792655291f2SDavid Xu break; 1793655291f2SDavid Xu } 1794655291f2SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts2); 1795655291f2SDavid Xu error = _mqueue_send(mq, msg, tvtohz(&tv)); 1796655291f2SDavid Xu if (error != ETIMEDOUT) 1797655291f2SDavid Xu break; 1798655291f2SDavid Xu } 1799655291f2SDavid Xu if (error == 0) 1800655291f2SDavid Xu return (0); 1801655291f2SDavid Xu bad: 1802655291f2SDavid Xu mqueue_freemsg(msg); 1803655291f2SDavid Xu return (error); 1804655291f2SDavid Xu } 1805655291f2SDavid Xu 1806655291f2SDavid Xu /* 1807655291f2SDavid Xu * Common routine to send a message 1808655291f2SDavid Xu */ 1809655291f2SDavid Xu static int 1810655291f2SDavid Xu _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo) 1811655291f2SDavid Xu { 1812655291f2SDavid Xu struct mqueue_msg *msg2; 1813655291f2SDavid Xu int error = 0; 1814655291f2SDavid Xu 1815655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 1816655291f2SDavid Xu while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) { 1817655291f2SDavid Xu if (timo < 0) { 1818655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1819655291f2SDavid Xu return (EAGAIN); 1820655291f2SDavid Xu } 1821655291f2SDavid Xu mq->mq_senders++; 1822655291f2SDavid Xu error = msleep(&mq->mq_senders, &mq->mq_mutex, 18230f180a7cSJohn Baldwin PCATCH, "mqsend", timo); 1824655291f2SDavid Xu mq->mq_senders--; 1825655291f2SDavid Xu if (error == EAGAIN) 1826655291f2SDavid Xu error = ETIMEDOUT; 1827655291f2SDavid Xu } 1828655291f2SDavid Xu if (mq->mq_curmsgs >= mq->mq_maxmsg) { 1829655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1830655291f2SDavid Xu return (error); 1831655291f2SDavid Xu } 1832655291f2SDavid Xu error = 0; 1833655291f2SDavid Xu if (TAILQ_EMPTY(&mq->mq_msgq)) { 1834655291f2SDavid Xu TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link); 1835655291f2SDavid Xu } else { 1836655291f2SDavid Xu if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) { 1837655291f2SDavid Xu TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link); 1838655291f2SDavid Xu } else { 1839655291f2SDavid Xu TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) { 1840655291f2SDavid Xu if (msg2->msg_prio < msg->msg_prio) 1841655291f2SDavid Xu break; 1842655291f2SDavid Xu } 1843655291f2SDavid Xu TAILQ_INSERT_BEFORE(msg2, msg, msg_link); 1844655291f2SDavid Xu } 1845655291f2SDavid Xu } 1846655291f2SDavid Xu mq->mq_curmsgs++; 1847655291f2SDavid Xu mq->mq_totalbytes += msg->msg_size; 1848655291f2SDavid Xu if (mq->mq_receivers) 1849655291f2SDavid Xu wakeup_one(&mq->mq_receivers); 1850655291f2SDavid Xu else if (mq->mq_notifier != NULL) 1851655291f2SDavid Xu mqueue_send_notification(mq); 1852655291f2SDavid Xu if (mq->mq_flags & MQ_RSEL) { 1853655291f2SDavid Xu mq->mq_flags &= ~MQ_RSEL; 185403f70aecSDavid Xu selwakeup(&mq->mq_rsel); 1855655291f2SDavid Xu } 1856655291f2SDavid Xu KNOTE_LOCKED(&mq->mq_rsel.si_note, 0); 1857655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1858655291f2SDavid Xu return (0); 1859655291f2SDavid Xu } 1860655291f2SDavid Xu 1861655291f2SDavid Xu /* 1862655291f2SDavid Xu * Send realtime a signal to process which registered itself 1863655291f2SDavid Xu * successfully by mq_notify. 1864655291f2SDavid Xu */ 1865655291f2SDavid Xu static void 1866655291f2SDavid Xu mqueue_send_notification(struct mqueue *mq) 1867655291f2SDavid Xu { 1868b2f92ef9SDavid Xu struct mqueue_notifier *nt; 1869cf7d9a8cSDavid Xu struct thread *td; 1870b2f92ef9SDavid Xu struct proc *p; 1871cf7d9a8cSDavid Xu int error; 1872655291f2SDavid Xu 1873655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 1874b2f92ef9SDavid Xu nt = mq->mq_notifier; 1875052ea11cSDavid Xu if (nt->nt_sigev.sigev_notify != SIGEV_NONE) { 1876b2f92ef9SDavid Xu p = nt->nt_proc; 1877cf7d9a8cSDavid Xu error = sigev_findtd(p, &nt->nt_sigev, &td); 1878cf7d9a8cSDavid Xu if (error) { 1879cf7d9a8cSDavid Xu mq->mq_notifier = NULL; 1880cf7d9a8cSDavid Xu return; 1881cf7d9a8cSDavid Xu } 1882cf7d9a8cSDavid Xu if (!KSI_ONQ(&nt->nt_ksi)) { 1883cf7d9a8cSDavid Xu ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev); 1884cf7d9a8cSDavid Xu tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi); 1885cf7d9a8cSDavid Xu } 1886b2f92ef9SDavid Xu PROC_UNLOCK(p); 1887052ea11cSDavid Xu } 1888655291f2SDavid Xu mq->mq_notifier = NULL; 1889655291f2SDavid Xu } 1890655291f2SDavid Xu 1891655291f2SDavid Xu /* 1892655291f2SDavid Xu * Get a message. if waitok is false, thread will not be 1893655291f2SDavid Xu * blocked if there is no data in queue, otherwise, absolute 1894655291f2SDavid Xu * time will be checked. 1895655291f2SDavid Xu */ 1896655291f2SDavid Xu int 1897655291f2SDavid Xu mqueue_receive(struct mqueue *mq, char *msg_ptr, 1898655291f2SDavid Xu size_t msg_len, unsigned *msg_prio, int waitok, 1899655291f2SDavid Xu const struct timespec *abs_timeout) 1900655291f2SDavid Xu { 1901655291f2SDavid Xu struct mqueue_msg *msg; 1902afde2b65SKonstantin Belousov struct timespec ts, ts2; 1903655291f2SDavid Xu struct timeval tv; 1904655291f2SDavid Xu int error; 1905655291f2SDavid Xu 1906655291f2SDavid Xu if (msg_len < mq->mq_msgsize) 1907655291f2SDavid Xu return (EMSGSIZE); 1908655291f2SDavid Xu 1909655291f2SDavid Xu /* O_NONBLOCK case */ 1910655291f2SDavid Xu if (!waitok) { 1911655291f2SDavid Xu error = _mqueue_recv(mq, &msg, -1); 1912655291f2SDavid Xu if (error) 1913655291f2SDavid Xu return (error); 1914655291f2SDavid Xu goto received; 1915655291f2SDavid Xu } 1916655291f2SDavid Xu 1917655291f2SDavid Xu /* we allow a null timeout (wait forever). */ 1918655291f2SDavid Xu if (abs_timeout == NULL) { 1919655291f2SDavid Xu error = _mqueue_recv(mq, &msg, 0); 1920655291f2SDavid Xu if (error) 1921655291f2SDavid Xu return (error); 1922655291f2SDavid Xu goto received; 1923655291f2SDavid Xu } 1924655291f2SDavid Xu 1925655291f2SDavid Xu /* try to get a message before checking time */ 1926655291f2SDavid Xu error = _mqueue_recv(mq, &msg, -1); 1927655291f2SDavid Xu if (error == 0) 1928655291f2SDavid Xu goto received; 1929655291f2SDavid Xu 1930655291f2SDavid Xu if (error != EAGAIN) 1931655291f2SDavid Xu return (error); 1932655291f2SDavid Xu 1933afde2b65SKonstantin Belousov if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) { 1934655291f2SDavid Xu error = EINVAL; 1935655291f2SDavid Xu return (error); 1936655291f2SDavid Xu } 1937655291f2SDavid Xu 1938655291f2SDavid Xu for (;;) { 1939afde2b65SKonstantin Belousov ts2 = *abs_timeout; 1940a6de716dSDavid Xu getnanotime(&ts); 1941655291f2SDavid Xu timespecsub(&ts2, &ts); 1942655291f2SDavid Xu if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) { 1943655291f2SDavid Xu error = ETIMEDOUT; 1944655291f2SDavid Xu return (error); 1945655291f2SDavid Xu } 1946655291f2SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, &ts2); 1947655291f2SDavid Xu error = _mqueue_recv(mq, &msg, tvtohz(&tv)); 1948655291f2SDavid Xu if (error == 0) 1949655291f2SDavid Xu break; 1950655291f2SDavid Xu if (error != ETIMEDOUT) 1951655291f2SDavid Xu return (error); 1952655291f2SDavid Xu } 1953655291f2SDavid Xu 1954655291f2SDavid Xu received: 1955655291f2SDavid Xu error = mqueue_savemsg(msg, msg_ptr, msg_prio); 1956655291f2SDavid Xu if (error == 0) { 1957655291f2SDavid Xu curthread->td_retval[0] = msg->msg_size; 1958655291f2SDavid Xu curthread->td_retval[1] = 0; 1959655291f2SDavid Xu } 1960655291f2SDavid Xu mqueue_freemsg(msg); 1961655291f2SDavid Xu return (error); 1962655291f2SDavid Xu } 1963655291f2SDavid Xu 1964655291f2SDavid Xu /* 1965655291f2SDavid Xu * Common routine to receive a message 1966655291f2SDavid Xu */ 1967655291f2SDavid Xu static int 1968655291f2SDavid Xu _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo) 1969655291f2SDavid Xu { 1970655291f2SDavid Xu int error = 0; 1971655291f2SDavid Xu 1972655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 1973655291f2SDavid Xu while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) { 1974655291f2SDavid Xu if (timo < 0) { 1975655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 1976655291f2SDavid Xu return (EAGAIN); 1977655291f2SDavid Xu } 1978655291f2SDavid Xu mq->mq_receivers++; 1979655291f2SDavid Xu error = msleep(&mq->mq_receivers, &mq->mq_mutex, 19800f180a7cSJohn Baldwin PCATCH, "mqrecv", timo); 1981655291f2SDavid Xu mq->mq_receivers--; 1982655291f2SDavid Xu if (error == EAGAIN) 1983655291f2SDavid Xu error = ETIMEDOUT; 1984655291f2SDavid Xu } 1985655291f2SDavid Xu if (*msg != NULL) { 1986655291f2SDavid Xu error = 0; 1987655291f2SDavid Xu TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link); 1988655291f2SDavid Xu mq->mq_curmsgs--; 1989655291f2SDavid Xu mq->mq_totalbytes -= (*msg)->msg_size; 1990655291f2SDavid Xu if (mq->mq_senders) 1991655291f2SDavid Xu wakeup_one(&mq->mq_senders); 1992655291f2SDavid Xu if (mq->mq_flags & MQ_WSEL) { 1993655291f2SDavid Xu mq->mq_flags &= ~MQ_WSEL; 199403f70aecSDavid Xu selwakeup(&mq->mq_wsel); 1995655291f2SDavid Xu } 1996655291f2SDavid Xu KNOTE_LOCKED(&mq->mq_wsel.si_note, 0); 1997655291f2SDavid Xu } 1998655291f2SDavid Xu if (mq->mq_notifier != NULL && mq->mq_receivers == 0 && 1999655291f2SDavid Xu !TAILQ_EMPTY(&mq->mq_msgq)) { 2000655291f2SDavid Xu mqueue_send_notification(mq); 2001655291f2SDavid Xu } 2002655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2003655291f2SDavid Xu return (error); 2004655291f2SDavid Xu } 2005655291f2SDavid Xu 2006b2f92ef9SDavid Xu static __inline struct mqueue_notifier * 2007b2f92ef9SDavid Xu notifier_alloc(void) 2008655291f2SDavid Xu { 2009b2f92ef9SDavid Xu return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO)); 2010655291f2SDavid Xu } 2011655291f2SDavid Xu 2012655291f2SDavid Xu static __inline void 2013b2f92ef9SDavid Xu notifier_free(struct mqueue_notifier *p) 2014655291f2SDavid Xu { 2015b2f92ef9SDavid Xu uma_zfree(mqnoti_zone, p); 2016b2f92ef9SDavid Xu } 2017b2f92ef9SDavid Xu 2018b2f92ef9SDavid Xu static struct mqueue_notifier * 2019b2f92ef9SDavid Xu notifier_search(struct proc *p, int fd) 2020b2f92ef9SDavid Xu { 2021b2f92ef9SDavid Xu struct mqueue_notifier *nt; 2022b2f92ef9SDavid Xu 2023b2f92ef9SDavid Xu LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) { 20249da8a32aSDavid Xu if (nt->nt_ksi.ksi_mqd == fd) 2025b2f92ef9SDavid Xu break; 2026b2f92ef9SDavid Xu } 2027b2f92ef9SDavid Xu return (nt); 2028b2f92ef9SDavid Xu } 2029b2f92ef9SDavid Xu 2030102178d0SDavid Xu static __inline void 2031b2f92ef9SDavid Xu notifier_insert(struct proc *p, struct mqueue_notifier *nt) 2032b2f92ef9SDavid Xu { 2033b2f92ef9SDavid Xu LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link); 2034b2f92ef9SDavid Xu } 2035b2f92ef9SDavid Xu 2036102178d0SDavid Xu static __inline void 2037b2f92ef9SDavid Xu notifier_delete(struct proc *p, struct mqueue_notifier *nt) 2038b2f92ef9SDavid Xu { 2039b2f92ef9SDavid Xu LIST_REMOVE(nt, nt_link); 2040b2f92ef9SDavid Xu notifier_free(nt); 2041b2f92ef9SDavid Xu } 2042b2f92ef9SDavid Xu 2043b2f92ef9SDavid Xu static void 2044b2f92ef9SDavid Xu notifier_remove(struct proc *p, struct mqueue *mq, int fd) 2045b2f92ef9SDavid Xu { 2046b2f92ef9SDavid Xu struct mqueue_notifier *nt; 2047b2f92ef9SDavid Xu 2048b2f92ef9SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2049b2f92ef9SDavid Xu PROC_LOCK(p); 2050b2f92ef9SDavid Xu nt = notifier_search(p, fd); 2051b2f92ef9SDavid Xu if (nt != NULL) { 2052b2f92ef9SDavid Xu if (mq->mq_notifier == nt) 2053b2f92ef9SDavid Xu mq->mq_notifier = NULL; 2054b2f92ef9SDavid Xu sigqueue_take(&nt->nt_ksi); 2055b2f92ef9SDavid Xu notifier_delete(p, nt); 2056b2f92ef9SDavid Xu } 2057b2f92ef9SDavid Xu PROC_UNLOCK(p); 2058655291f2SDavid Xu } 2059655291f2SDavid Xu 2060afde2b65SKonstantin Belousov static int 2061afde2b65SKonstantin Belousov kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode, 2062afde2b65SKonstantin Belousov const struct mq_attr *attr) 2063655291f2SDavid Xu { 2064655291f2SDavid Xu char path[MQFS_NAMELEN + 1]; 2065655291f2SDavid Xu struct mqfs_node *pn; 2066655291f2SDavid Xu struct filedesc *fdp; 2067655291f2SDavid Xu struct file *fp; 2068655291f2SDavid Xu struct mqueue *mq; 2069afde2b65SKonstantin Belousov int fd, error, len, cmode; 2070655291f2SDavid Xu 2071655291f2SDavid Xu fdp = td->td_proc->p_fd; 2072afde2b65SKonstantin Belousov cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT); 2073f72b11a4SDavid Xu mq = NULL; 2074afde2b65SKonstantin Belousov if ((flags & O_CREAT) != 0 && attr != NULL) { 2075afde2b65SKonstantin Belousov if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg) 2076655291f2SDavid Xu return (EINVAL); 2077afde2b65SKonstantin Belousov if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize) 2078655291f2SDavid Xu return (EINVAL); 2079afde2b65SKonstantin Belousov } 2080655291f2SDavid Xu 2081afde2b65SKonstantin Belousov error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL); 2082655291f2SDavid Xu if (error) 2083655291f2SDavid Xu return (error); 2084655291f2SDavid Xu 2085655291f2SDavid Xu /* 2086655291f2SDavid Xu * The first character of name must be a slash (/) character 2087655291f2SDavid Xu * and the remaining characters of name cannot include any slash 2088655291f2SDavid Xu * characters. 2089655291f2SDavid Xu */ 2090655291f2SDavid Xu len = strlen(path); 2091dc15eac0SEd Schouten if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL) 2092655291f2SDavid Xu return (EINVAL); 2093655291f2SDavid Xu 2094b68cf25fSJilles Tjoelker error = falloc(td, &fp, &fd, O_CLOEXEC); 2095655291f2SDavid Xu if (error) 2096655291f2SDavid Xu return (error); 2097655291f2SDavid Xu 2098655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2099*adb023aeSJamie Gritton pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred); 2100655291f2SDavid Xu if (pn == NULL) { 2101655291f2SDavid Xu if (!(flags & O_CREAT)) { 2102655291f2SDavid Xu error = ENOENT; 2103655291f2SDavid Xu } else { 2104afde2b65SKonstantin Belousov mq = mqueue_alloc(attr); 2105b2f92ef9SDavid Xu if (mq == NULL) { 2106b2f92ef9SDavid Xu error = ENFILE; 2107b2f92ef9SDavid Xu } else { 2108655291f2SDavid Xu pn = mqfs_create_file(mqfs_data.mi_root, 2109ba0360b1SDavid Xu path + 1, len - 1, td->td_ucred, 2110ba0360b1SDavid Xu cmode); 2111655291f2SDavid Xu if (pn == NULL) { 2112655291f2SDavid Xu error = ENOSPC; 2113b2f92ef9SDavid Xu mqueue_free(mq); 2114b2f92ef9SDavid Xu } 2115655291f2SDavid Xu } 2116655291f2SDavid Xu } 2117655291f2SDavid Xu 2118655291f2SDavid Xu if (error == 0) { 2119655291f2SDavid Xu pn->mn_data = mq; 2120655291f2SDavid Xu } 2121655291f2SDavid Xu } else { 2122655291f2SDavid Xu if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) { 2123655291f2SDavid Xu error = EEXIST; 2124655291f2SDavid Xu } else { 212515bc6b2bSEdward Tomasz Napierala accmode_t accmode = 0; 2126655291f2SDavid Xu 2127655291f2SDavid Xu if (flags & FREAD) 212815bc6b2bSEdward Tomasz Napierala accmode |= VREAD; 2129655291f2SDavid Xu if (flags & FWRITE) 213015bc6b2bSEdward Tomasz Napierala accmode |= VWRITE; 2131655291f2SDavid Xu error = vaccess(VREG, pn->mn_mode, pn->mn_uid, 213215bc6b2bSEdward Tomasz Napierala pn->mn_gid, accmode, td->td_ucred, NULL); 2133655291f2SDavid Xu } 2134655291f2SDavid Xu } 2135655291f2SDavid Xu 2136655291f2SDavid Xu if (error) { 2137655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 213890f54cbfSMateusz Guzik fdclose(td, fp, fd); 2139655291f2SDavid Xu fdrop(fp, td); 2140655291f2SDavid Xu return (error); 2141655291f2SDavid Xu } 2142655291f2SDavid Xu 2143655291f2SDavid Xu mqnode_addref(pn); 2144655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2145655291f2SDavid Xu 2146397c19d1SJeff Roberson finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn, 2147397c19d1SJeff Roberson &mqueueops); 2148655291f2SDavid Xu 2149655291f2SDavid Xu td->td_retval[0] = fd; 2150655291f2SDavid Xu fdrop(fp, td); 2151655291f2SDavid Xu return (0); 2152655291f2SDavid Xu } 2153655291f2SDavid Xu 2154655291f2SDavid Xu /* 2155afde2b65SKonstantin Belousov * Syscall to open a message queue. 2156afde2b65SKonstantin Belousov */ 2157afde2b65SKonstantin Belousov int 21588451d0ddSKip Macy sys_kmq_open(struct thread *td, struct kmq_open_args *uap) 2159afde2b65SKonstantin Belousov { 2160afde2b65SKonstantin Belousov struct mq_attr attr; 2161afde2b65SKonstantin Belousov int flags, error; 2162afde2b65SKonstantin Belousov 21636fbdb9f4SJilles Tjoelker if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC) 2164afde2b65SKonstantin Belousov return (EINVAL); 2165afde2b65SKonstantin Belousov flags = FFLAGS(uap->flags); 2166afde2b65SKonstantin Belousov if ((flags & O_CREAT) != 0 && uap->attr != NULL) { 2167afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr, sizeof(attr)); 2168afde2b65SKonstantin Belousov if (error) 2169afde2b65SKonstantin Belousov return (error); 2170afde2b65SKonstantin Belousov } 2171afde2b65SKonstantin Belousov return (kern_kmq_open(td, uap->path, flags, uap->mode, 2172afde2b65SKonstantin Belousov uap->attr != NULL ? &attr : NULL)); 2173afde2b65SKonstantin Belousov } 2174afde2b65SKonstantin Belousov 2175afde2b65SKonstantin Belousov /* 2176873fbcd7SRobert Watson * Syscall to unlink a message queue. 2177655291f2SDavid Xu */ 2178655291f2SDavid Xu int 21798451d0ddSKip Macy sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap) 2180655291f2SDavid Xu { 2181655291f2SDavid Xu char path[MQFS_NAMELEN+1]; 2182655291f2SDavid Xu struct mqfs_node *pn; 2183655291f2SDavid Xu int error, len; 2184655291f2SDavid Xu 2185655291f2SDavid Xu error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL); 2186655291f2SDavid Xu if (error) 2187655291f2SDavid Xu return (error); 2188655291f2SDavid Xu 2189655291f2SDavid Xu len = strlen(path); 2190dc15eac0SEd Schouten if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL) 2191655291f2SDavid Xu return (EINVAL); 2192655291f2SDavid Xu 2193655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2194*adb023aeSJamie Gritton pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1, td->td_ucred); 2195655291f2SDavid Xu if (pn != NULL) 2196655291f2SDavid Xu error = do_unlink(pn, td->td_ucred); 2197655291f2SDavid Xu else 2198655291f2SDavid Xu error = ENOENT; 2199655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2200655291f2SDavid Xu return (error); 2201655291f2SDavid Xu } 2202655291f2SDavid Xu 22037008be5bSPawel Jakub Dawidek typedef int (*_fgetf)(struct thread *, int, cap_rights_t *, struct file **); 2204655291f2SDavid Xu 2205655291f2SDavid Xu /* 2206655291f2SDavid Xu * Get message queue by giving file slot 2207655291f2SDavid Xu */ 2208655291f2SDavid Xu static int 22097008be5bSPawel Jakub Dawidek _getmq(struct thread *td, int fd, cap_rights_t *rightsp, _fgetf func, 2210655291f2SDavid Xu struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq) 2211655291f2SDavid Xu { 2212655291f2SDavid Xu struct mqfs_node *pn; 2213655291f2SDavid Xu int error; 2214655291f2SDavid Xu 22157008be5bSPawel Jakub Dawidek error = func(td, fd, rightsp, fpp); 2216655291f2SDavid Xu if (error) 2217655291f2SDavid Xu return (error); 2218655291f2SDavid Xu if (&mqueueops != (*fpp)->f_ops) { 2219655291f2SDavid Xu fdrop(*fpp, td); 2220655291f2SDavid Xu return (EBADF); 2221655291f2SDavid Xu } 2222b2f92ef9SDavid Xu pn = (*fpp)->f_data; 2223655291f2SDavid Xu if (ppn) 2224655291f2SDavid Xu *ppn = pn; 2225655291f2SDavid Xu if (pmq) 2226655291f2SDavid Xu *pmq = pn->mn_data; 2227655291f2SDavid Xu return (0); 2228655291f2SDavid Xu } 2229655291f2SDavid Xu 2230655291f2SDavid Xu static __inline int 2231655291f2SDavid Xu getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn, 2232655291f2SDavid Xu struct mqueue **pmq) 2233655291f2SDavid Xu { 22347008be5bSPawel Jakub Dawidek cap_rights_t rights; 22357008be5bSPawel Jakub Dawidek 2236ed5848c8SPawel Jakub Dawidek return _getmq(td, fd, cap_rights_init(&rights, CAP_EVENT), fget, 22377008be5bSPawel Jakub Dawidek fpp, ppn, pmq); 2238655291f2SDavid Xu } 2239655291f2SDavid Xu 2240655291f2SDavid Xu static __inline int 2241655291f2SDavid Xu getmq_read(struct thread *td, int fd, struct file **fpp, 2242655291f2SDavid Xu struct mqfs_node **ppn, struct mqueue **pmq) 2243655291f2SDavid Xu { 22447008be5bSPawel Jakub Dawidek cap_rights_t rights; 22457008be5bSPawel Jakub Dawidek 22467008be5bSPawel Jakub Dawidek return _getmq(td, fd, cap_rights_init(&rights, CAP_READ), fget_read, 22477008be5bSPawel Jakub Dawidek fpp, ppn, pmq); 2248655291f2SDavid Xu } 2249655291f2SDavid Xu 2250655291f2SDavid Xu static __inline int 2251655291f2SDavid Xu getmq_write(struct thread *td, int fd, struct file **fpp, 2252655291f2SDavid Xu struct mqfs_node **ppn, struct mqueue **pmq) 2253655291f2SDavid Xu { 22547008be5bSPawel Jakub Dawidek cap_rights_t rights; 22557008be5bSPawel Jakub Dawidek 22567008be5bSPawel Jakub Dawidek return _getmq(td, fd, cap_rights_init(&rights, CAP_WRITE), fget_write, 22577008be5bSPawel Jakub Dawidek fpp, ppn, pmq); 2258655291f2SDavid Xu } 2259655291f2SDavid Xu 2260afde2b65SKonstantin Belousov static int 2261afde2b65SKonstantin Belousov kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr, 2262afde2b65SKonstantin Belousov struct mq_attr *oattr) 2263655291f2SDavid Xu { 2264655291f2SDavid Xu struct mqueue *mq; 2265655291f2SDavid Xu struct file *fp; 2266397c19d1SJeff Roberson u_int oflag, flag; 2267655291f2SDavid Xu int error; 2268655291f2SDavid Xu 2269afde2b65SKonstantin Belousov if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0) 2270655291f2SDavid Xu return (EINVAL); 2271afde2b65SKonstantin Belousov error = getmq(td, mqd, &fp, NULL, &mq); 2272655291f2SDavid Xu if (error) 2273655291f2SDavid Xu return (error); 2274afde2b65SKonstantin Belousov oattr->mq_maxmsg = mq->mq_maxmsg; 2275afde2b65SKonstantin Belousov oattr->mq_msgsize = mq->mq_msgsize; 2276afde2b65SKonstantin Belousov oattr->mq_curmsgs = mq->mq_curmsgs; 2277afde2b65SKonstantin Belousov if (attr != NULL) { 2278397c19d1SJeff Roberson do { 2279397c19d1SJeff Roberson oflag = flag = fp->f_flag; 2280397c19d1SJeff Roberson flag &= ~O_NONBLOCK; 2281afde2b65SKonstantin Belousov flag |= (attr->mq_flags & O_NONBLOCK); 2282397c19d1SJeff Roberson } while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0); 2283397c19d1SJeff Roberson } else 2284397c19d1SJeff Roberson oflag = fp->f_flag; 2285afde2b65SKonstantin Belousov oattr->mq_flags = (O_NONBLOCK & oflag); 2286655291f2SDavid Xu fdrop(fp, td); 2287afde2b65SKonstantin Belousov return (error); 2288afde2b65SKonstantin Belousov } 2289afde2b65SKonstantin Belousov 2290afde2b65SKonstantin Belousov int 22918451d0ddSKip Macy sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap) 2292afde2b65SKonstantin Belousov { 2293afde2b65SKonstantin Belousov struct mq_attr attr, oattr; 2294afde2b65SKonstantin Belousov int error; 2295afde2b65SKonstantin Belousov 2296afde2b65SKonstantin Belousov if (uap->attr != NULL) { 2297afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr, sizeof(attr)); 2298afde2b65SKonstantin Belousov if (error != 0) 2299afde2b65SKonstantin Belousov return (error); 2300afde2b65SKonstantin Belousov } 2301afde2b65SKonstantin Belousov error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL, 2302afde2b65SKonstantin Belousov &oattr); 2303afde2b65SKonstantin Belousov if (error != 0) 2304afde2b65SKonstantin Belousov return (error); 2305afde2b65SKonstantin Belousov if (uap->oattr != NULL) 2306655291f2SDavid Xu error = copyout(&oattr, uap->oattr, sizeof(oattr)); 2307655291f2SDavid Xu return (error); 2308655291f2SDavid Xu } 2309655291f2SDavid Xu 2310655291f2SDavid Xu int 23118451d0ddSKip Macy sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap) 2312655291f2SDavid Xu { 2313655291f2SDavid Xu struct mqueue *mq; 2314655291f2SDavid Xu struct file *fp; 2315afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2316655291f2SDavid Xu int error; 2317655291f2SDavid Xu int waitok; 2318655291f2SDavid Xu 2319655291f2SDavid Xu error = getmq_read(td, uap->mqd, &fp, NULL, &mq); 2320655291f2SDavid Xu if (error) 2321655291f2SDavid Xu return (error); 2322afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2323afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets, sizeof(ets)); 2324afde2b65SKonstantin Belousov if (error != 0) 2325afde2b65SKonstantin Belousov return (error); 2326afde2b65SKonstantin Belousov abs_timeout = &ets; 2327afde2b65SKonstantin Belousov } else 2328afde2b65SKonstantin Belousov abs_timeout = NULL; 2329655291f2SDavid Xu waitok = !(fp->f_flag & O_NONBLOCK); 2330655291f2SDavid Xu error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len, 2331afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2332655291f2SDavid Xu fdrop(fp, td); 2333655291f2SDavid Xu return (error); 2334655291f2SDavid Xu } 2335655291f2SDavid Xu 2336655291f2SDavid Xu int 23378451d0ddSKip Macy sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap) 2338655291f2SDavid Xu { 2339655291f2SDavid Xu struct mqueue *mq; 2340655291f2SDavid Xu struct file *fp; 2341afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2342655291f2SDavid Xu int error, waitok; 2343655291f2SDavid Xu 2344655291f2SDavid Xu error = getmq_write(td, uap->mqd, &fp, NULL, &mq); 2345655291f2SDavid Xu if (error) 2346655291f2SDavid Xu return (error); 2347afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2348afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets, sizeof(ets)); 2349afde2b65SKonstantin Belousov if (error != 0) 2350afde2b65SKonstantin Belousov return (error); 2351afde2b65SKonstantin Belousov abs_timeout = &ets; 2352afde2b65SKonstantin Belousov } else 2353afde2b65SKonstantin Belousov abs_timeout = NULL; 2354655291f2SDavid Xu waitok = !(fp->f_flag & O_NONBLOCK); 2355655291f2SDavid Xu error = mqueue_send(mq, uap->msg_ptr, uap->msg_len, 2356afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2357655291f2SDavid Xu fdrop(fp, td); 2358655291f2SDavid Xu return (error); 2359655291f2SDavid Xu } 2360655291f2SDavid Xu 2361058262c6SKonstantin Belousov static int 2362058262c6SKonstantin Belousov kern_kmq_notify(struct thread *td, int mqd, struct sigevent *sigev) 2363655291f2SDavid Xu { 23642af0c790SGleb Smirnoff #ifdef CAPABILITIES 23657008be5bSPawel Jakub Dawidek cap_rights_t rights; 23662af0c790SGleb Smirnoff #endif 2367b2f92ef9SDavid Xu struct filedesc *fdp; 2368b2f92ef9SDavid Xu struct proc *p; 2369655291f2SDavid Xu struct mqueue *mq; 2370a9d2f8d8SRobert Watson struct file *fp, *fp2; 2371b2f92ef9SDavid Xu struct mqueue_notifier *nt, *newnt = NULL; 2372655291f2SDavid Xu int error; 2373655291f2SDavid Xu 2374058262c6SKonstantin Belousov if (sigev != NULL) { 2375058262c6SKonstantin Belousov if (sigev->sigev_notify != SIGEV_SIGNAL && 2376058262c6SKonstantin Belousov sigev->sigev_notify != SIGEV_THREAD_ID && 2377058262c6SKonstantin Belousov sigev->sigev_notify != SIGEV_NONE) 2378655291f2SDavid Xu return (EINVAL); 2379058262c6SKonstantin Belousov if ((sigev->sigev_notify == SIGEV_SIGNAL || 2380058262c6SKonstantin Belousov sigev->sigev_notify == SIGEV_THREAD_ID) && 2381058262c6SKonstantin Belousov !_SIG_VALID(sigev->sigev_signo)) 2382655291f2SDavid Xu return (EINVAL); 2383655291f2SDavid Xu } 2384058262c6SKonstantin Belousov p = td->td_proc; 2385058262c6SKonstantin Belousov fdp = td->td_proc->p_fd; 2386058262c6SKonstantin Belousov error = getmq(td, mqd, &fp, NULL, &mq); 2387655291f2SDavid Xu if (error) 2388655291f2SDavid Xu return (error); 2389b2f92ef9SDavid Xu again: 23905e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 2391058262c6SKonstantin Belousov fp2 = fget_locked(fdp, mqd); 2392a9d2f8d8SRobert Watson if (fp2 == NULL) { 2393a9d2f8d8SRobert Watson FILEDESC_SUNLOCK(fdp); 2394a9d2f8d8SRobert Watson error = EBADF; 2395a9d2f8d8SRobert Watson goto out; 2396a9d2f8d8SRobert Watson } 23972609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 23987008be5bSPawel Jakub Dawidek error = cap_check(cap_rights(fdp, mqd), 2399ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)); 2400a9d2f8d8SRobert Watson if (error) { 2401a9d2f8d8SRobert Watson FILEDESC_SUNLOCK(fdp); 2402a9d2f8d8SRobert Watson goto out; 2403a9d2f8d8SRobert Watson } 24042609222aSPawel Jakub Dawidek #endif 2405a9d2f8d8SRobert Watson if (fp2 != fp) { 24065e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 2407b2f92ef9SDavid Xu error = EBADF; 2408b2f92ef9SDavid Xu goto out; 2409b2f92ef9SDavid Xu } 2410655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 24115e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 2412058262c6SKonstantin Belousov if (sigev != NULL) { 2413655291f2SDavid Xu if (mq->mq_notifier != NULL) { 2414655291f2SDavid Xu error = EBUSY; 2415052ea11cSDavid Xu } else { 2416b2f92ef9SDavid Xu PROC_LOCK(p); 2417058262c6SKonstantin Belousov nt = notifier_search(p, mqd); 2418b2f92ef9SDavid Xu if (nt == NULL) { 2419b2f92ef9SDavid Xu if (newnt == NULL) { 2420b2f92ef9SDavid Xu PROC_UNLOCK(p); 2421b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2422b2f92ef9SDavid Xu newnt = notifier_alloc(); 2423b2f92ef9SDavid Xu goto again; 2424b2f92ef9SDavid Xu } 2425b2f92ef9SDavid Xu } 2426b2f92ef9SDavid Xu 2427b2f92ef9SDavid Xu if (nt != NULL) { 2428b2f92ef9SDavid Xu sigqueue_take(&nt->nt_ksi); 2429b2f92ef9SDavid Xu if (newnt != NULL) { 2430b2f92ef9SDavid Xu notifier_free(newnt); 2431b2f92ef9SDavid Xu newnt = NULL; 2432b2f92ef9SDavid Xu } 2433b2f92ef9SDavid Xu } else { 2434b2f92ef9SDavid Xu nt = newnt; 2435b2f92ef9SDavid Xu newnt = NULL; 2436b2f92ef9SDavid Xu ksiginfo_init(&nt->nt_ksi); 2437b2f92ef9SDavid Xu nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT; 2438b2f92ef9SDavid Xu nt->nt_ksi.ksi_code = SI_MESGQ; 2439b2f92ef9SDavid Xu nt->nt_proc = p; 2440058262c6SKonstantin Belousov nt->nt_ksi.ksi_mqd = mqd; 2441b2f92ef9SDavid Xu notifier_insert(p, nt); 2442b2f92ef9SDavid Xu } 2443058262c6SKonstantin Belousov nt->nt_sigev = *sigev; 2444b2f92ef9SDavid Xu mq->mq_notifier = nt; 2445b2f92ef9SDavid Xu PROC_UNLOCK(p); 2446655291f2SDavid Xu /* 2447b2f92ef9SDavid Xu * if there is no receivers and message queue 2448b2f92ef9SDavid Xu * is not empty, we should send notification 2449b2f92ef9SDavid Xu * as soon as possible. 2450655291f2SDavid Xu */ 2451655291f2SDavid Xu if (mq->mq_receivers == 0 && 2452655291f2SDavid Xu !TAILQ_EMPTY(&mq->mq_msgq)) 2453655291f2SDavid Xu mqueue_send_notification(mq); 2454655291f2SDavid Xu } 2455655291f2SDavid Xu } else { 2456058262c6SKonstantin Belousov notifier_remove(p, mq, mqd); 2457655291f2SDavid Xu } 2458655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2459b2f92ef9SDavid Xu 2460b2f92ef9SDavid Xu out: 2461655291f2SDavid Xu fdrop(fp, td); 2462b2f92ef9SDavid Xu if (newnt != NULL) 2463b2f92ef9SDavid Xu notifier_free(newnt); 2464655291f2SDavid Xu return (error); 2465655291f2SDavid Xu } 2466655291f2SDavid Xu 2467058262c6SKonstantin Belousov int 2468058262c6SKonstantin Belousov sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap) 2469058262c6SKonstantin Belousov { 2470058262c6SKonstantin Belousov struct sigevent ev, *evp; 2471058262c6SKonstantin Belousov int error; 2472058262c6SKonstantin Belousov 2473058262c6SKonstantin Belousov if (uap->sigev == NULL) { 2474058262c6SKonstantin Belousov evp = NULL; 2475058262c6SKonstantin Belousov } else { 2476058262c6SKonstantin Belousov error = copyin(uap->sigev, &ev, sizeof(ev)); 2477058262c6SKonstantin Belousov if (error != 0) 2478058262c6SKonstantin Belousov return (error); 2479058262c6SKonstantin Belousov evp = &ev; 2480058262c6SKonstantin Belousov } 2481058262c6SKonstantin Belousov return (kern_kmq_notify(td, uap->mqd, evp)); 2482058262c6SKonstantin Belousov } 2483058262c6SKonstantin Belousov 2484b2f92ef9SDavid Xu static void 2485b2f92ef9SDavid Xu mqueue_fdclose(struct thread *td, int fd, struct file *fp) 2486b2f92ef9SDavid Xu { 2487b2f92ef9SDavid Xu struct filedesc *fdp; 2488b2f92ef9SDavid Xu struct mqueue *mq; 2489b2f92ef9SDavid Xu 2490b2f92ef9SDavid Xu fdp = td->td_proc->p_fd; 24915e3f7694SRobert Watson FILEDESC_LOCK_ASSERT(fdp); 24925e3f7694SRobert Watson 2493b2f92ef9SDavid Xu if (fp->f_ops == &mqueueops) { 2494b2f92ef9SDavid Xu mq = FPTOMQ(fp); 2495b2f92ef9SDavid Xu mtx_lock(&mq->mq_mutex); 2496b2f92ef9SDavid Xu notifier_remove(td->td_proc, mq, fd); 2497b2f92ef9SDavid Xu 2498b2f92ef9SDavid Xu /* have to wakeup thread in same process */ 2499b2f92ef9SDavid Xu if (mq->mq_flags & MQ_RSEL) { 2500b2f92ef9SDavid Xu mq->mq_flags &= ~MQ_RSEL; 250103f70aecSDavid Xu selwakeup(&mq->mq_rsel); 2502b2f92ef9SDavid Xu } 2503b2f92ef9SDavid Xu if (mq->mq_flags & MQ_WSEL) { 2504b2f92ef9SDavid Xu mq->mq_flags &= ~MQ_WSEL; 250503f70aecSDavid Xu selwakeup(&mq->mq_wsel); 2506b2f92ef9SDavid Xu } 2507b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2508b2f92ef9SDavid Xu } 2509b2f92ef9SDavid Xu } 2510b2f92ef9SDavid Xu 2511b2f92ef9SDavid Xu static void 2512b2f92ef9SDavid Xu mq_proc_exit(void *arg __unused, struct proc *p) 2513b2f92ef9SDavid Xu { 2514b2f92ef9SDavid Xu struct filedesc *fdp; 2515b2f92ef9SDavid Xu struct file *fp; 2516b2f92ef9SDavid Xu struct mqueue *mq; 2517b2f92ef9SDavid Xu int i; 2518b2f92ef9SDavid Xu 2519b2f92ef9SDavid Xu fdp = p->p_fd; 25205e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 2521b2f92ef9SDavid Xu for (i = 0; i < fdp->fd_nfiles; ++i) { 2522b2f92ef9SDavid Xu fp = fget_locked(fdp, i); 2523b2f92ef9SDavid Xu if (fp != NULL && fp->f_ops == &mqueueops) { 2524b2f92ef9SDavid Xu mq = FPTOMQ(fp); 2525b2f92ef9SDavid Xu mtx_lock(&mq->mq_mutex); 2526b2f92ef9SDavid Xu notifier_remove(p, FPTOMQ(fp), i); 2527b2f92ef9SDavid Xu mtx_unlock(&mq->mq_mutex); 2528b2f92ef9SDavid Xu } 2529b2f92ef9SDavid Xu } 25305e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 2531b2f92ef9SDavid Xu KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left")); 2532b2f92ef9SDavid Xu } 2533b2f92ef9SDavid Xu 2534655291f2SDavid Xu static int 2535655291f2SDavid Xu mqf_poll(struct file *fp, int events, struct ucred *active_cred, 2536655291f2SDavid Xu struct thread *td) 2537655291f2SDavid Xu { 2538655291f2SDavid Xu struct mqueue *mq = FPTOMQ(fp); 2539655291f2SDavid Xu int revents = 0; 2540655291f2SDavid Xu 2541655291f2SDavid Xu mtx_lock(&mq->mq_mutex); 2542655291f2SDavid Xu if (events & (POLLIN | POLLRDNORM)) { 2543655291f2SDavid Xu if (mq->mq_curmsgs) { 2544655291f2SDavid Xu revents |= events & (POLLIN | POLLRDNORM); 2545655291f2SDavid Xu } else { 2546655291f2SDavid Xu mq->mq_flags |= MQ_RSEL; 2547655291f2SDavid Xu selrecord(td, &mq->mq_rsel); 2548655291f2SDavid Xu } 2549655291f2SDavid Xu } 2550655291f2SDavid Xu if (events & POLLOUT) { 2551655291f2SDavid Xu if (mq->mq_curmsgs < mq->mq_maxmsg) 2552655291f2SDavid Xu revents |= POLLOUT; 2553655291f2SDavid Xu else { 2554655291f2SDavid Xu mq->mq_flags |= MQ_WSEL; 2555655291f2SDavid Xu selrecord(td, &mq->mq_wsel); 2556655291f2SDavid Xu } 2557655291f2SDavid Xu } 2558655291f2SDavid Xu mtx_unlock(&mq->mq_mutex); 2559655291f2SDavid Xu return (revents); 2560655291f2SDavid Xu } 2561655291f2SDavid Xu 2562655291f2SDavid Xu static int 2563655291f2SDavid Xu mqf_close(struct file *fp, struct thread *td) 2564655291f2SDavid Xu { 2565655291f2SDavid Xu struct mqfs_node *pn; 2566655291f2SDavid Xu 2567655291f2SDavid Xu fp->f_ops = &badfileops; 2568b2f92ef9SDavid Xu pn = fp->f_data; 2569655291f2SDavid Xu fp->f_data = NULL; 2570655291f2SDavid Xu sx_xlock(&mqfs_data.mi_lock); 2571655291f2SDavid Xu mqnode_release(pn); 2572655291f2SDavid Xu sx_xunlock(&mqfs_data.mi_lock); 2573655291f2SDavid Xu return (0); 2574655291f2SDavid Xu } 2575655291f2SDavid Xu 2576655291f2SDavid Xu static int 2577655291f2SDavid Xu mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred, 2578655291f2SDavid Xu struct thread *td) 2579655291f2SDavid Xu { 2580b2f92ef9SDavid Xu struct mqfs_node *pn = fp->f_data; 2581655291f2SDavid Xu 2582655291f2SDavid Xu bzero(st, sizeof *st); 25839c00bb91SKonstantin Belousov sx_xlock(&mqfs_data.mi_lock); 2584510ea843SEd Schouten st->st_atim = pn->mn_atime; 2585510ea843SEd Schouten st->st_mtim = pn->mn_mtime; 2586510ea843SEd Schouten st->st_ctim = pn->mn_ctime; 2587510ea843SEd Schouten st->st_birthtim = pn->mn_birth; 2588655291f2SDavid Xu st->st_uid = pn->mn_uid; 2589655291f2SDavid Xu st->st_gid = pn->mn_gid; 2590655291f2SDavid Xu st->st_mode = S_IFIFO | pn->mn_mode; 25919c00bb91SKonstantin Belousov sx_xunlock(&mqfs_data.mi_lock); 2592655291f2SDavid Xu return (0); 2593655291f2SDavid Xu } 2594655291f2SDavid Xu 2595655291f2SDavid Xu static int 25969c00bb91SKonstantin Belousov mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 25979c00bb91SKonstantin Belousov struct thread *td) 25989c00bb91SKonstantin Belousov { 25999c00bb91SKonstantin Belousov struct mqfs_node *pn; 26009c00bb91SKonstantin Belousov int error; 26019c00bb91SKonstantin Belousov 26029c00bb91SKonstantin Belousov error = 0; 26039c00bb91SKonstantin Belousov pn = fp->f_data; 26049c00bb91SKonstantin Belousov sx_xlock(&mqfs_data.mi_lock); 26059c00bb91SKonstantin Belousov error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN, 26069c00bb91SKonstantin Belousov active_cred, NULL); 26079c00bb91SKonstantin Belousov if (error != 0) 26089c00bb91SKonstantin Belousov goto out; 26099c00bb91SKonstantin Belousov pn->mn_mode = mode & ACCESSPERMS; 26109c00bb91SKonstantin Belousov out: 26119c00bb91SKonstantin Belousov sx_xunlock(&mqfs_data.mi_lock); 26129c00bb91SKonstantin Belousov return (error); 26139c00bb91SKonstantin Belousov } 26149c00bb91SKonstantin Belousov 26159c00bb91SKonstantin Belousov static int 26169c00bb91SKonstantin Belousov mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 26179c00bb91SKonstantin Belousov struct thread *td) 26189c00bb91SKonstantin Belousov { 26199c00bb91SKonstantin Belousov struct mqfs_node *pn; 26209c00bb91SKonstantin Belousov int error; 26219c00bb91SKonstantin Belousov 26229c00bb91SKonstantin Belousov error = 0; 26239c00bb91SKonstantin Belousov pn = fp->f_data; 26249c00bb91SKonstantin Belousov sx_xlock(&mqfs_data.mi_lock); 26259c00bb91SKonstantin Belousov if (uid == (uid_t)-1) 26269c00bb91SKonstantin Belousov uid = pn->mn_uid; 26279c00bb91SKonstantin Belousov if (gid == (gid_t)-1) 26289c00bb91SKonstantin Belousov gid = pn->mn_gid; 26299c00bb91SKonstantin Belousov if (((uid != pn->mn_uid && uid != active_cred->cr_uid) || 26309c00bb91SKonstantin Belousov (gid != pn->mn_gid && !groupmember(gid, active_cred))) && 26319c00bb91SKonstantin Belousov (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0))) 26329c00bb91SKonstantin Belousov goto out; 26339c00bb91SKonstantin Belousov pn->mn_uid = uid; 26349c00bb91SKonstantin Belousov pn->mn_gid = gid; 26359c00bb91SKonstantin Belousov out: 26369c00bb91SKonstantin Belousov sx_xunlock(&mqfs_data.mi_lock); 26379c00bb91SKonstantin Belousov return (error); 26389c00bb91SKonstantin Belousov } 26399c00bb91SKonstantin Belousov 26409c00bb91SKonstantin Belousov static int 2641655291f2SDavid Xu mqf_kqfilter(struct file *fp, struct knote *kn) 2642655291f2SDavid Xu { 2643655291f2SDavid Xu struct mqueue *mq = FPTOMQ(fp); 2644655291f2SDavid Xu int error = 0; 2645655291f2SDavid Xu 2646655291f2SDavid Xu if (kn->kn_filter == EVFILT_READ) { 2647655291f2SDavid Xu kn->kn_fop = &mq_rfiltops; 2648655291f2SDavid Xu knlist_add(&mq->mq_rsel.si_note, kn, 0); 2649655291f2SDavid Xu } else if (kn->kn_filter == EVFILT_WRITE) { 2650655291f2SDavid Xu kn->kn_fop = &mq_wfiltops; 2651655291f2SDavid Xu knlist_add(&mq->mq_wsel.si_note, kn, 0); 2652655291f2SDavid Xu } else 2653655291f2SDavid Xu error = EINVAL; 2654655291f2SDavid Xu return (error); 2655655291f2SDavid Xu } 2656655291f2SDavid Xu 2657655291f2SDavid Xu static void 2658655291f2SDavid Xu filt_mqdetach(struct knote *kn) 2659655291f2SDavid Xu { 2660655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2661655291f2SDavid Xu 2662655291f2SDavid Xu if (kn->kn_filter == EVFILT_READ) 2663655291f2SDavid Xu knlist_remove(&mq->mq_rsel.si_note, kn, 0); 2664655291f2SDavid Xu else if (kn->kn_filter == EVFILT_WRITE) 2665655291f2SDavid Xu knlist_remove(&mq->mq_wsel.si_note, kn, 0); 2666655291f2SDavid Xu else 2667655291f2SDavid Xu panic("filt_mqdetach"); 2668655291f2SDavid Xu } 2669655291f2SDavid Xu 2670655291f2SDavid Xu static int 2671655291f2SDavid Xu filt_mqread(struct knote *kn, long hint) 2672655291f2SDavid Xu { 2673655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2674655291f2SDavid Xu 2675655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2676655291f2SDavid Xu return (mq->mq_curmsgs != 0); 2677655291f2SDavid Xu } 2678655291f2SDavid Xu 2679655291f2SDavid Xu static int 2680655291f2SDavid Xu filt_mqwrite(struct knote *kn, long hint) 2681655291f2SDavid Xu { 2682655291f2SDavid Xu struct mqueue *mq = FPTOMQ(kn->kn_fp); 2683655291f2SDavid Xu 2684655291f2SDavid Xu mtx_assert(&mq->mq_mutex, MA_OWNED); 2685655291f2SDavid Xu return (mq->mq_curmsgs < mq->mq_maxmsg); 2686655291f2SDavid Xu } 2687655291f2SDavid Xu 26889696feebSJohn Baldwin static int 26899696feebSJohn Baldwin mqf_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 26909696feebSJohn Baldwin { 26919696feebSJohn Baldwin 26929696feebSJohn Baldwin kif->kf_type = KF_TYPE_MQUEUE; 26939696feebSJohn Baldwin return (0); 26949696feebSJohn Baldwin } 26959696feebSJohn Baldwin 2696655291f2SDavid Xu static struct fileops mqueueops = { 26972d69d0dcSJohn Baldwin .fo_read = invfo_rdwr, 26982d69d0dcSJohn Baldwin .fo_write = invfo_rdwr, 26992d69d0dcSJohn Baldwin .fo_truncate = invfo_truncate, 27002d69d0dcSJohn Baldwin .fo_ioctl = invfo_ioctl, 2701655291f2SDavid Xu .fo_poll = mqf_poll, 2702655291f2SDavid Xu .fo_kqfilter = mqf_kqfilter, 2703655291f2SDavid Xu .fo_stat = mqf_stat, 27042d69d0dcSJohn Baldwin .fo_close = mqf_close, 27059c00bb91SKonstantin Belousov .fo_chmod = mqf_chmod, 27069c00bb91SKonstantin Belousov .fo_chown = mqf_chown, 2707ca04d21dSGleb Smirnoff .fo_sendfile = invfo_sendfile, 27089696feebSJohn Baldwin .fo_fill_kinfo = mqf_fill_kinfo, 2709655291f2SDavid Xu }; 2710655291f2SDavid Xu 2711655291f2SDavid Xu static struct vop_vector mqfs_vnodeops = { 2712655291f2SDavid Xu .vop_default = &default_vnodeops, 2713655291f2SDavid Xu .vop_access = mqfs_access, 2714655291f2SDavid Xu .vop_cachedlookup = mqfs_lookup, 2715655291f2SDavid Xu .vop_lookup = vfs_cache_lookup, 2716655291f2SDavid Xu .vop_reclaim = mqfs_reclaim, 2717655291f2SDavid Xu .vop_create = mqfs_create, 2718655291f2SDavid Xu .vop_remove = mqfs_remove, 2719655291f2SDavid Xu .vop_inactive = mqfs_inactive, 2720655291f2SDavid Xu .vop_open = mqfs_open, 2721655291f2SDavid Xu .vop_close = mqfs_close, 2722655291f2SDavid Xu .vop_getattr = mqfs_getattr, 2723655291f2SDavid Xu .vop_setattr = mqfs_setattr, 2724655291f2SDavid Xu .vop_read = mqfs_read, 2725655291f2SDavid Xu .vop_write = VOP_EOPNOTSUPP, 2726655291f2SDavid Xu .vop_readdir = mqfs_readdir, 2727655291f2SDavid Xu .vop_mkdir = VOP_EOPNOTSUPP, 2728655291f2SDavid Xu .vop_rmdir = VOP_EOPNOTSUPP 2729655291f2SDavid Xu }; 2730655291f2SDavid Xu 2731655291f2SDavid Xu static struct vfsops mqfs_vfsops = { 2732655291f2SDavid Xu .vfs_init = mqfs_init, 2733655291f2SDavid Xu .vfs_uninit = mqfs_uninit, 2734655291f2SDavid Xu .vfs_mount = mqfs_mount, 2735655291f2SDavid Xu .vfs_unmount = mqfs_unmount, 2736655291f2SDavid Xu .vfs_root = mqfs_root, 2737655291f2SDavid Xu .vfs_statfs = mqfs_statfs, 2738655291f2SDavid Xu }; 2739655291f2SDavid Xu 2740afde2b65SKonstantin Belousov static struct vfsconf mqueuefs_vfsconf = { 2741afde2b65SKonstantin Belousov .vfc_version = VFS_VERSION, 2742afde2b65SKonstantin Belousov .vfc_name = "mqueuefs", 2743afde2b65SKonstantin Belousov .vfc_vfsops = &mqfs_vfsops, 2744afde2b65SKonstantin Belousov .vfc_typenum = -1, 2745afde2b65SKonstantin Belousov .vfc_flags = VFCF_SYNTHETIC 2746afde2b65SKonstantin Belousov }; 2747655291f2SDavid Xu 2748afde2b65SKonstantin Belousov static struct syscall_helper_data mq_syscalls[] = { 2749afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_open), 2750afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_setattr), 2751afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_timedsend), 2752afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_timedreceive), 2753afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_notify), 2754afde2b65SKonstantin Belousov SYSCALL_INIT_HELPER(kmq_unlink), 2755afde2b65SKonstantin Belousov SYSCALL_INIT_LAST 2756afde2b65SKonstantin Belousov }; 2757afde2b65SKonstantin Belousov 2758afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32 2759afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32.h> 2760afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h> 2761058262c6SKonstantin Belousov #include <compat/freebsd32/freebsd32_signal.h> 2762afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h> 2763afde2b65SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h> 2764afde2b65SKonstantin Belousov 2765afde2b65SKonstantin Belousov static void 2766afde2b65SKonstantin Belousov mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to) 2767afde2b65SKonstantin Belousov { 2768afde2b65SKonstantin Belousov 2769afde2b65SKonstantin Belousov to->mq_flags = from->mq_flags; 2770afde2b65SKonstantin Belousov to->mq_maxmsg = from->mq_maxmsg; 2771afde2b65SKonstantin Belousov to->mq_msgsize = from->mq_msgsize; 2772afde2b65SKonstantin Belousov to->mq_curmsgs = from->mq_curmsgs; 2773afde2b65SKonstantin Belousov } 2774afde2b65SKonstantin Belousov 2775afde2b65SKonstantin Belousov static void 2776afde2b65SKonstantin Belousov mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to) 2777afde2b65SKonstantin Belousov { 2778afde2b65SKonstantin Belousov 2779afde2b65SKonstantin Belousov to->mq_flags = from->mq_flags; 2780afde2b65SKonstantin Belousov to->mq_maxmsg = from->mq_maxmsg; 2781afde2b65SKonstantin Belousov to->mq_msgsize = from->mq_msgsize; 2782afde2b65SKonstantin Belousov to->mq_curmsgs = from->mq_curmsgs; 2783afde2b65SKonstantin Belousov } 2784afde2b65SKonstantin Belousov 2785afde2b65SKonstantin Belousov int 2786afde2b65SKonstantin Belousov freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap) 2787afde2b65SKonstantin Belousov { 2788afde2b65SKonstantin Belousov struct mq_attr attr; 2789afde2b65SKonstantin Belousov struct mq_attr32 attr32; 2790afde2b65SKonstantin Belousov int flags, error; 2791afde2b65SKonstantin Belousov 27926fbdb9f4SJilles Tjoelker if ((uap->flags & O_ACCMODE) == O_ACCMODE || uap->flags & O_EXEC) 2793afde2b65SKonstantin Belousov return (EINVAL); 2794afde2b65SKonstantin Belousov flags = FFLAGS(uap->flags); 2795afde2b65SKonstantin Belousov if ((flags & O_CREAT) != 0 && uap->attr != NULL) { 2796afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr32, sizeof(attr32)); 2797afde2b65SKonstantin Belousov if (error) 2798afde2b65SKonstantin Belousov return (error); 2799afde2b65SKonstantin Belousov mq_attr_from32(&attr32, &attr); 2800afde2b65SKonstantin Belousov } 2801afde2b65SKonstantin Belousov return (kern_kmq_open(td, uap->path, flags, uap->mode, 2802afde2b65SKonstantin Belousov uap->attr != NULL ? &attr : NULL)); 2803afde2b65SKonstantin Belousov } 2804afde2b65SKonstantin Belousov 2805afde2b65SKonstantin Belousov int 2806afde2b65SKonstantin Belousov freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap) 2807afde2b65SKonstantin Belousov { 2808afde2b65SKonstantin Belousov struct mq_attr attr, oattr; 2809afde2b65SKonstantin Belousov struct mq_attr32 attr32, oattr32; 2810afde2b65SKonstantin Belousov int error; 2811afde2b65SKonstantin Belousov 2812afde2b65SKonstantin Belousov if (uap->attr != NULL) { 2813afde2b65SKonstantin Belousov error = copyin(uap->attr, &attr32, sizeof(attr32)); 2814afde2b65SKonstantin Belousov if (error != 0) 2815afde2b65SKonstantin Belousov return (error); 2816afde2b65SKonstantin Belousov mq_attr_from32(&attr32, &attr); 2817afde2b65SKonstantin Belousov } 2818afde2b65SKonstantin Belousov error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL, 2819afde2b65SKonstantin Belousov &oattr); 2820afde2b65SKonstantin Belousov if (error != 0) 2821afde2b65SKonstantin Belousov return (error); 2822afde2b65SKonstantin Belousov if (uap->oattr != NULL) { 2823afde2b65SKonstantin Belousov mq_attr_to32(&oattr, &oattr32); 2824afde2b65SKonstantin Belousov error = copyout(&oattr32, uap->oattr, sizeof(oattr32)); 2825afde2b65SKonstantin Belousov } 2826afde2b65SKonstantin Belousov return (error); 2827afde2b65SKonstantin Belousov } 2828afde2b65SKonstantin Belousov 2829afde2b65SKonstantin Belousov int 2830afde2b65SKonstantin Belousov freebsd32_kmq_timedsend(struct thread *td, 2831afde2b65SKonstantin Belousov struct freebsd32_kmq_timedsend_args *uap) 2832afde2b65SKonstantin Belousov { 2833afde2b65SKonstantin Belousov struct mqueue *mq; 2834afde2b65SKonstantin Belousov struct file *fp; 2835afde2b65SKonstantin Belousov struct timespec32 ets32; 2836afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2837afde2b65SKonstantin Belousov int error; 2838afde2b65SKonstantin Belousov int waitok; 2839afde2b65SKonstantin Belousov 284028f865b0SPawel Jakub Dawidek error = getmq_write(td, uap->mqd, &fp, NULL, &mq); 2841afde2b65SKonstantin Belousov if (error) 2842afde2b65SKonstantin Belousov return (error); 2843afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2844afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets32, sizeof(ets32)); 2845afde2b65SKonstantin Belousov if (error != 0) 2846afde2b65SKonstantin Belousov return (error); 2847afde2b65SKonstantin Belousov CP(ets32, ets, tv_sec); 2848afde2b65SKonstantin Belousov CP(ets32, ets, tv_nsec); 2849afde2b65SKonstantin Belousov abs_timeout = &ets; 2850afde2b65SKonstantin Belousov } else 2851afde2b65SKonstantin Belousov abs_timeout = NULL; 2852afde2b65SKonstantin Belousov waitok = !(fp->f_flag & O_NONBLOCK); 2853afde2b65SKonstantin Belousov error = mqueue_send(mq, uap->msg_ptr, uap->msg_len, 2854afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2855afde2b65SKonstantin Belousov fdrop(fp, td); 2856afde2b65SKonstantin Belousov return (error); 2857afde2b65SKonstantin Belousov } 2858afde2b65SKonstantin Belousov 2859afde2b65SKonstantin Belousov int 2860afde2b65SKonstantin Belousov freebsd32_kmq_timedreceive(struct thread *td, 2861afde2b65SKonstantin Belousov struct freebsd32_kmq_timedreceive_args *uap) 2862afde2b65SKonstantin Belousov { 2863afde2b65SKonstantin Belousov struct mqueue *mq; 2864afde2b65SKonstantin Belousov struct file *fp; 2865afde2b65SKonstantin Belousov struct timespec32 ets32; 2866afde2b65SKonstantin Belousov struct timespec *abs_timeout, ets; 2867afde2b65SKonstantin Belousov int error, waitok; 2868afde2b65SKonstantin Belousov 286928f865b0SPawel Jakub Dawidek error = getmq_read(td, uap->mqd, &fp, NULL, &mq); 2870afde2b65SKonstantin Belousov if (error) 2871afde2b65SKonstantin Belousov return (error); 2872afde2b65SKonstantin Belousov if (uap->abs_timeout != NULL) { 2873afde2b65SKonstantin Belousov error = copyin(uap->abs_timeout, &ets32, sizeof(ets32)); 2874afde2b65SKonstantin Belousov if (error != 0) 2875afde2b65SKonstantin Belousov return (error); 2876afde2b65SKonstantin Belousov CP(ets32, ets, tv_sec); 2877afde2b65SKonstantin Belousov CP(ets32, ets, tv_nsec); 2878afde2b65SKonstantin Belousov abs_timeout = &ets; 2879afde2b65SKonstantin Belousov } else 2880afde2b65SKonstantin Belousov abs_timeout = NULL; 2881afde2b65SKonstantin Belousov waitok = !(fp->f_flag & O_NONBLOCK); 2882afde2b65SKonstantin Belousov error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len, 2883afde2b65SKonstantin Belousov uap->msg_prio, waitok, abs_timeout); 2884afde2b65SKonstantin Belousov fdrop(fp, td); 2885afde2b65SKonstantin Belousov return (error); 2886afde2b65SKonstantin Belousov } 2887afde2b65SKonstantin Belousov 2888058262c6SKonstantin Belousov int 2889058262c6SKonstantin Belousov freebsd32_kmq_notify(struct thread *td, struct freebsd32_kmq_notify_args *uap) 2890058262c6SKonstantin Belousov { 2891058262c6SKonstantin Belousov struct sigevent ev, *evp; 2892058262c6SKonstantin Belousov struct sigevent32 ev32; 2893058262c6SKonstantin Belousov int error; 2894058262c6SKonstantin Belousov 2895058262c6SKonstantin Belousov if (uap->sigev == NULL) { 2896058262c6SKonstantin Belousov evp = NULL; 2897058262c6SKonstantin Belousov } else { 2898058262c6SKonstantin Belousov error = copyin(uap->sigev, &ev32, sizeof(ev32)); 2899058262c6SKonstantin Belousov if (error != 0) 2900058262c6SKonstantin Belousov return (error); 2901058262c6SKonstantin Belousov error = convert_sigevent32(&ev32, &ev); 2902058262c6SKonstantin Belousov if (error != 0) 2903058262c6SKonstantin Belousov return (error); 2904058262c6SKonstantin Belousov evp = &ev; 2905058262c6SKonstantin Belousov } 2906058262c6SKonstantin Belousov return (kern_kmq_notify(td, uap->mqd, evp)); 2907058262c6SKonstantin Belousov } 2908058262c6SKonstantin Belousov 2909afde2b65SKonstantin Belousov static struct syscall_helper_data mq32_syscalls[] = { 2910afde2b65SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_kmq_open), 2911afde2b65SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr), 2912afde2b65SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend), 2913afde2b65SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive), 2914058262c6SKonstantin Belousov SYSCALL32_INIT_HELPER(freebsd32_kmq_notify), 29158451d0ddSKip Macy SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink), 2916afde2b65SKonstantin Belousov SYSCALL_INIT_LAST 2917afde2b65SKonstantin Belousov }; 2918afde2b65SKonstantin Belousov #endif 2919afde2b65SKonstantin Belousov 2920afde2b65SKonstantin Belousov static int 2921afde2b65SKonstantin Belousov mqinit(void) 2922afde2b65SKonstantin Belousov { 2923afde2b65SKonstantin Belousov int error; 2924afde2b65SKonstantin Belousov 2925e015b1abSMateusz Guzik error = syscall_helper_register(mq_syscalls, SY_THR_STATIC_KLD); 2926afde2b65SKonstantin Belousov if (error != 0) 2927afde2b65SKonstantin Belousov return (error); 2928afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32 2929e015b1abSMateusz Guzik error = syscall32_helper_register(mq32_syscalls, SY_THR_STATIC_KLD); 2930afde2b65SKonstantin Belousov if (error != 0) 2931afde2b65SKonstantin Belousov return (error); 2932afde2b65SKonstantin Belousov #endif 2933afde2b65SKonstantin Belousov return (0); 2934afde2b65SKonstantin Belousov } 2935afde2b65SKonstantin Belousov 2936afde2b65SKonstantin Belousov static int 2937afde2b65SKonstantin Belousov mqunload(void) 2938afde2b65SKonstantin Belousov { 2939afde2b65SKonstantin Belousov 2940afde2b65SKonstantin Belousov #ifdef COMPAT_FREEBSD32 2941afde2b65SKonstantin Belousov syscall32_helper_unregister(mq32_syscalls); 2942afde2b65SKonstantin Belousov #endif 2943afde2b65SKonstantin Belousov syscall_helper_unregister(mq_syscalls); 2944afde2b65SKonstantin Belousov return (0); 2945afde2b65SKonstantin Belousov } 2946afde2b65SKonstantin Belousov 2947afde2b65SKonstantin Belousov static int 2948afde2b65SKonstantin Belousov mq_modload(struct module *module, int cmd, void *arg) 2949afde2b65SKonstantin Belousov { 2950afde2b65SKonstantin Belousov int error = 0; 2951afde2b65SKonstantin Belousov 2952afde2b65SKonstantin Belousov error = vfs_modevent(module, cmd, arg); 2953afde2b65SKonstantin Belousov if (error != 0) 2954afde2b65SKonstantin Belousov return (error); 2955afde2b65SKonstantin Belousov 2956afde2b65SKonstantin Belousov switch (cmd) { 2957afde2b65SKonstantin Belousov case MOD_LOAD: 2958afde2b65SKonstantin Belousov error = mqinit(); 2959afde2b65SKonstantin Belousov if (error != 0) 2960afde2b65SKonstantin Belousov mqunload(); 2961afde2b65SKonstantin Belousov break; 2962afde2b65SKonstantin Belousov case MOD_UNLOAD: 2963afde2b65SKonstantin Belousov error = mqunload(); 2964afde2b65SKonstantin Belousov break; 2965afde2b65SKonstantin Belousov default: 2966afde2b65SKonstantin Belousov break; 2967afde2b65SKonstantin Belousov } 2968afde2b65SKonstantin Belousov return (error); 2969afde2b65SKonstantin Belousov } 2970afde2b65SKonstantin Belousov 2971afde2b65SKonstantin Belousov static moduledata_t mqueuefs_mod = { 2972afde2b65SKonstantin Belousov "mqueuefs", 2973afde2b65SKonstantin Belousov mq_modload, 2974afde2b65SKonstantin Belousov &mqueuefs_vfsconf 2975afde2b65SKonstantin Belousov }; 2976afde2b65SKonstantin Belousov DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE); 297747bf2cf9SDavid Xu MODULE_VERSION(mqueuefs, 1); 2978