blob: 076399ce363ab9d7f80696128609672574aceabd [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>
Doug Ledford5b5c4d12012-05-31 16:26:30 -070027#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/netlink.h>
29#include <linux/syscalls.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050030#include <linux/audit.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070031#include <linux/signal.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080032#include <linux/mutex.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070033#include <linux/nsproxy.h>
34#include <linux/pid.h>
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070035#include <linux/ipc_namespace.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080036#include <linux/user_namespace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/sock.h>
40#include "util.h"
41
42#define MQUEUE_MAGIC 0x19800202
43#define DIRENT_SIZE 20
44#define FILENT_SIZE 80
45
46#define SEND 0
47#define RECV 1
48
49#define STATE_NONE 0
50#define STATE_PENDING 1
51#define STATE_READY 2
52
Doug Ledfordd6629852012-05-31 16:26:35 -070053struct posix_msg_tree_node {
54 struct rb_node rb_node;
55 struct list_head msg_list;
56 int priority;
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct 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
Doug Ledfordd6629852012-05-31 16:26:35 -070071 struct rb_root msg_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct mq_attr attr;
73
74 struct sigevent notify;
Cedric Le Goatera03fcb72006-10-02 02:17:26 -070075 struct pid* notify_owner;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -080076 struct user_namespace *notify_user_ns;
Adrian Bunk338cec32005-09-10 00:26:54 -070077 struct user_struct *user; /* user who created, for accounting */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct sock *notify_sock;
79 struct sk_buff *notify_cookie;
80
81 /* for tasks waiting for free space and messages, respectively */
82 struct ext_wait_queue e_wait_q[2];
83
84 unsigned long qsize; /* size of queue in memory (sum of all msgs) */
85};
86
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080087static const struct inode_operations mqueue_dir_inode_operations;
Arjan van de Ven9a321442007-02-12 00:55:35 -080088static const struct file_operations mqueue_file_operations;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070089static const struct super_operations mqueue_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void remove_notification(struct mqueue_inode_info *info);
91
Christoph Lametere18b8902006-12-06 20:33:20 -080092static struct kmem_cache *mqueue_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
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
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700101/*
102 * This routine should be called with the mq_lock held.
103 */
104static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode)
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700105{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700106 return get_ipc_ns(inode->i_sb->s_fs_info);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700107}
108
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700109static struct ipc_namespace *get_ns_from_inode(struct inode *inode)
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700110{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700111 struct ipc_namespace *ns;
112
113 spin_lock(&mq_lock);
114 ns = __get_ns_from_inode(inode);
115 spin_unlock(&mq_lock);
116 return ns;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700117}
118
Doug Ledfordd6629852012-05-31 16:26:35 -0700119/* Auxiliary functions to manipulate messages' list */
120static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
121{
122 struct rb_node **p, *parent = NULL;
123 struct posix_msg_tree_node *leaf;
124
125 p = &info->msg_tree.rb_node;
126 while (*p) {
127 parent = *p;
128 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
129
130 if (likely(leaf->priority == msg->m_type))
131 goto insert_msg;
132 else if (msg->m_type < leaf->priority)
133 p = &(*p)->rb_left;
134 else
135 p = &(*p)->rb_right;
136 }
137 leaf = kzalloc(sizeof(*leaf), GFP_ATOMIC);
138 if (!leaf)
139 return -ENOMEM;
140 rb_init_node(&leaf->rb_node);
141 INIT_LIST_HEAD(&leaf->msg_list);
142 leaf->priority = msg->m_type;
143 rb_link_node(&leaf->rb_node, parent, p);
144 rb_insert_color(&leaf->rb_node, &info->msg_tree);
145 info->qsize += sizeof(struct posix_msg_tree_node);
146insert_msg:
147 info->attr.mq_curmsgs++;
148 info->qsize += msg->m_ts;
149 list_add_tail(&msg->m_list, &leaf->msg_list);
150 return 0;
151}
152
153static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
154{
155 struct rb_node **p, *parent = NULL;
156 struct posix_msg_tree_node *leaf;
157 struct msg_msg *msg;
158
159try_again:
160 p = &info->msg_tree.rb_node;
161 while (*p) {
162 parent = *p;
163 /*
164 * During insert, low priorities go to the left and high to the
165 * right. On receive, we want the highest priorities first, so
166 * walk all the way to the right.
167 */
168 p = &(*p)->rb_right;
169 }
170 if (!parent) {
171 if (info->attr.mq_curmsgs) {
172 pr_warn_once("Inconsistency in POSIX message queue, "
173 "no tree element, but supposedly messages "
174 "should exist!\n");
175 info->attr.mq_curmsgs = 0;
176 }
177 return NULL;
178 }
179 leaf = rb_entry(parent, struct posix_msg_tree_node, rb_node);
180 if (list_empty(&leaf->msg_list)) {
181 pr_warn_once("Inconsistency in POSIX message queue, "
182 "empty leaf node but we haven't implemented "
183 "lazy leaf delete!\n");
184 rb_erase(&leaf->rb_node, &info->msg_tree);
185 info->qsize -= sizeof(struct posix_msg_tree_node);
186 kfree(leaf);
187 goto try_again;
188 } else {
189 msg = list_first_entry(&leaf->msg_list,
190 struct msg_msg, m_list);
191 list_del(&msg->m_list);
192 if (list_empty(&leaf->msg_list)) {
193 rb_erase(&leaf->rb_node, &info->msg_tree);
194 info->qsize -= sizeof(struct posix_msg_tree_node);
195 kfree(leaf);
196 }
197 }
198 info->attr.mq_curmsgs--;
199 info->qsize -= msg->m_ts;
200 return msg;
201}
202
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700203static struct inode *mqueue_get_inode(struct super_block *sb,
Al Viro1b9d5ff72011-07-24 14:18:20 -0400204 struct ipc_namespace *ipc_ns, umode_t mode,
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700205 struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
David Howells86a264a2008-11-14 10:39:18 +1100207 struct user_struct *u = current_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct inode *inode;
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700209 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 inode = new_inode(sb);
Jiri Slaby04715202011-07-26 16:08:46 -0700212 if (!inode)
213 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jiri Slaby04715202011-07-26 16:08:46 -0700215 inode->i_ino = get_next_ino();
216 inode->i_mode = mode;
217 inode->i_uid = current_fsuid();
218 inode->i_gid = current_fsgid();
219 inode->i_mtime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jiri Slaby04715202011-07-26 16:08:46 -0700221 if (S_ISREG(mode)) {
222 struct mqueue_inode_info *info;
Doug Ledfordd6629852012-05-31 16:26:35 -0700223 unsigned long mq_bytes, mq_treesize;
André Goddard Rosac8308b12010-02-23 04:04:23 -0300224
Jiri Slaby04715202011-07-26 16:08:46 -0700225 inode->i_fop = &mqueue_file_operations;
226 inode->i_size = FILENT_SIZE;
227 /* mqueue specific info */
228 info = MQUEUE_I(inode);
229 spin_lock_init(&info->lock);
230 init_waitqueue_head(&info->wait_q);
231 INIT_LIST_HEAD(&info->e_wait_q[0].list);
232 INIT_LIST_HEAD(&info->e_wait_q[1].list);
233 info->notify_owner = NULL;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800234 info->notify_user_ns = NULL;
Jiri Slaby04715202011-07-26 16:08:46 -0700235 info->qsize = 0;
236 info->user = NULL; /* set when all is ok */
Doug Ledfordd6629852012-05-31 16:26:35 -0700237 info->msg_tree = RB_ROOT;
Jiri Slaby04715202011-07-26 16:08:46 -0700238 memset(&info->attr, 0, sizeof(info->attr));
KOSAKI Motohirocef01842012-05-31 16:26:33 -0700239 info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max,
240 ipc_ns->mq_msg_default);
241 info->attr.mq_msgsize = min(ipc_ns->mq_msgsize_max,
242 ipc_ns->mq_msgsize_default);
Jiri Slaby04715202011-07-26 16:08:46 -0700243 if (attr) {
244 info->attr.mq_maxmsg = attr->mq_maxmsg;
245 info->attr.mq_msgsize = attr->mq_msgsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Doug Ledfordd6629852012-05-31 16:26:35 -0700247 /*
248 * We used to allocate a static array of pointers and account
249 * the size of that array as well as one msg_msg struct per
250 * possible message into the queue size. That's no longer
251 * accurate as the queue is now an rbtree and will grow and
252 * shrink depending on usage patterns. We can, however, still
253 * account one msg_msg struct per message, but the nodes are
254 * allocated depending on priority usage, and most programs
255 * only use one, or a handful, of priorities. However, since
256 * this is pinned memory, we need to assume worst case, so
257 * that means the min(mq_maxmsg, max_priorities) * struct
258 * posix_msg_tree_node.
259 */
260 mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
261 min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
262 sizeof(struct posix_msg_tree_node);
Jiri Slaby04715202011-07-26 16:08:46 -0700263
Doug Ledfordd6629852012-05-31 16:26:35 -0700264 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
265 info->attr.mq_msgsize);
Jiri Slaby04715202011-07-26 16:08:46 -0700266
267 spin_lock(&mq_lock);
268 if (u->mq_bytes + mq_bytes < u->mq_bytes ||
Davidlohr Bueso2a4e64b2012-01-20 14:34:01 -0800269 u->mq_bytes + mq_bytes > rlimit(RLIMIT_MSGQUEUE)) {
Jiri Slaby04715202011-07-26 16:08:46 -0700270 spin_unlock(&mq_lock);
271 /* mqueue_evict_inode() releases info->messages */
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700272 ret = -EMFILE;
Jiri Slaby04715202011-07-26 16:08:46 -0700273 goto out_inode;
274 }
275 u->mq_bytes += mq_bytes;
276 spin_unlock(&mq_lock);
277
278 /* all is ok */
279 info->user = get_uid(u);
280 } else if (S_ISDIR(mode)) {
281 inc_nlink(inode);
282 /* Some things misbehave if size == 0 on a directory */
283 inode->i_size = 2 * DIRENT_SIZE;
284 inode->i_op = &mqueue_dir_inode_operations;
285 inode->i_fop = &simple_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Jiri Slaby04715202011-07-26 16:08:46 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return inode;
289out_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 iput(inode);
Jiri Slaby04715202011-07-26 16:08:46 -0700291err:
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700292 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
296{
297 struct inode *inode;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700298 struct ipc_namespace *ns = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 sb->s_blocksize = PAGE_CACHE_SIZE;
301 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
302 sb->s_magic = MQUEUE_MAGIC;
303 sb->s_op = &mqueue_super_ops;
304
Al Viro48fde702012-01-08 22:15:13 -0500305 inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
306 if (IS_ERR(inode))
307 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Al Viro48fde702012-01-08 22:15:13 -0500309 sb->s_root = d_make_root(inode);
310 if (!sb->s_root)
311 return -ENOMEM;
312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Al Viroceefda62010-07-26 13:16:50 +0400315static struct dentry *mqueue_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700316 int flags, const char *dev_name,
Al Viroceefda62010-07-26 13:16:50 +0400317 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700319 if (!(flags & MS_KERNMOUNT))
320 data = current->nsproxy->ipc_ns;
Al Viroceefda62010-07-26 13:16:50 +0400321 return mount_ns(fs_type, flags, data, mqueue_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700324static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
327
Christoph Lametera35afb82007-05-16 22:10:57 -0700328 inode_init_once(&p->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331static struct inode *mqueue_alloc_inode(struct super_block *sb)
332{
333 struct mqueue_inode_info *ei;
334
Christoph Lametere94b1762006-12-06 20:33:17 -0800335 ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!ei)
337 return NULL;
338 return &ei->vfs_inode;
339}
340
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100341static void mqueue_i_callback(struct rcu_head *head)
342{
343 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100344 kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static void mqueue_destroy_inode(struct inode *inode)
348{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100349 call_rcu(&inode->i_rcu, mqueue_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Al Viro6d8af642010-06-05 16:29:45 -0400352static void mqueue_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct mqueue_inode_info *info;
355 struct user_struct *user;
Doug Ledfordd6629852012-05-31 16:26:35 -0700356 unsigned long mq_bytes, mq_treesize;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700357 struct ipc_namespace *ipc_ns;
Doug Ledfordd6629852012-05-31 16:26:35 -0700358 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jan Karadbd57682012-05-03 14:48:02 +0200360 clear_inode(inode);
Al Viro6d8af642010-06-05 16:29:45 -0400361
362 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return;
Al Viro6d8af642010-06-05 16:29:45 -0400364
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700365 ipc_ns = get_ns_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 info = MQUEUE_I(inode);
367 spin_lock(&info->lock);
Doug Ledfordd6629852012-05-31 16:26:35 -0700368 while ((msg = msg_get(info)) != NULL)
369 free_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock(&info->lock);
371
André Goddard Rosa8834cf72010-02-23 04:04:24 -0300372 /* Total amount of bytes accounted for the mqueue */
Doug Ledfordd6629852012-05-31 16:26:35 -0700373 mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) +
374 min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) *
375 sizeof(struct posix_msg_tree_node);
376
377 mq_bytes = mq_treesize + (info->attr.mq_maxmsg *
378 info->attr.mq_msgsize);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 user = info->user;
381 if (user) {
382 spin_lock(&mq_lock);
383 user->mq_bytes -= mq_bytes;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700384 /*
385 * get_ns_from_inode() ensures that the
386 * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns
387 * to which we now hold a reference, or it is NULL.
388 * We can't put it here under mq_lock, though.
389 */
390 if (ipc_ns)
391 ipc_ns->mq_queues_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_unlock(&mq_lock);
393 free_uid(user);
394 }
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700395 if (ipc_ns)
396 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static int mqueue_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -0400400 umode_t mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct inode *inode;
403 struct mq_attr *attr = dentry->d_fsdata;
404 int error;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700405 struct ipc_namespace *ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 spin_lock(&mq_lock);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700408 ipc_ns = __get_ns_from_inode(dir);
409 if (!ipc_ns) {
410 error = -EACCES;
411 goto out_unlock;
412 }
Doug Ledford02967ea2012-05-31 16:26:29 -0700413 if (ipc_ns->mq_queues_count >= HARD_QUEUESMAX ||
414 (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max &&
415 !capable(CAP_SYS_RESOURCE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 error = -ENOSPC;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700417 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700419 ipc_ns->mq_queues_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_unlock(&mq_lock);
421
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700422 inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr);
Jiri Slabyd40dcdb2011-07-26 16:08:47 -0700423 if (IS_ERR(inode)) {
424 error = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_lock(&mq_lock);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700426 ipc_ns->mq_queues_count--;
427 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700430 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dir->i_size += DIRENT_SIZE;
432 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
433
434 d_instantiate(dentry, inode);
435 dget(dentry);
436 return 0;
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700437out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_unlock(&mq_lock);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700439 if (ipc_ns)
440 put_ipc_ns(ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return error;
442}
443
444static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
445{
446 struct inode *inode = dentry->d_inode;
447
448 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
449 dir->i_size -= DIRENT_SIZE;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700450 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 dput(dentry);
452 return 0;
453}
454
455/*
456* This is routine for system read from queue file.
457* To avoid mess with doing here some sort of mq_receive we allow
458* to read only queue size & notification info (the only values
459* that are interesting from user point of view and aren't accessible
460* through std routines)
461*/
462static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700463 size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Josef Sipek6d630792006-12-08 02:37:11 -0800465 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 char buffer[FILENT_SIZE];
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700467 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 spin_lock(&info->lock);
470 snprintf(buffer, sizeof(buffer),
471 "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
472 info->qsize,
473 info->notify_owner ? info->notify.sigev_notify : 0,
474 (info->notify_owner &&
475 info->notify.sigev_notify == SIGEV_SIGNAL) ?
476 info->notify.sigev_signo : 0,
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800477 pid_vnr(info->notify_owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 spin_unlock(&info->lock);
479 buffer[sizeof(buffer)-1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Akinobu Mitaf1a43f92008-07-25 01:48:07 -0700481 ret = simple_read_from_buffer(u_data, count, off, buffer,
482 strlen(buffer));
483 if (ret <= 0)
484 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Josef Sipek6d630792006-12-08 02:37:11 -0800486 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 -0700487 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700490static int mqueue_flush_file(struct file *filp, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Josef Sipek6d630792006-12-08 02:37:11 -0800492 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 spin_lock(&info->lock);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700495 if (task_tgid(current) == info->notify_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 remove_notification(info);
497
498 spin_unlock(&info->lock);
499 return 0;
500}
501
502static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
503{
Josef Sipek6d630792006-12-08 02:37:11 -0800504 struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int retval = 0;
506
507 poll_wait(filp, &info->wait_q, poll_tab);
508
509 spin_lock(&info->lock);
510 if (info->attr.mq_curmsgs)
511 retval = POLLIN | POLLRDNORM;
512
513 if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
514 retval |= POLLOUT | POLLWRNORM;
515 spin_unlock(&info->lock);
516
517 return retval;
518}
519
520/* Adds current to info->e_wait_q[sr] before element with smaller prio */
521static void wq_add(struct mqueue_inode_info *info, int sr,
522 struct ext_wait_queue *ewp)
523{
524 struct ext_wait_queue *walk;
525
526 ewp->task = current;
527
528 list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
529 if (walk->task->static_prio <= current->static_prio) {
530 list_add_tail(&ewp->list, &walk->list);
531 return;
532 }
533 }
534 list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
535}
536
537/*
538 * Puts current task to sleep. Caller must hold queue lock. After return
539 * lock isn't held.
540 * sr: SEND or RECV
541 */
542static int wq_sleep(struct mqueue_inode_info *info, int sr,
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200543 ktime_t *timeout, struct ext_wait_queue *ewp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 int retval;
546 signed long time;
547
548 wq_add(info, sr, ewp);
549
550 for (;;) {
551 set_current_state(TASK_INTERRUPTIBLE);
552
553 spin_unlock(&info->lock);
Wanlong Gao32ea8452011-10-31 17:06:35 -0700554 time = schedule_hrtimeout_range_clock(timeout, 0,
555 HRTIMER_MODE_ABS, CLOCK_REALTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 while (ewp->state == STATE_PENDING)
558 cpu_relax();
559
560 if (ewp->state == STATE_READY) {
561 retval = 0;
562 goto out;
563 }
564 spin_lock(&info->lock);
565 if (ewp->state == STATE_READY) {
566 retval = 0;
567 goto out_unlock;
568 }
569 if (signal_pending(current)) {
570 retval = -ERESTARTSYS;
571 break;
572 }
573 if (time == 0) {
574 retval = -ETIMEDOUT;
575 break;
576 }
577 }
578 list_del(&ewp->list);
579out_unlock:
580 spin_unlock(&info->lock);
581out:
582 return retval;
583}
584
585/*
586 * Returns waiting task that should be serviced first or NULL if none exists
587 */
588static struct ext_wait_queue *wq_get_first_waiter(
589 struct mqueue_inode_info *info, int sr)
590{
591 struct list_head *ptr;
592
593 ptr = info->e_wait_q[sr].list.prev;
594 if (ptr == &info->e_wait_q[sr].list)
595 return NULL;
596 return list_entry(ptr, struct ext_wait_queue, list);
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600static inline void set_cookie(struct sk_buff *skb, char code)
601{
602 ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
603}
604
605/*
606 * The next function is only to split too long sys_mq_timedsend
607 */
608static void __do_notify(struct mqueue_inode_info *info)
609{
610 /* notification
611 * invoked when there is registered process and there isn't process
612 * waiting synchronously for message AND state of queue changed from
613 * empty to not empty. Here we are sure that no one is waiting
614 * synchronously. */
615 if (info->notify_owner &&
616 info->attr.mq_curmsgs == 1) {
617 struct siginfo sig_i;
618 switch (info->notify.sigev_notify) {
619 case SIGEV_NONE:
620 break;
621 case SIGEV_SIGNAL:
622 /* sends signal */
623
624 sig_i.si_signo = info->notify.sigev_signo;
625 sig_i.si_errno = 0;
626 sig_i.si_code = SI_MESGQ;
627 sig_i.si_value = info->notify.sigev_value;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -0800628 /* map current pid/uid into info->owner's namespaces */
629 rcu_read_lock();
Sukadev Bhattiprolua6684992009-01-07 18:08:50 -0800630 sig_i.si_pid = task_tgid_nr_ns(current,
631 ns_of_pid(info->notify_owner));
Eric W. Biederman76b6db02012-03-14 15:24:19 -0700632 sig_i.si_uid = from_kuid_munged(info->notify_user_ns, current_uid());
Serge E. Hallyn6b550f92012-01-10 15:11:37 -0800633 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700635 kill_pid_info(info->notify.sigev_signo,
636 &sig_i, info->notify_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638 case SIGEV_THREAD:
639 set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700640 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642 }
643 /* after notification unregisters process */
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700644 put_pid(info->notify_owner);
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800645 put_user_ns(info->notify_user_ns);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700646 info->notify_owner = NULL;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800647 info->notify_user_ns = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 wake_up(&info->wait_q);
650}
651
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200652static int prepare_timeout(const struct timespec __user *u_abs_timeout,
653 ktime_t *expires, struct timespec *ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200655 if (copy_from_user(ts, u_abs_timeout, sizeof(struct timespec)))
656 return -EFAULT;
657 if (!timespec_valid(ts))
658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200660 *expires = timespec_to_ktime(*ts);
661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static void remove_notification(struct mqueue_inode_info *info)
665{
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700666 if (info->notify_owner != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 info->notify.sigev_notify == SIGEV_THREAD) {
668 set_cookie(info->notify_cookie, NOTIFY_REMOVED);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700669 netlink_sendskb(info->notify_sock, info->notify_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700671 put_pid(info->notify_owner);
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800672 put_user_ns(info->notify_user_ns);
Cedric Le Goatera03fcb72006-10-02 02:17:26 -0700673 info->notify_owner = NULL;
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -0800674 info->notify_user_ns = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700677static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Doug Ledford2c12ea42012-05-31 16:26:36 -0700679 int mq_treesize;
680 unsigned long total_size;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
683 return 0;
684 if (capable(CAP_SYS_RESOURCE)) {
Doug Ledford02967ea2012-05-31 16:26:29 -0700685 if (attr->mq_maxmsg > HARD_MSGMAX ||
686 attr->mq_msgsize > HARD_MSGSIZEMAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700689 if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
690 attr->mq_msgsize > ipc_ns->mq_msgsize_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692 }
693 /* check for overflow */
694 if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
695 return 0;
Doug Ledford2c12ea42012-05-31 16:26:36 -0700696 mq_treesize = attr->mq_maxmsg * sizeof(struct msg_msg) +
697 min_t(unsigned int, attr->mq_maxmsg, MQ_PRIO_MAX) *
698 sizeof(struct posix_msg_tree_node);
699 total_size = attr->mq_maxmsg * attr->mq_msgsize;
700 if (total_size + mq_treesize < total_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702 return 1;
703}
704
705/*
706 * Invoked when creating a new queue via sys_mq_open
707 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700708static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
Al Viro4acdaf22011-07-26 01:42:34 -0400709 struct dentry *dentry, int oflag, umode_t mode,
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700710 struct mq_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
David Howells745ca242008-11-14 10:39:22 +1100712 const struct cred *cred = current_cred();
Dave Hansen4a3fd212008-02-15 14:37:48 -0800713 struct file *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int ret;
715
Al Viro564f6992008-12-14 04:02:26 -0500716 if (attr) {
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300717 if (!mq_attr_ok(ipc_ns, attr)) {
718 ret = -EINVAL;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500719 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* store for use during create */
Al Viro564f6992008-12-14 04:02:26 -0500722 dentry->d_fsdata = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Al Viroce3b0f82009-03-29 19:08:22 -0400725 mode &= ~current_umask();
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700726 ret = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800727 if (ret)
728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ret = vfs_create(dir->d_inode, dentry, mode, NULL);
730 dentry->d_fsdata = NULL;
731 if (ret)
Dave Hansen4a3fd212008-02-15 14:37:48 -0800732 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700734 result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800735 /*
736 * dentry_open() took a persistent mnt_want_write(),
737 * so we can now drop this one.
738 */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700739 mnt_drop_write(ipc_ns->mq_mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800740 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Hansen4a3fd212008-02-15 14:37:48 -0800742out_drop_write:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700743 mnt_drop_write(ipc_ns->mq_mnt);
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 -0500747 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/* Opens existing queue */
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700751static struct file *do_open(struct ipc_namespace *ipc_ns,
752 struct dentry *dentry, int oflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300754 int ret;
David Howells745ca242008-11-14 10:39:22 +1100755 const struct cred *cred = current_cred();
756
757 static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
758 MAY_READ | MAY_WRITE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Alexander Viro7c7dce92006-01-14 15:29:55 -0500760 if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) {
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300761 ret = -EINVAL;
762 goto err;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Al Virof419a2e2008-07-22 00:07:17 -0400765 if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) {
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300766 ret = -EACCES;
767 goto err;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700770 return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred);
André Goddard Rosa04db0dd2010-02-23 04:04:25 -0300771
772err:
773 dput(dentry);
774 mntput(ipc_ns->mq_mnt);
775 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Al Virodf0a4282011-07-26 05:26:10 -0400778SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
Heiko Carstensd5460c92009-01-14 14:14:27 +0100779 struct mq_attr __user *, u_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 struct dentry *dentry;
782 struct file *filp;
783 char *name;
Al Viro564f6992008-12-14 04:02:26 -0500784 struct mq_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int fd, error;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700786 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Al Viro564f6992008-12-14 04:02:26 -0500788 if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
789 return -EFAULT;
790
791 audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -0500792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (IS_ERR(name = getname(u_name)))
794 return PTR_ERR(name);
795
Ulrich Drepper269f2132008-05-03 15:28:45 -0400796 fd = get_unused_fd_flags(O_CLOEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (fd < 0)
798 goto out_putname;
799
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700800 mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
801 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (IS_ERR(dentry)) {
803 error = PTR_ERR(dentry);
André Goddard Rosa4294a8e2010-02-23 04:04:28 -0300804 goto out_putfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700806 mntget(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (oflag & O_CREAT) {
809 if (dentry->d_inode) { /* entry already exists */
Al Viro5a190ae2007-06-07 12:19:32 -0400810 audit_inode(name, dentry);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300811 if (oflag & O_EXCL) {
812 error = -EEXIST;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500813 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300814 }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700815 filp = do_open(ipc_ns, dentry, oflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 } else {
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700817 filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root,
818 dentry, oflag, mode,
Al Viro564f6992008-12-14 04:02:26 -0500819 u_attr ? &attr : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Alexander Viro7c7dce92006-01-14 15:29:55 -0500821 } else {
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300822 if (!dentry->d_inode) {
823 error = -ENOENT;
Alexander Viro7c7dce92006-01-14 15:29:55 -0500824 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300825 }
Al Viro5a190ae2007-06-07 12:19:32 -0400826 audit_inode(name, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700827 filp = do_open(ipc_ns, dentry, oflag);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (IS_ERR(filp)) {
831 error = PTR_ERR(filp);
832 goto out_putfd;
833 }
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 fd_install(fd, filp);
836 goto out_upsem;
837
Alexander Viro7c7dce92006-01-14 15:29:55 -0500838out:
839 dput(dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700840 mntput(ipc_ns->mq_mnt);
Alexander Viro7c7dce92006-01-14 15:29:55 -0500841out_putfd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 put_unused_fd(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 fd = error;
844out_upsem:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700845 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846out_putname:
847 putname(name);
848 return fd;
849}
850
Heiko Carstensd5460c92009-01-14 14:14:27 +0100851SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int err;
854 char *name;
855 struct dentry *dentry;
856 struct inode *inode = NULL;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700857 struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 name = getname(u_name);
860 if (IS_ERR(name))
861 return PTR_ERR(name);
862
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700863 mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
Peter Zijlstra7a434812007-03-06 01:42:09 -0800864 I_MUTEX_PARENT);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700865 dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (IS_ERR(dentry)) {
867 err = PTR_ERR(dentry);
868 goto out_unlock;
869 }
870
871 if (!dentry->d_inode) {
872 err = -ENOENT;
873 goto out_err;
874 }
875
876 inode = dentry->d_inode;
877 if (inode)
Al Viro7de9c6ee2010-10-23 11:11:40 -0400878 ihold(inode);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700879 err = mnt_want_write(ipc_ns->mq_mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800880 if (err)
881 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 err = vfs_unlink(dentry->d_parent->d_inode, dentry);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700883 mnt_drop_write(ipc_ns->mq_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884out_err:
885 dput(dentry);
886
887out_unlock:
Serge E. Hallyn614b84c2009-04-06 19:01:08 -0700888 mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 putname(name);
890 if (inode)
891 iput(inode);
892
893 return err;
894}
895
896/* Pipelined send and receive functions.
897 *
898 * If a receiver finds no waiting message, then it registers itself in the
899 * list of waiting receivers. A sender checks that list before adding the new
900 * message into the message array. If there is a waiting receiver, then it
901 * bypasses the message array and directly hands the message over to the
902 * receiver.
903 * The receiver accepts the message and returns without grabbing the queue
904 * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
905 * are necessary. The same algorithm is used for sysv semaphores, see
Serge E. Hallyn7b7a3172006-03-28 01:56:23 -0800906 * ipc/sem.c for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 *
908 * The same algorithm is used for senders.
909 */
910
911/* pipelined_send() - send a message directly to the task waiting in
912 * sys_mq_timedreceive() (without inserting message into a queue).
913 */
914static inline void pipelined_send(struct mqueue_inode_info *info,
915 struct msg_msg *message,
916 struct ext_wait_queue *receiver)
917{
918 receiver->msg = message;
919 list_del(&receiver->list);
920 receiver->state = STATE_PENDING;
921 wake_up_process(receiver->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700922 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 receiver->state = STATE_READY;
924}
925
926/* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
927 * gets its message and put to the queue (we have one free place for sure). */
928static inline void pipelined_receive(struct mqueue_inode_info *info)
929{
930 struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
931
932 if (!sender) {
933 /* for poll */
934 wake_up_interruptible(&info->wait_q);
935 return;
936 }
Doug Ledfordd6629852012-05-31 16:26:35 -0700937 if (msg_insert(sender->msg, info))
938 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 list_del(&sender->list);
940 sender->state = STATE_PENDING;
941 wake_up_process(sender->task);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700942 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 sender->state = STATE_READY;
944}
945
Heiko Carstensc4ea37c2009-01-14 14:14:28 +0100946SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr,
947 size_t, msg_len, unsigned int, msg_prio,
948 const struct timespec __user *, u_abs_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct file *filp;
951 struct inode *inode;
952 struct ext_wait_queue wait;
953 struct ext_wait_queue *receiver;
954 struct msg_msg *msg_ptr;
955 struct mqueue_inode_info *info;
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200956 ktime_t expires, *timeout = NULL;
957 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int ret;
959
Al Viroc32c8af2008-12-14 03:46:48 -0500960 if (u_abs_timeout) {
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200961 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
962 if (res)
963 return res;
964 timeout = &expires;
Al Viroc32c8af2008-12-14 03:46:48 -0500965 }
George C. Wilson20ca73b2006-05-24 16:09:55 -0500966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
968 return -EINVAL;
969
Carsten Emde9ca7d8e2010-04-02 22:40:20 +0200970 audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300973 if (unlikely(!filp)) {
974 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Josef Sipek6d630792006-12-08 02:37:11 -0800978 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300979 if (unlikely(filp->f_op != &mqueue_file_operations)) {
980 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400984 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300986 if (unlikely(!(filp->f_mode & FMODE_WRITE))) {
987 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -0300989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (unlikely(msg_len > info->attr.mq_msgsize)) {
992 ret = -EMSGSIZE;
993 goto out_fput;
994 }
995
996 /* First try to allocate memory, before doing anything with
997 * existing queues. */
998 msg_ptr = load_msg(u_msg_ptr, msg_len);
999 if (IS_ERR(msg_ptr)) {
1000 ret = PTR_ERR(msg_ptr);
1001 goto out_fput;
1002 }
1003 msg_ptr->m_ts = msg_len;
1004 msg_ptr->m_type = msg_prio;
1005
1006 spin_lock(&info->lock);
1007
1008 if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
1009 if (filp->f_flags & O_NONBLOCK) {
1010 spin_unlock(&info->lock);
1011 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 } else {
1013 wait.task = current;
1014 wait.msg = (void *) msg_ptr;
1015 wait.state = STATE_NONE;
1016 ret = wq_sleep(info, SEND, timeout, &wait);
1017 }
1018 if (ret < 0)
1019 free_msg(msg_ptr);
1020 } else {
1021 receiver = wq_get_first_waiter(info, RECV);
1022 if (receiver) {
1023 pipelined_send(info, msg_ptr, receiver);
1024 } else {
1025 /* adds message to the queue */
Doug Ledfordd6629852012-05-31 16:26:35 -07001026 if (msg_insert(msg_ptr, info)) {
1027 free_msg(msg_ptr);
1028 ret = -ENOMEM;
1029 spin_unlock(&info->lock);
1030 goto out_fput;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 __do_notify(info);
1033 }
1034 inode->i_atime = inode->i_mtime = inode->i_ctime =
1035 CURRENT_TIME;
1036 spin_unlock(&info->lock);
1037 ret = 0;
1038 }
1039out_fput:
1040 fput(filp);
1041out:
1042 return ret;
1043}
1044
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001045SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
1046 size_t, msg_len, unsigned int __user *, u_msg_prio,
1047 const struct timespec __user *, u_abs_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ssize_t ret;
1050 struct msg_msg *msg_ptr;
1051 struct file *filp;
1052 struct inode *inode;
1053 struct mqueue_inode_info *info;
1054 struct ext_wait_queue wait;
Carsten Emde9ca7d8e2010-04-02 22:40:20 +02001055 ktime_t expires, *timeout = NULL;
1056 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Al Viroc32c8af2008-12-14 03:46:48 -05001058 if (u_abs_timeout) {
Carsten Emde9ca7d8e2010-04-02 22:40:20 +02001059 int res = prepare_timeout(u_abs_timeout, &expires, &ts);
1060 if (res)
1061 return res;
1062 timeout = &expires;
Al Viroc32c8af2008-12-14 03:46:48 -05001063 }
George C. Wilson20ca73b2006-05-24 16:09:55 -05001064
Carsten Emde9ca7d8e2010-04-02 22:40:20 +02001065 audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001068 if (unlikely(!filp)) {
1069 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Josef Sipek6d630792006-12-08 02:37:11 -08001073 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001074 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1075 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 info = MQUEUE_I(inode);
Al Viro5a190ae2007-06-07 12:19:32 -04001079 audit_inode(NULL, filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001081 if (unlikely(!(filp->f_mode & FMODE_READ))) {
1082 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 /* checks if buffer is big enough */
1087 if (unlikely(msg_len < info->attr.mq_msgsize)) {
1088 ret = -EMSGSIZE;
1089 goto out_fput;
1090 }
1091
1092 spin_lock(&info->lock);
1093 if (info->attr.mq_curmsgs == 0) {
1094 if (filp->f_flags & O_NONBLOCK) {
1095 spin_unlock(&info->lock);
1096 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 } else {
1098 wait.task = current;
1099 wait.state = STATE_NONE;
1100 ret = wq_sleep(info, RECV, timeout, &wait);
1101 msg_ptr = wait.msg;
1102 }
1103 } else {
1104 msg_ptr = msg_get(info);
1105
1106 inode->i_atime = inode->i_mtime = inode->i_ctime =
1107 CURRENT_TIME;
1108
1109 /* There is now free space in queue. */
1110 pipelined_receive(info);
1111 spin_unlock(&info->lock);
1112 ret = 0;
1113 }
1114 if (ret == 0) {
1115 ret = msg_ptr->m_ts;
1116
1117 if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
1118 store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
1119 ret = -EFAULT;
1120 }
1121 free_msg(msg_ptr);
1122 }
1123out_fput:
1124 fput(filp);
1125out:
1126 return ret;
1127}
1128
1129/*
1130 * Notes: the case when user wants us to deregister (with NULL as pointer)
1131 * and he isn't currently owner of notification, will be silently discarded.
1132 * It isn't explicitly defined in the POSIX.
1133 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001134SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
1135 const struct sigevent __user *, u_notification)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 int ret;
1138 struct file *filp;
1139 struct sock *sock;
1140 struct inode *inode;
1141 struct sigevent notification;
1142 struct mqueue_inode_info *info;
1143 struct sk_buff *nc;
1144
Al Viro20114f72008-12-10 07:16:12 -05001145 if (u_notification) {
1146 if (copy_from_user(&notification, u_notification,
1147 sizeof(struct sigevent)))
1148 return -EFAULT;
1149 }
1150
1151 audit_mq_notify(mqdes, u_notification ? &notification : NULL);
George C. Wilson20ca73b2006-05-24 16:09:55 -05001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 nc = NULL;
1154 sock = NULL;
1155 if (u_notification != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (unlikely(notification.sigev_notify != SIGEV_NONE &&
1157 notification.sigev_notify != SIGEV_SIGNAL &&
1158 notification.sigev_notify != SIGEV_THREAD))
1159 return -EINVAL;
1160 if (notification.sigev_notify == SIGEV_SIGNAL &&
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001161 !valid_signal(notification.sigev_signo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -EINVAL;
1163 }
1164 if (notification.sigev_notify == SIGEV_THREAD) {
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001165 long timeo;
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /* create the notify skb */
1168 nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001169 if (!nc) {
1170 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (copy_from_user(nc->data,
1174 notification.sigev_value.sival_ptr,
1175 NOTIFY_COOKIE_LEN)) {
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001176 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto out;
1178 }
1179
1180 /* TODO: add a header? */
1181 skb_put(nc, NOTIFY_COOKIE_LEN);
1182 /* and attach it to the socket */
1183retry:
1184 filp = fget(notification.sigev_signo);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001185 if (!filp) {
1186 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 sock = netlink_getsockbyfilp(filp);
1190 fput(filp);
1191 if (IS_ERR(sock)) {
1192 ret = PTR_ERR(sock);
1193 sock = NULL;
1194 goto out;
1195 }
1196
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001197 timeo = MAX_SCHEDULE_TIMEOUT;
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001198 ret = netlink_attachskb(sock, nc, &timeo, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (ret == 1)
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001200 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (ret) {
1202 sock = NULL;
1203 nc = NULL;
1204 goto out;
1205 }
1206 }
1207 }
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001210 if (!filp) {
1211 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Josef Sipek6d630792006-12-08 02:37:11 -08001215 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001216 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1217 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 info = MQUEUE_I(inode);
1221
1222 ret = 0;
1223 spin_lock(&info->lock);
1224 if (u_notification == NULL) {
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001225 if (info->notify_owner == task_tgid(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 remove_notification(info);
1227 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1228 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001229 } else if (info->notify_owner != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 ret = -EBUSY;
1231 } else {
1232 switch (notification.sigev_notify) {
1233 case SIGEV_NONE:
1234 info->notify.sigev_notify = SIGEV_NONE;
1235 break;
1236 case SIGEV_THREAD:
1237 info->notify_sock = sock;
1238 info->notify_cookie = nc;
1239 sock = NULL;
1240 nc = NULL;
1241 info->notify.sigev_notify = SIGEV_THREAD;
1242 break;
1243 case SIGEV_SIGNAL:
1244 info->notify.sigev_signo = notification.sigev_signo;
1245 info->notify.sigev_value = notification.sigev_value;
1246 info->notify.sigev_notify = SIGEV_SIGNAL;
1247 break;
1248 }
Cedric Le Goatera03fcb72006-10-02 02:17:26 -07001249
1250 info->notify_owner = get_pid(task_tgid(current));
Eric W. Biederman6f9ac6d2011-11-16 22:57:55 -08001251 info->notify_user_ns = get_user_ns(current_user_ns());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1253 }
1254 spin_unlock(&info->lock);
1255out_fput:
1256 fput(filp);
1257out:
1258 if (sock) {
1259 netlink_detachskb(sock, nc);
1260 } else if (nc) {
1261 dev_kfree_skb(nc);
1262 }
1263 return ret;
1264}
1265
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001266SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
1267 const struct mq_attr __user *, u_mqstat,
1268 struct mq_attr __user *, u_omqstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int ret;
1271 struct mq_attr mqstat, omqstat;
1272 struct file *filp;
1273 struct inode *inode;
1274 struct mqueue_inode_info *info;
1275
1276 if (u_mqstat != NULL) {
1277 if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
1278 return -EFAULT;
1279 if (mqstat.mq_flags & (~O_NONBLOCK))
1280 return -EINVAL;
1281 }
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 filp = fget(mqdes);
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001284 if (!filp) {
1285 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 goto out;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Josef Sipek6d630792006-12-08 02:37:11 -08001289 inode = filp->f_path.dentry->d_inode;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001290 if (unlikely(filp->f_op != &mqueue_file_operations)) {
1291 ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto out_fput;
André Goddard Rosa8d8ffef2010-02-23 04:04:26 -03001293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 info = MQUEUE_I(inode);
1295
1296 spin_lock(&info->lock);
1297
1298 omqstat = info->attr;
1299 omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
1300 if (u_mqstat) {
Al Viro73929062008-12-10 06:58:59 -05001301 audit_mq_getsetattr(mqdes, &mqstat);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001302 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (mqstat.mq_flags & O_NONBLOCK)
1304 filp->f_flags |= O_NONBLOCK;
1305 else
1306 filp->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001307 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 inode->i_atime = inode->i_ctime = CURRENT_TIME;
1310 }
1311
1312 spin_unlock(&info->lock);
1313
1314 ret = 0;
1315 if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
1316 sizeof(struct mq_attr)))
1317 ret = -EFAULT;
1318
1319out_fput:
1320 fput(filp);
1321out:
1322 return ret;
1323}
1324
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001325static const struct inode_operations mqueue_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 .lookup = simple_lookup,
1327 .create = mqueue_create,
1328 .unlink = mqueue_unlink,
1329};
1330
Arjan van de Ven9a321442007-02-12 00:55:35 -08001331static const struct file_operations mqueue_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 .flush = mqueue_flush_file,
1333 .poll = mqueue_poll_file,
1334 .read = mqueue_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001335 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336};
1337
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001338static const struct super_operations mqueue_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 .alloc_inode = mqueue_alloc_inode,
1340 .destroy_inode = mqueue_destroy_inode,
Al Viro6d8af642010-06-05 16:29:45 -04001341 .evict_inode = mqueue_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343};
1344
1345static struct file_system_type mqueue_fs_type = {
1346 .name = "mqueue",
Al Viroceefda62010-07-26 13:16:50 +04001347 .mount = mqueue_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 .kill_sb = kill_litter_super,
1349};
1350
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001351int mq_init_ns(struct ipc_namespace *ns)
1352{
1353 ns->mq_queues_count = 0;
1354 ns->mq_queues_max = DFLT_QUEUESMAX;
1355 ns->mq_msg_max = DFLT_MSGMAX;
1356 ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
KOSAKI Motohirocef01842012-05-31 16:26:33 -07001357 ns->mq_msg_default = DFLT_MSG;
1358 ns->mq_msgsize_default = DFLT_MSGSIZE;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001359
1360 ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
1361 if (IS_ERR(ns->mq_mnt)) {
1362 int err = PTR_ERR(ns->mq_mnt);
1363 ns->mq_mnt = NULL;
1364 return err;
1365 }
1366 return 0;
1367}
1368
1369void mq_clear_sbinfo(struct ipc_namespace *ns)
1370{
1371 ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1372}
1373
1374void mq_put_mnt(struct ipc_namespace *ns)
1375{
Al Viro6f686572011-12-09 00:38:50 -05001376 kern_unmount(ns->mq_mnt);
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379static int __init init_mqueue_fs(void)
1380{
1381 int error;
1382
1383 mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
1384 sizeof(struct mqueue_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001385 SLAB_HWCACHE_ALIGN, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (mqueue_inode_cachep == NULL)
1387 return -ENOMEM;
1388
André Goddard Rosa2329e392010-02-23 04:04:27 -03001389 /* ignore failures - they are not fatal */
Serge E. Hallynbdc8e5f2009-04-06 19:01:11 -07001390 mq_sysctl_table = mq_register_sysctl_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 error = register_filesystem(&mqueue_fs_type);
1393 if (error)
1394 goto out_sysctl;
1395
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -07001396 spin_lock_init(&mq_lock);
1397
Al Viro6f686572011-12-09 00:38:50 -05001398 error = mq_init_ns(&init_ipc_ns);
1399 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto out_filesystem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return 0;
1403
1404out_filesystem:
1405 unregister_filesystem(&mqueue_fs_type);
1406out_sysctl:
1407 if (mq_sysctl_table)
1408 unregister_sysctl_table(mq_sysctl_table);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001409 kmem_cache_destroy(mqueue_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return error;
1411}
1412
1413__initcall(init_mqueue_fs);