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>
30 #define MQUEUE_MAGIC 0x19800202
31 #define DIRENT_SIZE 20
32 #define FILENT_SIZE 80
38 #define STATE_PENDING 1
43 #define CTL_QUEUESMAX 2
45 #define CTL_MSGSIZEMAX 4
48 #define DFLT_QUEUESMAX 256 /* max number of message queues */
49 #define DFLT_MSGMAX 10 /* max number of messages in each queue */
50 #define HARD_MSGMAX (131072/sizeof(void*))
51 #define DFLT_MSGSIZEMAX 8192 /* max message size */
53 #define NOTIFY_COOKIE_LEN 32
55 struct ext_wait_queue
{ /* queue of sleeping tasks */
56 struct task_struct
*task
;
57 struct list_head list
;
58 struct msg_msg
*msg
; /* ptr of loaded message */
59 int state
; /* one of STATE_* values */
62 struct mqueue_inode_info
{
64 struct inode vfs_inode
;
65 wait_queue_head_t wait_q
;
67 struct msg_msg
**messages
;
70 struct sigevent notify
;
72 struct user_struct
*user
; /* user who created, for accounting */
73 struct sock
*notify_sock
;
74 struct sk_buff
*notify_cookie
;
76 /* for tasks waiting for free space and messages, respectively */
77 struct ext_wait_queue e_wait_q
[2];
79 unsigned long qsize
; /* size of queue in memory (sum of all msgs) */
82 static struct inode_operations mqueue_dir_inode_operations
;
83 static struct file_operations mqueue_file_operations
;
84 static struct super_operations mqueue_super_ops
;
85 static void remove_notification(struct mqueue_inode_info
*info
);
87 static spinlock_t mq_lock
;
88 static kmem_cache_t
*mqueue_inode_cachep
;
89 static struct vfsmount
*mqueue_mnt
;
91 static unsigned int queues_count
;
92 static unsigned int queues_max
= DFLT_QUEUESMAX
;
93 static unsigned int msg_max
= DFLT_MSGMAX
;
94 static unsigned int msgsize_max
= DFLT_MSGSIZEMAX
;
96 static struct ctl_table_header
* mq_sysctl_table
;
98 static inline struct mqueue_inode_info
*MQUEUE_I(struct inode
*inode
)
100 return container_of(inode
, struct mqueue_inode_info
, vfs_inode
);
103 static struct inode
*mqueue_get_inode(struct super_block
*sb
, int mode
,
104 struct mq_attr
*attr
)
108 inode
= new_inode(sb
);
110 inode
->i_mode
= mode
;
111 inode
->i_uid
= current
->fsuid
;
112 inode
->i_gid
= current
->fsgid
;
113 inode
->i_blksize
= PAGE_CACHE_SIZE
;
115 inode
->i_mtime
= inode
->i_ctime
= inode
->i_atime
=
119 struct mqueue_inode_info
*info
;
120 struct task_struct
*p
= current
;
121 struct user_struct
*u
= p
->user
;
122 unsigned long mq_bytes
, mq_msg_tblsz
;
124 inode
->i_fop
= &mqueue_file_operations
;
125 inode
->i_size
= FILENT_SIZE
;
126 /* mqueue specific info */
127 info
= MQUEUE_I(inode
);
128 spin_lock_init(&info
->lock
);
129 init_waitqueue_head(&info
->wait_q
);
130 INIT_LIST_HEAD(&info
->e_wait_q
[0].list
);
131 INIT_LIST_HEAD(&info
->e_wait_q
[1].list
);
132 info
->messages
= NULL
;
133 info
->notify_owner
= 0;
135 info
->user
= NULL
; /* set when all is ok */
136 memset(&info
->attr
, 0, sizeof(info
->attr
));
137 info
->attr
.mq_maxmsg
= DFLT_MSGMAX
;
138 info
->attr
.mq_msgsize
= DFLT_MSGSIZEMAX
;
140 info
->attr
.mq_maxmsg
= attr
->mq_maxmsg
;
141 info
->attr
.mq_msgsize
= attr
->mq_msgsize
;
143 mq_msg_tblsz
= info
->attr
.mq_maxmsg
* sizeof(struct msg_msg
*);
144 mq_bytes
= (mq_msg_tblsz
+
145 (info
->attr
.mq_maxmsg
* info
->attr
.mq_msgsize
));
148 if (u
->mq_bytes
+ mq_bytes
< u
->mq_bytes
||
149 u
->mq_bytes
+ mq_bytes
>
150 p
->signal
->rlim
[RLIMIT_MSGQUEUE
].rlim_cur
) {
151 spin_unlock(&mq_lock
);
154 u
->mq_bytes
+= mq_bytes
;
155 spin_unlock(&mq_lock
);
157 info
->messages
= kmalloc(mq_msg_tblsz
, GFP_KERNEL
);
158 if (!info
->messages
) {
160 u
->mq_bytes
-= mq_bytes
;
161 spin_unlock(&mq_lock
);
165 info
->user
= get_uid(u
);
166 } else if (S_ISDIR(mode
)) {
168 /* Some things misbehave if size == 0 on a directory */
169 inode
->i_size
= 2 * DIRENT_SIZE
;
170 inode
->i_op
= &mqueue_dir_inode_operations
;
171 inode
->i_fop
= &simple_dir_operations
;
176 make_bad_inode(inode
);
181 static int mqueue_fill_super(struct super_block
*sb
, void *data
, int silent
)
185 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
186 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
187 sb
->s_magic
= MQUEUE_MAGIC
;
188 sb
->s_op
= &mqueue_super_ops
;
190 inode
= mqueue_get_inode(sb
, S_IFDIR
| S_ISVTX
| S_IRWXUGO
, NULL
);
194 sb
->s_root
= d_alloc_root(inode
);
203 static struct super_block
*mqueue_get_sb(struct file_system_type
*fs_type
,
204 int flags
, const char *dev_name
,
207 return get_sb_single(fs_type
, flags
, data
, mqueue_fill_super
);
210 static void init_once(void *foo
, kmem_cache_t
* cachep
, unsigned long flags
)
212 struct mqueue_inode_info
*p
= (struct mqueue_inode_info
*) foo
;
214 if ((flags
& (SLAB_CTOR_VERIFY
| SLAB_CTOR_CONSTRUCTOR
)) ==
215 SLAB_CTOR_CONSTRUCTOR
)
216 inode_init_once(&p
->vfs_inode
);
219 static struct inode
*mqueue_alloc_inode(struct super_block
*sb
)
221 struct mqueue_inode_info
*ei
;
223 ei
= kmem_cache_alloc(mqueue_inode_cachep
, SLAB_KERNEL
);
226 return &ei
->vfs_inode
;
229 static void mqueue_destroy_inode(struct inode
*inode
)
231 kmem_cache_free(mqueue_inode_cachep
, MQUEUE_I(inode
));
234 static void mqueue_delete_inode(struct inode
*inode
)
236 struct mqueue_inode_info
*info
;
237 struct user_struct
*user
;
238 unsigned long mq_bytes
;
241 if (S_ISDIR(inode
->i_mode
)) {
245 info
= MQUEUE_I(inode
);
246 spin_lock(&info
->lock
);
247 for (i
= 0; i
< info
->attr
.mq_curmsgs
; i
++)
248 free_msg(info
->messages
[i
]);
249 kfree(info
->messages
);
250 spin_unlock(&info
->lock
);
254 mq_bytes
= (info
->attr
.mq_maxmsg
* sizeof(struct msg_msg
*) +
255 (info
->attr
.mq_maxmsg
* info
->attr
.mq_msgsize
));
259 user
->mq_bytes
-= mq_bytes
;
261 spin_unlock(&mq_lock
);
266 static int mqueue_create(struct inode
*dir
, struct dentry
*dentry
,
267 int mode
, struct nameidata
*nd
)
270 struct mq_attr
*attr
= dentry
->d_fsdata
;
274 if (queues_count
>= queues_max
&& !capable(CAP_SYS_RESOURCE
)) {
279 spin_unlock(&mq_lock
);
281 inode
= mqueue_get_inode(dir
->i_sb
, mode
, attr
);
289 dir
->i_size
+= DIRENT_SIZE
;
290 dir
->i_ctime
= dir
->i_mtime
= dir
->i_atime
= CURRENT_TIME
;
292 d_instantiate(dentry
, inode
);
296 spin_unlock(&mq_lock
);
300 static int mqueue_unlink(struct inode
*dir
, struct dentry
*dentry
)
302 struct inode
*inode
= dentry
->d_inode
;
304 dir
->i_ctime
= dir
->i_mtime
= dir
->i_atime
= CURRENT_TIME
;
305 dir
->i_size
-= DIRENT_SIZE
;
312 * This is routine for system read from queue file.
313 * To avoid mess with doing here some sort of mq_receive we allow
314 * to read only queue size & notification info (the only values
315 * that are interesting from user point of view and aren't accessible
316 * through std routines)
318 static ssize_t
mqueue_read_file(struct file
*filp
, char __user
*u_data
,
319 size_t count
, loff_t
* off
)
321 struct mqueue_inode_info
*info
= MQUEUE_I(filp
->f_dentry
->d_inode
);
322 char buffer
[FILENT_SIZE
];
329 spin_lock(&info
->lock
);
330 snprintf(buffer
, sizeof(buffer
),
331 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
333 info
->notify_owner
? info
->notify
.sigev_notify
: 0,
334 (info
->notify_owner
&&
335 info
->notify
.sigev_notify
== SIGEV_SIGNAL
) ?
336 info
->notify
.sigev_signo
: 0,
338 spin_unlock(&info
->lock
);
339 buffer
[sizeof(buffer
)-1] = '\0';
340 slen
= strlen(buffer
)+1;
346 if (o
+ count
> slen
)
349 if (copy_to_user(u_data
, buffer
+ o
, count
))
353 filp
->f_dentry
->d_inode
->i_atime
= filp
->f_dentry
->d_inode
->i_ctime
= CURRENT_TIME
;
357 static int mqueue_flush_file(struct file
*filp
)
359 struct mqueue_inode_info
*info
= MQUEUE_I(filp
->f_dentry
->d_inode
);
361 spin_lock(&info
->lock
);
362 if (current
->tgid
== info
->notify_owner
)
363 remove_notification(info
);
365 spin_unlock(&info
->lock
);
369 static unsigned int mqueue_poll_file(struct file
*filp
, struct poll_table_struct
*poll_tab
)
371 struct mqueue_inode_info
*info
= MQUEUE_I(filp
->f_dentry
->d_inode
);
374 poll_wait(filp
, &info
->wait_q
, poll_tab
);
376 spin_lock(&info
->lock
);
377 if (info
->attr
.mq_curmsgs
)
378 retval
= POLLIN
| POLLRDNORM
;
380 if (info
->attr
.mq_curmsgs
< info
->attr
.mq_maxmsg
)
381 retval
|= POLLOUT
| POLLWRNORM
;
382 spin_unlock(&info
->lock
);
387 /* Adds current to info->e_wait_q[sr] before element with smaller prio */
388 static void wq_add(struct mqueue_inode_info
*info
, int sr
,
389 struct ext_wait_queue
*ewp
)
391 struct ext_wait_queue
*walk
;
395 list_for_each_entry(walk
, &info
->e_wait_q
[sr
].list
, list
) {
396 if (walk
->task
->static_prio
<= current
->static_prio
) {
397 list_add_tail(&ewp
->list
, &walk
->list
);
401 list_add_tail(&ewp
->list
, &info
->e_wait_q
[sr
].list
);
405 * Puts current task to sleep. Caller must hold queue lock. After return
409 static int wq_sleep(struct mqueue_inode_info
*info
, int sr
,
410 long timeout
, struct ext_wait_queue
*ewp
)
415 wq_add(info
, sr
, ewp
);
418 set_current_state(TASK_INTERRUPTIBLE
);
420 spin_unlock(&info
->lock
);
421 time
= schedule_timeout(timeout
);
423 while (ewp
->state
== STATE_PENDING
)
426 if (ewp
->state
== STATE_READY
) {
430 spin_lock(&info
->lock
);
431 if (ewp
->state
== STATE_READY
) {
435 if (signal_pending(current
)) {
436 retval
= -ERESTARTSYS
;
444 list_del(&ewp
->list
);
446 spin_unlock(&info
->lock
);
452 * Returns waiting task that should be serviced first or NULL if none exists
454 static struct ext_wait_queue
*wq_get_first_waiter(
455 struct mqueue_inode_info
*info
, int sr
)
457 struct list_head
*ptr
;
459 ptr
= info
->e_wait_q
[sr
].list
.prev
;
460 if (ptr
== &info
->e_wait_q
[sr
].list
)
462 return list_entry(ptr
, struct ext_wait_queue
, list
);
465 /* Auxiliary functions to manipulate messages' list */
466 static void msg_insert(struct msg_msg
*ptr
, struct mqueue_inode_info
*info
)
470 k
= info
->attr
.mq_curmsgs
- 1;
471 while (k
>= 0 && info
->messages
[k
]->m_type
>= ptr
->m_type
) {
472 info
->messages
[k
+ 1] = info
->messages
[k
];
475 info
->attr
.mq_curmsgs
++;
476 info
->qsize
+= ptr
->m_ts
;
477 info
->messages
[k
+ 1] = ptr
;
480 static inline struct msg_msg
*msg_get(struct mqueue_inode_info
*info
)
482 info
->qsize
-= info
->messages
[--info
->attr
.mq_curmsgs
]->m_ts
;
483 return info
->messages
[info
->attr
.mq_curmsgs
];
486 static inline void set_cookie(struct sk_buff
*skb
, char code
)
488 ((char*)skb
->data
)[NOTIFY_COOKIE_LEN
-1] = code
;
492 * The next function is only to split too long sys_mq_timedsend
494 static void __do_notify(struct mqueue_inode_info
*info
)
497 * invoked when there is registered process and there isn't process
498 * waiting synchronously for message AND state of queue changed from
499 * empty to not empty. Here we are sure that no one is waiting
501 if (info
->notify_owner
&&
502 info
->attr
.mq_curmsgs
== 1) {
503 struct siginfo sig_i
;
504 switch (info
->notify
.sigev_notify
) {
510 sig_i
.si_signo
= info
->notify
.sigev_signo
;
512 sig_i
.si_code
= SI_MESGQ
;
513 sig_i
.si_value
= info
->notify
.sigev_value
;
514 sig_i
.si_pid
= current
->tgid
;
515 sig_i
.si_uid
= current
->uid
;
517 kill_proc_info(info
->notify
.sigev_signo
,
518 &sig_i
, info
->notify_owner
);
521 set_cookie(info
->notify_cookie
, NOTIFY_WOKENUP
);
522 netlink_sendskb(info
->notify_sock
,
523 info
->notify_cookie
, 0);
526 /* after notification unregisters process */
527 info
->notify_owner
= 0;
529 wake_up(&info
->wait_q
);
532 static long prepare_timeout(const struct timespec __user
*u_arg
)
534 struct timespec ts
, nowts
;
538 if (unlikely(copy_from_user(&ts
, u_arg
,
539 sizeof(struct timespec
))))
542 if (unlikely(ts
.tv_nsec
< 0 || ts
.tv_sec
< 0
543 || ts
.tv_nsec
>= NSEC_PER_SEC
))
545 nowts
= CURRENT_TIME
;
546 /* first subtract as jiffies can't be too big */
547 ts
.tv_sec
-= nowts
.tv_sec
;
548 if (ts
.tv_nsec
< nowts
.tv_nsec
) {
549 ts
.tv_nsec
+= NSEC_PER_SEC
;
552 ts
.tv_nsec
-= nowts
.tv_nsec
;
556 timeout
= timespec_to_jiffies(&ts
) + 1;
558 return MAX_SCHEDULE_TIMEOUT
;
563 static void remove_notification(struct mqueue_inode_info
*info
)
565 if (info
->notify_owner
!= 0 &&
566 info
->notify
.sigev_notify
== SIGEV_THREAD
) {
567 set_cookie(info
->notify_cookie
, NOTIFY_REMOVED
);
568 netlink_sendskb(info
->notify_sock
, info
->notify_cookie
, 0);
570 info
->notify_owner
= 0;
573 static int mq_attr_ok(struct mq_attr
*attr
)
575 if (attr
->mq_maxmsg
<= 0 || attr
->mq_msgsize
<= 0)
577 if (capable(CAP_SYS_RESOURCE
)) {
578 if (attr
->mq_maxmsg
> HARD_MSGMAX
)
581 if (attr
->mq_maxmsg
> msg_max
||
582 attr
->mq_msgsize
> msgsize_max
)
585 /* check for overflow */
586 if (attr
->mq_msgsize
> ULONG_MAX
/attr
->mq_maxmsg
)
588 if ((unsigned long)(attr
->mq_maxmsg
* attr
->mq_msgsize
) +
589 (attr
->mq_maxmsg
* sizeof (struct msg_msg
*)) <
590 (unsigned long)(attr
->mq_maxmsg
* attr
->mq_msgsize
))
596 * Invoked when creating a new queue via sys_mq_open
598 static struct file
*do_create(struct dentry
*dir
, struct dentry
*dentry
,
599 int oflag
, mode_t mode
, struct mq_attr __user
*u_attr
)
605 if (u_attr
!= NULL
) {
606 if (copy_from_user(&attr
, u_attr
, sizeof(attr
)))
607 return ERR_PTR(-EFAULT
);
608 if (!mq_attr_ok(&attr
))
609 return ERR_PTR(-EINVAL
);
610 /* store for use during create */
611 dentry
->d_fsdata
= &attr
;
614 mode
&= ~current
->fs
->umask
;
615 ret
= vfs_create(dir
->d_inode
, dentry
, mode
, NULL
);
616 dentry
->d_fsdata
= NULL
;
620 filp
= dentry_open(dentry
, mqueue_mnt
, oflag
);
627 /* Opens existing queue */
628 static struct file
*do_open(struct dentry
*dentry
, int oflag
)
630 static int oflag2acc
[O_ACCMODE
] = { MAY_READ
, MAY_WRITE
,
631 MAY_READ
| MAY_WRITE
};
634 if ((oflag
& O_ACCMODE
) == (O_RDWR
| O_WRONLY
))
635 return ERR_PTR(-EINVAL
);
637 if (permission(dentry
->d_inode
, oflag2acc
[oflag
& O_ACCMODE
], NULL
))
638 return ERR_PTR(-EACCES
);
640 filp
= dentry_open(dentry
, mqueue_mnt
, oflag
);
648 asmlinkage
long sys_mq_open(const char __user
*u_name
, int oflag
, mode_t mode
,
649 struct mq_attr __user
*u_attr
)
651 struct dentry
*dentry
;
656 if (IS_ERR(name
= getname(u_name
)))
657 return PTR_ERR(name
);
659 fd
= get_unused_fd();
663 down(&mqueue_mnt
->mnt_root
->d_inode
->i_sem
);
664 dentry
= lookup_one_len(name
, mqueue_mnt
->mnt_root
, strlen(name
));
665 if (IS_ERR(dentry
)) {
666 error
= PTR_ERR(dentry
);
671 if (oflag
& O_CREAT
) {
672 if (dentry
->d_inode
) { /* entry already exists */
673 filp
= (oflag
& O_EXCL
) ? ERR_PTR(-EEXIST
) :
674 do_open(dentry
, oflag
);
676 filp
= do_create(mqueue_mnt
->mnt_root
, dentry
,
677 oflag
, mode
, u_attr
);
680 filp
= (dentry
->d_inode
) ? do_open(dentry
, oflag
) :
686 error
= PTR_ERR(filp
);
690 set_close_on_exec(fd
, 1);
691 fd_install(fd
, filp
);
700 up(&mqueue_mnt
->mnt_root
->d_inode
->i_sem
);
706 asmlinkage
long sys_mq_unlink(const char __user
*u_name
)
710 struct dentry
*dentry
;
711 struct inode
*inode
= NULL
;
713 name
= getname(u_name
);
715 return PTR_ERR(name
);
717 down(&mqueue_mnt
->mnt_root
->d_inode
->i_sem
);
718 dentry
= lookup_one_len(name
, mqueue_mnt
->mnt_root
, strlen(name
));
719 if (IS_ERR(dentry
)) {
720 err
= PTR_ERR(dentry
);
724 if (!dentry
->d_inode
) {
729 inode
= dentry
->d_inode
;
731 atomic_inc(&inode
->i_count
);
733 err
= vfs_unlink(dentry
->d_parent
->d_inode
, dentry
);
738 up(&mqueue_mnt
->mnt_root
->d_inode
->i_sem
);
746 /* Pipelined send and receive functions.
748 * If a receiver finds no waiting message, then it registers itself in the
749 * list of waiting receivers. A sender checks that list before adding the new
750 * message into the message array. If there is a waiting receiver, then it
751 * bypasses the message array and directly hands the message over to the
753 * The receiver accepts the message and returns without grabbing the queue
754 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
755 * are necessary. The same algorithm is used for sysv semaphores, see
756 * ipc/sem.c fore more details.
758 * The same algorithm is used for senders.
761 /* pipelined_send() - send a message directly to the task waiting in
762 * sys_mq_timedreceive() (without inserting message into a queue).
764 static inline void pipelined_send(struct mqueue_inode_info
*info
,
765 struct msg_msg
*message
,
766 struct ext_wait_queue
*receiver
)
768 receiver
->msg
= message
;
769 list_del(&receiver
->list
);
770 receiver
->state
= STATE_PENDING
;
771 wake_up_process(receiver
->task
);
773 receiver
->state
= STATE_READY
;
776 /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
777 * gets its message and put to the queue (we have one free place for sure). */
778 static inline void pipelined_receive(struct mqueue_inode_info
*info
)
780 struct ext_wait_queue
*sender
= wq_get_first_waiter(info
, SEND
);
784 wake_up_interruptible(&info
->wait_q
);
787 msg_insert(sender
->msg
, info
);
788 list_del(&sender
->list
);
789 sender
->state
= STATE_PENDING
;
790 wake_up_process(sender
->task
);
792 sender
->state
= STATE_READY
;
795 asmlinkage
long sys_mq_timedsend(mqd_t mqdes
, const char __user
*u_msg_ptr
,
796 size_t msg_len
, unsigned int msg_prio
,
797 const struct timespec __user
*u_abs_timeout
)
801 struct ext_wait_queue wait
;
802 struct ext_wait_queue
*receiver
;
803 struct msg_msg
*msg_ptr
;
804 struct mqueue_inode_info
*info
;
808 if (unlikely(msg_prio
>= (unsigned long) MQ_PRIO_MAX
))
811 timeout
= prepare_timeout(u_abs_timeout
);
818 inode
= filp
->f_dentry
->d_inode
;
819 if (unlikely(filp
->f_op
!= &mqueue_file_operations
))
821 info
= MQUEUE_I(inode
);
823 if (unlikely(!(filp
->f_mode
& FMODE_WRITE
)))
826 if (unlikely(msg_len
> info
->attr
.mq_msgsize
)) {
831 /* First try to allocate memory, before doing anything with
832 * existing queues. */
833 msg_ptr
= load_msg(u_msg_ptr
, msg_len
);
834 if (IS_ERR(msg_ptr
)) {
835 ret
= PTR_ERR(msg_ptr
);
838 msg_ptr
->m_ts
= msg_len
;
839 msg_ptr
->m_type
= msg_prio
;
841 spin_lock(&info
->lock
);
843 if (info
->attr
.mq_curmsgs
== info
->attr
.mq_maxmsg
) {
844 if (filp
->f_flags
& O_NONBLOCK
) {
845 spin_unlock(&info
->lock
);
847 } else if (unlikely(timeout
< 0)) {
848 spin_unlock(&info
->lock
);
852 wait
.msg
= (void *) msg_ptr
;
853 wait
.state
= STATE_NONE
;
854 ret
= wq_sleep(info
, SEND
, timeout
, &wait
);
859 receiver
= wq_get_first_waiter(info
, RECV
);
861 pipelined_send(info
, msg_ptr
, receiver
);
863 /* adds message to the queue */
864 msg_insert(msg_ptr
, info
);
867 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
=
869 spin_unlock(&info
->lock
);
878 asmlinkage ssize_t
sys_mq_timedreceive(mqd_t mqdes
, char __user
*u_msg_ptr
,
879 size_t msg_len
, unsigned int __user
*u_msg_prio
,
880 const struct timespec __user
*u_abs_timeout
)
884 struct msg_msg
*msg_ptr
;
887 struct mqueue_inode_info
*info
;
888 struct ext_wait_queue wait
;
890 timeout
= prepare_timeout(u_abs_timeout
);
897 inode
= filp
->f_dentry
->d_inode
;
898 if (unlikely(filp
->f_op
!= &mqueue_file_operations
))
900 info
= MQUEUE_I(inode
);
902 if (unlikely(!(filp
->f_mode
& FMODE_READ
)))
905 /* checks if buffer is big enough */
906 if (unlikely(msg_len
< info
->attr
.mq_msgsize
)) {
911 spin_lock(&info
->lock
);
912 if (info
->attr
.mq_curmsgs
== 0) {
913 if (filp
->f_flags
& O_NONBLOCK
) {
914 spin_unlock(&info
->lock
);
917 } else if (unlikely(timeout
< 0)) {
918 spin_unlock(&info
->lock
);
923 wait
.state
= STATE_NONE
;
924 ret
= wq_sleep(info
, RECV
, timeout
, &wait
);
928 msg_ptr
= msg_get(info
);
930 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
=
933 /* There is now free space in queue. */
934 pipelined_receive(info
);
935 spin_unlock(&info
->lock
);
941 if ((u_msg_prio
&& put_user(msg_ptr
->m_type
, u_msg_prio
)) ||
942 store_msg(u_msg_ptr
, msg_ptr
, msg_ptr
->m_ts
)) {
954 * Notes: the case when user wants us to deregister (with NULL as pointer)
955 * and he isn't currently owner of notification, will be silently discarded.
956 * It isn't explicitly defined in the POSIX.
958 asmlinkage
long sys_mq_notify(mqd_t mqdes
,
959 const struct sigevent __user
*u_notification
)
965 struct sigevent notification
;
966 struct mqueue_inode_info
*info
;
971 if (u_notification
!= NULL
) {
972 if (copy_from_user(¬ification
, u_notification
,
973 sizeof(struct sigevent
)))
976 if (unlikely(notification
.sigev_notify
!= SIGEV_NONE
&&
977 notification
.sigev_notify
!= SIGEV_SIGNAL
&&
978 notification
.sigev_notify
!= SIGEV_THREAD
))
980 if (notification
.sigev_notify
== SIGEV_SIGNAL
&&
981 !valid_signal(notification
.sigev_signo
)) {
984 if (notification
.sigev_notify
== SIGEV_THREAD
) {
985 /* create the notify skb */
986 nc
= alloc_skb(NOTIFY_COOKIE_LEN
, GFP_KERNEL
);
991 if (copy_from_user(nc
->data
,
992 notification
.sigev_value
.sival_ptr
,
993 NOTIFY_COOKIE_LEN
)) {
997 /* TODO: add a header? */
998 skb_put(nc
, NOTIFY_COOKIE_LEN
);
999 /* and attach it to the socket */
1001 filp
= fget(notification
.sigev_signo
);
1005 sock
= netlink_getsockbyfilp(filp
);
1008 ret
= PTR_ERR(sock
);
1013 ret
= netlink_attachskb(sock
, nc
, 0, MAX_SCHEDULE_TIMEOUT
);
1029 inode
= filp
->f_dentry
->d_inode
;
1030 if (unlikely(filp
->f_op
!= &mqueue_file_operations
))
1032 info
= MQUEUE_I(inode
);
1035 spin_lock(&info
->lock
);
1036 if (u_notification
== NULL
) {
1037 if (info
->notify_owner
== current
->tgid
) {
1038 remove_notification(info
);
1039 inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1041 } else if (info
->notify_owner
!= 0) {
1044 switch (notification
.sigev_notify
) {
1046 info
->notify
.sigev_notify
= SIGEV_NONE
;
1049 info
->notify_sock
= sock
;
1050 info
->notify_cookie
= nc
;
1053 info
->notify
.sigev_notify
= SIGEV_THREAD
;
1056 info
->notify
.sigev_signo
= notification
.sigev_signo
;
1057 info
->notify
.sigev_value
= notification
.sigev_value
;
1058 info
->notify
.sigev_notify
= SIGEV_SIGNAL
;
1061 info
->notify_owner
= current
->tgid
;
1062 inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1064 spin_unlock(&info
->lock
);
1069 netlink_detachskb(sock
, nc
);
1076 asmlinkage
long sys_mq_getsetattr(mqd_t mqdes
,
1077 const struct mq_attr __user
*u_mqstat
,
1078 struct mq_attr __user
*u_omqstat
)
1081 struct mq_attr mqstat
, omqstat
;
1083 struct inode
*inode
;
1084 struct mqueue_inode_info
*info
;
1086 if (u_mqstat
!= NULL
) {
1087 if (copy_from_user(&mqstat
, u_mqstat
, sizeof(struct mq_attr
)))
1089 if (mqstat
.mq_flags
& (~O_NONBLOCK
))
1098 inode
= filp
->f_dentry
->d_inode
;
1099 if (unlikely(filp
->f_op
!= &mqueue_file_operations
))
1101 info
= MQUEUE_I(inode
);
1103 spin_lock(&info
->lock
);
1105 omqstat
= info
->attr
;
1106 omqstat
.mq_flags
= filp
->f_flags
& O_NONBLOCK
;
1108 if (mqstat
.mq_flags
& O_NONBLOCK
)
1109 filp
->f_flags
|= O_NONBLOCK
;
1111 filp
->f_flags
&= ~O_NONBLOCK
;
1113 inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1116 spin_unlock(&info
->lock
);
1119 if (u_omqstat
!= NULL
&& copy_to_user(u_omqstat
, &omqstat
,
1120 sizeof(struct mq_attr
)))
1129 static struct inode_operations mqueue_dir_inode_operations
= {
1130 .lookup
= simple_lookup
,
1131 .create
= mqueue_create
,
1132 .unlink
= mqueue_unlink
,
1135 static struct file_operations mqueue_file_operations
= {
1136 .flush
= mqueue_flush_file
,
1137 .poll
= mqueue_poll_file
,
1138 .read
= mqueue_read_file
,
1141 static struct super_operations mqueue_super_ops
= {
1142 .alloc_inode
= mqueue_alloc_inode
,
1143 .destroy_inode
= mqueue_destroy_inode
,
1144 .statfs
= simple_statfs
,
1145 .delete_inode
= mqueue_delete_inode
,
1146 .drop_inode
= generic_delete_inode
,
1149 static struct file_system_type mqueue_fs_type
= {
1151 .get_sb
= mqueue_get_sb
,
1152 .kill_sb
= kill_litter_super
,
1155 static int msg_max_limit_min
= DFLT_MSGMAX
;
1156 static int msg_max_limit_max
= HARD_MSGMAX
;
1158 static int msg_maxsize_limit_min
= DFLT_MSGSIZEMAX
;
1159 static int msg_maxsize_limit_max
= INT_MAX
;
1161 static ctl_table mq_sysctls
[] = {
1163 .ctl_name
= CTL_QUEUESMAX
,
1164 .procname
= "queues_max",
1165 .data
= &queues_max
,
1166 .maxlen
= sizeof(int),
1168 .proc_handler
= &proc_dointvec
,
1171 .ctl_name
= CTL_MSGMAX
,
1172 .procname
= "msg_max",
1174 .maxlen
= sizeof(int),
1176 .proc_handler
= &proc_dointvec_minmax
,
1177 .extra1
= &msg_max_limit_min
,
1178 .extra2
= &msg_max_limit_max
,
1181 .ctl_name
= CTL_MSGSIZEMAX
,
1182 .procname
= "msgsize_max",
1183 .data
= &msgsize_max
,
1184 .maxlen
= sizeof(int),
1186 .proc_handler
= &proc_dointvec_minmax
,
1187 .extra1
= &msg_maxsize_limit_min
,
1188 .extra2
= &msg_maxsize_limit_max
,
1193 static ctl_table mq_sysctl_dir
[] = {
1195 .ctl_name
= FS_MQUEUE
,
1196 .procname
= "mqueue",
1198 .child
= mq_sysctls
,
1203 static ctl_table mq_sysctl_root
[] = {
1208 .child
= mq_sysctl_dir
,
1213 static int __init
init_mqueue_fs(void)
1217 mqueue_inode_cachep
= kmem_cache_create("mqueue_inode_cache",
1218 sizeof(struct mqueue_inode_info
), 0,
1219 SLAB_HWCACHE_ALIGN
, init_once
, NULL
);
1220 if (mqueue_inode_cachep
== NULL
)
1223 /* ignore failues - they are not fatal */
1224 mq_sysctl_table
= register_sysctl_table(mq_sysctl_root
, 0);
1226 error
= register_filesystem(&mqueue_fs_type
);
1230 if (IS_ERR(mqueue_mnt
= kern_mount(&mqueue_fs_type
))) {
1231 error
= PTR_ERR(mqueue_mnt
);
1232 goto out_filesystem
;
1235 /* internal initialization - not common for vfs */
1237 spin_lock_init(&mq_lock
);
1242 unregister_filesystem(&mqueue_fs_type
);
1244 if (mq_sysctl_table
)
1245 unregister_sysctl_table(mq_sysctl_table
);
1246 if (kmem_cache_destroy(mqueue_inode_cachep
)) {
1248 "mqueue_inode_cache: not all structures were freed\n");
1253 __initcall(init_mqueue_fs
);