UBI: use nicer 64-bit math
Get rid of 'do_div()' and use more user-friendly primitives from 'linux/math64.h'. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
This commit is contained in:
@@ -41,8 +41,8 @@
|
|||||||
#include <linux/capability.h>
|
#include <linux/capability.h>
|
||||||
#include <linux/uaccess.h>
|
#include <linux/uaccess.h>
|
||||||
#include <linux/compat.h>
|
#include <linux/compat.h>
|
||||||
|
#include <linux/math64.h>
|
||||||
#include <mtd/ubi-user.h>
|
#include <mtd/ubi-user.h>
|
||||||
#include <asm/div64.h>
|
|
||||||
#include "ubi.h"
|
#include "ubi.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -195,7 +195,6 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
|
|||||||
int err, lnum, off, len, tbuf_size;
|
int err, lnum, off, len, tbuf_size;
|
||||||
size_t count_save = count;
|
size_t count_save = count;
|
||||||
void *tbuf;
|
void *tbuf;
|
||||||
uint64_t tmp;
|
|
||||||
|
|
||||||
dbg_gen("read %zd bytes from offset %lld of volume %d",
|
dbg_gen("read %zd bytes from offset %lld of volume %d",
|
||||||
count, *offp, vol->vol_id);
|
count, *offp, vol->vol_id);
|
||||||
@@ -225,10 +224,7 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
|
|||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
len = count > tbuf_size ? tbuf_size : count;
|
len = count > tbuf_size ? tbuf_size : count;
|
||||||
|
lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
|
||||||
tmp = *offp;
|
|
||||||
off = do_div(tmp, vol->usable_leb_size);
|
|
||||||
lnum = tmp;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
cond_resched();
|
cond_resched();
|
||||||
@@ -279,7 +275,6 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
|
|||||||
int lnum, off, len, tbuf_size, err = 0;
|
int lnum, off, len, tbuf_size, err = 0;
|
||||||
size_t count_save = count;
|
size_t count_save = count;
|
||||||
char *tbuf;
|
char *tbuf;
|
||||||
uint64_t tmp;
|
|
||||||
|
|
||||||
dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
|
dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
|
||||||
count, *offp, vol->vol_id);
|
count, *offp, vol->vol_id);
|
||||||
@@ -287,10 +282,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
|
|||||||
if (vol->vol_type == UBI_STATIC_VOLUME)
|
if (vol->vol_type == UBI_STATIC_VOLUME)
|
||||||
return -EROFS;
|
return -EROFS;
|
||||||
|
|
||||||
tmp = *offp;
|
lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
|
||||||
off = do_div(tmp, vol->usable_leb_size);
|
|
||||||
lnum = tmp;
|
|
||||||
|
|
||||||
if (off & (ubi->min_io_size - 1)) {
|
if (off & (ubi->min_io_size - 1)) {
|
||||||
dbg_err("unaligned position");
|
dbg_err("unaligned position");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
@@ -882,7 +874,6 @@ static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
|
|||||||
case UBI_IOCRSVOL:
|
case UBI_IOCRSVOL:
|
||||||
{
|
{
|
||||||
int pebs;
|
int pebs;
|
||||||
uint64_t tmp;
|
|
||||||
struct ubi_rsvol_req req;
|
struct ubi_rsvol_req req;
|
||||||
|
|
||||||
dbg_gen("re-size volume");
|
dbg_gen("re-size volume");
|
||||||
@@ -902,9 +893,8 @@ static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp = req.bytes;
|
pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
|
||||||
pebs = !!do_div(tmp, desc->vol->usable_leb_size);
|
desc->vol->usable_leb_size);
|
||||||
pebs += tmp;
|
|
||||||
|
|
||||||
mutex_lock(&ubi->volumes_mutex);
|
mutex_lock(&ubi->volumes_mutex);
|
||||||
err = ubi_resize_volume(desc, pebs);
|
err = ubi_resize_volume(desc, pebs);
|
||||||
|
@@ -28,7 +28,7 @@
|
|||||||
* eraseblock size is equivalent to the logical eraseblock size of the volume.
|
* eraseblock size is equivalent to the logical eraseblock size of the volume.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <asm/div64.h>
|
#include <linux/math64.h>
|
||||||
#include "ubi.h"
|
#include "ubi.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,7 +109,6 @@ static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
|
|||||||
int err = 0, lnum, offs, total_read;
|
int err = 0, lnum, offs, total_read;
|
||||||
struct ubi_volume *vol;
|
struct ubi_volume *vol;
|
||||||
struct ubi_device *ubi;
|
struct ubi_device *ubi;
|
||||||
uint64_t tmp = from;
|
|
||||||
|
|
||||||
dbg_gen("read %zd bytes from offset %lld", len, from);
|
dbg_gen("read %zd bytes from offset %lld", len, from);
|
||||||
|
|
||||||
@@ -119,9 +118,7 @@ static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
|
|||||||
vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
|
vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
|
||||||
ubi = vol->ubi;
|
ubi = vol->ubi;
|
||||||
|
|
||||||
offs = do_div(tmp, mtd->erasesize);
|
lnum = div_u64_rem(from, mtd->erasesize, &offs);
|
||||||
lnum = tmp;
|
|
||||||
|
|
||||||
total_read = len;
|
total_read = len;
|
||||||
while (total_read) {
|
while (total_read) {
|
||||||
size_t to_read = mtd->erasesize - offs;
|
size_t to_read = mtd->erasesize - offs;
|
||||||
@@ -160,7 +157,6 @@ static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|||||||
int err = 0, lnum, offs, total_written;
|
int err = 0, lnum, offs, total_written;
|
||||||
struct ubi_volume *vol;
|
struct ubi_volume *vol;
|
||||||
struct ubi_device *ubi;
|
struct ubi_device *ubi;
|
||||||
uint64_t tmp = to;
|
|
||||||
|
|
||||||
dbg_gen("write %zd bytes to offset %lld", len, to);
|
dbg_gen("write %zd bytes to offset %lld", len, to);
|
||||||
|
|
||||||
@@ -173,8 +169,7 @@ static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|||||||
if (ubi->ro_mode)
|
if (ubi->ro_mode)
|
||||||
return -EROFS;
|
return -EROFS;
|
||||||
|
|
||||||
offs = do_div(tmp, mtd->erasesize);
|
lnum = div_u64_rem(to, mtd->erasesize, &offs);
|
||||||
lnum = tmp;
|
|
||||||
|
|
||||||
if (len % mtd->writesize || offs % mtd->writesize)
|
if (len % mtd->writesize || offs % mtd->writesize)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
#include <linux/err.h>
|
#include <linux/err.h>
|
||||||
#include <linux/crc32.h>
|
#include <linux/crc32.h>
|
||||||
#include <asm/div64.h>
|
#include <linux/math64.h>
|
||||||
#include "ubi.h"
|
#include "ubi.h"
|
||||||
|
|
||||||
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
|
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
|
||||||
@@ -904,10 +904,8 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
|
|||||||
dbg_msg("scanning is finished");
|
dbg_msg("scanning is finished");
|
||||||
|
|
||||||
/* Calculate mean erase counter */
|
/* Calculate mean erase counter */
|
||||||
if (si->ec_count) {
|
if (si->ec_count)
|
||||||
do_div(si->ec_sum, si->ec_count);
|
si->mean_ec = div_u64(si->ec_sum, si->ec_count);
|
||||||
si->mean_ec = si->ec_sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (si->is_empty)
|
if (si->is_empty)
|
||||||
ubi_msg("empty MTD device detected");
|
ubi_msg("empty MTD device detected");
|
||||||
|
@@ -40,7 +40,7 @@
|
|||||||
|
|
||||||
#include <linux/err.h>
|
#include <linux/err.h>
|
||||||
#include <linux/uaccess.h>
|
#include <linux/uaccess.h>
|
||||||
#include <asm/div64.h>
|
#include <linux/math64.h>
|
||||||
#include "ubi.h"
|
#include "ubi.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,7 +89,6 @@ static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
|
|||||||
long long bytes)
|
long long bytes)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
uint64_t tmp;
|
|
||||||
struct ubi_vtbl_record vtbl_rec;
|
struct ubi_vtbl_record vtbl_rec;
|
||||||
|
|
||||||
dbg_gen("clear update marker for volume %d", vol->vol_id);
|
dbg_gen("clear update marker for volume %d", vol->vol_id);
|
||||||
@@ -101,9 +100,9 @@ static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
|
|||||||
|
|
||||||
if (vol->vol_type == UBI_STATIC_VOLUME) {
|
if (vol->vol_type == UBI_STATIC_VOLUME) {
|
||||||
vol->corrupted = 0;
|
vol->corrupted = 0;
|
||||||
vol->used_bytes = tmp = bytes;
|
vol->used_bytes = bytes;
|
||||||
vol->last_eb_bytes = do_div(tmp, vol->usable_leb_size);
|
vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
|
||||||
vol->used_ebs = tmp;
|
&vol->last_eb_bytes);
|
||||||
if (vol->last_eb_bytes)
|
if (vol->last_eb_bytes)
|
||||||
vol->used_ebs += 1;
|
vol->used_ebs += 1;
|
||||||
else
|
else
|
||||||
@@ -131,7 +130,6 @@ int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
|
|||||||
long long bytes)
|
long long bytes)
|
||||||
{
|
{
|
||||||
int i, err;
|
int i, err;
|
||||||
uint64_t tmp;
|
|
||||||
|
|
||||||
dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
|
dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
|
||||||
ubi_assert(!vol->updating && !vol->changing_leb);
|
ubi_assert(!vol->updating && !vol->changing_leb);
|
||||||
@@ -161,9 +159,8 @@ int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
|
|||||||
if (!vol->upd_buf)
|
if (!vol->upd_buf)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
tmp = bytes;
|
vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
|
||||||
vol->upd_ebs = !!do_div(tmp, vol->usable_leb_size);
|
vol->usable_leb_size);
|
||||||
vol->upd_ebs += tmp;
|
|
||||||
vol->upd_bytes = bytes;
|
vol->upd_bytes = bytes;
|
||||||
vol->upd_received = 0;
|
vol->upd_received = 0;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -282,7 +279,6 @@ static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
|
|||||||
int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
|
int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||||
const void __user *buf, int count)
|
const void __user *buf, int count)
|
||||||
{
|
{
|
||||||
uint64_t tmp;
|
|
||||||
int lnum, offs, err = 0, len, to_write = count;
|
int lnum, offs, err = 0, len, to_write = count;
|
||||||
|
|
||||||
dbg_gen("write %d of %lld bytes, %lld already passed",
|
dbg_gen("write %d of %lld bytes, %lld already passed",
|
||||||
@@ -291,10 +287,7 @@ int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
|
|||||||
if (ubi->ro_mode)
|
if (ubi->ro_mode)
|
||||||
return -EROFS;
|
return -EROFS;
|
||||||
|
|
||||||
tmp = vol->upd_received;
|
lnum = div_u64_rem(vol->upd_received, vol->usable_leb_size, &offs);
|
||||||
offs = do_div(tmp, vol->usable_leb_size);
|
|
||||||
lnum = tmp;
|
|
||||||
|
|
||||||
if (vol->upd_received + count > vol->upd_bytes)
|
if (vol->upd_received + count > vol->upd_bytes)
|
||||||
to_write = count = vol->upd_bytes - vol->upd_received;
|
to_write = count = vol->upd_bytes - vol->upd_received;
|
||||||
|
|
||||||
|
@@ -24,7 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <linux/err.h>
|
#include <linux/err.h>
|
||||||
#include <asm/div64.h>
|
#include <linux/math64.h>
|
||||||
#include "ubi.h"
|
#include "ubi.h"
|
||||||
|
|
||||||
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
|
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
|
||||||
@@ -205,7 +205,6 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
|
|||||||
int i, err, vol_id = req->vol_id, do_free = 1;
|
int i, err, vol_id = req->vol_id, do_free = 1;
|
||||||
struct ubi_volume *vol;
|
struct ubi_volume *vol;
|
||||||
struct ubi_vtbl_record vtbl_rec;
|
struct ubi_vtbl_record vtbl_rec;
|
||||||
uint64_t bytes;
|
|
||||||
dev_t dev;
|
dev_t dev;
|
||||||
|
|
||||||
if (ubi->ro_mode)
|
if (ubi->ro_mode)
|
||||||
@@ -255,10 +254,8 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
|
|||||||
|
|
||||||
/* Calculate how many eraseblocks are requested */
|
/* Calculate how many eraseblocks are requested */
|
||||||
vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
|
vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
|
||||||
bytes = req->bytes;
|
vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
|
||||||
if (do_div(bytes, vol->usable_leb_size))
|
vol->usable_leb_size);
|
||||||
vol->reserved_pebs = 1;
|
|
||||||
vol->reserved_pebs += bytes;
|
|
||||||
|
|
||||||
/* Reserve physical eraseblocks */
|
/* Reserve physical eraseblocks */
|
||||||
if (vol->reserved_pebs > ubi->avail_pebs) {
|
if (vol->reserved_pebs > ubi->avail_pebs) {
|
||||||
@@ -301,10 +298,10 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
|
|||||||
vol->used_bytes =
|
vol->used_bytes =
|
||||||
(long long)vol->used_ebs * vol->usable_leb_size;
|
(long long)vol->used_ebs * vol->usable_leb_size;
|
||||||
} else {
|
} else {
|
||||||
bytes = vol->used_bytes;
|
vol->used_ebs = div_u64_rem(vol->used_bytes,
|
||||||
vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
|
vol->usable_leb_size,
|
||||||
vol->used_ebs = bytes;
|
&vol->last_eb_bytes);
|
||||||
if (vol->last_eb_bytes)
|
if (vol->last_eb_bytes != 0)
|
||||||
vol->used_ebs += 1;
|
vol->used_ebs += 1;
|
||||||
else
|
else
|
||||||
vol->last_eb_bytes = vol->usable_leb_size;
|
vol->last_eb_bytes = vol->usable_leb_size;
|
||||||
|
Reference in New Issue
Block a user