aboutsummaryrefslogtreecommitdiffstats
path: root/fs/fcntl.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/fcntl.c')
-rw-r--r--fs/fcntl.c71
1 files changed, 47 insertions, 24 deletions
diff --git a/fs/fcntl.c b/fs/fcntl.c
index 452d02f9075e..f74d270ba155 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -14,6 +14,7 @@
14#include <linux/dnotify.h> 14#include <linux/dnotify.h>
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/pipe_fs_i.h>
17#include <linux/security.h> 18#include <linux/security.h>
18#include <linux/ptrace.h> 19#include <linux/ptrace.h>
19#include <linux/signal.h> 20#include <linux/signal.h>
@@ -412,6 +413,10 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
412 case F_NOTIFY: 413 case F_NOTIFY:
413 err = fcntl_dirnotify(fd, filp, arg); 414 err = fcntl_dirnotify(fd, filp, arg);
414 break; 415 break;
416 case F_SETPIPE_SZ:
417 case F_GETPIPE_SZ:
418 err = pipe_fcntl(filp, cmd, arg);
419 break;
415 default: 420 default:
416 break; 421 break;
417 } 422 }
@@ -614,9 +619,15 @@ int send_sigurg(struct fown_struct *fown)
614 return ret; 619 return ret;
615} 620}
616 621
617static DEFINE_RWLOCK(fasync_lock); 622static DEFINE_SPINLOCK(fasync_lock);
618static struct kmem_cache *fasync_cache __read_mostly; 623static struct kmem_cache *fasync_cache __read_mostly;
619 624
625static void fasync_free_rcu(struct rcu_head *head)
626{
627 kmem_cache_free(fasync_cache,
628 container_of(head, struct fasync_struct, fa_rcu));
629}
630
620/* 631/*
621 * Remove a fasync entry. If successfully removed, return 632 * Remove a fasync entry. If successfully removed, return
622 * positive and clear the FASYNC flag. If no entry exists, 633 * positive and clear the FASYNC flag. If no entry exists,
@@ -625,8 +636,6 @@ static struct kmem_cache *fasync_cache __read_mostly;
625 * NOTE! It is very important that the FASYNC flag always 636 * NOTE! It is very important that the FASYNC flag always
626 * match the state "is the filp on a fasync list". 637 * match the state "is the filp on a fasync list".
627 * 638 *
628 * We always take the 'filp->f_lock', in since fasync_lock
629 * needs to be irq-safe.
630 */ 639 */
631static int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp) 640static int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
632{ 641{
@@ -634,17 +643,22 @@ static int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
634 int result = 0; 643 int result = 0;
635 644
636 spin_lock(&filp->f_lock); 645 spin_lock(&filp->f_lock);
637 write_lock_irq(&fasync_lock); 646 spin_lock(&fasync_lock);
638 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { 647 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
639 if (fa->fa_file != filp) 648 if (fa->fa_file != filp)
640 continue; 649 continue;
650
651 spin_lock_irq(&fa->fa_lock);
652 fa->fa_file = NULL;
653 spin_unlock_irq(&fa->fa_lock);
654
641 *fp = fa->fa_next; 655 *fp = fa->fa_next;
642 kmem_cache_free(fasync_cache, fa); 656 call_rcu(&fa->fa_rcu, fasync_free_rcu);
643 filp->f_flags &= ~FASYNC; 657 filp->f_flags &= ~FASYNC;
644 result = 1; 658 result = 1;
645 break; 659 break;
646 } 660 }
647 write_unlock_irq(&fasync_lock); 661 spin_unlock(&fasync_lock);
648 spin_unlock(&filp->f_lock); 662 spin_unlock(&filp->f_lock);
649 return result; 663 return result;
650} 664}
@@ -666,25 +680,30 @@ static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fa
666 return -ENOMEM; 680 return -ENOMEM;
667 681
668 spin_lock(&filp->f_lock); 682 spin_lock(&filp->f_lock);
669 write_lock_irq(&fasync_lock); 683 spin_lock(&fasync_lock);
670 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { 684 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
671 if (fa->fa_file != filp) 685 if (fa->fa_file != filp)
672 continue; 686 continue;
687
688 spin_lock_irq(&fa->fa_lock);
673 fa->fa_fd = fd; 689 fa->fa_fd = fd;
690 spin_unlock_irq(&fa->fa_lock);
691
674 kmem_cache_free(fasync_cache, new); 692 kmem_cache_free(fasync_cache, new);
675 goto out; 693 goto out;
676 } 694 }
677 695
696 spin_lock_init(&new->fa_lock);
678 new->magic = FASYNC_MAGIC; 697 new->magic = FASYNC_MAGIC;
679 new->fa_file = filp; 698 new->fa_file = filp;
680 new->fa_fd = fd; 699 new->fa_fd = fd;
681 new->fa_next = *fapp; 700 new->fa_next = *fapp;
682 *fapp = new; 701 rcu_assign_pointer(*fapp, new);
683 result = 1; 702 result = 1;
684 filp->f_flags |= FASYNC; 703 filp->f_flags |= FASYNC;
685 704
686out: 705out:
687 write_unlock_irq(&fasync_lock); 706 spin_unlock(&fasync_lock);
688 spin_unlock(&filp->f_lock); 707 spin_unlock(&filp->f_lock);
689 return result; 708 return result;
690} 709}
@@ -704,37 +723,41 @@ int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fap
704 723
705EXPORT_SYMBOL(fasync_helper); 724EXPORT_SYMBOL(fasync_helper);
706 725
707void __kill_fasync(struct fasync_struct *fa, int sig, int band) 726/*
727 * rcu_read_lock() is held
728 */
729static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
708{ 730{
709 while (fa) { 731 while (fa) {
710 struct fown_struct * fown; 732 struct fown_struct *fown;
711 if (fa->magic != FASYNC_MAGIC) { 733 if (fa->magic != FASYNC_MAGIC) {
712 printk(KERN_ERR "kill_fasync: bad magic number in " 734 printk(KERN_ERR "kill_fasync: bad magic number in "
713 "fasync_struct!\n"); 735 "fasync_struct!\n");
714 return; 736 return;
715 } 737 }
716 fown = &fa->fa_file->f_owner; 738 spin_lock(&fa->fa_lock);
717 /* Don't send SIGURG to processes which have not set a 739 if (fa->fa_file) {
718 queued signum: SIGURG has its own default signalling 740 fown = &fa->fa_file->f_owner;
719 mechanism. */ 741 /* Don't send SIGURG to processes which have not set a
720 if (!(sig == SIGURG && fown->signum == 0)) 742 queued signum: SIGURG has its own default signalling
721 send_sigio(fown, fa->fa_fd, band); 743 mechanism. */
722 fa = fa->fa_next; 744 if (!(sig == SIGURG && fown->signum == 0))
745 send_sigio(fown, fa->fa_fd, band);
746 }
747 spin_unlock(&fa->fa_lock);
748 fa = rcu_dereference(fa->fa_next);
723 } 749 }
724} 750}
725 751
726EXPORT_SYMBOL(__kill_fasync);
727
728void kill_fasync(struct fasync_struct **fp, int sig, int band) 752void kill_fasync(struct fasync_struct **fp, int sig, int band)
729{ 753{
730 /* First a quick test without locking: usually 754 /* First a quick test without locking: usually
731 * the list is empty. 755 * the list is empty.
732 */ 756 */
733 if (*fp) { 757 if (*fp) {
734 read_lock(&fasync_lock); 758 rcu_read_lock();
735 /* reread *fp after obtaining the lock */ 759 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
736 __kill_fasync(*fp, sig, band); 760 rcu_read_unlock();
737 read_unlock(&fasync_lock);
738 } 761 }
739} 762}
740EXPORT_SYMBOL(kill_fasync); 763EXPORT_SYMBOL(kill_fasync);