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