2 * POSIX message queues filesystem for Linux.
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
5 * Michal Wronski (michal.wronski@gmail.com)
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
11 * Audit: George Wilson (ltcgcw@us.ibm.com)
13 * This file is released under the GPL.
16 #include <linux/capability.h>
17 #include <linux/init.h>
18 #include <linux/pagemap.h>
19 #include <linux/file.h>
20 #include <linux/mount.h>
21 #include <linux/namei.h>
22 #include <linux/sysctl.h>
23 #include <linux/poll.h>
24 #include <linux/mqueue.h>
25 #include <linux/msg.h>
26 #include <linux/skbuff.h>
27 #include <linux/vmalloc.h>
28 #include <linux/netlink.h>
29 #include <linux/syscalls.h>
30 #include <linux/audit.h>
31 #include <linux/signal.h>
32 #include <linux/mutex.h>
33 #include <linux/nsproxy.h>
34 #include <linux/pid.h>
35 #include <linux/ipc_namespace.h>
36 #include <linux/user_namespace.h>
37 #include <linux/slab.h>
42 #define MQUEUE_MAGIC 0x19800202
43 #define DIRENT_SIZE 20
44 #define FILENT_SIZE 80
50 #define STATE_PENDING 1
53 struct posix_msg_tree_node {
54 struct rb_node rb_node;
55 struct list_head msg_list;
59 struct ext_wait_queue { /* queue of sleeping tasks */
60 struct task_struct *task;
61 struct list_head list;
62 struct msg_msg *msg; /* ptr of loaded message */
63 int state; /* one of STATE_* values */
66 struct mqueue_inode_info {
68 struct inode vfs_inode;
69 wait_queue_head_t wait_q;
71 struct rb_root msg_tree;
74 struct sigevent notify;
75 struct pid* notify_owner;
76 struct user_namespace *notify_user_ns;
77 struct user_struct *user; /* user who created, for accounting */
78 struct sock *notify_sock;
79 struct sk_buff *notify_cookie;
81 /* for tasks waiting for free space and messages, respectively */
82 struct ext_wait_queue e_wait_q[2];
84 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
87 static const struct inode_operations mqueue_dir_inode_operations;
88 static const struct file_operations mqueue_file_operations;
89 static const struct super_operations mqueue_super_ops;
90 static void remove_notification(struct mqueue_inode_info *info);
92 static struct kmem_cache *mqueue_inode_cachep;
94 static struct ctl_table_header * mq_sysctl_table;
96 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
98 return container_of(inode, struct mqueue_inode_info, vfs_inode);
102 * This routine should be called with the mq_lock held.
104 static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
106 return get_ipc_ns(inode->i_sb->s_fs_info);
109 static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
111 struct ipc_namespace *ns;
114 ns = __get_ns_from_inode(inode);
115 spin_unlock(&mq_lock);
119 /* Auxiliary functions to manipulate messages' list */
120 static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
122 struct rb_node **p, *parent = NULL;
123 struct posix_msg_tree_node *leaf;
125 p = &info->msg_tree.rb_node;
128 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
130 if (likely(leaf->priority == msg->m_type))
132 else if (msg->m_type < leaf->priority)
137 leaf = kzalloc(sizeof(*leaf), GFP_ATOMIC);
140 rb_init_node(&leaf->rb_node);
141 INIT_LIST_HEAD(&leaf->msg_list);
142 leaf->priority = msg->m_type;
143 rb_link_node(&leaf->rb_node, parent, p);
144 rb_insert_color(&leaf->rb_node, &info->msg_tree);
145 info->qsize += sizeof(struct posix_msg_tree_node);
147 info->attr.mq_curmsgs++;
148 info->qsize += msg->m_ts;
149 list_add_tail(&msg->m_list, &leaf->msg_list);
153 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
155 struct rb_node **p, *parent = NULL;
156 struct posix_msg_tree_node *leaf;
160 p = &info->msg_tree.rb_node;
164 * During insert, low priorities go to the left and high to the
165 * right. On receive, we want the highest priorities first, so
166 * walk all the way to the right.
171 if (info->attr.mq_curmsgs) {
172 pr_warn_once("Inconsistency in POSIX message queue, "
173 "no tree element, but supposedly messages "
175 info->attr.mq_curmsgs = 0;
179 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
180 if (list_empty(&leaf->msg_list)) {
181 pr_warn_once("Inconsistency in POSIX message queue, "
182 "empty leaf node but we haven't implemented "
183 "lazy leaf delete!\n");
184 rb_erase(&leaf->rb_node, &info->msg_tree);
185 info->qsize -= sizeof(struct posix_msg_tree_node);
189 msg = list_first_entry(&leaf->msg_list,
190 struct msg_msg, m_list);
191 list_del(&msg->m_list);
192 if (list_empty(&leaf->msg_list)) {
193 rb_erase(&leaf->rb_node, &info->msg_tree);
194 info->qsize -= sizeof(struct posix_msg_tree_node);
198 info->attr.mq_curmsgs--;
199 info->qsize -= msg->m_ts;
203 static struct inode *mqueue_get_inode(struct super_block *sb,
204 struct ipc_namespace *ipc_ns, umode_t mode,
205 struct mq_attr *attr)
207 struct user_struct *u = current_user();
211 inode = new_inode(sb);
215 inode->i_ino = get_next_ino();
216 inode->i_mode = mode;
217 inode->i_uid = current_fsuid();
218 inode->i_gid = current_fsgid();
219 inode->i_mtime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
222 struct mqueue_inode_info *info;
223 unsigned long mq_bytes, mq_treesize;
225 inode->i_fop = &mqueue_file_operations;
226 inode->i_size = FILENT_SIZE;
227 /* mqueue specific info */
228 info = MQUEUE_I(inode);
229 spin_lock_init(&info->lock);
230 init_waitqueue_head(&info->wait_q);
231 INIT_LIST_HEAD(&info->e_wait_q[0].list);
232 INIT_LIST_HEAD(&info->e_wait_q[1].list);
233 info->notify_owner = NULL;
234 info->notify_user_ns = NULL;
236 info->user = NULL; /* set when all is ok */
237 info->msg_tree = RB_ROOT;
238 memset(&info->attr, 0, sizeof(info->attr));
239 info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max,
240 ipc_ns->mq_msg_default);
241 info->attr.mq_msgsize = min(ipc_ns->mq_msgsize_max,
242 ipc_ns->mq_msgsize_default);
244 info->attr.mq_maxmsg = attr->mq_maxmsg;
245 info->attr.mq_msgsize = attr->mq_msgsize;
248 * We used to allocate a static array of pointers and account
249 * the size of that array as well as one msg_msg struct per
250 * possible message into the queue size. That's no longer
251 * accurate as the queue is now an rbtree and will grow and
252 * shrink depending on usage patterns. We can, however, still
253 * account one msg_msg struct per message, but the nodes are
254 * allocated depending on priority usage, and most programs
255 * only use one, or a handful, of priorities. However, since
256 * this is pinned memory, we need to assume worst case, so
257 * that means the min(mq_maxmsg, max_priorities) * struct
258 * posix_msg_tree_node.
260 mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
261 min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
262 sizeof(struct posix_msg_tree_node);
264 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
265 info->attr.mq_msgsize);
268 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
269 u->mq_bytes + mq_bytes > rlimit(RLIMIT_MSGQUEUE)) {
270 spin_unlock(&mq_lock);
271 /* mqueue_evict_inode() releases info->messages */
275 u->mq_bytes += mq_bytes;
276 spin_unlock(&mq_lock);
279 info->user = get_uid(u);
280 } else if (S_ISDIR(mode)) {
282 /* Some things misbehave if size == 0 on a directory */
283 inode->i_size = 2 * DIRENT_SIZE;
284 inode->i_op = &mqueue_dir_inode_operations;
285 inode->i_fop = &simple_dir_operations;
295 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
298 struct ipc_namespace *ns = data;
300 sb->s_blocksize = PAGE_CACHE_SIZE;
301 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
302 sb->s_magic = MQUEUE_MAGIC;
303 sb->s_op = &mqueue_super_ops;
305 inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
307 return PTR_ERR(inode);
309 sb->s_root = d_make_root(inode);
315 static struct dentry *mqueue_mount(struct file_system_type *fs_type,
316 int flags, const char *dev_name,
319 if (!(flags & MS_KERNMOUNT))
320 data = current->nsproxy->ipc_ns;
321 return mount_ns(fs_type, flags, data, mqueue_fill_super);
324 static void init_once(void *foo)
326 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
328 inode_init_once(&p->vfs_inode);
331 static struct inode *mqueue_alloc_inode(struct super_block *sb)
333 struct mqueue_inode_info *ei;
335 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
338 return &ei->vfs_inode;
341 static void mqueue_i_callback(struct rcu_head *head)
343 struct inode *inode = container_of(head, struct inode, i_rcu);
344 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
347 static void mqueue_destroy_inode(struct inode *inode)
349 call_rcu(&inode->i_rcu, mqueue_i_callback);
352 static void mqueue_evict_inode(struct inode *inode)
354 struct mqueue_inode_info *info;
355 struct user_struct *user;
356 unsigned long mq_bytes, mq_treesize;
357 struct ipc_namespace *ipc_ns;
362 if (S_ISDIR(inode->i_mode))
365 ipc_ns = get_ns_from_inode(inode);
366 info = MQUEUE_I(inode);
367 spin_lock(&info->lock);
368 while ((msg = msg_get(info)) != NULL)
370 spin_unlock(&info->lock);
372 /* Total amount of bytes accounted for the mqueue */
373 mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
374 min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
375 sizeof(struct posix_msg_tree_node);
377 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
378 info->attr.mq_msgsize);
383 user->mq_bytes -= mq_bytes;
385 * get_ns_from_inode() ensures that the
386 * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
387 * to which we now hold a reference, or it is NULL.
388 * We can't put it here under mq_lock, though.
391 ipc_ns->mq_queues_count--;
392 spin_unlock(&mq_lock);
399 static int mqueue_create(struct inode *dir, struct dentry *dentry,
400 umode_t mode, struct nameidata *nd)
403 struct mq_attr *attr = dentry->d_fsdata;
405 struct ipc_namespace *ipc_ns;
408 ipc_ns = __get_ns_from_inode(dir);
413 if (ipc_ns->mq_queues_count >= HARD_QUEUESMAX ||
414 (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
415 !capable(CAP_SYS_RESOURCE))) {
419 ipc_ns->mq_queues_count++;
420 spin_unlock(&mq_lock);
422 inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
424 error = PTR_ERR(inode);
426 ipc_ns->mq_queues_count--;
431 dir->i_size += DIRENT_SIZE;
432 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
434 d_instantiate(dentry, inode);
438 spin_unlock(&mq_lock);
444 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
446 struct inode *inode = dentry->d_inode;
448 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
449 dir->i_size -= DIRENT_SIZE;
456 * This is routine for system read from queue file.
457 * To avoid mess with doing here some sort of mq_receive we allow
458 * to read only queue size & notification info (the only values
459 * that are interesting from user point of view and aren't accessible
460 * through std routines)
462 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
463 size_t count, loff_t *off)
465 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
466 char buffer[FILENT_SIZE];
469 spin_lock(&info->lock);
470 snprintf(buffer, sizeof(buffer),
471 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
473 info->notify_owner ? info->notify.sigev_notify : 0,
474 (info->notify_owner &&
475 info->notify.sigev_notify == SIGEV_SIGNAL) ?
476 info->notify.sigev_signo : 0,
477 pid_vnr(info->notify_owner));
478 spin_unlock(&info->lock);
479 buffer[sizeof(buffer)-1] = '\0';
481 ret = simple_read_from_buffer(u_data, count, off, buffer,
486 filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
490 static int mqueue_flush_file(struct file *filp, fl_owner_t id)
492 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
494 spin_lock(&info->lock);
495 if (task_tgid(current) == info->notify_owner)
496 remove_notification(info);
498 spin_unlock(&info->lock);
502 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
504 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
507 poll_wait(filp, &info->wait_q, poll_tab);
509 spin_lock(&info->lock);
510 if (info->attr.mq_curmsgs)
511 retval = POLLIN | POLLRDNORM;
513 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
514 retval |= POLLOUT | POLLWRNORM;
515 spin_unlock(&info->lock);
520 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
521 static void wq_add(struct mqueue_inode_info *info, int sr,
522 struct ext_wait_queue *ewp)
524 struct ext_wait_queue *walk;
528 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
529 if (walk->task->static_prio <= current->static_prio) {
530 list_add_tail(&ewp->list, &walk->list);
534 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
538 * Puts current task to sleep. Caller must hold queue lock. After return
542 static int wq_sleep(struct mqueue_inode_info *info, int sr,
543 ktime_t *timeout, struct ext_wait_queue *ewp)
548 wq_add(info, sr, ewp);
551 set_current_state(TASK_INTERRUPTIBLE);
553 spin_unlock(&info->lock);
554 time = schedule_hrtimeout_range_clock(timeout, 0,
555 HRTIMER_MODE_ABS, CLOCK_REALTIME);
557 while (ewp->state == STATE_PENDING)
560 if (ewp->state == STATE_READY) {
564 spin_lock(&info->lock);
565 if (ewp->state == STATE_READY) {
569 if (signal_pending(current)) {
570 retval = -ERESTARTSYS;
578 list_del(&ewp->list);
580 spin_unlock(&info->lock);
586 * Returns waiting task that should be serviced first or NULL if none exists
588 static struct ext_wait_queue *wq_get_first_waiter(
589 struct mqueue_inode_info *info, int sr)
591 struct list_head *ptr;
593 ptr = info->e_wait_q[sr].list.prev;
594 if (ptr == &info->e_wait_q[sr].list)
596 return list_entry(ptr, struct ext_wait_queue, list);
600 static inline void set_cookie(struct sk_buff *skb, char code)
602 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
606 * The next function is only to split too long sys_mq_timedsend
608 static void __do_notify(struct mqueue_inode_info *info)
611 * invoked when there is registered process and there isn't process
612 * waiting synchronously for message AND state of queue changed from
613 * empty to not empty. Here we are sure that no one is waiting
615 if (info->notify_owner &&
616 info->attr.mq_curmsgs == 1) {
617 struct siginfo sig_i;
618 switch (info->notify.sigev_notify) {
624 sig_i.si_signo = info->notify.sigev_signo;
626 sig_i.si_code = SI_MESGQ;
627 sig_i.si_value = info->notify.sigev_value;
628 /* map current pid/uid into info->owner's namespaces */
630 sig_i.si_pid = task_tgid_nr_ns(current,
631 ns_of_pid(info->notify_owner));
632 sig_i.si_uid = from_kuid_munged(info->notify_user_ns, current_uid());
635 kill_pid_info(info->notify.sigev_signo,
636 &sig_i, info->notify_owner);
639 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
640 netlink_sendskb(info->notify_sock, info->notify_cookie);
643 /* after notification unregisters process */
644 put_pid(info->notify_owner);
645 put_user_ns(info->notify_user_ns);
646 info->notify_owner = NULL;
647 info->notify_user_ns = NULL;
649 wake_up(&info->wait_q);
652 static int prepare_timeout(const struct timespec __user *u_abs_timeout,
653 ktime_t *expires, struct timespec *ts)
655 if (copy_from_user(ts, u_abs_timeout, sizeof(struct timespec)))
657 if (!timespec_valid(ts))
660 *expires = timespec_to_ktime(*ts);
664 static void remove_notification(struct mqueue_inode_info *info)
666 if (info->notify_owner != NULL &&
667 info->notify.sigev_notify == SIGEV_THREAD) {
668 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
669 netlink_sendskb(info->notify_sock, info->notify_cookie);
671 put_pid(info->notify_owner);
672 put_user_ns(info->notify_user_ns);
673 info->notify_owner = NULL;
674 info->notify_user_ns = NULL;
677 static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
680 unsigned long total_size;
682 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
684 if (capable(CAP_SYS_RESOURCE)) {
685 if (attr->mq_maxmsg > HARD_MSGMAX ||
686 attr->mq_msgsize > HARD_MSGSIZEMAX)
689 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
690 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
693 /* check for overflow */
694 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
696 mq_treesize = attr->mq_maxmsg * sizeof(struct msg_msg) +
697 min_t(unsigned int, attr->mq_maxmsg, MQ_PRIO_MAX) *
698 sizeof(struct posix_msg_tree_node);
699 total_size = attr->mq_maxmsg * attr->mq_msgsize;
700 if (total_size + mq_treesize < total_size)
706 * Invoked when creating a new queue via sys_mq_open
708 static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
709 struct dentry *dentry, int oflag, umode_t mode,
710 struct mq_attr *attr)
712 const struct cred *cred = current_cred();
717 if (!mq_attr_ok(ipc_ns, attr)) {
721 /* store for use during create */
722 dentry->d_fsdata = attr;
725 mode &= ~current_umask();
726 ret = mnt_want_write(ipc_ns->mq_mnt);
729 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
730 dentry->d_fsdata = NULL;
734 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
736 * dentry_open() took a persistent mnt_want_write(),
737 * so we can now drop this one.
739 mnt_drop_write(ipc_ns->mq_mnt);
743 mnt_drop_write(ipc_ns->mq_mnt);
746 mntput(ipc_ns->mq_mnt);
750 /* Opens existing queue */
751 static struct file *do_open(struct ipc_namespace *ipc_ns,
752 struct dentry *dentry, int oflag)
755 const struct cred *cred = current_cred();
757 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
758 MAY_READ | MAY_WRITE };
760 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
765 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
770 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
774 mntput(ipc_ns->mq_mnt);
778 SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
779 struct mq_attr __user *, u_attr)
781 struct dentry *dentry;
786 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
788 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
791 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
793 if (IS_ERR(name = getname(u_name)))
794 return PTR_ERR(name);
796 fd = get_unused_fd_flags(O_CLOEXEC);
800 mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
801 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
802 if (IS_ERR(dentry)) {
803 error = PTR_ERR(dentry);
806 mntget(ipc_ns->mq_mnt);
808 if (oflag & O_CREAT) {
809 if (dentry->d_inode) { /* entry already exists */
810 audit_inode(name, dentry);
811 if (oflag & O_EXCL) {
815 filp = do_open(ipc_ns, dentry, oflag);
817 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
819 u_attr ? &attr : NULL);
822 if (!dentry->d_inode) {
826 audit_inode(name, dentry);
827 filp = do_open(ipc_ns, dentry, oflag);
831 error = PTR_ERR(filp);
835 fd_install(fd, filp);
840 mntput(ipc_ns->mq_mnt);
845 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
851 SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
855 struct dentry *dentry;
856 struct inode *inode = NULL;
857 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
859 name = getname(u_name);
861 return PTR_ERR(name);
863 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
865 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
866 if (IS_ERR(dentry)) {
867 err = PTR_ERR(dentry);
871 if (!dentry->d_inode) {
876 inode = dentry->d_inode;
879 err = mnt_want_write(ipc_ns->mq_mnt);
882 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
883 mnt_drop_write(ipc_ns->mq_mnt);
888 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
896 /* Pipelined send and receive functions.
898 * If a receiver finds no waiting message, then it registers itself in the
899 * list of waiting receivers. A sender checks that list before adding the new
900 * message into the message array. If there is a waiting receiver, then it
901 * bypasses the message array and directly hands the message over to the
903 * The receiver accepts the message and returns without grabbing the queue
904 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
905 * are necessary. The same algorithm is used for sysv semaphores, see
906 * ipc/sem.c for more details.
908 * The same algorithm is used for senders.
911 /* pipelined_send() - send a message directly to the task waiting in
912 * sys_mq_timedreceive() (without inserting message into a queue).
914 static inline void pipelined_send(struct mqueue_inode_info *info,
915 struct msg_msg *message,
916 struct ext_wait_queue *receiver)
918 receiver->msg = message;
919 list_del(&receiver->list);
920 receiver->state = STATE_PENDING;
921 wake_up_process(receiver->task);
923 receiver->state = STATE_READY;
926 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
927 * gets its message and put to the queue (we have one free place for sure). */
928 static inline void pipelined_receive(struct mqueue_inode_info *info)
930 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
934 wake_up_interruptible(&info->wait_q);
937 if (msg_insert(sender->msg, info))
939 list_del(&sender->list);
940 sender->state = STATE_PENDING;
941 wake_up_process(sender->task);
943 sender->state = STATE_READY;
946 SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
947 size_t, msg_len, unsigned int, msg_prio,
948 const struct timespec __user *, u_abs_timeout)
952 struct ext_wait_queue wait;
953 struct ext_wait_queue *receiver;
954 struct msg_msg *msg_ptr;
955 struct mqueue_inode_info *info;
956 ktime_t expires, *timeout = NULL;
961 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
967 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
970 audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL);
973 if (unlikely(!filp)) {
978 inode = filp->f_path.dentry->d_inode;
979 if (unlikely(filp->f_op != &mqueue_file_operations)) {
983 info = MQUEUE_I(inode);
984 audit_inode(NULL, filp->f_path.dentry);
986 if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
991 if (unlikely(msg_len > info->attr.mq_msgsize)) {
996 /* First try to allocate memory, before doing anything with
997 * existing queues. */
998 msg_ptr = load_msg(u_msg_ptr, msg_len);
999 if (IS_ERR(msg_ptr)) {
1000 ret = PTR_ERR(msg_ptr);
1003 msg_ptr->m_ts = msg_len;
1004 msg_ptr->m_type = msg_prio;
1006 spin_lock(&info->lock);
1008 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
1009 if (filp->f_flags & O_NONBLOCK) {
1010 spin_unlock(&info->lock);
1013 wait.task = current;
1014 wait.msg = (void *) msg_ptr;
1015 wait.state = STATE_NONE;
1016 ret = wq_sleep(info, SEND, timeout, &wait);
1021 receiver = wq_get_first_waiter(info, RECV);
1023 pipelined_send(info, msg_ptr, receiver);
1025 /* adds message to the queue */
1026 if (msg_insert(msg_ptr, info)) {
1029 spin_unlock(&info->lock);
1034 inode->i_atime = inode->i_mtime = inode->i_ctime =
1036 spin_unlock(&info->lock);
1045 SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
1046 size_t, msg_len, unsigned int __user *, u_msg_prio,
1047 const struct timespec __user *, u_abs_timeout)
1050 struct msg_msg *msg_ptr;
1052 struct inode *inode;
1053 struct mqueue_inode_info *info;
1054 struct ext_wait_queue wait;
1055 ktime_t expires, *timeout = NULL;
1058 if (u_abs_timeout) {
1059 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
1065 audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL);
1068 if (unlikely(!filp)) {
1073 inode = filp->f_path.dentry->d_inode;
1074 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1078 info = MQUEUE_I(inode);
1079 audit_inode(NULL, filp->f_path.dentry);
1081 if (unlikely(!(filp->f_mode & FMODE_READ))) {
1086 /* checks if buffer is big enough */
1087 if (unlikely(msg_len < info->attr.mq_msgsize)) {
1092 spin_lock(&info->lock);
1093 if (info->attr.mq_curmsgs == 0) {
1094 if (filp->f_flags & O_NONBLOCK) {
1095 spin_unlock(&info->lock);
1098 wait.task = current;
1099 wait.state = STATE_NONE;
1100 ret = wq_sleep(info, RECV, timeout, &wait);
1104 msg_ptr = msg_get(info);
1106 inode->i_atime = inode->i_mtime = inode->i_ctime =
1109 /* There is now free space in queue. */
1110 pipelined_receive(info);
1111 spin_unlock(&info->lock);
1115 ret = msg_ptr->m_ts;
1117 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
1118 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
1130 * Notes: the case when user wants us to deregister (with NULL as pointer)
1131 * and he isn't currently owner of notification, will be silently discarded.
1132 * It isn't explicitly defined in the POSIX.
1134 SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1135 const struct sigevent __user *, u_notification)
1140 struct inode *inode;
1141 struct sigevent notification;
1142 struct mqueue_inode_info *info;
1145 if (u_notification) {
1146 if (copy_from_user(¬ification, u_notification,
1147 sizeof(struct sigevent)))
1151 audit_mq_notify(mqdes, u_notification ? ¬ification : NULL);
1155 if (u_notification != NULL) {
1156 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1157 notification.sigev_notify != SIGEV_SIGNAL &&
1158 notification.sigev_notify != SIGEV_THREAD))
1160 if (notification.sigev_notify == SIGEV_SIGNAL &&
1161 !valid_signal(notification.sigev_signo)) {
1164 if (notification.sigev_notify == SIGEV_THREAD) {
1167 /* create the notify skb */
1168 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1173 if (copy_from_user(nc->data,
1174 notification.sigev_value.sival_ptr,
1175 NOTIFY_COOKIE_LEN)) {
1180 /* TODO: add a header? */
1181 skb_put(nc, NOTIFY_COOKIE_LEN);
1182 /* and attach it to the socket */
1184 filp = fget(notification.sigev_signo);
1189 sock = netlink_getsockbyfilp(filp);
1192 ret = PTR_ERR(sock);
1197 timeo = MAX_SCHEDULE_TIMEOUT;
1198 ret = netlink_attachskb(sock, nc, &timeo, NULL);
1215 inode = filp->f_path.dentry->d_inode;
1216 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1220 info = MQUEUE_I(inode);
1223 spin_lock(&info->lock);
1224 if (u_notification == NULL) {
1225 if (info->notify_owner == task_tgid(current)) {
1226 remove_notification(info);
1227 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1229 } else if (info->notify_owner != NULL) {
1232 switch (notification.sigev_notify) {
1234 info->notify.sigev_notify = SIGEV_NONE;
1237 info->notify_sock = sock;
1238 info->notify_cookie = nc;
1241 info->notify.sigev_notify = SIGEV_THREAD;
1244 info->notify.sigev_signo = notification.sigev_signo;
1245 info->notify.sigev_value = notification.sigev_value;
1246 info->notify.sigev_notify = SIGEV_SIGNAL;
1250 info->notify_owner = get_pid(task_tgid(current));
1251 info->notify_user_ns = get_user_ns(current_user_ns());
1252 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1254 spin_unlock(&info->lock);
1259 netlink_detachskb(sock, nc);
1266 SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1267 const struct mq_attr __user *, u_mqstat,
1268 struct mq_attr __user *, u_omqstat)
1271 struct mq_attr mqstat, omqstat;
1273 struct inode *inode;
1274 struct mqueue_inode_info *info;
1276 if (u_mqstat != NULL) {
1277 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1279 if (mqstat.mq_flags & (~O_NONBLOCK))
1289 inode = filp->f_path.dentry->d_inode;
1290 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1294 info = MQUEUE_I(inode);
1296 spin_lock(&info->lock);
1298 omqstat = info->attr;
1299 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1301 audit_mq_getsetattr(mqdes, &mqstat);
1302 spin_lock(&filp->f_lock);
1303 if (mqstat.mq_flags & O_NONBLOCK)
1304 filp->f_flags |= O_NONBLOCK;
1306 filp->f_flags &= ~O_NONBLOCK;
1307 spin_unlock(&filp->f_lock);
1309 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1312 spin_unlock(&info->lock);
1315 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1316 sizeof(struct mq_attr)))
1325 static const struct inode_operations mqueue_dir_inode_operations = {
1326 .lookup = simple_lookup,
1327 .create = mqueue_create,
1328 .unlink = mqueue_unlink,
1331 static const struct file_operations mqueue_file_operations = {
1332 .flush = mqueue_flush_file,
1333 .poll = mqueue_poll_file,
1334 .read = mqueue_read_file,
1335 .llseek = default_llseek,
1338 static const struct super_operations mqueue_super_ops = {
1339 .alloc_inode = mqueue_alloc_inode,
1340 .destroy_inode = mqueue_destroy_inode,
1341 .evict_inode = mqueue_evict_inode,
1342 .statfs = simple_statfs,
1345 static struct file_system_type mqueue_fs_type = {
1347 .mount = mqueue_mount,
1348 .kill_sb = kill_litter_super,
1351 int mq_init_ns(struct ipc_namespace *ns)
1353 ns->mq_queues_count = 0;
1354 ns->mq_queues_max = DFLT_QUEUESMAX;
1355 ns->mq_msg_max = DFLT_MSGMAX;
1356 ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
1357 ns->mq_msg_default = DFLT_MSG;
1358 ns->mq_msgsize_default = DFLT_MSGSIZE;
1360 ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
1361 if (IS_ERR(ns->mq_mnt)) {
1362 int err = PTR_ERR(ns->mq_mnt);
1369 void mq_clear_sbinfo(struct ipc_namespace *ns)
1371 ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1374 void mq_put_mnt(struct ipc_namespace *ns)
1376 kern_unmount(ns->mq_mnt);
1379 static int __init init_mqueue_fs(void)
1383 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1384 sizeof(struct mqueue_inode_info), 0,
1385 SLAB_HWCACHE_ALIGN, init_once);
1386 if (mqueue_inode_cachep == NULL)
1389 /* ignore failures - they are not fatal */
1390 mq_sysctl_table = mq_register_sysctl_table();
1392 error = register_filesystem(&mqueue_fs_type);
1396 spin_lock_init(&mq_lock);
1398 error = mq_init_ns(&init_ipc_ns);
1400 goto out_filesystem;
1405 unregister_filesystem(&mqueue_fs_type);
1407 if (mq_sysctl_table)
1408 unregister_sysctl_table(mq_sysctl_table);
1409 kmem_cache_destroy(mqueue_inode_cachep);
1413 __initcall(init_mqueue_fs);