net: Avoid extra wakeups of threads blocked in wait_for_packet()
[safe/jmp/linux-2.6] / net / core / net-sysfs.c
index 221a64a..2da59a0 100644 (file)
@@ -18,6 +18,9 @@
 #include <linux/wireless.h>
 #include <net/iw_handler.h>
 
+#include "net-sysfs.h"
+
+#ifdef CONFIG_SYSFS
 static const char fmt_hex[] = "%#x\n";
 static const char fmt_long_hex[] = "%#lx\n";
 static const char fmt_dec[] = "%d\n";
@@ -74,7 +77,9 @@ static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
        if (endp == buf)
                goto err;
 
-       rtnl_lock();
+       if (!rtnl_trylock())
+               return -ERESTARTSYS;
+
        if (dev_isalive(net)) {
                if ((ret = (*set)(net, new)) == 0)
                        ret = len;
@@ -84,6 +89,7 @@ static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
        return ret;
 }
 
+NETDEVICE_SHOW(dev_id, fmt_hex);
 NETDEVICE_SHOW(addr_len, fmt_dec);
 NETDEVICE_SHOW(iflink, fmt_dec);
 NETDEVICE_SHOW(ifindex, fmt_dec);
@@ -92,17 +98,6 @@ NETDEVICE_SHOW(type, fmt_dec);
 NETDEVICE_SHOW(link_mode, fmt_dec);
 
 /* use same locking rules as GIFHWADDR ioctl's */
-static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
-{
-       int i;
-       char *cp = buf;
-
-       for (i = 0; i < len; i++)
-               cp += sprintf(cp, "%02x%c", addr[i],
-                             i == (len - 1) ? '\n' : ':');
-       return cp - buf;
-}
-
 static ssize_t show_address(struct device *dev, struct device_attribute *attr,
                            char *buf)
 {
@@ -111,7 +106,7 @@ static ssize_t show_address(struct device *dev, struct device_attribute *attr,
 
        read_lock(&dev_base_lock);
        if (dev_isalive(net))
-           ret = format_addr(buf, net->dev_addr, net->addr_len);
+               ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
        read_unlock(&dev_base_lock);
        return ret;
 }
@@ -121,7 +116,7 @@ static ssize_t show_broadcast(struct device *dev,
 {
        struct net_device *net = to_net_dev(dev);
        if (dev_isalive(net))
-               return format_addr(buf, net->broadcast, net->addr_len);
+               return sysfs_format_mac(buf, net->broadcast, net->addr_len);
        return -EINVAL;
 }
 
@@ -216,22 +211,44 @@ static ssize_t store_tx_queue_len(struct device *dev,
        return netdev_store(dev, attr, buf, len, change_tx_queue_len);
 }
 
-NETDEVICE_SHOW(weight, fmt_dec);
-
-static int change_weight(struct net_device *net, unsigned long new_weight)
+static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
+                            const char *buf, size_t len)
 {
-       net->weight = new_weight;
-       return 0;
+       struct net_device *netdev = to_net_dev(dev);
+       size_t count = len;
+       ssize_t ret;
+
+       if (!capable(CAP_NET_ADMIN))
+               return -EPERM;
+
+       /* ignore trailing newline */
+       if (len >  0 && buf[len - 1] == '\n')
+               --count;
+
+       rtnl_lock();
+       ret = dev_set_alias(netdev, buf, count);
+       rtnl_unlock();
+
+       return ret < 0 ? ret : len;
 }
 
-static ssize_t store_weight(struct device *dev, struct device_attribute *attr,
-                           const char *buf, size_t len)
+static ssize_t show_ifalias(struct device *dev,
+                           struct device_attribute *attr, char *buf)
 {
-       return netdev_store(dev, attr, buf, len, change_weight);
+       const struct net_device *netdev = to_net_dev(dev);
+       ssize_t ret = 0;
+
+       rtnl_lock();
+       if (netdev->ifalias)
+               ret = sprintf(buf, "%s\n", netdev->ifalias);
+       rtnl_unlock();
+       return ret;
 }
 
 static struct device_attribute net_class_attributes[] = {
        __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
+       __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
+       __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
        __ATTR(iflink, S_IRUGO, show_iflink, NULL),
        __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
        __ATTR(features, S_IRUGO, show_features, NULL),
@@ -246,7 +263,6 @@ static struct device_attribute net_class_attributes[] = {
        __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
        __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
               store_tx_queue_len),
-       __ATTR(weight, S_IRUGO | S_IWUSR, show_weight, store_weight),
        {}
 };
 
@@ -256,19 +272,17 @@ static ssize_t netstat_show(const struct device *d,
                            unsigned long offset)
 {
        struct net_device *dev = to_net_dev(d);
-       struct net_device_stats *stats;
        ssize_t ret = -EINVAL;
 
-       if (offset > sizeof(struct net_device_stats) ||
-           offset % sizeof(unsigned long) != 0)
-               WARN_ON(1);
+       WARN_ON(offset > sizeof(struct net_device_stats) ||
+                       offset % sizeof(unsigned long) != 0);
 
        read_lock(&dev_base_lock);
-       if (dev_isalive(dev) && dev->get_stats &&
-           (stats = (*dev->get_stats)(dev)))
+       if (dev_isalive(dev)) {
+               const struct net_device_stats *stats = dev_get_stats(dev);
                ret = sprintf(buf, fmt_ulong,
                              *(unsigned long *)(((u8 *) stats) + offset));
-
+       }
        read_unlock(&dev_base_lock);
        return ret;
 }
@@ -340,7 +354,7 @@ static struct attribute_group netstat_group = {
        .attrs  = netstat_attrs,
 };
 
-#ifdef CONFIG_WIRELESS_EXT
+#ifdef CONFIG_WIRELESS_EXT_SYSFS
 /* helper function that does all the locking etc for wireless stats */
 static ssize_t wireless_show(struct device *d, char *buf,
                             ssize_t (*format)(const struct iw_statistics *,
@@ -407,25 +421,29 @@ static struct attribute_group wireless_group = {
 };
 #endif
 
+#endif /* CONFIG_SYSFS */
+
 #ifdef CONFIG_HOTPLUG
-static int netdev_uevent(struct device *d, char **envp,
-                        int num_envp, char *buf, int size)
+static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
 {
        struct net_device *dev = to_net_dev(d);
-       int i = 0;
-       int n;
+       int retval;
+
+       if (!net_eq(dev_net(dev), &init_net))
+               return 0;
 
        /* pass interface to uevent. */
-       envp[i++] = buf;
-       n = snprintf(buf, size, "INTERFACE=%s", dev->name) + 1;
-       buf += n;
-       size -= n;
+       retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
+       if (retval)
+               goto exit;
 
-       if ((size <= 0) || (i >= num_envp))
-               return -ENOMEM;
+       /* pass ifindex to uevent.
+        * ifindex is useful as it won't change (interface name may change)
+        * and is what RtNetlink uses natively. */
+       retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
 
-       envp[i] = NULL;
-       return 0;
+exit:
+       return retval;
 }
 #endif
 
@@ -439,49 +457,84 @@ static void netdev_release(struct device *d)
 
        BUG_ON(dev->reg_state != NETREG_RELEASED);
 
+       kfree(dev->ifalias);
        kfree((char *)dev - dev->padded);
 }
 
 static struct class net_class = {
        .name = "net",
        .dev_release = netdev_release,
+#ifdef CONFIG_SYSFS
        .dev_attrs = net_class_attributes,
+#endif /* CONFIG_SYSFS */
 #ifdef CONFIG_HOTPLUG
        .dev_uevent = netdev_uevent,
 #endif
 };
 
-void netdev_unregister_sysfs(struct net_device * net)
+/* Delete sysfs entries but hold kobject reference until after all
+ * netdev references are gone.
+ */
+void netdev_unregister_kobject(struct net_device * net)
 {
-       device_del(&(net->dev));
+       struct device *dev = &(net->dev);
+
+       kobject_get(&dev->kobj);
+
+       if (dev_net(net) != &init_net)
+               return;
+
+       device_del(dev);
 }
 
 /* Create sysfs entries for network device. */
-int netdev_register_sysfs(struct net_device *net)
+int netdev_register_kobject(struct net_device *net)
 {
        struct device *dev = &(net->dev);
        struct attribute_group **groups = net->sysfs_groups;
 
-       device_initialize(dev);
        dev->class = &net_class;
        dev->platform_data = net;
        dev->groups = groups;
 
        BUILD_BUG_ON(BUS_ID_SIZE < IFNAMSIZ);
-       strlcpy(dev->bus_id, net->name, BUS_ID_SIZE);
+       dev_set_name(dev, "%s", net->name);
 
-       if (net->get_stats)
-               *groups++ = &netstat_group;
+#ifdef CONFIG_SYSFS
+       *groups++ = &netstat_group;
 
-#ifdef CONFIG_WIRELESS_EXT
+#ifdef CONFIG_WIRELESS_EXT_SYSFS
        if (net->wireless_handlers && net->wireless_handlers->get_wireless_stats)
                *groups++ = &wireless_group;
 #endif
+#endif /* CONFIG_SYSFS */
+
+       if (dev_net(net) != &init_net)
+               return 0;
 
        return device_add(dev);
 }
 
-int netdev_sysfs_init(void)
+int netdev_class_create_file(struct class_attribute *class_attr)
+{
+       return class_create_file(&net_class, class_attr);
+}
+
+void netdev_class_remove_file(struct class_attribute *class_attr)
+{
+       class_remove_file(&net_class, class_attr);
+}
+
+EXPORT_SYMBOL(netdev_class_create_file);
+EXPORT_SYMBOL(netdev_class_remove_file);
+
+void netdev_initialize_kobject(struct net_device *net)
+{
+       struct device *device = &(net->dev);
+       device_initialize(device);
+}
+
+int netdev_kobject_init(void)
 {
        return class_register(&net_class);
 }