]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - net/unix/af_unix.c
[PATCH] POLLRDHUP/EPOLLRDHUP handling for half-closed devices notifications
[linux-2.6-omap-h63xx.git] / net / unix / af_unix.c
index acc73ba8bade52770d1babc0bbf19e8abfe1cbef..d901465ce0135093276d55adabbb7691591097c3 100644 (file)
 int sysctl_unix_max_dgram_qlen = 10;
 
 struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
-DEFINE_RWLOCK(unix_table_lock);
+DEFINE_SPINLOCK(unix_table_lock);
 static atomic_t unix_nr_socks = ATOMIC_INIT(0);
 
 #define unix_sockets_unbound   (&unix_socket_table[UNIX_HASH_SIZE])
@@ -130,7 +130,7 @@ static atomic_t unix_nr_socks = ATOMIC_INIT(0);
 
 /*
  *  SMP locking strategy:
- *    hash table is protected with rwlock unix_table_lock
+ *    hash table is protected with spinlock unix_table_lock
  *    each socket state is protected by separate rwlock.
  */
 
@@ -214,16 +214,16 @@ static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
 
 static inline void unix_remove_socket(struct sock *sk)
 {
-       write_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
        __unix_remove_socket(sk);
-       write_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
 }
 
 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
 {
-       write_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
        __unix_insert_socket(list, sk);
-       write_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
 }
 
 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
@@ -250,11 +250,11 @@ static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
 {
        struct sock *s;
 
-       read_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
        s = __unix_find_socket_byname(sunname, len, type, hash);
        if (s)
                sock_hold(s);
-       read_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
        return s;
 }
 
@@ -263,7 +263,7 @@ static struct sock *unix_find_socket_byinode(struct inode *i)
        struct sock *s;
        struct hlist_node *node;
 
-       read_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
        sk_for_each(s, node,
                    &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
                struct dentry *dentry = unix_sk(s)->dentry;
@@ -276,7 +276,7 @@ static struct sock *unix_find_socket_byinode(struct inode *i)
        }
        s = NULL;
 found:
-       read_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
        return s;
 }
 
@@ -473,7 +473,7 @@ static int unix_dgram_connect(struct socket *, struct sockaddr *,
 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
                                  struct msghdr *, size_t);
 
-static struct proto_ops unix_stream_ops = {
+static const struct proto_ops unix_stream_ops = {
        .family =       PF_UNIX,
        .owner =        THIS_MODULE,
        .release =      unix_release,
@@ -494,7 +494,7 @@ static struct proto_ops unix_stream_ops = {
        .sendpage =     sock_no_sendpage,
 };
 
-static struct proto_ops unix_dgram_ops = {
+static const struct proto_ops unix_dgram_ops = {
        .family =       PF_UNIX,
        .owner =        THIS_MODULE,
        .release =      unix_release,
@@ -515,7 +515,7 @@ static struct proto_ops unix_dgram_ops = {
        .sendpage =     sock_no_sendpage,
 };
 
-static struct proto_ops unix_seqpacket_ops = {
+static const struct proto_ops unix_seqpacket_ops = {
        .family =       PF_UNIX,
        .owner =        THIS_MODULE,
        .release =      unix_release,
@@ -547,7 +547,7 @@ static struct sock * unix_create1(struct socket *sock)
        struct sock *sk = NULL;
        struct unix_sock *u;
 
-       if (atomic_read(&unix_nr_socks) >= 2*files_stat.max_files)
+       if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
                goto out;
 
        sk = sk_alloc(PF_UNIX, GFP_KERNEL, &unix_proto, 1);
@@ -564,9 +564,9 @@ static struct sock * unix_create1(struct socket *sock)
        u         = unix_sk(sk);
        u->dentry = NULL;
        u->mnt    = NULL;
-       rwlock_init(&u->lock);
+       spin_lock_init(&u->lock);
        atomic_set(&u->inflight, sock ? 0 : -1);
-       init_MUTEX(&u->readsem); /* single task reading lock */
+       mutex_init(&u->readlock); /* single task reading lock */
        init_waitqueue_head(&u->peer_wait);
        unix_insert_socket(unix_sockets_unbound, sk);
 out:
@@ -623,7 +623,7 @@ static int unix_autobind(struct socket *sock)
        struct unix_address * addr;
        int err;
 
-       down(&u->readsem);
+       mutex_lock(&u->readlock);
 
        err = 0;
        if (u->addr)
@@ -642,12 +642,12 @@ retry:
        addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
        addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
 
-       write_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
        ordernum = (ordernum+1)&0xFFFFF;
 
        if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
                                      addr->hash)) {
-               write_unlock(&unix_table_lock);
+               spin_unlock(&unix_table_lock);
                /* Sanity yield. It is unusual case, but yet... */
                if (!(ordernum&0xFF))
                        yield();
@@ -658,10 +658,10 @@ retry:
        __unix_remove_socket(sk);
        u->addr = addr;
        __unix_insert_socket(&unix_socket_table[addr->hash], sk);
-       write_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
        err = 0;
 
-out:   up(&u->readsem);
+out:   mutex_unlock(&u->readlock);
        return err;
 }
 
@@ -744,7 +744,7 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
                goto out;
        addr_len = err;
 
-       down(&u->readsem);
+       mutex_lock(&u->readlock);
 
        err = -EINVAL;
        if (u->addr)
@@ -784,14 +784,14 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
                err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
                if (err)
                        goto out_mknod_dput;
-               up(&nd.dentry->d_inode->i_sem);
+               mutex_unlock(&nd.dentry->d_inode->i_mutex);
                dput(nd.dentry);
                nd.dentry = dentry;
 
                addr->hash = UNIX_HASH_SIZE;
        }
 
-       write_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
 
        if (!sunaddr->sun_path[0]) {
                err = -EADDRINUSE;
@@ -814,16 +814,16 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        __unix_insert_socket(list, sk);
 
 out_unlock:
-       write_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
 out_up:
-       up(&u->readsem);
+       mutex_unlock(&u->readlock);
 out:
        return err;
 
 out_mknod_dput:
        dput(dentry);
 out_mknod_unlock:
-       up(&nd.dentry->d_inode->i_sem);
+       mutex_unlock(&nd.dentry->d_inode->i_mutex);
        path_release(&nd);
 out_mknod_parent:
        if (err==-EEXIST)
@@ -1063,10 +1063,12 @@ restart:
        /* Set credentials */
        sk->sk_peercred = other->sk_peercred;
 
-       sock_hold(newsk);
-       unix_peer(sk)   = newsk;
        sock->state     = SS_CONNECTED;
        sk->sk_state    = TCP_ESTABLISHED;
+       sock_hold(newsk);
+
+       smp_mb__after_atomic_inc();     /* sock_hold() does an atomic_inc() */
+       unix_peer(sk)   = newsk;
 
        unix_state_wunlock(sk);
 
@@ -1414,7 +1416,7 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
        } else {
                sunaddr = NULL;
                err = -ENOTCONN;
-               other = unix_peer_get(sk);
+               other = unix_peer(sk);
                if (!other)
                        goto out_err;
        }
@@ -1425,15 +1427,15 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
        while(sent < len)
        {
                /*
-                *      Optimisation for the fact that under 0.01% of X messages typically
-                *      need breaking up.
+                *      Optimisation for the fact that under 0.01% of X
+                *      messages typically need breaking up.
                 */
 
-               size=len-sent;
+               size = len-sent;
 
                /* Keep two messages in the pipe so it schedules better */
-               if (size > sk->sk_sndbuf / 2 - 64)
-                       size = sk->sk_sndbuf / 2 - 64;
+               if (size > ((sk->sk_sndbuf >> 1) - 64))
+                       size = (sk->sk_sndbuf >> 1) - 64;
 
                if (size > SKB_MAX_ALLOC)
                        size = SKB_MAX_ALLOC;
@@ -1476,7 +1478,6 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
                other->sk_data_ready(other, size);
                sent+=size;
        }
-       sock_put(other);
 
        scm_destroy(siocb->scm);
        siocb->scm = NULL;
@@ -1491,8 +1492,6 @@ pipe_err:
                send_sig(SIGPIPE,current,0);
        err = -EPIPE;
 out_err:
-        if (other)
-               sock_put(other);
        scm_destroy(siocb->scm);
        siocb->scm = NULL;
        return sent ? : err;
@@ -1546,7 +1545,7 @@ static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
 
        msg->msg_namelen = 0;
 
-       down(&u->readsem);
+       mutex_lock(&u->readlock);
 
        skb = skb_recv_datagram(sk, flags, noblock, &err);
        if (!skb)
@@ -1601,7 +1600,7 @@ static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
 out_free:
        skb_free_datagram(sk,skb);
 out_unlock:
-       up(&u->readsem);
+       mutex_unlock(&u->readlock);
 out:
        return err;
 }
@@ -1677,7 +1676,7 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
                memset(&tmp_scm, 0, sizeof(tmp_scm));
        }
 
-       down(&u->readsem);
+       mutex_lock(&u->readlock);
 
        do
        {
@@ -1701,7 +1700,7 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
                        err = -EAGAIN;
                        if (!timeo)
                                break;
-                       up(&u->readsem);
+                       mutex_unlock(&u->readlock);
 
                        timeo = unix_stream_data_wait(sk, timeo);
 
@@ -1709,7 +1708,7 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
                                err = sock_intr_errno(timeo);
                                goto out;
                        }
-                       down(&u->readsem);
+                       mutex_lock(&u->readlock);
                        continue;
                }
 
@@ -1775,7 +1774,7 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
                }
        } while (size);
 
-       up(&u->readsem);
+       mutex_unlock(&u->readlock);
        scm_recv(sock, msg, siocb->scm, flags);
 out:
        return copied ? : err;
@@ -1860,7 +1859,7 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                }
 
                default:
-                       err = dev_ioctl(cmd, (void __user *)arg);
+                       err = -ENOIOCTLCMD;
                        break;
        }
        return err;
@@ -1879,6 +1878,8 @@ static unsigned int unix_poll(struct file * file, struct socket *sock, poll_tabl
                mask |= POLLERR;
        if (sk->sk_shutdown == SHUTDOWN_MASK)
                mask |= POLLHUP;
+       if (sk->sk_shutdown & RCV_SHUTDOWN)
+               mask |= POLLRDHUP;
 
        /* readable? */
        if (!skb_queue_empty(&sk->sk_receive_queue) ||
@@ -1917,7 +1918,7 @@ static struct sock *unix_seq_idx(int *iter, loff_t pos)
 
 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
 {
-       read_lock(&unix_table_lock);
+       spin_lock(&unix_table_lock);
        return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
 }
 
@@ -1932,7 +1933,7 @@ static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 
 static void unix_seq_stop(struct seq_file *seq, void *v)
 {
-       read_unlock(&unix_table_lock);
+       spin_unlock(&unix_table_lock);
 }
 
 static int unix_seq_show(struct seq_file *seq, void *v)