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