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