Commit 7e84c961 authored by Daniel Golle's avatar Daniel Golle Committed by Richard Weinberger

mtd: ubi: introduce pre-removal notification for UBI volumes

Introduce a new notification type UBI_VOLUME_SHUTDOWN to inform users
that a volume is just about to be removed.
This is needed because users (such as the NVMEM subsystem) expect that
at the time their removal function is called, the parenting device is
still available (for removal of sysfs nodes, for example, in case of
NVMEM which otherwise WARNs on volume removal).
Signed-off-by: default avatarDaniel Golle <daniel@makrotopia.org>
Signed-off-by: default avatarRichard Weinberger <richard@nod.at>
parent 927c1452
...@@ -93,7 +93,7 @@ static struct ubi_device *ubi_devices[UBI_MAX_DEVICES]; ...@@ -93,7 +93,7 @@ static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
/* Serializes UBI devices creations and removals */ /* Serializes UBI devices creations and removals */
DEFINE_MUTEX(ubi_devices_mutex); DEFINE_MUTEX(ubi_devices_mutex);
/* Protects @ubi_devices and @ubi->ref_count */ /* Protects @ubi_devices, @ubi->ref_count and @ubi->is_dead */
static DEFINE_SPINLOCK(ubi_devices_lock); static DEFINE_SPINLOCK(ubi_devices_lock);
/* "Show" method for files in '/<sysfs>/class/ubi/' */ /* "Show" method for files in '/<sysfs>/class/ubi/' */
...@@ -261,6 +261,9 @@ struct ubi_device *ubi_get_device(int ubi_num) ...@@ -261,6 +261,9 @@ struct ubi_device *ubi_get_device(int ubi_num)
spin_lock(&ubi_devices_lock); spin_lock(&ubi_devices_lock);
ubi = ubi_devices[ubi_num]; ubi = ubi_devices[ubi_num];
if (ubi && ubi->is_dead)
ubi = NULL;
if (ubi) { if (ubi) {
ubi_assert(ubi->ref_count >= 0); ubi_assert(ubi->ref_count >= 0);
ubi->ref_count += 1; ubi->ref_count += 1;
...@@ -298,7 +301,7 @@ struct ubi_device *ubi_get_by_major(int major) ...@@ -298,7 +301,7 @@ struct ubi_device *ubi_get_by_major(int major)
spin_lock(&ubi_devices_lock); spin_lock(&ubi_devices_lock);
for (i = 0; i < UBI_MAX_DEVICES; i++) { for (i = 0; i < UBI_MAX_DEVICES; i++) {
ubi = ubi_devices[i]; ubi = ubi_devices[i];
if (ubi && MAJOR(ubi->cdev.dev) == major) { if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) {
ubi_assert(ubi->ref_count >= 0); ubi_assert(ubi->ref_count >= 0);
ubi->ref_count += 1; ubi->ref_count += 1;
get_device(&ubi->dev); get_device(&ubi->dev);
...@@ -327,7 +330,7 @@ int ubi_major2num(int major) ...@@ -327,7 +330,7 @@ int ubi_major2num(int major)
for (i = 0; i < UBI_MAX_DEVICES; i++) { for (i = 0; i < UBI_MAX_DEVICES; i++) {
struct ubi_device *ubi = ubi_devices[i]; struct ubi_device *ubi = ubi_devices[i];
if (ubi && MAJOR(ubi->cdev.dev) == major) { if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) {
ubi_num = ubi->ubi_num; ubi_num = ubi->ubi_num;
break; break;
} }
...@@ -514,7 +517,7 @@ static void ubi_free_volumes_from(struct ubi_device *ubi, int from) ...@@ -514,7 +517,7 @@ static void ubi_free_volumes_from(struct ubi_device *ubi, int from)
int i; int i;
for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
if (!ubi->volumes[i]) if (!ubi->volumes[i] || ubi->volumes[i]->is_dead)
continue; continue;
ubi_eba_replace_table(ubi->volumes[i], NULL); ubi_eba_replace_table(ubi->volumes[i], NULL);
ubi_fastmap_destroy_checkmap(ubi->volumes[i]); ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
...@@ -1099,7 +1102,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) ...@@ -1099,7 +1102,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
return -EINVAL; return -EINVAL;
spin_lock(&ubi_devices_lock); spin_lock(&ubi_devices_lock);
put_device(&ubi->dev);
ubi->ref_count -= 1; ubi->ref_count -= 1;
if (ubi->ref_count) { if (ubi->ref_count) {
if (!anyway) { if (!anyway) {
...@@ -1110,6 +1112,13 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) ...@@ -1110,6 +1112,13 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
ubi_err(ubi, "%s reference count %d, destroy anyway", ubi_err(ubi, "%s reference count %d, destroy anyway",
ubi->ubi_name, ubi->ref_count); ubi->ubi_name, ubi->ref_count);
} }
ubi->is_dead = true;
spin_unlock(&ubi_devices_lock);
ubi_notify_all(ubi, UBI_VOLUME_SHUTDOWN, NULL);
spin_lock(&ubi_devices_lock);
put_device(&ubi->dev);
ubi_devices[ubi_num] = NULL; ubi_devices[ubi_num] = NULL;
spin_unlock(&ubi_devices_lock); spin_unlock(&ubi_devices_lock);
......
...@@ -152,7 +152,7 @@ struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) ...@@ -152,7 +152,7 @@ struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
spin_lock(&ubi->volumes_lock); spin_lock(&ubi->volumes_lock);
vol = ubi->volumes[vol_id]; vol = ubi->volumes[vol_id];
if (!vol) if (!vol || vol->is_dead)
goto out_unlock; goto out_unlock;
err = -EBUSY; err = -EBUSY;
......
...@@ -337,6 +337,7 @@ struct ubi_volume { ...@@ -337,6 +337,7 @@ struct ubi_volume {
int writers; int writers;
int exclusive; int exclusive;
int metaonly; int metaonly;
bool is_dead;
int reserved_pebs; int reserved_pebs;
int vol_type; int vol_type;
...@@ -561,6 +562,7 @@ struct ubi_device { ...@@ -561,6 +562,7 @@ struct ubi_device {
spinlock_t volumes_lock; spinlock_t volumes_lock;
int ref_count; int ref_count;
int image_seq; int image_seq;
bool is_dead;
int rsvd_pebs; int rsvd_pebs;
int avail_pebs; int avail_pebs;
......
...@@ -59,7 +59,7 @@ static ssize_t vol_attribute_show(struct device *dev, ...@@ -59,7 +59,7 @@ static ssize_t vol_attribute_show(struct device *dev,
struct ubi_device *ubi = vol->ubi; struct ubi_device *ubi = vol->ubi;
spin_lock(&ubi->volumes_lock); spin_lock(&ubi->volumes_lock);
if (!ubi->volumes[vol->vol_id]) { if (!ubi->volumes[vol->vol_id] || ubi->volumes[vol->vol_id]->is_dead) {
spin_unlock(&ubi->volumes_lock); spin_unlock(&ubi->volumes_lock);
return -ENODEV; return -ENODEV;
} }
...@@ -189,7 +189,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) ...@@ -189,7 +189,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
/* Ensure that the name is unique */ /* Ensure that the name is unique */
for (i = 0; i < ubi->vtbl_slots; i++) for (i = 0; i < ubi->vtbl_slots; i++)
if (ubi->volumes[i] && if (ubi->volumes[i] && !ubi->volumes[i]->is_dead &&
ubi->volumes[i]->name_len == req->name_len && ubi->volumes[i]->name_len == req->name_len &&
!strcmp(ubi->volumes[i]->name, req->name)) { !strcmp(ubi->volumes[i]->name, req->name)) {
ubi_err(ubi, "volume \"%s\" exists (ID %d)", ubi_err(ubi, "volume \"%s\" exists (ID %d)",
...@@ -352,6 +352,19 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) ...@@ -352,6 +352,19 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
err = -EBUSY; err = -EBUSY;
goto out_unlock; goto out_unlock;
} }
/*
* Mark volume as dead at this point to prevent that anyone
* can take a reference to the volume from now on.
* This is necessary as we have to release the spinlock before
* calling ubi_volume_notify.
*/
vol->is_dead = true;
spin_unlock(&ubi->volumes_lock);
ubi_volume_notify(ubi, vol, UBI_VOLUME_SHUTDOWN);
spin_lock(&ubi->volumes_lock);
ubi->volumes[vol_id] = NULL; ubi->volumes[vol_id] = NULL;
spin_unlock(&ubi->volumes_lock); spin_unlock(&ubi->volumes_lock);
......
...@@ -192,6 +192,7 @@ struct ubi_device_info { ...@@ -192,6 +192,7 @@ struct ubi_device_info {
* or a volume was removed) * or a volume was removed)
* @UBI_VOLUME_RESIZED: a volume has been re-sized * @UBI_VOLUME_RESIZED: a volume has been re-sized
* @UBI_VOLUME_RENAMED: a volume has been re-named * @UBI_VOLUME_RENAMED: a volume has been re-named
* @UBI_VOLUME_SHUTDOWN: a volume is going to removed, shutdown users
* @UBI_VOLUME_UPDATED: data has been written to a volume * @UBI_VOLUME_UPDATED: data has been written to a volume
* *
* These constants define which type of event has happened when a volume * These constants define which type of event has happened when a volume
...@@ -202,6 +203,7 @@ enum { ...@@ -202,6 +203,7 @@ enum {
UBI_VOLUME_REMOVED, UBI_VOLUME_REMOVED,
UBI_VOLUME_RESIZED, UBI_VOLUME_RESIZED,
UBI_VOLUME_RENAMED, UBI_VOLUME_RENAMED,
UBI_VOLUME_SHUTDOWN,
UBI_VOLUME_UPDATED, UBI_VOLUME_UPDATED,
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment