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