Skip to content

Commit 9ff0897

Browse files
UBI: Add initial support for scatter gather
Adds a new set of functions to deal with scatter gather. ubi_eba_read_leb_sg() will read from a LEB into a scatter gather list. The new data structure struct ubi_sgl will be used within UBI to hold the scatter gather list itself and metadata to have a cursor within the list. Signed-off-by: Richard Weinberger <richard@nod.at> Tested-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar> Reviewed-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
1 parent 892abde commit 9ff0897

4 files changed

Lines changed: 186 additions & 15 deletions

File tree

‎drivers/mtd/ubi/eba.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,61 @@ int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
479479
return err;
480480
}
481481

482+
/**
483+
* ubi_eba_read_leb_sg - read data into a scatter gather list.
484+
* @ubi: UBI device description object
485+
* @vol: volume description object
486+
* @lnum: logical eraseblock number
487+
* @sgl: UBI scatter gather list to store the read data
488+
* @offset: offset from where to read
489+
* @len: how many bytes to read
490+
* @check: data CRC check flag
491+
*
492+
* This function works exactly like ubi_eba_read_leb(). But instead of
493+
* storing the read data into a buffer it writes to an UBI scatter gather
494+
* list.
495+
*/
496+
int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
497+
struct ubi_sgl *sgl, int lnum, int offset, int len,
498+
int check)
499+
{
500+
int to_read;
501+
int ret;
502+
struct scatterlist *sg;
503+
504+
for (;;) {
505+
ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
506+
sg = &sgl->sg[sgl->list_pos];
507+
if (len < sg->length - sgl->page_pos)
508+
to_read = len;
509+
else
510+
to_read = sg->length - sgl->page_pos;
511+
512+
ret = ubi_eba_read_leb(ubi, vol, lnum,
513+
sg_virt(sg) + sgl->page_pos, offset,
514+
to_read, check);
515+
if (ret < 0)
516+
return ret;
517+
518+
offset += to_read;
519+
len -= to_read;
520+
if (!len) {
521+
sgl->page_pos += to_read;
522+
if (sgl->page_pos == sg->length) {
523+
sgl->list_pos++;
524+
sgl->page_pos = 0;
525+
}
526+
527+
break;
528+
}
529+
530+
sgl->list_pos++;
531+
sgl->page_pos = 0;
532+
}
533+
534+
return ret;
535+
}
536+
482537
/**
483538
* recover_peb - recover from write failure.
484539
* @ubi: UBI device description object

‎drivers/mtd/ubi/kapi.c‎

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,43 @@ void ubi_close_volume(struct ubi_volume_desc *desc)
365365
}
366366
EXPORT_SYMBOL_GPL(ubi_close_volume);
367367

368+
/**
369+
* leb_read_sanity_check - does sanity checks on read requests.
370+
* @desc: volume descriptor
371+
* @lnum: logical eraseblock number to read from
372+
* @offset: offset within the logical eraseblock to read from
373+
* @len: how many bytes to read
374+
*
375+
* This function is used by ubi_leb_read() and ubi_leb_read_sg()
376+
* to perform sanity checks.
377+
*/
378+
static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
379+
int offset, int len)
380+
{
381+
struct ubi_volume *vol = desc->vol;
382+
struct ubi_device *ubi = vol->ubi;
383+
int vol_id = vol->vol_id;
384+
385+
if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
386+
lnum >= vol->used_ebs || offset < 0 || len < 0 ||
387+
offset + len > vol->usable_leb_size)
388+
return -EINVAL;
389+
390+
if (vol->vol_type == UBI_STATIC_VOLUME) {
391+
if (vol->used_ebs == 0)
392+
/* Empty static UBI volume */
393+
return 0;
394+
if (lnum == vol->used_ebs - 1 &&
395+
offset + len > vol->last_eb_bytes)
396+
return -EINVAL;
397+
}
398+
399+
if (vol->upd_marker)
400+
return -EBADF;
401+
402+
return 0;
403+
}
404+
368405
/**
369406
* ubi_leb_read - read data.
370407
* @desc: volume descriptor
@@ -401,22 +438,10 @@ int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
401438

402439
dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
403440

404-
if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
405-
lnum >= vol->used_ebs || offset < 0 || len < 0 ||
406-
offset + len > vol->usable_leb_size)
407-
return -EINVAL;
408-
409-
if (vol->vol_type == UBI_STATIC_VOLUME) {
410-
if (vol->used_ebs == 0)
411-
/* Empty static UBI volume */
412-
return 0;
413-
if (lnum == vol->used_ebs - 1 &&
414-
offset + len > vol->last_eb_bytes)
415-
return -EINVAL;
416-
}
441+
err = leb_read_sanity_check(desc, lnum, offset, len);
442+
if (err < 0)
443+
return err;
417444

418-
if (vol->upd_marker)
419-
return -EBADF;
420445
if (len == 0)
421446
return 0;
422447

@@ -430,6 +455,46 @@ int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
430455
}
431456
EXPORT_SYMBOL_GPL(ubi_leb_read);
432457

458+
459+
/**
460+
* ubi_leb_read_sg - read data into a scatter gather list.
461+
* @desc: volume descriptor
462+
* @lnum: logical eraseblock number to read from
463+
* @buf: buffer where to store the read data
464+
* @offset: offset within the logical eraseblock to read from
465+
* @len: how many bytes to read
466+
* @check: whether UBI has to check the read data's CRC or not.
467+
*
468+
* This function works exactly like ubi_leb_read_sg(). But instead of
469+
* storing the read data into a buffer it writes to an UBI scatter gather
470+
* list.
471+
*/
472+
int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
473+
int offset, int len, int check)
474+
{
475+
struct ubi_volume *vol = desc->vol;
476+
struct ubi_device *ubi = vol->ubi;
477+
int err, vol_id = vol->vol_id;
478+
479+
dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
480+
481+
err = leb_read_sanity_check(desc, lnum, offset, len);
482+
if (err < 0)
483+
return err;
484+
485+
if (len == 0)
486+
return 0;
487+
488+
err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
489+
if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
490+
ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
491+
vol->corrupted = 1;
492+
}
493+
494+
return err;
495+
}
496+
EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
497+
433498
/**
434499
* ubi_leb_write - write data.
435500
* @desc: volume descriptor

‎drivers/mtd/ubi/ubi.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,9 @@ int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
795795
int lnum);
796796
int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
797797
void *buf, int offset, int len, int check);
798+
int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
799+
struct ubi_sgl *sgl, int lnum, int offset, int len,
800+
int check);
798801
int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
799802
const void *buf, int offset, int len);
800803
int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,

‎include/linux/mtd/ubi.h‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@
2323

2424
#include <linux/ioctl.h>
2525
#include <linux/types.h>
26+
#include <linux/scatterlist.h>
2627
#include <mtd/ubi-user.h>
2728

2829
/* All voumes/LEBs */
2930
#define UBI_ALL -1
3031

32+
/*
33+
* Maximum number of scatter gather list entries,
34+
* we use only 64 to have a lower memory foot print.
35+
*/
36+
#define UBI_MAX_SG_COUNT 64
37+
3138
/*
3239
* enum ubi_open_mode - UBI volume open mode constants.
3340
*
@@ -118,6 +125,35 @@ struct ubi_volume_info {
118125
dev_t cdev;
119126
};
120127

128+
/**
129+
* struct ubi_sgl - UBI scatter gather list data structure.
130+
* @list_pos: current position in @sg[]
131+
* @page_pos: current position in @sg[@list_pos]
132+
* @sg: the scatter gather list itself
133+
*
134+
* ubi_sgl is a wrapper around a scatter list which keeps track of the
135+
* current position in the list and the current list item such that
136+
* it can be used across multiple ubi_leb_read_sg() calls.
137+
*/
138+
struct ubi_sgl {
139+
int list_pos;
140+
int page_pos;
141+
struct scatterlist sg[UBI_MAX_SG_COUNT];
142+
};
143+
144+
/**
145+
* ubi_sgl_init - initialize an UBI scatter gather list data structure.
146+
* @usgl: the UBI scatter gather struct itself
147+
*
148+
* Please note that you still have to use sg_init_table() or any adequate
149+
* function to initialize the unterlaying struct scatterlist.
150+
*/
151+
static inline void ubi_sgl_init(struct ubi_sgl *usgl)
152+
{
153+
usgl->list_pos = 0;
154+
usgl->page_pos = 0;
155+
}
156+
121157
/**
122158
* struct ubi_device_info - UBI device description data structure.
123159
* @ubi_num: ubi device number
@@ -213,6 +249,8 @@ int ubi_unregister_volume_notifier(struct notifier_block *nb);
213249
void ubi_close_volume(struct ubi_volume_desc *desc);
214250
int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
215251
int len, int check);
252+
int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
253+
int offset, int len, int check);
216254
int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
217255
int offset, int len);
218256
int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
@@ -233,4 +271,14 @@ static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
233271
{
234272
return ubi_leb_read(desc, lnum, buf, offset, len, 0);
235273
}
274+
275+
/*
276+
* This function is the same as the 'ubi_leb_read_sg()' function, but it does
277+
* not provide the checking capability.
278+
*/
279+
static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum,
280+
struct ubi_sgl *sgl, int offset, int len)
281+
{
282+
return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0);
283+
}
236284
#endif /* !__LINUX_UBI_H__ */

0 commit comments

Comments
 (0)