Skip to content

Commit

Permalink
UBI: use nicer 64-bit math
Browse files Browse the repository at this point in the history
Get rid of 'do_div()' and use more user-friendly primitives from
'linux/math64.h'.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
  • Loading branch information
Artem Bityutskiy authored and Artem Bityutskiy committed Jan 18, 2009
1 parent f429b2e commit 3013ee3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 52 deletions.
20 changes: 5 additions & 15 deletions drivers/mtd/ubi/cdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
#include <linux/capability.h>
#include <linux/uaccess.h>
#include <linux/compat.h>
#include <linux/math64.h>
#include <mtd/ubi-user.h>
#include <asm/div64.h>
#include "ubi.h"

/**
Expand Down Expand Up @@ -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;
size_t count_save = count;
void *tbuf;
uint64_t tmp;

dbg_gen("read %zd bytes from offset %lld of volume %d",
count, *offp, vol->vol_id);
Expand Down Expand Up @@ -225,10 +224,7 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
return -ENOMEM;

len = count > tbuf_size ? tbuf_size : count;

tmp = *offp;
off = do_div(tmp, vol->usable_leb_size);
lnum = tmp;
lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);

do {
cond_resched();
Expand Down Expand Up @@ -279,18 +275,14 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
int lnum, off, len, tbuf_size, err = 0;
size_t count_save = count;
char *tbuf;
uint64_t tmp;

dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
count, *offp, vol->vol_id);

if (vol->vol_type == UBI_STATIC_VOLUME)
return -EROFS;

tmp = *offp;
off = do_div(tmp, vol->usable_leb_size);
lnum = tmp;

lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
if (off & (ubi->min_io_size - 1)) {
dbg_err("unaligned position");
return -EINVAL;
Expand Down Expand Up @@ -882,7 +874,6 @@ static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
case UBI_IOCRSVOL:
{
int pebs;
uint64_t tmp;
struct ubi_rsvol_req req;

dbg_gen("re-size volume");
Expand All @@ -902,9 +893,8 @@ static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
break;
}

tmp = req.bytes;
pebs = !!do_div(tmp, desc->vol->usable_leb_size);
pebs += tmp;
pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
desc->vol->usable_leb_size);

mutex_lock(&ubi->volumes_mutex);
err = ubi_resize_volume(desc, pebs);
Expand Down
11 changes: 3 additions & 8 deletions drivers/mtd/ubi/gluebi.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* eraseblock size is equivalent to the logical eraseblock size of the volume.
*/

#include <asm/div64.h>
#include <linux/math64.h>
#include "ubi.h"

/**
Expand Down Expand Up @@ -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;
struct ubi_volume *vol;
struct ubi_device *ubi;
uint64_t tmp = from;

dbg_gen("read %zd bytes from offset %lld", len, from);

Expand All @@ -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);
ubi = vol->ubi;

offs = do_div(tmp, mtd->erasesize);
lnum = tmp;

lnum = div_u64_rem(from, mtd->erasesize, &offs);
total_read = len;
while (total_read) {
size_t to_read = mtd->erasesize - offs;
Expand Down Expand Up @@ -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;
struct ubi_volume *vol;
struct ubi_device *ubi;
uint64_t tmp = to;

dbg_gen("write %zd bytes to offset %lld", len, to);

Expand All @@ -173,8 +169,7 @@ static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
if (ubi->ro_mode)
return -EROFS;

offs = do_div(tmp, mtd->erasesize);
lnum = tmp;
lnum = div_u64_rem(to, mtd->erasesize, &offs);

if (len % mtd->writesize || offs % mtd->writesize)
return -EINVAL;
Expand Down
8 changes: 3 additions & 5 deletions drivers/mtd/ubi/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#include <linux/err.h>
#include <linux/crc32.h>
#include <asm/div64.h>
#include <linux/math64.h>
#include "ubi.h"

#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
Expand Down Expand Up @@ -904,10 +904,8 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
dbg_msg("scanning is finished");

/* Calculate mean erase counter */
if (si->ec_count) {
do_div(si->ec_sum, si->ec_count);
si->mean_ec = si->ec_sum;
}
if (si->ec_count)
si->mean_ec = div_u64(si->ec_sum, si->ec_count);

if (si->is_empty)
ubi_msg("empty MTD device detected");
Expand Down
21 changes: 7 additions & 14 deletions drivers/mtd/ubi/upd.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

#include <linux/err.h>
#include <linux/uaccess.h>
#include <asm/div64.h>
#include <linux/math64.h>
#include "ubi.h"

/**
Expand Down Expand Up @@ -89,7 +89,6 @@ static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
long long bytes)
{
int err;
uint64_t tmp;
struct ubi_vtbl_record vtbl_rec;

dbg_gen("clear update marker for volume %d", vol->vol_id);
Expand All @@ -101,9 +100,9 @@ static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,

if (vol->vol_type == UBI_STATIC_VOLUME) {
vol->corrupted = 0;
vol->used_bytes = tmp = bytes;
vol->last_eb_bytes = do_div(tmp, vol->usable_leb_size);
vol->used_ebs = tmp;
vol->used_bytes = bytes;
vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
&vol->last_eb_bytes);
if (vol->last_eb_bytes)
vol->used_ebs += 1;
else
Expand Down Expand Up @@ -131,7 +130,6 @@ int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
long long bytes)
{
int i, err;
uint64_t tmp;

dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
ubi_assert(!vol->updating && !vol->changing_leb);
Expand Down Expand Up @@ -161,9 +159,8 @@ int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
if (!vol->upd_buf)
return -ENOMEM;

tmp = bytes;
vol->upd_ebs = !!do_div(tmp, vol->usable_leb_size);
vol->upd_ebs += tmp;
vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
vol->usable_leb_size);
vol->upd_bytes = bytes;
vol->upd_received = 0;
return 0;
Expand Down Expand Up @@ -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,
const void __user *buf, int count)
{
uint64_t tmp;
int lnum, offs, err = 0, len, to_write = count;

dbg_gen("write %d of %lld bytes, %lld already passed",
Expand All @@ -291,10 +287,7 @@ int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
if (ubi->ro_mode)
return -EROFS;

tmp = vol->upd_received;
offs = do_div(tmp, vol->usable_leb_size);
lnum = tmp;

lnum = div_u64_rem(vol->upd_received, vol->usable_leb_size, &offs);
if (vol->upd_received + count > vol->upd_bytes)
to_write = count = vol->upd_bytes - vol->upd_received;

Expand Down
17 changes: 7 additions & 10 deletions drivers/mtd/ubi/vmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

#include <linux/err.h>
#include <asm/div64.h>
#include <linux/math64.h>
#include "ubi.h"

#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
Expand Down Expand Up @@ -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;
struct ubi_volume *vol;
struct ubi_vtbl_record vtbl_rec;
uint64_t bytes;
dev_t dev;

if (ubi->ro_mode)
Expand Down Expand Up @@ -255,10 +254,8 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)

/* Calculate how many eraseblocks are requested */
vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
bytes = req->bytes;
if (do_div(bytes, vol->usable_leb_size))
vol->reserved_pebs = 1;
vol->reserved_pebs += bytes;
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) {
Expand Down Expand Up @@ -301,10 +298,10 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
vol->used_bytes =
(long long)vol->used_ebs * vol->usable_leb_size;
} else {
bytes = vol->used_bytes;
vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
vol->used_ebs = bytes;
if (vol->last_eb_bytes)
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;
Expand Down

0 comments on commit 3013ee3

Please sign in to comment.