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_base.h>
30 #include <linux/vs_context.h>
31 #include <linux/vs_limit.h>
36 #define MQUEUE_MAGIC 0x19800202
37 #define DIRENT_SIZE 20
38 #define FILENT_SIZE 80
44 #define STATE_PENDING 1
49 #define CTL_QUEUESMAX 2
51 #define CTL_MSGSIZEMAX 4
54 #define DFLT_QUEUESMAX 256 /* max number of message queues */
55 #define DFLT_MSGMAX 10 /* max number of messages in each queue */
56 #define HARD_MSGMAX (131072/sizeof(void*))
57 #define DFLT_MSGSIZEMAX 8192 /* max message size */
60 struct ext_wait_queue { /* queue of sleeping tasks */
61 struct task_struct *task;
62 struct list_head list;
63 struct msg_msg *msg; /* ptr of loaded message */
64 int state; /* one of STATE_* values */
67 struct mqueue_inode_info {
69 struct inode vfs_inode;
70 wait_queue_head_t wait_q;
72 struct msg_msg **messages;
75 struct sigevent notify;
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 struct inode_operations mqueue_dir_inode_operations;
88 static struct file_operations mqueue_file_operations;
89 static struct super_operations mqueue_super_ops;
90 static void remove_notification(struct mqueue_inode_info *info);
92 static spinlock_t mq_lock;
93 static kmem_cache_t *mqueue_inode_cachep;
94 static struct vfsmount *mqueue_mnt;
96 static unsigned int queues_count;
97 static unsigned int queues_max = DFLT_QUEUESMAX;
98 static unsigned int msg_max = DFLT_MSGMAX;
99 static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
101 static struct ctl_table_header * mq_sysctl_table;
103 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
105 return container_of(inode, struct mqueue_inode_info, vfs_inode);
108 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
109 struct mq_attr *attr)
113 inode = new_inode(sb);
115 inode->i_mode = mode;
116 inode->i_uid = current->fsuid;
117 inode->i_gid = current->fsgid;
118 inode->i_blksize = PAGE_CACHE_SIZE;
120 inode->i_mtime = inode->i_ctime = inode->i_atime =
124 struct mqueue_inode_info *info;
125 struct task_struct *p = current;
126 struct user_struct *u = p->user;
127 unsigned long mq_bytes, mq_msg_tblsz;
129 inode->i_fop = &mqueue_file_operations;
130 inode->i_size = FILENT_SIZE;
131 /* mqueue specific info */
132 info = MQUEUE_I(inode);
133 spin_lock_init(&info->lock);
134 init_waitqueue_head(&info->wait_q);
135 INIT_LIST_HEAD(&info->e_wait_q[0].list);
136 INIT_LIST_HEAD(&info->e_wait_q[1].list);
137 info->messages = NULL;
138 info->notify_owner = 0;
140 info->user = NULL; /* set when all is ok */
141 memset(&info->attr, 0, sizeof(info->attr));
142 info->attr.mq_maxmsg = DFLT_MSGMAX;
143 info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
145 info->attr.mq_maxmsg = attr->mq_maxmsg;
146 info->attr.mq_msgsize = attr->mq_msgsize;
148 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
149 mq_bytes = (mq_msg_tblsz +
150 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
153 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
154 u->mq_bytes + mq_bytes >
155 p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur ||
156 !vx_ipcmsg_avail(p->vx_info, mq_bytes)) {
157 spin_unlock(&mq_lock);
160 u->mq_bytes += mq_bytes;
161 vx_ipcmsg_add(p->vx_info, u, mq_bytes);
162 spin_unlock(&mq_lock);
164 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
165 if (!info->messages) {
167 u->mq_bytes -= mq_bytes;
168 vx_ipcmsg_sub(p->vx_info, u, mq_bytes);
169 spin_unlock(&mq_lock);
173 info->user = get_uid(u);
174 } else if (S_ISDIR(mode)) {
176 /* Some things misbehave if size == 0 on a directory */
177 inode->i_size = 2 * DIRENT_SIZE;
178 inode->i_op = &mqueue_dir_inode_operations;
179 inode->i_fop = &simple_dir_operations;
184 make_bad_inode(inode);
189 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
193 sb->s_blocksize = PAGE_CACHE_SIZE;
194 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
195 sb->s_magic = MQUEUE_MAGIC;
196 sb->s_op = &mqueue_super_ops;
198 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
202 sb->s_root = d_alloc_root(inode);
211 static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
212 int flags, const char *dev_name,
215 return get_sb_single(fs_type, flags, data, mqueue_fill_super);
218 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
220 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
222 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
223 SLAB_CTOR_CONSTRUCTOR)
224 inode_init_once(&p->vfs_inode);
227 static struct inode *mqueue_alloc_inode(struct super_block *sb)
229 struct mqueue_inode_info *ei;
231 ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
234 return &ei->vfs_inode;
237 static void mqueue_destroy_inode(struct inode *inode)
239 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
242 static void mqueue_delete_inode(struct inode *inode)
244 struct mqueue_inode_info *info;
245 struct user_struct *user;
246 unsigned long mq_bytes;
249 if (S_ISDIR(inode->i_mode)) {
253 info = MQUEUE_I(inode);
254 spin_lock(&info->lock);
255 for (i = 0; i < info->attr.mq_curmsgs; i++)
256 free_msg(info->messages[i]);
257 kfree(info->messages);
258 spin_unlock(&info->lock);
262 mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
263 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
266 struct vx_info *vxi = lookup_vx_info(user->xid);
269 user->mq_bytes -= mq_bytes;
270 vx_ipcmsg_sub(vxi, user, mq_bytes);
272 spin_unlock(&mq_lock);
278 static int mqueue_create(struct inode *dir, struct dentry *dentry,
279 int mode, struct nameidata *nd)
282 struct mq_attr *attr = dentry->d_fsdata;
286 if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
291 spin_unlock(&mq_lock);
293 inode = mqueue_get_inode(dir->i_sb, mode, attr);
301 dir->i_size += DIRENT_SIZE;
302 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
304 d_instantiate(dentry, inode);
308 spin_unlock(&mq_lock);
312 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
314 struct inode *inode = dentry->d_inode;
316 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
317 dir->i_size -= DIRENT_SIZE;
324 * This is routine for system read from queue file.
325 * To avoid mess with doing here some sort of mq_receive we allow
326 * to read only queue size & notification info (the only values
327 * that are interesting from user point of view and aren't accessible
328 * through std routines)
330 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
331 size_t count, loff_t * off)
333 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
334 char buffer[FILENT_SIZE];
341 spin_lock(&info->lock);
342 snprintf(buffer, sizeof(buffer),
343 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
345 info->notify_owner ? info->notify.sigev_notify : 0,
346 (info->notify_owner &&
347 info->notify.sigev_notify == SIGEV_SIGNAL) ?
348 info->notify.sigev_signo : 0,
350 spin_unlock(&info->lock);
351 buffer[sizeof(buffer)-1] = '\0';
352 slen = strlen(buffer)+1;
358 if (o + count > slen)
361 if (copy_to_user(u_data, buffer + o, count))
365 filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
369 static int mqueue_flush_file(struct file *filp)
371 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
373 spin_lock(&info->lock);
374 if (current->tgid == info->notify_owner)
375 remove_notification(info);
377 spin_unlock(&info->lock);
381 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
383 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
386 poll_wait(filp, &info->wait_q, poll_tab);
388 spin_lock(&info->lock);
389 if (info->attr.mq_curmsgs)
390 retval = POLLIN | POLLRDNORM;
392 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
393 retval |= POLLOUT | POLLWRNORM;
394 spin_unlock(&info->lock);
399 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
400 static void wq_add(struct mqueue_inode_info *info, int sr,
401 struct ext_wait_queue *ewp)
403 struct ext_wait_queue *walk;
407 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
408 if (walk->task->static_prio <= current->static_prio) {
409 list_add_tail(&ewp->list, &walk->list);
413 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
417 * Puts current task to sleep. Caller must hold queue lock. After return
421 static int wq_sleep(struct mqueue_inode_info *info, int sr,
422 long timeout, struct ext_wait_queue *ewp)
427 wq_add(info, sr, ewp);
430 set_current_state(TASK_INTERRUPTIBLE);
432 spin_unlock(&info->lock);
433 time = schedule_timeout(timeout);
435 while (ewp->state == STATE_PENDING)
438 if (ewp->state == STATE_READY) {
442 spin_lock(&info->lock);
443 if (ewp->state == STATE_READY) {
447 if (signal_pending(current)) {
448 retval = -ERESTARTSYS;
456 list_del(&ewp->list);
458 spin_unlock(&info->lock);
464 * Returns waiting task that should be serviced first or NULL if none exists
466 static struct ext_wait_queue *wq_get_first_waiter(
467 struct mqueue_inode_info *info, int sr)
469 struct list_head *ptr;
471 ptr = info->e_wait_q[sr].list.prev;
472 if (ptr == &info->e_wait_q[sr].list)
474 return list_entry(ptr, struct ext_wait_queue, list);
477 /* Auxiliary functions to manipulate messages' list */
478 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
482 k = info->attr.mq_curmsgs - 1;
483 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
484 info->messages[k + 1] = info->messages[k];
487 info->attr.mq_curmsgs++;
488 info->qsize += ptr->m_ts;
489 info->messages[k + 1] = ptr;
492 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
494 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
495 return info->messages[info->attr.mq_curmsgs];
498 static inline void set_cookie(struct sk_buff *skb, char code)
500 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
504 * The next function is only to split too long sys_mq_timedsend
506 static void __do_notify(struct mqueue_inode_info *info)
509 * invoked when there is registered process and there isn't process
510 * waiting synchronously for message AND state of queue changed from
511 * empty to not empty. Here we are sure that no one is waiting
513 if (info->notify_owner &&
514 info->attr.mq_curmsgs == 1) {
515 struct siginfo sig_i;
516 switch (info->notify.sigev_notify) {
522 sig_i.si_signo = info->notify.sigev_signo;
524 sig_i.si_code = SI_MESGQ;
525 sig_i.si_value = info->notify.sigev_value;
526 sig_i.si_pid = current->tgid;
527 sig_i.si_uid = current->uid;
529 kill_proc_info(info->notify.sigev_signo,
530 &sig_i, info->notify_owner);
533 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
534 netlink_sendskb(info->notify_sock,
535 info->notify_cookie, 0);
538 /* after notification unregisters process */
539 info->notify_owner = 0;
541 wake_up(&info->wait_q);
544 static long prepare_timeout(const struct timespec __user *u_arg)
546 struct timespec ts, nowts;
550 if (unlikely(copy_from_user(&ts, u_arg,
551 sizeof(struct timespec))))
554 if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
555 || ts.tv_nsec >= NSEC_PER_SEC))
557 nowts = CURRENT_TIME;
558 /* first subtract as jiffies can't be too big */
559 ts.tv_sec -= nowts.tv_sec;
560 if (ts.tv_nsec < nowts.tv_nsec) {
561 ts.tv_nsec += NSEC_PER_SEC;
564 ts.tv_nsec -= nowts.tv_nsec;
568 timeout = timespec_to_jiffies(&ts) + 1;
570 return MAX_SCHEDULE_TIMEOUT;
575 static void remove_notification(struct mqueue_inode_info *info)
577 if (info->notify_owner != 0 &&
578 info->notify.sigev_notify == SIGEV_THREAD) {
579 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
580 netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
582 info->notify_owner = 0;
585 static int mq_attr_ok(struct mq_attr *attr)
587 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
589 if (capable(CAP_SYS_RESOURCE)) {
590 if (attr->mq_maxmsg > HARD_MSGMAX)
593 if (attr->mq_maxmsg > msg_max ||
594 attr->mq_msgsize > msgsize_max)
597 /* check for overflow */
598 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
600 if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
601 (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
602 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
608 * Invoked when creating a new queue via sys_mq_open
610 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
611 int oflag, mode_t mode, struct mq_attr __user *u_attr)
618 if (copy_from_user(&attr, u_attr, sizeof(attr)))
621 if (!mq_attr_ok(&attr))
623 /* store for use during create */
624 dentry->d_fsdata = &attr;
627 mode &= ~current->fs->umask;
628 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
629 dentry->d_fsdata = NULL;
633 return dentry_open(dentry, mqueue_mnt, oflag);
641 /* Opens existing queue */
642 static struct file *do_open(struct dentry *dentry, int oflag)
644 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
645 MAY_READ | MAY_WRITE };
647 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
650 return ERR_PTR(-EINVAL);
653 if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL)) {
656 return ERR_PTR(-EACCES);
659 return dentry_open(dentry, mqueue_mnt, oflag);
662 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
663 struct mq_attr __user *u_attr)
665 struct dentry *dentry;
670 if (IS_ERR(name = getname(u_name)))
671 return PTR_ERR(name);
673 fd = get_unused_fd();
677 mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
678 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
679 if (IS_ERR(dentry)) {
680 error = PTR_ERR(dentry);
685 if (oflag & O_CREAT) {
686 if (dentry->d_inode) { /* entry already exists */
690 filp = do_open(dentry, oflag);
692 filp = do_create(mqueue_mnt->mnt_root, dentry,
693 oflag, mode, u_attr);
697 if (!dentry->d_inode)
699 filp = do_open(dentry, oflag);
703 error = PTR_ERR(filp);
707 set_close_on_exec(fd, 1);
708 fd_install(fd, filp);
719 mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
725 asmlinkage long sys_mq_unlink(const char __user *u_name)
729 struct dentry *dentry;
730 struct inode *inode = NULL;
732 name = getname(u_name);
734 return PTR_ERR(name);
736 mutex_lock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
737 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
738 if (IS_ERR(dentry)) {
739 err = PTR_ERR(dentry);
743 if (!dentry->d_inode) {
748 inode = dentry->d_inode;
750 atomic_inc(&inode->i_count);
752 err = vfs_unlink(dentry->d_parent->d_inode, dentry, NULL);
757 mutex_unlock(&mqueue_mnt->mnt_root->d_inode->i_mutex);
765 /* Pipelined send and receive functions.
767 * If a receiver finds no waiting message, then it registers itself in the
768 * list of waiting receivers. A sender checks that list before adding the new
769 * message into the message array. If there is a waiting receiver, then it
770 * bypasses the message array and directly hands the message over to the
772 * The receiver accepts the message and returns without grabbing the queue
773 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
774 * are necessary. The same algorithm is used for sysv semaphores, see
775 * ipc/sem.c for more details.
777 * The same algorithm is used for senders.
780 /* pipelined_send() - send a message directly to the task waiting in
781 * sys_mq_timedreceive() (without inserting message into a queue).
783 static inline void pipelined_send(struct mqueue_inode_info *info,
784 struct msg_msg *message,
785 struct ext_wait_queue *receiver)
787 receiver->msg = message;
788 list_del(&receiver->list);
789 receiver->state = STATE_PENDING;
790 wake_up_process(receiver->task);
792 receiver->state = STATE_READY;
795 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
796 * gets its message and put to the queue (we have one free place for sure). */
797 static inline void pipelined_receive(struct mqueue_inode_info *info)
799 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
803 wake_up_interruptible(&info->wait_q);
806 msg_insert(sender->msg, info);
807 list_del(&sender->list);
808 sender->state = STATE_PENDING;
809 wake_up_process(sender->task);
811 sender->state = STATE_READY;
814 asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
815 size_t msg_len, unsigned int msg_prio,
816 const struct timespec __user *u_abs_timeout)
820 struct ext_wait_queue wait;
821 struct ext_wait_queue *receiver;
822 struct msg_msg *msg_ptr;
823 struct mqueue_inode_info *info;
827 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
830 timeout = prepare_timeout(u_abs_timeout);
837 inode = filp->f_dentry->d_inode;
838 if (unlikely(filp->f_op != &mqueue_file_operations))
840 info = MQUEUE_I(inode);
842 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
845 if (unlikely(msg_len > info->attr.mq_msgsize)) {
850 /* First try to allocate memory, before doing anything with
851 * existing queues. */
852 msg_ptr = load_msg(u_msg_ptr, msg_len);
853 if (IS_ERR(msg_ptr)) {
854 ret = PTR_ERR(msg_ptr);
857 msg_ptr->m_ts = msg_len;
858 msg_ptr->m_type = msg_prio;
860 spin_lock(&info->lock);
862 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
863 if (filp->f_flags & O_NONBLOCK) {
864 spin_unlock(&info->lock);
866 } else if (unlikely(timeout < 0)) {
867 spin_unlock(&info->lock);
871 wait.msg = (void *) msg_ptr;
872 wait.state = STATE_NONE;
873 ret = wq_sleep(info, SEND, timeout, &wait);
878 receiver = wq_get_first_waiter(info, RECV);
880 pipelined_send(info, msg_ptr, receiver);
882 /* adds message to the queue */
883 msg_insert(msg_ptr, info);
886 inode->i_atime = inode->i_mtime = inode->i_ctime =
888 spin_unlock(&info->lock);
897 asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
898 size_t msg_len, unsigned int __user *u_msg_prio,
899 const struct timespec __user *u_abs_timeout)
903 struct msg_msg *msg_ptr;
906 struct mqueue_inode_info *info;
907 struct ext_wait_queue wait;
909 timeout = prepare_timeout(u_abs_timeout);
916 inode = filp->f_dentry->d_inode;
917 if (unlikely(filp->f_op != &mqueue_file_operations))
919 info = MQUEUE_I(inode);
921 if (unlikely(!(filp->f_mode & FMODE_READ)))
924 /* checks if buffer is big enough */
925 if (unlikely(msg_len < info->attr.mq_msgsize)) {
930 spin_lock(&info->lock);
931 if (info->attr.mq_curmsgs == 0) {
932 if (filp->f_flags & O_NONBLOCK) {
933 spin_unlock(&info->lock);
936 } else if (unlikely(timeout < 0)) {
937 spin_unlock(&info->lock);
942 wait.state = STATE_NONE;
943 ret = wq_sleep(info, RECV, timeout, &wait);
947 msg_ptr = msg_get(info);
949 inode->i_atime = inode->i_mtime = inode->i_ctime =
952 /* There is now free space in queue. */
953 pipelined_receive(info);
954 spin_unlock(&info->lock);
960 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
961 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
973 * Notes: the case when user wants us to deregister (with NULL as pointer)
974 * and he isn't currently owner of notification, will be silently discarded.
975 * It isn't explicitly defined in the POSIX.
977 asmlinkage long sys_mq_notify(mqd_t mqdes,
978 const struct sigevent __user *u_notification)
984 struct sigevent notification;
985 struct mqueue_inode_info *info;
990 if (u_notification != NULL) {
991 if (copy_from_user(¬ification, u_notification,
992 sizeof(struct sigevent)))
995 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
996 notification.sigev_notify != SIGEV_SIGNAL &&
997 notification.sigev_notify != SIGEV_THREAD))
999 if (notification.sigev_notify == SIGEV_SIGNAL &&
1000 !valid_signal(notification.sigev_signo)) {
1003 if (notification.sigev_notify == SIGEV_THREAD) {
1004 /* create the notify skb */
1005 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
1010 if (copy_from_user(nc->data,
1011 notification.sigev_value.sival_ptr,
1012 NOTIFY_COOKIE_LEN)) {
1016 /* TODO: add a header? */
1017 skb_put(nc, NOTIFY_COOKIE_LEN);
1018 /* and attach it to the socket */
1020 filp = fget(notification.sigev_signo);
1024 sock = netlink_getsockbyfilp(filp);
1027 ret = PTR_ERR(sock);
1032 ret = netlink_attachskb(sock, nc, 0,
1033 MAX_SCHEDULE_TIMEOUT, NULL);
1049 inode = filp->f_dentry->d_inode;
1050 if (unlikely(filp->f_op != &mqueue_file_operations))
1052 info = MQUEUE_I(inode);
1055 spin_lock(&info->lock);
1056 if (u_notification == NULL) {
1057 if (info->notify_owner == current->tgid) {
1058 remove_notification(info);
1059 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1061 } else if (info->notify_owner != 0) {
1064 switch (notification.sigev_notify) {
1066 info->notify.sigev_notify = SIGEV_NONE;
1069 info->notify_sock = sock;
1070 info->notify_cookie = nc;
1073 info->notify.sigev_notify = SIGEV_THREAD;
1076 info->notify.sigev_signo = notification.sigev_signo;
1077 info->notify.sigev_value = notification.sigev_value;
1078 info->notify.sigev_notify = SIGEV_SIGNAL;
1081 info->notify_owner = current->tgid;
1082 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1084 spin_unlock(&info->lock);
1089 netlink_detachskb(sock, nc);
1096 asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
1097 const struct mq_attr __user *u_mqstat,
1098 struct mq_attr __user *u_omqstat)
1101 struct mq_attr mqstat, omqstat;
1103 struct inode *inode;
1104 struct mqueue_inode_info *info;
1106 if (u_mqstat != NULL) {
1107 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1109 if (mqstat.mq_flags & (~O_NONBLOCK))
1118 inode = filp->f_dentry->d_inode;
1119 if (unlikely(filp->f_op != &mqueue_file_operations))
1121 info = MQUEUE_I(inode);
1123 spin_lock(&info->lock);
1125 omqstat = info->attr;
1126 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1128 if (mqstat.mq_flags & O_NONBLOCK)
1129 filp->f_flags |= O_NONBLOCK;
1131 filp->f_flags &= ~O_NONBLOCK;
1133 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1136 spin_unlock(&info->lock);
1139 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1140 sizeof(struct mq_attr)))
1149 static struct inode_operations mqueue_dir_inode_operations = {
1150 .lookup = simple_lookup,
1151 .create = mqueue_create,
1152 .unlink = mqueue_unlink,
1155 static struct file_operations mqueue_file_operations = {
1156 .flush = mqueue_flush_file,
1157 .poll = mqueue_poll_file,
1158 .read = mqueue_read_file,
1161 static struct super_operations mqueue_super_ops = {
1162 .alloc_inode = mqueue_alloc_inode,
1163 .destroy_inode = mqueue_destroy_inode,
1164 .statfs = simple_statfs,
1165 .delete_inode = mqueue_delete_inode,
1166 .drop_inode = generic_delete_inode,
1169 static struct file_system_type mqueue_fs_type = {
1171 .get_sb = mqueue_get_sb,
1172 .kill_sb = kill_litter_super,
1175 static int msg_max_limit_min = DFLT_MSGMAX;
1176 static int msg_max_limit_max = HARD_MSGMAX;
1178 static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
1179 static int msg_maxsize_limit_max = INT_MAX;
1181 static ctl_table mq_sysctls[] = {
1183 .ctl_name = CTL_QUEUESMAX,
1184 .procname = "queues_max",
1185 .data = &queues_max,
1186 .maxlen = sizeof(int),
1188 .proc_handler = &proc_dointvec,
1191 .ctl_name = CTL_MSGMAX,
1192 .procname = "msg_max",
1194 .maxlen = sizeof(int),
1196 .proc_handler = &proc_dointvec_minmax,
1197 .extra1 = &msg_max_limit_min,
1198 .extra2 = &msg_max_limit_max,
1201 .ctl_name = CTL_MSGSIZEMAX,
1202 .procname = "msgsize_max",
1203 .data = &msgsize_max,
1204 .maxlen = sizeof(int),
1206 .proc_handler = &proc_dointvec_minmax,
1207 .extra1 = &msg_maxsize_limit_min,
1208 .extra2 = &msg_maxsize_limit_max,
1213 static ctl_table mq_sysctl_dir[] = {
1215 .ctl_name = FS_MQUEUE,
1216 .procname = "mqueue",
1218 .child = mq_sysctls,
1223 static ctl_table mq_sysctl_root[] = {
1228 .child = mq_sysctl_dir,
1233 static int __init init_mqueue_fs(void)
1237 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1238 sizeof(struct mqueue_inode_info), 0,
1239 SLAB_HWCACHE_ALIGN, init_once, NULL);
1240 if (mqueue_inode_cachep == NULL)
1243 /* ignore failues - they are not fatal */
1244 mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
1246 error = register_filesystem(&mqueue_fs_type);
1250 if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
1251 error = PTR_ERR(mqueue_mnt);
1252 goto out_filesystem;
1255 /* internal initialization - not common for vfs */
1257 spin_lock_init(&mq_lock);
1262 unregister_filesystem(&mqueue_fs_type);
1264 if (mq_sysctl_table)
1265 unregister_sysctl_table(mq_sysctl_table);
1266 if (kmem_cache_destroy(mqueue_inode_cachep)) {
1268 "mqueue_inode_cache: not all structures were freed\n");
1273 __initcall(init_mqueue_fs);