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>
28 #define MQUEUE_MAGIC 0x19800202
29 #define DIRENT_SIZE 20
30 #define FILENT_SIZE 80
36 #define STATE_PENDING 1
41 #define CTL_QUEUESMAX 2
43 #define CTL_MSGSIZEMAX 4
46 #define DFLT_QUEUESMAX 64 /* max number of message queues */
47 #define DFLT_MSGMAX 40 /* max number of messages in each queue */
48 #define HARD_MSGMAX (131072/sizeof(void*))
49 #define DFLT_MSGSIZEMAX 16384 /* max message size */
51 #define NOTIFY_COOKIE_LEN 32
53 struct ext_wait_queue { /* queue of sleeping tasks */
54 struct task_struct *task;
55 struct list_head list;
56 struct msg_msg *msg; /* ptr of loaded message */
57 int state; /* one of STATE_* values */
60 struct mqueue_inode_info {
62 struct inode vfs_inode;
63 wait_queue_head_t wait_q;
65 struct msg_msg **messages;
68 struct sigevent notify;
70 struct sock *notify_sock;
71 struct sk_buff *notify_cookie;
73 /* for tasks waiting for free space and messages, respectively */
74 struct ext_wait_queue e_wait_q[2];
76 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
79 static struct inode_operations mqueue_dir_inode_operations;
80 static struct file_operations mqueue_file_operations;
81 static struct super_operations mqueue_super_ops;
82 static void remove_notification(struct mqueue_inode_info *info);
84 static spinlock_t mq_lock;
85 static kmem_cache_t *mqueue_inode_cachep;
86 static struct vfsmount *mqueue_mnt;
88 static unsigned int queues_count;
89 static unsigned int queues_max = DFLT_QUEUESMAX;
90 static unsigned int msg_max = DFLT_MSGMAX;
91 static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
93 static struct ctl_table_header * mq_sysctl_table;
95 static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
97 return container_of(inode, struct mqueue_inode_info, vfs_inode);
100 static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
101 struct mq_attr *attr)
105 inode = new_inode(sb);
107 inode->i_mode = mode;
108 inode->i_uid = current->fsuid;
109 inode->i_gid = current->fsgid;
110 inode->i_blksize = PAGE_CACHE_SIZE;
112 inode->i_mtime = inode->i_ctime = inode->i_atime =
116 struct mqueue_inode_info *info;
118 inode->i_fop = &mqueue_file_operations;
119 inode->i_size = FILENT_SIZE;
120 /* mqueue specific info */
121 info = MQUEUE_I(inode);
122 spin_lock_init(&info->lock);
123 init_waitqueue_head(&info->wait_q);
124 INIT_LIST_HEAD(&info->e_wait_q[0].list);
125 INIT_LIST_HEAD(&info->e_wait_q[1].list);
126 info->notify_owner = 0;
128 memset(&info->attr, 0, sizeof(info->attr));
129 info->attr.mq_maxmsg = DFLT_MSGMAX;
130 info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
132 info->attr.mq_maxmsg = attr->mq_maxmsg;
133 info->attr.mq_msgsize = attr->mq_msgsize;
135 info->messages = kmalloc(info->attr.mq_maxmsg * sizeof(struct msg_msg *), GFP_KERNEL);
136 if (!info->messages) {
137 make_bad_inode(inode);
141 } else if (S_ISDIR(mode)) {
143 /* Some things misbehave if size == 0 on a directory */
144 inode->i_size = 2 * DIRENT_SIZE;
145 inode->i_op = &mqueue_dir_inode_operations;
146 inode->i_fop = &simple_dir_operations;
152 static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
156 sb->s_blocksize = PAGE_CACHE_SIZE;
157 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
158 sb->s_magic = MQUEUE_MAGIC;
159 sb->s_op = &mqueue_super_ops;
161 inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
165 sb->s_root = d_alloc_root(inode);
174 static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
175 int flags, const char *dev_name,
178 return get_sb_single(fs_type, flags, data, mqueue_fill_super);
181 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
183 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
185 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
186 SLAB_CTOR_CONSTRUCTOR)
187 inode_init_once(&p->vfs_inode);
190 static struct inode *mqueue_alloc_inode(struct super_block *sb)
192 struct mqueue_inode_info *ei;
194 ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
197 return &ei->vfs_inode;
200 static void mqueue_destroy_inode(struct inode *inode)
202 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
205 static void mqueue_delete_inode(struct inode *inode)
207 struct mqueue_inode_info *info;
210 if (S_ISDIR(inode->i_mode)) {
214 info = MQUEUE_I(inode);
215 spin_lock(&info->lock);
216 for (i = 0; i < info->attr.mq_curmsgs; i++)
217 free_msg(info->messages[i]);
218 kfree(info->messages);
219 spin_unlock(&info->lock);
223 if (info->messages) {
226 spin_unlock(&mq_lock);
230 static int mqueue_create(struct inode *dir, struct dentry *dentry,
231 int mode, struct nameidata *nd)
234 struct mq_attr *attr = dentry->d_fsdata;
238 if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
243 spin_unlock(&mq_lock);
245 inode = mqueue_get_inode(dir->i_sb, mode, attr);
253 dir->i_size += DIRENT_SIZE;
254 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
256 d_instantiate(dentry, inode);
260 spin_unlock(&mq_lock);
264 static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
266 struct inode *inode = dentry->d_inode;
268 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
269 dir->i_size -= DIRENT_SIZE;
276 * This is routine for system read from queue file.
277 * To avoid mess with doing here some sort of mq_receive we allow
278 * to read only queue size & notification info (the only values
279 * that are interesting from user point of view and aren't accessible
280 * through std routines)
282 static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
283 size_t count, loff_t * off)
285 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
286 char buffer[FILENT_SIZE];
293 spin_lock(&info->lock);
294 snprintf(buffer, sizeof(buffer),
295 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
297 info->notify_owner ? info->notify.sigev_notify : 0,
298 (info->notify_owner &&
299 info->notify.sigev_notify == SIGEV_SIGNAL) ?
300 info->notify.sigev_signo : 0,
302 spin_unlock(&info->lock);
303 buffer[sizeof(buffer)-1] = '\0';
304 slen = strlen(buffer)+1;
310 if (o + count > slen)
313 if (copy_to_user(u_data, buffer + o, count))
317 filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
321 static int mqueue_flush_file(struct file *filp)
323 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
325 spin_lock(&info->lock);
326 if (current->tgid == info->notify_owner)
327 remove_notification(info);
329 spin_unlock(&info->lock);
333 static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
335 struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
338 poll_wait(filp, &info->wait_q, poll_tab);
340 spin_lock(&info->lock);
341 if (info->attr.mq_curmsgs)
342 retval = POLLIN | POLLRDNORM;
344 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
345 retval |= POLLOUT | POLLWRNORM;
346 spin_unlock(&info->lock);
351 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
352 static void wq_add(struct mqueue_inode_info *info, int sr,
353 struct ext_wait_queue *ewp)
355 struct ext_wait_queue *walk;
359 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
360 if (walk->task->static_prio <= current->static_prio) {
361 list_add_tail(&ewp->list, &walk->list);
365 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
369 * Puts current task to sleep. Caller must hold queue lock. After return
373 static int wq_sleep(struct mqueue_inode_info *info, int sr,
374 long timeout, struct ext_wait_queue *ewp)
379 wq_add(info, sr, ewp);
382 set_current_state(TASK_INTERRUPTIBLE);
384 spin_unlock(&info->lock);
385 time = schedule_timeout(timeout);
387 while (ewp->state == STATE_PENDING)
390 if (ewp->state == STATE_READY) {
394 spin_lock(&info->lock);
395 if (ewp->state == STATE_READY) {
399 if (signal_pending(current)) {
400 retval = -ERESTARTSYS;
408 list_del(&ewp->list);
410 spin_unlock(&info->lock);
416 * Returns waiting task that should be serviced first or NULL if none exists
418 static struct ext_wait_queue *wq_get_first_waiter(
419 struct mqueue_inode_info *info, int sr)
421 struct list_head *ptr;
423 ptr = info->e_wait_q[sr].list.prev;
424 if (ptr == &info->e_wait_q[sr].list)
426 return list_entry(ptr, struct ext_wait_queue, list);
429 /* Auxiliary functions to manipulate messages' list */
430 static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
434 k = info->attr.mq_curmsgs - 1;
435 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
436 info->messages[k + 1] = info->messages[k];
439 info->attr.mq_curmsgs++;
440 info->qsize += ptr->m_ts;
441 info->messages[k + 1] = ptr;
444 static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
446 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
447 return info->messages[info->attr.mq_curmsgs];
450 static inline void set_cookie(struct sk_buff *skb, char code)
452 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
456 * The next function is only to split too long sys_mq_timedsend
458 static void __do_notify(struct mqueue_inode_info *info)
461 * invoked when there is registered process and there isn't process
462 * waiting synchronously for message AND state of queue changed from
463 * empty to not empty. Here we are sure that no one is waiting
465 if (info->notify_owner &&
466 info->attr.mq_curmsgs == 1) {
467 struct siginfo sig_i;
468 switch (info->notify.sigev_notify) {
474 sig_i.si_signo = info->notify.sigev_signo;
476 sig_i.si_code = SI_MESGQ;
477 sig_i.si_value = info->notify.sigev_value;
478 sig_i.si_pid = current->tgid;
479 sig_i.si_uid = current->uid;
481 kill_proc_info(info->notify.sigev_signo,
482 &sig_i, info->notify_owner);
485 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
486 netlink_sendskb(info->notify_sock,
487 info->notify_cookie, 0);
490 /* after notification unregisters process */
491 info->notify_owner = 0;
493 wake_up(&info->wait_q);
496 static long prepare_timeout(const struct timespec __user *u_arg)
498 struct timespec ts, nowts;
502 if (unlikely(copy_from_user(&ts, u_arg,
503 sizeof(struct timespec))))
506 if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
507 || ts.tv_nsec >= NSEC_PER_SEC))
509 nowts = CURRENT_TIME;
510 /* first subtract as jiffies can't be too big */
511 ts.tv_sec -= nowts.tv_sec;
512 if (ts.tv_nsec < nowts.tv_nsec) {
513 ts.tv_nsec += NSEC_PER_SEC;
516 ts.tv_nsec -= nowts.tv_nsec;
520 timeout = timespec_to_jiffies(&ts) + 1;
522 return MAX_SCHEDULE_TIMEOUT;
527 static void remove_notification(struct mqueue_inode_info *info)
529 if (info->notify_owner != 0 &&
530 info->notify.sigev_notify == SIGEV_THREAD) {
531 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
532 netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
534 info->notify_owner = 0;
538 * Invoked when creating a new queue via sys_mq_open
540 static struct file *do_create(struct dentry *dir, struct dentry *dentry,
541 int oflag, mode_t mode, struct mq_attr __user *u_attr)
547 if (u_attr != NULL) {
548 if (copy_from_user(&attr, u_attr, sizeof(attr)))
549 return ERR_PTR(-EFAULT);
551 if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0)
552 return ERR_PTR(-EINVAL);
553 if (capable(CAP_SYS_RESOURCE)) {
554 if (attr.mq_maxmsg > HARD_MSGMAX)
555 return ERR_PTR(-EINVAL);
557 if (attr.mq_maxmsg > msg_max ||
558 attr.mq_msgsize > msgsize_max)
559 return ERR_PTR(-EINVAL);
561 /* store for use during create */
562 dentry->d_fsdata = &attr;
565 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
566 dentry->d_fsdata = NULL;
570 filp = dentry_open(dentry, mqueue_mnt, oflag);
577 /* Opens existing queue */
578 static struct file *do_open(struct dentry *dentry, int oflag)
580 static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
581 MAY_READ | MAY_WRITE };
584 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
585 return ERR_PTR(-EINVAL);
587 if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL))
588 return ERR_PTR(-EACCES);
590 filp = dentry_open(dentry, mqueue_mnt, oflag);
598 asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
599 struct mq_attr __user *u_attr)
601 struct dentry *dentry;
606 if (IS_ERR(name = getname(u_name)))
607 return PTR_ERR(name);
609 fd = get_unused_fd();
613 down(&mqueue_mnt->mnt_root->d_inode->i_sem);
614 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
615 if (IS_ERR(dentry)) {
616 error = PTR_ERR(dentry);
621 if (oflag & O_CREAT) {
622 if (dentry->d_inode) { /* entry already exists */
623 filp = (oflag & O_EXCL) ? ERR_PTR(-EEXIST) :
624 do_open(dentry, oflag);
626 filp = do_create(mqueue_mnt->mnt_root, dentry,
627 oflag, mode, u_attr);
630 filp = (dentry->d_inode) ? do_open(dentry, oflag) :
636 error = PTR_ERR(filp);
640 set_close_on_exec(fd, 1);
641 fd_install(fd, filp);
650 up(&mqueue_mnt->mnt_root->d_inode->i_sem);
656 asmlinkage long sys_mq_unlink(const char __user *u_name)
660 struct dentry *dentry;
661 struct inode *inode = NULL;
663 name = getname(u_name);
665 return PTR_ERR(name);
667 down(&mqueue_mnt->mnt_root->d_inode->i_sem);
668 dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
669 if (IS_ERR(dentry)) {
670 err = PTR_ERR(dentry);
674 if (!dentry->d_inode) {
679 inode = dentry->d_inode;
681 atomic_inc(&inode->i_count);
683 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
688 up(&mqueue_mnt->mnt_root->d_inode->i_sem);
696 /* Pipelined send and receive functions.
698 * If a receiver finds no waiting message, then it registers itself in the
699 * list of waiting receivers. A sender checks that list before adding the new
700 * message into the message array. If there is a waiting receiver, then it
701 * bypasses the message array and directly hands the message over to the
703 * The receiver accepts the message and returns without grabbing the queue
704 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
705 * are necessary. The same algorithm is used for sysv semaphores, see
706 * ipc/sem.c fore more details.
708 * The same algorithm is used for senders.
711 /* pipelined_send() - send a message directly to the task waiting in
712 * sys_mq_timedreceive() (without inserting message into a queue).
714 static inline void pipelined_send(struct mqueue_inode_info *info,
715 struct msg_msg *message,
716 struct ext_wait_queue *receiver)
718 receiver->msg = message;
719 list_del(&receiver->list);
720 receiver->state = STATE_PENDING;
721 wake_up_process(receiver->task);
723 receiver->state = STATE_READY;
726 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
727 * gets its message and put to the queue (we have one free place for sure). */
728 static inline void pipelined_receive(struct mqueue_inode_info *info)
730 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
734 wake_up_interruptible(&info->wait_q);
737 msg_insert(sender->msg, info);
738 list_del(&sender->list);
739 sender->state = STATE_PENDING;
740 wake_up_process(sender->task);
742 sender->state = STATE_READY;
745 asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
746 size_t msg_len, unsigned int msg_prio,
747 const struct timespec __user *u_abs_timeout)
751 struct ext_wait_queue wait;
752 struct ext_wait_queue *receiver;
753 struct msg_msg *msg_ptr;
754 struct mqueue_inode_info *info;
758 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
761 timeout = prepare_timeout(u_abs_timeout);
768 inode = filp->f_dentry->d_inode;
769 if (unlikely(filp->f_op != &mqueue_file_operations))
771 info = MQUEUE_I(inode);
773 if (unlikely(!(filp->f_mode & FMODE_WRITE)))
776 if (unlikely(msg_len > info->attr.mq_msgsize)) {
781 /* First try to allocate memory, before doing anything with
782 * existing queues. */
783 msg_ptr = load_msg(u_msg_ptr, msg_len);
784 if (unlikely(IS_ERR(msg_ptr))) {
785 ret = PTR_ERR(msg_ptr);
788 msg_ptr->m_ts = msg_len;
789 msg_ptr->m_type = msg_prio;
791 spin_lock(&info->lock);
793 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
794 if (filp->f_flags & O_NONBLOCK) {
795 spin_unlock(&info->lock);
797 } else if (unlikely(timeout < 0)) {
798 spin_unlock(&info->lock);
802 wait.msg = (void *) msg_ptr;
803 wait.state = STATE_NONE;
804 ret = wq_sleep(info, SEND, timeout, &wait);
809 receiver = wq_get_first_waiter(info, RECV);
811 pipelined_send(info, msg_ptr, receiver);
813 /* adds message to the queue */
814 msg_insert(msg_ptr, info);
817 inode->i_atime = inode->i_mtime = inode->i_ctime =
819 spin_unlock(&info->lock);
828 asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
829 size_t msg_len, unsigned int __user *u_msg_prio,
830 const struct timespec __user *u_abs_timeout)
834 struct msg_msg *msg_ptr;
837 struct mqueue_inode_info *info;
838 struct ext_wait_queue wait;
840 timeout = prepare_timeout(u_abs_timeout);
847 inode = filp->f_dentry->d_inode;
848 if (unlikely(filp->f_op != &mqueue_file_operations))
850 info = MQUEUE_I(inode);
852 if (unlikely(!(filp->f_mode & FMODE_READ)))
855 /* checks if buffer is big enough */
856 if (unlikely(msg_len < info->attr.mq_msgsize)) {
861 spin_lock(&info->lock);
862 if (info->attr.mq_curmsgs == 0) {
863 if (filp->f_flags & O_NONBLOCK) {
864 spin_unlock(&info->lock);
867 } else if (unlikely(timeout < 0)) {
868 spin_unlock(&info->lock);
873 wait.state = STATE_NONE;
874 ret = wq_sleep(info, RECV, timeout, &wait);
878 msg_ptr = msg_get(info);
880 inode->i_atime = inode->i_mtime = inode->i_ctime =
883 /* There is now free space in queue. */
884 pipelined_receive(info);
885 spin_unlock(&info->lock);
891 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
892 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
904 * Notes: the case when user wants us to deregister (with NULL as pointer)
905 * and he isn't currently owner of notification, will be silently discarded.
906 * It isn't explicitly defined in the POSIX.
908 asmlinkage long sys_mq_notify(mqd_t mqdes,
909 const struct sigevent __user *u_notification)
915 struct sigevent notification;
916 struct mqueue_inode_info *info;
921 if (u_notification != NULL) {
922 if (copy_from_user(¬ification, u_notification,
923 sizeof(struct sigevent)))
926 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
927 notification.sigev_notify != SIGEV_SIGNAL &&
928 notification.sigev_notify != SIGEV_THREAD))
930 if (notification.sigev_notify == SIGEV_SIGNAL &&
931 (notification.sigev_signo < 0 ||
932 notification.sigev_signo > _NSIG)) {
935 if (notification.sigev_notify == SIGEV_THREAD) {
936 /* create the notify skb */
937 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
942 if (copy_from_user(nc->data,
943 notification.sigev_value.sival_ptr,
944 NOTIFY_COOKIE_LEN)) {
948 /* TODO: add a header? */
949 skb_put(nc, NOTIFY_COOKIE_LEN);
950 /* and attach it to the socket */
952 filp = fget(notification.sigev_signo);
956 sock = netlink_getsockbyfilp(filp);
964 ret = netlink_attachskb(sock, nc, 0, MAX_SCHEDULE_TIMEOUT);
980 inode = filp->f_dentry->d_inode;
981 if (unlikely(filp->f_op != &mqueue_file_operations))
983 info = MQUEUE_I(inode);
986 spin_lock(&info->lock);
987 if (u_notification == NULL) {
988 if (info->notify_owner == current->tgid) {
989 remove_notification(info);
990 inode->i_atime = inode->i_ctime = CURRENT_TIME;
992 } else if (info->notify_owner != 0) {
995 switch (notification.sigev_notify) {
997 info->notify.sigev_notify = SIGEV_NONE;
1000 info->notify_sock = sock;
1001 info->notify_cookie = nc;
1004 info->notify.sigev_notify = SIGEV_THREAD;
1007 info->notify.sigev_signo = notification.sigev_signo;
1008 info->notify.sigev_value = notification.sigev_value;
1009 info->notify.sigev_notify = SIGEV_SIGNAL;
1012 info->notify_owner = current->tgid;
1013 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1015 spin_unlock(&info->lock);
1020 netlink_detachskb(sock, nc);
1027 asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
1028 const struct mq_attr __user *u_mqstat,
1029 struct mq_attr __user *u_omqstat)
1032 struct mq_attr mqstat, omqstat;
1034 struct inode *inode;
1035 struct mqueue_inode_info *info;
1037 if (u_mqstat != NULL) {
1038 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1040 if (mqstat.mq_flags & (~O_NONBLOCK))
1049 inode = filp->f_dentry->d_inode;
1050 if (unlikely(filp->f_op != &mqueue_file_operations))
1052 info = MQUEUE_I(inode);
1054 spin_lock(&info->lock);
1056 omqstat = info->attr;
1057 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1059 if (mqstat.mq_flags & O_NONBLOCK)
1060 filp->f_flags |= O_NONBLOCK;
1062 filp->f_flags &= ~O_NONBLOCK;
1064 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1067 spin_unlock(&info->lock);
1070 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1071 sizeof(struct mq_attr)))
1080 static struct inode_operations mqueue_dir_inode_operations = {
1081 .lookup = simple_lookup,
1082 .create = mqueue_create,
1083 .unlink = mqueue_unlink,
1086 static struct file_operations mqueue_file_operations = {
1087 .flush = mqueue_flush_file,
1088 .poll = mqueue_poll_file,
1089 .read = mqueue_read_file,
1092 static struct super_operations mqueue_super_ops = {
1093 .alloc_inode = mqueue_alloc_inode,
1094 .destroy_inode = mqueue_destroy_inode,
1095 .statfs = simple_statfs,
1096 .delete_inode = mqueue_delete_inode,
1097 .drop_inode = generic_delete_inode,
1100 static struct file_system_type mqueue_fs_type = {
1102 .get_sb = mqueue_get_sb,
1103 .kill_sb = kill_litter_super,
1106 static int msg_max_limit_min = DFLT_MSGMAX;
1107 static int msg_max_limit_max = HARD_MSGMAX;
1109 static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
1110 static int msg_maxsize_limit_max = INT_MAX;
1112 static ctl_table mq_sysctls[] = {
1114 .ctl_name = CTL_QUEUESMAX,
1115 .procname = "queues_max",
1116 .data = &queues_max,
1117 .maxlen = sizeof(int),
1119 .proc_handler = &proc_dointvec,
1122 .ctl_name = CTL_MSGMAX,
1123 .procname = "msg_max",
1125 .maxlen = sizeof(int),
1127 .proc_handler = &proc_dointvec_minmax,
1128 .extra1 = &msg_max_limit_min,
1129 .extra2 = &msg_max_limit_max,
1132 .ctl_name = CTL_MSGSIZEMAX,
1133 .procname = "msgsize_max",
1134 .data = &msgsize_max,
1135 .maxlen = sizeof(int),
1137 .proc_handler = &proc_dointvec_minmax,
1138 .extra1 = &msg_maxsize_limit_min,
1139 .extra2 = &msg_maxsize_limit_max,
1144 static ctl_table mq_sysctl_dir[] = {
1146 .ctl_name = FS_MQUEUE,
1147 .procname = "mqueue",
1149 .child = mq_sysctls,
1154 static ctl_table mq_sysctl_root[] = {
1159 .child = mq_sysctl_dir,
1164 static int __init init_mqueue_fs(void)
1168 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1169 sizeof(struct mqueue_inode_info), 0,
1170 SLAB_HWCACHE_ALIGN, init_once, NULL);
1171 if (mqueue_inode_cachep == NULL)
1174 mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
1175 if (!mq_sysctl_table) {
1180 error = register_filesystem(&mqueue_fs_type);
1184 if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
1185 error = PTR_ERR(mqueue_mnt);
1186 goto out_filesystem;
1189 /* internal initialization - not common for vfs */
1191 spin_lock_init(&mq_lock);
1196 unregister_filesystem(&mqueue_fs_type);
1198 unregister_sysctl_table(mq_sysctl_table);
1200 if (kmem_cache_destroy(mqueue_inode_cachep)) {
1202 "mqueue_inode_cache: not all structures were freed\n");
1207 __initcall(init_mqueue_fs);