blob: 41680cdad13fd889e67759631fcbd73c2cecbdd7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park2d262c42008-11-19 16:26:54 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Park2d262c42008-11-19 16:26:54 +01005 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
7
8/* This file mostly implements UBI kernel API functions */
9
Heiko Schocherff94bc42014-06-24 10:10:04 +020010#ifndef __UBOOT__
Simon Glass61b29b82020-02-03 07:36:15 -070011#include <dm/devres.h>
Kyungmin Park2d262c42008-11-19 16:26:54 +010012#include <linux/module.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020013#include <linux/slab.h>
14#include <linux/namei.h>
15#include <linux/fs.h>
Kyungmin Park2d262c42008-11-19 16:26:54 +010016#include <asm/div64.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020017#else
Kyungmin Park2d262c42008-11-19 16:26:54 +010018#include <ubi_uboot.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020019#endif
20#include <linux/err.h>
21
Kyungmin Park2d262c42008-11-19 16:26:54 +010022#include "ubi.h"
23
24/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020025 * ubi_do_get_device_info - get information about UBI device.
26 * @ubi: UBI device description object
27 * @di: the information is stored here
28 *
29 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
30 * device is locked and cannot disappear.
31 */
32void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
33{
34 di->ubi_num = ubi->ubi_num;
35 di->leb_size = ubi->leb_size;
36 di->leb_start = ubi->leb_start;
37 di->min_io_size = ubi->min_io_size;
38 di->max_write_size = ubi->max_write_size;
39 di->ro_mode = ubi->ro_mode;
40#ifndef __UBOOT__
41 di->cdev = ubi->cdev.dev;
42#endif
43}
44EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
45
46/**
Kyungmin Park2d262c42008-11-19 16:26:54 +010047 * ubi_get_device_info - get information about UBI device.
48 * @ubi_num: UBI device number
49 * @di: the information is stored here
50 *
51 * This function returns %0 in case of success, %-EINVAL if the UBI device
52 * number is invalid, and %-ENODEV if there is no such UBI device.
53 */
54int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
55{
56 struct ubi_device *ubi;
57
58 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
59 return -EINVAL;
Kyungmin Park2d262c42008-11-19 16:26:54 +010060 ubi = ubi_get_device(ubi_num);
61 if (!ubi)
62 return -ENODEV;
Heiko Schocherff94bc42014-06-24 10:10:04 +020063 ubi_do_get_device_info(ubi, di);
Kyungmin Park2d262c42008-11-19 16:26:54 +010064 ubi_put_device(ubi);
65 return 0;
66}
67EXPORT_SYMBOL_GPL(ubi_get_device_info);
68
69/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020070 * ubi_do_get_volume_info - get information about UBI volume.
71 * @ubi: UBI device description object
72 * @vol: volume description object
Kyungmin Park2d262c42008-11-19 16:26:54 +010073 * @vi: the information is stored here
74 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020075void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
76 struct ubi_volume_info *vi)
Kyungmin Park2d262c42008-11-19 16:26:54 +010077{
Kyungmin Park2d262c42008-11-19 16:26:54 +010078 vi->vol_id = vol->vol_id;
79 vi->ubi_num = ubi->ubi_num;
80 vi->size = vol->reserved_pebs;
81 vi->used_bytes = vol->used_bytes;
82 vi->vol_type = vol->vol_type;
83 vi->corrupted = vol->corrupted;
84 vi->upd_marker = vol->upd_marker;
85 vi->alignment = vol->alignment;
86 vi->usable_leb_size = vol->usable_leb_size;
87 vi->name_len = vol->name_len;
88 vi->name = vol->name;
89 vi->cdev = vol->cdev.dev;
90}
Heiko Schocherff94bc42014-06-24 10:10:04 +020091
92/**
93 * ubi_get_volume_info - get information about UBI volume.
94 * @desc: volume descriptor
95 * @vi: the information is stored here
96 */
97void ubi_get_volume_info(struct ubi_volume_desc *desc,
98 struct ubi_volume_info *vi)
99{
100 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
101}
Kyungmin Park2d262c42008-11-19 16:26:54 +0100102EXPORT_SYMBOL_GPL(ubi_get_volume_info);
103
104/**
105 * ubi_open_volume - open UBI volume.
106 * @ubi_num: UBI device number
107 * @vol_id: volume ID
108 * @mode: open mode
109 *
110 * The @mode parameter specifies if the volume should be opened in read-only
111 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
112 * nobody else will be able to open this volume. UBI allows to have many volume
113 * readers and one writer at a time.
114 *
115 * If a static volume is being opened for the first time since boot, it will be
116 * checked by this function, which means it will be fully read and the CRC
117 * checksum of each logical eraseblock will be checked.
118 *
119 * This function returns volume descriptor in case of success and a negative
120 * error code in case of failure.
121 */
122struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
123{
124 int err;
125 struct ubi_volume_desc *desc;
126 struct ubi_device *ubi;
127 struct ubi_volume *vol;
128
Heiko Schocherff94bc42014-06-24 10:10:04 +0200129 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100130
131 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
132 return ERR_PTR(-EINVAL);
133
134 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200135 mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100136 return ERR_PTR(-EINVAL);
137
138 /*
139 * First of all, we have to get the UBI device to prevent its removal.
140 */
141 ubi = ubi_get_device(ubi_num);
142 if (!ubi)
143 return ERR_PTR(-ENODEV);
144
145 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
146 err = -EINVAL;
147 goto out_put_ubi;
148 }
149
150 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
151 if (!desc) {
152 err = -ENOMEM;
153 goto out_put_ubi;
154 }
155
156 err = -ENODEV;
157 if (!try_module_get(THIS_MODULE))
158 goto out_free;
159
160 spin_lock(&ubi->volumes_lock);
161 vol = ubi->volumes[vol_id];
162 if (!vol)
163 goto out_unlock;
164
165 err = -EBUSY;
166 switch (mode) {
167 case UBI_READONLY:
168 if (vol->exclusive)
169 goto out_unlock;
170 vol->readers += 1;
171 break;
172
173 case UBI_READWRITE:
174 if (vol->exclusive || vol->writers > 0)
175 goto out_unlock;
176 vol->writers += 1;
177 break;
178
179 case UBI_EXCLUSIVE:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200180 if (vol->exclusive || vol->writers || vol->readers ||
181 vol->metaonly)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100182 goto out_unlock;
183 vol->exclusive = 1;
184 break;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200185
186 case UBI_METAONLY:
187 if (vol->metaonly || vol->exclusive)
188 goto out_unlock;
189 vol->metaonly = 1;
190 break;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100191 }
192 get_device(&vol->dev);
193 vol->ref_count += 1;
194 spin_unlock(&ubi->volumes_lock);
195
196 desc->vol = vol;
197 desc->mode = mode;
198
199 mutex_lock(&ubi->ckvol_mutex);
Quentin Schulz386f20c2019-09-12 16:41:01 +0200200 if (!vol->checked && !vol->skip_check) {
Kyungmin Park2d262c42008-11-19 16:26:54 +0100201 /* This is the first open - check the volume */
202 err = ubi_check_volume(ubi, vol_id);
203 if (err < 0) {
204 mutex_unlock(&ubi->ckvol_mutex);
205 ubi_close_volume(desc);
206 return ERR_PTR(err);
207 }
208 if (err == 1) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200209 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
Kyungmin Park2d262c42008-11-19 16:26:54 +0100210 vol_id, ubi->ubi_num);
211 vol->corrupted = 1;
212 }
213 vol->checked = 1;
214 }
215 mutex_unlock(&ubi->ckvol_mutex);
216
217 return desc;
218
219out_unlock:
220 spin_unlock(&ubi->volumes_lock);
221 module_put(THIS_MODULE);
222out_free:
223 kfree(desc);
224out_put_ubi:
225 ubi_put_device(ubi);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200226 ubi_err(ubi, "cannot open device %d, volume %d, error %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200227 ubi_num, vol_id, err);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100228 return ERR_PTR(err);
229}
230EXPORT_SYMBOL_GPL(ubi_open_volume);
231
232/**
233 * ubi_open_volume_nm - open UBI volume by name.
234 * @ubi_num: UBI device number
235 * @name: volume name
236 * @mode: open mode
237 *
238 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
239 */
240struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
241 int mode)
242{
243 int i, vol_id = -1, len;
244 struct ubi_device *ubi;
245 struct ubi_volume_desc *ret;
246
Heiko Schocherff94bc42014-06-24 10:10:04 +0200247 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100248
249 if (!name)
250 return ERR_PTR(-EINVAL);
251
252 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
253 if (len > UBI_VOL_NAME_MAX)
254 return ERR_PTR(-EINVAL);
255
256 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
257 return ERR_PTR(-EINVAL);
258
259 ubi = ubi_get_device(ubi_num);
260 if (!ubi)
261 return ERR_PTR(-ENODEV);
262
263 spin_lock(&ubi->volumes_lock);
264 /* Walk all volumes of this UBI device */
265 for (i = 0; i < ubi->vtbl_slots; i++) {
266 struct ubi_volume *vol = ubi->volumes[i];
267
268 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
269 vol_id = i;
270 break;
271 }
272 }
273 spin_unlock(&ubi->volumes_lock);
274
275 if (vol_id >= 0)
276 ret = ubi_open_volume(ubi_num, vol_id, mode);
277 else
278 ret = ERR_PTR(-ENODEV);
279
280 /*
281 * We should put the UBI device even in case of success, because
282 * 'ubi_open_volume()' took a reference as well.
283 */
284 ubi_put_device(ubi);
285 return ret;
286}
287EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
288
Heiko Schocherff94bc42014-06-24 10:10:04 +0200289#ifndef __UBOOT__
290/**
291 * ubi_open_volume_path - open UBI volume by its character device node path.
292 * @pathname: volume character device node path
293 * @mode: open mode
294 *
295 * This function is similar to 'ubi_open_volume()', but opens a volume the path
296 * to its character device node.
297 */
298struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
299{
300 int error, ubi_num, vol_id, mod;
301 struct inode *inode;
302 struct path path;
303
304 dbg_gen("open volume %s, mode %d", pathname, mode);
305
306 if (!pathname || !*pathname)
307 return ERR_PTR(-EINVAL);
308
309 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
310 if (error)
311 return ERR_PTR(error);
312
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200313 inode = d_backing_inode(path.dentry);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200314 mod = inode->i_mode;
315 ubi_num = ubi_major2num(imajor(inode));
316 vol_id = iminor(inode) - 1;
317 path_put(&path);
318
319 if (!S_ISCHR(mod))
320 return ERR_PTR(-EINVAL);
321 if (vol_id >= 0 && ubi_num >= 0)
322 return ubi_open_volume(ubi_num, vol_id, mode);
323 return ERR_PTR(-ENODEV);
324}
325EXPORT_SYMBOL_GPL(ubi_open_volume_path);
326#endif
327
Kyungmin Park2d262c42008-11-19 16:26:54 +0100328/**
329 * ubi_close_volume - close UBI volume.
330 * @desc: volume descriptor
331 */
332void ubi_close_volume(struct ubi_volume_desc *desc)
333{
334 struct ubi_volume *vol = desc->vol;
335 struct ubi_device *ubi = vol->ubi;
336
Heiko Schocherff94bc42014-06-24 10:10:04 +0200337 dbg_gen("close device %d, volume %d, mode %d",
338 ubi->ubi_num, vol->vol_id, desc->mode);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100339
340 spin_lock(&ubi->volumes_lock);
341 switch (desc->mode) {
342 case UBI_READONLY:
343 vol->readers -= 1;
344 break;
345 case UBI_READWRITE:
346 vol->writers -= 1;
347 break;
348 case UBI_EXCLUSIVE:
349 vol->exclusive = 0;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200350 break;
351 case UBI_METAONLY:
352 vol->metaonly = 0;
353 break;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100354 }
355 vol->ref_count -= 1;
356 spin_unlock(&ubi->volumes_lock);
357
358 kfree(desc);
359 put_device(&vol->dev);
360 ubi_put_device(ubi);
361 module_put(THIS_MODULE);
362}
363EXPORT_SYMBOL_GPL(ubi_close_volume);
364
365/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200366 * leb_read_sanity_check - does sanity checks on read requests.
367 * @desc: volume descriptor
368 * @lnum: logical eraseblock number to read from
369 * @offset: offset within the logical eraseblock to read from
370 * @len: how many bytes to read
371 *
372 * This function is used by ubi_leb_read() and ubi_leb_read_sg()
373 * to perform sanity checks.
374 */
375static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
376 int offset, int len)
377{
378 struct ubi_volume *vol = desc->vol;
379 struct ubi_device *ubi = vol->ubi;
380 int vol_id = vol->vol_id;
381
382 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
383 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
384 offset + len > vol->usable_leb_size)
385 return -EINVAL;
386
387 if (vol->vol_type == UBI_STATIC_VOLUME) {
388 if (vol->used_ebs == 0)
389 /* Empty static UBI volume */
390 return 0;
391 if (lnum == vol->used_ebs - 1 &&
392 offset + len > vol->last_eb_bytes)
393 return -EINVAL;
394 }
395
396 if (vol->upd_marker)
397 return -EBADF;
398
399 return 0;
400}
401
402/**
Kyungmin Park2d262c42008-11-19 16:26:54 +0100403 * ubi_leb_read - read data.
404 * @desc: volume descriptor
405 * @lnum: logical eraseblock number to read from
406 * @buf: buffer where to store the read data
407 * @offset: offset within the logical eraseblock to read from
408 * @len: how many bytes to read
409 * @check: whether UBI has to check the read data's CRC or not.
410 *
411 * This function reads data from offset @offset of logical eraseblock @lnum and
412 * stores the data at @buf. When reading from static volumes, @check specifies
413 * whether the data has to be checked or not. If yes, the whole logical
414 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
415 * checksum is per-eraseblock). So checking may substantially slow down the
416 * read speed. The @check argument is ignored for dynamic volumes.
417 *
418 * In case of success, this function returns zero. In case of failure, this
419 * function returns a negative error code.
420 *
421 * %-EBADMSG error code is returned:
422 * o for both static and dynamic volumes if MTD driver has detected a data
423 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
424 * o for static volumes in case of data CRC mismatch.
425 *
426 * If the volume is damaged because of an interrupted update this function just
427 * returns immediately with %-EBADF error code.
428 */
429int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
430 int len, int check)
431{
432 struct ubi_volume *vol = desc->vol;
433 struct ubi_device *ubi = vol->ubi;
434 int err, vol_id = vol->vol_id;
435
Heiko Schocherff94bc42014-06-24 10:10:04 +0200436 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100437
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200438 err = leb_read_sanity_check(desc, lnum, offset, len);
439 if (err < 0)
440 return err;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100441
Kyungmin Park2d262c42008-11-19 16:26:54 +0100442 if (len == 0)
443 return 0;
444
445 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000446 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200447 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100448 vol->corrupted = 1;
449 }
450
451 return err;
452}
453EXPORT_SYMBOL_GPL(ubi_leb_read);
454
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200455#ifndef __UBOOT__
456/**
457 * ubi_leb_read_sg - read data into a scatter gather list.
458 * @desc: volume descriptor
459 * @lnum: logical eraseblock number to read from
460 * @buf: buffer where to store the read data
461 * @offset: offset within the logical eraseblock to read from
462 * @len: how many bytes to read
463 * @check: whether UBI has to check the read data's CRC or not.
464 *
465 * This function works exactly like ubi_leb_read_sg(). But instead of
466 * storing the read data into a buffer it writes to an UBI scatter gather
467 * list.
468 */
469int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
470 int offset, int len, int check)
471{
472 struct ubi_volume *vol = desc->vol;
473 struct ubi_device *ubi = vol->ubi;
474 int err, vol_id = vol->vol_id;
475
476 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
477
478 err = leb_read_sanity_check(desc, lnum, offset, len);
479 if (err < 0)
480 return err;
481
482 if (len == 0)
483 return 0;
484
485 err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
486 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
487 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
488 vol->corrupted = 1;
489 }
490
491 return err;
492}
493EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
494#endif
495
Kyungmin Park2d262c42008-11-19 16:26:54 +0100496/**
497 * ubi_leb_write - write data.
498 * @desc: volume descriptor
499 * @lnum: logical eraseblock number to write to
500 * @buf: data to write
501 * @offset: offset within the logical eraseblock where to write
502 * @len: how many bytes to write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100503 *
504 * This function writes @len bytes of data from @buf to offset @offset of
Heiko Schocherff94bc42014-06-24 10:10:04 +0200505 * logical eraseblock @lnum.
Kyungmin Park2d262c42008-11-19 16:26:54 +0100506 *
507 * This function takes care of physical eraseblock write failures. If write to
508 * the physical eraseblock write operation fails, the logical eraseblock is
509 * re-mapped to another physical eraseblock, the data is recovered, and the
510 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
511 *
512 * If all the data were successfully written, zero is returned. If an error
513 * occurred and UBI has not been able to recover from it, this function returns
514 * a negative error code. Note, in case of an error, it is possible that
515 * something was still written to the flash media, but that may be some
516 * garbage.
517 *
518 * If the volume is damaged because of an interrupted update this function just
519 * returns immediately with %-EBADF code.
520 */
521int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200522 int offset, int len)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100523{
524 struct ubi_volume *vol = desc->vol;
525 struct ubi_device *ubi = vol->ubi;
526 int vol_id = vol->vol_id;
527
Heiko Schocherff94bc42014-06-24 10:10:04 +0200528 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100529
530 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
531 return -EINVAL;
532
533 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
534 return -EROFS;
535
536 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
537 offset + len > vol->usable_leb_size ||
538 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
539 return -EINVAL;
540
Kyungmin Park2d262c42008-11-19 16:26:54 +0100541 if (vol->upd_marker)
542 return -EBADF;
543
544 if (len == 0)
545 return 0;
546
Heiko Schocherff94bc42014-06-24 10:10:04 +0200547 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100548}
549EXPORT_SYMBOL_GPL(ubi_leb_write);
550
551/*
552 * ubi_leb_change - change logical eraseblock atomically.
553 * @desc: volume descriptor
554 * @lnum: logical eraseblock number to change
555 * @buf: data to write
556 * @len: how many bytes to write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100557 *
558 * This function changes the contents of a logical eraseblock atomically. @buf
559 * has to contain new logical eraseblock data, and @len - the length of the
Heiko Schocherff94bc42014-06-24 10:10:04 +0200560 * data, which has to be aligned. The length may be shorter than the logical
Kyungmin Park2d262c42008-11-19 16:26:54 +0100561 * eraseblock size, ant the logical eraseblock may be appended to more times
562 * later on. This function guarantees that in case of an unclean reboot the old
563 * contents is preserved. Returns zero in case of success and a negative error
564 * code in case of failure.
565 */
566int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200567 int len)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100568{
569 struct ubi_volume *vol = desc->vol;
570 struct ubi_device *ubi = vol->ubi;
571 int vol_id = vol->vol_id;
572
Heiko Schocherff94bc42014-06-24 10:10:04 +0200573 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100574
575 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
576 return -EINVAL;
577
578 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
579 return -EROFS;
580
581 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
582 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
583 return -EINVAL;
584
Kyungmin Park2d262c42008-11-19 16:26:54 +0100585 if (vol->upd_marker)
586 return -EBADF;
587
588 if (len == 0)
589 return 0;
590
Heiko Schocherff94bc42014-06-24 10:10:04 +0200591 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100592}
593EXPORT_SYMBOL_GPL(ubi_leb_change);
594
595/**
596 * ubi_leb_erase - erase logical eraseblock.
597 * @desc: volume descriptor
598 * @lnum: logical eraseblock number
599 *
600 * This function un-maps logical eraseblock @lnum and synchronously erases the
601 * correspondent physical eraseblock. Returns zero in case of success and a
602 * negative error code in case of failure.
603 *
604 * If the volume is damaged because of an interrupted update this function just
605 * returns immediately with %-EBADF code.
606 */
607int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
608{
609 struct ubi_volume *vol = desc->vol;
610 struct ubi_device *ubi = vol->ubi;
611 int err;
612
Heiko Schocherff94bc42014-06-24 10:10:04 +0200613 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100614
615 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
616 return -EROFS;
617
618 if (lnum < 0 || lnum >= vol->reserved_pebs)
619 return -EINVAL;
620
621 if (vol->upd_marker)
622 return -EBADF;
623
624 err = ubi_eba_unmap_leb(ubi, vol, lnum);
625 if (err)
626 return err;
627
Heiko Schocherff94bc42014-06-24 10:10:04 +0200628 return ubi_wl_flush(ubi, vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100629}
630EXPORT_SYMBOL_GPL(ubi_leb_erase);
631
632/**
633 * ubi_leb_unmap - un-map logical eraseblock.
634 * @desc: volume descriptor
635 * @lnum: logical eraseblock number
636 *
637 * This function un-maps logical eraseblock @lnum and schedules the
638 * corresponding physical eraseblock for erasure, so that it will eventually be
Heiko Schocherff94bc42014-06-24 10:10:04 +0200639 * physically erased in background. This operation is much faster than the
Kyungmin Park2d262c42008-11-19 16:26:54 +0100640 * erase operation.
641 *
642 * Unlike erase, the un-map operation does not guarantee that the logical
643 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
644 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
645 * happens after this, the logical eraseblocks will not necessarily be
646 * un-mapped again when this MTD device is attached. They may actually be
647 * mapped to the same physical eraseblocks again. So, this function has to be
648 * used with care.
649 *
650 * In other words, when un-mapping a logical eraseblock, UBI does not store
651 * any information about this on the flash media, it just marks the logical
652 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
653 * eraseblock is physically erased, it will be mapped again to the same logical
654 * eraseblock when the MTD device is attached again.
655 *
656 * The main and obvious use-case of this function is when the contents of a
657 * logical eraseblock has to be re-written. Then it is much more efficient to
Heiko Schocherff94bc42014-06-24 10:10:04 +0200658 * first un-map it, then write new data, rather than first erase it, then write
Kyungmin Park2d262c42008-11-19 16:26:54 +0100659 * new data. Note, once new data has been written to the logical eraseblock,
660 * UBI guarantees that the old contents has gone forever. In other words, if an
661 * unclean reboot happens after the logical eraseblock has been un-mapped and
662 * then written to, it will contain the last written data.
663 *
664 * This function returns zero in case of success and a negative error code in
665 * case of failure. If the volume is damaged because of an interrupted update
666 * this function just returns immediately with %-EBADF code.
667 */
668int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
669{
670 struct ubi_volume *vol = desc->vol;
671 struct ubi_device *ubi = vol->ubi;
672
Heiko Schocherff94bc42014-06-24 10:10:04 +0200673 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100674
675 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
676 return -EROFS;
677
678 if (lnum < 0 || lnum >= vol->reserved_pebs)
679 return -EINVAL;
680
681 if (vol->upd_marker)
682 return -EBADF;
683
684 return ubi_eba_unmap_leb(ubi, vol, lnum);
685}
686EXPORT_SYMBOL_GPL(ubi_leb_unmap);
687
688/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200689 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
Kyungmin Park2d262c42008-11-19 16:26:54 +0100690 * @desc: volume descriptor
691 * @lnum: logical eraseblock number
Kyungmin Park2d262c42008-11-19 16:26:54 +0100692 *
693 * This function maps an un-mapped logical eraseblock @lnum to a physical
Heiko Schocherff94bc42014-06-24 10:10:04 +0200694 * eraseblock. This means, that after a successful invocation of this
Kyungmin Park2d262c42008-11-19 16:26:54 +0100695 * function the logical eraseblock @lnum will be empty (contain only %0xFF
696 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
697 * happens.
698 *
699 * This function returns zero in case of success, %-EBADF if the volume is
700 * damaged because of an interrupted update, %-EBADMSG if the logical
701 * eraseblock is already mapped, and other negative error codes in case of
702 * other failures.
703 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200704int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
Kyungmin Park2d262c42008-11-19 16:26:54 +0100705{
706 struct ubi_volume *vol = desc->vol;
707 struct ubi_device *ubi = vol->ubi;
708
Heiko Schocherff94bc42014-06-24 10:10:04 +0200709 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100710
711 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
712 return -EROFS;
713
714 if (lnum < 0 || lnum >= vol->reserved_pebs)
715 return -EINVAL;
716
Kyungmin Park2d262c42008-11-19 16:26:54 +0100717 if (vol->upd_marker)
718 return -EBADF;
719
720 if (vol->eba_tbl[lnum] >= 0)
721 return -EBADMSG;
722
Heiko Schocherff94bc42014-06-24 10:10:04 +0200723 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100724}
725EXPORT_SYMBOL_GPL(ubi_leb_map);
726
727/**
728 * ubi_is_mapped - check if logical eraseblock is mapped.
729 * @desc: volume descriptor
730 * @lnum: logical eraseblock number
731 *
732 * This function checks if logical eraseblock @lnum is mapped to a physical
733 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
734 * mean it will still be un-mapped after the UBI device is re-attached. The
735 * logical eraseblock may become mapped to the physical eraseblock it was last
736 * mapped to.
737 *
738 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
739 * error code in case of failure. If the volume is damaged because of an
740 * interrupted update this function just returns immediately with %-EBADF error
741 * code.
742 */
743int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
744{
745 struct ubi_volume *vol = desc->vol;
746
Heiko Schocherff94bc42014-06-24 10:10:04 +0200747 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
Kyungmin Park2d262c42008-11-19 16:26:54 +0100748
749 if (lnum < 0 || lnum >= vol->reserved_pebs)
750 return -EINVAL;
751
752 if (vol->upd_marker)
753 return -EBADF;
754
755 return vol->eba_tbl[lnum] >= 0;
756}
757EXPORT_SYMBOL_GPL(ubi_is_mapped);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200758
759/**
760 * ubi_sync - synchronize UBI device buffers.
761 * @ubi_num: UBI device to synchronize
762 *
763 * The underlying MTD device may cache data in hardware or in software. This
764 * function ensures the caches are flushed. Returns zero in case of success and
765 * a negative error code in case of failure.
766 */
767int ubi_sync(int ubi_num)
768{
769 struct ubi_device *ubi;
770
771 ubi = ubi_get_device(ubi_num);
772 if (!ubi)
773 return -ENODEV;
774
775 mtd_sync(ubi->mtd);
776 ubi_put_device(ubi);
777 return 0;
778}
779EXPORT_SYMBOL_GPL(ubi_sync);
780
781/**
782 * ubi_flush - flush UBI work queue.
783 * @ubi_num: UBI device to flush work queue
784 * @vol_id: volume id to flush for
785 * @lnum: logical eraseblock number to flush for
786 *
787 * This function executes all pending works for a particular volume id / logical
788 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
789 * a wildcard for all of the corresponding volume numbers or logical
790 * eraseblock numbers. It returns zero in case of success and a negative error
791 * code in case of failure.
792 */
793int ubi_flush(int ubi_num, int vol_id, int lnum)
794{
795 struct ubi_device *ubi;
796 int err = 0;
797
798 ubi = ubi_get_device(ubi_num);
799 if (!ubi)
800 return -ENODEV;
801
802 err = ubi_wl_flush(ubi, vol_id, lnum);
803 ubi_put_device(ubi);
804 return err;
805}
806EXPORT_SYMBOL_GPL(ubi_flush);
807
808#ifndef __UBOOT__
809BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
810
811/**
812 * ubi_register_volume_notifier - register a volume notifier.
813 * @nb: the notifier description object
814 * @ignore_existing: if non-zero, do not send "added" notification for all
815 * already existing volumes
816 *
817 * This function registers a volume notifier, which means that
818 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
819 * removed, re-sized, re-named, or updated. The first argument of the function
820 * is the notification type. The second argument is pointer to a
821 * &struct ubi_notification object which describes the notification event.
822 * Using UBI API from the volume notifier is prohibited.
823 *
824 * This function returns zero in case of success and a negative error code
825 * in case of failure.
826 */
827int ubi_register_volume_notifier(struct notifier_block *nb,
828 int ignore_existing)
829{
830 int err;
831
832 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
833 if (err != 0)
834 return err;
835 if (ignore_existing)
836 return 0;
837
838 /*
839 * We are going to walk all UBI devices and all volumes, and
840 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
841 * event. We have to lock the @ubi_devices_mutex to make sure UBI
842 * devices do not disappear.
843 */
844 mutex_lock(&ubi_devices_mutex);
845 ubi_enumerate_volumes(nb);
846 mutex_unlock(&ubi_devices_mutex);
847
848 return err;
849}
850EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
851
852/**
853 * ubi_unregister_volume_notifier - unregister the volume notifier.
854 * @nb: the notifier description object
855 *
856 * This function unregisters volume notifier @nm and returns zero in case of
857 * success and a negative error code in case of failure.
858 */
859int ubi_unregister_volume_notifier(struct notifier_block *nb)
860{
861 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
862}
863EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
864#endif