netfiltr: ipt_CLUSTERIP: simplify seq_file codeA
[safe/jmp/linux-2.6] / net / atm / resources.c
index 415d261..56b7322 100644 (file)
@@ -8,7 +8,6 @@
  *          use the default destruct function initialized by sock_init_data */
 
 
-#include <linux/config.h>
 #include <linux/ctype.h>
 #include <linux/string.h>
 #include <linux/atmdev.h>
 #include <linux/kernel.h> /* for barrier */
 #include <linux/module.h>
 #include <linux/bitops.h>
+#include <linux/capability.h>
 #include <linux/delay.h>
+#include <linux/mutex.h>
+
 #include <net/sock.h>   /* for struct sock */
 
 #include "common.h"
 
 
 LIST_HEAD(atm_devs);
-DEFINE_SPINLOCK(atm_dev_lock);
+DEFINE_MUTEX(atm_dev_mutex);
 
 static struct atm_dev *__alloc_atm_dev(const char *type)
 {
        struct atm_dev *dev;
 
-       dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+       dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                return NULL;
-       memset(dev, 0, sizeof(*dev));
        dev->type = type;
        dev->signal = ATM_PHY_SIG_UNKNOWN;
        dev->link_rate = ATM_OC3_PCR;
@@ -52,7 +53,7 @@ static struct atm_dev *__atm_dev_lookup(int number)
 
        list_for_each(p, &atm_devs) {
                dev = list_entry(p, struct atm_dev, dev_list);
-               if ((dev->ops) && (dev->number == number)) {
+               if (dev->number == number) {
                        atm_dev_hold(dev);
                        return dev;
                }
@@ -64,12 +65,13 @@ struct atm_dev *atm_dev_lookup(int number)
 {
        struct atm_dev *dev;
 
-       spin_lock(&atm_dev_lock);
+       mutex_lock(&atm_dev_mutex);
        dev = __atm_dev_lookup(number);
-       spin_unlock(&atm_dev_lock);
+       mutex_unlock(&atm_dev_mutex);
        return dev;
 }
 
+
 struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
                                 int number, unsigned long *flags)
 {
@@ -81,11 +83,11 @@ struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
                    type);
                return NULL;
        }
-       spin_lock(&atm_dev_lock);
+       mutex_lock(&atm_dev_mutex);
        if (number != -1) {
                if ((inuse = __atm_dev_lookup(number))) {
                        atm_dev_put(inuse);
-                       spin_unlock(&atm_dev_lock);
+                       mutex_unlock(&atm_dev_mutex);
                        kfree(dev);
                        return NULL;
                }
@@ -105,57 +107,54 @@ struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
                memset(&dev->flags, 0, sizeof(dev->flags));
        memset(&dev->stats, 0, sizeof(dev->stats));
        atomic_set(&dev->refcnt, 1);
-       list_add_tail(&dev->dev_list, &atm_devs);
-       spin_unlock(&atm_dev_lock);
 
        if (atm_proc_dev_register(dev) < 0) {
                printk(KERN_ERR "atm_dev_register: "
                       "atm_proc_dev_register failed for dev %s\n",
                       type);
-               spin_lock(&atm_dev_lock);
-               list_del(&dev->dev_list);
-               spin_unlock(&atm_dev_lock);
-               kfree(dev);
-               return NULL;
+               goto out_fail;
+       }
+
+       if (atm_register_sysfs(dev) < 0) {
+               printk(KERN_ERR "atm_dev_register: "
+                      "atm_register_sysfs failed for dev %s\n",
+                      type);
+               atm_proc_dev_deregister(dev);
+               goto out_fail;
        }
 
+       list_add_tail(&dev->dev_list, &atm_devs);
+
+out:
+       mutex_unlock(&atm_dev_mutex);
        return dev;
+
+out_fail:
+       kfree(dev);
+       dev = NULL;
+       goto out;
 }
 
 
 void atm_dev_deregister(struct atm_dev *dev)
 {
-       unsigned long warning_time;
-
-       atm_proc_dev_deregister(dev);
-
-       spin_lock(&atm_dev_lock);
+       BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags));
+       set_bit(ATM_DF_REMOVED, &dev->flags);
+
+       /*
+        * if we remove current device from atm_devs list, new device
+        * with same number can appear, such we need deregister proc,
+        * release async all vccs and remove them from vccs list too
+        */
+       mutex_lock(&atm_dev_mutex);
        list_del(&dev->dev_list);
-       spin_unlock(&atm_dev_lock);
-
-        warning_time = jiffies;
-        while (atomic_read(&dev->refcnt) != 1) {
-                msleep(250);
-                if ((jiffies - warning_time) > 10 * HZ) {
-                        printk(KERN_EMERG "atm_dev_deregister: waiting for "
-                               "dev %d to become free. Usage count = %d\n",
-                               dev->number, atomic_read(&dev->refcnt));
-                        warning_time = jiffies;
-                }
-        }
+       mutex_unlock(&atm_dev_mutex);
 
-       kfree(dev);
-}
+       atm_dev_release_vccs(dev);
+       atm_unregister_sysfs(dev);
+       atm_proc_dev_deregister(dev);
 
-void shutdown_atm_dev(struct atm_dev *dev)
-{
-       if (atomic_read(&dev->refcnt) > 1) {
-               set_bit(ATM_DF_CLOSE, &dev->flags);
-               return;
-       }
-       if (dev->ops->dev_close)
-               dev->ops->dev_close(dev);
-       atm_dev_deregister(dev);
+       atm_dev_put(dev);
 }
 
 
@@ -196,31 +195,50 @@ static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, in
 }
 
 
-int atm_dev_ioctl(unsigned int cmd, void __user *arg)
+int atm_dev_ioctl(unsigned int cmd, void __user *arg, int compat)
 {
        void __user *buf;
        int error, len, number, size = 0;
        struct atm_dev *dev;
        struct list_head *p;
        int *tmp_buf, *tmp_p;
-       struct atm_iobuf __user *iobuf = arg;
-       struct atmif_sioc __user *sioc = arg;
+       int __user *sioc_len;
+       int __user *iobuf_len;
+
+#ifndef CONFIG_COMPAT
+       compat = 0; /* Just so the compiler _knows_ */
+#endif
+
        switch (cmd) {
                case ATM_GETNAMES:
-                       if (get_user(buf, &iobuf->buffer))
-                               return -EFAULT;
-                       if (get_user(len, &iobuf->length))
+
+                       if (compat) {
+#ifdef CONFIG_COMPAT
+                               struct compat_atm_iobuf __user *ciobuf = arg;
+                               compat_uptr_t cbuf;
+                               iobuf_len = &ciobuf->length;
+                               if (get_user(cbuf, &ciobuf->buffer))
+                                       return -EFAULT;
+                               buf = compat_ptr(cbuf);
+#endif
+                       } else {
+                               struct atm_iobuf __user *iobuf = arg;
+                               iobuf_len = &iobuf->length;
+                               if (get_user(buf, &iobuf->buffer))
+                                       return -EFAULT;
+                       }
+                       if (get_user(len, iobuf_len))
                                return -EFAULT;
-                       spin_lock(&atm_dev_lock);
+                       mutex_lock(&atm_dev_mutex);
                        list_for_each(p, &atm_devs)
                                size += sizeof(int);
                        if (size > len) {
-                               spin_unlock(&atm_dev_lock);
+                               mutex_unlock(&atm_dev_mutex);
                                return -E2BIG;
                        }
                        tmp_buf = kmalloc(size, GFP_ATOMIC);
                        if (!tmp_buf) {
-                               spin_unlock(&atm_dev_lock);
+                               mutex_unlock(&atm_dev_mutex);
                                return -ENOMEM;
                        }
                        tmp_p = tmp_buf;
@@ -228,9 +246,9 @@ int atm_dev_ioctl(unsigned int cmd, void __user *arg)
                                dev = list_entry(p, struct atm_dev, dev_list);
                                *tmp_p++ = dev->number;
                        }
-                       spin_unlock(&atm_dev_lock);
-                       error = ((copy_to_user(buf, tmp_buf, size)) ||
-                                       put_user(size, &iobuf->length))
+                       mutex_unlock(&atm_dev_mutex);
+                       error = ((copy_to_user(buf, tmp_buf, size)) ||
+                                       put_user(size, iobuf_len))
                                                ? -EFAULT : 0;
                        kfree(tmp_buf);
                        return error;
@@ -238,16 +256,36 @@ int atm_dev_ioctl(unsigned int cmd, void __user *arg)
                        break;
        }
 
-       if (get_user(buf, &sioc->arg))
-               return -EFAULT;
-       if (get_user(len, &sioc->length))
-               return -EFAULT;
-       if (get_user(number, &sioc->number))
-               return -EFAULT;
-
-       if (!(dev = atm_dev_lookup(number)))
+       if (compat) {
+#ifdef CONFIG_COMPAT
+               struct compat_atmif_sioc __user *csioc = arg;
+               compat_uptr_t carg;
+
+               sioc_len = &csioc->length;
+               if (get_user(carg, &csioc->arg))
+                       return -EFAULT;
+               buf = compat_ptr(carg);
+
+               if (get_user(len, &csioc->length))
+                       return -EFAULT;
+               if (get_user(number, &csioc->number))
+                       return -EFAULT;
+#endif
+       } else {
+               struct atmif_sioc __user *sioc = arg;
+
+               sioc_len = &sioc->length;
+               if (get_user(buf, &sioc->arg))
+                       return -EFAULT;
+               if (get_user(len, &sioc->length))
+                       return -EFAULT;
+               if (get_user(number, &sioc->number))
+                       return -EFAULT;
+       }
+       if (!(dev = try_then_request_module(atm_dev_lookup(number),
+                                           "atm-device-%d", number)))
                return -ENODEV;
-       
+
        switch (cmd) {
                case ATM_GETTYPE:
                        size = strlen(dev->type) + 1;
@@ -358,7 +396,7 @@ int atm_dev_ioctl(unsigned int cmd, void __user *arg)
                        size = error;
                        /* may return 0, but later on size == 0 means "don't
                           write the length" */
-                       error = put_user(size, &sioc->length)
+                       error = put_user(size, sioc_len)
                                ? -EFAULT : 0;
                        goto done;
                case ATM_SETLOOP:
@@ -380,19 +418,29 @@ int atm_dev_ioctl(unsigned int cmd, void __user *arg)
                        }
                        /* fall through */
                default:
-                       if (!dev->ops->ioctl) {
-                               error = -EINVAL;
-                               goto done;
+                       if (compat) {
+#ifdef CONFIG_COMPAT
+                               if (!dev->ops->compat_ioctl) {
+                                       error = -EINVAL;
+                                       goto done;
+                               }
+                               size = dev->ops->compat_ioctl(dev, cmd, buf);
+#endif
+                       } else {
+                               if (!dev->ops->ioctl) {
+                                       error = -EINVAL;
+                                       goto done;
+                               }
+                               size = dev->ops->ioctl(dev, cmd, buf);
                        }
-                       size = dev->ops->ioctl(dev, cmd, buf);
                        if (size < 0) {
                                error = (size == -ENOIOCTLCMD ? -EINVAL : size);
                                goto done;
                        }
        }
-       
+
        if (size)
-               error = put_user(size, &sioc->length)
+               error = put_user(size, sioc_len)
                        ? -EFAULT : 0;
        else
                error = 0;
@@ -414,19 +462,20 @@ static __inline__ void *dev_get_idx(loff_t left)
 
 void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
 {
-       spin_lock(&atm_dev_lock);
-       return *pos ? dev_get_idx(*pos) : (void *) 1;
+       mutex_lock(&atm_dev_mutex);
+       return *pos ? dev_get_idx(*pos) : SEQ_START_TOKEN;
 }
 
 void atm_dev_seq_stop(struct seq_file *seq, void *v)
 {
-       spin_unlock(&atm_dev_lock);
+       mutex_unlock(&atm_dev_mutex);
 }
+
 void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
        ++*pos;
-       v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
+       v = (v == SEQ_START_TOKEN)
+               ? atm_devs.next : ((struct list_head *)v)->next;
        return (v == &atm_devs) ? NULL : v;
 }
 
@@ -434,4 +483,3 @@ void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 EXPORT_SYMBOL(atm_dev_register);
 EXPORT_SYMBOL(atm_dev_deregister);
 EXPORT_SYMBOL(atm_dev_lookup);
-EXPORT_SYMBOL(shutdown_atm_dev);