2 * POSIX message queues filesystem for Linux.
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
5 * Michal Wronski (Michal.Wronski@motorola.com)
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
11 * This file is released under the GPL.
14 #include <linux/capability.h>
15 #include <linux/init.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/mount.h>
19 #include <linux/namei.h>
20 #include <linux/sysctl.h>
21 #include <linux/poll.h>
22 #include <linux/mqueue.h>
23 #include <linux/msg.h>
24 #include <linux/skbuff.h>
25 #include <linux/netlink.h>
26 #include <linux/syscalls.h>
27 #include <linux/signal.h>
28 #include <linux/mutex.h>
29 #include <linux/vs_context.h>
30 #include <linux/vs_limit.h>
35 #define MQUEUE_MAGIC 0x19800202
36 #define DIRENT_SIZE 20
37 #define FILENT_SIZE 80
43 #define STATE_PENDING 1
48 #define CTL_QUEUESMAX 2
50 #define CTL_MSGSIZEMAX 4
53 #define DFLT_QUEUESMAX 256 /* max number of message queues */
54 #define DFLT_MSGMAX 10 /* max number of messages in each queue */
55 #define HARD_MSGMAX (131072/sizeof(void*))
56 #define DFLT_MSGSIZEMAX 8192 /* max message size */
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 msg_msg **messages;
74 struct sigevent notify;
76 struct user_struct *user; /* user who created, for accounting */
77 struct sock *notify_sock;
78 struct sk_buff *notify_cookie;
80 /* for tasks waiting for free space and messages, respectively */
81 struct ext_wait_queue e_wait_q[2];
83 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
86 static struct inode_operations mqueue_dir_inode_operations;
87 static struct file_operations mqueue_file_operations;
88 static struct super_operations mqueue_super_ops;
89 static void remove_notification(struct mqueue_inode_info *info);
91 static spinlock_t mq_lock;
92 static kmem_cache_t *mqueue_inode_cachep;
93 static struct vfsmount *mqueue_mnt;
95 static unsigned int queues_count;
96 static unsigned int queues_max = DFLT_QUEUESMAX;
97 static unsigned int msg_max = DFLT_MSGMAX;
98 static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
100 static struct ctl_table_header * mq_sysctl_table;
102 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
104 return container_of(inode, struct mqueue_inode_info, vfs_inode);
107 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
108 struct mq_attr *attr)
112 inode = new_inode(sb);
114 inode->i_mode = mode;
115 inode->i_uid = current->fsuid;
116 inode->i_gid = current->fsgid;
117 inode->i_blksize = PAGE_CACHE_SIZE;
119 inode->i_mtime = inode->i_ctime = inode->i_atime =
123 struct mqueue_inode_info *info;
124 struct task_struct *p = current;
125 struct user_struct *u = p->user;
126 unsigned long mq_bytes, mq_msg_tblsz;
128 inode->i_fop = &mqueue_file_operations;
129 inode->i_size = FILENT_SIZE;
130 /* mqueue specific info */
131 info = MQUEUE_I(inode);
132 spin_lock_init(&info->lock);
133 init_waitqueue_head(&info->wait_q);
134 INIT_LIST_HEAD(&info->e_wait_q[0].list);
135 INIT_LIST_HEAD(&info->e_wait_q[1].list);
136 info->messages = NULL;
137 info->notify_owner = 0;
139 info->user = NULL; /* set when all is ok */
140 memset(&info->attr, 0, sizeof(info->attr));
141 info->attr.mq_maxmsg = DFLT_MSGMAX;
142 info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
144 info->attr.mq_maxmsg = attr->mq_maxmsg;
145 info->attr.mq_msgsize = attr->mq_msgsize;
147 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
148 mq_bytes = (mq_msg_tblsz +
149 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
152 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
153 u->mq_bytes + mq_bytes >
154 p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur ||
155 !vx_ipcmsg_avail(p->vx_info, mq_bytes)) {
156 spin_unlock(&mq_lock);
159 u->mq_bytes += mq_bytes;
160 vx_ipcmsg_add(p->vx_info, u, mq_bytes);
161 spin_unlock(&mq_lock);
163 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
164 if (!info->messages) {
166 u->mq_bytes -= mq_bytes;
167 vx_ipcmsg_sub(p->vx_info, u, mq_bytes);
168 spin_unlock(&mq_lock);
172 info->user = get_uid(u);
173 } else if (S_ISDIR(mode)) {
175 /* Some things misbehave if size == 0 on a directory */
176 inode->i_size = 2 * DIRENT_SIZE;
177 inode->i_op = &mqueue_dir_inode_operations;
178 inode->i_fop = &simple_dir_operations;
183 make_bad_inode(inode);
188 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
192 sb->s_blocksize = PAGE_CACHE_SIZE;
193 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
194 sb->s_magic = MQUEUE_MAGIC;
195 sb->s_op = &mqueue_super_ops;
197 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
201 sb->s_root = d_alloc_root(inode);
210 static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
211 int flags, const char *dev_name,
214 return get_sb_single(fs_type, flags, data, mqueue_fill_super);
217 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
219 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
221 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
222 SLAB_CTOR_CONSTRUCTOR)
223 inode_init_once(&p->vfs_inode);
226 static struct inode *mqueue_alloc_inode(struct super_block *sb)
228 struct mqueue_inode_info *ei;
230 ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
233 return &ei->vfs_inode;
236 static void mqueue_destroy_inode(struct inode *inode)
238 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
241 static void mqueue_delete_inode(struct inode *inode)
243 struct mqueue_inode_info *info;
244 struct user_struct *user;
245 unsigned long mq_bytes;
248 if (S_ISDIR(inode->i_mode)) {
252 info = MQUEUE_I(inode);
253 spin_lock(&info->lock);
254 for (i = 0; i < info->attr.mq_curmsgs; i++)
255 free_msg(info->messages[i]);
256 kfree(info->messages);
257 spin_unlock(&info->lock);
261 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
262 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
265 struct vx_info *vxi = lookup_vx_info(user->xid);
268 user->mq_bytes -= mq_bytes;
269 vx_ipcmsg_sub(vxi, user, mq_bytes);
271 spin_unlock(&mq_lock);
277 static int mqueue_create(struct inode *dir, struct dentry *dentry,
278 int mode, struct nameidata *nd)
281 struct mq_attr *attr = dentry->d_fsdata;
285 if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
290 spin_unlock(&mq_lock);
292 inode = mqueue_get_inode(dir->i_sb, mode, attr);
300 dir->i_size += DIRENT_SIZE;
301 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
303 d_instantiate(dentry, inode);
307 spin_unlock(&mq_lock);
311 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
313 struct inode *inode = dentry->d_inode;
315 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
316 dir->i_size -= DIRENT_SIZE;
323 * This is routine for system read from queue file.
324 * To avoid mess with doing here some sort of mq_receive we allow
325 * to read only queue size & notification info (the only values
326 * that are interesting from user point of view and aren't accessible
327 * through std routines)
329 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
330 size_t count, loff_t * off)
332 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
333 char buffer[FILENT_SIZE];
340 spin_lock(&info->lock);
341 snprintf(buffer, sizeof(buffer),
342 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
344 info->notify_owner ? info->notify.sigev_notify : 0,
345 (info->notify_owner &&
346 info->notify.sigev_notify == SIGEV_SIGNAL) ?
347 info->notify.sigev_signo : 0,
349 spin_unlock(&info->lock);
350 buffer[sizeof(buffer)-1] = '\0';
351 slen = strlen(buffer)+1;
357 if (o + count > slen)
360 if (copy_to_user(u_data, buffer + o, count))
364 filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
368 static int mqueue_flush_file(struct file *filp)
370 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
372 spin_lock(&info->lock);
373 if (current->tgid == info->notify_owner)
374 remove_notification(info);
376 spin_unlock(&info->lock);
380 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
382 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
385 poll_wait(filp, &info->wait_q, poll_tab);
387 spin_lock(&info->lock);
388 if (info->attr.mq_curmsgs)
389 retval = POLLIN | POLLRDNORM;
391 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
392 retval |= POLLOUT | POLLWRNORM;
393 spin_unlock(&info->lock);
398 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
399 static void wq_add(struct mqueue_inode_info *info, int sr,
400 struct ext_wait_queue *ewp)
402 struct ext_wait_queue *walk;
406 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
407 if (walk->task->static_prio <= current->static_prio) {
408 list_add_tail(&ewp->list, &walk->list);
412 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
416 * Puts current task to sleep. Caller must hold queue lock. After return
420 static int wq_sleep(struct mqueue_inode_info *info, int sr,
421 long timeout, struct ext_wait_queue *ewp)
426 wq_add(info, sr, ewp);
429 set_current_state(TASK_INTERRUPTIBLE);
431 spin_unlock(&info->lock);
432 time = schedule_timeout(timeout);
434 while (ewp->state == STATE_PENDING)
437 if (ewp->state == STATE_READY) {
441 spin_lock(&info->lock);
442 if (ewp->state == STATE_READY) {
446 if (signal_pending(current)) {
447 retval = -ERESTARTSYS;
455 list_del(&ewp->list);
457 spin_unlock(&info->lock);
463 * Returns waiting task that should be serviced first or NULL if none exists
465 static struct ext_wait_queue *wq_get_first_waiter(
466 struct mqueue_inode_info *info, int sr)
468 struct list_head *ptr;
470 ptr = info->e_wait_q[sr].list.prev;
471 if (ptr == &info->e_wait_q[sr].list)
473 return list_entry(ptr, struct ext_wait_queue, list);
476 /* Auxiliary functions to manipulate messages' list */
477 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
481 k = info->attr.mq_curmsgs - 1;
482 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
483 info->messages[k + 1] = info->messages[k];
486 info->attr.mq_curmsgs++;
487 info->qsize += ptr->m_ts;
488 info->messages[k + 1] = ptr;
491 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
493 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
494 return info->messages[info->attr.mq_curmsgs];
497 static inline void set_cookie(struct sk_buff *skb, char code)
499 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
503 * The next function is only to split too long sys_mq_timedsend
505 static void __do_notify(struct mqueue_inode_info *info)
508 * invoked when there is registered process and there isn't process
509 * waiting synchronously for message AND state of queue changed from
510 * empty to not empty. Here we are sure that no one is waiting
512 if (info->notify_owner &&
513 info->attr.mq_curmsgs == 1) {
514 struct siginfo sig_i;
515 switch (info->notify.sigev_notify) {
521 sig_i.si_signo = info->notify.sigev_signo;
523 sig_i.si_code = SI_MESGQ;
524 sig_i.si_value = info->notify.sigev_value;
525 sig_i.si_pid = current->tgid;
526 sig_i.si_uid = current->uid;
528 kill_proc_info(info->notify.sigev_signo,
529 &sig_i, info->notify_owner);
532 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
533 netlink_sendskb(info->notify_sock,
534 info->notify_cookie, 0);
537 /* after notification unregisters process */
538 info->notify_owner = 0;
540 wake_up(&info->wait_q);
543 static long prepare_timeout(const struct timespec __user *u_arg)
545 struct timespec ts, nowts;
549 if (unlikely(copy_from_user(&ts, u_arg,
550 sizeof(struct timespec))))
553 if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
554 || ts.tv_nsec >= NSEC_PER_SEC))
556 nowts = CURRENT_TIME;
557 /* first subtract as jiffies can't be too big */
558 ts.tv_sec -= nowts.tv_sec;
559 if (ts.tv_nsec < nowts.tv_nsec) {
560 ts.tv_nsec += NSEC_PER_SEC;
563 ts.tv_nsec -= nowts.tv_nsec;
567 timeout = timespec_to_jiffies(&ts) + 1;
569 return MAX_SCHEDULE_TIMEOUT;
574 static void remove_notification(struct mqueue_inode_info *info)
576 if (info->notify_owner != 0 &&
577 info->notify.sigev_notify == SIGEV_THREAD) {
578 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
579 netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
581 info->notify_owner = 0;
584 static int mq_attr_ok(struct mq_attr *attr)
586 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
588 if (capable(CAP_SYS_RESOURCE)) {
589 if (attr->mq_maxmsg > HARD_MSGMAX)
592 if (attr->mq_maxmsg > msg_max ||
593 attr->mq_msgsize > msgsize_max)
596 /* check for overflow */
597 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
599 if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
600 (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
601 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
607 * Invoked when creating a new queue via sys_mq_open
609 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
610 int oflag, mode_t mode, struct mq_attr __user *u_attr)
617 if (copy_from_user(&attr, u_attr, sizeof(attr)))
620 if (!mq_attr_ok(&attr))
622 /* store for use during create */
623 dentry->d_fsdata = &attr;
626 mode &= ~current->fs->umask;
627 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
628 dentry->d_fsdata = NULL;
632 return dentry_open(dentry, mqueue_mnt, oflag);
640 /* Opens existing queue */
641 static struct file *do_open(struct dentry *dentry, int oflag)
643 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
644 MAY_READ | MAY_WRITE };
646 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
649 return ERR_PTR(-EINVAL);
652 if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL)) {
655 return ERR_PTR(-EACCES);
658 return dentry_open(dentry, mqueue_mnt, oflag);
661 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
662 struct mq_attr __user *u_attr)
664 struct dentry *dentry;
669 if (IS_ERR(name = getname(u_name)))
670 return PTR_ERR(name);
672 fd = get_unused_fd();
676 mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
677 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
678 if (IS_ERR(dentry)) {
679 error = PTR_ERR(dentry);
684 if (oflag & O_CREAT) {
685 if (dentry->d_inode) { /* entry already exists */
689 filp = do_open(dentry, oflag);
691 filp = do_create(mqueue_mnt->mnt_root, dentry,
692 oflag, mode, u_attr);
696 if (!dentry->d_inode)
698 filp = do_open(dentry, oflag);
702 error = PTR_ERR(filp);
706 set_close_on_exec(fd, 1);
707 fd_install(fd, filp);
718 mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
724 asmlinkage long sys_mq_unlink(const char __user *u_name)
728 struct dentry *dentry;
729 struct inode *inode = NULL;
731 name = getname(u_name);
733 return PTR_ERR(name);
735 mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
736 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
737 if (IS_ERR(dentry)) {
738 err = PTR_ERR(dentry);
742 if (!dentry->d_inode) {
747 inode = dentry->d_inode;
749 atomic_inc(&inode->i_count);
751 err = vfs_unlink(dentry->d_parent->d_inode, dentry, NULL);
756 mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
764 /* Pipelined send and receive functions.
766 * If a receiver finds no waiting message, then it registers itself in the
767 * list of waiting receivers. A sender checks that list before adding the new
768 * message into the message array. If there is a waiting receiver, then it
769 * bypasses the message array and directly hands the message over to the
771 * The receiver accepts the message and returns without grabbing the queue
772 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
773 * are necessary. The same algorithm is used for sysv semaphores, see
774 * ipc/sem.c for more details.
776 * The same algorithm is used for senders.
779 /* pipelined_send() - send a message directly to the task waiting in
780 * sys_mq_timedreceive() (without inserting message into a queue).
782 static inline void pipelined_send(struct mqueue_inode_info *info,
783 struct msg_msg *message,
784 struct ext_wait_queue *receiver)
786 receiver->msg = message;
787 list_del(&receiver->list);
788 receiver->state = STATE_PENDING;
789 wake_up_process(receiver->task);
791 receiver->state = STATE_READY;
794 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
795 * gets its message and put to the queue (we have one free place for sure). */
796 static inline void pipelined_receive(struct mqueue_inode_info *info)
798 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
802 wake_up_interruptible(&info->wait_q);
805 msg_insert(sender->msg, info);
806 list_del(&sender->list);
807 sender->state = STATE_PENDING;
808 wake_up_process(sender->task);
810 sender->state = STATE_READY;
813 asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
814 size_t msg_len, unsigned int msg_prio,
815 const struct timespec __user *u_abs_timeout)
819 struct ext_wait_queue wait;
820 struct ext_wait_queue *receiver;
821 struct msg_msg *msg_ptr;
822 struct mqueue_inode_info *info;
826 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
829 timeout = prepare_timeout(u_abs_timeout);
836 inode = filp->f_dentry->d_inode;
837 if (unlikely(filp->f_op != &mqueue_file_operations))
839 info = MQUEUE_I(inode);
841 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
844 if (unlikely(msg_len > info->attr.mq_msgsize)) {
849 /* First try to allocate memory, before doing anything with
850 * existing queues. */
851 msg_ptr = load_msg(u_msg_ptr, msg_len);
852 if (IS_ERR(msg_ptr)) {
853 ret = PTR_ERR(msg_ptr);
856 msg_ptr->m_ts = msg_len;
857 msg_ptr->m_type = msg_prio;
859 spin_lock(&info->lock);
861 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
862 if (filp->f_flags & O_NONBLOCK) {
863 spin_unlock(&info->lock);
865 } else if (unlikely(timeout < 0)) {
866 spin_unlock(&info->lock);
870 wait.msg = (void *) msg_ptr;
871 wait.state = STATE_NONE;
872 ret = wq_sleep(info, SEND, timeout, &wait);
877 receiver = wq_get_first_waiter(info, RECV);
879 pipelined_send(info, msg_ptr, receiver);
881 /* adds message to the queue */
882 msg_insert(msg_ptr, info);
885 inode->i_atime = inode->i_mtime = inode->i_ctime =
887 spin_unlock(&info->lock);
896 asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
897 size_t msg_len, unsigned int __user *u_msg_prio,
898 const struct timespec __user *u_abs_timeout)
902 struct msg_msg *msg_ptr;
905 struct mqueue_inode_info *info;
906 struct ext_wait_queue wait;
908 timeout = prepare_timeout(u_abs_timeout);
915 inode = filp->f_dentry->d_inode;
916 if (unlikely(filp->f_op != &mqueue_file_operations))
918 info = MQUEUE_I(inode);
920 if (unlikely(!(filp->f_mode & FMODE_READ)))
923 /* checks if buffer is big enough */
924 if (unlikely(msg_len < info->attr.mq_msgsize)) {
929 spin_lock(&info->lock);
930 if (info->attr.mq_curmsgs == 0) {
931 if (filp->f_flags & O_NONBLOCK) {
932 spin_unlock(&info->lock);
935 } else if (unlikely(timeout < 0)) {
936 spin_unlock(&info->lock);
941 wait.state = STATE_NONE;
942 ret = wq_sleep(info, RECV, timeout, &wait);
946 msg_ptr = msg_get(info);
948 inode->i_atime = inode->i_mtime = inode->i_ctime =
951 /* There is now free space in queue. */
952 pipelined_receive(info);
953 spin_unlock(&info->lock);
959 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
960 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
972 * Notes: the case when user wants us to deregister (with NULL as pointer)
973 * and he isn't currently owner of notification, will be silently discarded.
974 * It isn't explicitly defined in the POSIX.
976 asmlinkage long sys_mq_notify(mqd_t mqdes,
977 const struct sigevent __user *u_notification)
983 struct sigevent notification;
984 struct mqueue_inode_info *info;
989 if (u_notification != NULL) {
990 if (copy_from_user(¬ification, u_notification,
991 sizeof(struct sigevent)))
994 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
995 notification.sigev_notify != SIGEV_SIGNAL &&
996 notification.sigev_notify != SIGEV_THREAD))
998 if (notification.sigev_notify == SIGEV_SIGNAL &&
999 !valid_signal(notification.sigev_signo)) {
1002 if (notification.sigev_notify == SIGEV_THREAD) {
1003 /* create the notify skb */
1004 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1009 if (copy_from_user(nc->data,
1010 notification.sigev_value.sival_ptr,
1011 NOTIFY_COOKIE_LEN)) {
1015 /* TODO: add a header? */
1016 skb_put(nc, NOTIFY_COOKIE_LEN);
1017 /* and attach it to the socket */
1019 filp = fget(notification.sigev_signo);
1023 sock = netlink_getsockbyfilp(filp);
1026 ret = PTR_ERR(sock);
1031 ret = netlink_attachskb(sock, nc, 0,
1032 MAX_SCHEDULE_TIMEOUT, NULL);
1048 inode = filp->f_dentry->d_inode;
1049 if (unlikely(filp->f_op != &mqueue_file_operations))
1051 info = MQUEUE_I(inode);
1054 spin_lock(&info->lock);
1055 if (u_notification == NULL) {
1056 if (info->notify_owner == current->tgid) {
1057 remove_notification(info);
1058 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1060 } else if (info->notify_owner != 0) {
1063 switch (notification.sigev_notify) {
1065 info->notify.sigev_notify = SIGEV_NONE;
1068 info->notify_sock = sock;
1069 info->notify_cookie = nc;
1072 info->notify.sigev_notify = SIGEV_THREAD;
1075 info->notify.sigev_signo = notification.sigev_signo;
1076 info->notify.sigev_value = notification.sigev_value;
1077 info->notify.sigev_notify = SIGEV_SIGNAL;
1080 info->notify_owner = current->tgid;
1081 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1083 spin_unlock(&info->lock);
1088 netlink_detachskb(sock, nc);
1095 asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
1096 const struct mq_attr __user *u_mqstat,
1097 struct mq_attr __user *u_omqstat)
1100 struct mq_attr mqstat, omqstat;
1102 struct inode *inode;
1103 struct mqueue_inode_info *info;
1105 if (u_mqstat != NULL) {
1106 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1108 if (mqstat.mq_flags & (~O_NONBLOCK))
1117 inode = filp->f_dentry->d_inode;
1118 if (unlikely(filp->f_op != &mqueue_file_operations))
1120 info = MQUEUE_I(inode);
1122 spin_lock(&info->lock);
1124 omqstat = info->attr;
1125 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1127 if (mqstat.mq_flags & O_NONBLOCK)
1128 filp->f_flags |= O_NONBLOCK;
1130 filp->f_flags &= ~O_NONBLOCK;
1132 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1135 spin_unlock(&info->lock);
1138 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1139 sizeof(struct mq_attr)))
1148 static struct inode_operations mqueue_dir_inode_operations = {
1149 .lookup = simple_lookup,
1150 .create = mqueue_create,
1151 .unlink = mqueue_unlink,
1154 static struct file_operations mqueue_file_operations = {
1155 .flush = mqueue_flush_file,
1156 .poll = mqueue_poll_file,
1157 .read = mqueue_read_file,
1160 static struct super_operations mqueue_super_ops = {
1161 .alloc_inode = mqueue_alloc_inode,
1162 .destroy_inode = mqueue_destroy_inode,
1163 .statfs = simple_statfs,
1164 .delete_inode = mqueue_delete_inode,
1165 .drop_inode = generic_delete_inode,
1168 static struct file_system_type mqueue_fs_type = {
1170 .get_sb = mqueue_get_sb,
1171 .kill_sb = kill_litter_super,
1174 static int msg_max_limit_min = DFLT_MSGMAX;
1175 static int msg_max_limit_max = HARD_MSGMAX;
1177 static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
1178 static int msg_maxsize_limit_max = INT_MAX;
1180 static ctl_table mq_sysctls[] = {
1182 .ctl_name = CTL_QUEUESMAX,
1183 .procname = "queues_max",
1184 .data = &queues_max,
1185 .maxlen = sizeof(int),
1187 .proc_handler = &proc_dointvec,
1190 .ctl_name = CTL_MSGMAX,
1191 .procname = "msg_max",
1193 .maxlen = sizeof(int),
1195 .proc_handler = &proc_dointvec_minmax,
1196 .extra1 = &msg_max_limit_min,
1197 .extra2 = &msg_max_limit_max,
1200 .ctl_name = CTL_MSGSIZEMAX,
1201 .procname = "msgsize_max",
1202 .data = &msgsize_max,
1203 .maxlen = sizeof(int),
1205 .proc_handler = &proc_dointvec_minmax,
1206 .extra1 = &msg_maxsize_limit_min,
1207 .extra2 = &msg_maxsize_limit_max,
1212 static ctl_table mq_sysctl_dir[] = {
1214 .ctl_name = FS_MQUEUE,
1215 .procname = "mqueue",
1217 .child = mq_sysctls,
1222 static ctl_table mq_sysctl_root[] = {
1227 .child = mq_sysctl_dir,
1232 static int __init init_mqueue_fs(void)
1236 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1237 sizeof(struct mqueue_inode_info), 0,
1238 SLAB_HWCACHE_ALIGN, init_once, NULL);
1239 if (mqueue_inode_cachep == NULL)
1242 /* ignore failues - they are not fatal */
1243 mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
1245 error = register_filesystem(&mqueue_fs_type);
1249 if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
1250 error = PTR_ERR(mqueue_mnt);
1251 goto out_filesystem;
1254 /* internal initialization - not common for vfs */
1256 spin_lock_init(&mq_lock);
1261 unregister_filesystem(&mqueue_fs_type);
1263 if (mq_sysctl_table)
1264 unregister_sysctl_table(mq_sysctl_table);
1265 if (kmem_cache_destroy(mqueue_inode_cachep)) {
1267 "mqueue_inode_cache: not all structures were freed\n");
1272 __initcall(init_mqueue_fs);