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