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