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