Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) International Business Machines Corp., 2006
0004  * Copyright (c) Nokia Corporation, 2006
0005  *
0006  * Author: Artem Bityutskiy (Битюцкий Артём)
0007  *
0008  * Jan 2007: Alexander Schmidt, hacked per-volume update.
0009  */
0010 
0011 /*
0012  * This file contains implementation of the volume update and atomic LEB change
0013  * functionality.
0014  *
0015  * The update operation is based on the per-volume update marker which is
0016  * stored in the volume table. The update marker is set before the update
0017  * starts, and removed after the update has been finished. So if the update was
0018  * interrupted by an unclean re-boot or due to some other reasons, the update
0019  * marker stays on the flash media and UBI finds it when it attaches the MTD
0020  * device next time. If the update marker is set for a volume, the volume is
0021  * treated as damaged and most I/O operations are prohibited. Only a new update
0022  * operation is allowed.
0023  *
0024  * Note, in general it is possible to implement the update operation as a
0025  * transaction with a roll-back capability.
0026  */
0027 
0028 #include <linux/err.h>
0029 #include <linux/uaccess.h>
0030 #include <linux/math64.h>
0031 #include "ubi.h"
0032 
0033 /**
0034  * set_update_marker - set update marker.
0035  * @ubi: UBI device description object
0036  * @vol: volume description object
0037  *
0038  * This function sets the update marker flag for volume @vol. Returns zero
0039  * in case of success and a negative error code in case of failure.
0040  */
0041 static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
0042 {
0043     int err;
0044     struct ubi_vtbl_record vtbl_rec;
0045 
0046     dbg_gen("set update marker for volume %d", vol->vol_id);
0047 
0048     if (vol->upd_marker) {
0049         ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
0050         dbg_gen("already set");
0051         return 0;
0052     }
0053 
0054     vtbl_rec = ubi->vtbl[vol->vol_id];
0055     vtbl_rec.upd_marker = 1;
0056 
0057     mutex_lock(&ubi->device_mutex);
0058     err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
0059     vol->upd_marker = 1;
0060     mutex_unlock(&ubi->device_mutex);
0061     return err;
0062 }
0063 
0064 /**
0065  * clear_update_marker - clear update marker.
0066  * @ubi: UBI device description object
0067  * @vol: volume description object
0068  * @bytes: new data size in bytes
0069  *
0070  * This function clears the update marker for volume @vol, sets new volume
0071  * data size and clears the "corrupted" flag (static volumes only). Returns
0072  * zero in case of success and a negative error code in case of failure.
0073  */
0074 static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
0075                    long long bytes)
0076 {
0077     int err;
0078     struct ubi_vtbl_record vtbl_rec;
0079 
0080     dbg_gen("clear update marker for volume %d", vol->vol_id);
0081 
0082     vtbl_rec = ubi->vtbl[vol->vol_id];
0083     ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
0084     vtbl_rec.upd_marker = 0;
0085 
0086     if (vol->vol_type == UBI_STATIC_VOLUME) {
0087         vol->corrupted = 0;
0088         vol->used_bytes = bytes;
0089         vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
0090                         &vol->last_eb_bytes);
0091         if (vol->last_eb_bytes)
0092             vol->used_ebs += 1;
0093         else
0094             vol->last_eb_bytes = vol->usable_leb_size;
0095     }
0096 
0097     mutex_lock(&ubi->device_mutex);
0098     err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
0099     vol->upd_marker = 0;
0100     mutex_unlock(&ubi->device_mutex);
0101     return err;
0102 }
0103 
0104 /**
0105  * ubi_start_update - start volume update.
0106  * @ubi: UBI device description object
0107  * @vol: volume description object
0108  * @bytes: update bytes
0109  *
0110  * This function starts volume update operation. If @bytes is zero, the volume
0111  * is just wiped out. Returns zero in case of success and a negative error code
0112  * in case of failure.
0113  */
0114 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
0115              long long bytes)
0116 {
0117     int i, err;
0118 
0119     dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
0120     ubi_assert(!vol->updating && !vol->changing_leb);
0121     vol->updating = 1;
0122 
0123     vol->upd_buf = vmalloc(ubi->leb_size);
0124     if (!vol->upd_buf)
0125         return -ENOMEM;
0126 
0127     err = set_update_marker(ubi, vol);
0128     if (err)
0129         return err;
0130 
0131     /* Before updating - wipe out the volume */
0132     for (i = 0; i < vol->reserved_pebs; i++) {
0133         err = ubi_eba_unmap_leb(ubi, vol, i);
0134         if (err)
0135             return err;
0136     }
0137 
0138     err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
0139     if (err)
0140         return err;
0141 
0142     if (bytes == 0) {
0143         err = clear_update_marker(ubi, vol, 0);
0144         if (err)
0145             return err;
0146 
0147         vfree(vol->upd_buf);
0148         vol->updating = 0;
0149         return 0;
0150     }
0151 
0152     vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
0153                    vol->usable_leb_size);
0154     vol->upd_bytes = bytes;
0155     vol->upd_received = 0;
0156     return 0;
0157 }
0158 
0159 /**
0160  * ubi_start_leb_change - start atomic LEB change.
0161  * @ubi: UBI device description object
0162  * @vol: volume description object
0163  * @req: operation request
0164  *
0165  * This function starts atomic LEB change operation. Returns zero in case of
0166  * success and a negative error code in case of failure.
0167  */
0168 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
0169              const struct ubi_leb_change_req *req)
0170 {
0171     ubi_assert(!vol->updating && !vol->changing_leb);
0172 
0173     dbg_gen("start changing LEB %d:%d, %u bytes",
0174         vol->vol_id, req->lnum, req->bytes);
0175     if (req->bytes == 0)
0176         return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
0177 
0178     vol->upd_bytes = req->bytes;
0179     vol->upd_received = 0;
0180     vol->changing_leb = 1;
0181     vol->ch_lnum = req->lnum;
0182 
0183     vol->upd_buf = vmalloc(ALIGN((int)req->bytes, ubi->min_io_size));
0184     if (!vol->upd_buf)
0185         return -ENOMEM;
0186 
0187     return 0;
0188 }
0189 
0190 /**
0191  * write_leb - write update data.
0192  * @ubi: UBI device description object
0193  * @vol: volume description object
0194  * @lnum: logical eraseblock number
0195  * @buf: data to write
0196  * @len: data size
0197  * @used_ebs: how many logical eraseblocks will this volume contain (static
0198  * volumes only)
0199  *
0200  * This function writes update data to corresponding logical eraseblock. In
0201  * case of dynamic volume, this function checks if the data contains 0xFF bytes
0202  * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
0203  * buffer contains only 0xFF bytes, the LEB is left unmapped.
0204  *
0205  * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
0206  * that we want to make sure that more data may be appended to the logical
0207  * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
0208  * this PEB won't be writable anymore. So if one writes the file-system image
0209  * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
0210  * space is writable after the update.
0211  *
0212  * We do not do this for static volumes because they are read-only. But this
0213  * also cannot be done because we have to store per-LEB CRC and the correct
0214  * data length.
0215  *
0216  * This function returns zero in case of success and a negative error code in
0217  * case of failure.
0218  */
0219 static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
0220              void *buf, int len, int used_ebs)
0221 {
0222     int err;
0223 
0224     if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
0225         int l = ALIGN(len, ubi->min_io_size);
0226 
0227         memset(buf + len, 0xFF, l - len);
0228         len = ubi_calc_data_len(ubi, buf, l);
0229         if (len == 0) {
0230             dbg_gen("all %d bytes contain 0xFF - skip", len);
0231             return 0;
0232         }
0233 
0234         err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
0235     } else {
0236         /*
0237          * When writing static volume, and this is the last logical
0238          * eraseblock, the length (@len) does not have to be aligned to
0239          * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
0240          * function accepts exact (unaligned) length and stores it in
0241          * the VID header. And it takes care of proper alignment by
0242          * padding the buffer. Here we just make sure the padding will
0243          * contain zeros, not random trash.
0244          */
0245         memset(buf + len, 0, vol->usable_leb_size - len);
0246         err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
0247     }
0248 
0249     return err;
0250 }
0251 
0252 /**
0253  * ubi_more_update_data - write more update data.
0254  * @ubi: UBI device description object
0255  * @vol: volume description object
0256  * @buf: write data (user-space memory buffer)
0257  * @count: how much bytes to write
0258  *
0259  * This function writes more data to the volume which is being updated. It may
0260  * be called arbitrary number of times until all the update data arriveis. This
0261  * function returns %0 in case of success, number of bytes written during the
0262  * last call if the whole volume update has been successfully finished, and a
0263  * negative error code in case of failure.
0264  */
0265 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
0266              const void __user *buf, int count)
0267 {
0268     int lnum, offs, err = 0, len, to_write = count;
0269 
0270     dbg_gen("write %d of %lld bytes, %lld already passed",
0271         count, vol->upd_bytes, vol->upd_received);
0272 
0273     if (ubi->ro_mode)
0274         return -EROFS;
0275 
0276     lnum = div_u64_rem(vol->upd_received,  vol->usable_leb_size, &offs);
0277     if (vol->upd_received + count > vol->upd_bytes)
0278         to_write = count = vol->upd_bytes - vol->upd_received;
0279 
0280     /*
0281      * When updating volumes, we accumulate whole logical eraseblock of
0282      * data and write it at once.
0283      */
0284     if (offs != 0) {
0285         /*
0286          * This is a write to the middle of the logical eraseblock. We
0287          * copy the data to our update buffer and wait for more data or
0288          * flush it if the whole eraseblock is written or the update
0289          * is finished.
0290          */
0291 
0292         len = vol->usable_leb_size - offs;
0293         if (len > count)
0294             len = count;
0295 
0296         err = copy_from_user(vol->upd_buf + offs, buf, len);
0297         if (err)
0298             return -EFAULT;
0299 
0300         if (offs + len == vol->usable_leb_size ||
0301             vol->upd_received + len == vol->upd_bytes) {
0302             int flush_len = offs + len;
0303 
0304             /*
0305              * OK, we gathered either the whole eraseblock or this
0306              * is the last chunk, it's time to flush the buffer.
0307              */
0308             ubi_assert(flush_len <= vol->usable_leb_size);
0309             err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,
0310                     vol->upd_ebs);
0311             if (err)
0312                 return err;
0313         }
0314 
0315         vol->upd_received += len;
0316         count -= len;
0317         buf += len;
0318         lnum += 1;
0319     }
0320 
0321     /*
0322      * If we've got more to write, let's continue. At this point we know we
0323      * are starting from the beginning of an eraseblock.
0324      */
0325     while (count) {
0326         if (count > vol->usable_leb_size)
0327             len = vol->usable_leb_size;
0328         else
0329             len = count;
0330 
0331         err = copy_from_user(vol->upd_buf, buf, len);
0332         if (err)
0333             return -EFAULT;
0334 
0335         if (len == vol->usable_leb_size ||
0336             vol->upd_received + len == vol->upd_bytes) {
0337             err = write_leb(ubi, vol, lnum, vol->upd_buf,
0338                     len, vol->upd_ebs);
0339             if (err)
0340                 break;
0341         }
0342 
0343         vol->upd_received += len;
0344         count -= len;
0345         lnum += 1;
0346         buf += len;
0347     }
0348 
0349     ubi_assert(vol->upd_received <= vol->upd_bytes);
0350     if (vol->upd_received == vol->upd_bytes) {
0351         err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
0352         if (err)
0353             return err;
0354         /* The update is finished, clear the update marker */
0355         err = clear_update_marker(ubi, vol, vol->upd_bytes);
0356         if (err)
0357             return err;
0358         vol->updating = 0;
0359         err = to_write;
0360         vfree(vol->upd_buf);
0361     }
0362 
0363     return err;
0364 }
0365 
0366 /**
0367  * ubi_more_leb_change_data - accept more data for atomic LEB change.
0368  * @ubi: UBI device description object
0369  * @vol: volume description object
0370  * @buf: write data (user-space memory buffer)
0371  * @count: how much bytes to write
0372  *
0373  * This function accepts more data to the volume which is being under the
0374  * "atomic LEB change" operation. It may be called arbitrary number of times
0375  * until all data arrives. This function returns %0 in case of success, number
0376  * of bytes written during the last call if the whole "atomic LEB change"
0377  * operation has been successfully finished, and a negative error code in case
0378  * of failure.
0379  */
0380 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
0381                  const void __user *buf, int count)
0382 {
0383     int err;
0384 
0385     dbg_gen("write %d of %lld bytes, %lld already passed",
0386         count, vol->upd_bytes, vol->upd_received);
0387 
0388     if (ubi->ro_mode)
0389         return -EROFS;
0390 
0391     if (vol->upd_received + count > vol->upd_bytes)
0392         count = vol->upd_bytes - vol->upd_received;
0393 
0394     err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);
0395     if (err)
0396         return -EFAULT;
0397 
0398     vol->upd_received += count;
0399 
0400     if (vol->upd_received == vol->upd_bytes) {
0401         int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);
0402 
0403         memset(vol->upd_buf + vol->upd_bytes, 0xFF,
0404                len - vol->upd_bytes);
0405         len = ubi_calc_data_len(ubi, vol->upd_buf, len);
0406         err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
0407                         vol->upd_buf, len);
0408         if (err)
0409             return err;
0410     }
0411 
0412     ubi_assert(vol->upd_received <= vol->upd_bytes);
0413     if (vol->upd_received == vol->upd_bytes) {
0414         vol->changing_leb = 0;
0415         err = count;
0416         vfree(vol->upd_buf);
0417     }
0418 
0419     return err;
0420 }