Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / net / atm / common.c
index 83454e1..950bd16 100644 (file)
@@ -3,7 +3,6 @@
 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
 
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kmod.h>
 #include <linux/net.h>         /* struct socket, struct proto_ops */
 #include "addr.h"              /* address registry */
 #include "signaling.h"         /* for WAITING and sigd_attach */
 
-
-#if 0
-#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
-#else
-#define DPRINTK(format,args...)
-#endif
-
 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
 DEFINE_RWLOCK(vcc_sklist_lock);
 
@@ -70,15 +62,15 @@ static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
        struct sk_buff *skb;
        struct sock *sk = sk_atm(vcc);
 
-       if (atomic_read(&sk->sk_wmem_alloc) && !atm_may_send(vcc, size)) {
-               DPRINTK("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
-                       atomic_read(&sk->sk_wmem_alloc), size,
+       if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
+               pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
+                       sk_wmem_alloc_get(sk), size,
                        sk->sk_sndbuf);
                return NULL;
        }
-       while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
-       DPRINTK("AlTx %d += %d\n", atomic_read(&sk->sk_wmem_alloc),
-               skb->truesize);
+       while (!(skb = alloc_skb(size, GFP_KERNEL)))
+               schedule();
+       pr_debug("AlTx %d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
        atomic_add(skb->truesize, &sk->sk_wmem_alloc);
        return skb;
 }
@@ -100,7 +92,7 @@ static void vcc_sock_destruct(struct sock *sk)
 static void vcc_def_wakeup(struct sock *sk)
 {
        read_lock(&sk->sk_callback_lock);
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+       if (sk_has_sleeper(sk))
                wake_up(sk->sk_sleep);
        read_unlock(&sk->sk_callback_lock);
 }
@@ -110,18 +102,18 @@ static inline int vcc_writable(struct sock *sk)
        struct atm_vcc *vcc = atm_sk(sk);
 
        return (vcc->qos.txtp.max_sdu +
-               atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
+               atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
 }
 
 static void vcc_write_space(struct sock *sk)
-{       
+{
        read_lock(&sk->sk_callback_lock);
 
        if (vcc_writable(sk)) {
-               if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+               if (sk_has_sleeper(sk))
                        wake_up_interruptible(sk->sk_sleep);
 
-               sk_wake_async(sk, 2, POLL_OUT);
+               sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
        }
 
        read_unlock(&sk->sk_callback_lock);
@@ -132,8 +124,8 @@ static struct proto vcc_proto = {
        .owner    = THIS_MODULE,
        .obj_size = sizeof(struct atm_vcc),
 };
-int vcc_create(struct socket *sock, int protocol, int family)
+
+int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
 {
        struct sock *sk;
        struct atm_vcc *vcc;
@@ -141,7 +133,7 @@ int vcc_create(struct socket *sock, int protocol, int family)
        sock->sk = NULL;
        if (sock->type == SOCK_STREAM)
                return -EINVAL;
-       sk = sk_alloc(family, GFP_KERNEL, &vcc_proto, 1);
+       sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
        if (!sk)
                return -ENOMEM;
        sock_init_data(sock, sk);
@@ -153,7 +145,7 @@ int vcc_create(struct socket *sock, int protocol, int family)
        memset(&vcc->local,0,sizeof(struct sockaddr_atmsvc));
        memset(&vcc->remote,0,sizeof(struct sockaddr_atmsvc));
        vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
-       atomic_set(&sk->sk_wmem_alloc, 0);
+       atomic_set(&sk->sk_wmem_alloc, 1);
        atomic_set(&sk->sk_rmem_alloc, 0);
        vcc->push = NULL;
        vcc->pop = NULL;
@@ -221,6 +213,29 @@ void vcc_release_async(struct atm_vcc *vcc, int reply)
 EXPORT_SYMBOL(vcc_release_async);
 
 
+void atm_dev_release_vccs(struct atm_dev *dev)
+{
+       int i;
+
+       write_lock_irq(&vcc_sklist_lock);
+       for (i = 0; i < VCC_HTABLE_SIZE; i++) {
+               struct hlist_head *head = &vcc_hash[i];
+               struct hlist_node *node, *tmp;
+               struct sock *s;
+               struct atm_vcc *vcc;
+
+               sk_for_each_safe(s, node, tmp, head) {
+                       vcc = atm_sk(s);
+                       if (vcc->dev == dev) {
+                               vcc_release_async(vcc, -EPIPE);
+                               sk_del_node_init(s);
+                       }
+               }
+       }
+       write_unlock_irq(&vcc_sklist_lock);
+}
+
+
 static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
 {
        int max_sdu;
@@ -247,7 +262,7 @@ static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
 }
 
 
-static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
+static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
 {
        struct hlist_head *head = &vcc_hash[vci &
                                        (VCC_HTABLE_SIZE - 1)];
@@ -275,7 +290,7 @@ static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
 }
 
 
-static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
+static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
 {
        static short p;        /* poor man's per-device cache */
        static int c;
@@ -332,12 +347,13 @@ static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
                return -EINVAL;
        if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
                return -EPERM;
-       error = 0;
+       error = -ENODEV;
        if (!try_module_get(dev->ops->owner))
-               return -ENODEV;
+               return error;
        vcc->dev = dev;
        write_lock_irq(&vcc_sklist_lock);
-       if ((error = find_ci(vcc, &vpi, &vci))) {
+       if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
+           (error = find_ci(vcc, &vpi, &vci))) {
                write_unlock_irq(&vcc_sklist_lock);
                goto fail_module_put;
        }
@@ -369,10 +385,10 @@ static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
        if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
        if (error)
                goto fail;
-       DPRINTK("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
-       DPRINTK("  TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
+       pr_debug("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
+       pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
            vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
-       DPRINTK("  RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
+       pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
            vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
 
        if (dev->ops->open) {
@@ -397,7 +413,7 @@ int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
        struct atm_vcc *vcc = ATM_SD(sock);
        int error;
 
-       DPRINTK("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
+       pr_debug("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
        if (sock->state == SS_CONNECTED)
                return -EISCONN;
        if (sock->state != SS_UNCONNECTED)
@@ -410,7 +426,7 @@ int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
        else
                if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
                        return -EINVAL;
-       DPRINTK("vcc_connect (TX: cl %d,bw %d-%d,sdu %d; "
+       pr_debug("vcc_connect (TX: cl %d,bw %d-%d,sdu %d; "
            "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
            vcc->qos.txtp.traffic_class,vcc->qos.txtp.min_pcr,
            vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu,
@@ -424,15 +440,15 @@ int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
            vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
                return -EINVAL;
        if (likely(itf != ATM_ITF_ANY)) {
-               dev = atm_dev_lookup(itf);
+               dev = try_then_request_module(atm_dev_lookup(itf), "atm-device-%d", itf);
        } else {
                dev = NULL;
-               spin_lock(&atm_dev_lock);
+               mutex_lock(&atm_dev_mutex);
                if (!list_empty(&atm_devs)) {
                        dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
                        atm_dev_hold(dev);
                }
-               spin_unlock(&atm_dev_lock);
+               mutex_unlock(&atm_dev_mutex);
        }
        if (!dev)
                return -ENODEV;
@@ -471,20 +487,20 @@ int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
        if (!skb)
                return error;
 
-       copied = skb->len; 
+       copied = skb->len;
        if (copied > size) {
-               copied = size; 
+               copied = size;
                msg->msg_flags |= MSG_TRUNC;
        }
 
-        error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
-        if (error)
-                return error;
-        sock_recv_timestamp(msg, sk, skb);
-        DPRINTK("RcvM %d -= %d\n", atomic_read(&sk->rmem_alloc), skb->truesize);
-        atm_return(vcc, skb->truesize);
-        skb_free_datagram(sk, skb);
-        return copied;
+       error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
+       if (error)
+               return error;
+       sock_recv_timestamp(msg, sk, skb);
+       pr_debug("RcvM %d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
+       atm_return(vcc, skb->truesize);
+       skb_free_datagram(sk, skb);
+       return copied;
 }
 
 
@@ -578,7 +594,7 @@ unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
        struct atm_vcc *vcc;
        unsigned int mask;
 
-       poll_wait(file, sk->sk_sleep, wait);
+       sock_poll_wait(file, sk->sk_sleep, wait);
        mask = 0;
 
        vcc = ATM_SD(sock);
@@ -630,7 +646,7 @@ static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
 }
 
 
-static int check_tp(struct atm_trafprm *tp)
+static int check_tp(const struct atm_trafprm *tp)
 {
        /* @@@ Should be merged with adjust_tp */
        if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
@@ -647,12 +663,12 @@ static int check_tp(struct atm_trafprm *tp)
 }
 
 
-static int check_qos(struct atm_qos *qos)
+static int check_qos(const struct atm_qos *qos)
 {
        int error;
 
        if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
-                return -EINVAL;
+               return -EINVAL;
        if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
            qos->txtp.traffic_class && qos->rxtp.traffic_class &&
            qos->txtp.traffic_class != ATM_ANYCLASS &&
@@ -663,7 +679,7 @@ static int check_qos(struct atm_qos *qos)
 }
 
 int vcc_setsockopt(struct socket *sock, int level, int optname,
-                  char __user *optval, int optlen)
+                  char __user *optval, unsigned int optlen)
 {
        struct atm_vcc *vcc;
        unsigned long value;
@@ -763,12 +779,18 @@ static int __init atm_init(void)
                printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
                goto out_atmpvc_exit;
        }
-        if ((error = atm_proc_init()) < 0) {
+       if ((error = atm_proc_init()) < 0) {
                printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
                goto out_atmsvc_exit;
        }
+       if ((error = atm_sysfs_init()) < 0) {
+               printk(KERN_ERR "atm_sysfs_init() failed with %d\n",error);
+               goto out_atmproc_exit;
+       }
 out:
        return error;
+out_atmproc_exit:
+       atm_proc_exit();
 out_atmsvc_exit:
        atmsvc_exit();
 out_atmpvc_exit:
@@ -781,12 +803,14 @@ out_unregister_vcc_proto:
 static void __exit atm_exit(void)
 {
        atm_proc_exit();
+       atm_sysfs_exit();
        atmsvc_exit();
        atmpvc_exit();
        proto_unregister(&vcc_proto);
 }
 
-module_init(atm_init);
+subsys_initcall(atm_init);
+
 module_exit(atm_exit);
 
 MODULE_LICENSE("GPL");