2 * POSIX message queues filesystem for Linux.
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
5 * Michal Wronski (wrona@mat.uni.torun.pl)
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/init.h>
15 #include <linux/pagemap.h>
16 #include <linux/file.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/sysctl.h>
20 #include <linux/poll.h>
21 #include <linux/mqueue.h>
22 #include <linux/msg.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
25 #include <linux/syscalls.h>
26 #include <linux/signal.h>
27 #include <linux/vs_context.h>
28 #include <linux/vs_limit.h>
32 #define MQUEUE_MAGIC 0x19800202
33 #define DIRENT_SIZE 20
34 #define FILENT_SIZE 80
40 #define STATE_PENDING 1
45 #define CTL_QUEUESMAX 2
47 #define CTL_MSGSIZEMAX 4
50 #define DFLT_QUEUESMAX 256 /* max number of message queues */
51 #define DFLT_MSGMAX 10 /* max number of messages in each queue */
52 #define HARD_MSGMAX (131072/sizeof(void*))
53 #define DFLT_MSGSIZEMAX 8192 /* max message size */
55 #define NOTIFY_COOKIE_LEN 32
57 struct ext_wait_queue { /* queue of sleeping tasks */
58 struct task_struct *task;
59 struct list_head list;
60 struct msg_msg *msg; /* ptr of loaded message */
61 int state; /* one of STATE_* values */
64 struct mqueue_inode_info {
66 struct inode vfs_inode;
67 wait_queue_head_t wait_q;
69 struct msg_msg **messages;
72 struct sigevent notify;
74 struct user_struct *user; /* user who created, for accouting */
75 struct sock *notify_sock;
76 struct sk_buff *notify_cookie;
78 /* for tasks waiting for free space and messages, respectively */
79 struct ext_wait_queue e_wait_q[2];
81 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
84 static struct inode_operations mqueue_dir_inode_operations;
85 static struct file_operations mqueue_file_operations;
86 static struct super_operations mqueue_super_ops;
87 static void remove_notification(struct mqueue_inode_info *info);
89 static spinlock_t mq_lock;
90 static kmem_cache_t *mqueue_inode_cachep;
91 static struct vfsmount *mqueue_mnt;
93 static unsigned int queues_count;
94 static unsigned int queues_max = DFLT_QUEUESMAX;
95 static unsigned int msg_max = DFLT_MSGMAX;
96 static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
98 static struct ctl_table_header * mq_sysctl_table;
100 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
102 return container_of(inode, struct mqueue_inode_info, vfs_inode);
105 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
106 struct mq_attr *attr)
110 inode = new_inode(sb);
112 inode->i_mode = mode;
113 inode->i_uid = current->fsuid;
114 inode->i_gid = current->fsgid;
115 inode->i_blksize = PAGE_CACHE_SIZE;
117 inode->i_mtime = inode->i_ctime = inode->i_atime =
121 struct mqueue_inode_info *info;
122 struct task_struct *p = current;
123 struct user_struct *u = p->user;
124 unsigned long mq_bytes, mq_msg_tblsz;
126 inode->i_fop = &mqueue_file_operations;
127 inode->i_size = FILENT_SIZE;
128 /* mqueue specific info */
129 info = MQUEUE_I(inode);
130 spin_lock_init(&info->lock);
131 init_waitqueue_head(&info->wait_q);
132 INIT_LIST_HEAD(&info->e_wait_q[0].list);
133 INIT_LIST_HEAD(&info->e_wait_q[1].list);
134 info->messages = NULL;
135 info->notify_owner = 0;
137 info->user = NULL; /* set when all is ok */
138 memset(&info->attr, 0, sizeof(info->attr));
139 info->attr.mq_maxmsg = DFLT_MSGMAX;
140 info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
142 info->attr.mq_maxmsg = attr->mq_maxmsg;
143 info->attr.mq_msgsize = attr->mq_msgsize;
145 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
146 mq_bytes = (mq_msg_tblsz +
147 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
150 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
151 u->mq_bytes + mq_bytes >
152 p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur ||
153 !vx_ipcmsg_avail(p->vx_info, mq_bytes)) {
154 spin_unlock(&mq_lock);
157 u->mq_bytes += mq_bytes;
158 vx_ipcmsg_add(p->vx_info, u, mq_bytes);
159 spin_unlock(&mq_lock);
161 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
162 if (!info->messages) {
164 u->mq_bytes -= mq_bytes;
165 vx_ipcmsg_sub(p->vx_info, u, mq_bytes);
166 spin_unlock(&mq_lock);
170 info->user = get_uid(u);
171 } else if (S_ISDIR(mode)) {
173 /* Some things misbehave if size == 0 on a directory */
174 inode->i_size = 2 * DIRENT_SIZE;
175 inode->i_op = &mqueue_dir_inode_operations;
176 inode->i_fop = &simple_dir_operations;
181 make_bad_inode(inode);
186 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
190 sb->s_blocksize = PAGE_CACHE_SIZE;
191 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
192 sb->s_magic = MQUEUE_MAGIC;
193 sb->s_op = &mqueue_super_ops;
195 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
199 sb->s_root = d_alloc_root(inode);
208 static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
209 int flags, const char *dev_name,
212 return get_sb_single(fs_type, flags, data, mqueue_fill_super);
215 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
217 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
219 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
220 SLAB_CTOR_CONSTRUCTOR)
221 inode_init_once(&p->vfs_inode);
224 static struct inode *mqueue_alloc_inode(struct super_block *sb)
226 struct mqueue_inode_info *ei;
228 ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
231 return &ei->vfs_inode;
234 static void mqueue_destroy_inode(struct inode *inode)
236 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
239 static void mqueue_delete_inode(struct inode *inode)
241 struct mqueue_inode_info *info;
242 struct user_struct *user;
243 unsigned long mq_bytes;
246 if (S_ISDIR(inode->i_mode)) {
250 info = MQUEUE_I(inode);
251 spin_lock(&info->lock);
252 for (i = 0; i < info->attr.mq_curmsgs; i++)
253 free_msg(info->messages[i]);
254 kfree(info->messages);
255 spin_unlock(&info->lock);
259 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
260 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
263 struct vx_info *vxi = locate_vx_info(user->xid);
266 user->mq_bytes -= mq_bytes;
267 vx_ipcmsg_sub(vxi, user, mq_bytes);
269 spin_unlock(&mq_lock);
275 static int mqueue_create(struct inode *dir, struct dentry *dentry,
276 int mode, struct nameidata *nd)
279 struct mq_attr *attr = dentry->d_fsdata;
283 if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
288 spin_unlock(&mq_lock);
290 inode = mqueue_get_inode(dir->i_sb, mode, attr);
298 dir->i_size += DIRENT_SIZE;
299 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
301 d_instantiate(dentry, inode);
305 spin_unlock(&mq_lock);
309 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
311 struct inode *inode = dentry->d_inode;
313 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
314 dir->i_size -= DIRENT_SIZE;
321 * This is routine for system read from queue file.
322 * To avoid mess with doing here some sort of mq_receive we allow
323 * to read only queue size & notification info (the only values
324 * that are interesting from user point of view and aren't accessible
325 * through std routines)
327 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
328 size_t count, loff_t * off)
330 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
331 char buffer[FILENT_SIZE];
338 spin_lock(&info->lock);
339 snprintf(buffer, sizeof(buffer),
340 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
342 info->notify_owner ? info->notify.sigev_notify : 0,
343 (info->notify_owner &&
344 info->notify.sigev_notify == SIGEV_SIGNAL) ?
345 info->notify.sigev_signo : 0,
347 spin_unlock(&info->lock);
348 buffer[sizeof(buffer)-1] = '\0';
349 slen = strlen(buffer)+1;
355 if (o + count > slen)
358 if (copy_to_user(u_data, buffer + o, count))
362 filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
366 static int mqueue_flush_file(struct file *filp)
368 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
370 spin_lock(&info->lock);
371 if (current->tgid == info->notify_owner)
372 remove_notification(info);
374 spin_unlock(&info->lock);
378 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
380 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
383 poll_wait(filp, &info->wait_q, poll_tab);
385 spin_lock(&info->lock);
386 if (info->attr.mq_curmsgs)
387 retval = POLLIN | POLLRDNORM;
389 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
390 retval |= POLLOUT | POLLWRNORM;
391 spin_unlock(&info->lock);
396 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
397 static void wq_add(struct mqueue_inode_info *info, int sr,
398 struct ext_wait_queue *ewp)
400 struct ext_wait_queue *walk;
404 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
405 if (walk->task->static_prio <= current->static_prio) {
406 list_add_tail(&ewp->list, &walk->list);
410 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
414 * Puts current task to sleep. Caller must hold queue lock. After return
418 static int wq_sleep(struct mqueue_inode_info *info, int sr,
419 long timeout, struct ext_wait_queue *ewp)
424 wq_add(info, sr, ewp);
427 set_current_state(TASK_INTERRUPTIBLE);
429 spin_unlock(&info->lock);
430 time = schedule_timeout(timeout);
432 while (ewp->state == STATE_PENDING)
435 if (ewp->state == STATE_READY) {
439 spin_lock(&info->lock);
440 if (ewp->state == STATE_READY) {
444 if (signal_pending(current)) {
445 retval = -ERESTARTSYS;
453 list_del(&ewp->list);
455 spin_unlock(&info->lock);
461 * Returns waiting task that should be serviced first or NULL if none exists
463 static struct ext_wait_queue *wq_get_first_waiter(
464 struct mqueue_inode_info *info, int sr)
466 struct list_head *ptr;
468 ptr = info->e_wait_q[sr].list.prev;
469 if (ptr == &info->e_wait_q[sr].list)
471 return list_entry(ptr, struct ext_wait_queue, list);
474 /* Auxiliary functions to manipulate messages' list */
475 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
479 k = info->attr.mq_curmsgs - 1;
480 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
481 info->messages[k + 1] = info->messages[k];
484 info->attr.mq_curmsgs++;
485 info->qsize += ptr->m_ts;
486 info->messages[k + 1] = ptr;
489 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
491 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
492 return info->messages[info->attr.mq_curmsgs];
495 static inline void set_cookie(struct sk_buff *skb, char code)
497 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
501 * The next function is only to split too long sys_mq_timedsend
503 static void __do_notify(struct mqueue_inode_info *info)
506 * invoked when there is registered process and there isn't process
507 * waiting synchronously for message AND state of queue changed from
508 * empty to not empty. Here we are sure that no one is waiting
510 if (info->notify_owner &&
511 info->attr.mq_curmsgs == 1) {
512 struct siginfo sig_i;
513 switch (info->notify.sigev_notify) {
519 sig_i.si_signo = info->notify.sigev_signo;
521 sig_i.si_code = SI_MESGQ;
522 sig_i.si_value = info->notify.sigev_value;
523 sig_i.si_pid = current->tgid;
524 sig_i.si_uid = current->uid;
526 kill_proc_info(info->notify.sigev_signo,
527 &sig_i, info->notify_owner);
530 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
531 netlink_sendskb(info->notify_sock,
532 info->notify_cookie, 0);
535 /* after notification unregisters process */
536 info->notify_owner = 0;
538 wake_up(&info->wait_q);
541 static long prepare_timeout(const struct timespec __user *u_arg)
543 struct timespec ts, nowts;
547 if (unlikely(copy_from_user(&ts, u_arg,
548 sizeof(struct timespec))))
551 if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
552 || ts.tv_nsec >= NSEC_PER_SEC))
554 nowts = CURRENT_TIME;
555 /* first subtract as jiffies can't be too big */
556 ts.tv_sec -= nowts.tv_sec;
557 if (ts.tv_nsec < nowts.tv_nsec) {
558 ts.tv_nsec += NSEC_PER_SEC;
561 ts.tv_nsec -= nowts.tv_nsec;
565 timeout = timespec_to_jiffies(&ts) + 1;
567 return MAX_SCHEDULE_TIMEOUT;
572 static void remove_notification(struct mqueue_inode_info *info)
574 if (info->notify_owner != 0 &&
575 info->notify.sigev_notify == SIGEV_THREAD) {
576 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
577 netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
579 info->notify_owner = 0;
582 static int mq_attr_ok(struct mq_attr *attr)
584 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
586 if (capable(CAP_SYS_RESOURCE)) {
587 if (attr->mq_maxmsg > HARD_MSGMAX)
590 if (attr->mq_maxmsg > msg_max ||
591 attr->mq_msgsize > msgsize_max)
594 /* check for overflow */
595 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
597 if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
598 (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
599 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
605 * Invoked when creating a new queue via sys_mq_open
607 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
608 int oflag, mode_t mode, struct mq_attr __user *u_attr)
614 if (u_attr != NULL) {
615 if (copy_from_user(&attr, u_attr, sizeof(attr)))
616 return ERR_PTR(-EFAULT);
617 if (!mq_attr_ok(&attr))
618 return ERR_PTR(-EINVAL);
619 /* store for use during create */
620 dentry->d_fsdata = &attr;
623 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
624 dentry->d_fsdata = NULL;
628 filp = dentry_open(dentry, mqueue_mnt, oflag);
635 /* Opens existing queue */
636 static struct file *do_open(struct dentry *dentry, int oflag)
638 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
639 MAY_READ | MAY_WRITE };
642 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
643 return ERR_PTR(-EINVAL);
645 if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL))
646 return ERR_PTR(-EACCES);
648 filp = dentry_open(dentry, mqueue_mnt, oflag);
656 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
657 struct mq_attr __user *u_attr)
659 struct dentry *dentry;
664 if (IS_ERR(name = getname(u_name)))
665 return PTR_ERR(name);
667 fd = get_unused_fd();
671 down(&mqueue_mnt->mnt_root->d_inode->i_sem);
672 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
673 if (IS_ERR(dentry)) {
674 error = PTR_ERR(dentry);
679 if (oflag & O_CREAT) {
680 if (dentry->d_inode) { /* entry already exists */
681 filp = (oflag & O_EXCL) ? ERR_PTR(-EEXIST) :
682 do_open(dentry, oflag);
684 filp = do_create(mqueue_mnt->mnt_root, dentry,
685 oflag, mode, u_attr);
688 filp = (dentry->d_inode) ? do_open(dentry, oflag) :
694 error = PTR_ERR(filp);
698 set_close_on_exec(fd, 1);
699 fd_install(fd, filp);
708 up(&mqueue_mnt->mnt_root->d_inode->i_sem);
714 asmlinkage long sys_mq_unlink(const char __user *u_name)
718 struct dentry *dentry;
719 struct inode *inode = NULL;
721 name = getname(u_name);
723 return PTR_ERR(name);
725 down(&mqueue_mnt->mnt_root->d_inode->i_sem);
726 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
727 if (IS_ERR(dentry)) {
728 err = PTR_ERR(dentry);
732 if (!dentry->d_inode) {
737 inode = dentry->d_inode;
739 atomic_inc(&inode->i_count);
741 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
746 up(&mqueue_mnt->mnt_root->d_inode->i_sem);
754 /* Pipelined send and receive functions.
756 * If a receiver finds no waiting message, then it registers itself in the
757 * list of waiting receivers. A sender checks that list before adding the new
758 * message into the message array. If there is a waiting receiver, then it
759 * bypasses the message array and directly hands the message over to the
761 * The receiver accepts the message and returns without grabbing the queue
762 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
763 * are necessary. The same algorithm is used for sysv semaphores, see
764 * ipc/sem.c fore more details.
766 * The same algorithm is used for senders.
769 /* pipelined_send() - send a message directly to the task waiting in
770 * sys_mq_timedreceive() (without inserting message into a queue).
772 static inline void pipelined_send(struct mqueue_inode_info *info,
773 struct msg_msg *message,
774 struct ext_wait_queue *receiver)
776 receiver->msg = message;
777 list_del(&receiver->list);
778 receiver->state = STATE_PENDING;
779 wake_up_process(receiver->task);
781 receiver->state = STATE_READY;
784 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
785 * gets its message and put to the queue (we have one free place for sure). */
786 static inline void pipelined_receive(struct mqueue_inode_info *info)
788 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
792 wake_up_interruptible(&info->wait_q);
795 msg_insert(sender->msg, info);
796 list_del(&sender->list);
797 sender->state = STATE_PENDING;
798 wake_up_process(sender->task);
800 sender->state = STATE_READY;
803 asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
804 size_t msg_len, unsigned int msg_prio,
805 const struct timespec __user *u_abs_timeout)
809 struct ext_wait_queue wait;
810 struct ext_wait_queue *receiver;
811 struct msg_msg *msg_ptr;
812 struct mqueue_inode_info *info;
816 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
819 timeout = prepare_timeout(u_abs_timeout);
826 inode = filp->f_dentry->d_inode;
827 if (unlikely(filp->f_op != &mqueue_file_operations))
829 info = MQUEUE_I(inode);
831 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
834 if (unlikely(msg_len > info->attr.mq_msgsize)) {
839 /* First try to allocate memory, before doing anything with
840 * existing queues. */
841 msg_ptr = load_msg(u_msg_ptr, msg_len);
842 if (IS_ERR(msg_ptr)) {
843 ret = PTR_ERR(msg_ptr);
846 msg_ptr->m_ts = msg_len;
847 msg_ptr->m_type = msg_prio;
849 spin_lock(&info->lock);
851 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
852 if (filp->f_flags & O_NONBLOCK) {
853 spin_unlock(&info->lock);
855 } else if (unlikely(timeout < 0)) {
856 spin_unlock(&info->lock);
860 wait.msg = (void *) msg_ptr;
861 wait.state = STATE_NONE;
862 ret = wq_sleep(info, SEND, timeout, &wait);
867 receiver = wq_get_first_waiter(info, RECV);
869 pipelined_send(info, msg_ptr, receiver);
871 /* adds message to the queue */
872 msg_insert(msg_ptr, info);
875 inode->i_atime = inode->i_mtime = inode->i_ctime =
877 spin_unlock(&info->lock);
886 asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
887 size_t msg_len, unsigned int __user *u_msg_prio,
888 const struct timespec __user *u_abs_timeout)
892 struct msg_msg *msg_ptr;
895 struct mqueue_inode_info *info;
896 struct ext_wait_queue wait;
898 timeout = prepare_timeout(u_abs_timeout);
905 inode = filp->f_dentry->d_inode;
906 if (unlikely(filp->f_op != &mqueue_file_operations))
908 info = MQUEUE_I(inode);
910 if (unlikely(!(filp->f_mode & FMODE_READ)))
913 /* checks if buffer is big enough */
914 if (unlikely(msg_len < info->attr.mq_msgsize)) {
919 spin_lock(&info->lock);
920 if (info->attr.mq_curmsgs == 0) {
921 if (filp->f_flags & O_NONBLOCK) {
922 spin_unlock(&info->lock);
925 } else if (unlikely(timeout < 0)) {
926 spin_unlock(&info->lock);
931 wait.state = STATE_NONE;
932 ret = wq_sleep(info, RECV, timeout, &wait);
936 msg_ptr = msg_get(info);
938 inode->i_atime = inode->i_mtime = inode->i_ctime =
941 /* There is now free space in queue. */
942 pipelined_receive(info);
943 spin_unlock(&info->lock);
949 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
950 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
962 * Notes: the case when user wants us to deregister (with NULL as pointer)
963 * and he isn't currently owner of notification, will be silently discarded.
964 * It isn't explicitly defined in the POSIX.
966 asmlinkage long sys_mq_notify(mqd_t mqdes,
967 const struct sigevent __user *u_notification)
973 struct sigevent notification;
974 struct mqueue_inode_info *info;
979 if (u_notification != NULL) {
980 if (copy_from_user(¬ification, u_notification,
981 sizeof(struct sigevent)))
984 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
985 notification.sigev_notify != SIGEV_SIGNAL &&
986 notification.sigev_notify != SIGEV_THREAD))
988 if (notification.sigev_notify == SIGEV_SIGNAL &&
989 !valid_signal(notification.sigev_signo)) {
992 if (notification.sigev_notify == SIGEV_THREAD) {
993 /* create the notify skb */
994 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
999 if (copy_from_user(nc->data,
1000 notification.sigev_value.sival_ptr,
1001 NOTIFY_COOKIE_LEN)) {
1005 /* TODO: add a header? */
1006 skb_put(nc, NOTIFY_COOKIE_LEN);
1007 /* and attach it to the socket */
1009 filp = fget(notification.sigev_signo);
1013 sock = netlink_getsockbyfilp(filp);
1016 ret = PTR_ERR(sock);
1021 ret = netlink_attachskb(sock, nc, 0, MAX_SCHEDULE_TIMEOUT);
1037 inode = filp->f_dentry->d_inode;
1038 if (unlikely(filp->f_op != &mqueue_file_operations))
1040 info = MQUEUE_I(inode);
1043 spin_lock(&info->lock);
1044 if (u_notification == NULL) {
1045 if (info->notify_owner == current->tgid) {
1046 remove_notification(info);
1047 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1049 } else if (info->notify_owner != 0) {
1052 switch (notification.sigev_notify) {
1054 info->notify.sigev_notify = SIGEV_NONE;
1057 info->notify_sock = sock;
1058 info->notify_cookie = nc;
1061 info->notify.sigev_notify = SIGEV_THREAD;
1064 info->notify.sigev_signo = notification.sigev_signo;
1065 info->notify.sigev_value = notification.sigev_value;
1066 info->notify.sigev_notify = SIGEV_SIGNAL;
1069 info->notify_owner = current->tgid;
1070 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1072 spin_unlock(&info->lock);
1077 netlink_detachskb(sock, nc);
1084 asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
1085 const struct mq_attr __user *u_mqstat,
1086 struct mq_attr __user *u_omqstat)
1089 struct mq_attr mqstat, omqstat;
1091 struct inode *inode;
1092 struct mqueue_inode_info *info;
1094 if (u_mqstat != NULL) {
1095 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1097 if (mqstat.mq_flags & (~O_NONBLOCK))
1106 inode = filp->f_dentry->d_inode;
1107 if (unlikely(filp->f_op != &mqueue_file_operations))
1109 info = MQUEUE_I(inode);
1111 spin_lock(&info->lock);
1113 omqstat = info->attr;
1114 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1116 if (mqstat.mq_flags & O_NONBLOCK)
1117 filp->f_flags |= O_NONBLOCK;
1119 filp->f_flags &= ~O_NONBLOCK;
1121 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1124 spin_unlock(&info->lock);
1127 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1128 sizeof(struct mq_attr)))
1137 static struct inode_operations mqueue_dir_inode_operations = {
1138 .lookup = simple_lookup,
1139 .create = mqueue_create,
1140 .unlink = mqueue_unlink,
1143 static struct file_operations mqueue_file_operations = {
1144 .flush = mqueue_flush_file,
1145 .poll = mqueue_poll_file,
1146 .read = mqueue_read_file,
1149 static struct super_operations mqueue_super_ops = {
1150 .alloc_inode = mqueue_alloc_inode,
1151 .destroy_inode = mqueue_destroy_inode,
1152 .statfs = simple_statfs,
1153 .delete_inode = mqueue_delete_inode,
1154 .drop_inode = generic_delete_inode,
1157 static struct file_system_type mqueue_fs_type = {
1159 .get_sb = mqueue_get_sb,
1160 .kill_sb = kill_litter_super,
1163 static int msg_max_limit_min = DFLT_MSGMAX;
1164 static int msg_max_limit_max = HARD_MSGMAX;
1166 static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
1167 static int msg_maxsize_limit_max = INT_MAX;
1169 static ctl_table mq_sysctls[] = {
1171 .ctl_name = CTL_QUEUESMAX,
1172 .procname = "queues_max",
1173 .data = &queues_max,
1174 .maxlen = sizeof(int),
1176 .proc_handler = &proc_dointvec,
1179 .ctl_name = CTL_MSGMAX,
1180 .procname = "msg_max",
1182 .maxlen = sizeof(int),
1184 .proc_handler = &proc_dointvec_minmax,
1185 .extra1 = &msg_max_limit_min,
1186 .extra2 = &msg_max_limit_max,
1189 .ctl_name = CTL_MSGSIZEMAX,
1190 .procname = "msgsize_max",
1191 .data = &msgsize_max,
1192 .maxlen = sizeof(int),
1194 .proc_handler = &proc_dointvec_minmax,
1195 .extra1 = &msg_maxsize_limit_min,
1196 .extra2 = &msg_maxsize_limit_max,
1201 static ctl_table mq_sysctl_dir[] = {
1203 .ctl_name = FS_MQUEUE,
1204 .procname = "mqueue",
1206 .child = mq_sysctls,
1211 static ctl_table mq_sysctl_root[] = {
1216 .child = mq_sysctl_dir,
1221 static int __init init_mqueue_fs(void)
1225 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1226 sizeof(struct mqueue_inode_info), 0,
1227 SLAB_HWCACHE_ALIGN, init_once, NULL);
1228 if (mqueue_inode_cachep == NULL)
1231 /* ignore failues - they are not fatal */
1232 mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
1234 error = register_filesystem(&mqueue_fs_type);
1238 if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
1239 error = PTR_ERR(mqueue_mnt);
1240 goto out_filesystem;
1243 /* internal initialization - not common for vfs */
1245 spin_lock_init(&mq_lock);
1250 unregister_filesystem(&mqueue_fs_type);
1252 if (mq_sysctl_table)
1253 unregister_sysctl_table(mq_sysctl_table);
1254 if (kmem_cache_destroy(mqueue_inode_cachep)) {
1256 "mqueue_inode_cache: not all structures were freed\n");
1261 __initcall(init_mqueue_fs);