Skip to content

Commit 6208e77

Browse files
committed
Merge git://git.infradead.org/~dedekind/ubi-2.6
2 parents 5eb9103 + 6dc4a87 commit 6208e77

13 files changed

Lines changed: 441 additions & 188 deletions

File tree

‎drivers/mtd/ubi/build.c‎

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
6666
/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
6767
struct class *ubi_class;
6868

69-
/* Slab cache for lock-tree entries */
70-
struct kmem_cache *ubi_ltree_slab;
71-
7269
/* Slab cache for wear-leveling entries */
7370
struct kmem_cache *ubi_wl_entry_slab;
7471

@@ -369,9 +366,6 @@ static int uif_init(struct ubi_device *ubi)
369366
int i, err;
370367
dev_t dev;
371368

372-
mutex_init(&ubi->volumes_mutex);
373-
spin_lock_init(&ubi->volumes_lock);
374-
375369
sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
376370

377371
/*
@@ -568,7 +562,7 @@ static int io_init(struct ubi_device *ubi)
568562
}
569563

570564
/* Similar for the data offset */
571-
ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
565+
ubi->leb_start = ubi->vid_hdr_offset + UBI_EC_HDR_SIZE;
572566
ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
573567

574568
dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
@@ -626,6 +620,58 @@ static int io_init(struct ubi_device *ubi)
626620
return 0;
627621
}
628622

623+
/**
624+
* autoresize - re-size the volume which has the "auto-resize" flag set.
625+
* @ubi: UBI device description object
626+
* @vol_id: ID of the volume to re-size
627+
*
628+
* This function re-sizes the volume marked by the @UBI_VTBL_AUTORESIZE_FLG in
629+
* the volume table to the largest possible size. See comments in ubi-header.h
630+
* for more description of the flag. Returns zero in case of success and a
631+
* negative error code in case of failure.
632+
*/
633+
static int autoresize(struct ubi_device *ubi, int vol_id)
634+
{
635+
struct ubi_volume_desc desc;
636+
struct ubi_volume *vol = ubi->volumes[vol_id];
637+
int err, old_reserved_pebs = vol->reserved_pebs;
638+
639+
/*
640+
* Clear the auto-resize flag in the volume in-memory copy of the
641+
* volume table, and 'ubi_resize_volume()' will propogate this change
642+
* to the flash.
643+
*/
644+
ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
645+
646+
if (ubi->avail_pebs == 0) {
647+
struct ubi_vtbl_record vtbl_rec;
648+
649+
/*
650+
* No avalilable PEBs to re-size the volume, clear the flag on
651+
* flash and exit.
652+
*/
653+
memcpy(&vtbl_rec, &ubi->vtbl[vol_id],
654+
sizeof(struct ubi_vtbl_record));
655+
err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
656+
if (err)
657+
ubi_err("cannot clean auto-resize flag for volume %d",
658+
vol_id);
659+
} else {
660+
desc.vol = vol;
661+
err = ubi_resize_volume(&desc,
662+
old_reserved_pebs + ubi->avail_pebs);
663+
if (err)
664+
ubi_err("cannot auto-resize volume %d", vol_id);
665+
}
666+
667+
if (err)
668+
return err;
669+
670+
ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,
671+
vol->name, old_reserved_pebs, vol->reserved_pebs);
672+
return 0;
673+
}
674+
629675
/**
630676
* ubi_attach_mtd_dev - attach an MTD device.
631677
* @mtd_dev: MTD device description object
@@ -702,6 +748,12 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
702748
ubi->mtd = mtd;
703749
ubi->ubi_num = ubi_num;
704750
ubi->vid_hdr_offset = vid_hdr_offset;
751+
ubi->autoresize_vol_id = -1;
752+
753+
mutex_init(&ubi->buf_mutex);
754+
mutex_init(&ubi->ckvol_mutex);
755+
mutex_init(&ubi->volumes_mutex);
756+
spin_lock_init(&ubi->volumes_lock);
705757

706758
dbg_msg("attaching mtd%d to ubi%d: VID header offset %d",
707759
mtd->index, ubi_num, vid_hdr_offset);
@@ -710,8 +762,6 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
710762
if (err)
711763
goto out_free;
712764

713-
mutex_init(&ubi->buf_mutex);
714-
mutex_init(&ubi->ckvol_mutex);
715765
ubi->peb_buf1 = vmalloc(ubi->peb_size);
716766
if (!ubi->peb_buf1)
717767
goto out_free;
@@ -733,6 +783,12 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
733783
goto out_free;
734784
}
735785

786+
if (ubi->autoresize_vol_id != -1) {
787+
err = autoresize(ubi, ubi->autoresize_vol_id);
788+
if (err)
789+
goto out_detach;
790+
}
791+
736792
err = uif_init(ubi);
737793
if (err)
738794
goto out_detach;
@@ -857,20 +913,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
857913
return 0;
858914
}
859915

860-
/**
861-
* ltree_entry_ctor - lock tree entries slab cache constructor.
862-
* @obj: the lock-tree entry to construct
863-
* @cache: the lock tree entry slab cache
864-
* @flags: constructor flags
865-
*/
866-
static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)
867-
{
868-
struct ubi_ltree_entry *le = obj;
869-
870-
le->users = 0;
871-
init_rwsem(&le->mutex);
872-
}
873-
874916
/**
875917
* find_mtd_device - open an MTD device by its name or number.
876918
* @mtd_dev: name or number of the device
@@ -933,17 +975,11 @@ static int __init ubi_init(void)
933975
goto out_version;
934976
}
935977

936-
ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab",
937-
sizeof(struct ubi_ltree_entry), 0,
938-
0, &ltree_entry_ctor);
939-
if (!ubi_ltree_slab)
940-
goto out_dev_unreg;
941-
942978
ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
943979
sizeof(struct ubi_wl_entry),
944980
0, 0, NULL);
945981
if (!ubi_wl_entry_slab)
946-
goto out_ltree;
982+
goto out_dev_unreg;
947983

948984
/* Attach MTD devices */
949985
for (i = 0; i < mtd_devs; i++) {
@@ -980,8 +1016,6 @@ static int __init ubi_init(void)
9801016
mutex_unlock(&ubi_devices_mutex);
9811017
}
9821018
kmem_cache_destroy(ubi_wl_entry_slab);
983-
out_ltree:
984-
kmem_cache_destroy(ubi_ltree_slab);
9851019
out_dev_unreg:
9861020
misc_deregister(&ubi_ctrl_cdev);
9871021
out_version:
@@ -1005,7 +1039,6 @@ static void __exit ubi_exit(void)
10051039
mutex_unlock(&ubi_devices_mutex);
10061040
}
10071041
kmem_cache_destroy(ubi_wl_entry_slab);
1008-
kmem_cache_destroy(ubi_ltree_slab);
10091042
misc_deregister(&ubi_ctrl_cdev);
10101043
class_remove_file(ubi_class, &ubi_version);
10111044
class_destroy(ubi_class);
@@ -1066,7 +1099,7 @@ static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
10661099
struct mtd_dev_param *p;
10671100
char buf[MTD_PARAM_LEN_MAX];
10681101
char *pbuf = &buf[0];
1069-
char *tokens[3] = {NULL, NULL, NULL};
1102+
char *tokens[2] = {NULL, NULL};
10701103

10711104
if (!val)
10721105
return -EINVAL;
@@ -1096,7 +1129,7 @@ static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
10961129
if (buf[len - 1] == '\n')
10971130
buf[len - 1] = '\0';
10981131

1099-
for (i = 0; i < 3; i++)
1132+
for (i = 0; i < 2; i++)
11001133
tokens[i] = strsep(&pbuf, ",");
11011134

11021135
if (pbuf) {

‎drivers/mtd/ubi/cdev.c‎

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,15 @@ static int vol_cdev_release(struct inode *inode, struct file *file)
132132
if (vol->updating) {
133133
ubi_warn("update of volume %d not finished, volume is damaged",
134134
vol->vol_id);
135+
ubi_assert(!vol->changing_leb);
135136
vol->updating = 0;
136137
vfree(vol->upd_buf);
138+
} else if (vol->changing_leb) {
139+
dbg_msg("only %lld of %lld bytes received for atomic LEB change"
140+
" for volume %d:%d, cancel", vol->upd_received,
141+
vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
142+
vol->changing_leb = 0;
143+
vfree(vol->upd_buf);
137144
}
138145

139146
ubi_close_volume(desc);
@@ -184,13 +191,13 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
184191
struct ubi_volume_desc *desc = file->private_data;
185192
struct ubi_volume *vol = desc->vol;
186193
struct ubi_device *ubi = vol->ubi;
187-
int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size;
194+
int err, lnum, off, len, tbuf_size;
188195
size_t count_save = count;
189196
void *tbuf;
190197
uint64_t tmp;
191198

192199
dbg_msg("read %zd bytes from offset %lld of volume %d",
193-
count, *offp, vol_id);
200+
count, *offp, vol->vol_id);
194201

195202
if (vol->updating) {
196203
dbg_err("updating");
@@ -204,7 +211,7 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
204211
return 0;
205212

206213
if (vol->corrupted)
207-
dbg_msg("read from corrupted volume %d", vol_id);
214+
dbg_msg("read from corrupted volume %d", vol->vol_id);
208215

209216
if (*offp + count > vol->used_bytes)
210217
count_save = count = vol->used_bytes - *offp;
@@ -274,7 +281,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
274281
uint64_t tmp;
275282

276283
dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
277-
count, *offp, desc->vol->vol_id);
284+
count, *offp, vol->vol_id);
278285

279286
if (vol->vol_type == UBI_STATIC_VOLUME)
280287
return -EROFS;
@@ -351,23 +358,32 @@ static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
351358
struct ubi_volume *vol = desc->vol;
352359
struct ubi_device *ubi = vol->ubi;
353360

354-
if (!vol->updating)
361+
if (!vol->updating && !vol->changing_leb)
355362
return vol_cdev_direct_write(file, buf, count, offp);
356363

357-
err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
364+
if (vol->updating)
365+
err = ubi_more_update_data(ubi, vol, buf, count);
366+
else
367+
err = ubi_more_leb_change_data(ubi, vol, buf, count);
368+
358369
if (err < 0) {
359-
ubi_err("cannot write %zd bytes of update data, error %d",
370+
ubi_err("cannot accept more %zd bytes of data, error %d",
360371
count, err);
361372
return err;
362373
}
363374

364375
if (err) {
365376
/*
366-
* Update is finished, @err contains number of actually written
367-
* bytes now.
377+
* The operation is finished, @err contains number of actually
378+
* written bytes.
368379
*/
369380
count = err;
370381

382+
if (vol->changing_leb) {
383+
revoke_exclusive(desc, UBI_READWRITE);
384+
return count;
385+
}
386+
371387
err = ubi_check_volume(ubi, vol->vol_id);
372388
if (err < 0)
373389
return err;
@@ -382,7 +398,6 @@ static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
382398
revoke_exclusive(desc, UBI_READWRITE);
383399
}
384400

385-
*offp += count;
386401
return count;
387402
}
388403

@@ -427,11 +442,46 @@ static int vol_cdev_ioctl(struct inode *inode, struct file *file,
427442
if (err < 0)
428443
break;
429444

430-
err = ubi_start_update(ubi, vol->vol_id, bytes);
445+
err = ubi_start_update(ubi, vol, bytes);
431446
if (bytes == 0)
432447
revoke_exclusive(desc, UBI_READWRITE);
448+
break;
449+
}
450+
451+
/* Atomic logical eraseblock change command */
452+
case UBI_IOCEBCH:
453+
{
454+
struct ubi_leb_change_req req;
455+
456+
err = copy_from_user(&req, argp,
457+
sizeof(struct ubi_leb_change_req));
458+
if (err) {
459+
err = -EFAULT;
460+
break;
461+
}
462+
463+
if (desc->mode == UBI_READONLY ||
464+
vol->vol_type == UBI_STATIC_VOLUME) {
465+
err = -EROFS;
466+
break;
467+
}
468+
469+
/* Validate the request */
470+
err = -EINVAL;
471+
if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
472+
req.bytes < 0 || req.lnum >= vol->usable_leb_size)
473+
break;
474+
if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
475+
req.dtype != UBI_UNKNOWN)
476+
break;
477+
478+
err = get_exclusive(desc);
479+
if (err < 0)
480+
break;
433481

434-
file->f_pos = 0;
482+
err = ubi_start_leb_change(ubi, vol, &req);
483+
if (req.bytes == 0)
484+
revoke_exclusive(desc, UBI_READWRITE);
435485
break;
436486
}
437487

@@ -447,7 +497,8 @@ static int vol_cdev_ioctl(struct inode *inode, struct file *file,
447497
break;
448498
}
449499

450-
if (desc->mode == UBI_READONLY) {
500+
if (desc->mode == UBI_READONLY ||
501+
vol->vol_type == UBI_STATIC_VOLUME) {
451502
err = -EROFS;
452503
break;
453504
}
@@ -457,11 +508,6 @@ static int vol_cdev_ioctl(struct inode *inode, struct file *file,
457508
break;
458509
}
459510

460-
if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
461-
err = -EROFS;
462-
break;
463-
}
464-
465511
dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
466512
err = ubi_eba_unmap_leb(ubi, vol, lnum);
467513
if (err)

0 commit comments

Comments
 (0)