Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * POSIX message queues filesystem for Linux. |
| 3 | * |
| 4 | * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl) |
Michal Wronski | f66e928 | 2006-10-03 23:23:27 +0200 | [diff] [blame] | 5 | * Michal Wronski (michal.wronski@gmail.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com) |
| 8 | * Lockless receive & send, fd based notify: |
| 9 | * Manfred Spraul (manfred@colorfullife.com) |
| 10 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 11 | * Audit: George Wilson (ltcgcw@us.ibm.com) |
| 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * This file is released under the GPL. |
| 14 | */ |
| 15 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/file.h> |
| 20 | #include <linux/mount.h> |
| 21 | #include <linux/namei.h> |
| 22 | #include <linux/sysctl.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/mqueue.h> |
| 25 | #include <linux/msg.h> |
| 26 | #include <linux/skbuff.h> |
Doug Ledford | 5b5c4d1 | 2012-05-31 16:26:30 -0700 | [diff] [blame] | 27 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/netlink.h> |
| 29 | #include <linux/syscalls.h> |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 30 | #include <linux/audit.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 31 | #include <linux/signal.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 33 | #include <linux/nsproxy.h> |
| 34 | #include <linux/pid.h> |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 35 | #include <linux/ipc_namespace.h> |
Serge E. Hallyn | 6b550f9 | 2012-01-10 15:11:37 -0800 | [diff] [blame] | 36 | #include <linux/user_namespace.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <net/sock.h> |
| 40 | #include "util.h" |
| 41 | |
| 42 | #define MQUEUE_MAGIC 0x19800202 |
| 43 | #define DIRENT_SIZE 20 |
| 44 | #define FILENT_SIZE 80 |
| 45 | |
| 46 | #define SEND 0 |
| 47 | #define RECV 1 |
| 48 | |
| 49 | #define STATE_NONE 0 |
| 50 | #define STATE_PENDING 1 |
| 51 | #define STATE_READY 2 |
| 52 | |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 53 | struct posix_msg_tree_node { |
| 54 | struct rb_node rb_node; |
| 55 | struct list_head msg_list; |
| 56 | int priority; |
| 57 | }; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct 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 | |
| 66 | struct mqueue_inode_info { |
| 67 | spinlock_t lock; |
| 68 | struct inode vfs_inode; |
| 69 | wait_queue_head_t wait_q; |
| 70 | |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 71 | struct rb_root msg_tree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct mq_attr attr; |
| 73 | |
| 74 | struct sigevent notify; |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 75 | struct pid* notify_owner; |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 76 | struct user_namespace *notify_user_ns; |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 77 | struct user_struct *user; /* user who created, for accounting */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | struct sock *notify_sock; |
| 79 | struct sk_buff *notify_cookie; |
| 80 | |
| 81 | /* for tasks waiting for free space and messages, respectively */ |
| 82 | struct ext_wait_queue e_wait_q[2]; |
| 83 | |
| 84 | unsigned long qsize; /* size of queue in memory (sum of all msgs) */ |
| 85 | }; |
| 86 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 87 | static const struct inode_operations mqueue_dir_inode_operations; |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 88 | static const struct file_operations mqueue_file_operations; |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 89 | static const struct super_operations mqueue_super_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static void remove_notification(struct mqueue_inode_info *info); |
| 91 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 92 | static struct kmem_cache *mqueue_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | static struct ctl_table_header * mq_sysctl_table; |
| 95 | |
| 96 | static 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. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 101 | /* |
| 102 | * This routine should be called with the mq_lock held. |
| 103 | */ |
| 104 | static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode) |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 105 | { |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 106 | return get_ipc_ns(inode->i_sb->s_fs_info); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 109 | static struct ipc_namespace *get_ns_from_inode(struct inode *inode) |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 110 | { |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 111 | struct ipc_namespace *ns; |
| 112 | |
| 113 | spin_lock(&mq_lock); |
| 114 | ns = __get_ns_from_inode(inode); |
| 115 | spin_unlock(&mq_lock); |
| 116 | return ns; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 119 | /* Auxiliary functions to manipulate messages' list */ |
| 120 | static 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); |
| 146 | insert_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 | |
| 153 | static 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 | |
| 159 | try_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. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 203 | static struct inode *mqueue_get_inode(struct super_block *sb, |
Al Viro | 1b9d5ff7 | 2011-07-24 14:18:20 -0400 | [diff] [blame] | 204 | struct ipc_namespace *ipc_ns, umode_t mode, |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 205 | struct mq_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 207 | struct user_struct *u = current_user(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | struct inode *inode; |
Jiri Slaby | d40dcdb | 2011-07-26 16:08:47 -0700 | [diff] [blame] | 209 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | inode = new_inode(sb); |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 212 | if (!inode) |
| 213 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 215 | inode->i_ino = get_next_ino(); |
| 216 | inode->i_mode = mode; |
| 217 | inode->i_uid = current_fsuid(); |
| 218 | inode->i_gid = current_fsgid(); |
| 219 | inode->i_mtime = inode->i_ctime = inode->i_atime = CURRENT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 221 | if (S_ISREG(mode)) { |
| 222 | struct mqueue_inode_info *info; |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 223 | unsigned long mq_bytes, mq_treesize; |
André Goddard Rosa | c8308b1 | 2010-02-23 04:04:23 -0300 | [diff] [blame] | 224 | |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 225 | inode->i_fop = &mqueue_file_operations; |
| 226 | inode->i_size = FILENT_SIZE; |
| 227 | /* mqueue specific info */ |
| 228 | info = MQUEUE_I(inode); |
| 229 | spin_lock_init(&info->lock); |
| 230 | init_waitqueue_head(&info->wait_q); |
| 231 | INIT_LIST_HEAD(&info->e_wait_q[0].list); |
| 232 | INIT_LIST_HEAD(&info->e_wait_q[1].list); |
| 233 | info->notify_owner = NULL; |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 234 | info->notify_user_ns = NULL; |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 235 | info->qsize = 0; |
| 236 | info->user = NULL; /* set when all is ok */ |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 237 | info->msg_tree = RB_ROOT; |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 238 | memset(&info->attr, 0, sizeof(info->attr)); |
KOSAKI Motohiro | cef0184 | 2012-05-31 16:26:33 -0700 | [diff] [blame] | 239 | info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max, |
| 240 | ipc_ns->mq_msg_default); |
| 241 | info->attr.mq_msgsize = min(ipc_ns->mq_msgsize_max, |
| 242 | ipc_ns->mq_msgsize_default); |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 243 | if (attr) { |
| 244 | info->attr.mq_maxmsg = attr->mq_maxmsg; |
| 245 | info->attr.mq_msgsize = attr->mq_msgsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 247 | /* |
| 248 | * We used to allocate a static array of pointers and account |
| 249 | * the size of that array as well as one msg_msg struct per |
| 250 | * possible message into the queue size. That's no longer |
| 251 | * accurate as the queue is now an rbtree and will grow and |
| 252 | * shrink depending on usage patterns. We can, however, still |
| 253 | * account one msg_msg struct per message, but the nodes are |
| 254 | * allocated depending on priority usage, and most programs |
| 255 | * only use one, or a handful, of priorities. However, since |
| 256 | * this is pinned memory, we need to assume worst case, so |
| 257 | * that means the min(mq_maxmsg, max_priorities) * struct |
| 258 | * posix_msg_tree_node. |
| 259 | */ |
| 260 | mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) + |
| 261 | min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) * |
| 262 | sizeof(struct posix_msg_tree_node); |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 263 | |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 264 | mq_bytes = mq_treesize + (info->attr.mq_maxmsg * |
| 265 | info->attr.mq_msgsize); |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 266 | |
| 267 | spin_lock(&mq_lock); |
| 268 | if (u->mq_bytes + mq_bytes < u->mq_bytes || |
Davidlohr Bueso | 2a4e64b | 2012-01-20 14:34:01 -0800 | [diff] [blame] | 269 | u->mq_bytes + mq_bytes > rlimit(RLIMIT_MSGQUEUE)) { |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 270 | spin_unlock(&mq_lock); |
| 271 | /* mqueue_evict_inode() releases info->messages */ |
Jiri Slaby | d40dcdb | 2011-07-26 16:08:47 -0700 | [diff] [blame] | 272 | ret = -EMFILE; |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 273 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return inode; |
| 289 | out_inode: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | iput(inode); |
Jiri Slaby | 0471520 | 2011-07-26 16:08:46 -0700 | [diff] [blame] | 291 | err: |
Jiri Slaby | d40dcdb | 2011-07-26 16:08:47 -0700 | [diff] [blame] | 292 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int mqueue_fill_super(struct super_block *sb, void *data, int silent) |
| 296 | { |
| 297 | struct inode *inode; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 298 | struct ipc_namespace *ns = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 301 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 302 | sb->s_magic = MQUEUE_MAGIC; |
| 303 | sb->s_op = &mqueue_super_ops; |
| 304 | |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 305 | inode = mqueue_get_inode(sb, ns, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL); |
| 306 | if (IS_ERR(inode)) |
| 307 | return PTR_ERR(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 309 | sb->s_root = d_make_root(inode); |
| 310 | if (!sb->s_root) |
| 311 | return -ENOMEM; |
| 312 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 315 | static struct dentry *mqueue_mount(struct file_system_type *fs_type, |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 316 | int flags, const char *dev_name, |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 317 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 319 | if (!(flags & MS_KERNMOUNT)) |
| 320 | data = current->nsproxy->ipc_ns; |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 321 | return mount_ns(fs_type, flags, data, mqueue_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 324 | static void init_once(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo; |
| 327 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 328 | inode_init_once(&p->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static struct inode *mqueue_alloc_inode(struct super_block *sb) |
| 332 | { |
| 333 | struct mqueue_inode_info *ei; |
| 334 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 335 | ei = kmem_cache_alloc(mqueue_inode_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (!ei) |
| 337 | return NULL; |
| 338 | return &ei->vfs_inode; |
| 339 | } |
| 340 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 341 | static void mqueue_i_callback(struct rcu_head *head) |
| 342 | { |
| 343 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 344 | kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode)); |
| 345 | } |
| 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | static void mqueue_destroy_inode(struct inode *inode) |
| 348 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 349 | call_rcu(&inode->i_rcu, mqueue_i_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Al Viro | 6d8af64 | 2010-06-05 16:29:45 -0400 | [diff] [blame] | 352 | static void mqueue_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct mqueue_inode_info *info; |
| 355 | struct user_struct *user; |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 356 | unsigned long mq_bytes, mq_treesize; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 357 | struct ipc_namespace *ipc_ns; |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 358 | struct msg_msg *msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 360 | clear_inode(inode); |
Al Viro | 6d8af64 | 2010-06-05 16:29:45 -0400 | [diff] [blame] | 361 | |
| 362 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return; |
Al Viro | 6d8af64 | 2010-06-05 16:29:45 -0400 | [diff] [blame] | 364 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 365 | ipc_ns = get_ns_from_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | info = MQUEUE_I(inode); |
| 367 | spin_lock(&info->lock); |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 368 | while ((msg = msg_get(info)) != NULL) |
| 369 | free_msg(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | spin_unlock(&info->lock); |
| 371 | |
André Goddard Rosa | 8834cf7 | 2010-02-23 04:04:24 -0300 | [diff] [blame] | 372 | /* Total amount of bytes accounted for the mqueue */ |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 373 | mq_treesize = info->attr.mq_maxmsg * sizeof(struct msg_msg) + |
| 374 | min_t(unsigned int, info->attr.mq_maxmsg, MQ_PRIO_MAX) * |
| 375 | sizeof(struct posix_msg_tree_node); |
| 376 | |
| 377 | mq_bytes = mq_treesize + (info->attr.mq_maxmsg * |
| 378 | info->attr.mq_msgsize); |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | user = info->user; |
| 381 | if (user) { |
| 382 | spin_lock(&mq_lock); |
| 383 | user->mq_bytes -= mq_bytes; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 384 | /* |
| 385 | * get_ns_from_inode() ensures that the |
| 386 | * (ipc_ns = sb->s_fs_info) is either a valid ipc_ns |
| 387 | * to which we now hold a reference, or it is NULL. |
| 388 | * We can't put it here under mq_lock, though. |
| 389 | */ |
| 390 | if (ipc_ns) |
| 391 | ipc_ns->mq_queues_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | spin_unlock(&mq_lock); |
| 393 | free_uid(user); |
| 394 | } |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 395 | if (ipc_ns) |
| 396 | put_ipc_ns(ipc_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static int mqueue_create(struct inode *dir, struct dentry *dentry, |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 400 | umode_t mode, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
| 402 | struct inode *inode; |
| 403 | struct mq_attr *attr = dentry->d_fsdata; |
| 404 | int error; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 405 | struct ipc_namespace *ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | spin_lock(&mq_lock); |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 408 | ipc_ns = __get_ns_from_inode(dir); |
| 409 | if (!ipc_ns) { |
| 410 | error = -EACCES; |
| 411 | goto out_unlock; |
| 412 | } |
Doug Ledford | 02967ea | 2012-05-31 16:26:29 -0700 | [diff] [blame] | 413 | if (ipc_ns->mq_queues_count >= HARD_QUEUESMAX || |
| 414 | (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max && |
| 415 | !capable(CAP_SYS_RESOURCE))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | error = -ENOSPC; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 417 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 419 | ipc_ns->mq_queues_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | spin_unlock(&mq_lock); |
| 421 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 422 | inode = mqueue_get_inode(dir->i_sb, ipc_ns, mode, attr); |
Jiri Slaby | d40dcdb | 2011-07-26 16:08:47 -0700 | [diff] [blame] | 423 | if (IS_ERR(inode)) { |
| 424 | error = PTR_ERR(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | spin_lock(&mq_lock); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 426 | ipc_ns->mq_queues_count--; |
| 427 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 430 | put_ipc_ns(ipc_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | dir->i_size += DIRENT_SIZE; |
| 432 | dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME; |
| 433 | |
| 434 | d_instantiate(dentry, inode); |
| 435 | dget(dentry); |
| 436 | return 0; |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 437 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | spin_unlock(&mq_lock); |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 439 | if (ipc_ns) |
| 440 | put_ipc_ns(ipc_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return error; |
| 442 | } |
| 443 | |
| 444 | static 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 Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 450 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | dput(dentry); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * This is routine for system read from queue file. |
| 457 | * To avoid mess with doing here some sort of mq_receive we allow |
| 458 | * to read only queue size & notification info (the only values |
| 459 | * that are interesting from user point of view and aren't accessible |
| 460 | * through std routines) |
| 461 | */ |
| 462 | static ssize_t mqueue_read_file(struct file *filp, char __user *u_data, |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 463 | size_t count, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 465 | struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | char buffer[FILENT_SIZE]; |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 467 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | spin_lock(&info->lock); |
| 470 | snprintf(buffer, sizeof(buffer), |
| 471 | "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n", |
| 472 | info->qsize, |
| 473 | info->notify_owner ? info->notify.sigev_notify : 0, |
| 474 | (info->notify_owner && |
| 475 | info->notify.sigev_notify == SIGEV_SIGNAL) ? |
| 476 | info->notify.sigev_signo : 0, |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 477 | pid_vnr(info->notify_owner)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | spin_unlock(&info->lock); |
| 479 | buffer[sizeof(buffer)-1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 481 | ret = simple_read_from_buffer(u_data, count, off, buffer, |
| 482 | strlen(buffer)); |
| 483 | if (ret <= 0) |
| 484 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 486 | filp->f_path.dentry->d_inode->i_atime = filp->f_path.dentry->d_inode->i_ctime = CURRENT_TIME; |
Akinobu Mita | f1a43f9 | 2008-07-25 01:48:07 -0700 | [diff] [blame] | 487 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 490 | static int mqueue_flush_file(struct file *filp, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 492 | struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | spin_lock(&info->lock); |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 495 | if (task_tgid(current) == info->notify_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | remove_notification(info); |
| 497 | |
| 498 | spin_unlock(&info->lock); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab) |
| 503 | { |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 504 | struct mqueue_inode_info *info = MQUEUE_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | int retval = 0; |
| 506 | |
| 507 | poll_wait(filp, &info->wait_q, poll_tab); |
| 508 | |
| 509 | spin_lock(&info->lock); |
| 510 | if (info->attr.mq_curmsgs) |
| 511 | retval = POLLIN | POLLRDNORM; |
| 512 | |
| 513 | if (info->attr.mq_curmsgs < info->attr.mq_maxmsg) |
| 514 | retval |= POLLOUT | POLLWRNORM; |
| 515 | spin_unlock(&info->lock); |
| 516 | |
| 517 | return retval; |
| 518 | } |
| 519 | |
| 520 | /* Adds current to info->e_wait_q[sr] before element with smaller prio */ |
| 521 | static 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 | */ |
| 542 | static int wq_sleep(struct mqueue_inode_info *info, int sr, |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 543 | ktime_t *timeout, struct ext_wait_queue *ewp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
| 545 | int retval; |
| 546 | signed long time; |
| 547 | |
| 548 | wq_add(info, sr, ewp); |
| 549 | |
| 550 | for (;;) { |
| 551 | set_current_state(TASK_INTERRUPTIBLE); |
| 552 | |
| 553 | spin_unlock(&info->lock); |
Wanlong Gao | 32ea845 | 2011-10-31 17:06:35 -0700 | [diff] [blame] | 554 | time = schedule_hrtimeout_range_clock(timeout, 0, |
| 555 | HRTIMER_MODE_ABS, CLOCK_REALTIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | while (ewp->state == STATE_PENDING) |
| 558 | cpu_relax(); |
| 559 | |
| 560 | if (ewp->state == STATE_READY) { |
| 561 | retval = 0; |
| 562 | goto out; |
| 563 | } |
| 564 | spin_lock(&info->lock); |
| 565 | if (ewp->state == STATE_READY) { |
| 566 | retval = 0; |
| 567 | goto out_unlock; |
| 568 | } |
| 569 | if (signal_pending(current)) { |
| 570 | retval = -ERESTARTSYS; |
| 571 | break; |
| 572 | } |
| 573 | if (time == 0) { |
| 574 | retval = -ETIMEDOUT; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | list_del(&ewp->list); |
| 579 | out_unlock: |
| 580 | spin_unlock(&info->lock); |
| 581 | out: |
| 582 | return retval; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Returns waiting task that should be serviced first or NULL if none exists |
| 587 | */ |
| 588 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | static 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 | */ |
| 608 | static 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. Hallyn | 6b550f9 | 2012-01-10 15:11:37 -0800 | [diff] [blame] | 628 | /* map current pid/uid into info->owner's namespaces */ |
| 629 | rcu_read_lock(); |
Sukadev Bhattiprolu | a668499 | 2009-01-07 18:08:50 -0800 | [diff] [blame] | 630 | sig_i.si_pid = task_tgid_nr_ns(current, |
| 631 | ns_of_pid(info->notify_owner)); |
Eric W. Biederman | 76b6db0 | 2012-03-14 15:24:19 -0700 | [diff] [blame] | 632 | sig_i.si_uid = from_kuid_munged(info->notify_user_ns, current_uid()); |
Serge E. Hallyn | 6b550f9 | 2012-01-10 15:11:37 -0800 | [diff] [blame] | 633 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 635 | kill_pid_info(info->notify.sigev_signo, |
| 636 | &sig_i, info->notify_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | break; |
| 638 | case SIGEV_THREAD: |
| 639 | set_cookie(info->notify_cookie, NOTIFY_WOKENUP); |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 640 | netlink_sendskb(info->notify_sock, info->notify_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | break; |
| 642 | } |
| 643 | /* after notification unregisters process */ |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 644 | put_pid(info->notify_owner); |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 645 | put_user_ns(info->notify_user_ns); |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 646 | info->notify_owner = NULL; |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 647 | info->notify_user_ns = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | wake_up(&info->wait_q); |
| 650 | } |
| 651 | |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 652 | static int prepare_timeout(const struct timespec __user *u_abs_timeout, |
| 653 | ktime_t *expires, struct timespec *ts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 655 | if (copy_from_user(ts, u_abs_timeout, sizeof(struct timespec))) |
| 656 | return -EFAULT; |
| 657 | if (!timespec_valid(ts)) |
| 658 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 660 | *expires = timespec_to_ktime(*ts); |
| 661 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static void remove_notification(struct mqueue_inode_info *info) |
| 665 | { |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 666 | if (info->notify_owner != NULL && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | info->notify.sigev_notify == SIGEV_THREAD) { |
| 668 | set_cookie(info->notify_cookie, NOTIFY_REMOVED); |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 669 | netlink_sendskb(info->notify_sock, info->notify_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 671 | put_pid(info->notify_owner); |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 672 | put_user_ns(info->notify_user_ns); |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 673 | info->notify_owner = NULL; |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 674 | info->notify_user_ns = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 677 | static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
| 679 | if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0) |
| 680 | return 0; |
| 681 | if (capable(CAP_SYS_RESOURCE)) { |
Doug Ledford | 02967ea | 2012-05-31 16:26:29 -0700 | [diff] [blame] | 682 | if (attr->mq_maxmsg > HARD_MSGMAX || |
| 683 | attr->mq_msgsize > HARD_MSGSIZEMAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return 0; |
| 685 | } else { |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 686 | if (attr->mq_maxmsg > ipc_ns->mq_msg_max || |
| 687 | attr->mq_msgsize > ipc_ns->mq_msgsize_max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | /* check for overflow */ |
| 691 | if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg) |
| 692 | return 0; |
André Goddard Rosa | 8834cf7 | 2010-02-23 04:04:24 -0300 | [diff] [blame] | 693 | if ((unsigned long)(attr->mq_maxmsg * (attr->mq_msgsize |
| 694 | + sizeof (struct msg_msg *))) < |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize)) |
| 696 | return 0; |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Invoked when creating a new queue via sys_mq_open |
| 702 | */ |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 703 | static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir, |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 704 | struct dentry *dentry, int oflag, umode_t mode, |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 705 | struct mq_attr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 707 | const struct cred *cred = current_cred(); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 708 | struct file *result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | int ret; |
| 710 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 711 | if (attr) { |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 712 | if (!mq_attr_ok(ipc_ns, attr)) { |
| 713 | ret = -EINVAL; |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 714 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | /* store for use during create */ |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 717 | dentry->d_fsdata = attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 720 | mode &= ~current_umask(); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 721 | ret = mnt_want_write(ipc_ns->mq_mnt); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 722 | if (ret) |
| 723 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | ret = vfs_create(dir->d_inode, dentry, mode, NULL); |
| 725 | dentry->d_fsdata = NULL; |
| 726 | if (ret) |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 727 | goto out_drop_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 729 | result = dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 730 | /* |
| 731 | * dentry_open() took a persistent mnt_want_write(), |
| 732 | * so we can now drop this one. |
| 733 | */ |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 734 | mnt_drop_write(ipc_ns->mq_mnt); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 735 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 737 | out_drop_write: |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 738 | mnt_drop_write(ipc_ns->mq_mnt); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 739 | out: |
| 740 | dput(dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 741 | mntput(ipc_ns->mq_mnt); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 742 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* Opens existing queue */ |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 746 | static struct file *do_open(struct ipc_namespace *ipc_ns, |
| 747 | struct dentry *dentry, int oflag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
André Goddard Rosa | 04db0dd | 2010-02-23 04:04:25 -0300 | [diff] [blame] | 749 | int ret; |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 750 | const struct cred *cred = current_cred(); |
| 751 | |
| 752 | static const int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE, |
| 753 | MAY_READ | MAY_WRITE }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 755 | if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY)) { |
André Goddard Rosa | 04db0dd | 2010-02-23 04:04:25 -0300 | [diff] [blame] | 756 | ret = -EINVAL; |
| 757 | goto err; |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 760 | if (inode_permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE])) { |
André Goddard Rosa | 04db0dd | 2010-02-23 04:04:25 -0300 | [diff] [blame] | 761 | ret = -EACCES; |
| 762 | goto err; |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 765 | return dentry_open(dentry, ipc_ns->mq_mnt, oflag, cred); |
André Goddard Rosa | 04db0dd | 2010-02-23 04:04:25 -0300 | [diff] [blame] | 766 | |
| 767 | err: |
| 768 | dput(dentry); |
| 769 | mntput(ipc_ns->mq_mnt); |
| 770 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Al Viro | df0a428 | 2011-07-26 05:26:10 -0400 | [diff] [blame] | 773 | SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode, |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 774 | struct mq_attr __user *, u_attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
| 776 | struct dentry *dentry; |
| 777 | struct file *filp; |
| 778 | char *name; |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 779 | struct mq_attr attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | int fd, error; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 781 | struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 783 | if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr))) |
| 784 | return -EFAULT; |
| 785 | |
| 786 | audit_mq_open(oflag, mode, u_attr ? &attr : NULL); |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (IS_ERR(name = getname(u_name))) |
| 789 | return PTR_ERR(name); |
| 790 | |
Ulrich Drepper | 269f213 | 2008-05-03 15:28:45 -0400 | [diff] [blame] | 791 | fd = get_unused_fd_flags(O_CLOEXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (fd < 0) |
| 793 | goto out_putname; |
| 794 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 795 | mutex_lock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex); |
| 796 | dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | if (IS_ERR(dentry)) { |
| 798 | error = PTR_ERR(dentry); |
André Goddard Rosa | 4294a8e | 2010-02-23 04:04:28 -0300 | [diff] [blame] | 799 | goto out_putfd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 801 | mntget(ipc_ns->mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | if (oflag & O_CREAT) { |
| 804 | if (dentry->d_inode) { /* entry already exists */ |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 805 | audit_inode(name, dentry); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 806 | if (oflag & O_EXCL) { |
| 807 | error = -EEXIST; |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 808 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 809 | } |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 810 | filp = do_open(ipc_ns, dentry, oflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } else { |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 812 | filp = do_create(ipc_ns, ipc_ns->mq_mnt->mnt_root, |
| 813 | dentry, oflag, mode, |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 814 | u_attr ? &attr : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 816 | } else { |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 817 | if (!dentry->d_inode) { |
| 818 | error = -ENOENT; |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 819 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 820 | } |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 821 | audit_inode(name, dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 822 | filp = do_open(ipc_ns, dentry, oflag); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | if (IS_ERR(filp)) { |
| 826 | error = PTR_ERR(filp); |
| 827 | goto out_putfd; |
| 828 | } |
| 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | fd_install(fd, filp); |
| 831 | goto out_upsem; |
| 832 | |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 833 | out: |
| 834 | dput(dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 835 | mntput(ipc_ns->mq_mnt); |
Alexander Viro | 7c7dce9 | 2006-01-14 15:29:55 -0500 | [diff] [blame] | 836 | out_putfd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | put_unused_fd(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | fd = error; |
| 839 | out_upsem: |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 840 | mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | out_putname: |
| 842 | putname(name); |
| 843 | return fd; |
| 844 | } |
| 845 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 846 | SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
| 848 | int err; |
| 849 | char *name; |
| 850 | struct dentry *dentry; |
| 851 | struct inode *inode = NULL; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 852 | struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
| 854 | name = getname(u_name); |
| 855 | if (IS_ERR(name)) |
| 856 | return PTR_ERR(name); |
| 857 | |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 858 | mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex, |
Peter Zijlstra | 7a43481 | 2007-03-06 01:42:09 -0800 | [diff] [blame] | 859 | I_MUTEX_PARENT); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 860 | dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | if (IS_ERR(dentry)) { |
| 862 | err = PTR_ERR(dentry); |
| 863 | goto out_unlock; |
| 864 | } |
| 865 | |
| 866 | if (!dentry->d_inode) { |
| 867 | err = -ENOENT; |
| 868 | goto out_err; |
| 869 | } |
| 870 | |
| 871 | inode = dentry->d_inode; |
| 872 | if (inode) |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 873 | ihold(inode); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 874 | err = mnt_want_write(ipc_ns->mq_mnt); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 875 | if (err) |
| 876 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | err = vfs_unlink(dentry->d_parent->d_inode, dentry); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 878 | mnt_drop_write(ipc_ns->mq_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | out_err: |
| 880 | dput(dentry); |
| 881 | |
| 882 | out_unlock: |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame] | 883 | mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | putname(name); |
| 885 | if (inode) |
| 886 | iput(inode); |
| 887 | |
| 888 | return err; |
| 889 | } |
| 890 | |
| 891 | /* Pipelined send and receive functions. |
| 892 | * |
| 893 | * If a receiver finds no waiting message, then it registers itself in the |
| 894 | * list of waiting receivers. A sender checks that list before adding the new |
| 895 | * message into the message array. If there is a waiting receiver, then it |
| 896 | * bypasses the message array and directly hands the message over to the |
| 897 | * receiver. |
| 898 | * The receiver accepts the message and returns without grabbing the queue |
| 899 | * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers |
| 900 | * are necessary. The same algorithm is used for sysv semaphores, see |
Serge E. Hallyn | 7b7a317 | 2006-03-28 01:56:23 -0800 | [diff] [blame] | 901 | * ipc/sem.c for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | * |
| 903 | * The same algorithm is used for senders. |
| 904 | */ |
| 905 | |
| 906 | /* pipelined_send() - send a message directly to the task waiting in |
| 907 | * sys_mq_timedreceive() (without inserting message into a queue). |
| 908 | */ |
| 909 | static inline void pipelined_send(struct mqueue_inode_info *info, |
| 910 | struct msg_msg *message, |
| 911 | struct ext_wait_queue *receiver) |
| 912 | { |
| 913 | receiver->msg = message; |
| 914 | list_del(&receiver->list); |
| 915 | receiver->state = STATE_PENDING; |
| 916 | wake_up_process(receiver->task); |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 917 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | receiver->state = STATE_READY; |
| 919 | } |
| 920 | |
| 921 | /* pipelined_receive() - if there is task waiting in sys_mq_timedsend() |
| 922 | * gets its message and put to the queue (we have one free place for sure). */ |
| 923 | static inline void pipelined_receive(struct mqueue_inode_info *info) |
| 924 | { |
| 925 | struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND); |
| 926 | |
| 927 | if (!sender) { |
| 928 | /* for poll */ |
| 929 | wake_up_interruptible(&info->wait_q); |
| 930 | return; |
| 931 | } |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 932 | if (msg_insert(sender->msg, info)) |
| 933 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | list_del(&sender->list); |
| 935 | sender->state = STATE_PENDING; |
| 936 | wake_up_process(sender->task); |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 937 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | sender->state = STATE_READY; |
| 939 | } |
| 940 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 941 | SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr, |
| 942 | size_t, msg_len, unsigned int, msg_prio, |
| 943 | const struct timespec __user *, u_abs_timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
| 945 | struct file *filp; |
| 946 | struct inode *inode; |
| 947 | struct ext_wait_queue wait; |
| 948 | struct ext_wait_queue *receiver; |
| 949 | struct msg_msg *msg_ptr; |
| 950 | struct mqueue_inode_info *info; |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 951 | ktime_t expires, *timeout = NULL; |
| 952 | struct timespec ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | int ret; |
| 954 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 955 | if (u_abs_timeout) { |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 956 | int res = prepare_timeout(u_abs_timeout, &expires, &ts); |
| 957 | if (res) |
| 958 | return res; |
| 959 | timeout = &expires; |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 960 | } |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX)) |
| 963 | return -EINVAL; |
| 964 | |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 965 | audit_mq_sendrecv(mqdes, msg_len, msg_prio, timeout ? &ts : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | filp = fget(mqdes); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 968 | if (unlikely(!filp)) { |
| 969 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 973 | inode = filp->f_path.dentry->d_inode; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 974 | if (unlikely(filp->f_op != &mqueue_file_operations)) { |
| 975 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | goto out_fput; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | info = MQUEUE_I(inode); |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 979 | audit_inode(NULL, filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 981 | if (unlikely(!(filp->f_mode & FMODE_WRITE))) { |
| 982 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | goto out_fput; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | if (unlikely(msg_len > info->attr.mq_msgsize)) { |
| 987 | ret = -EMSGSIZE; |
| 988 | goto out_fput; |
| 989 | } |
| 990 | |
| 991 | /* First try to allocate memory, before doing anything with |
| 992 | * existing queues. */ |
| 993 | msg_ptr = load_msg(u_msg_ptr, msg_len); |
| 994 | if (IS_ERR(msg_ptr)) { |
| 995 | ret = PTR_ERR(msg_ptr); |
| 996 | goto out_fput; |
| 997 | } |
| 998 | msg_ptr->m_ts = msg_len; |
| 999 | msg_ptr->m_type = msg_prio; |
| 1000 | |
| 1001 | spin_lock(&info->lock); |
| 1002 | |
| 1003 | if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) { |
| 1004 | if (filp->f_flags & O_NONBLOCK) { |
| 1005 | spin_unlock(&info->lock); |
| 1006 | ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | wait.task = current; |
| 1009 | wait.msg = (void *) msg_ptr; |
| 1010 | wait.state = STATE_NONE; |
| 1011 | ret = wq_sleep(info, SEND, timeout, &wait); |
| 1012 | } |
| 1013 | if (ret < 0) |
| 1014 | free_msg(msg_ptr); |
| 1015 | } else { |
| 1016 | receiver = wq_get_first_waiter(info, RECV); |
| 1017 | if (receiver) { |
| 1018 | pipelined_send(info, msg_ptr, receiver); |
| 1019 | } else { |
| 1020 | /* adds message to the queue */ |
Doug Ledford | d662985 | 2012-05-31 16:26:35 -0700 | [diff] [blame^] | 1021 | if (msg_insert(msg_ptr, info)) { |
| 1022 | free_msg(msg_ptr); |
| 1023 | ret = -ENOMEM; |
| 1024 | spin_unlock(&info->lock); |
| 1025 | goto out_fput; |
| 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | __do_notify(info); |
| 1028 | } |
| 1029 | inode->i_atime = inode->i_mtime = inode->i_ctime = |
| 1030 | CURRENT_TIME; |
| 1031 | spin_unlock(&info->lock); |
| 1032 | ret = 0; |
| 1033 | } |
| 1034 | out_fput: |
| 1035 | fput(filp); |
| 1036 | out: |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1040 | SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr, |
| 1041 | size_t, msg_len, unsigned int __user *, u_msg_prio, |
| 1042 | const struct timespec __user *, u_abs_timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | ssize_t ret; |
| 1045 | struct msg_msg *msg_ptr; |
| 1046 | struct file *filp; |
| 1047 | struct inode *inode; |
| 1048 | struct mqueue_inode_info *info; |
| 1049 | struct ext_wait_queue wait; |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 1050 | ktime_t expires, *timeout = NULL; |
| 1051 | struct timespec ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 1053 | if (u_abs_timeout) { |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 1054 | int res = prepare_timeout(u_abs_timeout, &expires, &ts); |
| 1055 | if (res) |
| 1056 | return res; |
| 1057 | timeout = &expires; |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 1058 | } |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1059 | |
Carsten Emde | 9ca7d8e | 2010-04-02 22:40:20 +0200 | [diff] [blame] | 1060 | audit_mq_sendrecv(mqdes, msg_len, 0, timeout ? &ts : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | filp = fget(mqdes); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1063 | if (unlikely(!filp)) { |
| 1064 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1068 | inode = filp->f_path.dentry->d_inode; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1069 | if (unlikely(filp->f_op != &mqueue_file_operations)) { |
| 1070 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | goto out_fput; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1072 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | info = MQUEUE_I(inode); |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 1074 | audit_inode(NULL, filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1076 | if (unlikely(!(filp->f_mode & FMODE_READ))) { |
| 1077 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | goto out_fput; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | /* checks if buffer is big enough */ |
| 1082 | if (unlikely(msg_len < info->attr.mq_msgsize)) { |
| 1083 | ret = -EMSGSIZE; |
| 1084 | goto out_fput; |
| 1085 | } |
| 1086 | |
| 1087 | spin_lock(&info->lock); |
| 1088 | if (info->attr.mq_curmsgs == 0) { |
| 1089 | if (filp->f_flags & O_NONBLOCK) { |
| 1090 | spin_unlock(&info->lock); |
| 1091 | ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } else { |
| 1093 | wait.task = current; |
| 1094 | wait.state = STATE_NONE; |
| 1095 | ret = wq_sleep(info, RECV, timeout, &wait); |
| 1096 | msg_ptr = wait.msg; |
| 1097 | } |
| 1098 | } else { |
| 1099 | msg_ptr = msg_get(info); |
| 1100 | |
| 1101 | inode->i_atime = inode->i_mtime = inode->i_ctime = |
| 1102 | CURRENT_TIME; |
| 1103 | |
| 1104 | /* There is now free space in queue. */ |
| 1105 | pipelined_receive(info); |
| 1106 | spin_unlock(&info->lock); |
| 1107 | ret = 0; |
| 1108 | } |
| 1109 | if (ret == 0) { |
| 1110 | ret = msg_ptr->m_ts; |
| 1111 | |
| 1112 | if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) || |
| 1113 | store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) { |
| 1114 | ret = -EFAULT; |
| 1115 | } |
| 1116 | free_msg(msg_ptr); |
| 1117 | } |
| 1118 | out_fput: |
| 1119 | fput(filp); |
| 1120 | out: |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Notes: the case when user wants us to deregister (with NULL as pointer) |
| 1126 | * and he isn't currently owner of notification, will be silently discarded. |
| 1127 | * It isn't explicitly defined in the POSIX. |
| 1128 | */ |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1129 | SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes, |
| 1130 | const struct sigevent __user *, u_notification) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
| 1132 | int ret; |
| 1133 | struct file *filp; |
| 1134 | struct sock *sock; |
| 1135 | struct inode *inode; |
| 1136 | struct sigevent notification; |
| 1137 | struct mqueue_inode_info *info; |
| 1138 | struct sk_buff *nc; |
| 1139 | |
Al Viro | 20114f7 | 2008-12-10 07:16:12 -0500 | [diff] [blame] | 1140 | if (u_notification) { |
| 1141 | if (copy_from_user(¬ification, u_notification, |
| 1142 | sizeof(struct sigevent))) |
| 1143 | return -EFAULT; |
| 1144 | } |
| 1145 | |
| 1146 | audit_mq_notify(mqdes, u_notification ? ¬ification : NULL); |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | nc = NULL; |
| 1149 | sock = NULL; |
| 1150 | if (u_notification != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | if (unlikely(notification.sigev_notify != SIGEV_NONE && |
| 1152 | notification.sigev_notify != SIGEV_SIGNAL && |
| 1153 | notification.sigev_notify != SIGEV_THREAD)) |
| 1154 | return -EINVAL; |
| 1155 | if (notification.sigev_notify == SIGEV_SIGNAL && |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 1156 | !valid_signal(notification.sigev_signo)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | if (notification.sigev_notify == SIGEV_THREAD) { |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1160 | long timeo; |
| 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | /* create the notify skb */ |
| 1163 | nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1164 | if (!nc) { |
| 1165 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | if (copy_from_user(nc->data, |
| 1169 | notification.sigev_value.sival_ptr, |
| 1170 | NOTIFY_COOKIE_LEN)) { |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1171 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | goto out; |
| 1173 | } |
| 1174 | |
| 1175 | /* TODO: add a header? */ |
| 1176 | skb_put(nc, NOTIFY_COOKIE_LEN); |
| 1177 | /* and attach it to the socket */ |
| 1178 | retry: |
| 1179 | filp = fget(notification.sigev_signo); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1180 | if (!filp) { |
| 1181 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | sock = netlink_getsockbyfilp(filp); |
| 1185 | fput(filp); |
| 1186 | if (IS_ERR(sock)) { |
| 1187 | ret = PTR_ERR(sock); |
| 1188 | sock = NULL; |
| 1189 | goto out; |
| 1190 | } |
| 1191 | |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1192 | timeo = MAX_SCHEDULE_TIMEOUT; |
Denis V. Lunev | 9457afe | 2008-06-05 11:23:39 -0700 | [diff] [blame] | 1193 | ret = netlink_attachskb(sock, nc, &timeo, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | if (ret == 1) |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1195 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | if (ret) { |
| 1197 | sock = NULL; |
| 1198 | nc = NULL; |
| 1199 | goto out; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | filp = fget(mqdes); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1205 | if (!filp) { |
| 1206 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1210 | inode = filp->f_path.dentry->d_inode; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1211 | if (unlikely(filp->f_op != &mqueue_file_operations)) { |
| 1212 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | goto out_fput; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | info = MQUEUE_I(inode); |
| 1216 | |
| 1217 | ret = 0; |
| 1218 | spin_lock(&info->lock); |
| 1219 | if (u_notification == NULL) { |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 1220 | if (info->notify_owner == task_tgid(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | remove_notification(info); |
| 1222 | inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1223 | } |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 1224 | } else if (info->notify_owner != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | ret = -EBUSY; |
| 1226 | } else { |
| 1227 | switch (notification.sigev_notify) { |
| 1228 | case SIGEV_NONE: |
| 1229 | info->notify.sigev_notify = SIGEV_NONE; |
| 1230 | break; |
| 1231 | case SIGEV_THREAD: |
| 1232 | info->notify_sock = sock; |
| 1233 | info->notify_cookie = nc; |
| 1234 | sock = NULL; |
| 1235 | nc = NULL; |
| 1236 | info->notify.sigev_notify = SIGEV_THREAD; |
| 1237 | break; |
| 1238 | case SIGEV_SIGNAL: |
| 1239 | info->notify.sigev_signo = notification.sigev_signo; |
| 1240 | info->notify.sigev_value = notification.sigev_value; |
| 1241 | info->notify.sigev_notify = SIGEV_SIGNAL; |
| 1242 | break; |
| 1243 | } |
Cedric Le Goater | a03fcb7 | 2006-10-02 02:17:26 -0700 | [diff] [blame] | 1244 | |
| 1245 | info->notify_owner = get_pid(task_tgid(current)); |
Eric W. Biederman | 6f9ac6d | 2011-11-16 22:57:55 -0800 | [diff] [blame] | 1246 | info->notify_user_ns = get_user_ns(current_user_ns()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1248 | } |
| 1249 | spin_unlock(&info->lock); |
| 1250 | out_fput: |
| 1251 | fput(filp); |
| 1252 | out: |
| 1253 | if (sock) { |
| 1254 | netlink_detachskb(sock, nc); |
| 1255 | } else if (nc) { |
| 1256 | dev_kfree_skb(nc); |
| 1257 | } |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1261 | SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes, |
| 1262 | const struct mq_attr __user *, u_mqstat, |
| 1263 | struct mq_attr __user *, u_omqstat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | { |
| 1265 | int ret; |
| 1266 | struct mq_attr mqstat, omqstat; |
| 1267 | struct file *filp; |
| 1268 | struct inode *inode; |
| 1269 | struct mqueue_inode_info *info; |
| 1270 | |
| 1271 | if (u_mqstat != NULL) { |
| 1272 | if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr))) |
| 1273 | return -EFAULT; |
| 1274 | if (mqstat.mq_flags & (~O_NONBLOCK)) |
| 1275 | return -EINVAL; |
| 1276 | } |
| 1277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | filp = fget(mqdes); |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1279 | if (!filp) { |
| 1280 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | goto out; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1284 | inode = filp->f_path.dentry->d_inode; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1285 | if (unlikely(filp->f_op != &mqueue_file_operations)) { |
| 1286 | ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | goto out_fput; |
André Goddard Rosa | 8d8ffef | 2010-02-23 04:04:26 -0300 | [diff] [blame] | 1288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | info = MQUEUE_I(inode); |
| 1290 | |
| 1291 | spin_lock(&info->lock); |
| 1292 | |
| 1293 | omqstat = info->attr; |
| 1294 | omqstat.mq_flags = filp->f_flags & O_NONBLOCK; |
| 1295 | if (u_mqstat) { |
Al Viro | 7392906 | 2008-12-10 06:58:59 -0500 | [diff] [blame] | 1296 | audit_mq_getsetattr(mqdes, &mqstat); |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1297 | spin_lock(&filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | if (mqstat.mq_flags & O_NONBLOCK) |
| 1299 | filp->f_flags |= O_NONBLOCK; |
| 1300 | else |
| 1301 | filp->f_flags &= ~O_NONBLOCK; |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1302 | spin_unlock(&filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1305 | } |
| 1306 | |
| 1307 | spin_unlock(&info->lock); |
| 1308 | |
| 1309 | ret = 0; |
| 1310 | if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat, |
| 1311 | sizeof(struct mq_attr))) |
| 1312 | ret = -EFAULT; |
| 1313 | |
| 1314 | out_fput: |
| 1315 | fput(filp); |
| 1316 | out: |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1320 | static const struct inode_operations mqueue_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | .lookup = simple_lookup, |
| 1322 | .create = mqueue_create, |
| 1323 | .unlink = mqueue_unlink, |
| 1324 | }; |
| 1325 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1326 | static const struct file_operations mqueue_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | .flush = mqueue_flush_file, |
| 1328 | .poll = mqueue_poll_file, |
| 1329 | .read = mqueue_read_file, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1330 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | }; |
| 1332 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1333 | static const struct super_operations mqueue_super_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | .alloc_inode = mqueue_alloc_inode, |
| 1335 | .destroy_inode = mqueue_destroy_inode, |
Al Viro | 6d8af64 | 2010-06-05 16:29:45 -0400 | [diff] [blame] | 1336 | .evict_inode = mqueue_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | .statfs = simple_statfs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | }; |
| 1339 | |
| 1340 | static struct file_system_type mqueue_fs_type = { |
| 1341 | .name = "mqueue", |
Al Viro | ceefda6 | 2010-07-26 13:16:50 +0400 | [diff] [blame] | 1342 | .mount = mqueue_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | .kill_sb = kill_litter_super, |
| 1344 | }; |
| 1345 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 1346 | int mq_init_ns(struct ipc_namespace *ns) |
| 1347 | { |
| 1348 | ns->mq_queues_count = 0; |
| 1349 | ns->mq_queues_max = DFLT_QUEUESMAX; |
| 1350 | ns->mq_msg_max = DFLT_MSGMAX; |
| 1351 | ns->mq_msgsize_max = DFLT_MSGSIZEMAX; |
KOSAKI Motohiro | cef0184 | 2012-05-31 16:26:33 -0700 | [diff] [blame] | 1352 | ns->mq_msg_default = DFLT_MSG; |
| 1353 | ns->mq_msgsize_default = DFLT_MSGSIZE; |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 1354 | |
| 1355 | ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns); |
| 1356 | if (IS_ERR(ns->mq_mnt)) { |
| 1357 | int err = PTR_ERR(ns->mq_mnt); |
| 1358 | ns->mq_mnt = NULL; |
| 1359 | return err; |
| 1360 | } |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | void mq_clear_sbinfo(struct ipc_namespace *ns) |
| 1365 | { |
| 1366 | ns->mq_mnt->mnt_sb->s_fs_info = NULL; |
| 1367 | } |
| 1368 | |
| 1369 | void mq_put_mnt(struct ipc_namespace *ns) |
| 1370 | { |
Al Viro | 6f68657 | 2011-12-09 00:38:50 -0500 | [diff] [blame] | 1371 | kern_unmount(ns->mq_mnt); |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | static int __init init_mqueue_fs(void) |
| 1375 | { |
| 1376 | int error; |
| 1377 | |
| 1378 | mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache", |
| 1379 | sizeof(struct mqueue_inode_info), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1380 | SLAB_HWCACHE_ALIGN, init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (mqueue_inode_cachep == NULL) |
| 1382 | return -ENOMEM; |
| 1383 | |
André Goddard Rosa | 2329e39 | 2010-02-23 04:04:27 -0300 | [diff] [blame] | 1384 | /* ignore failures - they are not fatal */ |
Serge E. Hallyn | bdc8e5f | 2009-04-06 19:01:11 -0700 | [diff] [blame] | 1385 | mq_sysctl_table = mq_register_sysctl_table(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
| 1387 | error = register_filesystem(&mqueue_fs_type); |
| 1388 | if (error) |
| 1389 | goto out_sysctl; |
| 1390 | |
Serge E. Hallyn | 7eafd7c | 2009-04-06 19:01:10 -0700 | [diff] [blame] | 1391 | spin_lock_init(&mq_lock); |
| 1392 | |
Al Viro | 6f68657 | 2011-12-09 00:38:50 -0500 | [diff] [blame] | 1393 | error = mq_init_ns(&init_ipc_ns); |
| 1394 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | goto out_filesystem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | return 0; |
| 1398 | |
| 1399 | out_filesystem: |
| 1400 | unregister_filesystem(&mqueue_fs_type); |
| 1401 | out_sysctl: |
| 1402 | if (mq_sysctl_table) |
| 1403 | unregister_sysctl_table(mq_sysctl_table); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1404 | kmem_cache_destroy(mqueue_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | return error; |
| 1406 | } |
| 1407 | |
| 1408 | __initcall(init_mqueue_fs); |