tcp: add tcp_min_snd_mss sysctl
[pandora-kernel.git] / fs / fcntl.c
1 /*
2  *  linux/fs/fcntl.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/syscalls.h>
8 #include <linux/init.h>
9 #include <linux/mm.h>
10 #include <linux/fs.h>
11 #include <linux/file.h>
12 #include <linux/fdtable.h>
13 #include <linux/capability.h>
14 #include <linux/dnotify.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/pipe_fs_i.h>
18 #include <linux/security.h>
19 #include <linux/ptrace.h>
20 #include <linux/signal.h>
21 #include <linux/rcupdate.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/shmem_fs.h>
24
25 #include <asm/poll.h>
26 #include <asm/siginfo.h>
27 #include <asm/uaccess.h>
28
29 void set_close_on_exec(unsigned int fd, int flag)
30 {
31         struct files_struct *files = current->files;
32         struct fdtable *fdt;
33         spin_lock(&files->file_lock);
34         fdt = files_fdtable(files);
35         if (flag)
36                 __set_close_on_exec(fd, fdt);
37         else
38                 __clear_close_on_exec(fd, fdt);
39         spin_unlock(&files->file_lock);
40 }
41
42 static bool get_close_on_exec(unsigned int fd)
43 {
44         struct files_struct *files = current->files;
45         struct fdtable *fdt;
46         bool res;
47         rcu_read_lock();
48         fdt = files_fdtable(files);
49         res = close_on_exec(fd, fdt);
50         rcu_read_unlock();
51         return res;
52 }
53
54 SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
55 {
56         int err = -EBADF;
57         struct file * file, *tofree;
58         struct files_struct * files = current->files;
59         struct fdtable *fdt;
60
61         if ((flags & ~O_CLOEXEC) != 0)
62                 return -EINVAL;
63
64         if (unlikely(oldfd == newfd))
65                 return -EINVAL;
66
67         spin_lock(&files->file_lock);
68         err = expand_files(files, newfd);
69         file = fcheck(oldfd);
70         if (unlikely(!file))
71                 goto Ebadf;
72         if (unlikely(err < 0)) {
73                 if (err == -EMFILE)
74                         goto Ebadf;
75                 goto out_unlock;
76         }
77         /*
78          * We need to detect attempts to do dup2() over allocated but still
79          * not finished descriptor.  NB: OpenBSD avoids that at the price of
80          * extra work in their equivalent of fget() - they insert struct
81          * file immediately after grabbing descriptor, mark it larval if
82          * more work (e.g. actual opening) is needed and make sure that
83          * fget() treats larval files as absent.  Potentially interesting,
84          * but while extra work in fget() is trivial, locking implications
85          * and amount of surgery on open()-related paths in VFS are not.
86          * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
87          * deadlocks in rather amusing ways, AFAICS.  All of that is out of
88          * scope of POSIX or SUS, since neither considers shared descriptor
89          * tables and this condition does not arise without those.
90          */
91         err = -EBUSY;
92         fdt = files_fdtable(files);
93         tofree = fdt->fd[newfd];
94         if (!tofree && fd_is_open(newfd, fdt))
95                 goto out_unlock;
96         get_file(file);
97         rcu_assign_pointer(fdt->fd[newfd], file);
98         __set_open_fd(newfd, fdt);
99         if (flags & O_CLOEXEC)
100                 __set_close_on_exec(newfd, fdt);
101         else
102                 __clear_close_on_exec(newfd, fdt);
103         spin_unlock(&files->file_lock);
104
105         if (tofree)
106                 filp_close(tofree, files);
107
108         return newfd;
109
110 Ebadf:
111         err = -EBADF;
112 out_unlock:
113         spin_unlock(&files->file_lock);
114         return err;
115 }
116
117 SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
118 {
119         if (unlikely(newfd == oldfd)) { /* corner case */
120                 struct files_struct *files = current->files;
121                 int retval = oldfd;
122
123                 rcu_read_lock();
124                 if (!fcheck_files(files, oldfd))
125                         retval = -EBADF;
126                 rcu_read_unlock();
127                 return retval;
128         }
129         return sys_dup3(oldfd, newfd, 0);
130 }
131
132 SYSCALL_DEFINE1(dup, unsigned int, fildes)
133 {
134         int ret = -EBADF;
135         struct file *file = fget_raw(fildes);
136
137         if (file) {
138                 ret = get_unused_fd();
139                 if (ret >= 0)
140                         fd_install(ret, file);
141                 else
142                         fput(file);
143         }
144         return ret;
145 }
146
147 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
148
149 static int setfl(int fd, struct file * filp, unsigned long arg)
150 {
151         struct inode * inode = filp->f_path.dentry->d_inode;
152         int error = 0;
153
154         /*
155          * O_APPEND cannot be cleared if the file is marked as append-only
156          * and the file is open for write.
157          */
158         if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
159                 return -EPERM;
160
161         /* O_NOATIME can only be set by the owner or superuser */
162         if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
163                 if (!inode_owner_or_capable(inode))
164                         return -EPERM;
165
166         /* required for strict SunOS emulation */
167         if (O_NONBLOCK != O_NDELAY)
168                if (arg & O_NDELAY)
169                    arg |= O_NONBLOCK;
170
171         if (arg & O_DIRECT) {
172                 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
173                         !filp->f_mapping->a_ops->direct_IO)
174                                 return -EINVAL;
175         }
176
177         if (filp->f_op && filp->f_op->check_flags)
178                 error = filp->f_op->check_flags(arg);
179         if (error)
180                 return error;
181
182         /*
183          * ->fasync() is responsible for setting the FASYNC bit.
184          */
185         if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
186                         filp->f_op->fasync) {
187                 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
188                 if (error < 0)
189                         goto out;
190                 if (error > 0)
191                         error = 0;
192         }
193         spin_lock(&filp->f_lock);
194         filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
195         spin_unlock(&filp->f_lock);
196
197  out:
198         return error;
199 }
200
201 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
202                      int force)
203 {
204         write_lock_irq(&filp->f_owner.lock);
205         if (force || !filp->f_owner.pid) {
206                 put_pid(filp->f_owner.pid);
207                 filp->f_owner.pid = get_pid(pid);
208                 filp->f_owner.pid_type = type;
209
210                 if (pid) {
211                         const struct cred *cred = current_cred();
212                         filp->f_owner.uid = cred->uid;
213                         filp->f_owner.euid = cred->euid;
214                 }
215         }
216         write_unlock_irq(&filp->f_owner.lock);
217 }
218
219 int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
220                 int force)
221 {
222         int err;
223
224         err = security_file_set_fowner(filp);
225         if (err)
226                 return err;
227
228         f_modown(filp, pid, type, force);
229         return 0;
230 }
231 EXPORT_SYMBOL(__f_setown);
232
233 int f_setown(struct file *filp, unsigned long arg, int force)
234 {
235         enum pid_type type;
236         struct pid *pid;
237         int who = arg;
238         int result;
239         type = PIDTYPE_PID;
240         if (who < 0) {
241                 type = PIDTYPE_PGID;
242                 who = -who;
243         }
244         rcu_read_lock();
245         pid = find_vpid(who);
246         result = __f_setown(filp, pid, type, force);
247         rcu_read_unlock();
248         return result;
249 }
250 EXPORT_SYMBOL(f_setown);
251
252 void f_delown(struct file *filp)
253 {
254         f_modown(filp, NULL, PIDTYPE_PID, 1);
255 }
256
257 pid_t f_getown(struct file *filp)
258 {
259         pid_t pid;
260         read_lock(&filp->f_owner.lock);
261         pid = pid_vnr(filp->f_owner.pid);
262         if (filp->f_owner.pid_type == PIDTYPE_PGID)
263                 pid = -pid;
264         read_unlock(&filp->f_owner.lock);
265         return pid;
266 }
267
268 static int f_setown_ex(struct file *filp, unsigned long arg)
269 {
270         struct f_owner_ex * __user owner_p = (void * __user)arg;
271         struct f_owner_ex owner;
272         struct pid *pid;
273         int type;
274         int ret;
275
276         ret = copy_from_user(&owner, owner_p, sizeof(owner));
277         if (ret)
278                 return -EFAULT;
279
280         switch (owner.type) {
281         case F_OWNER_TID:
282                 type = PIDTYPE_MAX;
283                 break;
284
285         case F_OWNER_PID:
286                 type = PIDTYPE_PID;
287                 break;
288
289         case F_OWNER_PGRP:
290                 type = PIDTYPE_PGID;
291                 break;
292
293         default:
294                 return -EINVAL;
295         }
296
297         rcu_read_lock();
298         pid = find_vpid(owner.pid);
299         if (owner.pid && !pid)
300                 ret = -ESRCH;
301         else
302                 ret = __f_setown(filp, pid, type, 1);
303         rcu_read_unlock();
304
305         return ret;
306 }
307
308 static int f_getown_ex(struct file *filp, unsigned long arg)
309 {
310         struct f_owner_ex * __user owner_p = (void * __user)arg;
311         struct f_owner_ex owner;
312         int ret = 0;
313
314         read_lock(&filp->f_owner.lock);
315         owner.pid = pid_vnr(filp->f_owner.pid);
316         switch (filp->f_owner.pid_type) {
317         case PIDTYPE_MAX:
318                 owner.type = F_OWNER_TID;
319                 break;
320
321         case PIDTYPE_PID:
322                 owner.type = F_OWNER_PID;
323                 break;
324
325         case PIDTYPE_PGID:
326                 owner.type = F_OWNER_PGRP;
327                 break;
328
329         default:
330                 WARN_ON(1);
331                 ret = -EINVAL;
332                 break;
333         }
334         read_unlock(&filp->f_owner.lock);
335
336         if (!ret) {
337                 ret = copy_to_user(owner_p, &owner, sizeof(owner));
338                 if (ret)
339                         ret = -EFAULT;
340         }
341         return ret;
342 }
343
344 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
345                 struct file *filp)
346 {
347         long err = -EINVAL;
348
349         switch (cmd) {
350         case F_DUPFD:
351         case F_DUPFD_CLOEXEC:
352                 if (arg >= rlimit(RLIMIT_NOFILE))
353                         break;
354                 err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
355                 if (err >= 0) {
356                         get_file(filp);
357                         fd_install(err, filp);
358                 }
359                 break;
360         case F_GETFD:
361                 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
362                 break;
363         case F_SETFD:
364                 err = 0;
365                 set_close_on_exec(fd, arg & FD_CLOEXEC);
366                 break;
367         case F_GETFL:
368                 err = filp->f_flags;
369                 break;
370         case F_SETFL:
371                 err = setfl(fd, filp, arg);
372                 break;
373         case F_GETLK:
374                 err = fcntl_getlk(filp, (struct flock __user *) arg);
375                 break;
376         case F_SETLK:
377         case F_SETLKW:
378                 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
379                 break;
380         case F_GETOWN:
381                 /*
382                  * XXX If f_owner is a process group, the
383                  * negative return value will get converted
384                  * into an error.  Oops.  If we keep the
385                  * current syscall conventions, the only way
386                  * to fix this will be in libc.
387                  */
388                 err = f_getown(filp);
389                 force_successful_syscall_return();
390                 break;
391         case F_SETOWN:
392                 err = f_setown(filp, arg, 1);
393                 break;
394         case F_GETOWN_EX:
395                 err = f_getown_ex(filp, arg);
396                 break;
397         case F_SETOWN_EX:
398                 err = f_setown_ex(filp, arg);
399                 break;
400         case F_GETSIG:
401                 err = filp->f_owner.signum;
402                 break;
403         case F_SETSIG:
404                 /* arg == 0 restores default behaviour. */
405                 if (!valid_signal(arg)) {
406                         break;
407                 }
408                 err = 0;
409                 filp->f_owner.signum = arg;
410                 break;
411         case F_GETLEASE:
412                 err = fcntl_getlease(filp);
413                 break;
414         case F_SETLEASE:
415                 err = fcntl_setlease(fd, filp, arg);
416                 break;
417         case F_NOTIFY:
418                 err = fcntl_dirnotify(fd, filp, arg);
419                 break;
420         case F_SETPIPE_SZ:
421         case F_GETPIPE_SZ:
422                 err = pipe_fcntl(filp, cmd, arg);
423                 break;
424         case F_ADD_SEALS:
425         case F_GET_SEALS:
426                 err = shmem_fcntl(filp, cmd, arg);
427                 break;
428         default:
429                 break;
430         }
431         return err;
432 }
433
434 static int check_fcntl_cmd(unsigned cmd)
435 {
436         switch (cmd) {
437         case F_DUPFD:
438         case F_DUPFD_CLOEXEC:
439         case F_GETFD:
440         case F_SETFD:
441         case F_GETFL:
442                 return 1;
443         }
444         return 0;
445 }
446
447 SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
448 {       
449         struct file *filp;
450         long err = -EBADF;
451
452         filp = fget_raw(fd);
453         if (!filp)
454                 goto out;
455
456         if (unlikely(filp->f_mode & FMODE_PATH)) {
457                 if (!check_fcntl_cmd(cmd)) {
458                         fput(filp);
459                         goto out;
460                 }
461         }
462
463         err = security_file_fcntl(filp, cmd, arg);
464         if (err) {
465                 fput(filp);
466                 return err;
467         }
468
469         err = do_fcntl(fd, cmd, arg, filp);
470
471         fput(filp);
472 out:
473         return err;
474 }
475
476 #if BITS_PER_LONG == 32
477 SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
478                 unsigned long, arg)
479 {       
480         struct file * filp;
481         long err;
482
483         err = -EBADF;
484         filp = fget_raw(fd);
485         if (!filp)
486                 goto out;
487
488         if (unlikely(filp->f_mode & FMODE_PATH)) {
489                 if (!check_fcntl_cmd(cmd)) {
490                         fput(filp);
491                         goto out;
492                 }
493         }
494
495         err = security_file_fcntl(filp, cmd, arg);
496         if (err) {
497                 fput(filp);
498                 return err;
499         }
500         err = -EBADF;
501         
502         switch (cmd) {
503                 case F_GETLK64:
504                         err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
505                         break;
506                 case F_SETLK64:
507                 case F_SETLKW64:
508                         err = fcntl_setlk64(fd, filp, cmd,
509                                         (struct flock64 __user *) arg);
510                         break;
511                 default:
512                         err = do_fcntl(fd, cmd, arg, filp);
513                         break;
514         }
515         fput(filp);
516 out:
517         return err;
518 }
519 #endif
520
521 /* Table to convert sigio signal codes into poll band bitmaps */
522
523 static const long band_table[NSIGPOLL] = {
524         POLLIN | POLLRDNORM,                    /* POLL_IN */
525         POLLOUT | POLLWRNORM | POLLWRBAND,      /* POLL_OUT */
526         POLLIN | POLLRDNORM | POLLMSG,          /* POLL_MSG */
527         POLLERR,                                /* POLL_ERR */
528         POLLPRI | POLLRDBAND,                   /* POLL_PRI */
529         POLLHUP | POLLERR                       /* POLL_HUP */
530 };
531
532 static inline int sigio_perm(struct task_struct *p,
533                              struct fown_struct *fown, int sig)
534 {
535         const struct cred *cred;
536         int ret;
537
538         rcu_read_lock();
539         cred = __task_cred(p);
540         ret = ((fown->euid == 0 ||
541                 fown->euid == cred->suid || fown->euid == cred->uid ||
542                 fown->uid  == cred->suid || fown->uid  == cred->uid) &&
543                !security_file_send_sigiotask(p, fown, sig));
544         rcu_read_unlock();
545         return ret;
546 }
547
548 static void send_sigio_to_task(struct task_struct *p,
549                                struct fown_struct *fown,
550                                int fd, int reason, int group)
551 {
552         /*
553          * F_SETSIG can change ->signum lockless in parallel, make
554          * sure we read it once and use the same value throughout.
555          */
556         int signum = ACCESS_ONCE(fown->signum);
557
558         if (!sigio_perm(p, fown, signum))
559                 return;
560
561         switch (signum) {
562                 siginfo_t si;
563                 default:
564                         /* Queue a rt signal with the appropriate fd as its
565                            value.  We use SI_SIGIO as the source, not 
566                            SI_KERNEL, since kernel signals always get 
567                            delivered even if we can't queue.  Failure to
568                            queue in this case _should_ be reported; we fall
569                            back to SIGIO in that case. --sct */
570                         si.si_signo = signum;
571                         si.si_errno = 0;
572                         si.si_code  = reason;
573                         /*
574                          * Posix definies POLL_IN and friends to be signal
575                          * specific si_codes for SIG_POLL.  Linux extended
576                          * these si_codes to other signals in a way that is
577                          * ambiguous if other signals also have signal
578                          * specific si_codes.  In that case use SI_SIGIO instead
579                          * to remove the ambiguity.
580                          */
581                         if (sig_specific_sicodes(signum))
582                                 si.si_code = SI_SIGIO;
583
584                         /* Make sure we are called with one of the POLL_*
585                            reasons, otherwise we could leak kernel stack into
586                            userspace.  */
587                         BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
588                         if (reason - POLL_IN >= NSIGPOLL)
589                                 si.si_band  = ~0L;
590                         else
591                                 si.si_band = band_table[reason - POLL_IN];
592                         si.si_fd    = fd;
593                         if (!do_send_sig_info(signum, &si, p, group))
594                                 break;
595                 /* fall-through: fall back on the old plain SIGIO signal */
596                 case 0:
597                         do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
598         }
599 }
600
601 void send_sigio(struct fown_struct *fown, int fd, int band)
602 {
603         struct task_struct *p;
604         enum pid_type type;
605         struct pid *pid;
606         int group = 1;
607         
608         read_lock(&fown->lock);
609
610         type = fown->pid_type;
611         if (type == PIDTYPE_MAX) {
612                 group = 0;
613                 type = PIDTYPE_PID;
614         }
615
616         pid = fown->pid;
617         if (!pid)
618                 goto out_unlock_fown;
619         
620         read_lock(&tasklist_lock);
621         do_each_pid_task(pid, type, p) {
622                 send_sigio_to_task(p, fown, fd, band, group);
623         } while_each_pid_task(pid, type, p);
624         read_unlock(&tasklist_lock);
625  out_unlock_fown:
626         read_unlock(&fown->lock);
627 }
628
629 static void send_sigurg_to_task(struct task_struct *p,
630                                 struct fown_struct *fown, int group)
631 {
632         if (sigio_perm(p, fown, SIGURG))
633                 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
634 }
635
636 int send_sigurg(struct fown_struct *fown)
637 {
638         struct task_struct *p;
639         enum pid_type type;
640         struct pid *pid;
641         int group = 1;
642         int ret = 0;
643         
644         read_lock(&fown->lock);
645
646         type = fown->pid_type;
647         if (type == PIDTYPE_MAX) {
648                 group = 0;
649                 type = PIDTYPE_PID;
650         }
651
652         pid = fown->pid;
653         if (!pid)
654                 goto out_unlock_fown;
655
656         ret = 1;
657         
658         read_lock(&tasklist_lock);
659         do_each_pid_task(pid, type, p) {
660                 send_sigurg_to_task(p, fown, group);
661         } while_each_pid_task(pid, type, p);
662         read_unlock(&tasklist_lock);
663  out_unlock_fown:
664         read_unlock(&fown->lock);
665         return ret;
666 }
667
668 static DEFINE_SPINLOCK(fasync_lock);
669 static struct kmem_cache *fasync_cache __read_mostly;
670
671 static void fasync_free_rcu(struct rcu_head *head)
672 {
673         kmem_cache_free(fasync_cache,
674                         container_of(head, struct fasync_struct, fa_rcu));
675 }
676
677 /*
678  * Remove a fasync entry. If successfully removed, return
679  * positive and clear the FASYNC flag. If no entry exists,
680  * do nothing and return 0.
681  *
682  * NOTE! It is very important that the FASYNC flag always
683  * match the state "is the filp on a fasync list".
684  *
685  */
686 int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
687 {
688         struct fasync_struct *fa, **fp;
689         int result = 0;
690
691         spin_lock(&filp->f_lock);
692         spin_lock(&fasync_lock);
693         for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
694                 if (fa->fa_file != filp)
695                         continue;
696
697                 spin_lock_irq(&fa->fa_lock);
698                 fa->fa_file = NULL;
699                 spin_unlock_irq(&fa->fa_lock);
700
701                 *fp = fa->fa_next;
702                 call_rcu(&fa->fa_rcu, fasync_free_rcu);
703                 filp->f_flags &= ~FASYNC;
704                 result = 1;
705                 break;
706         }
707         spin_unlock(&fasync_lock);
708         spin_unlock(&filp->f_lock);
709         return result;
710 }
711
712 struct fasync_struct *fasync_alloc(void)
713 {
714         return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
715 }
716
717 /*
718  * NOTE! This can be used only for unused fasync entries:
719  * entries that actually got inserted on the fasync list
720  * need to be released by rcu - see fasync_remove_entry.
721  */
722 void fasync_free(struct fasync_struct *new)
723 {
724         kmem_cache_free(fasync_cache, new);
725 }
726
727 /*
728  * Insert a new entry into the fasync list.  Return the pointer to the
729  * old one if we didn't use the new one.
730  *
731  * NOTE! It is very important that the FASYNC flag always
732  * match the state "is the filp on a fasync list".
733  */
734 struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
735 {
736         struct fasync_struct *fa, **fp;
737
738         spin_lock(&filp->f_lock);
739         spin_lock(&fasync_lock);
740         for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
741                 if (fa->fa_file != filp)
742                         continue;
743
744                 spin_lock_irq(&fa->fa_lock);
745                 fa->fa_fd = fd;
746                 spin_unlock_irq(&fa->fa_lock);
747                 goto out;
748         }
749
750         spin_lock_init(&new->fa_lock);
751         new->magic = FASYNC_MAGIC;
752         new->fa_file = filp;
753         new->fa_fd = fd;
754         new->fa_next = *fapp;
755         rcu_assign_pointer(*fapp, new);
756         filp->f_flags |= FASYNC;
757
758 out:
759         spin_unlock(&fasync_lock);
760         spin_unlock(&filp->f_lock);
761         return fa;
762 }
763
764 /*
765  * Add a fasync entry. Return negative on error, positive if
766  * added, and zero if did nothing but change an existing one.
767  */
768 static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
769 {
770         struct fasync_struct *new;
771
772         new = fasync_alloc();
773         if (!new)
774                 return -ENOMEM;
775
776         /*
777          * fasync_insert_entry() returns the old (update) entry if
778          * it existed.
779          *
780          * So free the (unused) new entry and return 0 to let the
781          * caller know that we didn't add any new fasync entries.
782          */
783         if (fasync_insert_entry(fd, filp, fapp, new)) {
784                 fasync_free(new);
785                 return 0;
786         }
787
788         return 1;
789 }
790
791 /*
792  * fasync_helper() is used by almost all character device drivers
793  * to set up the fasync queue, and for regular files by the file
794  * lease code. It returns negative on error, 0 if it did no changes
795  * and positive if it added/deleted the entry.
796  */
797 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
798 {
799         if (!on)
800                 return fasync_remove_entry(filp, fapp);
801         return fasync_add_entry(fd, filp, fapp);
802 }
803
804 EXPORT_SYMBOL(fasync_helper);
805
806 /*
807  * rcu_read_lock() is held
808  */
809 static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
810 {
811         while (fa) {
812                 struct fown_struct *fown;
813                 unsigned long flags;
814
815                 if (fa->magic != FASYNC_MAGIC) {
816                         printk(KERN_ERR "kill_fasync: bad magic number in "
817                                "fasync_struct!\n");
818                         return;
819                 }
820                 spin_lock_irqsave(&fa->fa_lock, flags);
821                 if (fa->fa_file) {
822                         fown = &fa->fa_file->f_owner;
823                         /* Don't send SIGURG to processes which have not set a
824                            queued signum: SIGURG has its own default signalling
825                            mechanism. */
826                         if (!(sig == SIGURG && fown->signum == 0))
827                                 send_sigio(fown, fa->fa_fd, band);
828                 }
829                 spin_unlock_irqrestore(&fa->fa_lock, flags);
830                 fa = rcu_dereference(fa->fa_next);
831         }
832 }
833
834 void kill_fasync(struct fasync_struct **fp, int sig, int band)
835 {
836         /* First a quick test without locking: usually
837          * the list is empty.
838          */
839         if (*fp) {
840                 rcu_read_lock();
841                 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
842                 rcu_read_unlock();
843         }
844 }
845 EXPORT_SYMBOL(kill_fasync);
846
847 static int __init fcntl_init(void)
848 {
849         /*
850          * Please add new bits here to ensure allocation uniqueness.
851          * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
852          * is defined as O_NONBLOCK on some platforms and not on others.
853          */
854         BUILD_BUG_ON(19 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
855                 O_RDONLY        | O_WRONLY      | O_RDWR        |
856                 O_CREAT         | O_EXCL        | O_NOCTTY      |
857                 O_TRUNC         | O_APPEND      | /* O_NONBLOCK | */
858                 __O_SYNC        | O_DSYNC       | FASYNC        |
859                 O_DIRECT        | O_LARGEFILE   | O_DIRECTORY   |
860                 O_NOFOLLOW      | O_NOATIME     | O_CLOEXEC     |
861                 __FMODE_EXEC    | O_PATH
862                 ));
863
864         fasync_cache = kmem_cache_create("fasync_cache",
865                 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
866         return 0;
867 }
868
869 module_init(fcntl_init)