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