SPDX: Convert all of our single license tags to Linux Kernel style
[oweals/u-boot.git] / drivers / mtd / ubi / upd.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  * Copyright (c) Nokia Corporation, 2006
5  *
6  * Author: Artem Bityutskiy (Битюцкий Артём)
7  *
8  * Jan 2007: Alexander Schmidt, hacked per-volume update.
9  */
10
11 /*
12  * This file contains implementation of the volume update and atomic LEB change
13  * functionality.
14  *
15  * The update operation is based on the per-volume update marker which is
16  * stored in the volume table. The update marker is set before the update
17  * starts, and removed after the update has been finished. So if the update was
18  * interrupted by an unclean re-boot or due to some other reasons, the update
19  * marker stays on the flash media and UBI finds it when it attaches the MTD
20  * device next time. If the update marker is set for a volume, the volume is
21  * treated as damaged and most I/O operations are prohibited. Only a new update
22  * operation is allowed.
23  *
24  * Note, in general it is possible to implement the update operation as a
25  * transaction with a roll-back capability.
26  */
27
28 #ifndef __UBOOT__
29 #include <linux/uaccess.h>
30 #else
31 #include <div64.h>
32 #include <ubi_uboot.h>
33 #endif
34 #include <linux/err.h>
35 #include <linux/math64.h>
36
37 #include "ubi.h"
38
39 /**
40  * set_update_marker - set update marker.
41  * @ubi: UBI device description object
42  * @vol: volume description object
43  *
44  * This function sets the update marker flag for volume @vol. Returns zero
45  * in case of success and a negative error code in case of failure.
46  */
47 static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
48 {
49         int err;
50         struct ubi_vtbl_record vtbl_rec;
51
52         dbg_gen("set update marker for volume %d", vol->vol_id);
53
54         if (vol->upd_marker) {
55                 ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
56                 dbg_gen("already set");
57                 return 0;
58         }
59
60         vtbl_rec = ubi->vtbl[vol->vol_id];
61         vtbl_rec.upd_marker = 1;
62
63         mutex_lock(&ubi->device_mutex);
64         err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
65         vol->upd_marker = 1;
66         mutex_unlock(&ubi->device_mutex);
67         return err;
68 }
69
70 /**
71  * clear_update_marker - clear update marker.
72  * @ubi: UBI device description object
73  * @vol: volume description object
74  * @bytes: new data size in bytes
75  *
76  * This function clears the update marker for volume @vol, sets new volume
77  * data size and clears the "corrupted" flag (static volumes only). Returns
78  * zero in case of success and a negative error code in case of failure.
79  */
80 static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
81                                long long bytes)
82 {
83         int err;
84         struct ubi_vtbl_record vtbl_rec;
85
86         dbg_gen("clear update marker for volume %d", vol->vol_id);
87
88         vtbl_rec = ubi->vtbl[vol->vol_id];
89         ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
90         vtbl_rec.upd_marker = 0;
91
92         if (vol->vol_type == UBI_STATIC_VOLUME) {
93                 vol->corrupted = 0;
94                 vol->used_bytes = bytes;
95                 vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
96                                             &vol->last_eb_bytes);
97                 if (vol->last_eb_bytes)
98                         vol->used_ebs += 1;
99                 else
100                         vol->last_eb_bytes = vol->usable_leb_size;
101         }
102
103         mutex_lock(&ubi->device_mutex);
104         err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
105         vol->upd_marker = 0;
106         mutex_unlock(&ubi->device_mutex);
107         return err;
108 }
109
110 /**
111  * ubi_start_update - start volume update.
112  * @ubi: UBI device description object
113  * @vol: volume description object
114  * @bytes: update bytes
115  *
116  * This function starts volume update operation. If @bytes is zero, the volume
117  * is just wiped out. Returns zero in case of success and a negative error code
118  * in case of failure.
119  */
120 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
121                      long long bytes)
122 {
123         int i, err;
124
125         dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
126         ubi_assert(!vol->updating && !vol->changing_leb);
127         vol->updating = 1;
128
129         vol->upd_buf = vmalloc(ubi->leb_size);
130         if (!vol->upd_buf)
131                 return -ENOMEM;
132
133         err = set_update_marker(ubi, vol);
134         if (err)
135                 return err;
136
137         /* Before updating - wipe out the volume */
138         for (i = 0; i < vol->reserved_pebs; i++) {
139                 err = ubi_eba_unmap_leb(ubi, vol, i);
140                 if (err)
141                         return err;
142         }
143
144         if (bytes == 0) {
145                 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
146                 if (err)
147                         return err;
148
149                 err = clear_update_marker(ubi, vol, 0);
150                 if (err)
151                         return err;
152
153                 vfree(vol->upd_buf);
154                 vol->updating = 0;
155                 return 0;
156         }
157
158         vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
159                                vol->usable_leb_size);
160         vol->upd_bytes = bytes;
161         vol->upd_received = 0;
162         return 0;
163 }
164
165 /**
166  * ubi_start_leb_change - start atomic LEB change.
167  * @ubi: UBI device description object
168  * @vol: volume description object
169  * @req: operation request
170  *
171  * This function starts atomic LEB change operation. Returns zero in case of
172  * success and a negative error code in case of failure.
173  */
174 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
175                          const struct ubi_leb_change_req *req)
176 {
177         ubi_assert(!vol->updating && !vol->changing_leb);
178
179         dbg_gen("start changing LEB %d:%d, %u bytes",
180                 vol->vol_id, req->lnum, req->bytes);
181         if (req->bytes == 0)
182                 return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
183
184         vol->upd_bytes = req->bytes;
185         vol->upd_received = 0;
186         vol->changing_leb = 1;
187         vol->ch_lnum = req->lnum;
188
189         vol->upd_buf = vmalloc(req->bytes);
190         if (!vol->upd_buf)
191                 return -ENOMEM;
192
193         return 0;
194 }
195
196 /**
197  * write_leb - write update data.
198  * @ubi: UBI device description object
199  * @vol: volume description object
200  * @lnum: logical eraseblock number
201  * @buf: data to write
202  * @len: data size
203  * @used_ebs: how many logical eraseblocks will this volume contain (static
204  * volumes only)
205  *
206  * This function writes update data to corresponding logical eraseblock. In
207  * case of dynamic volume, this function checks if the data contains 0xFF bytes
208  * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
209  * buffer contains only 0xFF bytes, the LEB is left unmapped.
210  *
211  * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
212  * that we want to make sure that more data may be appended to the logical
213  * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
214  * this PEB won't be writable anymore. So if one writes the file-system image
215  * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
216  * space is writable after the update.
217  *
218  * We do not do this for static volumes because they are read-only. But this
219  * also cannot be done because we have to store per-LEB CRC and the correct
220  * data length.
221  *
222  * This function returns zero in case of success and a negative error code in
223  * case of failure.
224  */
225 static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
226                      void *buf, int len, int used_ebs)
227 {
228         int err;
229
230         if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
231                 int l = ALIGN(len, ubi->min_io_size);
232
233                 memset(buf + len, 0xFF, l - len);
234                 len = ubi_calc_data_len(ubi, buf, l);
235                 if (len == 0) {
236                         dbg_gen("all %d bytes contain 0xFF - skip", len);
237                         return 0;
238                 }
239
240                 err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
241         } else {
242                 /*
243                  * When writing static volume, and this is the last logical
244                  * eraseblock, the length (@len) does not have to be aligned to
245                  * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
246                  * function accepts exact (unaligned) length and stores it in
247                  * the VID header. And it takes care of proper alignment by
248                  * padding the buffer. Here we just make sure the padding will
249                  * contain zeros, not random trash.
250                  */
251                 memset(buf + len, 0, vol->usable_leb_size - len);
252                 err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
253         }
254
255         return err;
256 }
257
258 /**
259  * ubi_more_update_data - write more update data.
260  * @ubi: UBI device description object
261  * @vol: volume description object
262  * @buf: write data (user-space memory buffer)
263  * @count: how much bytes to write
264  *
265  * This function writes more data to the volume which is being updated. It may
266  * be called arbitrary number of times until all the update data arriveis. This
267  * function returns %0 in case of success, number of bytes written during the
268  * last call if the whole volume update has been successfully finished, and a
269  * negative error code in case of failure.
270  */
271 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
272                          const void __user *buf, int count)
273 {
274 #ifndef __UBOOT__
275         int lnum, offs, err = 0, len, to_write = count;
276 #else
277         int lnum, err = 0, len, to_write = count;
278         u32 offs;
279 #endif
280
281         dbg_gen("write %d of %lld bytes, %lld already passed",
282                 count, vol->upd_bytes, vol->upd_received);
283
284         if (ubi->ro_mode)
285                 return -EROFS;
286
287         lnum = div_u64_rem(vol->upd_received,  vol->usable_leb_size, &offs);
288         if (vol->upd_received + count > vol->upd_bytes)
289                 to_write = count = vol->upd_bytes - vol->upd_received;
290
291         /*
292          * When updating volumes, we accumulate whole logical eraseblock of
293          * data and write it at once.
294          */
295         if (offs != 0) {
296                 /*
297                  * This is a write to the middle of the logical eraseblock. We
298                  * copy the data to our update buffer and wait for more data or
299                  * flush it if the whole eraseblock is written or the update
300                  * is finished.
301                  */
302
303                 len = vol->usable_leb_size - offs;
304                 if (len > count)
305                         len = count;
306
307                 err = copy_from_user(vol->upd_buf + offs, buf, len);
308                 if (err)
309                         return -EFAULT;
310
311                 if (offs + len == vol->usable_leb_size ||
312                     vol->upd_received + len == vol->upd_bytes) {
313                         int flush_len = offs + len;
314
315                         /*
316                          * OK, we gathered either the whole eraseblock or this
317                          * is the last chunk, it's time to flush the buffer.
318                          */
319                         ubi_assert(flush_len <= vol->usable_leb_size);
320                         err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,
321                                         vol->upd_ebs);
322                         if (err)
323                                 return err;
324                 }
325
326                 vol->upd_received += len;
327                 count -= len;
328                 buf += len;
329                 lnum += 1;
330         }
331
332         /*
333          * If we've got more to write, let's continue. At this point we know we
334          * are starting from the beginning of an eraseblock.
335          */
336         while (count) {
337                 if (count > vol->usable_leb_size)
338                         len = vol->usable_leb_size;
339                 else
340                         len = count;
341
342                 err = copy_from_user(vol->upd_buf, buf, len);
343                 if (err)
344                         return -EFAULT;
345
346                 if (len == vol->usable_leb_size ||
347                     vol->upd_received + len == vol->upd_bytes) {
348                         err = write_leb(ubi, vol, lnum, vol->upd_buf,
349                                         len, vol->upd_ebs);
350                         if (err)
351                                 break;
352                 }
353
354                 vol->upd_received += len;
355                 count -= len;
356                 lnum += 1;
357                 buf += len;
358         }
359
360         ubi_assert(vol->upd_received <= vol->upd_bytes);
361         if (vol->upd_received == vol->upd_bytes) {
362                 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
363                 if (err)
364                         return err;
365                 /* The update is finished, clear the update marker */
366                 err = clear_update_marker(ubi, vol, vol->upd_bytes);
367                 if (err)
368                         return err;
369                 vol->updating = 0;
370                 err = to_write;
371                 vfree(vol->upd_buf);
372         }
373
374         return err;
375 }
376
377 /**
378  * ubi_more_leb_change_data - accept more data for atomic LEB change.
379  * @ubi: UBI device description object
380  * @vol: volume description object
381  * @buf: write data (user-space memory buffer)
382  * @count: how much bytes to write
383  *
384  * This function accepts more data to the volume which is being under the
385  * "atomic LEB change" operation. It may be called arbitrary number of times
386  * until all data arrives. This function returns %0 in case of success, number
387  * of bytes written during the last call if the whole "atomic LEB change"
388  * operation has been successfully finished, and a negative error code in case
389  * of failure.
390  */
391 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
392                              const void __user *buf, int count)
393 {
394         int err;
395
396         dbg_gen("write %d of %lld bytes, %lld already passed",
397                 count, vol->upd_bytes, vol->upd_received);
398
399         if (ubi->ro_mode)
400                 return -EROFS;
401
402         if (vol->upd_received + count > vol->upd_bytes)
403                 count = vol->upd_bytes - vol->upd_received;
404
405         err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);
406         if (err)
407                 return -EFAULT;
408
409         vol->upd_received += count;
410
411         if (vol->upd_received == vol->upd_bytes) {
412                 int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);
413
414                 memset(vol->upd_buf + vol->upd_bytes, 0xFF,
415                        len - vol->upd_bytes);
416                 len = ubi_calc_data_len(ubi, vol->upd_buf, len);
417                 err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
418                                                 vol->upd_buf, len);
419                 if (err)
420                         return err;
421         }
422
423         ubi_assert(vol->upd_received <= vol->upd_bytes);
424         if (vol->upd_received == vol->upd_bytes) {
425                 vol->changing_leb = 0;
426                 err = count;
427                 vfree(vol->upd_buf);
428         }
429
430         return err;
431 }