// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) International Business Machines Corp., 2006 * * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
*/
/* * This is a small driver which implements fake MTD devices on top of UBI * volumes. This sounds strange, but it is in fact quite useful to make * MTD-oriented software (including all the legacy software) work on top of * UBI. * * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The * eraseblock size is equivalent to the logical eraseblock size of the volume.
*/
/** * struct gluebi_device - a gluebi device description data structure. * @mtd: emulated MTD device description object * @refcnt: gluebi device reference count * @desc: UBI volume descriptor * @ubi_num: UBI device number this gluebi device works on * @vol_id: ID of UBI volume this gluebi device works on * @list: link in a list of gluebi devices
*/ struct gluebi_device { struct mtd_info mtd; int refcnt; struct ubi_volume_desc *desc; int ubi_num; int vol_id; struct list_head list;
};
/* List of all gluebi devices */ static LIST_HEAD(gluebi_devices); static DEFINE_MUTEX(devices_mutex);
/** * find_gluebi_nolock - find a gluebi device. * @ubi_num: UBI device number * @vol_id: volume ID * * This function seraches for gluebi device corresponding to UBI device * @ubi_num and UBI volume @vol_id. Returns the gluebi device description * object in case of success and %NULL in case of failure. The caller has to * have the &devices_mutex locked.
*/ staticstruct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
{ struct gluebi_device *gluebi;
/** * gluebi_get_device - get MTD device reference. * @mtd: the MTD device description object * * This function is called every time the MTD device is being opened and * implements the MTD get_device() operation. Returns zero in case of success * and a negative error code in case of failure.
*/ staticint gluebi_get_device(struct mtd_info *mtd)
{ struct gluebi_device *gluebi; int ubi_mode = UBI_READONLY;
if (mtd->flags & MTD_WRITEABLE)
ubi_mode = UBI_READWRITE;
gluebi = container_of(mtd, struct gluebi_device, mtd);
mutex_lock(&devices_mutex); if (gluebi->refcnt > 0) { /* * The MTD device is already referenced and this is just one * more reference. MTD allows many users to open the same * volume simultaneously and do not distinguish between * readers/writers/exclusive/meta openers as UBI does. So we do * not open the UBI volume again - just increase the reference * counter and return.
*/
gluebi->refcnt += 1;
mutex_unlock(&devices_mutex); return 0;
}
/* * This is the first reference to this UBI volume via the MTD device * interface. Open the corresponding volume in read-write mode.
*/
gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
ubi_mode); if (IS_ERR(gluebi->desc)) {
mutex_unlock(&devices_mutex); return PTR_ERR(gluebi->desc);
}
gluebi->refcnt += 1;
mutex_unlock(&devices_mutex); return 0;
}
/** * gluebi_put_device - put MTD device reference. * @mtd: the MTD device description object * * This function is called every time the MTD device is being put. Returns * zero in case of success and a negative error code in case of failure.
*/ staticvoid gluebi_put_device(struct mtd_info *mtd)
{ struct gluebi_device *gluebi;
/** * gluebi_read - read operation of emulated MTD devices. * @mtd: MTD device description object * @from: absolute offset from where to read * @len: how many bytes to read * @retlen: count of read bytes is returned here * @buf: buffer to store the read data * * This function returns zero in case of success and a negative error code in * case of failure.
*/ staticint gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, unsignedchar *buf)
{ int err = 0, lnum, offs, bytes_left; struct gluebi_device *gluebi;
/** * gluebi_write - write operation of emulated MTD devices. * @mtd: MTD device description object * @to: absolute offset where to write * @len: how many bytes to write * @retlen: count of written bytes is returned here * @buf: buffer with data to write * * This function returns zero in case of success and a negative error code in * case of failure.
*/ staticint gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf)
{ int err = 0, lnum, offs, bytes_left; struct gluebi_device *gluebi;
/** * gluebi_erase - erase operation of emulated MTD devices. * @mtd: the MTD device description object * @instr: the erase operation description * * This function calls the erase callback when finishes. Returns zero in case * of success and a negative error code in case of failure.
*/ staticint gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
{ int err, i, lnum, count; struct gluebi_device *gluebi;
if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd)) return -EINVAL;
for (i = 0; i < count - 1; i++) {
err = ubi_leb_unmap(gluebi->desc, lnum + i); if (err) goto out_err;
} /* * MTD erase operations are synchronous, so we have to make sure the * physical eraseblock is wiped out. * * Thus, perform leb_erase instead of leb_unmap operation - leb_erase * will wait for the end of operations
*/
err = ubi_leb_erase(gluebi->desc, lnum + i); if (err) goto out_err;
/** * gluebi_create - create a gluebi device for an UBI volume. * @di: UBI device description object * @vi: UBI volume description object * * This function is called when a new UBI volume is created in order to create * corresponding fake MTD device. Returns zero in case of success and a * negative error code in case of failure.
*/ staticint gluebi_create(struct ubi_device_info *di, struct ubi_volume_info *vi)
{ struct gluebi_device *gluebi, *g; struct mtd_info *mtd;
gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL); if (!gluebi) return -ENOMEM;
/* * In case of dynamic a volume, MTD device size is just volume size. In * case of a static volume the size is equivalent to the amount of data * bytes.
*/ if (vi->vol_type == UBI_DYNAMIC_VOLUME)
mtd->size = (unsignedlonglong)vi->usable_leb_size * vi->size; else
mtd->size = vi->used_bytes;
/* Just a sanity check - make sure this gluebi device does not exist */
mutex_lock(&devices_mutex);
g = find_gluebi_nolock(vi->ubi_num, vi->vol_id); if (g)
err_msg("gluebi MTD device %d form UBI device %d volume %d already exists",
g->mtd.index, vi->ubi_num, vi->vol_id);
mutex_unlock(&devices_mutex);
/** * gluebi_remove - remove a gluebi device. * @vi: UBI volume description object * * This function is called when an UBI volume is removed and it removes * corresponding fake MTD device. Returns zero in case of success and a * negative error code in case of failure.
*/ staticint gluebi_remove(struct ubi_volume_info *vi)
{ int err = 0; struct mtd_info *mtd; struct gluebi_device *gluebi;
mutex_lock(&devices_mutex);
gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id); if (!gluebi) {
err_msg("got remove notification for unknown UBI device %d volume %d",
vi->ubi_num, vi->vol_id);
err = -ENOENT;
} elseif (gluebi->refcnt)
err = -EBUSY; else
list_del(&gluebi->list);
mutex_unlock(&devices_mutex); if (err) return err;
/** * gluebi_updated - UBI volume was updated notifier. * @vi: volume info structure * * This function is called every time an UBI volume is updated. It does nothing * if te volume @vol is dynamic, and changes MTD device size if the * volume is static. This is needed because static volumes cannot be read past * data they contain. This function returns zero in case of success and a * negative error code in case of error.
*/ staticint gluebi_updated(struct ubi_volume_info *vi)
{ struct gluebi_device *gluebi;
mutex_lock(&devices_mutex);
gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id); if (!gluebi) {
mutex_unlock(&devices_mutex);
err_msg("got update notification for unknown UBI device %d volume %d",
vi->ubi_num, vi->vol_id); return -ENOENT;
}
/** * gluebi_resized - UBI volume was re-sized notifier. * @vi: volume info structure * * This function is called every time an UBI volume is re-size. It changes the * corresponding fake MTD device size. This function returns zero in case of * success and a negative error code in case of error.
*/ staticint gluebi_resized(struct ubi_volume_info *vi)
{ struct gluebi_device *gluebi;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.