···6666/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */6767struct class *ubi_class;68686969-/* Slab cache for lock-tree entries */7070-struct kmem_cache *ubi_ltree_slab;7171-7269/* Slab cache for wear-leveling entries */7370struct kmem_cache *ubi_wl_entry_slab;7471···366369 int i, err;367370 dev_t dev;368371369369- mutex_init(&ubi->volumes_mutex);370370- spin_lock_init(&ubi->volumes_lock);371371-372372 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);373373374374 /*···562568 }563569564570 /* Similar for the data offset */565565- ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;571571+ ubi->leb_start = ubi->vid_hdr_offset + UBI_EC_HDR_SIZE;566572 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);567573568574 dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);···617623 * uninitialized and initialize it after scanning.618624 */619625626626+ return 0;627627+}628628+629629+/**630630+ * autoresize - re-size the volume which has the "auto-resize" flag set.631631+ * @ubi: UBI device description object632632+ * @vol_id: ID of the volume to re-size633633+ *634634+ * This function re-sizes the volume marked by the @UBI_VTBL_AUTORESIZE_FLG in635635+ * the volume table to the largest possible size. See comments in ubi-header.h636636+ * for more description of the flag. Returns zero in case of success and a637637+ * negative error code in case of failure.638638+ */639639+static int autoresize(struct ubi_device *ubi, int vol_id)640640+{641641+ struct ubi_volume_desc desc;642642+ struct ubi_volume *vol = ubi->volumes[vol_id];643643+ int err, old_reserved_pebs = vol->reserved_pebs;644644+645645+ /*646646+ * Clear the auto-resize flag in the volume in-memory copy of the647647+ * volume table, and 'ubi_resize_volume()' will propogate this change648648+ * to the flash.649649+ */650650+ ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;651651+652652+ if (ubi->avail_pebs == 0) {653653+ struct ubi_vtbl_record vtbl_rec;654654+655655+ /*656656+ * No avalilable PEBs to re-size the volume, clear the flag on657657+ * flash and exit.658658+ */659659+ memcpy(&vtbl_rec, &ubi->vtbl[vol_id],660660+ sizeof(struct ubi_vtbl_record));661661+ err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);662662+ if (err)663663+ ubi_err("cannot clean auto-resize flag for volume %d",664664+ vol_id);665665+ } else {666666+ desc.vol = vol;667667+ err = ubi_resize_volume(&desc,668668+ old_reserved_pebs + ubi->avail_pebs);669669+ if (err)670670+ ubi_err("cannot auto-resize volume %d", vol_id);671671+ }672672+673673+ if (err)674674+ return err;675675+676676+ ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,677677+ vol->name, old_reserved_pebs, vol->reserved_pebs);620678 return 0;621679}622680···748702 ubi->mtd = mtd;749703 ubi->ubi_num = ubi_num;750704 ubi->vid_hdr_offset = vid_hdr_offset;705705+ ubi->autoresize_vol_id = -1;706706+707707+ mutex_init(&ubi->buf_mutex);708708+ mutex_init(&ubi->ckvol_mutex);709709+ mutex_init(&ubi->volumes_mutex);710710+ spin_lock_init(&ubi->volumes_lock);751711752712 dbg_msg("attaching mtd%d to ubi%d: VID header offset %d",753713 mtd->index, ubi_num, vid_hdr_offset);···762710 if (err)763711 goto out_free;764712765765- mutex_init(&ubi->buf_mutex);766766- mutex_init(&ubi->ckvol_mutex);767713 ubi->peb_buf1 = vmalloc(ubi->peb_size);768714 if (!ubi->peb_buf1)769715 goto out_free;···781731 if (err) {782732 dbg_err("failed to attach by scanning, error %d", err);783733 goto out_free;734734+ }735735+736736+ if (ubi->autoresize_vol_id != -1) {737737+ err = autoresize(ubi, ubi->autoresize_vol_id);738738+ if (err)739739+ goto out_detach;784740 }785741786742 err = uif_init(ubi);···914858}915859916860/**917917- * ltree_entry_ctor - lock tree entries slab cache constructor.918918- * @obj: the lock-tree entry to construct919919- * @cache: the lock tree entry slab cache920920- * @flags: constructor flags921921- */922922-static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)923923-{924924- struct ubi_ltree_entry *le = obj;925925-926926- le->users = 0;927927- init_rwsem(&le->mutex);928928-}929929-930930-/**931861 * find_mtd_device - open an MTD device by its name or number.932862 * @mtd_dev: name or number of the device933863 *···975933 goto out_version;976934 }977935978978- ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab",979979- sizeof(struct ubi_ltree_entry), 0,980980- 0, <ree_entry_ctor);981981- if (!ubi_ltree_slab)982982- goto out_dev_unreg;983983-984936 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",985937 sizeof(struct ubi_wl_entry),986938 0, 0, NULL);987939 if (!ubi_wl_entry_slab)988988- goto out_ltree;940940+ goto out_dev_unreg;989941990942 /* Attach MTD devices */991943 for (i = 0; i < mtd_devs; i++) {···1016980 mutex_unlock(&ubi_devices_mutex);1017981 }1018982 kmem_cache_destroy(ubi_wl_entry_slab);10191019-out_ltree:10201020- kmem_cache_destroy(ubi_ltree_slab);1021983out_dev_unreg:1022984 misc_deregister(&ubi_ctrl_cdev);1023985out_version:···10391005 mutex_unlock(&ubi_devices_mutex);10401006 }10411007 kmem_cache_destroy(ubi_wl_entry_slab);10421042- kmem_cache_destroy(ubi_ltree_slab);10431008 misc_deregister(&ubi_ctrl_cdev);10441009 class_remove_file(ubi_class, &ubi_version);10451010 class_destroy(ubi_class);···10991066 struct mtd_dev_param *p;11001067 char buf[MTD_PARAM_LEN_MAX];11011068 char *pbuf = &buf[0];11021102- char *tokens[3] = {NULL, NULL, NULL};10691069+ char *tokens[2] = {NULL, NULL};1103107011041071 if (!val)11051072 return -EINVAL;···11291096 if (buf[len - 1] == '\n')11301097 buf[len - 1] = '\0';1131109811321132- for (i = 0; i < 3; i++)10991099+ for (i = 0; i < 2; i++)11331100 tokens[i] = strsep(&pbuf, ",");1134110111351102 if (pbuf) {
+64-18
drivers/mtd/ubi/cdev.c
···132132 if (vol->updating) {133133 ubi_warn("update of volume %d not finished, volume is damaged",134134 vol->vol_id);135135+ ubi_assert(!vol->changing_leb);135136 vol->updating = 0;137137+ vfree(vol->upd_buf);138138+ } else if (vol->changing_leb) {139139+ dbg_msg("only %lld of %lld bytes received for atomic LEB change"140140+ " for volume %d:%d, cancel", vol->upd_received,141141+ vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);142142+ vol->changing_leb = 0;136143 vfree(vol->upd_buf);137144 }138145···191184 struct ubi_volume_desc *desc = file->private_data;192185 struct ubi_volume *vol = desc->vol;193186 struct ubi_device *ubi = vol->ubi;194194- int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size;187187+ int err, lnum, off, len, tbuf_size;195188 size_t count_save = count;196189 void *tbuf;197190 uint64_t tmp;198191199192 dbg_msg("read %zd bytes from offset %lld of volume %d",200200- count, *offp, vol_id);193193+ count, *offp, vol->vol_id);201194202195 if (vol->updating) {203196 dbg_err("updating");···211204 return 0;212205213206 if (vol->corrupted)214214- dbg_msg("read from corrupted volume %d", vol_id);207207+ dbg_msg("read from corrupted volume %d", vol->vol_id);215208216209 if (*offp + count > vol->used_bytes)217210 count_save = count = vol->used_bytes - *offp;···281274 uint64_t tmp;282275283276 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",284284- count, *offp, desc->vol->vol_id);277277+ count, *offp, vol->vol_id);285278286279 if (vol->vol_type == UBI_STATIC_VOLUME)287280 return -EROFS;···358351 struct ubi_volume *vol = desc->vol;359352 struct ubi_device *ubi = vol->ubi;360353361361- if (!vol->updating)354354+ if (!vol->updating && !vol->changing_leb)362355 return vol_cdev_direct_write(file, buf, count, offp);363356364364- err = ubi_more_update_data(ubi, vol->vol_id, buf, count);357357+ if (vol->updating)358358+ err = ubi_more_update_data(ubi, vol, buf, count);359359+ else360360+ err = ubi_more_leb_change_data(ubi, vol, buf, count);361361+365362 if (err < 0) {366366- ubi_err("cannot write %zd bytes of update data, error %d",363363+ ubi_err("cannot accept more %zd bytes of data, error %d",367364 count, err);368365 return err;369366 }370367371368 if (err) {372369 /*373373- * Update is finished, @err contains number of actually written374374- * bytes now.370370+ * The operation is finished, @err contains number of actually371371+ * written bytes.375372 */376373 count = err;374374+375375+ if (vol->changing_leb) {376376+ revoke_exclusive(desc, UBI_READWRITE);377377+ return count;378378+ }377379378380 err = ubi_check_volume(ubi, vol->vol_id);379381 if (err < 0)···398382 revoke_exclusive(desc, UBI_READWRITE);399383 }400384401401- *offp += count;402385 return count;403386}404387···442427 if (err < 0)443428 break;444429445445- err = ubi_start_update(ubi, vol->vol_id, bytes);430430+ err = ubi_start_update(ubi, vol, bytes);446431 if (bytes == 0)447432 revoke_exclusive(desc, UBI_READWRITE);433433+ break;434434+ }448435449449- file->f_pos = 0;436436+ /* Atomic logical eraseblock change command */437437+ case UBI_IOCEBCH:438438+ {439439+ struct ubi_leb_change_req req;440440+441441+ err = copy_from_user(&req, argp,442442+ sizeof(struct ubi_leb_change_req));443443+ if (err) {444444+ err = -EFAULT;445445+ break;446446+ }447447+448448+ if (desc->mode == UBI_READONLY ||449449+ vol->vol_type == UBI_STATIC_VOLUME) {450450+ err = -EROFS;451451+ break;452452+ }453453+454454+ /* Validate the request */455455+ err = -EINVAL;456456+ if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||457457+ req.bytes < 0 || req.lnum >= vol->usable_leb_size)458458+ break;459459+ if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&460460+ req.dtype != UBI_UNKNOWN)461461+ break;462462+463463+ err = get_exclusive(desc);464464+ if (err < 0)465465+ break;466466+467467+ err = ubi_start_leb_change(ubi, vol, &req);468468+ if (req.bytes == 0)469469+ revoke_exclusive(desc, UBI_READWRITE);450470 break;451471 }452472···497447 break;498448 }499449500500- if (desc->mode == UBI_READONLY) {450450+ if (desc->mode == UBI_READONLY ||451451+ vol->vol_type == UBI_STATIC_VOLUME) {501452 err = -EROFS;502453 break;503454 }504455505456 if (lnum < 0 || lnum >= vol->reserved_pebs) {506457 err = -EINVAL;507507- break;508508- }509509-510510- if (vol->vol_type != UBI_DYNAMIC_VOLUME) {511511- err = -EROFS;512458 break;513459 }514460
+19-21
drivers/mtd/ubi/eba.c
···7878 */7979static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)8080{8181- if (vol_id == UBI_LAYOUT_VOL_ID)8181+ if (vol_id == UBI_LAYOUT_VOLUME_ID)8282 return UBI_LAYOUT_VOLUME_COMPAT;8383 return 0;8484}···137137{138138 struct ubi_ltree_entry *le, *le1, *le_free;139139140140- le = kmem_cache_alloc(ubi_ltree_slab, GFP_NOFS);140140+ le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);141141 if (!le)142142 return ERR_PTR(-ENOMEM);143143144144+ le->users = 0;145145+ init_rwsem(&le->mutex);144146 le->vol_id = vol_id;145147 le->lnum = lnum;146148···190188 spin_unlock(&ubi->ltree_lock);191189192190 if (le_free)193193- kmem_cache_free(ubi_ltree_slab, le_free);191191+ kfree(le_free);194192195193 return le;196194}···238236239237 up_read(&le->mutex);240238 if (free)241241- kmem_cache_free(ubi_ltree_slab, le);239239+ kfree(le);242240}243241244242/**···294292 free = 0;295293 spin_unlock(&ubi->ltree_lock);296294 if (free)297297- kmem_cache_free(ubi_ltree_slab, le);295295+ kfree(le);298296299297 return 1;300298}···323321324322 up_write(&le->mutex);325323 if (free)326326- kmem_cache_free(ubi_ltree_slab, le);324324+ kfree(le);327325}328326329327/**···340338 int lnum)341339{342340 int err, pnum, vol_id = vol->vol_id;343343-344344- ubi_assert(ubi->ref_count > 0);345345- ubi_assert(vol->ref_count > 0);346341347342 if (ubi->ro_mode)348343 return -EROFS;···388389 int err, pnum, scrub = 0, vol_id = vol->vol_id;389390 struct ubi_vid_hdr *vid_hdr;390391 uint32_t uninitialized_var(crc);391391-392392- ubi_assert(ubi->ref_count > 0);393393- ubi_assert(vol->ref_count > 0);394392395393 err = leb_read_lock(ubi, vol_id, lnum);396394 if (err)···612616 int err, pnum, tries = 0, vol_id = vol->vol_id;613617 struct ubi_vid_hdr *vid_hdr;614618615615- ubi_assert(ubi->ref_count > 0);616616- ubi_assert(vol->ref_count > 0);617617-618619 if (ubi->ro_mode)619620 return -EROFS;620621···745752 struct ubi_vid_hdr *vid_hdr;746753 uint32_t crc;747754748748- ubi_assert(ubi->ref_count > 0);749749- ubi_assert(vol->ref_count > 0);750750-751755 if (ubi->ro_mode)752756 return -EROFS;753757···859869 struct ubi_vid_hdr *vid_hdr;860870 uint32_t crc;861871862862- ubi_assert(ubi->ref_count > 0);863863- ubi_assert(vol->ref_count > 0);864864-865872 if (ubi->ro_mode)866873 return -EROFS;874874+875875+ if (len == 0) {876876+ /*877877+ * Special case when data length is zero. In this case the LEB878878+ * has to be unmapped and mapped somewhere else.879879+ */880880+ err = ubi_eba_unmap_leb(ubi, vol, lnum);881881+ if (err)882882+ return err;883883+ return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);884884+ }867885868886 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);869887 if (!vid_hdr)
···286286 * FIXME: but this is anyway obsolete and will be removed at287287 * some point.288288 */289289-290289 dbg_bld("using old crappy leb_ver stuff");290290+291291+ if (v1 == v2) {292292+ ubi_err("PEB %d and PEB %d have the same version %lld",293293+ seb->pnum, pnum, v1);294294+ return -EINVAL;295295+ }291296292297 abs = v1 - v2;293298 if (abs < 0)···395390 vfree(buf);396391out_free_vidh:397392 ubi_free_vid_hdr(ubi, vh);398398- ubi_assert(err < 0);399393 return err;400394}401395···858854 }859855860856 vol_id = be32_to_cpu(vidh->vol_id);861861- if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) {857857+ if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {862858 int lnum = be32_to_cpu(vidh->lnum);863859864860 /* Unsupported internal volume */
+39-16
drivers/mtd/ubi/ubi.h
···144144 * @readers: number of users holding this volume in read-only mode145145 * @writers: number of users holding this volume in read-write mode146146 * @exclusive: whether somebody holds this volume in exclusive mode147147- * @checked: if this static volume was checked148147 *149148 * @reserved_pebs: how many physical eraseblocks are reserved for this volume150149 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)···151152 * @used_ebs: how many logical eraseblocks in this volume contain data152153 * @last_eb_bytes: how many bytes are stored in the last logical eraseblock153154 * @used_bytes: how many bytes of data this volume contains154154- * @upd_marker: non-zero if the update marker is set for this volume155155- * @corrupted: non-zero if the volume is corrupted (static volumes only)156155 * @alignment: volume alignment157156 * @data_pad: how many bytes are not used at the end of physical eraseblocks to158157 * satisfy the requested alignment159158 * @name_len: volume name length160159 * @name: volume name161160 *162162- * @updating: whether the volume is being updated163161 * @upd_ebs: how many eraseblocks are expected to be updated164164- * @upd_bytes: how many bytes are expected to be received165165- * @upd_received: how many update bytes were already received166166- * @upd_buf: update buffer which is used to collect update data162162+ * @ch_lnum: LEB number which is being changing by the atomic LEB change163163+ * operation164164+ * @ch_dtype: data persistency type which is being changing by the atomic LEB165165+ * change operation166166+ * @upd_bytes: how many bytes are expected to be received for volume update or167167+ * atomic LEB change168168+ * @upd_received: how many bytes were already received for volume update or169169+ * atomic LEB change170170+ * @upd_buf: update buffer which is used to collect update data or data for171171+ * atomic LEB change167172 *168173 * @eba_tbl: EBA table of this volume (LEB->PEB mapping)174174+ * @checked: %1 if this static volume was checked175175+ * @corrupted: %1 if the volume is corrupted (static volumes only)176176+ * @upd_marker: %1 if the update marker is set for this volume177177+ * @updating: %1 if the volume is being updated178178+ * @changing_leb: %1 if the atomic LEB change ioctl command is in progress169179 *170180 * @gluebi_desc: gluebi UBI volume descriptor171181 * @gluebi_refcount: reference count of the gluebi MTD device···197189 int readers;198190 int writers;199191 int exclusive;200200- int checked;201192202193 int reserved_pebs;203194 int vol_type;···204197 int used_ebs;205198 int last_eb_bytes;206199 long long used_bytes;207207- int upd_marker;208208- int corrupted;209200 int alignment;210201 int data_pad;211202 int name_len;212203 char name[UBI_VOL_NAME_MAX+1];213204214214- int updating;215205 int upd_ebs;206206+ int ch_lnum;207207+ int ch_dtype;216208 long long upd_bytes;217209 long long upd_received;218210 void *upd_buf;219211220212 int *eba_tbl;213213+ int checked:1;214214+ int corrupted:1;215215+ int upd_marker:1;216216+ int updating:1;217217+ int changing_leb:1;221218222219#ifdef CONFIG_MTD_UBI_GLUEBI223223- /* Gluebi-related stuff may be compiled out */220220+ /*221221+ * Gluebi-related stuff may be compiled out.222222+ * TODO: this should not be built into UBI but should be a separate223223+ * ubimtd driver which works on top of UBI and emulates MTD devices.224224+ */224225 struct ubi_volume_desc *gluebi_desc;225226 int gluebi_refcount;226227 struct mtd_info gluebi_mtd;···265250 * @rsvd_pebs: count of reserved physical eraseblocks266251 * @avail_pebs: count of available physical eraseblocks267252 * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB268268- * handling253253+ * handling269254 * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling270255 *256256+ * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end257257+ * of UBI ititializetion271258 * @vtbl_slots: how many slots are available in the volume table272259 * @vtbl_size: size of the volume table in bytes273260 * @vtbl: in-RAM volume table copy···350333 int beb_rsvd_pebs;351334 int beb_rsvd_level;352335336336+ int autoresize_vol_id;353337 int vtbl_slots;354338 int vtbl_size;355339 struct ubi_vtbl_record *vtbl;356340 struct mutex volumes_mutex;357341358342 int max_ec;343343+ /* TODO: mean_ec is not updated run-time, fix */359344 int mean_ec;360345361346 /* EBA unit's stuff */···418399#endif419400};420401421421-extern struct kmem_cache *ubi_ltree_slab;422402extern struct kmem_cache *ubi_wl_entry_slab;423403extern struct file_operations ubi_ctrl_cdev_operations;424404extern struct file_operations ubi_cdev_operations;···438420void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);439421440422/* upd.c */441441-int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes);442442-int ubi_more_update_data(struct ubi_device *ubi, int vol_id,423423+int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,424424+ long long bytes);425425+int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,443426 const void __user *buf, int count);427427+int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,428428+ const struct ubi_leb_change_req *req);429429+int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,430430+ const void __user *buf, int count);444431445432/* misc.c */446433int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, int length);
+131-45
drivers/mtd/ubi/upd.c
···2222 */23232424/*2525- * This file contains implementation of the volume update functionality.2525+ * This file contains implementation of the volume update and atomic LEB change2626+ * functionality.2627 *2728 * The update operation is based on the per-volume update marker which is2829 * stored in the volume table. The update marker is set before the update···4645/**4746 * set_update_marker - set update marker.4847 * @ubi: UBI device description object4949- * @vol_id: volume ID4848+ * @vol: volume description object5049 *5151- * This function sets the update marker flag for volume @vol_id. Returns zero5050+ * This function sets the update marker flag for volume @vol. Returns zero5251 * in case of success and a negative error code in case of failure.5352 */5454-static int set_update_marker(struct ubi_device *ubi, int vol_id)5353+static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)5554{5655 int err;5756 struct ubi_vtbl_record vtbl_rec;5858- struct ubi_volume *vol = ubi->volumes[vol_id];59576060- dbg_msg("set update marker for volume %d", vol_id);5858+ dbg_msg("set update marker for volume %d", vol->vol_id);61596260 if (vol->upd_marker) {6363- ubi_assert(ubi->vtbl[vol_id].upd_marker);6161+ ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);6462 dbg_msg("already set");6563 return 0;6664 }67656868- memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));6666+ memcpy(&vtbl_rec, &ubi->vtbl[vol->vol_id],6767+ sizeof(struct ubi_vtbl_record));6968 vtbl_rec.upd_marker = 1;70697170 mutex_lock(&ubi->volumes_mutex);7272- err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);7171+ err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);7372 mutex_unlock(&ubi->volumes_mutex);7473 vol->upd_marker = 1;7574 return err;···7877/**7978 * clear_update_marker - clear update marker.8079 * @ubi: UBI device description object8181- * @vol_id: volume ID8080+ * @vol: volume description object8281 * @bytes: new data size in bytes8382 *8484- * This function clears the update marker for volume @vol_id, sets new volume8383+ * This function clears the update marker for volume @vol, sets new volume8584 * data size and clears the "corrupted" flag (static volumes only). Returns8685 * zero in case of success and a negative error code in case of failure.8786 */8888-static int clear_update_marker(struct ubi_device *ubi, int vol_id, long long bytes)8787+static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,8888+ long long bytes)8989{9090 int err;9191 uint64_t tmp;9292 struct ubi_vtbl_record vtbl_rec;9393- struct ubi_volume *vol = ubi->volumes[vol_id];94939595- dbg_msg("clear update marker for volume %d", vol_id);9494+ dbg_msg("clear update marker for volume %d", vol->vol_id);96959797- memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));9696+ memcpy(&vtbl_rec, &ubi->vtbl[vol->vol_id],9797+ sizeof(struct ubi_vtbl_record));9898 ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);9999 vtbl_rec.upd_marker = 0;100100···111109 }112110113111 mutex_lock(&ubi->volumes_mutex);114114- err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);112112+ err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);115113 mutex_unlock(&ubi->volumes_mutex);116114 vol->upd_marker = 0;117115 return err;···120118/**121119 * ubi_start_update - start volume update.122120 * @ubi: UBI device description object123123- * @vol_id: volume ID121121+ * @vol: volume description object124122 * @bytes: update bytes125123 *126124 * This function starts volume update operation. If @bytes is zero, the volume127125 * is just wiped out. Returns zero in case of success and a negative error code128126 * in case of failure.129127 */130130-int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes)128128+int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,129129+ long long bytes)131130{132131 int i, err;133132 uint64_t tmp;134134- struct ubi_volume *vol = ubi->volumes[vol_id];135133136136- dbg_msg("start update of volume %d, %llu bytes", vol_id, bytes);134134+ dbg_msg("start update of volume %d, %llu bytes", vol->vol_id, bytes);135135+ ubi_assert(!vol->updating && !vol->changing_leb);137136 vol->updating = 1;138137139139- err = set_update_marker(ubi, vol_id);138138+ err = set_update_marker(ubi, vol);140139 if (err)141140 return err;142141···149146 }150147151148 if (bytes == 0) {152152- err = clear_update_marker(ubi, vol_id, 0);149149+ err = clear_update_marker(ubi, vol, 0);153150 if (err)154151 return err;155152 err = ubi_wl_flush(ubi);···170167}171168172169/**170170+ * ubi_start_leb_change - start atomic LEB change.171171+ * @ubi: UBI device description object172172+ * @vol: volume description object173173+ * @req: operation request174174+ *175175+ * This function starts atomic LEB change operation. Returns zero in case of176176+ * success and a negative error code in case of failure.177177+ */178178+int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,179179+ const struct ubi_leb_change_req *req)180180+{181181+ ubi_assert(!vol->updating && !vol->changing_leb);182182+183183+ dbg_msg("start changing LEB %d:%d, %u bytes",184184+ vol->vol_id, req->lnum, req->bytes);185185+ if (req->bytes == 0)186186+ return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0,187187+ req->dtype);188188+189189+ vol->upd_bytes = req->bytes;190190+ vol->upd_received = 0;191191+ vol->changing_leb = 1;192192+ vol->ch_lnum = req->lnum;193193+ vol->ch_dtype = req->dtype;194194+195195+ vol->upd_buf = vmalloc(req->bytes);196196+ if (!vol->upd_buf)197197+ return -ENOMEM;198198+199199+ return 0;200200+}201201+202202+/**173203 * write_leb - write update data.174204 * @ubi: UBI device description object175175- * @vol_id: volume ID205205+ * @vol: volume description object176206 * @lnum: logical eraseblock number177207 * @buf: data to write178208 * @len: data size···231195 * This function returns zero in case of success and a negative error code in232196 * case of failure.233197 */234234-static int write_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf,235235- int len, int used_ebs)198198+static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,199199+ void *buf, int len, int used_ebs)236200{237237- int err, l;238238- struct ubi_volume *vol = ubi->volumes[vol_id];201201+ int err;239202240203 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {241241- l = ALIGN(len, ubi->min_io_size);242242- memset(buf + len, 0xFF, l - len);204204+ len = ALIGN(len, ubi->min_io_size);205205+ memset(buf + len, 0xFF, len - len);243206244244- l = ubi_calc_data_len(ubi, buf, l);245245- if (l == 0) {207207+ len = ubi_calc_data_len(ubi, buf, len);208208+ if (len == 0) {246209 dbg_msg("all %d bytes contain 0xFF - skip", len);247210 return 0;248211 }249249- if (len != l)250250- dbg_msg("skip last %d bytes (0xFF)", len - l);251212252252- err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, l, UBI_UNKNOWN);213213+ err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len, UBI_UNKNOWN);253214 } else {254215 /*255216 * When writing static volume, and this is the last logical···272239 * @count: how much bytes to write273240 *274241 * This function writes more data to the volume which is being updated. It may275275- * be called arbitrary number of times until all of the update data arrive.276276- * This function returns %0 in case of success, number of bytes written during277277- * the last call if the whole volume update was successfully finished, and a242242+ * be called arbitrary number of times until all the update data arriveis. This243243+ * function returns %0 in case of success, number of bytes written during the244244+ * last call if the whole volume update has been successfully finished, and a278245 * negative error code in case of failure.279246 */280280-int ubi_more_update_data(struct ubi_device *ubi, int vol_id,247247+int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,281248 const void __user *buf, int count)282249{283250 uint64_t tmp;284284- struct ubi_volume *vol = ubi->volumes[vol_id];285251 int lnum, offs, err = 0, len, to_write = count;286252287253 dbg_msg("write %d of %lld bytes, %lld already passed",···325293 * is the last chunk, it's time to flush the buffer.326294 */327295 ubi_assert(flush_len <= vol->usable_leb_size);328328- err = write_leb(ubi, vol_id, lnum, vol->upd_buf,329329- flush_len, vol->upd_ebs);296296+ err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,297297+ vol->upd_ebs);330298 if (err)331299 return err;332300 }···353321354322 if (len == vol->usable_leb_size ||355323 vol->upd_received + len == vol->upd_bytes) {356356- err = write_leb(ubi, vol_id, lnum, vol->upd_buf, len,357357- vol->upd_ebs);324324+ err = write_leb(ubi, vol, lnum, vol->upd_buf,325325+ len, vol->upd_ebs);358326 if (err)359327 break;360328 }···368336 ubi_assert(vol->upd_received <= vol->upd_bytes);369337 if (vol->upd_received == vol->upd_bytes) {370338 /* The update is finished, clear the update marker */371371- err = clear_update_marker(ubi, vol_id, vol->upd_bytes);339339+ err = clear_update_marker(ubi, vol, vol->upd_bytes);372340 if (err)373341 return err;374342 err = ubi_wl_flush(ubi);375343 if (err == 0) {344344+ vol->updating = 0;376345 err = to_write;377346 vfree(vol->upd_buf);378378- vol->updating = 0;379347 }348348+ }349349+350350+ return err;351351+}352352+353353+/**354354+ * ubi_more_leb_change_data - accept more data for atomic LEB change.355355+ * @vol: volume description object356356+ * @buf: write data (user-space memory buffer)357357+ * @count: how much bytes to write358358+ *359359+ * This function accepts more data to the volume which is being under the360360+ * "atomic LEB change" operation. It may be called arbitrary number of times361361+ * until all data arrives. This function returns %0 in case of success, number362362+ * of bytes written during the last call if the whole "atomic LEB change"363363+ * operation has been successfully finished, and a negative error code in case364364+ * of failure.365365+ */366366+int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,367367+ const void __user *buf, int count)368368+{369369+ int err;370370+371371+ dbg_msg("write %d of %lld bytes, %lld already passed",372372+ count, vol->upd_bytes, vol->upd_received);373373+374374+ if (ubi->ro_mode)375375+ return -EROFS;376376+377377+ if (vol->upd_received + count > vol->upd_bytes)378378+ count = vol->upd_bytes - vol->upd_received;379379+380380+ err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);381381+ if (err)382382+ return -EFAULT;383383+384384+ vol->upd_received += count;385385+386386+ if (vol->upd_received == vol->upd_bytes) {387387+ int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);388388+389389+ memset(vol->upd_buf + vol->upd_bytes, 0xFF, len - vol->upd_bytes);390390+ len = ubi_calc_data_len(ubi, vol->upd_buf, len);391391+ err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,392392+ vol->upd_buf, len, UBI_UNKNOWN);393393+ if (err)394394+ return err;395395+ }396396+397397+ ubi_assert(vol->upd_received <= vol->upd_bytes);398398+ if (vol->upd_received == vol->upd_bytes) {399399+ vol->changing_leb = 0;400400+ err = count;401401+ vfree(vol->upd_buf);380402 }381403382404 return err;
+1-13
drivers/mtd/ubi/vmt.c
···497497498498 dbg_msg("re-size volume %d to from %d to %d PEBs",499499 vol_id, vol->reserved_pebs, reserved_pebs);500500- ubi_assert(desc->mode == UBI_EXCLUSIVE);501501- ubi_assert(vol == ubi->volumes[vol_id]);502500503501 if (vol->vol_type == UBI_STATIC_VOLUME &&504502 reserved_pebs < vol->used_ebs) {···523525 goto out_free;524526 }525527 spin_unlock(&ubi->volumes_lock);526526-527528528529 /* Reserve physical eraseblocks */529530 pebs = reserved_pebs - vol->reserved_pebs;···743746 goto fail;744747 }745748746746- if (vol->upd_marker != 0 && vol->upd_marker != 1) {747747- ubi_err("bad upd_marker");748748- goto fail;749749- }750750-751749 if (vol->upd_marker && vol->corrupted) {752750 dbg_err("update marker and corrupted simultaneously");753751 goto fail;···777785778786 n = (long long)vol->used_ebs * vol->usable_leb_size;779787 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {780780- if (vol->corrupted != 0) {788788+ if (vol->corrupted) {781789 ubi_err("corrupted dynamic volume");782790 goto fail;783791 }···794802 goto fail;795803 }796804 } else {797797- if (vol->corrupted != 0 && vol->corrupted != 1) {798798- ubi_err("bad corrupted");799799- goto fail;800800- }801805 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {802806 ubi_err("bad used_ebs");803807 goto fail;
+17-6
drivers/mtd/ubi/vtbl.c
···8989 struct ubi_volume *layout_vol;90909191 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);9292- layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOL_ID)];9292+ layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];93939494 if (!vtbl_rec)9595 vtbl_rec = &empty_vtbl_record;···111111 }112112113113 paranoid_vtbl_check(ubi);114114- return ubi_wl_flush(ubi);114114+ return 0;115115}116116117117/**···269269 * this volume table copy was found during scanning. It has to be wiped270270 * out.271271 */272272- sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);272272+ sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);273273 if (sv)274274 old_seb = ubi_scan_find_seb(sv, copy);275275···281281 }282282283283 vid_hdr->vol_type = UBI_VID_DYNAMIC;284284- vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOL_ID);284284+ vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);285285 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;286286 vid_hdr->data_size = vid_hdr->used_ebs =287287 vid_hdr->data_pad = cpu_to_be32(0);···514514 vol->name[vol->name_len] = '\0';515515 vol->vol_id = i;516516517517+ if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {518518+ /* Auto re-size flag may be set only for one volume */519519+ if (ubi->autoresize_vol_id != -1) {520520+ ubi_err("more then one auto-resize volume (%d "521521+ "and %d)", ubi->autoresize_vol_id, i);522522+ return -EINVAL;523523+ }524524+525525+ ubi->autoresize_vol_id = i;526526+ }527527+517528 ubi_assert(!ubi->volumes[i]);518529 ubi->volumes[i] = vol;519530 ubi->vol_count += 1;···590579 vol->last_eb_bytes = vol->reserved_pebs;591580 vol->used_bytes =592581 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);593593- vol->vol_id = UBI_LAYOUT_VOL_ID;582582+ vol->vol_id = UBI_LAYOUT_VOLUME_ID;594583 vol->ref_count = 1;595584596585 ubi_assert(!ubi->volumes[i]);···743732 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;744733 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);745734746746- sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);735735+ sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);747736 if (!sv) {748737 /*749738 * No logical eraseblocks belonging to the layout volume were
-1
drivers/mtd/ubi/wl.c
···13031303 * Make sure all the works which have been done in parallel are13041304 * finished.13051305 */13061306- ubi_assert(ubi->ref_count > 0);13071306 down_write(&ubi->work_sem);13081307 up_write(&ubi->work_sem);13091308
-17
include/linux/mtd/ubi.h
···2626#include <mtd/ubi-user.h>27272828/*2929- * UBI data type hint constants.3030- *3131- * UBI_LONGTERM: long-term data3232- * UBI_SHORTTERM: short-term data3333- * UBI_UNKNOWN: data persistence is unknown3434- *3535- * These constants are used when data is written to UBI volumes in order to3636- * help the UBI wear-leveling unit to find more appropriate physical3737- * eraseblocks.3838- */3939-enum {4040- UBI_LONGTERM = 1,4141- UBI_SHORTTERM,4242- UBI_UNKNOWN4343-};4444-4545-/*4629 * enum ubi_open_mode - UBI volume open mode constants.4730 *4831 * UBI_READONLY: read-only mode
+44-3
include/mtd/ubi-header.h
···5858};59596060/*6161+ * Volume flags used in the volume table record.6262+ *6363+ * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume6464+ *6565+ * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume6666+ * table. UBI automatically re-sizes the volume which has this flag and makes6767+ * the volume to be of largest possible size. This means that if after the6868+ * initialization UBI finds out that there are available physical eraseblocks6969+ * present on the device, it automatically appends all of them to the volume7070+ * (the physical eraseblocks reserved for bad eraseblocks handling and other7171+ * reserved physical eraseblocks are not taken). So, if there is a volume with7272+ * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical7373+ * eraseblocks will be zero after UBI is loaded, because all of them will be7474+ * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared7575+ * after the volume had been initialized.7676+ *7777+ * The auto-resize feature is useful for device production purposes. For7878+ * example, different NAND flash chips may have different amount of initial bad7979+ * eraseblocks, depending of particular chip instance. Manufacturers of NAND8080+ * chips usually guarantee that the amount of initial bad eraseblocks does not8181+ * exceed certain percent, e.g. 2%. When one creates an UBI image which will be8282+ * flashed to the end devices in production, he does not know the exact amount8383+ * of good physical eraseblocks the NAND chip on the device will have, but this8484+ * number is required to calculate the volume sized and put them to the volume8585+ * table of the UBI image. In this case, one of the volumes (e.g., the one8686+ * which will store the root file system) is marked as "auto-resizable", and8787+ * UBI will adjust its size on the first boot if needed.8888+ *8989+ * Note, first UBI reserves some amount of physical eraseblocks for bad9090+ * eraseblock handling, and then re-sizes the volume, not vice-versa. This9191+ * means that the pool of reserved physical eraseblocks will always be present.9292+ */9393+enum {9494+ UBI_VTBL_AUTORESIZE_FLG = 0x01,9595+};9696+9797+/*6198 * Compatibility constants used by internal volumes.6299 *63100 * @UBI_COMPAT_DELETE: delete this internal volume before anything is written···299262300263/* The layout volume contains the volume table */301264302302-#define UBI_LAYOUT_VOL_ID UBI_INTERNAL_VOL_START265265+#define UBI_LAYOUT_VOLUME_ID UBI_INTERNAL_VOL_START266266+#define UBI_LAYOUT_VOLUME_TYPE UBI_VID_DYNAMIC267267+#define UBI_LAYOUT_VOLUME_ALIGN 1303268#define UBI_LAYOUT_VOLUME_EBS 2304269#define UBI_LAYOUT_VOLUME_NAME "layout volume"305270#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT···328289 * @upd_marker: if volume update was started but not finished329290 * @name_len: volume name length330291 * @name: the volume name331331- * @padding2: reserved, zeroes292292+ * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)293293+ * @padding: reserved, zeroes332294 * @crc: a CRC32 checksum of the record333295 *334296 * The volume table records are stored in the volume table, which is stored in···364324 __u8 upd_marker;365325 __be16 name_len;366326 __u8 name[UBI_VOL_NAME_MAX+1];367367- __u8 padding2[24];327327+ __u8 flags;328328+ __u8 padding[23];368329 __be32 crc;369330} __attribute__ ((packed));370331
+47-4
include/mtd/ubi-user.h
···6363 *6464 * Volume update should be done via the %UBI_IOCVOLUP IOCTL command of the6565 * corresponding UBI volume character device. A pointer to a 64-bit update6666- * size should be passed to the IOCTL. After then, UBI expects user to write6666+ * size should be passed to the IOCTL. After this, UBI expects user to write6767 * this number of bytes to the volume character device. The update is finished6868 * when the claimed number of bytes is passed. So, the volume update sequence6969 * is something like:···7272 * ioctl(fd, UBI_IOCVOLUP, &image_size);7373 * write(fd, buf, image_size);7474 * close(fd);7575+ *7676+ * Atomic eraseblock change7777+ * ~~~~~~~~~~~~~~~~~~~~~~~~7878+ *7979+ * Atomic eraseblock change operation is done via the %UBI_IOCEBCH IOCTL8080+ * command of the corresponding UBI volume character device. A pointer to8181+ * &struct ubi_leb_change_req has to be passed to the IOCTL. Then the user is8282+ * expected to write the requested amount of bytes. This is similar to the8383+ * "volume update" IOCTL.7584 */76857786/*···122113#define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t)123114/* An eraseblock erasure command, used for debugging, disabled by default */124115#define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, int32_t)116116+/* An atomic eraseblock change command */117117+#define UBI_IOCEBCH _IOW(UBI_VOL_IOC_MAGIC, 2, int32_t)125118126119/* Maximum MTD device name length supported by UBI */127120#define MAX_UBI_MTD_NAME_LEN 127121121+122122+/*123123+ * UBI data type hint constants.124124+ *125125+ * UBI_LONGTERM: long-term data126126+ * UBI_SHORTTERM: short-term data127127+ * UBI_UNKNOWN: data persistence is unknown128128+ *129129+ * These constants are used when data is written to UBI volumes in order to130130+ * help the UBI wear-leveling unit to find more appropriate physical131131+ * eraseblocks.132132+ */133133+enum {134134+ UBI_LONGTERM = 1,135135+ UBI_SHORTTERM = 2,136136+ UBI_UNKNOWN = 3,137137+};128138129139/*130140 * UBI volume type constants.···153125 */154126enum {155127 UBI_DYNAMIC_VOLUME = 3,156156- UBI_STATIC_VOLUME = 4,128128+ UBI_STATIC_VOLUME = 4,157129};158130159131/**···165137 *166138 * This data structure is used to specify MTD device UBI has to attach and the167139 * parameters it has to use. The number which should be assigned to the new UBI168168- * device is passed in @ubi_num. UBI may automatically assing the number if140140+ * device is passed in @ubi_num. UBI may automatically assign the number if169141 * @UBI_DEV_NUM_AUTO is passed. In this case, the device number is returned in170142 * @ubi_num.171143 *···204176 * @padding2: reserved for future, not used, has to be zeroed205177 * @name: volume name206178 *207207- * This structure is used by userspace programs when creating new volumes. The179179+ * This structure is used by user-space programs when creating new volumes. The208180 * @used_bytes field is only necessary when creating static volumes.209181 *210182 * The @alignment field specifies the required alignment of the volume logical···248220struct ubi_rsvol_req {249221 int64_t bytes;250222 int32_t vol_id;223223+} __attribute__ ((packed));224224+225225+/**226226+ * struct ubi_leb_change_req - a data structure used in atomic logical227227+ * eraseblock change requests.228228+ * @lnum: logical eraseblock number to change229229+ * @bytes: how many bytes will be written to the logical eraseblock230230+ * @dtype: data type (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)231231+ * @padding: reserved for future, not used, has to be zeroed232232+ */233233+struct ubi_leb_change_req {234234+ int32_t lnum;235235+ int32_t bytes;236236+ uint8_t dtype;237237+ uint8_t padding[7];251238} __attribute__ ((packed));252239253240#endif /* __UBI_USER_H__ */