/** * ubi_add_volume - add volume. * @ubi: UBI device description object * @vol: volume description object * * This function adds an existing volume and initializes all its data * structures. Returns zero in case of success and a negative error code in * case of failure. */ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) { int err = 0; dbg_gen("add volume"); sprintf(vol->dev.name, "%s.%s", dev_name(&ubi->dev), vol->name); vol->dev.id = DEVICE_ID_SINGLE; vol->dev.parent = &ubi->dev; err = register_device(&vol->dev); if (err) return err; /* Register character device for the volume */ err = ubi_volume_cdev_add(ubi, vol); if (err) { ubi_err(ubi, "cannot add character device for volume, error %d", err); return err; } self_check_volumes(ubi); return err; return err; }
/** * ubi_rename_volumes - re-name UBI volumes. * @ubi: UBI device description object * @rename_list: list of &struct ubi_rename_entry objects * * This function re-names or removes volumes specified in the re-name list. * Returns zero in case of success and a negative error code in case of * failure. */ int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list) { int err; struct ubi_rename_entry *re; err = ubi_vtbl_rename_volumes(ubi, rename_list); if (err) return err; list_for_each_entry(re, rename_list, list) { if (re->remove) { err = ubi_remove_volume(re->desc, 1); if (err) break; } else { struct ubi_volume *vol = re->desc->vol; vol->name_len = re->new_name_len; memcpy(vol->name, re->new_name, re->new_name_len + 1); ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED); } } if (!err) self_check_volumes(ubi); return err; }
/** * ubi_remove_volume - remove volume. * @desc: volume descriptor * @no_vtbl: do not change volume table if not zero * * This function removes volume described by @desc. The volume has to be opened * in "exclusive" mode. Returns zero in case of success and a negative error * code in case of failure. The caller has to have the @ubi->device_mutex * locked. */ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) { struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs; dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id); ubi_assert(desc->mode == UBI_EXCLUSIVE); ubi_assert(vol == ubi->volumes[vol_id]); if (ubi->ro_mode) return -EROFS; if (vol->ref_count > 1) { /* * The volume is busy, probably someone is reading one of its * sysfs files. */ err = -EBUSY; goto out_unlock; } ubi->volumes[vol_id] = NULL; if (!no_vtbl) { err = ubi_change_vtbl_record(ubi, vol_id, NULL); if (err) goto out_err; } for (i = 0; i < vol->reserved_pebs; i++) { err = ubi_eba_unmap_leb(ubi, vol, i); if (err) goto out_err; } unregister_device(&vol->dev); devfs_remove(&vol->cdev); ubi->rsvd_pebs -= reserved_pebs; ubi->avail_pebs += reserved_pebs; ubi_update_reserved(ubi); ubi->vol_count -= 1; ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED); if (!no_vtbl) self_check_volumes(ubi); return err; out_err: ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err); ubi->volumes[vol_id] = vol; out_unlock: return err; }
/** * ubi_add_volume - add volume. * @ubi: UBI device description object * @vol: volume description object * * This function adds an existing volume and initializes all its data * structures. Returns zero in case of success and a negative error code in * case of failure. */ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) { int err = 0; dbg_gen("add volume"); /* Register character device for the volume */ err = ubi_volume_cdev_add(ubi, vol); if (err) { ubi_err("cannot add character device for volume, error %d", err); return err; } self_check_volumes(ubi); return err; return err; }
/** * ubi_create_volume - create volume. * @ubi: UBI device description object * @req: volume creation request * * This function creates volume described by @req. If @req->vol_id id * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume * and saves it in @req->vol_id. Returns zero in case of success and a negative * error code in case of failure. Note, the caller has to have the * @ubi->device_mutex locked. */ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) { int i, err, vol_id = req->vol_id, do_free = 1; struct ubi_volume *vol; struct ubi_vtbl_record vtbl_rec; if (ubi->ro_mode) return -EROFS; vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); if (!vol) return -ENOMEM; if (vol_id == UBI_VOL_NUM_AUTO) { /* Find unused volume ID */ dbg_gen("search for vacant volume ID"); for (i = 0; i < ubi->vtbl_slots; i++) if (!ubi->volumes[i]) { vol_id = i; break; } if (vol_id == UBI_VOL_NUM_AUTO) { ubi_err(ubi, "out of volume IDs"); err = -ENFILE; goto out_unlock; } req->vol_id = vol_id; } dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s", ubi->ubi_num, vol_id, (unsigned long long)req->bytes, (int)req->vol_type, req->name); /* Ensure that this volume does not exist */ err = -EEXIST; if (ubi->volumes[vol_id]) { ubi_err(ubi, "volume %d already exists", vol_id); goto out_unlock; } /* Ensure that the name is unique */ for (i = 0; i < ubi->vtbl_slots; i++) if (ubi->volumes[i] && ubi->volumes[i]->name_len == req->name_len && !strcmp(ubi->volumes[i]->name, req->name)) { ubi_err(ubi, "volume \"%s\" exists (ID %d)", req->name, i); goto out_unlock; } /* Calculate how many eraseblocks are requested */ vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment; vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1, vol->usable_leb_size); /* Reserve physical eraseblocks */ if (vol->reserved_pebs > ubi->avail_pebs) { ubi_err(ubi, "not enough PEBs, only %d available", ubi->avail_pebs); if (ubi->corr_peb_count) ubi_err(ubi, "%d PEBs are corrupted and not used", ubi->corr_peb_count); err = -ENOSPC; goto out_unlock; } ubi->avail_pebs -= vol->reserved_pebs; ubi->rsvd_pebs += vol->reserved_pebs; vol->vol_id = vol_id; vol->alignment = req->alignment; vol->data_pad = ubi->leb_size % vol->alignment; vol->vol_type = req->vol_type; vol->name_len = req->name_len; memcpy(vol->name, req->name, vol->name_len); vol->ubi = ubi; /* * Finish all pending erases because there may be some LEBs belonging * to the same volume ID. */ err = ubi_wl_flush(ubi, vol_id, UBI_ALL); if (err) goto out_acc; vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL); if (!vol->eba_tbl) { err = -ENOMEM; goto out_acc; } for (i = 0; i < vol->reserved_pebs; i++) vol->eba_tbl[i] = UBI_LEB_UNMAPPED; if (vol->vol_type == UBI_DYNAMIC_VOLUME) { vol->used_ebs = vol->reserved_pebs; vol->last_eb_bytes = vol->usable_leb_size; vol->used_bytes = (long long)vol->used_ebs * vol->usable_leb_size; } else { vol->used_ebs = div_u64_rem(vol->used_bytes, vol->usable_leb_size, &vol->last_eb_bytes); if (vol->last_eb_bytes != 0) vol->used_ebs += 1; else vol->last_eb_bytes = vol->usable_leb_size; } sprintf(vol->dev.name, "%s.%s", dev_name(&ubi->dev), vol->name); vol->dev.id = DEVICE_ID_SINGLE; vol->dev.parent = &ubi->dev; err = register_device(&vol->dev); if (err) goto out_acc; /* Register character device for the volume */ err = ubi_volume_cdev_add(ubi, vol); if (err) { ubi_err(ubi, "cannot add character device"); goto out_mapping; } /* Fill volume table record */ memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record)); vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs); vtbl_rec.alignment = cpu_to_be32(vol->alignment); vtbl_rec.data_pad = cpu_to_be32(vol->data_pad); vtbl_rec.name_len = cpu_to_be16(vol->name_len); if (vol->vol_type == UBI_DYNAMIC_VOLUME) vtbl_rec.vol_type = UBI_VID_DYNAMIC; else vtbl_rec.vol_type = UBI_VID_STATIC; memcpy(vtbl_rec.name, vol->name, vol->name_len); err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); if (err) goto out_sysfs; ubi->volumes[vol_id] = vol; ubi->vol_count += 1; ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED); self_check_volumes(ubi); return err; out_sysfs: /* * We have registered our device, we should not free the volume * description object in this function in case of an error - it is * freed by the release function. * * Get device reference to prevent the release function from being * called just after sysfs has been closed. */ do_free = 0; out_mapping: if (do_free) kfree(vol->eba_tbl); unregister_device(&vol->dev); out_acc: ubi->rsvd_pebs -= vol->reserved_pebs; ubi->avail_pebs += vol->reserved_pebs; out_unlock: if (do_free) kfree(vol); ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err); return err; }
/** * ubi_resize_volume - re-size volume. * @desc: volume descriptor * @reserved_pebs: new size in physical eraseblocks * * This function re-sizes the volume and returns zero in case of success, and a * negative error code in case of failure. The caller has to have the * @ubi->device_mutex locked. */ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) { int i, err, pebs, *new_mapping; struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; struct ubi_vtbl_record vtbl_rec; int vol_id = vol->vol_id; if (ubi->ro_mode) return -EROFS; dbg_gen("re-size device %d, volume %d to from %d to %d PEBs", ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs); if (vol->vol_type == UBI_STATIC_VOLUME && reserved_pebs < vol->used_ebs) { ubi_err(ubi, "too small size %d, %d LEBs contain data", reserved_pebs, vol->used_ebs); return -EINVAL; } /* If the size is the same, we have nothing to do */ if (reserved_pebs == vol->reserved_pebs) return 0; new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL); if (!new_mapping) return -ENOMEM; for (i = 0; i < reserved_pebs; i++) new_mapping[i] = UBI_LEB_UNMAPPED; if (vol->ref_count > 1) { err = -EBUSY; goto out_free; } /* Reserve physical eraseblocks */ pebs = reserved_pebs - vol->reserved_pebs; if (pebs > 0) { if (pebs > ubi->avail_pebs) { ubi_err(ubi, "not enough PEBs: requested %d, available %d", pebs, ubi->avail_pebs); if (ubi->corr_peb_count) ubi_err(ubi, "%d PEBs are corrupted and not used", ubi->corr_peb_count); err = -ENOSPC; goto out_free; } ubi->avail_pebs -= pebs; ubi->rsvd_pebs += pebs; for (i = 0; i < vol->reserved_pebs; i++) new_mapping[i] = vol->eba_tbl[i]; kfree(vol->eba_tbl); vol->eba_tbl = new_mapping; } /* Change volume table record */ vtbl_rec = ubi->vtbl[vol_id]; vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs); err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); if (err) goto out_acc; if (pebs < 0) { for (i = 0; i < -pebs; i++) { err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i); if (err) goto out_acc; } ubi->rsvd_pebs += pebs; ubi->avail_pebs -= pebs; ubi_update_reserved(ubi); for (i = 0; i < reserved_pebs; i++) new_mapping[i] = vol->eba_tbl[i]; kfree(vol->eba_tbl); vol->eba_tbl = new_mapping; } vol->reserved_pebs = reserved_pebs; if (vol->vol_type == UBI_DYNAMIC_VOLUME) { vol->used_ebs = reserved_pebs; vol->last_eb_bytes = vol->usable_leb_size; vol->used_bytes = (long long)vol->used_ebs * vol->usable_leb_size; } ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED); self_check_volumes(ubi); return err; out_acc: if (pebs > 0) { ubi->rsvd_pebs -= pebs; ubi->avail_pebs += pebs; } out_free: kfree(new_mapping); return err; }