prevent deletion devices that are part of the current configuration
[project/netifd.git] / device.c
index 23befa3..f856f22 100644 (file)
--- a/device.c
+++ b/device.c
@@ -99,7 +99,7 @@ device_init_settings(struct device *dev, struct blob_attr **tb)
 
 static void __init dev_init(void)
 {
-       avl_init(&devices, avl_strcmp, false, NULL);
+       avl_init(&devices, avl_strcmp, true, NULL);
 }
 
 static void device_broadcast_event(struct device *dev, enum device_event ev)
@@ -167,7 +167,7 @@ void device_release(struct device_user *dep)
        device_broadcast_event(dev, DEV_EVENT_DOWN);
 }
 
-int check_device_state(struct device *dev)
+int device_check_state(struct device *dev)
 {
        if (!dev->type->check_state)
                return 0;
@@ -203,12 +203,25 @@ int device_init(struct device *dev, const struct device_type *type, const char *
        if (ret < 0)
                return ret;
 
-       check_device_state(dev);
+       device_check_state(dev);
 
        return 0;
 }
 
-struct device *device_get(const char *name, bool create)
+static struct device *
+device_create_default(const char *name)
+{
+       struct device *dev;
+
+       D(DEVICE, "Create simple device '%s'\n", name);
+       dev = calloc(1, sizeof(*dev));
+       device_init(dev, &simple_device_type, name);
+       dev->default_config = true;
+       return dev;
+}
+
+struct device *
+device_get(const char *name, bool create)
 {
        struct device *dev;
 
@@ -222,10 +235,18 @@ struct device *device_get(const char *name, bool create)
        if (!create)
                return NULL;
 
-       dev = calloc(1, sizeof(*dev));
-       device_init(dev, &simple_device_type, name);
+       return device_create_default(name);
+}
 
-       return dev;
+static void
+device_delete(struct device *dev)
+{
+       if (!dev->avl.key)
+               return;
+
+       D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
+       avl_delete(&devices, &dev->avl);
+       dev->avl.key = NULL;
 }
 
 void device_cleanup(struct device *dev)
@@ -240,8 +261,7 @@ void device_cleanup(struct device *dev)
                dep->cb(dep, DEV_EVENT_REMOVE);
        }
 
-       if (dev->avl.key)
-               avl_delete(&devices, &dev->avl);
+       device_delete(dev);
 }
 
 void device_set_present(struct device *dev, bool state)
@@ -268,7 +288,7 @@ void device_add_user(struct device_user *dep, struct device *dev)
 static void
 __device_free_unused(struct device *dev)
 {
-       if (!list_empty(&dev->users))
+       if (!list_empty(&dev->users) || dev->current_config)
                return;
 
        device_free(dev);
@@ -298,6 +318,20 @@ device_free_unused(struct device *dev)
                __device_free_unused(dev);
 }
 
+void
+device_init_pending(void)
+{
+       struct device *dev, *tmp;
+
+       avl_for_each_element_safe(&devices, dev, avl, tmp) {
+               if (!dev->config_pending)
+                       continue;
+
+               dev->type->config_init(dev);
+               dev->config_pending = false;
+       }
+}
+
 enum dev_change_type
 device_reload_config(struct device *dev, struct blob_attr *attr)
 {
@@ -319,8 +353,12 @@ device_reload_config(struct device *dev, struct blob_attr *attr)
 }
 
 static enum dev_change_type
-device_check_config(struct device *dev, struct blob_attr *attr)
+device_check_config(struct device *dev, const struct device_type *type,
+                   struct blob_attr *attr)
 {
+       if (type != dev->type)
+               return DEV_CONFIG_RECREATE;
+
        if (dev->type->reload)
                return dev->type->reload(dev, attr);
 
@@ -337,8 +375,8 @@ device_replace(struct device *dev, struct device *odev)
                device_set_present(odev, false);
 
        list_for_each_entry_safe(dep, tmp, &odev->users, list) {
-               list_move_tail(&dep->list, &dev->users);
-               dep->dev = dev;
+               device_remove_user(dep);
+               device_add_user(dep, dev);
        }
        device_free(odev);
 
@@ -346,6 +384,32 @@ device_replace(struct device *dev, struct device *odev)
                device_set_present(dev, true);
 }
 
+void
+device_reset_config(void)
+{
+       struct device *dev;
+
+       avl_for_each_element(&devices, dev, avl)
+               dev->current_config = false;
+}
+
+void
+device_reset_old(void)
+{
+       struct device *dev, *tmp, *ndev;
+
+       avl_for_each_element_safe(&devices, dev, avl, tmp) {
+               if (dev->current_config || dev->default_config)
+                       continue;
+
+               if (dev->type != &simple_device_type)
+                       continue;
+
+               ndev = device_create_default(dev->ifname);
+               device_replace(ndev, dev);
+       }
+}
+
 struct device *
 device_create(const char *name, const struct device_type *type,
              struct blob_attr *config)
@@ -353,29 +417,48 @@ device_create(const char *name, const struct device_type *type,
        struct device *odev = NULL, *dev;
        enum dev_change_type change;
 
+       D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
+       config = config_memdup(config);
+       if (!config)
+               return NULL;
+
        odev = device_get(name, false);
        if (odev) {
-               change = device_check_config(odev, config);
+               odev->current_config = true;
+               change = device_check_config(odev, type, config);
                switch (change) {
                case DEV_CONFIG_APPLIED:
+                       D(DEVICE, "Device '%s': config applied\n", odev->ifname);
                        free(odev->config);
-                       odev->config = config_memdup(config);
+                       odev->config = config;
                        if (odev->present) {
                                device_set_present(odev, false);
                                device_set_present(odev, true);
                        }
-                       /* fall through */
+                       free(config);
+                       return odev;
                case DEV_CONFIG_NO_CHANGE:
+                       D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
+                       free(config);
                        return odev;
                case DEV_CONFIG_RECREATE:
+                       D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
+                       device_delete(odev);
                        break;
                }
        }
 
        dev = type->create(config);
-       dev->config = config_memdup(config);
+       if (!dev)
+               return NULL;
+
+       dev->current_config = true;
+       dev->config = config;
        if (odev)
                device_replace(dev, odev);
 
+       if (!config_init && dev->config_pending)
+               type->config_init(dev);
+
        return dev;
 }