blob: 6e10a55a78c5e98e84aa94621a0785d002f265ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * POSIX message queues filesystem for Linux.
3 *
4 * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
Michal Wronskif66e9282006-10-03 23:23:27 +02005 * Michal Wronski (michal.wronski@gmail.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
8 * Lockless receive & send, fd based notify:
9 * Manfred Spraul (manfred@colorfullife.com)
10 *
George C. Wilson20ca73b2006-05-24 16:09:55 -050011 * Audit: George Wilson (ltcgcw@us.ibm.com)
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This file is released under the GPL.
14 */
15
Randy.Dunlapc59ede72006-01-11 12:17:46 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
George C. Wilson20ca73b2006-05-24 16:09:55 -050029#include <linux/audit.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080031#include <linux/mutex.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070032#include <linux/nsproxy.h>
33#include <linux/pid.h>
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070034#include <linux/ipc_namespace.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080035#include <linux/user_namespace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/sock.h>
39#include "util.h"
40
41#define MQUEUE_MAGIC 0x19800202
42#define DIRENT_SIZE 20
43#define FILENT_SIZE 80
44
45#define SEND 0
46#define RECV 1
47
48#define STATE_NONE 0
49#define STATE_PENDING 1
50#define STATE_READY 2
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct ext_wait_queue { /* queue of sleeping tasks */
53 struct task_struct *task;
54 struct list_head list;
55 struct msg_msg *msg; /* ptr of loaded message */
56 int state; /* one of STATE_* values */
57};
58
59struct mqueue_inode_info {
60 spinlock_t lock;
61 struct inode vfs_inode;
62 wait_queue_head_t wait_q;
63
64 struct msg_msg **messages;
65 struct mq_attr attr;
66
67 struct sigevent notify;
Cedric Le Goatera03fcb72006-10-02 02:17:26 -070068 struct pid* notify_owner;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -080069 struct user_namespace *notify_user_ns;
Adrian Bunk338cec32005-09-10 00:26:54 -070070 struct user_struct *user; /* user who created, for accounting */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct sock *notify_sock;
72 struct sk_buff *notify_cookie;
73
74 /* for tasks waiting for free space and messages, respectively */
75 struct ext_wait_queue e_wait_q[2];
76
77 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
78};
79
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080080static const struct inode_operations mqueue_dir_inode_operations;
Arjan van de Ven9a321442007-02-12 00:55:35 -080081static const struct file_operations mqueue_file_operations;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070082static const struct super_operations mqueue_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void remove_notification(struct mqueue_inode_info *info);
84
Christoph Lametere18b8902006-12-06 20:33:20 -080085static struct kmem_cache *mqueue_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static struct ctl_table_header * mq_sysctl_table;
88
89static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
90{
91 return container_of(inode, struct mqueue_inode_info, vfs_inode);
92}
93
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070094/*
95 * This routine should be called with the mq_lock held.
96 */
97static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070098{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070099 return get_ipc_ns(inode->i_sb->s_fs_info);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700100}
101
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700102static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700103{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700104 struct ipc_namespace *ns;
105
106 spin_lock(&mq_lock);
107 ns = __get_ns_from_inode(inode);
108 spin_unlock(&mq_lock);
109 return ns;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700110}
111
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700112static struct inode *mqueue_get_inode(struct super_block *sb,
Al Viro1b9d5ff72011-07-24 14:18:20 -0400113 struct ipc_namespace *ipc_ns, umode_t mode,
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700114 struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
David Howells86a264a2008-11-14 10:39:18 +1100116 struct user_struct *u = current_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct inode *inode;
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700118 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 inode = new_inode(sb);
Jiri Slaby04715202011-07-26 16:08:46 -0700121 if (!inode)
122 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jiri Slaby04715202011-07-26 16:08:46 -0700124 inode->i_ino = get_next_ino();
125 inode->i_mode = mode;
126 inode->i_uid = current_fsuid();
127 inode->i_gid = current_fsgid();
128 inode->i_mtime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jiri Slaby04715202011-07-26 16:08:46 -0700130 if (S_ISREG(mode)) {
131 struct mqueue_inode_info *info;
Jiri Slaby04715202011-07-26 16:08:46 -0700132 unsigned long mq_bytes, mq_msg_tblsz;
André Goddard Rosac8308b12010-02-23 04:04:23 -0300133
Jiri Slaby04715202011-07-26 16:08:46 -0700134 inode->i_fop = &mqueue_file_operations;
135 inode->i_size = FILENT_SIZE;
136 /* mqueue specific info */
137 info = MQUEUE_I(inode);
138 spin_lock_init(&info->lock);
139 init_waitqueue_head(&info->wait_q);
140 INIT_LIST_HEAD(&info->e_wait_q[0].list);
141 INIT_LIST_HEAD(&info->e_wait_q[1].list);
142 info->notify_owner = NULL;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800143 info->notify_user_ns = NULL;
Jiri Slaby04715202011-07-26 16:08:46 -0700144 info->qsize = 0;
145 info->user = NULL; /* set when all is ok */
146 memset(&info->attr, 0, sizeof(info->attr));
Doug Ledford858ee372012-05-31 16:26:29 -0700147 info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max, DFLT_MSG);
148 info->attr.mq_msgsize =
149 min(ipc_ns->mq_msgsize_max, DFLT_MSGSIZE);
Jiri Slaby04715202011-07-26 16:08:46 -0700150 if (attr) {
151 info->attr.mq_maxmsg = attr->mq_maxmsg;
152 info->attr.mq_msgsize = attr->mq_msgsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Jiri Slaby04715202011-07-26 16:08:46 -0700154 mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
155 info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
156 if (!info->messages)
157 goto out_inode;
158
159 mq_bytes = (mq_msg_tblsz +
160 (info->attr.mq_maxmsg * info->attr.mq_msgsize));
161
162 spin_lock(&mq_lock);
163 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
Davidlohr Bueso2a4e64b2012-01-20 14:34:01 -0800164 u->mq_bytes + mq_bytes > rlimit(RLIMIT_MSGQUEUE)) {
Jiri Slaby04715202011-07-26 16:08:46 -0700165 spin_unlock(&mq_lock);
166 /* mqueue_evict_inode() releases info->messages */
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700167 ret = -EMFILE;
Jiri Slaby04715202011-07-26 16:08:46 -0700168 goto out_inode;
169 }
170 u->mq_bytes += mq_bytes;
171 spin_unlock(&mq_lock);
172
173 /* all is ok */
174 info->user = get_uid(u);
175 } else if (S_ISDIR(mode)) {
176 inc_nlink(inode);
177 /* Some things misbehave if size == 0 on a directory */
178 inode->i_size = 2 * DIRENT_SIZE;
179 inode->i_op = &mqueue_dir_inode_operations;
180 inode->i_fop = &simple_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Jiri Slaby04715202011-07-26 16:08:46 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return inode;
184out_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 iput(inode);
Jiri Slaby04715202011-07-26 16:08:46 -0700186err:
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700187 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
191{
192 struct inode *inode;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700193 struct ipc_namespace *ns = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 sb->s_blocksize = PAGE_CACHE_SIZE;
196 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
197 sb->s_magic = MQUEUE_MAGIC;
198 sb->s_op = &mqueue_super_ops;
199
Al Viro48fde702012-01-08 22:15:13 -0500200 inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
201 if (IS_ERR(inode))
202 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Al Viro48fde702012-01-08 22:15:13 -0500204 sb->s_root = d_make_root(inode);
205 if (!sb->s_root)
206 return -ENOMEM;
207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Al Viroceefda62010-07-26 13:16:50 +0400210static struct dentry *mqueue_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700211 int flags, const char *dev_name,
Al Viroceefda62010-07-26 13:16:50 +0400212 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700214 if (!(flags & MS_KERNMOUNT))
215 data = current->nsproxy->ipc_ns;
Al Viroceefda62010-07-26 13:16:50 +0400216 return mount_ns(fs_type, flags, data, mqueue_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700219static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
222
Christoph Lametera35afb82007-05-16 22:10:57 -0700223 inode_init_once(&p->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226static struct inode *mqueue_alloc_inode(struct super_block *sb)
227{
228 struct mqueue_inode_info *ei;
229
Christoph Lametere94b1762006-12-06 20:33:17 -0800230 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!ei)
232 return NULL;
233 return &ei->vfs_inode;
234}
235
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100236static void mqueue_i_callback(struct rcu_head *head)
237{
238 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100239 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static void mqueue_destroy_inode(struct inode *inode)
243{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100244 call_rcu(&inode->i_rcu, mqueue_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Al Viro6d8af642010-06-05 16:29:45 -0400247static void mqueue_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct mqueue_inode_info *info;
250 struct user_struct *user;
251 unsigned long mq_bytes;
252 int i;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700253 struct ipc_namespace *ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jan Karadbd57682012-05-03 14:48:02 +0200255 clear_inode(inode);
Al Viro6d8af642010-06-05 16:29:45 -0400256
257 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return;
Al Viro6d8af642010-06-05 16:29:45 -0400259
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700260 ipc_ns = get_ns_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 info = MQUEUE_I(inode);
262 spin_lock(&info->lock);
263 for (i = 0; i < info->attr.mq_curmsgs; i++)
264 free_msg(info->messages[i]);
265 kfree(info->messages);
266 spin_unlock(&info->lock);
267
André Goddard Rosa8834cf72010-02-23 04:04:24 -0300268 /* Total amount of bytes accounted for the mqueue */
269 mq_bytes = info->attr.mq_maxmsg * (sizeof(struct msg_msg *)
270 + info->attr.mq_msgsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 user = info->user;
272 if (user) {
273 spin_lock(&mq_lock);
274 user->mq_bytes -= mq_bytes;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700275 /*
276 * get_ns_from_inode() ensures that the
277 * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
278 * to which we now hold a reference, or it is NULL.
279 * We can't put it here under mq_lock, though.
280 */
281 if (ipc_ns)
282 ipc_ns->mq_queues_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 spin_unlock(&mq_lock);
284 free_uid(user);
285 }
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700286 if (ipc_ns)
287 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static int mqueue_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -0400291 umode_t mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct inode *inode;
294 struct mq_attr *attr = dentry->d_fsdata;
295 int error;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700296 struct ipc_namespace *ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 spin_lock(&mq_lock);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700299 ipc_ns = __get_ns_from_inode(dir);
300 if (!ipc_ns) {
301 error = -EACCES;
302 goto out_unlock;
303 }
Doug Ledford02967ea2012-05-31 16:26:29 -0700304 if (ipc_ns->mq_queues_count >= HARD_QUEUESMAX ||
305 (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
306 !capable(CAP_SYS_RESOURCE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 error = -ENOSPC;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700308 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700310 ipc_ns->mq_queues_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 spin_unlock(&mq_lock);
312
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700313 inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700314 if (IS_ERR(inode)) {
315 error = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spin_lock(&mq_lock);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700317 ipc_ns->mq_queues_count--;
318 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700321 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dir->i_size += DIRENT_SIZE;
323 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
324
325 d_instantiate(dentry, inode);
326 dget(dentry);
327 return 0;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700328out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 spin_unlock(&mq_lock);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700330 if (ipc_ns)
331 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return error;
333}
334
335static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
336{
337 struct inode *inode = dentry->d_inode;
338
339 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
340 dir->i_size -= DIRENT_SIZE;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700341 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dput(dentry);
343 return 0;
344}
345
346/*
347* This is routine for system read from queue file.
348* To avoid mess with doing here some sort of mq_receive we allow
349* to read only queue size & notification info (the only values
350* that are interesting from user point of view and aren't accessible
351* through std routines)
352*/
353static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700354 size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Josef Sipek6d630792006-12-08 02:37:11 -0800356 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 char buffer[FILENT_SIZE];
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700358 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 spin_lock(&info->lock);
361 snprintf(buffer, sizeof(buffer),
362 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
363 info->qsize,
364 info->notify_owner ? info->notify.sigev_notify : 0,
365 (info->notify_owner &&
366 info->notify.sigev_notify == SIGEV_SIGNAL) ?
367 info->notify.sigev_signo : 0,
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800368 pid_vnr(info->notify_owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_unlock(&info->lock);
370 buffer[sizeof(buffer)-1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700372 ret = simple_read_from_buffer(u_data, count, off, buffer,
373 strlen(buffer));
374 if (ret <= 0)
375 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Josef Sipek6d630792006-12-08 02:37:11 -0800377 filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME;
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700378 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700381static int mqueue_flush_file(struct file *filp, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Josef Sipek6d630792006-12-08 02:37:11 -0800383 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 spin_lock(&info->lock);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700386 if (task_tgid(current) == info->notify_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 remove_notification(info);
388
389 spin_unlock(&info->lock);
390 return 0;
391}
392
393static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
394{
Josef Sipek6d630792006-12-08 02:37:11 -0800395 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int retval = 0;
397
398 poll_wait(filp, &info->wait_q, poll_tab);
399
400 spin_lock(&info->lock);
401 if (info->attr.mq_curmsgs)
402 retval = POLLIN | POLLRDNORM;
403
404 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
405 retval |= POLLOUT | POLLWRNORM;
406 spin_unlock(&info->lock);
407
408 return retval;
409}
410
411/* Adds current to info->e_wait_q[sr] before element with smaller prio */
412static void wq_add(struct mqueue_inode_info *info, int sr,
413 struct ext_wait_queue *ewp)
414{
415 struct ext_wait_queue *walk;
416
417 ewp->task = current;
418
419 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
420 if (walk->task->static_prio <= current->static_prio) {
421 list_add_tail(&ewp->list, &walk->list);
422 return;
423 }
424 }
425 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
426}
427
428/*
429 * Puts current task to sleep. Caller must hold queue lock. After return
430 * lock isn't held.
431 * sr: SEND or RECV
432 */
433static int wq_sleep(struct mqueue_inode_info *info, int sr,
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200434 ktime_t *timeout, struct ext_wait_queue *ewp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 int retval;
437 signed long time;
438
439 wq_add(info, sr, ewp);
440
441 for (;;) {
442 set_current_state(TASK_INTERRUPTIBLE);
443
444 spin_unlock(&info->lock);
Wanlong Gao32ea8452011-10-31 17:06:35 -0700445 time = schedule_hrtimeout_range_clock(timeout, 0,
446 HRTIMER_MODE_ABS, CLOCK_REALTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 while (ewp->state == STATE_PENDING)
449 cpu_relax();
450
451 if (ewp->state == STATE_READY) {
452 retval = 0;
453 goto out;
454 }
455 spin_lock(&info->lock);
456 if (ewp->state == STATE_READY) {
457 retval = 0;
458 goto out_unlock;
459 }
460 if (signal_pending(current)) {
461 retval = -ERESTARTSYS;
462 break;
463 }
464 if (time == 0) {
465 retval = -ETIMEDOUT;
466 break;
467 }
468 }
469 list_del(&ewp->list);
470out_unlock:
471 spin_unlock(&info->lock);
472out:
473 return retval;
474}
475
476/*
477 * Returns waiting task that should be serviced first or NULL if none exists
478 */
479static struct ext_wait_queue *wq_get_first_waiter(
480 struct mqueue_inode_info *info, int sr)
481{
482 struct list_head *ptr;
483
484 ptr = info->e_wait_q[sr].list.prev;
485 if (ptr == &info->e_wait_q[sr].list)
486 return NULL;
487 return list_entry(ptr, struct ext_wait_queue, list);
488}
489
490/* Auxiliary functions to manipulate messages' list */
491static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
492{
493 int k;
494
495 k = info->attr.mq_curmsgs - 1;
496 while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
497 info->messages[k + 1] = info->messages[k];
498 k--;
499 }
500 info->attr.mq_curmsgs++;
501 info->qsize += ptr->m_ts;
502 info->messages[k + 1] = ptr;
503}
504
505static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
506{
507 info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
508 return info->messages[info->attr.mq_curmsgs];
509}
510
511static inline void set_cookie(struct sk_buff *skb, char code)
512{
513 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
514}
515
516/*
517 * The next function is only to split too long sys_mq_timedsend
518 */
519static void __do_notify(struct mqueue_inode_info *info)
520{
521 /* notification
522 * invoked when there is registered process and there isn't process
523 * waiting synchronously for message AND state of queue changed from
524 * empty to not empty. Here we are sure that no one is waiting
525 * synchronously. */
526 if (info->notify_owner &&
527 info->attr.mq_curmsgs == 1) {
528 struct siginfo sig_i;
529 switch (info->notify.sigev_notify) {
530 case SIGEV_NONE:
531 break;
532 case SIGEV_SIGNAL:
533 /* sends signal */
534
535 sig_i.si_signo = info->notify.sigev_signo;
536 sig_i.si_errno = 0;
537 sig_i.si_code = SI_MESGQ;
538 sig_i.si_value = info->notify.sigev_value;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -0800539 /* map current pid/uid into info->owner's namespaces */
540 rcu_read_lock();
Sukadev Bhattiprolua6684992009-01-07 18:08:50 -0800541 sig_i.si_pid = task_tgid_nr_ns(current,
542 ns_of_pid(info->notify_owner));
Eric W. Biederman76b6db02012-03-14 15:24:19 -0700543 sig_i.si_uid = from_kuid_munged(info->notify_user_ns, current_uid());
Serge E. Hallyn6b550f92012-01-10 15:11:37 -0800544 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700546 kill_pid_info(info->notify.sigev_signo,
547 &sig_i, info->notify_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 case SIGEV_THREAD:
550 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700551 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 }
554 /* after notification unregisters process */
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700555 put_pid(info->notify_owner);
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800556 put_user_ns(info->notify_user_ns);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700557 info->notify_owner = NULL;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800558 info->notify_user_ns = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 wake_up(&info->wait_q);
561}
562
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200563static int prepare_timeout(const struct timespec __user *u_abs_timeout,
564 ktime_t *expires, struct timespec *ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200566 if (copy_from_user(ts, u_abs_timeout, sizeof(struct timespec)))
567 return -EFAULT;
568 if (!timespec_valid(ts))
569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200571 *expires = timespec_to_ktime(*ts);
572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575static void remove_notification(struct mqueue_inode_info *info)
576{
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700577 if (info->notify_owner != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 info->notify.sigev_notify == SIGEV_THREAD) {
579 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700580 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700582 put_pid(info->notify_owner);
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800583 put_user_ns(info->notify_user_ns);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700584 info->notify_owner = NULL;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800585 info->notify_user_ns = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700588static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
591 return 0;
592 if (capable(CAP_SYS_RESOURCE)) {
Doug Ledford02967ea2012-05-31 16:26:29 -0700593 if (attr->mq_maxmsg > HARD_MSGMAX ||
594 attr->mq_msgsize > HARD_MSGSIZEMAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700597 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
598 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600 }
601 /* check for overflow */
602 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
603 return 0;
André Goddard Rosa8834cf72010-02-23 04:04:24 -0300604 if ((unsigned long)(attr->mq_maxmsg * (attr->mq_msgsize
605 + sizeof (struct msg_msg *))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
607 return 0;
608 return 1;
609}
610
611/*
612 * Invoked when creating a new queue via sys_mq_open
613 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700614static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
Al Viro4acdaf22011-07-26 01:42:34 -0400615 struct dentry *dentry, int oflag, umode_t mode,
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700616 struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
David Howells745ca242008-11-14 10:39:22 +1100618 const struct cred *cred = current_cred();
Dave Hansen4a3fd212008-02-15 14:37:48 -0800619 struct file *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int ret;
621
Al Viro564f6992008-12-14 04:02:26 -0500622 if (attr) {
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300623 if (!mq_attr_ok(ipc_ns, attr)) {
624 ret = -EINVAL;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500625 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* store for use during create */
Al Viro564f6992008-12-14 04:02:26 -0500628 dentry->d_fsdata = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Al Viroce3b0f82009-03-29 19:08:22 -0400631 mode &= ~current_umask();
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700632 ret = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800633 if (ret)
634 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
636 dentry->d_fsdata = NULL;
637 if (ret)
Dave Hansen4a3fd212008-02-15 14:37:48 -0800638 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700640 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800641 /*
642 * dentry_open() took a persistent mnt_want_write(),
643 * so we can now drop this one.
644 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700645 mnt_drop_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800646 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Dave Hansen4a3fd212008-02-15 14:37:48 -0800648out_drop_write:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700649 mnt_drop_write(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500650out:
651 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700652 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500653 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/* Opens existing queue */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700657static struct file *do_open(struct ipc_namespace *ipc_ns,
658 struct dentry *dentry, int oflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300660 int ret;
David Howells745ca242008-11-14 10:39:22 +1100661 const struct cred *cred = current_cred();
662
663 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
664 MAY_READ | MAY_WRITE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Alexander Viro7c7dce92006-01-14 15:29:55 -0500666 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300667 ret = -EINVAL;
668 goto err;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Al Virof419a2e2008-07-22 00:07:17 -0400671 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300672 ret = -EACCES;
673 goto err;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700676 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300677
678err:
679 dput(dentry);
680 mntput(ipc_ns->mq_mnt);
681 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Al Virodf0a4282011-07-26 05:26:10 -0400684SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
Heiko Carstensd5460c92009-01-14 14:14:27 +0100685 struct mq_attr __user *, u_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct dentry *dentry;
688 struct file *filp;
689 char *name;
Al Viro564f6992008-12-14 04:02:26 -0500690 struct mq_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int fd, error;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700692 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Al Viro564f6992008-12-14 04:02:26 -0500694 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
695 return -EFAULT;
696
697 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -0500698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (IS_ERR(name = getname(u_name)))
700 return PTR_ERR(name);
701
Ulrich Drepper269f2132008-05-03 15:28:45 -0400702 fd = get_unused_fd_flags(O_CLOEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (fd < 0)
704 goto out_putname;
705
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700706 mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
707 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (IS_ERR(dentry)) {
709 error = PTR_ERR(dentry);
André Goddard Rosa4294a8e2010-02-23 04:04:28 -0300710 goto out_putfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700712 mntget(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (oflag & O_CREAT) {
715 if (dentry->d_inode) { /* entry already exists */
Al Viro5a190ae2007-06-07 12:19:32 -0400716 audit_inode(name, dentry);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300717 if (oflag & O_EXCL) {
718 error = -EEXIST;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500719 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300720 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700721 filp = do_open(ipc_ns, dentry, oflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700723 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
724 dentry, oflag, mode,
Al Viro564f6992008-12-14 04:02:26 -0500725 u_attr ? &attr : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Alexander Viro7c7dce92006-01-14 15:29:55 -0500727 } else {
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300728 if (!dentry->d_inode) {
729 error = -ENOENT;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500730 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300731 }
Al Viro5a190ae2007-06-07 12:19:32 -0400732 audit_inode(name, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700733 filp = do_open(ipc_ns, dentry, oflag);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (IS_ERR(filp)) {
737 error = PTR_ERR(filp);
738 goto out_putfd;
739 }
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 fd_install(fd, filp);
742 goto out_upsem;
743
Alexander Viro7c7dce92006-01-14 15:29:55 -0500744out:
745 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700746 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500747out_putfd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 put_unused_fd(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 fd = error;
750out_upsem:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700751 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752out_putname:
753 putname(name);
754 return fd;
755}
756
Heiko Carstensd5460c92009-01-14 14:14:27 +0100757SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int err;
760 char *name;
761 struct dentry *dentry;
762 struct inode *inode = NULL;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700763 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 name = getname(u_name);
766 if (IS_ERR(name))
767 return PTR_ERR(name);
768
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700769 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
Peter Zijlstra7a434812007-03-06 01:42:09 -0800770 I_MUTEX_PARENT);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700771 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (IS_ERR(dentry)) {
773 err = PTR_ERR(dentry);
774 goto out_unlock;
775 }
776
777 if (!dentry->d_inode) {
778 err = -ENOENT;
779 goto out_err;
780 }
781
782 inode = dentry->d_inode;
783 if (inode)
Al Viro7de9c6ee2010-10-23 11:11:40 -0400784 ihold(inode);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700785 err = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800786 if (err)
787 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700789 mnt_drop_write(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790out_err:
791 dput(dentry);
792
793out_unlock:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700794 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 putname(name);
796 if (inode)
797 iput(inode);
798
799 return err;
800}
801
802/* Pipelined send and receive functions.
803 *
804 * If a receiver finds no waiting message, then it registers itself in the
805 * list of waiting receivers. A sender checks that list before adding the new
806 * message into the message array. If there is a waiting receiver, then it
807 * bypasses the message array and directly hands the message over to the
808 * receiver.
809 * The receiver accepts the message and returns without grabbing the queue
810 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
811 * are necessary. The same algorithm is used for sysv semaphores, see
Serge E. Hallyn7b7a3172006-03-28 01:56:23 -0800812 * ipc/sem.c for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *
814 * The same algorithm is used for senders.
815 */
816
817/* pipelined_send() - send a message directly to the task waiting in
818 * sys_mq_timedreceive() (without inserting message into a queue).
819 */
820static inline void pipelined_send(struct mqueue_inode_info *info,
821 struct msg_msg *message,
822 struct ext_wait_queue *receiver)
823{
824 receiver->msg = message;
825 list_del(&receiver->list);
826 receiver->state = STATE_PENDING;
827 wake_up_process(receiver->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700828 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 receiver->state = STATE_READY;
830}
831
832/* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
833 * gets its message and put to the queue (we have one free place for sure). */
834static inline void pipelined_receive(struct mqueue_inode_info *info)
835{
836 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
837
838 if (!sender) {
839 /* for poll */
840 wake_up_interruptible(&info->wait_q);
841 return;
842 }
843 msg_insert(sender->msg, info);
844 list_del(&sender->list);
845 sender->state = STATE_PENDING;
846 wake_up_process(sender->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700847 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 sender->state = STATE_READY;
849}
850
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100851SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
852 size_t, msg_len, unsigned int, msg_prio,
853 const struct timespec __user *, u_abs_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct file *filp;
856 struct inode *inode;
857 struct ext_wait_queue wait;
858 struct ext_wait_queue *receiver;
859 struct msg_msg *msg_ptr;
860 struct mqueue_inode_info *info;
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200861 ktime_t expires, *timeout = NULL;
862 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int ret;
864
Al Viroc32c8af2008-12-14 03:46:48 -0500865 if (u_abs_timeout) {
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200866 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
867 if (res)
868 return res;
869 timeout = &expires;
Al Viroc32c8af2008-12-14 03:46:48 -0500870 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
873 return -EINVAL;
874
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200875 audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300878 if (unlikely(!filp)) {
879 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Josef Sipek6d630792006-12-08 02:37:11 -0800883 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300884 if (unlikely(filp->f_op != &mqueue_file_operations)) {
885 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400889 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300891 if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
892 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (unlikely(msg_len > info->attr.mq_msgsize)) {
897 ret = -EMSGSIZE;
898 goto out_fput;
899 }
900
901 /* First try to allocate memory, before doing anything with
902 * existing queues. */
903 msg_ptr = load_msg(u_msg_ptr, msg_len);
904 if (IS_ERR(msg_ptr)) {
905 ret = PTR_ERR(msg_ptr);
906 goto out_fput;
907 }
908 msg_ptr->m_ts = msg_len;
909 msg_ptr->m_type = msg_prio;
910
911 spin_lock(&info->lock);
912
913 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
914 if (filp->f_flags & O_NONBLOCK) {
915 spin_unlock(&info->lock);
916 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 } else {
918 wait.task = current;
919 wait.msg = (void *) msg_ptr;
920 wait.state = STATE_NONE;
921 ret = wq_sleep(info, SEND, timeout, &wait);
922 }
923 if (ret < 0)
924 free_msg(msg_ptr);
925 } else {
926 receiver = wq_get_first_waiter(info, RECV);
927 if (receiver) {
928 pipelined_send(info, msg_ptr, receiver);
929 } else {
930 /* adds message to the queue */
931 msg_insert(msg_ptr, info);
932 __do_notify(info);
933 }
934 inode->i_atime = inode->i_mtime = inode->i_ctime =
935 CURRENT_TIME;
936 spin_unlock(&info->lock);
937 ret = 0;
938 }
939out_fput:
940 fput(filp);
941out:
942 return ret;
943}
944
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100945SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
946 size_t, msg_len, unsigned int __user *, u_msg_prio,
947 const struct timespec __user *, u_abs_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 ssize_t ret;
950 struct msg_msg *msg_ptr;
951 struct file *filp;
952 struct inode *inode;
953 struct mqueue_inode_info *info;
954 struct ext_wait_queue wait;
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200955 ktime_t expires, *timeout = NULL;
956 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Al Viroc32c8af2008-12-14 03:46:48 -0500958 if (u_abs_timeout) {
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200959 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
960 if (res)
961 return res;
962 timeout = &expires;
Al Viroc32c8af2008-12-14 03:46:48 -0500963 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500964
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200965 audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300968 if (unlikely(!filp)) {
969 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Josef Sipek6d630792006-12-08 02:37:11 -0800973 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300974 if (unlikely(filp->f_op != &mqueue_file_operations)) {
975 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400979 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300981 if (unlikely(!(filp->f_mode & FMODE_READ))) {
982 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /* checks if buffer is big enough */
987 if (unlikely(msg_len < info->attr.mq_msgsize)) {
988 ret = -EMSGSIZE;
989 goto out_fput;
990 }
991
992 spin_lock(&info->lock);
993 if (info->attr.mq_curmsgs == 0) {
994 if (filp->f_flags & O_NONBLOCK) {
995 spin_unlock(&info->lock);
996 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 } else {
998 wait.task = current;
999 wait.state = STATE_NONE;
1000 ret = wq_sleep(info, RECV, timeout, &wait);
1001 msg_ptr = wait.msg;
1002 }
1003 } else {
1004 msg_ptr = msg_get(info);
1005
1006 inode->i_atime = inode->i_mtime = inode->i_ctime =
1007 CURRENT_TIME;
1008
1009 /* There is now free space in queue. */
1010 pipelined_receive(info);
1011 spin_unlock(&info->lock);
1012 ret = 0;
1013 }
1014 if (ret == 0) {
1015 ret = msg_ptr->m_ts;
1016
1017 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
1018 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
1019 ret = -EFAULT;
1020 }
1021 free_msg(msg_ptr);
1022 }
1023out_fput:
1024 fput(filp);
1025out:
1026 return ret;
1027}
1028
1029/*
1030 * Notes: the case when user wants us to deregister (with NULL as pointer)
1031 * and he isn't currently owner of notification, will be silently discarded.
1032 * It isn't explicitly defined in the POSIX.
1033 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001034SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1035 const struct sigevent __user *, u_notification)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 int ret;
1038 struct file *filp;
1039 struct sock *sock;
1040 struct inode *inode;
1041 struct sigevent notification;
1042 struct mqueue_inode_info *info;
1043 struct sk_buff *nc;
1044
Al Viro20114f72008-12-10 07:16:12 -05001045 if (u_notification) {
1046 if (copy_from_user(&notification, u_notification,
1047 sizeof(struct sigevent)))
1048 return -EFAULT;
1049 }
1050
1051 audit_mq_notify(mqdes, u_notification ? &notification : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -05001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 nc = NULL;
1054 sock = NULL;
1055 if (u_notification != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1057 notification.sigev_notify != SIGEV_SIGNAL &&
1058 notification.sigev_notify != SIGEV_THREAD))
1059 return -EINVAL;
1060 if (notification.sigev_notify == SIGEV_SIGNAL &&
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001061 !valid_signal(notification.sigev_signo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -EINVAL;
1063 }
1064 if (notification.sigev_notify == SIGEV_THREAD) {
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001065 long timeo;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* create the notify skb */
1068 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001069 if (!nc) {
1070 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (copy_from_user(nc->data,
1074 notification.sigev_value.sival_ptr,
1075 NOTIFY_COOKIE_LEN)) {
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001076 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto out;
1078 }
1079
1080 /* TODO: add a header? */
1081 skb_put(nc, NOTIFY_COOKIE_LEN);
1082 /* and attach it to the socket */
1083retry:
1084 filp = fget(notification.sigev_signo);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001085 if (!filp) {
1086 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 sock = netlink_getsockbyfilp(filp);
1090 fput(filp);
1091 if (IS_ERR(sock)) {
1092 ret = PTR_ERR(sock);
1093 sock = NULL;
1094 goto out;
1095 }
1096
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001097 timeo = MAX_SCHEDULE_TIMEOUT;
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001098 ret = netlink_attachskb(sock, nc, &timeo, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (ret == 1)
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001100 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (ret) {
1102 sock = NULL;
1103 nc = NULL;
1104 goto out;
1105 }
1106 }
1107 }
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001110 if (!filp) {
1111 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Josef Sipek6d630792006-12-08 02:37:11 -08001115 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001116 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1117 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 info = MQUEUE_I(inode);
1121
1122 ret = 0;
1123 spin_lock(&info->lock);
1124 if (u_notification == NULL) {
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001125 if (info->notify_owner == task_tgid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 remove_notification(info);
1127 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1128 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001129 } else if (info->notify_owner != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 ret = -EBUSY;
1131 } else {
1132 switch (notification.sigev_notify) {
1133 case SIGEV_NONE:
1134 info->notify.sigev_notify = SIGEV_NONE;
1135 break;
1136 case SIGEV_THREAD:
1137 info->notify_sock = sock;
1138 info->notify_cookie = nc;
1139 sock = NULL;
1140 nc = NULL;
1141 info->notify.sigev_notify = SIGEV_THREAD;
1142 break;
1143 case SIGEV_SIGNAL:
1144 info->notify.sigev_signo = notification.sigev_signo;
1145 info->notify.sigev_value = notification.sigev_value;
1146 info->notify.sigev_notify = SIGEV_SIGNAL;
1147 break;
1148 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001149
1150 info->notify_owner = get_pid(task_tgid(current));
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -08001151 info->notify_user_ns = get_user_ns(current_user_ns());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1153 }
1154 spin_unlock(&info->lock);
1155out_fput:
1156 fput(filp);
1157out:
1158 if (sock) {
1159 netlink_detachskb(sock, nc);
1160 } else if (nc) {
1161 dev_kfree_skb(nc);
1162 }
1163 return ret;
1164}
1165
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001166SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1167 const struct mq_attr __user *, u_mqstat,
1168 struct mq_attr __user *, u_omqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 int ret;
1171 struct mq_attr mqstat, omqstat;
1172 struct file *filp;
1173 struct inode *inode;
1174 struct mqueue_inode_info *info;
1175
1176 if (u_mqstat != NULL) {
1177 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1178 return -EFAULT;
1179 if (mqstat.mq_flags & (~O_NONBLOCK))
1180 return -EINVAL;
1181 }
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001184 if (!filp) {
1185 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Josef Sipek6d630792006-12-08 02:37:11 -08001189 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001190 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1191 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 info = MQUEUE_I(inode);
1195
1196 spin_lock(&info->lock);
1197
1198 omqstat = info->attr;
1199 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1200 if (u_mqstat) {
Al Viro73929062008-12-10 06:58:59 -05001201 audit_mq_getsetattr(mqdes, &mqstat);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001202 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (mqstat.mq_flags & O_NONBLOCK)
1204 filp->f_flags |= O_NONBLOCK;
1205 else
1206 filp->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001207 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1210 }
1211
1212 spin_unlock(&info->lock);
1213
1214 ret = 0;
1215 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1216 sizeof(struct mq_attr)))
1217 ret = -EFAULT;
1218
1219out_fput:
1220 fput(filp);
1221out:
1222 return ret;
1223}
1224
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001225static const struct inode_operations mqueue_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .lookup = simple_lookup,
1227 .create = mqueue_create,
1228 .unlink = mqueue_unlink,
1229};
1230
Arjan van de Ven9a321442007-02-12 00:55:35 -08001231static const struct file_operations mqueue_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 .flush = mqueue_flush_file,
1233 .poll = mqueue_poll_file,
1234 .read = mqueue_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001235 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236};
1237
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001238static const struct super_operations mqueue_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 .alloc_inode = mqueue_alloc_inode,
1240 .destroy_inode = mqueue_destroy_inode,
Al Viro6d8af642010-06-05 16:29:45 -04001241 .evict_inode = mqueue_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243};
1244
1245static struct file_system_type mqueue_fs_type = {
1246 .name = "mqueue",
Al Viroceefda62010-07-26 13:16:50 +04001247 .mount = mqueue_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 .kill_sb = kill_litter_super,
1249};
1250
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001251int mq_init_ns(struct ipc_namespace *ns)
1252{
1253 ns->mq_queues_count = 0;
1254 ns->mq_queues_max = DFLT_QUEUESMAX;
1255 ns->mq_msg_max = DFLT_MSGMAX;
1256 ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
1257
1258 ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
1259 if (IS_ERR(ns->mq_mnt)) {
1260 int err = PTR_ERR(ns->mq_mnt);
1261 ns->mq_mnt = NULL;
1262 return err;
1263 }
1264 return 0;
1265}
1266
1267void mq_clear_sbinfo(struct ipc_namespace *ns)
1268{
1269 ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1270}
1271
1272void mq_put_mnt(struct ipc_namespace *ns)
1273{
Al Viro6f686572011-12-09 00:38:50 -05001274 kern_unmount(ns->mq_mnt);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277static int __init init_mqueue_fs(void)
1278{
1279 int error;
1280
1281 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1282 sizeof(struct mqueue_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001283 SLAB_HWCACHE_ALIGN, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (mqueue_inode_cachep == NULL)
1285 return -ENOMEM;
1286
André Goddard Rosa2329e392010-02-23 04:04:27 -03001287 /* ignore failures - they are not fatal */
Serge E. Hallynbdc8e5f2009-04-06 19:01:11 -07001288 mq_sysctl_table = mq_register_sysctl_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 error = register_filesystem(&mqueue_fs_type);
1291 if (error)
1292 goto out_sysctl;
1293
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001294 spin_lock_init(&mq_lock);
1295
Al Viro6f686572011-12-09 00:38:50 -05001296 error = mq_init_ns(&init_ipc_ns);
1297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto out_filesystem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 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);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001307 kmem_cache_destroy(mqueue_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return error;
1309}
1310
1311__initcall(init_mqueue_fs);