tools/selftests: add mq_perf_tests
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / ipc / mqueue.c
CommitLineData
1da177e4
LT
1/*
2 * POSIX message queues filesystem for Linux.
3 *
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
f66e928b 5 * Michal Wronski (michal.wronski@gmail.com)
1da177e4
LT
6 *
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
10 *
20ca73bc
GW
11 * Audit: George Wilson (ltcgcw@us.ibm.com)
12 *
1da177e4
LT
13 * This file is released under the GPL.
14 */
15
c59ede7b 16#include <linux/capability.h>
1da177e4
LT
17#include <linux/init.h>
18#include <linux/pagemap.h>
19#include <linux/file.h>
20#include <linux/mount.h>
21#include <linux/namei.h>
22#include <linux/sysctl.h>
23#include <linux/poll.h>
24#include <linux/mqueue.h>
25#include <linux/msg.h>
26#include <linux/skbuff.h>
5b5c4d1a 27#include <linux/vmalloc.h>
1da177e4
LT
28#include <linux/netlink.h>
29#include <linux/syscalls.h>
20ca73bc 30#include <linux/audit.h>
7ed20e1a 31#include <linux/signal.h>
5f921ae9 32#include <linux/mutex.h>
b488893a
PE
33#include <linux/nsproxy.h>
34#include <linux/pid.h>
614b84cf 35#include <linux/ipc_namespace.h>
6b550f94 36#include <linux/user_namespace.h>
5a0e3ad6 37#include <linux/slab.h>
5f921ae9 38
1da177e4
LT
39#include <net/sock.h>
40#include "util.h"
41
42#define MQUEUE_MAGIC 0x19800202
43#define DIRENT_SIZE 20
44#define FILENT_SIZE 80
45
46#define SEND 0
47#define RECV 1
48
49#define STATE_NONE 0
50#define STATE_PENDING 1
51#define STATE_READY 2
52
d6629859
DL
53struct posix_msg_tree_node {
54 struct rb_node rb_node;
55 struct list_head msg_list;
56 int priority;
57};
58
1da177e4
LT
59struct ext_wait_queue { /* queue of sleeping tasks */
60 struct task_struct *task;
61 struct list_head list;
62 struct msg_msg *msg; /* ptr of loaded message */
63 int state; /* one of STATE_* values */
64};
65
66struct mqueue_inode_info {
67 spinlock_t lock;
68 struct inode vfs_inode;
69 wait_queue_head_t wait_q;
70
d6629859 71 struct rb_root msg_tree;
1da177e4
LT
72 struct mq_attr attr;
73
74 struct sigevent notify;
a03fcb73 75 struct pid* notify_owner;
6f9ac6d9 76 struct user_namespace *notify_user_ns;
338cec32 77 struct user_struct *user; /* user who created, for accounting */
1da177e4
LT
78 struct sock *notify_sock;
79 struct sk_buff *notify_cookie;
80
81 /* for tasks waiting for free space and messages, respectively */
82 struct ext_wait_queue e_wait_q[2];
83
84 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
85};
86
92e1d5be 87static const struct inode_operations mqueue_dir_inode_operations;
9a32144e 88static const struct file_operations mqueue_file_operations;
b87221de 89static const struct super_operations mqueue_super_ops;
1da177e4
LT
90static void remove_notification(struct mqueue_inode_info *info);
91
e18b890b 92static struct kmem_cache *mqueue_inode_cachep;
1da177e4
LT
93
94static struct ctl_table_header * mq_sysctl_table;
95
96static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
97{
98 return container_of(inode, struct mqueue_inode_info, vfs_inode);
99}
100
7eafd7c7
SH
101/*
102 * This routine should be called with the mq_lock held.
103 */
104static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
614b84cf 105{
7eafd7c7 106 return get_ipc_ns(inode->i_sb->s_fs_info);
614b84cf
SH
107}
108
7eafd7c7 109static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
614b84cf 110{
7eafd7c7
SH
111 struct ipc_namespace *ns;
112
113 spin_lock(&mq_lock);
114 ns = __get_ns_from_inode(inode);
115 spin_unlock(&mq_lock);
116 return ns;
614b84cf
SH
117}
118
d6629859
DL
119/* Auxiliary functions to manipulate messages' list */
120static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
121{
122 struct rb_node **p, *parent = NULL;
123 struct posix_msg_tree_node *leaf;
124
125 p = &info->msg_tree.rb_node;
126 while (*p) {
127 parent = *p;
128 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
129
130 if (likely(leaf->priority == msg->m_type))
131 goto insert_msg;
132 else if (msg->m_type < leaf->priority)
133 p = &(*p)->rb_left;
134 else
135 p = &(*p)->rb_right;
136 }
137 leaf = kzalloc(sizeof(*leaf), GFP_ATOMIC);
138 if (!leaf)
139 return -ENOMEM;
140 rb_init_node(&leaf->rb_node);
141 INIT_LIST_HEAD(&leaf->msg_list);
142 leaf->priority = msg->m_type;
143 rb_link_node(&leaf->rb_node, parent, p);
144 rb_insert_color(&leaf->rb_node, &info->msg_tree);
145 info->qsize += sizeof(struct posix_msg_tree_node);
146insert_msg:
147 info->attr.mq_curmsgs++;
148 info->qsize += msg->m_ts;
149 list_add_tail(&msg->m_list, &leaf->msg_list);
150 return 0;
151}
152
153static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
154{
155 struct rb_node **p, *parent = NULL;
156 struct posix_msg_tree_node *leaf;
157 struct msg_msg *msg;
158
159try_again:
160 p = &info->msg_tree.rb_node;
161 while (*p) {
162 parent = *p;
163 /*
164 * During insert, low priorities go to the left and high to the
165 * right. On receive, we want the highest priorities first, so
166 * walk all the way to the right.
167 */
168 p = &(*p)->rb_right;
169 }
170 if (!parent) {
171 if (info->attr.mq_curmsgs) {
172 pr_warn_once("Inconsistency in POSIX message queue, "
173 "no tree element, but supposedly messages "
174 "should exist!\n");
175 info->attr.mq_curmsgs = 0;
176 }
177 return NULL;
178 }
179 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
180 if (list_empty(&leaf->msg_list)) {
181 pr_warn_once("Inconsistency in POSIX message queue, "
182 "empty leaf node but we haven't implemented "
183 "lazy leaf delete!\n");
184 rb_erase(&leaf->rb_node, &info->msg_tree);
185 info->qsize -= sizeof(struct posix_msg_tree_node);
186 kfree(leaf);
187 goto try_again;
188 } else {
189 msg = list_first_entry(&leaf->msg_list,
190 struct msg_msg, m_list);
191 list_del(&msg->m_list);
192 if (list_empty(&leaf->msg_list)) {
193 rb_erase(&leaf->rb_node, &info->msg_tree);
194 info->qsize -= sizeof(struct posix_msg_tree_node);
195 kfree(leaf);
196 }
197 }
198 info->attr.mq_curmsgs--;
199 info->qsize -= msg->m_ts;
200 return msg;
201}
202
7eafd7c7 203static struct inode *mqueue_get_inode(struct super_block *sb,
1b9d5ff7 204 struct ipc_namespace *ipc_ns, umode_t mode,
7eafd7c7 205 struct mq_attr *attr)
1da177e4 206{
86a264ab 207 struct user_struct *u = current_user();
1da177e4 208 struct inode *inode;
d40dcdb0 209 int ret = -ENOMEM;
1da177e4
LT
210
211 inode = new_inode(sb);
04715206
JS
212 if (!inode)
213 goto err;
214
215 inode->i_ino = get_next_ino();
216 inode->i_mode = mode;
217 inode->i_uid = current_fsuid();
218 inode->i_gid = current_fsgid();
219 inode->i_mtime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
220
221 if (S_ISREG(mode)) {
222 struct mqueue_inode_info *info;
d6629859 223 unsigned long mq_bytes, mq_treesize;
04715206
JS
224
225 inode->i_fop = &mqueue_file_operations;
226 inode->i_size = FILENT_SIZE;
227 /* mqueue specific info */
228 info = MQUEUE_I(inode);
229 spin_lock_init(&info->lock);
230 init_waitqueue_head(&info->wait_q);
231 INIT_LIST_HEAD(&info->e_wait_q[0].list);
232 INIT_LIST_HEAD(&info->e_wait_q[1].list);
233 info->notify_owner = NULL;
6f9ac6d9 234 info->notify_user_ns = NULL;
04715206
JS
235 info->qsize = 0;
236 info->user = NULL; /* set when all is ok */
d6629859 237 info->msg_tree = RB_ROOT;
04715206 238 memset(&info->attr, 0, sizeof(info->attr));
cef0184c
KM
239 info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max,
240 ipc_ns->mq_msg_default);
241 info->attr.mq_msgsize = min(ipc_ns->mq_msgsize_max,
242 ipc_ns->mq_msgsize_default);
04715206
JS
243 if (attr) {
244 info->attr.mq_maxmsg = attr->mq_maxmsg;
245 info->attr.mq_msgsize = attr->mq_msgsize;
246 }
d6629859
DL
247 /*
248 * We used to allocate a static array of pointers and account
249 * the size of that array as well as one msg_msg struct per
250 * possible message into the queue size. That's no longer
251 * accurate as the queue is now an rbtree and will grow and
252 * shrink depending on usage patterns. We can, however, still
253 * account one msg_msg struct per message, but the nodes are
254 * allocated depending on priority usage, and most programs
255 * only use one, or a handful, of priorities. However, since
256 * this is pinned memory, we need to assume worst case, so
257 * that means the min(mq_maxmsg, max_priorities) * struct
258 * posix_msg_tree_node.
259 */
260 mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
261 min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
262 sizeof(struct posix_msg_tree_node);
04715206 263
d6629859
DL
264 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
265 info->attr.mq_msgsize);
1da177e4 266
04715206
JS
267 spin_lock(&mq_lock);
268 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
2a4e64b8 269 u->mq_bytes + mq_bytes > rlimit(RLIMIT_MSGQUEUE)) {
04715206
JS
270 spin_unlock(&mq_lock);
271 /* mqueue_evict_inode() releases info->messages */
d40dcdb0 272 ret = -EMFILE;
04715206 273 goto out_inode;
1da177e4 274 }
04715206
JS
275 u->mq_bytes += mq_bytes;
276 spin_unlock(&mq_lock);
277
278 /* all is ok */
279 info->user = get_uid(u);
280 } else if (S_ISDIR(mode)) {
281 inc_nlink(inode);
282 /* Some things misbehave if size == 0 on a directory */
283 inode->i_size = 2 * DIRENT_SIZE;
284 inode->i_op = &mqueue_dir_inode_operations;
285 inode->i_fop = &simple_dir_operations;
1da177e4 286 }
04715206 287
1da177e4
LT
288 return inode;
289out_inode:
1da177e4 290 iput(inode);
04715206 291err:
d40dcdb0 292 return ERR_PTR(ret);
1da177e4
LT
293}
294
295static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
296{
297 struct inode *inode;
7eafd7c7 298 struct ipc_namespace *ns = data;
1da177e4
LT
299
300 sb->s_blocksize = PAGE_CACHE_SIZE;
301 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
302 sb->s_magic = MQUEUE_MAGIC;
303 sb->s_op = &mqueue_super_ops;
304
48fde701
AV
305 inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
306 if (IS_ERR(inode))
307 return PTR_ERR(inode);
1da177e4 308
48fde701
AV
309 sb->s_root = d_make_root(inode);
310 if (!sb->s_root)
311 return -ENOMEM;
312 return 0;
1da177e4
LT
313}
314
ceefda69 315static struct dentry *mqueue_mount(struct file_system_type *fs_type,
454e2398 316 int flags, const char *dev_name,
ceefda69 317 void *data)
1da177e4 318{
7eafd7c7
SH
319 if (!(flags & MS_KERNMOUNT))
320 data = current->nsproxy->ipc_ns;
ceefda69 321 return mount_ns(fs_type, flags, data, mqueue_fill_super);
1da177e4
LT
322}
323
51cc5068 324static void init_once(void *foo)
1da177e4
LT
325{
326 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
327
a35afb83 328 inode_init_once(&p->vfs_inode);
1da177e4
LT
329}
330
331static struct inode *mqueue_alloc_inode(struct super_block *sb)
332{
333 struct mqueue_inode_info *ei;
334
e94b1766 335 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
1da177e4
LT
336 if (!ei)
337 return NULL;
338 return &ei->vfs_inode;
339}
340
fa0d7e3d 341static void mqueue_i_callback(struct rcu_head *head)
1da177e4 342{
fa0d7e3d 343 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
344 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
345}
346
fa0d7e3d
NP
347static void mqueue_destroy_inode(struct inode *inode)
348{
349 call_rcu(&inode->i_rcu, mqueue_i_callback);
350}
351
6d8af64c 352static void mqueue_evict_inode(struct inode *inode)
1da177e4
LT
353{
354 struct mqueue_inode_info *info;
355 struct user_struct *user;
d6629859 356 unsigned long mq_bytes, mq_treesize;
7eafd7c7 357 struct ipc_namespace *ipc_ns;
d6629859 358 struct msg_msg *msg;
1da177e4 359
dbd5768f 360 clear_inode(inode);
6d8af64c
AV
361
362 if (S_ISDIR(inode->i_mode))
1da177e4 363 return;
6d8af64c 364
7eafd7c7 365 ipc_ns = get_ns_from_inode(inode);
1da177e4
LT
366 info = MQUEUE_I(inode);
367 spin_lock(&info->lock);
d6629859
DL
368 while ((msg = msg_get(info)) != NULL)
369 free_msg(msg);
1da177e4
LT
370 spin_unlock(&info->lock);
371
8834cf79 372 /* Total amount of bytes accounted for the mqueue */
d6629859
DL
373 mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
374 min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
375 sizeof(struct posix_msg_tree_node);
376
377 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
378 info->attr.mq_msgsize);
379
1da177e4
LT
380 user = info->user;
381 if (user) {
382 spin_lock(&mq_lock);
383 user->mq_bytes -= mq_bytes;
7eafd7c7
SH
384 /*
385 * get_ns_from_inode() ensures that the
386 * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
387 * to which we now hold a reference, or it is NULL.
388 * We can't put it here under mq_lock, though.
389 */
390 if (ipc_ns)
391 ipc_ns->mq_queues_count--;
1da177e4
LT
392 spin_unlock(&mq_lock);
393 free_uid(user);
394 }
7eafd7c7
SH
395 if (ipc_ns)
396 put_ipc_ns(ipc_ns);
1da177e4
LT
397}
398
399static int mqueue_create(struct inode *dir, struct dentry *dentry,
4acdaf27 400 umode_t mode, struct nameidata *nd)
1da177e4
LT
401{
402 struct inode *inode;
403 struct mq_attr *attr = dentry->d_fsdata;
404 int error;
7eafd7c7 405 struct ipc_namespace *ipc_ns;
1da177e4
LT
406
407 spin_lock(&mq_lock);
7eafd7c7
SH
408 ipc_ns = __get_ns_from_inode(dir);
409 if (!ipc_ns) {
410 error = -EACCES;
411 goto out_unlock;
412 }
02967ea0
DL
413 if (ipc_ns->mq_queues_count >= HARD_QUEUESMAX ||
414 (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
415 !capable(CAP_SYS_RESOURCE))) {
1da177e4 416 error = -ENOSPC;
614b84cf 417 goto out_unlock;
1da177e4 418 }
614b84cf 419 ipc_ns->mq_queues_count++;
1da177e4
LT
420 spin_unlock(&mq_lock);
421
7eafd7c7 422 inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
d40dcdb0
JS
423 if (IS_ERR(inode)) {
424 error = PTR_ERR(inode);
1da177e4 425 spin_lock(&mq_lock);
614b84cf
SH
426 ipc_ns->mq_queues_count--;
427 goto out_unlock;
1da177e4
LT
428 }
429
7eafd7c7 430 put_ipc_ns(ipc_ns);
1da177e4
LT
431 dir->i_size += DIRENT_SIZE;
432 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
433
434 d_instantiate(dentry, inode);
435 dget(dentry);
436 return 0;
614b84cf 437out_unlock:
1da177e4 438 spin_unlock(&mq_lock);
7eafd7c7
SH
439 if (ipc_ns)
440 put_ipc_ns(ipc_ns);
1da177e4
LT
441 return error;
442}
443
444static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
445{
446 struct inode *inode = dentry->d_inode;
447
448 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
449 dir->i_size -= DIRENT_SIZE;
9a53c3a7 450 drop_nlink(inode);
1da177e4
LT
451 dput(dentry);
452 return 0;
453}
454
455/*
456* This is routine for system read from queue file.
457* To avoid mess with doing here some sort of mq_receive we allow
458* to read only queue size & notification info (the only values
459* that are interesting from user point of view and aren't accessible
460* through std routines)
461*/
462static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
f1a43f93 463 size_t count, loff_t *off)
1da177e4 464{
6d63079a 465 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
1da177e4 466 char buffer[FILENT_SIZE];
f1a43f93 467 ssize_t ret;
1da177e4
LT
468
469 spin_lock(&info->lock);
470 snprintf(buffer, sizeof(buffer),
471 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
472 info->qsize,
473 info->notify_owner ? info->notify.sigev_notify : 0,
474 (info->notify_owner &&
475 info->notify.sigev_notify == SIGEV_SIGNAL) ?
476 info->notify.sigev_signo : 0,
6c5f3e7b 477 pid_vnr(info->notify_owner));
1da177e4
LT
478 spin_unlock(&info->lock);
479 buffer[sizeof(buffer)-1] = '\0';
1da177e4 480
f1a43f93
AM
481 ret = simple_read_from_buffer(u_data, count, off, buffer,
482 strlen(buffer));
483 if (ret <= 0)
484 return ret;
1da177e4 485
6d63079a 486 filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
f1a43f93 487 return ret;
1da177e4
LT
488}
489
75e1fcc0 490static int mqueue_flush_file(struct file *filp, fl_owner_t id)
1da177e4 491{
6d63079a 492 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
1da177e4
LT
493
494 spin_lock(&info->lock);
a03fcb73 495 if (task_tgid(current) == info->notify_owner)
1da177e4
LT
496 remove_notification(info);
497
498 spin_unlock(&info->lock);
499 return 0;
500}
501
502static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
503{
6d63079a 504 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
1da177e4
LT
505 int retval = 0;
506
507 poll_wait(filp, &info->wait_q, poll_tab);
508
509 spin_lock(&info->lock);
510 if (info->attr.mq_curmsgs)
511 retval = POLLIN | POLLRDNORM;
512
513 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
514 retval |= POLLOUT | POLLWRNORM;
515 spin_unlock(&info->lock);
516
517 return retval;
518}
519
520/* Adds current to info->e_wait_q[sr] before element with smaller prio */
521static void wq_add(struct mqueue_inode_info *info, int sr,
522 struct ext_wait_queue *ewp)
523{
524 struct ext_wait_queue *walk;
525
526 ewp->task = current;
527
528 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
529 if (walk->task->static_prio <= current->static_prio) {
530 list_add_tail(&ewp->list, &walk->list);
531 return;
532 }
533 }
534 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
535}
536
537/*
538 * Puts current task to sleep. Caller must hold queue lock. After return
539 * lock isn't held.
540 * sr: SEND or RECV
541 */
542static int wq_sleep(struct mqueue_inode_info *info, int sr,
9ca7d8e6 543 ktime_t *timeout, struct ext_wait_queue *ewp)
1da177e4
LT
544{
545 int retval;
546 signed long time;
547
548 wq_add(info, sr, ewp);
549
550 for (;;) {
551 set_current_state(TASK_INTERRUPTIBLE);
552
553 spin_unlock(&info->lock);
32ea845d
WG
554 time = schedule_hrtimeout_range_clock(timeout, 0,
555 HRTIMER_MODE_ABS, CLOCK_REALTIME);
1da177e4
LT
556
557 while (ewp->state == STATE_PENDING)
558 cpu_relax();
559
560 if (ewp->state == STATE_READY) {
561 retval = 0;
562 goto out;
563 }
564 spin_lock(&info->lock);
565 if (ewp->state == STATE_READY) {
566 retval = 0;
567 goto out_unlock;
568 }
569 if (signal_pending(current)) {
570 retval = -ERESTARTSYS;
571 break;
572 }
573 if (time == 0) {
574 retval = -ETIMEDOUT;
575 break;
576 }
577 }
578 list_del(&ewp->list);
579out_unlock:
580 spin_unlock(&info->lock);
581out:
582 return retval;
583}
584
585/*
586 * Returns waiting task that should be serviced first or NULL if none exists
587 */
588static struct ext_wait_queue *wq_get_first_waiter(
589 struct mqueue_inode_info *info, int sr)
590{
591 struct list_head *ptr;
592
593 ptr = info->e_wait_q[sr].list.prev;
594 if (ptr == &info->e_wait_q[sr].list)
595 return NULL;
596 return list_entry(ptr, struct ext_wait_queue, list);
597}
598
1da177e4
LT
599
600static inline void set_cookie(struct sk_buff *skb, char code)
601{
602 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
603}
604
605/*
606 * The next function is only to split too long sys_mq_timedsend
607 */
608static void __do_notify(struct mqueue_inode_info *info)
609{
610 /* notification
611 * invoked when there is registered process and there isn't process
612 * waiting synchronously for message AND state of queue changed from
613 * empty to not empty. Here we are sure that no one is waiting
614 * synchronously. */
615 if (info->notify_owner &&
616 info->attr.mq_curmsgs == 1) {
617 struct siginfo sig_i;
618 switch (info->notify.sigev_notify) {
619 case SIGEV_NONE:
620 break;
621 case SIGEV_SIGNAL:
622 /* sends signal */
623
624 sig_i.si_signo = info->notify.sigev_signo;
625 sig_i.si_errno = 0;
626 sig_i.si_code = SI_MESGQ;
627 sig_i.si_value = info->notify.sigev_value;
6b550f94
SH
628 /* map current pid/uid into info->owner's namespaces */
629 rcu_read_lock();
a6684999
SB
630 sig_i.si_pid = task_tgid_nr_ns(current,
631 ns_of_pid(info->notify_owner));
76b6db01 632 sig_i.si_uid = from_kuid_munged(info->notify_user_ns, current_uid());
6b550f94 633 rcu_read_unlock();
1da177e4 634
a03fcb73
CLG
635 kill_pid_info(info->notify.sigev_signo,
636 &sig_i, info->notify_owner);
1da177e4
LT
637 break;
638 case SIGEV_THREAD:
639 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
7ee015e0 640 netlink_sendskb(info->notify_sock, info->notify_cookie);
1da177e4
LT
641 break;
642 }
643 /* after notification unregisters process */
a03fcb73 644 put_pid(info->notify_owner);
6f9ac6d9 645 put_user_ns(info->notify_user_ns);
a03fcb73 646 info->notify_owner = NULL;
6f9ac6d9 647 info->notify_user_ns = NULL;
1da177e4
LT
648 }
649 wake_up(&info->wait_q);
650}
651
9ca7d8e6
CE
652static int prepare_timeout(const struct timespec __user *u_abs_timeout,
653 ktime_t *expires, struct timespec *ts)
1da177e4 654{
9ca7d8e6
CE
655 if (copy_from_user(ts, u_abs_timeout, sizeof(struct timespec)))
656 return -EFAULT;
657 if (!timespec_valid(ts))
658 return -EINVAL;
1da177e4 659
9ca7d8e6
CE
660 *expires = timespec_to_ktime(*ts);
661 return 0;
1da177e4
LT
662}
663
664static void remove_notification(struct mqueue_inode_info *info)
665{
a03fcb73 666 if (info->notify_owner != NULL &&
1da177e4
LT
667 info->notify.sigev_notify == SIGEV_THREAD) {
668 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
7ee015e0 669 netlink_sendskb(info->notify_sock, info->notify_cookie);
1da177e4 670 }
a03fcb73 671 put_pid(info->notify_owner);
6f9ac6d9 672 put_user_ns(info->notify_user_ns);
a03fcb73 673 info->notify_owner = NULL;
6f9ac6d9 674 info->notify_user_ns = NULL;
1da177e4
LT
675}
676
614b84cf 677static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
1da177e4 678{
2c12ea49
DL
679 int mq_treesize;
680 unsigned long total_size;
681
1da177e4 682 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
113289cc 683 return -EINVAL;
1da177e4 684 if (capable(CAP_SYS_RESOURCE)) {
02967ea0
DL
685 if (attr->mq_maxmsg > HARD_MSGMAX ||
686 attr->mq_msgsize > HARD_MSGSIZEMAX)
113289cc 687 return -EINVAL;
1da177e4 688 } else {
614b84cf
SH
689 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
690 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
113289cc 691 return -EINVAL;
1da177e4
LT
692 }
693 /* check for overflow */
694 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
113289cc 695 return -EOVERFLOW;
2c12ea49
DL
696 mq_treesize = attr->mq_maxmsg * sizeof(struct msg_msg) +
697 min_t(unsigned int, attr->mq_maxmsg, MQ_PRIO_MAX) *
698 sizeof(struct posix_msg_tree_node);
699 total_size = attr->mq_maxmsg * attr->mq_msgsize;
700 if (total_size + mq_treesize < total_size)
113289cc
DL
701 return -EOVERFLOW;
702 return 0;
1da177e4
LT
703}
704
705/*
706 * Invoked when creating a new queue via sys_mq_open
707 */
614b84cf 708static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
4acdaf27 709 struct dentry *dentry, int oflag, umode_t mode,
614b84cf 710 struct mq_attr *attr)
1da177e4 711{
745ca247 712 const struct cred *cred = current_cred();
4a3fd211 713 struct file *result;
1da177e4
LT
714 int ret;
715
564f6993 716 if (attr) {
113289cc
DL
717 ret = mq_attr_ok(ipc_ns, attr);
718 if (ret)
7c7dce92 719 goto out;
1da177e4 720 /* store for use during create */
564f6993 721 dentry->d_fsdata = attr;
113289cc
DL
722 } else {
723 struct mq_attr def_attr;
724
725 def_attr.mq_maxmsg = min(ipc_ns->mq_msg_max,
726 ipc_ns->mq_msg_default);
727 def_attr.mq_msgsize = min(ipc_ns->mq_msgsize_max,
728 ipc_ns->mq_msgsize_default);
729 ret = mq_attr_ok(ipc_ns, &def_attr);
730 if (ret)
731 goto out;
1da177e4
LT
732 }
733
ce3b0f8d 734 mode &= ~current_umask();
614b84cf 735 ret = mnt_want_write(ipc_ns->mq_mnt);
4a3fd211
DH
736 if (ret)
737 goto out;
1da177e4
LT
738 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
739 dentry->d_fsdata = NULL;
740 if (ret)
4a3fd211
DH
741 goto out_drop_write;
742
614b84cf 743 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
4a3fd211
DH
744 /*
745 * dentry_open() took a persistent mnt_want_write(),
746 * so we can now drop this one.
747 */
614b84cf 748 mnt_drop_write(ipc_ns->mq_mnt);
4a3fd211
DH
749 return result;
750
751out_drop_write:
614b84cf 752 mnt_drop_write(ipc_ns->mq_mnt);
7c7dce92
AV
753out:
754 dput(dentry);
614b84cf 755 mntput(ipc_ns->mq_mnt);
7c7dce92 756 return ERR_PTR(ret);
1da177e4
LT
757}
758
759/* Opens existing queue */
614b84cf
SH
760static struct file *do_open(struct ipc_namespace *ipc_ns,
761 struct dentry *dentry, int oflag)
1da177e4 762{
04db0dde 763 int ret;
745ca247
DH
764 const struct cred *cred = current_cred();
765
766 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
767 MAY_READ | MAY_WRITE };
1da177e4 768
7c7dce92 769 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
04db0dde
AGR
770 ret = -EINVAL;
771 goto err;
7c7dce92 772 }
1da177e4 773
f419a2e3 774 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
04db0dde
AGR
775 ret = -EACCES;
776 goto err;
7c7dce92 777 }
1da177e4 778
614b84cf 779 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
04db0dde
AGR
780
781err:
782 dput(dentry);
783 mntput(ipc_ns->mq_mnt);
784 return ERR_PTR(ret);
1da177e4
LT
785}
786
df0a4283 787SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
d5460c99 788 struct mq_attr __user *, u_attr)
1da177e4
LT
789{
790 struct dentry *dentry;
791 struct file *filp;
792 char *name;
564f6993 793 struct mq_attr attr;
1da177e4 794 int fd, error;
7eafd7c7 795 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
1da177e4 796
564f6993
AV
797 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
798 return -EFAULT;
799
800 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
20ca73bc 801
1da177e4
LT
802 if (IS_ERR(name = getname(u_name)))
803 return PTR_ERR(name);
804
269f2134 805 fd = get_unused_fd_flags(O_CLOEXEC);
1da177e4
LT
806 if (fd < 0)
807 goto out_putname;
808
614b84cf
SH
809 mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
810 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
1da177e4
LT
811 if (IS_ERR(dentry)) {
812 error = PTR_ERR(dentry);
4294a8ee 813 goto out_putfd;
1da177e4 814 }
614b84cf 815 mntget(ipc_ns->mq_mnt);
1da177e4
LT
816
817 if (oflag & O_CREAT) {
818 if (dentry->d_inode) { /* entry already exists */
5a190ae6 819 audit_inode(name, dentry);
8d8ffefa
AGR
820 if (oflag & O_EXCL) {
821 error = -EEXIST;
7c7dce92 822 goto out;
8d8ffefa 823 }
614b84cf 824 filp = do_open(ipc_ns, dentry, oflag);
1da177e4 825 } else {
614b84cf
SH
826 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
827 dentry, oflag, mode,
564f6993 828 u_attr ? &attr : NULL);
1da177e4 829 }
7c7dce92 830 } else {
8d8ffefa
AGR
831 if (!dentry->d_inode) {
832 error = -ENOENT;
7c7dce92 833 goto out;
8d8ffefa 834 }
5a190ae6 835 audit_inode(name, dentry);
614b84cf 836 filp = do_open(ipc_ns, dentry, oflag);
7c7dce92 837 }
1da177e4
LT
838
839 if (IS_ERR(filp)) {
840 error = PTR_ERR(filp);
841 goto out_putfd;
842 }
843
1da177e4
LT
844 fd_install(fd, filp);
845 goto out_upsem;
846
7c7dce92
AV
847out:
848 dput(dentry);
614b84cf 849 mntput(ipc_ns->mq_mnt);
7c7dce92 850out_putfd:
1da177e4 851 put_unused_fd(fd);
1da177e4
LT
852 fd = error;
853out_upsem:
614b84cf 854 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
1da177e4
LT
855out_putname:
856 putname(name);
857 return fd;
858}
859
d5460c99 860SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
1da177e4
LT
861{
862 int err;
863 char *name;
864 struct dentry *dentry;
865 struct inode *inode = NULL;
7eafd7c7 866 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
1da177e4
LT
867
868 name = getname(u_name);
869 if (IS_ERR(name))
870 return PTR_ERR(name);
871
614b84cf 872 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
7a434814 873 I_MUTEX_PARENT);
614b84cf 874 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
1da177e4
LT
875 if (IS_ERR(dentry)) {
876 err = PTR_ERR(dentry);
877 goto out_unlock;
878 }
879
880 if (!dentry->d_inode) {
881 err = -ENOENT;
882 goto out_err;
883 }
884
885 inode = dentry->d_inode;
886 if (inode)
7de9c6ee 887 ihold(inode);
614b84cf 888 err = mnt_want_write(ipc_ns->mq_mnt);
0622753b
DH
889 if (err)
890 goto out_err;
1da177e4 891 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
614b84cf 892 mnt_drop_write(ipc_ns->mq_mnt);
1da177e4
LT
893out_err:
894 dput(dentry);
895
896out_unlock:
614b84cf 897 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
1da177e4
LT
898 putname(name);
899 if (inode)
900 iput(inode);
901
902 return err;
903}
904
905/* Pipelined send and receive functions.
906 *
907 * If a receiver finds no waiting message, then it registers itself in the
908 * list of waiting receivers. A sender checks that list before adding the new
909 * message into the message array. If there is a waiting receiver, then it
910 * bypasses the message array and directly hands the message over to the
911 * receiver.
912 * The receiver accepts the message and returns without grabbing the queue
913 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
914 * are necessary. The same algorithm is used for sysv semaphores, see
7b7a317c 915 * ipc/sem.c for more details.
1da177e4
LT
916 *
917 * The same algorithm is used for senders.
918 */
919
920/* pipelined_send() - send a message directly to the task waiting in
921 * sys_mq_timedreceive() (without inserting message into a queue).
922 */
923static inline void pipelined_send(struct mqueue_inode_info *info,
924 struct msg_msg *message,
925 struct ext_wait_queue *receiver)
926{
927 receiver->msg = message;
928 list_del(&receiver->list);
929 receiver->state = STATE_PENDING;
930 wake_up_process(receiver->task);
d59dd462 931 smp_wmb();
1da177e4
LT
932 receiver->state = STATE_READY;
933}
934
935/* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
936 * gets its message and put to the queue (we have one free place for sure). */
937static inline void pipelined_receive(struct mqueue_inode_info *info)
938{
939 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
940
941 if (!sender) {
942 /* for poll */
943 wake_up_interruptible(&info->wait_q);
944 return;
945 }
d6629859
DL
946 if (msg_insert(sender->msg, info))
947 return;
1da177e4
LT
948 list_del(&sender->list);
949 sender->state = STATE_PENDING;
950 wake_up_process(sender->task);
d59dd462 951 smp_wmb();
1da177e4
LT
952 sender->state = STATE_READY;
953}
954
c4ea37c2
HC
955SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
956 size_t, msg_len, unsigned int, msg_prio,
957 const struct timespec __user *, u_abs_timeout)
1da177e4
LT
958{
959 struct file *filp;
960 struct inode *inode;
961 struct ext_wait_queue wait;
962 struct ext_wait_queue *receiver;
963 struct msg_msg *msg_ptr;
964 struct mqueue_inode_info *info;
9ca7d8e6
CE
965 ktime_t expires, *timeout = NULL;
966 struct timespec ts;
1da177e4
LT
967 int ret;
968
c32c8af4 969 if (u_abs_timeout) {
9ca7d8e6
CE
970 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
971 if (res)
972 return res;
973 timeout = &expires;
c32c8af4 974 }
20ca73bc 975
1da177e4
LT
976 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
977 return -EINVAL;
978
9ca7d8e6 979 audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL);
1da177e4 980
1da177e4 981 filp = fget(mqdes);
8d8ffefa
AGR
982 if (unlikely(!filp)) {
983 ret = -EBADF;
1da177e4 984 goto out;
8d8ffefa 985 }
1da177e4 986
6d63079a 987 inode = filp->f_path.dentry->d_inode;
8d8ffefa
AGR
988 if (unlikely(filp->f_op != &mqueue_file_operations)) {
989 ret = -EBADF;
1da177e4 990 goto out_fput;
8d8ffefa 991 }
1da177e4 992 info = MQUEUE_I(inode);
5a190ae6 993 audit_inode(NULL, filp->f_path.dentry);
1da177e4 994
8d8ffefa
AGR
995 if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
996 ret = -EBADF;
1da177e4 997 goto out_fput;
8d8ffefa 998 }
1da177e4
LT
999
1000 if (unlikely(msg_len > info->attr.mq_msgsize)) {
1001 ret = -EMSGSIZE;
1002 goto out_fput;
1003 }
1004
1005 /* First try to allocate memory, before doing anything with
1006 * existing queues. */
1007 msg_ptr = load_msg(u_msg_ptr, msg_len);
1008 if (IS_ERR(msg_ptr)) {
1009 ret = PTR_ERR(msg_ptr);
1010 goto out_fput;
1011 }
1012 msg_ptr->m_ts = msg_len;
1013 msg_ptr->m_type = msg_prio;
1014
1015 spin_lock(&info->lock);
1016
1017 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
1018 if (filp->f_flags & O_NONBLOCK) {
1019 spin_unlock(&info->lock);
1020 ret = -EAGAIN;
1da177e4
LT
1021 } else {
1022 wait.task = current;
1023 wait.msg = (void *) msg_ptr;
1024 wait.state = STATE_NONE;
1025 ret = wq_sleep(info, SEND, timeout, &wait);
1026 }
1027 if (ret < 0)
1028 free_msg(msg_ptr);
1029 } else {
1030 receiver = wq_get_first_waiter(info, RECV);
1031 if (receiver) {
1032 pipelined_send(info, msg_ptr, receiver);
1033 } else {
1034 /* adds message to the queue */
d6629859
DL
1035 if (msg_insert(msg_ptr, info)) {
1036 free_msg(msg_ptr);
1037 ret = -ENOMEM;
1038 spin_unlock(&info->lock);
1039 goto out_fput;
1040 }
1da177e4
LT
1041 __do_notify(info);
1042 }
1043 inode->i_atime = inode->i_mtime = inode->i_ctime =
1044 CURRENT_TIME;
1045 spin_unlock(&info->lock);
1046 ret = 0;
1047 }
1048out_fput:
1049 fput(filp);
1050out:
1051 return ret;
1052}
1053
c4ea37c2
HC
1054SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
1055 size_t, msg_len, unsigned int __user *, u_msg_prio,
1056 const struct timespec __user *, u_abs_timeout)
1da177e4 1057{
1da177e4
LT
1058 ssize_t ret;
1059 struct msg_msg *msg_ptr;
1060 struct file *filp;
1061 struct inode *inode;
1062 struct mqueue_inode_info *info;
1063 struct ext_wait_queue wait;
9ca7d8e6
CE
1064 ktime_t expires, *timeout = NULL;
1065 struct timespec ts;
1da177e4 1066
c32c8af4 1067 if (u_abs_timeout) {
9ca7d8e6
CE
1068 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
1069 if (res)
1070 return res;
1071 timeout = &expires;
c32c8af4 1072 }
20ca73bc 1073
9ca7d8e6 1074 audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL);
1da177e4 1075
1da177e4 1076 filp = fget(mqdes);
8d8ffefa
AGR
1077 if (unlikely(!filp)) {
1078 ret = -EBADF;
1da177e4 1079 goto out;
8d8ffefa 1080 }
1da177e4 1081
6d63079a 1082 inode = filp->f_path.dentry->d_inode;
8d8ffefa
AGR
1083 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1084 ret = -EBADF;
1da177e4 1085 goto out_fput;
8d8ffefa 1086 }
1da177e4 1087 info = MQUEUE_I(inode);
5a190ae6 1088 audit_inode(NULL, filp->f_path.dentry);
1da177e4 1089
8d8ffefa
AGR
1090 if (unlikely(!(filp->f_mode & FMODE_READ))) {
1091 ret = -EBADF;
1da177e4 1092 goto out_fput;
8d8ffefa 1093 }
1da177e4
LT
1094
1095 /* checks if buffer is big enough */
1096 if (unlikely(msg_len < info->attr.mq_msgsize)) {
1097 ret = -EMSGSIZE;
1098 goto out_fput;
1099 }
1100
1101 spin_lock(&info->lock);
1102 if (info->attr.mq_curmsgs == 0) {
1103 if (filp->f_flags & O_NONBLOCK) {
1104 spin_unlock(&info->lock);
1105 ret = -EAGAIN;
1da177e4
LT
1106 } else {
1107 wait.task = current;
1108 wait.state = STATE_NONE;
1109 ret = wq_sleep(info, RECV, timeout, &wait);
1110 msg_ptr = wait.msg;
1111 }
1112 } else {
1113 msg_ptr = msg_get(info);
1114
1115 inode->i_atime = inode->i_mtime = inode->i_ctime =
1116 CURRENT_TIME;
1117
1118 /* There is now free space in queue. */
1119 pipelined_receive(info);
1120 spin_unlock(&info->lock);
1121 ret = 0;
1122 }
1123 if (ret == 0) {
1124 ret = msg_ptr->m_ts;
1125
1126 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
1127 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
1128 ret = -EFAULT;
1129 }
1130 free_msg(msg_ptr);
1131 }
1132out_fput:
1133 fput(filp);
1134out:
1135 return ret;
1136}
1137
1138/*
1139 * Notes: the case when user wants us to deregister (with NULL as pointer)
1140 * and he isn't currently owner of notification, will be silently discarded.
1141 * It isn't explicitly defined in the POSIX.
1142 */
c4ea37c2
HC
1143SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1144 const struct sigevent __user *, u_notification)
1da177e4
LT
1145{
1146 int ret;
1147 struct file *filp;
1148 struct sock *sock;
1149 struct inode *inode;
1150 struct sigevent notification;
1151 struct mqueue_inode_info *info;
1152 struct sk_buff *nc;
1153
20114f71 1154 if (u_notification) {
1da177e4
LT
1155 if (copy_from_user(&notification, u_notification,
1156 sizeof(struct sigevent)))
1157 return -EFAULT;
20114f71
AV
1158 }
1159
1160 audit_mq_notify(mqdes, u_notification ? &notification : NULL);
1da177e4 1161
20114f71
AV
1162 nc = NULL;
1163 sock = NULL;
1164 if (u_notification != NULL) {
1da177e4
LT
1165 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1166 notification.sigev_notify != SIGEV_SIGNAL &&
1167 notification.sigev_notify != SIGEV_THREAD))
1168 return -EINVAL;
1169 if (notification.sigev_notify == SIGEV_SIGNAL &&
7ed20e1a 1170 !valid_signal(notification.sigev_signo)) {
1da177e4
LT
1171 return -EINVAL;
1172 }
1173 if (notification.sigev_notify == SIGEV_THREAD) {
c3d8d1e3
PM
1174 long timeo;
1175
1da177e4
LT
1176 /* create the notify skb */
1177 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
8d8ffefa
AGR
1178 if (!nc) {
1179 ret = -ENOMEM;
1da177e4 1180 goto out;
8d8ffefa 1181 }
1da177e4
LT
1182 if (copy_from_user(nc->data,
1183 notification.sigev_value.sival_ptr,
1184 NOTIFY_COOKIE_LEN)) {
8d8ffefa 1185 ret = -EFAULT;
1da177e4
LT
1186 goto out;
1187 }
1188
1189 /* TODO: add a header? */
1190 skb_put(nc, NOTIFY_COOKIE_LEN);
1191 /* and attach it to the socket */
1192retry:
1193 filp = fget(notification.sigev_signo);
8d8ffefa
AGR
1194 if (!filp) {
1195 ret = -EBADF;
1da177e4 1196 goto out;
8d8ffefa 1197 }
1da177e4
LT
1198 sock = netlink_getsockbyfilp(filp);
1199 fput(filp);
1200 if (IS_ERR(sock)) {
1201 ret = PTR_ERR(sock);
1202 sock = NULL;
1203 goto out;
1204 }
1205
c3d8d1e3 1206 timeo = MAX_SCHEDULE_TIMEOUT;
9457afee 1207 ret = netlink_attachskb(sock, nc, &timeo, NULL);
1da177e4 1208 if (ret == 1)
8d8ffefa 1209 goto retry;
1da177e4
LT
1210 if (ret) {
1211 sock = NULL;
1212 nc = NULL;
1213 goto out;
1214 }
1215 }
1216 }
1217
1da177e4 1218 filp = fget(mqdes);
8d8ffefa
AGR
1219 if (!filp) {
1220 ret = -EBADF;
1da177e4 1221 goto out;
8d8ffefa 1222 }
1da177e4 1223
6d63079a 1224 inode = filp->f_path.dentry->d_inode;
8d8ffefa
AGR
1225 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1226 ret = -EBADF;
1da177e4 1227 goto out_fput;
8d8ffefa 1228 }
1da177e4
LT
1229 info = MQUEUE_I(inode);
1230
1231 ret = 0;
1232 spin_lock(&info->lock);
1233 if (u_notification == NULL) {
a03fcb73 1234 if (info->notify_owner == task_tgid(current)) {
1da177e4
LT
1235 remove_notification(info);
1236 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1237 }
a03fcb73 1238 } else if (info->notify_owner != NULL) {
1da177e4
LT
1239 ret = -EBUSY;
1240 } else {
1241 switch (notification.sigev_notify) {
1242 case SIGEV_NONE:
1243 info->notify.sigev_notify = SIGEV_NONE;
1244 break;
1245 case SIGEV_THREAD:
1246 info->notify_sock = sock;
1247 info->notify_cookie = nc;
1248 sock = NULL;
1249 nc = NULL;
1250 info->notify.sigev_notify = SIGEV_THREAD;
1251 break;
1252 case SIGEV_SIGNAL:
1253 info->notify.sigev_signo = notification.sigev_signo;
1254 info->notify.sigev_value = notification.sigev_value;
1255 info->notify.sigev_notify = SIGEV_SIGNAL;
1256 break;
1257 }
a03fcb73
CLG
1258
1259 info->notify_owner = get_pid(task_tgid(current));
6f9ac6d9 1260 info->notify_user_ns = get_user_ns(current_user_ns());
1da177e4
LT
1261 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1262 }
1263 spin_unlock(&info->lock);
1264out_fput:
1265 fput(filp);
1266out:
1267 if (sock) {
1268 netlink_detachskb(sock, nc);
1269 } else if (nc) {
1270 dev_kfree_skb(nc);
1271 }
1272 return ret;
1273}
1274
c4ea37c2
HC
1275SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1276 const struct mq_attr __user *, u_mqstat,
1277 struct mq_attr __user *, u_omqstat)
1da177e4
LT
1278{
1279 int ret;
1280 struct mq_attr mqstat, omqstat;
1281 struct file *filp;
1282 struct inode *inode;
1283 struct mqueue_inode_info *info;
1284
1285 if (u_mqstat != NULL) {
1286 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1287 return -EFAULT;
1288 if (mqstat.mq_flags & (~O_NONBLOCK))
1289 return -EINVAL;
1290 }
1291
1da177e4 1292 filp = fget(mqdes);
8d8ffefa
AGR
1293 if (!filp) {
1294 ret = -EBADF;
1da177e4 1295 goto out;
8d8ffefa 1296 }
1da177e4 1297
6d63079a 1298 inode = filp->f_path.dentry->d_inode;
8d8ffefa
AGR
1299 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1300 ret = -EBADF;
1da177e4 1301 goto out_fput;
8d8ffefa 1302 }
1da177e4
LT
1303 info = MQUEUE_I(inode);
1304
1305 spin_lock(&info->lock);
1306
1307 omqstat = info->attr;
1308 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1309 if (u_mqstat) {
7392906e 1310 audit_mq_getsetattr(mqdes, &mqstat);
db1dd4d3 1311 spin_lock(&filp->f_lock);
1da177e4
LT
1312 if (mqstat.mq_flags & O_NONBLOCK)
1313 filp->f_flags |= O_NONBLOCK;
1314 else
1315 filp->f_flags &= ~O_NONBLOCK;
db1dd4d3 1316 spin_unlock(&filp->f_lock);
1da177e4
LT
1317
1318 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1319 }
1320
1321 spin_unlock(&info->lock);
1322
1323 ret = 0;
1324 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1325 sizeof(struct mq_attr)))
1326 ret = -EFAULT;
1327
1328out_fput:
1329 fput(filp);
1330out:
1331 return ret;
1332}
1333
92e1d5be 1334static const struct inode_operations mqueue_dir_inode_operations = {
1da177e4
LT
1335 .lookup = simple_lookup,
1336 .create = mqueue_create,
1337 .unlink = mqueue_unlink,
1338};
1339
9a32144e 1340static const struct file_operations mqueue_file_operations = {
1da177e4
LT
1341 .flush = mqueue_flush_file,
1342 .poll = mqueue_poll_file,
1343 .read = mqueue_read_file,
6038f373 1344 .llseek = default_llseek,
1da177e4
LT
1345};
1346
b87221de 1347static const struct super_operations mqueue_super_ops = {
1da177e4
LT
1348 .alloc_inode = mqueue_alloc_inode,
1349 .destroy_inode = mqueue_destroy_inode,
6d8af64c 1350 .evict_inode = mqueue_evict_inode,
1da177e4 1351 .statfs = simple_statfs,
1da177e4
LT
1352};
1353
1354static struct file_system_type mqueue_fs_type = {
1355 .name = "mqueue",
ceefda69 1356 .mount = mqueue_mount,
1da177e4
LT
1357 .kill_sb = kill_litter_super,
1358};
1359
7eafd7c7
SH
1360int mq_init_ns(struct ipc_namespace *ns)
1361{
1362 ns->mq_queues_count = 0;
1363 ns->mq_queues_max = DFLT_QUEUESMAX;
1364 ns->mq_msg_max = DFLT_MSGMAX;
1365 ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
cef0184c
KM
1366 ns->mq_msg_default = DFLT_MSG;
1367 ns->mq_msgsize_default = DFLT_MSGSIZE;
7eafd7c7
SH
1368
1369 ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
1370 if (IS_ERR(ns->mq_mnt)) {
1371 int err = PTR_ERR(ns->mq_mnt);
1372 ns->mq_mnt = NULL;
1373 return err;
1374 }
1375 return 0;
1376}
1377
1378void mq_clear_sbinfo(struct ipc_namespace *ns)
1379{
1380 ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1381}
1382
1383void mq_put_mnt(struct ipc_namespace *ns)
1384{
6f686574 1385 kern_unmount(ns->mq_mnt);
7eafd7c7
SH
1386}
1387
1da177e4
LT
1388static int __init init_mqueue_fs(void)
1389{
1390 int error;
1391
1392 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1393 sizeof(struct mqueue_inode_info), 0,
20c2df83 1394 SLAB_HWCACHE_ALIGN, init_once);
1da177e4
LT
1395 if (mqueue_inode_cachep == NULL)
1396 return -ENOMEM;
1397
2329e392 1398 /* ignore failures - they are not fatal */
bdc8e5f8 1399 mq_sysctl_table = mq_register_sysctl_table();
1da177e4
LT
1400
1401 error = register_filesystem(&mqueue_fs_type);
1402 if (error)
1403 goto out_sysctl;
1404
7eafd7c7
SH
1405 spin_lock_init(&mq_lock);
1406
6f686574
AV
1407 error = mq_init_ns(&init_ipc_ns);
1408 if (error)
1da177e4 1409 goto out_filesystem;
1da177e4 1410
1da177e4
LT
1411 return 0;
1412
1413out_filesystem:
1414 unregister_filesystem(&mqueue_fs_type);
1415out_sysctl:
1416 if (mq_sysctl_table)
1417 unregister_sysctl_table(mq_sysctl_table);
1a1d92c1 1418 kmem_cache_destroy(mqueue_inode_cachep);
1da177e4
LT
1419 return error;
1420}
1421
1422__initcall(init_mqueue_fs);