[PATCH] sysctl: rose: remove unnecessary insert_at_head flag
[safe/jmp/linux-2.6] / net / socket.c
index 156f2ef..0778c54 100644 (file)
  */
 
 #include <linux/mm.h>
-#include <linux/smp_lock.h>
 #include <linux/socket.h>
 #include <linux/file.h>
 #include <linux/net.h>
 #include <linux/interrupt.h>
+#include <linux/rcupdate.h>
 #include <linux/netdevice.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
@@ -77,7 +77,6 @@
 #include <linux/cache.h>
 #include <linux/module.h>
 #include <linux/highmem.h>
-#include <linux/divert.h>
 #include <linux/mount.h>
 #include <linux/security.h>
 #include <linux/syscalls.h>
 #include <linux/netfilter.h>
 
 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
-static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
-                            size_t size, loff_t pos);
-static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
-                             size_t size, loff_t pos);
+static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
+                        unsigned long nr_segs, loff_t pos);
+static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
+                         unsigned long nr_segs, loff_t pos);
 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
 
 static int sock_close(struct inode *inode, struct file *file);
@@ -110,10 +109,6 @@ static long compat_sock_ioctl(struct file *file,
                              unsigned int cmd, unsigned long arg);
 #endif
 static int sock_fasync(int fd, struct file *filp, int on);
-static ssize_t sock_readv(struct file *file, const struct iovec *vector,
-                         unsigned long count, loff_t *ppos);
-static ssize_t sock_writev(struct file *file, const struct iovec *vector,
-                          unsigned long count, loff_t *ppos);
 static ssize_t sock_sendpage(struct file *file, struct page *page,
                             int offset, size_t size, loff_t *ppos, int more);
 
@@ -122,7 +117,7 @@ static ssize_t sock_sendpage(struct file *file, struct page *page,
  *     in the operation structures but are done directly via the socketcall() multiplexor.
  */
 
-static struct file_operations socket_file_ops = {
+static const struct file_operations socket_file_ops = {
        .owner =        THIS_MODULE,
        .llseek =       no_llseek,
        .aio_read =     sock_aio_read,
@@ -136,8 +131,6 @@ static struct file_operations socket_file_ops = {
        .open =         sock_no_open,   /* special open code to disallow open via /proc */
        .release =      sock_close,
        .fasync =       sock_fasync,
-       .readv =        sock_readv,
-       .writev =       sock_writev,
        .sendpage =     sock_sendpage,
        .splice_write = generic_splice_sendpage,
 };
@@ -146,51 +139,8 @@ static struct file_operations socket_file_ops = {
  *     The protocol list. Each protocol is registered in here.
  */
 
-static struct net_proto_family *net_families[NPROTO];
-
-#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-static atomic_t net_family_lockct = ATOMIC_INIT(0);
 static DEFINE_SPINLOCK(net_family_lock);
-
-/* The strategy is: modifications net_family vector are short, do not
-   sleep and veeery rare, but read access should be free of any exclusive
-   locks.
- */
-
-static void net_family_write_lock(void)
-{
-       spin_lock(&net_family_lock);
-       while (atomic_read(&net_family_lockct) != 0) {
-               spin_unlock(&net_family_lock);
-
-               yield();
-
-               spin_lock(&net_family_lock);
-       }
-}
-
-static __inline__ void net_family_write_unlock(void)
-{
-       spin_unlock(&net_family_lock);
-}
-
-static __inline__ void net_family_read_lock(void)
-{
-       atomic_inc(&net_family_lockct);
-       spin_unlock_wait(&net_family_lock);
-}
-
-static __inline__ void net_family_read_unlock(void)
-{
-       atomic_dec(&net_family_lockct);
-}
-
-#else
-#define net_family_write_lock() do { } while(0)
-#define net_family_write_unlock() do { } while(0)
-#define net_family_read_lock() do { } while(0)
-#define net_family_read_unlock() do { } while(0)
-#endif
+static const struct net_proto_family *net_families[NPROTO] __read_mostly;
 
 /*
  *     Statistics counters of the socket lists
@@ -280,13 +230,13 @@ int move_addr_to_user(void *kaddr, int klen, void __user *uaddr,
 
 #define SOCKFS_MAGIC 0x534F434B
 
-static kmem_cache_t *sock_inode_cachep __read_mostly;
+static struct kmem_cache *sock_inode_cachep __read_mostly;
 
 static struct inode *sock_alloc_inode(struct super_block *sb)
 {
        struct socket_alloc *ei;
 
-       ei = kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
+       ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
        if (!ei)
                return NULL;
        init_waitqueue_head(&ei->socket.wait);
@@ -297,7 +247,6 @@ static struct inode *sock_alloc_inode(struct super_block *sb)
        ei->socket.ops = NULL;
        ei->socket.sk = NULL;
        ei->socket.file = NULL;
-       ei->socket.flags = 0;
 
        return &ei->vfs_inode;
 }
@@ -308,7 +257,7 @@ static void sock_destroy_inode(struct inode *inode)
                        container_of(inode, struct socket_alloc, vfs_inode));
 }
 
-static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
+static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
 {
        struct socket_alloc *ei = (struct socket_alloc *)foo;
 
@@ -356,7 +305,14 @@ static struct file_system_type sock_fs_type = {
 
 static int sockfs_delete_dentry(struct dentry *dentry)
 {
-       return 1;
+       /*
+        * At creation time, we pretended this dentry was hashed
+        * (by clearing DCACHE_UNHASHED bit in d_flags)
+        * At delete time, we restore the truth : not hashed.
+        * (so that dput() can proceed correctly)
+        */
+       dentry->d_flags |= DCACHE_UNHASHED;
+       return 0;
 }
 static struct dentry_operations sockfs_dentry_operations = {
        .d_delete = sockfs_delete_dentry,
@@ -404,16 +360,22 @@ static int sock_attach_fd(struct socket *sock, struct file *file)
 
        this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
        this.name = name;
-       this.hash = SOCK_INODE(sock)->i_ino;
+       this.hash = 0;
 
-       file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
-       if (unlikely(!file->f_dentry))
+       file->f_path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
+       if (unlikely(!file->f_path.dentry))
                return -ENOMEM;
 
-       file->f_dentry->d_op = &sockfs_dentry_operations;
-       d_add(file->f_dentry, SOCK_INODE(sock));
-       file->f_vfsmnt = mntget(sock_mnt);
-       file->f_mapping = file->f_dentry->d_inode->i_mapping;
+       file->f_path.dentry->d_op = &sockfs_dentry_operations;
+       /*
+        * We dont want to push this dentry into global dentry hash table.
+        * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
+        * This permits a working /proc/$pid/fd/XXX on sockets
+        */
+       file->f_path.dentry->d_flags &= ~DCACHE_UNHASHED;
+       d_instantiate(file->f_path.dentry, SOCK_INODE(sock));
+       file->f_path.mnt = mntget(sock_mnt);
+       file->f_mapping = file->f_path.dentry->d_inode->i_mapping;
 
        sock->file = file;
        file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
@@ -445,24 +407,11 @@ int sock_map_fd(struct socket *sock)
 
 static struct socket *sock_from_file(struct file *file, int *err)
 {
-       struct inode *inode;
-       struct socket *sock;
-
        if (file->f_op == &socket_file_ops)
                return file->private_data;      /* set in sock_map_fd */
 
-       inode = file->f_dentry->d_inode;
-       if (!S_ISSOCK(inode->i_mode)) {
-               *err = -ENOTSOCK;
-               return NULL;
-       }
-
-       sock = SOCKET_I(inode);
-       if (sock->file != file) {
-               printk(KERN_ERR "socki_lookup: socket file changed!\n");
-               sock->file = file;
-       }
-       return sock;
+       *err = -ENOTSOCK;
+       return NULL;
 }
 
 /**
@@ -708,7 +657,6 @@ static ssize_t sock_sendpage(struct file *file, struct page *page,
 }
 
 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
-                                        char __user *ubuf, size_t size,
                                         struct sock_iocb *siocb)
 {
        if (!is_sync_kiocb(iocb)) {
@@ -719,16 +667,13 @@ static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
        }
 
        siocb->kiocb = iocb;
-       siocb->async_iov.iov_base = ubuf;
-       siocb->async_iov.iov_len = size;
-
        iocb->private = siocb;
        return siocb;
 }
 
 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
-                           struct file *file, struct iovec *iov,
-                           unsigned long nr_segs)
+               struct file *file, const struct iovec *iov,
+               unsigned long nr_segs)
 {
        struct socket *sock = file->private_data;
        size_t size = 0;
@@ -748,43 +693,27 @@ static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
        return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
 }
 
-static ssize_t sock_readv(struct file *file, const struct iovec *iov,
-                         unsigned long nr_segs, loff_t *ppos)
-{
-       struct kiocb iocb;
-       struct sock_iocb siocb;
-       struct msghdr msg;
-       int ret;
-
-       init_sync_kiocb(&iocb, NULL);
-       iocb.private = &siocb;
-
-       ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
-       if (-EIOCBQUEUED == ret)
-               ret = wait_on_sync_kiocb(&iocb);
-       return ret;
-}
-
-static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
-                            size_t count, loff_t pos)
+static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
+                               unsigned long nr_segs, loff_t pos)
 {
        struct sock_iocb siocb, *x;
 
        if (pos != 0)
                return -ESPIPE;
-       if (count == 0)         /* Match SYS5 behaviour */
+
+       if (iocb->ki_left == 0) /* Match SYS5 behaviour */
                return 0;
 
-       x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
+
+       x = alloc_sock_iocb(iocb, &siocb);
        if (!x)
                return -ENOMEM;
-       return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
-                           &x->async_iov, 1);
+       return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
 }
 
 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
-                            struct file *file, struct iovec *iov,
-                            unsigned long nr_segs)
+                       struct file *file, const struct iovec *iov,
+                       unsigned long nr_segs)
 {
        struct socket *sock = file->private_data;
        size_t size = 0;
@@ -806,39 +735,22 @@ static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
        return __sock_sendmsg(iocb, sock, msg, size);
 }
 
-static ssize_t sock_writev(struct file *file, const struct iovec *iov,
-                          unsigned long nr_segs, loff_t *ppos)
-{
-       struct msghdr msg;
-       struct kiocb iocb;
-       struct sock_iocb siocb;
-       int ret;
-
-       init_sync_kiocb(&iocb, NULL);
-       iocb.private = &siocb;
-
-       ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
-       if (-EIOCBQUEUED == ret)
-               ret = wait_on_sync_kiocb(&iocb);
-       return ret;
-}
-
-static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
-                             size_t count, loff_t pos)
+static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
+                         unsigned long nr_segs, loff_t pos)
 {
        struct sock_iocb siocb, *x;
 
        if (pos != 0)
                return -ESPIPE;
-       if (count == 0)         /* Match SYS5 behaviour */
+
+       if (iocb->ki_left == 0) /* Match SYS5 behaviour */
                return 0;
 
-       x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
+       x = alloc_sock_iocb(iocb, &siocb);
        if (!x)
                return -ENOMEM;
 
-       return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
-                            &x->async_iov, 1);
+       return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
 }
 
 /*
@@ -912,7 +824,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
                        break;
                case FIOGETOWN:
                case SIOCGPGRP:
-                       err = put_user(sock->file->f_owner.pid,
+                       err = put_user(f_getown(sock->file),
                                       (int __user *)argp);
                        break;
                case SIOCGIFBR:
@@ -939,11 +851,6 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
                                err = vlan_ioctl_hook(argp);
                        mutex_unlock(&vlan_ioctl_mutex);
                        break;
-               case SIOCGIFDIVERT:
-               case SIOCSIFDIVERT:
-                       /* Convert this to call through a hook */
-                       err = divert_ioctl(cmd, argp);
-                       break;
                case SIOCADDDLCI:
                case SIOCDELDLCI:
                        err = -ENOPKG;
@@ -1138,6 +1045,7 @@ static int __sock_create(int family, int type, int protocol,
 {
        int err;
        struct socket *sock;
+       const struct net_proto_family *pf;
 
        /*
         *      Check protocol is in range
@@ -1166,6 +1074,21 @@ static int __sock_create(int family, int type, int protocol,
        if (err)
                return err;
 
+       /*
+        *      Allocate the socket and allow the family to set things up. if
+        *      the protocol is 0, the family is instructed to select an appropriate
+        *      default.
+        */
+       sock = sock_alloc();
+       if (!sock) {
+               if (net_ratelimit())
+                       printk(KERN_WARNING "socket: no more sockets\n");
+               return -ENFILE; /* Not exactly a match, but its the
+                                  closest posix thing */
+       }
+
+       sock->type = type;
+
 #if defined(CONFIG_KMOD)
        /* Attempt to load a protocol module if the find failed.
         *
@@ -1173,72 +1096,61 @@ static int __sock_create(int family, int type, int protocol,
         * requested real, full-featured networking support upon configuration.
         * Otherwise module support will break!
         */
-       if (net_families[family] == NULL) {
+       if (net_families[family] == NULL)
                request_module("net-pf-%d", family);
-       }
 #endif
 
-       net_family_read_lock();
-       if (net_families[family] == NULL) {
-               err = -EAFNOSUPPORT;
-               goto out;
-       }
-
-/*
- *     Allocate the socket and allow the family to set things up. if
- *     the protocol is 0, the family is instructed to select an appropriate
- *     default.
- */
-
-       if (!(sock = sock_alloc())) {
-               if (net_ratelimit())
-                       printk(KERN_WARNING "socket: no more sockets\n");
-               err = -ENFILE;  /* Not exactly a match, but its the
-                                  closest posix thing */
-               goto out;
-       }
-
-       sock->type = type;
+       rcu_read_lock();
+       pf = rcu_dereference(net_families[family]);
+       err = -EAFNOSUPPORT;
+       if (!pf)
+               goto out_release;
 
        /*
         * We will call the ->create function, that possibly is in a loadable
         * module, so we have to bump that loadable module refcnt first.
         */
-       err = -EAFNOSUPPORT;
-       if (!try_module_get(net_families[family]->owner))
+       if (!try_module_get(pf->owner))
                goto out_release;
 
-       if ((err = net_families[family]->create(sock, protocol)) < 0) {
-               sock->ops = NULL;
+       /* Now protected by module ref count */
+       rcu_read_unlock();
+
+       err = pf->create(sock, protocol);
+       if (err < 0)
                goto out_module_put;
-       }
 
        /*
         * Now to bump the refcnt of the [loadable] module that owns this
         * socket at sock_release time we decrement its refcnt.
         */
-       if (!try_module_get(sock->ops->owner)) {
-               sock->ops = NULL;
-               goto out_module_put;
-       }
+       if (!try_module_get(sock->ops->owner))
+               goto out_module_busy;
+
        /*
         * Now that we're done with the ->create function, the [loadable]
         * module can have its refcnt decremented
         */
-       module_put(net_families[family]->owner);
-       *res = sock;
+       module_put(pf->owner);
        err = security_socket_post_create(sock, family, type, protocol, kern);
        if (err)
                goto out_release;
+       *res = sock;
 
-out:
-       net_family_read_unlock();
-       return err;
+       return 0;
+
+out_module_busy:
+       err = -EAFNOSUPPORT;
 out_module_put:
-       module_put(net_families[family]->owner);
-out_release:
+       sock->ops = NULL;
+       module_put(pf->owner);
+out_sock_release:
        sock_release(sock);
-       goto out;
+       return err;
+
+out_release:
+       rcu_read_unlock();
+       goto out_sock_release;
 }
 
 int sock_create(int family, int type, int protocol, struct socket **res)
@@ -1376,7 +1288,7 @@ asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  *     ready for listening.
  */
 
-int sysctl_somaxconn = SOMAXCONN;
+int sysctl_somaxconn __read_mostly = SOMAXCONN;
 
 asmlinkage long sys_listen(int fd, int backlog)
 {
@@ -1602,8 +1514,9 @@ asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
        struct file *sock_file;
 
        sock_file = fget_light(fd, &fput_needed);
+       err = -EBADF;
        if (!sock_file)
-               return -EBADF;
+               goto out;
 
        sock = sock_from_file(sock_file, &err);
        if (!sock)
@@ -1630,6 +1543,7 @@ asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
 
 out_put:
        fput_light(sock_file, fput_needed);
+out:
        return err;
 }
 
@@ -1661,12 +1575,13 @@ asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
        int fput_needed;
 
        sock_file = fget_light(fd, &fput_needed);
+       err = -EBADF;
        if (!sock_file)
-               return -EBADF;
+               goto out;
 
        sock = sock_from_file(sock_file, &err);
        if (!sock)
-               goto out;
+               goto out_put;
 
        msg.msg_control = NULL;
        msg.msg_controllen = 0;
@@ -1685,8 +1600,9 @@ asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
                if (err2 < 0)
                        err = err2;
        }
-out:
+out_put:
        fput_light(sock_file, fput_needed);
+out:
        return err;
 }
 
@@ -2109,13 +2025,16 @@ asmlinkage long sys_socketcall(int call, unsigned long __user *args)
 
 #endif                         /* __ARCH_WANT_SYS_SOCKETCALL */
 
-/*
+/**
+ *     sock_register - add a socket protocol handler
+ *     @ops: description of protocol
+ *
  *     This function is called by a protocol handler that wants to
  *     advertise its address family, and have it linked into the
- *     SOCKET module.
+ *     socket interface. The value ops->family coresponds to the
+ *     socket system call protocol family.
  */
-
-int sock_register(struct net_proto_family *ops)
+int sock_register(const struct net_proto_family *ops)
 {
        int err;
 
@@ -2124,33 +2043,44 @@ int sock_register(struct net_proto_family *ops)
                       NPROTO);
                return -ENOBUFS;
        }
-       net_family_write_lock();
-       err = -EEXIST;
-       if (net_families[ops->family] == NULL) {
+
+       spin_lock(&net_family_lock);
+       if (net_families[ops->family])
+               err = -EEXIST;
+       else {
                net_families[ops->family] = ops;
                err = 0;
        }
-       net_family_write_unlock();
+       spin_unlock(&net_family_lock);
+
        printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
        return err;
 }
 
-/*
+/**
+ *     sock_unregister - remove a protocol handler
+ *     @family: protocol family to remove
+ *
  *     This function is called by a protocol handler that wants to
  *     remove its address family, and have it unlinked from the
- *     SOCKET module.
+ *     new socket creation.
+ *
+ *     If protocol handler is a module, then it can use module reference
+ *     counts to protect against new references. If protocol handler is not
+ *     a module then it needs to provide its own protection in
+ *     the ops->create routine.
  */
-
-int sock_unregister(int family)
+void sock_unregister(int family)
 {
-       if (family < 0 || family >= NPROTO)
-               return -1;
+       BUG_ON(family < 0 || family >= NPROTO);
 
-       net_family_write_lock();
+       spin_lock(&net_family_lock);
        net_families[family] = NULL;
-       net_family_write_unlock();
+       spin_unlock(&net_family_lock);
+
+       synchronize_rcu();
+
        printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
-       return 0;
 }
 
 static int __init sock_init(void)
@@ -2250,7 +2180,7 @@ done:
 }
 
 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
-                   int flags)
+                  int flags)
 {
        return sock->ops->connect(sock, addr, addrlen, flags);
 }