📄 kapi.c
字号:
return -EINVAL; if (vol->vol_type == UBI_STATIC_VOLUME) { if (vol->used_ebs == 0) /* Empty static UBI volume */ return 0; if (lnum == vol->used_ebs - 1 && offset + len > vol->last_eb_bytes) return -EINVAL; } if (vol->upd_marker) return -EBADF; if (len == 0) return 0; err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { ubi_warn("mark volume %d as corrupted", vol_id); vol->corrupted = 1; } return err;}EXPORT_SYMBOL_GPL(ubi_leb_read);/** * ubi_leb_write - write data. * @desc: volume descriptor * @lnum: logical eraseblock number to write to * @buf: data to write * @offset: offset within the logical eraseblock where to write * @len: how many bytes to write * @dtype: expected data type * * This function writes @len bytes of data from @buf to offset @offset of * logical eraseblock @lnum. The @dtype argument describes expected lifetime of * the data. * * This function takes care of physical eraseblock write failures. If write to * the physical eraseblock write operation fails, the logical eraseblock is * re-mapped to another physical eraseblock, the data is recovered, and the * write finishes. UBI has a pool of reserved physical eraseblocks for this. * * If all the data were successfully written, zero is returned. If an error * occurred and UBI has not been able to recover from it, this function returns * a negative error code. Note, in case of an error, it is possible that * something was still written to the flash media, but that may be some * garbage. * * If the volume is damaged because of an interrupted update this function just * returns immediately with %-EBADF code. */int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, int offset, int len, int dtype){ struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int vol_id = vol->vol_id; dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); if (vol_id < 0 || vol_id >= ubi->vtbl_slots) return -EINVAL; if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) return -EROFS; if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || offset + len > vol->usable_leb_size || offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) return -EINVAL; if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && dtype != UBI_UNKNOWN) return -EINVAL; if (vol->upd_marker) return -EBADF; if (len == 0) return 0; return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);}EXPORT_SYMBOL_GPL(ubi_leb_write);/* * ubi_leb_change - change logical eraseblock atomically. * @desc: volume descriptor * @lnum: logical eraseblock number to change * @buf: data to write * @len: how many bytes to write * @dtype: expected data type * * This function changes the contents of a logical eraseblock atomically. @buf * has to contain new logical eraseblock data, and @len - the length of the * data, which has to be aligned. The length may be shorter then the logical * eraseblock size, ant the logical eraseblock may be appended to more times * later on. This function guarantees that in case of an unclean reboot the old * contents is preserved. Returns zero in case of success and a negative error * code in case of failure. */int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, int len, int dtype){ struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int vol_id = vol->vol_id; dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); if (vol_id < 0 || vol_id >= ubi->vtbl_slots) return -EINVAL; if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) return -EROFS; if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) return -EINVAL; if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && dtype != UBI_UNKNOWN) return -EINVAL; if (vol->upd_marker) return -EBADF; if (len == 0) return 0; return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);}EXPORT_SYMBOL_GPL(ubi_leb_change);/** * ubi_leb_erase - erase logical eraseblock. * @desc: volume descriptor * @lnum: logical eraseblock number * * This function un-maps logical eraseblock @lnum and synchronously erases the * correspondent physical eraseblock. Returns zero in case of success and a * negative error code in case of failure. * * If the volume is damaged because of an interrupted update this function just * returns immediately with %-EBADF code. */int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum){ struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int err; dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) return -EROFS; if (lnum < 0 || lnum >= vol->reserved_pebs) return -EINVAL; if (vol->upd_marker) return -EBADF; err = ubi_eba_unmap_leb(ubi, vol, lnum); if (err) return err; return ubi_wl_flush(ubi);}EXPORT_SYMBOL_GPL(ubi_leb_erase);/** * ubi_leb_unmap - un-map logical eraseblock. * @desc: volume descriptor * @lnum: logical eraseblock number * * This function un-maps logical eraseblock @lnum and schedules the * corresponding physical eraseblock for erasure, so that it will eventually be * physically erased in background. This operation is much faster then the * erase operation. * * Unlike erase, the un-map operation does not guarantee that the logical * eraseblock will contain all 0xFF bytes when UBI is initialized again. For * example, if several logical eraseblocks are un-mapped, and an unclean reboot * happens after this, the logical eraseblocks will not necessarily be * un-mapped again when this MTD device is attached. They may actually be * mapped to the same physical eraseblocks again. So, this function has to be * used with care. * * In other words, when un-mapping a logical eraseblock, UBI does not store * any information about this on the flash media, it just marks the logical * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical * eraseblock is physically erased, it will be mapped again to the same logical * eraseblock when the MTD device is attached again. * * The main and obvious use-case of this function is when the contents of a * logical eraseblock has to be re-written. Then it is much more efficient to * first un-map it, then write new data, rather then first erase it, then write * new data. Note, once new data has been written to the logical eraseblock, * UBI guarantees that the old contents has gone forever. In other words, if an * unclean reboot happens after the logical eraseblock has been un-mapped and * then written to, it will contain the last written data. * * This function returns zero in case of success and a negative error code in * case of failure. If the volume is damaged because of an interrupted update * this function just returns immediately with %-EBADF code. */int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum){ struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) return -EROFS; if (lnum < 0 || lnum >= vol->reserved_pebs) return -EINVAL; if (vol->upd_marker) return -EBADF; return ubi_eba_unmap_leb(ubi, vol, lnum);}EXPORT_SYMBOL_GPL(ubi_leb_unmap);/** * ubi_leb_map - map logical erasblock to a physical eraseblock. * @desc: volume descriptor * @lnum: logical eraseblock number * @dtype: expected data type * * This function maps an un-mapped logical eraseblock @lnum to a physical * eraseblock. This means, that after a successfull invocation of this * function the logical eraseblock @lnum will be empty (contain only %0xFF * bytes) and be mapped to a physical eraseblock, even if an unclean reboot * happens. * * This function returns zero in case of success, %-EBADF if the volume is * damaged because of an interrupted update, %-EBADMSG if the logical * eraseblock is already mapped, and other negative error codes in case of * other failures. */int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype){ struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) return -EROFS; if (lnum < 0 || lnum >= vol->reserved_pebs) return -EINVAL; if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && dtype != UBI_UNKNOWN) return -EINVAL; if (vol->upd_marker) return -EBADF; if (vol->eba_tbl[lnum] >= 0) return -EBADMSG; return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);}EXPORT_SYMBOL_GPL(ubi_leb_map);/** * ubi_is_mapped - check if logical eraseblock is mapped. * @desc: volume descriptor * @lnum: logical eraseblock number * * This function checks if logical eraseblock @lnum is mapped to a physical * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily * mean it will still be un-mapped after the UBI device is re-attached. The * logical eraseblock may become mapped to the physical eraseblock it was last * mapped to. * * This function returns %1 if the LEB is mapped, %0 if not, and a negative * error code in case of failure. If the volume is damaged because of an * interrupted update this function just returns immediately with %-EBADF error * code. */int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum){ struct ubi_volume *vol = desc->vol; dbg_gen("test LEB %d:%d", vol->vol_id, lnum); if (lnum < 0 || lnum >= vol->reserved_pebs) return -EINVAL; if (vol->upd_marker) return -EBADF; return vol->eba_tbl[lnum] >= 0;}EXPORT_SYMBOL_GPL(ubi_is_mapped);/** * ubi_sync - synchronize UBI device buffers. * @ubi_num: UBI device to synchronize * * The underlying MTD device may cache data in hardware or in software. This * function ensures the caches are flushed. Returns zero in case of success and * a negative error code in case of failure. */int ubi_sync(int ubi_num){ struct ubi_device *ubi; ubi = ubi_get_device(ubi_num); if (!ubi) return -ENODEV; if (ubi->mtd->sync) ubi->mtd->sync(ubi->mtd); ubi_put_device(ubi); return 0;}EXPORT_SYMBOL_GPL(ubi_sync);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -