Commit 3013ee31b6c5fd9a49a81816d6c13e1cdb7a1288

Authored by Artem Bityutskiy
1 parent f429b2ea8e

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>

Showing 5 changed files with 25 additions and 52 deletions Side-by-side Diff

drivers/mtd/ubi/cdev.c
... ... @@ -41,8 +41,8 @@
41 41 #include <linux/capability.h>
42 42 #include <linux/uaccess.h>
43 43 #include <linux/compat.h>
  44 +#include <linux/math64.h>
44 45 #include <mtd/ubi-user.h>
45   -#include <asm/div64.h>
46 46 #include "ubi.h"
47 47  
48 48 /**
... ... @@ -195,7 +195,6 @@
195 195 int err, lnum, off, len, tbuf_size;
196 196 size_t count_save = count;
197 197 void *tbuf;
198   - uint64_t tmp;
199 198  
200 199 dbg_gen("read %zd bytes from offset %lld of volume %d",
201 200 count, *offp, vol->vol_id);
202 201  
... ... @@ -225,11 +224,8 @@
225 224 return -ENOMEM;
226 225  
227 226 len = count > tbuf_size ? tbuf_size : count;
  227 + lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
228 228  
229   - tmp = *offp;
230   - off = do_div(tmp, vol->usable_leb_size);
231   - lnum = tmp;
232   -
233 229 do {
234 230 cond_resched();
235 231  
... ... @@ -279,7 +275,6 @@
279 275 int lnum, off, len, tbuf_size, err = 0;
280 276 size_t count_save = count;
281 277 char *tbuf;
282   - uint64_t tmp;
283 278  
284 279 dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
285 280 count, *offp, vol->vol_id);
... ... @@ -287,10 +282,7 @@
287 282 if (vol->vol_type == UBI_STATIC_VOLUME)
288 283 return -EROFS;
289 284  
290   - tmp = *offp;
291   - off = do_div(tmp, vol->usable_leb_size);
292   - lnum = tmp;
293   -
  285 + lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
294 286 if (off & (ubi->min_io_size - 1)) {
295 287 dbg_err("unaligned position");
296 288 return -EINVAL;
... ... @@ -882,7 +874,6 @@
882 874 case UBI_IOCRSVOL:
883 875 {
884 876 int pebs;
885   - uint64_t tmp;
886 877 struct ubi_rsvol_req req;
887 878  
888 879 dbg_gen("re-size volume");
... ... @@ -902,9 +893,8 @@
902 893 break;
903 894 }
904 895  
905   - tmp = req.bytes;
906   - pebs = !!do_div(tmp, desc->vol->usable_leb_size);
907   - pebs += tmp;
  896 + pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
  897 + desc->vol->usable_leb_size);
908 898  
909 899 mutex_lock(&ubi->volumes_mutex);
910 900 err = ubi_resize_volume(desc, pebs);
drivers/mtd/ubi/gluebi.c
... ... @@ -28,7 +28,7 @@
28 28 * eraseblock size is equivalent to the logical eraseblock size of the volume.
29 29 */
30 30  
31   -#include <asm/div64.h>
  31 +#include <linux/math64.h>
32 32 #include "ubi.h"
33 33  
34 34 /**
... ... @@ -109,7 +109,6 @@
109 109 int err = 0, lnum, offs, total_read;
110 110 struct ubi_volume *vol;
111 111 struct ubi_device *ubi;
112   - uint64_t tmp = from;
113 112  
114 113 dbg_gen("read %zd bytes from offset %lld", len, from);
115 114  
... ... @@ -119,9 +118,7 @@
119 118 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
120 119 ubi = vol->ubi;
121 120  
122   - offs = do_div(tmp, mtd->erasesize);
123   - lnum = tmp;
124   -
  121 + lnum = div_u64_rem(from, mtd->erasesize, &offs);
125 122 total_read = len;
126 123 while (total_read) {
127 124 size_t to_read = mtd->erasesize - offs;
... ... @@ -160,7 +157,6 @@
160 157 int err = 0, lnum, offs, total_written;
161 158 struct ubi_volume *vol;
162 159 struct ubi_device *ubi;
163   - uint64_t tmp = to;
164 160  
165 161 dbg_gen("write %zd bytes to offset %lld", len, to);
166 162  
... ... @@ -173,8 +169,7 @@
173 169 if (ubi->ro_mode)
174 170 return -EROFS;
175 171  
176   - offs = do_div(tmp, mtd->erasesize);
177   - lnum = tmp;
  172 + lnum = div_u64_rem(to, mtd->erasesize, &offs);
178 173  
179 174 if (len % mtd->writesize || offs % mtd->writesize)
180 175 return -EINVAL;
drivers/mtd/ubi/scan.c
... ... @@ -42,7 +42,7 @@
42 42  
43 43 #include <linux/err.h>
44 44 #include <linux/crc32.h>
45   -#include <asm/div64.h>
  45 +#include <linux/math64.h>
46 46 #include "ubi.h"
47 47  
48 48 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
... ... @@ -904,10 +904,8 @@
904 904 dbg_msg("scanning is finished");
905 905  
906 906 /* Calculate mean erase counter */
907   - if (si->ec_count) {
908   - do_div(si->ec_sum, si->ec_count);
909   - si->mean_ec = si->ec_sum;
910   - }
  907 + if (si->ec_count)
  908 + si->mean_ec = div_u64(si->ec_sum, si->ec_count);
911 909  
912 910 if (si->is_empty)
913 911 ubi_msg("empty MTD device detected");
drivers/mtd/ubi/upd.c
... ... @@ -40,7 +40,7 @@
40 40  
41 41 #include <linux/err.h>
42 42 #include <linux/uaccess.h>
43   -#include <asm/div64.h>
  43 +#include <linux/math64.h>
44 44 #include "ubi.h"
45 45  
46 46 /**
... ... @@ -89,7 +89,6 @@
89 89 long long bytes)
90 90 {
91 91 int err;
92   - uint64_t tmp;
93 92 struct ubi_vtbl_record vtbl_rec;
94 93  
95 94 dbg_gen("clear update marker for volume %d", vol->vol_id);
... ... @@ -101,9 +100,9 @@
101 100  
102 101 if (vol->vol_type == UBI_STATIC_VOLUME) {
103 102 vol->corrupted = 0;
104   - vol->used_bytes = tmp = bytes;
105   - vol->last_eb_bytes = do_div(tmp, vol->usable_leb_size);
106   - vol->used_ebs = tmp;
  103 + vol->used_bytes = bytes;
  104 + vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
  105 + &vol->last_eb_bytes);
107 106 if (vol->last_eb_bytes)
108 107 vol->used_ebs += 1;
109 108 else
... ... @@ -131,7 +130,6 @@
131 130 long long bytes)
132 131 {
133 132 int i, err;
134   - uint64_t tmp;
135 133  
136 134 dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
137 135 ubi_assert(!vol->updating && !vol->changing_leb);
... ... @@ -161,9 +159,8 @@
161 159 if (!vol->upd_buf)
162 160 return -ENOMEM;
163 161  
164   - tmp = bytes;
165   - vol->upd_ebs = !!do_div(tmp, vol->usable_leb_size);
166   - vol->upd_ebs += tmp;
  162 + vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
  163 + vol->usable_leb_size);
167 164 vol->upd_bytes = bytes;
168 165 vol->upd_received = 0;
169 166 return 0;
... ... @@ -282,7 +279,6 @@
282 279 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
283 280 const void __user *buf, int count)
284 281 {
285   - uint64_t tmp;
286 282 int lnum, offs, err = 0, len, to_write = count;
287 283  
288 284 dbg_gen("write %d of %lld bytes, %lld already passed",
... ... @@ -291,10 +287,7 @@
291 287 if (ubi->ro_mode)
292 288 return -EROFS;
293 289  
294   - tmp = vol->upd_received;
295   - offs = do_div(tmp, vol->usable_leb_size);
296   - lnum = tmp;
297   -
  290 + lnum = div_u64_rem(vol->upd_received, vol->usable_leb_size, &offs);
298 291 if (vol->upd_received + count > vol->upd_bytes)
299 292 to_write = count = vol->upd_bytes - vol->upd_received;
300 293  
drivers/mtd/ubi/vmt.c
... ... @@ -24,7 +24,7 @@
24 24 */
25 25  
26 26 #include <linux/err.h>
27   -#include <asm/div64.h>
  27 +#include <linux/math64.h>
28 28 #include "ubi.h"
29 29  
30 30 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
... ... @@ -205,7 +205,6 @@
205 205 int i, err, vol_id = req->vol_id, do_free = 1;
206 206 struct ubi_volume *vol;
207 207 struct ubi_vtbl_record vtbl_rec;
208   - uint64_t bytes;
209 208 dev_t dev;
210 209  
211 210 if (ubi->ro_mode)
... ... @@ -255,10 +254,8 @@
255 254  
256 255 /* Calculate how many eraseblocks are requested */
257 256 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
258   - bytes = req->bytes;
259   - if (do_div(bytes, vol->usable_leb_size))
260   - vol->reserved_pebs = 1;
261   - vol->reserved_pebs += bytes;
  257 + vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
  258 + vol->usable_leb_size);
262 259  
263 260 /* Reserve physical eraseblocks */
264 261 if (vol->reserved_pebs > ubi->avail_pebs) {
... ... @@ -301,10 +298,10 @@
301 298 vol->used_bytes =
302 299 (long long)vol->used_ebs * vol->usable_leb_size;
303 300 } else {
304   - bytes = vol->used_bytes;
305   - vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
306   - vol->used_ebs = bytes;
307   - if (vol->last_eb_bytes)
  301 + vol->used_ebs = div_u64_rem(vol->used_bytes,
  302 + vol->usable_leb_size,
  303 + &vol->last_eb_bytes);
  304 + if (vol->last_eb_bytes != 0)
308 305 vol->used_ebs += 1;
309 306 else
310 307 vol->last_eb_bytes = vol->usable_leb_size;